(ΦωΦ)<これの答えですよー。目次はここ。
---
問4-3
(ΦωΦ)<再帰
#include <iostream>
#include <algorithm>
using namespace std;
const int mod = int(1e9) + 7;
class RockPaperScissorsMagicEasy { public: int count(vector<int> card, int score); };
vector<int> card;
int n, score;
vector<vector<int>> dp;
vector<vector<bool>> seen;
int rec(int i, int wincnt) {
int &res = dp[i][wincnt];
if(i == n) { return res = score == wincnt; }
if(seen[i][wincnt]) { return res; }
res = 0;
int bob = card[i];
for(int alice=0; alice<3; alice++) {
int alice_win = (alice - bob + 3) % 3 == 2;
res += rec(i+1, wincnt+alice_win);
res %= mod;
}
seen[i][wincnt] = true;
return res;
}
int RockPaperScissorsMagicEasy::
count(vector<int> _card, int _score) {
card = _card;
score = _score;
n = card.size();
dp.assign(2010, vector<int>(2010));
seen.assign(2010, vector<bool>(2010));
int res = rec(0, 0);
return res;
}
(ΦωΦ)<forループ
#include <iostream>
#include <algorithm>
using namespace std;
class RockPaperScissorsMagicEasy { public: int count(vector<int> card, int score); };
const int mod = int(1e9) + 7;
int RockPaperScissorsMagicEasy::
count(vector<int> card, int score) {
int n = card.size();
vector<vector<int>> dp(2010, vector<int>(2010));
dp[0][0] = 1;
for(int i=0; i<n; i++) {
int bob = card[i];
for(int j=0; j<n; j++) {
for(int alice=0; alice<3; alice++) {
int alice_win = (alice - bob + 3) % 3 == 2;
dp[i+1][j+alice_win] += dp[i][j];
dp[i+1][j+alice_win] %= mod;
}
}
}
int res = dp[n][score];
return res;
}
(ΦωΦ)<おしまい