POJ - 1845 G - Sumdiv (唯一分解定理)

时间:2023-03-09 20:02:51
POJ - 1845   G - Sumdiv  (唯一分解定理)
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  求A^B的所有因子之和
思路: 首先求一个数的所有因子之和,我们就可以使用我们的唯一分解定理
(1+p1^1+p1^2+p1^3...p1^n)*(1+p2^1.....p2^n)*...*(1+pn^1+....+pn^n)
然后每一个括号里面我们可以用一个等比数列公式来求得 
a1*(1-q^n)/(1-q)    
不过既然我们要用除法,那我们为了保证精确度肯定要求逆元,还有求q^n的时候,因为范围比较大,你就要使用快速幂求得
这也说了,这是一个数的所有因子之和,
题目所求得是   A^B的所有因子之和,所以你要想想,因为数据比较大所以我们不能直接求解,这样取模容易丢失精度
运用算术基本原理  4^6=(2^2) ^6=2^12
所以可以直接算出素因子时直接个数乘以幂数即可
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define mod 9901
#define MAX 1001
using namespace std;
typedef long long ll;
int cnt;
vector<ll> prime;
vector<ll> times;
ll qpow(ll a,ll b)//快速幂加快求幂数
{
ll ans=;
while(b)
{
if(b&) ans=(ans*a)%mod;
b>>=;
a=(a*a)%mod;
}
return ans;
}
void divide(ll n) {//求出n里所有的素数银子
for(ll i=;i*i<=n;++i) {
if(n%i==) {
prime.push_back(i);ll cnt=;
while(n%i==) {n/=i;++cnt;}
times.push_back(cnt);
}
}
if(n>) {prime.push_back(n);times.push_back();}
}
int main()
{
ll a,b;
scanf("%lld%lld",&a,&b);
divide(a);
ll ans=;
for(int i=,end=prime.size();i<end;++i) {
times[i]*=b;//乘了括号外的乘方
if((prime[i]-)%mod==) ans=ans*(times[i]+)%mod;//当底数为1时,就是乘以项数
else ans=ans*((qpow(prime[i],times[i]+)-+mod)*qpow(prime[i]-,mod-)%mod)%mod;//等比数列公式
}
printf("%lld\n",ans);
return ;
}