F - Dima and Lisa(哥德巴赫猜想)
Problem description
Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.
More formally, you are given an odd numer n. Find a set of numbers pi (1 ≤ i ≤ k), such that
- 1 ≤ k ≤ 3
- pi is a prime
- $\sum_{i=1}^k p_i = n $
The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.
Input
The single line contains an odd number n (3 ≤ n < 109).
Output
In the first line print k (1 ≤ k ≤ 3), showing how many numbers are in the representation you found.
In the second line print numbers pi in any order. If there are multiple possible solutions, you can print any of them.
Input
27
Output
3
5 11 11
Note
A prime is an integer strictly larger than one that is divisible only by one and by itself.
解题思路:将一个奇数拆分成1~3个素数,暴力即过!
哥德巴赫猜想:随便取某一个奇数,比如77,可以把它写成三个素数之和,即77=53+17+7;再任取一个奇数,比如461,可以表示成461=449+7+5,也是三个素数之和,461还可以写成257+199+5,仍然是三个素数之和。例子多了,即发现“任何大于5的奇数都是三个素数之和。”当然此题只是要求可以拆分成1~3个素数,并不要求一定是3个素数,譬如461本身就是素数,则此时直接输出1\n461即可。
AC代码(31ms):
#include<bits/stdc++.h>
using namespace std;
bool isprime(int x){
if(x<=)return false;
for(int i=;i*i<=x;++i)
if(x%i==)return false;
return true;
}
int main(){
int n;cin>>n;bool flag=false;
if(isprime(n))cout<<"1\n"<<n<<endl;//如果本身是素数,直接输出即可
else{
for(int i=;i<=n;i+=){//从3开始按奇数来枚举
if(isprime(i)){
int tmp=n-i;
if(isprime(tmp)){cout<<"2\n"<<i<<' '<<tmp<<endl;break;}
for(int j=;j<=n;j+=)//从3开始按奇数来枚举
if(isprime(j) && isprime(tmp-j)){cout<<"3\n"<<i<<' '<<j<<' '<<(tmp-j)<<endl;flag=true;break;}
if(flag)break;
}
}
}
return ;
}
F - Dima and Lisa(哥德巴赫猜想)的更多相关文章
- 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 ...
- Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...
- 洛谷——P1579 哥德巴赫猜想(升级版)
P1579 哥德巴赫猜想(升级版) 题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约 ...
- Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
D. Dima and Lisa Dima loves representing an odd num ...
- Java实现蓝桥杯算法提高 哥德巴赫猜想
试题 算法提高 哥德巴赫猜想 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 根据所给函数(判断一个整数是否是素数),然后依托该函数,将输入N内的偶数(6-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 ...
- 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拆成任意个数的 ...
随机推荐
- js实现年月日三级联动
当我们注册一个qq的时候,会看到一个三级年月日的联动菜单,下面简单介绍. <!doctype html> <html lang="en"> <head ...
- 【JSP】简单登陆界面
学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...
- 【sqli-labs】 less22 Cookie Injection- Error Based- Double Quotes - string (基于错误的双引号字符型Cookie注入)
注入的过程和less 20 21一样,这次闭合cookie的使用的双引号
- Appium Desired Capabilities信息配置
编写APPium脚本,必须要配置Desired Capabilities信息 Desired Capabilities 在启动 session 的时候是必须提供的. Desired Capabilit ...
- react-router @4用法整理
在React Router 3上写了一篇文章后不久,我第一次在React Rally 2016上遇到了Michael Jackson.Michael是React Router和Ryan Florenc ...
- InnoDB undo log物理结构的初始化
水平有限,如果有误请指出.一直以来未对Innodb 的undo进行好好的学习,最近刚好有点时间准备学习一下,通过阿里内核月报和自己看代码的综合总结一下.本文环境: 代码版本 percona 5.7.2 ...
- 【转载】使用 IntelliJ IDEA 新建一个 web项目
IntelliJ IDEA 创建Web项目(全教程) 说明:IntelliJ IDEA 版本为14.JDK 版本为1.7tomcat 版本为apache-tomcat-7.0.70 注:在创建过程 ...
- vue点击实现 路由的跳转
点击按钮实现路由的跳转 <div @click="gotoMenu">按钮</div> 实现跳转 methods: { gotoMenu(){ //跳转到上 ...
- C#第四节课
分值语句(1) using System;using System.Collections.Generic;using System.Linq;using System.Text;using Syst ...
- 9.Spring的IOC+MyBaits+log4j+Servlet
1.创建如下mysql脚本 drop table if exists book_info; create table if not exists book_info( book_id ) primar ...