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 ...
随机推荐
- MySQL Field排序法
检索 id = 2 or id = 5 or id = 9 or id = 56 or id = 38.然后按照 2 , 5, 9, 56, 38 这个顺序排列,这是题目要求 以下为解决方案: 1 ...
- 讲的好,php后端模式,php-fpm以及php-cgi, fast-cgi,以及与nginx的关系
关于cgi是什么,fast-cgi是什么,php-cgi是什么,fast-cgi是什么,下面这篇讲的很清楚: https://segmentfault.com/q/1010000000256516 另 ...
- 转:IOS的推送。是一个强大的功能
IOS下我们很多人天天开着 邮件推送 微信推送 QQ推送 微博推送 新浪微博推送,安卓敢吗? 五个后台进程消耗电.流量谁敢这么做?现在安卓也出了推送,但绝对是伪推送.实际是挂了个进程,关掉了就收不到. ...
- HDOJ 5419 Victor and Toys 树状数组
分母是一定的C(m,3) 树状数组求每一个数能够在那些段中出现,若x出如今了s段中,分子加上w[x]*C(s,3) Victor and Toys Time Limit: 2000/1000 MS ( ...
- jquery06 jQuery.extend 给jQuery函数添加、继承 静态方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- js--11对象的创建方式
<html> <head> <title>Object</title> </head> <body> <script ty ...
- 13.Axis创建webservice客户端和服务端
转自:https://blog.csdn.net/chenghui0317/article/details/9318317 一.Axis的介绍 Web Service是现在最适合实现SOA的技术,而A ...
- c# 装箱与拆箱的概念
1装箱 就是将值类型的数据赋值给引用类型的实例中 比如 int类型的123赋值给Object o int i=123; Object o=(Object) i; 2拆箱 就是从引用类型的数据中提取数据 ...
- c# 用代码来设置程序的PrivatePath
原文:c# 用代码来设置程序的PrivatePath 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sweety820/article/detail ...
- POJ 4047 Garden
Garden Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 404 ...