poj1845 数论 快速幂

时间:2023-03-10 02:31:45
poj1845 数论 快速幂
Sumdiv
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16466   Accepted: 4101

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的所有因子的和之后再mod 9901的值。

(1+a1+a1^2+...a1^n1)*(1+a2+a2^2+...a2^n2)*(1+a3+a3^2+...a3^n2)*...(1+am+am^2+...am^nm) mod 9901。

对于每一个(1+a1+a1^2+...a1^n1) mod 9901

等于 (a1^(n1+1)-1)/(a1-1) mod 9901,这里用到逆元的知识:a/b mod c = (a mod (b*c))/ b

所以就等于(a1^(n1+1)-1)mod (9901*(a1-1)) / (a1-1)。

至于前面的a1^(n1+1),快速幂。

 #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#define mod 9901
#define N 10007
#define ll long long
using namespace std; int prime[]; void getPrime()
{
for(int i=;i<=;i++)
{
if(!prime[i])prime[++prime[]]=i;
for(int j=;j<=prime[]&&prime[j]*i<=;j++)
{
prime[prime[j]*i]=;
if(i%prime[j]==) break;
}
}
} long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt=;
long long tmp=x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
factor[fatCnt][]=;
if(tmp%prime[i]==)
{
factor[fatCnt][]=prime[i];
while(tmp%prime[i]==)
{
factor[fatCnt][]++;
tmp/=prime[i];
}
fatCnt++;
}
}
if(tmp!=)
{
factor[fatCnt][]=tmp;
factor[fatCnt++][]=;
}
return fatCnt;
}
long long pow_m(long long a,long long n)//快速模幂运算
{
ll ans=;a%=mod;
while(n)
{
if (n&) ans=(ans*a)%mod;
n>>=;
a=(a*a)%mod;
}
return ans;
}
long long sum(long long p,long long n)//计算1+p+p^2+````+p^n
{
if(p==)return ;
if(n==)return ;
if(n&) return ((+pow_m(p,n/+))%mod*sum(p,n/)%mod)%mod;
else return ((+pow_m(p,n/+))%mod*sum(p,n/-)+pow_m(p,n/)%mod)%mod; }
int main()
{
int A,B;
getPrime();
while(~scanf("%d%d",&A,&B))
{
getFactors(A);
long long ans=;
for(int i=;i<fatCnt;i++)
ans=(ans*sum(factor[i][],B*factor[i][])%mod)%mod;
printf("%lld\n",ans);
}
}