URAL 1356. Something Easier(哥德巴赫猜想)
题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数。
思路 :
哥德巴赫猜想 :
任何一个大于 6的偶数都可以表示成两个素数之和。
任何一个大于9的奇数都可以表示成三个素数之和。
而在该题中,偶数中2本身就是个素数,奇数中小于9的都是素数,所以只要写一个判断素数的函数即可,这样不在范围内的数就可以直接判断输出了。
任何一个整数N(N>=2)最多由三个素数相加构成。要分情况考虑:
1. 如果N为偶数,1)如果N==2,直接输出;
2)如果N>2,那么N一定可以写成两个素数的和;
2.如果N为奇数,1)如果N自身就是素数,则直接输出;
2)如果N由两个素数构成,这两个素数只可能是:2 和 N-2;
3)N为三个素数之和。
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; bool prime(int n)
{
for(int i = ; i * i <= n ; i++)
{
if(n % i == ) return false ;
}
return true ;
}
int main()
{
int T,n ;
scanf("%d",&T) ;
while(T--)
{
scanf("%d",&n) ;
if(n % == )
{
if(n == )
printf("2\n") ;
// else if(n == 4) printf("2 2\n") ;
else{
for(int i = ; i <= n ; i += )
{
if(prime(i) && prime(n-i))
{
printf("%d %d\n",i,n-i) ;
break ;
}
}
}
}
else
{
if(prime(n)) printf("%d\n",n) ;
else if(prime(n-)) printf("2 %d\n",n-) ;
//else if(prime(n-4)) printf("2 2 %d\n",n-4) ;
else
{
bool flag = false ;
for(int i = ; i <= n ; i += )
{
for(int j = ; j <= n ; j += )
{
if(prime(i) && prime(j) && prime(n-i-j))
{
printf("%d %d %d\n",i,j,n-i-j);
flag = true ;
break;
}
}
if(flag) break ;
}
}
}
}
return ;
}
URAL 1356. Something Easier(哥德巴赫猜想)的更多相关文章
- ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...
- *CF2.D(哥德巴赫猜想)
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- C#实现哥德巴赫猜想
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Goet ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- CF735D Taxes 哥德巴赫猜想\判定素数 \进一步猜想
http://codeforces.com/problemset/problem/735/D 题意是..一个数n的贡献是它的最大的因子,这个因子不能等于它本身 然后呢..现在我们可以将n拆成任意个数的 ...
- Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想
D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...
- Codeforces 735D:Taxes(哥德巴赫猜想)
http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想
D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...
随机推荐
- java使用sftp下载远程服务器文件
使用的是springboot的项目,只是贴出主要配置与类,代码较长,可以先折叠: 参考:https://www.cnblogs.com/xyzq/p/7049369.html 操作工具类SftpUti ...
- 【转】Jmeter JDBC请求的问题
如何添加一个JDBC请求?本次以Orale为例. 1 From网上下载一个名为Class12.jar 的驱动包,然后放到该目录下:[否则会提示no suitable driver] 2 查看链接O ...
- 分布式锁之三:mysql实现-待整理
下面我们来看下开源dubbo推荐的业界成熟的zookeeper做为注册中心, zookeeper是hadoop的一个子项目是分布式系统的可靠协调者,他提供了配置维护,名字服务,分布式同步等服务.对于z ...
- Java调用Groovy
记录一下 http://docs.groovy-lang.org/latest/html/documentation/guide-integrating.html
- 进程句柄和进程ID的区别和关系
进程和进程句柄和进程id含义 进程是一个正在运行的程序,进程里可以包括多个模块(DLL,OCX,等)进程句柄是程序访问时用到的东西,当前进程句柄等于主模块的句柄,当你使用OpenProcess时的进程 ...
- springboot成神之——spring的文件上传
本文介绍spring的文件上传 目录结构 配置application DemoApplication WebConfig TestController 前端上传 本文介绍spring的文件上传 目录结 ...
- angular结合echarts创建图表
原理: 利用angularjs中的指令(directive)将echarts封装. 步骤: 1.封装函数: app.directive('line', function() { return { re ...
- leetcode378
public class Solution { public int KthSmallest(int[,] matrix, int k) { ); ); var list = new List< ...
- Codeforces Round #499 (Div. 2)(1011)
Natasha is planning an expedition to Mars for nn people. One of the important tasks is to provide fo ...
- 【Dubbo学习】
dubbo的介绍 dubbo是一个分布式的开源框架,其核心部分如下: 1.服务提供者:provider 2.服务消费者:consumer 3.注册中心:registry (仅仅只是负责通知) 服务者在 ...