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

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);
    }
}
上一篇:Gym The 17th Zhejiang Provincial Collegiate Programming Contest E.Easy DP Problem(主席树)


下一篇:The 2021 Zhejiang University City College Freshman Programming Contest