您的位置:首页 > 其它

2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325

2016-10-05 18:49 435 查看
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <queue>
#define Pi acos(-1.0)
using namespace std;

typedef long long  ll;
ll DP[22][11][5][3];
int DIG[22];
// type 1 不减 2 不加
ll ddp[11][22][5];
void getddp(){
memset(ddp, 0, sizeof(ddp));
for (int i = 0; i < 10; i++){
for (int j = 0; j < 3; j++){
ddp[i][0][j] = 1;
}
}
for (int i = 0; i < 19; i++){
ddp[9][i][1] = 1;
}
for (int i = 8; i >= 0; i--){
for (int j = 1; j < 19; j++){
for (int k = i; k < 10; k++){
ddp[i][j][1] += ddp[k][j-1][1];
}
}
}

for(int i = 0;i <= 9;i++){
for(int k = 0;k < 19;k++){
ddp[9-i][k][2] = ddp[i][k][1];
}
}

for(int i = 0;i <= 9;i++){
for(int k = 0;k < 19;k++){
ddp[i][k][0] = ddp[i][k][1] + ddp[i][k][2] - 1;
}
}

/*
for (int i = 0; i < 19; i++){
ddp[0][i][2] = 1;
}
for (int i = 1; i <= 9; i++){
for (int j = 1; j < 19; j++){
for (int k = i; k > 0; k--){
ddp[i][j][1] += ddp[k][j-1][1];
}
}
}*/

}
int    dfs(int pos,int pre,int limit,int type,int judge)
{

if(pos < 0)
return 1;

if(!limit && DP[pos][pre][type][judge] != -1)
return    DP[pos][pre][type][judge];

int    end = limit ? DIG[pos] : 9;
int    ret = 0;

if(judge == 0){
//	printf("?");
//ret += ddp[0][pos][1] +  ddp[0][pos][2] - 1;
ret += dfs(pos-1,0,0,type,0);
for(int i = 1;i <= end;i++){
ret += ddp[i][pos][1] + ddp[i][pos][2] - 1;
//	ret += dfs(pos-1,i,0,type,1);
}
}
else{
for(int i = 0;i <= end;i++){
if(type==1 && i <pre) continue;
if(type==2 && i >pre) continue;
if(type == 0)
{
int ntype;
if(i == pre){
ntype = type;
}
else if(i > pre){
ntype = 1;
}
else{
ntype = 2;
}
if(limit)
ret += dfs(pos-1,i,limit && (i == end),ntype,1);
else{
ret += ddp[i][pos][ntype];
}

}
else{
if(limit)
ret += dfs(pos-1,i,limit && (i == end),type,1);
else{
ret += ddp[i][pos][type];
}
}
}
}

if(!limit)
DP[pos][pre][type][judge] = ret;

return    ret;
}
ll solove(ll x){
int num = 0;
ll tem = x;
memset(DP,-1,sizeof DP);
while(x){
DIG[num] = x % 10;
num++;
x/=10;
}
ll ans = 0;
ans += dfs(num-2,0,0,0,0);

for(int i = 1;i <= DIG[num-1];i++){
//	printf("ans = %lld\n" , ans);
ans += dfs(num-2,i,i==DIG[num-1],0,1);

}

return ans;

}
int main(){
ll l,r;
int m;
scanf("%d", &m);
getddp();
while(m--){
scanf("%lld%lld",&l, &r);
printf("%lld\n", solove(r)-solove(l-1));
}

}


数位DP
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp