KONGJUNE / / ACM / 阅读量

### Problem Description

For a positive integer $n$, let's denote function $f(n,m)$ as the $m$-th smallest integer $x$ that $x>n$ and $\mathrm{gcd}(x,n)=1$. For example, $f(5,1)=6$ and $f(5,5)=11$.

You are given the value of $m$ and $(f(n,m)−n)\oplus n$, where $\oplus$ denotes the bitwise XOR operation. Please write a program to find the smallest positive integer $n$ that $(f(n,m)−n)\oplus n=k$, or determine (whether) it is impossible.

### Input

The first line of the input contains an integer $T$($1 \leq T \leq 10$), denoting the number of test cases.

In each test case, there are two integers $k,m$($1 \leq k\leq 10^{18}$,$1\leq m\leq100$).

### Output

For each test case, print a single line containing an integer, denoting the smallest $n$. If there is no solution, output $-1$ instead.

### Sample Input

2
3 5
6 100


### Sample Output

5
-1


## 题意 & 题解

$$N = P_1^{a_{1}}P_2^{a_{2}}P_3^{a_{3}} \cdots P_n^{a_{n}}=\prod_{i=1}^nP_i^{a_i}$$

$$K = (a_1+1)(a_2+1)(a_3+1)\cdots(a_k+1)=\prod_{i=1}^n(a_i+1)$$

## AC代码

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std ;
typedef long long ll ;

ll gcd (ll x , ll y){
if (y==0) return x ;
else return gcd(y,x%y) ;
}
const ll maxn = 1e18+5 ;
int main(){
int t ;
scanf("%d",&t) ;
while(t--){
ll k ;
int m ;
ll ans = maxn ;
scanf("%lld %d",&k , &m) ;
for (ll i = 1; i <= 631 ; i++) { // 631是第115个质数
ll n = i^k ;
if (gcd(i,n)!=1) continue ;
int temp = 0 ;
for (ll j = 1 ; j< i ; j++) {
if (gcd(j,n)==1) temp++ ;
}
if (temp==m-1&&ans>n){
ans = n ;
}
}
if (ans == maxn) cout << -1 << endl ;
else cout << ans << endl ;
}
return 0 ;
}