一切的一切都源于这道题
题目描述:
Consider two natural numbers A and B. Let S be the sum of all natural divisors of AB.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.
示例1
输入:
2 3
输出:
15
说明
23=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,写了一下果不其然TLE了。
那就换个方式吧,脑海之中隐约有点印象是关于约数个数定理(可能不叫这个,但是先暂且叫他这个吧)和约数和定理,但是又有点模糊。
这三个东西都离不开唯一分解定理
唯一分解定理
任意n均可表示为
n=p1a1 * p2a2 * p3a3 ……*pkak
其中ai为指数,pi为素数
此定理表明 :任何一个大于 1 的正整数都可以表示为素数的积。
好,有了这个定理就可以表示下面的约数个数定理,约数和定理了。
约数个数定理
由唯一分解定理得到了n的各个素数因子pi的指数ai,对于每个素因子,我们可以选择0,1,2…ai个,有(ai+1)种可能
于是根据乘法定理,可得:
设F(n)表示n的正因子个数
F(n)=(1+a1)(1+a2)……(1+ak)
约数和定理
设G(n)表示n的正因子的和
G(n)=(1+p12+p13+…+p1a1)(1+p22+p23+…p2a2)…*(1+pn1+pn2+…+pnan)
Latex这里不好打,但是这个公式可以用等比数列求和再简化一下
欧拉函数
φ(n)表示小于n的正整数中与n互质的数的个数
欧拉还告诉我们
其中p取到n所有的质因数
给个代码,参考大佬的,现在还有些疑惑,晚点再更新
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
ll n, m,p=9901,tot=0;
const int N=1e5+10;
int isp[N] = {1, 1}, pri[N];
ll qp(ll a,ll b)
{
if(b==0)return 1;
if(b&1)return (qp(a%p, b >> 1) % p * qp(a%p, b >> 1) % p * a % p) % p;
else return (qp(a%p, b >> 1) % p * qp(a%p, b >> 1) % p) % p;
}
ll sum(ll t,ll k)
{
if(k==0)return 1;
if(k%2==0)return (t % p * sum(t, k - 1) + 1) % p;
return (1 + qp(t, k / 2 + 1)) * sum(t, k / 2) % p;
}
int main()
{
IOS;
cin >> n >> m;
ll res = 1;
for (ll i = 2; i <= n;i++)
{
ll s = 0;
while(n%i==0)
{
s++;
n /= i;
}
if(s) res = res * sum(i, s * m) % p;
}
if(!n)res = 0;
cout << res << endl;
return 0;
}