Clarke and chemistry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 541 Accepted Submission(s): 294
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∗ .
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 )
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
Recommend
那么问题来了,你经历过绝望么?
让配平化学方程式的,直接拿第一组测试数据来说,a里边有两个元素,一个是A有2个,一个是B有2个,b里边有3 个元素,一个是C有3个,一个是D有三个,一个是E有3个,
c里边有5 个元素,同上,如果让A里边的元素*2+b里边的元素*3就可以得到c里边的元素了,求的就是一个系数,由于数据不大,直接暴力模拟答案,但是,由于储存不是很容易储存,我们这里用了map,去存:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
int i,j,k,l,m,n,p,A,B,C,ans1,ans2,num;
int main()
{
scanf("%d",&p);
while(p--)
{
scanf("%d%d%d",&A,&B,&C);
getchar();
// getchar();
map<char ,int>a;
map<char ,int>b;
map<char ,int>c;
char s[110];
a.clear();
b.clear();
c.clear();
for(i=0;i<A;i++)
{
scanf("%s%d",s,&num);
getchar();
if(a[s[0]]==0)
a[s[0]]=num;
else
a[s[0]]+=num;
}
for(i=0;i<B;i++)
{
scanf("%s%d",s,&num);
getchar();
if(b[s[0]]==0)
b[s[0]]=num;
else
b[s[0]]+=num;
}
for(i=0;i<C;i++)
{
scanf("%s%d",s,&num);
getchar();
if(c[s[0]]==0)
c[s[0]]=num;
else
c[s[0]]+=num;
}
int help;
int cnt;
int cmt;
int flag=0;
int daan1,daan2;
for(ans1=1;ans1<100;ans1++)
{
if(flag)
break;
for(ans2=1;ans2+ans1<100;ans2++)
{
for(i=0;i<26;i++)
{
a['A'+i]*=ans1;
b['A'+i]*=ans2;
if(a['A'+i]+b['A'+i]!=c['A'+i])
{
a['A'+i]/=ans1;
b['A'+i]/=ans2;
break;
}
else
{
a['A'+i]/=ans1;
b['A'+i]/=ans2;
}
}
if(i==26)
{
daan1=ans1;
daan2=ans2;
flag=1;
}
}
}
if(flag)
printf("%d %d\n",daan1,daan2);
else
printf("NO\n");
}
}