public static void main(String[] args) throws Exception {
   InputReader in = new InputReader(System.in);
   OutputWriter out = new OutputWriter(System.out);
   int n = in.readInt();
   long m = in.readLong();
   long[] b = new long[n];
   long mf = 0;
   for (int i = 0; i < n; i++) {
     b[i] = in.readLong();
   }
   long ans = 0;
   long cntm = 0;
   for (int j = 0; j < n; j++) {
     ans += ((b[j] * (b[j] * (b[j] + 1) / 2)) % m);
     if (b[j] <= m) {
       ans += FACT(b[j], m);
       // ans=ans.mod(m);
     } else {
       if (cntm == 0) {
         mf = FACT(m, m);
         cntm++;
       }
       ans += mf;
       // ans=ans.mod(m);
     }
     // System.out.println(ans);
     // ans=ans.mod(new BigInteger(Integer.toString(m)));
   }
   out.printLine(ans);
   {
     out.close();
   }
 }
Esempio n. 2
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      String a = in.nextString();
      String b = in.nextString();
      int[][] dp = new int[2][b.length() + 1];
      dp[1][0] = 1;
      for (int i = 0; i <= b.length(); i++) {
        dp[0][i] = i;
      }

      for (int i = 1, r = a.length(); i <= r; i++) {
        dp[i % 2][0] = i;
        for (int j = 1, c = b.length(); j <= c; j++) {
          if (a.charAt(i - 1) == b.charAt(j - 1)) {
            dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
          } else {
            dp[i % 2][j] = 1 + min(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1], dp[i % 2][j - 1]);
          }
        }
      }

      out.println(dp[a.length() % 2][b.length()]);
    }

    out.flush();
    out.close();
  }
  public static void main(String[] args) {
    BufferedReader in;

    try {
      in =
          new BufferedReader(
              new FileReader(
                  "/Users/rahulkhairwar/Documents/IntelliJ IDEA Workspace/Competitive "
                      + "Programming/src/com/google/codejam16/qualificationround/inputB.txt"));

      OutputWriter out = new OutputWriter(System.out);
      Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 28);

      thread.start();

      try {
        thread.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      out.flush();

      in.close();
      out.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int t = in.readInt();
    int[] a = new int[400000000];
    for (int j = 1; j <= 400000000; j++) {
      int ans2 = 0;
      int j1 = j;
      while (j1 > 0) {
        int x = j1 % 10;
        if (x % 2 == 0) {
          ans2 += (2 * x);
        } else {
          ans2 += x;
        }
        j1 = j1 / 10;
      }
      a[j - 1] = ans2 % 10;
    }
    for (int i = 0; i < t; i++) {
      int a1 = in.readInt();
      int b1 = in.readInt();
      int ans = 0;
      for (int j = a1; j <= b1; j++) {

        ans += a[j - 1];
      }
      out.printLine(ans);
    }

    {
      out.close();
    }
  }
  public static void main(String[] args) throws Exception {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);

    {
      out.close();
    }
  }
Esempio n. 6
0
 public static void main(String[] args) {
   InputStream inputStream = System.in;
   OutputStream outputStream = System.out;
   InputReader in = new InputReader(inputStream);
   OutputWriter out = new OutputWriter(outputStream);
   Task10107 solver = new Task10107();
   solver.solve(1, in, out);
   out.close();
 }
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    Solver solver = new Solver(in, out);

    solver.solve();
    in.close();
    out.flush();
    out.close();
  }
Esempio n. 8
0
 public static void main(String[] args) {
   InputReader ir = new InputReader(System.in);
   OutputWriter ow = new OutputWriter(System.out);
   int tests = ir.readInt();
   for (int i = 0; i < tests; i++) {
     int num = ir.readInt();
     if ((num & 1) == 0) ow.printLine("ALICE");
     else ow.printLine("BOB");
   }
   ow.close();
 }
  public static void main(String[] args) {
    in = new InputReader(System.in);
    out = new OutputWriter(System.out);

    solve();

    out.flush();

    in.close();
    out.close();
  }
Esempio n. 10
0
    public static void main(String[] argv) throws IOException
    {
        Task t;
        boolean home = argv.length > 0 && argv[0].equals("test");
        InputStream inputStream;
        OutputStream outputStream;
        if (home) {
            inputStream = new FileInputStream(Task.filename + ".in");
            outputStream = new FileOutputStream(Task.filename + ".out");
        } else {
            switch (Task.read) {
                case 0:
                    inputStream = new FileInputStream(Task.filename + ".in");
                    outputStream = new FileOutputStream(Task.filename + ".out");
                    break;
                case 1:
                    inputStream = new FileInputStream("input.txt");
                    outputStream = new FileOutputStream("output.txt");
                    break;
                default:
                    inputStream = System.in;
                    outputStream = System.out;
                    break;

            }

        }
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream, home);


        if (home)
            do {
                long time = System.currentTimeMillis();
                t = new Task();
                t.in = in;
                t.out = out;
                t.run();

                out.writeln();
                out.writeln("=====Time:" + (System.currentTimeMillis() - time));
                out.flush();
            } while (in.toNextTest());
        else {
            t = new Task();
            t.in = in;
            t.out = out;
            t.run();
        }

        out.close();

    }
