Count SIN Numbers

题目链接:http://codeforces.com/group/gRkn7bDfsN/contest/211954/problem/D

数位DP

定义状态:dp[当前第i位数][当前位上的数字j][前i位数是否与给定数s相同][当前位上的数是否为谷值][前i位数所代表的数是否前导零]表示从零到长为i末尾数字为j的符合状态的数的个数。

状态转移方程:

当前位上的数为谷值且前导零时,下一位只能为零:

  dp[i+1][0][k&(0==s[i]-'0')][!m][n==1]+=dp[i][j][k][m][n];

否则可取下一位数为p,for p in range(l,r+1):

  dp[i+1][p][k&(p==s[i]-'0')][!m][n==1&&p==0]+=dp[i][j][k][m][n],其中l和r的值由前i位数是否与给定数s相同以及当前位上的数是否为谷值共同决定。

故A到B满足条件的数的个数=从零到B符合条件的数的个数-从零到A符合条件的数的个数+(A符合条件?1:0)

代码如下:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 using namespace std;
 6 typedef long long ll;
 7 ll dp[20][10][2][2][2],ans;
 8 string a,b;
 9 ll js(string s){
10     ll len=s.length();
11     memset(dp,0,sizeof(dp));
12     dp[0][0][1][1][1]=dp[0][0][1][0][1]=1LL;
13     for(int i=0;i<len;++i)
14     for(int j=0;j<=9;++j)
15     for(int k=0;k<2;++k)
16     for(int m=0;m<2;++m)
17     for(int n=0;n<2;++n){
18         if(dp[i][j][k][m][n]){
19             int l=0,r=(k==1?s[i]-'0':9);
20             if(n!=1){
21                 if(m==0)r=min(r,j-1);
22                 else l=j+1;
23             }
24             if(0<=l&&r<=9&&l<=r){
25                 if(m==1&&n==1){
26                     dp[i+1][0][k&(0==s[i]-'0')][!m][n==1]
27                             +=dp[i][j][k][m][n];
28                 }else{
29                     for(int p=l;p<=r;++p)
30                         dp[i+1][p][k&(p==s[i]-'0')][!m][n==1&&p==0]
31                             +=dp[i][j][k][m][n];
32                 }
33             }
34         }
35     }
36     ll t=0;
37     for(int j=0;j<=9;++j)
38     for(int k=0;k<2;++k)
39     for(int m=0;m<2;++m)
40     for(int n=0;n<2;++n)
41         t+=dp[len][j][k][m][n];
42     return t-2;
43 }
44 int main(void){
45     cin>>a>>b;
46     ans=js(b)-js(a)+1;
47     for(int i=1;i<(int)a.length();++i)
48     if((i%2==1&&a[i]<=a[i-1])||(i%2==0&&a[i]>=a[i-1])){
49         ans--;
50         break;
51     }
52     cout<<ans<<endl;
53 }
54         

转载于:https://www.cnblogs.com/barrier/p/6412071.html

Count SIN Numbers相关推荐

  1. 力扣1523.在区间范围内统计奇数数目Count Odd Numbers in an Interval Range

    题目描述 给你两个非负整数 low 和 high .请你返回 low 和 high 之间(包括二者)奇数的数目. 示例 示例 1:输入:low = 3, high = 7 输出:3 解释:3 到 7 ...

  2. leetcode -- 357. Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. 357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...

  5. LeetCode 357. Count Numbers with Unique Digits

    357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique ...

  6. pat1100. Mars Numbers (20)

    1100. Mars Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People o ...

  7. count在python中的作用,Python中这个count用法是啥

    Python中这个count用法是啥意思 count 在这里只是一个变量名,你把它换成adict就很好理解了:关于python的一段代码,谁给具体解析一下,特别是 count+=number,在下初中 ...

  8. 1100 Mars Numbers

    1100 Mars Numbers People on Mars count their numbers with base 13: Zero on Earth is called "tre ...

  9. PAT 1100. Mars Numbers (20)

    People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...

最新文章

  1. 人类基因组最后一块拼图完成!Science罕见6篇连发
  2. ibm java_IBM i 上Java 虚拟机简介
  3. C++ 智能指针最佳实践源码分析
  4. php 图片 3d旋转图片,html5实现图片的3D旋转效果
  5. mysql 任务计划 /etc/cron.d_Linux /etc/cron.d增加定时任务
  6. 程序编写经验教训_编写您永远都不会忘记的有效绩效评估的经验教训。
  7. mysql启动找不见socket
  8. Theano学习笔记:Theano的艰辛安装体验
  9. mathtype 公式分节隐藏
  10. VS2008+QT+CYAPI开发USB程序问题
  11. matlab中xlsread无法打开文件,Matlab xlsread打开文件并清理
  12. 崇明东平森林公园一日游
  13. 阿波罗服务器的投资项目,阿波罗未来产业城调整规划范围 将重点打造“两轴一片”空间格局...
  14. [风铃开发系列]IView动态菜单配置
  15. tomcat启动找不到jer报错
  16. 广州土地市场有所回暖?分析广州房价,看看有什么新发现
  17. javascript 实现类似超市排队结账算法,求最少时间
  18. 受面板跌价拖累 韩国LG显示器四季度净利创近两年新低
  19. 团队作业-Beta冲刺(2)
  20. 利用Python turtle库实现六角形、无角正方形的绘制

热门文章

  1. 求正负数交互序列的和的MFC版
  2. Ubuntu下搜狗输入法乱码(二)
  3. java数组之binarySearch查找
  4. MySQL服务安装和可视化工具安装
  5. 【探路者】贪吃蛇β发布展示(视频展示)
  6. select resharper shortcuts scheme
  7. Linux搭建FTP
  8. CentOS系统bash: groupadd: command not found问题
  9. mysql 排序后 下一条记录_什么是MySQL InnoDB 二级索引的排序?|附实例详解
  10. 自旋表达的到底是什么?