You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10010; //质数因子个数上限因为a的数据范围是5*10^7,开根号就是10^4肯定能装得下了
const int MOD = 9901; //要模的质数
LL res = 1; //结果,因为是要乘来乘去初始值需要给1
int a, B; //a的b次方
// 快速幂模板[与yxc的有一点点区别把p=MOD写在代码里了减少了参数传入]
int qmi(int a, int k) {
int res = 1; //答案
while (k) { //一路让k变小直到为0停止
if (k & 1) res = (LL) res * a % MOD; //如果k的个位是1的话
k >>= 1; //右移一位
a = (LL) a * a % MOD; //1-2-4-8-16就是每进一位是把a=a*a,注意使用long long 防止在乘积过程中爆了int
}
return res;
}
/**
* 功能:约数和公式的一部分,等比数列求和公式
* @param p
* @param k
* @return
*/
int sum(int p, int k) {
int res;
k *= b; //滚动生成幂次
if ((p - 1) % MOD == 0) res = (k + 1) % MOD; //当p-1是MOD倍数时逆元不存在 res=(k+1)%MOD 就是上面推导的含义,
else {
//MOD是质数
int inv = qmi((p - 1) % MOD, MOD - 2); //快速幂+费马小定理求出逆元
//利用等比数列求和公式
res = (qmi(p % MOD, k + 1) - 1) % MOD * inv % MOD;
}
return res;
}
/**
* 功能:分解质数因数
* @param a 待分解的数字
*/
int primes[N]; //质数因子数组
int idx; //质数因子数组下标游标
int mc[N]; //mc对应着幂次
void Decomposition(int a) {
//清空,防止重复
memset(primes, 0, sizeof primes);
memset(mc, 0, sizeof mc);
idx = 0;
//开始
for (int i = 2; i * i <= a; i++) {
//如果发现a的一个因子i
if (a % i == 0) {
primes[++idx] = i; //质因子数组维护
mc[idx] = 1; //指数幂次数+1
a /= i; //去掉这个因子
while (a % i == 0) { //除干净它
mc[idx]++; //指数幂次数+1
a /= i; //继续缩小
}
}
}
//可能剩余一个很大的质因子数
if (a > 1) {
primes[++idx] = a;
mc[idx] = 1;
}
}
int main() {
//读入a和b,准备计算a^b
cin >> a >> b;
//对a分解质因子
Decomposition(a);
//连乘,遍历所有质数因子
for (int i = 1; i <= idx; i++)
res = res * sum(primes[i], mc[i]) % MOD;
//输出
printf("%d\n", (res % MOD + MOD) % MOD);//测试数据14中有负数这样写才能通过
return 0;
}