AtCoder Beginner Contest 161 F.Division or Subtraction

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}
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 变为 1 1 必有两个数 n 1 n-1 n n 两个数(2除外,因为1不符合条件)
如果满足除操作,那么 k k 一定是 n n 的因子;如果满足减操作得到1,那么 k k 一定是 n 1 n-1 的因子,所以可选的所有数必然在 n 的因子内,即 1 n 1-\sqrt{n}
对数 k k ,我们可以直接模拟上述过程判断,对 n / k n/k ,我们可以直接用 ( n 1 ) % k (n-1)\%k 进行判断,注意特判一下 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;
}
发布了479 篇原创文章 · 获赞 38 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/105580237
今日推荐