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 ...
随机推荐
- Linux下MySQL 5.7的初始化
要用管理员账号运行. systemctl start mysql#启动MySQL服务 mysqld_safe --user=mysql &#启动MySQL服务(安全方式) mysql -u r ...
- Spring Boot . 2 -- 用Spring Boot 创建一个Java Web 应用
通过 start.spring.io 创建工程 通过 IDEA 创建工程
- oracle分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总
语法:sum(col1) over(partition by col2 order by col3 ) 准备数据: DEPT_ID ENAME SAL1 1000 ...
- group by两个条件
学生表: 成绩表: 问题:统计各系各门课程的平均成绩 答案: select sdept,cno,AVG(grade)avg_grade from S join SC on S.sno = SC.sno ...
- [Python3网络爬虫开发实战] 1.3.3-pyquery的安装
pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.本节中,我们就来了解一下它的安装方式. 1. 相关链接 GitHub:h ...
- jmeter 接口测试
web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开源).soupUI(开源&商业版). 下面将对前一篇Postman做接口测试中的接口用Jmeter ...
- 76-Bears/Bulls Power,熊力/牛力震荡指标.(2015.7.1)
Bears/Bulls Power 熊力/牛力震荡指标 Power,熊力/牛力震荡指标.(2015.7.1)" title="76-Bears/Bulls Power,熊力/牛力震 ...
- vector元素的删除 remove的使用 unique的使用
在vector删除指定元素可用以下语句 : v.erase(remove(v.begin(), v.end(), element), installed.end()); 可将vector中所有值为el ...
- 九度oj 题目1190:大整数排序
题目1190:大整数排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4142 解决:1867 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,( ...
- 怎样签发SSL证书
最近在做怎样让网站有SSL,搞了一天,现在总结一下 首先要安装OPENSSL和 Java的 keytool 先用OPENSSL生成私钥和CSR openssl req -newkey rsa:2048 ...