一、题目

二、题目链接

  http://codeforces.com/contest/920/problem/G

三、题意

  给定一个$t$,表示有t次查询。每次查询给定一个$x$, $p$, $k$,需要输出一个大于$x$、与$p$互质、第$k$大的数字。如样例1所示,比$7$大、与$22$互质、第$1$大的数字是$9$,第$2$大的数字是$13$,第$3$大的数字是$15$。

四、思路

  二分+容斥原理。

  二分一个数字$mid$,看看$[x+1,mid]$之间与$p$互质的数的个数。需要注意的是,如果个数是$k$,还要判断二分的$mid$是不是与$p$互质。如果是,说明要输出的值在$[x+1, mid)$之间,要需要继续做二分。(注意括号样式哦)

  统计$[x+1, mid]$之间与$p$互质的数的个数,方法是:把这个区间内$p$的素因子的倍数全部筛掉,剩余的就是与$p$互质的了。在筛的过程中,比如$p$是$12$,素因子是$2$、$3$,筛掉$2$的倍数的方法是:区间长度$len - (mid / 2 - x / 2)$。同理,筛掉$3$的倍数的方法也一样。但是,对于所有$6$的倍数,会被减两次,所以,需要再加一次。而这个过程,其实就是容斥原理。

  在这个题目中,利用容斥原理统计个数时,有两个写法:

    1、每次二分一个上界值$mid$,就枚举二进制;

    2、保存所有的询问,对每个询问的$p$,都预处理出它的所有素因子组合的乘积。然后,再枚举每一个询问,传入二分上界值$mid$时,枚举预处理的$p$的所有素因子组 合的乘积即可。(详细的写法可参考源代码)

  另外,还有一点,在做询问之前,需要预处理出$[1, 1e6]$区间内所有数字的素因子。

五、源代码

  1、写法一:每次二分一个上界值$mid$,就枚举二进制;

#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
;

template <class T> inline void read(T &x) {
    int t;
    bool flag = false;
    ')) ;
    if(t == '-') flag = true, t = getchar();
    x = t - ';
     + t - ';
    if(flag) x = -x;
}

bool prime[MAXN];
vector<LL> d[MAXN];//d[i]:数字i的所有素因子

void init() {
    ;
    ; i <= N; ++i)d[i].clear(), prime[i] = true;
    ; i <= N; ++i) {
        if(prime[i]) {
            for(int j = i + i; j <= N; j += i)prime[j] = false;
        }
    }
    ; i <= N; ++i) {
        if(prime[i]) {
            for(int j = i; j <= N; j += i)d[j].push_back(LL(i));
        }
    }
}

/**[x+1, mid]**/
LL calc(LL x, LL p, LL mid) {
    LL res = mid - x;
    /*枚举二进制:
        如果i=5,二进制为0101,从低位数起,第0位和第2位为1,
        那么就取d[p]的第0个和第2个素因子,计算乘积,去做容斥操作。
        同时,需要统计取得素因子的个数。
        如果是奇数,那么,容斥操作中符号是+,否则是-。
    */
    , t =  << d[p].size(); i < t; ++i) {
        LL cnt = , prod = ;
        ; j > ; j >>= , ++k) {
            ) {
                cnt++;
                prod *= d[p][k];
            }
        }
        res -= (mid / prod - x / prod) * (cnt %  ==  ?  : -);
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Ginput.txt", "r", stdin);
#endif // ONLINE_JUDGE
    LL T, x, p, k, low, high, mid, ans = , tmp;
    init();
    read(T);
    while(T--) {
        read(x), read(p), read(k);
        low = x, high = 1LL << ;/*这个地方要特别注意,写大了就超时。*/
        ) {
            mid = (low + high) / ;
            tmp = calc(x, p, mid);
            if(tmp < k)low = mid;
            else if(tmp > k)high = mid;
            else {
                if(__gcd(mid, p) > 1LL)high = mid;
                else {
                    ans = mid;
                    break;
                }
            }
        }
        printf("%lld\n", ans);
    }
    ;
}

  2、保存所有的询问,对每个询问的$p$,都预处理出它的所有素因子组合的乘积。然后,再枚举每一个询问,传入二分上界值$mid$时,枚举预处理的$p$的所有素因子组 合的乘积;

#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef tuple<LL, LL, LL> T3L;
typedef pair<LL, LL> P2L;
;

template <class T> inline void read(T &x) {
    int t;
    bool flag = false;
    ')) ;
    if(t == '-') flag = true, t = getchar();
    x = t - ';
     + t - ';
    if(flag) x = -x;
}

