14 Mar 2013

Lucky Number_solved


/*

CODECHEF
PROBLEM CODE :  LUCKY2

*/

#include <stdio.h>
#include <string.h>
 
#define siz 1005
 
int len;
int q[siz][siz][4];
char A[siz], B[siz];
 
char S(int x)
{
if(!x) return 0;
for(;x%10==7||x%10==4; x/=10);
return !x;
}
 
int T(int i, int p, int s)
{
int j, res=0, ob;
if(i==-1) return S(p);
if(q[i][p][s]!=-1) return q[i][p][s];
for(j=(s&2)?-1:A[i]-1,ob=(s&1)?9:B[i]; ++j<=ob; res=(res+T(i-1,p+(j==7||j==4),s|(j<B[i])|((j>A[i])<<1)))%1000000007);
return q[i][p][s]=res;
}
 
main()
{
int fall, x, i, j;
char L[siz], U[siz];
for(i=-1; ++i<siz;)
 for(j=-1; ++j<siz; q[i][j][3]=-1);
for(scanf("%d",&fall); fall--; printf("%d\n",T(len-1,0,0)))
 {
 for(i=-!!scanf("%s %s",L,U),len=strlen(U),x=strlen(L); ++i<len; A[i]=B[i]=0);
 for(i=0,j=len-1; i<len; B[j--]=U[i++]-'0');
 for(i=x-1,j=0; i>=0; A[i--]=L[j++]-'0');
 for(i=-1; ++i<=len;)
  for(j=-1; ++j<=len; q[i][j][0]=q[i][j][1]=q[i][j][2]=-1);
 }
return 0;
} 

Categories: , , ,

0 comments:

Post a Comment

Copyright © UPgradeCODING | Powered by Blogger