poj 2773(容斥原理)
容斥原理入门题吧。
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 9798 | Accepted: 3341 |
Description
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
Output
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(容斥原理)的更多相关文章
- Happy 2006 POJ - 2773 容斥原理+二分
题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...
- POJ 2773 Happy 2006#素数筛选+容斥原理+二分
http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...
- poj 2773 Happy 2006 - 二分答案 - 容斥原理
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11161 Accepted: 3893 Description Two ...
- [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)
题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...
- poj 2773 Happy 2006 容斥原理+二分
题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...
- POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Descr ...
- POJ 2773 Happy 2006 数学题
题目地址:http://poj.org/problem?id=2773 因为k可能大于m,利用gcd(m+k,m)=gcd(k,m)=gcd(m,k)的性质,最后可以转化为计算在[1,m]范围内的个数 ...
- poj 2773 Happy 2006
// 题意 :给你两个数 m(10^6),k(10^8) 求第k个和m互质的数是什么这题主要需要知道这样的结论gcd(x,n)=1 <==> gcd(x+n,n)=1证明 假设 gcd(x ...
- POJ 2773 Happy 2006(欧几里德算法)
题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...
随机推荐
- uiview 单边圆角或者单边框
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)]; view2.backgroundColor = ...
- DB2 的create or update方法
通过merge方法实现的: MERGE INTO IFEBASE.STYLE AS MT USING (SELECT :scenario AS SCENARIO_ID, :style AS SHAPE ...
- CKEditor (Toolbar Definition)工具栏自定义配置
JS是大小写敏感的, 在设置配置文件的时候需要注意 以CKEditor 4为基础我们可以通过两种方式配置CKEditor的工具栏,一种是是通过config.js配置文件设置, 另一种是IN-PAGE方 ...
- jquery easyui tab加载内容的几种方法
转:http://my.oschina.net/u/2331760/blog/391937?fromerr=saqeoxxB jQuery Easyui 的tabs插件有两种方式加载某个tab(标签页 ...
- Python编程指南 chapter 1
1.python使用方括号[]来存取一个序列中的某个数据项,像字符串.列表等包含若干数据项的序列都采用这种方法. 2.强制类型转换,int('24234'),str(235) 3.python中没有变 ...
- HDU 4496 D-City(并查集,逆思维)
题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...
- iOS警告-Warning: Error creating LLDB target at path(模拟器警告)
Warning: Error creating LLDB target at path '/Users/apple/Library/Developer/Xcode/DerivedData/Observ ...
- POJ 2105
#include <iostream> #include <cmath> #include <string> using namespace std; int ma ...
- 虚拟专用网络VPN
寒假回到家里需要下载论文,怎样才能访问学校图书馆的数据库呢?解决方法是学校图书馆在内网中架设一台VPN服务器,VPN服务器有两块网卡,一块连接内网,一块连接公网.然后就可以通过互联网找到VPN服务器, ...
- (转)STL中set的用法
转载自here 1.关于set map容器是键-值对的集合,好比以人名为键的地址和电话号码.相反地,set容器只是单纯的键的集合.例如,某公司可能定义了一个名为bad_checks的set容器,用于记 ...