牛客练习赛43F Tachibana Kanade Loves Game

题目地址

Link

题解

这题其实就是求1~n中有多少与2~20互质的数,然后其实只跟1~20里面的质数有关。
那么考虑容斥一下求出来一共有多少个不互质的,用n减一下就是互质的数的个数了。然后判一下ans+k是否大于q即可。题解莫反反而麻烦了。本质思路是一样的。
复杂度是\(O(T*8*2^8)\)

#include <bits/stdc++.h>
using namespace std;

#define ll long long
int p[] = {2, 3, 5, 7, 11, 13, 17, 19};
int cnt, T, m;
ll k, q, n;

int main() {
    cin >> T;
    while(T--) {
        cnt = 0; 
        cin >> k >> q >> n >> m;
        if(n < q || !k) {puts("QAQ"); continue;}
        for(int i = 0; i < 8; ++i) if(p[i] <= m) ++cnt;
        ll ans = n;
        for(int S = 1; S < (1 << cnt); ++S) {
            int tot = 0; ll sum = 1;
            for(int i = 0; i < cnt; ++i) if((S >> i) & 1) ++tot, sum *= p[i];
            if(tot & 1) ans -= n / sum;
            else ans += n / sum;
        }
        if(ans + k > q) puts("Yes");
        else puts("QAQ");
    }
}

猜你喜欢

转载自www.cnblogs.com/henry-1202/p/10661383.html
今日推荐