多种方法过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是否 ...
随机推荐
- alpha-4
前言 失心疯病源4 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 19:00~21:50 利用背景相减法完成背景构建与更新模块,查找关于blob更多的论文资 ...
- web 性能测试与报告
web性能测试大家第一都会想到:loadrunner.ab.siege.http_load等工具.但是这些工具生成的测试报告都不是我想要的. 这里给大家推荐一个sitespeed,使用简单,生成非常详 ...
- BZOJ 2115 Xor(线性基)
题意:给定一个n<=50000个点m<=100000条边的无向联通图,每条边上有一个权值wi<=1e18.请你求一条从1到n的路径,使得路径上的边的异或和最大. 任意一条1到n的路径 ...
- c/c++中的关键字(static、const、inline、friend)
static:1.a.c语言中static修饰的局部变量在编译时赋初始值,只赋初始值一次,在函数运行时已有初值,每次调用函数时不用重新赋值,指示保留上次 函 数调用结束时的值. 如果定义局部变量不赋初 ...
- Necklace - CF613C
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of b ...
- 通过logger命令记录日志
通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...
- 配置ssh
1. 实现只允许特定用户ssh登录 1.1. 直接配置ssh来实现 编辑/etc/ssh/sshd_config,在末尾添加如下一行 AllowUsers user1 user2 user3 然后 ...
- 个人vim配置
YouCompletMe支持golang cd .vim/bundle/YouCompleteMe ./install.sh --clang-completer --go-completer clan ...
- [AHOI2009]中国象棋 DP,递推,组合数
DP,递推,组合数 其实相当于就是一个递推推式子,然后要用到一点组合数的知识 一道很妙的题,因为不能互相攻击,所以任意行列不能有超过两个炮 首先令f[i][j][k]代表前i行,有j列为一个炮,有k列 ...
- BZOJ5319 & 洛谷4559 & LOJ2551:[JSOI2018]军训列队——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5319 https://www.luogu.org/problemnew/show/P4559 ht ...