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

本文解析了CodeForces竞赛中一道题目C的解决方案,通过观察规律和使用前缀和方法,快速计算出指定区间内满足特定条件的数字数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:https://codeforces.com/contest/1342/problem/C

题意:
	输入两个整数a,b且(1 <= a, b <= 200)和一个查询次数q且(1 <= q <= 500)。
	每次查询输入一个区间l,r且(1 <= l <= r <= 1e18)
	问在这个区间内的数字有多少数字i满足(i % a) % b != (i % b) % a。
解决方法:
	通过无脑for循环打表找到了规律。
	使用前缀和的方法记录一个区间(a * b)内有多少数字满足条件。
	得出结果:(aa[n - 1] * (right / n) + aa[right % n]) - (aa[n - 1] * ((left - 1) / n) + aa[(left - 1) % n])
	aa[n - 1]:一个区间内的数量
	(right / n):查询区间右端点包含了多少个区间
	aa[right % n]:查询区间右端点余下的区间
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
#define dd double
#define PI acos(-1.0)
#define f(i, x, y) for(ll i = x; i < y; i++)
using namespace std;
const ll MAXN = 2e5 + 5;

ll aa[50000];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll t; cin >> t;
	while (t--) {
		ll a, b, q; cin >> a >> b >> q;
		aa[0] = 0;
		ll n = a * b;
		f(i, 1, n) {
			if ((i % a) % b != (i % b) % a) {
				aa[i] = aa[i - 1] + 1;
			}
			else {
				aa[i] = aa[i - 1];
			}
		}
		while (q--) {
			ll left, right;
			cin >> left >> right;
			cout << (aa[n - 1] * (right / n) + aa[right % n]) - (aa[n - 1] * ((left - 1) / n) + aa[(left - 1) % n]) << endl;
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值