Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem
2020-06-08 04:42
204 查看
题面
题意
给定三个数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 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
- codeforces Educational Codeforces Round 41 (Rated for Div. 2) for B problem
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
- Educational Codeforces Round 64 (Rated for Div. 2) Problem - A - Codeforces Inscribed Figures
- Educational Codeforces Round 66 (Rated for Div. 2), problem: (C) Electrification
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
- Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
- Educational Codeforces Round 33 (Rated for Div. 2)
- Educational Codeforces Round 33 (Rated for Div. 2) 题解
- Educational Codeforces Round 63 (Rated for Div. 2)C - Alarm Clocks Everywhere
- Educational Codeforces Round 62 (Rated for Div. 2)
- Educational Codeforces Round 41 (Rated for Div. 2) G. Partitions
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 68 (Rated for Div. 2)-C-From S To T
- Educational Codeforces Round 37 (Rated for Div. 2) C. Swap Adjacent Elements
- Educational Codeforces Round 61 (Rated for Div. 2)(A、B、C、D、E、F)
- Educational Codeforces Round 61 (Rated for Div. 2) D. Stressful Training 详解
- C. Playlist---(贪心+优先队列)Educational Codeforces Round 62 (Rated for Div. 2)
- Educational Codeforces Round 36 (Rated for Div. 2) C 不大于某个数的序列变换