Problem description
  一个数x的欧拉函数Φ(x)定义为全部小于x的正整数中与x互质的数的数目,如小于5且和5互质的数有1、2、3、4,一共4个,故Φ(5)=4。

对于随意正整数x,我们定义两种操作: 

1、f(x) = x + Φ(x);

2、g(x) = x * Φ(x);



如今,给定一个数a,问从1開始,须要多少步操作能得到a。

(如,当a = 2时,f(1)即为所求,故答案为1。而当a = 3时,f(f(1))即为所求。故答案为2)

Input
  每行输入一个整数a(0<a<=100000)。 
Output
  输出须要的步数,假设无法得到,输出-1。
Sample Input
2
3
Sample Output
1
2
Problem Source
  HUNNU Contest 

打出函数表,然后搜索便能够了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <time.h>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100000
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8 int oula[N+5];
int ans[N+5];
void bfs()
{
int i,j,k;
queue<int> Q;
for(i = 1;i<=N;i++)
{
oula[i]=i;
ans[i] = INF;
}
for(i = 2;i<=N;i++)
{
if(i==oula[i])
{
for(j = 1;j*i<=N;j++)
{
oula[j*i] = (oula[j*i]/i)*(i-1);
}
}
}
ans[1] = 0;
Q.push(1);
while(!Q.empty())
{
int s = Q.front();
Q.pop();
if(s+oula[s]<=N&&ans[s+oula[s]]>ans[s]+1)
{
ans[s+oula[s]]=ans[s]+1;
Q.push(s+oula[s]);
}
if((LL)s*oula[s]>N) continue;
if((LL)s*oula[s]<=N&&ans[s*oula[s]]>ans[s]+1)
{
ans[s*oula[s]]=ans[s]+1;
Q.push(s*oula[s]);
}
}
} int main()
{
LL i,j,k;
int n;
bfs();
while(~scanf("%d",&n))
{
if(ans[n]==INF) puts("-1");
else
printf("%d\n",ans[n]);
} return 0;
}

hunnu11550:欧拉函数的更多相关文章

  1. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  2. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  3. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  4. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

  5. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  6. 51Nod-1136 欧拉函数

    51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...

  7. 欧拉函数 - HDU1286

    欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...

  8. FZU 1759 欧拉函数 降幂公式

    Description   Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...

  9. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. oracle基础入门(一)

    一:Oracle的安装     我所知的Oracle的数据库有 10g,11g, 12g, 我们常用的是10g ,因为10g有scott的用户,而11和12现在还没有(哪位大神看到有问题可以指出哦), ...

  2. 洛谷—— P1629 邮递员送信

    https://www.luogu.org/problem/show?pid=1629 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比 ...

  3. Linux中 ps aux 命令

    $ ps aux USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND root 11 100.0 0.0 0 16 ?? RL 4Dec09 ...

  4. openfire 开发遇到的些问题

    openfire的 jid    账户名 + '@" + 你的域名      可是当你的账户名中 有大拼音的 时候  就会变成小写   比如     Test  ,  jid  =  tes ...

  5. OpenCascade Sweep Algorithm

    OpenCascade Sweep Algorithm eryar@163.com Abstract. Sweeps are the objects you obtain by sweeping a ...

  6. H.264视频编解码SoC满足高清DVR设计需求

    硬盘录像机(DVR)作为监控系统的核心部件之一,在10年里高速发展,从模拟磁带机的替代品演变成具有自己独特价值的专业监控数字平台,并被市场广泛接受.监控系统伴随DVR这些年的发展向着IP化.智能化发展 ...

  7. express中的中间件理解

    什么是中间件 中间件是一个可访问请求对象(req)和响应对象(res)的函数,在 Express 应用的请求-响应循环里,下一个内联的中间件通常用变量 next 表示.中间件的功能包括: 执行任何代码 ...

  8. qgis显示引擎研究(一)

    作者:朱金灿 来源:http://blog.csdn.net/clever101 Qgis是一个著名的开源地理信息系统软件.今天研究了一下它的显示那一块,算是有一点眉目了. 在Windows环境下所有 ...

  9. C#开发 —— 高级应用

    迭代器 可以返回相同类型的值的有序序列的一段代码,可用作方法,运算符或get访问器的代码体 使用 yield return 语句依次返回每个元素,yield break 语句可将终止迭代 迭代器的返回 ...

  10. HDU 2564 饭卡

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...