题目描述

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导出引用jar包的文件

    安装Eclipse打包插件Fat Jar      方案一对于含有较多第三方jar文件或含有第三方图片资源等就显得不合适,太繁琐.这时可以使用一个打包的插件-Fat Jar.      Fat Jar ...

  2. Leetcode题库——8.字符串转为整数【##】

    @author: ZZQ @software: PyCharm @file: myAtoi.py @time: 2018/9/20 20:54 要求:实现 atoi,将字符串转为整数. 1)根据需要丢 ...

  3. Hibernate利用纯sql

    String hql = "select * from shop where shop.strid in(select strid from moneythreeshop where mon ...

  4. Aspose 插件

    百度:Aspose Aspose.Cells.dll Aspose.Slides.dll Aspose.Words.dll

  5. alphe4

    队名:massivehard 组员1:(组长:晓辉) 今天完成了哪些任务: 服务器基本架设完毕 明日计划: 服务器与客户端对接 用户手写记录功能的完善 还剩下哪些计划: 用户手写记录功能 服务器与客户 ...

  6. Alpha版本冲刺(一)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  7. 05_Java基础语法_第5天(方法)_讲义

    今日内容介绍 1.方法基础知识 2.方法高级内容 3.方法案例 01方法的概述 * A: 为什么要有方法 * 提高代码的复用性 * B: 什么是方法 * 完成特定功能的代码块. 02方法的定义格式 * ...

  8. angularJS1笔记-(9)-自定义指令(restrict/template/replace)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  9. Scrum团队 《构建之法》第6~7章

    Scrum团队成立 团队名称: 22# 团队目标:做好每次布置的任务 还有提升自己 团队口号:做好现在,展望未来 团队成员:陈楷淇,张裕发,陈泽展,彭一建 角色分配 产品负责人(决定开发内容和优先级排 ...

  10. Beta阶段——4

    一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 完善了用户管理模式的功能 (2) 今天计划完成的工作: 对用户功能的添加. (3) ...