We say that a positive integer nn is kk-good for some positive integer kk if nn can be expressed as a sum of kk positive integers which give kk distinct remainders when divided by kk.
Given a positive integer nn, find some k≥2k≥2 so that nn is kk-good or tell that such a kk does not exist.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases.
Each test case consists of one line with an integer nn (2≤n≤10182≤n≤1018).
Output
For each test case, print a line with a value of kk such that nn is kk-good (k≥2k≥2), or −1−1 if nn is not kk-good for any kk. If there are multiple valid values of kk, you can print any of them.
Example
input
Copy
5 2 4 6 15 20
output
Copy
-1 -1 3 3 5
Note
66 is a 33-good number since it can be expressed as a sum of 33 numbers which give different remainders when divided by 33: 6=1+2+36=1+2+3.
1515 is also a 33-good number since 15=1+5+915=1+5+9 and 1,5,91,5,9 give different remainders when divided by 33.
2020 is a 55-good number since 20=2+3+4+5+620=2+3+4+5+6 and 2,3,4,5,62,3,4,5,6 give different remainders when divided by 55.
思路:
1,差值一定,某一段求和,可用等差数列求和
2,n=k*(k+1)/2+m*k(m>=0)==>2*n==k*(2*m+1+k);
3,因为右侧一奇一偶,提取2^i,根据奇数情况判断
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxj =1e6+100,mod = 1e6+100;
int v[50],w[50],dp[55][3100];
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int te=1;
while(n%2==0){
n/=2;
te*=2;
}
if(n==1)cout<<-1<<'\n';//全是奇数
else cout<<min(te*2,n)<<'\n';//奇偶中最小作为k
}
return 0;
}