题目的愿意非常easy。给你一个n,求在升序排列的情况下,第k个与之相互素的数。


解法:首先我们要知道gcd(b×t+a,b)=gcd(a。b),那么接下来就非常easy了。全部与之互素的数都是以phi(n),为周期的,所以暴力求解就可以。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
#define N 1000010
#define pi acos(-1.0)
#define inf 100000000
typedef int ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b){
    if(b==0) return a;
    else return gcd(b,a%b);
}
ll p[N];
int main(){
    //freopen("in.txt","r",stdin);
    ll n,k;
    int e;
    while(~scanf("%d%d",&n,&k)){
        e=0;
        for(int i=1;i<=n;i++){
            if(gcd(i,n)==1)
              p[++e]=i;
        }
        printf("%d\n",p[(k-1)%e+1]+(k-1)/e*n);
    }
    return 0;
}
posted on 2017-08-07 14:11  yutingliuyl  阅读(172)  评论(0编辑  收藏  举报