题目链接:洛谷 P10702
考虑最优方式。如果 LNC 取了 y 个棋子,LJJ 取 k - y mod k,可以让 LNC 先手必赢。这也是最优的方案。换言之,如果当前的 y 一次性不能被取完,那么此时的 y 就是答案。由于 3 ≤ x,所以 y 需要从 2 开始枚举。
于是这个题变成了求最小的 k,使得 k ∤ x 且 k 最小。
时间复杂度最高 O(T log x)。
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int ans(int x){
for(int i=2;i<=x;i++){
if(x%i!=0) return i;
}
return -1;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int k;
cin>>k;
while(k--){
int x;
cin>>x;
cout<