其他分享
首页 > 其他分享> > cf1342 C. Yet Another Counting Problem(找规律,数论)

cf1342 C. Yet Another Counting Problem(找规律,数论)

作者:互联网

题意:

给定正整数 a 和 b,q次询问 \(l_i,r_i\) ,输出满足 \(l_i\le x \le r_i,(x\%a\%b)\neq (x\%b\%a)\) 的 \(x\) 的个数

a,b <= 200,l,r <= 1e18

思路:

\(x\%a\%b=(x+lcm(a,b))\%a\%b\),即 “每个数是否满足条件” 以 lcm 为周期。预处理 \([1,lcm]\) 即可。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll s[40005];
signed main()
{
int T; cin >> T; while(T--)
{
    int a, b, q; scanf("%d%d%d", &a, &b, &q);

    ll lcm = a * b / __gcd(a, b);

    for(int x = 1; x <= lcm; x++) //预处理成前缀和
        s[x] = s[x-1] + (x%a%b != x%b%a);

    while(q--)
    {
        ll l, r; scanf("%lld%lld", &l, &r);
        ll sl = (l-1)/lcm * s[lcm] + s[(l-1)%lcm];
        ll sr = r/lcm * s[lcm] + s[r%lcm];
        printf("%lld ", sr - sl);
    }

    puts("");
}

    return 0;
}

标签:le,int,ll,d%,long,Another,cf1342,Problem,lcm
来源: https://www.cnblogs.com/wushansinger/p/15730642.html