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<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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值