题目描述

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. 进阶系列(11)—— C#多线程

    一.多线程的相关概念 1.进程:是操作系统结构的基础:是一个正在执行的程序:计算机中正在运行的程序实例:可以分配给处理器并由处理器执行的一个实体:由单一顺序的执行显示,一个当前状态和一组相关的系统资源 ...

  2. Leetcode题库——34.在排序数组中国查找元素的第一个和最后一个位置

    @author: ZZQ @software: PyCharm @file: searchRange.py @time: 2018/11/12 19:19 要求:给定一个按照升序排列的整数数组 num ...

  3. 其实servlet就是一种mvc设计思想的一种实现

    看图,不说话

  4. Swift-KVC构造函数中数据类型和私有属性

  5. TCP系列55—拥塞控制—18、其他拥塞控制算法及相关内容概述

    前面我们演示分析了100+个wireshark TCP实例,拥塞控制部分也介绍常见的拥塞处理场景以及4种拥塞撤销机制,但是我们一直使用的都是reno拥塞控制算法.实际上拥塞控制发展到今天已经有了各种各 ...

  6. python读取文件解码失败

    python2.7 urllib2 抓取新浪乱码 中的: 报错的异常是 UnicodeDecodeError: 'gbk' codec can't decode bytes in position 2 ...

  7. [转帖]Kerberos简介

    1.  Kerberos简介 https://www.cnblogs.com/wukenaihe/p/3732141.html 1.1. 功能 一个安全认证协议 用tickets验证 避免本地保存密码 ...

  8. 笔记之分布式文件系统(DFS)

    不知何故,老外都挺喜欢使用DFS,但是国内公司用这个的不多.一个具体的需求就是,备份服务器在国外,所以启用DFS把国内的数据同步一份到国外进行备份.最近有机会接触DFS,把一些心得体会记录一下. 1. ...

  9. QTime的本质上是一个int,QDateTime本质上是一个qint64

    研究这个问题的起因发现使用<=比较时间的不准确,所以怀疑是一个浮点数(Delphi里的time就是一个浮点数).结果却发现是一个int class Q_CORE_EXPORT QTime { e ...

  10. iptables 开放端口

    #iptables -A INPUT -p tcp --dport 5000 -j ACCEPT #service iptables save