Count Numbers

题目描述

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

dp+矩阵快速冥,cnt[i]存数位和为i的方案数,sum[i]存数位和为i的整数的和;

cnt[i]=求和(cnt[i-j]){1<=j<=9},sum[i]=求和(10*sum[i-j]+j*cnt[i-j]){1<=j<=9}

因为sum数组中j从1到9走了一次,但是有两种乘积形式,所以我们的转移矩阵需要开18*18的。之后就是实现快速冥就行了

因为数据范围很大,需要用int128。

或者,使用java实现。

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p=1e9+7;
int add(int a,int b)
{
    return a+b>=p?a+b-p:a+b;
}
int mul(ll a,int b)
{
    return a*b%p;
}
struct Mat
{
    int v[18][18];
    Mat()
    {
        memset(v,0,sizeof(v));
    }
    void init()//单位矩阵
    {
        for(int i=0;i<18;i++)
        {
            v[i][i]=1;
        }
    }
};
Mat operator *(Mat x,Mat y)
{
    Mat z;
    for(int i=0;i<18;i++)
    {
        for(int j=0;j<18;j++)
        {
            if(x.v[i][j])
            {
                for(int k=0;k<18;k++)
                {
                    if(y.v[j][k])
                    {
                        z.v[i][k]=add(z.v[i][k],mul(x.v[i][j]%p,y.v[j][k]%p));
                    }
                }
            }
        }
    }
    return z;
}
Mat Matqmod(Mat a,__int128 b)
{
    Mat c;
    c.init();
    while(b)
    {
        if(b&1)
        {
            c=c*a;
        }
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    ll cnt[15]={0},sum[15]={0};
    cnt[0]=1;
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cnt[i]+=cnt[i-j];
            sum[i]+=10*sum[i-j]+j*cnt[i-j];
        }
    }
    Mat a,b;
    a.v[0][0]=10;
    for(int i=1;i<9;i++)
    {
        a.v[0][i]=10;
        a.v[i][i-1]=1;
    }
    for(int i=9;i<18;i++)
    {
        a.v[0][i]=i-8;
        a.v[9][i]=1;
    }
    for(int i=10;i<18;i++)
    {
        a.v[i][i-1]=1;
    }
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,m;
        scanf("%lld %lld %lld",&n,&m,&p);
        for(int i=0;i<9;i++)
        {
            b.v[i][0]=sum[9-i]%p;
        }
        for(int i=9;i<18;i++)
        {
            b.v[i][0]=cnt[18-i]%p;
        }
        __int128 now=n;
        for(int i=2;i<=m;i++)
        {
            now=now*(__int128)n;
        }
        if(now<=9)
        {
            printf("%lld\n",sum[now]%p);
            continue;
        }
        Mat c=Matqmod(a,now-9)*b;
        printf("%lld\n",c.v[0][0]);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值