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

链接:https://codeforces.com/contest/1342/problem/C

题意:给定两个数a、b, 再给 q 个范围 l、r,问在 l~r 的范围中有多少个数x是 x % a % b != x % b % a ,输出符合条件的数的个数

显然,求余是周期性的,比如,(1-20)%4,余数1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0;

所以a,b具有这样的性质,a*b就是周期,然后将一个周期内符合条件的数统计一下,乘以周期数即可,前缀和预处理,

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+5;
int pre[N];
int gcd(int a,int b)
{
   return b==0?a:gcd(b,a%b);
}
ll cal(ll num,int n)
{
   return pre[n-1]*(num/n)+pre[num%n];
}
void solve()
{
   int a,b,q;
   ll l,r;
   cin>>a>>b>>q;
   int n=a*b;
   for(int i=1; i<n; i++)
   {
      pre[i]=pre[i-1];
      if(i%a%b!=i%b%a)
       pre[i]++;
   }
   while(q--)
   {
      cin>>l>>r;
      cout<<cal(r,n)-cal(l-1,n)<<" ";
   }
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--) solve();
   //system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/sweetlittlebaby/p/12789197.html