跳过正文
  1. Posts/

hdu 1575 Tr A (矩阵快速幂模板题)

·1 分钟

http://acm.hdu.edu.cn/showproblem.php?pid=1575

题意:A为一方阵,求(A^k)73得到的矩阵的主对角线的和。

思路:矩阵快速幂。模板题。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年02月21日 星期日 10时28分33秒
  4File Name :code/hdu/1575.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=12;
 34const int MOD = 9973;
 35struct Mat
 36{
 37    int mat[N][N];
 38    void clear()
 39    {
 40	ms(mat,0);
 41    }
 42}A;
 43int n,k;
 44
 45Mat operator * (Mat a,Mat b)
 46{
 47    Mat c;
 48    c.clear();
 49    for ( int i = 0 ; i < n ; i++)
 50	for ( int j = 0 ; j < n ; j++)
 51	    for (int k = 0 ; k < n ; k++)
 52		c.mat[i][j] =(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
 53
 54    return c;
 55
 56}
 57Mat operator ^ (Mat a,int b)
 58{
 59    Mat c;
 60    for ( int i = 0 ; i < n ; i++)
 61	for ( int j = 0 ; j < n ; j++ )
 62	    c.mat[i][j]=(i==j);
 63    while (b)
 64    {
 65	if (b&1) c = c * a;
 66	b = b>>1;
 67	a = a * a;
 68    }
 69    return c;
 70}
 71int main()
 72{
 73	#ifndef  ONLINE_JUDGE
 74	freopen("code/in.txt","r",stdin);
 75  #endif
 76
 77	int T;
 78	scanf("%d",&T);
 79	while (T--)
 80	{
 81	    scanf("%d %d",&n,&k);
 82	    A.clear();
 83	    for ( int i = 0 ; i < n ; i++)
 84		for ( int j = 0 ; j < n; j ++)
 85		    scanf("%d",&A.mat[i][j]);
 86
 87	    Mat res;
 88	    res.clear();
 89	    res = A^k;
 90
 91	    int ans = 0 ;
 92	    for ( int i = 0 ;  i < n ;i++) ans = (ans +res.mat[i][i])%MOD;
 93
 94	    printf("%d\n",ans);
 95
 96	}
 97
 98
 99  #ifndef ONLINE_JUDGE
100  fclose(stdin);
101  #endif
102    return 0;
103}

相关文章

bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 (分块)

·1 分钟
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意+思路: 同codeforces 13 E holes. 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月21日 星期日 02时29分39秒 4File Name :code/bzoj/2002.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=2E5+7; 34int n,m; 35int siz = 450; //sqrt(2E5) 36int a[N]; 37int pos[N]; 38int cnt[N]; 39int nxt[N]; 40int end[N]; 41 42 43void go( int x) 44{ 45 int ans = 0 ; 46 while (1) 47 { 48 if (x>=n) 49 { 50 printf("%d\n",ans); 51 break; 52 } 53 ans +=cnt[x]; 54 x = nxt[x]; 55// cout<<"nxt[x]:"<<nxt[x]<<endl; 56 } 57} 58void update ( int i,int j) 59{ 60 if (j>=n) 61 { 62 cnt[i]=1; 63 nxt[i]=n; 64 end[i]=i; 65 } 66 else 67 { 68 if (pos[i]==pos[j]) 69 { 70 cnt[i] = cnt[j] + 1; 71 nxt[i] = nxt[j]; 72 end[i]=end[j]; 73 } 74 else 75 { 76 cnt[i] = 1; 77 nxt[i] = j; 78 end[i] = end[j]; 79 } 80 } 81} 82int main() 83{ 84 #ifndef ONLINE_JUDGE 85 freopen("code/in.txt","r",stdin); 86 #endif 87 88 scanf("%d",&n); 89 for ( int i = 0 ; i < n ;i++) 90 { 91 scanf("%d",&a[i]); 92 pos[i] = i/siz; 93 } 94 95 for ( int i = n - 1 ; i >= 0 ; i--) update(i,i+a[i]); 96 97 scanf("%d",&m); 98 while (m--) 99 { 100 int opt; 101 scanf("%d",&opt); 102 if (opt==1) 103 { 104 int x; 105 scanf("%d",&x); 106 go(x); 107 } 108 else 109 { 110 int x,y; 111 scanf("%d %d",&x,&y); 112 a[x]=y; 113 update(x,x+a[x]); 114 int p = pos[x]*siz; 115 for ( int i = x-1 ; i >= p ; i--) update(i,i+a[i]); 116 } 117 } 118 119 #ifndef ONLINE_JUDGE 120 fclose(stdin); 121 #endif 122 return 0; 123}

codeforces 13 E. Holes (分块)

·2 分钟
http://codeforces.com/problemset/problem/13/E 题意:给你n个洞,进入某个洞后会跑到另一个洞,到了另一个洞之后又可能会继续到下一个洞,问你从一个洞进去,钻了几个洞才会出来,在哪个洞出来

codeforces #339 div2 D

·2 分钟
http://codeforces.com/contest/613/problem/B 题意:有n个技能,初始每个技能的level为a[i],每个技能最大level为A(不妨称为满级技能),设满级技能个数为maxnum,最小的技能level为minval,问如何将m个技能点分配到n个技能上使得cfmaxsum+cmminval (n<=1E5,a[i],A<=1E9,cf,cm<=1E3,m<=1E15)