C. Yet Another Counting Problem

time limit per test 3.5 seconds
memory limit per test 256 megabytes
input
standard input
output
standard output
思路:算出a,b的最小公倍数n,再判断0-n是否满足条件,则前i个数共有cnt[i]个符合条件的个数,后续的数段都可以从前面推出。

You are given two integers aa and bb , and qq queries. The i -th query consists of two numbers li and ri , and the answer to it is the number of integers x such that lixri , and ((xmoda)modb)((xmodb)moda). Calculate the answer for each query.

Recall that ymodz is the remainder of the division of y by z . For example, 5mod3=2 , 7mod8=7 , 9mod4=1 , 9mod9=0 .

Input

The first line contains one integer t (1t100 ) — the number of test cases. Then the test cases follow.

The first line of each test case contains three integers aa , bb and qq (1a,b200 ; 1≤q≤500 ).

Then q lines follow, each containing two integers lili and riri (1liri1018 ) for the corresponding query.

Output

For each test case, print q 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 
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
const int N=4e4+5;
int cnt[N];

int main(){
ios::sync_with_stdio(0);
ll t,a,b,q,l,r;
cin>>t;
while(t--){
    cin>>a>>b>>q;
    ll n=a*b/__gcd(a,b);
    for(ll i=0;i<n;i++){
        cnt[i+1]=cnt[i];
        if(i%a%b!=i%b%a)
            cnt[i+1]++;
    }
    //cout<<cnt[n]<<endl;
    while(q--){
        cin>>l>>r;
        r++;
        cout<<(r/n-l/n)*cnt[n]-cnt[l%n]+cnt[r%n]<<' ';
    }
    cout<<endl;
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/asunayi/p/12799314.html