7-1 Forever (20 minutes)
"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
【statement】
Since this question has not been on the official website PAT exam, there is no test set is only through a sample, if errors are found, correct me thank comments.
Solution:
This question can be violent, but it must be judged in the process, thereby reducing cycle times.
DFS is the opponent violence in general, so this question is relatively simple to use DFS
But the point is to timely pruning, or no distinction between violent and
In particular there is described code comments.
[Suddenly found a law, the condition is always after k-2 bits are bits is 9,0-1 and for the m- 9 * (k-2) in combination, if this rule was established, it would be much simpler ^ ^ ...]
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 using namespace std; 6 int nums, k, m, n; 7 vector<pair<int, int>>res; 8 int divisor(int a, int b)//求最大公约数 9 { 10 if (a%b == 0) 11 return b; 12 return divisor(b, a %B); 13 is } 14 BOOL the isPrime ( int X) // judgment is not a prime number greater than 15 { 16 IF (X <= 2 ) return to false ; . 17 for ( int I = 2 ; I * I <= X; ++ I) 18 is IF (% I X == 0 ) return to false ; . 19 return to true ; 20 is } 21 is int GetSum ( int X) 22 is { 23 is int sum = 0; 24 while (x) 25 { 26 sum += x % 10; 27 x /= 10; 28 } 29 return sum; 30 } 31 bool check(int a) 32 { 33 int b = a + 1; 34 n = 0; 35 while (b) 36 { 37 n += b % 10; 38 B / = 10 ; 39 } 40 IF (the isPrime (divisor (m, n-))) 41 is return to true ; 42 is the else 43 is return to false ; 44 is } 45 void the DFS ( String & STR, int index, int digit for, int SUM) 46 is { 47 IF (index> SUM = k ||> m) return ; // no more than k bits, starting from 0 48 IF (+ SUM . 9 * (k - index - . 1 ) <m)return ; // first index number is too small to fill all of the other bits and which are lower than 9 m 49 STR [index] = digit for + ' 0 ' ; // this step preceding write Oh, because the sum has been added 50 IF (SUM m && index == K == - . 1 ) // satisfy 51 is { 52 is int a = Stoi (str.c_str ()); 53 is IF (Check (a)) 54 is res.push_back ( the make_pair (n-, A)); 55 return ; 56 is } 57 is for ( int I = 0 ; I < 10 ; ++i) 58 DFS(str, index + 1, i, sum + i); 59 } 60 int main() 61 { 62 cin >> nums; 63 for (int i = 1; i <= nums; ++i) 64 { 65 cin >> k >> m; 66 res.clear(); 67 string str(k, '0'); 68 for (int j = 1; j < 10; ++j) 69 DFS(str, 0, j, j);//第一位不能为0 70 sort(res.begin(), res.end(), [](pair<int, int>v1, pair<int, int>v2) {//排序 71 if (v1.first == v2.first) 72 return v1.second < v2.second; 73 return v1.first < v2.first; 74 }); 75 printf("Case %d\n", i); 76 if (res.size() > 0) 77 for (auto a : res) 78 cout << a.first << " " << a.second << endl; 79 else 80 printf("No Solution\n"); 81 } 82 return 0; 83 }