1071. Nikifor 2
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Nikifor has a number
x. He doesn't need it. He needs a number
y. Nikifor tries to obtain the required number by erasing some digits from
x. But he is not lucky in the meanwhile. May be he is to choose an appropriate number system?
Write a program that reads numbers
x and
y, and determines a minimal radix of a number system that it is possible to obtain in it the number
y from
x by erasing some digits. If it is impossible, your program should write to an output a message "No solution".
Input
The only line contains integers
x and
y (1 ≤
y <
x ≤ 1 000 000), separated with a space.
Output
Output either the message "No solution", if there is no appropriate number system, or an integer, not less than 2, that is an answer in the problem.
Sample
input | output |
---|---|
127 16 | 3 |
Problem Author: Dmitry Filimonenkov
Problem Source: Ural State Univerisity Personal Contest Online February'2001 Students Session
Problem Source: Ural State Univerisity Personal Contest Online February'2001 Students Session
Tags: none
hide tags for unsolved problems
Difficulty: 568
题意:给出x,y,确定一个最小的进制基数,使在这种进制下,能通过删除x的某些数字,得到y。
分析:仍然是暴力
View Code
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define ft first 30 #define sd second 31 #define mk make_pair 32 33 inline int Getint() 34 { 35 int Ret = 0; 36 char Ch = ' '; 37 bool Flag = 0; 38 while(!(Ch >= '0' && Ch <= '9')) 39 { 40 if(Ch == '-') Flag ^= 1; 41 Ch = getchar(); 42 } 43 while(Ch >= '0' && Ch <= '9') 44 { 45 Ret = Ret * 10 + Ch - '0'; 46 Ch = getchar(); 47 } 48 return Flag ? -Ret : Ret; 49 } 50 51 const int N = 30; 52 int x, y; 53 int arr[N], len1, brr[N], len2; 54 55 inline void Input() 56 { 57 cin >> x >> y; 58 } 59 60 inline void Change(int *a, int &len, int x, int base) 61 { 62 len = 0; 63 while(x) 64 { 65 a[len++] = x % base; 66 x /= base; 67 } 68 } 69 70 inline void Solve() 71 { 72 for(int k = 2; k <= x; k++) 73 { 74 Change(arr, len1, x, k); 75 Change(brr, len2, y, k); 76 77 int index = 0; 78 bool flag = 1; 79 for(int j = 0; j < len2; j++) 80 { 81 while(index < len1 && arr[index] != brr[j]) 82 index++; 83 if(index < len1) index++; 84 else 85 { 86 flag = 0; 87 break; 88 } 89 } 90 if(flag) 91 { 92 printf("%d\n", k); 93 return; 94 } 95 } 96 printf("No solution\n"); 97 } 98 99 int main() 100 { 101 freopen("c.in", "r", stdin); 102 Input(); 103 Solve(); 104 return 0; 105 }