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;
}
思路:
我们设横切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;
}
相关文章推荐
- zoj 3407 Doraemon's Cake Machine
- jw player 流媒体拖曳不成功的问题——nginx在支持flv方面不能用代理
- jw player 流媒体拖曳不成功的问题――nginx在支持flv方面不能用代理
- 错误 1402 不能打开HKEY_LOCAL_MACHINE\Software\Classes\Msxml.2.DOMDocument.4.0\CLSID 的问题(server2003 , virtual server 2005 r1)
- 面对员工挑衅,管理者需要从哪几个方面考虑问题来化解危机?
- Spring-statemachine Action不能并发执行的问题
- 双赢的思维考虑问题
- ZOJ 3929 Deque and Balls (放球问题_DP思维好题)
- wemall doraemon中Android app商城系统解决左侧抽屉菜单和viewpager不能兼容问题
- 电路板焊接完成发现CPU没有工作,需要从哪些方面考虑解决这个问题?
- 写程序时细心,全方位+逆向思维考虑问题
- 面向对象的精髓在于考虑问题的思路是从现实世界的人类思维习惯出发
- 从五个方面考虑Oracle的性能调优问题
- Spring-statemachine fork一个region后不能进入join状态的问题
- 数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……
- 剑指offer 数值的整数次幂,求base的exponent次方,不能使用库函数,同时不需要考虑大数问题
- boj 1328 简单问题 不过要考虑输入大小 long long可以 不过VC++不能编译
- 用大数据的思维考虑问题
- 将整数n分成k份,且每份不能为空,任意两种分法不能相同(不考虑顺序)【划分问题】
- 3.如何用面向对象的思维考虑问题