Given is a positive integer N.
We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.
In how many choices of K will N become 1 in the end?
2
≤
N
≤
1
0
12
2≤N≤10^{12}
2≤N≤1012
N is an integer.
Input is given from Standard Input in the following format:
N
Print the number of choices of K in which N becomes 1 in the end.
6
3
3141
13
314159265358
9
对每个数
n
n
n,使
n
n
n 变为
1
1
1 必有两个数
n
−
1
n-1
n−1 和
n
n
n 两个数(2除外,因为1不符合条件)
如果满足除操作,那么
k
k
k 一定是
n
n
n 的因子;如果满足减操作得到1,那么
k
k
k 一定是
n
−
1
n-1
n−1 的因子,所以可选的所有数必然在 n 的因子内,即
1
−
n
1-\sqrt{n}
1−n
对数
k
k
k,我们可以直接模拟上述过程判断,对
n
/
k
n/k
n/k,我们可以直接用
(
n
−
1
)
%
k
(n-1)\%k
(n−1)%k 进行判断,注意特判一下
k
∗
k
!
=
n
−
1
k*k!=n-1
k∗k!=n−1,避免重复判断。
AC代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n,ans=2;
cin>>n;
if(n==2) ans=1;
for(ll k=2;k*k<=n;k++){
ll m=n;
while(m>=k) m%k?m%=k:m/=k;
if(m==1) ans++;
if((n-1)%k==0 && k*k!=(n-1)) ans++;
}
cout<<ans<<endl;
return 0;
}