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. C++的STL在C#中的应用

    这里主要讲几个重要的STL在C#中的应用:vector, map, hash_map, queue, set, stack, list. vector: 在C#中换成了list using Syste ...

  2. 自动开票-不能获取汇款地址 Cannot get remit to address

    1. Cannot get remit to address 1. 查看客户Bill-to Address的Country信息; 2. 选择Receivable Manager职责,通过路径Setup ...

  3. zw版【转发·台湾nvp系列Delphi例程】HALCON GenGridRegion

    zw版[转发·台湾nvp系列Delphi例程]HALCON GenGridRegion unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...

  4. TortoiseGit文件夹和文件图标不显示解决方法

    试了两种方法, 1.修改Max Cached Icons http://www.open-open.com/lib/view/open1414396787325.html 2.修改图标排序 http: ...

  5. 锋利的JQuery(四)

    表单: 一个表单有三个基本组成部分:表单标签.表单域.表单按钮 Cookie: 在jQuery中有一款Cookie插件,<script src="js/jquery.cookie.js ...

  6. tmpfs

    什么是tmpfs tmpfs是Linux/Unix系统上的一种基于内存的文件系统.tmpfs可以使用您的内存或swap分区来存储文件.由此可见,temfs主要存储暂存的文件. linux内核中的VM子 ...

  7. scala伴生对象

    package com.test.scala.test /** * 伴生对象指的是在类中建立一个object */ class AssociatedObject { private var count ...

  8. webpack笔记_(1)_webpack 安装

    webpack不仅可以解析jsx,也可以将es6转换为es5语法.最终,它把这些代码都打包成一个叫bundle.js的文件,我们在html文件中只引入这么一个js文件就可以了! 打包后,引用的语法im ...

  9. CDN流量放大攻击思路

    首先,为了对CDN进行攻击,我们必须清楚CDN的工作原理,这里我们再来简单介绍一下CDN的工作模型. CDN的全称是Content Delivery Network(内容分发网络),通过在网络各处的加 ...

  10. DBLINK的session无法关闭,报异常!

    ------解决方法-------------------------------------------------------- --ALTER SESSION alter session clo ...