Nyoj 114 Some sort (large number of water + optimized large number)

Source: Internet
Author: User

Some sequence time limit: theMs | Memory Limit:65535KB Difficulty:4
Describe
Sequence a satisfies an = An-1 + An-2 + An-3, n >= 3
Write programs, given A0, A1, and A2, to calculate A99
Input
Input contains multiple rows of data
Each row of data contains 3 integers A0, A1, A2 (0 <= A0, A1, A2 <= 100000000)
Data ends with EOF
Output
The value of the output A99 for each line of input
Sample input
1 1 1
Sample output
69087442470169316923566147
Source
Water problem competition in the less water

Uploaded by

Idea: (water)
High-precision Addition

A 1-length number is stored at each location

Time 44 Memory 640

#include <bits/stdc++.h>using namespace std; #define MEM (a) memset (A,0,sizeof (a)) int a,b,c;int a[100][1000];//    int b[1000],c[1000],d[1000],sum[1000];void Save (int n,int *a) {int i=0;        while (n) {a[i++]=n%10;    n/=10;    }}void Add (int *b,int *c,int *d,int *sum) {int up=0;    int t;        for (int i=0;i<=999;i++) {t=b[i]+c[i]+d[i]+up;        sum[i]=t%10;    UP=T/10; }}int Main () {while (~scanf ("%d%d%d", &a,&b,&c)) {if (! A &&! B &&!                        C) {printf ("0\n");                    Continue                } mem (a);                Save (A,a[0]);                Save (b,a[1]);                Save (c,a[2]);                for (int i=3;i<=99;i++) {Add (A[i-1],a[i-2],a[i-3],a[i]);                } int i=999;                while (a[99][i]==0) i--;     for (int j = i; j >= 0; j--)           {printf ("%d", a[99][j]);        } printf ("\ n"); }}


Optimization idea: A 7-length number per unit is stored

time:0 Memory: 232

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm>using namespace        Std;int Main () {int a,b,c;        int a[100][100];                while (~SCANF ("%d%d%d", &a,&b,&c)) {int len=1;                int up=0;                memset (A,0,sizeof (a));                A[0][0]=a;                A[1][0]=b;                A[2][0]=c;                            for (int i = 3;i <, i++) {for (int j = 0;j < Len; j + +) {                            int ret = A[i-1][j] + a[i-2][j] + a[i-3][j] + up;                            A[I][J] = ret% 10000000;                    up = ret/10000000;                        } if (UP) {a[i][len++] = up;                    up = 0;                }} printf ("%d", a[99][len-1]); for (int i = len-2; i>=0; i--) printf("%07d", A[99][i]);        printf ("\ n"); }}


Nyoj 114 Some sort (large number of water + optimized large number)

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.