CF1269A Equation

题意

要找两个合数,使他们两个的差为\(n\),\(n\)为题目给出的数

思路

我们可以枚举减数\(now\),判断一下是不是质数,如果是质数就让\(now++\),然后用一个数\(tot\)记录被减数,也就是\(now\)加\(n\),判断\(tot\)是不是质数,如果是质数再让\(now++\),如果不是质数我们就找到了答案,因为我们已经保证了\(now\)为合数,所以就可以直接跳出循环输出答案啦~

因为数据范围并不大,所以我们可以直接判断一个数是不是质数,如下

//判断是否为质数,是质数返回1,否则返回0
bool check(int wh) {
if(wh <= 1) return 0;
if(wh == 2) return 1;
for(int i = 2; i * i <= wh; i++) if(wh % i == 0) return 0;
return 1;
}

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f; inline int read() {
char c = getchar(); int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
} //判断是否为质数,是质数返回1,否则返回0
bool check(int wh) {
if(wh <= 1) return 0; if(wh == 2) return 1;
for(int i = 2; i * i <= wh; i++) if(wh % i == 0) return 0;
return 1;
} int main() {
int n = read(), now = 2, tot;
while(1) {
if(check(now)) now++;
//判断now是否为质数,如果是质数就++,否则继续判断tot的值
else {
tot = now + n;
if(check(tot)) now++;
//判断tot是否为质数,如果不是质数就可以跳出输出答案了
else break;
}
}
return cout << tot << " " << now << '\n', 0;
}

CF1269A Equation的更多相关文章

  1. CodeForces460B. Little Dima and Equation

    B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. ACM: FZU 2102 Solve equation - 手速题

     FZU 2102   Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  3. HDU 5937 Equation

    题意: 有1~9数字各有a1, a2, -, a9个, 有无穷多的+和=. 问只用这些数字, 最多能组成多少个不同的等式x+y=z, 其中x,y,z∈[1,9]. 等式中只要有一个数字不一样 就是不一 ...

  4. coursera机器学习笔记-多元线性回归,normal equation

    #对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...

  5. CF460B Little Dima and Equation (水题?

    Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...

  6. Linear regression with multiple variables(多特征的线型回归)算法实例_梯度下降解法(Gradient DesentMulti)以及正规方程解法(Normal Equation)

    ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ...

  7. ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分

    Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  8. [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...

  9. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. Docker Compose部署GitLab服务,搭建自己的代码托管平台(图文教程)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. 在Vue中添加css扩展语言sass

    npm install vue-loader --save-dev npm install node-sass --save-dev npm install sass-loader --save-de ...

  3. [转]BEC Vantage

    https://www.examenglish.com/BEC/BEC_Vantage.html https://www.cambridgeenglish.org/exams-and-tests/bu ...

  4. iOS利用剪切板在app中传递信息

    利用iOS剪切板在app中传递信息 App1 中添加URLSchemes   app1 App2 中国添加URLSchemes   app2 App1中进入app2: UIApplication.sh ...

  5. java 初学 :求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一个数字。几个 数相加由键盘控制。

    import java.util.Scanner; public static void main(String[] args) {       Scanner input=new Scanner(S ...

  6. 解读并加工BeautifulReport 报告模板

    使用unittest框架的脚本执行完成后,会生成一个html格式的报告 这个报告是提前制作了一个html的模板,然后将对应的内容写入到模板中,并生成一个最终的报告,这个报告模板在通过 pip inst ...

  7. 10-Node.js学习笔记-异步函数

    异步函数 异步函数是异步编程语法的终极解决方案,它可以让我们将异步代码写成同步的形式,让代码不再有回调函数嵌套,是代码变得清晰明了 const fn = async()=>{} async fu ...

  8. 使用原生代码实现一个Events模块,可以实现自定义事件的订阅、触发、移除功能

    function Events() { // 放置所有添加的 监听事件 this._events = {} } Events.prototype = { on: function (name, fn, ...

  9. Java连载55-Mail编程

    一.电子邮件的历史 1.起源: 1969 Lenoard K. 教授发给同事的“LO” 1971 美国国防部自主的阿帕网(Arpanet)的通讯机制 通讯地址里用@ 1987年中国的第一份电子邮件 “ ...

  10. Redis令牌桶限流

    一 .场景描述 在开发接口服务器的过程中,为了防止客户端对于接口的滥用,保护服务器的资源, 通常来说我们会对于服务器上的各种接口进行调用次数的限制.比如对于某个 用户,他在一个时间段(interval ...