题目收集
题目列表题单
返回列表

Mei Ti Le

CodeforcesP0073· 2000ms· 1024 MB· ZJU Summer 2026 Contest 7原题链接
数学贪心

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You are given two positive integers nnn and kkk. Initially, the set is S={1,2,…,n}.S=\{1,2,\ldots,n\}.S={1,2,…,n}.

You may remove some numbers from SSS. After doing so, you must ensure that for every pair of distinct remaining numbers xxx and yyy, their sum is not equal to any non-negative integer power of kkk. In other words, there must be no integer t≥0t\ge 0t≥0 such that x+y=kt.x+y=k^t.x+y=kt.

Find the minimum number of elements that must be removed.

You need to answer TTT independent test cases.

输入格式

The first line contains one integer T(1≤T≤106)T (1\le T\le 10^6)T(1≤T≤106), the number of test cases.

Each of the next TTT lines contains two integers nnn and k(1≤n,k≤1018)k (1\le n,k\le 10^{18})k(1≤n,k≤1018), describing one test case.

输出格式

For each test case, output one integer on a separate line: the minimum number of elements that must be removed.

样例输入 #1

3
4 1
4 2
4 3

样例输出 #1

0
1
1