整理的算法模板:ACM算法模板總結(分類詳細版)
C. Yet Another Counting Problem
time limit per test
3.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two integers aa and bb, and qq queries. The ii-th query consists of two numbers lili and riri, and the answer to it is the number of integers xx such that li≤x≤rili≤x≤ri, and ((xmoda)modb)≠((xmodb)moda)((xmoda)modb)≠((xmodb)moda). Calculate the answer for each query.
Recall that ymodzymodz is the remainder of the division of yy by zz. For example, 5mod3=25mod3=2, 7mod8=77mod8=7, 9mod4=19mod4=1, 9mod9=09mod9=0.
Input
The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then the test cases follow.
The first line of each test case contains three integers aa, bb and qq (1≤a,b≤2001≤a,b≤200; 1≤q≤5001≤q≤500).
Then qq lines follow, each containing two integers lili and riri (1≤li≤ri≤10181≤li≤ri≤1018) for the corresponding query.
Output
For each test case, print qq integers — the answers to the queries of this test case in the order they appear.
Example
input
Copy
2 4 6 5 1 1 1 3 1 5 1 7 1 9 7 10 2 7 8 100 200
output
Copy
0 0 0 2 4 0 91
題意:求區間 [ l , r ] 之間有多少個x滿足 x%a%b!=x%b%a;
思路:首先滿足x%a%b==x%b%a這個性質的x一定是a,b的最小公倍數m +y ( y==max(a,b));那麼就求區間L~R之間不滿足這個性質的數有多少就行了;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll a,b,q,l,r;
cin>>a>>b>>q;
ll xx=max(a,b);
ll yy=a*b/__gcd(a,b);
while(q--)
{
cin>>l>>r;
ll x,y;
if(r%yy<xx) x=r/yy*(yy-xx);
else x=r/yy*(yy-xx)+(r%yy)-xx+1;
if((l-1)%yy<xx) y=(l-1)/yy*(yy-xx);
else y=(l-1)/yy*(yy-xx)+((l-1)%yy)-xx+1;
cout<<x-y<<" ";
}
cout <<endl;
}
return 0;
}