AtCoder Beginner Contest 161 F.Division or Subtraction

鲜于勇
2023-12-01

AtCoder Beginner Contest 161 F.Division or Subtraction

题目链接

Problem Statement

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.

  • Operation: if K divides N, replace N with N/K; otherwise, replace N with N−K.

In how many choices of K will N become 1 in the end?

Constraints

2 ≤ N ≤ 1 0 12 2≤N≤10^{12} 2N1012
N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9

对每个数 n n n,使 n n n 变为 1 1 1 必有两个数 n − 1 n-1 n1 n n n 两个数(2除外,因为1不符合条件)
如果满足除操作,那么 k k k 一定是 n n n 的因子;如果满足减操作得到1,那么 k k k 一定是 n − 1 n-1 n1 的因子,所以可选的所有数必然在 n 的因子内,即 1 − n 1-\sqrt{n} 1n
对数 k k k,我们可以直接模拟上述过程判断,对 n / k n/k n/k,我们可以直接用 ( n − 1 ) % k (n-1)\%k (n1)%k 进行判断,注意特判一下 k ∗ k ! = n − 1 k*k!=n-1 kk!=n1,避免重复判断。
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;
}
 类似资料:

相关阅读

相关文章

相关问答