【模板】龟速乘&快速乘&快速幂&压位高精快速幂

龟速乘

#define int long long
inline int mul_slow(int x,int y,int mod){
	int res=0;
	while(y){
		if(y&1) res=(res+x)%mod;
		x=(x+x)%mod;
		y>>=1;
	}
	return res%mod;
}

快速乘

#define ull unsigned long long
inline ull mul_quick(ull a,ull b,ull mod){
	//卡bug:long double>long long 
	ull d=(ull)((long double)(a/mod*b))*mod,x=a*b%mod;
	ull res=x-(d%mod);
	return res+(res<0?mod:0);
}

快速幂

#define int long long
inline int power(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1) res=mul_slow(res,x,mod); 
		x=mul_slow(x,x,mod); 
		y>>=1;
	}
	return res%mod;
}

压位高精快速幂

#include<bits/stdc++.h>
#pragma GCC optimize(2)//火车头
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
#define int long long
const int N=1e8,Q=8;//可自行更改压的位数 
static int st[35], top;
int n,p;
struct node{
    int a[66699]={0},l=1;
    void init(int y){a[0]=y;l=1;}
    inline void Put (int num, bool x) {
        top = 0;
        while (num) st[++top] = num % 10, num /= 10;
        if(x) for (int i = 1; i <= 8 - top; ++ i) putchar ('0');
        while (top) putchar (st[top] + '0'), --top;
        return;
    }
    void print(){
        Put(a[l-1], 0);
        for(int i=l-2;i>=0;--i)
            Put(a[i], 1);
        return;
    }
    node operator * (node x) const{
        node t=*this,id;
        id.l=t.l+x.l-1;
        for(int i=0;i<t.l;++i){
            for(int j=0;j<x.l;++j){
                id.a[i+j]+=t.a[i]*x.a[j];
                if(id.a[i+j]>=N){
                    id.a[i+j+1]+=id.a[i+j]/N;
                    id.a[i+j]%=N;
                }
            }
        }
        while(id.a[id.l]){
            id.a[id.l+1]+=id.a[id.l]/N;
            id.a[id.l++]%=N;
        }
        return id;
    }
}q1,p1;
inline void power(int r,int t){
    q1.init(1);p1.init(r);
    while(t){
        if(t&1) q1=q1*p1;
        p1=p1*p1;
        t>>=1;
    }
    return ;
}
static inline void write(__int128 x) {
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
//static inline __int128 po(int a, int b){
//	__int128 res = 1;
//	while (b){
//		for (int i = 1; i <= b % 10; i++)
//			res = (res * a);
//		__int128 x = a * a ;
//		__int128 y = x * x ;
//		__int128 z = y * y ;
//		a= z * x ;
//		b /= 10;
//	}
//	return res;
//}
signed main(){
    scanf("%lld%lld",&n,&p);
    power(n,p);
    q1.print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值