XHXJ's LIS -數位DP-NlogN-LIS
阿新 • • 發佈:2018-12-13
- S^(1<<i)把第i位變為0其餘的保持不變
-
#include<bits/stdc++.h> using namespace std; #define ll long long #define maxn 1<<11 ll dp[25][maxn][15],l,r; int k,a[25],t; int getsum(int num) { int cnt=0; while(num) { num&=(num-1); cnt++; } return cnt; } int updata (int num,int ind) { for(int i=ind; i<=9; i++) if(num&(1<<i)) return (num^(1<<i))|(1<<ind); return num|(1<<ind); } ll dfs(int len,int num,bool limit) { if(len==0) return getsum(num)==k; if(!limit&&dp[len][num][k]>=0) return dp[len][num][k]; int up=(limit?a[len]:9); ll ans=0; for(int i=0; i<=up; i++) ans+=dfs(len-1,num==0&&i==0?0:updata(num,i),limit&&i==up); if(!limit) dp[len][num][k]=ans; return ans; } ll solve(ll x) { int len=0; while(x) { a[++len]=x%10; x/=10; } return dfs(len,0,1); } int main() { scanf("%d",&t); memset(dp,-1,sizeof(dp)); for(int T=1; T<=t; T++) { scanf("%lld%lld%d",&l,&r,&k); printf("Case #%d: %lld\n",T,solve(r)-solve(l-1)); } return 0; }