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) N(≤5) . Then N lines follow, each gives a pair of K(3<K<10) K(3<K<10) and m(1<m<90) 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

【声明】

  由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

Solution:

  这道题可以使用暴力,但一定要在过程中进行判断,从而减少循环次数。

  一般暴力的对手就是DFS了,所以这道题使用DFS比较简单

  不过重点就是要及时剪枝,不然和暴力没区别了

  具体的在代码的注释中有说明。

  【突然发现个规律,满足条件的永远是后k-2位都是9,0-1位是和为m- 9*(k-2) 的组合,若这个规律成立,那就简单多了^…^】

 #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int nums, k, m, n;
vector<pair<int, int>>res;
int divisor(int a, int b)//求最大公约数
{
if (a%b == )
return b;
return divisor(b, a % b);
}
bool isPrime(int x)//判断是不是大于的素数
{
if (x <= )return false;
for (int i = ; i*i <= x; ++i)
if (x%i == )return false;
return true;
}
int getSum(int x)
{
int sum = ;
while (x)
{
sum += x % ;
x /= ;
}
return sum;
}
bool check(int a)
{
int b = a + ;
n = ;
while (b)
{
n += b % ;
b /= ;
}
if (isPrime(divisor(m, n)))
return true;
else
return false;
}
void DFS(string &str, int index, int digit, int sum)
{
if (index >= k || sum > m)return;//不能超过k位,从0开始
if (sum + * (k - index - ) < m)return;//第index数字太小,以至于其他位数全部填9其和都达不到m
str[index] = digit + '';//这步要写在前哦,因为sum已经加上了
if (sum == m && index == k - )//满足要求
{
int a = stoi(str.c_str());
if (check(a))
res.push_back(make_pair(n, a));
return;
}
for (int i = ; i < ; ++i)
DFS(str, index + , i, sum + i);
}
int main()
{
cin >> nums;
for (int i = ; i <= nums; ++i)
{
cin >> k >> m;
res.clear();
string str(k, '');
for (int j = ; j < ; ++j)
DFS(str, , j, j);//第一位不能为0
sort(res.begin(), res.end(), [](pair<int, int>v1, pair<int, int>v2) {//排序
if (v1.first == v2.first)
return v1.second < v2.second;
return v1.first < v2.first;
});
printf("Case %d\n", i);
if (res.size() > )
for (auto a : res)
cout << a.first << " " << a.second << endl;
else
printf("No Solution\n");
}
return ;
}

PAT甲级【2019年9月考题】——A1160 Forever【20】的更多相关文章

  1. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...

  2. PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

    7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...

  3. PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】

    7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...

  4. PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】

    7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n  L1=a1→a ...

  5. PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  6. PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

    Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...

  7. PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】

    Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...

  8. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...

  9. 【PAT甲级】1116 Come on! Let's C (20分)

    题意: 输入一个正整数N(<=10000),接着依次输入N个学生的ID.输入一个正整数Q,接着询问Q次,每次输入一个学生的ID,如果这个学生的ID不出现在之前的排行榜上输出Are you kid ...

随机推荐

  1. An easy problem (位运算)

    [题目描述] 给出一个整数,输出比其大的第一个数,要求输出的数二进制表示和原数二进制表示下1的个数相同. [题目链接] http://noi.openjudge.cn/ch0406/1455/ [算法 ...

  2. Codeforces - 1194C - From S To T - 子序列 - 排序

    https://codeforces.com/contest/1194/problem/C 好像没什么好说的,要能构造s必须是t的子序列,并且相差的字符集合d是p的子集. 用双指针法求两遍子序列就可以 ...

  3. 攻防世界--insanity

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/d2a7dde552e647688230e80b2767b912 1.准备 获得信息: ...

  4. 安卓构架组件——向项目添加组件(Adding Components to your Project)

    在开始之前,建议阅读 应用架构指南. Before getting started, we recommend reading the Architecture Components Guide to ...

  5. ELKStack之操作深入(中)

    ELKStack之操作深入(中) 链接:https://pan.baidu.com/s/1V2aYpB86ZzxL21Hf-AF1rA 提取码:7izv 复制这段内容后打开百度网盘手机App,操作更方 ...

  6. 一个历时五天的 Bug

    一个程序员在没有成长成为架构师之前,几乎都要跟 Bug为伴,程序员有很多时间都是花在了查找各种 Bug上. 我印象深刻的一个Bug, 是一个服务器网络框架无锁队列的 Bug .那个 Bug 连续查找了 ...

  7. 转载:Eclipse下的java工程目录

    原文链接:https://www.cnblogs.com/milantgh/p/4029003.html 对新手来讲,一个Java工程内部的多个文件夹经常会让大家困惑.更可恶的是莫名其妙的路径问题,在 ...

  8. centos7在线安装mysql8.0.16

    一.官网复制安装源地址: 1.进入官网地址:https://dev.mysql.com/downloads/repo/yum/ 二.进入/usr/local目录下 ,创建mysql文件夹 三.使用命令 ...

  9. 每天一个Linux命令:locate(19)

    locate locate命令 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中 ...

  10. DOS基础使用专题(强烈推荐)2

    DOS下硬件设备的使用与设置 由于电脑的普及和应用的日益深入,为了满足人们的需要,电脑的功能随着它的发展变得越来越强大,硬件设备也越来越多,如从原来的ISA及PCI声卡.调制解调器等到现在的USB硬盘 ...