容斥原理入门题吧。

Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9798   Accepted: 3341

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 relatively prime to 2006.

Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.

Input

The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).

Output

Output the K-th element in a single line.

Sample Input

2006 1
2006 2
2006 3

Sample Output

1
3
5

Source

 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <stdlib.h>
using namespace std; int m,k;
int mark[];
int save[];
int pcnt;
int g[];
long long int sum;
int cnt; void getprime()
{
//1不是素数
for(int i=;i<=;i++)
{
if(mark[i]==) continue;
save[pcnt++]=i;
for(int j=i;j<=;j+=i)
mark[j]=;
}
} void dfs(int n,long long num,int s,long long int key)
{
if(n==)
{
sum += key/num;
return ;
}
if(s>=cnt) return ; for(int i=s;i<cnt;i++)
{
if(num*g[i]>key) continue;
else dfs(n-,num*g[i],i+,key);
}
} long long int fuc(long long int x)
{
if(x==) return ; long long ans=; int sign=;
for(int i=;i<=cnt;i++)
{
sum=;
if(sign==)
{
dfs(i,,,x);
ans+=sum;
}
else
{
dfs(i,,,x);
ans-=sum;
}
sign=sign^;
} return x-ans;//这里面应该不会出现负数吧
} int main()
{
getprime();
while(scanf("%d%d",&m,&k)!=EOF)
{
//然后就是分解一个数了
cnt=;
for(int i=;i<=m;i++)
{
int flag=;
while(m%i==)
{
if(flag==)
{
g[cnt++]=i;
}
flag=;
m/=i;
}
}
if(m!=) g[cnt++]=m; //然后就是容斥原理 int b=,d=;
while(b<d)
{
int mid=(b+d)/;
int key=fuc(mid);
if(key>=k) d=mid;
else b=mid+;
}
printf("%d\n",b);
}
return ;
}

poj 2773(容斥原理)的更多相关文章

  1. Happy 2006 POJ - 2773 容斥原理+二分

    题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...

  2. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

  3. poj 2773 Happy 2006 - 二分答案 - 容斥原理

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11161   Accepted: 3893 Description Two ...

  4. [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)

    题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...

  5. poj 2773 Happy 2006 容斥原理+二分

    题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...

  6. POJ 2773 Happy 2006(容斥原理+二分)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10827   Accepted: 3764 Descr ...

  7. POJ 2773 Happy 2006 数学题

    题目地址:http://poj.org/problem?id=2773 因为k可能大于m,利用gcd(m+k,m)=gcd(k,m)=gcd(m,k)的性质,最后可以转化为计算在[1,m]范围内的个数 ...

  8. poj 2773 Happy 2006

    // 题意 :给你两个数 m(10^6),k(10^8) 求第k个和m互质的数是什么这题主要需要知道这样的结论gcd(x,n)=1 <==> gcd(x+n,n)=1证明 假设 gcd(x ...

  9. POJ 2773 Happy 2006(欧几里德算法)

    题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...

随机推荐

  1. openSSL漏洞原理及安全加固

    2014年4月8日晚,互联网爆出了又一重量级安全漏洞,即CVE-2014-0160,通俗来讲就是OpenSSL出现了安全漏洞. 说 这个漏洞前,先介绍一下OpenSSL,OpenSSL是一个强大的安全 ...

  2. AS3之正则表达式讲解

    限制输入内容 (一).my_txt.restrict = "A-Z 0-9";               仅允许在文本字段中输入大写字符.空格和数字 (二).my_txt.res ...

  3. Android App集成支付宝

    原地址:http://blog.csdn.net/wenbingoon/article/details/7933078 手机的在线支付,被认为是2012年最看好的功能,我个人认为这也是移动互联网较传统 ...

  4. Treap模板

    平衡树总是有用的,set由于过度封装没有办法实现找比x小的元素有多少个,这就显得很不方便了,所以封装了个Treap,万一以后用的着呢- -01 #pragma warning(disable:4996 ...

  5. GCD使用小结

    - ( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{         NSLog(; i < ; i ++) {         [self o ...

  6. javascript console

    javascript console console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格 ...

  7. poj 3317 Stake Your Claim 极大极小搜索

    思路:为了方便,当c1>c2时将0变为1,1变为0. 空格最多有10个,每个空格有3个状态,如果不状态压缩,会TLE的.所以最多有3^10种情况 代码如下: #include<iostre ...

  8. 离开csdn来到blog园

    csdn里没有限制阅读访问的功能,所以我选择来到cnblog 但是不得不说,cnblog做的界面很丑,我个人很不喜欢,但是没办法

  9. JSON.stringify 函数 (JavaScript)

    在bsrck项目中,使用jQuery.Form.js的ajaxSubmit时,遇到有文件上传的form提交,在firefox和chrome浏览器中测试,报Bad Request的错误,经查代码后发现是 ...

  10. UVA 10780 Again Prime? No Time. 分解质因子

    The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...