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.

1≤T≤1001≤T≤100

1≤q≤10111≤q≤1011

2≤n≤1052≤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; }
}

  

Negigent Norbert Gym - 102680E的更多相关文章

  1. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

  10. 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)

    Gym Class  Accepts: 849  Submissions: 4247  Time Limit: 6000/1000 MS (Java/Others)  Memory Limit: 65 ...

随机推荐

  1. odoo15里面密码与附件加密方式

    一.odoo里面用户设置的密码加密方式 加密是用[Passlib生成的PBKDF2 SHA512哈希] 加密后位数是128位 ` def _set_password(self): ctx = self ...

  2. Java stream实现list转化为map

    在Stream流中将List转换为Map,是使用Collectors.toMap方法来进行转换. key和value都是对象中的某个属性值. Map<String, String> use ...

  3. DOS命令快速启动和关闭MySQL服务

    为了搭建网格服务框架,在本地创建了MySQL数据库,但是,为了减少内存占用,MySQL数据库服务没有设置为自动启动,所以,需要手动的开启和关闭服务.因此,需要掌握一些短小精悍的DOS命令,下面介绍启动 ...

  4. Web性能优化:从 2 秒到秒开

    前不久发布了个人笔记软件 Nebula Note 的Web预览版(传送门),整体开发体验和使用效果都很满意.但作为Web工程师的我习惯性的打开了浏览器开发者工具的Network面板,主要想观察首次加载 ...

  5. 「Log」2023.8.22 小记

    序幕 早上不到七点到校,6bit 早就到了. 写博客写博客写博客. \(\texttt{8:21}\):把 LCT 的博客写查不多了,SAM 的还是再咕咕咕,先打代码. 学长讲题,LCT 的,讲完吃饭 ...

  6. DRF之分页类源码分析

    DRF之分页类源码分析 [一]分页类介绍 Django REST framework(DRF)是一个用于构建Web API的强大工具,它提供了分页功能,使你能够控制API响应的数据量. 在DRF中,分 ...

  7. 在 GoRoute 中使用 NavigationBar

    前言 在App 中通常会把主要的几个页面放在下方icon,让使用者能够方便操作,这个元件在flutter 中称为BottomNavigationBar. 而GoRouter则是Flutter 官方所提 ...

  8. CF1809D Binary String Sorting 题解

    CF1809D Binary String Sorting 贪心.由于每次操作的代价都很大,所以需要优先减少操作次数,然后尽量多使用交换操作. 易得交换操作最多只会发生一次,因为每次交换操作只能消除一 ...

  9. 前端开发系列087-Node篇之Buffer

    一.Buffer介绍 Buffer是Node中特有的数据类型,它是Node作为运行时对JavaScript进行的拓展,专门用来处理二进制数据流.Buffer属于固有(built-in)类型的全局变量, ...

  10. 谷云科技 iPaaS 成功入选Gartner 2025 API 管理市场指南

    近日,国际权威咨询机构Gartner发布了2025中国API管理市场指南<Market Guide for API Management,China>,谷云科技凭借其卓越的API管理平台成 ...