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. ...
随机推荐
- (六)maven之本地仓库
本地仓库 ① 运行机制: 当用户在pom.xml文件中添加依赖jar包时,maven会先从本地仓库查找,如果这个jar包在本地仓库中找不到,就从中央仓库下载到本地仓库,中央仓库是maven默认 ...
- Itunes共享机制实现
http://www.raywenderlich.com/1948/itunes-tutorial-for-ios-how-to-integrate-itunes-file-sharing-with- ...
- bfs染色法判定二分图
#include<iostream> #include<queue> #include<cstring> #include<cstdio> using ...
- WPF知识点全攻略05- XAML内容控件
此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...
- A. Pride (emmmm练习特判的好题)
题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...
- 博弈论入门 Bash 、Nim 、Wythoff's Game结论及c++代码实现
SG函数先不说,给自己总结下三大博弈.和二进制及黄金分割联系密切,数学真奇妙,如果不用考试就更好了. 1.Bash Game:n个物品,最少取1个,最多取m个,先取完者胜. 给对手留下(m+1)的倍数 ...
- 【Java_基础】java中的多态性
方法的重载.重写和动态链接构成了java的多态性. 1.方法的重载 同一个类中多个同名但形参有所差异的方法,在调用时会根据参数的不同做出选择. 2.方法的重写 子类中重新定义了父类的方法,有关方法重写 ...
- php登录加密加盐
1 背景 涉及身份验证的系统都需要存储用户的认证信息,常用的用户认证方式主要为用户名和密码的方式,为了安全起见,用户输入的密码需要保存为密文形式,可采用已公开的不可逆的hash加密算法 ...
- 常用JS方法整理
目录: 截取指定字节数的字符串 判断是否微信 获取时间格式的几个举例 获取字符串字节长度 对象克隆.深拷贝 组织结构代码证验证 身份证号验证 js正则为url添加http标识 URL有效性校验方法 自 ...
- 嵌入式之:Linux下文件编译过程
本文主要三个部分:1.GNU GCC简介 2.C/C++交叉编译器arm-elf-gcc 3.make文件,用于工程管理 部分一:GNU GCC简介: 该编译器基本功能: (1)输出预处理后的文件(展 ...