题目描述

Illustration of exponial(3) (not to scale), Picture by C.M. de Talleyrand-Périgord via Wikimedia Commons 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:

In this problem we look at their lesser-known love-child the exponial , which is an operation defined for all positive integers n as

For example, exponial(1) = 1 and  which is already pretty big. Note that exponentiation is right-associative:  .
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 computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).

输入

The input consists of two integers n (1 ≤ n ≤ 109 ) and m (1 ≤ m ≤ 109 ).

输出

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

样例输入

2 42

样例输出

2
a^b %c= a^(b%phi(c)+phi(c)) %c (b>=phi(c))
如果 phi(c)>b 直接 a^b%c 对这个题来说,当n>4可以直接用这个算了
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll fi(ll n)
{
ll ans=n;
for (int i=;i*i<=n;i++)
{
if (n%i==)
{
ans-=ans/i;
while (n%i==) n/=i;
}
}
if (n>) ans-=ans/n;
return ans;
} ll qpow(ll a, ll n, ll m) {
a%=m;
ll ret = ;
while(n)
{
if (n&) ret=ret*a%m;
a=a*a%m;
n>>=;
}
return ret;
}
ll f(ll n, ll m)
{
if (m==) return ;
if (n==) return ;
if (n==) return %m;
if (n==) return %m;
if (n==) return %m;
return qpow(n, f(n-, fi(m)) % fi(m) + fi(m), m);
}
int main()
{
ll n, m;
while(cin >> n >> m)
{
cout << f(n, m) << endl;
}
return ;
}
 

NCPC2016-E- 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

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

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

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

  5. Urozero Autumn 2016. NCPC 2016

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

  6. NCPC 2016:简单题解

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

  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字符串操作、文件操作,英文词频统计预处理

    1.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 解析身份证号:生日.性别.出生地等 def function3(): print('请输入身份证号') ...

  2. Eclipse+MySQL+Tomcat web开发安装配置

    转载文章: 链接:https://blog.csdn.net/bbyyz01/article/details/78142126 1.Eclipse 这里选择Eclipse集成开发环境. 可以直接在官网 ...

  3. grunt入门讲解2:如何使用 Gruntfile 配置任务

    Grunt的task配置都是在 Gruntfile 中的grunt.initConfig方法中指定的.此配置主要包括以任务名称命名的属性,和其他任意数据.一旦这些代表任意数据的属性与任务所需要的属性相 ...

  4. PAT 甲级 1143 Lowest Common Ancestor

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...

  5. shutdown&&isTerminated

    shutdownvoid shutdown()启动一次顺序关闭,执行以前提交的任务,但不接受新任务.若已经关闭,则调用没有其他作用.抛出:SecurityException - 如果安全管理器存在并且 ...

  6. Apache+Nginx+php共存(一)

    在实际开发中个人的电脑中经常需要安装 WNMRP.WAMRP.LNMRP.LAMRP等各种开发环境来应对不同的开发需求. 此篇主要是对WINDOWS系统下 Apache+Nginx + PHP +My ...

  7. [转帖]高通推出八核笔电处理器骁龙8cx 能超英特尔吗?

    高通推出八核笔电处理器骁龙8cx 能超英特尔吗? https://baijiahao.baidu.com/s?id=1619154699684981202&wfr=spider&for ...

  8. bzoj4385 & POJ2015 Wilcze doły

    Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. Input 第一 ...

  9. 【hdu4285】 circuits

    http://acm.hdu.edu.cn/showproblem.php?pid=4285 (题目链接) 题意 求不不能嵌套的回路个数为K的路径方案数. Solution 插头dp,时限卡得太紧了, ...

  10. python模块之 paramiko

    paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能.这是一个第三方的软件包,使用之前需要安装. 1 基于用户名和密码的 sshclient 方式登录 # 建立一个s ...