zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    第11条:用zip函数同时遍历两个迭代器
    第10条:尽量用enumerate取代range
    第9条:用生成器表达式来改写数据量较大的列表推导式
    MySQL的约束
    VMware下所有的系统网卡启动不起来
    windows下的mysql闪退问题
    大型网站架构模式
    MySQL的information_schema库
    mysql复制表结构和内容
    希尔排序 堆排序 归并排序
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626792.html
Copyright © 2011-2022 走看看