多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))
题目链接:http://codeforces.com/contest/472/problem/A
题目:


题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和。此题则是将其修改为:一个大于等于12的数一定能表示为两个合数的和。
思路:这个很容易,下面是三种方法的代码。
奇偶法:一个数要么是奇数要么是偶数,众所周知大于2的偶数都是合数(因为都能被2整除嘛),所以只要把该数分解为两个非2的偶数的和即可。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
if(n%==)cout<<<<' '<<n-<<endl;
else cout<<<<' '<<n-<<endl;
return ;
}
打表法:将素数筛出来,然后进行遍历即可。
#include <iostream>
using namespace std; const int maxn = 1e6 + ;
int n;
int p[maxn]; void init() {
for(int i = ; i < maxn; i++) {
p[i] = ;
}
for(int i = ; i * i < maxn; i++) {
if(p[i]) {
for(int j = i * i; j < maxn; j += i) {
p[j] = ;
}
}
}
} int main() {
init();
cin >>n;
for(int i = n / ; i >= ; i--) {
if(!p[i] && !p[n - i]) {
cout <<i <<" " <<n - i <<endl;
return ;
}
}
return ;
}
Miller_Rabin法:这个是关键,其实这种方法的思路和上一种方法一样,不过不是打表,而是用Miller_Rabin来判断是否为素数,最重要的是Miller_Rabin法可以判断大素数,而打表却不可以!!!(计蒜客上一题也是用这个方法,且是大数据,打表不可过,链接为:https://nanti.jisuanke.com/t/25985,这个题的题解链接为https://www.cnblogs.com/Dillonh/p/9301991.html).
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
int n; ll multi(ll a, ll b, ll mod) {
ll ret = ;
while(b) {
if(b & )
ret = ret + a;
if(ret >= mod)
ret -= mod; a = a + a;
if(a >= mod)
a -= mod;
b >>= ;
}
return ret;
}
ll quick_pow(ll a, ll b, ll mod) {
ll ret = ;
while(b) {
if(b & )
ret = multi(ret, a, mod);
a = multi(a, a, mod);
b >>= ;
}
return ret;
}
bool Miller_Rabin(ll n) {
ll u = n - , pre, x;
int i, j, k = ;
if(n == || n == || n == || n == || n == )
return true;
if(n == || (!(n % )) || (!(n % )) || (!(n % )) || (!(n % )) || (!(n % )))
return false;
for(; !(u & ); k++, u >>= );
srand(time(NULL));
for(i = ; i < ; i++) {
x = rand() % (n - ) + ;
x = quick_pow(x, u, n);
pre = x;
for(j = ; j < k; j++) {
x = multi(x, x, n);
if(x == && pre != && pre != (n - ))
return false;
pre = x;
}
if(x != )
return false;
}
return true;
} int main() {
cin >>n;
for(int i = n / ; i >= ; i--) {
if(!Miller_Rabin(i) && !Miller_Rabin(n - i)) {
cout <<i <<" " <<n - i <<endl;
return ;
}
}
return ;
}
多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))的更多相关文章
- Codeforces Round #270 1003
Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...
- Codeforces Round #270 1002
Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...
- Codeforces Round #270 1001
Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...
- Codeforces Round #270 A~D
Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...
- Codeforces Round #270 D C B A
谈论最激烈的莫过于D题了! 看过的两种做法不得不ORZ,特别第二种,简直神一样!!!!! 1th:构造最小生成树. 我们提取所有的边出来按边排序,因为每次我们知道边的权值>0, 之后每次把边加入 ...
- Codeforces Round #270
A 题意:给出一个数n,求满足a+b=n,且a+b均为合数的a,b 方法一:可以直接枚举i,n-i,判断a,n-i是否为合数 #include<iostream> #include< ...
- Codeforces Round #270(利用prim算法)
D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...
- codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)
题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...
随机推荐
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
1.为什么要使用SQLite3? •大量数据需要存储 •管理数据,存储数据 SQLite是一种关系型数据库(也是目前移动客户端的主流数据库) 2.SQLite3的几种存储类型 a.NU ...
- C#创建Window服务图解,安装、配置、以及C#操作Windows服务
一.首先打开VS2013,创建Windows服务项目 二.创建完成后对"Service1.cs"重命名位"ServiceDemo":然后切换到代码视图,写个服务 ...
- wine update错误 "the cache has no package" error when wine update is available
网址:https://bugs.launchpad.net/pipelight/+bug/1318321/
- sublime Text3 设置用新标签页打开新的文件
今天用sublime Text3 打开项目文件,发现单击文件就可以打开,但是有一个问题:每次打开新文件就会覆盖当前的标签页,无法在新的标签页打开.于是在网上查了一下. 网上有人说在Preference ...
- 基于c++和opencv底层的图像旋转
图像旋转:本质上是对旋转后的图片中的每个像素计算在原图的位置. 在opencv包里有自带的旋转函数,当你知道倾斜角度theta时: 用getRotationMatrix2D可得2X3的旋转变换矩阵 M ...
- spring cloud 之 客户端负载均衡 Ribbon
一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...
- DELPHI Showmodal 模式窗体
Showmodal 是个函数, Show 是个过程 1. Showmodal: 概念 : 当你调用一个窗口用 SHOWMODAL 时 , 当这个窗口显示出来后 , 程序不会继续自己执行 , 而 ...
- BIO、NIO、AIO通信机制
一.BIO的理解 首先我们通过通信模型图来熟悉下BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立的Acceptor线程负责监听客户端的连接,它接收到客户端的连接请求之后为每个客户端创 ...
- mysql(一) 关联查询的方式
mysql做关联查询时,一般使用join....on.....的语法. 但还有其它两种语法形式,三者的主要区别在于书写形式,其余方面并无太多差异. 如下三种形式: select * from trad ...
- html的body内标签之input系列1
1. Form的作用:提交当前的表单. 类似于去了银行提交的纸质单子,递到后台去办理相关业务. text,password只有输入的功能:button,submit只有点击的功能.想要把这些信息提交, ...