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 ...
随机推荐
- py2exe打包python脚本
在工作中遇到将python脚本转换成exe可执行程序的需求,通过查询可以使用py2exe来构建满足要求的程序,这里简要说明一下使用步骤. 一.py2exe是一个将python脚本转换成windows上 ...
- VUE里子组件获取父组件动态变化的值
在VUE里父组件给子组件间使用props方式传递数据,但是希望父组件的一个状态值改变然后子组件也能监听到这个数据的改变来更新子组件的状态. 场景:子组件通过props获取父组件传过来的数据,子组件存在 ...
- Scrapy请求传参
scrapy.Request(url=url, callback=self.parse_item, meta={'item': item}, headers=headers) url: 要请求的地址 ...
- debian8平滑升级到debian9
本文在Creative Commons许可证下发布. 首先,在升级时可以查看一下自己的版本号: uname -a ##查看内核信息 cat /etc/issue ##查看发行版本号 方法1:利用网 ...
- cogs 49. 跳马问题
49. 跳马问题 ★ 输入文件:horse.in 输出文件:horse.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 有一只中国象棋中的 “ 马 ” ,在半张 ...
- [Javascript] Compose multiple functions for new behavior in JavaScript
In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...
- CCNA Cloud CLDFND 210-451 QUIZ: Server Virtualization
Author:海峰 http://weibo.com/344736086 http://yanheven.github.io/ http://blog.csdn.net/yanheven1 1.Whi ...
- android5.x加入sim1,sim2标识
1,mobile_signal_group.xml ..... <FrameLayout android:id="@+id/mobile_combo" android:la ...
- select选择框实现跳转
select选择框实现跳转 零.启示 1.启示把bom里面的几个对象稍微有点印象,那么主干有了,学其它的就感觉添置加瓦,很简单 就是关注树木的主干 2.万能的百度,不过当然要你基础好学得多才能看得懂, ...
- 63.note.js之 Mongodb在Nodejs上的配置及session会话机制的实现
转自:https://www.cnblogs.com/alvin_xp/p/4751784.html 1.第一步安装mongodb数据库,这直接官网下载,这里不介绍. 2.也可以使用npm实现直接下载 ...