题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985

We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,…, k".
Let function(n, k) be the number of elegant strings of length n which only contains digits from 0 to k (inclusive). Please calculate function(n, k).
INPUT
Input starts with an integer T (T ≤ 400), denoting the number of test cases.
Each case contains two integers, n and k. n (1 ≤ n ≤ 1018) represents the length of the strings, and k (1 ≤ k ≤ 9) represents the biggest digit in the string
2
1 1
7 6
OUTPUT
For each case, first output the case number as "Case #x: ", and x is the case number. Then output function(n, k) mod 20140518 in this case.
Case #1: 2
Case #2: 818503
source:2014 ACM-ICPC Beijing Invitational Programming Contest
 
题意:首先定义一个串elegant string:在这个串里的任何子串都没有包括0~k的一个全排列,现在给出n和k,要求求出长度为n的elegant string的个数。
解法:DP+矩阵快速幂。我们先用DP推出递推公式,然后用矩阵快速幂解决这个公式。具体思想如下:
定义DP数组:dp[12][12](dp[i][j]表示长度为 i 时字符串末尾连续有 j 个不同数字,例:1233末尾只有一个数字3,2234末尾有3个:2,3,4)。
以第二组数据为例:n=7   k=6
 
初始化:dp[1][1]=k+1,dp[1][2,,,k]=0;
dp[i+1][1]=dp[i][1]+dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]
dp[i+1][2]=6*dp[i][1]+dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]
dp[i+1][3]=5*dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]
dp[i+1][4]=4*dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]
dp[i+1][5]=3*dp[i][4]+dp[i][5]+dp[i][6]
dp[i+1][6]=2*dp[i][5]+dp[i][6]
把递推式改为如下矩阵:
 
然后我们就可以用快速幂来解决了
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long ll;
const int maxn=;
const ll mod=; ll n,k;
struct matrix
{
ll an[maxn][maxn];
}A,B;
matrix multiply(matrix x,matrix y)
{
matrix sum;
memset(sum.an,,sizeof(sum.an));
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
{
for (int k2= ;k2<=k ;k2++) {
sum.an[i][j]=(sum.an[i][j]+x.an[i][k2]*y.an[k2][j]%mod);
if (sum.an[i][j]>=mod) sum.an[i][j] -= mod;
}
}
}
return sum;
}
matrix power(ll K,matrix q)
{
matrix temp;
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
temp.an[i][j]= i==j ;
}
while (K)
{
if (K&) temp=multiply(temp,q);
q=multiply(q,q);
K >>= ;
}
return temp;
}
int main()
{
int t,ncase=;
scanf("%d",&t);
while (t--)
{
scanf("%lld%lld",&n,&k);
if (n==)
{
printf("Case #%d: %d\n",ncase++,k+);continue;
}
matrix q;
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
{
if (j>=i) q.an[i][j]=(ll);
else if (j==i-) q.an[i][j]=(ll)(k+-i);
else q.an[i][j]=(ll);
}
}
q=power(n-,q);
// for (int i=1 ;i<=k ;i++)
// {
// for (int j=1 ;j<=k ;j++)
// cout<<q.an[i][j]<<" ";
// cout<<endl;
// }
ll ans=;
for (int i= ;i<=k ;i++)
{
ans=(ans+(ll)q.an[i][]*(ll)(k+))%mod;
}
printf("Case #%d: %lld\n",ncase++,ans);
}
return ;
}
 
 
 
 
后续:感谢提出宝贵的意见。。。。
 

bnuoj 34985 Elegant String DP+矩阵快速幂的更多相关文章

  1. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  2. 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂

    [题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...

  3. 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂

    [题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...

  4. BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*

    BZOJ5298 CQOI2018 交错序列 [DP+矩阵快速幂优化] Description 我们称一个仅由0.1构成的序列为"交错序列",当且仅当序列中没有相邻的1(可以有相邻 ...

  5. Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】

    题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...

  6. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  7. [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)

    [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...

  8. poj4474 Scout YYF I(概率dp+矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4100   Accepted: 1051 Descr ...

  9. hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)

    http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...

随机推荐

  1. Jquery调用Webservice传递Json数组

    Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...

  2. 集群session的一致性

    一. 何为session 用户使用网站的服务,基本上需要浏览器和web服务器进行多次交互,web服务器如何知道哪些请求是来自哪个会话的? 具体方式为:在会话开始时,分配一个唯一的会话标识(sessio ...

  3. cassandra 之 jdbc 使用【java、scala】

    1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地 ...

  4. Thinkphp 获取当前url

    $_GET['_URL_'] 获取整个url,返回值是数组 $Think.MODULE_NAME 获取当前class的名称 $Think.ACTION_NAME 获取当前action的方法名称

  5. WordPress 主题开发 - (十三) Archive模板 待翻译

    What archive.php does (and all its related templates) is show posts based on a select criteria. A da ...

  6. Delphi CxGrid 汇总(4)

    1.     CxGrid汇总功能 ① OptionsView-Footer设置为True,显示页脚   ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义 ...

  7. 利用HttpWebRequest访问WebApi

    WebApi现在越来越流行,下面给出利用HttpWebRequest访问WebApi的工具方法: 1.利用基准URL和参数字典生成完整URL /// <summary> /// 生成URL ...

  8. jquery.validate新的写法(jquery.validate1.13.js)

    <script src="../js/jquery.js"></script> <script src="../js/jquery.vali ...

  9. ETL之增量抽取方式

    1.触发器方式 触发器方式是普遍采取的一种增量抽取机制.该方式是根据抽取要求,在要被抽取的源表上建立插入.修改.删除3个触发器,每当源表中的数据发生变化,就被相应的触发器将变化的数据写入一个增量日志表 ...

  10. SoundCloud 的开发功能

    SoundCloud开发功能:https://developers.soundcloud.com/docs     来自为知笔记(Wiz)