BZOJ2900 Fun Number Games

Source: Internet
Author: User

Fun number Games

TK is also fond of playing games while abusing problems.
Now, there's a game like this, the rules are this:
Give a random number n, and then you write a row of all the numbers 1 to n on the playground, like this:
123456789101112131415 .....
Then you add a minus sign in front of each number, and a plus sign on every number that is on the odd digit, and a minus sign whenever the number is on an even digit. Well... In the end you get a super long formula. And we can figure out the result of this equation.
TK think this game is very interesting, so he played all night and night to play AH play ah ...
Maybe you think this game is not interesting ... Well... But, if you are TK, for a given n, can you figure out what the final result should be?

Input format: multiple sets of data. The number of data groups per test point does not exceed 1000 groups.
Each row is only a positive integer n. Make sure there are no extra strange characters.
The last line of data for each test point must be the number 0. Represents the end of this test point.  Output format: For each set of data, output the corresponding results. Most of the methods are digital DP. I felt that the analysis of the parity was not very clear, and I heard Czt's approach: for a K-bit long number, on the J-bit, the number is I contribution to the final result, aIntuitiveThe law of the I this number in the J-bit after the deletion of the remaining digits in the sequence of the number is the number of its items anotherIntuitiveThe law found that the above mentioned state in turn write down the contribution to the answer to find it is a male ratio of 1 or-1 of the geometric series!!! So you can use the mathematical method to complete the problem PS: this question or the analysis of the odd even too little brain memory is too small there is no way to improve the improvement??
1#include <cstdio>2 #definell Long Long3 using namespacestd;4ll ans,n,pows[ +];5 intMain ()6 {7pows[0]=1; for(intI=1; i<= -; i++) 8pows[i]=pows[i-1]*Ten;9      while(1) Ten     { Onescanf"%lld", &n);if(n==0)return 0;  A          for(intI=1; i<=9; i++) for(intj=0; j<= the; j + +) for(intk=j;k<= the; k++) -         {         - ll Start,end,fr; the             if(pows[j]*i+ (k>j) *pows[k]>n)Continue;  -Start=j==k?0:p ows[k-1]; -             if(n>=pows[j]-1+pows[j]*i+pows[k+1]-pows[j+1]) end=pows[k]-1;  -             Else if(n%pows[j+1]/pows[j]<i) +end=n/pows[j+1]*pows[j]-1;  -             Else if(n%pows[j+1]/pows[j]==i) +end=n/pows[j+1]*pows[j]+n%Pows[j]; A             Elseend=n/pows[j+1]*pows[j]+pows[j]-1;  atFr= (((i&1&&j==0&& (k&1)==0) ^ (k&1) ^ (j&1))*2-1)*i; -             if((k&1)==0&&j) -             { -                 if((start^end) &1)==0) ans+=fr; -             }  -             Elseans+= (end-start+1)*fr; in         } -printf"%lld\n", ans); ans=0; to     } +}

BZOJ2900 Fun Number Games

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.