POJ-2773 Happy 2006(容斥,二分,dfs)

8 篇文章 0 订阅
2 篇文章 0 订阅

Happy 2006

Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9…are all relatively prime to 2006.

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
The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).
Output
Output the K-th element in a single line.
Sample Input
2006 1
2006 2
2006 3
Sample Output
1
3
5

题意:给你m和k,从小到大排序,问第k个与m互质的数是多少。

思路:首先打一个素数表,把m分解成素因数,然后我们可以通过容斥定理求出x以内与m互质的数的个数,然后我们通过二分来找x,使得find(x)=k,满足条件的x会有多个,最后循环递减找一下最小的满足条件的x就是答案。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 1000007;
const int MOD = 30021;
const double eps = 1e-8;
const double PI = acos(-1.0);

int prime[MAXN], primesize;
bool isprime[MAXN];
void getlist(int listsize)
{
    memset(isprime, 1, sizeof(isprime));
    isprime[1] = false;
    for (int i = 2; i <= listsize; i++)
    {
        if (isprime[i])prime[++primesize] = i;
        for (int j = 1; j <= primesize&&i*prime[j] <= listsize; j++)
        {
            isprime[i*prime[j]] = false;
            if (i%prime[j] == 0)break;
        }
    }
}

int t;
int num[10];

void solve(int x)
{
    t = 0;
    memset(num, 0, sizeof num);
    for (int i = 1; prime[i]*prime[i] <= x; i++)
    {
        if (x%prime[i] == 0)
            t++;
        while (x%prime[i]==0)
        {
            num[t] = prime[i];
            x /= prime[i];
        }
    }
    if (x != 1)
        num[++t] = x;
}

LL ans;

void dfs(LL k,LL place,LL mul,LL flag)
{
    if (place == t)
    {
        if (mul != 1)
            if (flag)ans += k / mul;
            else ans -= k / mul;
        return;
    }
    dfs(k, place + 1, mul, flag);
    dfs(k, place + 1, mul*num[place + 1], flag ^ 1);
}

LL find(LL x)
{
    ans = 0;
    dfs(x, 0, 1, 0);
    return x - ans;
}

LL Find(int x)
{
    LL mid;
    LL l = 1;
    LL r = 1LL << 60;
    while (l<r)
    {
        mid = (l + r) / 2;
        if (find(mid) == x)
            return mid;
        if (find(mid) > x)
            r = mid;
        else
            l = mid;
    }
}

int main()
{
    int m, K;
    LL s;
    getlist(1000003);
    while (scanf("%d%d",&m,&K)!=EOF)
    {
        solve(m);
        s=Find(K);
        while (find(s-1)==K)
            s--;
        printf("%lld\n", s);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值