POJ 1845 Sumdiv【数论+递归+分治+快速幂】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37867156/article/details/81877997

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<=A,B<=5*10^7).

应用定理主要有三个:

(1)   整数的唯一分解定理:

      任意正整数都有且只有一种方式写出其素因子的乘积表达式。

      A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数

(2)   约数和公式:

对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)

有A的所有因子之和为

   S = (1+p1+p1^2+p1^3+...p1^k1) * (1+p2+p2^2+p2^3+...p2^k2) * (1+p3+ p3^3+…+ p3^k3) * ... * (1+pn+pn^2+pn^3+...pn^kn)

(3)   同余模公式:

(a+b)%m=(a%m+b%m)%m

(a*b)%m=(a%m*b%m)%m

分析:

代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define LL long long
//#include <bits/stdc++.h>
using namespace std;
const int maxn=10010;
const int mod=9901;
LL quick_pow(LL a, LL b){
    LL ans=1,res=a;
    while(b){
        if(b&1) ans=ans*res%mod;
        res=res*res%mod;
        b>>=1;
    }
    return ans;
}
LL sum(LL p, LL c){
    if(c==0) return 1;
    if(c&1) return (1+quick_pow(p,(c+1)/2))*sum(p,(c-1)/2)%mod;
    else return (quick_pow(p,c)+(1+quick_pow(p,c/2))*sum(p,c/2-1)%mod)%mod;
}
int main()
{
    int a,b;
    int p[maxn],c[maxn];
    while(~scanf("%d %d",&a,&b)){
        int t=0;
        LL ans=1;
        memset(c,0,sizeof c);
        for(int i=2;i<=a;i++){
            if(a%i==0){
                p[t]=i;
                while(a%i==0){
                    a/=i;
                    c[t]++;
                }
                t++;
            }
        }
        for(int i=0;i<t;i++){
            ans=ans*sum(p[i],b*c[i])%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/81877997