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. ORA-16032和ORA-07286 LOG_ARCHIVE_DEST_1没生效

    主备切换在备库startup时出现归档路径没写到spfile里...注意:修改参数时最好带上scope=spfile或scope=both,以免重启出现异常.SQL> startup mount ...

  2. 12C-使用跨平台增量备份减少可移动表空间的停机时间 (Doc ID 2005729.1)

    12C - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 20057 ...

  3. Drawable.SetDither(Boolean) Method

    https://docs.microsoft.com/zh-cn/dotnet/api/android.graphics.drawables.drawable.setdither?view=xamar ...

  4. 上手OrangePi Zero+

    一.安装系统 所需材料:系统镜像文件.镜像烧录工具.至少2G的内存卡(推荐8G以上).读卡器 1.下载系统镜像 官方系统下载地址,由于官方系统更新截止到17年,所以我选择的是Armbian系统.下载地 ...

  5. HTTP常见响应状态码及解释、常用请求头及解释

    1.HTTP常见响应状态码及解释2XX Success(成功状态码) 200 表示从客户端发来的请求在服务器端被正常处理204 该状态码表示服务器接收的请求已成功处理,但在返回的响应报文中不含实体的主 ...

  6. 再次梳理AMD、CMD、CommonJS、ES6 Module的区别

    AMD AMD一开始是CommonJS规范中的一个草案,全称是Asynchronous Module Definition,即异步模块加载机制.后来由该草案的作者以RequireJS实现了AMD规范, ...

  7. [Vue warn]: Invalid prop: type check failed for prop "fullscreen"

    fullscreen属性是Dialog弹窗中定义是否为全屏 Dialog的属性,element 官方文档中默认值是false ,于是加入是对其赋值 true,然后报了下面的错误: 解决办法:实际上并不 ...

  8. three.js通过canvas实现球体世界平面地图

    概况如下: 1.SphereGeometry实现自转的地球: 2.THREE.CatmullRomCurve3实现球体线条地图点确定: 3.THREE.Math.degToRad,Math.sin,M ...

  9. SpringCloud(九):springcloud——链路追踪springcloud-sleuth

    Spring-Cloud-Sleuth是Spring Cloud的组成部分之一,为SpringCloud应用实现了一种分布式追踪解决方案,其兼容了Zipkin, HTrace和log-based追踪, ...

  10. PHP 7.0 7.3 (Unix) - 'gc' Disable Functions Bypass

    <?php # PHP 7.0-7.3 disable_functions bypass PoC (*nix only) # # Bug: https://bugs.php.net/bug.ph ...