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).
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;}
- Sumdiv&&http://poj.org/problem?id=1845&&约数和问题
- POJ 1845 Sumdiv (数论,约数和)
- 棋盘问题&&http://poj.org/problem?id=1321
- http://poj.org/problem?id=3984 迷宫问题
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1125
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=2406
- http://poj.org/problem?id=1961
- http://poj.org/problem?id=1486
- http://poj.org/problem?id=1062
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=3159
- http://poj.org/problem?id=1201
- http://poj.org/problem?id=3259
- http://poj.org/problem?id=3233
- http://poj.org/problem?id=1273
- ant构建代码模板一
- C#代码与javaScript函数的相互调用
- 用C写的一个cgi的例子(使用lighttpd)【by joyride】
- IOS 如果要给navigationBar设置背景,必须放在所有修改之后设置,否则IOS4.3会失败
- android布局文件中的单位
- Sumdiv&&http://poj.org/problem?id=1845&&约数和问题
- ant构建代码模板二
- linux memory check commands
- C#连接access ,sqlserver,excel
- 定位到程序崩溃的地方,TOMBSTONE
- 转(C++计算几何函数库 )
- MVC3 项目总结 (转自cnblogs xling)
- nginx+fastcgi+c/c++搭建高性能Web框架
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)