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);
    
            }
        }
    }
  • 相关阅读:
    VS.NET提示"试图运行项目时出错:无法启动调试。绑定句柄无效"解决办法
    鼠标移动之hook学习
    今天完成任务之SQL中len的使用
    继承(2)方法《.NET 2.0面向对象编程揭秘 》
    框架设计:CLR Via C# 第二章
    启动IIS时提示“服务没有及时响应启动或控制请求”几种解决方法
    C#中处理字符串和数字
    TreeView实现权限管理
    鼠标单击右击双击简单功能的实现
    richTextBox 中插入图片
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5664806.html
Copyright © 2011-2022 走看看