题目链接  Tetration

题意  给定一个排列  现在可以任意调整这个排列的顺序

   求$a_{1}^{a_{2}^{a_{3}^{...^{a_{n}}}}}$对$p$取模的最小值

直接枚举$a$的每一个排列,然后计算取最小值即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 10; int n;
int T;
int f[N];
LL a[N], c[N];
LL m;
LL ans;
map <LL, LL> mp; LL phi(LL n){
if (mp.count(n)) return mp[n];
LL ans = n, z = n;
for (LL i = 2; i * i <= n; ++i){
if (n % i == 0){
ans -= ans / i;
while (n % i == 0) n /= i;
}
} if (n > 1) ans -= ans / n;
return mp[z] = ans;
} LL Pow(LL a, LL b, LL mod){
LL ret = 1;
LL fl = a >= mod;
for (; b; b >>= 1){
if (b & 1){
ret *= a;
if (ret >= mod) fl = 1, ret %= mod;
} a *= a;
if (a >= mod) a %= mod, fl = 1;
} return ret + fl * mod;
} LL solve(int l, int r, LL mod){
if (l == r) return c[l];
if (mod == 1) return 1;
return Pow(c[l], solve(l + 1, r, phi(mod)), mod);
} int main(){ scanf("%d", &T);
while (T--){
scanf("%d%lld", &n, &m);
ans = 1e18;
rep(i, 1, n) scanf("%lld", a + i);
rep(i, 1, n) f[i] = i;
do{
rep(i, 1, n) c[i] = a[f[i]];
ans = min(ans, solve(1, n, m) % m);
}
while (next_permutation(f + 1, f + n + 1));
printf("%lld\n", ans);
} return 0;
}

  

玲珑杯 Round #5 Problem E Tetration (枚举 + 欧拉公式)的更多相关文章

  1. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  2. 玲珑杯 Round 19 A simple math problem

    Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 DESCRIPTION You have a sequence anan, ...

  3. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  4. codeforces Round 286# problem A. Mr. Kitayuta's Gift

    Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked ...

  5. UVALive 7457 Discrete Logarithm Problem (暴力枚举)

    Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...

  6. Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心

    After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...

  7. HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  8. 【玲珑杯 round#18 A】计算几何你瞎暴力

    [Link]:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 [Description] [Solution] 因为每个点的(xi ...

  9. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

随机推荐

  1. Codeforces Round #460 (Div. 2)-D. Substring

    D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...

  2. Java 关键字volatile 与 synchronized 作用与区别

     1,volatile 它所修饰的变量不保留拷贝,直接访问主内存中的.    在Java内存模型中,有main memory,每个线程也有自己的memory (例如寄存器).为了性能,一个线程会在自己 ...

  3. Android Kotlin适用小函数

    都是一些Android适用的Kotlin小函数. 1.点击空白隐藏键盘 //点击空白隐藏键盘 override fun onTouchEvent(event: MotionEvent): Boolea ...

  4. 【ELK】ELK安装与配置

    一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...

  5. OpenResty入门

    写一个小例子--输出随机字符串 编写nginx配置文件 location /random { content_by_lua_file /usr/local/openresty/nginx/conf/l ...

  6. Python面向对象之什么是类(1)

    1.C#.Java :只能用面向对象编程 Ruby.Python :函数编程+ 面向对象 面向对象编程不是在所有地方都比函数式编程方便的,类是为了封装,下面是简单的使用方法 在创建类的时候要用clas ...

  7. JS判断是否是IE浏览器的几种方式

    1.得到浏览器的信息,返回由客户机发送服务器的 user-agent 头部的值. if(navigator.userAgent.indexOf("MSIE 8.0")>0){ ...

  8. nyoj 题目7 街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...

  9. 解决报错DEFAULT_INCOMPATIBLE_IMPROVEMENTS

    freemarker整合springMVC报错如下:警告: Exception encountered during context initialization - cancelling refre ...

  10. P1463 [HAOI2007]反素数

    题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...