Advertisement
Guest User

Untitled

a guest
May 25th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main implements Runnable {
  5.  
  6.     final long MOD = 1 << 16;
  7.  
  8.     public void _main() throws IOException {
  9.         while (true) {
  10.             int n = nextInt();
  11.             if (n == 0)
  12.                 break;
  13.             long a = 1;
  14.             long b = 0;
  15.             for (int i = 0; i < n; i++) {
  16.                 String com = next();
  17.                 if (com.equals("ADD")) {
  18.                     b += nextInt();
  19.                     if (b >= MOD)
  20.                         b -= MOD;
  21.                 }
  22.                 else {
  23.                     int v = nextInt();
  24.                     a = (a * v) % MOD;
  25.                     b = (b * v) % MOD;
  26.                 }
  27.             }
  28.             boolean[] can = new boolean[(int)MOD];
  29.             for (int x = 0; x < MOD; x++)
  30.                 can[(int)((a * x + b) % MOD)] = true;
  31.             int res = 0;
  32.             for (int i = 0; i < MOD; i++)
  33.                 if (can[i])
  34.                     ++res;
  35.             out.println(res);
  36.         }
  37.     }
  38.  
  39.     private BufferedReader in;
  40.     private PrintWriter out;
  41.     private StringTokenizer st;
  42.  
  43.     private String next() throws IOException {
  44.         while (st == null || !st.hasMoreTokens()) {
  45.             String rl = in.readLine();
  46.             if (rl == null)
  47.                 return null;
  48.             st = new StringTokenizer(rl);
  49.         }
  50.         return st.nextToken();
  51.     }
  52.  
  53.     private int nextInt() throws IOException {
  54.         return Integer.parseInt(next());
  55.     }
  56.  
  57.     private long nextLong() throws IOException {
  58.         return Long.parseLong(next());
  59.     }
  60.  
  61.     private double nextDouble() throws IOException {
  62.         return Double.parseDouble(next());
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         new Thread(new Main()).start();
  67.     }
  68.  
  69.     public void run() {
  70.         try {
  71.             in = new BufferedReader(new InputStreamReader(System.in));
  72.             out = new PrintWriter(System.out);
  73.  
  74.             _main();
  75.  
  76.             out.close();
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.             System.exit(202);
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement