zoukankan      html  css  js  c++  java
  • Codeforces 735D:Taxes(哥德巴赫猜想)

    http://codeforces.com/problemset/problem/735/D

    题意:给出一个n,这个n可以分解成 n = n1 + n2 + …… + nk,其中k可以取任意数。要使得分解以后所有的n的最大因子(不包括自己本身)的和最小,问最小的和是多少。

    思路:比赛的时候想到全部拆成素数是最好的,但是不知道怎么拆,看别人跑的特别快,就知道是数论题,绝望之下试了两发暴力,都是TLE了,GG。早上起来才知道有“哥德巴赫猜想”这个东西。

    内容大概是如下两点:

    1、所有大于2的偶数可以被分解成两个素数。

    2、所有大于7的奇数可以被分解成三个素数。(n-3)为偶数,3是一个素数,所以是三个。

    所以知道这个猜想之后就变得简单了:

    1、偶数:n为2,答案是1,否则答案是2.

    2、奇数:首先,n最少可以拆成三个素数,还有两种情况要考虑:n本身是一个素数的话答案就是1,n-2是一个素数答案就是2(一个奇数可以拆成一个偶数+一个奇数,偶数只有2是素数)。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <iostream>
     6 using namespace std;
     7 const int N = 10004;
     8 
     9 bool check(int n) {
    10     int tmp = sqrt(n * 1.0);
    11     for(int i = 2; i <= tmp; i++) {
    12         if(n % i == 0) return false;
    13     }
    14     return true;
    15 }
    16 
    17 int main() {
    18     int n;
    19     scanf("%d", &n);
    20     int ans;
    21     if(n & 1) {
    22         if(check(n)) ans = 1;
    23         else if(check(n-2)) ans = 2;
    24         else ans = 3;
    25     } else {
    26         if(n == 2) ans = 1;
    27         else ans = 2;
    28     }
    29     printf("%d
    ", ans);
    30     return 0;
    31 }
  • 相关阅读:
    Leetcode-Pascal's Triangle
    SRM 619
    请用漂亮欢呼-------Day38
    创建list方法总结
    [ZJOI2019]语言
    jekyll 在博客添加流程图
    jekyll 在博客添加流程图
    HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具
    HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具
    How to use code to exit the application in UWP
  • 原文地址:https://www.cnblogs.com/fightfordream/p/6109397.html
Copyright © 2011-2022 走看看