码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-03-21 13:06:27      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:return   air   algorithm   algo   names   print   clu   sync   memset   

题目链接
??我们首先要知道怎么来求A的约数之和。首先,把A分解质因数,可得:\(A = q1^{k1}\times q2^{k2} ... \times \ qn^{kn}\)
然后我们用乘法的分配律可得A的因数之和为\(F(A) = (q1^0 + q1^1 + ... + q1^{k1}) \times (q2^0 + q2^1 + ... + q2^{k2})\ ... \times \ (qn^0 + qn^1 + ... + qn^{kn})\)
??那么对于\(A^B\)来说,\(A^B = q1^{k1\times B}\times q2^{k2\times B} ... \times \ qn^{kn\times B}\)\(F(A^B) = (q1^0 + q1^B + ... + q1^{k1\times B}) \times (q2^0 + q2^B + ... + q2^{k2\times B})\ ... \times \ (qn^0 + qn^B + ... + qn^{kn\times B})\)
所以说,我们只要求出来所有因数的所有幂次之和这道题就能很容易的求出来了\(q^0 + q^B + ... + q^{k\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;
}

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

标签:return   air   algorithm   algo   names   print   clu   sync   memset   

原文地址:https://www.cnblogs.com/shuitiangong/p/12538482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!