欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem

程序员文章站 2022-06-02 22:14:46
...

题面

Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem

题意

给定三个数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;
}
相关标签: acm竞赛