A. Slackline Adventure(思维 + 莫比乌斯)(2018-2019 ACM-ICPC Brazil Subregional Programming Contest)
A. Slackline Adventure
考慮枚舉每個矩形的長跟寬,再統計這個矩形能在坐標軸上出現幾次(同行相鄰同列相鄰的單獨算),然后有如下式子:
2×∑i=1n?1∑j=1m?1(n?i)(m?j)[gcd?(i,j)=1][L2≤i2+j2≤R2]2 \times \sum_{i = 1} ^{n - 1} \sum_{j = 1} ^{m - 1}(n - i)(m - j)[\gcd(i, j) = 1][L ^ 2 \leq i ^ 2 + j ^ 2 \leq R ^ 2]\\ 2×i=1∑n?1?j=1∑m?1?(n?i)(m?j)[gcd(i,j)=1][L2≤i2+j2≤R2]
設F(n,m,T2)=∑i=1n?1∑j=1m?1(n?i)(m?j)[gcd?(i,j)=1,i2+j2≤T2]F(n, m, T ^ 2) = \sum\limits\limits_{i = 1} ^{n - 1} \sum\limits_{j = 1} ^{m - 1} (n - i)(m - j)[\gcd(i, j) = 1, i ^ 2 + j ^ 2 \leq T ^ 2]F(n,m,T2)=i=1∑n?1?j=1∑m?1?(n?i)(m?j)[gcd(i,j)=1,i2+j2≤T2],則ans=F(n,m,R2)?F(n,m,L2?1)ans = F(n,m, R ^ 2) - F(n, m, L ^ 2 - 1)ans=F(n,m,R2)?F(n,m,L2?1)。
∑k=1n?1μ(k)∑i=1n?1k∑j=1m?1k(n?ik)(m?jk)[i2k2+j2k2≤T2]∑k=1n?1μ(k)∑i=1n?1k(n?ik)calc()\sum_{k = 1} ^{n - 1} \mu(k) \sum_{i = 1} ^{\frac{n - 1}{k}} \sum_{j = 1} ^{\frac{m - 1}{k}} (n - ik)(m - jk)[i ^ 2 k ^ 2 + j ^ 2 k ^ 2 \leq T ^ 2]\\ \sum_{k = 1} ^{n - 1} \mu(k) \sum_{i = 1} ^{\frac{n - 1}{k}} (n - ik) calc()\\ k=1∑n?1?μ(k)i=1∑kn?1??j=1∑km?1??(n?ik)(m?jk)[i2k2+j2k2≤T2]k=1∑n?1?μ(k)i=1∑kn?1??(n?ik)calc()
calc()calc()calc()就是當已知i,k,Ti, k, Ti,k,T,有多少jjj滿足i2k2+j2k2≤T2i ^ 2 k ^ 2 + j ^ 2 k ^ 2 \leq T ^ 2i2k2+j2k2≤T2,的m?jkm - jkm?jk求和,整體復雜度O(k×nlog?n)O(k \times n \log n)O(k×nlogn),kkk是個比較小的常數
今天VP的時候碰到的,到了一直沒看這題,到了最后才發現是個數論題。
說一點點坑點吧,注意函數不能寫成f(n,m,T)=∑i=1n?1∑j=1m?1(n?i)(m?j)[gcd?(i,j)=1,i2+j2≤T2]f(n, m, T) = \sum\limits\limits_{i = 1} ^{n - 1} \sum\limits_{j = 1} ^{m - 1} (n - i)(m - j)[\gcd(i, j) = 1, i ^ 2 + j ^ 2 \leq T ^ 2]f(n,m,T)=i=1∑n?1?j=1∑m?1?(n?i)(m?j)[gcd(i,j)=1,i2+j2≤T2],
然后答案是f(n,m,R)?f(n,m,L?1)f(n, m, R) - f(n, m, L - 1)f(n,m,R)?f(n,m,L?1),這樣會使滿足條件得滿足條件的一些數(L?1)2<i2k2+j2k2<L2(L - 1) ^ 2 <i ^ 2 k ^ 2 + j ^ 2 k ^ 2 < L ^ 2(L?1)2<i2k2+j2k2<L2,
沒有被刪去而算在貢獻里了,會導致答案出錯,就因為這個調了一晚上,一直不知道哪里錯了,,,
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10, mod = 1e9 + 7;int prime[N], mu[N], cnt, n, m, L, R;bool st[N];inline int add(int x, int y) {return x + y < mod ? x + y : x + y - mod; }inline int sub(int x, int y) {return x >= y ? x - y : x - y + mod; }void init() {mu[1] = 1;for (int i = 2; i < N; i++) {if (!st[i]) {mu[i] = -1;prime[++cnt] = i;}for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if (i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}} }int calc(int i, int k, long long T) { // i * i * k * k + j * j * k * k <= Tint l = 0, r = (m - 1) / k;while (l < r) {int mid = l + r + 1 >> 1;if (1ll * i * i * k * k + 1ll * mid * mid * k * k > T) {r = mid - 1;}else {l = mid;}}return 1ll * (m - k + m - l * k) * l / 2 % mod; }int f(long long T) {int ans = 0;for (int k = 1, lim, res; k <= n - 1; k++) {if (mu[k] == 0) {continue;}lim = (n - 1) / k, res = 0;for (int i = 1; i <= lim; i++) {res = add(res, 1ll * (n - i * k) * calc(i, k, T) % mod);}if (mu[k] == 1) {ans = add(ans, res);}else {ans = sub(ans, res);}}ans = 2ll * ans % mod;if (T >= 1) {ans = add(ans, add(1ll * n * (m - 1) % mod, 1ll * (n - 1) * m % mod));}return ans; }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);init();scanf("%d %d %d %d", &n, &m, &L, &R);printf("%d\n", sub(f(1ll * R * R), f(1ll * L * L - 1)));return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的A. Slackline Adventure(思维 + 莫比乌斯)(2018-2019 ACM-ICPC Brazil Subregional Programming Contest)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 戈登和芭芭拉最后在一起了吗 戈登的人物简
- 下一篇: 黄油能做什么简单食物 用黄油可以做哪些简