标签:
hdu1757:
Description
Input
Output
Sample Input
10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
Sample Output
45 104
求f(k);
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10; int a[N],k,m; struct mat { int g[N][N]; mat operator*(mat a) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { for(int k = 0; k < 10; k++) { tmp.g[i][j] = (tmp.g[i][j] + this->g[i][k] * a.g[k][j]) % m; } } } return tmp; } }res,ori; void cul(int n) { while(n) { if(n & 1) res = res * ori; n >>= 1; ori = ori * ori; } printf("%d\n", res.g[0][0]); } int main() { while(scanf("%d%d",&k,&m) != EOF) { memset(res.g, 0, sizeof(res.g)); memset(ori.g, 0, sizeof(ori.g)); for(int i = 0; i < 10; i++) { scanf("%d",&ori.g[i][0]); res.g[0][i] = 9 - i; } for(int i = 0; i < 9; i++) { ori.g[i][i + 1] = 1; } cul(k - 9); } }
hdu1575
Description
Input
Output
Sample Input
2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9
Sample Output
2 2686
求矩阵A的k次方的对角线和(模板题)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct mat { int g[10][10]; }res,ori; int n,k; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 9973; } } } return tmp; } void cul(int k) { while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } int ans = 0; for(int i = 0; i < n; i++) { ans = (ans + res.g[i][i]) % 9973; } printf("%d\n",ans); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); memset(res.g, 0, sizeof(res.g)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { scanf("%d",&res.g[i][j]); } } ori = res; cul(k - 1); } }
Description
Input
Output
Sample Input
3 8 4 7 4 8
Sample Output
6 2 1
题意:一个序列只有f和m问长度为L的序列,不含fmf和fff的有几个;
思路:
结尾总共有四种
ff,fm,mf,ff
以ff结尾的再加一位,可以推出fm结尾,即ffm(不能推出ff,因为这样就有fff了)
以fm结尾能推出mm;以mm结尾能推出mm和mf;
以mm结尾的能推出mm和mf;
然后就能构造矩阵乘:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct mat { int g[4][4]; }res, ori; int n,m; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { for(int k = 0; k < 4; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % m; } } } return tmp; } void cul(int k) { while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } int ans = 0; for(int i = 0; i < 4; i++) { ans = (ans + res.g[0][i]) % m; } printf("%d\n",ans); } int main() { while(scanf("%d%d",&n,&m) != EOF) { if(n == 0) { printf("0\n"); continue; }else if(n == 1) { printf("2\n"); continue; }else if(n == 2) { printf("4\n"); continue; } memset(ori.g, 0, sizeof(ori.g)); memset(res.g, 0, sizeof(res.g)); ori.g[0][3] = 1; ori.g[1][0] = 1; ori.g[2][0] = 1; ori.g[2][1] = 1; ori.g[3][2] = 1; ori.g[3][3] = 1; for(int i = 0; i < 4; i++) res.g[0][i] = 1; cul(n - 2); } }
#include<cstdio> #include<cstring> #include<cmath> struct mat { int g[2][2]; }res, ori; int n; mat mul(mat a,mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j])%1024; } } } return tmp; } void cul(int k) { while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } int ans = (res.g[0][0] * 5 + res.g[0][1] * 2) %1024; printf("%d\n",(ans * 2 - 1) % 1024); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); if(n == 1) { printf("9\n"); continue; } ori.g[0][0] = 5; ori.g[0][1] = 12; ori.g[1][0] = 2; ori.g[1][1] = 5; res = ori; cul(n - 2); } }
hdu2276
#include<cstdio> #include<cstring> #define ll long long struct mat { int g[105][105]; }res, ori; int n, len; char str[105]; char change[2] = {'1','0'}; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) { tmp.g[0][i] = (tmp.g[0][i] + a.g[0][j] * b.g[j][i]) % 2; } } return tmp; } mat mul2(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) { for(int k = 0; k < len; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 2; } } } return tmp; } void cul(int k){ while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul2(ori, ori); } for(int i = 0; i <len; i++) { printf("%d",res.g[0][i]); } printf("\n"); } int main() { while(scanf("%d",&n) != EOF) { scanf("%s",str); len = strlen(str); memset(ori.g, 0, sizeof(ori.g)); memset(res.g, 0, sizeof(res.g)); for(int i = 0; i < len; i++) { res.g[0][i] = str[i] - '0'; } for(int i = 0; i < len; i++) { if(i == 0) ori.g[i][i] = ori.g[len - 1][i] = 1; else ori.g[i][i] = ori.g[i - 1][i] = 1; } cul(n); } }
hdu4686
Description
Input
Output
Sample Input
1 1 2 3 4 5 6 2 1 2 3 4 5 6 3 1 2 3 4 5 6
Sample Output
4 134 1902
#include<cstdio> #include<cstring> #define ll long long const ll mod = 1000000007; struct mat{ ll g[5][5]; }res, ori; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { for(int k = 0; k < 5; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j])%mod; } } } return tmp; } void cul(ll k){ while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } printf("%lld\n",res.g[0][0]); } ll a0,ax,ay; ll b0,bx,by; ll n; int main() { while(scanf("%lld",&n) != EOF) { memset(res.g,0,sizeof(res.g)); memset(ori.g,0,sizeof(ori.g)); scanf("%lld%lld%lld%lld%lld%lld",&a0,&ax,&ay,&b0,&bx,&by); res.g[0][1]=a0 * b0 % mod; res.g[0][2]=a0 % mod; res.g[0][3]=b0 % mod; res.g[0][4]=1; ori.g[0][0]=1; ori.g[1][0]=1; ori.g[1][1]=(ax * bx) % mod; ori.g[2][1]=ax * by % mod; ori.g[2][2]=ax % mod; ori.g[3][1]=bx * ay % mod; ori.g[3][3]=bx % mod; ori.g[4][1]=ay * by % mod; ori.g[4][2]=ay % mod; ori.g[4][3]=by % mod; ori.g[4][4]=1; cul(n); } return 0; }
#include<cstdio> #include<cstring> #define ll long long struct mat { ll g[2][2]; }res, ori, ans; ll n,m,k; const ll M = 1000000007; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % M; } } } return tmp; } void cul(int l){ while(l) { if(l & 1) res = mul(res, ori); l >>= 1; ori = mul(ori, ori); } ll sum = 0; sum = (sum + (res.g[0][0] * (m - k) % M)) % M; sum = (sum + (res.g[0][1] * k % M)) % M; sum = (sum + (res.g[1][0] * (m - k) % M)) % M; sum = (sum + (res.g[1][1] * k % M)) % M; printf("%lld\n",sum); } int main() { while(scanf("%lld%lld%lld",&n,&m,&k) != EOF) { memset(ori.g, 0, sizeof(ori.g)); memset(res.g, 0, sizeof(res.g)); ori.g[0][0] = m-k; ori.g[0][1] = m-k; ori.g[1][0] = k; ori.g[1][1] = k - 1; res.g[0][1] = res.g[1][0] = 0; res.g[0][0] = res.g[1][1] = 1; cul(n - 1); } }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct mat{ int g[4][4]; }res, ori; int n; int cas = 1; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { for(int k = 0; k < 4; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 2009; } } } return tmp; } mat mul2(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { tmp.g[0][i] = (tmp.g[0][i] + a.g[0][j] * b.g[j][i]) % 2009; } } return tmp; } void cul(int k) { while(k) { if(k & 1) res = mul2(res,ori); k >>= 1; ori = mul(ori, ori); } printf("Case %d: %d\n",cas++,res.g[0][3]); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); if(n == 0) { printf("Case %d: 1\n",cas++); continue; }else if(n == 1) { printf("Case %d: 4\n",cas++); continue; }else if(n == 2) { printf("Case %d: 9\n",cas++); continue; } memset(res.g, 0, sizeof(res.g)); memset(ori.g, 0, sizeof(ori.g)); res.g[0][0] = 5, res.g[0][1] = 3, res.g[0][2] = 1, res.g[0][3] = 9; ori.g[0][0] = 3, ori.g[1][0] = 2, ori.g[2][0] = 7; ori.g[0][1] = 1, ori.g[1][2] = 1; ori.g[0][3] = 3; ori.g[1][3] = 2; ori.g[2][3] = 7; ori.g[3][3] = 1; cul(n - 2); } return 0; }
#include<cstdio> #include<cstring> #define ll long long struct mat { ll g[4][4]; }res, ori, ans; ll n,x,y; mat mul(mat a, mat b) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { for(int k = 0; k < 4; k++) { tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 10007; } } } return tmp; } void cul(ll k){ while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } printf("%lld\n",res.g[0][0]); } int main() { while(scanf("%lld%lld%lld",&n,&x,&y) != EOF) { memset(ori.g, 0, sizeof(ori.g)); memset(res.g, 0, sizeof(res.g)); ori.g[0][0] = 1; ori.g[1][0] = 1; ori.g[1][1] = (x * x) % 10007; ori.g[1][2] = 1; ori.g[1][3] = x % 10007; ori.g[2][1] = (y * y) % 10007; ori.g[3][1] = (2 * x * y) % 10007; ori.g[3][3] = y % 10007; res.g[0][0] = 1; res.g[0][1] = 1; res.g[0][2] = 1; res.g[0][3] = 1; cul(n); } }
#include<cstdio> #include<cstring> #define ll long long struct mat { ll g[2][2]; }res, ori; ll a,b, m; ll n; mat mul(mat c, mat d) { mat tmp; memset(tmp.g, 0, sizeof(tmp.g)); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { tmp.g[i][j] = (tmp.g[i][j] + c.g[i][k] * d.g[k][j]) % m; } } } return tmp; } void cul(ll k) { while(k) { if(k & 1) res = mul(res, ori); k >>= 1; ori = mul(ori, ori); } ll ans =(res.g[0][0] * 2)% m; printf("%lld\n",ans); } int main() { while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m) != EOF) { memset(ori.g, 0, sizeof(ori.g)); ori.g[0][0] = a; ori.g[0][1] = b; ori.g[1][0] = 1; ori.g[1][1] = a; res = ori; cul(n - 1); } }
标签:
原文地址:http://blog.csdn.net/yeyeyeguoguo/article/details/46277153