PAT 1160 Forever

PAT(甲级)2019年秋季考试 (第一题)

7-1 Forever (20 分)

“Forever number” is a positive integer A with K digits, satisfying the following constrains:

the sum of all the digits of A is m;
the sum of all the digits of A+1 is n; and
the greatest common divisor of m and n is a prime number which is greater than 2.
Now you are supposed to find these forever numbers.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3<K<10) and m (1<m<90), of which the meanings are given in the problem description.

Output Specification:

For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution

看起来很简单,但实际有坑的一道题,主要考数学常识。如果直接从给个某个位数到下一位数找必定最后一个测试点超时,因为二者之间存在很多根本不可能满足要求的数,对它们求公约数浪费大量时间。假定两个数1087,1088,显然这两位的m=16和n=17只相差1,最大公约数只能为1,同理1089和1090的m=18和n=10只相差8,对于这一类,最大公约数只能为1,2,4,8,而1099的m和n相差为17,因此能满足要求的尾数至少为99。

#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int sumdigit(int x) {return !x?0:sumdigit(x/10) + x%10;
}
int gcd(int a, int b) {return !b?a:gcd(b, a%b);
}bool isprime(int x){if (x<=3) return false;int sqr=sqrt(x);for(int i=2; i<=sqr; i++) {if (x%i==0) return false;}return true;
}
struct node{int n, num;
};
bool cmp(const node &a, const node &b) {if (a.n!=b.n) return a.n<b.n;return a.num<b.num;
}int main() {int N, a, b, x, y, m, n;scanf("%d", &N);for (int i=1; i<=N; i++) {scanf("%d%d", &a, &m);printf("Case %d\n", i);x=pow(10, a-1);y=x*10;vector<node> vi;for (int j=x; j<y; j++) {if (j%100!=99) continue;if (sumdigit(j)==m) {n=sumdigit(j+1);if (isprime(gcd(n, m))) {vi.push_back(node{n, j});}}}sort(vi.begin(), vi.end(), cmp);for (auto &i: vi){printf("%d %d\n", i.n, i.num);}if (vi.empty()) printf("No Solution\n");}return 0;
}

PAT 1160 Forever相关推荐

  1. PAT A1160 Forever

    1160 Forever 分数 20 作者 陈越 单位 浙江大学 "Forever number" is a positive integer A with K digits, s ...

  2. 【PTA Advanced】1160 Forever(C++)

    目录 题目 Input Specification: Output Specification: Sample Input: Sample Output: 思路 代码 题目 "Forever ...

  3. 1160 Forever (20 分)

    正解应该是dfs剪纸. 但是,第一题这样做说实话接受不了.这题可以通过暴力来做,不过有技巧. 思路:在给定的K位数中找到符合的存入数组,排序后输出. 怎么找?若k=10,100000000~99999 ...

  4. 2020年9月PAT甲级满分必备刷题技巧

    2020年7月的考试结束了,除了本次的考题更新,短期内不会更新. [7月题目的特点:首次线上考试,没出链表.树相关的模板题,第2到4题背景新颖,大大降低了抄袭历年代码的可能性,可以看作是线上考试的新趋 ...

  5. 力扣PTA~每天至少三题

    文章目录 算法 双指针 平方数之和 167.两数之和-输入有序数组 Reverse Vowels of a string 二分法 153. Find Minimum in Rotated Sorted ...

  6. 19年秋季第一题 PAT甲级 1161 Forever (20 分) 有点儿意思

    如果喜欢我的文章请点赞让我知道噢 题目 7-1 Forever (20 分) "Forever number" is a positive integer A with K dig ...

  7. 2019秋季PAT甲级_C++题解

    2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...

  8. 算法唯手熟尔(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/3/5 PAT A1119_C 2020/3/6 PAT A1123_C PAT A1115_C PAT A1114_C leetcode 206. 反转链表_C leetcode ...

  9. 2019年9月 第一次参加PAT考试体验及题解

    2020-12-16 更新: 时间过得好快,大四上学期已经上完了. 楼主现在保研本校(北京邮电大学)计算机科学与技术(计算机学硕),计算机网络+机器学习方向,现在国内经济不景气,我在学校深深体会到了学 ...

  10. 2019年12月PAT甲级满分备考经验

    PAT甲级满分备考经验 答题过程 备考经验 答题过程   总得来说,我觉得我幸运,碰上了一次PAT甲级题目相当简单(1085中有190人满分),最终提前一小时交卷,实时排名为35.   我按照1 2 ...

最新文章

  1. DNS自述:我是如何为域名找到家的
  2. 【HTTP协议】HTTP状态码列表大全
  3. 常见算法的python实现(Github标星75.5k+)
  4. 解决IE6下 position的fixed定位问题
  5. 李宏毅机器学习课程---2、Regression - Case Study
  6. 消息中间件系列(八):Kafka、RocketMQ、RabbitMQ等的优劣势比较
  7. 安卓案例:View动画——弹球碰壁
  8. listview选中高亮
  9. 微信小程序开发基础知识总结
  10. 贝叶斯(Bayes)决策理论
  11. html5理财计算,理财产品的收益怎么算(个人理财收益计算器)
  12. 深入理解C#面向对象之继承
  13. 计算机游戏cpu,千元级战斗机游戏CPU体验,大学4年够你天天开黑了
  14. 基于动态邻域的切换粒子群优化算法
  15. python挂机脚本怎么运行,Python实现自动挂机脚本(基础篇)
  16. glog logging library for C++
  17. 3dmax室内灯光强度一般打多少才合适
  18. Ubuntu ROS 安装
  19. Windows自带远程桌面和远程协助用法
  20. python模块学习笔记

热门文章

  1. c语言中错误c2084,错误_C2084_函数“int multi(int)”已有主体
  2. html显示当前脚本错误,访问网页总是提示当前网页脚本发生错误怎么办?
  3. Mac如何共享wifi热点
  4. 解决 虚拟机安装 centos8 时出现设置基础软件仓库时出错
  5. echars折线图y轴自定义数值会被转换为整数的问题
  6. 如何做社群运营才能提高用户的活跃度?
  7. layui模板引擎 (laytpl) 与Flask模板引擎 (jinja2)冲突的问题
  8. docker容器必须要有前台进程
  9. reference to ‘ max‘ is ambiguous
  10. 计算机缓存怎样更改,计算机的缓存大小在哪设置?