[codeforces-542-C]YY?
链接:http://codeforces.com/problemset/problem/542/C
题意:对一个函数f(x),定义域[1,n], 令f(k,x) = f(f(f(f...f(x))))(共迭代k次)。求最小的k,使得f(k, x) 满足 g(g(x)) = g(x)的性质,也就是f(k,f(k,x)) = f(k,x)(x属于[1,n])。
思路:方法虽然简单,但比较巧妙,值得一做。由于f(k,x)只与f的迭代次数有关,而f数组一开始是给定的,所以不妨对所有的x属于[1,n]单独进行处理。考虑f(1,x),f(2,x),f(3,x)...,f(k,x)组成的序列,因为f(k,f(k,x)) = f(2k, x),所以转化为求序列中第k位置的数Ak,使得Ak = A2k。由于序列的后一项是前一项进行一次f()操作得到的,所以必存在循环节,且循环节长度小于等于n,不妨令序列从P0位置开始循环,循环节长度为K0,则这种情况下的k的取值集合就是{sK0, (s+1)K0, (s+2)K0, ...},sK0为大于等于P0的最小数。然后对x取遍[1,n],求k的取值集合的交集元素的最小值便是答案,但显然不能这样求。由于对自变量的每个值,k的取值总是那种情况下的循环节K0的倍数,不妨先对所有的K0求一下最小公倍数,然后再调整结果使得它满足大于等于所有的P0,调整的时候只需每次加上所有K0的最小公倍数,直到满足条件。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 3e4 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } int f[];
int minv; int find(int x) {
int used[], c = ;
mem0(used);
while (!used[f[x]]) {
c ++;
used[x = f[x]] = c;
}
max_update(minv, used[f[x]]);
return c - used[f[x]] + ;
} int main() {
//freopen("in.txt", "r", stdin);
int n;
cin >> n;
rep_up1(i, n) {
sint(f[i]);
}
LL ans = ;
rep_up1(i, n) {
LL tmp = find(i);
ans = ans / gcd(ans, tmp) * tmp;
}
LL tmp = ans;
while (ans < minv) ans += tmp;
cout << ans << endl;
return ;
}
[codeforces-542-C]YY?的更多相关文章
- 如何获取codeforces的完整数据?(玄学方法)
做cf题总是wa,wa了以后还没发看完整数据,好气哦! 怎么办? 这其实非常简单 首先看一下wa的那个数据有什么特点 比如说n = 1111,m = 1111 那么就if(n == 1111 & ...
- [Codeforces 1037D] Valid BFS?
[题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...
- 【Codeforces 20C】 Dijkstra?
[题目链接] 点击打开链接 [算法] dijkstra [代码] #include<bits/stdc++.h> using namespace std; typedef long lon ...
- 遇到个小问题,Java泛型真的是鸡肋吗?
今天遇到一个小问题,让我感觉Java的泛型(因为背负了历史的包袱导致的)有点鸡肋啊. 我们经常会遇到要一些自定义的key-value字符串,比如: "key1:1k;key2:2;key3: ...
- 如何获取codeforces的完整数据
推荐: 如何获取codeforces的完整数据?(玄学方法) http://www.cnblogs.com/Saurus/p/6220513.html
- [C++]Yellow Cards - GYM - 102348A(Practice *) - CodeForces
1 Problem Description Problem The final match of the Berland Football Cup has been held recently. Th ...
- 1.Java网络编程之概述
黑马程序员_毕向东_Java基础视频教程第23天-01-网络编程(概述)学习笔记 网络通讯三要素: 1.IP地址 I.网络中设备的标识 II.不易记忆,可用主机名 www 万维网组织,baidu主机 ...
- 输出缓存与CachePanel
缓存的级别 缓存的作用自不必说,提高系统性能最重要的手段之一.上至应用框架,下至文件系统乃至CPU,计算机中各部分设计都能见到缓存的身影.许多朋友一直在追求如何提高Web应用程序的性能,其实最容易被理 ...
- Java安全之CC4,5,7
前言 前边已经将CC链中的关键部分学习差不多,接下来就是一些扩展思路, CC4 ObjectInputStream.readObject() PriorityQueue.readObject() Pr ...
随机推荐
- cwyth(自动核销代码)
财务一体化系统,自动核销大数据代码: import pymysql import random import time #指定数据库地址.用户.密码.端口,使用connect()方法声明一个Mysql ...
- Os-Hax: 1 靶机记录
靶机地址:172.16.1.197 Kali地址:172.16.1.108 1 信息搜集 靶机首页 相关信息查看 端口扫描: 开放22和80 目录扫描: 访问http://172.16.1.197/c ...
- Java 集合框架总结--导图
java的集合导图总结:
- js拼接php拼接
当我们用到ajax的时候,局部替换的时候,我们可以在前台拼接,后台拼接,这个取决于你是前端后端这样拼接判断比较好, 判断不拼接,判断的值进行拼接 然后在html 替换 $.each 前台循环 ...
- SNMP History and OID/MIB Tour
https://www.pei.com/snmp-history-oid-mib/ Description: This document describes a bit of history and ...
- How to check if directory exist using C++ and winAPI
如果看文件夹是否存在,必须看返回值是不是 INVALID_FILE_ATTRIBUTES #include <windows.h> #include <string> bool ...
- [Hands-on-Machine-Learning-master] 02 Housing
用到的函数 numpy.random.permutation随机排列一个序列,返回一个排列的序列. >>> np.random.permutation(10) array([1, 7 ...
- 2 个案例带你迅速入门 Python Flask 框架
Flask 是 python 中非常流行的一个 web 框架,容易学习.这篇文章主要通过 2 个实际案例讲解 Flask 如何使用.第一个例子是实现一个调用公交车到站信息的接口服务:第二个例子是通过接 ...
- Element UI表格组件技巧:如何简洁实现跨页勾选、跨页统计功能
业务场景 在使用Element UI的Table组件时,常常面对这样的业务需求: 表格数据的每一项都要提供勾选框,当切换分页时,能够记忆所有页面勾选的数据,以实现批量提交不同页面勾选数据的功能.并且, ...
- Mybatis详解系列(五)--Mybatis Generator和全注解风格的MyBatis3DynamicSql
简介 Mybatis Generator (MBG) 是 Mybatis 官方提供的代码生成器,通过它可以在项目中自动生成简单的 CRUD 方法,甚至"无所不能"的高级条件查询(M ...