1104. Forever (20 points)

"Eternal number" refers to a K-digit positive integer A, which satisfies the following conditions: the sum of the digits of A is m, the sum of the digits of A+1 is n, and the greatest common divisor of m and n is greater than 2 prime numbers. This question asks you to find out these everlasting numbers.

Input format:

The input gives a positive integer N (≤5) in the first line, followed by N lines, and each line gives a pair of K (3<K<10) and m (1<m<90), the meaning of which is as described in the title.

Output format:

For each pair of input K and m, first output in one line Case X, where Xis the output number (starting from 1); then output the corresponding n and A in one line, and the numbers are separated by spaces. If the solution is not unique, each set of solutions occupies one line and is output in the increasing order of n; if it is still not unique, it is output in the increasing order of A. If the solution does not exist, it is output on one line No Solution.

Input sample:

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

I used to be a pure C fighter, but when I did the questions later, I found that some questions were too troublesome to implement in pure C. At the same time, I found that C++ is really easy to use, it is really easy to use.

This problem can be solved with deep search + pruning. You can write a demo first, output 000 to 999 in a loop, and then add pruning and judgment conditions, and the topic is completed.

The demo is as follows (not an answer):

After understanding the demo, I can write it myself later.

#include<cstdio>
#include<set>
#include<map>
#include<cmath> 
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int num[10];
//cnt 当前第cnt位  sum 前cnt位的数字累计之和 K查找到多少位 
void dfs(int cnt,int sum,int K){
	//边界条件 到多少层跳出循环,不然无限向下,死循环了 
	//输出 第3位赋值之后 已经达到目标 3位数 
	//第三层 在3位数字赋值之后 还会执行cnt+1,会再进入下一层,而此时作为第四层的开头 cnt为 4 所以为 cnt(4) == K(3)+1 为结束条件 
	if(cnt== (K+1)){
		for(int i=1;i<=K;i++){
			cout<<num[i];
		} 
		cout<<" sum = "<<sum<<endl;
		return;
	}
	
	for(int i=0;i<=9;i++)
	{
		//赋值第cnt位为 i 循环调用 就会把cnt位 分别复制位 0 - 9 挨个往下查询 
		num[cnt]=i;
		//cnt+1 代表搜索下一位数字 sum + i 为累计和  K仅传递值 不做改变,定义为全局变量也行 
		dfs(cnt+1,sum+i,K);
	}
}

int main(){
	//从第1位开始搜索,初始和位0,总共3位 
	dfs(1,0,3);
	return 0;
}

The AC code is as follows:​​​​​​

#include <cstdio>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
int num[15];
//标记 ture表示没有找到解
bool flag = true;
//有序set容器ans pair的作用是可以按照第一个int升序,若相等则按第二个int升序排序
set< pair<int, int>   > ans;

bool isPrime(int num)
{
	//题目要求大于2的素数
	if (num <= 2)
		return false;
	int n = sqrt(num);
	for (int i = 2; i <= n; i++)
		if (num % i == 0)
			return false;
	return true;
}
//求最大公约数
int gcd(int x, int y)
{
	int z = y;
	while (x % y != 0)
	{
		z = x % y;
		x = y;
		y = z;
	}
	return z;
}

int converse(int K)
{
	int sum = 0;
	//从 第 1 位 到 第 N 位 
	for (int i = 1; i <= K; i++)
	{
		sum = sum * 10 + num[i];
	}
	return sum;
}

int getSum(int N)
{
	int sum = 0;
	while (N > 0)
	{
		sum += N % 10;
		N = N / 10;
	}
	return sum;
}



void dfs(int cnt, int K, int sum, int m)
{

	//如果检索到了最后一位
	if (cnt > K)
	{
		//将数组转为整数 A 
		int A = converse(K);
		//求A+1的各位数之和 n
		int n = getSum(A + 1);
		//如果A的各位数实际的和sum ==  目标和 m, 且 m 和 n 的最小公约数是大于2的质数
		if (sum == m && isPrime(gcd(m, n)))
		{
			ans.insert({n, A}); //将符合条件的n和A插入到ans中
			//找到解了
			flag = false;
		}
		return;
	}
    //【 剪枝关键 】
	//如果现有前cnt位数字之和 加上后面尚未列举的数字全部置为 9 的情况下
	//仍然无法使得这个数字的所有位数之和大于等于 m 就无需继续往下查找了 
	//注意这里是 K + 1 -cnt 而不是 K - cnt ,因为当第一位赋为 1 后,进入第二层 dfs ,此时cnt = 2 但是 实际上 第二位还尚未赋值
	//真正赋值是在后面的for循环进行的 所以此时实际上只有第一位 赋值了 累计和 sum 中也只有第一位的和  所以 后续还有 6(K) - 2(此时cnt) + 1 个数字 可以计算为9 
	if (sum + (K + 1 - cnt) * 9 < m)
	{
		return;
	}

	for (int i = 0; i <= 9; i++)
	{
		num[cnt] = i;
		//首位不为 0 
		if(cnt ==1 && i==0){
			continue;
		}
		dfs(cnt + 1, K, sum + i , m);
	}
}

int main()
{
	int N, K, m;
	cin >> N;
	for (int t = 1; t <= N; t++)
	{
		//清空上次计算记录 
		ans.clear();
		//重置  标记 ture表示没有找到解
		flag = true;
		cin >> K >> m;
		cout << "Case " << t << endl;
		//从第一位开始查找,总共K位,初始和位0,目标是和要等于m 
		dfs(1, K, 0, m);
		if (flag)
		{
			cout << "No Solution" << endl;
		}
		else
		{
			//cout<<ans; 注意  <int, int> > 这里 > 和 > 不能相邻 否则会识别为 >>  
			for (set< pair<int, int> >::iterator it = ans.begin(); it != ans.end(); it++) //用迭代器遍历整个容器
			{
				cout << (*it).first << " " << (*it).second << endl;
			}
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_16382227/article/details/125377209