Problem Description
You are given an integer n.
You are required to calculate (n mod 1) or (n mod 2) or … or (n mod (n - 1)) or (n mod n).
The “or” operation means “bitwise OR”.
Input
The first line contains an integer T(1≤T≤5000)representing the
number of test cases.For each test case, there is an integer n(1≤n≤1012)in one line.
Output
For each test case, print the answer in one line.
Sample Input
5
1
2
3
4
5
Sample Output
0
0
1
1
3
当n为偶数时,设m=n/2-1
当n为奇数时,设m=(n-1)/2
可以发现,n mod i<=m,且当i<=m时,有 n mod (n-i)=i。于是可以得出n mod i取到0~m的所有整数,因此答案会是2^k-1,k的具体值判断一下即可。
换句话说,就是其存在log(n)-1的规律。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL T,n;
int main()
{
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);
if((n&(-n))==n){
printf("%lld\n",max(0ll,n/2-1));
continue;
}
while((n&(-n))!=n)
n-=(n&(-n));
printf("%lld\n",n-1);
}
}