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.

81 lines
2.6 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;
#define int long long
#define endl "\n"
const int mod = 1e9 + 7; // 尽量这样定义mod ,减少非必要的麻烦
// 快速幂
int qmi(int a, int b) {
int res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
vector<int> p; // 将m拆分成的质数因子序列p
signed main() {
#ifndef ONLINE_JUDGE
freopen("SpareTire.in", "r", stdin);
#endif
int n, m;
int Six = qmi(6, mod - 2); // 因为需要用到 % 1e9+7 下6的逆元用费马小定理+快速幂求逆元
int Two = qmi(2, mod - 2); // 因为需要用到 % 1e9+7 下2的逆元用费马小定理+快速幂求逆元
while (cin >> n >> m) {
// 所结果拆分为平方和公式,等差数列两部分
// 注意:现在求的是整体值,还没有去掉不符合条件的数字
int first = n * (n + 1) % mod * (2 * n + 1) % mod * Six % mod;
int second = n * (n + 1) % mod * Two % mod;
int res = (first + second) % mod;
// 对m进行质因子分解
int t = m; // 复制出来
for (int i = 2; i * i <= t; i++) {
if (t % i == 0) {
p.push_back(i);
while (t % i == 0) t = t / i;
}
}
if (t > 1) p.push_back(t);
/*
容斥原理
例如有3个因子那么1<<3=8(1000二进制)
然后i从1开始枚举直到7(111二进制i中二进制的位置1表式取这个位置的因子
例如i=3(11二进制) 表示取前两个因子i=5101表示取第1个和第3个的因子
*/
int s = 0;
for (int i = 1; i < (1 << p.size()); i++) {
int cnt = 0, t = 1;
for (int j = 0; j < p.size(); j++)
if ((i >> j) & 1) {
cnt++;
t *= p[j];
}
// 比如找到了s=6=2*3需要知道s是奇数个还是偶数个因子
// n/s:范围内6的倍数有多少个
int k = n / t;
int x = k * (k + 1) % mod * (2 * k + 1) % mod * Six % mod;
x = x * t % mod * t % mod; // 乘上t^2
// 还需要累加等差数列部分
// 首项是t,项数是k,末项 t*k
x = (x + k * (t + t * k) % mod * Two % mod) % mod;
if (cnt & 1)
s = (s + x) % mod;
else
s = (s - x + mod) % mod;
}
// 输出
cout << (res - s + mod) % mod << endl;
}
}