Codeforces 524C Idempotent functions
题目链接:http://codeforces.com/problemset/problem/542/C
1 second
256 megabytes
standard input
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.
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 minimum k such that function f(k)(x) is idempotent.
4 1 2 2 4
1
3 2 3 3
2
3 2 3 1
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相关推荐
- Codeforces 524C Idempotent functions
题目链接:http://codeforces.com/problemset/problem/542/C 题目大意:给定一种运算f,对于输入的数组来说,一步操作,f(x) = a[x],两步操作,f^2 ...
- 【CodeForces - 789C】Functions again(最大子段和变形,dp,思维)
题干: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian s ...
- CSUOJ 1525 Algebraic Teamwork
Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...
- grpc调用_避免使用套接字远程过程调用以及如何使用grpc进行程序通信
grpc调用 As we all know, it's common to use sockets to create a communication channel between 2 progra ...
- codeforces 757e Bash Plays with Functions
题意: 给你两个数 n和r 可以做r次分解 当r大于0 n可以分解成两个因子u和v u*v=n 当r 等于0 n分解出的两个因子的gcd要为1 如题意所给的公式来计算最终结果 思路: 仔细观察 在 ...
- 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 ...
- Codeforces数学1600day3[数学CodeForces - 1213D2, CodeForces - 1165E 数论,CodeForces - 1165D 因子分解]
ps:day2太水了不写qwq A - Equalizing by Division (hard version) CodeForces - 1213D2 题目大意:给你n个数和一个k,然后你可以执行 ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...
最新文章
- CCS编译出错:缺少头文件的解决办法
- 【LeetCode从零单排】No14.LongestCommonPrefix
- 如何避免成为新技术背后的“奶嘴一族”
- AJAX淋漓尽致的发挥(Google个性化主页 VS. Windows Live.COM)站在互联网浪尖上窃喜...
- 数据结构与算法之二冒泡排序
- linux终端执行二进制文件命令,Linux下查看二进制文件命令
- PADS9.5 导入立创元器件库卡顿
- 报表热切换是什么意思?如何做到?
- 浏览器兼容性及调试插件
- 基因本体论GO(Gene Ontology)
- 不再害怕打字慢(python信息轰炸)
- Python 数据可视化--Seaborn绘图总结1
- Ubuntu 台式机 usb WiFi 接收器设置方法
- 一元享移动怎么样_移动新套餐:18元享“全免流”+1元1G流量,阿里鱼卡要遭殃?...
- HTML5--表单新特性使用自定义错误提示消息validity属性
- 工具篇 之 简单掌握 Chrome 以及 IE DeBug 。。。
- ServerSocket通过构造方法绑定端口
- 16.【Linux】window和linux下文件格式相互转换
- 【转】光通信主要会议和期刊总结
- php验证码有图片没数字,php验证码图片不显示
热门文章
- 海思芯片中VPSS的group和channel的概念.
- 袋鼠云数据中台专栏(五):数栈,企业级一站式数据中台PaaS
- 做软件还是做电商,跪舔还是尊严?
- 宝塔linux取消登录,宝塔面板如何关闭安全入口
- 王倩兮金碧山水作品《绿水青山就是金山银山》
- 打开PHP时候,提示找不到指定模块解决办法
- 华为手机语音通话时断时续原因
- 一个简单光栅器的实现(五) 光栅化阶段
- 当Myeclipse或者Eclipse出现launching client
- 当你发现微信好友朋友圈是“一条杠”,你会把她、他删除吗?