1.模板
求n的m次方
#include<iostream> #define MOD 1000000007 using namespace std; //递归快速幂 int quickpow(long long a,long long n){ if(n==0) return 1; else if(n%2==1){ return quickpow(a,n-1)*a%MOD; }else{ int t=quickpow(a,n/2)%MOD; return t*t%MOD; } } int Uquickpow(int a,int n){ int ans=1; while(n){ if(n&1){ //n最后一位等于1 ans=ans*a; } a=a*a; n>>=1;//n右移一位 } return ans; } int main() { cout<<Uquickpow(7,5); cout<<quickpow(7,5); }
2.题目
洛古p1045
#include<bits/stdc++.h> const long long mod=10000000000; using namespace std; const int N=1e5+10; int l=1,la=1; int ans[N]= {},a[N]= {},c[N]={}; using namespace std; void cheng1(){//ans*a memset(c,0,sizeof(c));//临时数组c for (int i = 1; i <= l; ++i) { for (int j = 1; j <= la; ++j) { c[i+j-1] += ans[i] * a[j]; c[i+j] += ( c[i+j-1] ) / 10; c[i+j-1] %= 10; } } int lc = l + la; while( c[lc] == 0 ) -- lc; for(int i = 1;i <= lc; ++i){ ans[i] = c[i]; } //l=lc if(lc>500){ l=500; }else{ l=lc; } } void cheng2(){//a*a memset(c,0,sizeof(c));//临时数组c for (int i = 1; i <= la; ++i) { for (int j = 1; j <= la; ++j) { c[i+j-1] += a[i] * a[j]; c[i+j] += ( c[i+j-1] ) / 10; c[i+j-1] %= 10; } } int lc = la + la; while( c[lc] == 0 ) -- lc; for(int i = 1;i <= lc; ++i){ a[i] = c[i]; } // la=lc; if(lc>500){ la=500; }else{ la=lc; } } void qpow(int P){ int ans=1; while(P){ if(P&1){ cheng1();//ans=ans*a } cheng2();//a=a*a P>>=1; } } int main() { int P; cin>>P; memset( a, 0, sizeof(a)); memset( ans, 0, sizeof(ans)); ans[1]=1; a[1]=2; qpow(P); // cout<<l<<endl; cout<<int((P*log10(2.0)+1))<<endl; ans[1]--; int num=0; for(int i=500;i>=1;i--){ cout<<ans[i]; num++; if(num==50){ cout<<endl; num=0; } } }View Code