考前两个月疯狂刷题155 * 1.5 + 晴神 20 + 模拟题 * 4 时间投入的可有点大,初试冲啊
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). 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深搜K位数字A并验证A+1之和与A各位数字的最大公约数是大于2的质数
(要不是考试思路太乱 。。。。一个小时就AK) 实际上菜的一笔 ,写完第四题才回来刚第一题,调了一下就过了样例,95分。时间还剩一个小时我开始慌了 70+人过了100分。。。。在坚持不懈的查找bug之下发现判断质数居然跳过了质因数2。怎么说?还不是我自己思路混乱,看到啥写啥,o(︶︿︶)o 唉
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define sget(s) atoi( s.c_str() )
const int M = 1e3;
const int INF = 0x3ffffff;
typedef long long ll;
struct Node{//可以用pair排序
ll a, b;
Node(ll c, ll d){ a = c; b= d;}
};
int cmp(Node a, Node b){
if(a.a != b.a) return a.a < b.a;
else return a.b < b.b;
}
vector< Node > ans;
ll num[20] , k, m, n, a;
ll getnum(){//dfs深搜的K位数字
ll tmp = 0;
for(ll i = 0;i < k; i++){
tmp *= 10;
tmp += num[i];
}
return tmp;
}
ll getsum(){
ll sum = 0;//k位之和
for(ll i = 0; i < k; i++)
sum += num[i];
return sum;
}
ll judge(ll &add2){//判断是否合法
ll tmp = getnum() + 1, p0 = 0, sum2 = 0, sum1 = getsum();
while(tmp){
sum2 += tmp % 10;
tmp /= 10;
}
// tmp = getnum() + 1;
add2 = sum2;
// if(tmp < 199999) cout << tmp << ' ' <<sum1 << ' ' << sum2 << endl;
ll more = 2;
for( ll i = 3; i<= min(sum2, sum1 ); i++ ){
if( sum2 % i == 0 && sum1 % i == 0)
more = i;
}
// if(tmp < 199999) cout <&l