USACO3.2 Feed Ratios(ratios)

        98年ACM Finals的题目,看上去比较简单,就是一个数学题,自己推导出公式,需要细心,然后注意约分,毫无压力0秒撸过。不过有一个陷阱,目标饲料的比例可能不是最简化的比,简直是坑爹啊。

/*
ID:jzzlee1
PROB:ratios
LANG:C++
*/
//#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
ifstream cin("ratios.in");
ofstream cout("ratios.out");
void check(int &x,int &y,int &z)
{
	int i=2;
	while(i<=100)
	{
		if(x%i==0&&y%i==0&&z%i==0)
		{
			x/=i;
			y/=i;
			z/=i;
			i=2;
		}
		else
			i++;
	}
}
int main()
{
	int a1,a2,a3,a4,a5,a6,a7,a8,a9,b1,b2,b3,s1,s2,s3,s4,s5,s6,t1,t2,t3,t4,t5,t6,w1,w2,w3,w4,w5,w6,x,y,z;
	double yz,xz;
	cin>>b1>>b2>>b3;
	cin>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9;
	t1=a1*b2;
	t2=a4*b2;
	t3=a7*b2;
	t4=a2*b1;
	t5=a5*b1;
	t6=a8*b1;
	s1=a1*b3;
	s2=a4*b3;
	s3=a7*b3;
	s4=a3*b1;
	s5=a6*b1;
	s6=a9*b1;
	w1=t6-t3;
	w2=t5-t2;
	w3=t1-t4;
	w4=s6-s3;
	w5=s5-s2;
	w6=s1-s4;
	yz=(double)(w3*w4-w1*w6)/(w6*w2-w3*w5);
	xz=(double)(w2*w4-w1*w5)/(w6*w2-w3*w5);
	if(yz<0||xz<0)
		cout<<"NONE"<<endl;
	else
	{
		x=abs(w2*w4-w1*w5);
		y=abs(w3*w4-w1*w6);
		z=abs(w6*w2-w3*w5);
		check(x,y,z);
		if(a1*x+a4*y+a7*z<b1)
		{
			int t=b1/(a1*x+a4*y+a7*z);
			x*=t;
			y*=t;
			z*=t;
		}
		if(x>=100||y>=100||z>=100)
			cout<<"NONE"<<endl;
		else
			cout<<x<<" "<<y<<" "<<z<<" "<<(a1*x+a4*y+a7*z)/b1<<endl;
	}
	return 0;
}

你可能感兴趣的:(USACO)