G02 高精度快速幂
视频链接:https://www.bilibili.com/video/BV1R14y1e7r7
Luogu P1045 [NOIP2003 普及组] 麦森数
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int N=500; typedef vector<int> VI; VI a(N),res(N); int p; VI mul(VI &a, VI &b){//高精度 VI t(N*2); for(int i=0; i<N; i++) for(int j=0; j<N; j++){ t[i+j] += a[i]*b[j]; t[i+j+1] += t[i+j]/10; t[i+j] %= 10; } return t; } void quickpow(int p){//快速幂 res[0]=1, a[0]=2; while(p){ if(p & 1) res = mul(res,a); a = mul(a,a); p >>= 1; } res[0]--; //个位修正 } int main(){ cin >> p; printf("%d\n",int(p*log10(2))+1); quickpow(p); for(int i=0, k=499; i<10; i++){ for(int j=0; j<50; j++, k--) printf("%d",res[k]); puts(""); } return 0; }
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int N=500; int a[N*2],res[N*2],t[N*2]; int p; void mul(int*a, int*b, int*c){//高精度 memset(t,0,sizeof(t)); for(int i=0; i<N; i++) for(int j=0; j<N; j++){ t[i+j] += a[i]*b[j]; t[i+j+1] += t[i+j]/10; t[i+j] %= 10; } memcpy(c,t,sizeof(t)); } void quickpow(int p){//快速幂 res[0]=1, a[0]=2; while(p){ if(p & 1) mul(res,a,res); mul(a,a,a); p >>= 1; } res[0]--; //个位修正 } int main(){ cin >> p; printf("%d\n",int(p*log10(2))+1); quickpow(p); for(int i=0, k=499; i<10; i++){ for(int j=0; j<50; j++, k--) printf("%d",res[k]); puts(""); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!