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. 2017-11-7 NOIP模拟赛

    1.数学老师的报复 #include<iostream> #include<cstdio> using namespace std; int cnt; ]; long long ...

  2. springBoot2.0 配置@ControllerAdvice 捕获异常统一处理

    一.前言 基于上一篇 springBoot2.0 配置shiro实现权限管理 这一篇配置 异常统一处理 二.新建文件夹:common,param 三.返回结果集对象 1.ResultData.java ...

  3. [USACO1.4]等差数列 Arithmetic Progressions

    题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...

  4. MySQL服务器与MySQL57服务器区别与不同处在哪里,他们各自的领域范围,能不能同时启动服务?

    安装了MySQL-5.7.18.0版本数据库,版本中包含了MySQL Workbench可视化试图工具,在服务列表栏中会有MySQL的两个服务器:如果启动第一项MySQL服务器就只能操作数据库,外界不 ...

  5. crawlspider的源码学习

    Spider基本上能做很多事情了,但是如果你想爬取全站的话,可能需要一个更强大的武器.CrawlSpider基于Spider,但是可以说是为全站爬取而生.CrawlSpiders是Spider的派生类 ...

  6. Helvetic Coding Contest 2016 online mirror B1

    Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...

  7. Codeforces Round #365 (Div. 2) B

    Description Little Mishka is a great traveller and she visited many countries. After thinking about ...

  8. 强制更新客户端Silverlight XAP 文件

    在发布小程序更新的时候访问的总是原来的程序,猜想应该是缓存的原因.在网上查找方法 <div id="silverlightControlHost"> <objec ...

  9. Sum vs XOR

    https://www.hackerrank.com/contests/hourrank-13/challenges/arthur-and-coprimes 要求找出所有x <= n   x + ...

  10. CheatEngine-内存修改

    0.备注+待完成 //备注 a). 如果有方括号,就是说CE认为找 到了数值的指针了 //待完成 a). 自动导出外挂 b). 菜单栏中"表单"下的lua是做什么用的 c). CE ...