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;
}
时间: 2024-10-05 19:27:29

XTU 1242 Yada Number 容斥的相关文章

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 no

xtu 1242 Yada Number 打表

Yada Number       Time Limit : 2000 MS   Memory Limit : 65536 KB 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,

ZOJ 3233 Lucky Number 容斥

给你a数组和b数组 求x到y之间有多少个数至少被a中一个数整除并且至少不被b中一个数整除 容斥第一问很简单 第二问可以考虑反面 设满足被a中至少一个数整除的数有sum1个 在被a中至少一个数整除的前提下 被b中所有数整除的数有sum2 答案就是sum1-sum2 在dfs的时候溢出了 借鉴了某大牛的方法 #include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int

双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good). 思路: 真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样. ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3). ANS就是:全排列 -

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

hdu 5471(状压DP or 容斥)

想了最复杂的思路,用了最纠结的方法,花了最长的时间,蒙了一种规律然后莫名其妙的过了. MD 我也太淼了. 后面想了下用状压好像还是挺好写的,而且复杂度也不高.推出的这个容斥的规律也没完全想透我就CAO. Count the Grid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 400    Accepted Submission(s)

HDU 4135 Co-prime(组合+容斥)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other tha

hdu-5794 A Simple Chess(容斥+lucas+dp)

题目链接: A Simple Chess Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description There is a n×m board, a chess want to go to the position (n,m) from the position (1,1).The chess is able to go to position (