1sting
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3240 Accepted Submission(s): 1243
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.
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 8import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); BigInteger[] num=new BigInteger [220]; num[1]=new BigInteger("1"); num[2]=new BigInteger("2"); for(int i=3; i<=200; i++){ num[i]=num[i-1].add(num[i-2]); } //String []str=new String[1010]; int m,n=in.nextInt(); while(n>0){ String str=in.next(); m=str.length(); System.out.println(num[m]); n--; } } }使用大数的常规解法,AC: #include<cstdio> #include<cstring> int a[202][300]; void f(int len) { a[1][0]=1; a[2][0]=2; int d=1,ans=0,i,j,c; for(i=3; i<=len; i++) { c=0; for(j=0;j<d; j++) { ans = a[i-2][j]+a[i-1][j]+c; a[i][j]=ans%10; c=ans/10; } while(c) { a[i][d++] = c%10; c/=10; } } } int main() { int n,len,i,j; char s[202]; scanf("%d",&n); while(n--) { scanf("%s",s); len = strlen(s); f(len); j=300; while(a[len][j]==0) { j--; } for(i=j; i>=0; i--) { printf("%d",a[len][i]); } printf("\n"); } return 0; }
用数组的第二维存储多位一直超时,不知道什么原因。 超时代码: #include<cstdio> #include<cstring> int a[202][10000]; void f(int len){ int i,j,c,d,ans; memset(a,0,sizeof(a)); a[1][0]=1; a[2][0]=2; for(i=3,d=1; i<=len; i++){ c=0; for(j=0; j<d; j++){ ans=a[i-1][j]+a[i-2][j]+c; a[i][j]=ans%10000; c=ans/10000; } while(c){ a[i][d++] =c%10000; c/=10000; } } j = d; while(a[len][j]==0){ j--; } printf("%d",a[len][j]); for(i=j-1; i>=0; i--) printf("%04d",a[len][i]); printf("\n"); } int main(){ int n,len,i,j; char s[202]; scanf("%d",&n); while(n--){ scanf("%s",s); len=strlen(s); f(len); } return 0; }