HPU省赛训练(二)

 

 

 

HPU省赛训练(二)

H - Mountain Number

Mountain Number

题目描述

One integer number x is called “Mountain Number” if:
(1) x>0 and x is an integer;
(2) Assume x=a[0]a[1]…a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[2i+2](if exists).
For example, 111, 132, 893, 7 are “Mountain Number” while 123, 10, 76889 are not “Mountain Number”.
Now you are given L and R, how many “Mountain Number” can be found between L and R (inclusive) ?

Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.
Then T cases, for any case, only two integers L and R (1≤L≤R≤1,000,000,000).

Output

For each test case, output the number of “Mountain Number” between L and R in a single line.

思路

应该是数位dp以前学习的不扎实,这次好好的补一下, 三维dp,dp[i][j][k] 代表这个数的第i位前面一位为j的时候k代表当前为是否为偶数位,如果k=1代表是偶数位,否则不是,

code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int t ; 
int v[20] ,cnt ;
int dp[20][20][2] ;

int dfs(int pos ,int per ,bool flag ,int odd) {
	if ( cnt + 1 == pos ) return 1 ; 
	if ( !flag && dp[pos][per][odd] ) return dp[pos][per][odd] ;
	int r = flag ? v[pos] : 9 ;
	int ret = 0 ;
	for (int i = 0 ; i <= r ; i ++ ) {
		if ( odd && i >= per ) ret += dfs(pos + 1 ,i ,flag&&(i == v[pos]) ,odd^1) ;
		else if ( !odd && i <= per ) ret += dfs(pos + 1 ,i ,flag&&(i == v[pos] ),odd^1 ) ;  
	}
	if ( !flag ) dp[pos][per][odd] = ret ;
	return ret ;
}

int slove(int x) {
	cnt = 0 ;
	while( x ) {
		v[++cnt] = x % 10 ;
		x /= 10 ;
	}
	if ( cnt == 1 ) return v[1] ;
	reverse(v + 1 ,v + cnt + 1 ) ;
	memset(dp ,0 ,sizeof(dp)) ;
	return dfs(1 ,9 ,true ,0 ) ;
}

int main() {
	cin >> t ;
	while ( t -- ) {
		int l ,r ;
		scanf("%d %d",&l ,&r ) ;
		printf("%d\n",slove(r) - slove(l-1)) ;
	}
	return 0;
}

D - Digits Count

题目描述

Given N integers A={A[0],A[1],…,A[N-1]}. Here we have some operations:
Operation 1: AND opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] AND opn (here “AND” is bitwise operation).
Operation 2: OR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] OR opn (here “OR” is bitwise operation).
Operation 3: XOR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] XOR opn (here “XOR” is bitwise operation).
Operation 4: SUM L R
We want to know the result of A[L]+A[L+1]+…+A[R].
Now can you solve this easy problem?

input

The first line of the input contains an integer T, indicating the number of test cases. (T≤100)
Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.
Then one line follows n integers A[0], A[1], …, A[n-1] (0≤A[i]<16,0≤i<n).
Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)

output

For each test case and for each “SUM” operation, please output the result with a single line.

待更来日再补

 

posted @ 2019-04-07 16:52  Nlifea  阅读(149)  评论(0编辑  收藏  举报