Codeforces Round #324 (Div. 2) D. Dima and Lisa (哥德巴赫猜想 + 暴力)

D. Dima and Lisa

Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.

More formally, you are given an odd numer n. Find a set of numbers pi (1 ≤ i ≤ k), such that

  1. 1 ≤ k ≤ 3
  2. pi is a prime

The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.

Input

The single line contains an odd number n (3 ≤ n < 109).

Output

In the first line print k (1 ≤ k ≤ 3), showing how many numbers are in the representation you found.

In the second line print numbers pi in any order. If there are multiple possible solutions, you can print any of them.

Examples
input
27
output
3
5 11 11
Note

A prime is an integer strictly larger than one that is divisible only by one and by itself.

题意是将一个奇数n拆分成三个以下素数之和。

根据哥德巴赫猜想,任一大于7的奇数都可写成三个质数之和。这样我们随意找一个是奇数的素数a作为第一个素数,这样子n-a就是偶数了,根据原始的哥德巴赫猜想,任一大于2的偶数都可写成两个质数之和,我们就可以放心的去找剩下的两个素数。素数之间的距离最多只有几百,所以实际上是可以放心暴力的(然而我并不知道)。所以我套了米勒-罗宾算法来检测素数(就当做试模板了)。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 10;
ll mod_mul(ll a, ll b, ll n) {
    ll res = 0;
    while(b) {
        if(b&1)    res = (res + a) % n;
        a = (a + a) % n;
        b >>= 1;
    }
    return res;
}
ll mod_exp(ll a, ll b, ll n) {
    ll res = 1;
    while(b) {
        if(b&1)    res = mod_mul(res, a, n);
        a = mod_mul(a, a, n);
        b >>= 1;
    }
    return res;
}
bool miller_rabin(ll n) {
    if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11)    return true;
    if(n == 1 || !(n%2) || !(n%3) || !(n%5) || !(n%7) || !(n%11))    return false;
    ll x, pre, u;
    int i, j, k = 0;
    u = n - 1;  
    while(!(u&1)) {    
        k++; u >>= 1;
    }
    srand((ll)time(0));
    for(i = 0; i < 20; ++i) {    
        x = rand()%(n-2) + 2;   
        if((x%n) == 0)    continue;

        x = mod_exp(x, u, n);    
        pre = x;
        for(j = 0; j < k; ++j) {
            x = mod_mul(x, x, n);
            if(x == 1 && pre != 1 && pre != n-1)    return false;   
            pre = x;
        }
        if(x != 1)    return false; 
    }
    return true;
}
int main() {
    ll n;
    while(~scanf("%I64d", &n)) {
        ll a;
        if(n == 3 || n == 5) printf("1\n%I64d\n", n);
        else {
            if(n == 9) {printf("2\n2 7\n"); return 0;}
            if(n == 7) {printf("1\n7\n"); return 0;}
            a = n / 3;
            if(a % 2 == 0) a++;
            while(1) {
                a += 2;
                if(miller_rabin(a)) break;
            }
            puts("3");
            ll b, c;
            for(b = 3; ;b += 2) {
                c = n - b - a;
                //cout << b << " " << c << " " << a << endl;
                if(miller_rabin(b) && miller_rabin(c)) {
                    break;
                }
            }
            printf("%I64d %I64d %I64d\n", a, b, c);

        }
    }
}

转载于:https://www.cnblogs.com/lonewanderer/p/5664806.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值