bool prime[MAXN];
vector<LL> d[MAXN];/*d[i]:数字i的素因子*/
vector<P2L> f[MAXN];/*f[i]:数字i的素因子组合的乘积和该组合的素因子个数*/
vector<T3L> qs;
void init() {
    ;
    qs.clear();
    ; i <= N; ++i)d[i].clear(), f[i].clear(), prime[i] = true;
    ; i <= N; ++i) {
        if(prime[i]) {
            for(int j = i + i; j <= N; j += i)prime[j] = false;
        }
    }
    ; i <= N; ++i) {
        if(prime[i]) {
            for(int j = i; j <= N; j += i)d[j].push_back(LL(i));
        }
    }
}

/**[x+1, mid]**/
LL calc(LL x, LL p, LL mid) {
    LL res = mid - x;
    ; i < f[p].size(); ++i) {/*直接枚举素因子组合的乘积*/
        LL prod = f[p][i].first, cnt = f[p][i].second;
        res -= (mid / prod - x / prod) * (cnt %  ==  ?  : -);
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Ginput.txt", "r", stdin);
#endif // ONLINE_JUDGE
    LL T, x, p, k, low, high, mid, ans = , tmp;
    init();
    read(T);
    ; i < T; ++i) {
        read(x), read(p), read(k);
        qs.push_back(make_tuple(x, p, k));/*保存所有询问*/
    }
    /*预处理每个询问的p的素因子组合的乘积、组合个数。
        写法同方法1。
    */
    ; t < T; ++t) {
        tmp = >(qs[t]);
        ) {
            , top =  << d[tmp].size(); i < top; ++i) {
                LL cnt = , prod = ;
                ; j > ; j >>= , ++k) {
                    ) {
                        cnt++;
                        prod *= d[tmp][k];
                    }
                }
                f[tmp].push_back(make_pair(prod, cnt));
            }
        }
    }
    ; t < T; ++t) {
        x = >(qs[t]), p = >(qs[t]), k = >(qs[t]);
        low = x, high = 1LL << ;/*这个地方要特别注意,写大了就超时。*/
        ) {
            mid = (low + high) / ;
            tmp = calc(x, p, mid);
            if(tmp < k)low = mid;
            else if(tmp > k)high = mid;
            else {
                if(__gcd(mid, p) > 1LL)high = mid;
                else {
                    ans = mid;
                    break;
                }
            }
        }
        printf("%lld\n", ans);
    }
    ;
}

Educational Codeforces Round 37-G.List Of Integers题解的更多相关文章

  1. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  2. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  3. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  4. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  5. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  6. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  9. Educational Codeforces Round 37 (Rated for Div. 2) G

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  10. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. LA 7278 Game of Cards(SG函数)

    https://vjudge.net/problem/UVALive-7278 题意: 两个人玩游戏,现在有n堆牌,轮到自己时,先在牌堆中选一堆牌,先在牌堆中选择拿走0~k张牌(至少得剩下一张),然后 ...

  2. Java - PriorityQueue

    JDK 10.0.2 前段时间在网上刷题,碰到一个求中位数的题,看到有网友使用PriorityQueue来实现,感觉其解题思想挺不错的.加上我之前也没使用过PriorityQueue,所以我也试着去读 ...

  3. Looper.loop() android线程中的消息循环

    Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建一个消息循环,调用Loope ...

  4. MVP框架模式

    一.基本概念 MVP是Model-View-Presenter的简称,即模型-视图-表现层的缩写.MVP是由MVC模式进化而来的,MVP改进了MVC中的控制器过于臃肿的问题.与MVC一样,MVP将应用 ...

  5. 在Firefox中发现一个在Linux下查看chm文档的插件

    在Firefox浏览器插件中搜索插件chmfox插件,安装后就可以在linux下通过Firefox浏览器阅读chm文档了.

  6. hdu 6158 The Designer( 反演圆)

    The Designer Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. iOS导航栏的正确隐藏方式

    在项目中经常碰到首页顶部是无限轮播,需要靠最上面显示.有的设置导航栏为透明等一系列的方法,这个可以借助第三方.或者干脆简单粗暴的直接隐藏掉导航栏.可是push到下一个页面的时候是需要导航栏的,如何做了 ...

  8. 通过命令启动一个activity(am pm 命令)

    一.am的含义是activityManager 主要作用是启动activity.service .broadcast    1.通过adb命令启动acitvity,首先需要设置activity 的 e ...

  9. 怎样使用visio画数据库模型图

    怎样使用visio画数据库模型图     计算机专业的学生在做程序的需求分析文档或者毕业设计的时候,针对自己开发的系统,肯定少不了要画数据库的模型图.许多同学喜欢用word实现,当然,word可以满足 ...

  10. leetcode122 买卖股票的最佳时机 python

    题目:给定一个数组,它表示了一只股票的价格浮动,第i个元素代表的是股票第i天的价格.设计一个函数,计算出该股票的最大收益,注意,可以多次买入卖出,但下一次买入必须是在本次持有股票卖出之后.比如[1,7 ...