ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier
Memory limit: 64 MB
Input
Output
Sample
| input | output |
|---|---|
7 |
2 |
题意
一位物理学家和一位数学家正在争论最少几个质数的和为N。
其中一个说这无从知晓,另一个说3个就够了。
input
第一行包含一个整数T,表示测试点数量。
接下来T行,每行一个整数N。
(0<=T<=20,2<=N<=10^9)
output
输出一些质数,使它们的和为N,质数的个数要尽量少。
思路:根据哥德巴赫猜想,
任一大于2的偶数都可写成两个质数之和。
任一大于7的奇数都可写成三个素数之和。
详细内容可参照维基百科http://zh.wikipedia.org/wiki/%E5%93%A5%E5%BE%B7%E5%B7%B4%E8%B5%AB%E7%8C%9C%E6%83%B3。
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <deque>
#include <bitset>
#include <functional>
#include <utility>
#include <iomanip>
#include <cctype>
using namespace std; #define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define max(a,b) ((a) > (b)) ? (a) : (b)
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x) typedef long long LL;
typedef vector<int> VI; const int MAXN = ;
const int hash_size = ;
const int INF = 0x7f7f7f7f; bool p[MAXN]={}, flag;
int prime[MAXN]={}, q[], n, d;
void init()
{
int i;
for(i = ; i <= ; i++)
if(!p[i])
{
prime[]+=;
prime[prime[]]=i;
for(int j=i+i;j<=;j+=i)
p[j]=true;
}
} bool isprime(int n){//判断n是否是一个质数
if (n == )
return true;
else {
int sq, i;
sq = int(sqrt(n*1.0));
for (i = ; i <= sq+; ++i)
if (n%i == )
return false;
return true;
}
} void dfs(int k,int x,int y)
{//将奇数进行分解
int i;
if (flag) return;
if (k == )
{
if (isprime(x))
{
FORD(i, d, )
printf("%d ", prime[q[i]]);//进行输出
printf("%d\n", x);
flag = true;//找到了一个分解
}
return;
}
for (i = y; i<=prime[]; ++i)
{
if (prime[i]*k > x) return;
q[k] = i;
dfs(k-, x-prime[i], i);
}
} int main()
{
init();
int t, i;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
if (isprime(n))
printf("%d\n", n);
else if (n&) {
d = ;
flag = false;
while (!flag)
dfs(++d, n, );//先分2分,再分3个
}
else {
int tmp;
FORE(i, , prime[]) {
tmp = n - prime[i];
if (isprime(tmp)) {
printf("%d %d\n", prime[i], tmp);
break;
}
}
}
}
return ;
}
ural 1356. Something Easier(数论,哥德巴赫猜想)的更多相关文章
- URAL 1356. Something Easier(哥德巴赫猜想)
题目链接 题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数. 思路 : 哥德巴赫猜想 : 任何一个大于 6的偶数都可以表示成两个素数之和. 任何一个大于9的奇数都可以表示成三个素数 ...
- 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拆成任意个数的 ...
- *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 ...
- 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 ...
随机推荐
- Android_AndroidStudio配置
IDE降低了程序编译的门槛, 让Android程序的编译和运行变得简单易操作. 但无论Eclipse还是Android Studio, IDE都不是非常智能和可靠的, 总会出大大小小的问题. 很多时候 ...
- 【Python】使用super初始化超类
初始化超类的传统方式,在子类的实例中调用超类的__init__()方法. 但是传统的方法有两个问题,比如: 问题1: class MyBaseClass: def __init__(self, val ...
- cocos2dx 举例说明 convertToNodeSpace 与 convertToWorldSpace 的使用
convertToNodeSpace:把世界坐标转换到当前节点的本地坐标系中. //可以应用于判断子节点是否被点击,这时就需要把坐标从世界坐标系转换为父节点的坐标系. //当然大多数情况会用CCMen ...
- eclipse 导入tomcat7源码
导入tomcat的源码其实说简单也不简单,说不简单也简单,主要还是环境问题,中间花费了我很多时间,网上找了很多都没什么用,参考一些文章,然后自己慢慢摸索出来的. 环境:(1)jdk:jdk1.6.0_ ...
- Super Jumping! Jumping! Jumping!杭电1087
Description Problem Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumpi ...
- JAVA的if用法,比如if(...){} 和if()没有大括号直接写下面的区别是什么
有大括号的时候 大括号里面所有的 都归if管.只有条件为真的时候 才会执行.没有大括号的时候 只有下面的一句归if管,也就是说 当只有一句的时候 大括号可以省略 其它的 没区别.
- centos7,yum安装的redis用systemctl无法启动
因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...
- slf4j 之logback日志之sl4j架构【二】
一.整体介绍 介绍: The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for v ...
- 10676 涂鸦跳跃(sort)
10676 涂鸦跳跃 该题有题解 时间限制:1000MS 内存限制:65535K提交次数:240 通过次数:19 题型: 编程题 语言: G++;GCC Description 你玩过曾经风靡一 ...
- 计算机网络课程优秀备考PPT之第六章传输层(六)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...