poj 2786:Pell數列

#include<iostream>
using namespace std;
int main()
{
	int a[100];
	a[0] = 1;
	a[1] = 2;
	int m,n;
	cin>>m;
	while(m--)
	{
		cin>>n;
		if(n == 1)
		{
			cout<<1<<endl;
			continue;
		}
		if(n == 2)
		{
			cout<<2<<endl;
			continue;
		}
		n -= 2;
		int t0 = a[0];
		int t1 = a[1];
		int t2;
		while(n--)
		{
			t2 = 2*t1 + t0;
			t2 %= 32767;
			t0 = t1;
			t1 = t2;
		}
		cout<<t2<<endl;
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章