Submission #1038740

# Submission time Handle Problem Language Result Execution time Memory
1038740 2024-07-30T07:09:10 Z vjudge1 Slon (COCI15_slon) C++17
120 / 120
9 ms 860 KB
#include<bits/stdc++.h>

using namespace std;

int solution(string s, int M)
{
  // apply_product;

  string pf;
  int num = 0;
  for(int i = 0; i < s.size(); i++)
    {
      // cerr << "i : " << i << endl;
      if('0' <= s[i] && s[i] <= '9')
	num = (num * 10ll % M + s[i] - '0') % M;
      else if(s[i] != '*')
	pf += to_string(num), num = 0, pf += s[i];
      else
	{
	  int num2 = 0;
	  bool b = true;
	  for(int j = i + 1; j < s.size(); j++)
	    {
	      if(s[j] < '0' || s[j] > '9') {
		i = j - 1;
		b = false;
		break;
	      }
	      num2 = (num2 * 10ll % M + s[j] - '0') % M;
	    }
	  // cerr << num << ' ' << num2 << endl;
	  // cerr << num * num2 % M << endl;
	  num = 1ll * num * num2  % M;
	  if(b) break;
	}
    }
  // cerr << num << endl;
  pf += to_string(num);
  pf = "+" + pf;
  // cerr << pf << endl;
  int ans = 0;
  for(int i = 0; i < pf.size(); i ++)
    {
      int c = (pf[i] == '+') ? 1 : -1;
      int num2 = 0;
      bool b = true;
      for(int j = i + 1; j < pf.size(); j++)
	{
	  if(pf[j] < '0' || pf[j] > '9') {
	    i = j - 1;
	    b = false;
	    break;
	  }
	  num2 = (num2 * 10ll % M + pf[j] - '0') % M;
	}
      // cerr << num2 << endl;
      num2 *= c;
      ans = ((ans + num2) % M + M) % M;
      if(b) break;
    }
  // cerr << ans << endl;
  return ans;
}

int solve(string s, int x, int M)
{
  string problem = '(' + s + ')';
  vector<int> last;
  vector<string> exp = {""};
  for(char i : problem)
    {
      if(i == '(')
	exp.push_back("");
      else if(i == ')')
	{
	  // cerr << "exp: " << exp.back() << endl;
	  int val = solution(exp.back(), M);
	  exp.pop_back();
	  exp.back() += to_string(val);
	}
      else if(i == 'x')
	exp.back() += to_string(x);
      else
	exp.back() += i;
    }
  return stoi(exp.back()) % M;
}

int main()
{
  string s;
  int P, M;
  cin >> s >> P >> M;
  int f = solve(s, 0, M), sec = solve(s, 1, M);
  int d = ((sec - f) % M + M) % M;
  // cerr << f << ' ' << sec << endl;
  for(int i = 0; i < M; i++)
    {
      f = (f % M + M) % M;
      if(f == P)
	{
	  cout << i << endl;
	  return 0;
	}
      f += d;
    }
  assert(false);
  cout << "I was just kidding\n";
  return 0;
}

Compilation message

slon.cpp: In function 'int solution(std::string, int)':
slon.cpp:11:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |   for(int i = 0; i < s.size(); i++)
      |                  ~~^~~~~~~~~~
slon.cpp:22:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |    for(int j = i + 1; j < s.size(); j++)
      |                       ~~^~~~~~~~~~
slon.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for(int i = 0; i < pf.size(); i ++)
      |                  ~~^~~~~~~~~~~
slon.cpp:47:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |       for(int j = i + 1; j < pf.size(); j++)
      |                          ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 9 ms 860 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 604 KB Output is correct
10 Correct 6 ms 860 KB Output is correct