codeforces 603B (数学)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

B. Moodular Arithmetic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that

for some function . (This equation should hold for any integer x in the range0 to p - 1, inclusive.)

It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 109 + 7.

Input

The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 0000 ≤ k ≤ p - 1) on a single line. It is guaranteed thatp is an odd prime number.

Output

Print a single integer, the number of distinct functions f modulo 109 + 7.

Sample test(s)
input
3 2
output
3
input
5 4
output
25
Note

In the first sample, p = 3 and k = 2. The following functions work:

  1. f(0) = 0f(1) = 1f(2) = 2.
  2. f(0) = 0f(1) = 2f(2) = 1.
  3. f(0) = f(1) = f(2) = 0.

题意是给你一个奇质数p和小于p的数k,求满足题目所给方程的所有映射的种数.

因为p是质数,显然x*k属于不同的模p剩余系,也就是把x=0...p-1每一个都带进去以后左边的必然是f(0) f(1) ... f(p-1)的一个排列,右边本身是f(0)...f (p-1)的一个排列,这样就可以对于

f(x1) = k*f(x2)%p,找到对应的f(x2) = k*f(x3)%p,一直迭代下去,知道发现f(xl) = k*f(x1)%p,如此就可以把这一个环路转化为 f(x1) = k^l * f(x1)%p,把右边前面的系数先模p,发现当且仅当k^l%p==1时,f(x1)的值可以任取,也就是可以有p种取法,否则f(x1)的值只能为1.

这样就很简单了,建个图,搞个环,判断系数模p.

注意特判和long long.

#include <bits/stdc++.h>
using namespace std;
#define maxn 1111111
#define mod 1000000007

long long p, k;
long long ans;
int head[maxn], cnt;
struct node {
    long long from, to, next;
}edge[maxn];

long long qpow (long long a, long long b, long long m) {
    if (b == 0)
        return 1;
    long long ans = qpow (a, b>>1, m);
    ans = ans*ans%m;
    if (b&1)
        ans = ans*a%m;
    return ans;
}

void add_edge (int from, int to) {
    edge[cnt].from = from, edge[cnt].to = to, edge[cnt].next = head[from], head[from] = cnt++;
}

bool vis[maxn]; long long cur;

void dfs (int u) {
    cur++;
    vis[u] = 1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if (!vis[v]) {
            dfs (v);
        }
    }
    return ;
}

long long find_loop () {
    memset (vis, 0, sizeof vis);
    long long ans = 1;
    for (int i = 0; i < p; i++) {
        if (!vis[i]) {
            cur = 0;
            dfs (i); 
            if (qpow (k, cur, p) == 1)
                ans = ans*p%mod;
        }
    }
    return ans;
}

int main () {
    while (cin >> p >> k) {
        if (k == 0) {
            ans = qpow (p, p-1, mod);
        }
        else if (k == 1) {
            ans = qpow (p, p, mod);
        }
        else {
            memset (head, -1, sizeof head);
            cnt = 0;
            for (int i = 0; i < p; i++) {
                add_edge (k*i%p, i);
            }
            ans = find_loop ();
        }
        cout << ans << endl;
    }
    return 0;
}






Codeforces Round #603 (Div. 2) 题解 题目链接:https://codeforces.com/contest/1263 A - Sweet Problem 把最小值分摊给其他两个,然后剩下的再加上最小值 #include<bits/stdc++.h> #define x first #define y second #define inf 0x3f3f3f3f using namespace std; typ... 阅读详情

相关推荐

codeforces 156D Clues(prufer序列)

codeforces 156D Clues 题意 给定一个无向图,不保证联通。求添加最少的边使它联通的方案数。 题解 根据prufer序列,带标号无根树的方案数是\(n^{n-2}\) 依这个思想构建树即可。 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #defin...

weixin_30267691的博客 285

codeforces 603B Moodular Arithmetic

http://www.elijahqi.win/archives/2942 As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. D...

Elijahqi 339

Codeforces 603B - Moodular Arithmetic 数论 应用群的基本性质

题意 一类函数,他们的定义域是{0,1,..,P-1},值域也是这个,但不一定是一一映射 其中p是奇数,且是质数 就是给你p和k,问你满足,给定的等式的函数有多少个,k是等式中的参数 等式是f(k * x % p) = k * f(x) % p。 k的范围是0~p-1,p<10^6 思路 这题主要应用了群的性质,除去0的模p运算,是一个群 先不考虑0,让x和k都非0 我们先证明k * x % p的值

luke2834的专栏 615

Codeforces Round #334 (603B) Moodular Arithmetic [基础数学]

As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. —— 给出方程f(kx%p)=kf(x)%p ,问在集合A->B上不同的映射函数f 有几种,其中A=B={0,1,2..p-1},p为素数(除了2),k为小于p的一个常数

Asuna_acmer 1040

CodeForces Gym 101620简要题解

Assignment Algorithm模拟。#include <bits/stdc++.h> #define xx first #define yy second #define mp make_pair #define pb push_back #define mset(x, y) memset(x, y, sizeof x) #define mcpy(x, y) memcpy(x, y, si

wxh010910 1825

Codeforces Round #603 (Div. 2)

Codeforces Round #603 (Div. 2) A. Sweet Problem 题意:给三种糖果每种分别有r,g,b个,一天吃两种糖果分别吃一个,问能吃几天。 思路:每次吃数量最小的和最大糖果。r<=g<=b,如果r+g<=b则能吃r+g天,如果r+g>b能吃r+(g+b-r)/2天; #include<bits/stdc++.h> using ...

weixin_44836230的博客 179

codeforces #334 div1 B 603B Moodular Arithmetic(数论)

题目链接:codeforce 603B题目大意:给出f(kx mod p)≡kf(x) mod pf(kx\ mod\ p ) \equiv kf(x)\ mod \ p,求满足条件的f(x)的数量。题目分析:首先考虑两种特殊情况,即k=0和k=1的情况。 当k = 0 时, {f(x)=0f(x)={0,⋯p−1},x=0,x>0\left\{ \begin{aligned} &f(x) =

llin-黎辰 2380

codeforces 603A. Alternative Thinking(数学找规律 或者 dp)

题目链接:【A. Alternative Thinking】 输入一串长为n的01串,翻转其中的一段,所谓的翻转就是0变1,1变0,求最终最长01交替的序列长度 样例1:10000011   翻转后得到  10100011   最终的长度就是5  (红色部分) 方法一:数学找规律 原先相邻的变化后仍然相邻,所以我们可以先算出原本就相邻的最长是多少,一个子串翻转后,我们的要求是最长交替01串

Grush 1107

Codeforces Round #603 (Div. 2)ABCD题解 A数学 Bvis记录 C数学 D并查集

A 题意: 有三堆糖果分别为r,g,b。 每天只可以吃两个,(且这两个糖果的口味必须不同) 问: 最多吃多少天? 思路:(简单数学) 把三种颜色,按数量排序。a<b<c. 假如a+b<c那么只可能有(a+b)天。 假如a+b>c,那么就(a+b+c)/2 ...

qq_45377553的博客 160

思维找规律题--CodeForces - 603A

题目链接:https://vjudge.net/problem/CodeForces-603A 题目大意: 给你一个长为n的串,只包含0和1字符,求这个串的最长的交替子序列(不连续),例如010,1,1010等都是交替序列,1001,11等都不是。还可以对串的一个连续子串做一个操作,将子串中的0都反转为1,或着,将子串中的1都反转为0. 求这个串的最长的交替子序列。 解题思路:...

weixin_44757834的博客 220

Codeforces #603(div.2)C题,分块

题意:即问n/x(x取1到inf),有几种答案且这些答案分别是多少? 与hdu6555有联系,l = 1 ,r = n/(n/l)这个区间内n/x(x属于[l,r])答案是一样的,推下去就可以 hud6555 /* 分块例题 题意: 求∑(1,n) [n/i] 的奇偶性。 例如:n = 24 ;计算:24/1+24/2+24/3+....24/24; 可以用分块求得,第一组:24/1,第二组:2...

weixin_44083561的博客 319

CodeForces 603 B. Moodular Arithmetic(数论+并查集)

Description 给出一个整数k以及一个奇素数p,问满足f(kx%p)=k*f(x)%p的映射f:{0,1,…,p-1}->{0,1,…,p-1}的数量(结果模1e9+7) Input 两个整数k和p(3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) Output 输出满足条件的映射f的个数 Sample Input 3 2 Sample Output 3

ZSQ 790

CodeForces 603 C.Lieges of Legendre(博弈论)

Description 有nnn堆石子,第iii堆有aiaia_i个石子,每轮可以有两种操作,第一种是选一堆非空石子取走一个,第二种是对于一个有2x2x2x个石子的偶数堆,将其分成kkk个xxx个石子的堆,问谁必胜 Input 第一行两个整数n,kn,kn,k,之后输入nnn个整数ai(1≤n≤105,1≤k,ai≤109)ai(1≤n≤105,1≤k,ai≤109)a_i(1\le n\l...

ZSQ 565

codeforces 603E(LCT+优先队列)

我在比赛(noip模拟)时看到这题,一脸懵逼,上数学课想了很久也想不完整,总有地方实现不了。后来知道了某个dalao用LCT过的,然后和kscla吹B了半个晚上,就把这题吹出来了。 先讲LCT部分,朴素的LCT可以支持在有Link和Cut操作的森林上修改路径和维护路径信息,比如最大值。 那么就可以维护只有加边情况下的动态最小生成树。 (就是假若出现环,就把环上最大的一条边Cut掉)

KKiseki的博客 669

CF603 (Div. 2)题解

传送门:https://codeforces.com/contest/1263 A题 (经典的贪心) 题意: 给你三个糖,每天只能选其中两堆,各拿一个糖。问你最多能拿多少天. 思路: 选最大的两堆,然后将剩下一堆贪心的放入这两堆里。数据过大,可以直接公式算一下. B题 题意: 给你n个(n<=10)个四位数(可含前导0),问你最少做多少次操作使得这些值互异(对每个数最多只能改一个值) 思路:...

qq_35577488的博客 142

【索引】CodeForces各类比赛、杂题索引

CodeForces比赛、杂题索引 Part 1.CF比赛题解 Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2)ABCD题题解 Part 2.CF题目分类 1.DP CodeForces 8C Looking for Order CodeForces 10D LCIS CodeForces 11D A Simp...

IDDFS 1239

Codeforces Round #334 (Div. 1)

自从CF的rating改版以后终于回到DIV1了。打了一场还比预期好,如果C题不错的话应该可以继续上分,可惜了。 Codeforces 603A. Alternative Thinking 题目链接:http://codeforces.com/problemset/problem/603/A 题意:给你一个01串,要你选择其中一个非空的01子串,将其反转(0变成1,1变成0),要

Fry_guest的专栏 1217

codeforces div2_603 F. Economic Difficulties(树dfs预处理+dp)

题目连接:http://codeforces.com/contest/1263/problem/F 题意:有n个设备,上和下分别连接着一颗树,上下两棵树每棵树的叶子节点连接一个设备,两棵树的根节点都是1,1是源点可以发电供给叶结点连接的设备,现在问最多删除多少条边可以保证从根结点1发电后仍然可以使得所有设备都有电? 如上图删除红色的边(5条)仍然可以保证所有设备能供电 思路:...

qq_41949535的博客 295
上一篇: codefoeces 603C (博弈 SG函数找规律)
下一篇: codefoces 603A (数学 水~)
morejarphone
博客等级 码龄12年 54粉丝 462原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值