G - Forge the Best Sword Together! 2025 Nowcoder Winter Algorithm Basic Training Camp 2 Solutions

Last updated on January 24, 2025 am

This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.

Link: https://ac.nowcoder.com/acm/contest/95334/G Source: Nowcoder

Problem Description

Niu Kele learned from ancient books that the closer the sword-forging temperature is to n degrees, the better the quality of the sword. Now, he is studying his family's sword-forging furnace, hoping to forge the best sword in the whole village. When the furnace was started, Niu Kele had already added firewood once, so the initial temperature of the furnace is m degrees. After that, each time Niu Kele adds firewood, the temperature of the furnace increases to m times its original value, i.e., the temperature becomes m^2, m^3, ......

Niu Kele wants to know the minimum number of times he needs to add firewood (including the time added when starting the furnace) so that the temperature of the furnace is as close to n degrees as possible, allowing him to forge a sword of the best quality.

Input Description:

Each test file contains multiple test cases. The first line contains an integer T(1≦T≦105) representing the number of test cases. Each test case is described as follows:

A single line contains two integers n,m(1≦n,m≦10^9), representing the optimal sword-forging temperature and the factor by which the temperature increases each time Niu Kele adds firewood.

Output Description:

For each test case, output on a new line. Output an integer representing the minimum number of times Niu Kele needs to add firewood.

Input

3
7 6
6 2
9 9

Output

1
2
1

Explanation

For the first test case, when Niu Kele starts the furnace, the temperature of the furnace is 666 degrees. If he adds firewood once more, the temperature will reach 36 degrees, which is clearly worse. So Niu Kele only needs to add firewood once at the start. For the second test case, when Niu Kele starts the furnace, the temperature of the furnace is 222 degrees. After that, each time he adds firewood, the temperature of the furnace becomes: 4,8,16,32,⋯

Solutions

Note that the constraints of the problem are 1≦n,m≦10^9, and $2^32> 10^9$, so we only need to iterate 32 times

Additionally, how do we measure whether ans is appropriate? We can define a difference variable, continuously taking the minimum and updating ans

The code implementation is as follows

代码CPP · 21 行
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18;
int T,n,m;
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m;
        ll minEps=inf,ans,nowTemp=1;
        for(int i=1;i<=32;i++){
            if(nowTemp>=inf/m) break; //如果当前温度*m 要比 inf 大了就 break 防止溢出
            nowTemp*=m;
            if(abs(n-nowTemp)<minEps){
                minEps=abs(n-nowTemp); //更新最小差值
                ans=i;
            }
        }
        cout<<ans<<endl;
    }
}

G - Forge the Best Sword Together! 2025 Nowcoder Winter Algorithm Basic Training Camp 2 Solutions
https://blog.wendain.ing/en/posts/nowcoder-2025-winter-camp2-g-build-best-sword-solution/
Author
wendaining
Posted on
January 24, 2025
Updated on
January 24, 2025
Licensed under