Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation of x is pairwised different.
Given (L,R,K), please count how many K-wolf numbers in range of L,RL,R .
Input
The input contains multiple test cases. There are about 10 test cases.
Each test case contains three integers L, R and K.
1≤L≤R≤1e18
2≤K≤52≤K≤5
Output
For each test case output a line contains an integer.
Sample Input
1 1 2 20 100 5
Sample Output
1 72
问有多少个数,任意连续k位都不相同
记一下前k-1位,有没有前导零
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 LL n,len,l,r,k; 26 LL f[20][2300][2]; 27 int zhan[110]; 28 int d[110]; 29 inline LL dfs(int now,int stat,int lead,int fp) 30 { 31 if (now==1)return 1; 32 if (!fp&&f[now][stat][lead]!=-1)return f[now][stat][lead]; 33 LL ans=0,mx=fp?d[now-1]:9; 34 int lst=0; 35 for(int i=now;i<=min(now+k-2,len);i++)if (zhan[i]!=-1)lst|=1<<zhan[i]; 36 for (int i=0;i<=mx;i++) 37 { 38 if (lst&(1<<i))continue; 39 if (i==0&&lead&&now-1!=1)zhan[now-1]=-1;else zhan[now-1]=i; 40 if (zhan[now-1]!=-1)lst|=(1<<i); 41 ans+=dfs(now-1,lst,lead&&i==0&&now-1!=1,fp&&(i==d[now-1])); 42 if (zhan[now-1]!=-1)lst-=(1<<i); 43 } 44 if (!fp)f[now][stat][lead]=ans; 45 return ans; 46 } 47 inline LL calc(LL x) 48 { 49 if (!x)return 1; 50 LL xxx=x; 51 len=0; 52 while (xxx) 53 { 54 d[++len]=xxx%10; 55 xxx/=10; 56 } 57 LL sum=0; 58 for (int i=0;i<=d[len];i++) 59 { 60 if (i==0&&len!=1)zhan[len]=-1;else zhan[len]=i; 61 if (zhan[len]!=-1)sum+=dfs(len,1<<zhan[len],zhan[len]==-1,i==d[len]); 62 else sum+=dfs(len,0,zhan[len]==-1,i==d[len]); 63 zhan[len]=-1; 64 } 65 return sum; 66 } 67 main() 68 { 69 while (~scanf("%lld%lld%lld",&l,&r,&k)) 70 { 71 memset(f,-1,sizeof(f)); 72 printf("%lld\n",calc(r)-calc(l-1)); 73 } 74 }