Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1
考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可
有奇数个的话, 余下一个2*p不划分, 其余全部划分
最后再将2的倍数全部划分一下即可
- #include <iostream>
- #include <math.h>
- #include <string.h>
- #include <algorithm>
- #include <cstdio>
- #include <vector>
- #define x first
- #define y second
- #define pb push_back
- #define REP(i,a,n) for(int i=a;i<=n;++i)
- using namespace std;
- typedef pair<int,int> pii;
- const int N = 1e5+10;
- int n;
- int vis[N], p[N];
- vector<pii> ans;
- void seive(int n) {
- int mx = sqrt(n+0.5);
- REP(i,2,mx) if (!vis[i]) {
- for (int j=i*i; j<=n; j+=i) {
- vis[j] = 1;
- }
- }
- REP(i,3,n) if (!vis[i]) p[++*p]=i;
- }
- int main() {
- scanf("%d", &n);
- seive(n/2);
- memset(vis, 0, sizeof vis);
- vector<int> ret;
- REP(i,1,*p) {
- vector<int> num;
- for (int t=p[i]; t<=n; t+=p[i]) {
- if (!vis[t]) num.pb(t);
- }
- while (num.size()>=2) {
- int x = num.back();
- num.pop_back();
- vis[x] = 1;
- int y = num.back();
- num.pop_back();
- vis[y] = 1;
- if (y==2*p[i]) {
- ret.pb(y);
- y = p[i];
- vis[y] = 1;
- }
- ans.pb(pii(x,y));
- }
- }
- for (int t=2; t<=n; t<<=1) ret.pb(t);
- while (ret.size()>=2) {
- int x = ret.back();
- ret.pop_back();
- int y = ret.back();
- ret.pop_back();
- ans.pb(pii(x,y));
- }
- printf("%d\n", int(ans.size()));
- for (auto t:ans) {
- printf("%d %d\n",t.x,t.y);
- }
- }
Jzzhu and Apples CodeForces - 449C (构造,数学)的更多相关文章
- Codeforces 772C 构造 数学 + dp + exgcd
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...
- Codeforces 449C Jzzhu and Apples 贪心 (看题解)
Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...
- 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 ...
- Codeforces 449.C Jzzhu and Apples
C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 450E Jzzhu and Apples 数学+模拟
E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
- CF449 C. Jzzhu and Apples
/* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...
- Alice, Bob, Oranges and Apples CodeForces - 586E
E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
随机推荐
- 微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉
微信公众号为什么要加粉?流量,广告,KPI,吸粉,增粉 1.曾有人这样比喻:当你的粉丝超过100人时,你就像是一本内刊:超过1000人,你就像个布告栏:超过1万人,你就好比一本杂志:超过10万人,你就 ...
- Linux中Postfix反病毒和垃圾邮件工具(十)
amavisd-new amavisd-new呼叫器是一个连接MTA和内容检测工具(诸如病毒扫描工具和SpamAssassin)的高性能接口程序,使用perl语言写成.它一般通过SMTP.ESMTP或 ...
- ”MySQL索引“学习总结
序 learn by doing 是最快的学习方式.在百度外卖研发中心,我每天工作接触数据库方面最多的就是"索引",另外面试官在面试时也一定会考察到索引. Part 1, Expl ...
- 01: requests模块
目录: 1.1 requests模块简介 1.2 使用requests模块发送get请求 1.3 使用requests模块发送post请求 1.4 requests.request()参数介绍 1.1 ...
- Vue 父组件循环使用refs调用子组件方法出现undefined的问题
Vue 父组件循环使用refs调用子组件方法出现undefined的问题 1. 背景 最近前端项目遇到一个问题,我在父组件中使用了两个相同的子组件child,分别设置ref为add和update.其中 ...
- 如何在一台主机上管理自己的多个git repository
在使用git时,通常是直接ssh-keygen生成默认秘钥.然后将共钥添加到远程仓库,就可以访问了. 但是,当我们有多个repository时,这种方式就不适用了,因为一个秘钥只能关联一个远程仓库. ...
- 51nod 1080 两个数的平方和
没心情写数学题啦啊 好难啊 #include<bits/stdc++.h> using namespace std; set<int> s; set<int>: ...
- Codeforces Round #528 div1
完了,看来上一次的flag要应验了,我大概是真的要掉成pupil了吧.. A - Connect Three 这个就是找到x的中间值,y的中间值,然后切一下,然后把所有的点挂到这条边上.但是我做的还是 ...
- 论文笔记: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 ...
- Java初始化块的作用
1.使代码更简洁 eg: public class Test { private void init() { System.out.println("初始化状态"); } publ ...