Horse Races (数位dp)
来源:互联网 发布:淘宝整机烈士墙2016 编辑:程序博客网 时间:2024/06/14 00:54
Petya likes horse racing very much. Horses numbered from l to r take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. Anearly lucky number is an integer number that has at least two lucky digits the distance between which does not exceedk. Petya learned from some of his mates from Lviv that lucky digits are digits4 and 7. The distance between the digits is the absolute difference between their positions in the number of a horse. For example, ifk = 2, then numbers 412395497, 404, 4070400000070004007 are nearly lucky and numbers4, 4123954997, 4007000040070004007 are not.
Petya prepared t intervals [li, ri] and invented numberk, common for all of them. Your task is to find how many nearly happy numbers there are in each of these segments. Since the answers can be quite large, output them modulo1000000007 (109 + 7).
The first line contains two integers t andk (1 ≤ t, k ≤ 1000) — the number of segments and the distance between the numbers correspondingly. Nextt lines contain pairs of integers li andri (1 ≤ l ≤ r ≤ 101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space character.
Output t lines. In each line print one integer — the answer for the corresponding segment modulo1000000007 (109 + 7).
1 21 100
4
1 270 77
2
2 11 2080 100
00
题目大概:
找出给定区间内,4和7 或 4和4 或7和7 间隔小于k的数字的个数。
思路:
因为是只要存在便可,没要求数量,所以一般只需判断一次即可,以后遇到4和7,还是直接当什么也每遇到即可。
但是这个数据量大,用字符串输入,最后还要判断最小的数是不是符合条件,符合的话,要加上。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int mod=1000000007;int dp[1005][2010];int a[1005];int k;int sove(int pos,int ju,int limit){ if(pos==-1)return ju==0; if(!limit&&dp[pos][ju]!=-1)return dp[pos][ju]; int end=limit?a[pos]:9; int ans=0; for(int i=0;i<=end;i++) { int nju=(ju==0?0:ju+1); if(i==4||i==7) { if(ju<=k)nju=0; else nju=1; } ans=(ans+sove(pos-1,nju,limit&&i==end))%mod; } if(!limit)dp[pos][ju]=ans; return ans;}int go(char s[]){ int pos=0; int l=strlen(s); for(int i=l-1;i>=0;i--) { a[pos++]=s[i]-'0'; } return sove(pos-1,k+2,1);}int check(char s[]){ int ju=k+1; for(int i=0;s[i];i++) { if(ju==0)break; if(s[i]=='4'||s[i]=='7') { if(ju<=k)ju=0; else ju=1; } else ju++; } return ju==0;}int main(){ int t; scanf("%d%d",&t,&k); char s1[1005],s2[1005]; memset(dp,-1,sizeof(dp)); while(t--) { scanf("%s%s",s1,s2); int b=go(s2); int a=go(s1); if(check(s1))a--; b=((b-a)%mod+mod)%mod; printf("%d\n",b); } return 0;}
- Horse Races (数位dp)
- 数位dp--cf96e Horse races
- CodeForces 115E Linear Kingdom Races(线段树 + DP)
- hdu3652(数位DP)
- hdu3709(数位dp)
- codeforces55D(数位DP)
- hdu4734(数位DP)
- ural1057(数位dp)
- lightoj1205(数位DP)
- hdu4352(数位DP)
- hdu4507(数位DP)
- hdu4734(数位DP)
- poj2282(数位dp)
- hdu3652(数位dp)
- hdu_3555_Bomb(数位DP)
- hdu4734(数位DP)
- hdu2089(数位dp)
- 数位dp(hdu2089)
- 5.6
- dp之背包总结篇
- Java中的break和continue关键字使用总结
- NIO的非阻塞通信
- React Naive编写holle wrode
- Horse Races (数位dp)
- 经验总结-完整介绍Android Studio中Git的使用之Android Studio上传项目至GitHub(三)
- fastjson
- 字符串与字符数组的关系
- CentOS 7 巨大变动之 systemd 取代 SysV的Init
- 额。。。自加自减我都没理解透彻。。代码白敲了。。。。
- VS提示scanf不安全问题
- shell处理—文件汇总到一个文件里,用sqlldr装入数据库这些文件数据都少一个字段而字段内容为这个文件的名字
- python创建空列表