
[英]Find indices i<j in an array of size n so that the sum of values at those indices is equal to i + j
[英]Given integers C and N, (c <= n <= 10^9), find the amount of pairs i,j (n >= i >= j >= 1), where gcd(i,j) == C
给定整数 C 和 N,(c <= n <= 10^9),找出对 i,j (n >= i >= j >= 1) 的数量,其中 gcd(i,j) == C
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void solve(int tt){
int c,n;
cin >> c >> n;
ll ans = 0;
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
if(gcd(i,j) == c) ans++;
}
}
cout << ans;
return;
}
这已经超时了,我尝试了各种不同的方法来尝试修复它 - 没有任何效果......有人知道优化它的代码吗? (输出 %100000007)
“给定整数 C 和 N,(c <= n <= 10^9),找出对 i,j (n >= i >= j >= 1) 的数量,其中 gcd(i,j) == C “
我们可以将所有内容除以 C 得到:
有多少整数 i 和 j 满足:2 <= j < i <= n/c,并且互质?
让我们检查一些:
n/c count (new pairs listed in parens)
<=2 0
3 1 (2,3)
4 2 (3,4)
5 5 (2,5), (3,5), (4,5)
6 6 (5,6)
7 11 (2,7), (3,7), (4,7), (5,7), (6,7)
8 14 (3,8), (5,8), (7,8)
9 19 (2,9), (4,9), (5,9), (7,9), (8,9)
每次我们增加 i 时,我们都可以将它与所有不具有任何相同因子的 j >= 2 的较小值配对。 对于质数,这是所有较小的 j >= 2 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.