前言
快速幂专题其实很久以前就刷完了,但是还是再写一篇博客巩固一下记忆。
序列的第K个数
我们知道,等差数列的第n项公式是 S n = a 1 + ( n − 1 ) × q Sn=a1+(n-1)\times q Sn=a1+(n−1)×q,等比数列的第n项是 S n = a 1 ( 1 − q n ) / ( 1 − q ) Sn=a1 (1-q^n)/ (1-q) Sn=a1(1−qn)/(1−q)。
根据题意,我们可以判断这个数列属于等差数列还是等比数列,如果是等比数列就用快速幂来求。
#include <bits/stdc++.h>
using namespace std;
const int mod=200907;
typedef unsigned long long ll;//需要开longlong
ll qpow(ll a,ll b){//快速幂模板
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res%mod;
}
int n;
int main(){
cin>>n;
while(n--){
ll a,b,c,k;
cin>>a>>b>>c>>k;
if(c-b==b-a){//如果是等差数列
cout<<(a+(k-1)*(c-b))%mod<<endl;//等差数列公式
}
else cout<<a*qpow(b/a,k-1)%mod<<endl;//否则就是等比数列,用公式求一下就行了
}
return 0;
}
A的B次方
快速幂模板题。
#include <bits/stdc++.h>
using namespace std;
const int mod=200907;
typedef long long ll;//需要开longlong
ll qpow(ll a,ll b,ll p){//快速幂模板
ll res=1;
while(b){
if(b&1) res=(res*a)%p;
a=(a*a)%p;
b>>=1;
}
return res%p;
}
int n;
int main(){
ll a,b,m;
cin>>a>>b>>m;
cout<<qpow(a,b,m);//快速幂
return 0;
}
转圈游戏
#include <bits/stdc++.h>
using namespace std;
const int mod=200907;
typedef long long ll;//需要开longlong
ll qpow(ll a,ll b,ll p){//快速幂模板
ll res=1;
while(b){
if(b&1) res=(res*a)%p;
a=(a*a)%p;
b>>=1;
}
return res%p;
}
int main(){
ll n,m,k,x;
cin>>n>>m>>k>>x;
cout<<(x+qpow(10,k,n)*m)%n;///按照刚才的推导求即可
return 0;
}
越狱
#include <bits/stdc++.h>
using namespace std;
const int mod=100003;
typedef long long ll;//开longlong
ll qpow(ll a,ll b){//快速幂模板
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res%mod;
}
int main(){
ll m,n;
cin>>m>>n;
cout<<(qpow(m,n)%mod-m*qpow(m-1,n-1)%mod+mod)%mod;
}