CodeForces - 226A Flying Saucer Segments 矩阵快速幂或等比数列求和
2017-08-18 18:51
1201 查看
http://codeforces.com/problemset/problem/226/A
题意:有①—②—③三个点如此连接。有n个数在位置③,要全部移动到位置①。数A可以从一个点移动到另一个点,当且仅当A大于这两个点的所有数。
题解:多写几个可以发现递推式:a
=3*a[n-1]+2。
法一:构造矩阵进行矩阵快速幂。
| a
1 | | 3 0 |
| 0 0 | | 2 1 |
法二:等比数列求和:a
+1=3*(a[n-1]+1),所以a
=3^n-1。
矩阵快速幂:
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<iostream>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 2;
LL mod;
struct node{
LL m
;
};
node Mul(node a,node b){
node c;
mem(c.m,0);
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
if(a.m[i][k]){
for(int j=0;j<N;j++){
c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%mod+mod)%mod;
}
}
}
}
return c;
}
node quickmod(node a,LL b){
node res;
mem(res.m,0);
for(int i=0;i<N;i++){
res.m[i][i]=1;
}
while(b){
if(b&1){
res=Mul(res,a);
}
a=Mul(a,a);
b>>=1;
}
return res;
}
int main(){
LL n;node a,b,ans;
cin>>n>>mod;
mem(a.m,0),mem(b.m,0);
b.m[0][0]=0,b.m[0][1]=1;
a.m[0][0]=3,a.m[1][0]=2,a.m[1][1]=1;
a=quickmod(a,n);
ans=Mul(b,a);
cout<<ans.m[0][0]<<endl;
return 0;
}
等比数列:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100000 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
LL quickMod(LL a,LL b,LL m){
LL ans=1;
a%=m;
while(b){
if(b&1)
ans=(ans*a)%m;
a=(a*a)%m;
b>>=1;
}
return ans;
}
int main(){
LL n,m,ans;
cin>>n>>m;
ans=quickMod(3,n,m)-1;
ans=(ans+m)%m;
cout<<ans<<endl;
return 0;
}
题意:有①—②—③三个点如此连接。有n个数在位置③,要全部移动到位置①。数A可以从一个点移动到另一个点,当且仅当A大于这两个点的所有数。
题解:多写几个可以发现递推式:a
=3*a[n-1]+2。
法一:构造矩阵进行矩阵快速幂。
| a
1 | | 3 0 |
| 0 0 | | 2 1 |
法二:等比数列求和:a
+1=3*(a[n-1]+1),所以a
=3^n-1。
矩阵快速幂:
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<iostream>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 2;
LL mod;
struct node{
LL m
;
};
node Mul(node a,node b){
node c;
mem(c.m,0);
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
if(a.m[i][k]){
for(int j=0;j<N;j++){
c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%mod+mod)%mod;
}
}
}
}
return c;
}
node quickmod(node a,LL b){
node res;
mem(res.m,0);
for(int i=0;i<N;i++){
res.m[i][i]=1;
}
while(b){
if(b&1){
res=Mul(res,a);
}
a=Mul(a,a);
b>>=1;
}
return res;
}
int main(){
LL n;node a,b,ans;
cin>>n>>mod;
mem(a.m,0),mem(b.m,0);
b.m[0][0]=0,b.m[0][1]=1;
a.m[0][0]=3,a.m[1][0]=2,a.m[1][1]=1;
a=quickmod(a,n);
ans=Mul(b,a);
cout<<ans.m[0][0]<<endl;
return 0;
}
等比数列:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100000 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
LL quickMod(LL a,LL b,LL m){
LL ans=1;
a%=m;
while(b){
if(b&1)
ans=(ans*a)%m;
a=(a*a)%m;
b>>=1;
}
return ans;
}
int main(){
LL n,m,ans;
cin>>n>>m;
ans=quickMod(3,n,m)-1;
ans=(ans+m)%m;
cout<<ans<<endl;
return 0;
}
相关文章推荐
- POJ 3233 Matrix Power Series (矩阵&快速等比数列求和取模)
- hdu 1588 Gauss Fibonacci(斐波那契,等比数列求和,矩阵快速幂)
- 冒泡水 矩阵快速幂和矩阵等比数列求和
- hdoj 1588 矩阵快速幂 二分等比数列求和模板
- hdu 奥运(矩阵快速幂+等比数列快速求和+数论)
- HDU1588-Gauss Fibonacci(矩阵快速幂+等比数列二分求和)
- POJ 3233 Matrix Power Series (矩阵快速幂+等比数列二分求和)
- HDU1588 Gauss Fibonacci (矩阵快速幂+等比数列二分求和)
- POJ 3233 Matrix Power (矩阵快速幂+等比数列求和)
- codeforces-327C Magic Five(等比数列求和+快速幂+逆元)
- codeforces 185A Plant(矩阵快速幂)
- 矩阵快速幂 CodeForces - 582B Once Again...
- 【codeforces 691 E】【矩阵快速幂 思维题】【给定序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问满足条件的序列个数】
- POJ 3233 Matrix Power Series (矩阵乘法+快速幂+等比二分求和) -
- 【矩阵二维或三维dp】最大子矩阵,子矩阵快速求和(用到最大直方图)
- CodeForces 185A. Plant(矩阵快速幂) 构造
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- Codeforces 514E Darth Vader and Tree DP + 矩阵快速幂
- Codeforces 691E Xor-sequences【矩阵快速幂,好题】
- POJ 3233 矩阵运算,等比数列二分求和,矩阵