题目链接  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. Crane UVA - 1611 思路+构造

    题目:题目链接 思路:思路+构造,假设 i  在pos 位置,那么如果 (pos-i-1)*2+i+1 <= n,那么可以操作一次换过来,如果他们之间元素个数是偶数,那么交换 i - pos,如 ...

  2. 记忆化搜索:POJ1088-滑雪(经典的记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑 ...

  3. Python 信号处理 signal 模块

    Table of Contents 1. signal模块简介 1.1. signal简单示例 1.2. signal说明 1.2.1. 基本的信号名 1.2.2. 常用信号处理函数 2. signa ...

  4. ECMAScript5.1

    http://lzw.me/pages/ecmascript/  ECMAScript5.1中文版 https://msdn.microsoft.com/zh-cn/library/dn656907. ...

  5. [问题解决]docker启动不了

    问题描述:昨天下午整合了同事的代码,发现docker启动好后,docker ps查看不到,docker ps -a发现docker容器没有启动. 尝试多次启动发现都是启动不了. 经过搜索发现 http ...

  6. ServletConfig和ServletContext 区别

      ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...

  7. 【转】Unity3D 入门小技巧——鼠标拾取并移动物体

    http://blog.csdn.net/sysujackjiao/article/details/69396274 一.鼠标拾取物体的原理 在Unity3D当中,想要在观察面(Aspect)中拾取物 ...

  8. NodeJS npm 包装包失败的解决方案

    这个也是网上搜的,亲自试过,非常好用! 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set reg ...

  9. Codeforces 1038F Wrap Around (Codeforces Round #508 (Div. 2) F) 题解

    写在前面 此题数据量居然才出到\(n=40\)???(黑人问号)以下给出一个串长\(|S|=100,n=10^9\)的题解. 题目描述 给一个长度不超过\(m\)的01串S,问有多少个长度不超过\(n ...

  10. 【SPOJ1297】Palindrome (SA+RMQ)

    求最长回文串.把原串翻转后,加在原串后面,中间插入一个辨别字符.然后求SA,Height.然后枚举每个字母作为回文串中心,分长度为奇数和偶数去讨论:奇数求 suffix(i)和suffix(n-i+1 ...