super_log(欧拉降幂)

题意:就是给你a,b,m求a^a^a^a^a....(b个a)%m的大小

题解:首先看欧拉函数:

1.欧拉函数:小于或等于n且与n互素的正整数个数,称为欧拉函数。代码中是phi()函数

2.若n的质因数为p1,p2,p3,,,,,pn,则欧拉函数phi(n)可以表示为phi(n)=n*(1-1/p1)*(1-1/p2)*(1-1/p3)*.....(1-1/pn).

3.如果p是一个素数,n是正整数,则\LARGE phi(p^n)=p^n-p^(n-1)

欧拉函数的实现:直接用公式phi(n)=n*(1-1/p1)*(1-1/p2)*(1-1/p3)*.....(1-1/pn)。
 代码如下

ll phi(ll m)//欧拉函数 
{
	ll res=m;
	ll n=m;
	for(int i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			res=res-res/i;
		}
		while(n%i==0) n=n/i;
	}
	if(n>1) res=res-res/n;
	return res;
} 

2、欧拉降幂

上述式子的第二个和第三个是广义的欧拉降幂,可以用于gcd(a,p)=1或者gcd(a,p)!=1;

对于本题的推到:

令f(p)=a^a^a^a........%m=a^(a^a^----%phi(m)+phi(m)) mod m = a^( f(phi(m)) + phi(m) ) mod m

所以来看这是一个递归(上述推导还没有考虑phi(m)和m的大小关系);

如果phi(m)<m f(p)=a^( f(phi(m))  ) %m;

否则  f(p)=a^( f(phi(m)) + phi(m) )%m;

 

 

sIn 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
 #include<stdio.h>
typedef long long ll;
ll gcd(ll a,ll b)
{
	return b?gcd(b,a%b):a;
}
ll ksm(ll a,ll b,ll mod)
{
	ll res=1;
	a=a%mod;
	while(b)
	{
		if(b&1) res=res*a%mod;
		b>>=1;
		a=a*a%mod;
	}
	return res%mod;
 } 
ll phi(ll m)//欧拉函数 
{
	ll res=m;
	ll n=m;
	for(int i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			res=res-res/i;
		}
		while(n%i==0) n=n/i;
	}
	if(n>1) res=res-res/n;
	return res;
} 
ll solve(ll a,ll b,ll m)//欧拉降幂 
{
	if(b==0) return 1;
	if(m==1) return 0;
	ll p=solve(a,b-1,phi(m));
	if(p<m&&p) return ksm(a,p,m);
	else return ksm(a,p+phi(m),m);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		ll a,b,m;
		scanf("%lld%lld%lld",&a,&b,&m);
		ll w=solve(a,b,m);
		printf("%lld\n",w%m);
	}
	return 0;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值