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

整理的算法模板: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的最小公倍数+y ( y==max(a,b));那么就求区间L~R之间不满足这个性质的数有多少就行了;

#include
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

 

你可能感兴趣的:(基础算法——数论)