hdu 5297 容斥原理

很容易想到我们需要这样一个函数f(n)表示的是1~n的数字中在y sequence中的个数,于是可以想到用容斥来做,先假设答案是n然后计算n中y sequence的个数,然后n加上不够的,继续判断,一直迭代求出答案。

小技巧:预处理的时候素数设为负数方便判断容斥的时候是应该加还是减。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 #include <cstdio>
 5 #include <cmath>
 6 using namespace std;
 7
 8 typedef long long ll;
 9 int p[] = {-2,-3,-5,-7,-11,-13,-17,-19,-23,-29,-31,-37,-41,-43,-47,-53,-59,-61,-67};
10 vector<int> v;
11
12 void init( int r )
13 {
14     v.clear();
15     for ( int i = 0; -p[i] <= r; i++ )
16     {
17         int bound = v.size();
18         for ( int j = 0; j < bound; j++ )
19         {
20             if ( abs( v[j] * p[i] ) <= 63 )
21             {
22                 v.push_back( v[j] * p[i] );
23             }
24         }
25         v.push_back(p[i]);
26     }
27 }
28
29 ll cal( ll n )
30 {
31     ll res = 0;
32     for ( int i = 0; i < v.size(); i++ )
33     {
34         ll tmp = pow( n + 0.5, 1.0 / abs(v[i]) ) - 1;
35         if ( v[i] < 0 ) res += tmp;
36         else res -= tmp;
37     }
38     return n - res - 1;
39 }
40
41 int main ()
42 {
43     int t;
44     scanf("%d", &t);
45     while ( t-- )
46     {
47         ll n, cp, tmp;
48         int r;
49         scanf("%I64d %d", &n, &r);
50         init(r);
51         cp = n, tmp = cal(cp);
52         while ( tmp < n )
53         {
54             cp += n - tmp;
55             tmp = cal(cp);
56         }
57         printf("%I64d\n", cp);
58     }
59     return 0;
60 }
时间: 2024-12-27 13:15:24

hdu 5297 容斥原理的相关文章

hdu 5297 Y sequence(容斥)

题目链接:hdu 5297 Y sequence 考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上.计算x为指数的不满足数直接pow(n,1/x)即可. #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; type

[2015hdu多校联赛补题]hdu 5297 Y sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给你一个所有正整数的序列,然后去掉满足x^(2~r)的所有数(x为所有正整数,r>=2题目给出),问你现在这个序列的第n个数是什么 解:首先想到写一个函数func(y),它可以计算出给定数字y是序列中第几个数,这样我们大概可以二分答案~(事实上会TLE,得用迭代法,当然迭代的话也是用这个函数) 那么如何实现func: 首先想去掉满足x^2的所有数,我们可以用pow(y, 1/2)计算出y

hdu 1695 容斥原理或莫比乌斯反演

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)

hdu 2597(容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297: 题意:给出n和r,求数列Y的第n个元素是多少.其中数列Y是正整数数列去除a^b(2<=b<=r)后的数 分析: 我的天啊,出一个数n,则1~n以内的正整数必定会有被去除的(假设被去除x1个),则这n个数中只有(n-x1)个数在Y数列中:如果想得到n个数在Y数列中,那么我们至少要在这n个数后面加上x1个数,设n1=n+x1,则在1~n1内的数有多少在Y数列中呢(假设有m个在Y数列中,去除了x

HDU 4336 容斥原理 || 状压DP

状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示什么都不取得概率,p(x1)表示的是取x1的概率,最后要加一因为有又多拿了一次.整理一下就可以了. 1 #include <cstdio> 2 const int Maxn=23; 3 double F[1<<Maxn],p[Maxn]; 4 int n; 5 int main() 6

hdu 4135 容斥原理

又搞了一道容斥原理. 题目:求[1,n]区间对m互质的数有多少个? #include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL __int64 const int maxn = 1e5+8; LL a[maxn],cn,numpri[maxn],vis[maxn]

HDU 1796 容斥原理

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7439    Accepted Submission(s): 2200 Problem Description Now you get a number N, and a M-integers set, you should

HDU 5297

用x ^ (1 / n) 来求个数,容斥原理 , Num会向后移动, 迭代到不再变化,退出循环 #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; const int Primes[] =

HDU 4059 容斥原理+快速幂+逆元

题目求1-n中与n互质的数的4次方之和,即S=a1^4+a2^4+……; a1,a2……均小于等于n且与n互质. 先求出1^4+2^4+……n^4然后减去与n不互质的数的4次方. 必然要先要用到4次方的求和公式.接下来简单的证明一下,这里前提是你知道3次方的公式,如果不会照下面的模式可以利用2次公式推出3次公式 (x+1)^5=x^5+5*x^4+10*x^3+10*x^2+5*x+1; 则 1=1; 2^5=(1+1)^5=1^5+5*1^4+10*1^3+10*1^2+5*1^1+1; 3^