SGU 390-Tickets(数位dp)

时间:2021-01-23 00:39:16

题意:有标号l-r的票,要给路人发,当给的票的编号的各数位的总和(可能一个人多张票)不小k时,才开始发给下一个人,求能发多少人。

分析:这个题挺难想的,参考了一下题解,dp[i][sum][left] 长度i 当前数位和sum  前一子树剩余的和

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll l,r;
int k,lbit[],rbit[],used[][][];
struct node{
ll num,left;//num能发的人数、left前一子树剩余和
node(ll num = , ll left = ) : num(num), left(left) {}
node operator += (node b)
{
num += b.num;
left = b.left;
return *this;
}
}dp[][][];
node dfs(int i,int sum,int left,int le,int re){
if(i==){
if(sum+left>=k)
return node(,);
return node(,sum+left);
}
if(used[i][sum][left]&&!le&&!re)
return dp[i][sum][left];
int ll=le?lbit[i]:;
int rr=re?rbit[i]:;
node a(,left);
for(int v=ll;v<=rr;++v){
a+=dfs(i-,sum+v,a.left,le&&(v==ll),re&&(v==rr));
}
if(!le&&!re){
dp[i][sum][left]=a;
used[i][sum][left]=;
}
return a;
}
void solve(){
memset(used,,sizeof(used));
memset(dp,,sizeof(dp));
int len=;
while(r){
rbit[++len]=r%;
r/=;
lbit[len]=l%;
l/=;
}
node t=dfs(len,,,,);
printf("%I64d\n",t.num);
}
int main()
{
scanf("%I64d%I64d%d",&l,&r,&k);
solve();
return ;
}