Description

Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instance:

  • Exponentiation: 422016=42⋅42⋅...⋅422016 times422016=42⋅42⋅...⋅42⏟2016 times.
  • Factorials: 2016!=2016 ⋅ 2015 ⋅ ... ⋅ 2 ⋅ 1.

In this problem we look at their lesser-known love-child the exponial, which is an operation defined for all positive integers n​ as 
exponial(n)=n(n − 1)(n − 2)⋯21
For example, exponial(1)=1 and exponial(5)=54321 ≈ 6.206 ⋅ 10183230 which is already pretty big. Note that exponentiation is right-associative: abc = a(bc).

Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computesexponial(n) mod m (the remainder of exponial(n) when dividing by m).

Input

There will be several test cases. For the each case, the input consists of two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 109).

Output

Output a single integer, the value of exponial(n) mod m.

Sample Input

2 42
5 123456789
94 265

Sample Output

2
16317634
39

题解:题意很好理解;直接说题。这是利用欧拉函数降幂公式求的,标准模版(记得开 long long);看代码()

AC代码为:(我下面有一篇讲下欧拉函数降幂公式)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long LL;
LL N,M;

LL eular(LL m)
{
LL res=m,a=m;
for(LL i=2;i*i<=a;i++)
{
if(a%i==0)
{
res=res/i*(i-1);
while(a%i==0)
a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}

LL Fast_mod(LL x,LL n,LL m)
{
LL res=1;
while(n>0)
{
if(n & 1) res=(res*x)%m;
x=(x*x)%m;
n/=2;
}
return res;
}

LL work(LL n,LL m)
{
LL ans;
if(m==1) return 0;
else if(n==1) return 1;
else if(n==2) return 2%m;
else if(n==3) return 9%m;
else if(n==4) return Fast_mod(4,9,m);
else
{
LL phi=eular(m);
LL z=work(n-1,phi);
ans=Fast_mod(n,phi+z,m);
}
return ans;
}

int main()
{
cin>>N>>M;
cout<<work(N,M)<<endl;
return 0;
}

Exponial的更多相关文章

  1. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  2. Exponial~(欧拉函数)~(发呆题)

    Description Everybody loves big numbers (if you do not, you might want to stop reading at this point ...

  3. [数学][欧拉降幂定理]Exponial

    Exponial 题目 http://exam.upc.edu.cn/problem.php?cid=1512&pid=4 欧拉降幂定理:当b>phi(p)时,有a^b%p = a^(b ...

  4. Urozero Autumn 2016. NCPC 2016

    A. Artwork 倒过来并查集维护即可. #include<cstdio> #include<algorithm> using namespace std; const i ...

  5. NCPC 2016:简单题解

    A .Artwork pro:给定N*M的白色格子,然后Q次黑棒,输出每次加黑棒后白色连通块的数量.(N,M<1e3, Q<1e4) sol:倒着离线做,并查集即可. (在线做法:http ...

  6. NCPC2016-E- Exponial

    题目描述 Illustration of exponial(3) (not to scale), Picture by C.M. de Talleyrand-Périgord via Wikimedi ...

  7. Nordic Collegiate Programming Contest (NCPC) 2016

    A Artwork B Bless You Autocorrect! C Card Hand Sorting D Daydreaming Stockbroker 贪心,低买高卖,不要爆int. #in ...

  8. ACM-数论-广义欧拉降幂

    https://www.cnblogs.com/31415926535x/p/11447033.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题 ...

随机推荐

  1. python中字符串的常见操作(一)

    如有字符串: str1 = '192.168.1.1' str2 = 'asdfghjk' str3 = 'Asd fg hj ki' str4 = ' ' str5 = '' 以下是常见操作:# i ...

  2. Havok Physics 2012(2)

    目录 Havok Physics 2012 Chapter 2. Creating a Simulation 创建一个模拟世界 1. Creating Physics 2012 Objects Hav ...

  3. java多线程与线程并发五:多个线程访问共享对象和数据的方式

    本节的内容主要是对前面几节提到的线程间共享数据的方式做一个总结. 线程之间共享数据有以下几种方式: 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象. 2.如果每个线程执行的代码不同 ...

  4. BASH 编程之变量高级篇

    内部变量 • $$与$BASHPID都代表着执行程序的进程 ID,我们可以通过 echo 打印,并用 ps 指令检查得到相同的进程 ID [root@oracle ~]# echo $BASHPID ...

  5. Composer依赖管理 – PHP的利器

    别再到处搜PHP类扩展包了,对于现代语言而言,包管理器基本上是标配.Java 有 Maven,Python 有 pip,Ruby 有 gem,Nodejs 有 npm.PHP 的则是 PEAR,不过 ...

  6. Java流程控制之(四)中断

    目录 break continue return 标签 在程序设计时,循环直接的跳转显得十分重要,虽然Java没有提供goto语句去控制程序的跳转,但为了控制循环,Java提供了continue,br ...

  7. 2019-10-10:渗透测试,基础学习,mysql语法基础,笔记

    mysql常用命令mysql -u用户名 -p,登录方式也称为,二进制方式exit 退出mysql 查看mysql版本select version(); 查看mysql所有数据库show dateba ...

  8. python--BMI

    #bmi height,weight = eval(input("请输入身高(m) 体重(kg),以逗号隔开\n")) bmi = weight/pow(weight,2) pri ...

  9. C#Windows Forms 计算器--xdd

    一.计算器 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  10. RAM、ROM和fFLASH相关概念整理

    一:ROM ROM:Read Only Memory.只读存储器    是一种半导体内存,又叫做非挥发性内存.其特性是一旦数据被存储就无法再将之改变或删除.存储的数据不会因为电源关闭而消失.   二: ...