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 ...
随机推荐
- EditText动态转换只读/编辑状态
public class MyActivity extends Activity { private KeyListener listener; private EditText editText; ...
- 用Toast来增加调试效率的小技巧
import android.content.Context; import android.widget.Toast; /** * Created by apple on 10/7/15. */ p ...
- 开学初的c
cout<<a[i]<<" " 这个是先输出a[i]再输出空格 cout<<endl 这个是直接换行cout<<a[i]& ...
- java内存基础(一)
博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 35 文章- 0 评论- 29 关于Java 数组内存分配一点认识 //总结:[ 数组引用变量存储在栈内存中,数组对象存储在堆内存当中.数 ...
- 概念与用法-cookie,session,auth (认证系统)
COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...
- python开发面向对象基础:封装
一,封装 [封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内 ...
- vue-cli脚手架config目录下index.js配置文件详解
此文章介绍vue-cli脚手架config目录下index.js配置文件 此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单 ...
- Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available
maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...
- Java对网络图片/本地图片转换成Base64编码和解码
一.将本地图片转换成Base64编码字符串 /** * 将本地图片转换成Base64编码字符串 * * @param imgFile 图片目录路径 * @return */ public static ...
- 小白之js原生轮播图
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...