Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
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
The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.
The single line contains an odd number n (3 ≤ n < 109).
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.
27
3
5 11 11
A prime is an integer strictly larger than one that is divisible only by one and by itself.
题意:给你一个n,让你找出K个(1<=k<=3)素数,它们的和为n
题解:随机素数判断法,
知道一个大于2的偶数必然存在两个素数和为本身,n-3就可以了
对于n小的数可以打表判断
///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000
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;
} #define maxn 5500
int p[maxn],H[maxn];
vector<int >G;
void init()
{
mem(H);
H[]=;
for(int i=;i<maxn;i++)
{
if(!H[i])
for(int j=i+i;j<maxn;j+=i)
{
H[j]=;
}
}
for(int i=;i<maxn;i++)
{
if(!H[i])
{
G.push_back(i);
}
}
}
int main()
{
init();
int n;
cin>>n;
if(n<=)
{
for(int i=;i<G.size();i++)if(G[i]==n){
cout<<<<endl;
cout<<G[i]<<endl;return ;
}
for(int i=;i<G.size();i++)
for(int j=;j<G.size();j++)
if(i!=j&&G[i]+G[j]==n)
{
cout<<<<endl;
cout<<G[i]<<" "<<G[j]<<endl;
return ;
}
for(int i=;i<G.size();i++)
for(int j=;j<G.size();j++)
for(int k=;k<G.size();k++)
if(i!=j&&i!=k&&j!=k&&G[i]+G[j]+G[k]==n)
{
cout<<<<endl;
cout<<G[i]<<" "<<G[j]<<" "<<G[k]<<endl;return ;
}
}
else {
n=n-;
for(int i=n-;i>=;i--)
{
int x=n-i;
int y=i;
if(Miller_Rabin(x)&&Miller_Rabin(y))
{
cout<<<<endl;
cout<<<<" "<<x<<" "<<y<<endl;return ;
}
}
}
return ;
}
代码
Codeforces Round #324 (Div. 2)D. 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)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #622 (Div. 2) B. Different Rules(数学)
Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...
- 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 ...
- Codeforces Round #324 (Div. 2)
CF的rating设置改了..人太多了,决定开小号打,果然是明智的选择! 水 A - Olesya and Rodion #include <bits/stdc++.h> using na ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...
- 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 ...
随机推荐
- MySQL-----唯一索引
唯一索引: 单列唯一索引和联合唯一索引 索引是为了加速查找. 唯一索引是加了约束条件.例如主外键. 唯一索引的约束: 约束不能重复(可以为空) 主键不能重复(不能为空) 加速查找 create tab ...
- Python之面向对象新式类和经典类
Python之面向对象新式类和经典类 新式类和经典类的继承原理: 在Python3中,就只有新式类一种了. 先看Python3中新式类: 类是有继承顺序的: Python的类是可以继承多个类的,也就是 ...
- MFC对话框使用CPrintDialog实现打印,指定打印机、后台打印
推荐下 不错. 对话框打印,网上一搜一大堆,基本分2类: A类: CPrintDialog.DoModal,然后在模态对话框里选打印机.打印配置: B类:GetPrinterDeviceDefault ...
- DFS求连通块(漫水填充法)
G - DFS(floodfill),推荐 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I6 ...
- java中"=="和equals方法究竟有什么区别?
为什么会说到这个问题呢,是因为在java中遇到这个问题太常见了,无论是在写代码时还是在面试时.下面就一起探讨一下它们之间的联系与区别吧. 首先对于这样的问题,一般是先单独把一个东西说清楚,然后再说另一 ...
- 将json格式转为url参数格式的方法(xjl456852整理修改)
测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...
- W3C Blog: HTML-下一步是什么?
HTML5.1 草稿版: 地址:https://www.w3.org/TR/html51/introduction.html#a-quick-introduction-to-html 2016年3月0 ...
- SIGPIPE 13 和其他信号的对照表
SIGPIPE 13 和其他信号的对照表 SIGHUP 1 在控制终端上检测到挂断或控制线程死亡 是SIGINT 2 交互注意信号 是SIGQUIT 3 交互中止信号 是SIGILL 4 检测到非法硬 ...
- Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)
C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell ...
- Linux下汇编语言学习笔记55 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...