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\n",Ans);
}
int main() {
    int T;
    cnt = 0;
    init();
    scanf("%d",&T);
    while(T--) {
        solve();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zxhl/p/5475495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值