public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int   n = fs.NextInt(), m = fs.NextInt();
             int[] a   = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32);
             int[] b   = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32);
             int   max = -1;
             for (int i = 0; i < n; i++)
             {
                 int ind = Find(b, a[i]);
                 int min;
                 if (ind != -1)
                 {
                     min = 0;
                 }
                 else
                 {
                     int l = FindLowerBound(b, a[i]);
                     int u = FindUpperBound(b, a[i]);
                     min = Math.Min(l != -1 ? a[i] - b[l] : int.MaxValue, u != -1 ? b[u] - a[i] : int.MaxValue);
                 }
                 max = Math.Max(max, min);
             }
             writer.WriteLine(max);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             try
             {
                 int   F = fs.NextInt(), I = fs.NextInt(), T = fs.NextInt();
                 int[] likes = new int[I];
                 if (F <= 10 && F >= 1 && I <= 10 && I >= 1 && T <= F && T >= 1)
                 {
                     for (int i = 0; i < F; i++)
                     {
                         string str = fs.ReadLine();
                         if (str.Length != I)
                         {
                             writer.WriteLine(0);
                             return;
                         }
                         for (int j = 0; j < I; j++)
                         {
                             if (str[j] == 'Y')
                             {
                                 likes[j]++;
                             }
                             else if (str[j] != 'Y' && str[j] != 'N')
                             {
                                 writer.WriteLine(0);
                                 return;
                             }
                         }
                     }
                     if (!String.IsNullOrEmpty(fs.ReadLine()))
                     {
                         writer.WriteLine(0);
                         return;
                     }
                     int ans = 0;
                     for (int i = 0; i < I; i++)
                     {
                         if (likes[i] >= T)
                         {
                             ans++;
                         }
                     }
                     writer.WriteLine(ans);
                 }
                 else
                 {
                     writer.WriteLine(0);
                 }
             }
             catch (Exception e)
             {
                 writer.WriteLine(0);
                 return;
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             string s   = fs.ReadLine();
             long   ans = 0;
             for (int i = 0; i < s.Length; i++)
             {
                 int d = Convert.ToInt32("" + s[i]);
                 if (i < s.Length - 1)
                 {
                     int dd = Convert.ToInt32("" + s[i] + s[i + 1]);
                     if (dd % 4 == 0)
                     {
                         ans += i + 1;
                     }
                 }
                 if (d % 4 == 0)
                 {
                     ans++;
                 }
             }
             writer.WriteLine(ans);
         }
 }
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
                using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
                {
                    string[] exp = fs.ReadLine().Split('e');
                    string[] num = exp[0].Split('.');

                    int           b  = int.Parse(exp[1]);
                    StringBuilder sb = new StringBuilder(200);
                    sb.Append(num[0]);
                    int i = 0;
                    for (i = 0; i < b; i++)
                    {
                        if (i >= num[1].Length)
                        {
                            sb.Append("0");
                        }
                        else
                        {
                            sb.Append(num[1][i]);
                        }
                    }
                    if (b < num[1].Length && num[1] != "0")
                    {
                        sb.Append(".");
                        for (; i < num[1].Length; i++)
                        {
                            sb.Append(num[1][i]);
                        }
                    }
                    writer.WriteLine(sb);
                }
        }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             bool[] met = new bool[26];
             int    n = fs.NextInt();
             string str = fs.ReadLine();
             int    dup = 0, unused = 0;
             foreach (char c in str)
             {
                 if (met[c - 'a'])
                 {
                     dup++;
                 }
                 else
                 {
                     met[c - 'a'] = true;
                 }
             }
             foreach (bool b in met)
             {
                 if (!b)
                 {
                     unused++;
                 }
             }
             writer.WriteLine(dup > unused ? -1 : dup);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int t = fs.NextInt();
             Dictionary <string, int> count = new Dictionary <string, int>();
             while (t-- > 0)
             {
                 string[] cmd = fs.ReadLine().Split();
                 if (cmd[0] == "+")
                 {
                     long   a       = Convert.ToInt64(cmd[1]);
                     string pattern = NumToPattern(a);
                     if (!count.ContainsKey(pattern))
                     {
                         count.Add(pattern, 0);
                     }
                     count[pattern]++;
                 }
                 else if (cmd[0] == "-")
                 {
                     long   a       = Convert.ToInt64(cmd[1]);
                     string pattern = NumToPattern(a);
                     count[pattern]--;
                 }
                 else
                 {
                     string pattern = AppendZeros(cmd[1]);
                     writer.WriteLine(count.ContainsKey(pattern) ? count[pattern] : 0);
                 }
             }
         }
 }
