Codeforces Round #334 D. Moodular Arithmetic(并查集)

题意:现在有一个函数

给出p和k,问这种函数有多少个。

思路:并查集,对于一个函数值,如果确定它,那么就可以确定所有和它有关系的函数值,用并查集维护一下即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

//const int MAXN = 5000000 + 5;
//const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n;
LL p, k;
set<LL> s;
LL fa[1000100];
int Find(LL x) {
    if(x != fa[x]) return fa[x] = Find(fa[x]);
    return x;
} 

int cnt[1001000];
int main() {
    //freopen("input.txt", "r", stdin);
    cin >> p >> k;
    s.insert(0);
    LL ans = 1;
    if(k==0) {
        for(int i = 1; i < p; i++) ans = ans * p % mod;
        cout << ans;
        return 0;
    }
    for(int i = 0; i < p; i++) fa[i] = i;
    for(int i = 0; i < p; i++) {
        LL shit = k * i % p;
        int x1 = Find(i), x2 = Find(shit);
        fa[x1] = x2;
    }
    for(int i = 0; i < p; i++) {
        int z = Find(i);
        cnt[z]++;
    }
    for(int i = 1; i < p; i++)
        if(cnt[i]) ans = ans * p % mod;
    if(k == 1) ans = ans * p % mod;
    cout << ans;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值