ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Count Numbers

2019-08-06 10:35:30  阅读:187  来源: 互联网

标签:Count cnt int sum 1000000 Numbers test ll


题目描述

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;
}

 

标签:Count,cnt,int,sum,1000000,Numbers,test,ll
来源: https://blog.csdn.net/weixin_41370251/article/details/81940106

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有