Пример #7
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             string     str   = fs.ReadLine();
             List <int> list  = new List <int>(str.Length);
             int        first = -1;
             for (int i = 0; i < str.Length; i++)
             {
                 if (str[i] == 'F')
                 {
                     if (first == -1 && list.Count != i)
                     {
                         first = list.Count;
                     }
                     list.Add(i);
                 }
             }
             if (list.Count == 0 || list.Count == str.Length || first == -1)
             {
                 writer.WriteLine(0);
                 return;
             }
             int t = 0;
             for (int i = first + 1; i < list.Count; i++)
             {
                 t = Math.Max(0, t - list[i] + list[i - 1] + 2);
             }
             writer.WriteLine(t + list.Last() - list.Count + 1);
         }
 }
Пример #8
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int            n = fs.NextInt();
             int[]          a = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32);
             SumSegmentTree seg = new SumSegmentTree(new long[n + 1]);
             double         expDiff = 0, denom = n * 1L * (n + 1) / 2, expInvCount = 0;
             long           inv = 0;
             for (int i = n - 1; i >= 0; i--)
             {
                 inv         += seg.QuerySum(0, a[i] - 1);
                 expInvCount += ExpectedInverseCount(n - i);
                 expDiff     += (expInvCount - inv) / denom;
                 seg.Update(a[i], n - i);
             }
             long initialInverseCount = 0;
             seg = new SumSegmentTree(new long[n + 1]);
             for (int i = n - 1; i >= 0; i--)
             {
                 initialInverseCount += seg.QuerySum(0, a[i] - 1);
                 seg.Update(a[i], 1);
             }
             writer.WriteLine((initialInverseCount + expDiff).ToString().Replace(",", "."));
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             n             = fs.NextInt();
             m             = fs.NextInt();
             char[,] a     = new char[n, m];
             int[][,] dist = new int[4][, ];
             for (int i = 0; i < 4; i++)
             {
                 dist[i] = new int[n, m];
             }
             for (int i = 0; i < n; i++)
             {
                 string line = fs.ReadLine();
                 for (int j = 0; j < m; j++)
                 {
                     a[i, j] = line[j];
                     for (int k = 0; k < 4; k++)
                     {
                         dist[k][i, j] = int.MaxValue;
                     }
                 }
             }
             int xt = fs.NextInt() - 1, yt = fs.NextInt() - 1;
             int xm = fs.NextInt() - 1, ym = fs.NextInt() - 1;
             RunBFS(a, dist, xt, yt);
             int ans = Math.Min(dist[0][xm, ym], Math.Min(dist[1][xm, ym], Math.Min(dist[2][xm, ym], dist[3][xm, ym])));
             writer.WriteLine(ans == int.MaxValue ? -1 : ans);
         }
 }
