#1005 Number Sequence

來源:互聯網
上載者:User

標籤:

http://acm.hdu.edu.cn/showproblem.php?pid=1005

 

一看以為只是個普通的遞迴調用,寫完如下

 1 #include<stdio.h> 2  3 int A, B; 4 int f(int n) 5 { 6     if (n == 1 || n == 2) 7     { 8         return 1; 9     }10     return (A*f(n - 1) + B*f(n - 2)) % 7;11 }12 int main()13 {14     int n;15     while (scanf("%d %d %d", &A, &B, &n) && A!=0 && B!=0 && n!=0)16     {17         printf("%d\n", f(n));18     }19     return 0;20 }

結果一提交報錯Memory Limit Exceeded超出記憶體限制,慌了,沒遇到過。

 

題目的n輸入範圍是0到一億,這是一個相當大的數字,遞迴那麼多次必然要超出記憶體限制了。

百度了一下發現在17行處改為

printf("%d\n", f(n%49));

就能AC了,並不能理解,再看看其他人的答案,連遞迴都不見了,我是不是錯過了什麼。。。我再找找原因。

#1005 Number Sequence

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.