poj1845Sumdiv+约数和定理
来源:互联网 发布:java nanotime 单位 编辑:程序博客网 时间:2024/05/21 22:30
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).
题意:A^B的所有约数和;
解法:
两个定理:
一。约数的个数=
二。约数和=
直接套公式就好。
但是,计算一下中间结果就算是除法取膜先膜((q-1)*mod),如果q是一个很大的质数,中间也可能溢出了。(wa5,6次)
所以要用递归二分来求
对于
当n为奇数时,可以化简为
当n为偶数时,可以化简为
#include<cstdio>#include<algorithm>#include<iostream>#include<map>#include<cstring>using namespace std;#define LL long long#define MOD 9901int prime[10005];bool visit[10005];int cnt;void getprime(){ cnt=0; memset(visit,false,sizeof(visit)); for(int i=2;i<=10005;i++){ if(visit[i]==false){ prime[cnt++]=i; for(int j=i*i;j<=10005;j+=i) visit[j]=true; } }}map<int,int>Q;map<int,int>::iterator it;LL quick_pow(LL a,LL n,LL mod){ LL ans=1; while(n){ if(n&1) ans=ans*a%mod; a=a*a%mod; n>>=1; } return ans;}LL sum(LL p,LL n){ //计算1+p+p^2+````+p^n if(p==0) return 0; if(n==0) return 1; if(n&1) return (1+quick_pow(p,n/2+1,MOD))*sum(p,n/2)%MOD; else return ((1+quick_pow(p,n/2+1,MOD))*sum(p,n/2-1)%MOD+quick_pow(p,n/2,MOD))%MOD;}int main(){ getprime(); LL A,B; while(cin>>A>>B){ Q.clear(); for(int i=0;i<cnt&&prime[i]*prime[i]<=A;i++){ if(A%prime[i]==0){ int cntp=0; while(A%prime[i]==0){ cntp++; A/=prime[i]; } Q[prime[i]]=cntp; } } if(A>1){ Q[A]=1; } //for(it=Q.begin();it!=Q.end();it++) cout<<it->first<<" "<<it->second<<endl; LL ans=1; for(it=Q.begin();it!=Q.end();it++){ LL temp=(LL)it->first;//p LL temp2=((LL)(it->second))*B;//n+1 ans=ans*(sum(temp,temp2)%MOD)%MOD; } cout<<ans<<endl; } return 0;}
- poj1845Sumdiv+约数和定理
- 约数个数定理 和 约数和定理
- 约数个数定理and约数和定理
- 约数和定理
- 数论 约数定理 约数和定理 欧拉定理
- 约数定理(约数个数定理,约束和定理)
- BZOJ 3629 约数和定理+搜索
- HDU 1452 Happy 2004(约数和定理)
- hdu-1452(约数和定理+求逆元)
- 【原创】【数论】HDU-1452 Happy 2004(约数和定理)
- 哦,是约数定理
- 约数个数定理
- 约数个数定理
- 关于约数定理
- 约数个数定理
- HDU 1215 七夕节 【约数和定理&&唯一分解定理(模板)】
- 约数个数定理及实现
- hdu 1492 约数个数定理
- 继承的基础知识1——有关继承的基本概念
- 【HDU 5499】+ sort 结构体排序
- JVM 垃圾回收机制主要原理
- PowerDesigner导出表到word
- 有关makefile
- poj1845Sumdiv+约数和定理
- C Primer Plus学习 六 基本运算符
- iOS开发笔记之五十一——跳转到app store应用下载评分
- Spring in action--Part1-Core Spring
- 乱码之MyEclipse控制台
- 程序员笔试题收集汇总(二)
- android debug set screen rotation
- 谷歌的高级搜索
- 同一问题的递归与动态规划解法