hdu1865 1sting

1sting

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2374    Accepted Submission(s): 949


Problem Description
You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.
 

Input
The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.
 

Output
The output contain n lines, each line output the number of result you can get .
 

Sample Input
3 1 11 11111
 

Sample Output
1 2 8
 

我的測試數據

輸入
4
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111

輸出
1264937032042997393488322
139583862445
86267571272
53316291173

AC代碼
#include<iostream>
using namespace std;
int main()
{
	int i,j,t,n;
	char str[210];
	int num[201][260]={0};
	num[1][0]=1;num[2][0]=2;
	for(i=3;i<201;i++)
	{
		for(j=0;j<260;j++)
			num[i][j]=num[i-1][j]+num[i-2][j];
		for(j=0;j<259;j++)
			if(num[i][j]>100000000)
			{
				num[i][j+1]+=num[i][j]/100000000;
				num[i][j]%=100000000;
			}
	}//本題爲斐波那契數列,就是1輸出1,11輸出2,則111輸出1+2,即當前項值等於前兩項相加
	//題目到200,所以先存下所有的次數
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",str);
		n=strlen(str);//它的長度爲當前爲第幾項
		for(i=259;i>0;i--)
			if(num[n][i]!=0)
				break;
			printf("%d",num[n][i]);
			for(j=i-1;j>=0;j--)
				printf("%08d",num[n][j]);
			printf("\n");
	}

	return 0;
}


本題爲簡單數論題,主要是掌握這種方法!

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