去年742,今年72,也算一种小小的进步。
明年前30(笑
1. Drawing Rooted Binary Trees
给定一个树的中序和前序的遍历,要求输出这棵树(包括空格的)
1 #include <iostream>// 2 #include <string.h>// 3 #include <stdio.h>// 4 #include <stdlib.h>// 5 #include <math.h>// 6 #include <string.h>// 7 #include <vector>//STL vetor 8 #include <list>//STL list 9 #include <map>// STL map 10 #include <queue>// STL queue 11 #include <set>// STL set 12 #include <stack>//sTL stack 13 #include <bitset>//bitset 14 #include <algorithm>//STL 15 #include <numeric>// 16 #include <functional>//STL 17 #include <limits.h>// 18 #include <fstream> 19 using namespace std; 20 typedef int LL; 21 int Tr[30][4]; 22 int n; 23 char z[30]; 24 char q[30]; 25 int D; 26 int w; 27 int DFS(int L,int R,int x,int d){ 28 int i; 29 Tr[ q[x]-'a' ][3]=d; 30 int xx=x; 31 // printf("%d %d %c %d xx=%d,w=%d ",L,R,q[x],d,x,w); 32 if (L==R){ 33 D=max(D,d); 34 Tr[q[x]-'a'][0]=Tr[q[x]-'a'][1]='@'; 35 Tr[q[x]-'a'][2]=w; 36 w++; 37 return x; 38 } 39 for (i=L;i<=R;i++){ 40 if (z[i]==q[x]) break; 41 } 42 if (i!=L){ 43 Tr[q[x]-'a'][0]=q[x+1]; 44 x++; 45 x=DFS(L,i-1,x,d+1); 46 }else{ 47 Tr[q[x]-'a'][0]='@'; 48 } 49 Tr[q[xx]-'a'][2]=w; 50 w++; 51 if (i!=R){ 52 Tr[ q[xx]-'a'][1]=q[x+1]; 53 x=DFS(i+1,R,x+1,d+1); 54 }else{ 55 Tr[q[xx]-'a'][1]='@'; 56 } 57 return x; 58 } 59 char Out[30]; 60 int main(){ 61 62 while(scanf("%s%s",z,q)!=EOF){ 63 n=strlen(z); 64 D=1; 65 w=0; 66 DFS(0,n-1,0,1); 67 //printf("%d ",D); 68 // for (int j=0;j<n;j++){printf("%c L=%c R=%c %d D=%d ",q[j],Tr[ q[j]-'a' ][0], Tr[ q[j]-'a' ][1],Tr[ q[j]-'a' ][2],Tr[ q[j]-'a' ][3]); } 69 for (int i=1;i<=D;i++){ 70 for (int j=0;j<n;j++){ Out[j]=' ';} 71 Out[n]=0; 72 for (int j=0;j<n;j++){ 73 if ( Tr[ q[j]-'a' ][3]==i ){ 74 Out[ Tr[q[j]-'a' ][2] ]=q[j]; 75 } 76 } 77 printf("%s ",Out); 78 } 79 } 80 return 0; 81 }
2. Barter System
Standard input
The first line of the input is an integer N which indicates the number of given exchange rates to follow. The next N lines consists of A, B, rA,B,r triplets where A and B are the commodities and r is the exchange rate such that A = rB {mod}A=r⋅B (mod 998244353).
The next line contains another integer QQ which represents the number of queries. The following Q lines consists of a pair of commodities K and L.
Standard output
For each of the Q queries you need to find r such that $K = r cdot LK=r⋅L$.
It can be shown that rr can be represented as $frac{X}{Y}YX$, where X and Y are coprime integers and $X e 0 ( ext{mod} 998244353)X≠0 (mod 998244353)$. For each query print $X cdot Y^{-1} ( ext{mod} 998244353)X⋅Y−1 (mod 998244353).$
If the exchange rate can not be computed using the given information, print -1
.
Constraints and notes
- $1 leq N, Q leq 2 cdot 10^41≤N,Q≤2⋅104$
- $1 leq |A|, |B| leq 501≤∣A∣,∣B∣≤50$
- $1 leq r < 9982443531≤r<998244353$
- All exchange rates are consistent
1 #pragma GCC optimize(3) 2 #include <bits/stdc++.h> 3 #include <unordered_map> 4 using namespace std; 5 6 const double EPS = 1e-9; 7 const int INF = 2147483647; 8 const int LLINF = 9223372036854775807; 9 const double PI = acos(-1.0); 10 11 #define REP(i, a, b) for(int i = (a); i < (b); i++) 12 #define PER(i, a, b) for(int i = (a); i > (b); i--) 13 #define FOREACH(i,t) for(typeof(t.begin()) i=t.begin(); i!=t.end(); i++) 14 #define NEED_CLOCK printf("Time: %f ",double(clock())/CLOCKS_PER_SEC) 15 #define MP(x, y) make_pair(x, y) 16 #define PB(x) push_back(x) 17 #define SET(a) memset(a,-1,sizeof(a)) 18 #define CLR(a) memset(a,0,sizeof(a)); 19 #define MEM(a,x) memset(a,x,sizeof(a)) 20 #define ALL(x) begin(x),end(x) 21 #define LL long long 22 #define Lson (index * 2) 23 #define Rson (index * 2 + 1) 24 #define pii pair< int, int > 25 #define pll pair< LL, LL > 26 #define MAXN 21000+5 27 28 template< class T > inline T _abs(T a) { return a >= 0 ? a : -a; } 29 template< class T > inline T sqr(T a) { return a*a; } 30 template< class T > inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % b)); } 31 template< class T > inline T lcm(T a, T b) { return ((a) / gcd((a), (b))*(b)); } 32 template< class T > inline T lowbit(T x) { return x&-x; } 33 34 inline int READ() { 35 char ch; 36 while ((ch = getchar()) < 48 || 57 < ch); 37 int ans = ch - 48; 38 while (48 <= (ch = getchar()) && ch <= 57) 39 ans = (ans << 3) + (ans << 1) + ch - 48; 40 return ans; 41 } 42 ///************************************START**************************************/// 43 const long long MOD = 998244353; 44 int pre[MAXN]; 45 LL dis[MAXN]; 46 int findSet(int x) { 47 if (pre[x] == x)return x; 48 if (x < 0)return -1; 49 int ths = pre[x]; 50 int rhs = findSet(pre[x]); 51 if (ths >= 0 && rhs >= 0) 52 pre[x] = rhs; 53 dis[x] = (dis[x] * dis[ths]) % MOD; 54 return rhs; 55 } 56 LL poww(LL x, LL n) 57 { 58 LL res = 1; 59 while (n > 0) 60 { 61 if (n % 2 == 1) 62 { 63 res = res*x; 64 res = res%MOD; 65 } 66 x = x*x; 67 x = x%MOD; 68 n >>= 1; 69 } 70 return res; 71 } 72 LL inv(LL x) { 73 return poww(x, MOD - 2); 74 } 75 string s1, s2; 76 unordered_map<string, int>mp; 77 int main() { 78 int n; scanf("%d", &n); 79 //MEM(dis, 1); 80 for (int i = 0; i < n+100; i++){ 81 pre[i] = i; 82 dis[i] = 1; 83 } 84 int id = 1; 85 while (n--) { 86 LL rate; 87 cin >> s1 >> s2 >> rate; 88 if (mp.find(s1)==mp.end())mp[s1] = id++; 89 if (mp.find(s2)==mp.end())mp[s2] = id++; 90 int fx = findSet(mp[s1]), fy = findSet(mp[s2]); 91 dis[fy] = (1ll * (dis[mp[s1]] * rate) % MOD*inv(dis[mp[s2]])) % MOD; 92 pre[fy] = fx; 93 } 94 int q; scanf("%d", &q); 95 while (q--) { 96 cin >> s1 >> s2; 97 if (s1 == s2) { 98 printf("1 "); 99 continue; 100 } 101 if (mp.find(s1) == mp.end() || mp.find(s2) == mp.end()) { 102 printf("-1 "); 103 continue; 104 } 105 int fx = findSet(mp[s1]), fy = findSet(mp[s2]); 106 if (fx != fy) { 107 printf("-1 "); 108 continue; 109 } 110 else { 111 printf("%lld ", (dis[mp[s2]] * inv(dis[mp[s1]])) % MOD); 112 } 113 } 114 return 0; 115 }
3. Troll Coder
You have found a huge treasure on an island, but the only access point to that island is a bridge guarded by a giant Troll! In order to cross the bridge, you have to guess a sequence of N bits by submitting queries. For each query, the Troll will tell you how many bits you guesses correctly until you guess the correct sequence.
Interaction
Your program must exchange information with the Troll by submitting queries and reading answers.
Note that you must flush the buffer so that the output reaches the Troll. Here we ilustrate it for several languages.
At the beginning of each test case, the Troll will give you a single integer N which will represent the length of the sequence.
To submit a query, your program should output the letter Q
followed by a space and by a binary sequence of length N with each bit separated by a space. After each query you will receive an integer denoting the number of correct bits. The last submission will be your final answer and it should start with an A
followed by a space and by a binary sequence of length N with each bit separated by a space.
Constraints and notes
- This task is NOT adaptive
- $1 le N le 1001≤N≤100 $
- Your program can submit at most N + 1 queries before arriving at the correct answer.
1 #include<iostream> 2 using namespace std; 3 int a[110]; 4 int n,cnt,tmp; 5 int flip(int &num){ 6 if(num==0){ 7 num=1; 8 return 1; 9 }else{ 10 num=0; 11 return 0; 12 } 13 } 14 int main() 15 { 16 cin>>n; 17 cout<<"Q "; 18 for(int i=0;i<n-1;++i){ 19 a[i]=0; 20 cout<<a[i]<<" "; 21 } 22 a[n-1]=0; 23 cout<<a[n-1]<<endl; 24 cin>>cnt; 25 if(cnt==n){ 26 cout<<"A "; 27 for(int i=0;i<n-1;++i){ 28 cout<<a[i]<<" "; 29 } 30 cout<<a[n-1]<<endl; 31 }else if(cnt==0){ 32 cout<<"A "; 33 for(int i=0;i<n-1;++i){ 34 cout<<flip(a[i])<<" "; 35 } 36 cout<<flip(a[n-1])<<endl; 37 }else{ 38 for(int i=0;i<n;++i){ 39 cout<<"Q "; 40 for(int j=0;j<n-1;++j){ 41 if(i!=j) cout<<0<<" "; 42 else cout<<1<<" "; 43 } 44 if(i!=n-1) cout<<0<<endl; 45 else cout<<1<<endl; 46 cin>>tmp; 47 if(tmp>cnt){ 48 a[i]=1; 49 } 50 } 51 cout<<"A "; 52 for(int i=0;i<n-1;++i){ 53 cout<<a[i]<<" "; 54 } 55 cout<<a[n-1]<<endl; 56 57 } 58 return 0; 59 }
4. Xtreme Fake Coins
Help IBM research's puzzlemaster to verify solutions for May 2018's challenge.
There are NN coins, represented by the first NN capital letters of the English alphabet. Exactly two of them are counterfeit and have a slightly smaller weight. MM weightings using a double-pan balance scale have been performed, but they may not uniquely determine the pair of counterfeit coins.
Find all counterexamples of two pairs of coins ((a, b), (c, d)) (a < b, a leq c, c < d, (a, b) eq (c, d))((a,b),(c,d)) (a<b, a≤c, c<d, (a,b)≠(c,d)) whose weights are indistinguishable with respect to the MM weightings.
Standard input
The first line contains two comma separated integers, NN and MM.
The next MM lines contain two strings of disjoint subsets of the first NN English capital letters, separated by a -
sign.
There always is an equal amount of coins on both sides.
Standard output
List of lexicographically ordered counterexamples for the solution.
Each of them consists of two letters, an =
sign and then another two letters.
A counterexample is a set of two pairs that cannot be distinguished by the set of MM weightings.
Constraints and notes
- 0 leq M leq 100≤M≤10
- 2 leq N leq 262≤N≤26
- The counterexamples should be formed using only the first NN letters
1 #pragma GCC optimize(3) 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 const double EPS = 1e-9; 6 const int INF = 2147483647; 7 const int LLINF = 9223372036854775807; 8 const double PI = acos(-1.0); 9 10 #define REP(i, a, b) for(int i = (a); i < (b); i++) 11 #define PER(i, a, b) for(int i = (a); i > (b); i--) 12 #define FOREACH(i,t) for(typeof(t.begin()) i=t.begin(); i!=t.end(); i++) 13 #define NEED_CLOCK printf("Time: %f ",double(clock())/CLOCKS_PER_SEC) 14 #define MP(x, y) make_pair(x, y) 15 #define PB(x) push_back(x) 16 #define SET(a) memset(a,-1,sizeof(a)) 17 #define CLR(a) memset(a,0,sizeof(a)); 18 #define MEM(a,x) memset(a,x,sizeof(a)) 19 #define ALL(x) begin(x),end(x) 20 #define LL long long 21 #define Lson (index * 2) 22 #define Rson (index * 2 + 1) 23 #define pii pair< int, int > 24 #define pll pair< LL, LL > 25 #define MOD ((int)1000000007) 26 #define MAXN 300000 27 28 template< class T > inline T _abs(T a) { return a >= 0 ? a : -a; } 29 template< class T > inline T sqr(T a) { return a*a; } 30 template< class T > inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % b)); } 31 template< class T > inline T lcm(T a, T b) { return ((a) / gcd((a), (b))*(b)); } 32 template< class T > inline T lowbit(T x) { return x&-x; } 33 34 inline int READ() { 35 char ch; 36 while ((ch = getchar()) < 48 || 57 < ch); 37 int ans = ch - 48; 38 while (48 <= (ch = getchar()) && ch <= 57) 39 ans = (ans << 3) + (ans << 1) + ch - 48; 40 return ans; 41 } 42 ///************************************START**************************************/// 43 string g[MAXN][2]; 44 string ans[MAXN][2]; 45 int n, m; 46 char c[MAXN]; 47 48 vector<string>v; 49 void generate() { 50 for (int i = 0; i < n; i++) { 51 for (int j = i + 1; j < n; j++) { 52 string a; 53 a.push_back('A' + i); 54 a.push_back('A' + j); 55 v.push_back(a); 56 } 57 } 58 return; 59 } 60 61 int check(int k, string s) { 62 int l0 = g[k][0].find(s[0]) == -1 ? 0 : 1; 63 int r0 = g[k][1].find(s[0]) == -1 ? 0 : 1; 64 int l1 = g[k][0].find(s[1]) == -1 ? 0 : 1; 65 int r1 = g[k][1].find(s[1]) == -1 ? 0 : 1; 66 67 int left = l0 + l1, right = r0 + r1; 68 if (left == right)return 0; 69 else if (left > right)return 1; 70 else if (left < right)return 2; 71 } 72 73 int main() { 74 scanf("%d,%d", &n, &m); 75 generate(); 76 77 for (int i = 0; i < m; i++) { 78 scanf("%s", c); 79 int j = 0; 80 string a, b; 81 for (j = 0; j < strlen(c); j++) { 82 if (c[j] != '-') a.push_back(c[j]); 83 else break; 84 } 85 for (j += 1; j < strlen(c); j++) { 86 b.push_back(c[j]); 87 } 88 g[i][0] = a; 89 g[i][1] = b; 90 } 91 92 int cnt = 0; 93 for (int i = 0; i < v.size(); i++) { 94 for (int j = i + 1; j < v.size(); j++) { 95 //cout << v[i] << " " << v[j] << endl; 96 int flag = 1; 97 for (int k = 0; k < m; k++) { 98 if (check(k, v[i]) != check(k, v[j])){ 99 flag = 0; 100 break; 101 } 102 } 103 if (flag) { 104 ans[cnt][0] = v[i]; 105 ans[cnt][1] = v[j]; 106 cnt++; 107 } 108 } 109 } 110 111 for (int i = 0; i < cnt; i++) { 112 cout << ans[i][0] << '=' << ans[i][1] << endl; 113 } 114 115 return 0; 116 }
5. Magic Spell
Write a program that gives the correct output for a legal input – to discover the correct function see the input/output examples.
Standard Input
The first line contains an integer NN. Each of the following NN lines will contain a legal string.
Standard output
Print the answer on the first line. It is guaranteed that it can be represented as a signed 6464 integer.
1 #include <iostream>//数据输入输出流 2 #include <string.h>//字符串操作函数 3 #include <stdio.h>//C的输入输出 4 #include <stdlib.h>//定义杂项函数及内存分配函数 5 #include <math.h>//C中的数学函数 6 #include <string.h>//c++中的string类 他不能用strcpy等c函数去操作 7 #include <vector>//STL vetor容器 8 #include <list>//STL list 9 #include <map>// STL map 10 #include <queue>// STL queue 11 #include <set>// STL set 12 #include <stack>//sTL stack 13 #include <bitset>//bitset可按位定义串//比如:bitset <1000> all;定义一个1000位的串 14 #include <algorithm>//STL各种算法 比如 swap sort merge max min 比较 15 #include <numeric>//常用数字操作 一般和algorithm搭配使用 16 #include <functional>//STL定义运算函数(代替运算符) 17 #include <limits.h>//定义各种数据类型最值常量 18 #include <fstream> 19 #include <cstring> 20 using namespace std; 21 typedef int LL; 22 int a[20]; 23 LL Get(char *s) { 24 if (strcasecmp(s, "xrtp") == 0) return 0ll; 25 if (strcasecmp(s, "pmr") == 0) return 1ll; 26 if (strcasecmp(s, "yep") == 0) return 2ll; 27 if (strcasecmp(s + 1, "jtrr") == 0) return 3ll; 28 if (strlen(s) == 4 && s[0] == 'g' &&s[1] == 'p') return 4ll; 29 if (strcasecmp(s, "gobr") == 0) return 5ll; 30 if (strlen(s) == 3 && s[0] == 'd' && s[1] == 'o') return 6ll; 31 if (strcasecmp(s, "drbrm") == 0) return 7ll; 32 if (strcasecmp(s, "rohjy") == 0) return 8ll; 33 if (strcasecmp(s, "momr") == 0) return 9ll; 34 if (strcasecmp(s, "yrm") == 0) return 10ll; 35 if (strcasecmp(s + 2, "rbrm") == 0) return 11ll; 36 if (strlen(s) == 6 && s[0] == 'y' &&s[1] == 'e'&&s[2] == 'r') return 12ll; 37 if (strlen(s) == 8 && s[1] == 'j' &&s[2] == 'o'&&s[3] == 't') return 13ll; 38 if (strlen(s) == 8 && s[0] == 'g' &&s[1] == 'p'&&s[3] == 't') return 14ll; 39 if (strlen(s) == 7 && s[0] == 'g' &&s[1] == 'o'&&s[2] == 'g') return 15ll; 40 return -1; 41 } 42 char s[1000]; 43 char ss[1000]; 44 string ns; 45 long long x, ans; 46 int n; 47 vector<vector<string> > v; 48 int main() { 49 /*scanf("%d", &n); 50 ans = 1ll; 51 for (int i = 0; i < n; i++) { 52 getline(cin, ns); 53 x = 0ll; 54 int Len = 0; 55 int j, cnt = 0; 56 puts(ss); 57 x = x * 16ll + Get(ss); 58 Len += strlen(ss); 59 60 ans = ans*x; 61 } 62 printf("%lld ", ans); 63 return 0;*/ 64 65 int n; cin >> n; 66 ans = 1ll; 67 while (n--) { 68 vector<string> vt; 69 while (1) { 70 string s; 71 cin >> s; 72 vt.push_back(s); 73 if (getchar() == ' ') break; 74 } 75 v.push_back(vt); 76 } 77 for (int i = 0; i < v.size();i++) { 78 x = 0ll; 79 for (int j = 0; j < v[i].size(); j++) { 80 for (int k = 0; k < v[i][j].size(); k++) { 81 ss[k] = v[i][j][k]; 82 } 83 ss[v[i][j].size()] = '