POJ 2773 Happy 2006 数学题
题目地址:http://poj.org/problem?id=2773
因为k可能大于m,利用gcd(m+k,m)=gcd(k,m)=gcd(m,k)的性质,最后可以转化为计算在[1,m]范围内的个数t。
1、AC代码:
开始的时候从1开始枚举if(gcd(n,i)==1),果断跑了2000ms
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cfloat>
using namespace std; typedef __int64 LL;
const int N=1000002;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); inline int in()
{
char ch = getchar();
int data = 0;
while (ch < '0' || ch > '9')
{
ch = getchar();
}
do
{
data=data*10+ch-'0';
ch=getchar();
}while(ch>='0'&&ch<='9');
return data;
} int xh[N]; int gcd(int n,int m)
{
int t;
while(m)
{
t=n%m;
n=m;
m=t;
}
return n;
} int main()
{
int i,j,n,k;
xh[1]=1;
while(cin>>n>>k)
{
if(n==1)
{
printf("%d\n",k);
continue;
}
int x=1;
for(i=2;i<n;i++)
{
if(gcd(n,i)==1)
xh[++x]=i;
}
int t=k%x,p=(k-1)/x;
if(t==0)
t=x;
printf("%d\n",p*n+xh[t]);
}
return 0;
}
2、AC代码
将于m互质的数记录下来。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cfloat>
using namespace std; typedef __int64 LL;
const int N=1000002;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); inline int in()
{
char ch = getchar();
int data = 0;
while (ch < '0' || ch > '9')
{
ch = getchar();
}
do
{
data=data*10+ch-'0';
ch=getchar();
}while(ch>='0'&&ch<='9');
return data;
} int xh[N];
LL pri[N],x;
bool vis[N]; void prime()//求素数
{
LL i,j;
x=0;
memset(vis,false,sizeof(vis));
for(i=2;i<N;i++)
{
if(!vis[i])
pri[++x]=i;
for(j=1;j<=x&&pri[j]*i<N;j++)
{
vis[pri[j]*i]=true;
if(i%pri[j]==0)
break;
}
}
} int main()
{
LL n,k,i,j;
prime();
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
LL q=n,sum=n;
if(n==1)
{
printf("%I64d\n",k);
continue;
}
memset(xh,0,sizeof(xh));
for(i=1;i<=x&&pri[i]*pri[i]<=n;i++)
{
if(n%pri[i]==0)
{
sum=sum*(pri[i]-1)/pri[i];
for(j=1;j*pri[i]<=q;j++)
xh[pri[i]*j]=1;//这个地方和上面的可能越界,所以要用__int64
}
while(n%pri[i]==0)
{
n/=pri[i];
}
}
if(n>1)
{
sum=sum*(n-1)/n;
for(j=1;j*n<=q;j++)
xh[j*n]=1;
}
//sum m以内与m互素的个数
LL t=k%sum,p=k/sum;
if(t==0)
{
t=sum;
p--;
}
LL temp=0;
for(i=1;i<=q;i++)
{
if(xh[i]==0)
temp++;
if(temp==t)
{
printf("%I64d\n",p*q+i);
break;
}
} }
return 0;
}
POJ 2773 Happy 2006 数学题的更多相关文章
- 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 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...
- [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)
题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...
- POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Descr ...
- POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...
- 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 容斥原理+二分
题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...
- POJ 2773 Happy 2006(欧几里德算法)
题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...
- Happy 2006 POJ - 2773 容斥原理+二分
题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...
随机推荐
- Android学习笔记:如何高效显示图片,避免内存溢出 和 ImageView无法显示大尺寸的图片
因为手机的内存资源是有限的,每个app可使用的内存是受限的.而现在采用高分辨率拍的照片往往很大.如果加载时不注意方法,很有可能会引起java.lang.OutofMemoryError: bitmap ...
- FLUSH TABLES WITH READ LOCK 锁全局
[root@wx03 ~]# cat a3.sh mysql -uroot -p1234567<<eof use scan; FLUSH TABLES WITH READ LOCK; sy ...
- HDU 4739 求正方形个数
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711707 求所有可能围成的正方形,借个代码 #include <que ...
- Swift编程语言学习11—— 枚举全局变量、局部变量与类型属性
全局变量和局部变量 计算属性和属性监视器所描写叙述的模式也能够用于全局变量和局部变量,全局变量是在函数.方法.闭包或不论什么类型之外定义的变量,局部变量是在函数.方法或闭包内部定义的变量. 前面章节提 ...
- SVN - 笔记
SVN(版本控制) 1.什么是SVN · 多人共同开发同一个项目,内部最大的问题是,在比较短的时间内如果有多人同时开发同一个文件,会造成彼此的代码相互覆盖的情况发生. · 管理着随时间改变的数据,这些 ...
- [转]mysql慢查询日志
原文链接:http://www.cnblogs.com/zhangjing0502/archive/2012/07/30/2615570.html 参考博文:http://blog.chinaunix ...
- nginx提示:500 Internal Server Error错误的解决方法
现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 ...
- 三、IF...ELSE和缩进
IF...ELSE和缩进 根据用户输入的不同做不同的事情 注意语法结尾的冒号. 例1: name = input("Please input your name:") if nam ...
- iOS开发之使用Ad Hoc进行测试
由于最近某个项目需要给别人测试,使用的是Ad Hoc方法 首先登录开发者官网配置证书 1.添加Certificates,从电脑获取certSigningRequest然后添加进去 2.在Identif ...
- LifecyclePhaseNotFoundException(转)
This error is generated if you try to invoke a build command that Maven does not understand. In gene ...