Sumdiv&&http://poj.org/problem?id=1845&&约数和问题

来源:互联网 发布:群排名优化教程 编辑:程序博客网 时间:2024/05/20 08:25
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 9761 Accepted: 2274

Description

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901).

Input

The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.

Output

The only line of the output will contain S modulo 9901.

Sample Input

2 3

Sample Output

15

Hint

2^3 = 8. 
The natural divisors of 8 are: 1,2,4,8. Their sum is 15. 
15 modulo 9901 is 15 (that should be output). 
这是一道数学性比较强的题,求的是给定数的所有约数和。
思路:首先找到该数的所有素因子及个数,然后求和,有两种方法:一乘法逆元,二,二分幂法,其中第一种方法有局限,只有当存在逆元时,才可以用。
AC代码:
#include<iostream>#include<string.h>#include<cstdio>#include<cmath>#define M 9901#define N 10000#define CLR(arr,val) memset(arr,val,sizeof(arr))using namespace std;typedef long long  L;int prim[N];int sum[N];int res;L pow( L p,L n){L res=1;while(n){if(1&n) res=(res*p)%M; p=(p%M*p%M)%M; n=n>>1;}return res;}L _pow(L  n,L  m){if(n==0) return 0;else if(n==1||m==0) return 1;else{if(m&1) return (_pow(n,m/2)%M*(1+pow(n,m/2+1))%M)%M;else   return( _pow(n,m/2-1)%M*(1+pow(n,m/2+1))%M)%M+pow(n,m/2)%M;}}int main(){int  a,b;scanf("%d%d",&a,&b);CLR(prim,0);CLR(sum,0);int count=0;for(int i=2;i*i<=a;++i){if(a%i==0){prim[++count]=i;while(a%i==0){sum[count]++;a/=i;}}}if(a!=1) prim[++count]=a,sum[count]=1;      L ans=1;for(int i=1;i<=count;++i) if(sum[i])  ans=ans*_pow(prim[i],sum[i]*b)%M; printf("%d\n",ans); return 0;}



原创粉丝点击