您的位置:首页 > 其它

zoj 3407 Doraemon's Cake Machine (思维不能太狭隘了 ,从各个方面考虑问题)

2014-05-20 10:38 211 查看
题目链接:http://vjudge.net/problem/viewProblem.action?id=12980

思路:

  我们设横切y次,竖切x次,copy  z 次;

  可以得到方程  x+y+z=m--------(1;                        (y+1)*2*x+z=n-----(2;(注意x=0的情况)

    (2  -  (1    2*y*x+x-y+m=n;      明显有  y*x+x-y<=n;    由对称性 我们只需要枚举 1~sqrt(n)  即可得到最小值;

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
int n,m,t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
if(n<=m){
puts("-1");
continue;
}
if(n-m==1){
puts("0");
continue;
}
double temp=sqrt(n*1);
int ans=0x7fffffff;
for(int x=1;x<=temp;x++){
int y=(n-m-x)/(2*x-1);
int z=m-y-x;
if(y<0||z<0||2*x*y+2*x+z!=n||ans<z)
continue;
ans=z;
}
for(int y=1;y<=temp;y++){
int x=(n-m-y)/(2*y+1);
int z=m-x-y;
if(x<0||z<0||2*x*y+2*x+z!=n||ans<z)
continue;
ans=z;
}
if(ans!=0x7fffffff)
printf("%d\n",ans);
else
puts("-1");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