#35316: 95% 最後一個測資TLE

#### robinlin.su@gmail.com (Robin)

School : 國立中央大學附屬中壢高級中學
ID : 221849
2023-06-06 05:04:19
c184. 盈虧互補 -- | From: [123.0.250.229] | Post Date : 2023-05-25 13:35

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

long long sum_factors(int n)  // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}

void print_factors(int num)  // 印出所有真因數
{
long long sum = 0;
vector<int> factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}

void check(int n)   // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);

if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'<<n<<" has no friends."<<'\n';
}
else
{
print_factors(n);
print_factors(sum);
cout<<n<<" has no friends."<<'\n';
}
}
}

int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}

#35329: Re: 95% 最後一個測資TLE

#### cges30901 (cges30901)

School : No School
ID : 30877
2023-06-07 14:08:37
c184. 盈虧互補 -- | From: [118.160.184.73] | Post Date : 2023-05-26 19:02

#include
#include
#include

using namespace std;

long long sum_factors(int n)  // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}

void print_factors(int num)  // 印出所有真因數
{
long long sum = 0;
vector factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}

void check(int n)   // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);

if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'<        }
else
{
print_factors(n);
print_factors(sum);
cout<        }
}
}

int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}

#35336: Re: 95% 最後一個測資TLE

#### robinlin.su@gmail.com (Robin)

School : 國立中央大學附屬中壢高級中學
ID : 221849
2023-06-06 05:04:19
c184. 盈虧互補 -- | From: [123.0.250.229] | Post Date : 2023-05-27 06:06

#include
#include
#include

using namespace std;

long long sum_factors(int n)  // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}

void print_factors(int num)  // 印出所有真因數
{
long long sum = 0;
vector factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}

void check(int n)   // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);

if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'<        }
else
{
print_factors(n);
print_factors(sum);
cout<        }
}
}

int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}

ZeroJudge Forum