Sumdiv(poj1845)(数学)

来源:互联网 发布:下软件赚钱的软件ios 编辑:程序博客网 时间:2024/06/06 00:18
/*
http://poj.org/problem?id=1845
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11846 Accepted: 2835
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). 
Source


Romania OI 2002
解析:求A^B得所有约数之和,即因子之和,并其结果模去9901
思路:
递归+分治
这里运用了整数划分定理和求约数和公式
1.整数唯一分解定理:任何正整数有且只有一种方式写出素因子的乘积表达式
即:A=(p1^k1)*(p2*k2)*(p3^k3)*.....(pn*kn);
(pi均为素数)
2.约分和公式:(从定理1以及排列组合原理可得理解)
s=(1+p1^1+....+p1^k1)*(1+p2^1+.....+p2^k2).....(1+pn^1+....+pn^kn);
3.利用递归分治法求s:令sum=1+p^1+p^2+...p^n则
(1)n%2==1:sum=(1+p+p^2+....p^(n/2))*(1+p^(n/2+1));
  (2)n%2==0:sum=(1+p+p^2+...p^(n/2-1))*(1+p^(n/2+1))+p^(n/2)
Problem Result Memory Time Language Length Submit Time
Accepted 1836 KB 47 ms C++ 1631 B 2013-07-24 16:56:20
*/
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include <iostream>using namespace std;const int maxn=100000+10;bool isprime[maxn];long long  prime[maxn],k[maxn],p[maxn];//long long cnt,t;int n,m;const int mod=9901;void makeprime()//构造素数表{memset(prime,0,sizeof(prime));memset(isprime,true,sizeof(isprime));t=0;for(int i=2;i<=maxn;i++){   if(isprime[i])     prime[t++]=i;for(int j=0;j<t&&i*prime[j]<=maxn;j++){isprime[i*prime[j]]=false;if(i%prime[j]==0) break;}}//for(int i=0;i<t;i++)//printf("%d\n",prime[i]);}void dividedata()//整数划分{memset(k,0,sizeof(k));int i,j;for(i=0;i<t&&prime[i]<=n;i++){if(n%prime[i]==0){p[++cnt]=prime[i];while(n%prime[i]==0) {k[cnt]++;n=n/prime[i]; }}}if(n!=1)//还需要对余下的数进行判断{p[++cnt]=n;k[cnt]=1;}}int pow_mod(long long a,long long  b){if(b==0) return 1; int x=pow_mod(a,b/2); long long ans=(long long )x*x%mod; if(b%2==1) ans=ans*a%mod; return (int)ans;}int sum_mod(long long  a,long long b){if(b==0) return 1; long long s1;long long s2; if(b%2==1) {s1=sum_mod(a,b/2);s2=pow_mod(a,b/2+1); return (s1+s1*s2)%mod; } else {     s1=sum_mod(a,b/2-1);     s2=pow_mod(a,b/2);     return (s1+s2+a*s1*s2)%mod; }}int main(){ //freopen("out.txt","w",stdout);makeprime();int i,j;scanf("%d%d",&n,&m);if(n==0){printf("0\n");return 0;}if(m==0||n==1){printf("1\n");return 0;}cnt=0;dividedata();int ans=1;for(i=1;i<=cnt;i++)  ans=ans*sum_mod(p[i],k[i]*m)%mod;  printf("%d\n",ans);return 0;}


原创粉丝点击