FZU-1759 Super A^B mod C---欧拉降幂&指数循环节

题目链接:

https://cn.vjudge.net/problem/FZU-1759

题目大意:

求A^B%C

解题思路:

注意,这里long long需要用%I64读入,不能用%lld

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn = 2e6 + 10;
 8 int euler_phi(int n)//求单个
 9 {
10     int m = (int)sqrt(n + 0.5);
11     int ans = n;
12     for(int i = 2; i <= m; i++)if(n % i == 0)
13     {
14         ans = ans / i * (i - 1);
15         while(n % i == 0)n /= i;
16     }
17     if(n > 1)ans = ans / n * (n - 1);
18     return ans;
19 }
20 ll mul(ll a, ll b, ll m)
21 //求a*b%m
22 {
23     ll ans = 0;
24     a %= m;
25     while(b)
26     {
27         if(b & 1)ans = (ans + a) % m;
28         b /= 2;
29         a = (a + a) % m;
30     }
31     return ans;
32 }
33 ll pow(ll a, ll b, ll m)
34 {
35     ll ans = 1;
36     a %= m;
37     while(b)
38     {
39         if(b & 1)ans = mul(a, ans, m);
40         b /= 2;
41         a = mul(a, a, m);
42     }
43     ans %= m;
44     return ans;
45 }
46 ll super_pow(ll a, char b[], ll c)
47 {
48     ll t = euler_phi(c), n = strlen(b), tot = 0;
49     for(int i = 0; i < n; i++)
50     {
51         tot = tot * 10 + b[i] - ‘0‘;
52         tot %= t;
53     }
54     tot += t;
55     return pow(a, tot, c);
56 }
57 char b[maxn];
58 int main()
59 {
60     ll a, c;
61     while(~scanf("%I64d%s%I64d", &a, b, &c))
62     {
63         printf("%I64d\n", super_pow(a, b, c));
64     }
65     return 0;
66 }

原文地址:https://www.cnblogs.com/fzl194/p/9074509.html

时间: 2024-09-30 19:27:46

FZU-1759 Super A^B mod C---欧拉降幂&指数循环节的相关文章

FZU - 1759 Super A^B mod C 降幂公式

知道降幂公式这题就很好办了 B>=Phi(c)的时候可以降幂然后快速幂计算,否则就直接快速幂计算. 这里的大数对小数取模直接利用取模性质按位取就行了. //A^B %C=A^( B%phi(C)+phi(C) ) %C #include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include<string> #include<cmath&g

CodeForces 906D Power Tower &lt;&lt;欧拉降幂

题意 给定n个数,q次询问,每次输出[l,r]区间的超级幂,对m取模. 思路 超级幂问题就想到用欧拉降幂来处理 欧拉降幂公式:$a^b \% m=a^{b\%\varphi (m)+\varphi(m)}\%m,(b>\varphi(m))$ 本题用递归处理欧拉降幂,在$logm$次降幂后$\varphi(m)=1$,然后回溯时用快速幂进行计算,总的复杂度大约是$log^{2}m$ $w_0^{w_1^{w_2^{w_3^{...}}}}\% m = w_0^{[w_1^{w_2^{w_3^{.

FZU 1759 题解 欧拉降幂

本题考点:欧拉降幂 Super A^B mod C Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separa

FZU1759(SummerTrainingDay04-B 欧拉降幂公式)

Problem 1759 Super A^B mod C Accept: 1056    Submit: 3444Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There

bzoj3884: 上帝与集合的正确用法 欧拉降幂公式

欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert/article/details/43955611 注:知道欧拉公式是远远不够的,还要知道欧拉降幂公式,因为当指数很大的时候需要用 然后欧拉降幂公式不要求A,C互质,但是B必须大于等于C的欧拉函数 吐槽:感觉记忆化搜索影响不大啊,当然肯定是因为太水了 这样复杂度是O(T*sqrt(p)*logp)

HDU4704(SummerTrainingDay04-A 欧拉降幂公式)

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3245    Accepted Submission(s): 1332 Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input fil

[数学][欧拉降幂定理]Exponial

题目描述 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 nu

Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\end{aligned} \] 思路 我们通过迭代发现\(f_n\)其实就是由\(c^{x_1},f_1^{x_2},f_2^{x_3},f_3^{x_4}\)相乘得到,因此我们可以分别用矩阵快速幂求出\(x_1,x_2,x_3,x_4\),最后用快速幂求得答案. 对\(f_1,f_2,f_3\): \[ \begin{aligned} (x_n&&

super_log (广义欧拉降幂)(2019南京网络赛)

题目: In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So