codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments

Hill Number

Time Limits:  5000 MS   Memory Limits:  200000 KB

64-bit interger IO format:  %lld   Java class name:  Main

Description

A Hill Number is a number whose digits possibly rise and then possibly fall, but never fall and then rise.

• 12321 is a hill number.
• 101 is not a hill number.
• 1111000001111 is not a hill number.

Given an integer n, if it is a hill number, print the number of hill numbers less than it. If it is not a hill number, print -1.

Input

Input will start with a single line giving the number of test cases. Each test case will be a single positive integer on a single line, with up to 70 digits. The result will always fit into a 64-bit long.

Output

For each test case, print -1 if the input is not a hill number. Print the number of hill numbers less than the input value if the input value is a hill number.

Sample Input

5
10
55
101
1000
1234321

Output for Sample Input

10
55
-1
715
94708

https://open.kattis.com/problems/hillnumbers 可以在这里交题目

给你一个数,问你这个是不是山峰数。所谓山峰数,就是类似于12321这种,但是可以都一样。如果是山峰数,求出比他小的所有的山峰数个数。

思路:

数字很大,一看就是数位dp。

dp[i][k][w],表示第i位,为数字k的时候,状态是w的个数。w有3中状态,一种递增,一种递减,一种先递增,后递减。

dp[i][k][w] = sum(dp[i-1][j][p]);

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 100;
ll dp[MAXN][10][5];
int digit[MAXN];
char s[MAXN];
int ok(char s[])
{
    int len = strlen(s);
    int flag = 0;
    for(int i = 1; i < len; i++){
        if(s[i] > s[i-1] && flag){
            return 0;
        }
        else if(s[i] < s[i-1] && !flag){
            flag = 1;
        }
    }
    return 1;
}
ll dfs(int len,int w,int ismax,int pa)
{
    if(len == 0)return 1;
    if(!ismax && dp[len][pa][w]){//第i位 为pa数字 状态为w的时候的个数
        return dp[len][pa][w];
    }
    ll ans = 0;
    ll fans = 0;
    int maxv = ismax ? digit[len] : 9;
    for(int i = 0; i <= maxv; i++){
        if(w == 1){
            if(i >= pa){
                ans += dfs(len-1,1,ismax && i == maxv,i);
            }
            else {
                ans += dfs(len-1,3,ismax && i == maxv,i);
            }
        }
        else if(w == 2){
            if(i > pa)continue;
            else {
                ans += dfs(len-1,2,ismax && i == maxv,i);
            }
        }
        else if(w == 3) {
            if(i > pa)continue;
            else {
                ans += dfs(len-1,3,ismax && i == maxv,i);
            }
        }
    }
    if(!ismax)dp[len][pa][w] = ans;
    return ans;
}
void solve()
{
    if(!ok(s)){
        printf("-1\n");
        return ;
    }
    int len = 0;
    memset(dp,0,sizeof(dp));
    for(int i = strlen(s) - 1; i >= 0; i--){
        digit[++len] = s[i] - ‘0‘;
    }
    printf("%lld\n",dfs(len,1,1,-1) - 1);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        solve();
    }
    return 0;
}
时间: 2024-12-29 07:22:35

codeforces Hill Number 数位dp的相关文章

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

CodeForces - 55D(数位dp,离散化)

题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue wit

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

fzu 2109 Mountain Number 数位DP

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109 题意: 如果一个>0的整数x,满足a[2*i+1] >= a[2*i]和a[2*i+2],则这个数为Mountain Number. 给出L, R,求区间[L, R]有多少个Mountain Number. 思路: 数位DP,判断当前是偶数位还是奇数位(从0开始),如果是偶数位,那么它要比前一个数的值小, 如果是奇数位,那么它要比前一个数的值大. 1 #include <iostream>

hdu3709---Balanced Number(数位dp)

Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the nu

HDU 3709 Balanced Number (数位DP)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3798    Accepted Submission(s): 1772 Problem Description A balanced number is a non-negative integer that can be balanced if a pi