350 字
2 分钟
Blackslex-and-Password

题面#

Blackslex is designing a log-in system for Gean Dev and discovered that most users use weak passwords.

To resolve this issue, he posed the following conditions, dependent on two variables kk and xx, for all passwords. Each password is a string ss of length nn satisfying these properties.

  • ss uses only the first kk lowercase letters of the English alphabet.
  • For every pair of indices 1i<jn1 \le i \lt j \le n such that (ji)(j-i) is divisible by xx, the letters sis_i and sjs_j are different.

Find the smallest integer nn such that no valid string of length nn exists.

Input

The first line contains a single integer tt (1t5001 \le t \le 500) — the number of test cases.

The first and only line of each test case contains two integers kk and xx (1k261 \le k \le 26, 1x151 \le x \le 15).

Output

For each test case, output the minimal nn.

思路#

将下标按 imodxi\bmod x 分组,若 (ji)(j-i) 可被 xx 整除则 ij(modx)i\equiv j\pmod x,因此约束仅发生在同一组内

同一组内任意两位置字符必须不同,因此每组最多容纳 kk 个位置,否则鸽巢原理必有重复字母

长度为 nn 时共有 xx 组,各组大小为 n/x\lfloor n/x\rfloorn/x\lceil n/x\rceil,最大组大小为 n/x\lceil n/x\rceil

可行当且仅当 n/xk\lceil n/x\rceil\le k,等价于 nkxn\le kx

n=kxn=kx 时可构造,每个余数组依次填入 kk 个不同字母,组与组之间允许重复
因此最小不可行长度为 kx+1kx+1,直接输出 n=kx+1n=kx+1 即可

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int k, x;
cin >> k >> x;
int ans = k * x + 1;
cout << ans << endl;
}
}
Blackslex-and-Password
https://github.com/posts/blackslex-and-password/
作者
FZSGBall
发布于
2026-05-17
许可协议
CC BY-NC-SA 4.0