• HDU 6156 Palindrome Function (数位DP)


    题意:给定上 l,r,a, b,让你求从 l 到 r 这个区间内的在a进制到b进制如果是回文数就加上该进制的大小,如果不是,那么就加上1.

    析:数位DP。

    dp[i][j][k] 表示 i 进制下,前 j 位,回文串的长度是k有多少个,然后统计完后,再乘以进制即可,最后再加上那些不是回文。

    但是好像跑的有点慢,3700ms多,不知道什么情况。。。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 63248 + 100;
    const int mod = 1e9;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
        return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    LL dp[40][40][40];
    int num[40];
    int a[40];
    
    LL dfs(int x, int pos, int last, bool is, bool ok){
      if(!pos)  return 1;
      LL &ans = dp[x][pos][last];
      if(!is && !ok && ans >= 0)  return ans;
    
      LL res = 0;
      int n = ok ? a[pos] : x-1;
      for(int i = 0; i <= n; ++i){
        if(is){
          num[last] = i;
          res += dfs(x, pos-1, last-(i == 0), i == 0, i == n && ok);
        }
        else{
          int m = (last+1) / 2;
          if(last&1){
            if(pos >= m){
              num[pos] = i;
              res += dfs(x, pos-1, last, is && i == 0, i == n && ok);
            }
            else if(i == num[2*m-pos])
              res += dfs(x, pos-1, last, is && i == 0, ok && i == n);
          }
          else{
            if(pos > m){
              num[pos] = i;
              res += dfs(x, pos-1, last, is && i == 0, i == n && ok);
            }
            else if(i == num[last+1-pos])
              res += dfs(x, pos-1, last, is && i == 0, i == n && ok);
          }
        }
      }
    
      if(!ok && !is)  ans = res;
      return res;
    }
    
    LL solve(int n, int x, int y){
      LL ans = 0;
      LL sum = 0;
      for(int i = x; i <= y; ++i){
        int xx = n;
        int len = 0;
        while(xx){
          a[++len] = xx % i;
          xx /= i;
        }
        LL tmp = dfs(i, len, len, 1, 1);
        sum += tmp;
        ans += tmp * i;
      }
      ans += (LL)(y-x+1) * n - sum;
      return ans;
    }
    
    inline bool scan(int &ret){
      char c;
      if(c=getchar(),c==EOF)  return 0;
      while(c<'0'&&c>'9')c=getchar();
      ret=c-'0';
      while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0');
      return true;
    }
    
    int main(){
      ms(dp, -1);
      int T;  scan(T);
      for(int kase = 1; kase <= T; ++kase){
        int a, b, l, r;
        scan(l); scan(r);  scan(a);  scan(b);
        printf("Case #%d: %I64d
    ", kase, solve(r, a, b) - solve(l-1, a, b));
      }
      return 0;
    }
    

      

  • 相关阅读:
    iOS汇编系列-汇编入门
    C开发系列-指针
    iOS开发系列-LLVM、Clang
    java开发系列-Http协议
    iOS开发系列-SQLite
    iOS逆向系列-theos
    <Java><类加载机制><反射>
    <Java><!!!><面试题>
    <Java><修饰符>
    <Java><类与对象><OOP>
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7397649.html
Copyright © 2020-2023  润新知