题意:

最多有2000组测试样例,每组样例代表n,m;

n代表要把蛋糕平分的份数,m代表必须进行多少次操作。

一共有三种操作

1.竖切   经过蛋糕圆心,将蛋糕整个向下切。

2.横切   平行于蛋糕平面进行平切。

3.复制某块小蛋糕    这种操作只能在1和2所有操作都进行完才能进行。

求:

最少进行多少次复制操作可以将蛋糕分为一样的恰好n种。注意需要用恰好m次操作。

如果没有可行解输出-1。

思路:

假设进行三种操作的数目分别是xyz。然后连立两个式子把z消掉,get

2x(y+1)-(x+y)=n-m;

因为x+y+z=m且z>=0所以有x+y<=m;

所以有2x(y+1)<=n;

进而xy<n;(x和y都是非负整数)

所以得到结论在符合要求的等式中必定有x<=sqrt(n)||y<=sqrt(n);

所以对两者枚举到sqrt(n)即可。

坑:

我们应该明白,每次进行操作至少会使得蛋糕的数量增加1.所以当m>=n的时候直接输出-1.

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{
int t;
scanf("%d",&t);
while(t--){
long long ans=inf,n,m;
scanf("%lld%lld",&n,&m);
if(n<=m){
printf("-1\n");
continue;
}
for(int i=;i<=sqrt(n)+;i++){
if((n-m-i)%(*i-)==&&n-m-i>=){
long long y=(n-m-i)/(*i-);
if(i+y<=m){
ans=min(ans,m-i-y);
}
}
}
for(int i=;i<=sqrt(n)+;i++){
if((n-m+i)%(*i+)==&&n-m+i>=){
long long x=(n-m+i)/(*i+);
if(i+x<=m){
ans=min(ans,m-i-x);
}
}
}
if(m+==n)ans=;
if(ans==inf)ans=-;
printf("%lld\n",ans);
}
}

ZOJ 3407 Doraemon's Cake Machine [数学]的更多相关文章

  1. POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang

    Problem Description As we all know, machine scheduling is a very classical problem in computer scien ...

  2. ZOJ 3230 Solving the Problems(数学 优先队列啊)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3230 Programming is fun, Aaron is ...

  3. One Person Game ZOJ - 3329(期望dp, 数学)

    There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. ...

  4. ZOJ - 3450 Doraemon's Railgun (dp)

    https://vjudge.net/problem/ZOJ-3450 题意 一座位落(X0,Y0)的城市将遭受n个敌人的摧残.现在我们手上有某科学的超电磁炮,每次攻击都是一条射线,对于共线的敌人,必 ...

  5. ZOJ - 1504 Slots of Fun 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1504 题意 给出一串字符串 里面的每个字符的位置 都按照题目的意 ...

  6. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

  7. CSU训练分类

    √√第一部分 基础算法(#10023 除外) 第 1 章 贪心算法 √√#10000 「一本通 1.1 例 1」活动安排 √√#10001 「一本通 1.1 例 2」种树 √√#10002 「一本通 ...

  8. Machine Learning Algorithms Study Notes(6)—遗忘的数学知识

    机器学习中遗忘的数学知识 最大似然估计( Maximum likelihood ) 最大似然估计,也称为最大概似估计,是一种统计方法,它用来求一个样本集的相关概率密度函数的参数.这个方法最早是遗传学家 ...

  9. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

随机推荐

  1. Google Proposes to Enhance JSON with Jsonnet

    Google has open sourced Jsonnet, a configuration language that supersedes JSON and adds new features ...

  2. Amazon后台模拟登陆

    本文基于python3.4的selenium库打开浏览器,并将浏览器中的登陆cookie保存到本地,那么下次登陆就可以直接利用cookie了: # !/usr/bin/python3.4 # -*- ...

  3. 现在流行什么 JS库/框架?

    现在大家最感兴趣的 JS 库和框架是什么? jQuery 91.5% Underscore 38.6% AngularJS 28.5% Backbone 18.6% React 15.7% Knock ...

  4. jquery的$.extend和$.fn.extend作用及区别

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); (1)类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax ...

  5. web首页设置如下代码可判断用户是用什么设备登录的?

    var OnePage=true;//用来判断staticHtml.js中首页登入的信息判断var _mobileUrl = "http://a.abc.com";//手机用户通过 ...

  6. (zt)Lua的多任务机制——协程(coroutine)

    原帖:http://blog.csdn.net/soloist/article/details/329381 并发是现实世界的本质特征,而聪明的计算机科学家用来模拟并发的技术手段便是多任务机制.大致上 ...

  7. Asp.Net MVC 路由 - Asp.Net 编程 - 张子阳

    http://cache.baiducontent.com/c?m=9d78d513d98316fa03acd2294d01d6165909c7256b96c4523f8a9c12d522195646 ...

  8. 【linux】bash常用快捷键

    Ctrl + r:逆向搜索命令历史 Ctrl + l:清屏 Ctrl + c:终止命令 Ctrl + u:删除光标前的指令 Ctrl + k:删除光标后的指令 Ctrl + d:退出登陆

  9. 去掉IntelliJ IDEA的拼写检查

    Settings→Editor→Inspections→Spelling 去掉Spelling下的Typo复选框即可 来自为知笔记(Wiz)

  10. rsync 同步文件

    rsync 同步文件 rsync -avz roo@192.168.4.12::/home/a ./a  --exclude "data" exclude 去掉/a/data 文件 ...