[Digital DP] spoj 10606 balanced numbers

Source: Internet
Author: User

Question:

For each bit of a number.

For each odd number, an even number must appear.

For each even number, an odd number of times is required.

Ideas:

Store the status of each number in a three-in-one mode. values 0 and 1 are odd and 2 are even.

Then the others are the same as the common digit DP.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "queue" # include "algorithm" # include "iostream" using namespace STD; // February 10: 03: 08 long DP [22] [180000]; int num [22]; int OK (INT X) {int V [50], CNT = 0; while (x) {v [CNT ++] = x % 3; X/= 3 ;}for (INT I = 0; I <CNT; I ++) {If (! V [I]) continue; if (I % 2 = 0 & V [I]! = 1) return 0; if (I % 2! = 0 & V [I]! = 2) return 0;} return 1;} int JS (int n, int X) {int V [50], CNT = 0, TEP = 0; memset (V, 0, sizeof (v); While (n) {v [CNT ++] = n % 3; N/= 3;} If (V [x] <2) V [x] ++; else V [x] = 1; for (INT I = 9; I> = 0; I --) TEP = TEP * 3 + V [I]; return TEP;} long DFS (INT site, int N, int zero, int f) {If (Site = 0) {If (zero) return 0; Return OK (n);} If (! F &&! Zero &&~ DP [site] [N]) return DP [site] [N]; int Len = f? Num [site]: 9; long ans = 0; For (INT I = 0; I <= Len; I ++) {If (zero) {if (I = 0) ans + = DFS (site-1, n, zero & I = 0, F & I = Len ); else ans + = DFS (site-1, JS (n, I), zero & I = 0, F & I = Len );} else ans + = DFS (site-1, JS (n, I), zero & I = 0, F & I = Len);} If (! F &&! Zero) DP [site] [N] = ans; return ans;} long solve (long x) {int CNT = 0; while (X) {num [++ CNT] = x % 10; X/= 10;} return DFS (CNT, 0, 1);} int main () {int T; cin> T; memset (DP,-1, sizeof (DP); While (t --) {long X, Y; scanf ("% LLD ", & X, & Y); printf ("% LLD \ n", solve (y)-solve (x-1);} return 0;} // September 25, 2014 10:39:37


[Digital DP] spoj 10606 balanced numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.