您的位置:首页 > 其它

poj 1845 数论

2017-04-01 20:31 295 查看
题意:

求A^B所有因子的和模9901的值

分析:

对于A我们可以分解为 


所以A^B可以分解为


那么所求的值为: 


等比数列求和以为涉及除法和模所以需要逆元

参考http://blog.csdn.net/acdreamers/article/details/8220787blog

ACcode:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
#define eps 1e-6
#define mod 9901
#define ll long long
int p[mod+100];
bool prime[mod+100];
int cnt;
void get_prime(){
cnt=0;
memset(prime,true,sizeof(prime));
for(int i=2;i<mod+100;++i)
if(prime[i]){
p[cnt++]=i;
for(int j=i+i;j<mod+100;j+=i)
prime[j]=false;
}
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll extend_gcd(ll a,ll b,ll &x, ll&y){
if(a==0&&b==0)return -1;
if(b==0){x=1;y=0;return a;}
ll d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll mod_reverse(ll a,ll n){
ll x,y;
ll d=extend_gcd(a,n,x,y);
if(d==1)return (x%n+n)%n;
else return -1;
}
ll q_mul(ll a,ll b, ll m){
ll ans=0;
while(b){
if(b&1){
b--;
ans=(ans+a)%m;
}
b>>=1;
a=(a+a)%m;
}
return ans;
}
ll pow(ll a,ll b,ll m){
ll ans=1;
while(b){
if(b&1)ans=q_mul(ans,a,m);
b>>=1;
a=q_mul(a,a,m);
}
return ans;
}
int main(){
ll a,b;
get_prime();
while(cin>>a>>b){
ll ans=1;
for(int i=0;p[i]*p[i]<=a;++i){
if(a%p[i]==0){
int num=0;
while(a%p[i]==0){
num++;
a/=p[i];
}
ll m=(p[i]-1)*mod;
ans*=(pow(p[i],num*b+1,m)+m-1)/(p[i]-1);
ans%=mod;
}
}
if(a>1){
ll m=mod*(a-1);
ans*=(pow(a,b+1,m)+m-1)/(a-1);
ans%=mod;
}
cout<<ans<<'\12';
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: