D. Moodular Arithmetic

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/604/problem/D

Description

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 range 0 top - 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 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that pis an odd prime number.

Output

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

Sample Input

3 2

Sample Output

3

HINT

题意

给你k,p

然后让你构造映射 f(k*x %p) = k*f(x)%p

然后问你一共有多少种映射满足这个条件

题解:

由于gcd(k,p)==1,那么很显然k*x%p = z,这个等式中,x属于(0,p-1),z属于(0,p-1),那么的话,一定是一一对应的

那么我们就可以找环了,如果其中y = k*x%p中和其他的构成了一个环,那么这个环中只要确定了一个数,那么这个环中就能够全部确认

所以答案就和环的个数有关了~

再特判k = 1和k = 0的情况

代码:

#include<iostream>
#include<stdio.h>
using namespace std; const long long mod = 1e9+; #define maxn 1000005
long long quickpow(long long m,long long n)
{
long long b = ;
while (n > )
{
if (n & )
b = (b*m)%mod;
n = n >> ;
m = (m*m)%mod;
}
return b;
} long long a[maxn];
int vis[maxn];
long long p,k;
void dfs(long long x)
{
if(vis[x])return;
vis[x]=;
dfs(k*x%p);
}
int main()
{ scanf("%lld%lld",&p,&k);
if(k==)
{
printf("%lld\n",quickpow(p,p-));
return ;
}
if(k==)
{
printf("%lld\n",quickpow(p,p));
return ;
}
long long ans = ;
for(int i=;i<p;i++)
{
if(vis[i])continue;
dfs(i);
ans++;
}
printf("%lld\n",quickpow(p,ans));
}

Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数的更多相关文章

  1. Codeforces Round #334 (Div. 1) B. Moodular Arithmetic

    B - Moodular Arithmetic 题目大意:题意:告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f( ...

  2. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  3. Codeforces Round #334 (Div. 2)

    水 A - Uncowed Forces #include <bits/stdc++.h> using namespace std; typedef long long ll; const ...

  4. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  5. Codeforces Round #334 (Div. 2) B. More Cowbell 二分

    B. More Cowbell Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/probl ...

  6. Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression

    http://codeforces.com/contest/978/problem/D 题目大意: 给你一个长度为n的b(i)数组,你有如下操作: 对数组中的某个元素+1,+0,-1.并且这个元素只能 ...

  7. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  8. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  9. Codeforces Round #334 (Div. 1) C. Lieges of Legendre

    Lieges of Legendre 题意:有n堆牛,每堆有ai头牛.两个人玩一个游戏,游戏规则为: <1>从任意一个非空的堆中移走一头牛: <2>将偶数堆2*x变成k堆,每堆 ...

随机推荐

  1. 安装--SambaServce

    参考地址:快跑蚂蚁的linux之旅--redhat安装配置samba实验win共享linux主机目录 1.使用rpm -qa|grep "samba",查看samba安装包是否安装 ...

  2. Andorid-如何为你的Android应用缩放图片

    很难为你的应用程序得到正确的图像缩放吗?是你的图片过大,造成内存问题?还是图片不正确缩放造成不良用户体验的结果?为了寻求一个好的解决方案,我们咨询了Andreas Agvard(索尼爱立信软件部门), ...

  3. TabHost Tab的添加和删除

    TabHost 添加Tab项: tabhost = this.getTabHost(); TabSpec tabSpec = tabhost.newTabSpec("news"); ...

  4. SQL注入中利用XP_cmdshell提权的用法(转)

    先来介绍一下子服务器的基本情况,windows 2000 adv server 中文版,据称 打过了sp3,asp+iis+mssql .首先扫描了一下子端口,呵呵,开始的一般步骤. 端口21开放: ...

  5. Delphi RICHEDIT中插入图象

    unit InsRich;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. 第一个简单的android项目

    开发平台:windows7+Eclipse+andriod SDK(24.0)+ADT(23.0.4).这个环境的搭建在前一篇文章(Mobile testing下的appium测试)里面已经描述了. ...

  7. [LeetCode] Single Number III ( a New Questions Added today)

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  8. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  9. 【暑假】[基本数据结构]根据BFS与DFS确定树

    UVa10410 Tree Reconstruction 算法:根据BFS构造pos数组以区分关系,在此基础上对DFS序列操作.注:栈中存父结点,栈顶是最优先的父结点. 代码如下: #include& ...

  10. java易混淆概念之类变量、成员变量、局部变量

      类变量.成员变量.局部变量 类变量(也叫静态变量)是类中独立于方法之外的变量,用static 修饰.(static表示“全局的”.“静态的”,用来修饰成员变量和成员方法,或静态代码块(静态代码块独 ...