UVa 10692 Huge Mods

方法:数论

其实不是很明白,为什么这个公式可行

a^b % m = a^(b%phi[m] + phi[m]) % m

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72 int phi(int n)
 73 {
 74     int ret = n;
 75     for (int i = 2; i * i <= n; ++i)
 76     {
 77         if (n % i) continue;
 78         ret -= ret/i;
 79         while (n % i == 0) n /= i;
 80     }
 81     if (n != 1)
 82         ret -= ret/n;
 83     return ret;
 84 }
 85 int m, n;
 86 int a[10];
 87 ll pow_mod(ll base, ll p, ll mod)
 88 {
 89     if (!base) return 0;
 90     ll ret = 1;
 91     while (p)
 92     {
 93         if (p & 1) ret = ret * base % mod;
 94         p >>= 1;
 95         base = base * base % mod;
 96     }
 97     return ret;
 98 }
 99 int dfs(int cur, int mod)
100 {
101     if (cur == n-1) return a[cur] % mod;
102     int ph = phi(mod);
103     return pow_mod(a[cur], dfs(cur+1, ph)%ph + ph, mod);
104 }
105 int main()
106 {
107     int kase = 0;
108     while (cin >> m >> n)
109     {
110         rep(i, n) cin >> a[i];
111         cout << "Case #" << ++kase << ": " << dfs(0, m) << newline;
112     }
113 }
114 /*
115  10 4 2 3 4 5
116  100 2 5 2
117  53 3 2 3 2
118  #
119 */

时间: 2024-12-20 12:52:45

UVa 10692 Huge Mods的相关文章

UVA 10692 - Huge Mods(数论)

UVA 10692 - Huge Mods 题目链接 题意:求a0a1a2...mod m 思路:直接算肯定不行,利用欧拉定理ab=a(b mod phi(m) + phi(m))(b>=phi(m)),对指数进行降值处理,然后就可以利用快速幂去计算了,计算过程利用递归求解. 代码: #include <stdio.h> #include <string.h> const int N = 1005; int phi[N * 10], vis[N * 10], m, n, a[

uva 10692 Huge Mods 超大数取模

vjudge上题目链接:Huge Mods 附上截图: 题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B > phi(C) ) 呢?后来发现确实需要用到,而且因为它有很多重指数,所以需要 dfs,深搜到最后一层后才返回,每次向上一层返回用求幂公式处理好的指数,然后本层用同样的原理去处理好当前层取模的值,并向上一层返回.欧拉函数预处理即可,这题的结束也有点卡人,我是用输入挂来处理的. 1 #include

UVA - 10692 Huge Mods (欧拉函数)

Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity for ex

UVA 10692 Huge Mods(指数循环节)

指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断 //by:Gavin http://www.cnblogs.com/IMGavin/ //指数循环节 递归处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include&l

UVA 10692 Huge Mod

Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity for ex

Uva 10629 Huge Mods (指数循环节)

题意:求 a1?a2?a3?. . .?aN mod m 思路:利用 和递归求解 代码: #include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N=15; typedef long long ll; int MOD; int A[N],k; int phi(int n) { int rea = n; for(int i=2; i*i<=n;

E - Huge Mods (UVA - 10692)

- 题目大意 如题目中所说一样,求计算a1^a2^a3^a4......^an模m的值. - 解题思路 利用欧拉降幂公式,和欧拉函数即可解决. - 代码 #include<iostream> #include<cstring> using namespace std; long long b; long long num[10000]; long long powMod(long long a, long long n,long long p) { long long ans =

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

数论小结1.

从白书上面的习题开始版切....但是发现时间不够了.... 今天粗略的做了不少题目.....但是有些题目感觉实在是比较诡异.......感觉考的话也不太可能...不过还是写下来吧. 1.Uva 10673 已知x, k, 求 x = p * floor(x / k) + q * ceil(x / k).的解. 思路: 其实分类讨论就 ok 了. 当 x % k == 0 时, 取 p = x * k, q = 0; 否则, floor 与 ceil 相差 1. 怎么做? 你懂得. 2. Uva