Count Numbers
Count Numbers
时间限制: 8 Sec 内存限制: 128 MB
题目描述
However we all know the number of this kind of integers are unlimited. So she decides to sum up all these numbers whose each digit is non-zero.
Since the answer could be large, she only needs the remainder when the answer divided by a given integer p.
输入
For each test case, a line consisting of three integers a, b (1 ≤ a, b ≤ 20) and p (2 ≤ p ≤ 109 ) describes the restriction of the digit sum and the given integer p.
输出
Here we provide an explanation of the following sample output. All integers satisfying the restriction in the input are 4, 13, 31, 22, 121, 112, 211 and 1111. The sum of them all is 4 + 13 + 31 + 22 + 121 + 112 + 211 + 1111 = 1625 and that is exactly the sample output.
样例输入
5
2 1 1000000
3 1 1000000
2 2 1000000
3 3 1000000
10 1 1000000
样例输出
13
147
1625
877377
935943
题意:求十进制下各个位上的数字和为n的数的总和。
分析:首先n很大,要用__int128来存,其次要能求出关于n的递推式。分析n=k的情况,我们尝试来构造出这些满足条件的数。如果这个数的最后一位为1,那么就需要求出所有k-1的答案数字,然后在其最后加上1,如果最后一位为2,那么就需要求出所有k-2的答案数字,然后在其最后加上2,。。。。。。。
一直可以分析到最后一位为9的情况。那么我们需要两个数组ans[i],cut[i],ans[i]代表n=i时的答案是多少,cut[i]代表n=i时满足数字和是i的数字有多少个。因此就可以推出递推公式:cut[i]=sum(cut[i-j]){1<=j<=9},ans[i]=sum(10*ans[i-j]+j*cut[i-j]){1<=j<=9}。
有了递推式就可以套矩阵快速幂了,这里要注意矩阵要开18*18的,这样方便转移状态。
还有要注意的就是矩阵最好写成int类型的,以防超时。(顺便mark一下加取模和乘取模的函数)。
最后一点就是矩阵乘法可以放弃以往的一行乘一列的写法,用一种新的写法,这样可以省下不少时间。
#include<bits/stdc++.h>
//#define __int128 long long
using namespace std;
long long p=1e9+; int addmod(int a,int b) //加法取模
{
return a+b>=p?a+b-p:a+b;
}
int mulmod(long long a,int b) //乘法取模
{
return a*b%p;
} struct Mat
{
int v[][]; Mat()
{
memset(v, , sizeof(v));
}
void init()
{
for (int i=; i<; i++)
v[i][i]=(int);
} };
Mat operator *(Mat a,Mat b)
{
Mat c;
for (int i=; i<; i++)
{
for (int j=; j<; j++) //换了一种写法,节省计算0的时间
if(a.v[i][j])
{
for (int k=; k<; k++)
if(b.v[j][k])c.v[i][k]=addmod(c.v[i][k],mulmod(a.v[i][j]%p,b.v[j][k]%p));
}
}
return c;
} Mat qmod(Mat a,__int128 k)
{
Mat c;
c.init(); while (k>)
{
if (k&) c=c*a;
a=a*a;
k>>=;
}
return c;
} int main()
{
long long ans[]= {},cut[]= {};
cut[]=;
for(int i=; i<=; i++)
for(int j=; j<=i; j++)ans[i]+=*ans[i-j]+j*cut[i-j],cut[i]+=cut[i-j]; Mat a,b; for(int i=; i<; i++)a.v[][i]=;
for(int i=; i<; i++)a.v[][i]=i-;
for(int i=; i<; i++)a.v[i][i-]=;
for(int i=; i<; i++)a.v[][i]=;
for(int i=; i<; i++)a.v[i][i-]=; int t;
scanf("%d",&t);
while(t--)
{
long long aa,bb;
scanf("%lld %lld %lld",&aa,&bb,&p); for(int i=; i<; i++)b.v[i][]=ans[-i]%p;
for(int i=; i<; i++)b.v[i][]=cut[-i]%p; __int128 now=aa;
for(int i=; i<=bb; i++)now=now*(__int128)aa; if(now<=)
{
printf("%lld\n",ans[now]%p);
continue;
} Mat c=qmod(a,now-)*b;
printf("%lld\n",c.v[][]); }
return ;
}
Count Numbers的更多相关文章
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
- LC 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- [Swift]LeetCode357. 计算各个位数不同的数字个数 | Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Java [Leetcode 357]Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
随机推荐
- 在Windows笔记本上调试运行在iOS设备上的前端应用
我在每天工作中需要在不同的移动设备上测试我们开发的前端应用是否正常工作,比如iOS设备和Android设备.我用的工作笔记本电脑又是Lenovo的,安装的是Windows操作系统. 有的时候一个开发好 ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- CDN加速静态文件服务器的访问
1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...
- 用python编写九九乘法表
for i in range(1,10): for j in range(1,10): if j >i: print(end='') else: print(j,'*',i,'=',i*j,en ...
- soapui测试https双向验证p12项目
1.准备好p12 和jsk秘钥文件 2.配置soapui ssl 其中: 1:jks就是放在trustStore那里,密码填写为 106075 2:p12放到keystore,密码填写:180000 ...
- vue 动态合并单元格、并添加小计合计功能
1.效果图 2.后台返回数据格式(平铺式) 3.后台返回数据后,整理所需要展示的属性存储到(items)数组内 var obj = { "id": curItems[i].id, ...
- 利用VS自带的命令行工具查看和生产PublicKeyToken
使用VS2008(或其他版本)命令行工具,键入:SN -T C:\*****.dll 就会显示出该dll具体的PublicKeyToken数值. 如果该程序集没有强命 名,则不会有PublicKeyT ...
- Python基础篇 -- 小数据池和再谈编码
小数据池 1. id() 通过id()可以查看到一个变量表示的值在内存中的地址 s = "Agoni" print(id(s)) # 2410961093272 2. is 和 = ...
- Java中的线程--线程的互斥与同步通信
Java中的线程之前也提到过,但是还是想再详细的学习一下,跟着张孝祥老师,系统的再学习一下. 一.线程中的互斥 线程安全中的问题解释:线程安全问题可以用银行中的转账 例题描述: 线程A与线程B分别访问 ...
- 同时使用多个UITableView
1.xib\storyboard中给2个tableView设置constraints(等宽) 方法 : ①设置mainTableView的上\下\左\三部分的约束为0:subTableView上\下\ ...