[Digital statistics] spoj 1433 the sum

Source: Internet
Author: User

For a number plus one digit minus one digit, given n, evaluate 1 ~ The sum of N.

Example 12 = 1-2 + 3-4.....-1 + 2 = 5

Ideas:

My personal thoughts may be complicated, but the ideas are clear.

First, we divide a number N into two parts, for example, 4568 = 1 ~ 999 + 1000 ~ 4568,567 = 1 ~ 99 + 100 ~ 567

That is to say, we can calculate the number of thousands in a whole hundred, even though we can find the rule ~

Is 1 ~ 999 = 1 ~ 99 + 100 ~ 999.

In this case, the final computing problem is solved, and functions are more unified.

Then we can find that the number of digits is an odd number, except that the number before a single digit is offset by two.

For example, 100 ~ 105 =

-1 + 0-0

+ 1-0 + 1

-1 + 0-2

+ 1-0 + 3

....

In addition, the single digit is 0 ~ 9 and

Therefore, you only need to calculate the parity of the number and the Division relationship of 10.

Then, for the number of even numbers

For example, 1000 ~ 9876

Calculate the value 1000 ~ 8999 and then 9000 ~ 9876

1000 ~ 8999 is actually the difference between the first and end numbers, which is offset in the middle.

Then 9000 ~ In 9876, the bitwise computation is done, subtraction, adding, and so on.

The rule cannot be found.


Then pay attention to two points for this question ..

1. I don't understand why the limit of 3 is 64 bits. I handled it specially.

2. In special processing, the integer cannot be so long. Simply add ll to the end.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "stack" # include "algorithm" # include "iostream" using namespace STD; long long xx = 9999999999998ll; long a [2] [10], ANS [16], ten [16]; long solve (long x) {long Len, T = 1, n, TEP, ANS = 0; Len = (long) log10 (x * 1.0) + 1; n = len-1; while (n --) T * = 10; If (LEN % 2) // odd number of digits {TEP = x-t + 1; ans + = TEP/10 * A [1] [9]; if (TEP % 10) ans + = A [1] [TEP % 10- 1]; If (TEP % 2) {x/= 10; T/= 10; int f = 0; while (x) {If (F % 2 = 0) ans-= x/T; else ans + = x/T; X % = T; T/= 10; F ++ ;}}} else // even digits {ans = ANS-T * A [0] [x/T-1]; ans + = (x/t-1) * (T/10) * A [0] [9]; // 1000 ~ 8999 ANS-= (x/t) * (X % t + 1); TEP = 0; int f = 0; X % = T; T/= 10; while (x) {If (f) {ANS-= TEP * T * A [0] [9]; ANS-= T * A [0] [x/T-1]; ans-= (x/t) * (X % t + 1);} else {ans + = TEP * T * A [0] [9]; ans + = T * A [0] [x/T-1]; ans + = (x/t) * (X % t + 1 );} TEP = TEP * 10 + x/T; X % = T; T/= 10; f ^ = 1 ;}return ans ;}int main () {memset (, 0, sizeof (a); int I; for (I = 1; I <= 9; I ++) A [0] [I] = A [0] [I-1] + I; for (I = 1; I <= 9; I ++) {if (I % 2) A [1] [I] = A [1] [I-1] + I; else a [1] [I] = A [1] [I-1]-I ;} long long T = 9; ans [1] = A [1] [9]; for (I = 2; I <= 15; I ++) {long sum = 0; t = T * 10 + 9; sum = solve (t); ans [I] = ans [I-1] + sum;} long N; while (scanf ("% LLD", & N), n) {long Len; If (n> = XX) {If (n = XX) puts ("409090909090901"); else if (n = xx + 1) puts ("409090909090910"); else if (n = xx + 2) puts ("409090909090909"); continue;} If (n <10) {printf ("% LLD \ n", a [1] [N]); continue ;} len = (long) log10 (N x 1.0) + 1; printf ("% LLD \ n", ANS [len-1] + solve (n);} return 0 ;}


[Digital statistics] spoj 1433 the sum

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.