zoukankan      html  css  js  c++  java
  • Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想

    原题链接:http://codeforces.com/contest/584/problem/D

    题意:

    给你一个奇数,让你寻找三个以内素数,使得和为这个奇数。

    题解:

    这题嘛。。。瞎比搞搞就好,首先寻找一个最大的小于这个数的素数,然后减掉之后,就是一个很小的偶数了,然后由哥德巴赫猜想,这个偶数一定能够分解成两个素数的和,然后再瞎比暴力一下就好。复杂度大概是$O(sqrt{n}log(n))$。

    代码:

    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int n;
    
    bool check(int x) {
        for (int i = 2; i * i <= x; i++)
            if (x % i == 0)return false;
        return true;
    }
    
    int main() {
        cin >> n;
        if (check(n)) {
            cout << 1 << endl;
            cout << n << endl;
            return 0;
        }
        for (int i = n; i >= 0; i -= 2) {
            if (check(i)) {
                int j = n - i;
                if (check(j)) {
                    cout << 2 << endl;
                    cout << i << " " << j << endl;
                    return 0;
                }
                for (int k = j - 2; k >= 0; k--) {
                    if (check(k) && check(j - k)) {
                        cout << 3 << endl;
                        cout << i << " " << k << " " << j - k << endl;
                        return 0;
                    }
                }
            }
        }
        return 0;
    }

     

  • 相关阅读:
    CodeForces 375D. Tree and Queries【树上启发式合并】
    JavaWeb(一)-Servlet知识
    XML解析
    XML约束
    XML
    什么是JWT
    Springboot @ConditionalOnProperty注解
    带你了解HTTP协议(二)
    带你了解HTTP协议(一)
    JAVA十大经典排序算法最强总结(含JAVA代码实现)
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4858620.html
Copyright © 2011-2022 走看看