【题解】牛客挑战赛32 (两道水题+一题矩阵快速幂)
点击比赛链接:https://ac.nowcoder.com/acm/contest/1087#question
思路:
直接看末尾是否有AK即可。
AC:
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 int n; 22 char str[110][110]; 23 int main() 24 { 25 cin>>n; 26 for (int i=1;i<=n;i++) 27 cin>>str[i]; 28 for (int i=1;i<=n;i++){ 29 int len=strlen(str[i]); 30 if (str[i][len-1]=='K'&&str[i][len-2]=='A'){ 31 for (int j=0;j<len-2;j++) 32 printf("%c",str[i][j]); 33 printf("\n"); 34 } 35 } 36 return 0; 37 }
B:
思路:先通过打表,将114514所有的因数及其个数记录下来,在通过组合即可。
AC:
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef unsigned long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 const int MAXN=3e5+10; 22 ll n,cnt[MAXN]; 23 int main() 24 { 25 ll n,x; 26 memset(cnt,0,sizeof(cnt)); 27 cin>>n; 28 for (ll i=1;i<=n;i++){ 29 cin>>x; 30 if (x<200000) 31 cnt[x]++; 32 } 33 ll ans=0; 34 if (cnt[2]&&cnt[31]&&cnt[1847]) //2*31*1847 35 ans=ans+cnt[2]*cnt[31]*cnt[1847]; 36 if (cnt[31]&&cnt[3694]) //31*3694 37 ans=ans+cnt[31]*cnt[3694]; 38 if (cnt[62]&&cnt[1847]) //62*1847 39 ans=ans+cnt[62]*cnt[1847]; 40 if (cnt[2]&&cnt[57257]) //2*57257 41 ans=ans+cnt[2]*cnt[57257]; 42 if (cnt[114514]) //113514 43 ans=ans+cnt[114514]; 44 if (cnt[1]) 45 ans=ans+ans*cnt[1]; 46 cout<<ans<<endl; 47 return 0; 48 }
C:
先通过自行推导,得到An的线性推导式。
然后
推得A型矩阵为
最后直接套模板矩阵快速幂就行
AC:
1 #include<iostream> 2 #include<cstdio> 3 #include<ctime> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<algorithm> 11 #define Max(a,b) ((a)>(b)?(a):(b)) 12 #define Min(a,b) ((a)<(b)?(a):(b)) 13 #define Mem0(x) memset(x,0,sizeof(x)) 14 #define Mem1(x) memset(x,-1,sizeof(x)) 15 #define MemX(x) memset(x,0x3f,sizeof(x)) 16 using namespace std; 17 typedef long long ll; 18 const int inf=0x3f3f3f; 19 const double pi=acos(-1.0); 20 21 22 23 ll mod=998244353,n; 24 struct s { 25 ll mp[10][10]; 26 }; 27 s ans,matrix,res; 28 void init(){ 29 matrix.mp[0][0] = 2; matrix.mp[0][1] = 1; matrix.mp[0][2] = 0; matrix.mp[0][3] = 0; 30 matrix.mp[1][0] = 1; matrix.mp[1][1] = 0; matrix.mp[1][2] = 1; matrix.mp[1][3] = 0; 31 matrix.mp[2][0] = -2; matrix.mp[2][1] = 0; matrix.mp[2][2] = 0; matrix.mp[2][3] = 1; 32 matrix.mp[3][0] = -1; matrix.mp[3][1] = 0; matrix.mp[3][2] = 0; matrix.mp[3][3] = 0; 33 memset(res.mp, 0, sizeof(res.mp)); 34 res.mp[0][0] = 5, res.mp[0][1] = 2, res.mp[0][2] = 1, res.mp[0][3] = 0; 35 for (int i = 1; i < 4; i++) { 36 for (int j = 0; j < 4; j++) { 37 res.mp[i][j] = 0; 38 } 39 } 40 return ; 41 } 42 s mul(s x, s y) 43 { 44 s tmp; 45 memset(tmp.mp, 0, sizeof(tmp.mp)); 46 for (int i = 0; i < 4; i++) 47 for (int j = 0; j < 4; j++) 48 for (int k = 0; k < 4; k++) 49 tmp.mp[i][j] += (x.mp[i][k] * y.mp[k][j]) % mod,tmp.mp[i][j]=(tmp.mp[i][j]+mod)%mod; 50 51 return tmp; 52 } 53 54 ll quick_matrix(ll b) 55 { 56 while (b){ 57 if (b&1) 58 res=mul(res,matrix); 59 matrix=mul(matrix,matrix); 60 b>>=1; 61 } 62 return res.mp[0][3]; 63 } 64 65 66 int main() 67 { 68 init(); 69 scanf("%lld",&n); 70 cout<<quick_matrix(n-1)<<endl; 71 72 return 0; 73 }