Croc Champ 2012 ? Round 1 : A Rock-Paper-Scissors

方針

  • n が大きいので単純に数える事はしない
  • 多くとも m*k ラウンドやれば後は周期的なので、m*k までシミュレーション
/*! if g++ -g a.cpp; then ./a.out < a.test; fi
 */

#include <cmath>

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

using namespace std;

typedef unsigned long long ull;

enum wins {A, B, D};

wins match(char a, char b){
  if((a=='R' && b=='S') ||
     (a=='S' && b=='P') ||
     (a=='P' && b=='R')) return A;
  if((b=='R' && a=='S') ||
     (b=='S' && a=='P') ||
     (b=='P' && a=='R')) return B;
  else return D;
}


int main(int argc, char *argv[]){
  ull n;
  cin >> n;
  string a, b;
  cin >> a >> b;

  ull lcm = a.size() * b.size();

  ull temp_a=0, temp_b=0;
  ull ret_a=0, ret_b=0;
  ull res = n % lcm;
  for(ull i = 0; i < min(n, lcm); i++){
    switch(match(a[i % a.size()], b[i % b.size()])){
    case A:{
      ret_b++;
      break;
    }
    case B:{
      ret_a++;
      break;
    }
    case D:{
      break;
    }
    }
    if((i+1)==res){
      temp_a = ret_a;
      temp_b = ret_b;
    }
  }

  ull x = n / lcm;
  cout << x * ret_a + temp_a << " "
       << x * ret_b + temp_b << endl;
  return 0;
}