いろいろがんばりたいブログ

情報科学科の人がいろいろ書きます。

Codeforces 70A Cookies

http://codeforces.com/problemset/problem/70/A

よく見ると同じ構造が3つあることに気づく。1のときは空が1であることに注意。

f:id:tomoki_imai:20130223110459j:plain

#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int MOD = pow(10,6) + 3;

int main(){
    int n;
    cin >> n;
    vector<int> K(1001);
    K[0] = 1;
    K[1] = 1;
    K[2] = 3;

    for(int i=3;i<=1000;i++){
        K[i] = 3 * K[i-1] % MOD;
    }
    cout << K[n] << endl;
    return 0;
}

n=1000だからまぁいいけど、もしn=10000000000000とかだったらmod_powを使う。