题目链接: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;
}