upc 3026 Exponial

本文探讨了如何通过欧拉降幂算法高效计算Exponial函数的值,并提供了一个具体的C++代码实现,该算法能有效解决大规模数值运算的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Exponial

时间限制: 1 Sec  内存限制: 64 MB
提交: 229  解决: 54
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述

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 numbers known to humankind, for instance:

In this problem we look at their lesser-known love-child the exponial , which is an operation defined for all positive integers n as

For example, exponial(1) = 1 and  which is already pretty big. Note that exponentiation is right-associative:  .
Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).

 

输入
The input consists of two integers n (1 ≤ n ≤ 10 9 ) and m (1 ≤ m ≤ 10 9 ).

 

输出
Output a single integer, the value of exponial(n) mod m.

 

样例输入
2 42

 

样例输出

2

题意

给一个N,M,求模M的结果。

分析

欧拉降幂的经典例题

欧拉降幂公式:

写递归求答案就可以了。

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
long long int mod;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b,ll mod){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
//inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );

ll a[]={0,1,2,9,(1<<18)},n;
ll phi(ll n)
{
    ll ans=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 ans;
}


ll f(ll n,ll m)
{
    if(m==1) return 1;
    if(n<=4)
    {
        if(a[n]>=m) return a[n]%m+m;
        return a[n];
    }
    ll exp=f(n-1,phi(m));
    return qpow(n,exp,m)+m;
}
int main()
{

    scanf("%lld%lld",&n,&mod);
    ll exp=f(n-1,phi(mod));
    ll ans=qpow(n,exp,mod)%mod;
    printf("%lld\n",ans%mod);

    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Kissheart/p/9748676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值