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.

77 lines
2.3 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 <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 10000;
const LL Six = 166666668; /// 62关于mod的乘法逆元
const LL Two = 500000004;
const LL mod = 1e9 + 7; /// 尽量这样定义mod ,减少非必要的麻烦
bool book[maxn];
int pri[1300], cnt; /// 素数表
int factor[130]; /// 因子表
void Prime() {
memset(book, 0, sizeof(book));
cnt = 0;
book[0] = book[1] = 1;
for (int i = 2; i < maxn; i++) {
if (!book[i])
pri[cnt++] = i;
for (int j = 0; j < cnt && pri[j] * i < maxn; j++) {
book[i * pri[j]] = 1;
if (i % pri[j] == 0)
break;
}
}
}
inline LL Mod(LL a, LL b) {
return (a % mod) * (b % mod) % mod;
}
inline LL F(LL k, LL n) { /// 求(k*n)^2+k*n
return (Mod(k, k) * Mod(Mod(n, n + 1), Mod(n + n + 1, Six)) % mod + Mod(Mod(1 + n, n), Mod(k, Two))) % mod;
}
int main() {
LL n, m;
Prime();
while (~scanf("%lld%lld", &n, &m)) {
int fac_cnt = 0; /// 素数因子的个数
for (int i = 0; i < cnt; i++) {
if (pri[i] > m) break;
if (m % pri[i] == 0) {
factor[fac_cnt++] = pri[i];
while (m % pri[i] == 0) m /= pri[i];
}
}
if (m > 1) factor[fac_cnt++] = m;
LL sum = F(1LL, n), ans = 0; /// 计算总和sum
LL item = 1 << fac_cnt; /// 开始容斥
/// 例如有3个因子那么item=1<<3=8(1000二进制)
/// 然后i从1开始枚举直到7(111二进制i中二进制的位置1表式取这个位置的因子
/// 例如i=3(11二进制) 表示去前两个因子i=5101表示取第1个和第3个的因子
for (int i = 1; i < item; i++) {
int num = 0, x = 1;
for (int j = 0; j < fac_cnt; j++) {
if (1 & (i >> j)) num++, x *= factor[j];
}
if (num & 1)
ans = (ans + F(x, n / x)) % mod; /// 根据容斥,取奇数个因子时,应加上
else
ans = ((ans - F(x, n / x)) % mod + mod) % mod;
}
printf("%lld\n", ((sum - ans) % mod + mod) % mod);
}
return 0;
}