hunnu11550:欧拉函数
Problem description |
一个数x的欧拉函数Φ(x)定义为全部小于x的正整数中与x互质的数的数目,如小于5且和5互质的数有1、2、3、4,一共4个,故Φ(5)=4。
对于随意正整数x,我们定义两种操作: |
Input |
每行输入一个整数a(0<a<=100000)。 |
Output |
输出须要的步数,假设无法得到,输出-1。 |
Sample Input |
2 |
Sample Output |
1 |
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:欧拉函数的更多相关文章
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- COGS2531. [HZOI 2016]函数的美 打表+欧拉函数
题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- 51Nod-1136 欧拉函数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...
- 欧拉函数 - HDU1286
欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- BZOJ2668: [cqoi2012]交换棋子(费用流)
Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列的格子只能参与mi,j次交换. Input 第一行 ...
- python编程练习
python练习之冒泡排序: python代码: #coding=utf-8 if __name__=="__main__": arr=[3,2,1,7,11,4,5,8] pri ...
- Cocos2d-x游戏的一般验证分析
Coco2d-x引擎是相对于Unity3D的又一实力派引擎.尽管随着3D游戏的热门,很多其它的厂商偏向于Unity3D.可是Coco2d-x的普及量也不容小觑,特别是一些比較大的手游公司.比方触控科技 ...
- 关于exports 和 module.exports
本文来源为node.js社区附上链接 http://cnodejs.org/topic/5231a630101e574521e45ef8 require 用来加载代码,而 exports 和 modu ...
- Windows共享上网的详细设置
作者:朱金灿 来源:http://blog.csdn.net/clever101 在Windows环境下在A和B在同一个局域网上,A机子可以上网,B机子可以通过A机子可以通过设置的网络共享来上网.其中 ...
- 请求头header里的contentType为application/json和capplition/x-www-form-urlencoded
application/x-www-form-urlencoded 最常见的 POST 提交数据的方式了.浏览器的原生 <form> 表单,如果不设置 enctype 属性,那么最终就会以 ...
- background 背景认知
background 背景 背景颜色 /*背景颜色为红色*/ p { background-color:ren; } 网页背景不仅可以设置颜色还可以插入图片 /*为背景插入图片*/ body { ba ...
- 窗体是不出现在Alt+Tab中(窗体不出现在任务管理器中的应用程序列中)
窗体是不出现在Alt+Tab中和不出现在任务管理器中的应用程序中 重写 CreateParams即可: public class MyForm : Form{ protected override C ...
- 【习题 8-9 1613】 K-Graph Oddity
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉最大度数|1就是最多需要的个数了. 就贪心一下. 然后模拟染色的过程就可以了. (贪心染色就可以了 (看看周围哪个颜色没有,就用 ...
- 如何在win10上同时安装python2和python3
哎,其实本人已经用惯了python2,听说python3的语法有很多不一样的地方,那我之前写的算法改起来岂不是日了狗了吗?所以一直没改用python3.但是谷歌的那个TensorFlow,在windo ...