Negigent Norbert Gym

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;

	}
}

  

原文地址:https://www.cnblogs.com/SutsuharaYuki/p/13813554.html