TT的神秘任务2(思路)

问题描述

在你们的帮助下,TT 轻松地完成了上一个神秘任务。

但是令人没有想到的是,几天后,TT 再次遇到了那个神秘人。

而这一次,神秘人决定加大难度,并许诺 TT,如果能够完成便给他一个奖励。

任务依旧只给了两个数字,分别表示 n 和 k,不过这一次是要求 TT 给出无法被 n 整除的第 k 大的正整数。

例如 n = 3,k = 7,则前 7 个无法被 n 整除的正整数为 [1 2 4 5 7 8 10],答案为 10。

好奇的 TT 想要知道奖励究竟是什么,你能帮帮他吗?

Input

第一行一个整数 T,表示数据组数,不超过 1000。

之后 T 行,每一行给出两个正整数,分别表示 n(2 ≤ n ≤ 1e9)、k(1 ≤ k ≤ 1e9)。

Output

对于每一组数据,输出无法被 n 整除的第 k 大的正整数。

Sample input

6
3 7
4 12
2 1000000000
7 97
1000000000 1000000000
2 1

Sample output

10
15
1999999999
113
1000000001
1

解题思路

这个题是一个数学题,思路很简单,前k个能够被n整除的数是k/n个,但是k加上k/n后,会出现另外被n整除的,所以用k/(n-1)来计算总共需要去除的(为什么用这个可以自己推一下)。然后还要考虑是否整除的情况,如果是整除,结果再减一。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int n,k,t;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>k;
        int temp=k/(n-1);
        if(k%(n-1)==0) temp-=1;
        cout<<k+temp<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/106215753
TT