CF1073E Segment Sum 解题报告

CF1073E Segment Sum

题意翻译

给定\(K,L,R\),求\(L~R\)之间最多不包含超过\(K\)个数码的数的和。

\(K\le 10,L,R\le 10^{18}\)



数位dp

\(dp_{i,s}\)前\(i\)位出现集合\(s\)的贡献和和出现次数

然后记忆化的时候转移一下就行了

然而写的时候还是怪麻烦的



Code:

#include <cstdio>
#include <cstring>
#define ll long long
const int mod=998244353;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
#define mul(a,b) (1ll*(a)*(b)%mod)
int po[20],bit[20],len,k;
struct node
{
    int val,cnt;
    node(){}
    node(int v,int c){val=v,cnt=c;}
    node friend operator +(node a,node b){return node(add(a.val,b.val),add(a.cnt,b.cnt));}
}dp[20][1<<10];
node dfs(int pos,int sta,int lead,int lim)//前导0和最高位限制
{
    int cnt=0;
    for(int i=0;i<10;i++) cnt+=sta>>i&1;
    if(cnt>pos) return node(0,0);
    if(!pos) return node(0,1);
    if(!lim&&!lead&&~dp[pos][sta].val) return dp[pos][sta];
    node ret=node(0,0),bee;
    if(lead) ret=ret+dfs(pos-1,sta,lead,lim&&!bit[pos]);
    else if(sta&1) ret=ret+dfs(pos-1,sta,lead,lim&&!bit[pos])+dfs(pos-1,sta^1,lead,lim&&!bit[pos]);
    for(int i=1,up=lim?bit[pos]:9;i<=up;i++)
        if(sta>>i&1)
        {
            bee=dfs(pos-1,sta,0,lim&&i==up)+dfs(pos-1,sta^(1<<i),0,lim&&i==up);
            ret=ret+bee;
            ret.val=add(ret.val,mul(bee.cnt,mul(i,po[pos-1])));
        }
    return !lim&&!lead?dp[pos][sta]=ret:ret;
}
int cal(ll x)
{
    len=0;while(x) bit[++len]=x%10,x/=10;
    memset(dp,-1,sizeof dp);int ans=0;
    for(int s=0;s<1<<10;s++)
    {
        int cnt=0;
        for(int i=0;i<10;i++) cnt+=s>>i&1;
        if(cnt<=k) ans=add(ans,dfs(len,s,1,1).val);
    }
    return ans;
}
int main()
{
    ll l,r;
    scanf("%lld%lld%d",&l,&r,&k);
    po[0]=1;for(int i=1;i<=18;i++) po[i]=mul(po[i-1],10);
    printf("%d\n",add(cal(r),mod-cal(l-1)));
    return 0;
}


2019.2.9

原文地址:https://www.cnblogs.com/ppprseter/p/10358066.html

时间: 2024-10-03 15:48:20

CF1073E Segment Sum 解题报告的相关文章

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

USACO Section2.3 Zero Sum 解题报告 【icedream61】

zerosum解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N. 把1到N依次写出,每相邻两个数字中间加上一个字符,三选一:'+','-',' '. 如此,便可形成很多表达式,把其中计算结果为0的按字典序输出.[数据范围] 3<=N<

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Winter-1-D Max Sum 解题报告及测试数据

Time Limit:1000MS Memory Limit:32768KB Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The f

LeetCode: Path Sum 解题报告

Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22,              5             / \        

LeetCode: Minimum Path Sum 解题报告

Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. SOLUTION 1: 相当基础

Winter-1-B Sum 解题报告及测试数据

Time Limit:500MS Memory Limit:32768KB Description ?Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The input will consist of a series of integers n, one inte

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Ex

【LeetCode】Minimum Path Sum 解题报告

[题目] Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [思路] 求从左上角到右下角的最小路径值,典型的动态规划