Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9815ea2

Browse files
fibonacci modified, js big mul and big add implemented, passed 8/10 test case
1 parent 42e7630 commit 9815ea2

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

‎fibonacci-modified.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', _ => {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
function add(a, b) {
28+
let carry = 0;
29+
let i = 0;
30+
for (i = 0; i < b.length; i++) {
31+
let n = a[i] + b[i] + carry;
32+
a[i] = n % 10;
33+
carry = Math.floor(n / 10);
34+
}
35+
while (carry > 0) {
36+
a[i] = typeof a[i] !== 'undefined' ? a[i] : 0
37+
let n = a[i] + carry;
38+
a[i] = n % 10;
39+
carry = Math.floor(n / 10);
40+
i++;
41+
}
42+
return a;
43+
}
44+
45+
const mul = (b, a) => {
46+
let out = [];
47+
let k = 0, carry = 0;
48+
for (let i = 0; i < a.length; i++) {
49+
for (let j = 0; j < b.length; j++) {
50+
let e = typeof out[k] !== 'undefined' ? out[k] : 0;
51+
let n = (a[i] * b[j]) + carry + e;
52+
out[k] = n % 10;
53+
carry = Math.floor(n / 10);
54+
k++;
55+
}
56+
if (carry > 0) {
57+
out[k] = carry;
58+
carry = 0;
59+
}
60+
k = i + 1;
61+
}
62+
return out;
63+
}
64+
function fibonacciModified(t1, t2, n) {
65+
let hash = {};
66+
hash[1] = [t1];
67+
hash[2] = [t2];
68+
for (let i = 3; i < n + 1; i++) {
69+
hash[i] = add(mul(hash[i - 1].map(x => x), hash[i - 1].map(x => x)), hash[i - 2].map(x => x))
70+
}
71+
return hash[n].reverse().join('');
72+
}
73+
function main() {
74+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
75+
76+
const t1T2n = readLine().split(' ');
77+
78+
const t1 = parseInt(t1T2n[0], 10);
79+
80+
const t2 = parseInt(t1T2n[1], 10);
81+
82+
const n = parseInt(t1T2n[2], 10);
83+
84+
let result = fibonacciModified(t1, t2, n);
85+
86+
ws.write(result + "\n");
87+
88+
ws.end();
89+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /