geronimo

http://192.168.102.138/JudgeOnline/problem.php?cid=1279&pid=0

知识点:1.碰到mod就要留个心眼(什么除啊减啊这些东西可能不能直接mod)

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,mod;
int f[500100];
int vis[500100],v[500100];
int cnt = 0;
int gcd(int x,int y)
{
    if(x % y == 0)return y;
    else return gcd(y,x % y);
}
int qpow(int x,int t)
{
    int tans = x,ret = 1;
    while(t)
    {
        if(t&1)ret *= tans,ret %= mod;
        tans *= tans;
        tans %= mod;
        t >>= 1;
    }
    return ret % mod;
}
signed main()
{
    scanf("%lld%lld",&n,&mod);
    for(int i = 1;i <= n;i++)
        scanf("%lld",&f[i]);
    for(int i = 1;i <= n;i++)
    {
        if(f[i] == i)continue;
        if(!vis[i])
        {
            vis[i] = 1;
            int d = 1;
            int t = f[i];
            while(t != i)
            {
                d++;
                vis[t] = 1;
                t = f[t];
            }
            v[++cnt] = d;
        }
    }
    v[0] = 0;
    int pri = 1;
    for(int i = 1;i <= cnt;i++)
    {
        pri *= v[i];
        pri %= mod;
    }
    for(int i = 2;i <= cnt;i++)
    {
        pri *= qpow(gcd(v[i],v[i - 1]),mod - 2);
        pri %= mod;
    }
    printf("%lld",pri);
    return 0;
}//这是假代码,只能处理mod为质数的情况

 

posted @ 2020-09-24 13:08  ywwywwyww  阅读(189)  评论(0编辑  收藏  举报