• [POJ 3208] Apocalypse Someday


    [题目链接]

              http://poj.org/problem?id=3208

    [算法]

            数位DP

    [代码]

            

    #include <algorithm>  
    #include <bitset>  
    #include <cctype>  
    #include <cerrno>  
    #include <clocale>  
    #include <cmath>  
    #include <complex>  
    #include <cstdio>  
    #include <cstdlib>  
    #include <cstring>  
    #include <ctime>  
    #include <deque>  
    #include <exception>  
    #include <fstream>  
    #include <functional>  
    #include <limits>  
    #include <list>  
    #include <map>  
    #include <iomanip>  
    #include <ios>  
    #include <iosfwd>  
    #include <iostream>  
    #include <istream>  
    #include <ostream>  
    #include <queue>  
    #include <set>  
    #include <sstream>  
    #include <stdexcept>  
    #include <streambuf>  
    #include <string>  
    #include <utility>  
    #include <vector>  
    #include <cwchar>  
    #include <cwctype>  
    #include <stack>  
    #include <limits.h> 
    using namespace std;
    
    int T;
    long long n;
    long long f[25][5];
    
    inline void dp()
    {
            int i,j;
            f[0][0] = 1;
            for (i = 0; i <= 20; i++)
            {
                    for (j = 0; j < 3; j++)
                    {
                            f[i + 1][j + 1] += f[i][j];
                            f[i + 1][0] += f[i][j] * 9; 
                    }
                    f[i + 1][3] += f[i][3] * 10;
            }
            
    }
    inline void calc(long long n)
    {
            int i,j,k,len,now;
            long long sum;
            for (len = 3; f[len][3] < n; len++);
            now = 0;  
            sum = 0;
            for (i = len; i >= 1; i--)
            {
                    for (j = 0; j <= 9; j++)
                    {
                            if (j == 6 || now == 3)
                            {
                                    sum = f[i - 1][3];
                                    for (k = max(3 - now - (j == 6),0); k < 3; k++) sum += f[i - 1][k];
                                    if (sum < n) n -= sum;
                                    else 
                                    {
                                            printf("%d",j);
                                            if (now < 3 && j == 6) now++;
                                            break;
                                    }
                            } else
                            {
                                    if (f[i - 1][3] < n)
                                            n -= f[i - 1][3];
                                    else 
                                    {
                                            now = 0;
                                            printf("%d",j);
                                            break;
                                    }
                            }
                    }    
          }                               
            printf("
    ");                                                                                                                                                                                                                                                                                                                                                                                       
    }
    
    int main() 
    {
            
            dp();
            scanf("%d",&T);
            while (T--)
            {
                    scanf("%lld",&n);
                    calc(n);    
            }
            
            return 0;
        
    }
  • 相关阅读:
    基于RockMq 实现分布式事务
    开机启动脚本编写
    JS 校验笔记
    git 小计
    EasyExcel简单导入示例
    vue的rule中使用validator(异步请求)验证
    字符串、数值、布尔值、函数参数的解构赋值以及圆括号问题
    对象解构赋值
    数组的解构赋值与yield
    window10的cmd重置子系统ubuntu用户密码以及ubuntu设置密码切换用户
  • 原文地址:https://www.cnblogs.com/evenbao/p/9348212.html
Copyright © 2020-2023  润新知