Exemple #1
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long A = fs.NextLong(), B = fs.NextLong(), n = fs.NextLong(), x = fs.NextLong();
             if (n == 0)
             {
                 writer.WriteLine(x);
             }
             else if (n == 1)
             {
                 writer.WriteLine((A * x + B) % _mod);
             }
             else
             {
                 long[,] initial = new long[, ] {
                     { A *x + B, 1 }
                 };
                 long[,] factor = new long[, ] {
                     { A, 0 }, { B, 1 }
                 };
                 long[,] res = MultiplyMatricesMod(initial, BinPowMatrixMod(factor, n - 1, _mod), _mod);
                 writer.WriteLine(res[0, 0]);
             }
         }
 }
Exemple #2
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long n = fs.NextLong(), k = fs.NextLong(), m = n - k;
             int  p = fs.NextInt();
             while (p-- > 0)
             {
                 long x = fs.NextLong() - 1;
                 if (m == 0)
                 {
                     writer.Write("X");
                 }
                 else if (k == 0)
                 {
                     writer.Write(".");
                 }
                 else
                 {
                     if (n % 2 == 0)
                     {
                         if (x % 2 == 0 && x <= 2 * Math.Min(m, n / 2) - 2)
                         {
                             writer.Write(".");
                         }
                         else if (x % 2 == 1 && x <= 2 * (m - n / 2) - 1)
                         {
                             writer.Write(".");
                         }
                         else
                         {
                             writer.Write("X");
                         }
                     }
                     else
                     {
                         if (x % 2 == 0 && x <= 2 * Math.Min(m, (n - 1) / 2) - 2)
                         {
                             writer.Write(".");
                         }
                         else if (x % 2 == 1 && x <= 2 * (m - (n - 1) / 2) - 1)
                         {
                             writer.Write(".");
                         }
                         else
                         {
                             writer.Write("X");
                         }
                     }
                 }
             }
         }
 }
Exemple #3
0
 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();
             for (int i = 1; i <= t; i++)
             {
                 long s = fs.NextLong();
                 long upper = (long)Math.Ceiling(Math.Sqrt(s));
                 long lower = upper - 1;
                 long lt = s - lower * lower, x, y;
                 if ((upper & 1) == 0)
                 {
                     x = Math.Min(upper, lt);
                     y = upper - (lt > upper ? (lt - upper) : 0);
                 }
                 else
                 {
                     x = upper - (lt > upper ? (lt - upper) : 0);
                     y = Math.Min(upper, lt);
                 }
                 writer.WriteLine("Case " + i + ": " + x + " " + y);
             }
         }
 }
 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();
             for (int i = 1; i <= t; i++)
             {
                 long w = fs.NextLong();
                 if ((w & 1) == 0)
                 {
                     long m = 1;
                     while ((w & 1) == 0)
                     {
                         m <<= 1;
                         w >>= 1;
                     }
                     writer.WriteLine("Case " + i + ": " + w + " " + m);
                 }
                 else
                 {
                     writer.WriteLine("Case " + i + ": Impossible");
                 }
             }
         }
 }
