一、题目

二、题目链接

  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$,就枚举二进制;

  1. #pragma GCC optimize(2)
  2. #pragma comment(linker, "/STACK:102400000, 102400000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. typedef long long LL;
  6. ;
  7.  
  8. template <class T> inline void read(T &x) {
  9. int t;
  10. bool flag = false;
  11. ')) ;
  12. if(t == '-') flag = true, t = getchar();
  13. x = t - ';
  14. + t - ';
  15. if(flag) x = -x;
  16. }
  17.  
  18. bool prime[MAXN];
  19. vector<LL> d[MAXN];//d[i]:数字i的所有素因子
  20.  
  21. void init() {
  22. ;
  23. ; i <= N; ++i)d[i].clear(), prime[i] = true;
  24. ; i <= N; ++i) {
  25. if(prime[i]) {
  26. for(int j = i + i; j <= N; j += i)prime[j] = false;
  27. }
  28. }
  29. ; i <= N; ++i) {
  30. if(prime[i]) {
  31. for(int j = i; j <= N; j += i)d[j].push_back(LL(i));
  32. }
  33. }
  34. }
  35.  
  36. /**[x+1, mid]**/
  37. LL calc(LL x, LL p, LL mid) {
  38. LL res = mid - x;
  39. /*枚举二进制:
  40. 如果i=5,二进制为0101,从低位数起,第0位和第2位为1,
  41. 那么就取d[p]的第0个和第2个素因子,计算乘积,去做容斥操作。
  42. 同时,需要统计取得素因子的个数。
  43. 如果是奇数,那么,容斥操作中符号是+,否则是-。
  44. */
  45. , t = << d[p].size(); i < t; ++i) {
  46. LL cnt = , prod = ;
  47. ; j > ; j >>= , ++k) {
  48. ) {
  49. cnt++;
  50. prod *= d[p][k];
  51. }
  52. }
  53. res -= (mid / prod - x / prod) * (cnt % == ? : -);
  54. }
  55. return res;
  56. }
  57.  
  58. int main() {
  59. #ifndef ONLINE_JUDGE
  60. freopen("Ginput.txt", "r", stdin);
  61. #endif // ONLINE_JUDGE
  62. LL T, x, p, k, low, high, mid, ans = , tmp;
  63. init();
  64. read(T);
  65. while(T--) {
  66. read(x), read(p), read(k);
  67. low = x, high = 1LL << ;/*这个地方要特别注意,写大了就超时。*/
  68. ) {
  69. mid = (low + high) / ;
  70. tmp = calc(x, p, mid);
  71. if(tmp < k)low = mid;
  72. else if(tmp > k)high = mid;
  73. else {
  74. if(__gcd(mid, p) > 1LL)high = mid;
  75. else {
  76. ans = mid;
  77. break;
  78. }
  79. }
  80. }
  81. printf("%lld\n", ans);
  82. }
  83. ;
  84. }

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

  1. #pragma GCC optimize(2)
  2. #pragma comment(linker, "/STACK:102400000, 102400000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. typedef long long LL;
  6. typedef tuple<LL, LL, LL> T3L;
  7. typedef pair<LL, LL> P2L;
  8. ;
  9.  
  10. template <class T> inline void read(T &x) {
  11. int t;
  12. bool flag = false;
  13. ')) ;
  14. if(t == '-') flag = true, t = getchar();
  15. x = t - ';
  16. + t - ';
  17. if(flag) x = -x;
  18. }
  19.  
  20. bool prime[MAXN];
  21. vector<LL> d[MAXN];/*d[i]:数字i的素因子*/
  22. vector<P2L> f[MAXN];/*f[i]:数字i的素因子组合的乘积和该组合的素因子个数*/
  23. vector<T3L> qs;
  24. void init() {
  25. ;
  26. qs.clear();
  27. ; i <= N; ++i)d[i].clear(), f[i].clear(), prime[i] = true;
  28. ; i <= N; ++i) {
  29. if(prime[i]) {
  30. for(int j = i + i; j <= N; j += i)prime[j] = false;
  31. }
  32. }
  33. ; i <= N; ++i) {
  34. if(prime[i]) {
  35. for(int j = i; j <= N; j += i)d[j].push_back(LL(i));
  36. }
  37. }
  38. }
  39.  
  40. /**[x+1, mid]**/
  41. LL calc(LL x, LL p, LL mid) {
  42. LL res = mid - x;
  43. ; i < f[p].size(); ++i) {/*直接枚举素因子组合的乘积*/
  44. LL prod = f[p][i].first, cnt = f[p][i].second;
  45. res -= (mid / prod - x / prod) * (cnt % == ? : -);
  46. }
  47. return res;
  48. }
  49.  
  50. int main() {
  51. #ifndef ONLINE_JUDGE
  52. freopen("Ginput.txt", "r", stdin);
  53. #endif // ONLINE_JUDGE
  54. LL T, x, p, k, low, high, mid, ans = , tmp;
  55. init();
  56. read(T);
  57. ; i < T; ++i) {
  58. read(x), read(p), read(k);
  59. qs.push_back(make_tuple(x, p, k));/*保存所有询问*/
  60. }
  61. /*预处理每个询问的p的素因子组合的乘积、组合个数。
  62. 写法同方法1。
  63. */
  64. ; t < T; ++t) {
  65. tmp = >(qs[t]);
  66. ) {
  67. , top = << d[tmp].size(); i < top; ++i) {
  68. LL cnt = , prod = ;
  69. ; j > ; j >>= , ++k) {
  70. ) {
  71. cnt++;
  72. prod *= d[tmp][k];
  73. }
  74. }
  75. f[tmp].push_back(make_pair(prod, cnt));
  76. }
  77. }
  78. }
  79. ; t < T; ++t) {
  80. x = >(qs[t]), p = >(qs[t]), k = >(qs[t]);
  81. low = x, high = 1LL << ;/*这个地方要特别注意,写大了就超时。*/
  82. ) {
  83. mid = (low + high) / ;
  84. tmp = calc(x, p, mid);
  85. if(tmp < k)low = mid;
  86. else if(tmp > k)high = mid;
  87. else {
  88. if(__gcd(mid, p) > 1LL)high = mid;
  89. else {
  90. ans = mid;
  91. break;
  92. }
  93. }
  94. }
  95. printf("%lld\n", ans);
  96. }
  97. ;
  98. }

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. msgsrvmgr.cpp:5:37: fatal error: kdl_conversions/kdl_msg.h: No such file or directory #include <kdl_conversions/kdl_msg.h>

    /home/xxx/ros_workspace/src/bp_protocol_bridge/protospot/src/msgsrvmgr.cpp::: fatal error: kdl_conve ...

  2. PHP自定义XML类实现数组到XML文件的转换

    这两天在公司写和各应用商店应用内搜索的接口,大致就像百度应用内搜索这样的东西,具体可以点下面的链接查看. 百度应用内搜索 有的应用商店需要JSON格式的数据,所以我只需要用下面的语句就可以返回对方服务 ...

  3. 递归--练习11--noi9273 PKU2506Tiling

    递归--练习11--noi9273 PKU2506Tiling 一.心得 25 a[i]%=10;(高精度时) 26 这里错了,花了好久改好 27 28 29 int* f(int n){ 30 if ...

  4. wpf--- TextBlock文字设置属性

    ProgressBar控件的重要属性:        FontFamily——控件中显示文本的字体        FontSize——控件中显示的字体的大小        Foreground——控件 ...

  5. python json与字典对象互相转换

    改文章转自:https://www.cnblogs.com/Lin-Yi/p/7640147.html 1 import requests 2 import json 3 ''' 4 json.loa ...

  6. Tornado的cookie过期问题

    首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...

  7. 个人知识管理系统Version1.0开发记录(10)

    物理分页 这次我们运用Mybatis拦截器来实现物理分页,后面会运用动态sql来实现,或者运用Map/CollectionUtils/StringUtils编写工具类来实现.oracle是运用的row ...

  8. Meteor.js异步全解

    翻译来源: http://phucnguyen.info/blog/everything-you-need-to-know-about-async-meteor/ posted in Web Deve ...

  9. 导入arr包

    提起项目的aar包 导入目标项目中 添加依赖 

  10. 433.92 TX RX module design and test recording。。

    This paper records the process of 433.92 TX RX module  design and test,fyi.  1 RX module The circuit ...