Negigent Norbert Gym - 102680E

https://vjudge.net/problem/Gym-102680E/origin

https://vjudge.net/contest/396206#problem/E

Naughty Negligent Norbert has been given a notoriously repetitive responsibility: answering the clarification requests for a programming competition! Norbert realizes that since all the questions are perfectly unambiguous, he doesn't really have to do his job, and the only reason he is responding to requests at all is so he can get service hours. The savvy programmers, however, have implemented a system to prevent Norbert from simply giving the same answer to all clarification requests.

Having seen the source code for the competition, Norbert knows that he will lose all his service hours for not doing his job correctly if any two of his responses are identical. Negligent Norbert decides that for each of his TT competitions, he will respond to the qq clarification requests using as few characters as possible. Also, because Norbert is very diverse and speaks many languages, he will answer all questions in a given competition using a language with nn characters. What is the fewest number of keystrokes Norbert can type in each competition to still get his service hours?

Each response must have at least 1 character, and responses do not necessarily all have to be the same length. Norbert will click the submit button with his mouse instead of pressing the return key, so the total number of keystrokes is the sum of the lengths of each response.

Input

The first line will contain an integer TT. TT lines follow, each containing two space-separated integers, qq and nn, the number of clarification requests and the number of characters in the language for the current competition.

1T1001≤T≤100

1q10111≤q≤1011

2n1052≤n≤105

Output

Output TT lines, each containing a single integer representing the number of characters in all of Norbert's responses for a competition.

Example

Input
3
7 3
5 26
14 3

Output
11
5
27

Note

For the first test case, Norbert is using a language with 3 characters, Norbert could answer, for example, AC, CA, A, BC, C, AA, and B, for a total character count of 2+2+1+2+1+2+1 = 11.

For the second test case, Norbert could answer each of the five clarification requests with a different character, using a total of 5 characters. So we output 5 on a new line.

For the third test case, Norbert will need to use all of the one-letter and two-letter words, and two more three-letter words, for a total of 27 characters.

Sponsor

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
//const int maxn=1e5+10;
const int maxn = 5010;
//const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

int main()
{
	int T = 0;
	cin >> T;
	while(T--)
	{
		ll request;
		ll ch_L;
		cin >> request >> ch_L;
		ll l = 1;
		ll tot = 0;
		while(request > 0)
		{
			ll p = pow(ch_L, l);
		 	ll min = p < request ? p : request;
			tot += l * min;
			request -= min;
			l += 1;
		}
		cout << tot <<endl;

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值