• XTU 1242 Yada Number 容斥


    Yada Number

    Problem Description:

    Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even.

    For instance, 18=2 * 3 * 3 is not a yada number since the sum of amount of 2, 3 is 3, an odd number; while 170 = 2 * 5 * 17 is a yada number since the sum of amount of 2, 5 is 2, a even number that satifies the definition of yada number.

    Now, Duoxida wonders how many yada number are among all integers in [1,n].

    Input

    The first line contains a integer T(no more than 50) which indicating the number of test cases. In the following T lines containing a integer n. ()

    Output

    For each case, output the answer in one single line.

    Sample Input

    2
    18
    21

    Sample Output

    9
    11
    











    题意:

      给你一个n,问你1到n里面有多少个数满足 因子中是2,3,5,7,11,13的个数为偶数个

    题解:

       预处理出所有的x,满足x只含有2,3,5,7,11,3这几个质因子,且数目为偶数。x的数目略大于10000

      注意加入0个的情况,即1.

         对于一个数n,枚举所有的x,对于一个x,f(n/x)即求出[1,n/x]中不含有2,3,5,7,11,13作为因子的数有多少个,这个是经典的容斥问题。

       最后对所有的f(n/x)求和即可

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 3e6+20, M = 1e6+10, mod = 1e9+7,inf = 1e9;
    
    typedef long long ll;
    const ll maxn = 1e9;
    int cnt = 0, ans,n;
    ll b[N];
    int a[] = {2,3,5,7,11,13};
    ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
    void dfs(ll x,int f,int num) {
        if(num==6) {
            if(!f) b[cnt++] = x;
            return ;
        }
        while(x<=maxn) {
            dfs(x,f,num+1);
            x*=a[num];
            f^=1;
        }
    }
    void init() {
        dfs(1,0,0);
        sort(b,b+cnt);
    }
    
    void inclu(int i,int num,ll tmp) {
        if(tmp>n) return ;
        if(i>=6) {
            if(num==0) ans = 0;
            else {
                if(num&1) ans = ans+n/tmp;
                else ans = ans-n/tmp;
            }
            return ;
        }
        inclu(i+1,num,tmp);
        inclu(i+1,num+1,tmp*a[i]/gcd(tmp,a[i]));
    }
    
    void solve() {
        int Ans = 0;
        scanf("%d",&n);
        int tm = n;
        for(int i=0;i<cnt&&b[i]<=tm;i++) {
            n = tm/b[i];
            ans = 0;
            inclu(0,0,1);
            Ans+=(n - ans);
        }
        printf("%d
    ",Ans);
    }
    int main() {
        int T;
        cnt = 0;
        init();
        scanf("%d",&T);
        while(T--) {
            solve();
        }
        return 0;
    }
  • 相关阅读:
    8.26 树状数组
    8.27 神异之旅
    8.26 雇佣
    8.28 Jack与Rose
    8.28 ISN
    保存和加载网络
    快速搭建网络
    分类网络
    torch中的回归
    pytorch中的Variable
  • 原文地址:https://www.cnblogs.com/zxhl/p/5475495.html
Copyright © 2020-2023  润新知