1190 最小公倍数之和 V2
∑i=ablcm(i,b)∑i=abi×bgcd(i,b)b∑d∣b∑i=⌈ad⌉bdi[gcd(i,bd)=1]b∑d∣b∑k∣bdμ(k)k∑i=⌈⌈ad⌉k⌉abkib∑T∣n∑i=⌈aT⌉bTi∑k∣Tμ(k)kb∑T∣n(bT+⌈aT⌉)×(bT−⌈aT⌉+1)2∑k∣Tμ(k)k设f(n)=∑d∣nμ(d)d,f(1)=1,f(p)=1−p,f(pk)=1−p,且为积性函数\sum_{i = a} ^{b} lcm(i, b)\\ \sum_{i = a} ^{b} \frac{i \times b}{\gcd(i, b)}\\ b \sum_{d \mid b} \sum_{i = \lceil \frac{a}{d} \rceil} ^{\frac{b}{d}} i [gcd(i, \frac{b}{d}) = 1]\\ b \sum_{d \mid b} \sum_{k \mid \frac{b}{d}} \mu(k) k \sum_{i = \lceil \frac{ \lceil \frac{a}{d} \rceil}{k} \rceil} ^{\frac{a}{bk}}i\\ b \sum_{T \mid n} \sum_{i = \lceil \frac{a}{T} \rceil} ^{\frac{b}{T}} i \sum_{k \mid T} \mu(k) k\\ b \sum_{T \mid n} \frac{ (\frac{b}{T} + \lceil \frac{a}{T} \rceil) \times ( \frac{b}{T} - \lceil \frac{a}{T} \rceil + 1)}{2} \sum_{k \mid T} \mu(k) k\\ 设f(n) = \sum_{d \mid n} \mu(d) d,f(1) = 1, f(p) = 1 - p, f(p ^ k) = 1- p,且为积性函数\\ i=a∑blcm(i,b)i=a∑bgcd(i,b)i×bbd∣b∑i=⌈da⌉∑dbi[gcd(i,db)=1]bd∣b∑k∣db∑μ(k)ki=⌈k⌈da⌉⌉∑bkaibT∣n∑i=⌈Ta⌉∑Tbik∣T∑μ(k)kbT∣n∑2(Tb+⌈Ta⌉)×(Tb−⌈Ta⌉+1)k∣T∑μ(k)k设f(n)=d∣n∑μ(d)d,f(1)=1,f(p)=1−p,f(pk)=1−p,且为积性函数
所以最后我们只要2prime_facofb2 ^{prime\_fac\ of\ b}2prime_fac of b的复杂度去枚举ddd即可。
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}const int N = 1e5 + 10, mod = 1e9 + 7;int prime[N], cnt;bool st[N];void init() {for(int i = 2; i < N; i++) {if(!st[i]) prime[cnt++] = i;for(int j = 0; j < cnt && i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) break;}}
}ll a, b, ans;int fac[50], num[50], tot;void dfs(int step, ll T, ll g){if(step == tot + 1){ans = (ans + ((1 + b / T ) * (b / T) / 2 - (1 + a / T) * (a / T) / 2) % mod * g % mod) % mod;return ;}ll sum = 1;dfs(step + 1, T * sum, g);for(int i = 1; i <= num[step]; i++){sum = sum * fac[step];dfs(step + 1, T * sum, g * (1 - fac[step]));}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();int T = read();while(T--) {a = read() - 1, b = read(); ll n = b;tot = 0;for(int i = 0; prime[i] * prime[i] <= n; i++) {if(n % prime[i] == 0) {tot++;fac[tot] = prime[i], num[tot] = 0;while(n % prime[i] == 0) {n /= prime[i];num[tot]++;}}}if(n != 1) {tot++;fac[tot] = n, num[tot] = 1;}ans = 0;dfs(1, 1, 1);printf("%lld\n",(ans * b % mod + mod) % mod);}return 0;
}