【Codeforces】542C - Idempotent functions(思路)

题目大意思:

给定一个数的next值,问最小的k值,使得每次进行k次操作得到的数都是一样的。

将每个位置i记为点i

记circle[i]为从i出发出现环的环内元素,我们的任务就是求这些数的最小公倍数lcm,还要考虑‘6’型循环的情况,这个我们只需要求最大的多余长度之后对lcm向上取整就好了

#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 205;
int next[maxn],vis[maxn];
LL circle[maxn],maxv = 0;
LL gcd(LL a,LL b){
    return b == 0 ? a : gcd(b,a % b);
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
        scanf("%d",&next[i]);
    for(int i = 1; i <= n; i++){
        stack<int>Stack;
        memset(vis,0,sizeof(vis));
        int pos = i;
        circle[i] = 0LL;
        while(!vis[pos]){
                vis[pos] = 1;
                Stack.push(pos);
                pos = next[pos];
        }
        while(true){
             int e = Stack.top(); Stack.pop();
             circle[i] ++;
             if(e == pos) break;
        }
        maxv = max(maxv,(LL)Stack.size());
    }
    LL lcm = circle[1];
    for(int i = 2; i <= n; i++){
         lcm = lcm / gcd(lcm,circle[i]) * circle[i];
    }
    LL temp = maxv / lcm;
    if(maxv % lcm || maxv == 0) temp ++;
    LL ans = temp * lcm;
    printf("%lld\n",ans);
    return 0;
}
时间: 2024-10-24 14:27:46

【Codeforces】542C - Idempotent functions(思路)的相关文章

Codeforces 542C - 漫长艰辛的解题历程

尽管在下面的一部分解题的方法都有问题... 但是每一次重新提交都是打了一个有意义的补丁的呢 可见在我做题的过程中有多少问题存在... 现在想想,类似于是变量没有开int64,过程函数忘开int64..还有就是中间的很多问题 C. Idempotent functions Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., n} is

POJ - 1080 - Human Gene Functions (LCS的变形)

题目传送:Human Gene Functions 思路:LCS的变形,定义状态dp[ i ][ j ]为取字符串s前i个字符字符串t前j个字符所获得的最大值,则可以得到状态转移方程为: dp[ i ][ j ] = max(dp[ i ][ j - 1] + f[ ' - ' ][ t[ j ] ], dp[ i - 1 ][ j ] + f[ s [ i ] ][ ' - ' ], dp[i - 1][ j - 1] + f[ s [ i ] ][ t [ j ] ]); AC代码: #in

第三届H-star 程序设计竞赛初赛题解

1.剪纸片:这是一道简单的题目,假如你身边有一张纸,一把剪刀,在H-star的比赛现场,你会这么做:(1). 将这张纸剪成两片(平行于短边剪开):(2)将其中一片剪成一个圆,作为圆柱的底面:(3) 纸的另一片的一边沿着圆的周长将圆围起来,直到围成一圈,形成一个无盖的圆柱体.需要注意的是,纸片可能会有重叠部分.聪明的你机智的你喜欢思考的你这时候就开始想,一张纸片按上述方式所组成的圆柱的最大体积是多少呢?请你用编程解决这个问题. 输入 输入第一行包含一个数字t代表接下来有t组数据: 接下来的t行,输

【USACO1.1.2】Greedy Gift Givers(map)

Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receiv

CSUOJ 1525 Algebraic Teamwork

Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate and join their theories. In group theory, permutations – also known as bijective functions – play an important role. For a finite set A, a function σ

codeforces——思路与规律

codeforces 804B     http://codeforces.com/problemset/problem/804/B /* 题意:给定一个只含ab的序列,每次操作可将ab变为bba 问至少变换多少次使序列不含形式为"ab"的子串 这题没想出来......唉. 思路:从后往前处理,每次经过一个a,都要将它移到最后, 共经过x个b,将ans+=x,同时b的个数翻倍x += x:每经过一个b,x++. */ #include<iostream> #include&

看个人思路吧,清晰的话就简单 CodeForces 271A - Beautiful Year

It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits. Now you are suggested to solve the following problem: given a year number, find the minim

CodeForces 534C - Polycarpus&#39; Dice(思路)

题意:给定n (1 <= n <= 2*10^5) 个骰子,给定每个骰子最大可以掷出的最大数(最小数始终为1),给定所有骰子掷出的点数和A,求每个骰子不可能掷出的点数个数. 考虑最大和最小情况,作差即可(详情见代码注释) #include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream&g

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about