Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 144    Accepted Submission(s): 84


Problem Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a researcher, did a research on digits. 

He wants to know the number of positive integers which have a length in [l,r] and
are divisible by 7 and
the sum of any adjacent digits can not be k.
 

Input
The first line contains an integer T(1≤T≤5),
the number of the test cases. 

Each test case contains three integers l,r,k(1≤l≤r≤109,0≤k≤18).
 

Output
Each test case print a line with a number, the answer modulo 109+7.
 

Sample Input

2
1 2 5
2 3 5
 

Sample Output

13
125

Hint:
At the first sample there are 13 number $7,21,28,35,42,49,56,63,70,77,84,91,98$ satisfied.

 

这题先列出dp方程,dp[i][x][ (t*10+x)%7 ]+=dp[i-1][j][t],因为i太大,所以要用矩阵快速幂加速。

可以构造矩阵【dp[i][0][0],dp[i][1][0],..dp[i][0][6],...dp[i][9][6],sum[i-1] 】*A=【dp[i+1][0][0],dp[i+1][1][0],..dp[i+1][0][6],...dp[i+1][9][6],sum[i]】,其中dp[i][j][k]中j表示的是最后一位的数,k表示的是这个数模7后的余数,为了表示前缀和,我们只要把第71列的前10个变为1,dp[70][70]变为1就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define MOD 1000000007
struct matrix{
ll n,m,i;
ll data[72][72];
void init_danwei(){
for(i=0;i<n;i++){
data[i][i]=1;
}
}
}; matrix multi(matrix &a,matrix &b){
ll i,j,k;
matrix temp;
temp.n=a.n;
temp.m=b.m;
for(i=0;i<temp.n;i++){
for(j=0;j<temp.m;j++){
temp.data[i][j]=0;
}
}
for(i=0;i<a.n;i++){
for(k=0;k<a.m;k++){
if(a.data[i][k]>0){
for(j=0;j<b.m;j++){
temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD;
}
}
}
}
return temp;
} matrix fast_mod(matrix &a,ll n){
matrix ans;
ans.n=a.n;
ans.m=a.m;
memset(ans.data,0,sizeof(ans.data));
ans.init_danwei();
while(n>0){
if(n&1)ans=multi(ans,a);
a=multi(a,a);
n>>=1;
}
return ans;
} int main()
{
ll n,k,m,i,j,T,l,r,t,x;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld",&l,&r,&k);
matrix a;
a.n=a.m=71;
memset(a.data,0,sizeof(a.data));
for(t=0;t<=6;t++){
for(j=0;j<=9;j++){
for(x=0;x<=9;x++){
if(x+j!=k){
a.data[j+10*t ][x+(t*10+x)%7*10 ]=1;
}
}
}
}
for(i=0;i<10;i++){
a.data[i][70]=1;
}
a.data[70][70]=1; matrix a1;
a1.n=a1.m=71;
memset(a1.data,0,sizeof(a1.data));
for(i=0;i<=70;i++){
for(j=0;j<=70;j++){
a1.data[i][j]=a.data[i][j];
}
} matrix b;
b.n=1;b.m=71;
memset(b.data,0,sizeof(b.data));
for(j=1;j<=9;j++){
b.data[0][j+j%7*10]=1;
} matrix b1;
b1.n=1;b1.m=71;
memset(b1.data,0,sizeof(b1.data));
for(j=1;j<=9;j++){
b1.data[0][j+j%7*10]=1;
}
ll sum1,sum2;
matrix ans;
ans=fast_mod(a,l-1);
matrix cnt;
cnt=multi(b,ans);
sum1=cnt.data[0][70]; matrix ans1;
ans1=fast_mod(a1,r);
/*
for(i=0;i<=70;i++){
for(j=0;j<=70;j++){
printf("%lld ",ans1.data[i][j]); }
printf("\n");
}
*/ matrix cnt1;
cnt1=multi(b1,ans1);
sum2=cnt1.data[0][70]; printf("%lld\n",(sum2-sum1+MOD)%MOD );
}
return 0;
}

hdu5564 Clarke and digits的更多相关文章

  1. hdu 5564 Clarke and digits 矩阵快速幂优化数位dp

    Clarke and digits Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和

    Clarke and digits  Accepts: 16  Submissions: 29  Time Limit: 5000/3000 MS (Java/Others)  Memory Limi ...

  3. HDU 5564 Clarke and digits 状压dp+矩阵加速

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...

  4. 【HDOJ】5564 Clarke and digits

    DP+快速矩阵幂.注意base矩阵的初始化,不难. /* 5564 */ #include <iostream> #include <string> #include < ...

  5. hdu 5564 Clarke and digits

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5564 ------------------------------------------------ ...

  6. hdu5564--Clarke and digits(数位dp+矩阵快速幂)

    Clarke and digits 问题描述 克拉克是一名人格分裂患者.某一天,克拉克变成了一个研究人员,在研究数字. 他想知道在所有长度在[l,r]之间的能被7整除且相邻数位之和不为k的正整数有多少 ...

  7. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  8. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

随机推荐

  1. 2020周阳SpringCloud完整版笔记--一

    微服务架构入门 微服务 的概念最早产生于Martin Fowler在2014年的一篇论文中. 微服务架构是一种架构模式,他提倡将单一应用程序划分成一组小的服务,服务与服务之间互相协调.相互配合,为用户 ...

  2. 【SpringBoot】Spring Boot,开发社区讨论交流网站首页。

    初识Spring Boot,开发社区讨论交流网站首页. 文章目录 初识Spring Boot,开发社区讨论交流网站首页. 1.项目简介 2. 搭建开发环境 JDK Apache Maven Intel ...

  3. 创建一个简单MyBatis程序

    文章目录 MyBatis基础 MyBatis 简介 创建一个MyBatis程序 1. 创建Java项目 2. 加载MyBatis包 3. 编写POJO类和映射文件 4.创建mybatis-config ...

  4. 【Linux】ssh反映特别慢,但是网络没有问题的时怎么办

    用crt连接服务器的时候,感觉很久才有反映,大约持续2秒以上,这种情况下,是解析的问题 这里有一个方法可以优化ssh cd /etc/ssh/ cp sshd_config sshd_config.b ...

  5. Eclipse中的可视化图形界面设计插件windowbuilder

    对于eclipse平台上的可视化开发工具插件,有windowbuilder.visual editor等,今天就对windowbuilder说明: WindowBuilder功能特性等介绍,参考如下网 ...

  6. M8 E147 不可能为条目*确立账户

    今天用BAPI做发票校验, BAPI_INCOMINGINVOICE_CREATE这个函数使用都正常,可是突然就无法做发票检验了 报了个错误,"不可能为条目BOXT TR 确立账户" ...

  7. Vue 标签Style 动态三元判断绑定

    <div  :style=" 1==1 ? 'display:block' : 'display:none' "></div> v-bind:style 的 ...

  8. (02)-Python3之--列表(list)操作

    1.定义 列表的关键字:list 列表以[]括起来,数据之间用 , 隔开.列表当中的数据,可以是任意类型.数值是可以重复的. 列表元素是 可变的,顺序是 有序的. 例如: b = ["萝卜& ...

  9. MySQL调优之索引优化

    一.索引基本知识 1.索引的优点 1.减少了服务器需要扫描的数据量 2.帮助服务器避免排序和临时表 例子: select * from emp orde by sal desc; 那么执行顺序: 所以 ...

  10. :setting:`task_soft_time_limit` celery 异步任务 执行时间限制 内存限制

    https://docs.celeryproject.org/en/stable/userguide/configuration.html?highlight=control_exchange#new ...