本文主要是介绍NOI2016 循环之美(莫比乌斯反演)(杜教筛),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
考虑什么样的 x y \frac{x}{y} yx 可以成为纯循环小数
设其循环节为 L L L,那么有
x y ∗ k L − x y \frac{x}{y}*k^L-\frac{x}{y} yx∗kL−yx 为整数
每一对贡献在 g c d ( x , y ) = 1 gcd(x,y)=1 gcd(x,y)=1 的时候统计,于是上面这个条件可以转换为
∃ L , s . t , k L − 1 ≡ 0 ( m o d y ) \exists L,s.t,k^L-1\equiv0(mod\ y) ∃L,s.t,kL−1≡0(mod y)
发现这个条件等价于 g c d ( k , y ) = 1 gcd(k,y)=1 gcd(k,y)=1
推式子,大力莫比乌斯反演
A n s = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = 1 ] [ g c d ( j , k ) = 1 ] Ans=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=1][gcd(j,k)=1] Ans=i=1∑nj=1∑m[gcd(i,j)=1][gcd(j,k)=1]
= ∑ d = 1 n μ ( d ) ⌊ n d ⌋ ∑ d ∣ j [ g c d ( j , k ) = 1 ] =\sum_{d=1}^n\mu(d)\lfloor \frac{n}{d}\rfloor \sum_{d|j}[gcd(j,k)=1] =d=1∑nμ(d)⌊dn⌋d∣j∑[gcd(j,k)=1]
= ∑ d = 1 n μ ( d ) ⌊ n d ⌋ ∑ j = 1 m / d [ g c d ( j d , k ) = 1 ] =\sum_{d=1}^n\mu(d)\lfloor \frac{n}{d}\rfloor \sum_{j=1}^{m/d}[gcd(jd,k)=1] =d=1∑nμ(d)⌊dn⌋j=1∑m/d[gcd(jd,k)=1]
后面的那个 g c d gcd gcd 很明显可以拆成两个
= ∑ d = 1 n μ ( d ) ⌊ n d ⌋ [ g c d ( d , k ) = 1 ] ∑ j = 1 m / d [ g c d ( j , k ) = 1 ] =\sum_{d=1}^n\mu(d)\lfloor \frac{n}{d}\rfloor [gcd(d,k)=1]\sum_{j=1}^{m/d}[gcd(j,k)=1] =d=1∑nμ(d)⌊dn⌋[gcd(d,k)=1]j=1∑m/d[gcd(j,k)=1]
k k k 很小,后面那坨 f ( m / d ) f(m/d) f(m/d) 可以用 f ( n ) = φ ( k ) ∗ ⌊ n k ⌋ + f ( x % k ) f(n)=\varphi(k)*\lfloor \frac{n}{k}\rfloor + f(x\% k) f(n)=φ(k)∗⌊kn⌋+f(x%k) O ( 1 ) O(1) O(1) 算
于是有
A n s = ∑ d = 1 n μ ( d ) ⌊ n d ⌋ [ g c d ( d , k ) = 1 ] f ( ⌊ m d ⌋ ) Ans=\sum_{d=1}^n\mu(d)\lfloor \frac{n}{d}\rfloor [gcd(d,k)=1]f(\lfloor \frac{m}{d}\rfloor) Ans=d=1∑nμ(d)⌊dn⌋[gcd(d,k)=1]f(⌊dm⌋)
如果可以快速求出 g ( n , k ) = ∑ i = 1 n μ ( i ) [ g c d ( i , k ) = 1 ] g(n,k)=\sum_{i=1}^n\mu(i)[gcd(i,k)=1] g(n,k)=∑i=1nμ(i)[gcd(i,k)=1] 的话,就可以整除分块了
继续莫比乌斯反演
g ( n , k ) = ∑ i = 1 n μ ( i ) [ g c d ( i , k ) = 1 ] g(n,k)=\sum_{i=1}^n\mu(i)[gcd(i,k)=1] g(n,k)=i=1∑nμ(i)[gcd(i,k)=1]
= ∑ l ∣ k μ ( l ) ∑ l ∣ i μ ( i ) =\sum_{l|k}\mu(l)\sum_{l|i}\mu(i) =l∣k∑μ(l)l∣i∑μ(i)
= ∑ l ∣ k μ ( l ) ∑ i = 1 n / l μ ( i l ) =\sum_{l|k}\mu(l)\sum_{i=1}^{n/l}\mu(il) =l∣k∑μ(l)i=1∑n/lμ(il)
这里有一个关于 μ ( p q ) \mu(pq) μ(pq) 的套路,就是
μ ( p q ) = μ ( p ) ∗ μ ( q ) ∗ [ g c d ( p , q ) = 1 ] \mu(pq)=\mu(p)*\mu(q)*[gcd(p,q)=1] μ(pq)=μ(p)∗μ(q)∗[gcd(p,q)=1]
意义显然,那么
g ( n , k ) = ∑ l ∣ k μ ( l ) 2 ∑ i = 1 n / l μ ( i ) [ g c d ( i , l ) = 1 ] g(n,k)=\sum_{l|k}\mu(l)^2\sum_{i=1}^{n/l}\mu(i)[gcd(i,l)=1] g(n,k)=l∣k∑μ(l)2i=1∑n/lμ(i)[gcd(i,l)=1]
看似没有化简,但仔细观察发现后面一坨就是 g ( n / l , l ) g(n/l,l) g(n/l,l)
于是有
g ( n , k ) = ∑ l ∣ k μ ( l ) 2 g ( n / l , l ) g(n,k)=\sum_{l|k}\mu(l)^2g(n/l,l) g(n,k)=l∣k∑μ(l)2g(n/l,l)
边界条件 l = 1 l=1 l=1 用杜教筛算 μ \mu μ 的前缀和就可以了
#include<bits/stdc++.h>
#define cs const
using namespace std;
cs int N = 5e6 + 5;
typedef long long ll;
int n, m, k;
int prim[N], mu[N], tot;
bool isp[N]; ll f[N], g[N], sm[N];
vector<int> fac[N];
int gcd(int a, int b){ return !b ? a : gcd(b, a % b); }
void prework(int n){mu[1] = sm[1] = 1;for(int i = 2; i <= n; i++){if(!isp[i]) prim[++tot] = i, mu[i] = -1;for(int j = 1; j <= tot; j++){if(i * prim[j] > n) break;isp[i * prim[j]] = 1; if(i % prim[j] == 0) break;mu[i * prim[j]] = -mu[i];} sm[i] = sm[i-1] + mu[i];}for(int i = 1; i <= k; i++) for(int j = i; j <= k; j += i) if(mu[i]) fac[j].push_back(i);for(int i = 1; i <= k; i++) g[i] = (gcd(i, k) == 1) + g[i-1];
}
ll G(int x){ return g[x % k] + (ll)g[k] * (x / k); }
unordered_map<int, ll> sm2;
ll Mu(int x){if(x <= N-5) return sm[x];if(sm2.count(x)) return sm2[x];ll ans = 1; for(int l=2, r; l<=x; l=r+1){int v = x/l; r = x/v;ans -= (ll)(r - l + 1) * Mu(v);} return sm2[x] = ans;
}
unordered_map<int, ll> mp[N];
ll F(int x, int k){if(x == 0) return 0;if(mp[k].count(x)) return mp[k][x];if(k == 1) return mp[k][x] = Mu(x);ll ans = 0;for(int i = 0; i < fac[k].size(); i++){int l = fac[k][i]; ans += F(x/l,l);} return mp[k][x] = ans;
}
int main(){cin >> n >> m >> k; prework(N-5);ll ans = 0;for(int l = 1, r, las = 0, now, lim = min(n,m); l <= lim; l = r+1){int v1 = n/l, v2 = m/l; r = min(n/v1, m/v2);now = F(r, k); ans += 1ll * v1 * G(v2) * (now - las); las = now;} cout << ans; return 0;
}
这篇关于NOI2016 循环之美(莫比乌斯反演)(杜教筛)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!