zoj 3407 Doraemon's Cake Machine

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3407

 

x+y+z=m

2x(y+1)+z=n

 

2xy+2x+m-x-y=n

so 2xy+x-y+m>=xy-x-y+1

 

所以枚举x,y到sqrt(n)就好了

 

#include<iostream> #include<stdio.h> #include<math.h> using namespace std; #define INF 100000000 int cases,m,n; int main() { scanf("%d",&cases); while(cases--) { int i,t,x,y,z,ans; bool flag; ans=INF; flag=0; scanf("%d%d",&n,&m); t=(int)sqrt((double)n); if(n<=m) { printf("-1/n"); continue; } if(n-m==1) { printf("0/n"); continue; } //try x y=1; for(x=1;x<=t&&y>=0;x++) { y=(n-m-x)/(2*x-1); z=m-x-y; if(z>=0&&2*x*(y+1)+z==n&&ans>z) ans=z,flag=1; } //try y x=1; for(y=1;y<=t&&x>=0;y++) { x=(n-m+y)/(2*y+1); z=m-x-y; if(z>=0&&2*x*(y+1)+z==n&&ans>z) ans=z,flag=1; } if(!flag) printf("-1/n"); else printf("%d/n",ans); } return 0; }

你可能感兴趣的:(zoj 3407 Doraemon's Cake Machine)