题目地址: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 数学题的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 ...

  6. poj 2773 Happy 2006

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

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

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

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

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

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

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

随机推荐

  1. Hdu 1158 Employment Planning(DP)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1158 一道dp题,或许是我对dp的理解的还不够,看了题解才做出来,要加油了. 只能先上代码了 ...

  2. jQuery 快速结束当前动画

    当需要快速结束一个当前正在执行的jquery 动画时(还没执行完成),如 fadeOut(),可以在执行当前动画的对象上执行 stop(true);方法 如: <script type=&quo ...

  3. linux下利用sed重命名文件

    3 for file in `ls ./*.*`  4 do  5 mv $file `echo $file|sed 's/IM_21R_ID331/1M21R_ID331/g'`  6 done  ...

  4. linux popen函数

    描述 popen() 函数 用 创建管道 的 方式启动一个 进程, 并调用 shell. 因为 管道是被定义成单向的, 所以 type 参数 只能定义成 只读或者 只写, 不能是 两者同时, 结果流也 ...

  5. haproxy path_beg

    path_beg : prefix match 前缀匹配 path_dir : subdir match path_dom : domain match path_end : suffix match ...

  6. 存几个html画图的网站

    http://jvectormap.com/ http://julying.com/lab/raphael-js/docs/#Paper.path http://www.highcharts.com/ ...

  7. Android---60---Notification 通知栏的简单使用

    Notification是显示在手机状态栏的通知 通过Notification.Builder类创建Notification对象. Notification.Builder经常用法: setDefau ...

  8. mysql 初识之日志文件篇

    日志文件 1. err日志     error log 记录mysql在运行的过程中所有较为严重的警告和错误信息,以及mysql server每次启动和关闭的详细信息.系统在默认情况下关闭error ...

  9. C#中对文件的操作

    详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801 源码举例: public class FileSystemManager { ...

  10. Apache Thrift的简单使用

    Apache Thrift的简单使用 ---------------------- 1. 简介 Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架.它有一个代码生成器来对它所 ...