D. Dima and Lisa

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/584/problem/D

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. 1 ≤ k ≤ 3
  2. pi is a prime

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.

Sample Input

27

Sample Output

3
5 11 11

HINT

题意

给你一个奇数n,然后让你找到k(k<=3)个质数,使得这k个质数加起来等于n

题解:

哥德巴赫猜想,任何一个大于2的偶数都可以由俩素数组成

于是我们就让n-3,然后暴力搞就行了……

素数其实是非常密集的,在n->2n之间 一定有一个素数

所以这个复杂度还是比较客观的(雾

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§&szlig;§é§à§é¨f§3
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** //****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=;//随机算法判定次数,S越大,判错概率越小 //计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的
// a,b,c <2^63
long long mult_mod(long long a,long long b,long long c)
{
a%=c;
b%=c;
long long ret=;
while(b)
{
if(b&){ret+=a;ret%=c;}
a<<=;
if(a>=c)a%=c;
b>>=;
}
return ret;
} //计算 x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
if(n==)return x%mod;
x%=mod;
long long tmp=x;
long long ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret=pow_mod(a,x,n);
long long last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(long long n)
{
if(n<)return false;
if(n==)return true;
if((n&)==) return false;//偶数
long long x=n-;
long long t=;
while((x&)==){x>>=;t++;}
for(int i=;i<S;i++)
{
long long a=rand()%(n-)+;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************ long long gcd(long long a,long long b)
{
if(a==)return ;//??????
if(a<) return gcd(-a,b);
while(b)
{
long long t=a%b;
a=b;
b=t;
}
return a;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
const int MAXN = ;
bool flag[MAXN];
int primes[MAXN], pi;
void GetPrime_1()
{
int i, j;
pi = ;
memset(flag, false, sizeof(flag));
for (i = ; i < MAXN; i++)
if (!flag[i])
{
primes[i] = ;//素数标识为1
for (j = i; j < MAXN; j += i)
flag[j] = true;
}
} ll p[maxn];
vector<int> DDD;
int main()
{
srand(time(NULL));
GetPrime_1();
int tot = ;
for(int i=;;i++)
{
if(tot>)break;
if(primes[i])
{
DDD.push_back(i);
tot++;
}
}
ll n;cin>>n;
if(Miller_Rabin(n))
{
cout<<""<<endl;
cout<<n<<endl;
return ;
}
if(Miller_Rabin(n-))
{
cout<<""<<endl;
cout<<"2 "<<n-<<endl;
return ;
}
if(n<)
{
for(int i=;i<DDD.size();i++)
{
for(int j=i;j<=DDD.size();j++)
{
if(DDD[j]+DDD[i]>n)break;
if(Miller_Rabin(n-DDD[i]-DDD[j]))
{
printf("3\n");
printf("%d %d %d\n",DDD[i],DDD[j],n-DDD[i]-DDD[j]);
return ;
}
}
}
}
else
{
n-=;
for(int i=n-;i>=;i--)
{
int x = n-i,y = i;
if(Miller_Rabin(x)&&Miller_Rabin(y))
{
printf("3\n");
cout<<"3 "<<x<<" "<<y<<endl;
return ;
}
}
}
}

Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想的更多相关文章

  1. Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)

                                                     D. Dima and Lisa Dima loves representing an odd num ...

  2. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  3. Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想

    原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...

  4. Codeforces Round #324 (Div. 2) (哥德巴赫猜想)

    题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...

  5. Codeforces Round #324 (Div. 2) D

    D. Dima and Lisa time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Round #324 (Div. 2)

    CF的rating设置改了..人太多了,决定开小号打,果然是明智的选择! 水 A - Olesya and Rodion #include <bits/stdc++.h> using na ...

  7. Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合

    题目链接: http://codeforces.com/problemset/problem/272/D D. Dima and Two Sequences time limit per test2 ...

  8. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  9. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

随机推荐

  1. Java线程池的工作原理与实现

    简单介绍 创建线程有两种方式:继承Thread或实现Runnable.Thread实现了Runnable接口,提供了一个空的run()方法,所以不论是继承Thread还是实现Runnable,都要有自 ...

  2. 【转】Android自定义View的实现方法,带你一步步深入了解View(四)

    原文网址: 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17357967 不知不觉中,带你一步步深入了解View系列的文章已经写到 ...

  3. 学习面试题Day07

    1.打印出100以内的素数 该编程题的思路大致如下: (1)完成一个判断某整数是否为素数的方法: (2)循环1--100: (3)每循环一次就判断一次,返回true则打印:package com.ex ...

  4. ejabberd的多域名(domain)设置

    在ejabberd中可以支持多个domain,我讲一下我的配置过程我的ejabberd系统是:ejabberd server+sql server+openldap+gateway.我总共使用了5台机 ...

  5. NopCommerce架构分析之三---数据库初试化及数据操作

    系统启动时执行任务:IStartupTask,启动时执行的任务主要是数据库的初始化和加载. IStartupTask调用IEfDataProvider进行数据库的初始化. IEfDataProvide ...

  6. volatile之一--volatile不能保证原子性

    Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和 volatile 关键字机制在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这 ...

  7. MyEclipse2014安装ADT插件(适用于其他版本)

    这次,本文采用公认的最佳插件安装方式——link方式来安装ADT插件,此方法适用于Eclipse以及MyEclipse其他版本.下面为大家一一道来: 大致过程如下: 官方的在线安装很麻烦,找了很久,终 ...

  8. MSP430看门狗

    其实430的看门狗,与51的大同小异,都是为了防止程序跑飞而出现不可预知的错误而专门设定的,所以说,看门狗的应用,是项目马上要进行实际应用中必须要进行的一环,也是电子工程师必须掌握的一环,下面介绍一下 ...

  9. IO_REMOVE_LOCK使用方法小结(转载加改正)

    原文链接:http://www.programlife.net/io_remove_lock.html IO_REMOVE_LOCK(删除锁)的具体结构没有公开,WDK的文档中中查不到IO_REMOV ...

  10. eclipse配置tomcat加大内存的方法

    双击tomcat -Dcatalina.base="E:\work\whykt\.metadata\.plugins\org.eclipse.wst.server.core\tmp0&quo ...