AcWing - 97 - 约数之和(分治、因数和)

题目链接

分析

  首先,由唯一分解定理的推论可以得到下面的式子:
\(F(A) = (q_1^0+q_1^1+...+q_1^{k_1})\times (q_2^0+q_2^1+...+q_2^{k_2})...\times (q_n^0 + q_n^1 + ... + q_n^{k_n})\)
  然后我们把\(A\)替换成\(A^B\)可得:
\(F(A^B)=(q_1^0+q_1^B+...+{q_1^{k_1}}\times B)\times (q_2^0+q_2^B+...+{q_2^{k_2}}\times B)...\times (q_n^0+q_n^B+...+{q_n^{k_n}}\times B)\)

解题思路

  所以说,我们只要求出来所有因数的所有幂次之和这道题就能很容易的求出来了,但是我们发现\(k\times B\)看起来并不好求,用暴力的方法显然会超时,关于这个式子的求法可以看这里

//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("==================================================\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  9901;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e4+10;
P fac[maxn];
ll a, b; int kase;
void solve(int a) { //分解因数
    int t = a; kase = 0;
    for (int i = 2; i*i<=t && a!=1; ++i) {
        ll cnt = 0;
        while(!(a%i)) {
            a /= i;
            ++cnt;
        }
        if (cnt) fac[kase++] = P(i, cnt*b);
    }
    if (a>1) fac[kase++] = P(a, b); //如果a本身就是素数,那么a的因数就只有1和自己
}
ll solve2(ll x, int y) { //快速幂
    ll ans = 1; x %= MOD;
    while(y) {
        if (y&1) ans = ans*x%MOD;
        x = x*x%MOD;
        y >>= 1;
    }
    return ans;
}
ll solve3(ll a, ll b) { //计算每个因数所有幂次的累加和
    if (!b) return 1;
    ll res = 1;
    if (b&1) res = res*(1+solve2(a, b/2+1))%MOD*solve3(a, b/2)%MOD;
    else res = res*(((1+solve2(a, b/2))*solve3(a, b/2-1)%MOD + solve2(a, b))%MOD)%MOD;
    return res;
}
int main(void) {
    while(cin >> a >> b) {
        if (!a) {
            cout << 0 << endl;
            continue;
        }
        solve(a);
        ll ans = 1;
        for (int i = 0; i<kase; ++i)
            ans = ans*solve3(fac[i].first, fac[i].second)%MOD;
        cout << ans << endl;
    }
    return 0;
}
posted @ 2020-03-21 12:11  shuitiangong  阅读(164)  评论(0编辑  收藏  举报