题目链接:传送门 题目: B. Friends and Presents time limit per test second memory limit per test megabytes input standard input output standard output You have two friends. You want to present each of them several positive integers. You want to present cnt1 num…
2440: [中山市选2011]完全平方数 Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 W 想送一个数给他作为生日礼物.当然他不能送一个小X讨厌的数.他列出了所有小X不讨厌的数,然后选取了第 K个数送给了小X.小X很开心地收下了. 然而现在小 W 却记不起送给小X的是哪个数了.你能帮他一下吗? Input 包含多组测试数据.文…
题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12942   Accepted: 4557 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.…
题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数 s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数 整除的个数! 那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! #includ…
8493833                 2014-10-31 08:41:26     njczy2010     B - Friends and Presents             GNU C++     Accepted 31 ms 4 KB B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output st…
Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11458   Accepted: 4001 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a…
题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned about coprime numbers. A pair of numbers $(a, b)$ is called coprime if the maximum number that divides both $a$ and $b$ is equal to one. Your friend…
几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一种可行方案,或是让你求出满足条件的最小的k.这种问题叫做k染色问题.众所周知,当k>2时,k染色问题是NP的.但是相比$O(k^n)$的暴力搜索来说,人们还是找到了很多复杂度比较优越的指数级做法.本文简单介绍其中几种. 因为对于$O(n^22^n)$来说,$O(n^2)$小得可以忽略不计,所以在本文…
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/G 三.题意 给定一个$t$,表示有t次查询.每次查询给定一个$x$, $p$, $k$,需要输出一个大于$x$.与$p$互质.第$k$大的数字.如样例1所示,比$7$大.与$22$互质.第$1$大的数字是$9$,第$2$大的数字是$13$,第$3$大的数字是$15$. 四.思路 二分+容斥原理. 二分一个数字$mid$,看看$[x+1,mid]$之间与$p$互质的数的个数.需要注意的是,…
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Status][Discuss] Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 W 想送一个数给他作为生日礼物.当然他不能送一个小X讨厌…
http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1,m]中与m互质的第k个数,先打表,找到m的所有质因数,然后用二分实现,最开始区间为[1,2^60],利用容斥原理去找区间[1,mid]内素数的个数t,不断进行二分,直到所查找的区间[l,r]内素数的个数t等于k,mid=l=r,则此时的l就是第k个与m互质的数. #include<iostream>…
传送门 题意简述:qqq次询问(q≤500)(q\le500)(q≤500),每次问第kkk个不被除111以外的完全平方数整除的数是多少(k≤1e9)(k\le1e9)(k≤1e9). 思路:考虑二分答案为xxx,然后用容斥原理来解决,ans=n−只有一个质数因子次数大于等于2的个数+只有2个质数因子大于等于2的个数−...ans=n-只有一个质数因子次数大于等于2的个数+只有2个质数因子大于等于2的个数-...ans=n−只有一个质数因子次数大于等于2的个数+只有2个质数因子大于等于2的个数−…
Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11161   Accepted: 3893 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relative…
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to…
Coprime Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 849    Accepted Submission(s): 232 Problem Description Please write a program to calculate the k-th positive integer that is coprime with…
题目大意:求第k个无平方因子数是多少(无视原题干.1也是全然平方数那岂不是一个数也送不出去了? 无平方因子数(square-free number),即质因数分解之后全部质因数的次数都为1的数 首先二分答案 问题转化为求x以内有多少个无平方因子数 依据容斥原理可知 对于√x以内的全部质数 x以内的无平方因子数=无需是不论什么质数的倍数的数的数量(即x)-是至少一个质数平方倍数的数的数量+是至少两个质数平方倍数的数的数量-是至少三个质数平方倍数的数的数量... 我们回去考虑莫比乌斯函数,我们发现每…
题目链接:5198. 丑数 III 请你帮忙设计一个程序,用来找出第 n 个丑数. 丑数是可以被 a 或 b 或 c 整除的 正整数. 示例 1: 输入:n = 3, a = 2, b = 3, c = 5 输出:4 解释:丑数序列为 2, 3, 4, 5, 6, 8, 9, 10- 其中第 3 个是 4. 示例 2: 输入:n = 4, a = 2, b = 3, c = 4 输出:6 解释:丑数序列为 2, 3, 4, 6, 8, 9, 12- 其中第 4 个是 6. 示例 3: 输入:n…
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些 数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而 这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 W 想送一个数给他作为生日礼物.当然他不能送一 个小X讨厌的数.他列出了所有小X不讨厌的数,然后选取了第 K个数送给了 小X.小X很开心地收下了. 然而现在…
题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<math.h> 6 #include<queue> 7 using…
先二分答案m,<=m的有m-∑(m/pi*pi)+∑(m/pi*pi*pj*pj)-……个符合题意的(容斥原理), 容斥系数就是莫比乌斯函数μ(预处理)... ------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<cmath>   us…
题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue&…
题意:求第$n \le 10^{10}$个不是无平方因子数 二分答案, 容斥一下,0个质数的平方因子-1个..... 枚举$\sqrt$的平方因子乘上莫比乌斯函数,最后求出无平方因子数的个数取补集 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int…
483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset((a),(b),sizeof(a)) const ll INF=1e18; ll c1,c2,…
http://www.lydsy.com/JudgeOnline/problem.php?id=2440 我觉得网上很多题解都没说清楚...(还是我太弱了? 首先我们可以将问题转换为判定性问题,即给出一个数$x$,有多少个小于$x$等于的数是不能分解的,即不是完全平方数(不包括1). 而每个数都可以写成质数积,那么显然只要质数的平方的倍数就可以代替所有数的平方的倍数. 考虑质数个数,假设质数集$P$,那么根据容斥原理,在$[1, x]$范围内的整数不能能分解的个数有: $$x - (A_{p_1…
[题目大意] 求第k个不是完全平方数或完全平方数整数倍的数. [思路] 由于μ(i)*(n/i^2)=n,可以直接从1开始,得出非完全平方数/完全平方数倍数的数的个数 注意一下二分的写法,这里用的是我一直比较喜欢的一种二分写法: int lb=下界-1,ub=上界 while (ub-lb>1) { int mid=(lb+ub)>>1; if (a[mid]>=k) ub=mid; else lb=mid; } ans=ub; #include<iostream> #…
https://codeforc.es/contest/920/problem/G G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y)…
Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10827   Accepted: 3764 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a…
题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> #include<cstdio> #include<cstring> #include<iostream> using namespace std; ; +; int m,k,cnt; int a[N],p[N]; void getprime(int x) { cnt=; ;…
可以把每头牛看作一个位置,有几对牛可以放置相当于有几对位置可以给它睡觉,没有牛可以在其他牛的位置睡觉,所以有几对牛放置的可能答案就乘多少(相当于在原本的两个集合里分别插入一个元素,元素代表它睡觉的位置) 容斥的时候,第二遍会把当前位置i+1这个点没有牛在睡觉的情况全部去掉,尝试写了不容斥,直接计算当前枚举位置i一定有牛睡觉的方法,发现巨麻烦无比,遂放弃55555 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespa…
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to…