Exemple #5
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int    k = fs.NextInt(), mod = (int)1e9 + 7;
             long[] a      = new long[k];
             bool   isEven = false;
             for (int i = 0; i < k; i++)
             {
                 a[i] = fs.NextLong();
                 if (!isEven && a[i] % 2 == 0)
                 {
                     isEven = true;
                 }
             }
             long Dn = 2;
             for (int i = 0; i < k; i++)
             {
                 Dn = BinPowMod(Dn, a[i], mod);
             }
             Dn = (Dn * BinPowMod(2, mod - 2, mod)) % mod;
             long Nm = Dn + (isEven ? 1 : -1);
             Nm = (Nm * BinPowMod(3, mod - 2, mod)) % mod;
             writer.WriteLine(Nm + "/" + Dn);
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long   n = fs.NextInt(), m = fs.NextLong();
             long[] a = new long[n];
             for (int i = 0; i < n; i++)
             {
                 a[i] = fs.NextLong();
             }
             Array.Sort(a);
             long          sum = 0, j = 1, k = 0, count = 0;
             StringBuilder sb = new StringBuilder();
             while (sum < m && j <= 1e9)
             {
                 if (k < n && j == a[k])
                 {
                     k++;
                 }
                 else
                 {
                     if (sum + j <= m)
                     {
                         count++;
                         sum += j;
                         sb.Append(j + " ");
                     }
                     else
                     {
                         break;
                     }
                 }
                 j++;
             }
             //string ans = sb.ToString();
             //if (ans.Length > 0 && ans.Last() == ' ') ans = ans.Substring(0, ans.Length - 1);
             writer.WriteLine(count);
             if (count > 0)
             {
                 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())))
         {
             long m = fs.NextLong(), d = 0;
             long count = Solve(m, ref d);
             writer.WriteLine(count + " " + (m - d));
         }
 }
Exemple #8
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long sn     = fs.NextLong();
             long n      = (long)Math.Ceiling((Math.Sqrt(8 * sn + 1) - 1) / 2);
             long number = sn - (n * (n - 1)) / 2;
             writer.WriteLine(number);
         }
 }
Exemple #9
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long l1 = fs.NextLong(), r1 = fs.NextLong();
             long l2 = fs.NextLong(), r2 = fs.NextLong();
             long k = fs.NextLong();
             if ((l1 >= l2 && l1 <= r2) || (l2 >= l1 && l2 <= r1))
             {
                 long r = Math.Min(r1, r2), l = Math.Max(l1, l2);
                 long ans = r - l + 1;
                 writer.WriteLine(k >= l && k <= r ? ans - 1 : ans);
             }
             else
             {
                 writer.WriteLine(0);
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long maxCapacity      = (long)1e16;
             long ways             = fs.NextLong();
             long requiredCapacity = FindVal(1, maxCapacity, ways);
             writer.WriteLine(requiredCapacity);
         }
 }
