2020牛客寒假算法基础集训营1 E. rin和快速迭代
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
”数论真的太好玩了喵~“——hoshizora rin
rin最近喜欢上了数论。
然而数论实在太复杂了,她只能研究一些简单的问题。
这天,她在研究正整数因子个数的时候,想到了一个“快速迭代”算法。设f(x)为x的因子个数,将f迭代下去,rin猜想任意正整数最终都会变成2。
例如:f(12)=6,f(6)=4,f(4)=3,f(3)=2。
她希望你帮她验证一下。她会给你一个正整数n,让你输出它在迭代过程中,第一次迭代成2的迭代次数。
输入描述
一个正整数 n ( 3 < = n < = 1 0 1 2 ) n(3<=n<=10^12) n(3<=n<=1012)
输出描述
一个正整数,为n迭代至2的次数
示例1
输入
12
输出
4
说明
12的因子:1,2,3,4,6,12。共6个。 6的因子:1,2,3,6。共4个。 4的因子:1,2,4。共3个。 3的因子:1,3。共2个。 12 → 6 → 4 → 3 → 2 , 故迭代了4次。
思路
本来想着是用唯一分解定理,但是没想到,直接暴力就可以
因为一个数的因子数是远小于这个数的
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cctype>
#include <stack>
#include <map>
#include <cstring>
#include <sstream>
#include <set>
#include <list>
#include <fstream>
#include <iomanip>
#include <assert.h>
#define ll long long
#define ull unsigned long long
#define pii std::pair<int, int>
#define op \
std::ios::sync_with_stdio(false); \
std::cin.tie(0);
const int INF = 0x3f3f3f3f;
const int maxn = (100000 + 5);
const int mod = (998244353);
ll solve(ll x)
{
ll num=0;
for(ll i=1;i<=sqrt(x);i++)
if(x%i==0)
if(i*i==x)
num++;
else
num+=2;
return num;
}
int main()
{
ll n;
int ans=0;
scanf("%lld",&n);
while(n!=2)
{
ans++;
n=solve(n);
}
printf("%d\n",ans);
return 0;
}