E. Elegant String

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 65536KB
 
64-bit integer IO format: %lld      Java class name: Main
Font Size: + -
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.
 
 

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. 
 
 

Sample Input

2
1 1
7 6
 

Sample Output

Case #1: 2
Case #2: 818503
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const LL p = ;
struct Matrix
{
LL mat[][];
void init()
{
memset(mat,,sizeof(mat));
}
void first(int n)
{
int i,j;
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i==j)mat[i][j]=;
else mat[i][j]=;
}
}M_hxl,M_tom;
Matrix multiply(Matrix cur,Matrix ans,int n)
{
Matrix now;
now.init();
int i,j,k;
for(i=;i<=n;i++)
{
for(k=;k<=n;k++)
{
for(j=;j<=n;j++)
{
now.mat[i][j]=now.mat[i][j]+cur.mat[i][k]*ans.mat[k][j];
now.mat[i][j]%=p;
}
}
}
return now;
}
Matrix pow_mod(Matrix ans,LL n,LL m)
{
Matrix cur;
cur.first(m);
while(n)
{
if(n&) cur=multiply(cur,ans,m);
n=n>>;
ans=multiply(ans,ans,m);
}
return cur;
}
LL solve(LL n,LL k)
{
M_hxl.init();
int i,j;
for(i=;i<=k-;i++)M_hxl.mat[][i]=;
for(i=;i<=k-;i++)
{
for(j=;j<=k-;j++)
{
if(i-j>)continue;
if(i-j==) M_hxl.mat[i][j]=k-i+;
else M_hxl.mat[i][j]=;
}
}/**ok**/
M_tom = pow_mod(M_hxl,n,k-);
LL sum=(M_tom.mat[][]*k)%p;
return sum; }
int main()
{
int T,t;
LL n,k;
scanf("%d",&T);
for(t=;t<=T;t++)
{
scanf("%lld%lld",&n,&k);
k++;
LL ans=solve(n,k);
printf("Case #%d: %lld\n",t,ans);
}
return ;
}

北邀 E Elegant String的更多相关文章

  1. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  2. BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...

  3. 2014 ACM/ICPC 北京邀请赛 部分 题解

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...

  4. 2014 北京邀请赛ABDHJ题解

    A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...

  5. RestClient(接口请求)

    一.电子签章:通过接口,传入参数中有pdf文件,和其他参数,在文件上盖上电子签章. 引入:通过NuGet安装RestSharp,注意版本. 备注:如果后面程序运行有错,可以看是引用是否更改了web.c ...

  6. 尚硅谷Java高级笔记

    尚硅谷Java高级笔记 idea的使用: 一些小区别: 其他细节参考idea配置pdf 多线程: 基本概念: 多线程的优点: 何时需要多线程: 线程的创建和使用: 创建多线程的第一种方式: /** * ...

  7. elegant 的长整数加法 string 实现

    string strAdd(string &v1, string &v2){ string res = ""; ; int len1 = v1.size(), le ...

  8. [小北De编程手记] : Lesson 05 玩转 xUnit.Net 之 从Assert谈UT框架实践

    这一篇,本文会介绍一下基本的断言概念,但重点会放在企业级单元测试的相关功能上面.下面来跟大家分享一下xUnit.Net的断言,主要涉及到以下内容: 关于断言的概念 xUnit.Net常用的断言 关于单 ...

  9. [小北De编程手记] : Lesson 04 玩转 xUnit.Net 之 Fixture(下)

    上一篇文章<[小北De编程手记] : Lesson 03 玩转 xUnit.Net 之 Fixture(上)>向大家介绍了xUnit.Net 共享数据的方式.Test Case的构造函数 ...

随机推荐

  1. meta标签的理解

    一直习惯的使用meta标签,还真么认真理解过,至少英文意思都还没弄明白... 下面是摘自网络的解释: 互动百科: 元素可提供相关页面的元信息(meta-information),比如针对搜索引擎和更新 ...

  2. Codeforce Round #215 Div2 C

    还以为就这么点分了,不会跪了,起码有点加,生活都这么艰难了,为什么不让我好好地活下去! 是不是世界对我充满了恶意! 当然还是自己太菜! B题没初始化第一个就杯具了一次 C题大概的弄出来了,调了半个小时 ...

  3. yii框架中应用jquery表单验证插件

    效果图: 视图层: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  4. 夺命雷公狗---node.js---4net模块(上)

    node.js为我们提供了一个net模块,主要是为了提供了一些底层通信的小工具,包含了创建服务器/客户端方法,引入方式也很简单: var net = require('net'); net模块也为我们 ...

  5. 夺命雷公狗---Thinkphp----10之后台登录.注销一条龙

    首先我们还是还是写一个控制器名字叫LoginController.class.php的控制器,首先来写一个code的方法来让验证码先显示出来: public function Code(){ //创建 ...

  6. clock divider

    一个clock的产生: 1) Clock source的选择: cgm_mux5(.clk_out, .clk_in0, .clk_in1, .clk_in2, .clk_in3, .clk_in4, ...

  7. mysql-5.5.28源码安装过程中错误总结

    介绍一下关于mysql-5.5.28源码安装过程中几大错误总结,希望此文章对各位同学有所帮助.系统centOS 6.3 mini (没有任何编译环境)预编译环境首先装了众所周知的 cmake(yum ...

  8. ASP+Access UTF-8 网页乱码问题解决办法

    用ACCESS数据库和ASP做网站时用UTF-8编码有时会出现乱码,再者网页出错或者刷新页面后就是乱码,如果数据库取值乱码在开头加上<%@LANGUAGE="VBSCRIPT" ...

  9. javax.xml.ws.soap.SOAPFaultException: 没有Header,拦截器实施拦截

    客户端代码: client.getOutInterceptors().add(new AuthInterceptor("zhangsan", "123456") ...

  10. Hibernate,JPA注解@Entity

    通过@Entity注解将一个类声明为一个实体bean(即一个持久化POJO类), @Id注解则声明了该实体bean的标识属性. 其他的映射定义是隐式的. 就是说一个持久化POJO类,除了主键ID需要@ ...