Пример #10
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    a  = fs.NextInt();
             string s1 = fs.NextToken();
             string s2 = new string(fs.ReadLine().Reverse().ToArray());
             long   c  = 0;
             for (int i = 0; i < s2.Length; i++)
             {
                 if (s2[i] != '0')
                 {
                     c += (long)Math.Pow(a, i) * SingleDigitToDecimalBase(s2[i]);
                 }
             }
             if (s1 == "R")
             {
                 writer.WriteLine(ToRomanNumeral(c.ToString(), 0));
             }
             else
             {
                 int b = Convert.ToInt32(s1);
                 writer.WriteLine(DecimalToAnyBase(c, b));
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int      n = fs.NextInt(), p = fs.NextInt();
             string[] buyers = new string[n];
             for (int i = n - 1; i >= 0; i--)
             {
                 buyers[i] = fs.ReadLine();
             }
             long   revenue = p / 2;
             double prevVal = 1;
             for (int i = 1; i < n; i++)
             {
                 if (buyers[i] == "half")
                 {
                     prevVal *= 2;
                 }
                 else
                 {
                     prevVal = prevVal * 2 + 1;
                 }
                 revenue += (long)((prevVal / 2) * p);
             }
             writer.WriteLine(revenue);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int   n    = fs.NextInt();
             int[] rows = new int[n], cols = new int[n];
             for (int i = 0; i < n; i++)
             {
                 string s = fs.ReadLine();
                 for (int j = 0; j < n; j++)
                 {
                     if (s[j] == 'C')
                     {
                         rows[i]++;
                         cols[j]++;
                     }
                 }
             }
             long ans = 0;
             for (int i = 0; i < n; i++)
             {
                 ans += (rows[i] * (rows[i] - 1)) / 2;
                 ans += (cols[i] * (cols[i] - 1)) / 2;
             }
             writer.WriteLine(ans);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
     using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
     {
         int n = fs.NextInt(), m = fs.NextInt();
         Player[] p = new Player[n];
         string[] ans = Enumerable.Repeat("", m).ToArray();
         for (int i = 0; i < n; i++)
         {
             string[] s = fs.ReadLine().Split();
             p[i] = new Player { Name = s[0], Region = Convert.ToInt32(s[1]) - 1, Score = Convert.ToInt32(s[2]) };
         }
         p = p.OrderBy(x => x.Region).ThenByDescending(x => x.Score).ToArray();
         int j = 0, lastRegion = -1;
         for (int i = 0; i < n; i++)
         {
             if (p[i].Region != lastRegion)
             {
                 j = 0;
                 lastRegion = p[i].Region;
             }
             if (j == 0) ans[p[i].Region] += p[i].Name + " ";
             else if (j == 1) ans[p[i].Region] += p[i].Name;
             else if (j == 2 && p[i].Score == p[i - 1].Score) ans[p[i].Region] = "?";
             j++;
         }
         for (int i = 0; i < m; i++)
         {
             writer.WriteLine(ans[i]);
         }
     }
 }
Пример #14
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int   n    = fs.NextInt();
             int[] male = new int[367], fem = new int[367];
             for (int i = 0; i < n; i++)
             {
                 string[] line = fs.ReadLine().Split(' ');
                 if (line[0] == "M")
                 {
                     int l = Convert.ToInt32(line[1]), r = Convert.ToInt32(line[2]);
                     for (int j = l; j <= r; j++)
                     {
                         male[j]++;
                     }
                 }
                 else if (line[0] == "F")
                 {
                     int l = Convert.ToInt32(line[1]), r = Convert.ToInt32(line[2]);
                     for (int j = l; j <= r; j++)
                     {
                         fem[j]++;
                     }
                 }
             }
             int ans = 0;
             for (int i = 1; i < 367; i++)
             {
                 ans = Math.Max(ans, Math.Min(male[i], fem[i]) * 2);
             }
             writer.WriteLine(ans);
         }
 }
Пример #15
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int  n   = fs.NextInt();
             long x   = fs.NextInt();
             int  sad = 0;
             for (int i = 0; i < n; i++)
             {
                 string[] line = fs.ReadLine().Split();
                 if (line[0] == "+")
                 {
                     x += Convert.ToInt32(line[1]);
                 }
                 else
                 {
                     int d = Convert.ToInt32(line[1]);
                     if (x < d)
                     {
                         sad++;
                     }
                     else
                     {
                         x -= d;
                     }
                 }
             }
             writer.WriteLine(x + " " + sad);
         }
 }
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
                using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
                {
                    int    n = fs.NextInt(), x = fs.NextInt();
                    int    mod = (int)1e9 + 7;
                    long[] a   = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt64);

                    long sum = a.Sum();
                    SortedDictionary <long, int> count = new SortedDictionary <long, int>();
                    for (int i = 0; i < n; i++)
                    {
                        long pow = sum - a[i];
                        if (!count.ContainsKey(pow))
                        {
                            count[pow] = 0;
                        }
                        count[pow]++;
                    }
                    LinkedList <Frequency> freqs = new LinkedList <Frequency>();
                    foreach (var pair in count)
                    {
                        freqs.AddLast(new Frequency {
                            Pow = pair.Key, Value = pair.Value
                        });
                    }
                    long minPow = 0;
                    while (freqs.Count > 0)
                    {
                        var node = freqs.First;
                        freqs.RemoveFirst();
                        var  next = freqs.First;
                        long pow  = node.Value.Pow;
                        int  freq = node.Value.Value;
                        if (freq % x == 0)
                        {
                            if (next != null && next.Value.Pow == pow + 1)
                            {
                                next.Value.Value += freq / x;
                            }
                            else
                            {
                                freqs.AddFirst(new Frequency {
                                    Pow = pow + 1, Value = freq / x
                                });
                            }
                        }
                        else
                        {
                            minPow = pow;
                            break;
                        }
                    }
                    writer.WriteLine(BinPowMod(x, Math.Min(minPow, sum), mod));
                }
        }
