AcWing - 97 - 约数之和(分治因数和)
Posted shuitiangong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AcWing - 97 - 约数之和(分治因数和)相关的知识,希望对你有一定的参考价值。
题目链接
??我们首先要知道怎么来求A的约数之和。首先,把A分解质因数,可得:(A = q1^{k1} imes q2^{k2} ... imes qn^{kn})
然后我们用乘法的分配律可得A的因数之和为(F(A) = (q1^0 + q1^1 + ... + q1^{k1}) imes (q2^0 + q2^1 + ... + q2^{k2}) ... imes (qn^0 + qn^1 + ... + qn^{kn}))
??那么对于(A^B)来说,(A^B = q1^{k1 imes B} imes q2^{k2 imes B} ... imes qn^{kn imes B}),(F(A^B) = (q1^0 + q1^B + ... + q1^{k1 imes B}) imes (q2^0 + q2^B + ... + q2^{k2 imes B}) ... imes (qn^0 + qn^B + ... + qn^{kn imes B}))
所以说,我们只要求出来所有因数的所有幂次之和这道题就能很容易的求出来了(q^0 + q^B + ... + q^{k imes B}),但是我们发现(k imes 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 '
'
#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("==================================================
")
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 - 约数之和(分治因数和)的主要内容,如果未能解决你的问题,请参考以下文章
《算法竞赛进阶指南》-AcWing-97. 约数之和 Sumdiv-题解