hdu 5625 Clarke and chemistry
Problem Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a junior student and took a chemistry exam. But he did not get full score in this exam. He checked his test paper and found a naive mistake, he was wrong with a simple chemical equation balancer. He was unhappy and wanted to make a program to solve problems like this. This chemical equation balancer follow the rules: Two valences A combined by |A| elements and B combined by |B| elements. We get a new valence C by a combination reaction and the stoichiometric coefficient of C is 1. Please calculate the stoichiometric coefficient a of A and b of B that aA+bB=C, a,b∈N∗.
Input
The first line contains an integer T(1≤T≤10), the number of test cases. For each test case, the first line contains three integers A,B,C(1≤A,B,C≤26), denotes |A|,|B|,|C| respectively. Then A+B+C lines follow, each line looks like X c, denotes the number of element X of A,B,C respectively is c. (X is one of 26 capital letters, guarantee X of one valence only appear one time, 1≤c≤100)
Output
For each test case, if we can balance the equation, print a and b. If there are multiple answers, print the smallest one, a is smallest then b is smallest. Otherwise print NO.
Sample Input
2
2 3 5
A 2
B 2
C 3
D 3
E 3
A 4
B 4
C 9
D 9
E 9
2 2 2
A 4
B 4
A 3
B 3
A 9
B 9
Sample Output
2 3
NO
Hint:
The first test case, $a=2, b=3$ can make equation right.
The second test case, no any answer.
Source
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 1000000007 18 #define N 1000000 19 #define inf 1e12 20 int a,b,c; 21 int mp[3][27]; 22 int mpp[3][27]; 23 int vis[27]; 24 int main() 25 { 26 int t; 27 scanf("%d",&t); 28 while(t--){ 29 memset(mpp,0,sizeof(mpp)); 30 31 scanf("%d%d%d",&a,&b,&c); 32 char s[2]; 33 int cnt; 34 for(int i=0;i<a;i++){ 35 scanf("%s%d",s,&cnt); 36 int index = s[0]-'A'; 37 mpp[0][index]+=cnt; 38 //printf("1---%d\n",mp[0][index]); 39 } 40 for(int i=a;i<a+b;i++){ 41 scanf("%s%d",s,&cnt); 42 int index = s[0]-'A'; 43 mpp[1][index]+=cnt; 44 //printf("2---%d\n",mp[1][index]); 45 } 46 for(int i=a+b;i<a+b+c;i++){ 47 scanf("%s%d",s,&cnt); 48 int index = s[0]-'A'; 49 mpp[2][index]+=cnt; 50 //printf("3---%d\n",mp[2][index]); 51 } 52 53 int i,j; 54 int flag=0; 55 for(i=1;i<=1006;i++){ 56 for(j=1;j<=1006;j++){ 57 for(int k=0;k<26;k++){ 58 mp[0][k]=mpp[0][k]; 59 mp[1][k]=mpp[1][k]; 60 mp[2][k]=mpp[2][k]; 61 } 62 memset(vis,0,sizeof(vis)); 63 for(int k=0;k<26;k++){ 64 mp[0][k]*=i; 65 mp[1][k]*=j; 66 } 67 int w=1; 68 for(int k=0;k<26;k++){ 69 if(mp[2][k]){ 70 if(mp[0][k]+mp[1][k] == mp[2][k]){ 71 vis[k]=1; 72 }else{ 73 w=0; 74 break; 75 } 76 } 77 } 78 79 if(w==1){ 80 for(int k=0;k<26;k++){ 81 if(vis[k]==0 && (mp[0][k] || mp[1][k] || mp[2][k])){ 82 w=0; 83 break; 84 } 85 } 86 } 87 if(w==1){ 88 flag=1; 89 break; 90 } 91 } 92 if(flag==1) break; 93 } 94 if(flag==1){ 95 printf("%d %d\n",i,j); 96 }else{ 97 printf("NO\n"); 98 } 99 } 100 return 0; 101 } 102 103 //A 6 2 12 104 //B 6 3 18 105 //C 12 5 60