码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 735D:Taxes(哥德巴赫猜想)

时间:2016-11-28 14:55:36      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:href   素数   math   targe   tar   target   false   div   set   

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\n", ans);
30     return 0;
31 }

 

Codeforces 735D:Taxes(哥德巴赫猜想)

标签:href   素数   math   targe   tar   target   false   div   set   

原文地址:http://www.cnblogs.com/fightfordream/p/6109397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!