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 in practical application, we often assume α(n) \le 4α(n)≤4.

However O(α(n))O(α(n)) is greater than O(1)O(1), that means if nn is large enough, α(n)α(n) can greater than any constant value.

Now your task is let another slowly function log*log∗ xx reach a constant value bb. Here log*log∗ is iterated logarithm function, it means “the number of times the logarithm function iteratively applied on xx before the result is less than logarithm base aa”.

Formally, consider a iterated logarithm function log_{a}^*loga∗?

Find the minimum positive integer argument xx, let log_{a}^* (x) \ge bloga∗?(x)≥b. The answer may be very large, so just print the result xx after mod mm.

Input

The first line of the input is a single integer T(T\le 300)T(T≤300) indicating the number of test cases.

Each of the following lines contains 33 integers aa, bb and mm.

1 \le a \le 10000001≤a≤1000000

0 \le b \le 10000000≤b≤1000000

1 \le m \le 10000001≤m≤1000000

Note that if a==1, we consider the minimum number x is 1.

Output

For each test case, output xx mod mm in a single line.

Hint

In the 4-th4−th query, a=3a=3 and b=2b=2. Then log_{3}^* (27) = 1+ log_{3}^* (3) = 2 + log_{3}^* (1)=3+(-1)=2 \ge blog3∗?(27)=1+log3∗?(3)=2+log3∗?(1)=3+(−1)=2≥b, so the output is 2727 mod 16 = 1116=11.

样例输入复制

5
2 0 3
3 1 2
3 1 100
3 2 16
5 3 233

样例输出复制

1
1
3
11
223

解题报告:严重怀疑这场比赛的出题方,读题实在是太困难了,看了半个小时才理解了要求解什么东西,一看就是一个广义欧拉降幂,然后就直接去写的之前的模板,求解无穷个2的幂次,但是修修改改的过了样例,但是忘记了广义欧拉降幂的实现,疯狂wa,在比赛结束后找了好久才发现是没有去使用广义欧拉降幂的性质,忘记在该加欧拉(m)的地方进行加了,隔壁队的数论手,给了一个能够实现这个功能的快速幂,稍微一修改就A了。这个需要掌握了,当时时间太紧,头脑已经混了。

ac代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10
11 const int maxn=1e7+10;
12 vector<int> prime;
13 int phi[maxn];
14
15 void db_Onion()
16 {
17     phi[1]=1;
18     for(int i=2;i<maxn;i++)
19     {
20         if(phi[i]==0)
21         {
22             prime.push_back(i);
23             phi[i]=i-1;
24         }
25         for(int j=0;j<prime.size()&&prime[j]*i<maxn;j++)
26         {
27             if(i%prime[j]==0)
28             {
29                 phi[i*prime[j]]=phi[i]*prime[j];
30                 break;
31             }
32             else
33             {
34                 phi[i*prime[j]]=phi[i]*(prime[j]-1);
35             }
36         }
37     }
38 }
39 ll a,b,m;
40 ll cal(ll x,ll m)
41 {
42     if(x<m)
43         return x;
44     else
45         return x%m+m;
46 }
47
48 ll ans;
49
50 ll ksm(ll r,ll n,ll m)
51 {
52     ll d=r,ans=1;
53     bool f=0;
54     if(r==0&&n==0)return 1;
55     while(n){
56         if(n&1){
57             ans=ans*d;
58             if(ans>=m){
59                 ans%=m;f=1;
60             }
61         }
62         d=d*d;
63         if(n>1&&d>=m){
64             d%=m;f=1;
65         }
66         n>>=1;
67     }
68     if(f)ans+=m;
69     return ans;
70 }
71 ll slove(ll p,int tot)
72 {
73     if(tot==b+1)
74         return 1;
75     if(p==1)
76         return 1;
77
78     ll tmp=slove(phi[p],tot+1);
79     return ksm(a,tmp,p);
80 }
81
82 int main()
83 {
84     db_Onion();
85     int t;
86     scanf("%d",&t);
87     while(t--)
88     {
89         scanf("%lld%lld%lld",&a,&b,&m);
90         if(b==0||a==1)
91         {
92             printf("%lld\n",1%m);
93             continue;
94         }
95         ans=0;
96         printf("%lld\n",slove(m,1)%m);
97     }
98 }


原文地址:https://www.cnblogs.com/Spring-Onion/p/11444096.html

时间: 2024-08-30 13:17:19

super_log (广义欧拉降幂)(2019南京网络赛)的相关文章

[欧拉降幂][2019南京网络赛B]super_log

题意:输入a,b,p,求(底数a的右上角有b-1个a) 思路: 广义欧拉定理: AC代码: #include<bits/stdc++.h> typedef long long ll; using namespace std; ll a,b,m; ll vis[1000005]; ll prime[1000005],num=0; ll phi[1000005]; void getphi(ll n=1000000){ phi[1]=1; for(ll i=2;i<=n;i++){ if(!v

ACM-数论-广义欧拉降幂

https://www.cnblogs.com/31415926535x/p/11448002.html 曾今一时的懒,造就今日的泪 记得半年前去武大参加的省赛,当时的A题就是一个广义欧拉降幂的板子题,后来回来补了一下,因为没有交的地方,于是就测了数据就把代码扔了,,,然后,,昨天的南京网络赛就炸了,,,一样的广义欧拉降幂的板子题,,然后因为忘记了当初自己想出来的那中写法,,一直想着回想起之前的写法,,然后到结束都没弄出来,,,emmmm,, 赛后看了一下别人的解法,,别人的处理方法很巧妙,,当

2019ICPC网赛南京站B题 super_log(欧拉降幂

https://nanti.jisuanke.com/t/41299 题意:让算a^(a^(a^(...))),一共b个a, (mod p)的结果. 思路:这是个幂塔函数,用欧拉降幂公式递归求解. #include<bits/stdc++.h> #define ll long long using namespace std; map<int,int> euler; ll a,b,mod; int phi(int n) { int now=n; int ret=n; if(eule

广义欧拉降幂

欧拉降幂 \[f(x)=\left\{ {\begin{array}{}a^b\equiv a^{b \mod \phi(p) }(mod\ p,gcd(a,p)=1)\\a^b\equiv a^b(mod\ p,b<\phi(p))\\a^b\equiv a^{b\%\phi(p)+\phi(x)} (mod\ p,b\ge \phi(p))\end{array}} \right.\] 第一个公式主要根据欧拉定理: \[a^{\phi(p)}\equiv1(mod \ p,gcd(a,p)=1

The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

(施工中……) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出度为$0$的点只有$n$. 现在一个机器人从$1$出发,每天都会以相同的概率前往相邻节点之一或静止不动. 每天机器人消耗的耐久等于经过的天数. 求机器人到点$n$期望消耗的耐久. 划水划的很愉快,唯一一道做出来的题.但是和题解做法不同(感觉我的方法麻烦),因此砸了3h在这题上面(正在试图读懂题解ing). 设

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest C. Xyjj’s sequence(动态规划+欧拉降幂)

题目链接:https://nanti.jisuanke.com/t/40255 中文题面: 解题思路:先用欧拉降幂求出A,B两个序列,定义dp[0][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为B[j],同理dp[1][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为A[i],那么就很容易得到状态转移方程.那么最后答案即为max(dp[0][n][n],dp[1][n][n]).还有值得注意的是:该题需要使用滚动数组,不然会超内存. 在此贴两个关于欧拉降幂

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