zoukankan      html  css  js  c++  java
  • SP10606 BALNUM

    题意

    一个数被称为是平衡的数当且仅当对于所有出现过的数位,偶数出现奇数次,奇数出现偶数次。给定A,B,请统计出[A,B]内所有平衡数的个数。

    1<=A<=B<=10^19

    题解

    一开始没看懂题,把奇数看成整体,偶数看成整体了。

    后来看了题解才知道是0,1,2....这些数如果出现就要满足条件。

    当然最莽夫的方法就是把每一个数都开一维。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    #define ll long long
    int t;
    ll l,r;
    int len,num[25],pos[15];
    ll f[20][2][3][3][3][3][3][3][3][3][3][3];
    
    ll dfs(int s,bool qdl,bool lim,int pos0,int pos1,int pos2,int pos3,int pos4,int pos5,int pos6,int pos7,int pos8,int pos9){
      if(!s) return pos0&&pos1^1&&pos2&&pos3^1&&pos4&&pos5^1&&pos6&&pos7^1&&pos8&&pos9^1;
      if(!lim&&f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9]!=-1)
        return f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9];
      int mx=lim ? num[s] : 9 ;
      ll ret=0;
      for (int i=0;i<=mx;++i){
      switch(i){
        case 0: ret+=dfs(s-1,qdl,lim&&i==mx,qdl ? pos0 : pos0==2 ? 1 : !pos0,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9); break;
        case 1: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1==2?1:!pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9); break;
        case 2: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2==2?1:!pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9); break;
        case 3: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3==2?1:!pos3,pos4,pos5,pos6,pos7,pos8,pos9); break;
        case 4: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4==2?1:!pos4,pos5,pos6,pos7,pos8,pos9); break;
        case 5: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4,pos5==2?1:!pos5,pos6,pos7,pos8,pos9); break;
        case 6: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4,pos5,pos6==2?1:!pos6,pos7,pos8,pos9); break;
        case 7: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4,pos5,pos6,pos7==2?1:!pos7,pos8,pos9); break;
        case 8: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8==2?1:!pos8,pos9); break;
        case 9: ret+=dfs(s-1,false,lim&&i==mx,pos0,pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9==2?1:!pos9); break;
        }
      }
      if(!lim) f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9]=ret;
      return ret;
    }
    
    ll cx(ll x){
      len=0;
      while(x){
        num[++len]=x%10;
        x/=10;
      }
      return dfs(len,true,true,2,2,2,2,2,2,2,2,2,2);
    }
    
    int main(){
      memset(f,-1,sizeof(f));
      scanf("%d",&t);
      while(t--){
        scanf("%lld%lld",&l,&r);
        printf("%lld
    ",cx(r)-cx(l-1));
      }
    }
    View Code

     这种写法还挺好看,就是有点长。

    最后的判断如果是2的话会满足条件所以可以。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    #define ll long long
    int t;
    ll l,r;
    int len,num[25],pos[15];
    ll f[20][2][3][3][3][3][3][3][3][3][3][3];
    
    ll dfs(int s,bool qdl,bool lim,int pos0,int pos1,int pos2,int pos3,int pos4,int pos5,int pos6,int pos7,int pos8,int pos9){
      if(!s) return pos0&&pos1^1&&pos2&&pos3^1&&pos4&&pos5^1&&pos6&&pos7^1&&pos8&&pos9^1;
      if(!lim&&f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9]!=-1)
        return f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9];
      int mx=lim ? num[s] : 9 ;
      ll ret=0;
      for (int i=0;i<=mx;++i)
       ret+=dfs(s-1,qdl&&!i,lim&&i==mx,!i&&!qdl?(pos0+1)&1:pos0,i==1?(pos1+1)&1:pos1,i==2?(pos2+1)&1:pos2,i==3?(pos3+1)&1:pos3,i==4?(pos4+1)&1:pos4,i==5?(pos5+1)&1:pos5,i==6?(pos6+1)&1:pos6,i==7?(pos7+1)&1:pos7,i==8?(pos8+1)&1:pos8,i==9?(pos9+1)&1:pos9);
      if(!lim) f[s][qdl][pos0][pos1][pos2][pos3][pos4][pos5][pos6][pos7][pos8][pos9]=ret;
      return ret;
    }
    
    ll cx(ll x){
      len=0;
      while(x){
        num[++len]=x%10;
        x/=10;
      }
      return dfs(len,true,true,2,2,2,2,2,2,2,2,2,2);
    }
    
    int main(){
      memset(f,-1,sizeof(f));
      scanf("%d",&t);
      while(t--){
        scanf("%lld%lld",&l,&r);
        printf("%lld
    ",cx(r)-cx(l-1));
      }
    }
    View Code

    还可以弄成一行。

    正解好像是弄三进制状压

  • 相关阅读:
    首页三级菜单显示
    mysql引擎(转)
    nginx配置虚拟主机
    macos10.9 配置nginx,php-fpm
    排序算法 java实现
    Struts清楚session方法
    No result defined for action and result input的错误
    java throw throws
    try catch finally中return语句与非return语句的执行顺序问题
    java操作Excel
  • 原文地址:https://www.cnblogs.com/sto324/p/11391331.html
Copyright © 2011-2022 走看看