hdu5564 Clarke and digits
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 144 Accepted Submission(s): 84
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.
the number of the test cases.
Each test case contains three integers l,r,k(1≤l≤r≤109,0≤k≤18).
1 2 5
2 3 5
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的更多相关文章
- hdu 5564 Clarke and digits 矩阵快速幂优化数位dp
Clarke and digits Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和
Clarke and digits Accepts: 16 Submissions: 29 Time Limit: 5000/3000 MS (Java/Others) Memory Limi ...
- HDU 5564 Clarke and digits 状压dp+矩阵加速
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...
- 【HDOJ】5564 Clarke and digits
DP+快速矩阵幂.注意base矩阵的初始化,不难. /* 5564 */ #include <iostream> #include <string> #include < ...
- hdu 5564 Clarke and digits
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5564 ------------------------------------------------ ...
- hdu5564--Clarke and digits(数位dp+矩阵快速幂)
Clarke and digits 问题描述 克拉克是一名人格分裂患者.某一天,克拉克变成了一个研究人员,在研究数字. 他想知道在所有长度在[l,r]之间的能被7整除且相邻数位之和不为k的正整数有多少 ...
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
随机推荐
- 【Flutter】功能型组件之跨组件状态共享
前言 在Flutter开发中,状态管理是一个永恒的话题. 一般的原则是:如果状态是组件私有的,则应该由组件自己管理:如果状态要跨组件共享,则该状态应该由各个组件共同的父元素来管理. 对于组 ...
- 【Linux】tcpdump
tcpdump介绍 tcpdump 是一个运行在命令行下的抓包工具.它允许用户拦截和显示发送或收到过网络连接到该计算机的TCP/IP和其他数据包.tcpdump 适用于 大多数的类Unix系统操作系统 ...
- Linux下Too many open files问题排查与解决
作者: Grey 原文地址: Github 语雀 博客园 Too many open files是Linux系统中常见的错误,从字面意思上看就是说程序打开的文件数过多,不过这里的files不单是文件的 ...
- 关于cin, cin.get(), getchar(),getline()的字符问题
一.getchar()和cin.get() getchar()会将开头的空格或者回车作为输入 1 #include<iostream> 2 using namespace std; 3 i ...
- VBScript调用winscp,实现sftp操作
最新有一个需求,需要在ssis中调用sftp下载文件,由于服务器上只有framework2.0,并且需要用sqlserver代理调用作业,限制了很多. 首先用的是脚本任务,进程调用winscp.com ...
- SQLSERVER 修改数据实例的排序规则
SQL Server服务器修改排序规则的方法 操作及验证步骤: 1 登录数据库后,查看当前安装数据库默认排序规则的两种方式 方式一.使用SQL Server 2014 Management Studi ...
- uni-app开发经验分享六:页面跳转及提示框
在我们开发的uni-app的过程中,页面跳转及提示框往往是我们做数据交互及结果反馈所要使用的功能,这里分享下我收集的一些方法及看法. 一:页面跳转 事件跳转 :指通过tap等事件来实现页面的跳转,跳转 ...
- C++中输出变量类型的方法
C++中输出变量类型的方法 在c++中输出变量或者数据类型,使用typeid().name()的方法.如下例子: #include <iostream> #include <stri ...
- wordpress迁移报错
背景: 因为一些原因迁移wordpress的博客.备份好数据库和网站源码到另一台生产环境上线的时候报错: Warning: require(/www/wwwroot/pazzn/wp-includes ...
- vue-cli快速创建项目,可视化创建
之前学习了交互式创建,发现过程无聊,而且不方便,后面又学习了图形可视化创建,下面进行分享 1.打开cmd 2.输入vue ui,输入后会出现如下 C:\Users\12235>vue ui St ...