Number Sequence(找規律,,,,,反正我沒找到,遞迴超記憶體了....)
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 178534 Accepted Submission(s): 44354 Problem Description A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output For each test case, print the value of f(n) on a single line.
Sample Input 1 1 3 1 2 10 0 0 0 Sample Output 2 5 一看這題立馬寫了個遞迴然後出了個這個Memory Limit Exceeded
#include <stdio.h> #include <iostream> #include <algorithm> #include <stdlib.h> #include <string.h> using namespace std; int f(int A, int B, int n){ if(n==1) return 1; if(n==2) return 1; return (A*f(A, B, n-1)+B*f(A, B, n-2))%7; } int main(){ int n, A, B; while(scanf("%d%d%d",&A,&B,&n), A&&B&&n){ printf("%d\n",f(A,B,n)); } return 0; }
然後發現應該是個迴圈,再寫被T了;
#include <stdio.h> #include <iostream> #include <algorithm> #include <stdlib.h> #include <string.h> using namespace std; int main(){ int n, A, B; while(scanf("%d%d%d",&A,&B,&n)){ if(A==0 && B==0 && n==0) break; int ff[1000]; ff[1]=ff[2]=1; int i=3; for(i=3; i<=n; i++){ ff[i]=(A*ff[i-1]+B*ff[i-2])%7; // printf("%d ",ff[i]); if(ff[i]==1&&ff[i-1]==1) break; } //printf("\n"); if(i>n) printf("%d\n", ff[n]); else{ int cnt=i-2; int ans; if(n%cnt==0) ans=cnt; else ans=n%cnt; printf("%d\n",ff[ans]); } } return 0; }
然後再改,RE,,,,
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(){
int n, A, B;
while(scanf("%d%d%d",&A,&B,&n)){
if(A==0 && B==0 && n==0) break;
int ff[100];
ff[1]=ff[2]=1;
int i=3;
for(i=3; i<=n; i++){
ff[i]=(A*ff[i-1]+B*ff[i-2])%7;
//printf("%d ",ff[i]);
if(ff[i]==1&&ff[i-1]==1) break;
}
// printf("\n");
if(i>n) printf("%d\n", ff[n]);
else{
int cnt=i-2;
// printf("cnt: %d\n",cnt);
ff[0]=ff[cnt];
printf("%d\n",ff[n%cnt]);
}
}
return 0;
}
當我把for迴圈裡的n改成50就A了,改成100還是RE,醉了;#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(){
int n, A, B;
while(scanf("%d%d%d",&A,&B,&n)){
if(A==0 && B==0 && n==0) break;
int ff[100];
ff[1]=ff[2]=1;
int i=3;
for(i=3; i<=50; i++){ //就是這裡的50,改成n就RE,,,,,,,
ff[i]=(A*ff[i-1]+B*ff[i-2])%7;
//printf("%d ",ff[i]);
if(ff[i]==1&&ff[i-1]==1) break;
}
// printf("\n");
if(i>n) printf("%d\n", ff[n]);
else{
int cnt=i-2;
// printf("cnt: %d\n",cnt);
ff[0]=ff[cnt];
printf("%d\n",ff[n%cnt]);
}
}
return 0;
}
最後附上大神的AC程式碼,就是強啊,咋想到的呢,,,,,
#include <iostream>
using namespace std;
int main()
{
int A,B,n,i;
int a[48];
cin >> A >> B >> n;
while( A!=0 || B!=0 || n!=0)
{
a[0]=a[1]=1;
for(i=2;i<48;i++)
a[i] = ( A * a[i-1] + B * a[i-2]) % 7;
cout << a[(n-1)%48] <<endl;//48個數必定迴圈一遍,或以48的因子數一個迴圈......膜了;
cin >> A >> B >> n;
}
return 0;
}
相關推薦
Number Sequence(找規律,,,,,反正我沒找到,遞迴超記憶體了....)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 178534 Accep
請求找不到主機,請檢查該名稱,然後重試(ping ip沒問題,ping域名不行)
開始----執行----輸入cmd----輸入netsh winsock reset確認,重啟 命令提示符是在作業系統中,提示進行命令輸入的一種工作提示符。在不同的作業系統環境下,命令提示符各不相同。 這個命令作用是重置 Winsock 目錄。如果一臺機器上的Winsock協議配置有問題的話
驀然回首,陸茜文已把我勾住,不是想找個賢內助,只渴望心靈的歸宿,奈何他物件把我吼住,勾引之事,緣起在對方處,不要再把我勾住,就此打住。
2014 臺灣高考滿分作文 問世間情為何物, 汽車渴望公路, 花草渴望雨露, 靈魂渴望超度, 心靈渴望歸宿, 而我則~ 迫切渴望著有個媳婦。 眾裡尋她千百度, 踏平腳下路, 驀然回首細環顧, 大嬸大娘無數。 都說男兒有淚不撲簌, 但那絕對是未到
資料結構實驗-C語言-二叉樹的建立,前、中、後序遍歷的遞迴演算法和非遞迴演算法,求葉子結點數目,求二叉樹深度,判斷二叉樹是否相似,求二叉樹左右子樹互換,二叉樹層序遍歷的演算法,判斷二叉樹是否是完全二叉樹
1.實驗目的 熟練掌握二叉樹的二叉連結串列儲存結構的C語言實現。掌握二叉樹的基本操作-前序、中序、後序遍歷二叉樹的三種方法。瞭解非遞迴遍歷過程中“棧”的作用和狀態,而且能靈活運用遍歷演算法實現二叉樹的其它操作。 2.實驗內容 (1)二叉樹的二叉連結串列的建立 (2)二叉樹的前、中、後
有一天,派大星走了。海綿寶寶說:我沒哭,
有一塊海綿,他叫做海綿寶寶。 他很好,每天臉上都掛著大大的微笑_ 他很好,他的世界裡從沒有悲傷_ 他很好,能做出好吃的蟹黃堡_ 他很好,不會委屈,不會嘲笑_ 他很好,永遠有一個朋友陪他笑陪他玩_ 有一塊海星,他叫做派大星。 他很傻,卻能給人安全感。 他很
excel操作技巧,你知道嗎?反正我不知道
今天就是聖誕節了,不知道小夥伴們的公司給你們發了什麼福利呢?滿滿的祝福還是紅包大獎勵呢?這些都是大家的一點心意,今天小編給你們分享一些福利,就算是給大家拜個早年了! 對於excel來說,我們會操作的技巧都是你或多或少的,excel的操作技巧有很多,小編今天就和你們分享一些比較好用的exce
輸入兩個整數n和m,從數列1,2,3,...,n中隨意取幾個數,使其和等於m,將其所有可能的組合列出來。 遞迴求解
/* *[email protected] 轉載請註明出處 *問題:輸入兩個整數n和m,從數列1,2,3,...,n中隨意取幾個數, *使其和等於m,將其所有可能的組合列出來。 *求解思路:(遞迴求解) *(1)如果n>m則數列中>m的部分不可能參與組
n個元素進棧,輸出所有出棧序列-卡特蘭數-遞迴
#include <iostream> #include <stack> #include <queue> #include <algorithm> #include <string.h> #include <
線索化二叉樹,前序中序後序遍歷(遞迴和非遞迴實現)
#include<iostream> #include<string.h> using namespace std; enum PionerInfo { LINK,THREAD }; template<class T> struct
無向圖的鄰接矩陣,深度優先遍歷廣度優先遍歷的遞迴與非遞迴演算法
/*(1)輸入一組頂點,建立無向圖的鄰接矩陣。 進行DFS(深度優先遍歷)和BFS(廣度優先遍歷)。 寫出深度優先遍歷的遞迴和非遞迴演算法。*/ #include<stdio.h> #define max 40 //最大頂點個數 #define M 10000
圖演算法 - 只需“五步” ,獲取兩節點間的所有路徑(非遞迴方式)
在實現 “圖” 資料結構時,會遇到 “獲取兩點之間是所有路徑” 這個演算法問題,網上的資料大多都是利用遞迴演算法來實現(見文末的參考文章)。 我們知道在 JS 中用遞迴演算法很容易會讓呼叫棧溢位,為了能在生產環境中使用,必須要用非遞迴方式的去實現。 經過一番
HDU 1005 Number Sequence(找規律,思維)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 153140 Accepted Submissio
1005:Number Sequence(hdu,數學規律題)
his arch ear iostream tput ostream htm 數據 long Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (
洛谷P2158儀仗隊(數學,觀察找規律,歐拉函數)
show 找規律 == += 數值 std pro tchar urn 題目鏈接:https://www.luogu.org/problemnew/show/P2158 讀完題被嚇到了,這是什麽東西。 總之,需要觀察+找規律啊!觀察可以發現,從第三行開始,第i行中當前直
洛谷P1498南蠻圖騰(找規律,輸出圖形題技巧)
這題還是很有意思的,關鍵有三點: 1.觀察規律力強,能觀察到基礎圖形寬和高的規律。 2.要能倒著儲存基礎圖形(到這就完成一半了)。 3.能根據寬和高不斷複製基礎圖形 另外要注意並且能發現到: 這題你只能倒著操作,倒著存倒著輸出。 因為要加寬加高,正著
hdoj6298:Maximum Multiple(找規律,總結)
目錄 Maximum Multiple 題目解釋: 解題思路: ac程式碼: Maximum Multiple Time Limit: 4000/2000 MS (Java/Others) M
java作業,找規律打印出字母
題:如下所示,根據輸入行數列印字母,字母個數呈等差數列遞減,行首空格數呈等差數列遞增: 請輸入行數 5 EEEEEEEEE DDDDDDD CCCCC BBB A 請輸入行數 9 IIIIIIIIIIIIIIIII HHHHHHHHHHHHHHH GGGGG
C語言中的模運算-hdu6124(打表,找規律)
題目連結:https://vjudge.net/problem/HDU-6124 題目描述: 題目大意就是給你一個數,判斷這個數 % 其它數後共有幾種結果。 這題對我來說最大的難點是我不太知道每個數 餘 其他的數應該得出什麼結果,後來參考了別人的部落格,才弄清楚了。現在我就舉一些例子來說明一下:
764A】Taymyr is calling you (找規律,水題)
題幹: Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist
I - Again Stone Game LightOJ - 1296(博弈論,sg打表找規律)
Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and th