BestCoder Round #72 (div.2)--1001

Clarke and chemistry

 
 Accepts: 225
 
 Submissions: 601
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
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 AA combined by |A|A elements and BB combined by |B|B elements.
We get a new valence CC by a combination reaction and the stoichiometric coefficient of CC is 11. Please calculate the stoichiometric coefficient aa of AA and bb of BB that aA + bB = C,\ \ a, b \in \text{N}^*aA+bB=C,  a,bN.

Input

The first line contains an integer T(1 \le T \le 10)T(1T10), the number of test cases.
For each test case, the first line contains three integers A, B, C(1 \le A, B, C \le 26)A,B,C(1A,B,C26), denotes |A|, |B|, |C|A,B,C respectively.
Then A+B+CA+B+C lines follow, each line looks like X\ cX c, denotes the number of element XX of A, B, CA,B,C respectively is cc. (XX is one of 2626 capital letters, guarantee XX of one valence only appear one time, 1 \le c \le 1001c100)

Output

For each test case, if we can balance the equation, print aa and bb. If there are multiple answers, print the smallest one, aa is smallest then bb 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=3a=2,b=3 can make equation right.  

The second test case, no any answer.

代码如下:

#include<stdio.h>
#include<string.h>
int num1[30];
int num2[30];
int sum[30];
bool check(int a,int b){
	int i;
	for(i=0;i<26;i++){
		if(a*num1[i]+b*num2[i]!=sum[i])
		return false;
	}
	return true;
}
int main(){
	int t,flag;
	int a,b,c,n,i,j;
	char x[5];
	scanf("%d",&t);
	while(t--){
		flag=0;
		memset(num1,0,sizeof(num1));
		memset(num2,0,sizeof(num2));
		memset(sum,0,sizeof(sum));
		scanf("%d%d%d",&a,&b,&c);
	    for(i=1;i<=a;i++){
	    	scanf("%s%d",x,&n);
	    	num1[x[0]-'A']=n;
	    }
	    for(i=1;i<=b;i++){
	    	scanf("%s%d",x,&n);
	    	num2[x[0]-'A']=n;
	    }
	    for(i=1;i<=c;i++){
	    	scanf("%s%d",x,&n);
	    	sum[x[0]-'A']=n;
	    }
	    for(i=1;i<=100;i++){
	    	for(j=1;j<=100;j++){
	    		if(check(i,j)){
	    			flag=1;
	    			a=i;
	    			b=j;
	    			break;
	    		}	    		
	    	}
	    	if(flag)break;
	    }
	    if(flag)printf("%d %d\n",a,b);
	    else printf("NO\n");
	}
	return 0;
}


你可能感兴趣的:(BestCoder Round #72 (div.2)--1001)