Count Numbers

时间限制: 8 Sec  内存限制: 128 MB
提交: 43  解决: 19
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Now Alice wants to sum up all integers whose digit sum is exactly ab .
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.

输入

The input has several test cases and the first line contains the integer t (1 ≤ t ≤ 400) which is the number of test cases.
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.

输出

For each test case, output a line with the required answer.
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来存。如果这个数的最后一位为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的,这样方便转移状态。
最后一点就是矩阵乘法可以放弃以往的一行乘一列的写法,用一种新的写法,这样可以省下不少时间。
构造的矩阵(n大于9时,用于以n==9为基础往上递推,n小于等于9时直接暴力)为:

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p;
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]=;
}
} };
Mat operator *(Mat a,Mat b)
{
Mat c;
for (int i=; i<; i++)
{
for (int j=; j<; j++)
{
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()
{
ll ans[]={},cut[]={};
ll aa,bb,t;
__int128 now;
Mat a,b,c;
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];
}
}
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-]=;
scanf("%lld",&t);
while(t--)
{
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;
now=aa;
for(int i=; i<=bb; i++) now=now*(__int128)aa;
if(now<=)
{
printf("%lld\n",ans[now]%p);
continue;
}
c=qmod(a,now-)*b;
printf("%lld\n",c.v[][]);
}
return ;
}

注意:__int128在有的情况下不能编译!!!

												

Count Numbers(矩阵快速幂)的更多相关文章

  1. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  2. HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others)    ...

  3. Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)

    题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...

  4. HDU 6470:Count(矩阵快速幂)

    Count Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. Sam's Numbers 矩阵快速幂优化dp

    https://www.hackerrank.com/contests/hourrank-21/challenges/sams-numbers 设dp[s][i]表示产生的总和是s的时候,结尾符是i的 ...

  6. 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)

    Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...

  7. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  8. HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )

    链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...

  9. UOJ424 Count 生成函数、多项式求逆、矩阵快速幂

    传送门 两个序列相同当且仅当它们的笛卡尔树相同,于是变成笛卡尔树计数. 然后注意到每一个点的权值一定会比其左儿子的权值大,所以笛卡尔树上还不能够存在一条从根到某个节点的路径满足向左走的次数\(> ...

随机推荐

  1. uva10570(枚举基准,贪心)

    uva10570(枚举基准,贪心) 输入一个1至n的排列(n<=500),每次可以交换两个整数,用最小的交换次数把排列变成1至n的一个环状排列. 首先用\(O(n)\)的时间枚举一个排列,接着问 ...

  2. 洛谷P5170 【模板】类欧几里得算法(数论)

    传送门 此题剧毒,公式恐惧症患者请直接转去代码→_→ 前置芝士 基本数论芝士 题解 本题就是要我们求三个函数的值 \[f(a,b,c,n)=\sum_{i=0}^n \left\lfloor\frac ...

  3. springboot Consider defining a bean of type 'xxx' in your configuration

    这个错误是service的bean注入失败,主要是Application位置不对,要保证项目中的类在Application启动服务器类的下一级目录,如图:

  4. js unicode

    参考 http://www.ruanyifeng.com/blog/2014/12/unicode.html

  5. Tensorflow安装教程-Win10环境下

    背景:最新版的Tensoflow已经支持Python3.6 首先,下载并安装Anaconda3 内置Python3.6的版本 https://www.continuum.io/downloads 安装 ...

  6. KEYCODE列表

    电话键 键名 描述 键值   KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOME 按键Home 3 KEYCODE_MENU 菜单键 82 KE ...

  7. how to keep impact-crusher in good condition

    how to keep impact-crusher in good condition Why we have to maintenance impact crusher? As we talked ...

  8. (转)Word快捷键大全 Word2013/2010/2007/2003常用快捷键大全

    Word对于我们办公来说,是不可缺少的办公软件,因为没有它我们可能无法进行许多任务.所以现在的文员和办公室工作的人,最基础的就是会熟悉的使用Office办公软件.在此,为提高大家Word使用水平,Wo ...

  9. 【好书推荐】《剑指Offer》之软技能

    俗话说,对于程序员来讲,每年都应该出去面试一下,看看自己的技术能力在外面处于什么水平.程序员在一个公司一个环境一个业务干得太久,很容易丧失学习的动力,获得的仅仅是从新手到熟手.当然,我相信在各行各业均 ...

  10. Oracle 取上周一到周末的sql

    -- 这样取的是 在一周内第几天,是以周日为开始的 select to_char(to_date('20131005','yyyymmdd'),'d') from dual; --结果:7 注释:20 ...