The Goddess Of The Moon

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 943    Accepted Submission(s): 426

Problem Description
Chang’e (嫦娥) is a well-known character in Chinese ancient mythology. She’s the goddess of the Moon. There are many tales about Chang'e, but there's a well-known story regarding the origin of the Mid-Autumn Moon Festival. In a very distant past, ten suns had risen together to the heavens, thus causing hardship for the people. The archer Yi shot down nine of them and was given the elixir of immortality as a reward, but he did not consume it as he did not want to gain immortality without his beloved wife Chang'e.

However, while Yi went out hunting, Fengmeng broke into his house and forced Chang'e to give up the elixir of immortality to him, but she refused to do so. Instead, Chang'e drank it and flew upwards towards the heavens, choosing the moon as residence to be nearby her beloved husband.

Yi discovered what had transpired and felt sad, so he displayed the fruits and cakes that his wife Chang'e had liked, and gave sacrifices to her. Now, let’s help Yi to the moon so that he can see his beloved wife. Imagine the earth is a point and the moon is also a point, there are n kinds of short chains in the earth, each chain is described as a number, we can also take it as a string, the quantity of each kind of chain is infinite. The only condition that a string A connect another string B is there is a suffix of A , equals a prefix of B, and the length of the suffix(prefix) must bigger than one(just make the joint more stable for security concern), Yi can connect some of the chains to make a long chain so that he can reach the moon, but before he connect the chains, he wonders that how many different long chains he can make if he choose m chains from the original chains.

 
Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m. 
(n <= 50, m <= 1e9)
The following line contains n integer numbers describe the n kinds of chains.
All the Integers are less or equal than 1e9.
 
Output
Output the answer mod 1000000007.
 
Sample Input
2
10 50
12 1213 1212 1313231 12312413 12312 4123 1231 3 131
5 50
121 123 213 132 321
 
Sample Output
86814837
797922656
 
Hint

11 111 is different with 111 11

 
Author
ZSTU
 
Source
 
解题:矩阵快速幂加速dp
 
$dp[i][j] = \sum{dp[i-1][k]*a[k][j]}$ a[k][j]表示j是否可以跟在i后面
 
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn = ;
  5. const LL mod = ;
  6. int n,m,d[maxn];
  7. struct Matrix {
  8. int m[maxn][maxn];
  9. Matrix() {
  10. memset(m,,sizeof m);
  11. }
  12. Matrix operator*(const Matrix &t) const {
  13. Matrix ret;
  14. for(int i = ; i < n; ++i)
  15. for(int j = ; j < n; ++j)
  16. for(int k = ; k < n; ++k)
  17. ret.m[i][j] = (ret.m[i][j] + (LL)m[i][k]*t.m[k][j]%mod)%mod;
  18. return ret;
  19. }
  20. };
  21.  
  22. bool check(int a,int b) {
  23. char sa[],sb[];
  24. sprintf(sa,"%d",d[a]);
  25. sprintf(sb,"%d",d[b]);
  26. for(int i = ,j; sa[i]; ++i) {
  27. for(j = ; sb[j] && sa[i+j] && sb[j] == sa[i+j]; ++j);
  28. if(!sa[i+j] && j > ) return true;
  29. }
  30. return false;
  31. }
  32. Matrix quickPow(Matrix b,int a) {
  33. Matrix ret;
  34. for(int i = ; i < n; ++i)
  35. ret.m[i][i] = ;
  36. while(a) {
  37. if(a&) ret = ret*b;
  38. a >>= ;
  39. b = b*b;
  40. }
  41. return ret;
  42. }
  43. int main() {
  44. int kase;
  45. scanf("%d",&kase);
  46. while(kase--) {
  47. scanf("%d%d",&n,&m);
  48. for(int i = ; i < n; ++i)
  49. scanf("%d",d+i);
  50. sort(d,d+n);
  51. n = unique(d,d+n) - d;
  52. Matrix a;
  53. for(int i = ; i < n; ++i)
  54. for(int j = ; j < n; ++j)
  55. a.m[i][j] = check(i,j);
  56. Matrix ret = quickPow(a,m-);
  57. int ans = ;
  58. for(int i = ; i < n; ++i)
  59. for(int j = ; j < n; ++j)
  60. ans = (ans + ret.m[i][j])%mod;
  61. printf("%d\n",ans);
  62. }
  63. return ;
  64. }

2015 Multi-University Training Contest 3 hdu 5318 The Goddess Of The Moon的更多相关文章

  1. hdu 5318 The Goddess Of The Moon 矩阵高速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...

  2. hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  5. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  6. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  7. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  9. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】

    Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  2. Docker推出了Docker云,给大家介绍下哈!

    Docker推出了Docker云,给大家介绍下哈. 收到了Docker官网的邮件邀请,他们推出了Docker云:https://cloud.docker.com 账号信息栏目下有: 云提供商:眼下支持 ...

  3. Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)

    用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...

  4. 编译libvlc。。。

    https://wiki.videolan.org/Win32Compile按照官网教程,安装所需工具,参考 :http://qjw.qiujinwu.com/blog/2014/12/08/cros ...

  5. HD-ACM算法专攻系列(22)——Max Sum

    问题描述: AC源码: 此题考察动态规划,解题思路:遍历(但有技巧),在于当前i各之和为负数时,直接选择以第i+1个为开头,在于当前i各之和为正数时,第i个可以不用作为开头(因为前i+1个之和一定大于 ...

  6. php.ini配置文件参数优化

    用于生产环境中的PHP需要对其进行优化,让PHP自身发挥更好的性能,除了写好PHP代码,还要配置好php-fpm以及php.ini调优.本文从内存.OPcache.上传.会话以及安全等方面讲解php. ...

  7. SQL的几个路径

    这个是主数据库文件存放的地方 C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER2014\MSSQL\DATA

  8. HDU 1203 I NEED A OFFER!【01背包】

    解题思路:攒下的钱n相当于包的容量,然后是m个学校的申请费用,申请费用相当于每一个物品的消耗,得到offer的概率相当于每一个物品的价值. 因为要求的是至少得到一份0ffer的概率的最大概率,可以转化 ...

  9. ZBrush中Mrgb、Rgb和M的使用

    ZBrush®软件工具架中所有的命令选项都是我们平时较为常用的,位于工具架的中间位置,有一个Mrgb.Rgb和M选项它们所表示的是材质及颜色,那么,在Zbrush中只要选择相应的选项,就可以同时绘制材 ...

  10. ZBrush软件特性之Color调控板

    ZBrush®中的Color调色板显示当前颜色并提供数值的方法选择颜色,而且选择辅助色,使用描绘工具可以产生混合的色彩效果. ZBrush 4R8下载:http://wm.makeding.com/i ...