POJ 2773 Happy 2006 (二分答案+容斥)

题目链接:http://poj.org/problem?id=2773

题意:

求第k个与m互质的数;

分析:

很明显随着数的增大与m互质的数就越多,因此我们可以二分答案,

中间需要用到容斥原理求[1,mid]内与m互质的数的个数;

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

const int maxn = 100;
typedef long long LL;
int m,k,cnt;
int p[maxn];

void fen(int m)//对m因子分解
{
    cnt=0;
    for(int i=2;i*i<=m;i++){
        if(m%i==0){
            p[cnt++]=i;
            while(m%i==0)
                m/=i;
        }
    }
    if(m>1) p[cnt++]=m;
}

LL calu(LL n)//容斥计数
{
    LL sum=0;
    for(LL i=1;i<(1<<cnt);i++){
        LL mult=1,f=0;
        for(int j=0;j<cnt;j++){
            if(i&(1<<j)){
                f++;
                mult*=p[j];
            }
        }
        if(f&1) sum+=n/mult;
        else sum-=n/mult;
    }
    return n-sum;
}

int main()
{
    while(~scanf("%d%d",&m,&k)){
        LL l=0,r=((LL)1<<62);
        LL mid,ans=0;
        fen(m);
        while(l<=r){//二分答案
            mid=(l+r)/2;

            LL sum = calu(mid);
            if(sum>=k){
                r=mid-1;
                if(sum==k) ans=mid;
            }
            else  l=mid+1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-12-21 14:24:33

POJ 2773 Happy 2006 (二分答案+容斥)的相关文章

POJ 2773 Happy 2006 二分+容斥(入门

题目链接:点击打开链接 题意: 输入n ,k 求与n互质的第k个数(这个数可能>n) 思路: solve(mid)表示[1,mid]中有多少个和n互质,然后二分一下最小的mid 使得互质个数==k solve(x) 实现: 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description 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 a

[二分+容斥原理] poj 2773 Happy 2006

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.

POJ 2773 Happy 2006

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9170   Accepted: 3092 Description 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 al

POJ 3258 River Hopscotch 二分答案

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6193   Accepted: 2685 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. T

BZOJ2440(完全平方数)二分+莫比乌斯容斥

题意:完全平方数是指含有平方数因子的数.求第ki个非完全平方数. 解法:比较明显的二分,getsum(int middle)求1-middle有多少个非完全平方数,然后二分.求1-middle的非完全平方数个数可以用总数减掉完全平方数个数.计算完全平方数的个数用容斥: 首先加上n/(2*2)+n/(3*3)+n/(5*5)+n/(7*7)...+...然后减掉出现两次的,然后加上三次的...奇加偶减.这就是mou的原型,用mou数组计算很简单: 代码: /********************

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ 3273 Monthly Expense 二分答案

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13281   Accepted: 5362 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and r

poj 2773 Happy 2006(欧拉函数应用)

http://poj.org/problem?id=2773 题意:输入n,k,求与n不互素的第k个数,k可能大于n. 思路:以n=6为例,与6互素的数有一定规律.{1,5},{7,12},{13,18}......,发现在[1,n],[n+1,n*2]......[m*n+1,(m+1)*n]区间内素数个数相同,且对应位置的数都相差n的整数倍.因此只要求出[1,n]内的与n互素的数即可.这个过程没必要一个一个枚举,可以用欧拉函数解决.因为欧拉函数已经求出了n的所有质因子,与n不互素的数都与n有