天天看点

Codeforces Round #382 (Div. 2)D. Taxes哥德巴赫猜想哥德巴赫猜想

哥德巴赫猜想

  • (一)任意大于2的偶数 n n n都可以表示成两个质数的和
  • (二)任意大于5的整数 n n n都可以表示成三个质数的和

    事 实 上 ( 二 ) 可 以 由 ( 一 ) 得 出 { n 为 奇 数 : n = 3 + ( n − 3 ) , ( n − 3 ) 为 偶 数 n 为 偶 数 : n = 2 + ( n − 2 ) , ( n − 2 ) 为 偶 数 事实上(二)可以由(一)得出\begin{cases}\\ n为奇数:n=3+(n-3),(n-3)为偶数\\ n为偶数:n=2+(n-2),(n-2)为偶数 \end{cases}\\ 事实上(二)可以由(一)得出{n为奇数:n=3+(n−3),(n−3)为偶数n为偶数:n=2+(n−2),(n−2)为偶数​

题目链接:https://codeforces.com/contest/735/problem/D

题意:给出一个数,让你分解为若干个数,使得 ∑ i = 1 k f ( n i ) , ( 1 ≤ i ≤ k ) \sum_{i=1}^kf(n_i),(1\leq i \leq k) ∑i=1k​f(ni​),(1≤i≤k)最小

f ( x ) = { 1 , x 为 质 数 最 大 的 不 包 括 自 身 的 因 数 , x 为 合 数 f(x)=\begin{cases}\\ 1,x为质数\\ 最大的不包括自身的因数,x为合数 \end{cases}\\ f(x)={1,x为质数最大的不包括自身的因数,x为合数​

做法:我一开始还傻乎乎的企图去多分解几个质数出来,第一是复杂度太大,另一个就是分成几个最优问题,然后才想起来好像有那么个定理(哥德巴赫猜想)是针对几个素数和的性质的

1.先判断原来的数字是不是个质数,如果是的话最简单不用分解原来的数

2.如果是个偶数,根据猜想可以分解为两个质数,输出2肯定是最优的

3.如果是个奇数,根据猜想可以分解为三个质数,但是还得判断是不是可以分成一个偶素数(2)和一个奇素数的形式,此时就是输出2

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
using namespace std;
const int maxn=2e6+7;
bool isprime(int x)
{
    if (x==2||x==3)return true;
    for (int i=2;i<=sqrt(x);i++)
    {
        if (x%i==0)return false;
    }
    return true;
}
int main()
{
    ll n;
    scanf("%lld",&n);
    if (isprime(n)) W(1);
    else if (n%2==0) W(2);
    else
    {
        if (isprime(n-2))W(2);
        else W(3);
    }
    return 0;
}