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. SQL中索引的原理

    (一)深入浅出理解索引结构         实际上,您可以把索引理解为一种特殊的目录.微软的SQL   SERVER提供了两种索引:聚集索引(clustered   index,也称聚类索引.簇集索引 ...

  2. java中Date的getTime() 方法奇葩问题

    今天遇到了一个奇葩问题: 从数据库中读取了3个Date类型的数据: DATE1:2015-03-12 12:10:42 DATE2:2015-03-12 12:04:40 DATE3:2015-03- ...

  3. 关于用php插入汉字到oracle中出现不乱码问题

    $conn = oci_pconnect("IBADMINTON", "5206576360", $db,'utf8'); 在代码中加入‘utf8’即可:   ...

  4. Axis2、Axis1 以及其他接口的调用方式

    在请求的时候出现问题,使用下面的方式请求就不会出现问题. package webservice.client.utils; import java.util.Iterator; import java ...

  5. 在linux中的virtualbox无法挂载usb设备的解决方法

    方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...

  6. 1.js基础

    1.如何在html文档中使用js 1)使用<script></script>将JS语法嵌入到html中,可以使用多个,每个之间都是有关联的 2)href="javas ...

  7. 使用Node.js的socket.io模块开发实时web程序

    首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...

  8. python中遇到的各种问题

    一 编码问题 python的默认编码是ascii码,可以修改为utf-8 在python\Lib\site-packages\下添加一个文件sitecustomize.py 内容是 import sy ...

  9. 【python cookbook】【字符串与文本】7.定义实现最短匹配的正则表达式

    问题:使用正则表达式对文本模式匹配,将识别出来的最长的可能匹配修改为找出最短的可能匹配 解决方法:在匹配模式中的*操作符后加上?修饰符 import re # Sample text text = ' ...

  10. datatables条件判断列显示还是隐藏

    定义一个状态变量status根据条件取值true或者false,然后在datatables参数设置中相应的columns属性的data后面加上"visible":status,该列 ...