AtCoder Beginner Contest 161 F.Division or Subtraction

114 篇文章 1 订阅
108 篇文章 0 订阅

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值