题目链接  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. BZOJ - 2744 朋友圈 (二分图上的最大团)

    [题目大意] 在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着.一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最大数目.两 ...

  2. HDU:1358-Period

    Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...

  3. 库函数的使用:sscanf的使用方法

    先贴代码,可以看懂代码的直接看代码: /***************************************************** ** Name : sscanf.c ** Auth ...

  4. 03017_ajax

    1.Ajax概述 (1)什么是同步,什么是异步? ①同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待卡死状态: ②异步现象:客户端发送请求到服务器端,无论服务器是否返回响应, ...

  5. sql server备份

    完全备份 declare @device varchar(255),@path varchar(255),@dbname varchar(255)set @dbname='MIS_TEMP'set @ ...

  6. Windows下Eclipse安装PyDev

    事后证明PyDev不好用,推荐使用pycharm!!!   1.安装eclipse,这个网上一大堆,就不说了 2.安装python,这个网上一大堆,就不说了 3.Eclipse安装PyDev 第一种在 ...

  7. Python-S9-Day128——算法基础Algorithm

    01 算法基本概念与递归: 02 二分查找与LOW B三人组 03 快速排序 04 归并排序 01 算法基本概念与递归: 1.1 算法概念 1.2 复习:递归 1.3 时间复杂度 1.4 空间复杂度 ...

  8. mojoportal中使用jquey的插件

    以前在mojo中使用jquery的插件,都是把插件的文件内容直接写到了相关的模块中,这样的问题是不整洁,一大串代码. 如果直接在layout.master中引入插件文件,或者在自定义模块中引入插件文件 ...

  9. 实战小项目之IMX6 VPU使用

    项目简介 基于官方的demo进行修改,限于能力问题,并没有将功能代码完全从官方的demo中分离出来,还是基于原来的框架进行修改,做了一些简单的封装,我做的工作如下: 使用自己的采集程序 定义6中工作模 ...

  10. activemq 简单聊天

    有兴趣加群qq:200634530