"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 i s 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:
1 2 3
2 6 45 7 80
Sample Output:
1 2 3 4 5 6 7 8 9 10 11 12
Case 1 10 189999 10 279999 10 369999 10 459999 10 549999 10 639999 10 729999 10 819999 10 909999 Case 2 No Solution
#include<vector> #include<iostream> #include<algorithm> #include<cstdio> #include<map> usingnamespace std; int N, K, n, m; intpowint(int n){ int data[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; return data[n-1]; } intsumup(int k){ int sum = 0; for (int i = 0; i < K; i++) { sum += k % 10; k /= 10; } return sum; } boolisprime(int n){ if (n < 2) returnfalse; for (int i = 2; i * i <= n; i++) { if (n%i == 0) returnfalse; } returntrue; } boolfind(int a, int b){ if (a > b) { int t = a; a = b; b = t; } int rr = -1; for (int i = 3; i <= a; i++) { if (b%i == 0 && a%i == 0) { rr = i; } } returnisprime(rr); } structnode { int n, A; }; vector<node>result; map<int, int> n2n; voidfunc(int base, int digt, int tm){ for (int i = 0; i <= 9; i++) { if (digt == K && i == 0) continue; int resM = tm - i; if (resM == 0) { int t = base + i * powint(digt); int n = sumup(t+1); int fd = find(n, m); if (fd) { result.push_back({ n,t }); } } if (resM < 0) { break; } if (resM > 0 && digt > 1 && resM <= digt*9) { func(base + i * powint(digt), digt-1, resM); } } }
intmain(){
cin >> N; for (int in = 0; in < N; in++) { cin >> K >> m; int num = 1; for (int i = 0; i < K - 1; i++) { num *= 10; } cout << "Case " << in + 1 << "\n"; result.clear(); func(0, K, m); if (result.size() == 0) { cout << "No Solution\n"; } else { sort(result.begin(), result.end(), [](node& l, node& r) { if (l.n != r.n) { return l.n < r.n; } else { return l.A < r.A; } }); for (auto it : result) { printf("%d %d\n", it.n, it.A); } } } return0; }
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M\(\$\)). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$$$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:
Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15). Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15). Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15). Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.
If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threthold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.