Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem
程序员文章站
2022-06-02 22:14:46
...
题面
题意
给定三个数a,b,q,再给q组整数l和r,求从l到r有多少个x满足(x%a)%b!=(x%b)%a
思路
分析了一波数据,发现这是个规律题,lcm(a,b)为一个循环节,那就可以直接存一个循环,用后缀存,并且存下一个循环里面满足条件的个数,然后求出l-1和r对应的后缀取差值即可
代码
#include <bits/stdc++.h>
using namespace std;
long long ans[50000];
long long gcd(long long a, long long b)
{
if (a < b)
swap(a, b);
while (a % b != 0)
{
long long t = b;
b = a % b;
a = t;
}
return b;
}
long long gbs(long long a, long long b)
{
return a * b / gcd(a, b);
}
int main()
{
long long t;
cin >> t;
while (t--)
{
long long a, b, q, num = 0;
cin >> a >> b >> q;
int len = gbs(a, b);
for (long long i = 1; i <= len; i++)
{
if (i % a % b == i % b % a)
ans[i] = 0;
else
{
num++;
ans[i] = 1;
}
}
for (long long ss = 0; ss < q; ss++)
{
long long l, r, l1 = 0, r1 = 0;
cin >> l >> r;
l--;
l1 = l / len * num;
for (long long i = 1; i <= l % len; i++)
l1 += ans[i];
r1 = r / len * num;
for (long long i = 1; i <= r % len; i++)
r1 += ans[i];
cout << r1 - l1;
if (ss == q - 1)
cout << endl;
else
cout << ' ';
}
}
return 0;
}
推荐阅读
-
Educational Codeforces Round 85 (Rated for Div. 2) C. Circle of Monsters(前缀和 预处理 贪心)
-
Educational Codeforces Round 99 (Rated for Div. 2) C. Ping-pong
-
C. Mortal Kombat Tower(动态规划)Educational Codeforces Round 95 (Rated for Div. 2)
-
Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water[题解](数学)
-
Educational Codeforces Round 95 (Rated for Div. 2)C. Mortal Kombat Tower【dp】
-
Educational Codeforces Round 86 (Rated for Div. 2)---C
-
codeforces Educational Codeforces Round 80 (Rated for Div. 2) D - Minimax Problem(状压)
-
Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem
-
Educational Codeforces Round 86 (Rated for Div. 2) 总结
-
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms(思维)