Пример #17
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             string s = fs.ReadLine();
             int    lc = 0, rc = 0, uc = 0, dc = 0;
             lc = s.Where(x => x == 'L').Count();
             rc = s.Where(x => x == 'R').Count();
             uc = s.Where(x => x == 'U').Count();
             dc = s.Where(x => x == 'D').Count();
             if (s.Length % 2 == 1)
             {
                 writer.WriteLine(-1);
                 return;
             }
             int ans = 0;
             if ((lc + rc) % 2 == 1)
             {
                 if (lc > rc)
                 {
                     lc--;
                 }
                 else
                 {
                     rc--;
                 }
                 if (uc > dc)
                 {
                     dc++;
                 }
                 else
                 {
                     uc++;
                 }
                 ans++;
             }
             if (lc > rc)
             {
                 ans += (lc - rc) / 2;
             }
             else if (lc < rc)
             {
                 ans += (rc - lc) / 2;
             }
             if (uc > dc)
             {
                 ans += (uc - dc) / 2;
             }
             else if (uc < dc)
             {
                 ans += (dc - uc) / 2;
             }
             writer.WriteLine(ans);
         }
 }
Пример #18
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    n     = fs.NextInt();
             string s     = fs.ReadLine();
             int[]  count = new int[52];
             int    total = 0;
             foreach (char c in s)
             {
                 if (count[Index(c)] == 0)
                 {
                     total++;
                 }
                 count[Index(c)]++;
             }
             count = new int[52];
             int k = 0, ans = int.MaxValue;
             for (int l = 0, r = -1; l < n; l++)
             {
                 if (l > r)
                 {
                     r = l;
                     if (count[Index(s[l])] == 0)
                     {
                         k++;
                     }
                     count[Index(s[l])]++;
                 }
                 while (k < total && r < n - 1)
                 {
                     r++;
                     if (count[Index(s[r])] == 0)
                     {
                         k++;
                     }
                     count[Index(s[r])]++;
                 }
                 if (k == total)
                 {
                     ans = Math.Min(ans, r - l + 1);
                 }
                 else
                 {
                     break;
                 }
                 if (count[Index(s[l])] == 1)
                 {
                     k--;
                 }
                 count[Index(s[l])]--;
             }
             writer.WriteLine(ans);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             string s = fs.ReadLine(), r = new string(s.Reverse().ToArray());
             n        = s.Length;
             hashS    = new long[n];
             hashR    = new long[n];
             pows     = new long[n];
             hashS[0] = ToLong(s[0]);
             hashR[0] = ToLong(r[0]);
             pows[0]  = 1;
             for (int i = 1; i < n; i++)
             {
                 pows[i] = pows[i - 1] * p;
             }
             for (int i = 1; i < n; i++)
             {
                 hashS[i] = hashS[i - 1] + ToLong(s[i]) * pows[i];
                 hashR[i] = hashR[i - 1] + ToLong(r[i]) * pows[i];
             }
             int[] dp = new int[n];
             dp[0] = 1;
             if (n > 1)
             {
                 dp[1] = s[0] == s[1] ? 2 : 0;
             }
             if (n > 2)
             {
                 dp[2] = s[0] == s[2] ? 2 : 0;
             }
             for (int i = 3; i < n; i++)
             {
                 int mid = i / 2;
                 if (i % 2 == 0)
                 {
                     if (IsPalindrome(0, mid - 1, mid + 1, i))
                     {
                         dp[i] = dp[mid - 1] + 1;
                     }
                 }
                 else
                 {
                     if (IsPalindrome(0, mid, mid + 1, i))
                     {
                         dp[i] = dp[mid] + 1;
                     }
                 }
             }
             writer.WriteLine(dp.Sum());
         }
 }
Пример #20
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    n = fs.NextInt(), k = fs.NextInt();
             string s   = fs.ReadLine();
             int    max = Solve(s, n, k, 'a');
             max = Math.Max(Solve(s, n, k, 'b'), max);
             writer.WriteLine(max);
         }
 }
