zoukankan      html  css  js  c++  java
  • 【SPOJ 10606】Balanced Numbers

    BALNUM - Balanced Numbers

    no tags 

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if:

    1)      Every even digit appears an odd number of times in its decimal representation

    2)      Every odd digit appears an even number of times in its decimal representation

    For example, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.

    Given an interval [A, B], your task is to find the amount of balanced numbers in [A, B] where both A and B are included.

    Input

    The first line contains an integer T representing the number of test cases.

    A test case consists of two numbers A and B separated by a single space representing the interval. You may assume that 1 <= A <= B <= 1019 

    Output

    For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval

    Example

    Input:
    2
    1 1000
    1 9
    Output:
    147
    4

    题解:救救孩子吧,肝不动了,数位 DP

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define FOR(i,a,b) for(int i=a;i<=b;++i)
    #define clr(f,z) memset(f,z,sizeof(f))
    typedef long long LL;
    using namespace std;
    LL dp[20][60000];
    int bit[20],num[20];
    bool check(int x)
    {
      int pos=0;
      while(x)
      {
        num[pos++]=x%3;
        x/=3;
      }
      FOR(i,0,pos-1)
      if(i%2==0&&num[i]==2)return 0;
      else if(i%2==1&&num[i]==1)return 0;
      return 1;
    }
    int turn(int s,int x)
    { int pos=0;
      clr(num,0);
      while(s)
      {
        num[pos++]=s%3;
        s/=3;
      }
      if(num[x]==0)++num[x];
      else num[x]=3-num[x];
      int z=max(x,pos-1);
      s=0;
      for(int i=z;i>=0;--i)
        {
          s=s*3+num[i];
        }
        return s;
    }
    LL DP(int pp,int s,bool nozero,bool big)
    {
      if(pp==0)return check(s);
      if(big&&dp[pp][s]!=-1)return dp[pp][s];
      int kn=big?9:bit[pp];
      LL ret=0;
      FOR(i,0,kn)
      {
        ret+=DP(pp-1,(nozero||i!=0)?turn(s,i):0,nozero||i!=0,big||kn!=i);
      }
      if(big)dp[pp][s]=ret;
      return ret;
    }
    LL get(LL x)
    { int pos=0;
     
      while(x)
      {
        bit[++pos]=x%10;x/=10;
      }
      return DP(pos,0,0,0);
    }
    int main()
    {
      LL a,b;
      int cas;clr(dp,-1);
      while(~scanf("%d",&cas))
      {
        while(cas--)
        {
          scanf("%lld%lld",&a,&b);
          printf("%lld
    ",get(b)-get(a-1));
        }
      }
      return 0;
    }
  • 相关阅读:
    Django跨域问题
    DOM,jquery,vue
    from和modelform的用法和介绍
    元类的__call__和__new__方法的作用
    Django学习之路由分发和反向解析
    Django 自定义auth_user
    Django创建对象的create和save方法
    Flask--(登录注册)抽取视图函数
    Flask--(项目准备)--添加日志
    Flask--(项目准备)--框架搭建,配置文件抽取,业务逻辑抽取
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11249048.html
Copyright © 2011-2022 走看看