题目描述

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. java中static使用之静态方法注意点

    1.静态方法可以直接调用同类中的静态成员,但是不能直接调用非静态成员,这是为什么呢?大家想一下,静态成员在对象创建之前就要写入内存,所以它在内存中是实实在在的存在的,而非静态还不存在内存中,所以不能调 ...

  2. 树莓派配置RTC时钟(DS3231,I2C接口)

    1.购买基于DS3231的RTC时钟模块,并且支持3.3V的那种 2.配置树莓派 a.打开树莓派的i2c接口 sudo raspi-config -->Interfacing Options - ...

  3. angularJS1笔记-(5)-过滤器练习

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. c# 判断两条线段是否相交(判断地图多边形是否相交)

    private void button1_Click(object sender, EventArgs e) { //var result = intersect3(point1, point2, p ...

  5. Activity设置背景透明之开发坑

    Activity设置背景透明的常规方法 方法一.在Manifest.xml中,直接在需要设置的Activity中添加主题样式: Android:theme="@android:style/T ...

  6. 区别mouseover与mouseenter?

    区别mouseover与mouseenter? * mouseover: 在移入子元素时也会触发, 对应mouseout,进入子元素的时候,父元素显示离开状态 * mouseenter: 只在移入当前 ...

  7. Internet History, Technology and Security (Week 8)

    Week 8 Security: Encrypting and Signing This week we start two weeks of Internet Security. It is a l ...

  8. (一)Jmeter 简单的录制

    JMeter介绍 1.JMeter,一个100%的纯Java桌面应用,它是Apache组织的开放源代码项目,它是功能和性能测试的工具.JMeter可以用于测试静态或者动态资源的性能(文件.Servle ...

  9. 对一致性Hash算法及java实现(转)

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  10. 软盘相关知识和通过BIOS中断访问软盘

    一. 软盘基础知识介绍 (1) 3.5英寸软盘 3.5英寸软盘分为上下两面,每面有80个磁道,每个磁道又分为18个扇区,每个扇区大小为512个字节. 软盘大小计算: 2面 * 80磁道 * 18扇区 ...