poj_2773

本文介绍了一个简单的算法,用于找到与给定整数n升序排列下的第k个互质数。通过利用欧几里得算法计算最大公约数,并基于欧拉函数的性质发现互质数呈现周期性规律,从而实现快速查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目的愿意非常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;
}

转载于:https://www.cnblogs.com/yutingliuyl/p/7298829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值