1342C Yet Another Counting Problem(数学,前缀和)

题目链接

题目大意

  问区间内\(((xmoda)modb)≠((xmodb)moda)\)\(x\)的个数。

解题思路

  因为模运算的性质,所以模数的结果肯定有一个长度为\(a\times b\)的循环节,算出这个循环节然后用前缀和就能解决了。

代码

const int maxn = 1e5+10;
ll tmp[maxn], ans[maxn];
int main(void) {
    int t; scanf("%d", &t);
    while(t--) {
        ll a, b; int q;
        scanf("%lld%lld%d", &a, &b, &q);
        ll n = a*b;
        for (int i = 1; i<=n; ++i) {
            tmp[i] = 0;
            tmp[i] += tmp[i-1];
            if (i%a%b!=i%b%a) ++tmp[i];
        }
        while(q--) {
            ll l,r;
            scanf("%lld%lld", &l, &r);
            --l;
            ll ans = r/n*tmp[n] + tmp[r%n];
            ans -= l/n*tmp[n] + tmp[l%n];
            printf("%lld\n", ans);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shuitiangong/p/12812695.html