SPOJ 10606 Balanced Numbers Digital DP

Source: Internet
Author: User

Topic links

A number is called the balance number, which satisfies the number in each digit, odd occurrences even several times, even the odd number of occurrences, and the number of equilibrium in a range.

With the three-way compression, a number does not appear in 0, the odd number of occurrences with 1, the occurrence of an even number of times with 2, so that only the need to open an array of 20*60000.

1#include <bits/stdc++.h>2 using namespacestd;3 #definePB (x) push_back (x)4 #definell Long Long5 #defineMK (x, y) make_pair (x, y)6 #defineLson L, M, rt<<17 #defineMem (a) memset (a, 0, sizeof (a))8 #defineRson m+1, R, rt<<1|19 #defineMem1 (a) memset (a,-1, sizeof (a))Ten #defineMEM2 (a) memset (a, 0x3f, sizeof (a)) One #defineRep (I, A, n) for (int i = A; i<n; i++) A #defineull unsigned long Long -typedef pair<int,int>PLL; - Const DoublePI = ACOs (-1.0); the Const DoubleEPS = 1e-8; - Const intMoD = 1e9+7; - Const intINF =1061109567; - Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} }; + intdigit[ -], a[ -]; -ll dp[ -][60000]; + intJudgeintnum) { A     intCNT =0; at      for(inti =0; i<Ten; i++) { -A[i] = num%3; -Num/=3; -     } -      for(inti =0; i<Ten; i++) { -         if(i%2==0&&a[i]==2) in             return 0; -         if(i%2==1&&a[i]==1) to             return 0; +     } -     return 1; the } * intCalintNuminttmp) { $     intCNT =0;Panax Notoginseng      for(inti =0; i<Ten; i++) { -A[i] = num%3; theNum/=3; +     } Aa[tmp]++; the     if(a[tmp]==3) +a[tmp]=1; -      for(inti =9; i>=0; i--) { $num = num*3+A[i]; $     } -     returnnum; - } thell Dfs (intLenintNumintFpBOOLFirst ) { -     if(!Len) {Wuyi         returnjudge (num); the     } -     if(!fp&&dp[len][num]!=-1) { Wu         returnDp[len][num]; -     } AboutLL ret =0; $     intMaxx = Fp?digit[len]:9; -      for(inti =0; i<=maxx; i++) { -RET + = DFS (len-1, (first&&i==0)?0: Cal (Num, i), Fp&&i==maxx, i==0&&First ); -     } A     if(!FP) +         returnDp[len][num] =ret; the     returnret; - } $ ll Cal (ll N) { the     intLen =0; the      while(n) { theDigit[++len] = n%Ten; theN/=Ten; -     } in     returnDFS (Len,0,1,true); the } the intMain () About { the Mem1 (DP); the     intT; the ll A, B; +Cin>>T; -      while(t--) { thescanf"%lld%lld", &a, &b);//I64d will time out ...Bayiprintf"%lld\n", Cal (b)-cal (A-1)); the     } the}

SPOJ 10606 Balanced Numbers Digital DP

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.