容斥原理入门题吧。

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. web之困:现代web应用安全指南

    <web之困:现代web应用安全指南>在web安全领域有“圣经”的美誉,在世界范围内被安全工作者和web从业人员广为称道,由来自google chrome浏览器团队的世界顶级黑客.国际一流 ...

  2. 寒假222_Topcoder SRM648

    序号1: A,随便模拟好了,最后30秒发现一个都比的错误,无奈输错格式. 2: B,问你出去两个点,以及所产生的边 问你:产生多的联通快 加答案加1. 数据小,随便写暴力 3: copy思路. 我们先 ...

  3. Sqli-labs less 44

    Less-44 本关是基于盲注的,这里盲注主要是要没有报错信息,所以要采用盲注.这关与42关的区别就在于没有报错信息,同时,我们使用同样方式的payload: 登录 username:admin Pa ...

  4. 单选项框RadioGroup的综合应用

    大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只 ...

  5. eclipse创建python项目

    http://jingyan.baidu.com/article/19192ad8173300e53f570757.html

  6. android-non-ui-ui-thread-communications-part-5-5

    This is the last post in my series regarding Android thread communications.  Parts 1 through 4 are l ...

  7. 如何学好oracle?(准备)

    循序渐进 多练习 http://www.tudou.com/listplay/ScoGxMJZGQc/Nw9HE62XiGo.html

  8. lintcode :最长公共前缀

    题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP ...

  9. linux环境几个特殊的shell变量

    特殊的shell变量: $0  获取当前执行的shell脚本的文件名 $n  获取当前执行的shell脚本的第n个参数值,n=1..9 $*  获取当前shell的所有参数 “$1 $2 $3 …注意 ...

  10. Arraylist和Vector的区别与HashMap和Hashtable的区别

    1.ArrayList和HashMap都是线程异步的,所以它们的特点是效率高,但是安全性低: 2.Vector和Hashtable都是线程同步的,所以它们的特点是效率低,但是安全性高.