Problem Description
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,R].
Given (L,R,K), please count how many K-wolf numbers in range of [L,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≤5
Each test case contains three integers L, R and K.
1≤L≤R≤1e18
2≤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
直接数位dp
f[i][d1]……[dk]表示前i位前面各位分别为d1……dk的方案数
直接枚举位数转移即可
注意处理前导0和001023这种情况
可以考虑把前导0赋值成10
因为不想传递数组各种判断,就k=2 3 4 5分别写了一份程序
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[21];
long long f2[21][11][11];
int dd[11];
inline long long dfs2(int len,int d1,int d2,bool lim,bool lim2)
{
int i,j;
dd[1]=d1;dd[2]=d2;
bool flag=true;
for(i=1;i<=2;i++)
{
if(dd[i]==10)
continue;
for(j=i+1;j<=2;j++)
{
if(dd[i]==dd[j])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag)
return 0;
else if(lim2&&len==0)
return 0;
else if(len==0)
return 1;
if(!lim&&f2[len][d1][d2]!=0)
return f2[len][d1][d2];
int limit;
if(lim)
limit=a[len];
else
limit=9;
long long ans=0;
for(i=0;i<=limit;i++)
{
int dx=i;
if(dx==0&&lim2)
dx=10;
ans+=dfs2(len-1,d2,dx,lim&&i==a[len],lim2&&i==0);
}
if(!lim)
f2[len][d1][d2]=ans;
return ans;
}
inline long long cale2(long long x)
{
int len=0;
while(x!=0)
{
len++;
a[len]=x%(long long)10;
x=x/(long long)10;
}
int i,j;
long long ans=0;
ans+=dfs2(len,10,10,true,true);
return ans;
}
//--------------------------3-----------------------------------
long long f3[21][11][11][11];
inline long long dfs3(int len,int d1,int d2,int d3,bool lim,bool lim2)
{
int i,j;
dd[1]=d1;dd[2]=d2;dd[3]=d3;
bool flag=true;
for(i=1;i<=3;i++)
{
if(dd[i]==10)
continue;
for(j=i+1;j<=3;j++)
{
if(dd[i]==dd[j])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag)
return 0;
else if(lim2&&len==0)
return 0;
else if(len==0)
return 1;
if(!lim&&f3[len][d1][d2][d3]!=0)
return f3[len][d1][d2][d3];
int limit;
if(lim)
limit=a[len];
else
limit=9;
long long ans=0;
for(i=0;i<=limit;i++)
{
int dx=i;
if(dx==0&&lim2)
dx=10;
ans+=dfs3(len-1,d2,d3,dx,lim&&i==a[len],lim2&&i==0);
}
if(!lim)
f3[len][d1][d2][d3]=ans;
return ans;
}
inline long long cale3(long long x)
{
int len=0;
while(x!=0)
{
len++;
a[len]=x%(long long)10;
x=x/(long long)10;
}
int i,j;
long long ans=0;
ans+=dfs3(len,10,10,10,true,true);
return ans;
}
//--------------------------4-----------------------------------
long long f4[21][11][11][11][11];
inline long long dfs4(int len,int d1,int d2,int d3,int d4,bool lim,bool lim2)
{
int i,j;
dd[1]=d1;dd[2]=d2;dd[3]=d3;dd[4]=d4;
bool flag=true;
for(i=1;i<=4;i++)
{
if(dd[i]==10)
continue;
for(j=i+1;j<=4;j++)
{
if(dd[i]==dd[j])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag)
return 0;
else if(lim2&&len==0)
return 0;
else if(len==0)
return 1;
if(!lim&&f4[len][d1][d2][d3][d4]!=0)
return f4[len][d1][d2][d3][d4];
int limit;
if(lim)
limit=a[len];
else
limit=9;
long long ans=0;
for(i=0;i<=limit;i++)
{
int dx=i;
if(dx==0&&lim2)
dx=10;
ans+=dfs4(len-1,d2,d3,d4,dx,lim&&i==a[len],lim2&&i==0);
}
if(!lim)
f4[len][d1][d2][d3][d4]=ans;
return ans;
}
inline long long cale4(long long x)
{
int len=0;
while(x!=0)
{
len++;
a[len]=x%(long long)10;
x=x/(long long)10;
}
int i,j;
long long ans=0;
ans+=dfs4(len,10,10,10,10,true,true);
return ans;
}
//--------------------------5-----------------------------------
long long f5[21][11][11][11][11][11];
inline long long dfs5(int len,int d1,int d2,int d3,int d4,int d5,bool lim,bool lim2)
{
int i,j;
dd[1]=d1;dd[2]=d2;dd[3]=d3;dd[4]=d4;dd[5]=d5;
bool flag=true;
for(i=1;i<=5;i++)
{
if(dd[i]==10)
continue;
for(j=i+1;j<=5;j++)
{
if(dd[i]==dd[j])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag)
return 0;
else if(lim2&&len==0)
return 0;
else if(len==0)
return 1;
if(!lim&&f5[len][d1][d2][d3][d4][d5]!=0)
return f5[len][d1][d2][d3][d4][d5];
int limit;
if(lim)
limit=a[len];
else
limit=9;
long long ans=0;
for(i=0;i<=limit;i++)
{
int dx=i;
if(dx==0&&lim2)
dx=10;
ans+=dfs5(len-1,d2,d3,d4,d5,dx,lim&&i==a[len],lim2&&i==0);
}
if(!lim)
f5[len][d1][d2][d3][d4][d5]=ans;
return ans;
}
inline long long cale5(long long x)
{
int len=0;
while(x!=0)
{
len++;
a[len]=x%(long long)10;
x=x/(long long)10;
}
int i,j;
long long ans=0;
ans+=dfs5(len,10,10,10,10,10,true,true);
return ans;
}
int main()
{
long long l,r;
int k;
while(scanf("%I64d%I64d%d",&l,&r,&k)!=EOF)
{
if(k==2)
printf("%I64d\n",cale2(r)-cale2(l-1));
else if(k==3)
printf("%I64d\n",cale3(r)-cale3(l-1));
else if(k==4)
printf("%I64d\n",cale4(r)-cale4(l-1));
else if(k==5)
printf("%I64d\n",cale5(r)-cale5(l-1));
}
return 0;
}