SGU 258 almost Lucky Numbers close to Lucky number (digital DP)

Source: Internet
Author: User

Test instructions

Defines a positive number with a 2n bit, the sum of the first n bits and the sum of the last n bits, or lucky. Given an interval, ask how many positive numbers can be changed to lucky by modifying a single number? Note You cannot include leading 0.

Ideas:

My idea is to record those non-lucky number, and then find a way to statistics, and later found a bit unworkable, unable to know its front and back of the sum is equal. If you record the number of lucky, and then by counting the number on each bit to become lucky number, this is more troublesome, because it will be repeated statistics, such as 11 and 22 is the number of lucky, and 21 can be modified 1 bits to become lucky number, was counted two times.

Learn the methods of predecessors, but also forced their own other templates. According to my understanding of this template, the first time the solution is directly solved, but all the statistics have been recorded, the next time if you use the return directly on the line. Complexity is all-in-one, 109, and each example is O (10*len). That is, for a fixed prefix or to be honest calculation.

This is the code of the predecessors, to understand, and by the way adapt to the new template.

1#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 0x7f3f3f3f A #defineLL Long Long - #defineULL unsigned long Long - using namespacestd; the Const DoublePI = ACOs (-1.0); - Const intn=Ten; - Const intm= $; - Const intmod=1e9+7; +  - intf[n][n][ About][n][n], bit[n+2]; +  A intDfsintIintUpintSumintMoreintLessBOOLE//e means: is the prefix?  at { -     if(i==0) -         return(Sum!=m && (sum+more>=m && sum-less<=M)); -  -     if(!e && ~f[i][up][sum][more][less])//has been calculated (non-prefix is OK) -         returnf[i][up][sum][more][less]; in  -     intans=0; to     intD= i==up?1:0;//Start, note that the first is not 0. +     intu= e? Bit[i]:9;//stop, pay attention to the bottom not super Ah -      for(; d<=u; d++)//whether the last one depends on the parameter e the     { *         intssum= i> (up>>1)? sum+d:sum-d;//Single -peak shape $         intMmore= i> (up>>1)? Max (More,9-D): Max (more, D);//front: Can be added. Rear part: can be reducedPanax Notoginseng         intlless= i> (up>>1)? Max (Less, i==up?d-1:D): Max (less,9-D);//front: can be reduced. Rear: can be added -Ans+=dfs (I-1, up, Ssum, Mmore, lless, e&&d==u); the     } +     returnE? Ans:f[i][up][sum][more][less]=ans;//The return of the prefix is different A } the  +  - intCalintN) $ { $     if(n<Ten)return 0;//only single digit cannot be lucky number -     intlen=0, ans=0; -      while(n)//Split number the     { -bit[++len]=n%Ten;WuyiN/=Ten; the     } -  Wu      for(intI=2; i<=len; i+=2)//I is the length of the number -Ans+=dfs (I,i,0+m,0,0, i==len); About     returnans; $ } -  - intMain () - { A     //freopen ("Input.txt", "R", stdin); +memset (f,-1,sizeof(f)); the     intL, R; -      while(~SCANF ("%d%d",&l,&R)) $printf"%d\n", Cal (R)-cal (l1) ); the     return 0; the}
AC Code

SGU 258 almost Lucky Numbers close to Lucky number (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.