【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
",len);
    rep1(i,0,len-1)
    {
        printf("%d",ans[i]);
        if (i==len-1)
            puts("");
        else
            putchar(' ');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626792.html