• poj 2773 Happy 2006 容斥原理+二分


    题目链接

    容斥原理求第k个与n互质的数。

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    vector<int> v;
    void init(int n) {
        v.clear();
        for(int i = 2; i*i<=n; i++) {
            if(n%i==0) {
                v.pb(i);
            }
            while(n%i==0)
                n/=i;
        }
        if(n!=1)
            v.pb(n);
    }
    ll solve(ll x) {
        int len = v.size();
        ll ret = 0;
        for(int i = 1; i<(1<<len); i++) {
            ll cnt = 0, ans = 1;
            for(int j = 0; j<len; j++) {
                if((1<<j)&i) {
                    cnt++;
                    ans *= v[j];
                }
            }
            if(cnt&1) {
                ret += x/ans;
            } else {
                ret -= x/ans;
            }
        }
        return x-ret;
    }
    int main()
    {
        int n, m;
        while(cin>>n>>m) {
            init(n);
            ll l = 0, r = 1LL<<61, ans;
            while(r>=l) {
                ll mid = (l+r)/2;
                ll ret = solve(mid);
                if(ret>=m) 
                    r = mid-1;else
                    l = mid+1;
            }
            cout<<l<<endl;
        }
        return 0;
    }
  • 相关阅读:
    镜像转换:img转换为iso
    IDM的谷歌插件安装
    Adams和UG许可证冲突问题
    步进电机驱动总结
    比赛官网收集
    树莓派安装pip包管理工具
    光固化打印后处理过程
    Arduino 串口库函数
    六足蜘蛛机器人行走控制
    温湿度传感器DHT11程序示例
  • 原文地址:https://www.cnblogs.com/yohaha/p/5082996.html
Copyright © 2020-2023  润新知