技术标签: cf
F. Fair Distribution
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
There are n robots and m energy bars in the Dream Kingdom. DreamGrid, the king, is trying to make a fair distribution of the energy bars. A fair distribution exists if and only if the number of the energy bars is a multiple of the number of robots.
The only tool DreamGrid has is a powerful laser gun. Every time he turns on the laser gun, he can do exactly one of the two things:
Create a new energy bar.
Destroy a robot.
To avoid the extinction of robots, it’s forbidden to destroy all the n robots. It takes one dollar to turn on the laser gun once. You are asked to find the minimum cost of making a fair distribution.
Input
There are multiple test cases. The first line of the input contains an integer T (1≤T≤1000), indicating the number of test cases. For each test case:
The only line contains two integers n and m (1≤n,m≤108), indicating the initial number of robots and energy bars.
Output
For each test case output one line containing an integer, indicating the minimum cost to get a fair distribution.
Example
inputCopy
3
3 12
10 6
8 20
outputCopy
0
4
2
Note
For the third sample, the best way is to destroy a robot and create an energy bar. After that, we have 7 robots and 21 energy bars, which leads to a fair distribution.
总结反思:
原来:
m%n是最大,之后n–看的方法
——超时
如何想到:
这分析了超时的特点:
我们可以发现:
每一个倍数浪费了多次,而只有第一次出现这个倍数时才有用
所以我们一个倍数只看头一次
——总结特点:要对一个一个算的地方感冒,可以多想一点,跨步否
人多想一点,计算机就算少一点啊
//时间复杂度:不是o(n),1e8??——
//但是却n会非线性减少
//以[a]表示不比a小的整数
//n->cur是[m/n] ->n是[m/cur] ->cur是[m/(n-1)],更大->n是[m/cur]更小,m越大减小越明显
//正确性:n,cur取遍了所有可能整数
//mn*cur恰>=m始终成立,
//而由于mn每次-1,即不符合mn*原cur>=m了,
//故cur每次必增加,即mn每次必减小
//时间复杂度是o(n^(1/2))
//意义上是:m是增加的,n是减少的,所以k*n>=m能让m赶来
//比一般的想法:m%n是最大,之后n--看的方法
//多想到了很多步浪费的,可以直接增加倍数
//#pragma GCC optimize(3)
#include <cstdio>
#include <queue>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <fstream>
#include <cassert>
#include <complex>
#include <ctime>
//#include <tr1/unordered_map>
#include <bitset>
#define pii pair<int,int>
#define pdd pair<double,double>
#define pb push_back
#define fi first
#define se second
#define is insert
#define es erase
#define mkp make_pair
#define per(i,a,b) for(int i = a; i >= b; --i)
#define rep(i,a,b) for(int i = a; i <= b; ++i)
using namespace std;
//using namespace tr1;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
int x = 0;
int f = 0;
char ch = 0;
while(!isdigit(ch))
{
f |= (ch == '-') ? 1 : 0;
ch = getchar();
}
while(isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return f ? -x : x;
}
int t,n,m;
int main()
{
t = read();
while(t--)
{
n = read();
m = read();
int cur = (m - 1) / n + 1;//恰符合cur*n>=m
int ans = 2e9;
while(1)
{
int mn = (m - 1) / cur + 1;//符合恰mn*cur>=m , n的实际值
ans = min((ll)ans,n - mn + (ll)mn * cur - m);//n-mn是n的减少, mn * cur - m是m的增加
if(mn == 1)//到1 无法再减 ,n从n到1
break;
cur = (m - 1) / (mn - 1) + 1;//符合恰cur*(mn-1)>=m ,
}
//时间复杂度:不是o(n),1e8??——
//但是却n会非线性减少
//以[a]表示不比a小的整数
//n->cur是[m/n] ->n是[m/cur] ->cur是[m/(n-1)],更大->n是[m/cur]更小,m越大减小越明显
//正确性:n,cur取遍了所有可能整数
//mn*cur恰>=m始终成立,
//而由于mn每次-1,即不符合mn*原cur>=m了,
//故cur每次必增加,即mn每次必减小
//时间复杂度是o(n^(1/2))
//意义上是:m是增加的,n是减少的,所以k*n>=m能让m赶来
//比一般的想法:m%n是最大,之后n--看的方法
//多想到了很多步浪费的,可以直接增加倍数
printf("%d\n",ans);
}
}
文章浏览阅读718次。第七章 数据库设计数据库设计分6个阶段1.需求分析:通过前期调查和分析是否做得充分与准确,决定了构建数据库的速度和质量。2.概念结构设计:通过对用户需求进行综合、归纳与抽象,用E-R图形成一个独立于具体数据库管理系统的概念模型。3.逻辑结构设计:将概念结构转换为某个数据库管理系统所支持的数据模型,并对其进行优化。逻辑模式、外模式4.物理结构设计:为逻辑数据结构选取一个最适合应用环境的物理结构。包括存储结构和存取方法。内模式5.数据库实施:根据逻辑设计和物理设计的结果构建数据库,编写与调试应用程_数据库系统概论第五版与第七版有何不同
文章浏览阅读2.4w次,点赞10次,收藏46次。<template> <div> <button @click="getEvent($event)">点击</button> </div></template><script> export default { data(){ return{ ..._vue event.target 获取同级下一个元素
文章浏览阅读598次。1、问题:拿到一大段项目代码怎么看?!2、解决方案:使用工具生成函数调用链图,帮助分析整个项目方法的用法以及功能3、工具简介:(安装此两种工具,然后可以方便的分析代码,生成相应的函数调用图)doxygen:(http://www.stack.nl/~dimitri/doxygen/index.html)graphviz:(http://www.graphviz.org/)3.0、打开DoxyWizard时,如果弹出“打不开 XXX,因为它来自身份不明的开发者”请进入如下网址寻找解决办法http:_qt 怎么没有 doxygen插件
文章浏览阅读104次。Javascript练习题正则表达式验证邮箱摘要: View Code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3..._js 写法技巧
文章浏览阅读1.1k次。Ubuntu Linux是由南非人马克·沙特尔沃思(Mark Shuttleworth)创办的基于Debian Linux的操作系统,Ubuntu几乎包含了所有常用的应用软件:文字处理、电子邮件、软件开发工具和Web服务等。用户下载、使用、分享Ubuntu系统,以及获得技术支持与服务,无需支付任何许可费用。那如何实现vnc远程连接ubuntu呢?如图:(官网:IIS7服务器管理工具)IIS7服务器管理工具能实现一键导出或导入,一键批量打开VNC,一键批量关闭VNC,服务器到期提醒,支持多台V_vncuxq
文章浏览阅读2w次。在进行嵌入式开发时常常需要在板子上用nfs挂载宿电脑,公司现在的架构是远程服务器加windows个人电脑,因此需要在个人电脑的windows系统上搭建NFS服务器。一、windows上搭建NFS服务器NFS服务器软件:haneWIN 1.1.69 汉化已注册的版本互联网上有下载,我这里再放一个我上传到CSDN的链接。https://download.csdn.net/download/z..._hanewin 1.1.69
文章浏览阅读2.6k次。本文转载自博主“Richar-张”的文章,出处为:https://blog.csdn.net/zj573453769/article/details/53446426仅用于本人记录TX2的学习历程,若有侵权,请告知我,会立即删除1.Fcits安装:[python] view plain copysudo apt install fcitx fcitx-googlepinyin fcitx-tabl..._tx2 genghuanyuyan
文章浏览阅读1w次,点赞3次,收藏11次。Visual Studio Code Kubernetes Tools1. vscode配置连通k8s1.1拿到k8s集群的配置文件信息1.2 vscode安装插件[Visual Studio Code Kubernetes Tools](https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-k..._vscode visual studio k5
文章浏览阅读6.7k次,点赞2次,收藏8次。打开网址下载pip:https://pypi.org/project/pip/#files解压到你的anaconda包路径中,我的是下图:现在开始安装进入cmd之后,进入d盘:d:进入d盘之后cdD:\anaconda\Lib\site-packages\pip-19.0.3进入目录之后python setup.py install然后..._pipanzhuangming;
文章浏览阅读470次。网络地址转换(NAT)互联网如火如荼的应用,加剧了IP地址匮乏的问题,为了缓解这一问题,一个重要的应用:NAT(Network Address Translation―网络地址转换),日益广泛地应用起来。NAT通过地址转换的方式,使企业可以仅使用较少的互联网有效IP地址,就能获得互联网接入的能力,有效地缓解了地址不足的问题,同时提供了一定的安全性。 NAT的实..._rsa-encr和rsa-sig的区别
文章浏览阅读842次,点赞8次,收藏21次。Java架构进阶面试及知识点文档笔记这份文档共498页,其中包括Java集合,并发编程,JVM,Dubbo,Redis,Spring全家桶,MySQL,Kafka等面试解析及知识点整理Java分布式高级面试问题解析文档其中都是包括分布式的面试问题解析,内容有分布式消息队列,Redis缓存,分库分表,微服务架构,分布式高可用,读写分离等等!互联网Java程序员面试必备问题解析及文档学习笔记Java架构进阶视频解析合集《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》
文章浏览阅读1w次,点赞7次,收藏40次。问题描述:一辆汽车加满油后可行驶n公里。旅途中有若干个加油站。设计一个有效算法,指出应在哪些加油站停靠加油,使沿途加油次数最少。对于给定的n(n 在哪些加油站停靠加油,使沿途加油次数最少。对于给定的n(n 要求:输入:第一行有2个正整数n和k,表示汽车加满油后可行驶n公里,且旅途中有k个加油站。接下来的1 行中,有k+1 个整数,表示第k个加油站与第k-1 个加油站之间的距_实验3.3 汽车加油问题 1、问题描述:一辆汽车加满油后可行驶nkm。旅途中有若干加油