HDU 4927 Big Data

Source: Internet
Author: User

The question is simple:

For the number of n characters in length, perform n-1 times to generate a new series:

B1 = a2-a1 b2= a3-a2 B3 = a4-a3

C1 = b2-b1 C2 = b3-b2

Ans = c2-c1

Finally, the formula is as follows: Yang Hui triangle of N's row

For example:

3

1 2 3

Ans = 1*1-2*2 + 1*3 = 0

4

1 5 7 2

Ans =-1*1 + 3*5-3*7 + 1*2 =-5

You can optimize each number in the Yang Hui triangle. The last number can be multiplied and divided by the previous one.

Java uses the number of seconds... But not Java --#

Use C ++ to simulate large numbers ..


# Include "stdio. H "# include" string. H "int A [3010] ;__ int64 mark [3010], ANS [3010], C [3010]; int max (int A, int B) {if (a <B) return B; else return a;} void make_mul (int x) {int I; for (I = 1; I <= mark [0]; I ++) MARK [I] * = x; for (I = 1; I <= mark [0]; I ++) {mark [I + 1] + = mark [I]/1000000; Mark [I] % = 1000000;} while (MARK [MARK [0] + 1]! = 0) {mark [0] ++; Mark [MARK [0] + 1] = mark [MARK [0]/1000000; mark [MARK [0] % = 1000000 ;}} void make_div (int x) {int I; for (I = mark [0]; I> = 2; I --) {mark [I-1] + = (MARK [I] % x) x 1000000; Mark [I]/= x;} mark [1]/= X; while (MARK [MARK [0] = 0) MARK [0] --;} void make_add () {int I, OP; If (ANS [0]> 0) {for (I = 1; I <= mark [0]; I ++) {ans [I] + = mark [I]; ans [I + 1] + = ans [I]/1000000; ans [I] % = 1000000;} while (ANS [ans [0] + 1]! = 0) {ans [0] ++; ans [ans [0] + 1] = ans [ans [0]/1000000; ans [ans [0] % = 1000000;} return;} else {If (-ans [0]> MARK [0]) OP =-1; else if (-ans [0] <mark [0]) OP = 1; else {for (I = mark [0]; I> = 1; I --) if (MARK [I]> ans [I]) {op = 1; break;} else if (MARK [I] <ans [I]) {op =-1; break;} if (I = 0) {memset (ANS, 0, sizeof (ANS); ans [0] = 1; return ;}} if (OP = 1) {for (I = 1; I <= mark [0]; I ++) {ans [I] = mark [I]-ans [I]; If (ANS [I] <0) {Mark [I + 1] --; ans [I] + = 1000000 ;}} ans [0] =-ans [0]; while (ANS [ans [0] = 0) ans [0] --;} else {for (I = 1; I <=-ans [0]; I ++) {ans [I] = ans [I]-MARK [I]; If (ANS [I] <0) {ans [I + 1] --; ans [I] + = 1000000;} while (ANS [-ans [0] = 0) ans [0] ++ ;}} void make_red () {int I, OP; If (ANS [0] <0) {for (I = 1; I <= mark [0]; I ++) {ans [I] + = mark [I]; ans [I + 1] + = ans [I]/1000000; ans [I] % = 1000000 ;} while (ANS [-ans [0] + 1]! = 0) {ans [0] --; ans [-ans [0] + 1] = ans [-ans [0]/1000000; ans [-ans [0] % = 1000000;} return;} else {If (ANS [0]> MARK [0]) OP = 1; else if (ANS [0] <mark [0]) OP =-1; else {for (I = mark [0]; I> = 1; I --) if (MARK [I]> ans [I]) {op =-1; break;} else if (MARK [I] <ans [I]) {op = 1; break;} if (I = 0) {memset (ANS, 0, sizeof (ANS); ans [0] = 1; return ;}} if (OP = 1) {for (I = 1; I <= mark [0]; I ++) {ans [I] = ans [I]-MARK [I]; If (ANS [I] <0) {ans [I + 1] --; ans [I] + = 1000000;} while (ANS [ans [0] = 0) ans [0] --;} else {for (I = 1; I <= mark [0]; I ++) {ans [I] = mark [I]-ans [I]; If (ANS [I] <0) {mark [I + 1] --; ans [I] + = 1000000 ;}} ans [0] = mark [0]; while (ANS [ans [0] = 0) ans [0] --; ans [0] =-ans [0] ;}} int main () {int t, n, I, X, Y, J, m, OP; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% d ", & A [I]); memset (ANS, 0, sizeof (ANS); ans [0] = 1; if (I = 1) {printf ("% d \ n", a [1]); continue;} memset (mark, 0, sizeof (Mark); Mark [0] = 1; mark [1] = 1; if (N % 2 = 1) ans [1] = A [1]; else ans [1] =-A [1]; X = 1; y = n-1; for (I = 2; I <= N; I ++) {make_mul (y); y --; make_div (X ); X ++; memcpy (C, Mark, sizeof (Mark); make_mul (A [I]); If (N % 2 = 1) {if (I % 2 = 1) make_add (); else make_red ();} else {if (I % 2 = 0) make_add (); else make_red ();} memcpy (mark, C, sizeof (c);} If (ANS [0]> 0) {printf ("% i64d ", ans [ans [0]); for (I = ans [0]-1; I> = 1; I --) printf ("% 06i64d ", ans [I]); printf ("\ n");} else {printf ("-"); printf ("% i64d ", ans [-ans [0]); for (I =-ans [0]-1; I> = 1; I --) printf ("% 06i64d ", ans [I]); printf ("\ n") ;}} return 0 ;}



Related Article

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.