zoukankan      html  css  js  c++  java
  • 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
    %I64d
    ", n);
            else {
                if(n == 9) {printf("2
    2 7
    "); return 0;}
                if(n == 7) {printf("1
    7
    "); 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
    ", a, b, c);
    
            }
        }
    }
  • 相关阅读:
    java多线程的简单demo
    对RedisTemplate接口二次封装成自定义工具接口
    开发中常遇到的linux系统配置操作整理
    Mybatis传递参数的三种方式
    创建Springmvc项目时,特殊拦截器失效情况的原因及解决办法
    Quartz的Hello world
    JAVA 中数组的几种排序方法
    二叉树的遍历
    eclipse 修改中英文显示
    Spring加载配置文件的三种方式
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5664806.html
Copyright © 2011-2022 走看看