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 ...
随机推荐
- window.location.href和window.top.location.href的区别
if (window.location.href == window.top.location.href) { window.top.location.href = "/index. ...
- 洛谷—— P2387 魔法森林
题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...
- HTTP请求具体解释
1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,"消息头/消息体"的切割方式是非经常常使用的.消息头告诉对方这个消息是干什么的,消息体告诉对方怎么 ...
- jquery05 继承
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 43.安装npm及cnpm(Windows)
转自:https://www.cnblogs.com/yominhi/p/7039795.html [工具官网] Node.js : http://nodejs.cn/ 淘宝NPM: https:// ...
- SSH远程快速登录Linux
SSH远程快速登录Linux 使用SSH管理linux服务器,通常要使用ssh,然后输入用户,密码,其实只要配置一个文件就可以方便登录.假设要登录server域名是www.interne ...
- javafx mouseEvent
public class EffectTest extends Application { Path path; @Override public void start(Stage primarySt ...
- javafx ComboBox Event and change cell color
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- 「HAOI2018」字串覆盖
「HAOI2018」字串覆盖 题意: 给你两个字符串,长度都为\(N\),以及一个参数\(K\),有\(M\)个询问,每次给你一个\(B\)串的一个子串,问用这个字串去覆盖\(A\)串一段区间的最 ...
- 【例题 8-11 UVA-10954】Add All
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 就是合并果子.. 每次都合并最小的就可以啦. 别忘了初始化 [代码] /* 1.Shoud it use long long ? 2 ...