一道编程的题,跪求答案,奖赏丰厚,各位大侠帮帮忙阿

来源:百度知道 编辑:UC知道 时间:2024/05/31 21:00:45
Function f(n) is recursively defined as:

* f(n) = f(n-1) + f(n-3), n > 3
* f(n) = n, n <= 3

Write a program to calculate f(n) modulo m.

n <= 10000, 2 <= m <= 10000.

Input

There are multiple test cases. Each test case consists of two integers: n and m. n = 0 and m = 0 denotes the end of input, and you should not process this case.

Output

For each test case, print f(n) modulo m in a single line.

Sample Input:

1 2
10000 999
0 0

Sample Output:

1
433
C和C++都行

#include<iostream>
using namespace std;

int seq[10001] = {0, 1, 2, 3};

int main(){
int n, m;
while((cin >> n >> m) && (n!=0 || m!=0)){
if(n <= 3){
cout << n % m << '\n';
continue;
}
for(int i = 4; i <= n; ++i)
seq[i] = (seq[i-1] + seq[i-3]) % m;
cout << seq[n] << '\n';
}
}

用什么?C?C++?