【codeforces 749A】Bachgold Problem
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.
Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors — 1 and k.
Input
The only line of the input contains a single integer n (2 ≤ n ≤ 100 000).
Output
The first line of the output contains a single integer k — maximum possible number of primes in representation.
The second line should contain k primes with their sum equal to n. You can print them in any order. If there are several optimal solution, print any of them.
Examples
input
5
output
2
2 3
input
6
output
3
2 2 2
【题目链接】:http://codeforces.com/contest/749/problem/A
【题解】
显然奇数的话就先减个3,然后就是偶数了,一直减2就好;
偶数的话就全都是2。
我写了个枚举。
不知道自己怎么想的。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
vector <int> a,ans;
bool is(int x)
{
int len = sqrt(x);
rep1(i,2,len)
if ((x%i)==0)
return false;
return true;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,2,n)
if (is(i))
a.pb(i);
int now = 0;
while (n)
{
if (n-a[now]>1 || n-a[now]==0)
{
n-=a[now];
ans.pb(a[now]);
}
else
{
now++;
ans.pb(a[now]);
n-=a[now];
}
}
int len = ans.size();
printf("%d\n",len);
rep1(i,0,len-1)
{
printf("%d",ans[i]);
if (i==len-1)
puts("");
else
putchar(' ');
}
return 0;
}
最新文章
- zk 起别名时候碰到的问题
- 此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求
- IOS Socket 05-XMPP开始&;安装服务器openfire&;安装配置客户端
- 你真的了解UIEvent、UITouch吗?
- MySQL 约束的讲解
- JS初学之-效果没出来怎么办?-alert函数测试
- PHP中使用CURL实现get和post请求(总结)
- Codeforces Gym 100418K Cards 暴力打表
- thinkphp 独立分组配置
- php url字符转义操作
- Smallest Rectangle Enclosing Black Pixels 解答
- Unity 之 AfterFixedUpdate,在所有 GameObject FixedUpdate 后执行
- javascript 中的闭包
- 负载均衡之基于L7负载
- JS正则表达式的基础用法
- 深入解析 SQL Server 高可用镜像实现原理
- Lintcode389 Valid Sudoku solution 题解
- django中引入bootstrap4.3
- [转帖] CA如何保护自己的私钥
- 团队Alpha博客链接目录