Hdu 1250 Hat's Fibonacci (high-precision addition + indentation + Fibonacci number)

Source: Internet
Author: User

Calculate the nth entry of a Fibonacci series. The upper limit of n is 10000.

 


Solution: Because the upper limit of this question is 10000, indentation is required for high precision.

 

# Include <iostream> # include <string. h> using namespace std; const int MAXN = 100000000; const int N = 500; const int M = 10000; struct bign {int len; int s [N]; bign () {this-> len = 1; memset (s, 0, sizeof (s);} bign (int number) {* this = number;} bign (const char * number) {* this = number;} bign change (bign cur) {bign now; now = cur; for (int I = 0; I <cur. len; I ++) now. s [I] = cur. s [cur. len-I-1]; retu Rn now;} void delZore () {// Delete the leading 0. bign now = change (* this); while (now. s [now. len-1] = 0 & now. len> 1) {now. len --;} * this = change (now);} void put () {// output value. DelZore (); printf ("% d", s [0]); for (int I = 1; I <len; I ++) printf ("% 08d ", s [I]);} bign operator = (const char * number) {memset (s, 0, sizeof (s); int dist = strlen (number ); int k = dist % 8; for (int I = 0; I <k; I ++) s [0] = s [0] * 10 + number [I]-'0'; int cnt = 0; for (int I = k; I <dist; I ++, cnt ++) s [cnt/8 + 1] = s [cnt/8 + 1] * 10 + number [I]-'0 '; len = cnt/8 + 1; return * this;} bign operator = (int number) {char string [N]; sprintf (string, "% d", number ); * this = string; return * this;} bign operator + (const bign & cur) {bign sum, a, B; sum. len = 0; a =. change (* this); B = B. change (cur); for (int I = 0, g = 0; g | I <. len | I <B. len; I ++) {int x = g; if (I <. len) x + =. s [I]; if (I <B. len) x + = B. s [I]; sum. s [sum. len ++] = x % MAXN; g = x/MAXN;} return sum. change (sum) ;}}; bign num [M + 10]; int main () {int n; num [1] = num [2] = num [3] = num [4] = 1; for (int I = 5; I <M + 5; I ++) num [I] = num [I-1] + num [I-2] + num [I-3] + num [I-4]; while (scanf ("% d", & n) = 1) {num [n]. put (); 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.