CodeForces - 628D (數位dp)
Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.
For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magicand 71 is 1-magic.
Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).
Input
The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement.
The second line contains positive integer a in decimal presentation (without leading zeroes).
The third line contains positive integer b in decimal presentation (without leading zeroes).
It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.
Output
Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.
Examples
Input
2 6 10 99
Output
8
Input
2 0 1 9
Output
4
Input
19 7 1000 9999
Output
6
題目大意:
求一個區間內某種數的個數,需要是m的倍數,並且奇數位不能包含d,偶數為必須包含d。數的位數是2000位。
思路:
首先是數位dp。
關於m的限制,直接便計算邊取模就行。d限制,需要分奇偶討論一下。
數的位數很大,需要用字串,這樣的話,閉區間,左邊的數減一不好計算,需要特判一下。
程式碼:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>
#include <ctime>
using namespace std;
#define ll long long
const int mod=1e9+7;
long long a[2200];
long long dp[2200][2100][2];
int m, id;
int l;
long long dfs(int pos, int sum, int jo,int limit)
{
if (pos == -1)
{
return !sum&&jo;
}
if (!limit&&dp[pos][sum][jo] != -1)return dp[pos][sum][jo];
long long sun = 0;
int end = limit ? a[pos] : 9;
for (int i = 0;i <= end;i++)
{
if ((l-pos)%2==0)
{
sun = (sun+dfs(pos - 1, (sum*10 + i) % m,jo&&(i==id), limit&&i == a[pos]))%mod;
}
else
{
sun = (sun+dfs(pos - 1, (sum*10 + i) % m, (jo&&!(i==id)),limit&&i == a[pos]))%mod;
}
}
if (!limit)dp[pos][sum][jo] = sun;
return sun;
}
long long go(char x[])
{
int pos = strlen(x);
l=pos;
memset(a,0,sizeof(a));
for(int i=0;i<pos;i++)
{
a[pos-i-1]=x[i]-'0';
}
return dfs(pos - 1, 0, 1,1);
}
char b[2200],c[2200];
int pan(char x[])
{
int pos = strlen(x);
int sum=0;
int flag=0;
int flag1=0;
for(int i=0;i<pos;i++)
{
int v=x[i]-'0';
sum=(sum*10+v)%m;
if(i%2==1)
{
if(v!=id)
{
flag=1;
break;
}
}
else
{
if(v==id)
{
flag1=1;
break;
}
}
}
if(!sum&&!flag&&!flag1)return 1;
else return 0;
}
int main()
{
scanf("%d%d", &m, &id);
scanf("%s%s", b, c);
memset(dp, -1, sizeof(dp));
printf("%lld\n", (go(c)-go(b)+pan(b)+mod)%mod);
return 0;
}
相關文章
- CodeForces 401D 數位DP
- 【數位dp】Beautiful numbers CodeForces - 55D
- [DP] 數位DP
- 數位 dp
- 【數位dp】學習
- 數位DP小記
- Codeforces 914C Travelling Salesman and Special Numbers:數位dp
- 學習筆記:數位dp筆記
- 數位DP 學習筆記筆記
- 【學習筆記】數位DP筆記
- codeforces 341C Iahub and Permutations(組合數dp)
- 1082. 數字遊戲 (數位DP)遊戲
- 演算法隨筆——數位DP演算法
- hdu5435 數位dp(大數的處理)
- codeforces 148 D 概率dp
- codeforces455A Boredom (裸DP)
- Codeforces 869C The Intriguing Obsession:組合數 or dpGUISession
- Codeforces 461B. Appleman and Tree[樹形DP 方案數]APP
- Codeforces 372B Counting Rectangles is Fun:dp套dp
- Codeforces 180C Letter:dp
- BZOJ 3329 Xorequ:數位dp + 矩陣快速冪矩陣
- Codeforces 895C Square Subsets:狀壓dp【組合數結論】
- Codeforces 158E Phone Talks:dp
- Codeforces 432D Prefixes and Suffixes:KMP + dpKMP
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】codeforces 837D Round Subset
- 【dp】CodeForces - 623B Array GCDGC
- 【dp】codeforces 830-A Office Keys
- codeforces 148 D Bag of mice(概率dp)
- [筆記]數位dp例題及詳解(更新中)筆記
- BZOJ3329: Xorequ(二進位制數位dp 矩陣快速冪)矩陣
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
- 【數位dp】(涉及到處理前導0問題)
- Codeforces Round #358 (Div. 2) D dp
- Codeforces 459E Pashmak and Graph:dp + 貪心
- Codeforces 148D Bag of mice (概率dp)
- 【數位dp入門】51nod 1009 數字1的數量
- Codeforces 893E Counting Arrays:dp + 線性篩 + 分解質因數 + 組合數結論