ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

F. Fair Distribution——The 18th Zhejiang Provincial Collegiate Programming Contest

2021-04-30 20:30:05  阅读:477  来源: 互联网

标签:Provincial cur Fair Contest int energy mn include define


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);
    }
}

标签:Provincial,cur,Fair,Contest,int,energy,mn,include,define
来源: https://blog.csdn.net/qq_51945248/article/details/116308962

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有