2013 Multi-University Training Contest 9(hdu 4686 - 4691)dp(好)+矩阵快速幂+一般图匹配带花树+后缀数组
2015-08-27 14:38
711 查看
A - 1001
Description
An Arc of Dream is a curve defined by following function:AoD(n)=∑n−1i=0ai∗biAoD(n)=\sum_{i=0}^{n-1}a_i*b_i
where
a0=A0a_0 = A_0
ai=ai−1∗AX+AYa_i = a_{i-1}*AX+AY
b0=B0b_0 = B_0
bi=bi−1∗BX+BYb_i = b_{i-1}*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
Input
There are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 10 18, and all the other integers are no more than 2×10 9.
Output
For each test case, output AoD(N) modulo 1,000,000,007.
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
构造矩阵,其实就是由aibi=(ai−1∗AX+AY)∗(bi−1∗BX+BY)a_ib_i=(a_{i-1}*AX+AY)*(b_{i-1}*BX+BY)推出来的
|aibi||a_ib_i| |AXBXAXBYAYBXAYBY00||AXBX AXBY AYBX AYBY 0 0|
|ai|| a_i | = |0AX0AY00||0 AX 0 AY 0 0|
|bi|| b_i | |00BXBY00||0 0 BX BY 0 0|
|1|| 1 | |000100||0 0 0 1 0 0|
|S|| S | |AXBXAXBYAYBXAYBY11||AXBX AXBY AYBX AYBY 1 1|
[code]#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; typedef long long LL; const int MOD=1e9+7; LL N; LL A0,AX,AY,B0,BX,BY; struct Matrix{ LL mat[10][10]; int n; Matrix (int _n=5){ n=_n; memset(mat,0,sizeof(mat)); } Matrix operator*(const Matrix &a)const{ Matrix res; for(int k=0;k<n;k++){ for(int i=0;i<n;i++){ if(mat[i][k]==0)continue; for(int j=0;j<n;j++){ if(a.mat[k][j]==0)continue; res.mat[i][j]=(res.mat[i][j]+mat[i][k]*a.mat[k][j]%MOD); if(res.mat[i][j]>=MOD)res.mat[i][j]-=MOD; } } } return res; } }; Matrix pow_mul(Matrix A,LL n){ Matrix res; for(int i=0;i<res.n;i++)res.mat[i][i]=1; while(n){ if(n&1)res=res*A; A=A*A; n>>=1; } return res; } int main(){ while(scanf("%I64d",&N)!=EOF){ scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&A0,&AX,&AY,&B0,&BX,&BY); Matrix A,B; if(N==0){ printf("0\n"); continue; } A0%=MOD,B0%=MOD,AX%=MOD,AY%=MOD,BX%=MOD,BY%=MOD; A.mat[0][0]=A.mat[4][0]=AX*BX%MOD; A.mat[0][1]=A.mat[4][1]=AX*BY%MOD; A.mat[0][2]=A.mat[4][2]=AY*BX%MOD; A.mat[0][3]=A.mat[4][3]=AY*BY%MOD;A.mat[4][4]=1; A.mat[1][1]=AX;A.mat[1][3]=AY; A.mat[2][2]=BX,A.mat[2][3]=BY; A.mat[3][3]=1; B.mat[0][0]=B.mat[4][0]=A0*B0%MOD,B.mat[1][0]=A0,B.mat[2][0]=B0; B.mat[3][0]=1; A=pow_mul(A,N-1); A=A*B; printf("%I64d\n",A.mat[4][0]); } return 0; }
B题是一般图匹配,见前面的博客:
hdu - 4687
D - 1004
Description
A derangement is a permutation such that none of the elements appear in their original position. For example, [5, 4, 1, 2, 3] is a derangement of [1, 2, 3, 4, 5]. Subtracting the original permutation from the derangement, we get the derangement difference [4, 2, -2, -2, -2], where none of its elements is zero. Taking the signs of these differences, we get the derangement sign [+, +, -, -, -]. Now given a derangement sign, how many derangements are there satisfying the given derangement sign?Input
There are multiple test cases. Process to the End of File.Each test case is a line of derangements sign whose length is between 1 and 20, inclusively.
Output
For each test case, output the number of derangements.Sample Input
+-++—
Sample Output
113
递推,令dp[i][j]表示错位排列的前i个数中有j个对应+的位置尚未填写。这里的j还有令一层意思,即0~i-1中也恰好有j个数未出现在已知的部分错位排列中。原因是如果某个位置填写了数,那么必须是0~i-1其中之一,而有j个位置尚未填写,故0~i-1中有j个未出现。
假如已经考虑了前i位,考虑a[i]:
[code]+:这个位置的数也无法确定,算作“未填写”,而之前j个未填的地方可以挑一个填i,对应:dp[i+1][j] += dp[i][j] * j; 也可以不用i,对应:dp[i+1][j+1] += dp[i][j] -:这个位置的数必须确定,由于0~i-1中恰有j个数未被填写,所以位置i可以填上这j个数中的任何一个。 另外之前j个未确定的位置可以填i(对应:dp[i+1][j-1] += dp[i][j] * j * j); 也可以不用i(对应:dp[i+1][j] += dp[i][j] * j)
[code]#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=50; typedef long long LL; LL dp[maxn][maxn]; int N; char s[maxn]; LL DP(int i,int j){ if(dp[i][j]!=-1)return dp[i][j]; if(i==N){ dp[i][j]=(j==0); return dp[i][j]; } LL ans=0; if(s[i]=='+'){ ans+=j*DP(i+1,j); ans+=DP(i+1,j+1); } else { ans+=j*j*DP(i+1,j-1); ans+=j*DP(i+1,j); } return dp[i][j]=ans; } int main(){ while(scanf("%s",s)!=EOF){ N=strlen(s); memset(dp,-1,sizeof(dp)); printf("%I64d\n",DP(0,0)); } return 0; }
F后缀数组的简单应用,见以前的博客:
hdu 4691
相关文章推荐
- 再谈应用环境下的TIME_WAIT和CLOSE_WAIT
- AvailableAnnotations
- 解决 iBooks 无法显示 epub 电子书本身的 thumbnail
- Pair
- uva 10651 Pebble Solitaire
- org.artofsolving.jodconverter.office.OfficeException: failed to start and connect
- 浅谈层次化的AI架构
- IBDesignable Errors Failed to update auto layout status:……
- strong,weak, retain, assign的区别
- [转]Skia之二 —— SkPaint 篇
- 领域模型驱动设计(Domain Driven Design)入门概述
- 出现( linker command failed with exit code 1)错误总结
- UVA 253 Cube painting(思维题)
- usaco Barn Repair
- strong,weak, retain, assign的区别
- timed out waiting for input: auto-logout
- ubuntu下出现su: Authentication failture
- 2015 Multi-University Training Contest 9(hdu 5396 - hdu 5405)
- AIDL的简单示例与解析
- hdoj 1023 Train Problem II 【卡特兰数】