Problem 1: 0/1 Tiles

i am trying to solve this problem in c++ --> http://www.icm2010.org.in/index.php/problems/01TILES

but i am getting wrong answer for larges inputs,
here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main(){

	long long unsigned int n;
	cin >> n;
	long long unsigned int pre = 1;
	long long unsigned int cur = 1;
	long long int fib;

	for(long long unsigned int i = 0;i < n-1;i ++){
		fib = pre+cur;
		pre = cur;
		cur = fib;
	}

	cout << cur%15746;


	return 0;
}


i dont know if my logic is wrong, but i dont think it is,
please help me with the problem, thanks
How large? Even long long data types have limits. Whenever you see this sort of problem, usually the issue is overflow. There is probably a mathematical solution that they want you to discover. Off the top of my head, given k = 15746, if pre%k = 5 and cur %k = 11, pre+cur%k will be 16. Does it matter how large pre and cur are, given the resulting modulus? So why are you bothering to store that info? What would happen if you set fib = (pre+cur)%k? I think that's likely your solution, but if it's not I expect it's something along those lines.
thanks a lot, it worked
Last edited on
Topic archived. No new replies allowed.