题目描述:
"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
主要思路:DFS+剪枝
Tip:(1)因为是按照A和n增序输出的,所以除了每一位都按照1~9遍历暂时没想到其他更好的搜索方法。
(2)对于DFS,剪枝很重要,否则就会超时。
①能算的数都尽量在传参的时候传进去,最后找到一个可行解再算value可能会超时。
②当前位数之和已经等于m的话,不要再搜下去了,直接算value并判断。
③必须进行的剪枝:剩余位数哪怕全填9也满足不了所有位数之和等于m,直接返回,可以大大加快运算。
#include<cstdio>
#include<iostream>
#include<math.h>
#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
vector<int>prime;
const int maxn = 100005;
bool is_prime[maxn + 1];
void init() {
fill(is_prime, is_prime + maxn, true);
is_prime[0] = false;
is_prime[1] = false;
for (int i = 2; i < maxn; i++) {
if (is_prime[i] == true) {
for (int j = i + i; j < maxn; j = j + i)
is_prime[j] = false;
}
}
}
int gcd(int a,int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int temp2num(vector<int>A) {
int sum = 0;
for (int i = 0; i < A.size(); i++) {
sum = sum * 10 + A[i];
}
return sum;
}
struct node {
int value;
int n;
};
vector<int>temp;
vector<node>p;
int cal_sum(int N) {
int n = 0;
while (N) {
n += N % 10;
N = N / 10;
}
return n;
}
//digit从1到K
void DFS(int now_digit,int value, int K, int init_m,int m)
{
if ((K + 1 - now_digit) * 9 < m)
return;
if ( now_digit>K) {
if (m != 0)
return;
int n = 0;
// bool flag = is_forever(temp, K, m,n);
n = cal_sum(value + 1);
int f = gcd(init_m, n);
if (is_prime[f] == true && f > 2) {
node t;
t.value =value;
t.n = n;
p.push_back(t);
}
return;
}
else if (m == 0) {
/* temp.push_back(0);
DFS(now_digit + 1, K, m);
temp.pop_back();*/
for (int i = now_digit; i <= K; i++)
value = value * 10;
int n = 0;
// bool flag = is_forever(temp, K, m,n);
n = cal_sum(value + 1);
int f = gcd(init_m, n);
if (is_prime[f] == true && f > 2) {
node t;
t.value = value;
t.n = n;
p.push_back(t);
}
return;
}
if (now_digit == 1) {
for (int i = 1; i <= 9; i++) {
if (i > m)
continue;
temp.push_back(i);
DFS(now_digit+ 1, value * 10 + i, K, init_m, m - i);
temp.pop_back();
}
}
else {
for (int i = 0; i <= 9; i++) {
if (i > m)
continue;
temp.push_back(i);
DFS(now_digit + 1, value*10+i,K, init_m, m - i);
temp.pop_back();
}
}
return;
}
bool cmp(node a, node b) {
if (a.n != b.n)
return a.n < b.n;
else
return a.value < b.value;
}
int main() {
int n;
scanf("%d", &n);
int K, m;
init();
int index = 1;
for (int i = 0; i < n; i++)
{
scanf("%d %d", &K, &m);
printf("Case %d\n", index);
index++;
temp.clear();
p.clear();
DFS(1,0, K, m,m);
sort(p.begin(), p.end(), cmp);
if (p.size() == 0)
printf("No Solution");
else {
for (int i = 0; i < p.size(); i++) {
printf("%d %d", p[i].n, p[i].value);
if (i != p.size() - 1)
printf("\n");
}
}
if (i != n - 1)
printf("\n");
}
return 0;
}
1093

被折叠的 条评论
为什么被折叠?



