CF1342C Yet Another Counting Problem

题目链接

题目描述

给定两个数字a,b,有q次询问,每次询问给出两个数字 l , r, 求 l 到 r 的范围内有多少数x使得 x % a % b != x % b % a

思路

要求x % a % b和 x % b % a,可以发现每次的循环区间是lcm(a,b),可以通过循环节解决问题。
因为 l,r 的范围在1e18, 考虑 l ~ r的范围就想到了前缀和的思维。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

const int N = 4e4 + 10;
LL f[N];
LL a, b, q, num;

LL gcd(LL a, LL b) {
	return b == 0 ? a : gcd(b, a % b);
}

LL cacl(LL x) {
	return f[num] * (x / num) + f[x % num];
}

void solve() {
	scanf("%lld %lld %lld", &a, &b, &q);
	if(a < b) swap(a, b);
	num = a / gcd(a, b) * b;
	for(int i = 1; i <= num; i++) {
		f[i] = f[i - 1];
		if(i % a % b != i % b % a) f[i]++;
	}
	while(q--) {
		LL l, r;
		LL cnt = 0;
		scanf("%lld %lld", &l, &r);
		printf("%lld ", cacl(r) - cacl(l - 1));
	}
	puts("");
}

int main() {
//	freopen("in.txt", "r", stdin);
	int t;
	scanf("%d", &t);
	while(t--) {
		solve();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值