233 matrix
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,m in the 233 matrix?
Input
There are multiple test cases. Please process till EOF.
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
Output
For each case, output a n,m mod 10000007.
Sample Input
1 1
1
2 2
0 0
3 7
23 47 16
Sample Output
234
2799
72937
Hint
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int modn=1e7+7;
const int maxn=15;
#define ll long long
ll n,m;
struct node{
ll s[maxn][maxn];
node(){
memset(s,0,sizeof(s));
}
};
node multi(node a,node b){
node t;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
t.s[i][j]=(t.s[i][j]+a.s[i][k]*b.s[k][j]%modn+modn)%modn;
}
}
}
return t;
}
node quick(node a,ll nn){
node res;
for(int i=0;i<n;i++){
res.s[i][i]=1;
}
while(nn){
if(nn&1) res=multi(res,a);
a=multi(a,a);
nn>>=1;
}
return res;
}
int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
ll a[15]; node b;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
a[0]=23;
a[n+1]=3;
n++;
n++;
for(int i=0;i<n;i++){
if(i!=n-1) b.s[i][0]=10;
for(int j=1;j<=i;j++){
if(i!=n-1)
b.s[i][j]=1;
}
b.s[i][n-1]=1;
}
/* for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<b.s[i][j]<<" ";
}
cout<<endl;
}*/
node res=quick(b,m);
//b[n-1][n-1]=1;
ll ans=0;
/* for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<res.s[i][j]<<" h";
}
cout<<endl;
}*/
for(int i=0;i<n;i++){
ans=(a[i]*(res.s[n-2][i])%modn+ans)%modn;
// cout<<a[i]<<" "<<res.s[n-2][i]<<"wer"<<endl;
}
cout<<ans<<endl;
}
return 0;
}
这道题目,怎么说呢,递归的话,很有可能用矩阵,我直接找规律,又忘记了复杂度过不了了
还有细节问题,快速幂,稍微推一下就行了
jzzhu and sequences
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
Examples
Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006
Note
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int modn=1e9+7;
struct node{
ll m[2][2];
node(){
memset(m,0,sizeof(m));
}
};
node multi(node a,node b){
node res;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
res.m[i][j]=0;
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
res.m[i][j]=(ll)((ll)(a.m[i][k]*b.m[k][j]+modn)+res.m[i][j])%modn;
// cout<<a.m[i][k]<<" "<<b.m[k][j]<<" "<<res.m[i][j]<<" "<<i<<" "<<j<<"end"<<endl;
}
}
}
// cout<<res.m[0][0]<<" "<<res.m[0][1]<<" "<<res.m[1][0]<<"f "<<res.m[1][1]<<endl;
return res;
}
//node res;
node qui(node r,int n){
node res;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
res.m[i][j]=0;
}
for(int i=0;i<2;i++){
res.m[i][i]=1;
}
res.m[0][1]=0;
res.m[1][0]=0;
while(n){
// cout<<n<<"res"<<endl;
if(n&1)
res=multi(res,r);
// cout<<res.m[0][1]<<"ans"<<endl;
r=multi(r,r);
n>>=1;
}
return res;
}
//ll a[2][2];
int main(){
ll x,y,n;
while( scanf("%lld%lld%lld",&x,&y,&n)!=EOF)
{
node a;
node b;
b.m[0][0]=x;
b.m[0][1]=y;
a.m[0][1]=-1;
a.m[1][0]=1;
a.m[1][1]=1;
a.m[0][0]=0;
if(n==2){
cout<<(y%modn+modn)%modn<<endl;
return 0;
}
if(n==1){
cout<<(x%modn+modn)%modn<<endl;
return 0;
}
node res=qui(a,n-2);
//res=multi(b,res);
ll ans=(ll)((ll)(res.m[0][1]*x)%modn+(ll)(res.m[1][1]*y)%modn)%modn;
if(ans<0) ans+=modn;
if(ans<0) ans+=modn;
cout<<ans<<endl;
// cout<<(res.m[0][1]+modn)%modn<<endl;
}
return 0;
}
这道题目忘了用longlong了一直wa, 差点把我劝退了。
总之,细节问题,很扎心