题目链接:http://codeforces.com/problemset/problem/542/C

C. Idempotent functions
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., n} is such function , that for any  the formula g(g(x)) = g(x) holds.

Let's denote as f(k)(x) the function f applied k times to the value x. More formally, f(1)(x) = f(x), f(k)(x) = f(f(k - 1)(x)) for each k > 1.

You are given some function . Your task is to find minimum positive integer k such that functionf(k)(x) is idempotent.

Input

In the first line of the input there is a single integer n (1 ≤ n ≤ 200) — the size of function f domain.

In the second line follow f(1), f(2), ..., f(n) (1 ≤ f(i) ≤ n for each 1 ≤ i ≤ n), the values of a function.

Output

Output minimum k such that function f(k)(x) is idempotent.

Sample test(s)
input
4
1 2 2 4

output
1

input
3
2 3 3

output
2

input
3
2 3 1

output
3

题目大意:给定一种运算f,对于输入的数组来说,一步操作,f(x) = a[x],两步操作,f^2(x) = a[a[x]]....倘若每次进行k步操作之后,得到的都是同一个数,即k为符合条件的步数。求出能令所有数都符合条件的最小步数。

初步我们可以很快想到,如果能求出每个数的符合条件的最小步数,然后求出这些步数的最小公倍数即可。但是需要注意的是,对于一开始并没有直接进入循环的情况,即"6"字形的情况,我们要分开求出多余的长度len[i]和循环节长度step[i],选出最长的MAXlen,ans即为MAXlen对所有step[i]的最小公倍数的上取整。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAXN 500
typedef long long LL;
int n;
int a[MAXN];
LL step[MAXN];
int vis[MAXN], v[MAXN];
int main() {LL ans, maxNum = 0;scanf("%d", &n);for(int i = 1; i <= n; i++)scanf("%d", &a[i]);for(int i = 1; i <= n; i++) {memset(vis, 0, sizeof(vis));stack<int> s;int j;for(j = i; !vis[j]; j = a[j]) {vis[j] = 1;s.push(j);}LL num = 1;while(s.top() != j) {s.pop(); num++;}s.pop();step[i] = num;maxNum = max(maxNum, (LL)s.size());}ans = step[1];for(int i = 1; i <= n; i++) {ans = ans / __gcd(ans, step[i]) * step[i];}LL temp = maxNum / ans;if(maxNum % ans || maxNum == 0) temp++;ans = temp * ans;printf("%I64d\n", ans);return 0;
}

Codeforces 524C Idempotent functions相关推荐

  1. Codeforces 524C Idempotent functions

    题目链接:http://codeforces.com/problemset/problem/542/C 题目大意:给定一种运算f,对于输入的数组来说,一步操作,f(x) = a[x],两步操作,f^2 ...

  2. 【CodeForces - 789C】Functions again(最大子段和变形,dp,思维)

    题干: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian s ...

  3. CSUOJ 1525 Algebraic Teamwork

    Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...

  4. grpc调用_避免使用套接字远程过程调用以及如何使用grpc进行程序通信

    grpc调用 As we all know, it's common to use sockets to create a communication channel between 2 progra ...

  5. codeforces 757e Bash Plays with Functions

    题意: 给你两个数 n和r 可以做r次分解 当r大于0 n可以分解成两个因子u和v   u*v=n 当r 等于0 n分解出的两个因子的gcd要为1 如题意所给的公式来计算最终结果 思路: 仔细观察 在 ...

  6. CodeForces 757 E.Bash Plays with Functions(积性函数+dp)

    Description 定义f0(n)f_0(n)为满足p⋅q=n,gcd(p,q)=1p\cdot q=n,gcd(p,q)=1的有序对(p,q)(p,q)对数,定义fr+1(n)=∑u⋅v=nfr ...

  7. Codeforces数学1600day3[数学CodeForces - 1213D2, CodeForces - 1165E 数论,CodeForces - 1165D 因子分解]

    ps:day2太水了不写qwq A - Equalizing by Division (hard version) CodeForces - 1213D2 题目大意:给你n个数和一个k,然后你可以执行 ...

  8. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  9. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

最新文章

  1. CCS编译出错:缺少头文件的解决办法
  2. 【LeetCode从零单排】No14.LongestCommonPrefix
  3. 如何避免成为新技术背后的“奶嘴一族”
  4. AJAX淋漓尽致的发挥(Google个性化主页 VS. Windows Live.COM)站在互联网浪尖上窃喜...
  5. 数据结构与算法之二冒泡排序
  6. linux终端执行二进制文件命令,Linux下查看二进制文件命令
  7. PADS9.5 导入立创元器件库卡顿
  8. 报表热切换是什么意思?如何做到?
  9. 浏览器兼容性及调试插件
  10. 基因本体论GO(Gene Ontology)
  11. 不再害怕打字慢(python信息轰炸)
  12. Python 数据可视化--Seaborn绘图总结1
  13. Ubuntu 台式机 usb WiFi 接收器设置方法
  14. 一元享移动怎么样_移动新套餐:18元享“全免流”+1元1G流量,阿里鱼卡要遭殃?...
  15. HTML5--表单新特性使用自定义错误提示消息validity属性
  16. 工具篇 之 简单掌握 Chrome 以及 IE DeBug 。。。
  17. ServerSocket通过构造方法绑定端口
  18. 16.【Linux】window和linux下文件格式相互转换
  19. 【转】光通信主要会议和期刊总结
  20. php验证码有图片没数字,php验证码图片不显示

热门文章

  1. 海思芯片中VPSS的group和channel的概念.
  2. 袋鼠云数据中台专栏(五):数栈,企业级一站式数据中台PaaS
  3. 做软件还是做电商,跪舔还是尊严?
  4. 宝塔linux取消登录,宝塔面板如何关闭安全入口
  5. 王倩兮金碧山水作品《绿水青山就是金山银山》
  6. 打开PHP时候,提示找不到指定模块解决办法
  7. 华为手机语音通话时断时续原因
  8. 一个简单光栅器的实现(五) 光栅化阶段
  9. 当Myeclipse或者Eclipse出现launching client
  10. 当你发现微信好友朋友圈是“一条杠”,你会把她、他删除吗?