Пример #21
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    n = fs.NextInt();
             string str = fs.ReadLine();
             bool[] isDenied = new bool[n];
             int    d = 0, r = 0;
             int    dcount = str.Count(x => x == 'D');
             int    rcount = str.Count(x => x == 'R');
             int    i;
             for (i = 0; true; i = (i + 1) % n)
             {
                 if (isDenied[i])
                 {
                     continue;
                 }
                 if (str[i] == 'D')
                 {
                     if (rcount == 0)
                     {
                         break;
                     }
                     while (str[r] != 'R' || isDenied[r])
                     {
                         r = (r + 1) % n;
                     }
                     isDenied[r] = true;
                     rcount--;
                     r = (r + 1) % n;
                     d = (i + 1) % n;
                 }
                 else
                 {
                     if (dcount == 0)
                     {
                         break;
                     }
                     while (str[d] != 'D' || isDenied[d])
                     {
                         d = (d + 1) % n;
                     }
                     isDenied[d] = true;
                     dcount--;
                     d = (d + 1) % n;
                     r = (i + 1) % n;
                 }
             }
             writer.WriteLine(str[i]);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int     n       = fs.NextInt();
             int[]   a       = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32);
             int     m       = fs.NextInt();
             Query[] queries = new Query[m];
             for (int i = 0; i < m; i++)
             {
                 queries[i] = new Query {
                     Id = i, Left = fs.NextInt() - 1, Right = fs.NextInt() - 1
                 };
             }
             queries = queries.OrderBy(q => q.Right).ToArray();
             int[] xor     = new int[n];
             int[] answers = new int[m];
             xor[0] = a[0];
             for (int i = 1; i < n; i++)
             {
                 xor[i] = xor[i - 1] ^ a[i];
             }
             Dictionary <int, int> lastPos   = new Dictionary <int, int>();
             XorSegmentTreeFast    distincts = new XorSegmentTreeFast(new int[n]);
             int r = -1;
             foreach (Query q in queries)
             {
                 while (r < q.Right)
                 {
                     r++;
                     if (lastPos.ContainsKey(a[r]))
                     {
                         distincts.Update(lastPos[a[r]], 0);
                         lastPos[a[r]] = r;
                     }
                     else
                     {
                         lastPos.Add(a[r], r);
                     }
                     distincts.Update(r, a[r]);
                 }
                 answers[q.Id]  = q.Left > 0 ? xor[q.Right] ^ xor[q.Left - 1] : xor[q.Right];
                 answers[q.Id] ^= distincts.QueryXor(q.Left, q.Right);
             }
             for (int i = 0; i < m; i++)
             {
                 writer.WriteLine(answers[i]);
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int       n    = fs.NextInt();
             Request[] reqs = new Request[n];
             for (int i = 0; i < n; i++)
             {
                 reqs[i] = new Request(fs.ReadLine());
             }
             reqs = reqs.OrderBy(r => r.Path).ToArray();
             Dictionary <string, Host> hostsByName = new Dictionary <string, Host>();
             Dictionary <string, LinkedList <Host> > hostsListByPaths = new Dictionary <string, LinkedList <Host> >();
             for (int i = 0; i < n; i++)
             {
                 if (!hostsByName.ContainsKey(reqs[i].Hostname))
                 {
                     hostsByName.Add(reqs[i].Hostname, new Host(reqs[i].Hostname));
                 }
                 hostsByName[reqs[i].Hostname].AddPath(reqs[i].Path);
             }
             int k = 0;
             foreach (KeyValuePair <string, Host> item in hostsByName)
             {
                 string paths = item.Value.GetPaths();
                 if (!hostsListByPaths.ContainsKey(paths))
                 {
                     hostsListByPaths.Add(paths, new LinkedList <Host>());
                 }
                 hostsListByPaths[paths].AddLast(item.Value);
                 if (hostsListByPaths[paths].Count == 2)
                 {
                     k++;
                 }
             }
             writer.WriteLine(k);
             foreach (KeyValuePair <string, LinkedList <Host> > item in hostsListByPaths)
             {
                 if (item.Value.Count > 1)
                 {
                     foreach (Host host in item.Value)
                     {
                         writer.Write(host.Hostname + " ");
                     }
                     writer.WriteLine();
                 }
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             _str   = fs.ReadLine();
             solved = new bool[2, _str.Length];
             Find(_str.Length - 1, "");
             writer.WriteLine(_set.Count);
             foreach (string suffix in _set)
             {
                 writer.WriteLine(suffix);
             }
         }
 }
