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 ...
随机推荐
- 外键,check,索引等,根据ID来检索详细信息
sql server OBJECTPROPERTY 函数 分类: Sql server2008-11-26 11:11 1562人阅读 评论(0) 收藏 举报 sql serverinsertdele ...
- Linux内核0.11体系结构 ——《Linux内核完全注释》笔记打卡
0 总体介绍 一个完整的操作系统主要由4部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如图0.1所示.操作系统内核程序主要用于对硬件资源的抽象和访问调度. 图0.1 操作系统组成部分 内核 ...
- python 连接sqlserver: pymssql
停了一个月,终于还是把这个做了,工作需要!!!在装pymssql时,一直报错,确定了要先装freetds: 1. 安装freetds时报错,搜索到要先进行如下操作: brew unlink freet ...
- js 小练习
js 学习之路代码记录 js 加载时间线 1.创建Document对象,开始解析web页面.解析HTML元素和他们的文本内容后添加Element对象和Text节点到文档中.这个阶段document.r ...
- Texture 纹理贴图
基础贴图Shader:只有纹理 1. 在属性中声明纹理贴图: _MainTex ("Texture", 2D) = "white" {} 2. 在Pass中声明 ...
- 创建Javaweb项目及MyEclipse视图的配置
在myEclipse里--右键new--Web Project 视图的配置--Window--Show View-Other在里面输入要找的视图例如(servers)或者直接 Window--rese ...
- 7-9 旅游规划(25 分)(Dijkstra最短路径算法)
有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便 ...
- HDU 4948 (傻比图论)
Kingdom Problem Description Teacher Mai has a kingdom consisting of n cities. He has planned the tra ...
- 【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...
- Linux下汇编语言学习笔记54 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...