北邀 E Elegant String

E. Elegant String

Time Limit: 1000ms

Case Time Limit: 1000ms

Memory Limit: 65536KB

64-bit integer IO
format: %lld      Java class
name: Main

Submit Status PID:
34985

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


 1 #include<iostream>
2 #include<stdio.h>
3 #include<cstring>
4 #include<cstdlib>
5 using namespace std;
6 typedef long long LL;
7
8 const LL p = 20140518;
9 struct Matrix
10 {
11 LL mat[11][11];
12 void init()
13 {
14 memset(mat,0,sizeof(mat));
15 }
16 void first(int n)
17 {
18 int i,j;
19 for(i=1;i<=n;i++)
20 for(j=1;j<=n;j++)
21 if(i==j)mat[i][j]=1;
22 else mat[i][j]=0;
23 }
24 }M_hxl,M_tom;
25 Matrix multiply(Matrix cur,Matrix ans,int n)
26 {
27 Matrix now;
28 now.init();
29 int i,j,k;
30 for(i=1;i<=n;i++)
31 {
32 for(k=1;k<=n;k++)
33 {
34 for(j=1;j<=n;j++)
35 {
36 now.mat[i][j]=now.mat[i][j]+cur.mat[i][k]*ans.mat[k][j];
37 now.mat[i][j]%=p;
38 }
39 }
40 }
41 return now;
42 }
43 Matrix pow_mod(Matrix ans,LL n,LL m)
44 {
45 Matrix cur;
46 cur.first(m);
47 while(n)
48 {
49 if(n&1) cur=multiply(cur,ans,m);
50 n=n>>1;
51 ans=multiply(ans,ans,m);
52 }
53 return cur;
54 }
55 LL solve(LL n,LL k)
56 {
57 M_hxl.init();
58 int i,j;
59 for(i=1;i<=k-1;i++)M_hxl.mat[1][i]=1;
60 for(i=2;i<=k-1;i++)
61 {
62 for(j=1;j<=k-1;j++)
63 {
64 if(i-j>1)continue;
65 if(i-j==1) M_hxl.mat[i][j]=k-i+1;
66 else M_hxl.mat[i][j]=1;
67 }
68 }/**ok**/
69 M_tom = pow_mod(M_hxl,n,k-1);
70 LL sum=(M_tom.mat[1][1]*k)%p;
71 return sum;
72
73 }
74 int main()
75 {
76 int T,t;
77 LL n,k;
78 scanf("%d",&T);
79 for(t=1;t<=T;t++)
80 {
81 scanf("%lld%lld",&n,&k);
82 k++;
83 LL ans=solve(n,k);
84 printf("Case #%d: %lld\n",t,ans);
85 }
86 return 0;
87 }

北邀 E Elegant String

时间: 2024-10-07 18:42:18

北邀 E Elegant String的相关文章

bnu 34985 Elegant String(矩阵快速幂+dp推导公式)

Elegant String Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component      Cy

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main 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

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

题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的某一个全排列 邀请赛上A的较多的一道题,比赛的时候死活想不出,回来之后突然就想通了,简直..... = =! 解题思路: 对于所有串我们都只考虑末尾最多有多少位能构成全排列的一部分(用l来表示),即最多有多少位不重复的数字出现,将问题转化为求末尾最多有k位能构成全排列的串的总数量 假设k为5,有一个

bnu oj 34985 Elegant String (矩阵+dp)

Elegant String 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

bnu 34895 Elegant String(矩阵快速幂)

题目链接:bnu 34895 Elegant String 题目大意:给定n和k,表示有一个长度为n的序列,序列中的元素由0~k组成,问说有多少个串满足不包含0~k的全排列. 解题思路:矩阵快速幂,根据dp[i][j]表示说第i为有j个相同,写出递推式,根据递推式求出矩阵. #include <cstdio> #include <cstring> typedef long long ll; const ll MOD = 20140518; const int N = 20; str

bnuoj 34985 Elegant String

题目链接: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 s

2014 北京邀请赛ABDHJ题解

A. A Matrix 点击打开链接 构造,结论是从第一行开始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; #defi

2014 BNU 邀请赛E题(递推+矩阵快速幂)

Elegant String 题意:给定一个字符串,由0-k数字组成,要求该串中,子串不包含0-k全排列的方案数 思路:dp[i][j]表示放到i个数字,后面有j个不相同,然后想递推式,大概就是对应每种情况k分别能由那几种状态转移过来,在纸上画画就能构造出矩阵了,由于n很大,所以用快速幂解决 代码: #include <stdio.h> #include <string.h> const long long MOD = 20140518; int t; long long n; i

CTSC2016滚粗记

现在的时间是2016年5月5日22:48:48,是CTSC的结束日晚上,也是APIO的开始日晚上. 鉴于喜闻乐见狗牌滚粗,于是就写个游记纪念一下吧. day -∞ 要从R1完了的那天开始说起. 教练被R1给吓怕了.他第一次意识到SD变的高手如云,他从未想过省选上有这么多人A题,他没想到分数普遍特别高,他也没想到我能考的这么烂. 考试这种事情,尤其是坑爹的OI比赛,变数是很大的.不光知识的储备量,还包括考试策略.考试技巧.骗分手段.RP等等.然而我的运气很差,首先是习惯开大数组的我第一次MLE了,