题解:P13635 [NWRRC 2021] Halfway There
给定一个整数
这道题的核心在于利用互质数的对称性。如果
对于
因此,我们不需要真的去计算欧拉函数,只需要从
#include<bits/stdc++.h>
using ll = long long;
inline ll read(){
char ch;ll s = 0;ll w = 1;
while((ch = getchar()) >'9' || ch < '0' )if(ch == '-') w = -1;
while(ch >= '0' && ch <= '9')s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar();
return s * w;
}
void print(ll x){
if(x < 0) putchar('-'),x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
}
inline void solve(){
ll n = read();
if(n == 2){
puts("1");
return ;
}
ll s = (n - 1) >> 1;
for(ll i = s;i >= 1;i--){
if(std::__gcd(i,n) == 1){
print(i);
puts("");
break;
}
}
}
int main(){
ll t = read();
while(t--){
solve();
}
exit(0);
}
求通过