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. eclipse JAVA 类元素 快速添加set和get方法

    鼠标在代码窗口,鼠标右键 然后选择如下图 然后在下面图中,选择需要个类的元素添加的set和get 选择好了点击OK,这样eclipse就自动生成get和set方法

  2. DataBase: MySQL在.NET中的应用

    首先需要下载MySQL: 1. 官方下载 dev.mysql.com/downloads/mysql/ 2. 解压到你所想要安装的位置,在文件夹里创建my.ini文件 [mysql] # 设置mysq ...

  3. WebView自适应屏幕大小

    webView.getSettings().setUseWideViewPort(true); webView.getSettings().setLoadWithOverviewMode(true); ...

  4. Linux内核之旅 链表实现

    #include "stdio.h" #include "stdlib.h" struct list_head{ struct list_head *prev; ...

  5. Android -- 自定义View小Demo(一)

    1,现在要实现下图的简单效果,很简单  ,就是使用paint在canvas上绘制5中不同颜色的圆圈,效果图如下: 这是绘制基本图形一种最简单的方法,下面是它的代码 ,注释写的很详细,也就不去讲解了 M ...

  6. Mysql索引总结(一)

    数据库开发中索引的使用占了很重要的位置,好的索引会使数据库的读写效率加倍,烂的索引则会拖累整个系统甚至引发灾难. 索引分三类: index ----普通的索引,数据可以重复 unique ----唯一 ...

  7. js获取单选框radio的值

    遇到一个js获取radio值的问题,原来根据frm.type.value取到的值在ie下是空值 解决办法:type为每个radio的值 var chkObjs=document.getElements ...

  8. php使用过滤器filter_var轻松验证邮箱url和ip地址等

    以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱.url和ip地址格式是否符合都是用正则表达式.后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来 ...

  9. vscode icon in elementary os

    chmod +x /home/shenfeng/vscode/code /home/shenfeng/vscode/code sudo ln -s /home/shenfeng/vscode/code ...

  10. java中的拷贝(二)深克隆

    浅拷贝(Object类中的clone()方法)是指在拷贝对象时,对于基本数据类型的变量会重新复制一份,而对于引用类型的变量只是对引用进行拷贝. 深拷贝(或叫深克隆) 则是对对象及该对象关联的对象内容, ...