您的位置:首页 > 其它

hdu 5787 K-wolf Number(数位dp)

2016-08-05 15:18 393 查看


K-wolf Number

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 807 Accepted Submission(s): 300



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].

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

Output

For each test case output a line contains an integer.

Sample Input

1 1 2
20 100 5


Sample Output

1
72


Author

ZSTU

Source

2016 Multi-University Training Contest 5

【题意】每次输入l,r,k 三个数,求(l,r)区间中有多少个数满足(相邻k位数字都不相同)。

【分析】很明显的数位dp,我们可以枚举前k-1位数字,作为我们保存的状态,然后套数位dp。(注意前导0的情况)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>

#define F first
#define S second
#define mp make_pair
using namespace std;
typedef __int64 LL;
LL digit[100],dp[20][100010],k;//对于为什么存五位,有点bug,还没解决。
bool judge(LL d,LL s)
{
int x=k-1;
while(x--)
{
if(d==(s%10)) return false;
s/=10;
}
return true;
}
const int ten[] = {1, 10, 100, 1000, 10000, 100000};
LL get(LL d,LL s,bool first)
{
if(first&&!d) return 0;
if(!first)
{
//        LL sum=1;
//        for(int i=0; i<k-2; i++) sum*=10;
//        cout<<sum<<" "<<s<<endl;
//        while(s>=sum) s-=sum;
//        cout<<s<<endl;
return (s*10+d)%ten[k];
}
else
{
LL sum=0;
for(int i=0; i<k-1; i++)
{
sum=sum*10+d;
}
return sum;
}
}
LL dfs(LL cnt,LL s,bool e,bool first)//first 用来标记前导0
{
if(!cnt) return 1LL;
if(!e && ~dp[cnt][s])
return dp[cnt][s];
LL ret=0;
LL to=e?digit[cnt]:9;
for(LL d=0; d<=to; d++)
{
if(!judge(d,s)&&!first)
{
continue;
}
LL as=get(d,s,first);
ret+=dfs(cnt-1,as,e&&d==to,first&&!d);
}
if(!e) dp[cnt][s]=ret;
return ret;
}

LL calc(LL x)
{
memset(dp, -1, sizeof(dp));
LL cnt = 0;
while(x) digit[++cnt] = x % 10, x /= 10;
return dfs(cnt, 0, 1, 1);
}

//bool judge1(int x, int k)
//{
//    int cnt = 0;
//    for(; x; x /= 10) digit[++cnt] = x % 10;
//
//    for(int i = 1; i <= cnt; ++i)
//    {
//        bool ok = true;
//        for(int j = 1; j < k; ++j)
//        {
//            if(i - j >= 1 && digit[i] == digit[i - j])
//            {
//                ok = false;
//                break;
//            }
//        }
//        if(!ok) return false;
//    }
//    return true;
//}
//int sum[1000005];
int main()
{
//    for(k = 1; k <= 5; ++k)
//    {
//        for(int i = 1; i <= 100000; ++i)
//        {
//            sum[i] = sum[i - 1] + judge1(i, k);
//            if(sum[i]  + 1!= calc(i))
//            {
//                printf("%d: %d %d %d\n", i, sum[i] + 1, calc(i), k);
//                printf("WA\n");
//                break;
//            }
//        }
//    }
LL l,r;
while(~scanf("%I64d%I64d%I64d",&l,&r,&k))
{
printf("%I64d\n",calc(r)-calc(l-1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: