PAT甲级 1160 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). 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

题意

给定数的长度k和数的各位之和m,要求这个数加一之后各位之和为n,n必须和m的gcd大于2

思路

通过观察样例可以看到,可能情况的数,其最后一位为9。
非9可能吗,稍作思考便会发现不可能。因为如果不为9,其加一之后数的各位之和必然也大一,即n=m+1。但任何相邻两个数很显然不可能有大于2的公因子。
所以我们可以得出,这个数最后必须是9。
那么9会带来什么效益呢?末尾是1个9,那么这个数的各位之和会减8,末尾是两个9,各位之和会减9再减8……
那么由此可见,m减去n必然是 8 + 9 ∗ i , i &gt; = 0 8+9*i, i&gt;=0 8+9i,i>=0
那么只需要枚举m的大于2的因子,然后再枚举n,算出9的个数后再去爆搜即可

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4+10;

vector<pair<int, int> > res;
int needlen, n, num9;
int now;

void dfs(int len, int sum)
{
	if (len*9 < sum) return; //剪枝,加不加无所谓 
	if (len == 0)
	{
		if (sum == 0)
		{
			int temp = now;
			for (int i = 0; i < num9; i++) temp = temp*10+9;
			res.push_back(make_pair(n, temp));
		}
		return ;
	}
	if (sum < 0 || len <= 0) return ; //剪枝,加不加无所谓 
	for (int i = 0; i <= 9; i++)
	{
		if (i == 0 && len == needlen || i == 9 && len == 1) continue;
		now = now*10+i;
		dfs(len-1, sum-i);
		now = now/10;
	}
}

int main(void)
{
	int i, j, temp;
	int q; scanf("%d", &q);
	for (j = 1; j <= q; j++)
	{
		printf("Case %d\n", j);
		int k, m; scanf("%d%d", &k, &m);
		set<int> se;
		temp = m;
		for (i = 2; i <= temp; i++)
		{
			if (temp % i == 0)
			{
				se.insert(i);
				temp /= i;
				i--;
			}
		}
		if (*se.rbegin() <= 2)
		{
			printf("No Solution");
			continue;
		}
		res.clear();
		for (auto it = se.begin(); it != se.end(); ++it)
		{
			if (*it > 2)
			{
				int prime = *it;
				for (i = 1; ; i++)
				{
					n = i*prime; if (n >= m) break;
					if ((m-n-8)%9 != 0) continue;
					num9 = (m-n-8)/9+1;
					int len = k-num9, sum = m-num9*9; //将问题转换为长度为len, 数字和为sum的问题
					needlen = len;
					dfs(len, sum);
				}
			}
		}
		sort(res.begin(), res.end());
		if (res.size() == 0) printf("No Solution\n");
		else
		{
			for (i = 0; i < res.size(); i++)
			{
				printf("%d %d\n", res[i].first, res[i].second);
			}
		}
	}
}

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值