Exemple #11
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long d = fs.NextLong(), k = fs.NextLong(), a = fs.NextLong(), b = fs.NextLong(), t = fs.NextLong();
             if (k >= d)
             {
                 writer.WriteLine(d * a);
             }
             else
             {
                 long dc  = d / k * k;
                 long res = d * b + ((long)Math.Ceiling(dc * 1.0 / k) - 1) * t + dc * (a - b);
                 res = Math.Min(d * b + ((long)Math.Ceiling(d * 1.0 / k) - 1) * t + d * (a - b), res);
                 res = Math.Min(d * b + k * (a - b), res);
                 writer.WriteLine(res);
             }
         }
 }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             long s = fs.NextLong(), x = fs.NextLong();
             if (s < x || (s - x) % 2 != 0)
             {
                 writer.WriteLine(0);
                 return;
             }
             long   and = (s - x) / 2;
             string xorStr = Convert.ToString(x, 2), andStr = Convert.ToString(and, 2);
             int    onesNum = 0;
             foreach (char c in xorStr)
             {
                 if (c == '1')
                 {
                     onesNum++;
                 }
             }
             string shortStr = xorStr;
             string longStr  = andStr;
             if (xorStr.Length > andStr.Length)
             {
                 shortStr = andStr;
                 longStr  = xorStr;
             }
             int p = longStr.Length - 1;
             for (int i = shortStr.Length - 1; i >= 0; i--, p--)
             {
                 if (shortStr[i] == '1' && longStr[p] == '1')
                 {
                     writer.WriteLine(0);
                     return;
                 }
             }
             writer.WriteLine(Math.Pow(2, onesNum) - (and == 0 ? 2 : 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    mod = (int)1e9 + 7;
                    long   k   = fs.NextLong();
                    long[] a   = new long[n];
                    long[,] D1 = new long[1, n];
                    long[,] C  = new long[n, n];

                    for (int i = 0; i < n; i++)
                    {
                        D1[0, i] = 1;
                        a[i]     = fs.NextLong();
                    }
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            C[i, j] = IsValidPair(a[i], a[j]);
                        }
                    }
                    if (k == 1)
                    {
                        writer.WriteLine(n);
                        return;
                    }
                    long[,] Dk = MatrixProductMod(D1, BinPowMatrixMod(C, k - 1, mod), mod);
                    long sum = 0;
                    for (int i = 0; i < n; i++)
                    {
                        sum += Dk[0, i];
                        sum %= mod;
                    }
                    writer.WriteLine(sum);
                    var s = 0;
                }
        }
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int         q = fs.NextInt();
             long        u, v, w;
             List <Edge> path;
             while (q-- > 0)
             {
                 int t = fs.NextInt();
                 if (t == 1)
                 {
                     u    = fs.NextLong();
                     v    = fs.NextLong();
                     w    = fs.NextLong();
                     path = GetPath(u, v);
                     foreach (var edge in path)
                     {
                         if (!costs.ContainsKey(edge))
                         {
                             costs.Add(edge, 0);
                         }
                         costs[edge] += w;
                     }
                 }
                 else
                 {
                     u    = fs.NextLong();
                     v    = fs.NextLong();
                     path = GetPath(u, v);
                     writer.WriteLine(GetCost(path));
                 }
             }
         }
 }
 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();
             Arr = new long[N];
             for (int i = 0; i < N; i++)
             {
                 Arr[i] = fs.NextLong();
             }
             long sum = Count(0, 0);
             for (int i = 1; i < N; i++)
             {
                 sum += Count(i, i) - Count(i - 1, i);
             }
             Console.WriteLine(sum);
         }
 }
 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();
             long k = fs.NextLong();
             A       = new Hay[N, M];
             Answer  = new int[N, M];
             Visited = new bool[N, M];
             List <Hay>  hays = new List <Hay>(N * M);
             DisjointSet dsu  = new DisjointSet();
             for (int i = 0; i < N; i++)
             {
                 for (int j = 0; j < M; j++)
                 {
                     A[i, j] = new Hay {
                         Id = M * i + j, Row = i, Column = j, Height = fs.NextLong()
                     };
                     hays.Add(A[i, j]);
                 }
             }
             hays = hays.OrderByDescending(h => h.Height).ToList();
             foreach (Hay hay in hays)
             {
                 dsu.MakeSet(hay.Id);
                 if (hay.Row > 0 && hay.Height <= A[hay.Row - 1, hay.Column].Height)
                 {
                     dsu.Union(hay.Id, A[hay.Row - 1, hay.Column].Id);
                 }
                 if (hay.Column > 0 && hay.Height <= A[hay.Row, hay.Column - 1].Height)
                 {
                     dsu.Union(hay.Id, A[hay.Row, hay.Column - 1].Id);
                 }
                 if (hay.Row < N - 1 && hay.Height <= A[hay.Row + 1, hay.Column].Height)
                 {
                     dsu.Union(hay.Id, A[hay.Row + 1, hay.Column].Id);
                 }
                 if (hay.Column < M - 1 && hay.Height <= A[hay.Row, hay.Column + 1].Height)
                 {
                     dsu.Union(hay.Id, A[hay.Row, hay.Column + 1].Id);
                 }
                 int count = dsu.GetSetElementCount(hay.Id);
                 if (k % hay.Height == 0)
                 {
                     long need = k / hay.Height;
                     int  temp = (int)need;
                     if (need <= (long)count)
                     {
                         RunDFS(hay.Row, hay.Column, (int)hay.Height, ref temp);
                         writer.WriteLine("YES");
                         for (int i = 0; i < N; i++)
                         {
                             for (int j = 0; j < M; j++)
                             {
                                 writer.Write(Answer[i, j] + " ");
                             }
                             writer.WriteLine();
                         }
                         return;
                     }
                 }
             }
             writer.WriteLine("NO");
         }
 }