Пример #25
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49 };
             int   count  = 0;
             for (int i = 0; i < primes.Length; i++)
             {
                 writer.WriteLine(primes[i]);
                 writer.Flush();
                 count += fs.ReadLine() == "yes" ? 1 : 0;
             }
             writer.WriteLine(count <= 1 ? "prime" : "composite");
             writer.Flush();
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    n = fs.NextInt(), k = fs.NextInt(), totalSum = 0;
             string s    = fs.ReadLine();
             int[]  sum  = new int[n];
             int[]  sign = new int[n];
             for (int i = 0; i < n; i++)
             {
                 sum[i]    = (s[i] - 'a') <= 12 ? 'z' - s[i] : s[i] - 'a';
                 sign[i]   = (s[i] - 'a') <= 12 ? 1 : -1;
                 totalSum += sum[i];
             }
             if (totalSum < k)
             {
                 writer.WriteLine(-1);
             }
             else
             {
                 StringBuilder sb = new StringBuilder();
                 int           j  = 0;
                 while (k > 0)
                 {
                     if (k > sum[j])
                     {
                         k -= sum[j];
                         sb.Append((char)(s[j] + sign[j] * sum[j]));
                     }
                     else
                     {
                         sb.Append((char)(s[j] + sign[j] * k));
                         k = 0;
                     }
                     j++;
                 }
                 while (j < n)
                 {
                     sb.Append(s[j++]);
                 }
                 writer.WriteLine(sb.ToString());
             }
         }
 }
Пример #27
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    n = fs.NextInt(), k = fs.NextInt();
             long[] c = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt64);
             bool[] isCap = new bool[n];
             long   sum = c.Sum(), res = 0, caps = 0;
             for (int i = 0; i < k; i++)
             {
                 int ind = fs.NextInt() - 1;
                 isCap[ind] = true;
                 caps      += c[ind];
             }
             for (int i = 0; i < n; i++)
             {
                 int l = i - 1, r = (i + 1) % n;
                 if (l < 0)
                 {
                     l = n - 1;
                 }
                 if (isCap[i])
                 {
                     res += c[i] * (sum - c[i]);
                 }
                 else
                 {
                     res += c[i] * caps;
                     if (!isCap[l])
                     {
                         res += c[i] * c[l];
                     }
                     if (!isCap[r])
                     {
                         res += c[i] * c[r];
                     }
                 }
             }
             writer.WriteLine(res / 2);
         }
 }
Пример #28
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int      n     = fs.NextInt();
             bool[]   exist = new bool[7];
             string[] codes = new string[n];
             for (int i = 0; i < n; i++)
             {
                 codes[i] = fs.ReadLine();
             }
             for (int i = 0; i < n; i++)
             {
                 for (int j = i + 1; j < n; j++)
                 {
                     int count = CountSameDigits(codes[i], codes[j]);
                     if (!exist[count])
                     {
                         exist[count] = true;
                     }
                 }
             }
             int ans = 6;
             if (n > 1)
             {
                 if (!exist[5] && !exist[4])
                 {
                     ans = 1;
                     if (!exist[3] && !exist[2])
                     {
                         ans = 2;
                     }
                 }
                 else
                 {
                     ans = 0;
                 }
             }
             writer.WriteLine(ans);
         }
 }
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
                using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
                {
                    map = new Dictionary <char, int>();
                    int val = 0;
                    for (char c = '0'; c <= '9'; c++)
                    {
                        map.Add(c, val++);
                    }
                    for (char c = 'A'; c <= 'Z'; c++)
                    {
                        map.Add(c, val++);
                    }
                    for (char c = 'a'; c <= 'z'; c++)
                    {
                        map.Add(c, val++);
                    }
                    map.Add('-', val++);
                    map.Add('_', val++);

                    string s   = fs.ReadLine();
                    long   ans = 1;
                    foreach (char c in s)
                    {
                        string bin = Convert.ToString(map[c], 2);
                        for (int i = bin.Length + 1; i <= 6; i++)
                        {
                            ans = (ans * 3) % 1000000007;
                        }
                        foreach (char b in bin)
                        {
                            if (b == '0')
                            {
                                ans = (ans * 3) % 1000000007;
                            }
                        }
                    }
                    writer.WriteLine(ans);
                }
        }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int      n = fs.NextInt();
             string[] a = new string[n];
             for (int i = 0; i < n; i++)
             {
                 a[i] = fs.ReadLine();
             }
             Array.Sort(a, new StringComparer());
             StringBuilder sb = new StringBuilder();
             foreach (string str in a)
             {
                 sb.Append(str);
             }
             writer.WriteLine(sb);
         }
 }