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;
}
求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;
}
相关文章推荐
- poj 1845 Sumdiv <数论>
- poj1845 Sumdiv(数论,因数和,等比数列,快速幂)
- poj 1845 数论(唯一分解定理+分治法求等比数列前n项的和mod m的值)
- 数论poj1845 因子和,等比数列求和
- POJ 1845(2013.9.15周赛A题数论或者二分)
- poj 1845 Sumdiv (很多数论知识 ,很经典 )
- POJ 1845 Sumdiv 简单数论问题
- POJ 1845Sumdiv(数论)
- poj 1845 Sumdiv (数论)
- 【数论】POJ_1845_Sumdiv
- poj 1845 Sumdiv(数论:欧拉函数+二分求等比数列前n项和+快速幂取模)
- poj 1845 Sumdiv (数论)
- poj——1845(数论之因子的和)
- poj1845--Sumdiv(数论篇3--真滴是数论啊。。。。)
- POJ 1845 sumdiv 数论 A^B 的所有约数之和
- POJ 1845 数论
- [POJ1845]Sumdiv(数论+矩乘)
- POJ 1845 Sumdiv(数论,求A^B的所有约数和)
- poj1845——Sumdiv(数论+幂的因子和)
- poj 1845 Sumdiv(数论+二分)