poj_2480 Longge's problem(素因子分解+积性函数+欧拉phi函数)

来源:互联网 发布:免费qq群排名优化 编辑:程序博客网 时间:2024/06/05 07:37
Longge's problem
Time Limit: 1000MS Memory Limit: 65536KTotal 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.

Input

Input contain several test case.
A number N per line.

Output

For each N, output ,∑gcd(i, N) 1<=i <=N, a line

Sample Input

26

Sample Output

315
#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 longusing 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;}


0 0
原创粉丝点击