大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1

考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可

有奇数个的话, 余下一个2*p不划分, 其余全部划分

最后再将2的倍数全部划分一下即可

  1. #include <iostream>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <vector>
  7. #define x first
  8. #define y second
  9. #define pb push_back
  10. #define REP(i,a,n) for(int i=a;i<=n;++i)
  11. using namespace std;
  12. typedef pair<int,int> pii;
  13.  
  14. const int N = 1e5+10;
  15. int n;
  16. int vis[N], p[N];
  17. vector<pii> ans;
  18.  
  19. void seive(int n) {
  20. int mx = sqrt(n+0.5);
  21. REP(i,2,mx) if (!vis[i]) {
  22. for (int j=i*i; j<=n; j+=i) {
  23. vis[j] = 1;
  24. }
  25. }
  26. REP(i,3,n) if (!vis[i]) p[++*p]=i;
  27. }
  28.  
  29. int main() {
  30. scanf("%d", &n);
  31. seive(n/2);
  32. memset(vis, 0, sizeof vis);
  33. vector<int> ret;
  34. REP(i,1,*p) {
  35. vector<int> num;
  36. for (int t=p[i]; t<=n; t+=p[i]) {
  37. if (!vis[t]) num.pb(t);
  38. }
  39. while (num.size()>=2) {
  40. int x = num.back();
  41. num.pop_back();
  42. vis[x] = 1;
  43. int y = num.back();
  44. num.pop_back();
  45. vis[y] = 1;
  46. if (y==2*p[i]) {
  47. ret.pb(y);
  48. y = p[i];
  49. vis[y] = 1;
  50. }
  51. ans.pb(pii(x,y));
  52. }
  53. }
  54. for (int t=2; t<=n; t<<=1) ret.pb(t);
  55. while (ret.size()>=2) {
  56. int x = ret.back();
  57. ret.pop_back();
  58. int y = ret.back();
  59. ret.pop_back();
  60. ans.pb(pii(x,y));
  61. }
  62. printf("%d\n", int(ans.size()));
  63. for (auto t:ans) {
  64. printf("%d %d\n",t.x,t.y);
  65. }
  66. }

Jzzhu and Apples CodeForces - 449C (构造,数学)的更多相关文章

  1. Codeforces 772C 构造 数学 + dp + exgcd

    首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...

  2. Codeforces 450E:Jzzhu and Apples(构造,数学)

    E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...

  3. Codeforces 449C Jzzhu and Apples 贪心 (看题解)

    Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...

  4. Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces 449.C Jzzhu and Apples

    C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CF 450E Jzzhu and Apples 数学+模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  8. CF449 C. Jzzhu and Apples

    /* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...

  9. Alice, Bob, Oranges and Apples CodeForces - 586E

    E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...

随机推荐

  1. 微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉

    微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉 1.曾有人这样比喻:当你的粉丝超过100人时,你就像是一本内刊:超过1000人,你就像个布告栏:超过1万人,你就好比一本杂志:超过10万人,你就 ...

  2. Linux中Postfix反病毒和垃圾邮件工具(十)

    amavisd-new amavisd-new呼叫器是一个连接MTA和内容检测工具(诸如病毒扫描工具和SpamAssassin)的高性能接口程序,使用perl语言写成.它一般通过SMTP.ESMTP或 ...

  3. ”MySQL索引“学习总结

    序 learn by doing 是最快的学习方式.在百度外卖研发中心,我每天工作接触数据库方面最多的就是"索引",另外面试官在面试时也一定会考察到索引. Part 1, Expl ...

  4. 01: requests模块

    目录: 1.1 requests模块简介 1.2 使用requests模块发送get请求 1.3 使用requests模块发送post请求 1.4 requests.request()参数介绍 1.1 ...

  5. Vue 父组件循环使用refs调用子组件方法出现undefined的问题

    Vue 父组件循环使用refs调用子组件方法出现undefined的问题 1. 背景 最近前端项目遇到一个问题,我在父组件中使用了两个相同的子组件child,分别设置ref为add和update.其中 ...

  6. 如何在一台主机上管理自己的多个git repository

    在使用git时,通常是直接ssh-keygen生成默认秘钥.然后将共钥添加到远程仓库,就可以访问了. 但是,当我们有多个repository时,这种方式就不适用了,因为一个秘钥只能关联一个远程仓库. ...

  7. 51nod 1080 两个数的平方和

    没心情写数学题啦啊   好难啊 #include<bits/stdc++.h> using namespace std; set<int> s; set<int>: ...

  8. Codeforces Round #528 div1

    完了,看来上一次的flag要应验了,我大概是真的要掉成pupil了吧.. A - Connect Three 这个就是找到x的中间值,y的中间值,然后切一下,然后把所有的点挂到这条边上.但是我做的还是 ...

  9. 论文笔记:Mastering the game of Go with deep neural networks and tree search

    Mastering the game of Go with deep neural networks and tree search Nature 2015  这是本人论文笔记系列第二篇 Nature ...

  10. Java初始化块的作用

    1.使代码更简洁 eg: public class Test { private void init() { System.out.println("初始化状态"); } publ ...