POJ 1845 Sumdiv (整数唯一分解定理)

题目链接

Sumdiv
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 25841   Accepted: 6382

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的约数和。

题解:

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

任意一个整数都可以写成素数相乘的形式

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

 (2)约数:

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%mod = (a%b*mod)/b;

 (4) 快速幂。

有了以上基础,最终:

A^B的所有约数之和为:

     sum = [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...* [1+pn+pn^2+...+pn^(an*B)].

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <algorithm>
#include <deque>
#include <vector>
#define mem(arr,num) memset(arr,0,sizeof(arr))
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define __for(i, a, b) for(int i = a; i >=b; i--)
#define IO ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
using namespace std;
typedef long long ll;
typedef vector<int > vi;
const ll INF = 0x3f3f3f3f;
const int mod = 9901;
const int N = 50000+ 5;
bool vis[N];
int prime[N],num;
void getprime() {
    _for(i, 2, N){
        if(!vis[i]) prime[++num] = i;
        for(int j = 1; j <= num && i * prime[j] <= N; j++){
            vis[i * prime[j]] = true;
            if(i % prime[j] == 0) break;
        }
    }
}
/*
ll quick_pow(ll a, ll b, ll m) {
    ll ret = 1;
    a %= m;
    while(b) {
        if(b & 1) ret = (ret * a) % m;
        b >>= 1;
        a = (a * a) % m;
    }
    return ret; 
} 
有可能爆long long;
*/
ll quick_pow1(ll a, ll b, ll m){
    ll ret = 0;
    a %= m;
    while(b) {
        if(b & 1) ret = (ret + a) % m;
        b >>= 1;
        a = (a + a) % m;
    }
    return ret;
}
ll quick_pow(ll a, ll b, ll m){
    ll ret = 1;
    while(b) {
        if(b & 1) ret = quick_pow1(ret,a,m);
        a = quick_pow1(a,a,m);
        b >>= 1;
    }
    return ret;
}
int main() {
    ll A, B, ans = 1;
    getprime();
    cin >> A >> B;
    for(int i = 1; prime[i] * prime[i] <= A; i++){
        int cnt = 0;
        if(A % prime[i] == 0){
            while(A % prime[i] == 0){
            cnt++;
            A /= prime[i];
        }
            // a/b%c = (a%b*c/b)
            ll M = (prime[i] - 1) * mod;
            ans *= (quick_pow(prime[i], cnt * B +1, M) + M - 1)/(prime[i] - 1);
            ans %= mod;
        }
    }
    if(A > 1){
            ll M = (A - 1) * mod;
            ans *= (quick_pow(A, B +1, M) + M - 1)/(A - 1);
            ans %= mod;
        }
    cout << ans << endl;
    return 0;
}
宝剑锋从磨砺出 梅花香自苦寒来
原文地址:https://www.cnblogs.com/GHzcx/p/8797651.html