Longge's problem
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8297 | Accepted: 2765 |
Description
Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N.
"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.
"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.
Input
Input contain several test case.
A number N per line.
A number N per line.
Output
For each N, output ,∑gcd(i, N) 1<=i <=N, a line
Sample Input
2 6
Sample Output
3 15
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf_LL 4223372036854775807
#define inf 0x3f3f3f3f
#define maxn 10010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
int p[maxn], k[maxn], pn; //n = p1^k1+p2^k2+...
void get_factor(int n)
{
pn = 0;
int m = (int)sqrt(n+0.5);
for(int i = 2; i <= m; i++) if(n%i == 0)
{
p[pn] = i, k[pn] = 0;
while(n % i == 0){ k[pn]++; n /= i; }
pn++;
}
if(n != 1) { k[pn] = 1; p[pn++] = n; }
}
int main()
{
int n;
while(~scanf("%d", &n))
{
LL ans = n;
get_factor(n);
for(int i = 0; i < pn; i++)
{
ans = ans + ans*k[i]*(p[i]-1)/p[i];
}
printf("%lld\n", ans);
}
return 0;
}