算法提高课 97. 约数之和
作者:
羽笙
,
2024-03-13 10:13:59
,
所有人可见
,
阅读 24
直接套约数之和公式,10/14
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int mod = 9901;
int a,b;
typedef pair<int, int> PII;
vector<PII> ans;
signed main()
{
cin>>a>>b;
int x = a,res = a;
for(int i = 2;i<=x/i;i++)
{
if(res % i == 0)
{
int num = 0;
while(res % i == 0)
{
num++;
res /= i;
}
//cout<<i<<" "<<num<<endl;
ans.push_back({i,num * b});
}
}
if(res > 1)ans.push_back({res,b});
int result = 1;
for(auto item:ans)
{
int p = item.first,num = item.second;
int tmp = 1;
for(int i = 0;i<num;i++)tmp = (tmp * p % mod + 1) % mod;
result = result * tmp % mod;
}
if(a == 0)result = 0;
if(b == 0)result = 1;
cout<<result<<endl;
return 0;
}
用等比公式快速求和+快速幂
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int mod = 9901;
int a,b;
typedef pair<int, int> PII;
vector<PII> ans;
int qmi(int a,int b)
{
int res = 1;
while(b)
{
if(b & 1)res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
signed main()
{
cin>>a>>b;
int x = a,res = a;
for(int i = 2;i<=x/i;i++)
{
if(res % i == 0)
{
int num = 0;
while(res % i == 0)
{
num++;
res /= i;
}
//cout<<i<<" "<<num<<endl;
ans.push_back({i,num * b});
}
}
if(res > 1)ans.push_back({res,b});
int result = 1;
for(auto item:ans)
{
int p = item.first,num = item.second;
int tmp = 1;
//for(int i = 0;i<num;i++)tmp = (tmp * p % mod + 1) % mod;
if((p - 1) % mod == 0)
{
tmp = (num + 1) % mod;
}
else
{
tmp = (qmi(p,num+1) - 1) % mod * qmi((p-1),mod-2) % mod;
}
result = result * tmp % mod;
}
if(a == 0)result = 0;
if(b == 0)result = 1;
cout<<(result % mod + mod) % mod<<endl;
return 0;
}