Esempio n. 11
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      int n = in.nextInt();
      out.println(countTri(n));
    }

    out.flush();
    out.close();
  }
Esempio n. 12
0
 public static void main(String[] args) throws IOException {
   // InputStream inputStream = new FileInputStream("replaceme.in");
   // OutputStream outputStream = new FileOutputStream("replaceme.out");
   /////////////////////////////////////////////////////////////////////
   InputStream inputStream = System.in;
   OutputStream outputStream = System.out;
   InputReader in = new InputReader(inputStream);
   OutputWriter out = new OutputWriter(outputStream);
   Task solver = new Task();
   solver.solve(1, in, out);
   out.close();
   System.exit(0);
 }
  public static void main(String[] args) {
    First first = new First();

    reader = first.new InputReader(System.in);
    writer = first.new OutputWriter(System.out);

    getAttributes();

    writer.flush();

    reader.close();
    writer.close();
  }
  public static void main(String[] args) {
    AdditionAndMultiplication aAM = new AdditionAndMultiplication();

    reader = aAM.new InputReader(System.in);
    writer = aAM.new OutputWriter(System.out);

    getAttributes();

    writer.flush();

    reader.close();
    writer.close();
  }
 public static void main(String[] args) throws Exception {
   InputReader in = new InputReader(System.in);
   OutputWriter out = new OutputWriter(System.out);
   int t = in.readInt();
   for (int i = 0; i < t; i++) {
     int a = in.readInt();
     if (a % 2 == 0) {
       out.printLine(a);
     } else {
       out.printLine(a - 1);
     }
   }
   {
     out.close();
   }
 }
Esempio n. 16
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    while (true) {
      int n = in.nextInt();
      if (n == 0) break;
      int c = 0;
      while (n > 1) {
        n /= 2;
        c++;
      }

      out.println(c);
    }
    out.flush();
    out.close();
  }
  public static void main(String[] args) throws Exception {
    // System.setIn(new
    // FileInputStream("E:/Shreyans/Documents/Code/CODE/SPOJ/Stdin_File_Read.txt"));
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int n = in.readInt(); // entering number of vertices
    int[][] a = new int[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a[i][j] = in.readInt();
      }
    }

    {
      out.close();
    }
  }
Esempio n. 18
0
 public static void main(String[] args) {
   InputStream inputStream;
   try {
     inputStream = new FileInputStream(args[0]);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   OutputStream outputStream;
   try {
     outputStream = new FileOutputStream("output.txt");
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   InputReader in = new InputReader(inputStream);
   OutputWriter out = new OutputWriter(outputStream);
   TaskC solver = new TaskC();
   int testCount = Integer.parseInt(in.next());
   for (int i = 1; i <= testCount; i++) solver.solve(i, in, out);
   out.close();
 }
  public static void main(String[] args) throws Exception {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int t = in.readInt();
    for (int i = 0; i < t; i++) {
      String a = in.readString();
      int kmax = 0, k = 0, is = 0, ie = 0, max1 = 0, max2 = 0;
      int rmax = 0, r = 0;
      for (int j = 0; j < a.length(); j++) {
        if (a.charAt(j) == 'K') {
          if (r > rmax) {
            rmax = r;
          }
          if (k == 0) {
            is = j;
          } else {
            ie = j;
          }
          k++;
        } else {
          r++;
          if (k > kmax) {
            kmax = k;
          }
          if (ie - is > (max2 - max1)) {
            max1 = is;
            max2 = ie;
          }
          k = 0;
        }
      }
      if (r > rmax) {
        rmax = r;
      }
      if (k > kmax) {
        kmax = k;
      }
      if (ie - is > (max2 - max1)) {
        max1 = is;
        max2 = ie;
      }
      if (rmax > kmax) {
        out.printLine(rmax);
      } else {
        int ans = kmax;
        for (int j = max1 - 1; j >= 0; j--) {
          if (a.charAt(j) == 'R') {
            ans++;
          }
        }
        for (int j = max2 + 1; j < a.length(); j++) {
          if (a.charAt(j) == 'R') {
            ans++;
          }
        }
        out.printLine(ans);
      }
    }

    {
      out.close();
    }
  }