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 ;
}

最新文章

  1. Linux学习日记(二)
  2. Bootstap datetimepicker报错TypeError: intermediate value
  3. &lt;java基础学习&gt;JAVA 对象和类
  4. 关于&lt;/div&gt;的粗浅理解
  5. leetcode51. N-Queens
  6. linux文件系统---10
  7. 基于SMB协议的共享文件读写 博客分类: Java
  8. VB调用自持字体
  9. Spark系列(四)整体架构分析
  10. ios开发之通知事件
  11. 使用自定义类型做qmap,qhash的key
  12. DDD领域驱动之干活(四)补充篇!
  13. Fibonacci Numbers
  14. Python实现常用排序算法
  15. JavaBean动作元素
  16. MongoDB、redis、memcached
  17. js setTimeout setInterval 第三个参数说明
  18. bootstrap 兼容哪些浏览器
  19. 【数组】Unique Paths II
  20. 20145216史婧瑶《Java程序设计》第三次实验报告

热门文章

  1. [Bzoj1008][HNOI2008]越狱(组合计数)
  2. 2.etcd集群的安装(cfssl版)
  3. thinkphp开发微信小程序后台流程
  4. k8s ingress路由强制跳转至https设置
  5. 【JAVA】 03-Java中的异常和包的使用
  6. 浏览器报406 错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request &quot;accept&quot; headers
  7. ActiveMQ修改连接的用户名密码
  8. Jenkins ant打包部署
  9. &lt;select multiple=&quot;multiple&quot;&gt; 数据回显
  10. $Noip$前的小总结哦