题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109
题目大意:
设x=a[0]a[1]a[2]....a[len-1],对任意a[2*i+1]大于a[2*i]和a[2*i+2]。
求L~R(1<=L<=R<=10^9)中有多少个满足条件的x。
题目思路:
第一道数位dp。
dfs(i,s,p,first,flag);
第i位(包括前导0),前一位数为s,p表示是否为峰顶,first表示是否是开头,flag表示是否是前缀。
代码:
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ll __int64 #define ls rt<<1 #define rs ls|1 #define lson l,mid,ls #define rson mid+1,r,rs #define middle (l+r)>>1 #define clr_all(x,c) memset(x,c,sizeof(x)) #define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1)) #define eps (1e-8) #define MOD 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #pragma comment(linker, "/STACK:102400000,102400000") template <class T> T _max(T x,T y){return x>y? x:y;} template <class T> T _min(T x,T y){return x<y? x:y;} template <class T> T _abs(T x){return (x < 0)? -x:x;} template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;} template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;} template <class T> void getmax(T& x,T y){x=(y > x)? y:x;} template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;} int TS,cas=1; const int M=10+5; int l,r; int num[M],len; int f[M][M][2]; int dfs(int i,int s,bool p,bool first,bool flag){ if(i==-1) return 1; if(!flag && ~f[i][s][p]) return f[i][s][p]; int res=0,u=(flag? num[i]:9); for(int j=0;j<=u;j++){ if(first && !j) res+=dfs(i-1,9,0,1,0); else if(p && j>=s) res+=dfs(i-1,j,p^1,0,flag&&(j==u)); else if(!p && j<=s) res+=dfs(i-1,j,p^1,0,flag&&(j==u)); } return flag? res:f[i][s][p]=res; } int cal(int n){ clr_all(f,-1); for(len=0;n;) num[len++]=n%10,n/=10; return dfs(len-1,9,0,1,1); } void run(){ int i,j; scanf("%d%d",&l,&r); printf("%d\n",cal(r)-cal(l-1)); } void preSof(){ } int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); //run(); //while((~scanf("%d",&n))) run(); for(scanf("%d",&TS);cas<=TS;cas++) run(); return 0; }
作者:gotoac 发表于2013-4-27 15:52:32 原文链接
阅读:33 评论:0 查看评论