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. variable precision SWAR算法

    计算二进制形式中1的数量这种问题,在各种刷题网站上比较常见,以往都是选择最笨的遍历方法“蒙混”过关.在了解Redis的过程中接触到了variable precision SWAR算法(以下简称VP-S ...

  2. Conda/Miniconda/Anaconda 常用命令整理及介绍

    作者:HELO 出处:http://www.cnblogs.com/HELO-K 欢迎转载, 转载时请保留此声明, 谢谢! 在这里整理一份全一点的 Conda 常用命令, 方便大家日常使用时参考, 一 ...

  3. webpack3、4的基本的使用方法

    webpack的基本使用 webpack的安装 webpack的使用时需要借助 node 的环境的 在 node 中自动下载了 npm 这个包管理工具,之后的操作我们需要使用npm包管理工具进行相关操 ...

  4. python-语言元素

    变量命令 对于每个变量我们需要给它取一个名字.在python中,变量命名需要遵循一下这些必须遵守硬性规则和强烈建议遵守的非硬性规则. 硬性规则 变量名由字母(广义的Unicode字符,不包括特殊字符) ...

  5. 三种方法教你HTML实现点击某一个元素之外触发事件

    HTML实现点击某一个元素之外触发事件 大致编写的HTML界面渲染后是这个样子的,我们现在想要实现的需求是点击Button所在的div不会触发事件,而在点击Button所在的div之外的区域时会触发事 ...

  6. Install aws cli

    下载 https://s3.amazonaws.com/aws-cli/AWSCLI64PY3.msi 添加环境变量

  7. 【论文阅读】Clustering Convolutional Kernels to Compress Deep Neural Networks

    文章:Clustering Convolutional Kernels to Compress Deep Neural Networks 链接:http://openaccess.thecvf.com ...

  8. 在UEFI+GPT下使用rEFind实现Win10 + Kali2.0 双引导

    转载自:在UEFI+GPT下使用rEFind实现Win10 + Kali2.0 双引导 https://www.linuxidc.com/Linux/2016-07/133717.htm

  9. webpackd学习的意义

    高速发展的前端技术,与浏览器支持的不相匹配.导致前端必须把前端比较先进的技术进行一层编码从而使得浏览器可以加载. 比如前端框架Vue,Angular,React.Less,Sass.TypeScrip ...

  10. linux磁盘分区三步走

    为了便于理解硬盘的物理结构 ,可将硬盘看作一个圆,它是坚硬金属材料制成的涂以磁性介质的盘片,不同容量硬盘的盘片数不等.每个盘有两面,都可记录信息.要了解硬盘的物理结构,需要弄懂磁道.扇区.柱面.簇等几 ...