buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
change_count
int64
0
100
using System; using System.Collections.Generic; using System.Linq; public class Prog { public void run() { int N = int.Parse(Console.ReadLine()); var input = Console.ReadLine().Split(); int[] A = Array.ConvertAll(input, int.Parse); input = Console.ReadLine().Split(); int[] B = Array.ConvertAll(input, int.Parse); input = Console.ReadLine().Split(); int[] C = Array.ConvertAll(input, int.Parse); int Old = 0; int New = 0; int ans = 0; for (int i = 0; i < N; i++) { New = B[A[i] - 1]; if (Old == A[i] - 1) { New += C[Old - 1]; } Old = A[i]; ans += New; } Console.WriteLine(ans); } public static void Main() { var prog = new Prog(); prog.run(); } /* 最小公倍数 public static long Lcm(long a, long b) { return a * b / Gcd(a, b); } // ユークリッドの互除法 public static long Gcd(long a, long b) { if (a < b) // 引数を入替えて自分を呼び出す return Gcd(b, a); while (b != 0) { var remainder = a % b; a = b; b = remainder; } return a; } */ // a<=c, b<=d; } // Array.ConvertAll(int.Parse) // str.IndexOf("Stage",System.StringComparison.CurrentCulture)
using System; using System.Collections.Generic; using System.Linq; public class Prog { public void run() { int N = int.Parse(Console.ReadLine()); var input = Console.ReadLine().Split(); int[] A = Array.ConvertAll(input, int.Parse); input = Console.ReadLine().Split(); int[] B = Array.ConvertAll(input, int.Parse); input = Console.ReadLine().Split(); int[] C = Array.ConvertAll(input, int.Parse); int Old = -5; int New = 0; int ans = 0; for (int i = 0; i < N; i++) { New = B[A[i] - 1]; if (Old == A[i] - 1) { New += C[Old - 1]; } Old = A[i]; ans += New; } Console.WriteLine(ans); } public static void Main() { var prog = new Prog(); prog.run(); } /* 最小公倍数 public static long Lcm(long a, long b) { return a * b / Gcd(a, b); } // ユークリッドの互除法 public static long Gcd(long a, long b) { if (a < b) // 引数を入替えて自分を呼び出す return Gcd(b, a); while (b != 0) { var remainder = a % b; a = b; b = remainder; } return a; } */ // a<=c, b<=d; } // Array.ConvertAll(int.Parse) // str.IndexOf("Stage",System.StringComparison.CurrentCulture)
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
235
3
using System; using System.Linq; using System.Text.RegularExpressions; using System.Collections.Generic; using static System.Console; using static Program.Input; using static Program.MyMath; using static Program.Const; namespace Program { static class Const { public static string alp = "abcdefghijklmnopqrstuvwxyz"; public static string Alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public const long MOD = 1000000007; public static int INF = int.MaxValue; public static long INFL = long.MaxValue; } public class Program { public static void Main() { var s = ReadLine(); for (int i = 0; i < s.Length; i++) if (s[i] == "LR"[i % 2]) { WriteLine("No"); } WriteLine("Yes"); } } static class Input { public static void InputLine(out int a, out int b) { var input = ScanIntArray(); a = input[0]; b = input[1]; } public static void InputLine(out int a, out int b, out int c) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; } public static void InputLine(out int a, out int b, out int c, out int d) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; } public static void InputLine(out long a, out long b) { var input = ScanLongArray(); a = input[0]; b = input[1]; } public static void InputLine(out long a, out long b, out long c) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; } public static void InputLine(out long a, out long b, out long c, out long d) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; } public static void InputLine(out string a, out string b) { var input = ScanStrArray(); a = input[0]; b = input[1]; } public static int ScanInt() { return int.Parse(ReadLine()); } static public uint ScanUint() { return uint.Parse(ReadLine()); } static public double ScanDouble() { return double.Parse(ReadLine()); } static public long ScanLong() { return long.Parse(ReadLine()); } static public ulong ScanUlong() { return ulong.Parse(ReadLine()); } static public int[] ScanIntArray() { return ReadLine().Split().Select(x => int.Parse(x)).ToArray(); } static public uint[] ScanUintArray() { return ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); } static public double[] ScanDoubleArray() { return ReadLine().Split().Select(x => double.Parse(x)).ToArray(); } static public long[] ScanLongArray() { return ReadLine().Split().Select(x => long.Parse(x)).ToArray(); } static public ulong[] ScanUlongArray() { return ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); } static public string ScanStr() { return ReadLine(); } static public string[] ScanStrArray() { return ScanStr().Split(); } static public string RegaxReplace(this string arg1, string pattern, string replacement) { return Regex.Replace(arg1, pattern, replacement); } } static class MyMath { static public int CountBit(this long a) { if (a == 0) return 0; int c = 1; for (; a > 1; a /= 2) if (a % 2 == 1) c++; return c; } static public int Floor2(this int a) { int N = 1; while (N < a) N *= 2; return N / 2; } static public long Floor2(this long a) { long N = 1; while (N < a) N *= 2; return N / 2; } static public int Ceiling2(this int a) { int N = 1; while (N < a) N *= 2; return N; } static public long Ceiling2(this long a) { long N = 1; while (N < a) N *= 2; return N; } static public long[] CumSum(this long[] a) { var res = new long[a.Length]; long s = 0; for (int i = 0; i < a.Length; i++) res[i] = (s += a[i]); return res; } static public IEnumerable<int> PrimeFactors(this int n) { int tmp = n; for (int i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; } if (tmp != 1) yield return tmp; } static public IEnumerable<long> PrimeFactors(this long n) { var tmp = n; for (long i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; } if (tmp != 1) yield return tmp; } static public int Digit(this int n, int d) { if (d < 0) return 0; return (n % (int)Math.Pow(10, d + 1)) / (int)Math.Pow(10, d); } static public int Length(this int n) { if (n < 0) return 0; return (int)Math.Log10(n) + 1; } static public int Top(this int n) { return n.Digit(n.Length() - 1); } public static bool isPrime(this int p) { if (p <= 1) return false; for (int i = 2; i * i <= p; i++) if (p % i == 0) return false; return true; } public static bool isPrime(this long p) { if (p <= 1) return false; for (long i = 2; i * i <= p; i++) if (p % i == 0) return false; return true; } public static int GCD(int a, int b) { Func<int, int, int> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static long GCD(long a, long b) { Func<long, long, long> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static ulong extGCD(ulong a, ulong b, ref ulong x, ref ulong y) { if (b == 0) { x = 1; y = 0; return a; } var d = extGCD(b, a % b, ref y, ref x); y -= a / b * x; return d; } public static int LCM(int a, int b) { return a * b / GCD(a, b); } public static long LCM(long a, long b) { return a * b / GCD(a, b); } public static long Inv(this long a, long m = MOD) { long b = m, u = 1, v = 0; while (b != 0) { long t = a / b; a -= t * b; swap(ref a, ref b); u -= t * v; swap(ref u, ref v); } u %= m; if (u < 0) u += m; return u; } private static void swap(ref long a, ref long b) { var t = a; a = b; b = t; } public static long ModPow(long a, long n, long m = MOD) { long res = 1; while (n > 0) { if (n % 2 == 1) res = (res * a) % m; a = (a * a) % m; n >>= 1; } return res; } } static class MyLinq { public static void Sort(this string[] array) { Array.Sort(array, StringComparer.OrdinalIgnoreCase); } public static void StringSort(this List<string> list) { list.Sort(StringComparer.OrdinalIgnoreCase); } public static string Join<T>(this IEnumerable<T> source, string separator) { return string.Join(separator, source); } public static IEnumerable<TItem> Distinct<TItem, TKey>(this IEnumerable<TItem> source, Func<TItem, TKey> keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); var set = new HashSet<TKey>(); foreach (var item in source) if (set.Add(keySelector(item))) yield return item; } } }
using System; using System.Linq; using System.Text.RegularExpressions; using System.Collections.Generic; using static System.Console; using static Program.Input; using static Program.MyMath; using static Program.Const; namespace Program { static class Const { public static string alp = "abcdefghijklmnopqrstuvwxyz"; public static string Alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public const long MOD = 1000000007; public static int INF = int.MaxValue; public static long INFL = long.MaxValue; } public class Program { public static void Main() { var s = ReadLine(); for (int i = 0; i < s.Length; i++) if (s[i] == "LR"[i % 2]) { WriteLine("No"); return; } WriteLine("Yes"); } } static class Input { public static void InputLine(out int a, out int b) { var input = ScanIntArray(); a = input[0]; b = input[1]; } public static void InputLine(out int a, out int b, out int c) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; } public static void InputLine(out int a, out int b, out int c, out int d) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; } public static void InputLine(out long a, out long b) { var input = ScanLongArray(); a = input[0]; b = input[1]; } public static void InputLine(out long a, out long b, out long c) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; } public static void InputLine(out long a, out long b, out long c, out long d) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; } public static void InputLine(out string a, out string b) { var input = ScanStrArray(); a = input[0]; b = input[1]; } public static int ScanInt() { return int.Parse(ReadLine()); } static public uint ScanUint() { return uint.Parse(ReadLine()); } static public double ScanDouble() { return double.Parse(ReadLine()); } static public long ScanLong() { return long.Parse(ReadLine()); } static public ulong ScanUlong() { return ulong.Parse(ReadLine()); } static public int[] ScanIntArray() { return ReadLine().Split().Select(x => int.Parse(x)).ToArray(); } static public uint[] ScanUintArray() { return ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); } static public double[] ScanDoubleArray() { return ReadLine().Split().Select(x => double.Parse(x)).ToArray(); } static public long[] ScanLongArray() { return ReadLine().Split().Select(x => long.Parse(x)).ToArray(); } static public ulong[] ScanUlongArray() { return ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); } static public string ScanStr() { return ReadLine(); } static public string[] ScanStrArray() { return ScanStr().Split(); } static public string RegaxReplace(this string arg1, string pattern, string replacement) { return Regex.Replace(arg1, pattern, replacement); } } static class MyMath { static public int CountBit(this long a) { if (a == 0) return 0; int c = 1; for (; a > 1; a /= 2) if (a % 2 == 1) c++; return c; } static public int Floor2(this int a) { int N = 1; while (N < a) N *= 2; return N / 2; } static public long Floor2(this long a) { long N = 1; while (N < a) N *= 2; return N / 2; } static public int Ceiling2(this int a) { int N = 1; while (N < a) N *= 2; return N; } static public long Ceiling2(this long a) { long N = 1; while (N < a) N *= 2; return N; } static public long[] CumSum(this long[] a) { var res = new long[a.Length]; long s = 0; for (int i = 0; i < a.Length; i++) res[i] = (s += a[i]); return res; } static public IEnumerable<int> PrimeFactors(this int n) { int tmp = n; for (int i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; } if (tmp != 1) yield return tmp; } static public IEnumerable<long> PrimeFactors(this long n) { var tmp = n; for (long i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; } if (tmp != 1) yield return tmp; } static public int Digit(this int n, int d) { if (d < 0) return 0; return (n % (int)Math.Pow(10, d + 1)) / (int)Math.Pow(10, d); } static public int Length(this int n) { if (n < 0) return 0; return (int)Math.Log10(n) + 1; } static public int Top(this int n) { return n.Digit(n.Length() - 1); } public static bool isPrime(this int p) { if (p <= 1) return false; for (int i = 2; i * i <= p; i++) if (p % i == 0) return false; return true; } public static bool isPrime(this long p) { if (p <= 1) return false; for (long i = 2; i * i <= p; i++) if (p % i == 0) return false; return true; } public static int GCD(int a, int b) { Func<int, int, int> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static long GCD(long a, long b) { Func<long, long, long> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static ulong extGCD(ulong a, ulong b, ref ulong x, ref ulong y) { if (b == 0) { x = 1; y = 0; return a; } var d = extGCD(b, a % b, ref y, ref x); y -= a / b * x; return d; } public static int LCM(int a, int b) { return a * b / GCD(a, b); } public static long LCM(long a, long b) { return a * b / GCD(a, b); } public static long Inv(this long a, long m = MOD) { long b = m, u = 1, v = 0; while (b != 0) { long t = a / b; a -= t * b; swap(ref a, ref b); u -= t * v; swap(ref u, ref v); } u %= m; if (u < 0) u += m; return u; } private static void swap(ref long a, ref long b) { var t = a; a = b; b = t; } public static long ModPow(long a, long n, long m = MOD) { long res = 1; while (n > 0) { if (n % 2 == 1) res = (res * a) % m; a = (a * a) % m; n >>= 1; } return res; } } static class MyLinq { public static void Sort(this string[] array) { Array.Sort(array, StringComparer.OrdinalIgnoreCase); } public static void StringSort(this List<string> list) { list.Sort(StringComparer.OrdinalIgnoreCase); } public static string Join<T>(this IEnumerable<T> source, string separator) { return string.Join(separator, source); } public static IEnumerable<TItem> Distinct<TItem, TKey>(this IEnumerable<TItem> source, Func<TItem, TKey> keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); var set = new HashSet<TKey>(); foreach (var item in source) if (set.Add(keySelector(item))) yield return item; } } }
[["+", 0, 7, 8, 57, 64, 196, 0, 37, 0, 38], ["+", 0, 7, 8, 57, 64, 196, 0, 37, 0, 35]]
8
2,008
2
using System; using System.Linq; namespace ABC141 { class Program { static void Main(string[] args) { string S = CIN.String(); for (int i = 0; i < S.Length; i++) { if (i % 2 == 0 && S[i] == 'R') { Console.WriteLine("No"); return; } if (i % 2 == 1 && S[i] == 'L') { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } } internal static class CIN { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
using System; using System.Linq; namespace ABC141 { class Program { static void Main(string[] args) { string S = CIN.String(); for (int i = 0; i < S.Length; i++) { if (i % 2 == 1 && S[i] == 'R') { Console.WriteLine("No"); return; } if (i % 2 == 0 && S[i] == 'L') { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } } internal static class CIN { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203]]
8
289
4
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var s = Console.ReadLine(); Console.WriteLine(!s.Where((c, i) => i + 1 % 2 == 1).Contains('L') && !s.Where((c, i) => i + 1 % 2 == 0).Contains('R') ? "Yes" : "No"); } } }
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var s = Console.ReadLine(); Console.WriteLine(!s.Where((c, i) => (i + 1) % 2 == 1).Contains('L') && !s.Where((c, i) => (i + 1) % 2 == 0).Contains('R') ? "Yes" : "No"); } } }
[["+", 0, 218, 8, 16, 31, 16, 31, 23, 0, 24], ["+", 0, 218, 8, 16, 31, 16, 31, 23, 0, 25]]
8
103
4
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var res = "Yes"; for (int i = 0; i < s.Length; i++) { if (i + 1 % 2 == 0) { if (s[i] == 'R') { res = "No"; break; } } else { if (s[i] == 'L') { res = "No"; break; } } } Console.WriteLine(res); } } }
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var res = "Yes"; for (int i = 0; i < s.Length; i++) { if ((i + 1) % 2 == 0) { if (s[i] == 'R') { res = "No"; break; } } else { if (s[i] == 'L') { res = "No"; break; } } } Console.WriteLine(res); } } }
[["+", 0, 57, 15, 16, 31, 16, 31, 23, 0, 24], ["+", 0, 57, 15, 16, 31, 16, 31, 23, 0, 25]]
8
125
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { static class Program { static void Main() { Solve(); Console.ReadKey(); } static void Solve() { var S = IO.GetString(); var ans = "Yes"; for (int i = 0; i < S.Length; i++) { if ((i % 2 == 0 && S[i] == 'R') || (i % 2 != 0 && S[i] == 'L')) { ans = "No"; break; } } Console.WriteLine(ans); } } static class IO { static public string GetString() { return Console.ReadLine(); } static public int GetInt() { return int.Parse(Console.ReadLine()); } static public long GetLong() { return long.Parse(Console.ReadLine()); } static public List<string> GetStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> GetIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> GetLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { static class Program { static void Main() { Solve(); Console.ReadKey(); } static void Solve() { var S = IO.GetString(); var ans = "Yes"; for (int i = 0; i < S.Length; i++) { if ((i % 2 != 0 && S[i] == 'R') || (i % 2 == 0 && S[i] == 'L')) { ans = "No"; break; } } Console.WriteLine(ans); } } static class IO { static public string GetString() { return Console.ReadLine(); } static public int GetInt() { return int.Parse(Console.ReadLine()); } static public long GetLong() { return long.Parse(Console.ReadLine()); } static public List<string> GetStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> GetIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> GetLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } } } }
[["-", 15, 16, 31, 23, 0, 16, 31, 16, 17, 60], ["+", 15, 16, 31, 23, 0, 16, 31, 16, 17, 79], ["-", 15, 16, 12, 23, 0, 16, 31, 16, 17, 79], ["+", 15, 16, 12, 23, 0, 16, 31, 16, 17, 60]]
8
380
4
using Atcoder; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Atcoder { class Program { static void Main(string[] args) { string S = Console.ReadLine(); char odd = 'R'; char even = 'L'; bool ret = true; for (int i = 1; i < S.Length; i++) { if ((i % 2 == 0 && S[i - 1] == odd) || (i % 2 == 1 && S[i - 1] == even)) { ret = false; break; } } if (ret) { Console.Write("Yes"); } else { Console.Write("No"); } } // static void Main(string[] args) //{ // var temp = new Atcoder.ABC130.D(); // temp.Run(); //} } }
using Atcoder; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Atcoder { class Program { static void Main(string[] args) { string S = Console.ReadLine(); char odd = 'R'; char even = 'L'; bool ret = true; for (int i = 1; i <= S.Length; i++) { if ((i % 2 == 0 && S[i - 1] == odd) || (i % 2 == 1 && S[i - 1] == even)) { ret = false; break; } } if (ret) { Console.Write("Yes"); } else { Console.Write("No"); } } // static void Main(string[] args) //{ // var temp = new Atcoder.ABC130.D(); // temp.Run(); //} } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
178
2
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { string input = Console.ReadLine(); bool flag = true; for (int i = 0; i < input.Length; i++) { if (i % 2 == 1 && input[i] == 'L') { flag = false; } if (i % 2 == 0 && input[i] == 'R') { flag = false; } } if (flag == false) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { string input = Console.ReadLine(); bool flag = true; for (int i = 0; i < input.Length; i++) { if (i % 2 == 1 && input[i] == 'R') { flag = false; } if (i % 2 == 0 && input[i] == 'L') { flag = false; } } if (flag == false) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } }
[["-", 0, 57, 15, 16, 12, 16, 12, 252, 0, 253], ["+", 0, 57, 15, 16, 12, 16, 12, 252, 0, 253]]
8
137
4
namespace AtCoder { using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using static System.Console; using static System.Math; using static Util; class AtCoder { /*------------------------------------------------------------------*/ void Solve() { var S = io.String; var oddEve = S.ToCharArray().Indexed().Split(x => ODD(x.Idx)); var oddSet = new HashSet<char>() { 'R', 'U', 'D' }; var eveSet = new HashSet<char>() { 'L', 'U', 'D' }; var odd = oddEve.Item1.UnIndexed(); var eve = oddEve.Item2.UnIndexed(); io.Yn(odd.All(x => oddSet.Contains(x)) && eve.All(x => eveSet.Contains(x))); } /*------------------------------------------------------------------*/ void Test() { //--- TEST ---// } public static void Main(string[] _) => new AtCoder().Stream(); IO io = new IO(); private void Stream() { /* var exStdIn = new System.IO.StreamReader("stdin.txt"); SetIn(exStdIn); Test(); */ Solve(); io.Flush(); } } class IO { #region INPUT #region FastRead private const int Size = 1 << 12; private readonly Stream _stream = OpenStandardInput(); private readonly byte[] _buffer = new byte[Size]; private int _ptr = 0, _buffLen = 0; private bool HasNextByte() { if (_ptr < _buffLen) return true; _ptr = 0; _buffLen = _stream.Read(_buffer, 0, Size); return _buffLen > 0; } private byte ReadByte() { if (HasNextByte()) return _buffer[_ptr++]; else // inputの末尾の改行がないとerror throw new EndOfStreamException(); } private bool IsAscii(int c) => '!' <= c && c <= '~'; private char NextChar() { var b = ReadByte(); while (!IsAscii(b)) b = ReadByte(); return (char)b; } private string Next() { var sb = new StringBuilder(); for (var b = NextChar(); IsAscii(b); b = (char)ReadByte()) sb.Append(b); return sb.ToString(); } public string String => Next(); public char Char => NextChar(); public int Int => int.Parse(Next()); public long Long => long.Parse(Next()); public double Double => double.Parse(Next()); #endregion #region LazyRead private void scan<T>(ref T x) { if (x is int) __refvalue(__makeref(x), int) = int.Parse(Next()); else if (x is long) __refvalue(__makeref(x), long) = long.Parse(Next()); else if (x is double) __refvalue(__makeref(x), double) = double.Parse(Next()); else if (x is char) __refvalue(__makeref(x), char) = NextChar(); else __refvalue(__makeref(x), string) = Next(); } public void i<T>(out T t) { t = default(T); scan(ref t); } public void i<T, TU>(out T t, out TU u) { i(out t); i(out u); } public void i<T, TU, TV>(out T t, out TU u, out TV v) { i(out t, out u); i(out v); } public void i<T, TU, TV, TW>(out T t, out TU u, out TV v, out TW w) { i(out t, out u); i(out v, out w); } public void i<T, TU, TV, TW, TX>(out T t, out TU u, out TV v, out TW w, out TX x) { i(out t, out u, out v); i(out w, out x); } public void i<T, TU, TV, TW, TX, TY>(out T t, out TU u, out TV v, out TW w, out TX x, out TY y) { i(out t, out u, out v); i(out w, out x, out y); } public void ini<T>(out T[] a, int n) { a = new T[n]; for (int i = 0; i < n; ++i) i<T>(out a[i]); } public void ini<T, TU>(out T[] a, out TU[] b, int n) { a = new T[n]; b = new TU[n]; for (int i = 0; i < n; ++i) i<T, TU>(out a[i], out b[i]); } public void ini<T, TU, TV>(out T[] a, out TU[] b, out TV[] c, int n) { a = new T[n]; b = new TU[n]; c = new TV[n]; for (var i = 0; i < n; ++i) i<T, TU, TV>(out a[i], out b[i], out c[i]); } public void ini<T>(out T[,] a, int h, int w) { a = new T[h, w]; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) i<T>(out a[i, j]); } #endregion #endregion #region OUTPUT private readonly StreamWriter _sw = new StreamWriter(OpenStandardOutput()) { #if DEBUG AutoFlush = true #else AutoFlush = false #endif }; public void Flush() => Out.Flush(); public void YN(bool f) => yn(f, "YES", "NO"); public void Yn(bool f) => yn(f, "Yes", "No"); public void yn(bool f, string y = "yes", string n = "no") => WriteLine(f ? y : n); #endregion public IO() { SetOut(_sw); } } static class Util { public static IEnumerable<TResult> ZIP<T1, T2, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys, Func<T1, T2, TResult> f = null) { using (var e1 = xs.GetEnumerator()) using ( var e2 = ys.GetEnumerator()) while (e1.MoveNext() && e2.MoveNext()) yield return f(e1.Current, e2.Current); } public static IEnumerable<Tuple<T1, T2>> ZIP<T1, T2>(IEnumerable<T1> xs, IEnumerable<T2> ys) => ZIP(xs, ys, (x, y) => new Tuple<T1, T2>(x, y)); public static IEnumerable<TResult> ZIP<T1, T2, T3, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys, IEnumerable<T3> zs, Func<T1, T2, T3, TResult> f) { using (var e1 = xs.GetEnumerator()) using (var e2 = ys.GetEnumerator()) using ( var e3 = zs.GetEnumerator()) while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext()) yield return f(e1.Current, e2.Current, e3.Current); } public static IEnumerable<Tuple<T1, T2, T3>> ZIP<T1, T2, T3>(IEnumerable<T1> xs, IEnumerable<T2> ys, IEnumerable<T3> zs) => ZIP(xs, ys, zs, (x, y, z) => new Tuple<T1, T2, T3>(x, y, z)); public static bool ODD(int x) => x % 2 != 0; public static bool ODD(long x) => x % 2L != 0L; } static class Ex { public struct IndexedItem<T> { public int Idx; public T Val; public IndexedItem(int idx, T val) { Idx = idx; Val = val; } } public static Tuple<IEnumerable<T>, IEnumerable<T>> Split<T>(this IEnumerable<T> xs, Func<T, bool> f) { var ys = xs.Where(f); var zs = xs.Where(x => !f(x)); return Tuple.Create(ys, zs); } public static void Iter<T>(this IEnumerable<T> xs, Action<T> f) { foreach (var x in xs) f(x); } public static void Prt<T>(this T x) => WriteLine(x); public static void Prt<T>(this IEnumerable<T> xs) => xs.Iter(x => WriteLine(x)); public static IEnumerable<IndexedItem<T>> Indexed<T>(this IEnumerable<T> xs) => xs.Select((x, idx) => new IndexedItem<T>(idx, x)); public static IEnumerable<T> UnIndexed<T>(this IEnumerable<IndexedItem<T>> xs) => xs.Select(x => x.Val); } }
namespace AtCoder { using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using static System.Console; using static System.Math; using static Util; class AtCoder { /*------------------------------------------------------------------*/ void Solve() { var S = io.String; var oddEve = S.ToCharArray().Indexed().Split(x => ODD(x.Idx + 1)); var oddSet = new HashSet<char>() { 'R', 'U', 'D' }; var eveSet = new HashSet<char>() { 'L', 'U', 'D' }; var odd = oddEve.Item1.UnIndexed(); var eve = oddEve.Item2.UnIndexed(); io.Yn(odd.All(x => oddSet.Contains(x)) && eve.All(x => eveSet.Contains(x))); } /*------------------------------------------------------------------*/ void Test() { //--- TEST ---// } public static void Main(string[] _) => new AtCoder().Stream(); IO io = new IO(); private void Stream() { /* var exStdIn = new System.IO.StreamReader("stdin.txt"); SetIn(exStdIn); Test(); */ Solve(); io.Flush(); } } class IO { #region INPUT #region FastRead private const int Size = 1 << 12; private readonly Stream _stream = OpenStandardInput(); private readonly byte[] _buffer = new byte[Size]; private int _ptr = 0, _buffLen = 0; private bool HasNextByte() { if (_ptr < _buffLen) return true; _ptr = 0; _buffLen = _stream.Read(_buffer, 0, Size); return _buffLen > 0; } private byte ReadByte() { if (HasNextByte()) return _buffer[_ptr++]; else // inputの末尾の改行がないとerror throw new EndOfStreamException(); } private bool IsAscii(int c) => '!' <= c && c <= '~'; private char NextChar() { var b = ReadByte(); while (!IsAscii(b)) b = ReadByte(); return (char)b; } private string Next() { var sb = new StringBuilder(); for (var b = NextChar(); IsAscii(b); b = (char)ReadByte()) sb.Append(b); return sb.ToString(); } public string String => Next(); public char Char => NextChar(); public int Int => int.Parse(Next()); public long Long => long.Parse(Next()); public double Double => double.Parse(Next()); #endregion #region LazyRead private void scan<T>(ref T x) { if (x is int) __refvalue(__makeref(x), int) = int.Parse(Next()); else if (x is long) __refvalue(__makeref(x), long) = long.Parse(Next()); else if (x is double) __refvalue(__makeref(x), double) = double.Parse(Next()); else if (x is char) __refvalue(__makeref(x), char) = NextChar(); else __refvalue(__makeref(x), string) = Next(); } public void i<T>(out T t) { t = default(T); scan(ref t); } public void i<T, TU>(out T t, out TU u) { i(out t); i(out u); } public void i<T, TU, TV>(out T t, out TU u, out TV v) { i(out t, out u); i(out v); } public void i<T, TU, TV, TW>(out T t, out TU u, out TV v, out TW w) { i(out t, out u); i(out v, out w); } public void i<T, TU, TV, TW, TX>(out T t, out TU u, out TV v, out TW w, out TX x) { i(out t, out u, out v); i(out w, out x); } public void i<T, TU, TV, TW, TX, TY>(out T t, out TU u, out TV v, out TW w, out TX x, out TY y) { i(out t, out u, out v); i(out w, out x, out y); } public void ini<T>(out T[] a, int n) { a = new T[n]; for (int i = 0; i < n; ++i) i<T>(out a[i]); } public void ini<T, TU>(out T[] a, out TU[] b, int n) { a = new T[n]; b = new TU[n]; for (int i = 0; i < n; ++i) i<T, TU>(out a[i], out b[i]); } public void ini<T, TU, TV>(out T[] a, out TU[] b, out TV[] c, int n) { a = new T[n]; b = new TU[n]; c = new TV[n]; for (var i = 0; i < n; ++i) i<T, TU, TV>(out a[i], out b[i], out c[i]); } public void ini<T>(out T[,] a, int h, int w) { a = new T[h, w]; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) i<T>(out a[i, j]); } #endregion #endregion #region OUTPUT private readonly StreamWriter _sw = new StreamWriter(OpenStandardOutput()) { #if DEBUG AutoFlush = true #else AutoFlush = false #endif }; public void Flush() => Out.Flush(); public void YN(bool f) => yn(f, "YES", "NO"); public void Yn(bool f) => yn(f, "Yes", "No"); public void yn(bool f, string y = "yes", string n = "no") => WriteLine(f ? y : n); #endregion public IO() { SetOut(_sw); } } static class Util { public static IEnumerable<TResult> ZIP<T1, T2, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys, Func<T1, T2, TResult> f = null) { using (var e1 = xs.GetEnumerator()) using ( var e2 = ys.GetEnumerator()) while (e1.MoveNext() && e2.MoveNext()) yield return f(e1.Current, e2.Current); } public static IEnumerable<Tuple<T1, T2>> ZIP<T1, T2>(IEnumerable<T1> xs, IEnumerable<T2> ys) => ZIP(xs, ys, (x, y) => new Tuple<T1, T2>(x, y)); public static IEnumerable<TResult> ZIP<T1, T2, T3, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys, IEnumerable<T3> zs, Func<T1, T2, T3, TResult> f) { using (var e1 = xs.GetEnumerator()) using (var e2 = ys.GetEnumerator()) using ( var e3 = zs.GetEnumerator()) while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext()) yield return f(e1.Current, e2.Current, e3.Current); } public static IEnumerable<Tuple<T1, T2, T3>> ZIP<T1, T2, T3>(IEnumerable<T1> xs, IEnumerable<T2> ys, IEnumerable<T3> zs) => ZIP(xs, ys, zs, (x, y, z) => new Tuple<T1, T2, T3>(x, y, z)); public static bool ODD(int x) => x % 2 != 0; public static bool ODD(long x) => x % 2L != 0L; } static class Ex { public struct IndexedItem<T> { public int Idx; public T Val; public IndexedItem(int idx, T val) { Idx = idx; Val = val; } } public static Tuple<IEnumerable<T>, IEnumerable<T>> Split<T>(this IEnumerable<T> xs, Func<T, bool> f) { var ys = xs.Where(f); var zs = xs.Where(x => !f(x)); return Tuple.Create(ys, zs); } public static void Iter<T>(this IEnumerable<T> xs, Action<T> f) { foreach (var x in xs) f(x); } public static void Prt<T>(this T x) => WriteLine(x); public static void Prt<T>(this IEnumerable<T> xs) => xs.Iter(x => WriteLine(x)); public static IEnumerable<IndexedItem<T>> Indexed<T>(this IEnumerable<T> xs) => xs.Select((x, idx) => new IndexedItem<T>(idx, x)); public static IEnumerable<T> UnIndexed<T>(this IEnumerable<IndexedItem<T>> xs) => xs.Select(x => x.Val); } }
[["+", 8, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 8, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
2,006
2
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class Program { static void Main() { string S = Console.ReadLine(); string odd = "RUD"; string even = "LUD"; for (int i = 0; i < S.Length; i++) { if (i % 2 == 0) { // even if (!even.Contains(S[i].ToString())) { Console.WriteLine("No"); return; } } else { // odd if (!odd.Contains(S[i].ToString())) { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } } }
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class Program { static void Main() { string S = Console.ReadLine(); string odd = "RUD"; string even = "LUD"; for (int i = 0; i < S.Length; i++) { if (i % 2 == 0) { // odd if (!odd.Contains(S[i].ToString())) { Console.WriteLine("No"); return; } } else { // even if (!even.Contains(S[i].ToString())) { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } } }
[["-", 0, 57, 15, 241, 0, 213, 63, 214, 205, 22], ["+", 0, 57, 15, 241, 0, 213, 63, 214, 205, 22]]
8
155
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { var dance = Console.ReadLine(); var ret = Check(dance) ? "Yse" : "No"; Console.WriteLine(ret); } public static bool Check(string dance) { for (int i = 0; i < dance.Length; i++) { if (i == 0) { if (dance[i].ToString() == "L") { return false; } } else { if (i % 2 == 1) { if (dance[i].ToString() == "R") { return false; } } else { if (dance[i].ToString() == "L") { return false; } } } } return true; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { var dance = Console.ReadLine(); var ret = Check(dance) ? "Yes" : "No"; Console.WriteLine(ret); } public static bool Check(string dance) { for (int i = 0; i < dance.Length; i++) { if (i == 0) { if (dance[i].ToString() == "L") { return false; } } else { if (i % 2 == 1) { if (dance[i].ToString() == "R") { return false; } } else { if (dance[i].ToString() == "L") { return false; } } } } return true; } }
[["-", 0, 200, 0, 212, 0, 41, 64, 5, 0, 222], ["+", 0, 200, 0, 212, 0, 41, 64, 5, 0, 222]]
8
189
2
using System; using System.Linq; using System.Collections.Generic; namespace B { class Program { static void Main(string[] args) { string s = Console.ReadLine(); int count = 0; for (int i = 0; i < s.Length; i += 2) { if (s[i] == 'R') { count++; } } for (int i = 1; i < s.Length; i += 2) { if (s[i] == 'L') { count++; } } Console.WriteLine(count > 0 ? "No" : "Yes"); } } }
using System; using System.Linq; using System.Collections.Generic; namespace B { class Program { static void Main(string[] args) { string s = Console.ReadLine(); int count = 0; for (int i = 0; i < s.Length; i += 2) { if (s[i] == 'L') { count++; } } for (int i = 1; i < s.Length; i += 2) { if (s[i] == 'R') { count++; } } Console.WriteLine(count > 0 ? "No" : "Yes"); } } }
[["-", 8, 196, 0, 57, 15, 16, 12, 252, 0, 253], ["+", 8, 196, 0, 57, 15, 16, 12, 252, 0, 253]]
8
135
4
using System; using System.Collections.Generic; using System.Linq; class Program { static Scanner sc = new Scanner(); static void Main(string[] args) { var S = sc.ReadStr(); var ans = true; var odd = new char[] { 'R', 'U', 'D' }; var even = new char[] { 'L', 'U', 'D' }; for (int i = 0; i < S.Length; i++) { var c = S[i]; if ((i + 1) % 2 == 0) { if (!even.Contains(c)) { ans = false; break; } } else if ((i + 1) % 2 + 1 == 1) { if (!odd.Contains(c)) { ans = false; break; } } } Console.WriteLine(ans ? "Yes" : "No"); } } class Modulo { public readonly int MOD; private readonly List<int> Facs = new List<int>() { 1 }; public Modulo(int mod = 1000000007) { this.MOD = mod; } public int Mul(int a, int b) { return (int)(Math.BigMul(a, b) % MOD); } public int Div(int a, int b) { return Mul(a, Pow(b, MOD - 2)); } public int Pow(int a, int n) { switch (n) { case 0: return 1; case 1: return a; default: int p1 = Pow(a, n / 2); int p2 = Mul(p1, p1); return ((n % 2) == 0) ? p2 : Mul(p2, a); } } public int Fac(int n) { for (int i = this.Facs.Count; i <= n; i++) { this.Facs.Add(Mul(i, Facs[i - 1])); } return this.Facs[n]; } public int nPr(int n, int r) => Div(Fac(n), Fac(n - r)); public int nCr(int n, int r) { if (n < r) return 0; if (n == r) return 1; int res = Fac(n); res = Div(res, Fac(r)); res = Div(res, Fac(n - r)); return res; } } class Debug { public static void Dump<T>(T[,] array) { Console.WriteLine(); var h = array.GetLength(0); var w = array.GetLength(1); for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { Console.Write(array[r, c]); Console.Write(" "); } Console.WriteLine(); } Console.WriteLine(); } public static void Dump<T>(T[] array) { Console.WriteLine(); Console.WriteLine(string.Join(" ", array)); Console.WriteLine(); } } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } } class HashMap<K, V> : Dictionary<K, V> where V : new() { private V DefaltValue = new V(); public HashMap() {} public HashMap(V defaultValue) { this.DefaltValue = new V(); } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = this.DefaltValue; } set { base[i] = value; } } }
using System; using System.Collections.Generic; using System.Linq; class Program { static Scanner sc = new Scanner(); static void Main(string[] args) { var S = sc.ReadStr(); var ans = true; var odd = new char[] { 'R', 'U', 'D' }; var even = new char[] { 'L', 'U', 'D' }; for (int i = 0; i < S.Length; i++) { var c = S[i]; if ((i + 1) % 2 == 0) { if (!even.Contains(c)) { ans = false; break; } } else if ((i + 1) % 2 == 1) { if (!odd.Contains(c)) { ans = false; break; } } } Console.WriteLine(ans ? "Yes" : "No"); } } class Modulo { public readonly int MOD; private readonly List<int> Facs = new List<int>() { 1 }; public Modulo(int mod = 1000000007) { this.MOD = mod; } public int Mul(int a, int b) { return (int)(Math.BigMul(a, b) % MOD); } public int Div(int a, int b) { return Mul(a, Pow(b, MOD - 2)); } public int Pow(int a, int n) { switch (n) { case 0: return 1; case 1: return a; default: int p1 = Pow(a, n / 2); int p2 = Mul(p1, p1); return ((n % 2) == 0) ? p2 : Mul(p2, a); } } public int Fac(int n) { for (int i = this.Facs.Count; i <= n; i++) { this.Facs.Add(Mul(i, Facs[i - 1])); } return this.Facs[n]; } public int nPr(int n, int r) => Div(Fac(n), Fac(n - r)); public int nCr(int n, int r) { if (n < r) return 0; if (n == r) return 1; int res = Fac(n); res = Div(res, Fac(r)); res = Div(res, Fac(n - r)); return res; } } class Debug { public static void Dump<T>(T[,] array) { Console.WriteLine(); var h = array.GetLength(0); var w = array.GetLength(1); for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { Console.Write(array[r, c]); Console.Write(" "); } Console.WriteLine(); } Console.WriteLine(); } public static void Dump<T>(T[] array) { Console.WriteLine(); Console.WriteLine(string.Join(" ", array)); Console.WriteLine(); } } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } } class HashMap<K, V> : Dictionary<K, V> where V : new() { private V DefaltValue = new V(); public HashMap() {} public HashMap(V defaultValue) { this.DefaltValue = new V(); } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = this.DefaltValue; } set { base[i] = value; } } }
[["-", 0, 57, 75, 57, 15, 16, 31, 16, 17, 72], ["-", 0, 57, 75, 57, 15, 16, 31, 16, 12, 203]]
8
1,213
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { var str = Console.ReadLine(); for (var i = 0; i < str.Length; i++) { if (i % 2 == 0) { if (str[i] == 'R') { Console.WriteLine("No"); return; } } else { if (str[i] == 'L') { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { var str = Console.ReadLine(); for (var i = 0; i < str.Length; i++) { if (i % 2 != 0) { if (str[i] == 'R') { Console.WriteLine("No"); return; } } else { if (str[i] == 'L') { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 79]]
8
143
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Diagnostics; class Myon { public Myon() {} public static int Main() { new Myon().calc(); return 0; } Scanner cin; Func cin2; void calc() { cin = new Scanner(); cin2 = new Func(); string s = cin.next(); int n = s.Length; string ans = "Yes"; for (int i = 0; i < n; i++) { if (i % 2 == 0 && s[i] == 'L') { ans = "No"; break; } if (i % 2 == 1 && s[i] == 'R') { ans = "Yes"; break; } } Console.WriteLine(ans); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class Answer { public void Int2DArrayAns(int[,] x, char c) { for (int i = 0; i < x.GetLength(0); i++) { for (int j = 0; j < x.GetLength(1) - 1; j++) { Console.Write(x[i, j]); Console.Write(c); } Console.WriteLine(x[i, x.GetLength(1) - 1]); } } public void Char2DArrayAns(char[,] x, char c) { for (int i = 0; i < x.GetLength(0); i++) { for (int j = 0; j < x.GetLength(1) - 1; j++) { Console.Write(x[i, j]); Console.Write(c); } Console.WriteLine(x[i, x.GetLength(1) - 1]); } } } class Func { public long GCDLong(long a, long b) { while (a != 0) { long t = a; a = b % a; b = t; } return b; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Diagnostics; class Myon { public Myon() {} public static int Main() { new Myon().calc(); return 0; } Scanner cin; Func cin2; void calc() { cin = new Scanner(); cin2 = new Func(); string s = cin.next(); int n = s.Length; string ans = "Yes"; for (int i = 0; i < n; i++) { if (i % 2 == 0 && s[i] == 'L') { ans = "No"; break; } if (i % 2 == 1 && s[i] == 'R') { ans = "No"; break; } } Console.WriteLine(ans); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class Answer { public void Int2DArrayAns(int[,] x, char c) { for (int i = 0; i < x.GetLength(0); i++) { for (int j = 0; j < x.GetLength(1) - 1; j++) { Console.Write(x[i, j]); Console.Write(c); } Console.WriteLine(x[i, x.GetLength(1) - 1]); } } public void Char2DArrayAns(char[,] x, char c) { for (int i = 0; i < x.GetLength(0); i++) { for (int j = 0; j < x.GetLength(1) - 1; j++) { Console.Write(x[i, j]); Console.Write(c); } Console.WriteLine(x[i, x.GetLength(1) - 1]); } } } class Func { public long GCDLong(long a, long b) { while (a != 0) { long t = a; a = b % a; b = t; } return b; } }
[["-", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222], ["+", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222]]
8
783
2
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { public static void Main() { string s = Console.ReadLine(); var a = s.Where((x, y) => y % 2 == 1) .All(x => x == 'R' || x == 'U' || x == 'D'); var b = s.Where((x, y) => y % 2 == 0) .All(x => x == 'L' || x == 'U' || x == 'D'); Console.WriteLine(a && b ? "Yes" : "No"); } }
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { public static void Main() { string s = Console.ReadLine(); var a = s.Where((x, y) => y % 2 == 1) .All(x => x == 'L' || x == 'U' || x == 'D'); var b = s.Where((x, y) => y % 2 == 0) .All(x => x == 'R' || x == 'U' || x == 'D'); Console.WriteLine(a && b ? "Yes" : "No"); } }
[["-", 8, 16, 31, 16, 31, 16, 12, 252, 0, 253], ["+", 8, 16, 31, 16, 31, 16, 12, 252, 0, 253]]
8
210
4
using System; using System.Linq; using System.Collections.Generic; namespace c { class Program { static void Main(string[] args) { var _ = Console.ReadLine().Split(); var n = long.Parse(_[0]); var k = long.Parse(_[1]); var q = long.Parse(_[2]); var d = new Dictionary<int, long>(); for (var i = 0; i < q; i++) { var a = int.Parse(Console.ReadLine()); if (d.ContainsKey(a)) { d[a] += 1; } else { d.Add(a, 0); } } for (var i = 1; i < n + 1; i++) { var p = q; if (d.ContainsKey(i)) { p -= (d[i] * 2); } Console.WriteLine(k - p > 0 ? "Yes" : "No"); } } } }
using System; using System.Linq; using System.Collections.Generic; namespace c { class Program { static void Main(string[] args) { var _ = Console.ReadLine().Split(); var n = long.Parse(_[0]); var k = long.Parse(_[1]); var q = long.Parse(_[2]); var d = new Dictionary<int, long>(); for (var i = 0; i < q; i++) { var a = int.Parse(Console.ReadLine()); if (d.ContainsKey(a)) { d[a] += 1; } else { d.Add(a, 1); } } for (var i = 1; i < n + 1; i++) { var p = q; if (d.ContainsKey(i)) { p -= d[i]; } Console.WriteLine(k - p > 0 ? "Yes" : "No"); } } } }
[["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["-", 64, 196, 0, 1, 0, 11, 12, 23, 0, 24], ["-", 0, 1, 0, 11, 12, 23, 0, 16, 17, 48], ["-", 0, 1, 0, 11, 12, 23, 0, 16, 12, 203], ["-", 64, 196, 0, 1, 0, 11, 12, 23, 0, 25]]
8
223
6
using System; using System.Linq; class Program { static void Main(string[] args) { string[] NKQ = Console.ReadLine().Split(' '); int N = int.Parse(NKQ[0]); long K = int.Parse(NKQ[1]); long Q = int.Parse(NKQ[2]); long[] point = Enumerable.Repeat(K - Q, N).ToArray(); for (int i = 0; i < Q; i++) { int A = int.Parse(Console.ReadLine()); point[A]++; } for (int a = 0; a < N; a++) { if (point[a] <= 0) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } } }
using System; using System.Linq; class Program { static void Main(string[] args) { string[] NKQ = Console.ReadLine().Split(' '); int N = int.Parse(NKQ[0]); long K = int.Parse(NKQ[1]); int Q = int.Parse(NKQ[2]); long[] point = Enumerable.Repeat(K - Q, N).ToArray(); for (int i = 0; i < Q; i++) { int A = int.Parse(Console.ReadLine()); point[A - 1]++; } for (int a = 0; a < N; a++) { if (point[a] <= 0) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
184
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var NKQ = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); var N = NKQ[0]; var K = NKQ[1]; var Q = NKQ[2]; int[] A = new int[N + 1]; for (int i = 0; i < Q; i++) { var B = int.Parse(Console.ReadLine()); A[B]++; } for (int i = 1; i <= N; i++) { Console.WriteLine(Q - A[i] < K ? "Yse" : "No"); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var NKQ = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); var N = NKQ[0]; var K = NKQ[1]; var Q = NKQ[2]; int[] A = new int[N + 1]; for (int i = 0; i < Q; i++) { var B = int.Parse(Console.ReadLine()); A[B]++; } for (int i = 1; i <= N; i++) { Console.WriteLine(Q - A[i] < K ? "Yes" : "No"); } } } }
[["-", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222]]
8
179
2
using System; class Info { public int p; public string anser = "Yes"; } class Program { static void Main(string[] args) { string S = Console.ReadLine(); string[] arr = S.Split(' '); int N = int.Parse(arr[0]); int K = int.Parse(arr[1]); int Q = int.Parse(arr[2]); Info[] Point = new Info[N]; for (int i = 0; i < Point.Length; i++) { Point[i] = new Info(); Point[i].p = K - Q; } int ANSER; for (int i = 0; i < Q; i++) { ANSER = int.Parse(Console.ReadLine()); Point[ANSER].p++; } for (int i = 0; i < Point.Length; i++) { if (Point[i].p <= 0) { Point[i].anser = "No"; } Console.WriteLine(Point[i].anser); } } }
using System; class Info { public int p; public string anser = "Yes"; } class Program { static void Main(string[] args) { string S = Console.ReadLine(); string[] arr = S.Split(' '); int N = int.Parse(arr[0]); int K = int.Parse(arr[1]); int Q = int.Parse(arr[2]); Info[] Point = new Info[N]; for (int i = 0; i < Point.Length; i++) { Point[i] = new Info(); Point[i].p = K - Q; } int ANSER; for (int i = 0; i < Q; i++) { ANSER = int.Parse(Console.ReadLine()); Point[ANSER - 1].p++; } for (int i = 0; i < Point.Length; i++) { if (Point[i].p <= 0) { Point[i].anser = "No"; } Console.WriteLine(Point[i].anser); } } }
[["+", 205, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 205, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
240
2
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class Program { static void Main() { int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray(); int personCount = input[0]; int point = input[1]; int quizCount = input[2]; int[] person = new int[personCount]; for (int i = 0; i < quizCount; i++) { int a = int.Parse(Console.ReadLine()); person[a - 1]++; } foreach (var p in person) { if (p + point <= 0) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } } } }
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class Program { static void Main() { int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray(); int personCount = input[0]; int point = input[1]; int quizCount = input[2]; int[] person = new int[personCount]; for (int i = 0; i < quizCount; i++) { int a = int.Parse(Console.ReadLine()); person[a - 1]++; } foreach (var p in person) { if (p + point - quizCount <= 0) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } } } }
[["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 33], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 22]]
8
169
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC141 { class Program { static void Main(string[] args) { // b(); c(); } public static void a() { string s = Console.ReadLine(); if (s == "Sunny") Console.WriteLine("Cloudy"); else if (s == "Cloudy") Console.WriteLine("Rainy"); else Console.WriteLine("Sunny"); } public static void b() { string s = Console.ReadLine(); for (int i = 0; i < s.Length; i++) { if (i % 2 == 1) { if (s[i] == 'R') { Console.WriteLine("No"); return; } } else if (i % 2 == 0) { if (s[i] == 'L') { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } public static void c() { var dic = new Dictionary<int, int>(); int[] nkq = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); for (int i = 0; i < nkq[0]; i++) { dic.Add(i, nkq[1] - nkq[2]); } for (int i = 0; i < nkq[2]; i++) { int q = int.Parse(Console.ReadLine()); dic[q]++; } foreach (var x in dic) { if (x.Value > 0) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC141 { class Program { static void Main(string[] args) { // b(); c(); } public static void a() { string s = Console.ReadLine(); if (s == "Sunny") Console.WriteLine("Cloudy"); else if (s == "Cloudy") Console.WriteLine("Rainy"); else Console.WriteLine("Sunny"); } public static void b() { string s = Console.ReadLine(); for (int i = 0; i < s.Length; i++) { if (i % 2 == 1) { if (s[i] == 'R') { Console.WriteLine("No"); return; } } else if (i % 2 == 0) { if (s[i] == 'L') { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } public static void c() { var dic = new Dictionary<int, int>(); int[] nkq = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); for (int i = 0; i < nkq[0]; i++) { dic.Add(i, nkq[1] - nkq[2]); } for (int i = 0; i < nkq[2]; i++) { int q = int.Parse(Console.ReadLine()); dic[q - 1]++; } foreach (var x in dic) { // Console.WriteLine("{0} {1}", x.Key, x.Value); if (x.Value > 0) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } }
[["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
392
2
using System; using System.Linq; class Program { static void Main(string[] args) { var initial = Console.ReadLine(); var N = int.Parse(initial.Split(' ')[0]); var K = int.Parse(initial.Split(' ')[1]); var Q = int.Parse(initial.Split(' ')[2]); if (K > Q) { for (int i = 0; i < N; i++) { Console.WriteLine("Yes"); } return; } var answers = new int[N]; for (int i = 0; i < Q; i++) { answers[int.Parse(Console.ReadLine()) - 1] += 1; } for (int i = 0; i < N; i++) { Console.WriteLine(Q - answers[i] > K ? "Yes" : "No"); } } }
using System; using System.Linq; class Program { static void Main(string[] args) { var initial = Console.ReadLine(); var N = int.Parse(initial.Split(' ')[0]); var K = int.Parse(initial.Split(' ')[1]); var Q = int.Parse(initial.Split(' ')[2]); if (K > Q) { for (int i = 0; i < N; i++) { Console.WriteLine("Yes"); } return; } var answers = new int[N]; for (int i = 0; i < Q; i++) { answers[int.Parse(Console.ReadLine()) - 1] += 1; } for (int i = 0; i < N; i++) { Console.WriteLine(K - Q + answers[i] > 0 ? "Yes" : "No"); } } }
[["-", 0, 28, 0, 41, 15, 16, 31, 16, 31, 22], ["+", 0, 41, 15, 16, 31, 16, 31, 16, 31, 22], ["+", 0, 41, 15, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 28, 0, 41, 15, 16, 31, 16, 17, 72], ["-", 3, 4, 0, 28, 0, 41, 15, 16, 12, 22], ["+", 3, 4, 0, 28, 0, 41, 15, 16, 12, 203]]
8
208
6
using System; class Program { static void Main(string[] args) { // 入力値を取得 string[] input = Console.ReadLine().Split(' '); int n = int.Parse(input[0]); int k = int.Parse(input[1]); int q = int.Parse(input[2]); // 初期値 int[] points = new int[n]; for (int i = 0; i < q; i++) { points[i] = k - q; } // ラウンド中のポイント設定 for (int j = 0; j < q; j++) { int winner = int.Parse(Console.ReadLine()) - 1; points[winner]++; } // 出力 for (int m = 0; m < n; m++) { Console.WriteLine(points[m] <= 0 ? "No" : "Yes"); } } }
using System; class Program { static void Main(string[] args) { // 入力値を取得 string[] input = Console.ReadLine().Split(' '); int n = int.Parse(input[0]); int k = int.Parse(input[1]); int q = int.Parse(input[2]); // 初期値 int[] points = new int[n]; for (int i = 0; i < n; i++) { points[i] = k - q; } // ラウンド中のポイント設定 for (int j = 0; j < q; j++) { int winner = int.Parse(Console.ReadLine()) - 1; points[winner]++; } // 出力 for (int m = 0; m < n; m++) { Console.WriteLine(points[m] <= 0 ? "No" : "Yes"); } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22]]
8
189
2
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace atcoder { class Program { static void Main(string[] args) { var nm = Console.ReadLine().Split(' ').Select(num => int.Parse(num)).ToArray(); var m = nm[1]; var pQueue = new PriorityQueue<int>(nm[0], 1); foreach (var p in Console.ReadLine().Split(' ').Select( num => int.Parse(num))) { pQueue.Push(p); } for (var i = 0; i < m; i++) { pQueue.Push(pQueue.Pop() / 2); } Console.WriteLine(pQueue.ToArray().Sum()); } public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(int maxSize, IComparer<T> comparer) { _heap = new T[maxSize]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="maxSize">max size</param> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int maxSize, int type = 0) { _heap = new T[maxSize]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace atcoder { class Program { static void Main(string[] args) { var nm = Console.ReadLine().Split(' ').Select(num => int.Parse(num)).ToArray(); var m = nm[1]; var pQueue = new PriorityQueue<long>(nm[0], 1); foreach (var p in Console.ReadLine().Split(' ').Select( num => long.Parse(num))) { pQueue.Push(p); } for (var i = 0; i < m; i++) { pQueue.Push(pQueue.Pop() / 2); } Console.WriteLine(pQueue.ToArray().Sum()); } public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(int maxSize, IComparer<T> comparer) { _heap = new T[maxSize]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="maxSize">max size</param> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int maxSize, int type = 0) { _heap = new T[maxSize]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199]]
8
730
4
using System; using System.Collections.Generic; namespace abc141_d { class Program { static void Main(string[] args) { var n = CIn.ReadInt(); var m = CIn.ReadInt(); var pq = new PriorityQueue<int>(); for (var i = 0; i < n; i++) { pq.Enqueue(CIn.ReadInt()); } for (var i = 0; i < m; i++) { var a = pq.Dequeue(); if (a == 0) { Console.WriteLine(0); return; } a /= 2; pq.Enqueue(a); } var ans = 0; for (var i = 0; i < n; i++) { ans += pq.Dequeue(); } Console.WriteLine(ans); } } class PriorityQueue<T> where T : IComparable<T> { private readonly List<T> heap = new List<T>(); private readonly int order; public int Count { get { return heap.Count; } } public PriorityQueue(bool ascOrder = false) { order = ascOrder ? -1 : 1; } public void Enqueue(T element) { var n = heap.Count; heap.Add(element); while (n != 0) { var i = (n - 1) / 2; if (heap[n].CompareTo(heap[i]) * order > 0) { var tmp = heap[n]; heap[n] = heap[i]; heap[i] = tmp; } n = i; } } public T Dequeue() { if (heap.Count == 0) { throw new InvalidOperationException("PriorityQueue is Empty"); } var n = heap.Count - 1; var element = heap[0]; heap[0] = heap[n]; heap.RemoveAt(n); var i = 0; var j = 2 * i + 1; while (j < n) { if (j != n - 1 && heap[j].CompareTo(heap[j + 1]) * order < 0) { j++; } if (heap[i].CompareTo(heap[j]) * order < 0) { var tmp = heap[i]; heap[i] = heap[j]; heap[j] = tmp; } i = j; j = 2 * i + 1; } return element; } public T Peek() { if (heap.Count == 0) { throw new InvalidOperationException("PriorityQueue is Empty"); } return heap[0]; } } class CIn { private static readonly Queue<string> fieldBuffer = new Queue<string>(); public static string Read() { if (fieldBuffer.Count == 0) { foreach (var field in Console.ReadLine().Split()) { fieldBuffer.Enqueue(field); } } return fieldBuffer.Dequeue(); } public static int ReadInt() => int.Parse(Read()); public static long ReadLong() => long.Parse(Read()); public static double ReadDouble() => double.Parse(Read()); public static string[] ReadStringArray(long n) { var array = new string[n]; for (var i = 0; i < n; i++) { array[i] = Read(); } return array; } public static int[] ReadIntArray(long n) { var array = new int[n]; for (var i = 0; i < n; i++) { array[i] = ReadInt(); } return array; } public static long[] ReadLongArray(long n) { var array = new long[n]; for (var i = 0; i < n; i++) { array[i] = ReadLong(); } return array; } public static double[] ReadDoubleArray(long n) { var array = new double[n]; for (var i = 0; i < n; i++) { array[i] = ReadDouble(); } return array; } } }
using System; using System.Collections.Generic; namespace abc141_d { class Program { static void Main(string[] args) { var n = CIn.ReadInt(); var m = CIn.ReadInt(); var pq = new PriorityQueue<int>(); for (var i = 0; i < n; i++) { pq.Enqueue(CIn.ReadInt()); } for (var i = 0; i < m; i++) { var a = pq.Dequeue(); if (a == 0) { Console.WriteLine(0); return; } a /= 2; pq.Enqueue(a); } var ans = 0L; for (var i = 0; i < n; i++) { ans += pq.Dequeue(); } Console.WriteLine(ans); } } class PriorityQueue<T> where T : IComparable<T> { private readonly List<T> heap = new List<T>(); private readonly int order; public int Count { get { return heap.Count; } } public PriorityQueue(bool ascOrder = false) { order = ascOrder ? -1 : 1; } public void Enqueue(T element) { var n = heap.Count; heap.Add(element); while (n != 0) { var i = (n - 1) / 2; if (heap[n].CompareTo(heap[i]) * order > 0) { var tmp = heap[n]; heap[n] = heap[i]; heap[i] = tmp; } n = i; } } public T Dequeue() { if (heap.Count == 0) { throw new InvalidOperationException("PriorityQueue is Empty"); } var n = heap.Count - 1; var element = heap[0]; heap[0] = heap[n]; heap.RemoveAt(n); var i = 0; var j = 2 * i + 1; while (j < n) { if (j != n - 1 && heap[j].CompareTo(heap[j + 1]) * order < 0) { j++; } if (heap[i].CompareTo(heap[j]) * order < 0) { var tmp = heap[i]; heap[i] = heap[j]; heap[j] = tmp; } i = j; j = 2 * i + 1; } return element; } public T Peek() { if (heap.Count == 0) { throw new InvalidOperationException("PriorityQueue is Empty"); } return heap[0]; } } class CIn { private static readonly Queue<string> fieldBuffer = new Queue<string>(); public static string Read() { if (fieldBuffer.Count == 0) { foreach (var field in Console.ReadLine().Split()) { fieldBuffer.Enqueue(field); } } return fieldBuffer.Dequeue(); } public static int ReadInt() => int.Parse(Read()); public static long ReadLong() => long.Parse(Read()); public static double ReadDouble() => double.Parse(Read()); public static string[] ReadStringArray(long n) { var array = new string[n]; for (var i = 0; i < n; i++) { array[i] = Read(); } return array; } public static int[] ReadIntArray(long n) { var array = new int[n]; for (var i = 0; i < n; i++) { array[i] = ReadInt(); } return array; } public static long[] ReadLongArray(long n) { var array = new long[n]; for (var i = 0; i < n; i++) { array[i] = ReadLong(); } return array; } public static double[] ReadDoubleArray(long n) { var array = new double[n]; for (var i = 0; i < n; i++) { array[i] = ReadDouble(); } return array; } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
854
2
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace SplitLength { class Program { static void Main(string[] args) { var array = Console.ReadLine().Split().Select(int.Parse).ToArray(); var N = array[0]; var M = array[1]; var priceArray = Console.ReadLine().Split().Select(int.Parse).ToArray(); var queue = new PriorityQueue4<decimal>(priceArray.Length, (a, b) => a.CompareTo(b)); for (int i = 0; i < priceArray.Length; i++) { queue.Enqueue(priceArray[i]); } for (int i = 0; i < M; i++) { var max = queue.Dequeue(); queue.Enqueue(max / 2); } Console.WriteLine(queue.m_list.Sum(_ => Math.Floor(_))); } class PriorityQueue4<T> { public readonly List<T> m_list; private readonly Func<T, T, int> m_compare; private int m_count; public PriorityQueue4(int capacity, Func<T, T, int> compare) { m_list = new List<T>(capacity); m_compare = compare; m_count = 0; } private int Add(T value) { if (m_count == m_list.Count) { m_list.Add(value); } else { m_list[m_count] = value; } return m_count++; } private void Swap(int a, int b) { T tmp = m_list[a]; m_list[a] = m_list[b]; m_list[b] = tmp; } public void Enqueue(T value) { int c = Add(value); while (c > 0) { int p = (c - 1) / 2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } c = p; } } public T Dequeue() { T value = m_list[0]; m_list[0] = m_list[--m_count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= m_count) { break; } int c = (c2 >= m_count || m_compare(m_list[c1], m_list[c2]) < 0) ? c1 : c2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } p = c; } return value; } } public class PriorityQueue3<T> : IEnumerable<T> { private readonly IComparer<T> _comparer; private readonly List<T> _data; private readonly bool _isDescending; public PriorityQueue3(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _data = new List<T>(); _isDescending = isDescending; } public PriorityQueue3(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue3(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue2<TKey, TValue> : IEnumerable<TValue> where TKey : IComparable<TKey> { private readonly bool _isDescending; private readonly List<KeyValuePair<TKey, TValue>> _data; private readonly Func<TValue, TKey> _keySelector; public PriorityQueue2(Func<TValue, TKey> keySelector, bool isDescending = true) { _isDescending = isDescending; _keySelector = keySelector; _data = new List<KeyValuePair<TKey, TValue>>(); } public void Enqueue(TValue item) { _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item)); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public TValue Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0) childIndex = rightChild; if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem.Value; } public TValue Peek() { return _data[0].Value; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(TKey a, TKey b) { return _isDescending ? b.CompareTo(a) : a.CompareTo(b); } public int Count => _data.Count; public IEnumerator<TValue> GetEnumerator() { return _data.Select(r => r.Value).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T> { private readonly bool _isDescending; private readonly List<T> _data; public PriorityQueue(bool isDescending = true) : this(0, isDescending) {} public PriorityQueue(int capacity, bool isDescending = true) { _data = new List<T>(capacity); _isDescending = isDescending; } public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? b.CompareTo(a) : a.CompareTo(b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace SplitLength { class Program { static void Main(string[] args) { var array = Console.ReadLine().Split().Select(int.Parse).ToArray(); var N = array[0]; var M = array[1]; var priceArray = Console.ReadLine().Split().Select(int.Parse).ToArray(); var queue = new PriorityQueue4<decimal>(priceArray.Length, (a, b) => b.CompareTo(a)); for (int i = 0; i < priceArray.Length; i++) { queue.Enqueue(priceArray[i]); } for (int i = 0; i < M; i++) { var max = queue.Dequeue(); queue.Enqueue(max / 2); } Console.WriteLine(queue.m_list.Sum(_ => Math.Floor(_))); } class PriorityQueue4<T> { public readonly List<T> m_list; private readonly Func<T, T, int> m_compare; private int m_count; public PriorityQueue4(int capacity, Func<T, T, int> compare) { m_list = new List<T>(capacity); m_compare = compare; m_count = 0; } private int Add(T value) { if (m_count == m_list.Count) { m_list.Add(value); } else { m_list[m_count] = value; } return m_count++; } private void Swap(int a, int b) { T tmp = m_list[a]; m_list[a] = m_list[b]; m_list[b] = tmp; } public void Enqueue(T value) { int c = Add(value); while (c > 0) { int p = (c - 1) / 2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } c = p; } } public T Dequeue() { T value = m_list[0]; m_list[0] = m_list[--m_count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= m_count) { break; } int c = (c2 >= m_count || m_compare(m_list[c1], m_list[c2]) < 0) ? c1 : c2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } p = c; } return value; } } public class PriorityQueue3<T> : IEnumerable<T> { private readonly IComparer<T> _comparer; private readonly List<T> _data; private readonly bool _isDescending; public PriorityQueue3(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _data = new List<T>(); _isDescending = isDescending; } public PriorityQueue3(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue3(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue2<TKey, TValue> : IEnumerable<TValue> where TKey : IComparable<TKey> { private readonly bool _isDescending; private readonly List<KeyValuePair<TKey, TValue>> _data; private readonly Func<TValue, TKey> _keySelector; public PriorityQueue2(Func<TValue, TKey> keySelector, bool isDescending = true) { _isDescending = isDescending; _keySelector = keySelector; _data = new List<KeyValuePair<TKey, TValue>>(); } public void Enqueue(TValue item) { _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item)); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public TValue Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0) childIndex = rightChild; if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem.Value; } public TValue Peek() { return _data[0].Value; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(TKey a, TKey b) { return _isDescending ? b.CompareTo(a) : a.CompareTo(b); } public int Count => _data.Count; public IEnumerator<TValue> GetEnumerator() { return _data.Select(r => r.Value).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T> { private readonly bool _isDescending; private readonly List<T> _data; public PriorityQueue(bool isDescending = true) : this(0, isDescending) {} public PriorityQueue(int capacity, bool isDescending = true) { _data = new List<T>(capacity); _isDescending = isDescending; } public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? b.CompareTo(a) : a.CompareTo(b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } }
[["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 22], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 22], ["-", 0, 218, 8, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 218, 8, 213, 3, 4, 0, 28, 0, 22]]
8
1,865
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { // static string S; static int[] N; static long[] A; // static int[] C; static List<long> list = new List<long>(); static void INPUT() { N = LineInt(); // for(var i = 0; i < N[0]; i++) // list.Add( NextInt()); A = LineLong(); } static void OUT() { Array.Sort<long>(A); var pq = new PriorityQueue<long>(); foreach (var a in A) pq.Push(a); for (var i = 0; i < N[1]; i++) { pq.Push(pq.Pop() / 2); } Console.WriteLine(pq.ToArray().Sum()); } static void Main(string[] args) { #if DEBUG TESTINPUT(); INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() {} } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util { /// <summary> /// 優先度付きキュー /// </summary> /// <typeparam name="T"></typeparam> public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(IComparer<T> comparer) { _heap = new T[128]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int type = 0) { _heap = new T[128]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; if (_count > _heap.Length) { var newheap = new T[_heap.Length * 2]; for (int n = 0; n < _heap.Length; n++) newheap[n] = _heap[n]; _heap = newheap; } // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { // static string S; static int[] N; static long[] A; // static int[] C; static List<long> list = new List<long>(); static void INPUT() { N = LineInt(); // for(var i = 0; i < N[0]; i++) // list.Add( NextInt()); A = LineLong(); } static void OUT() { Array.Sort<long>(A); var pq = new PriorityQueue<long>(1); foreach (var a in A) pq.Push(a); for (var i = 0; i < N[1]; i++) { pq.Push(pq.Pop() / 2); } Console.WriteLine(pq.ToArray().Sum()); } static void Main(string[] args) { #if DEBUG TESTINPUT(); INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() {} } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util { /// <summary> /// 優先度付きキュー /// </summary> /// <typeparam name="T"></typeparam> public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(IComparer<T> comparer) { _heap = new T[128]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int type = 0) { _heap = new T[128]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; if (_count > _heap.Length) { var newheap = new T[_heap.Length * 2]; for (int n = 0; n < _heap.Length; n++) newheap[n] = _heap[n]; _heap = newheap; } // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } }
[["+", 0, 212, 0, 230, 3, 4, 0, 28, 0, 203]]
8
1,129
1
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class ABC { static void Main() { Solve(); } private static void Solve() { int N = int.Parse(Console.ReadLine()); string INPUT = Console.ReadLine(); int res = 0; for (int i = 0; i < N - 1; i++) { if (INPUT[i] != INPUT[i + 1]) { res++; } } Console.WriteLine(res); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; namespace Contest { class ABC { static void Main() { Solve(); } private static void Solve() { int N = int.Parse(Console.ReadLine()); string INPUT = Console.ReadLine(); int res = 1; for (int i = 0; i < N - 1; i++) { if (INPUT[i] != INPUT[i + 1]) { res++; } } Console.WriteLine(res); Console.ReadLine(); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
120
2
using System; namespace C___Slimes { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string S = Console.ReadLine(); int Slimes = 1; for (int i = 0; i < S.Length; i++) { if (S[i] != S[i + 1]) { Slimes += 1; } } Console.WriteLine(Slimes); } } }
using System; namespace C___Slimes { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string S = Console.ReadLine(); int Slimes = 1; for (int i = 0; i < S.Length - 1; i++) { if (S[i] != S[i + 1]) { Slimes += 1; } } Console.WriteLine(Slimes); } } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203]]
8
95
2
using System; using System.Linq; namespace lineAralgebra { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var S = Console.ReadLine(); var deleteCount = 0; for (var i = 0; i < S.Length; i++) { for (var j = i + 1; j < S.Length; j++) { if (S[i] == S[j]) deleteCount++; else { i = j - 1; break; } } } System.Console.WriteLine(N - deleteCount); } } } // 0 2 4
using System; using System.Linq; namespace lineAralgebra { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var S = Console.ReadLine(); var deleteCount = 0; for (var i = 0; i < S.Length; i++) { for (var j = i + 1; j < S.Length; j++) { if (S[i] == S[j]) deleteCount++; else { break; } i = j - 1; } } System.Console.WriteLine(N - deleteCount); } } } // 0 2 4
[["+", 8, 196, 0, 57, 75, 196, 0, 93, 0, 94], ["+", 8, 196, 0, 57, 75, 196, 0, 93, 0, 35], ["+", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46], ["-", 8, 196, 0, 57, 75, 196, 0, 1, 0, 35], ["-", 8, 196, 0, 57, 75, 196, 0, 93, 0, 94], ["-", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46]]
8
131
6
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace AtCoder.D { public class Program { public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); } private static object GetResult() { var N = ReadLong(); var L = ReadLongs().OrderByDescending(x => x).ToArray(); var count = 0L; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { var k = L[i] - L[j]; var l = j + 1; var r = N - 1; while (true) { if (L[l] <= k) { break; } if (L[r] > k) { count += r - j; break; } var mid = (l + r) / 2D; var up = (int)Math.Ceiling(mid); if (k >= L[up]) { r = (int)Math.Floor(mid); } else { l = up; } if (r - l == 1 || r == l) { count += r - j; break; } } } } return count; } #region Console public static string ReadText() { return Console.ReadLine(); } public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); } public static long ReadLong() { return long.Parse(Console.ReadLine()); } public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); } #endregion } }
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace AtCoder.D { public class Program { public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); } private static object GetResult() { var N = ReadLong(); var L = ReadLongs().OrderByDescending(x => x).ToArray(); var count = 0L; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { var k = L[i] - L[j]; var l = j + 1; var r = N - 1; while (true) { if (L[l] <= k) { break; } if (L[r] > k) { count += r - j; break; } var mid = (l + r) / 2D; var up = (int)Math.Ceiling(mid); if (k >= L[up]) { r = (int)Math.Floor(mid); } else { l = up; } if (r == l) { count += r - j; break; } } } } return count; } #region Console public static string ReadText() { return Console.ReadLine(); } public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); } public static long ReadLong() { return long.Parse(Console.ReadLine()); } public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); } #endregion } }
[["-", 0, 57, 15, 16, 31, 16, 31, 16, 17, 33], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 12, 22], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 60], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["-", 0, 52, 8, 196, 0, 57, 15, 16, 17, 106], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22]]
8
390
6
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using AtCoder.Collections; using AtCoder.Mathematics; using AtCoder.Extensions; namespace AtCoder { class Program { static void Main(string[] args) { var N = ConsoleExtension.ReadInt(); var array = ConsoleExtension.ReadIntArray().OrderBy(_ => _).ToArray(); var count = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { var a = array[i]; var b = array[j]; count += array.LowerBound(j, array.Length, a + b) - (j + 1); } } Console.WriteLine(count); } } } /* namespace AtCoder.Algorithms { public static partial class Algorithm { } } */ namespace AtCoder.Collections { public class Deque<T> { int dx; T[] buf; int mask; public int Count { get; private set; } public Deque() : this(8) {} public Deque(int capacity) { Debug.Assert(capacity == (capacity & -capacity)); mask = capacity - 1; buf = new T[capacity]; } public T this[int index] { get { Debug.Assert(0 <= index && index < Count); return buf[(dx + index) & mask]; } set { Debug.Assert(0 <= index && index < Count); buf[(dx + index) & mask] = value; } } public void PushFront(T item) { if (Count == buf.Length) extend(); dx = (dx + buf.Length - 1) & mask; buf[dx] = item; Count++; } public T PopFront() { Debug.Assert(Count > 0); var ret = buf[dx = (dx + 1) & mask]; Count--; return ret; } public void PushBack(T item) { if (Count == buf.Length) extend(); buf[(dx + Count++) & mask] = item; } public T PopBack() { Debug.Assert(Count > 0); var ret = buf[(dx + --Count) & mask]; return ret; } public T Last() { Debug.Assert(Count > 0); return buf[(dx + Count - 1) & mask]; } public T First() { Debug.Assert(Count > 0); return buf[dx]; } void extend() { var nbuf = new T[buf.Length * 2]; for (int i = 0; i < buf.Length; i++) nbuf[i] = buf[(dx + i) & mask]; mask = mask * 2 + 1; dx = 0; buf = nbuf; } public void Clear() { Count = 0; } public bool Any() { return Count != 0; } public void Insert(int index, T item) { Debug.Assert(0 <= index && index <= Count); PushFront(item); for (int i = 0; i < index; i++) this[i] = this[i + 1]; this[index] = item; } public T RemoveAt(int index) { Debug.Assert(0 <= index && index < Count); var ret = this[index]; for (int i = index; i > 0; i--) this[i] = this[i - 1]; PopFront(); return ret; } public T[] Items { get { var ret = new T[Count]; for (int i = 0; i < Count; i++) ret[i] = this[i]; return ret; } } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue<TKey, TValue> : IEnumerable<TValue> { private readonly List<KeyValuePair<TKey, TValue>> _data = new List<KeyValuePair<TKey, TValue>>(); private readonly bool _isDescending; private readonly Func<TValue, TKey> _keySelector; private readonly IComparer<TKey> _keyComparer; public PriorityQueue(Func<TValue, TKey> keySelector, bool isDescending = true) : this(keySelector, Comparer<TKey>.Default, isDescending) {} public PriorityQueue(Func<TValue, TKey> keySelector, IComparer<TKey> keyComparer, bool isDescending = true) { _keySelector = keySelector; _keyComparer = keyComparer; _isDescending = isDescending; } public void Enqueue(TValue item) { _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item)); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public TValue Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0) childIndex = rightChild; if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem.Value; } public TValue Peek() { return _data[0].Value; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(TKey a, TKey b) { return _isDescending ? _keyComparer.Compare(b, a) : _keyComparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<TValue> GetEnumerator() { return _data.Select(r => r.Value).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } namespace AtCoder.Mathematics { public class MathEx { /// <summary> /// 最小公倍数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } /// <summary> /// 最大公約数 /// </summary> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); } } public struct ModInt { public const long Mod = (int)1e9 + 7; public long num; public ModInt(long n) { num = n; } public override string ToString() { return num.ToString(); } public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; } public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; } public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); } public static ModInt operator /(ModInt l, ModInt r) { return l * Pow(r, Mod - 2); } public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } public static ModInt Pow(long v, long k) { long ret = 1; for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod) if ((k & 1) == 1) ret = ret * v % Mod; return new ModInt(ret); } public static ModInt Combination(long n, long r) { ModInt ans = 1; for (int i = 0; i < r; i++) { ans *= n - i; } for (int i = 1; i <= r; i++) { ans /= i; } return ans; } public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } } namespace AtCoder.Extensions { public static class Extension { static int BinarySearch<T>(IList<T> source, T value, IComparer<T> comparer, bool isLowerBound) { return BinarySearch(source, 0, source.Count - 1, value, comparer, isLowerBound); } static int BinarySearch<T>(IList<T> source, int start, int end, T value, IComparer<T> comparer, bool isLowerBound) { while (start <= end) { var mid = (start + end) / 2; var result = comparer.Compare(source[mid], value); if (result < 0 || (result == 0 && !isLowerBound)) start = mid + 1; else end = mid - 1; } return start; } public static int LowerBound<T>(this IList<T> source, T value, Comparison<T> f) => BinarySearch(source, value, Comparer<T>.Create(f), true); public static int LowerBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> f) => BinarySearch(source, start, end, value, Comparer<T>.Create(f), true); public static int LowerBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, true); public static int LowerBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, true); public static int UpperBound<T>(this IList<T> source, T value, Comparison<T> cmp) => BinarySearch(source, value, Comparer<T>.Create(cmp), false); public static int UpperBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> cmp) => BinarySearch(source, start, end, value, Comparer<T>.Create(cmp), false); public static int UpperBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, false); public static int UpperBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, false); public static PriorityQueue<T> AsPriorityQueue<T>(this IEnumerable<T> source, bool isDescending = true) { var queue = new PriorityQueue<T>(isDescending); foreach (var item in source) { queue.Enqueue(item); } return queue; } public static PriorityQueue<TKey, TSource> AsPriorityQueue<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool isDescending = true) { var queue = new PriorityQueue<TKey, TSource>(keySelector, isDescending); foreach (var item in source) { queue.Enqueue(item); } return queue; } } public static class ConsoleExtension { public static int ReadInt() => int.Parse(Console.ReadLine()); public static long ReadLong() => long.Parse(Console.ReadLine()); public static string[] ReadstringArray() => Console.ReadLine().Split().ToArray(); public static string[] ReadstringArray(int n) => Enumerable.Range(0, n).Select(_ => Console.ReadLine()).ToArray(); public static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static int[] ReadIntArray(int n) => Enumerable.Range(0, n).Select(_ => ReadInt()).ToArray(); public static long[] ReadLongArray() => Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); public static long[] ReadLongArray(int n) => Enumerable.Range(0, n).Select(_ => ReadLong()).ToArray(); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using AtCoder.Collections; using AtCoder.Mathematics; using AtCoder.Extensions; namespace AtCoder { class Program { static void Main(string[] args) { var N = ConsoleExtension.ReadInt(); var array = ConsoleExtension.ReadIntArray().OrderBy(_ => _).ToArray(); var count = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { var a = array[i]; var b = array[j]; count += array.LowerBound(j, array.Length - 1, a + b) - (j + 1); } } Console.WriteLine(count); } } } /* namespace AtCoder.Algorithms { public static partial class Algorithm { } } */ namespace AtCoder.Collections { public class Deque<T> { int dx; T[] buf; int mask; public int Count { get; private set; } public Deque() : this(8) {} public Deque(int capacity) { Debug.Assert(capacity == (capacity & -capacity)); mask = capacity - 1; buf = new T[capacity]; } public T this[int index] { get { Debug.Assert(0 <= index && index < Count); return buf[(dx + index) & mask]; } set { Debug.Assert(0 <= index && index < Count); buf[(dx + index) & mask] = value; } } public void PushFront(T item) { if (Count == buf.Length) extend(); dx = (dx + buf.Length - 1) & mask; buf[dx] = item; Count++; } public T PopFront() { Debug.Assert(Count > 0); var ret = buf[dx = (dx + 1) & mask]; Count--; return ret; } public void PushBack(T item) { if (Count == buf.Length) extend(); buf[(dx + Count++) & mask] = item; } public T PopBack() { Debug.Assert(Count > 0); var ret = buf[(dx + --Count) & mask]; return ret; } public T Last() { Debug.Assert(Count > 0); return buf[(dx + Count - 1) & mask]; } public T First() { Debug.Assert(Count > 0); return buf[dx]; } void extend() { var nbuf = new T[buf.Length * 2]; for (int i = 0; i < buf.Length; i++) nbuf[i] = buf[(dx + i) & mask]; mask = mask * 2 + 1; dx = 0; buf = nbuf; } public void Clear() { Count = 0; } public bool Any() { return Count != 0; } public void Insert(int index, T item) { Debug.Assert(0 <= index && index <= Count); PushFront(item); for (int i = 0; i < index; i++) this[i] = this[i + 1]; this[index] = item; } public T RemoveAt(int index) { Debug.Assert(0 <= index && index < Count); var ret = this[index]; for (int i = index; i > 0; i--) this[i] = this[i - 1]; PopFront(); return ret; } public T[] Items { get { var ret = new T[Count]; for (int i = 0; i < Count; i++) ret[i] = this[i]; return ret; } } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class PriorityQueue<TKey, TValue> : IEnumerable<TValue> { private readonly List<KeyValuePair<TKey, TValue>> _data = new List<KeyValuePair<TKey, TValue>>(); private readonly bool _isDescending; private readonly Func<TValue, TKey> _keySelector; private readonly IComparer<TKey> _keyComparer; public PriorityQueue(Func<TValue, TKey> keySelector, bool isDescending = true) : this(keySelector, Comparer<TKey>.Default, isDescending) {} public PriorityQueue(Func<TValue, TKey> keySelector, IComparer<TKey> keyComparer, bool isDescending = true) { _keySelector = keySelector; _keyComparer = keyComparer; _isDescending = isDescending; } public void Enqueue(TValue item) { _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item)); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public TValue Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0) childIndex = rightChild; if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem.Value; } public TValue Peek() { return _data[0].Value; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(TKey a, TKey b) { return _isDescending ? _keyComparer.Compare(b, a) : _keyComparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<TValue> GetEnumerator() { return _data.Select(r => r.Value).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } namespace AtCoder.Mathematics { public class MathEx { /// <summary> /// 最小公倍数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } /// <summary> /// 最大公約数 /// </summary> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); } } public struct ModInt { public const long Mod = (int)1e9 + 7; public long num; public ModInt(long n) { num = n; } public override string ToString() { return num.ToString(); } public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; } public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; } public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); } public static ModInt operator /(ModInt l, ModInt r) { return l * Pow(r, Mod - 2); } public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } public static ModInt Pow(long v, long k) { long ret = 1; for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod) if ((k & 1) == 1) ret = ret * v % Mod; return new ModInt(ret); } public static ModInt Combination(long n, long r) { ModInt ans = 1; for (int i = 0; i < r; i++) { ans *= n - i; } for (int i = 1; i <= r; i++) { ans /= i; } return ans; } public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } } namespace AtCoder.Extensions { public static class Extension { static int BinarySearch<T>(IList<T> source, T value, IComparer<T> comparer, bool isLowerBound) { return BinarySearch(source, 0, source.Count - 1, value, comparer, isLowerBound); } static int BinarySearch<T>(IList<T> source, int start, int end, T value, IComparer<T> comparer, bool isLowerBound) { while (start <= end) { var mid = (start + end) / 2; var result = comparer.Compare(source[mid], value); if (result < 0 || (result == 0 && !isLowerBound)) start = mid + 1; else end = mid - 1; } return start; } public static int LowerBound<T>(this IList<T> source, T value, Comparison<T> f) => BinarySearch(source, value, Comparer<T>.Create(f), true); public static int LowerBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> f) => BinarySearch(source, start, end, value, Comparer<T>.Create(f), true); public static int LowerBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, true); public static int LowerBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, true); public static int UpperBound<T>(this IList<T> source, T value, Comparison<T> cmp) => BinarySearch(source, value, Comparer<T>.Create(cmp), false); public static int UpperBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> cmp) => BinarySearch(source, start, end, value, Comparer<T>.Create(cmp), false); public static int UpperBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, false); public static int UpperBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, false); public static PriorityQueue<T> AsPriorityQueue<T>(this IEnumerable<T> source, bool isDescending = true) { var queue = new PriorityQueue<T>(isDescending); foreach (var item in source) { queue.Enqueue(item); } return queue; } public static PriorityQueue<TKey, TSource> AsPriorityQueue<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool isDescending = true) { var queue = new PriorityQueue<TKey, TSource>(keySelector, isDescending); foreach (var item in source) { queue.Enqueue(item); } return queue; } } public static class ConsoleExtension { public static int ReadInt() => int.Parse(Console.ReadLine()); public static long ReadLong() => long.Parse(Console.ReadLine()); public static string[] ReadstringArray() => Console.ReadLine().Split().ToArray(); public static string[] ReadstringArray(int n) => Enumerable.Range(0, n).Select(_ => Console.ReadLine()).ToArray(); public static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static int[] ReadIntArray(int n) => Enumerable.Range(0, n).Select(_ => ReadInt()).ToArray(); public static long[] ReadLongArray() => Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); public static long[] ReadLongArray(int n) => Enumerable.Range(0, n).Select(_ => ReadLong()).ToArray(); } }
[["+", 31, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["+", 31, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
3,080
2
using System; using System.Linq; public class AtCoderD { static void Main() { int n = int.Parse(Console.ReadLine()); int[] nums = new int[n]; { var line = Console.ReadLine(); var words = line.Split(' '); for (int i = 0; i < n; i++) { nums[i] = int.Parse(words[i]); } } Array.Sort(nums); int answer = 0; for (int i = 0; i < n - 2; i++) { int a = nums[i]; for (int j = i + 1; j < n - 1; j++) { int b = nums[j]; int sum = a + b; if (sum > nums[n - 1]) { answer += n - j; continue; } for (int k = j + 1; k < n; k++) { int c = nums[k]; if (sum <= c) { break; } else { answer++; } } } } Console.WriteLine(answer); } }
using System; using System.Linq; public class AtCoderD { static void Main() { int n = int.Parse(Console.ReadLine()); int[] nums = new int[n]; { var line = Console.ReadLine(); var words = line.Split(' '); for (int i = 0; i < n; i++) { nums[i] = int.Parse(words[i]); } } Array.Sort(nums); int answer = 0; for (int i = 0; i < n - 2; i++) { int a = nums[i]; for (int j = i + 1; j < n - 1; j++) { int b = nums[j]; int sum = a + b; if (sum > nums[n - 1]) { answer += n - j - 1; continue; } for (int k = j + 1; k < n; k++) { int c = nums[k]; if (sum <= c) { break; } else { answer++; } } } } Console.WriteLine(answer); } }
[["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
242
2
using System; using System.Collections.Generic; using System.Linq; using static Input; using NX = System.Int64; public class Prog { public struct Pair { public int x, y; public Pair(int a, int b) { x = a; y = b; } } public static void Solve() { int N = NextInt(); int[] L = LineInt().OrderBy(v => v).ToArray(); int ans = 0; for (int x = 0; x < N; x++) for (int y = x + 1; y < N; y++) { int a = L[x], b = L[y]; int ok = -1, ng = N; while (ng - ok != 1) { int mid = (ok + ng) / 2; if (L[mid] < (a + b)) { ok = mid; } else { ng = mid; } } int up = ok; int z = Math.Max(a - b, b - a); ok = N; ng = -1; while (ok - ng != 1) { int mid = (ok + ng) / 2; if (z < L[mid]) { ok = mid; } else { ng = mid; } } int low = ok; int box = up - low + 1; if (low <= x && x <= up) { box--; } if (low <= y && y <= up) { box--; } ans += Math.Max(0, box); } Console.WriteLine(ans / 3); } public static void Main() { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); return; } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } public long Abs(long a, long b) => Math.Abs(a - b); }
using System; using System.Collections.Generic; using System.Linq; using static Input; using NX = System.Int64; public class Prog { public struct Pair { public int x, y; public Pair(int a, int b) { x = a; y = b; } } public static void Solve() { int N = NextInt(); int[] L = LineInt().OrderBy(v => v).ToArray(); NX ans = 0; for (int x = 0; x < N; x++) for (int y = x + 1; y < N; y++) { int a = L[x], b = L[y]; int ok = -1, ng = N; while (ng - ok != 1) { int mid = (ok + ng) / 2; if (L[mid] < (a + b)) { ok = mid; } else { ng = mid; } } int up = ok; int z = Math.Max(a - b, b - a); ok = N; ng = -1; while (ok - ng != 1) { int mid = (ok + ng) / 2; if (z < L[mid]) { ok = mid; } else { ng = mid; } } int low = ok; int box = up - low + 1; if (low <= x && x <= up) { box--; } if (low <= y && y <= up) { box--; } ans += Math.Max(0, box); } Console.WriteLine(ans / 3); } public static void Main() { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); return; } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } public long Abs(long a, long b) => Math.Abs(a - b); }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 22]]
8
713
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jugyo1024 { class Program { static void Main(string[] args) { var _ = Console.ReadLine(); var arr = Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .OrderBy(x => x) .ToArray(); int cnt = 0; for (int i = 0; i < arr.Length; i++) for (int j = i + 1; j < arr.Length; j++) { int c = arr[i] + arr[j]; int n = LowerBound(arr, j + 1, arr.Length, c) - j - 1; if (n > 0) cnt += n; } Console.WriteLine(cnt); } static int LowerBound(int[] arr, int start, int end, int value) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid] <= value) low = mid + 1; else high = mid; } return low; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace jugyo1024 { class Program { static void Main(string[] args) { var _ = Console.ReadLine(); var arr = Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .OrderBy(x => x) .ToArray(); int cnt = 0; for (int i = 0; i < arr.Length; i++) for (int j = i + 1; j < arr.Length; j++) { int c = arr[i] + arr[j]; int n = LowerBound(arr, j + 1, arr.Length, c) - j - 1; if (n > 0) cnt += n; } Console.WriteLine(cnt); } static int LowerBound(int[] arr, int start, int end, int value) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid] < value) low = mid + 1; else high = mid; } return low; } } }
[["-", 0, 52, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 18]]
8
265
2
using System; using System.Linq; using System.Collections.Generic; class ProgramD { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); string[] L = Console.ReadLine().Split(); int count = 0; int[] Li = new int[n]; for (int i = 0; i < n; i++) Li[i] = int.Parse(L[i]); Array.Sort(Li); for (int i = n - 1; i > 2; i--) { int a = Li[i]; for (int j = i - 1; j >= 0; j--) { int b = Li[j]; for (int k = j - 1; k >= 0; k--) { int c = Li[k]; if (a < c + b) count++; else break; } } } Console.WriteLine(count); } }
using System; using System.Linq; using System.Collections.Generic; class ProgramD { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); string[] L = Console.ReadLine().Split(); int count = 0; int[] Li = new int[n]; for (int i = 0; i < n; i++) Li[i] = int.Parse(L[i]); Array.Sort(Li); for (int i = n - 1; i > 1; i--) { int a = Li[i]; for (int j = i - 1; j >= 0; j--) { int b = Li[j]; for (int k = j - 1; k >= 0; k--) { int c = Li[k]; if (a < c + b) count++; else continue; } } } Console.WriteLine(count); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 75, 93, 0, 94], ["+", 0, 7, 8, 196, 0, 57, 75, 116, 0, 117]]
8
215
4
using AtCoderBeginnerContest143.Questions; using AtCoderBeginnerContest143.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace AtCoderBeginnerContest143.Questions { public class QuestionE : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var nml = inputStream.ReadIntArray(); var n = nml[0]; var m = nml[1]; var l = nml[2]; const int sugoiDekaiKazu = int.MaxValue >> 4; var distances = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { distances[i, j] = sugoiDekaiKazu; } } } for (int i = 0; i < m; i++) { var abc = inputStream.ReadIntArray(); var a = abc[0] - 1; var b = abc[1] - 1; var c = abc[2]; distances[a, b] = c; distances[b, a] = c; } WarshalFloyd(distances); var refuelCounts = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { refuelCounts[i, j] = distances[i, j] <= l ? 1 : sugoiDekaiKazu; } } WarshalFloyd(refuelCounts); var q = inputStream.ReadInt(); for (int i = 0; i < q; i++) { var st = inputStream.ReadIntArray(); var s = st[0] - 1; var t = st[1] - 1; var refuel = refuelCounts[s, t]; if (refuel < sugoiDekaiKazu) { yield return refuel - 1; // タンク1回分なら補給不要 } else { yield return -1; } } } private void WarshalFloyd(int[,] distances) { var n = distances.GetLength(0); for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { distances[i, j] = Math.Min(distances[i, j], distances[i, k] + distances[k, j]); } } } } } } namespace AtCoderBeginnerContest143 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionE(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace AtCoderBeginnerContest143.Questions { public interface IAtCoderQuestion { IEnumerable<object> Solve(string input); IEnumerable<object> Solve(TextReader inputStream); } public abstract class AtCoderQuestionBase : IAtCoderQuestion { public IEnumerable<object> Solve(string input) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(input)); var reader = new StreamReader(stream, Encoding.Unicode); return Solve(reader); } public abstract IEnumerable<object> Solve(TextReader inputStream); } } #endregion #region Extensions namespace AtCoderBeginnerContest143.Extensions { internal static class TextReaderExtensions { internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader)); internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader)); internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader)); internal static string ReadString(this TextReader reader) => reader.ReadLine(); internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray(); internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray(); internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray(); internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator); } } #endregion
using AtCoderBeginnerContest143.Questions; using AtCoderBeginnerContest143.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace AtCoderBeginnerContest143.Questions { public class QuestionE : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var nml = inputStream.ReadIntArray(); var n = nml[0]; var m = nml[1]; var l = nml[2]; const int sugoiDekaiKazu = int.MaxValue >> 1; var distances = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { distances[i, j] = sugoiDekaiKazu; } } } for (int i = 0; i < m; i++) { var abc = inputStream.ReadIntArray(); var a = abc[0] - 1; var b = abc[1] - 1; var c = abc[2]; distances[a, b] = c; distances[b, a] = c; } WarshalFloyd(distances); var refuelCounts = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { refuelCounts[i, j] = distances[i, j] <= l ? 1 : sugoiDekaiKazu; } } WarshalFloyd(refuelCounts); var q = inputStream.ReadInt(); for (int i = 0; i < q; i++) { var st = inputStream.ReadIntArray(); var s = st[0] - 1; var t = st[1] - 1; var refuel = refuelCounts[s, t]; if (refuel < sugoiDekaiKazu) { yield return refuel - 1; // タンク1回分なら補給不要 } else { yield return -1; } } } private void WarshalFloyd(int[,] distances) { var n = distances.GetLength(0); for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { distances[i, j] = Math.Min(distances[i, j], distances[i, k] + distances[k, j]); } } } } } } namespace AtCoderBeginnerContest143 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionE(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace AtCoderBeginnerContest143.Questions { public interface IAtCoderQuestion { IEnumerable<object> Solve(string input); IEnumerable<object> Solve(TextReader inputStream); } public abstract class AtCoderQuestionBase : IAtCoderQuestion { public IEnumerable<object> Solve(string input) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(input)); var reader = new StreamReader(stream, Encoding.Unicode); return Solve(reader); } public abstract IEnumerable<object> Solve(TextReader inputStream); } } #endregion #region Extensions namespace AtCoderBeginnerContest143.Extensions { internal static class TextReaderExtensions { internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader)); internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader)); internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader)); internal static string ReadString(this TextReader reader) => reader.ReadLine(); internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray(); internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray(); internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray(); internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator); } } #endregion
[["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
891
2
using System; using System.Collections.Generic; using static Assistant.Input; using System.Linq; using Assistant; using System.Text; namespace ABC143E { class Program { static public long L; static void Main(string[] args) { var N = ReadInt; var M = ReadInt; L = ReadLong; WarshallFloyd wf = new WarshallFloyd(N); for (int i = 0; i < M; i++) { int from = ReadInt - 1; int to = ReadInt - 1; int c = ReadInt; if (c > L) continue; wf.AddEdge(from, to, c); wf.AddEdge(to, from, c); } bool negativeCycle; var distance = wf.Start(out negativeCycle); WarshallFloyd wf2 = new WarshallFloyd(N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (distance[i, j] <= L) wf2.AddEdge(i, j, 1); } } bool negativeCycle2; var count = wf2.Start(out negativeCycle2); var Q = ReadInt; StringBuilder sb = new StringBuilder(); for (int i = 0; i < Q; i++) { int s = ReadInt - 1; int t = ReadInt - 1; long ans = count[s, t]; if (ans == long.MaxValue) sb.AppendLine("-1"); else sb.AppendLine(ans.ToString()); } Console.Write(sb); } } /// <summary> /// ワーシャルフロイド、負の辺に対応、O(V^3)、全頂点間距離 /// </summary> class WarshallFloyd { long INF = long.MaxValue; int _v; long[,] dp; public WarshallFloyd(int V) { _v = V; dp = new long[V, V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dp[i, j] = INF; } } } public void AddEdge(int from, int to, long cost) { dp[from, to] = cost; } public long[,] Start(out bool negativeCycle) { negativeCycle = false; for (int k = 0; k < _v; k++) { for (int i = 0; i < _v; i++) { for (int j = 0; j < _v; j++) { if (dp[i, k] == long.MaxValue || dp[k, j] == long.MaxValue) continue; dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]); } if (dp[i, i] < 0) negativeCycle = true; } } return dp; } public long Distance(int from, int to) { return dp[from, to]; } } } namespace Assistant { static class Input { static List<string> line = new List<string>(); static int index = 0; static String ReadNext() { if (line.Count <= index) line.AddRange(Console.ReadLine().Split()); return line[index++]; } public static int ReadInt => int.Parse(ReadNext()); public static long ReadLong => long.Parse(ReadNext()); public static int[] ReadInts => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static long[] NextLongs => Console.ReadLine().Split().Select(long.Parse).ToArray(); } static class Debug { // drawmap // drawarray } }
using System; using System.Collections.Generic; using static Assistant.Input; using System.Linq; using Assistant; using System.Text; namespace ABC143E { class Program { static public long L; static void Main(string[] args) { var N = ReadInt; var M = ReadInt; L = ReadLong; WarshallFloyd wf = new WarshallFloyd(N); for (int i = 0; i < M; i++) { int from = ReadInt - 1; int to = ReadInt - 1; int c = ReadInt; if (c > L) continue; wf.AddEdge(from, to, c); wf.AddEdge(to, from, c); } bool negativeCycle; var distance = wf.Start(out negativeCycle); WarshallFloyd wf2 = new WarshallFloyd(N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (distance[i, j] <= L) wf2.AddEdge(i, j, 1); } } bool negativeCycle2; var count = wf2.Start(out negativeCycle2); var Q = ReadInt; StringBuilder sb = new StringBuilder(); for (int i = 0; i < Q; i++) { int s = ReadInt - 1; int t = ReadInt - 1; long ans = count[s, t] - 1; if (ans == long.MaxValue - 1) sb.AppendLine("-1"); else sb.AppendLine(ans.ToString()); } Console.Write(sb); } } /// <summary> /// ワーシャルフロイド、負の辺に対応、O(V^3)、全頂点間距離 /// </summary> class WarshallFloyd { long INF = long.MaxValue; int _v; long[,] dp; public WarshallFloyd(int V) { _v = V; dp = new long[V, V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dp[i, j] = INF; } } } public void AddEdge(int from, int to, long cost) { dp[from, to] = cost; } public long[,] Start(out bool negativeCycle) { negativeCycle = false; for (int k = 0; k < _v; k++) { for (int i = 0; i < _v; i++) { for (int j = 0; j < _v; j++) { if (dp[i, k] == long.MaxValue || dp[k, j] == long.MaxValue) continue; dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]); } if (dp[i, i] < 0) negativeCycle = true; } } return dp; } public long Distance(int from, int to) { return dp[from, to]; } } } namespace Assistant { static class Input { static List<string> line = new List<string>(); static int index = 0; static String ReadNext() { if (line.Count <= index) line.AddRange(Console.ReadLine().Split()); return line[index++]; } public static int ReadInt => int.Parse(ReadNext()); public static long ReadLong => long.Parse(ReadNext()); public static int[] ReadInts => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static long[] NextLongs => Console.ReadLine().Split().Select(long.Parse).ToArray(); } static class Debug { // drawmap // drawarray } }
[["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203]]
8
757
4
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var N = sc.ReadInt(); var M = sc.ReadInt(); var L = sc.ReadLong(); var dist = new long[N, N]; var LINF = long.MaxValue / 2; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dist[i, j] = LINF; } } var G = new List<Tuple<int, long>>[N]; for (int i = 0; i < N; i++) { G[i] = new List<Tuple<int, long>>(); } for (int i = 0; i < M; i++) { var a = sc.ReadInt(); var b = sc.ReadInt(); a--; b--; var c = sc.ReadLong(); if (c > L) continue; G[a].Add(Tuple.Create(b, c)); G[b].Add(Tuple.Create(a, c)); dist[a, b] = c; dist[b, a] = c; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (dist[i, k] == LINF || dist[k, j] == LINF) continue; dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]); } } } var dp = new long[N, N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i, j] = LINF; if (dist[i, j] <= L) dp[i, j] = 1; } } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (dp[i, k] == LINF || dp[k, j] == LINF) continue; dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]); } } } var Q = sc.ReadInt(); for (int i = 0; i < Q; i++) { var s = sc.ReadInt(); var t = sc.ReadInt(); s--; t--; var ans = dp[s, t] - 1; if (ans == LINF) ans = -1; Console.WriteLine(ans); } } class WarshallFloyd { public int V { get; set; } public long[,] DP { get; set; } public long INF = long.MaxValue; public bool[] Negative; public WarshallFloyd(int v) { this.V = v; this.DP = new long[V, V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { this.DP[i, j] = INF; } this.DP[i, i] = 0; } } public void SetCost(int a, int b, long c = 1) { this.DP[a, b] = c; } public long[,] CalcMinCost() { this.Negative = new bool[V]; for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (DP[i, k] == INF || DP[k, j] == INF) continue; DP[i, j] = Math.Min(DP[i, j], DP[i, k] + DP[k, j]); } } } for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (DP[i, k] == INF || DP[k, j] == INF) continue; if (DP[i, j] > DP[i, k] + DP[k, j]) { this.Negative[i] = true; this.Negative[j] = true; this.Negative[k] = true; } } } } return DP; } public long GetMinCost(int a, int b) { return DP[a, b]; } public bool IsINF(int a, int b) { return DP[a, b] >= INF; } public bool IsNegative(params int[] vs) { if (vs.Length == 0) vs = Enumerable.Range(0, V).ToArray(); foreach (var v in vs) { if (Negative[v]) return true; } return false; } } static Scanner sc = new Scanner(); } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var N = sc.ReadInt(); var M = sc.ReadInt(); var L = sc.ReadLong(); var dist = new long[N, N]; var LINF = long.MaxValue / 2; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dist[i, j] = LINF; } } var G = new List<Tuple<int, long>>[N]; for (int i = 0; i < N; i++) { G[i] = new List<Tuple<int, long>>(); } for (int i = 0; i < M; i++) { var a = sc.ReadInt(); var b = sc.ReadInt(); a--; b--; var c = sc.ReadLong(); if (c > L) continue; G[a].Add(Tuple.Create(b, c)); G[b].Add(Tuple.Create(a, c)); dist[a, b] = c; dist[b, a] = c; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (dist[i, k] == LINF || dist[k, j] == LINF) continue; dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]); } } } var dp = new long[N, N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i, j] = LINF; if (dist[i, j] <= L) dp[i, j] = 1; } } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (dp[i, k] == LINF || dp[k, j] == LINF) continue; dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]); } } } var Q = sc.ReadInt(); for (int i = 0; i < Q; i++) { var s = sc.ReadInt(); var t = sc.ReadInt(); s--; t--; var ans = dp[s, t] - 1; if (dp[s, t] == LINF) ans = -1; Console.WriteLine(ans); } } class WarshallFloyd { public int V { get; set; } public long[,] DP { get; set; } public long INF = long.MaxValue; public bool[] Negative; public WarshallFloyd(int v) { this.V = v; this.DP = new long[V, V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { this.DP[i, j] = INF; } this.DP[i, i] = 0; } } public void SetCost(int a, int b, long c = 1) { this.DP[a, b] = c; } public long[,] CalcMinCost() { this.Negative = new bool[V]; for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (DP[i, k] == INF || DP[k, j] == INF) continue; DP[i, j] = Math.Min(DP[i, j], DP[i, k] + DP[k, j]); } } } for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (DP[i, k] == INF || DP[k, j] == INF) continue; if (DP[i, j] > DP[i, k] + DP[k, j]) { this.Negative[i] = true; this.Negative[j] = true; this.Negative[k] = true; } } } } return DP; } public long GetMinCost(int a, int b) { return DP[a, b]; } public bool IsINF(int a, int b) { return DP[a, b] >= INF; } public bool IsNegative(params int[] vs) { if (vs.Length == 0) vs = Enumerable.Range(0, V).ToArray(); foreach (var v in vs) { if (Negative[v]) return true; } return false; } } static Scanner sc = new Scanner(); } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 70], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 21], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 73]]
8
2,149
7
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace AtCoder { partial class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static int[] ReadIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } static long[] ReadLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static int[] ZAlgotithm(string s) { var len = s.Length; var a = new int[len]; a[0] = len; var i = 1; var j = 0; while (i < len) { while (i + j < len && s[j] == s[i + j]) j++; a[i] = j; if (j == 0) { i++; continue; } var k = 1; while (i + k < len && k + a[k] < j) { a[i + k] = a[k]; k++; } i += k; j -= k; } return a; } public class MaxHeap<T> : IEnumerable<T> { private List<T> _list; private Comparer<T> _comp; public int Count { get { return _list.Count; } } public MaxHeap() : this(0) {} public MaxHeap(int capacity, Comparison<T> comp = null) { _list = new List<T>(capacity); if (comp == null) { _comp = Comparer<T>.Default; } else { _comp = Comparer<T>.Create(comp); } } public void Push(T elem) { Upheap(elem); } public T Pop() { return Downheap(); } public T Peek() { return _list[0]; } private void Swap(int i, int j) { var a = _list[j]; _list[j] = _list[i]; _list[i] = a; } private void Upheap(T elem) { _list.Add(elem); var i = _list.Count - 1; while (true) { if (i == 0) return; var j = (i - 1) / 2; var e = _list[i]; var p = _list[j]; var c = _comp.Compare(e, p); if (c > 0) { Swap(i, j); i = j; continue; } else { break; } } } private T Downheap() { var result = _list[0]; var last = _list.Count - 1; Swap(0, last); _list.RemoveAt(last); if (_list.Count == 0) return result; var i = 0; while (true) { if (i == _list.Count - 1) return result; var p = _list[i]; var j = i * 2 + 1; if (j >= _list.Count) return result; if (j + 1 < _list.Count && _comp.Compare(_list[j], _list[j + 1]) < 0) { j++; } if (_comp.Compare(_list[i], _list[j]) < 0) { Swap(i, j); i = j; } else { return result; } } } public MaxHeap<T> Copy(Comparison<T> comp = null) { var c = new MaxHeap<T>(0, comp); c._list = _list.ToList(); return c; } public MaxHeap(List<T> list) { _list = list; _comp = Comparer<T>.Default; } public List<T> GetList() { return _list; } public IEnumerator<T> GetEnumerator() { return _list.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } class InputLine { private string[] _inputs; private int _index = 0; InputLine(string input) { _inputs = input.Split(); _index = 0; } public static InputLine Read() { return new InputLine(Console.ReadLine()); } public int NextInt() { return int.Parse(_inputs[_index++]); } public long NextLong() { return long.Parse(_inputs[_index++]); } public double NextDouble() { return double.Parse(_inputs[_index++]); } } public static int GCD(int x, int y) { var a = Math.Max(x, y); var b = x + y - a; while (b != 0) { var c = b; b = a % b; a = c; } return a; } public static long GCD(long x, long y) { var a = Math.Max(x, y); var b = x + y - a; while (b != 0) { var c = b; b = a % b; a = c; } return a; } static long ModPower(long x, long y, long mod) { var bytes = BitConverter.GetBytes(y); var bits = new System.Collections.BitArray(bytes); var max2 = 0; for (int i = bits.Length - 1; i >= 0; i--) { if (bits[i]) { max2 = i; break; } } long result = 1; for (int i = max2; i >= 0; i--) { result = (result * result) % mod; if (bits[i]) { result = (result * x) % mod; } } return result; } static long ModFact(long n, long mod) { long result = 1; for (int i = 1; i <= n; i++) { result = result * i % mod; } return result; } static long ModMal(long x, long y, long mod) { return ((x % mod) * (y % mod)) % mod; } static long ModDiv(long x, long y, long mod) { //フェルマーの小定理より、a^(-1) = a^(p-2) return ModMal(x, ModPower(y, mod - 2, mod), mod); } static long ModComb(long n, long r, long mod) { long numerator = 1; for (int i = 0; i < r; i++) { numerator = ModMal(numerator, n - i, mod); } long denominator = ModFact(r, mod); return ModDiv(numerator, denominator, mod); } class Graph { private int _size; private List<int>[] _map; public IEnumerable<int> this[int i] { get { if (_map[i] == null) return Enumerable.Empty<int>(); return _map[i]; } } public Graph(int size) { _size = size; _map = new List<int>[size]; } public void Add(int from, int to) { if (_map[from] == null) { _map[from] = new List<int>(); } _map[from].Add(to); } } static long p1097 = 1000000007; static void Main(string[] args) { //↓ var line = InputLine.Read(); var n = line.NextInt(); var m = line.NextInt(); var l = line.NextInt(); var edges = "".Select(s => new { A = 1, B = 2, C = 3 }).ToList(); edges.Clear(); for (int i = 0; i < m; i++) { var abc = ReadIntArray(); edges.Add(new { A = abc[0], B = abc[1], C = abc[2] }); } var q = ReadInt(); var sts = "".Select(s => new { s = 0, t = 0 }).ToList(); sts.Clear(); for (int i = 0; i < q; i++) { var st = ReadIntArray(); sts.Add(new { s = st[0], t = st[1] }); } var g = new double[n + 1, n + 1]; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { if (i == j) { g[i, j] = 0; } else { g[i, j] = double.PositiveInfinity; g[j, i] = double.PositiveInfinity; } } } foreach (var e in edges) { g[e.A, e.B] = e.C; g[e.B, e.A] = e.C; } for (int k = 1; k <= n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i, j] = Math.Min(g[i, j], g[i, k] + g[k, j]); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; var c = g[i, j]; if (c <= l) g[i, j] = 1; else g[i, j] = double.PositiveInfinity; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { g[i, j] = Math.Min(g[i, j], g[i, k] + g[k, j]); } } } foreach (var st in sts) { var c = g[st.s, st.t]; if (c == double.PositiveInfinity) Console.WriteLine(-1); else Console.WriteLine(c - 1); } //↑ } } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace AtCoder { partial class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static int[] ReadIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } static long[] ReadLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static int[] ZAlgotithm(string s) { var len = s.Length; var a = new int[len]; a[0] = len; var i = 1; var j = 0; while (i < len) { while (i + j < len && s[j] == s[i + j]) j++; a[i] = j; if (j == 0) { i++; continue; } var k = 1; while (i + k < len && k + a[k] < j) { a[i + k] = a[k]; k++; } i += k; j -= k; } return a; } public class MaxHeap<T> : IEnumerable<T> { private List<T> _list; private Comparer<T> _comp; public int Count { get { return _list.Count; } } public MaxHeap() : this(0) {} public MaxHeap(int capacity, Comparison<T> comp = null) { _list = new List<T>(capacity); if (comp == null) { _comp = Comparer<T>.Default; } else { _comp = Comparer<T>.Create(comp); } } public void Push(T elem) { Upheap(elem); } public T Pop() { return Downheap(); } public T Peek() { return _list[0]; } private void Swap(int i, int j) { var a = _list[j]; _list[j] = _list[i]; _list[i] = a; } private void Upheap(T elem) { _list.Add(elem); var i = _list.Count - 1; while (true) { if (i == 0) return; var j = (i - 1) / 2; var e = _list[i]; var p = _list[j]; var c = _comp.Compare(e, p); if (c > 0) { Swap(i, j); i = j; continue; } else { break; } } } private T Downheap() { var result = _list[0]; var last = _list.Count - 1; Swap(0, last); _list.RemoveAt(last); if (_list.Count == 0) return result; var i = 0; while (true) { if (i == _list.Count - 1) return result; var p = _list[i]; var j = i * 2 + 1; if (j >= _list.Count) return result; if (j + 1 < _list.Count && _comp.Compare(_list[j], _list[j + 1]) < 0) { j++; } if (_comp.Compare(_list[i], _list[j]) < 0) { Swap(i, j); i = j; } else { return result; } } } public MaxHeap<T> Copy(Comparison<T> comp = null) { var c = new MaxHeap<T>(0, comp); c._list = _list.ToList(); return c; } public MaxHeap(List<T> list) { _list = list; _comp = Comparer<T>.Default; } public List<T> GetList() { return _list; } public IEnumerator<T> GetEnumerator() { return _list.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } class InputLine { private string[] _inputs; private int _index = 0; InputLine(string input) { _inputs = input.Split(); _index = 0; } public static InputLine Read() { return new InputLine(Console.ReadLine()); } public int NextInt() { return int.Parse(_inputs[_index++]); } public long NextLong() { return long.Parse(_inputs[_index++]); } public double NextDouble() { return double.Parse(_inputs[_index++]); } } public static int GCD(int x, int y) { var a = Math.Max(x, y); var b = x + y - a; while (b != 0) { var c = b; b = a % b; a = c; } return a; } public static long GCD(long x, long y) { var a = Math.Max(x, y); var b = x + y - a; while (b != 0) { var c = b; b = a % b; a = c; } return a; } static long ModPower(long x, long y, long mod) { var bytes = BitConverter.GetBytes(y); var bits = new System.Collections.BitArray(bytes); var max2 = 0; for (int i = bits.Length - 1; i >= 0; i--) { if (bits[i]) { max2 = i; break; } } long result = 1; for (int i = max2; i >= 0; i--) { result = (result * result) % mod; if (bits[i]) { result = (result * x) % mod; } } return result; } static long ModFact(long n, long mod) { long result = 1; for (int i = 1; i <= n; i++) { result = result * i % mod; } return result; } static long ModMal(long x, long y, long mod) { return ((x % mod) * (y % mod)) % mod; } static long ModDiv(long x, long y, long mod) { //フェルマーの小定理より、a^(-1) = a^(p-2) return ModMal(x, ModPower(y, mod - 2, mod), mod); } static long ModComb(long n, long r, long mod) { long numerator = 1; for (int i = 0; i < r; i++) { numerator = ModMal(numerator, n - i, mod); } long denominator = ModFact(r, mod); return ModDiv(numerator, denominator, mod); } class Graph { private int _size; private List<int>[] _map; public IEnumerable<int> this[int i] { get { if (_map[i] == null) return Enumerable.Empty<int>(); return _map[i]; } } public Graph(int size) { _size = size; _map = new List<int>[size]; } public void Add(int from, int to) { if (_map[from] == null) { _map[from] = new List<int>(); } _map[from].Add(to); } } static long p1097 = 1000000007; static void Main(string[] args) { //↓ var line = InputLine.Read(); var n = line.NextInt(); var m = line.NextInt(); var l = line.NextInt(); var edges = "".Select(s => new { A = 1, B = 2, C = 3 }).ToList(); edges.Clear(); for (int i = 0; i < m; i++) { var abc = ReadIntArray(); edges.Add(new { A = abc[0], B = abc[1], C = abc[2] }); } var q = ReadInt(); var sts = "".Select(s => new { s = 0, t = 0 }).ToList(); sts.Clear(); for (int i = 0; i < q; i++) { var st = ReadIntArray(); sts.Add(new { s = st[0], t = st[1] }); } var g = new double[n + 1, n + 1]; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { if (i == j) { g[i, j] = 0; } else { g[i, j] = double.PositiveInfinity; g[j, i] = double.PositiveInfinity; } } } foreach (var e in edges) { g[e.A, e.B] = e.C; g[e.B, e.A] = e.C; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { g[i, j] = Math.Min(g[i, j], g[i, k] + g[k, j]); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; var c = g[i, j]; if (c <= l) g[i, j] = 1; else g[i, j] = double.PositiveInfinity; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { g[i, j] = Math.Min(g[i, j], g[i, k] + g[k, j]); } } } foreach (var st in sts) { var c = g[st.s, st.t]; if (c == double.PositiveInfinity) Console.WriteLine(-1); else Console.WriteLine(c - 1); } //↑ } } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
8
2,166
8
using System; using System.Linq; using System.Collections.Generic; class ProgramE { static void Main(string[] args) { string[] input = Console.ReadLine().Split(); int n = int.Parse(input[0]); int m = int.Parse(input[1]); long L = long.Parse(input[2]); int[,] road = new int[n, n]; int[,] gas = new int[n, n]; int Inf = 100000000; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == j) road[i, j] = 0; else road[i, j] = Inf; } for (int i = 0; i < m; i++) { string[] input_road = Console.ReadLine().Split(); int a = int.Parse(input_road[0]); int b = int.Parse(input_road[1]); int c = int.Parse(input_road[2]); road[a - 1, b - 1] = c; road[b - 1, a - 1] = c; } for (int s = 0; s < n; s++) for (int t = 0; t < n; t++) for (int u = 0; u < n; u++) road[t, u] = Math.Min(road[t, u], road[t, s] + road[s, u]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (road[i, j] <= L) gas[i, j] = 1; else gas[i, j] = Inf; } for (int s = 0; s < n; s++) for (int t = 0; t < n; t++) for (int u = 0; u < n; u++) gas[t, u] = Math.Min(gas[t, u], gas[t, s] + gas[s, u]); string input_Q = Console.ReadLine(); int Q = int.Parse(input_Q); for (int i = 0; i < Q; i++) { string[] input_st = Console.ReadLine().Split(); int s = int.Parse(input_st[0]); int t = int.Parse(input_st[1]); if (gas[s - 1, t - 1] >= Inf) Console.WriteLine("-1"); else Console.WriteLine(gas[s - 1, t - 1] - 1); } } }
using System; using System.Linq; using System.Collections.Generic; class ProgramE { static void Main(string[] args) { string[] input = Console.ReadLine().Split(); int n = int.Parse(input[0]); int m = int.Parse(input[1]); long L = long.Parse(input[2]); int[,] road = new int[n, n]; int[,] gas = new int[n, n]; int Inf = 1000000000 + 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == j) road[i, j] = 0; else road[i, j] = Inf; } for (int i = 0; i < m; i++) { string[] input_road = Console.ReadLine().Split(); int a = int.Parse(input_road[0]); int b = int.Parse(input_road[1]); int c = int.Parse(input_road[2]); road[a - 1, b - 1] = c; road[b - 1, a - 1] = c; } for (int s = 0; s < n; s++) for (int t = 0; t < n; t++) for (int u = 0; u < n; u++) road[t, u] = Math.Min(road[t, u], road[t, s] + road[s, u]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (road[i, j] <= L) gas[i, j] = 1; else gas[i, j] = Inf; } for (int s = 0; s < n; s++) for (int t = 0; t < n; t++) for (int u = 0; u < n; u++) gas[t, u] = Math.Min(gas[t, u], gas[t, s] + gas[s, u]); string input_Q = Console.ReadLine(); int Q = int.Parse(input_Q); for (int i = 0; i < Q; i++) { string[] input_st = Console.ReadLine().Split(); int s = int.Parse(input_st[0]); int t = int.Parse(input_st[1]); if (gas[s - 1, t - 1] >= Inf) Console.WriteLine("-1"); else Console.WriteLine(gas[s - 1, t - 1] - 1); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 31, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
597
4
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { Scanner cin = new Scanner(); int n = cin.Int(); int m = cin.Int(); int l = cin.Int(); long inf = (long)(1e9) + 1; var d = new long[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { d[i, j] = inf; } } } for (int i = 0; i < m; i++) { int a = cin.Int() - 1; int b = cin.Int() - 1; long c = cin.Long(); d[a, b] = c; d[b, a] = c; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { d[j, k] = Math.Min(d[j, k], d[j, i] + d[i, k]); } } } var d2 = new long[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i, j] <= l) { d2[i, j] = 1; } else { d2[i, j] = inf; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { d2[j, k] = Math.Min(d2[j, k], d2[j, i] + d2[i, k]); } } } int q = cin.Int(); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { int a = cin.Int() - 1; int b = cin.Int() - 1; if (d[a, b] >= inf) { sb.AppendLine("-1"); } else { sb.AppendLine((d2[a, b] - 1).ToString()); } } Console.WriteLine(sb.ToString()); } } public class Scanner { private readonly char[] delimiter_ = new char[] { ' ' }; private string[] buf_; private int index_; public Scanner() { buf_ = new string[0]; index_ = 0; } public string Next() { if (index_ < buf_.Length) { return buf_[index_++]; } string st = Console.ReadLine(); while (st == "") { st = Console.ReadLine(); } buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries); if (buf_.Length == 0) { return Next(); } index_ = 0; return buf_[index_++]; } public int Int() => int.Parse(Next()); public long Long() => long.Parse(Next()); public double Double() => double.Parse(Next()); public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = Int() + add; } return Array; } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = Long() + add; } return Array; } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = Double() + add; } return Array; } } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { Scanner cin = new Scanner(); int n = cin.Int(); int m = cin.Int(); int l = cin.Int(); long inf = (long)(1e9) + 1; var d = new long[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { d[i, j] = inf; } } } for (int i = 0; i < m; i++) { int a = cin.Int() - 1; int b = cin.Int() - 1; long c = cin.Long(); d[a, b] = c; d[b, a] = c; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { d[j, k] = Math.Min(d[j, k], d[j, i] + d[i, k]); } } } var d2 = new long[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i, j] <= l) { d2[i, j] = 1; } else { d2[i, j] = inf; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { d2[j, k] = Math.Min(d2[j, k], d2[j, i] + d2[i, k]); } } } int q = cin.Int(); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { int a = cin.Int() - 1; int b = cin.Int() - 1; if (d2[a, b] >= inf) { sb.AppendLine("-1"); } else { sb.AppendLine((d2[a, b] - 1).ToString()); } } Console.WriteLine(sb.ToString()); } } public class Scanner { private readonly char[] delimiter_ = new char[] { ' ' }; private string[] buf_; private int index_; public Scanner() { buf_ = new string[0]; index_ = 0; } public string Next() { if (index_ < buf_.Length) { return buf_[index_++]; } string st = Console.ReadLine(); while (st == "") { st = Console.ReadLine(); } buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries); if (buf_.Length == 0) { return Next(); } index_ = 0; return buf_[index_++]; } public int Int() => int.Parse(Next()); public long Long() => long.Parse(Next()); public double Double() => double.Parse(Next()); public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = Int() + add; } return Array; } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = Long() + add; } return Array; } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = Double() + add; } return Array; } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22]]
8
934
2
using System; using System.Linq; using System.Collections.Generic; using System.IO; using static System.Console; using System.Numerics; using System.Text.RegularExpressions; using System.Runtime.Serialization.Formatters.Binary; namespace atcorder2 { [Serializable] static class Program { public static long Read() { return long.Parse(Console.ReadLine()); } public static long[] Reads() { return Console.ReadLine().Split().Select(long.Parse).ToArray(); } public static long Gcd(long x, long y) { if (x < y) Gcd(y, x); while (y > 0) { var z = x; x = y; y = z % y; } return x; } public static long Lcm(long x, long y) { return x / Gcd(x, y) * y; } public static bool IsLowercaseAlphabet(char target) { return new Regex("^[a-z]+$").IsMatch(target.ToString()); } public static T DeepCopy<T>(this T src) { using (var memoryStream = new MemoryStream()) { var binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, src); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return (T)binaryFormatter.Deserialize(memoryStream); } } public static bool Prime(long n) { if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.Sqrt(n); i++) { if (n % i == 0) return false; } return true; } public static int UpperBound<T>(T[] a, T v) { return UpperBound(a, v, Comparer<T>.Default); } public static int UpperBound<T>(T[] a, T v, Comparer<T> cmp) { var l = 0; var r = a.Length - 1; while (l <= r) { var mid = l + (r - 1) / 2; var res = cmp.Compare(a[mid], v); if (res <= 0) l = mid + 1; else r = mid - 1; } return l; } public static int LowerBound(long[] a, long v) { var l = -1; var r = a.Length; while (r - l > 1) { var mid = (l + r) / 2; if (a[mid] >= v) r = mid; else l = mid; } return r; } public static bool LowerBoundf(long[] a, long v) { var l = -1; var r = a.Length; var f = false; while (r - l > 1) { var mid = (l + r) / 2; if (a[mid] >= v) { r = mid; f = true; } else l = mid; } return f; } public static List<int> GeneratePrime(long max) { System.Diagnostics.Debug.Assert(max >= 2); int prime; var sqMax = (int)Math.Sqrt(max); var primelist = new List<int>(); var searchList = Enumerable.Range(2, sqMax).ToList(); do { prime = searchList.First(); primelist.Add(prime); searchList.RemoveAll(n => n % prime == 0); } while (prime < sqMax); primelist.AddRange(searchList); return primelist; } static void Main(string[] args) { var sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); var s = ReadLine(); var k = Read(); var c = new List<long>(); var m = s[0]; c.Add(1); long ans = 0; for (int i = 1; i < s.Length; i++) { if (s[i] == m) { c[c.Count - 1]++; } else { ans += c[c.Count - 1] / 2; c.Add(1); m = s[i]; } } if (s.Length > 1 && s[s.Length - 2] == s[s.Length - 1]) ans += c[c.Count - 1] / 2; ans *= k; if (s[0] == s[s.Length - 1]) { ans -= (c[0] / 2 + c[c.Count - 1] / 2 - (c[0] + c[c.Count - 1]) / 2) * (k - 1); } WriteLine(s.Length != 1 ? ans : k / 2); Out.Flush(); } } }
using System; using System.Linq; using System.Collections.Generic; using System.IO; using static System.Console; using System.Numerics; using System.Text.RegularExpressions; using System.Runtime.Serialization.Formatters.Binary; namespace atcorder2 { [Serializable] static class Program { public static long Read() { return long.Parse(Console.ReadLine()); } public static long[] Reads() { return Console.ReadLine().Split().Select(long.Parse).ToArray(); } public static long Gcd(long x, long y) { if (x < y) Gcd(y, x); while (y > 0) { var z = x; x = y; y = z % y; } return x; } public static long Lcm(long x, long y) { return x / Gcd(x, y) * y; } public static bool IsLowercaseAlphabet(char target) { return new Regex("^[a-z]+$").IsMatch(target.ToString()); } public static T DeepCopy<T>(this T src) { using (var memoryStream = new MemoryStream()) { var binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, src); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return (T)binaryFormatter.Deserialize(memoryStream); } } public static bool Prime(long n) { if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.Sqrt(n); i++) { if (n % i == 0) return false; } return true; } public static int UpperBound<T>(T[] a, T v) { return UpperBound(a, v, Comparer<T>.Default); } public static int UpperBound<T>(T[] a, T v, Comparer<T> cmp) { var l = 0; var r = a.Length - 1; while (l <= r) { var mid = l + (r - 1) / 2; var res = cmp.Compare(a[mid], v); if (res <= 0) l = mid + 1; else r = mid - 1; } return l; } public static int LowerBound(long[] a, long v) { var l = -1; var r = a.Length; while (r - l > 1) { var mid = (l + r) / 2; if (a[mid] >= v) r = mid; else l = mid; } return r; } public static bool LowerBoundf(long[] a, long v) { var l = -1; var r = a.Length; var f = false; while (r - l > 1) { var mid = (l + r) / 2; if (a[mid] >= v) { r = mid; f = true; } else l = mid; } return f; } public static List<int> GeneratePrime(long max) { System.Diagnostics.Debug.Assert(max >= 2); int prime; var sqMax = (int)Math.Sqrt(max); var primelist = new List<int>(); var searchList = Enumerable.Range(2, sqMax).ToList(); do { prime = searchList.First(); primelist.Add(prime); searchList.RemoveAll(n => n % prime == 0); } while (prime < sqMax); primelist.AddRange(searchList); return primelist; } static void Main(string[] args) { var sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); var s = ReadLine(); var k = Read(); var c = new List<long>(); var m = s[0]; c.Add(1); long ans = 0; for (int i = 1; i < s.Length; i++) { if (s[i] == m) { c[c.Count - 1]++; } else { ans += c[c.Count - 1] / 2; c.Add(1); m = s[i]; } } if (s.Length > 1 && s[s.Length - 2] == s[s.Length - 1]) ans += c[c.Count - 1] / 2; ans *= k; if (s[0] == s[s.Length - 1]) { ans -= (c[0] / 2 + c[c.Count - 1] / 2 - (c[0] + c[c.Count - 1]) / 2) * (k - 1); } WriteLine(c.Count != 1 ? ans : s.Length * k / 2); Out.Flush(); } } }
[["-", 0, 28, 0, 41, 15, 16, 31, 214, 205, 22], ["+", 0, 28, 0, 41, 15, 16, 31, 214, 205, 22], ["-", 0, 28, 0, 41, 15, 16, 31, 214, 141, 22], ["+", 0, 28, 0, 41, 15, 16, 31, 214, 141, 22], ["+", 0, 41, 75, 16, 31, 16, 31, 214, 205, 22], ["+", 0, 41, 75, 16, 31, 16, 31, 214, 0, 131], ["+", 0, 41, 75, 16, 31, 16, 31, 214, 141, 22], ["+", 0, 28, 0, 41, 75, 16, 31, 16, 17, 48]]
8
1,034
8
using System; using System.Text; using System.Collections.Generic; using System.Collections; using System.Linq; using E = System.Linq.Enumerable; class Program { static char[] abc = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); static char[] moji = "atcoder@".ToCharArray(); static long mod = 1000000007; static void Main(string[] args) { var S = Console.ReadLine(); var K = long.Parse(Console.ReadLine()); // var A = Console.ReadLine().Split(' ').Select(x => // long.Parse(x)).ToArray(); var T = (S + S).ToCharArray(); long fir = 0; long count = 0; long sa = 0; long N = S.Length; for (int i = 1; i < 2 * N; i++) { if (T[i] == T[i - 1]) { count++; T[i] = '@'; } if (i == N - 1) { fir = count; count = 0; } if (i == 2 * N - 1) { sa = count; } } var same = true; for (int i = 1; i < N; i++) { if (S[i] != S[i - 1]) { same = false; break; } } if (same) { Console.WriteLine(N * (K / 2)); } else { Console.WriteLine(fir + sa * (K - 1)); } } }
using System; using System.Text; using System.Collections.Generic; using System.Collections; using System.Linq; using E = System.Linq.Enumerable; class Program { static char[] abc = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); static char[] moji = "atcoder@".ToCharArray(); static long mod = 1000000007; static void Main(string[] args) { var S = Console.ReadLine(); var K = long.Parse(Console.ReadLine()); // var A = Console.ReadLine().Split(' ').Select(x => // long.Parse(x)).ToArray(); var T = (S + S).ToCharArray(); long fir = 0; long count = 0; long sa = 0; long N = S.Length; for (int i = 1; i < 2 * N; i++) { if (T[i] == T[i - 1]) { count++; T[i] = '@'; } if (i == N - 1) { fir = count; count = 0; } if (i == 2 * N - 1) { sa = count; } } var same = true; for (int i = 1; i < N; i++) { if (S[i] != S[i - 1]) { same = false; break; } } if (same) { Console.WriteLine((N * K) / 2); } else { Console.WriteLine(fir + sa * (K - 1)); } } }
[["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 24], ["-", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 25], ["-", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
303
4
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Numerics; using System.Xml.Schema; using System.Threading; using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; using System.IO.Pipes; namespace AtCoder { public static class Ex { public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); } public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); } public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); } public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); } public static int BuildBitCount(this int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } } partial class AtCoder { public string GetStr() { return Console.ReadLine().Trim(); } public char GetChar() { return Console.ReadLine().Trim()[0]; } public int GetInt() { return int.Parse(Console.ReadLine().Trim()); } public long GetLong() { return long.Parse(Console.ReadLine().Trim()); } public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); } public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); } public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); } public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H) .Select(s => Enumerable.Repeat(value, W).ToArray()) .ToArray(); } public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetIntArray().ToArray()) .ToArray(); } public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetLongArray().ToArray()) .ToArray(); } public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetStr().ToCharArray()) .ToArray(); } public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); } Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<int>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; dic[a].Add(b); if (isDirected == false) dic[b].Add(a); } return dic; } Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<Edge>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; var c = input[2]; dic[a].Add(new Edge(b, c)); if (isDirected == false) dic[b].Add(new Edge(a, c)); } return dic; } bool eq<T, U>() => typeof(T).Equals(typeof(U)); T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s)) : eq<T, long>() ? ct<T, long>(long.Parse(s)) : eq<T, double>() ? ct<T, double>(double.Parse(s)) : eq<T, char>() ? ct<T, char>(s[0]) : ct<T, string>(s); void Multi<T>(out T a) => a = cv<T>(GetStr()); void Multi<T, U>(out T a, out U b) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); } void Multi<T, U, V>(out T a, out U b, out V c) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); } void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]); } } class Edge { public Edge(int to, long length) { To = to; Length = length; } public int To { get; set; } public long Length { get; set; } } partial class Program { static void Main() { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new AtCoder().Solve(); Console.Out.Flush(); Console.Read(); } } public partial class AtCoder { public void Solve() { var S = GetStr().ToCharArray(); var K = GetLong(); long ans = 0; if (S.Distinct().Count() == 1) { if (S.Length % 2 == 0) { ans = S.Length / 2 * K; } else { ans = S.Length * (K / 2) + K % 2 * (S.Length / 2 + 1); } } else { var ls = new List<string>(); var tmp = new List<char>(); tmp.Add(S[0]); for (int i = 0; i < S.Length - 1; i++) { if (S[i] != S[i + 1]) { ls.Add(new string(tmp.ToArray())); tmp = new List<char>(); } tmp.Add(S[i + 1]); } ls.Add(new string(tmp.ToArray())); if (S.First() == S.Last()) { var A = ls.First().Length; var B = ls.Last().Length; ls.RemoveAt(0); ls.RemoveAt(ls.Count - 1); ans = (A + B) / 2 * (K - 1) + A / 2 + B / 2 + ls.Sum(s => s.Length / 2) * K; } else { ans = ls.Sum(s => s.Length / 2) * K; } } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Numerics; using System.Xml.Schema; using System.Threading; using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; using System.IO.Pipes; namespace AtCoder { public static class Ex { public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); } public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); } public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); } public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); } public static int BuildBitCount(this int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } } partial class AtCoder { public string GetStr() { return Console.ReadLine().Trim(); } public char GetChar() { return Console.ReadLine().Trim()[0]; } public int GetInt() { return int.Parse(Console.ReadLine().Trim()); } public long GetLong() { return long.Parse(Console.ReadLine().Trim()); } public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); } public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); } public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); } public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H) .Select(s => Enumerable.Repeat(value, W).ToArray()) .ToArray(); } public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetIntArray().ToArray()) .ToArray(); } public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetLongArray().ToArray()) .ToArray(); } public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetStr().ToCharArray()) .ToArray(); } public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); } Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<int>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; dic[a].Add(b); if (isDirected == false) dic[b].Add(a); } return dic; } Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<Edge>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; var c = input[2]; dic[a].Add(new Edge(b, c)); if (isDirected == false) dic[b].Add(new Edge(a, c)); } return dic; } bool eq<T, U>() => typeof(T).Equals(typeof(U)); T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s)) : eq<T, long>() ? ct<T, long>(long.Parse(s)) : eq<T, double>() ? ct<T, double>(double.Parse(s)) : eq<T, char>() ? ct<T, char>(s[0]) : ct<T, string>(s); void Multi<T>(out T a) => a = cv<T>(GetStr()); void Multi<T, U>(out T a, out U b) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); } void Multi<T, U, V>(out T a, out U b, out V c) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); } void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]); } } class Edge { public Edge(int to, long length) { To = to; Length = length; } public int To { get; set; } public long Length { get; set; } } partial class Program { static void Main() { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new AtCoder().Solve(); Console.Out.Flush(); Console.Read(); } } public partial class AtCoder { public void Solve() { var S = GetStr().ToCharArray(); var K = GetLong(); long ans = 0; if (S.Distinct().Count() == 1) { if (S.Length % 2 == 0) { ans = S.Length / 2 * K; } else { ans = S.Length * (K / 2) + K % 2 * (S.Length / 2); } } else { var ls = new List<string>(); var tmp = new List<char>(); tmp.Add(S[0]); for (int i = 0; i < S.Length - 1; i++) { if (S[i] != S[i + 1]) { ls.Add(new string(tmp.ToArray())); tmp = new List<char>(); } tmp.Add(S[i + 1]); } ls.Add(new string(tmp.ToArray())); if (S.First() == S.Last()) { var A = ls.First().Length; var B = ls.Last().Length; ls.RemoveAt(0); ls.RemoveAt(ls.Count - 1); ans = (A + B) / 2 * (K - 1) + A / 2 + B / 2 + ls.Sum(s => s.Length / 2) * K; } else { ans = ls.Sum(s => s.Length / 2) * K; } } Console.WriteLine(ans); } } }
[["-", 12, 16, 12, 16, 12, 23, 0, 16, 17, 72], ["-", 12, 16, 12, 16, 12, 23, 0, 16, 12, 203]]
8
2,195
2
using System; using System.Diagnostics; using System.IO; using System.Text; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Security.Cryptography; class Program { const string Yes = "Yes"; const string No = "No"; const char White = '.'; const char Black = '#'; const long Mod = 1000000007; static void Main(string[] args) { #if DEBUG MyDebugger.Test(process, "../TextFile1.txt"); #else object ret = process(new StreamReader(Console.OpenStandardInput())); Console.WriteLine(ret); #endif } static object process(TextReader input) { var s = input.ReadLine(); var k = int.Parse(input.ReadLine()); var n = s.Length; var first = 0; for (first = 0; first < n && s[first] == s[0]; first++) ; var last = 0; for (last = 0; last < n && s[n - last - 1] == s[n - 1]; last++) ; if (n == last) { return n * k / 2; } if (s[0] != s[n - 1]) { return count(s) * k; } else { long sum = 0; sum += count(s.Substring(0, n - last)); var ss = s.Substring(n - last, last) + s.Substring(0, n - last); sum += count(ss) * (k - 1); sum += count(s.Substring(n - last, last)); return sum; } } static long count(string s) { long co = 0; for (int i = 1; i < s.Length; i++) { if (s[i - 1] == s[i]) { co++; i++; } } return co; } static long[] ToIntArray(string strs, int n) { var ret = new long[n]; var spl = strs.Split(' '); for (int i = 0; i < n; i++) ret[i] = long.Parse(spl[i]); return ret; } }
using System; using System.Diagnostics; using System.IO; using System.Text; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Security.Cryptography; class Program { const string Yes = "Yes"; const string No = "No"; const char White = '.'; const char Black = '#'; const long Mod = 1000000007; static void Main(string[] args) { #if DEBUG MyDebugger.Test(process, "../TextFile1.txt"); #else object ret = process(new StreamReader(Console.OpenStandardInput())); Console.WriteLine(ret); #endif } static object process(TextReader input) { var s = input.ReadLine(); var k = int.Parse(input.ReadLine()); var n = s.Length; var first = 0; for (first = 0; first < n && s[first] == s[0]; first++) ; var last = 0; for (last = 0; last < n && s[n - last - 1] == s[n - 1]; last++) ; if (n == last) { return (long)n * k / 2; } if (s[0] != s[n - 1]) { return count(s) * k; } else { long sum = 0; sum += count(s.Substring(0, n - last)); var ss = s.Substring(n - last, last) + s.Substring(0, n - last); sum += count(ss) * (k - 1); sum += count(s.Substring(n - last, last)); return sum; } } static long count(string s) { long co = 0; for (int i = 1; i < s.Length; i++) { if (s[i - 1] == s[i]) { co++; i++; } } return co; } static long[] ToIntArray(string strs, int n) { var ret = new long[n]; var spl = strs.Split(' '); for (int i = 0; i < n; i++) ret[i] = long.Parse(spl[i]); return ret; } }
[["+", 0, 37, 0, 16, 31, 16, 31, 74, 0, 24], ["+", 0, 37, 0, 16, 31, 16, 31, 74, 39, 199], ["+", 0, 37, 0, 16, 31, 16, 31, 74, 0, 25]]
8
485
3
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Numerics; using System.IO; using System.Runtime.InteropServices; using static System.Math; using static Math2; using static io; using static Utils; public class CompetitiveProgramming { public void Solve() { var S = Str; var K = Long1; var pre = 0; var suf = 0; if (S[0] != S[S.Length - 1]) { long ans = 0; for (int i = 0; i < S.Length; ++i) { int j = i; int cnt = 0; while (j < S.Length && S[i] == S[j]) { ++j; ++cnt; } i = j - 1; ans += cnt / 2; } Put(ans * K); return; } long ans2 = 0; for (int i = 0; i < S.Length; ++i) { if (S[i] == S[0]) ++pre; else break; } if (pre == S.Length) { Put((S.Length * K / 2)); return; } for (int i = S.Length - 1; i >= 0; --i) { if (S[i] == S[S.Length - 1]) ++suf; else break; } ans2 += (pre + suf) / 2 * (K - 1); long r = 0; for (int i = 1; i < S.Length - 1; ++i) { int j = i; int cnt = 0; while (j < S.Length && S[i] == S[j]) { ++j; ++cnt; } i = j - 1; r += cnt / 2; } ans2 += (K)*r; ans2 += pre / 2; ans2 += suf / 2; Put(ans2); } } public static class Math2 { public const int INF = 1 << 29; public const long INFL = 1L << 60; public const long MOD = 1000000007; public const int MOD2 = 998244353; public static long Power(long a, long b, long MOD = 1000000007) // i^N { long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } public static long Power2(long a, long b) // i^N { long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a; a = a * a; b >>= 1; } return res; } public static long GCD(long a, long b) { while (b > 0) { var r = a % b; a = b; b = r; } return a; } public static long LCM(long a, long b) => (b / GCD(a, b)) * a; public static long Comb(long n, long r, int MOD = 1000000007) { if (r > n - r) r = n - r; long Nume = 1; long Deno = 1; if (r > n - r) r = n - r; for (long i = 1; i <= r; ++i) { Deno = (Deno * i) % MOD; Nume = Nume * (n - i + 1) % MOD; } return (Nume * inv(Deno)) % MOD; } public static long Comb2(long n, long r) { long Nume = 1; long Deno = 1; if (r > n - r) r = n - r; for (long i = 1; i <= r; ++i) { Deno *= i; Nume *= n - i + 1; } return Nume / Deno; } public static long inv(long x, int MOD = 1000000007) { return Power(x, MOD - 2, MOD); } } public static class Utils { public static void Swap<T>(ref T A, ref T B) { T x = A; A = B; B = x; } public static int DigitSum(int N) { string s = N.ToString(); int ret = 0; for (int i = 0; i < s.Length; ++i) ret += s[i] - '0'; return ret; } public static string ConvertBase(long N, int K) { StringBuilder ret = new StringBuilder(); while (N > 0) { var r = N % K; N /= K; ret.Append(r); } return new string(ret.ToString().ToCharArray().Reverse().ToArray()); } } class Program { static void Main(string[] args) { var CompetitiveProgramming = new CompetitiveProgramming(); CompetitiveProgramming.Solve(); } } public static class io { public static string Str => Console.ReadLine(); public static string[] Strs => Str.Split(' '); public static long[] Longs => Strs.Select(long.Parse).ToArray(); public static int[] Ints => Strs.Select(int.Parse).ToArray(); public static char[] Chars => Str.ToArray(); public static double[] Doubles => Strs.Select(double.Parse).ToArray(); public static long Long1 => Longs[0]; public static int Int1 => Ints[0]; public static char Char1 => Chars[0]; public static double Double1 => Doubles[0]; public static long[] VerticalRead(int N) { long[] A = new long[N]; for (int i = 0; i < N; ++i) A[i] = Long1; return A; } public static void Put(string a) => Console.WriteLine(a); public static void Put(params object[] i) => Put(string.Join(" ", i)); public static void Put<T>(IEnumerable<T> a) => Put(string.Join(" ", a)); public static void VerticalPut<T>(IEnumerable<T> a) { foreach (var z in a) Put(z); } public static void YN(bool i) { if (i) Put("Yes"); else Put("No"); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Numerics; using System.IO; using System.Runtime.InteropServices; using static System.Math; using static Math2; using static io; using static Utils; public class CompetitiveProgramming { public void Solve() { var S = Str; var K = Long1; var pre = 0; var suf = 0; if (S[0] != S[S.Length - 1]) { long ans = 0; for (int i = 0; i < S.Length; ++i) { int j = i; int cnt = 0; while (j < S.Length && S[i] == S[j]) { ++j; ++cnt; } i = j - 1; ans += cnt / 2; } Put(ans * K); return; } long ans2 = 0; for (int i = 0; i < S.Length; ++i) { if (S[i] == S[0]) ++pre; else break; } if (pre == S.Length) { Put((S.Length * K / 2)); return; } for (int i = S.Length - 1; i >= 0; --i) { if (S[i] == S[S.Length - 1]) ++suf; else break; } ans2 += (pre + suf) / 2 * (K - 1); long r = 0; for (int i = pre; i < S.Length - suf; ++i) { int j = i; int cnt = 0; while (j < S.Length && S[i] == S[j]) { ++j; ++cnt; } i = j - 1; r += cnt / 2; } ans2 += (K)*r; ans2 += pre / 2; ans2 += suf / 2; Put(ans2); } } public static class Math2 { public const int INF = 1 << 29; public const long INFL = 1L << 60; public const long MOD = 1000000007; public const int MOD2 = 998244353; public static long Power(long a, long b, long MOD = 1000000007) // i^N { long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } public static long Power2(long a, long b) // i^N { long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a; a = a * a; b >>= 1; } return res; } public static long GCD(long a, long b) { while (b > 0) { var r = a % b; a = b; b = r; } return a; } public static long LCM(long a, long b) => (b / GCD(a, b)) * a; public static long Comb(long n, long r, int MOD = 1000000007) { if (r > n - r) r = n - r; long Nume = 1; long Deno = 1; if (r > n - r) r = n - r; for (long i = 1; i <= r; ++i) { Deno = (Deno * i) % MOD; Nume = Nume * (n - i + 1) % MOD; } return (Nume * inv(Deno)) % MOD; } public static long Comb2(long n, long r) { long Nume = 1; long Deno = 1; if (r > n - r) r = n - r; for (long i = 1; i <= r; ++i) { Deno *= i; Nume *= n - i + 1; } return Nume / Deno; } public static long inv(long x, int MOD = 1000000007) { return Power(x, MOD - 2, MOD); } } public static class Utils { public static void Swap<T>(ref T A, ref T B) { T x = A; A = B; B = x; } public static int DigitSum(int N) { string s = N.ToString(); int ret = 0; for (int i = 0; i < s.Length; ++i) ret += s[i] - '0'; return ret; } public static string ConvertBase(long N, int K) { StringBuilder ret = new StringBuilder(); while (N > 0) { var r = N % K; N /= K; ret.Append(r); } return new string(ret.ToString().ToCharArray().Reverse().ToArray()); } } class Program { static void Main(string[] args) { var CompetitiveProgramming = new CompetitiveProgramming(); CompetitiveProgramming.Solve(); } } public static class io { public static string Str => Console.ReadLine(); public static string[] Strs => Str.Split(' '); public static long[] Longs => Strs.Select(long.Parse).ToArray(); public static int[] Ints => Strs.Select(int.Parse).ToArray(); public static char[] Chars => Str.ToArray(); public static double[] Doubles => Strs.Select(double.Parse).ToArray(); public static long Long1 => Longs[0]; public static int Int1 => Ints[0]; public static char Char1 => Chars[0]; public static double Double1 => Doubles[0]; public static long[] VerticalRead(int N) { long[] A = new long[N]; for (int i = 0; i < N; ++i) A[i] = Long1; return A; } public static void Put(string a) => Console.WriteLine(a); public static void Put(params object[] i) => Put(string.Join(" ", i)); public static void Put<T>(IEnumerable<T> a) => Put(string.Join(" ", a)); public static void VerticalPut<T>(IEnumerable<T> a) { foreach (var z in a) Put(z); } public static void YN(bool i) { if (i) Put("Yes"); else Put("No"); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 22], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 22]]
8
1,370
4
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { AGC039.A(); } class AGC039 { public static void A() { var S = Read.Str(); var K = Read.Long(); int count0 = 0; bool lastMod0 = false; for (int i = 1; i < S.Length; ++i) { if (S[i] == S[i - 1]) { count0++; if (i == S.Length - 1) { lastMod0 = true; } i++; } } if (S[0] != S.Last() || lastMod0) { Console.WriteLine(K * count0); return; } int count1 = 1; bool lastMod1 = false; for (int i = 2; i < S.Length; ++i) { if (S[i] == S[i - 1]) { count1++; if (i == S.Length - 1) { lastMod1 = true; } i++; } } if (lastMod1) { Console.WriteLine((K + 1) / 2 * count0 + K / 2 * count1); } else { Console.WriteLine(count0 + (K - 1) * count1); } } public static void B() { var N = Read.Int(); long[,] dist = new long[N, N]; for (int i = 0; i < N; ++i) { var S = Read.Str(); for (int j = 0; j < N; ++j) { dist[i, j] = i == j ? 0 : (S[j] == '1' ? 1 : 1000000); } } var wf = Util.Dup(dist); Util.WarshallFloyd(wf); List<int> even = new List<int>(); List<int> odd = new List<int>(); for (int i = 0; i < N; ++i) { (wf[0, i] % 2 == 0 ? even : odd).Add(i); } for (int i = 0; i < even.Count; ++i) { for (int j = i + 1; j < even.Count; ++j) { if (dist[even[i], even[j]] == 1) { Console.WriteLine(-1); return; } } } for (int i = 0; i < odd.Count; ++i) { for (int j = i + 1; j < odd.Count; ++j) { if (dist[odd[i], odd[j]] == 1) { Console.WriteLine(-1); return; } } } long max = 0; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { max = Math.Max(max, wf[i, j]); } } Console.WriteLine(max + 1); } } class Util { public static T[,] Dup<T>(T[,] matrix) { var n0 = matrix.GetLength(0); var n1 = matrix.GetLength(1); var m = new T[n0, n1]; for (int i = 0; i < n0; ++i) { for (int j = 0; j < n1; ++j) { m[i, j] = matrix[i, j]; } } return m; } public static void WarshallFloyd(long[,] dist) { var N = dist.GetLength(0); for (int k = 0; k < N; ++k) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]); } } } } } static class Read { static Queue<string> queue = new Queue<string>(); static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } } public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); } public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); } public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); } public static int[] IntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } public static long[] LongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } public static string[] StrN() { return Console.ReadLine().Split(' '); } public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M) { Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); } return dict; } } } }
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { AGC039.A(); } class AGC039 { public static void A() { var S = Read.Str(); var K = Read.Long(); int count0 = 0; bool lastMod0 = false; for (int i = 1; i < S.Length; ++i) { if (S[i] == S[i - 1]) { count0++; if (i == S.Length - 1) { lastMod0 = true; } i++; } } if (S[0] != S.Last() || lastMod0) { Console.WriteLine(K * count0); return; } int count1 = 1; bool lastMod1 = S.Length == 1; for (int i = 2; i < S.Length; ++i) { if (S[i] == S[i - 1]) { count1++; if (i == S.Length - 1) { lastMod1 = true; } i++; } } if (lastMod1) { Console.WriteLine((K + 1) / 2 * count0 + K / 2 * count1); } else { Console.WriteLine(count0 + (K - 1) * count1); } } public static void B() { var N = Read.Int(); long[,] dist = new long[N, N]; for (int i = 0; i < N; ++i) { var S = Read.Str(); for (int j = 0; j < N; ++j) { dist[i, j] = i == j ? 0 : (S[j] == '1' ? 1 : 1000000); } } var wf = Util.Dup(dist); Util.WarshallFloyd(wf); List<int> even = new List<int>(); List<int> odd = new List<int>(); for (int i = 0; i < N; ++i) { (wf[0, i] % 2 == 0 ? even : odd).Add(i); } for (int i = 0; i < even.Count; ++i) { for (int j = i + 1; j < even.Count; ++j) { if (dist[even[i], even[j]] == 1) { Console.WriteLine(-1); return; } } } for (int i = 0; i < odd.Count; ++i) { for (int j = i + 1; j < odd.Count; ++j) { if (dist[odd[i], odd[j]] == 1) { Console.WriteLine(-1); return; } } } long max = 0; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { max = Math.Max(max, wf[i, j]); } } Console.WriteLine(max + 1); } } class Util { public static T[,] Dup<T>(T[,] matrix) { var n0 = matrix.GetLength(0); var n1 = matrix.GetLength(1); var m = new T[n0, n1]; for (int i = 0; i < n0; ++i) { for (int j = 0; j < n1; ++j) { m[i, j] = matrix[i, j]; } } return m; } public static void WarshallFloyd(long[,] dist) { var N = dist.GetLength(0); for (int k = 0; k < N; ++k) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]); } } } } } static class Read { static Queue<string> queue = new Queue<string>(); static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } } public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); } public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); } public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); } public static int[] IntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } public static long[] LongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } public static string[] StrN() { return Console.ReadLine().Split(' '); } public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M) { Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); } return dict; } } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 211, 0, 147], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 205, 22], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 0, 131], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 141, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 60], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
1,882
6
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; class TEST { static void Main() { Sol mySol = new Sol(); mySol.Solve(); } } class Sol { public void Solve() { int N = S.Length; List<int> cnt = new List<int>(); List<char> key = new List<char>(); cnt.Add(1); key.Add(S[0]); for (int i = 1; i < N; i++) { if (S[i] == key[key.Count - 1]) { cnt[cnt.Count - 1]++; } else { cnt.Add(1); key.Add(S[i]); } } if (cnt.Count == 1) { long tot = N * K; Console.WriteLine(tot / 2); return; } int M = cnt.Count; long ans = 0; ans += cnt[0] / 2; ans += cnt[M - 1]; for (int i = 1; i < M - 1; i++) { ans += K * (cnt[i] / 2); } if (key[0] == key[M - 1]) { ans += (K - 1) * ((cnt[0] + cnt[M - 1]) / 2); } else { ans += (K - 1) * (cnt[0] / 2); ans += (K - 1) * (cnt[M - 1] / 2); } Console.WriteLine(ans); } String S; long K; public Sol() { S = rs(); K = rl(); } static String rs() { return Console.ReadLine(); } static int ri() { return int.Parse(Console.ReadLine()); } static long rl() { return long.Parse(Console.ReadLine()); } static double rd() { return double.Parse(Console.ReadLine()); } static String[] rsa(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ria(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] rda(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; class TEST { static void Main() { Sol mySol = new Sol(); mySol.Solve(); } } class Sol { public void Solve() { int N = S.Length; List<int> cnt = new List<int>(); List<char> key = new List<char>(); cnt.Add(1); key.Add(S[0]); for (int i = 1; i < N; i++) { if (S[i] == key[key.Count - 1]) { cnt[cnt.Count - 1]++; } else { cnt.Add(1); key.Add(S[i]); } } if (cnt.Count == 1) { long tot = N * K; Console.WriteLine(tot / 2); return; } int M = cnt.Count; long ans = 0; ans += cnt[0] / 2; ans += cnt[M - 1] / 2; for (int i = 1; i < M - 1; i++) { ans += K * (cnt[i] / 2); } if (key[0] == key[M - 1]) { ans += (K - 1) * ((cnt[0] + cnt[M - 1]) / 2); } else { ans += (K - 1) * (cnt[0] / 2); ans += (K - 1) * (cnt[M - 1] / 2); } Console.WriteLine(ans); } String S; long K; public Sol() { S = rs(); K = rl(); } static String rs() { return Console.ReadLine(); } static int ri() { return int.Parse(Console.ReadLine()); } static long rl() { return long.Parse(Console.ReadLine()); } static double rd() { return double.Parse(Console.ReadLine()); } static String[] rsa(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ria(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] rda(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } }
[["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 85], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
599
2
using System; using System.Collections.Generic; using System.IO; using System.Linq; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class AGC039A { static public int numberOfRandomCases = 0; static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker) { var ary = new List<char>(); for (var i = 0; i < 40; i++) { ary.Add((char)('a' + xorshift % 2)); } var K = xorshift % 1000 + 1; _input.Add(new string(ary.ToArray())); _input.Add(K.ToString()); var tmp = new List<char>(); for (var i = 0; i < K; i++) { tmp.AddRange(ary); } var count1 = 0; for (var i = 1; i < tmp.Count; i++) { if (tmp[i - 1] == tmp[i]) { ++count1; tmp[i] = '#'; } } _output.Add(count1.ToString()); } static public void Solve() { var S = NS.ToArray(); var K = NN; var Sd = S.ToList(); Sd.AddRange(S); var count1 = 0L; var count2 = 0L; var list = new List<long>(); { var count = 0; var before = '!'; foreach (var item in S) { if (before != item) { if (count != 0) { list.Add(count); count = 0; } } ++count; before = item; } if (count != 0) { list.Add(count); } count1 = list.Sum(e => e / 2); } if (K == 1) { Console.WriteLine(count1); } else { if (S.Length == 1) { Console.WriteLine(K / 2); } else { if (list.Count == 1) { Console.WriteLine(K * list.First() / 2); } else if (S.First() == S.Last()) { var a = list.First() / 2; var b = list.Last() / 2; Console.WriteLine(K * count1 - (a / 2 + b / 2 - (a + b) / 2) * (K - 1)); } else { Console.WriteLine(K * count1); } } } } static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); } static class Console_ { static Queue<string> param = new Queue<string>(); public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); } } static long NN => long.Parse(Console_.NextString()); static double ND => double.Parse(Console_.NextString()); static string NS => Console_.NextString(); static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } static IEnumerable<long> Primes(long x) { if (x < 2) yield break; yield return 2; var halfx = x / 2; var table = new bool[halfx + 1]; var max = (long)(Math.Sqrt(x) / 2); for (long i = 1; i <= max; ++i) { if (table[i]) continue; var add = 2 * i + 1; yield return add; for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true; } for (long i = max + 1; i <= halfx; ++i) if (!table[i] && 2 * i + 1 <= x) yield return 2 * i + 1; } static IEnumerable<long> Factors(long x) { if (x < 2) yield break; while (x % 2 == 0) { x /= 2; yield return 2; } var max = (long)Math.Sqrt(x); for (long i = 3; i <= max; i += 2) while (x % i == 0) { x /= i; yield return i; } if (x != 1) yield return x; } static IEnumerable<long> Divisor(long x) { if (x < 1) yield break; var max = (long)Math.Sqrt(x); for (long i = 1; i <= max; ++i) { if (x % i != 0) continue; yield return i; if (i != x / i) yield return x / i; } } static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } } static IEnumerator<uint> _xsi = _xsc(); static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } static long GCD(long a, long b) { while (b > 0) { var tmp = b; b = a % b; a = tmp; } return a; } static long LCM(long a, long b) => a * b / GCD(a, b); static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); static Mod Pow(Mod x, long y) { Mod a = 1; while (y != 0) { if ((y & 1) == 1) a.Mul(x); x.Mul(x); y >>= 1; } return a; } static long Pow(long x, long y) { long a = 1; while (y != 0) { if ((y & 1) == 1) a *= x; x *= x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static long Comb(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static long Perm(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } static Func<T1, TR> Lambda<T1, TR>(Func<T1, Func<T1, TR>, TR> f) { Func<T1, TR> t = x1 => default(TR); return t = x1 => f(x1, t); } static Func<T1, T2, TR> Lambda<T1, T2, TR>(Func<T1, T2, Func<T1, T2, TR>, TR> f) { Func<T1, T2, TR> t = (x1, x2) => default(TR); return t = (x1, x2) => f(x1, x2, t); } static Func<T1, T2, T3, TR> Lambda<T1, T2, T3, TR>(Func<T1, T2, T3, Func<T1, T2, T3, TR>, TR> f) { Func<T1, T2, T3, TR> t = (x1, x2, x3) => default(TR); return t = (x1, x2, x3) => f(x1, x2, x3, t); } static Func<T1, T2, T3, T4, TR> Lambda<T1, T2, T3, T4, TR>( Func<T1, T2, T3, T4, Func<T1, T2, T3, T4, TR>, TR> f) { Func<T1, T2, T3, T4, TR> t = (x1, x2, x3, x4) => default(TR); return t = (x1, x2, x3, x4) => f(x1, x2, x3, x4, t); } static List<T> LCS<T>(T[] s, T[] t) where T : IEquatable<T> { int sl = s.Length, tl = t.Length; var dp = new int[sl + 1, tl + 1]; for (var i = 0; i < sl; i++) for (var j = 0; j < tl; j++) dp[i + 1, j + 1] = s[i].Equals(t[j]) ? dp[i, j] + 1 : Max(dp[i + 1, j], dp[i, j + 1]); { var r = new List<T>(); int i = sl, j = tl; while (i > 0 && j > 0) if (s[--i].Equals(t[--j])) r.Add(s[i]); else if (dp[i, j + 1] > dp[i + 1, j]) ++j; else ++i; r.Reverse(); return r; } } static long LIS<T>(T[] array, bool strict) { var l = new List<T>(); foreach (var e in array) { var i = l.BinarySearch(e); if (i < 0) i = ~i; else if (!strict) ++i; if (i == l.Count) l.Add(e); else l[i] = e; } return l.Count; } class PQ<T> where T : IComparable { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} public void Push(T v) { var i = h.Count; h.Add(v); while (i > 0) { var ni = (i - 1) / 2; if (c(v, h[ni]) >= 0) break; h[i] = h[ni]; i = ni; } h[i] = v; } public T Pop() { var r = h[0]; var v = h[h.Count - 1]; h.RemoveAt(h.Count - 1); if (h.Count == 0) return r; var i = 0; while (i * 2 + 1 < h.Count) { var i1 = i * 2 + 1; var i2 = i * 2 + 2; if (i2 < h.Count && c(h[i1], h[i2]) > 0) i1 = i2; if (c(v, h[i1]) <= 0) break; h[i] = h[i1]; i = i1; } h[i] = v; return r; } } class PQ<TK, TV> where TK : IComparable { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; public PQ(int cap, Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; public UF(long s) { d = Repeat(-1L, s).ToArray(); } public bool Unite(long x, long y) { x = Root(x); y = Root(y); if (x != y) { if (d[y] < d[x]) { var t = y; y = x; x = t; } d[x] += d[y]; d[y] = x; } return x != y; } public bool IsSame(long x, long y) => Root(x) == Root(y); public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); public long Count(long x) => -d[Root(x)]; } struct Mod : IEquatable<Mod>, IEquatable<long> { static public long _mod = 1000000007; long v; public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; } static public implicit operator Mod(long x) => new Mod(x); static public implicit operator long(Mod x) => x.v; public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; } public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; } public void Mul(Mod x) => v = (v * x.v) % _mod; public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod; static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; } static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; } static public Mod operator *(Mod x, Mod y) => x.v * y.v; static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v); static public bool operator ==(Mod x, Mod y) => x.v == y.v; static public bool operator !=(Mod x, Mod y) => x.v != y.v; static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; } public bool Equals(Mod x) => v == x.v; public bool Equals(long x) => v == x; public override bool Equals(object x) => x == null ? false : Equals((Mod)x); public override int GetHashCode() => v.GetHashCode(); public override string ToString() => v.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } } struct Mat<T> { T[,] m; public Mat(T[,] v) { m = (T[,])v.Clone(); } static public implicit operator Mat<T>(T[,] v) => new Mat<T>(v); public T this[int r, int c] { get { return m[r, c]; } set { m[r, c] = value; } } static public Mat<T> operator +(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)x; return tm; } static public Mat<T> operator +(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)b[r, c]; return tm; } static public Mat<T> operator -(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)x; return tm; } static public Mat<T> operator -(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)b[r, c]; return tm; } static public Mat<T> operator *(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] *= (dynamic)x; return tm; } static public Mat<T> operator *(Mat<T> a, Mat<T> b) { var nr = a.m.GetLength(0); var nc = b.m.GetLength(1); var tm = new T[nr, nc]; for (int i = 0; i < nr; ++i) for (int j = 0; j < nc; ++j) tm[i, j] = (dynamic)0; for (int r = 0; r < nr; ++r) for (int c = 0; c < nc; ++c) for (int i = 0; i < a.m.GetLength(1); ++i) tm[r, c] += a[r, i] * (dynamic)b[i, c]; return tm; } static public Mat<T> Pow(Mat<T> x, long y) { var n = x.m.GetLength(0); var t = (Mat<T>)new T[n, n]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) t[i, j] = (dynamic)(i == j ? 1 : 0); while (y != 0) { if ((y & 1) == 1) t *= x; x *= x; y >>= 1; } return t; } } class Tree { long N; int l; List<long>[] p; int[] d; long[][] pr; long r; Tuple<long, long, int>[] e; Tuple<long, long>[] b; bool lca; bool euler; bool bfs; public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } public Tuple<long, long>[] BFSRoot() { if (!bfs) { var nb = new List<Tuple<long, long>>(); var q = new Queue<long>(); var d = new bool[N]; nb.Add(Tuple.Create(r, -1L)); d[r] = true; q.Enqueue(r); while (q.Count > 0) { var w = q.Dequeue(); foreach (var i in p[w]) { if (d[i]) continue; d[i] = true; q.Enqueue(i); nb.Add(Tuple.Create(i, w)); } } b = nb.ToArray(); bfs = true; } return b; } public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); public Tuple<long, long, int>[] Euler() { if (!euler) { var ne = new List<Tuple<long, long, int>>(); var s = new Stack<Tuple<long, long>>(); var d = new bool[N]; d[r] = true; s.Push(Tuple.Create(r, -1L)); while (s.Count > 0) { var w = s.Peek(); var ad = true; foreach (var i in p[w.Item1]) { if (d[i]) continue; d[i] = true; ad = false; s.Push(Tuple.Create(i, w.Item1)); } if (!ad || p[w.Item1].Count == 1) ne.Add(Tuple.Create(w.Item1, w.Item2, 1)); if (ad) { s.Pop(); ne.Add(Tuple.Create(w.Item1, w.Item2, -1)); } } e = ne.ToArray(); euler = true; } return e; } public long LCA(long u, long v) { if (!lca) { l = 0; while (N > (1 << l)) l++; d = new int[N]; pr = Repeat(0, l).Select(_ => new long[N]).ToArray(); d[r] = 0; pr[0][r] = -1; var q = new Stack<long>(); q.Push(r); while (q.Count > 0) { var w = q.Pop(); foreach (var i in p[w]) { if (i == pr[0][w]) continue; q.Push(i); d[i] = d[w] + 1; pr[0][i] = w; } } for (var k = 0; k + 1 < l; k++) for (var w = 0; w < N; w++) if (pr[k][w] < 0) pr[k + 1][w] = -1; else pr[k + 1][w] = pr[k][pr[k][w]]; lca = true; } if (d[u] > d[v]) { var t = u; u = v; v = t; } for (var k = 0; k < l; k++) if ((((d[v] - d[u]) >> k) & 1) != 0) v = pr[k][v]; if (u == v) return u; for (var k = l - 1; k >= 0; k--) if (pr[k][u] != pr[k][v]) { u = pr[k][u]; v = pr[k][v]; } return pr[0][u]; } } class Graph { int n; List<Tuple<int, int, long>> pathList; Dictionary<int, long>[] vtxPath; long INF = (long.MaxValue >> 1) - 1; public Graph(long _n) { n = (int)_n; pathList = new List<Tuple<int, int, long>>(); vtxPath = Repeat(0, n).Select(_ => new Dictionary<int, long>()).ToArray(); } public void AddPath(long a, long b, long c) { pathList.Add(Tuple.Create((int)a, (int)b, c)); vtxPath[a][(int)b] = vtxPath[a].ContainsKey((int)b) ? Min(vtxPath[a][(int)b], c) : c; } public long[,] WarshallFloyd() { var ret = new long[n, n]; for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) ret[i, j] = vtxPath[i].ContainsKey(j) ? vtxPath[i][j] : INF; for (var k = 0; k < n; k++) for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) ret[i, j] = Min(ret[i, j], ret[i, k] + ret[k, j]); return ret; } public Tuple<long[], int?[], bool[]> BellmanFord(long s) { var dist = Repeat(INF, n).ToArray(); var pred = new int?[n]; var neg = new bool[n]; dist[s] = 0; for (var i = 1; i < n; i++) foreach (var path in pathList) if (dist[path.Item2] > (dist[path.Item1] == INF ? INF : dist[path.Item1] + path.Item3)) { dist[path.Item2] = dist[path.Item1] + path.Item3; pred[path.Item2] = path.Item1; } for (var i = 0; i < n; i++) foreach (var path in pathList) if (dist[path.Item2] > (dist[path.Item1] == INF ? INF : dist[path.Item1] + path.Item3) || neg[path.Item1]) { dist[path.Item2] = dist[path.Item1] + path.Item3; neg[path.Item2] = true; } return Tuple.Create(dist, pred, neg); } public Tuple<long[], int?[]> Dijkstra(long s) { var dist = Repeat(long.MaxValue >> 2, n).ToArray(); var pred = new int?[n]; dist[s] = 0; var q = new PQ<long, int>(); q.Push(0, (int)s); while (q.Count > 0) { var u = q.Pop().Item2; foreach (var path in vtxPath[u]) { var v = path.Key; var alt = dist[u] + path.Value; if (dist[v] > alt) { dist[v] = alt; pred[v] = u; q.Push(alt, v); } } } return Tuple.Create(dist, pred); } } class BT<T> where T : IComparable { class Node { public Node l; public Node r; public T v; public bool b; public int c; } Comparison<T> c; Node r; bool ch; T lm; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} bool R(Node n) => n != null && !n.b; bool B(Node n) => n != null && n.b; long C(Node n) => n?.c ?? 0; Node RtL(Node n) { Node m = n.r, t = m.l; m.l = n; n.r = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtR(Node n) { Node m = n.l, t = m.r; m.r = n; n.l = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } public void Add(T x) { r = A(r, x); r.b = true; } Node A(Node n, T x) { if (n == null) { ch = true; return new Node() { v = x, c = 1 }; } if (c(x, n.v) < 0) n.l = A(n.l, x); else n.r = A(n.r, x); n.c++; return Bl(n); } Node Bl(Node n) { if (!ch) return n; if (!B(n)) return n; if (R(n.l) && R(n.l.l)) { n = RtR(n); n.l.b = true; } else if (R(n.l) && R(n.l.r)) { n = RtLR(n); n.l.b = true; } else if (R(n.r) && R(n.r.l)) { n = RtRL(n); n.r.b = true; } else if (R(n.r) && R(n.r.r)) { n = RtL(n); n.r.b = true; } else ch = false; return n; } public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } Node Rm(Node n, T x) { if (n == null) { ch = false; return n; } n.c--; var r = c(x, n.v); if (r < 0) { n.l = Rm(n.l, x); return BlL(n); } if (r > 0) { n.r = Rm(n.r, x); return BlR(n); } if (n.l == null) { ch = n.b; return n.r; } n.l = RmM(n.l); n.v = lm; return BlL(n); } Node RmM(Node n) { n.c--; if (n.r != null) { n.r = RmM(n.r); return BlR(n); } lm = n.v; ch = n.b; return n.l; } Node BlL(Node n) { if (!ch) return n; if (B(n.r) && R(n.r.l)) { var b = n.b; n = RtRL(n); n.b = b; n.l.b = true; ch = false; } else if (B(n.r) && R(n.r.r)) { var b = n.b; n = RtL(n); n.b = b; n.r.b = true; n.l.b = true; ch = false; } else if (B(n.r)) { ch = n.b; n.b = true; n.r.b = false; } else { n = RtL(n); n.b = true; n.l.b = false; n.l = BlL(n.l); ch = false; } return n; } Node BlR(Node n) { if (!ch) return n; if (B(n.l) && R(n.l.r)) { var b = n.b; n = RtLR(n); n.b = b; n.r.b = true; ch = false; } else if (B(n.l) && R(n.l.l)) { var b = n.b; n = RtR(n); n.b = b; n.l.b = true; n.r.b = true; ch = false; } else if (B(n.l)) { ch = n.b; n.b = true; n.l.b = false; } else { n = RtR(n); n.b = true; n.r.b = false; n.r = BlR(n.r); ch = false; } return n; } public T this[long i] { get { return At(r, i); } } T At(Node n, long i) { if (n == null) return default(T); if (n.l == null) if (i == 0) return n.v; else return At(n.r, i - 1); if (n.l.c == i) return n.v; if (n.l.c > i) return At(n.l, i); return At(n.r, i - n.l.c - 1); } public bool Have(T x) { var t = FindUpper(x); return t < C(r) && At(r, t).CompareTo(x) == 0; } public long FindUpper(T x, bool allowSame = true) => allowSame ? FL(r, x) + 1 : FU(r, x); long FU(Node n, T x) { if (n == null) return 0; var r = c(x, n.v); if (r < 0) return FU(n.l, x); return C(n.l) + 1 + FU(n.r, x); } public long FindLower(T x, bool allowSame = true) { var t = FL(r, x); if (allowSame) return t + 1 < C(r) && At(r, t + 1).CompareTo(x) == 0 ? t + 1 : t < 0 ? C(r) : t; return t < 0 ? C(r) : t; } long FL(Node n, T x) { if (n == null) return -1; var r = c(x, n.v); if (r > 0) return C(n.l) + 1 + FL(n.r, x); return FL(n.l, x); } public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } public bool Any() => r != null; public long Count() => C(r); public IEnumerable<T> List() => L(r); IEnumerable<T> L(Node n) { if (n == null) yield break; foreach (var i in L(n.l)) yield return i; yield return n.v; foreach (var i in L(n.r)) yield return i; } } class Dict<K, V> : Dictionary<K, V> { Func<K, V> d; public Dict(Func<K, V> _d) { d = _d; } public Dict() : this(_ => default(V)) {} new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = d(i); } set { base[i] = value; } } } class Deque<T> { T[] b; int o, c; public int Count; public T this[int i] { get { return b[gi(i)]; } set { b[gi(i)] = value; } } public Deque(int cap = 16) { b = new T[c = cap]; } int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } public T PopFront() { if (Count-- == 0) throw new Exception(); var r = b[o++]; if (o >= c) o -= c; return r; } public T Front => b[o]; public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; void e() { T[] nb = new T[c << 1]; if (o > c - Count) { var l = b.Length - o; Array.Copy(b, o, nb, 0, l); Array.Copy(b, 0, nb, l, Count - l); } else Array.Copy(b, o, nb, 0, Count); b = nb; o = 0; c <<= 1; } public void Insert(int i, T x) { if (i > Count) throw new Exception(); this.PushFront(x); for (int j = 0; j < i; j++) this[j] = this[j + 1]; this[i] = x; } public T RemoveAt(int i) { if (i < 0 || i >= Count) throw new Exception(); var r = this[i]; for (int j = i; j > 0; j--) this[j] = this[j - 1]; this.PopFront(); return r; } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; public SegTree(long _n, T _ti, Func<T, T, T> _f) { n = 1; while (n < _n) n <<= 1; ti = _ti; f = _f; dat = Repeat(ti, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public SegTree(List<T> l, T _ti, Func<T, T, T> _f) : this(l.Count, _ti, _f) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public void Update(long i, T v) { dat[i += n] = v; while ((i >>= 1) > 0) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public T Query(long l, long r) { var vl = ti; var vr = ti; for (long li = n + l, ri = n + r; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, dat[li++]); if ((ri & 1) == 1) vr = f(dat[--ri], vr); } return f(vl, vr); } public T this[long idx] { get { return dat[idx + n]; } set { Update(idx, value); } } } class LazySegTree<T, E> { int n, height; T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; T[] dat; E[] laz; public LazySegTree(long _n, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { n = 1; height = 0; while (n < _n) { n <<= 1; ++height; } ti = _ti; ei = _ei; f = _f; g = _g; h = _h; dat = Repeat(ti, n << 1).ToArray(); laz = Repeat(ei, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public LazySegTree(List<T> l, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) : this(l.Count, _ti, _ei, _f, _g, _h) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); void Eval(long i) { if (laz[i].Equals(ei)) return; laz[(i << 1) | 0] = h(laz[(i << 1) | 0], laz[i]); laz[(i << 1) | 1] = h(laz[(i << 1) | 1], laz[i]); dat[i] = Reflect(i); laz[i] = ei; } void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } public void Update(long l, long r, E v) { Thrust(l += n); Thrust(r += n - 1); for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) { laz[li] = h(laz[li], v); ++li; } if ((ri & 1) == 1) { --ri; laz[ri] = h(laz[ri], v); } } Recalc(l); Recalc(r); } public T Query(long l, long r) { Thrust(l += n); Thrust(r += n - 1); var vl = ti; var vr = ti; for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, Reflect(li++)); if ((ri & 1) == 1) vr = f(Reflect(--ri), vr); } return f(vl, vr); } public T this[long idx] { get { Thrust(idx += n); return dat[idx] = Reflect(idx); } set { Thrust(idx += n); dat[idx] = value; laz[idx] = ei; Recalc(idx); } } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class AGC039A { static public int numberOfRandomCases = 0; static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker) { var ary = new List<char>(); for (var i = 0; i < 40; i++) { ary.Add((char)('a' + xorshift % 2)); } var K = xorshift % 1000 + 1; _input.Add(new string(ary.ToArray())); _input.Add(K.ToString()); var tmp = new List<char>(); for (var i = 0; i < K; i++) { tmp.AddRange(ary); } var count1 = 0; for (var i = 1; i < tmp.Count; i++) { if (tmp[i - 1] == tmp[i]) { ++count1; tmp[i] = '#'; } } _output.Add(count1.ToString()); } static public void Solve() { var S = NS.ToArray(); var K = NN; var Sd = S.ToList(); Sd.AddRange(S); var count1 = 0L; var count2 = 0L; var list = new List<long>(); { var count = 0; var before = '!'; foreach (var item in S) { if (before != item) { if (count != 0) { list.Add(count); count = 0; } } ++count; before = item; } if (count != 0) { list.Add(count); } count1 = list.Sum(e => e / 2); } if (K == 1) { Console.WriteLine(count1); } else { if (S.Length == 1) { Console.WriteLine(K / 2); } else { if (list.Count == 1) { Console.WriteLine(K * list.First() / 2); } else if (S.First() == S.Last()) { var a = list.First(); var b = list.Last(); Console.WriteLine(K * count1 - (a / 2 + b / 2 - (a + b) / 2) * (K - 1)); } else { Console.WriteLine(K * count1); } } } } static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); } static class Console_ { static Queue<string> param = new Queue<string>(); public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); } } static long NN => long.Parse(Console_.NextString()); static double ND => double.Parse(Console_.NextString()); static string NS => Console_.NextString(); static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } static IEnumerable<long> Primes(long x) { if (x < 2) yield break; yield return 2; var halfx = x / 2; var table = new bool[halfx + 1]; var max = (long)(Math.Sqrt(x) / 2); for (long i = 1; i <= max; ++i) { if (table[i]) continue; var add = 2 * i + 1; yield return add; for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true; } for (long i = max + 1; i <= halfx; ++i) if (!table[i] && 2 * i + 1 <= x) yield return 2 * i + 1; } static IEnumerable<long> Factors(long x) { if (x < 2) yield break; while (x % 2 == 0) { x /= 2; yield return 2; } var max = (long)Math.Sqrt(x); for (long i = 3; i <= max; i += 2) while (x % i == 0) { x /= i; yield return i; } if (x != 1) yield return x; } static IEnumerable<long> Divisor(long x) { if (x < 1) yield break; var max = (long)Math.Sqrt(x); for (long i = 1; i <= max; ++i) { if (x % i != 0) continue; yield return i; if (i != x / i) yield return x / i; } } static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } } static IEnumerator<uint> _xsi = _xsc(); static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } static long GCD(long a, long b) { while (b > 0) { var tmp = b; b = a % b; a = tmp; } return a; } static long LCM(long a, long b) => a * b / GCD(a, b); static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); static Mod Pow(Mod x, long y) { Mod a = 1; while (y != 0) { if ((y & 1) == 1) a.Mul(x); x.Mul(x); y >>= 1; } return a; } static long Pow(long x, long y) { long a = 1; while (y != 0) { if ((y & 1) == 1) a *= x; x *= x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static long Comb(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static long Perm(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } static Func<T1, TR> Lambda<T1, TR>(Func<T1, Func<T1, TR>, TR> f) { Func<T1, TR> t = x1 => default(TR); return t = x1 => f(x1, t); } static Func<T1, T2, TR> Lambda<T1, T2, TR>(Func<T1, T2, Func<T1, T2, TR>, TR> f) { Func<T1, T2, TR> t = (x1, x2) => default(TR); return t = (x1, x2) => f(x1, x2, t); } static Func<T1, T2, T3, TR> Lambda<T1, T2, T3, TR>(Func<T1, T2, T3, Func<T1, T2, T3, TR>, TR> f) { Func<T1, T2, T3, TR> t = (x1, x2, x3) => default(TR); return t = (x1, x2, x3) => f(x1, x2, x3, t); } static Func<T1, T2, T3, T4, TR> Lambda<T1, T2, T3, T4, TR>( Func<T1, T2, T3, T4, Func<T1, T2, T3, T4, TR>, TR> f) { Func<T1, T2, T3, T4, TR> t = (x1, x2, x3, x4) => default(TR); return t = (x1, x2, x3, x4) => f(x1, x2, x3, x4, t); } static List<T> LCS<T>(T[] s, T[] t) where T : IEquatable<T> { int sl = s.Length, tl = t.Length; var dp = new int[sl + 1, tl + 1]; for (var i = 0; i < sl; i++) for (var j = 0; j < tl; j++) dp[i + 1, j + 1] = s[i].Equals(t[j]) ? dp[i, j] + 1 : Max(dp[i + 1, j], dp[i, j + 1]); { var r = new List<T>(); int i = sl, j = tl; while (i > 0 && j > 0) if (s[--i].Equals(t[--j])) r.Add(s[i]); else if (dp[i, j + 1] > dp[i + 1, j]) ++j; else ++i; r.Reverse(); return r; } } static long LIS<T>(T[] array, bool strict) { var l = new List<T>(); foreach (var e in array) { var i = l.BinarySearch(e); if (i < 0) i = ~i; else if (!strict) ++i; if (i == l.Count) l.Add(e); else l[i] = e; } return l.Count; } class PQ<T> where T : IComparable { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} public void Push(T v) { var i = h.Count; h.Add(v); while (i > 0) { var ni = (i - 1) / 2; if (c(v, h[ni]) >= 0) break; h[i] = h[ni]; i = ni; } h[i] = v; } public T Pop() { var r = h[0]; var v = h[h.Count - 1]; h.RemoveAt(h.Count - 1); if (h.Count == 0) return r; var i = 0; while (i * 2 + 1 < h.Count) { var i1 = i * 2 + 1; var i2 = i * 2 + 2; if (i2 < h.Count && c(h[i1], h[i2]) > 0) i1 = i2; if (c(v, h[i1]) <= 0) break; h[i] = h[i1]; i = i1; } h[i] = v; return r; } } class PQ<TK, TV> where TK : IComparable { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; public PQ(int cap, Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; public UF(long s) { d = Repeat(-1L, s).ToArray(); } public bool Unite(long x, long y) { x = Root(x); y = Root(y); if (x != y) { if (d[y] < d[x]) { var t = y; y = x; x = t; } d[x] += d[y]; d[y] = x; } return x != y; } public bool IsSame(long x, long y) => Root(x) == Root(y); public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); public long Count(long x) => -d[Root(x)]; } struct Mod : IEquatable<Mod>, IEquatable<long> { static public long _mod = 1000000007; long v; public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; } static public implicit operator Mod(long x) => new Mod(x); static public implicit operator long(Mod x) => x.v; public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; } public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; } public void Mul(Mod x) => v = (v * x.v) % _mod; public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod; static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; } static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; } static public Mod operator *(Mod x, Mod y) => x.v * y.v; static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v); static public bool operator ==(Mod x, Mod y) => x.v == y.v; static public bool operator !=(Mod x, Mod y) => x.v != y.v; static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; } public bool Equals(Mod x) => v == x.v; public bool Equals(long x) => v == x; public override bool Equals(object x) => x == null ? false : Equals((Mod)x); public override int GetHashCode() => v.GetHashCode(); public override string ToString() => v.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } } struct Mat<T> { T[,] m; public Mat(T[,] v) { m = (T[,])v.Clone(); } static public implicit operator Mat<T>(T[,] v) => new Mat<T>(v); public T this[int r, int c] { get { return m[r, c]; } set { m[r, c] = value; } } static public Mat<T> operator +(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)x; return tm; } static public Mat<T> operator +(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)b[r, c]; return tm; } static public Mat<T> operator -(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)x; return tm; } static public Mat<T> operator -(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)b[r, c]; return tm; } static public Mat<T> operator *(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] *= (dynamic)x; return tm; } static public Mat<T> operator *(Mat<T> a, Mat<T> b) { var nr = a.m.GetLength(0); var nc = b.m.GetLength(1); var tm = new T[nr, nc]; for (int i = 0; i < nr; ++i) for (int j = 0; j < nc; ++j) tm[i, j] = (dynamic)0; for (int r = 0; r < nr; ++r) for (int c = 0; c < nc; ++c) for (int i = 0; i < a.m.GetLength(1); ++i) tm[r, c] += a[r, i] * (dynamic)b[i, c]; return tm; } static public Mat<T> Pow(Mat<T> x, long y) { var n = x.m.GetLength(0); var t = (Mat<T>)new T[n, n]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) t[i, j] = (dynamic)(i == j ? 1 : 0); while (y != 0) { if ((y & 1) == 1) t *= x; x *= x; y >>= 1; } return t; } } class Tree { long N; int l; List<long>[] p; int[] d; long[][] pr; long r; Tuple<long, long, int>[] e; Tuple<long, long>[] b; bool lca; bool euler; bool bfs; public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } public Tuple<long, long>[] BFSRoot() { if (!bfs) { var nb = new List<Tuple<long, long>>(); var q = new Queue<long>(); var d = new bool[N]; nb.Add(Tuple.Create(r, -1L)); d[r] = true; q.Enqueue(r); while (q.Count > 0) { var w = q.Dequeue(); foreach (var i in p[w]) { if (d[i]) continue; d[i] = true; q.Enqueue(i); nb.Add(Tuple.Create(i, w)); } } b = nb.ToArray(); bfs = true; } return b; } public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); public Tuple<long, long, int>[] Euler() { if (!euler) { var ne = new List<Tuple<long, long, int>>(); var s = new Stack<Tuple<long, long>>(); var d = new bool[N]; d[r] = true; s.Push(Tuple.Create(r, -1L)); while (s.Count > 0) { var w = s.Peek(); var ad = true; foreach (var i in p[w.Item1]) { if (d[i]) continue; d[i] = true; ad = false; s.Push(Tuple.Create(i, w.Item1)); } if (!ad || p[w.Item1].Count == 1) ne.Add(Tuple.Create(w.Item1, w.Item2, 1)); if (ad) { s.Pop(); ne.Add(Tuple.Create(w.Item1, w.Item2, -1)); } } e = ne.ToArray(); euler = true; } return e; } public long LCA(long u, long v) { if (!lca) { l = 0; while (N > (1 << l)) l++; d = new int[N]; pr = Repeat(0, l).Select(_ => new long[N]).ToArray(); d[r] = 0; pr[0][r] = -1; var q = new Stack<long>(); q.Push(r); while (q.Count > 0) { var w = q.Pop(); foreach (var i in p[w]) { if (i == pr[0][w]) continue; q.Push(i); d[i] = d[w] + 1; pr[0][i] = w; } } for (var k = 0; k + 1 < l; k++) for (var w = 0; w < N; w++) if (pr[k][w] < 0) pr[k + 1][w] = -1; else pr[k + 1][w] = pr[k][pr[k][w]]; lca = true; } if (d[u] > d[v]) { var t = u; u = v; v = t; } for (var k = 0; k < l; k++) if ((((d[v] - d[u]) >> k) & 1) != 0) v = pr[k][v]; if (u == v) return u; for (var k = l - 1; k >= 0; k--) if (pr[k][u] != pr[k][v]) { u = pr[k][u]; v = pr[k][v]; } return pr[0][u]; } } class Graph { int n; List<Tuple<int, int, long>> pathList; Dictionary<int, long>[] vtxPath; long INF = (long.MaxValue >> 1) - 1; public Graph(long _n) { n = (int)_n; pathList = new List<Tuple<int, int, long>>(); vtxPath = Repeat(0, n).Select(_ => new Dictionary<int, long>()).ToArray(); } public void AddPath(long a, long b, long c) { pathList.Add(Tuple.Create((int)a, (int)b, c)); vtxPath[a][(int)b] = vtxPath[a].ContainsKey((int)b) ? Min(vtxPath[a][(int)b], c) : c; } public long[,] WarshallFloyd() { var ret = new long[n, n]; for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) ret[i, j] = vtxPath[i].ContainsKey(j) ? vtxPath[i][j] : INF; for (var k = 0; k < n; k++) for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) ret[i, j] = Min(ret[i, j], ret[i, k] + ret[k, j]); return ret; } public Tuple<long[], int?[], bool[]> BellmanFord(long s) { var dist = Repeat(INF, n).ToArray(); var pred = new int?[n]; var neg = new bool[n]; dist[s] = 0; for (var i = 1; i < n; i++) foreach (var path in pathList) if (dist[path.Item2] > (dist[path.Item1] == INF ? INF : dist[path.Item1] + path.Item3)) { dist[path.Item2] = dist[path.Item1] + path.Item3; pred[path.Item2] = path.Item1; } for (var i = 0; i < n; i++) foreach (var path in pathList) if (dist[path.Item2] > (dist[path.Item1] == INF ? INF : dist[path.Item1] + path.Item3) || neg[path.Item1]) { dist[path.Item2] = dist[path.Item1] + path.Item3; neg[path.Item2] = true; } return Tuple.Create(dist, pred, neg); } public Tuple<long[], int?[]> Dijkstra(long s) { var dist = Repeat(long.MaxValue >> 2, n).ToArray(); var pred = new int?[n]; dist[s] = 0; var q = new PQ<long, int>(); q.Push(0, (int)s); while (q.Count > 0) { var u = q.Pop().Item2; foreach (var path in vtxPath[u]) { var v = path.Key; var alt = dist[u] + path.Value; if (dist[v] > alt) { dist[v] = alt; pred[v] = u; q.Push(alt, v); } } } return Tuple.Create(dist, pred); } } class BT<T> where T : IComparable { class Node { public Node l; public Node r; public T v; public bool b; public int c; } Comparison<T> c; Node r; bool ch; T lm; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} bool R(Node n) => n != null && !n.b; bool B(Node n) => n != null && n.b; long C(Node n) => n?.c ?? 0; Node RtL(Node n) { Node m = n.r, t = m.l; m.l = n; n.r = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtR(Node n) { Node m = n.l, t = m.r; m.r = n; n.l = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } public void Add(T x) { r = A(r, x); r.b = true; } Node A(Node n, T x) { if (n == null) { ch = true; return new Node() { v = x, c = 1 }; } if (c(x, n.v) < 0) n.l = A(n.l, x); else n.r = A(n.r, x); n.c++; return Bl(n); } Node Bl(Node n) { if (!ch) return n; if (!B(n)) return n; if (R(n.l) && R(n.l.l)) { n = RtR(n); n.l.b = true; } else if (R(n.l) && R(n.l.r)) { n = RtLR(n); n.l.b = true; } else if (R(n.r) && R(n.r.l)) { n = RtRL(n); n.r.b = true; } else if (R(n.r) && R(n.r.r)) { n = RtL(n); n.r.b = true; } else ch = false; return n; } public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } Node Rm(Node n, T x) { if (n == null) { ch = false; return n; } n.c--; var r = c(x, n.v); if (r < 0) { n.l = Rm(n.l, x); return BlL(n); } if (r > 0) { n.r = Rm(n.r, x); return BlR(n); } if (n.l == null) { ch = n.b; return n.r; } n.l = RmM(n.l); n.v = lm; return BlL(n); } Node RmM(Node n) { n.c--; if (n.r != null) { n.r = RmM(n.r); return BlR(n); } lm = n.v; ch = n.b; return n.l; } Node BlL(Node n) { if (!ch) return n; if (B(n.r) && R(n.r.l)) { var b = n.b; n = RtRL(n); n.b = b; n.l.b = true; ch = false; } else if (B(n.r) && R(n.r.r)) { var b = n.b; n = RtL(n); n.b = b; n.r.b = true; n.l.b = true; ch = false; } else if (B(n.r)) { ch = n.b; n.b = true; n.r.b = false; } else { n = RtL(n); n.b = true; n.l.b = false; n.l = BlL(n.l); ch = false; } return n; } Node BlR(Node n) { if (!ch) return n; if (B(n.l) && R(n.l.r)) { var b = n.b; n = RtLR(n); n.b = b; n.r.b = true; ch = false; } else if (B(n.l) && R(n.l.l)) { var b = n.b; n = RtR(n); n.b = b; n.l.b = true; n.r.b = true; ch = false; } else if (B(n.l)) { ch = n.b; n.b = true; n.l.b = false; } else { n = RtR(n); n.b = true; n.r.b = false; n.r = BlR(n.r); ch = false; } return n; } public T this[long i] { get { return At(r, i); } } T At(Node n, long i) { if (n == null) return default(T); if (n.l == null) if (i == 0) return n.v; else return At(n.r, i - 1); if (n.l.c == i) return n.v; if (n.l.c > i) return At(n.l, i); return At(n.r, i - n.l.c - 1); } public bool Have(T x) { var t = FindUpper(x); return t < C(r) && At(r, t).CompareTo(x) == 0; } public long FindUpper(T x, bool allowSame = true) => allowSame ? FL(r, x) + 1 : FU(r, x); long FU(Node n, T x) { if (n == null) return 0; var r = c(x, n.v); if (r < 0) return FU(n.l, x); return C(n.l) + 1 + FU(n.r, x); } public long FindLower(T x, bool allowSame = true) { var t = FL(r, x); if (allowSame) return t + 1 < C(r) && At(r, t + 1).CompareTo(x) == 0 ? t + 1 : t < 0 ? C(r) : t; return t < 0 ? C(r) : t; } long FL(Node n, T x) { if (n == null) return -1; var r = c(x, n.v); if (r > 0) return C(n.l) + 1 + FL(n.r, x); return FL(n.l, x); } public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } public bool Any() => r != null; public long Count() => C(r); public IEnumerable<T> List() => L(r); IEnumerable<T> L(Node n) { if (n == null) yield break; foreach (var i in L(n.l)) yield return i; yield return n.v; foreach (var i in L(n.r)) yield return i; } } class Dict<K, V> : Dictionary<K, V> { Func<K, V> d; public Dict(Func<K, V> _d) { d = _d; } public Dict() : this(_ => default(V)) {} new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = d(i); } set { base[i] = value; } } } class Deque<T> { T[] b; int o, c; public int Count; public T this[int i] { get { return b[gi(i)]; } set { b[gi(i)] = value; } } public Deque(int cap = 16) { b = new T[c = cap]; } int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } public T PopFront() { if (Count-- == 0) throw new Exception(); var r = b[o++]; if (o >= c) o -= c; return r; } public T Front => b[o]; public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; void e() { T[] nb = new T[c << 1]; if (o > c - Count) { var l = b.Length - o; Array.Copy(b, o, nb, 0, l); Array.Copy(b, 0, nb, l, Count - l); } else Array.Copy(b, o, nb, 0, Count); b = nb; o = 0; c <<= 1; } public void Insert(int i, T x) { if (i > Count) throw new Exception(); this.PushFront(x); for (int j = 0; j < i; j++) this[j] = this[j + 1]; this[i] = x; } public T RemoveAt(int i) { if (i < 0 || i >= Count) throw new Exception(); var r = this[i]; for (int j = i; j > 0; j--) this[j] = this[j - 1]; this.PopFront(); return r; } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; public SegTree(long _n, T _ti, Func<T, T, T> _f) { n = 1; while (n < _n) n <<= 1; ti = _ti; f = _f; dat = Repeat(ti, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public SegTree(List<T> l, T _ti, Func<T, T, T> _f) : this(l.Count, _ti, _f) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public void Update(long i, T v) { dat[i += n] = v; while ((i >>= 1) > 0) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public T Query(long l, long r) { var vl = ti; var vr = ti; for (long li = n + l, ri = n + r; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, dat[li++]); if ((ri & 1) == 1) vr = f(dat[--ri], vr); } return f(vl, vr); } public T this[long idx] { get { return dat[idx + n]; } set { Update(idx, value); } } } class LazySegTree<T, E> { int n, height; T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; T[] dat; E[] laz; public LazySegTree(long _n, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { n = 1; height = 0; while (n < _n) { n <<= 1; ++height; } ti = _ti; ei = _ei; f = _f; g = _g; h = _h; dat = Repeat(ti, n << 1).ToArray(); laz = Repeat(ei, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public LazySegTree(List<T> l, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) : this(l.Count, _ti, _ei, _f, _g, _h) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); void Eval(long i) { if (laz[i].Equals(ei)) return; laz[(i << 1) | 0] = h(laz[(i << 1) | 0], laz[i]); laz[(i << 1) | 1] = h(laz[(i << 1) | 1], laz[i]); dat[i] = Reflect(i); laz[i] = ei; } void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } public void Update(long l, long r, E v) { Thrust(l += n); Thrust(r += n - 1); for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) { laz[li] = h(laz[li], v); ++li; } if ((ri & 1) == 1) { --ri; laz[ri] = h(laz[ri], v); } } Recalc(l); Recalc(r); } public T Query(long l, long r) { Thrust(l += n); Thrust(r += n - 1); var vl = ti; var vr = ti; for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, Reflect(li++)); if ((ri & 1) == 1) vr = f(Reflect(--ri), vr); } return f(vl, vr); } public T this[long idx] { get { Thrust(idx += n); return dat[idx] = Reflect(idx); } set { Thrust(idx += n); dat[idx] = value; laz[idx] = ei; Recalc(idx); } } } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 16, 17, 85], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
10,539
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Console; namespace AtCoder_1 { class Program { static void Main(string[] args) { var sb = Cin(); var S = new StringBuilder(sb); var K = CinL(); long c = 0; // First; for (int i = 1; i < S.Length; i++) { if (S[i - 1] == S[i]) { S[i] = '0'; c++; } } // Cout(S + " : " + c); long kc = 0; bool lasc = S[S.Length - 1] == '0'; S = new StringBuilder(sb); if (K > 1) { for (int i = 0; i < S.Length; i++) { if (i == 0) { if (!lasc) { if (S[0] == S[S.Length - 1]) { S[0] = '0'; kc++; } } } else { if (S[i - 1] == S[i]) { S[i] = '0'; kc++; } } } } // Cout(S + " : " + kc); HashSet<char> test = new HashSet<char>(sb.ToArray()); long t = 0; if (test.Count == 1 && S.Length > 2) { long even = K / 2; long odd = even; if (K % 2 != 0) { odd++; } t = c * odd + kc * even; } else { t = c + kc * (K - 1); } Cout(t); Cin(); } static int loop(int num, int max) { if (num > max) { return 0; } else if (num < 0) { return max; } else { return num; } } static long GCD(long a, long b) { if (a < b) { swap(ref a, ref b); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } } static void swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; } static readonly int MOD10E9 = (1000000000 + 7); static bool isInt(double d) { return (int)d == d; } static string Cin() { return ReadLine(); } static string[] CinAr() { return ReadLine().Split(' '); } static int CinT() { return S2i(Cin()); } static int[] CinTAr() { return CinAr().Select((x, i) => { return S2i(x); }).ToArray(); } static long CinL() { return S2L(Cin()); } static long[] CinLAr() { return CinAr().Select((x, i) => { return S2L(x); }).ToArray(); } static double CinD() { return S2D(Cin()); } static double[] CinDAr() { return CinAr().Select((x, i) => { return S2D(x); }).ToArray(); } static int S2i(string s) { return int.Parse(s); } static long S2L(string s) { return long.Parse(s); } static double S2D(string s) { return double.Parse(s); } static void Cout(object s) { WriteLine(s); } static long[] yakusuu(long n) { if (n == 1) { return new long[] { 1 }; } List<long> res = new List<long>(); res.Add(1); for (long i = 2; i <= n / 2 + 1; i++) { if (n % i == 0) { res.Add(i); } } res.Add(n); return res.ToArray(); } static bool isPrime(long num) { if (num == 1) { return false; } for (long i = 2; i <= Math.Sqrt(num) + 1; i++) { if (num % i == 0) { return false; } } return true; } static void CoutAr<Type>(IEnumerable<Type> types) { string text = string.Join(" ", types); WriteLine(text); } public static long nCr(int n, int r) { // naive: return Factorial(n) / (Factorial(r) * Factorial(n - r)); return nPr(n, r) / Factorial(r); } public static long nPr(int n, int r) { // naive: return Factorial(n) / Factorial(n - r); return FactorialDivision(n, n - r); } private static long FactorialDivision(int topFactorial, int divisorFactorial) { long result = 1; for (int i = topFactorial; i > divisorFactorial; i--) result *= i; return result; } private static long Factorial(int i) { if (i <= 1) return 1; return i * Factorial(i - 1); } } public struct Pair<T> { public T a; public T b; public Pair(T _a, T _b) { a = _a; b = _b; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Console; namespace AtCoder_1 { class Program { static void Main(string[] args) { var sb = Cin(); var S = new StringBuilder(sb); var K = CinL(); long c = 0; // First; for (int i = 1; i < S.Length; i++) { if (S[i - 1] == S[i]) { S[i] = '0'; c++; } } // Cout(S + " : " + c); long kc = 0; bool lasc = S[S.Length - 1] == '0'; S = new StringBuilder(sb); if (K > 1) { for (int i = 0; i < S.Length; i++) { if (i == 0) { if (!lasc) { if (S[0] == S[S.Length - 1]) { S[0] = '0'; kc++; } } } else { if (S[i - 1] == S[i]) { S[i] = '0'; kc++; } } } } // Cout(S + " : " + kc); HashSet<char> test = new HashSet<char>(sb.ToArray()); long t = 0; if (test.Count == 1) { long even = K / 2; long odd = even; if (K % 2 != 0) { odd++; } t = c * odd + kc * even; } else { t = c + kc * (K - 1); } Cout(t); Cin(); } static int loop(int num, int max) { if (num > max) { return 0; } else if (num < 0) { return max; } else { return num; } } static long GCD(long a, long b) { if (a < b) { swap(ref a, ref b); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } } static void swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; } static readonly int MOD10E9 = (1000000000 + 7); static bool isInt(double d) { return (int)d == d; } static string Cin() { return ReadLine(); } static string[] CinAr() { return ReadLine().Split(' '); } static int CinT() { return S2i(Cin()); } static int[] CinTAr() { return CinAr().Select((x, i) => { return S2i(x); }).ToArray(); } static long CinL() { return S2L(Cin()); } static long[] CinLAr() { return CinAr().Select((x, i) => { return S2L(x); }).ToArray(); } static double CinD() { return S2D(Cin()); } static double[] CinDAr() { return CinAr().Select((x, i) => { return S2D(x); }).ToArray(); } static int S2i(string s) { return int.Parse(s); } static long S2L(string s) { return long.Parse(s); } static double S2D(string s) { return double.Parse(s); } static void Cout(object s) { WriteLine(s); } static long[] yakusuu(long n) { if (n == 1) { return new long[] { 1 }; } List<long> res = new List<long>(); res.Add(1); for (long i = 2; i <= n / 2 + 1; i++) { if (n % i == 0) { res.Add(i); } } res.Add(n); return res.ToArray(); } static bool isPrime(long num) { if (num == 1) { return false; } for (long i = 2; i <= Math.Sqrt(num) + 1; i++) { if (num % i == 0) { return false; } } return true; } static void CoutAr<Type>(IEnumerable<Type> types) { string text = string.Join(" ", types); WriteLine(text); } public static long nCr(int n, int r) { // naive: return Factorial(n) / (Factorial(r) * Factorial(n - r)); return nPr(n, r) / Factorial(r); } public static long nPr(int n, int r) { // naive: return Factorial(n) / Factorial(n - r); return FactorialDivision(n, n - r); } private static long FactorialDivision(int topFactorial, int divisorFactorial) { long result = 1; for (int i = topFactorial; i > divisorFactorial; i--) result *= i; return result; } private static long Factorial(int i) { if (i <= 1) return 1; return i * Factorial(i - 1); } } public struct Pair<T> { public T a; public T b; public Pair(T _a, T _b) { a = _a; b = _b; } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 0, 57, 15, 16, 12, 16, 31, 214, 205, 22], ["-", 0, 57, 15, 16, 12, 16, 31, 214, 0, 131], ["-", 0, 57, 15, 16, 12, 16, 31, 214, 141, 22], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 47], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203]]
8
1,118
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.AGC_039 { class A { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); long k = ReadLong(); List<long> blocks = new List<long>(); blocks.Add(1); for (int i = 1; i < s.Length; i++) { if (s[i] == s[i - 1]) { blocks[blocks.Count - 1]++; } else { blocks.Add(1); } } if (blocks.Count == 1) { WriteLine(blocks[0] * k / 2); } else if (s[s.Length - 1] == s[0]) { long cnt = 0; cnt += blocks[0] / 2; for (int i = 1; i < blocks.Count - 1; i++) cnt += blocks[i] / 2; cnt += (blocks[0] + blocks[blocks.Count - 1]) / 2 * (k - 1); cnt += blocks[blocks.Count - 1] / 2; WriteLine(cnt); } else { long cnt = 0; for (int i = 0; i < blocks.Count; i++) cnt += blocks[i] / 2; WriteLine(cnt); } } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.AGC_039 { class A { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); long k = ReadLong(); List<long> blocks = new List<long>(); blocks.Add(1); for (int i = 1; i < s.Length; i++) { if (s[i] == s[i - 1]) { blocks[blocks.Count - 1]++; } else { blocks.Add(1); } } if (blocks.Count == 1) { WriteLine(blocks[0] * k / 2); } else if (s[s.Length - 1] == s[0]) { long cnt = 0; cnt += blocks[0] / 2; for (int i = 1; i < blocks.Count - 1; i++) cnt += blocks[i] / 2 * k; cnt += (blocks[0] + blocks[blocks.Count - 1]) / 2 * (k - 1); cnt += blocks[blocks.Count - 1] / 2; WriteLine(cnt); } else { long cnt = 0; for (int i = 0; i < blocks.Count; i++) cnt += blocks[i] / 2 * k; WriteLine(cnt); } } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } } }
[["+", 0, 7, 8, 1, 0, 11, 12, 16, 17, 48], ["+", 0, 7, 8, 1, 0, 11, 12, 16, 12, 22]]
8
535
4
using System; using System.Linq; using System.Collections.Generic; namespace Algorithm { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var k = long.Parse(Console.ReadLine()); if (s.Length == 1 && k == 1) { Console.WriteLine(0); return; } var index = 0; var arr = new int[s.Length]; arr[0] = 1; for (var i = 1; i < s.Length; i++) { if (s[i] == s[i - 1]) arr[index]++; else { index++; arr[index]++; } } var add = 0; if (s[0] == s[s.Length - 1]) add = arr[0] / 2 + arr[index] / 2 - (arr[0] + arr[index]) / 2; var count = 0L; for (var i = 0; i <= index; i++) { if (arr[i] == 1) continue; count += arr[i] / 2; } if (count == 0) Console.WriteLine(k / 2); else Console.WriteLine(k * count - add * (k - 1)); } } }
using System; using System.Linq; using System.Collections.Generic; namespace Algorithm { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var k = long.Parse(Console.ReadLine()); if (s.Length == 1 && k == 1) { Console.WriteLine(0); return; } var index = 0; var arr = new int[s.Length]; arr[0] = 1; for (var i = 1; i < s.Length; i++) { if (s[i] == s[i - 1]) arr[index]++; else { index++; arr[index]++; } } var add = 0; if (s[0] == s[s.Length - 1]) add = arr[0] / 2 + arr[index] / 2 - (arr[0] + arr[index]) / 2; var count = 0L; for (var i = 0; i <= index; i++) { if (arr[i] == 1) continue; count += arr[i] / 2; } if (index == 0) Console.WriteLine(s.Length * k / 2); else Console.WriteLine(k * count - add * (k - 1)); } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 28, 0, 16, 31, 16, 31, 214, 205, 22], ["+", 0, 28, 0, 16, 31, 16, 31, 214, 0, 131], ["+", 0, 28, 0, 16, 31, 16, 31, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48]]
8
278
6
using System; class ConnectionandDisconnection { static void Main(string[] args) { string S = Console.ReadLine(); int K = int.Parse(Console.ReadLine()); Console.WriteLine(disconnect(S, K)); } static long disconnect(string conn_str, int loop) { long totalCounter = 0; if (conn_str.Length == 0) return 0; else if (conn_str.Length == 1) return (long)loop / 2; // 先頭 DisconnectedParts first = getDisconnectedStringForOneLoop(conn_str, null, true, false); totalCounter += (long)first.counter; // Console.WriteLine(first.counter+" "+totalCounter); // 先頭・最終以外 string last_targetPrevString = first.disconnected_str; if (loop > 2) { DisconnectedParts part1 = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, false); totalCounter += (long)part1.counter; // Console.WriteLine(part1.counter+" "+totalCounter); last_targetPrevString = part1.disconnected_str; if (loop > 3) { DisconnectedParts part2 = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, false); if (part1.disconnected_str != part2.disconnected_str) { // 偶数のとき if ((loop % 2) == 0) { totalCounter += (long)((part1.counter * ((loop - 2) / 2)) + (part2.counter * ((loop - 2) / 2)) - part1.counter); last_targetPrevString = part2.disconnected_str; // Console.WriteLine((long)(part1.counter*((loop-2)/2))+(part2.counter*((loop-2)/2))-part1.counter); } // 奇数のとき else { totalCounter += (long)(part1.counter * (((loop - 2) / 2) + 1) + (part2.counter * ((loop - 2) / 2)) - part1.counter); // Console.WriteLine(((long)part1.counter*((loop-2)/2+1)+(part2.counter*((loop-2)/2))-part1.counter)+" // "+totalCounter); } } else { totalCounter += (long)part1.counter * (loop - 3); } } } // 最終 if (loop > 1) { DisconnectedParts last = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, true); totalCounter += (long)last.counter; // Console.WriteLine(last.counter); } return totalCounter; } static DisconnectedParts getDisconnectedStringForOneLoop(string conn_str, string prev_str, Boolean isStartForLoop, Boolean isEndForLoop) { int size = conn_str.Length; string alph = "abcdefghijklmnopqrstuvwxyz"; string disconnected_str = ""; DisconnectedParts part = new DisconnectedParts(); if (isStartForLoop) { disconnected_str += conn_str[0]; } else { if (conn_str[0] == prev_str[size - 1]) { int index = (getIndexNum(alph, conn_str[0]) + 1) % alph.Length; while (prev_str[size - 1] == alph[index] || alph[index] == conn_str[1]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; } else { disconnected_str += conn_str[0]; } } for (int i = 0; i < size - 2; i++) { if (disconnected_str[i] == conn_str[i + 1]) { int index = (getIndexNum(alph, conn_str[i + 1]) + 1) % alph.Length; while (disconnected_str[i] == alph[index] || alph[index] == conn_str[i + 2]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[i + 1]; } } if (!isEndForLoop) { // 終端文字の判定 if (disconnected_str[size - 2] == conn_str[size - 1]) { int index = (getIndexNum(alph, conn_str[size - 1]) + 1) % alph.Length; while (disconnected_str[size - 2] == alph[index] || alph[index] == conn_str[0]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[size - 1]; } } else { if (disconnected_str[size - 2] == conn_str[size - 1]) { int index = (getIndexNum(alph, conn_str[size - 1]) + 1) % alph.Length; while (disconnected_str[size - 2] == alph[index]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[size - 1]; } } part.setDiscpnnectedString(disconnected_str); // Console.WriteLine(disconnected_str); return part; } static int getIndexNum(string basestr, char target) { for (int i = 0; i < basestr.Length; i++) { if (basestr[i] == target) { return i; } } return -1; } } class DisconnectedParts { public int counter; public string disconnected_str; public DisconnectedParts() { counter = 0; disconnected_str = ""; } public void count() { counter++; } public void setDiscpnnectedString(string disconn_str) { this.disconnected_str = disconn_str; } }
using System; class ConnectionandDisconnection { static void Main(string[] args) { string S = Console.ReadLine(); long K = long.Parse(Console.ReadLine()); Console.WriteLine(disconnect(S, K)); } static long disconnect(string conn_str, long loop) { long totalCounter = 0; if (conn_str.Length == 0) return 0; else if (conn_str.Length == 1) return (long)loop / 2; // 先頭 DisconnectedParts first = getDisconnectedStringForOneLoop(conn_str, null, true, false); totalCounter += (long)first.counter; // Console.WriteLine(first.counter+" "+totalCounter); // 先頭・最終以外 string last_targetPrevString = first.disconnected_str; if (loop > 2) { DisconnectedParts part1 = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, false); totalCounter += (long)part1.counter; // Console.WriteLine(part1.counter+" "+totalCounter); last_targetPrevString = part1.disconnected_str; if (loop > 3) { DisconnectedParts part2 = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, false); if (part1.disconnected_str != part2.disconnected_str) { // 偶数のとき if ((loop % 2) == 0) { totalCounter += (long)((part1.counter * ((loop - 2) / 2)) + (part2.counter * ((loop - 2) / 2)) - part1.counter); last_targetPrevString = part2.disconnected_str; // Console.WriteLine((long)(part1.counter*((loop-2)/2))+(part2.counter*((loop-2)/2))-part1.counter); } // 奇数のとき else { totalCounter += (long)(part1.counter * (((loop - 2) / 2) + 1) + (part2.counter * ((loop - 2) / 2)) - part1.counter); // Console.WriteLine(((long)part1.counter*((loop-2)/2+1)+(part2.counter*((loop-2)/2))-part1.counter)+" // "+totalCounter); } } else { totalCounter += (long)part1.counter * (loop - 3); } } } // 最終 if (loop > 1) { DisconnectedParts last = getDisconnectedStringForOneLoop( conn_str, last_targetPrevString, false, true); totalCounter += (long)last.counter; // Console.WriteLine(last.counter); } return totalCounter; } static DisconnectedParts getDisconnectedStringForOneLoop(string conn_str, string prev_str, Boolean isStartForLoop, Boolean isEndForLoop) { int size = conn_str.Length; string alph = "abcdefghijklmnopqrstuvwxyz"; string disconnected_str = ""; DisconnectedParts part = new DisconnectedParts(); if (isStartForLoop) { disconnected_str += conn_str[0]; } else { if (conn_str[0] == prev_str[size - 1]) { int index = (getIndexNum(alph, conn_str[0]) + 1) % alph.Length; while (prev_str[size - 1] == alph[index] || alph[index] == conn_str[1]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; } else { disconnected_str += conn_str[0]; } } for (int i = 0; i < size - 2; i++) { if (disconnected_str[i] == conn_str[i + 1]) { int index = (getIndexNum(alph, conn_str[i + 1]) + 1) % alph.Length; while (disconnected_str[i] == alph[index] || alph[index] == conn_str[i + 2]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[i + 1]; } } if (!isEndForLoop) { // 終端文字の判定 if (disconnected_str[size - 2] == conn_str[size - 1]) { int index = (getIndexNum(alph, conn_str[size - 1]) + 1) % alph.Length; while (disconnected_str[size - 2] == alph[index] || alph[index] == conn_str[0]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[size - 1]; } } else { if (disconnected_str[size - 2] == conn_str[size - 1]) { int index = (getIndexNum(alph, conn_str[size - 1]) + 1) % alph.Length; while (disconnected_str[size - 2] == alph[index]) { index = (index + 1) % alph.Length; // Console.WriteLine(index); } part.count(); disconnected_str += alph[index]; // Console.WriteLine(disconnected_str); } else { disconnected_str += conn_str[size - 1]; } } part.setDiscpnnectedString(disconnected_str); // Console.WriteLine(disconnected_str); return part; } static int getIndexNum(string basestr, char target) { for (int i = 0; i < basestr.Length; i++) { if (basestr[i] == target) { return i; } } return -1; } } class DisconnectedParts { public int counter; public string disconnected_str; public DisconnectedParts() { counter = 0; disconnected_str = ""; } public void count() { counter++; } public void setDiscpnnectedString(string disconn_str) { this.disconnected_str = disconn_str; } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["-", 8, 201, 0, 195, 54, 55, 0, 220, 39, 199], ["+", 8, 201, 0, 195, 54, 55, 0, 220, 39, 199]]
8
978
6
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string S = Console.ReadLine(); long K = long.Parse(Console.ReadLine()); long ans1 = 0; long ct = 0; for (var i = 0; i < S.Length - 1; i++) { if (S.Substring(i, 1) == S.Substring(i + 1, 1)) { ct++; } else { ans1 += (ct + 1) / 2; ct = 0; } } ans1 += (ct + 1) / 2; long ans = 0; if (ct == S.Length - 1) { ans = (S.Length * K) / 2; } else if (S.Substring(0, 1) != S.Substring(S.Length - 1, 1)) { ans = ans1 * K; } else { long ct2 = 0; for (var i = 0; i < S.Length - 1; i++) { if (S.Substring(i, 1) == S.Substring(i + 1, 1)) { ct2++; } else { break; } } long ct3 = 0; for (var i = S.Length - 1; i >= 1; i--) { if (S.Substring(i, 1) == S.Substring(i - 1, 1)) { ct3++; } else { break; } } ans = ans1 * K - (ct2 + 1) / 2 * (K - 1) - (ct3 + 1) / 2 * (K - 1) + (ct2 + ct3 + 1) / 2 * (K - 1); } Console.WriteLine(ans); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string S = Console.ReadLine(); long K = long.Parse(Console.ReadLine()); long ans1 = 0; long ct = 0; for (var i = 0; i < S.Length - 1; i++) { if (S.Substring(i, 1) == S.Substring(i + 1, 1)) { ct++; } else { ans1 += (ct + 1) / 2; ct = 0; } } ans1 += (ct + 1) / 2; long ans = 0; if (ct == S.Length - 1) { ans = (S.Length * K) / 2; } else if (S.Substring(0, 1) != S.Substring(S.Length - 1, 1)) { ans = ans1 * K; } else { long ct2 = 0; for (var i = 0; i < S.Length - 1; i++) { if (S.Substring(i, 1) == S.Substring(i + 1, 1)) { ct2++; } else { break; } } long ct3 = 0; for (var i = S.Length - 1; i >= 1; i--) { if (S.Substring(i, 1) == S.Substring(i - 1, 1)) { ct3++; } else { break; } } ans = ans1 * K - (ct2 + 1) / 2 * (K - 1) - (ct3 + 1) / 2 * (K - 1) + (ct2 + ct3 + 2) / 2 * (K - 1); } Console.WriteLine(ans); } }
[["-", 12, 16, 31, 16, 31, 23, 0, 16, 12, 203], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 203]]
8
368
2
using System; namespace AGC039 { class A { static void Main(string[] args) { string S = Console.ReadLine(); long K = long.Parse(Console.ReadLine()); long count = 1; long res = 0; bool eQu = false; bool sC = false; bool eC = false; for (int i = 0; i < S.Length - 1;) { if (S[i] != S[i + 1]) { i++; } else { while (i < S.Length - 1 && S[i] == S[i + 1]) { count++; i++; } i++; } res += count / 2; if (count == S.Length) eQu = true; count = 1; } res *= K; int count2 = 1; int count3 = 1; if (1 < K && S[0] == S[S.Length - 1]) { for (int i = 0; i < S.Length - 1; i++) { if (S[i] == S[i + 1]) count2++; else break; } if (count2 % 2 == 1) { sC = true; } for (int i = S.Length - 1; 0 < i; i--) { if (S[i] == S[i - 1]) count3++; else break; } if (count3 % 2 == 1) { eC = true; } } // if (S.Length != 1 && 2 <= K && S[0] != S[1] && S[S.Length - 2] != // S[S.Length - 1] && S[0] == S[S.Length - 1]) { res += K - 1; } if (S.Length != 1 && 2 <= K && sC && eC && S[0] == S[S.Length - 1]) { res += K - 1; } if (S.Length == 1) res = K / 2; if (eQu && S.Length % 2 == 1) res += K / 2; Console.WriteLine(res); } } }
using System; namespace AGC039 { class A { static void Main(string[] args) { string S = Console.ReadLine(); long K = long.Parse(Console.ReadLine()); long count = 1; long res = 0; bool eQu = false; bool sC = false; bool eC = false; for (int i = 0; i < S.Length - 1;) { if (S[i] != S[i + 1]) { i++; } else { while (i < S.Length - 1 && S[i] == S[i + 1]) { count++; i++; } i++; } res += count / 2; if (count == S.Length) eQu = true; count = 1; } res *= K; int count2 = 1; int count3 = 1; if (1 < K && S[0] == S[S.Length - 1] && !eQu) { for (int i = 0; i < S.Length - 1; i++) { if (S[i] == S[i + 1]) count2++; else break; } if (count2 % 2 == 1) { sC = true; } for (int i = S.Length - 1; 0 < i; i--) { if (S[i] == S[i - 1]) count3++; else break; } if (count3 % 2 == 1) { eC = true; } } // if (S.Length != 1 && 2 <= K && S[0] != S[1] && S[S.Length - 2] != // S[S.Length - 1] && S[0] == S[S.Length - 1]) { res += K - 1; } if (S.Length != 1 && 2 <= K && sC && eC && S[0] == S[S.Length - 1]) { res += K - 1; } if (S.Length == 1) res = K / 2; if (eQu && S.Length % 2 == 1) res += K / 2; Console.WriteLine(res); } } }
[["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 98], ["+", 8, 196, 0, 57, 15, 16, 12, 241, 0, 111], ["+", 8, 196, 0, 57, 15, 16, 12, 241, 0, 22]]
8
388
3
using System; using System.Linq; using System.Numerics; using System.Collections.Generic; using System.IO; using System.Text; using N = System.Int64; // intつかうときとlongつかうときでかえる public class Solver { public void Solve() { string s = rs; N k = ri; N ans = 0; N a = 0, b = 0; bool p = false, q = false; for (int i = 1; i <= s.Length; i++) { if (s[i - 1] == s[i % s.Length]) { if (i == s.Length) { p = true; } a++; i++; } } for (int i = 2; i <= s.Length; i++) { if (s[i - 1] == s[i % s.Length]) { if (i == s.Length) { q = true; } b++; i++; } } if (p && q) { ans = a + (k - 1) * b; } else if (p && !q) { ans = a * (int)((k + 1) / 2) + b * (int)(k / 2); if (k % 2 == 1) { ans--; } } else { ans = a * k; } Write(ans); } int ri { get { return sc.Integer(); } } long rl { get { return sc.Long(); } } double rd { get { return sc.Double(); } } string rs { get { return sc.Scan(); } } public StreamScanner sc = new StreamScanner(Console.OpenStandardInput()); public Algorithm alg = new Algorithm(); private const N mod = 1000000007; N Reverse(N n) { return Pow(n, mod - 2); } N Mul(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p * n[i]) % mod; } return p; } N Pow(N n, N p) { N t = n, q = 1; while (p > 0) { if (p % 2 == 1) { q = (q * t) % mod; } t = (t * t) % mod; p >>= 1; } return q; } N PowUpper(N n, N b) { N p = 1; while (p <= n) { p *= b; } return p; } N Sign(N n) { return n == 0 ? 0 : n > 0 ? 1 : -1; } N Abs(N n) { return (n > 0) ? n : -n; } N Gcd(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = Gcd(p, n[i]); } return p; } N Gcd(N a, N b) { return (a == 0) ? b : Gcd(b % a, a); } N Lcm(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = Lcm(p, n[i]); } return p; } N Lcm(N a, N b) { return (a / Gcd(a, b) * b) % mod; } N Max(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p > n[i]) ? p : n[i]; } return p; } N Min(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p < n[i]) ? p : n[i]; } return p; } void Write<T>(T t) { Console.WriteLine(t.ToString()); } void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); } void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); } void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); } } static class Program { static public void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var solver = new Solver(); solver.Solve(); Console.Out.Flush(); } } public class Algorithm { public int[] ZAlgorithm(string s) { int[] p = new int[s.Length]; p[0] = s.Length; int i = 1, j = 0; while (i < s.Length) { while (i + j < s.Length && s[j] == s[i + j]) ++j; p[i] = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < s.Length && k + p[k] < j) { p[i + k] = p[k]; ++k; } i += k; j -= k; } return p; } public List<int> SieveOfEratosthenes(int n) { List<int> l = new List<int>(); if (n >= 2) { l.Add(2); } bool[] t = new bool[(n - 1) / 2]; for (int p = 3; p <= n; p += 2) { if (!t[(p - 3) / 2]) { l.Add(p); for (int q = p * p; q <= n; q += p) { t[(q - 3) / 2] = true; } } } return l; } public Dictionary<int, int> PrimeFactor(int n, List<int> primetable) { Dictionary<int, int> d = new Dictionary<int, int>(); int t = 0; foreach (int i in primetable) { if (n % i == 0) { while (n % 2 == 0) { n /= i; t++; } d.Add(i, t); } } if (n > 1) { d.Add(n, 1); } return d; } } public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(IComparer<T> comparer) { _heap = new T[128]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int type = 0) { _heap = new T[128]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; if (_count > _heap.Length) { var newheap = new T[_heap.Length * 2]; for (int n = 0; n < _heap.Length; n++) newheap[n] = _heap[n]; _heap = newheap; } // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } private readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) throw new EndOfStreamException(); if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public long Long() { long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (int)Long(); } public double Double() { return double.Parse(Scan()); } }
using System; using System.Linq; using System.Numerics; using System.Collections.Generic; using System.IO; using System.Text; using N = System.Int64; // intつかうときとlongつかうときでかえる public class Solver { public void Solve() { string s = rs; N k = ri; N ans = 0; N a = 0, b = 0; bool p = false, q = false; for (int i = 1; i <= s.Length; i++) { if (s[i - 1] == s[i % s.Length]) { if (i == s.Length) { p = true; } a++; i++; } } for (int i = 2; i <= s.Length; i++) { if (s[i - 1] == s[i % s.Length]) { if (i == s.Length) { q = true; } b++; i++; } } if (p && q) { ans = a + (k - 1) * b - 1; } else if (p && !q) { ans = a * (int)((k + 1) / 2) + b * (int)(k / 2); if (k % 2 == 1) { ans--; } } else { ans = a * k; } Write(ans); } int ri { get { return sc.Integer(); } } long rl { get { return sc.Long(); } } double rd { get { return sc.Double(); } } string rs { get { return sc.Scan(); } } public StreamScanner sc = new StreamScanner(Console.OpenStandardInput()); public Algorithm alg = new Algorithm(); private const N mod = 1000000007; N Reverse(N n) { return Pow(n, mod - 2); } N Mul(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p * n[i]) % mod; } return p; } N Pow(N n, N p) { N t = n, q = 1; while (p > 0) { if (p % 2 == 1) { q = (q * t) % mod; } t = (t * t) % mod; p >>= 1; } return q; } N PowUpper(N n, N b) { N p = 1; while (p <= n) { p *= b; } return p; } N Sign(N n) { return n == 0 ? 0 : n > 0 ? 1 : -1; } N Abs(N n) { return (n > 0) ? n : -n; } N Gcd(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = Gcd(p, n[i]); } return p; } N Gcd(N a, N b) { return (a == 0) ? b : Gcd(b % a, a); } N Lcm(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = Lcm(p, n[i]); } return p; } N Lcm(N a, N b) { return (a / Gcd(a, b) * b) % mod; } N Max(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p > n[i]) ? p : n[i]; } return p; } N Min(params N[] n) { N p = n[0]; for (N i = 1; i < n.Length; i++) { p = (p < n[i]) ? p : n[i]; } return p; } void Write<T>(T t) { Console.WriteLine(t.ToString()); } void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); } void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); } void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); } } static class Program { static public void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var solver = new Solver(); solver.Solve(); Console.Out.Flush(); } } public class Algorithm { public int[] ZAlgorithm(string s) { int[] p = new int[s.Length]; p[0] = s.Length; int i = 1, j = 0; while (i < s.Length) { while (i + j < s.Length && s[j] == s[i + j]) ++j; p[i] = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < s.Length && k + p[k] < j) { p[i + k] = p[k]; ++k; } i += k; j -= k; } return p; } public List<int> SieveOfEratosthenes(int n) { List<int> l = new List<int>(); if (n >= 2) { l.Add(2); } bool[] t = new bool[(n - 1) / 2]; for (int p = 3; p <= n; p += 2) { if (!t[(p - 3) / 2]) { l.Add(p); for (int q = p * p; q <= n; q += p) { t[(q - 3) / 2] = true; } } } return l; } public Dictionary<int, int> PrimeFactor(int n, List<int> primetable) { Dictionary<int, int> d = new Dictionary<int, int>(); int t = 0; foreach (int i in primetable) { if (n % i == 0) { while (n % 2 == 0) { n /= i; t++; } d.Add(i, t); } } if (n > 1) { d.Add(n, 1); } return d; } } public class PriorityQueue<T> where T : IComparable { private IComparer<T> _comparer = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority Queue with custom comparer /// </summary> public PriorityQueue(IComparer<T> comparer) { _heap = new T[128]; _comparer = comparer; } /// <summary> /// Priority queue /// </summary> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int type = 0) { _heap = new T[128]; _type = type; } private int Compare(T x, T y) { if (_comparer != null) return _comparer.Compare(x, y); return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; if (_count > _heap.Length) { var newheap = new T[_heap.Length * 2]; for (int n = 0; n < _heap.Length; n++) newheap[n] = _heap[n]; _heap = newheap; } // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } private readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) throw new EndOfStreamException(); if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public long Long() { long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (int)Long(); } public double Double() { return double.Parse(Scan()); } }
[["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
2,364
2
using System; using System.Collections.Generic; public class Program { public void Solve() { var sc = new Scanner(); var S = sc.Next(); int N = S.Length; int K = sc.NextInt(); long ans = 0; var l = new List<int>(); char c = '\0'; for (int i = 0; i < N; i++) { if (S[i] != c) { l.Add(0); c = S[i]; } l[l.Count - 1]++; } if (l.Count == 1) { Console.WriteLine((long)(K * N) / 2); return; } if (S[0] == S[N - 1]) { ans += l[0] / 2; ans += l[l.Count - 1] / 2; ans += (long)(K - 1) * ((l[0] + l[l.Count - 1]) / 2); for (int i = 1; i < l.Count - 1; i++) { ans += (long)K * (l[i] / 2); } } else { foreach (int i in l) { ans += (long)K * (i / 2); } } Console.WriteLine(ans); } public static void Main(string[] args) { new Program().Solve(); } } class Scanner { public Scanner() { _pos = 0; _line = new string[0]; } const char Separator = ' '; private int _pos; private string[] _line; #region スペース区切りで取得 public string Next() { if (_pos >= _line.Length) { _line = Console.ReadLine().Split(Separator); _pos = 0; } return _line[_pos++]; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } #endregion #region 型変換 private int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion #region 配列取得 public string[] Array() { if (_pos >= _line.Length) _line = Console.ReadLine().Split(Separator); _pos = _line.Length; return _line; } public int[] IntArray() { return ToIntArray(Array()); } public long[] LongArray() { return ToLongArray(Array()); } public double[] DoubleArray() { return ToDoubleArray(Array()); } #endregion }
using System; using System.Collections.Generic; public class Program { public void Solve() { var sc = new Scanner(); var S = sc.Next(); int N = S.Length; long K = sc.NextInt(); long ans = 0; var l = new List<long>(); char c = '\0'; for (int i = 0; i < N; i++) { if (S[i] != c) { l.Add(0); c = S[i]; } l[l.Count - 1]++; } if (l.Count == 1) { Console.WriteLine((long)(K * N) / 2); return; } if (S[0] == S[N - 1]) { ans += l[0] / 2; ans += l[l.Count - 1] / 2; ans += (long)(K - 1) * ((l[0] + l[l.Count - 1]) / 2); for (int i = 1; i < l.Count - 1; i++) { ans += (long)K * (l[i] / 2); } } else { foreach (long i in l) { ans += (long)K * (i / 2); } } Console.WriteLine(ans); } public static void Main(string[] args) { new Program().Solve(); } } class Scanner { public Scanner() { _pos = 0; _line = new string[0]; } const char Separator = ' '; private int _pos; private string[] _line; #region スペース区切りで取得 public string Next() { if (_pos >= _line.Length) { _line = Console.ReadLine().Split(Separator); _pos = 0; } return _line[_pos++]; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } #endregion #region 型変換 private int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion #region 配列取得 public string[] Array() { if (_pos >= _line.Length) _line = Console.ReadLine().Split(Separator); _pos = _line.Length; return _line; } public int[] IntArray() { return ToIntArray(Array()); } public long[] LongArray() { return ToLongArray(Array()); } public double[] DoubleArray() { return ToDoubleArray(Array()); } #endregion }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["-", 8, 196, 0, 57, 75, 196, 0, 210, 39, 199], ["+", 8, 196, 0, 57, 75, 196, 0, 210, 39, 199]]
8
732
6
using System; using static System.Console; class Program { static long count(string s) { long ans = 0; char curr = '*'; long nseries = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == curr) nseries++; else { ans += nseries / 2; curr = s[i]; nseries = 1; } } ans += nseries / 2; return ans; } public static void Main() { string s = ReadLine(); int k = int.Parse(ReadLine()); int L = s.Length; if (s[0] != s[L - 1]) { WriteLine(count(s) * k); } else { int n1 = 0; while (n1 < L && s[n1] == s[0]) n1++; int n3 = 0; if (n1 < L) while (n3 < L && s[L - 1 - n3] == s[L - 1]) n3++; int n2 = L - n1 - n3; if (n1 == L) { WriteLine((n1 * k) / 2); } else { string t = s.Substring(n1, n2); long ans = n1 / 2 + count(t) * k + (n3 + n1) / 2 * (k - 1) + n3 / 2; WriteLine(ans); } } } }
using System; using static System.Console; class Program { static long count(string s) { long ans = 0; char curr = '*'; long nseries = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == curr) nseries++; else { ans += nseries / 2; curr = s[i]; nseries = 1; } } ans += nseries / 2; return ans; } public static void Main() { string s = ReadLine(); long k = long.Parse(ReadLine()); int L = s.Length; if (s[0] != s[L - 1]) { WriteLine(count(s) * k); } else { int n1 = 0; while (n1 < L && s[n1] == s[0]) n1++; int n3 = 0; if (n1 < L) while (n3 < L && s[L - 1 - n3] == s[L - 1]) n3++; int n2 = L - n1 - n3; if (n1 == L) { WriteLine((n1 * k) / 2); } else { string t = s.Substring(n1, n2); long ans = n1 / 2 + count(t) * k + (n3 + n1) / 2 * (k - 1) + n3 / 2; WriteLine(ans); } } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199]]
8
301
4
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { static int n; public static void Main() { int n = int.Parse(Console.ReadLine()); int[][] matrix = Enumerable.Range(0, n) .Select(h => Console.ReadLine() .Select((x, w) => h == w ? 0 : x == '1' ? 1 : 1 << 29) .ToArray()) .ToArray(); PotentializedUnionFind<bool> puf = new PotentializedUnionFind<bool>(n, (x, y) => x ^ y, x => !x, false); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (matrix[i][j] == 1 && !puf.TryUnite(i, j, true)) { Console.WriteLine(-1); return; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) matrix[j][k] = Min(matrix[j][k], matrix[j][i] + matrix[i][k]); Console.WriteLine(matrix.Max(x => x.Max()) + 1); } } class PotentializedUnionFind<T> where T : IEquatable<T> { public int Size { get; private set; } public int GroupCount { get; private set; } public IEnumerable<int> AllRepresents => Parent.Where((x, y) => x == y); int[] Sizes; int[] Parent; T[] Value; Func<T, T, T> Operate; Func<T, T> Inverse; T Identity; public PotentializedUnionFind(int count, Func<T, T, T> operate, Func<T, T> inverse, T identity) { Size = count; GroupCount = count; Parent = new int[count]; Sizes = new int[count]; Value = new T[count]; for (int i = 0; i < count; i++) { Sizes[Parent[i] = i] = 1; Value[i] = identity; } Operate = operate; Inverse = inverse; Identity = identity; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryUnite(int x, int y, T distance) { T xd, yd; int xp = Find(x, out xd); int yp = Find(y, out yd); if (yp == xp) return Operate(distance, yd).Equals(xd); GroupCount--; if (Sizes[xp] < Sizes[yp]) { Value[xp] = Operate(Operate(Inverse(xd), distance), yd); Parent[xp] = yp; Sizes[yp] += Sizes[xp]; } else { Value[yp] = Operate(Operate(Inverse(yd), Inverse(distance)), xd); Parent[yp] = xp; Sizes[xp] += Sizes[yp]; } return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetSize(int x) => Sizes[Find(x)]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public T GetPotential(int x, int y) { T xd, yd; int xp = Find(x, out xd); int yp = Find(y, out yd); if (yp == xp) return Operate(Inverse(xd), yd); else return Identity; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Find(int x) { while (x != Parent[x]) { Value[x] = Operate(Value[x], Value[Parent[x]]); Parent[x] = Parent[Parent[x]]; x = Parent[x]; } return x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private int Find(int x, out T distance) { distance = Identity; while (x != Parent[x]) { Value[x] = Operate(Value[x], Value[Parent[x]]); Parent[x] = Parent[Parent[x]]; distance = Operate(distance, Value[x]); x = Parent[x]; } return x; } }
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { static int n; public static void Main() { int n = int.Parse(Console.ReadLine()); int[][] matrix = Enumerable.Range(0, n) .Select(h => Console.ReadLine() .Select((x, w) => h == w ? 0 : x == '1' ? 1 : 1 << 29) .ToArray()) .ToArray(); PotentializedUnionFind<bool> puf = new PotentializedUnionFind<bool>(n, (x, y) => x ^ y, x => x, false); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (matrix[i][j] == 1 && !puf.TryUnite(i, j, true)) { Console.WriteLine(-1); return; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) matrix[j][k] = Min(matrix[j][k], matrix[j][i] + matrix[i][k]); Console.WriteLine(matrix.Max(x => x.Max()) + 1); } } class PotentializedUnionFind<T> where T : IEquatable<T> { public int Size { get; private set; } public int GroupCount { get; private set; } public IEnumerable<int> AllRepresents => Parent.Where((x, y) => x == y); int[] Sizes; int[] Parent; T[] Value; Func<T, T, T> Operate; Func<T, T> Inverse; T Identity; public PotentializedUnionFind(int count, Func<T, T, T> operate, Func<T, T> inverse, T identity) { Size = count; GroupCount = count; Parent = new int[count]; Sizes = new int[count]; Value = new T[count]; for (int i = 0; i < count; i++) { Sizes[Parent[i] = i] = 1; Value[i] = identity; } Operate = operate; Inverse = inverse; Identity = identity; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryUnite(int x, int y, T distance) { T xd, yd; int xp = Find(x, out xd); int yp = Find(y, out yd); if (yp == xp) return Operate(distance, yd).Equals(xd); GroupCount--; if (Sizes[xp] < Sizes[yp]) { Value[xp] = Operate(Operate(Inverse(xd), distance), yd); Parent[xp] = yp; Sizes[yp] += Sizes[xp]; } else { Value[yp] = Operate(Operate(Inverse(yd), Inverse(distance)), xd); Parent[yp] = xp; Sizes[xp] += Sizes[yp]; } return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetSize(int x) => Sizes[Find(x)]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public T GetPotential(int x, int y) { T xd, yd; int xp = Find(x, out xd); int yp = Find(y, out yd); if (yp == xp) return Operate(Inverse(xd), yd); else return Identity; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Find(int x) { while (x != Parent[x]) { Value[x] = Operate(Value[x], Value[Parent[x]]); Parent[x] = Parent[Parent[x]]; x = Parent[x]; } return x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private int Find(int x, out T distance) { distance = Identity; while (x != Parent[x]) { Value[x] = Operate(Value[x], Value[Parent[x]]); Parent[x] = Parent[Parent[x]]; distance = Operate(distance, Value[x]); x = Parent[x]; } return x; } }
[["-", 3, 4, 0, 28, 0, 218, 8, 241, 0, 111]]
8
996
1
using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Security.Cryptography.X509Certificates; namespace atcorder { class Program { static void Main(string[] args) { double N = double.Parse(Console.ReadLine()); if (N % 2 == 0) { Console.WriteLine(N / 2 / N); } else { Console.WriteLine((N / 2 + 1) / N); } } } } //文字配列の取得 // string[] strArray = Console.ReadLine().Split(' ').ToArray(); // string S = strArray[0]; // string T = strArray[1]; //文字列の取得 // string s = Console.ReadLine(); //数字配列の取得 // int[] intArray = Console.ReadLine().Split(' ').Select(i => // int.Parse(i)).ToArray(); int H = intArray[0]; int W = intArray[1]; //数字の取得 // int N = int.Parse(Console.ReadLine()); //リスト型(int)の取得 // List<int> A = new List<int>(Console.ReadLine().Split(' ').Select(i => // int.Parse(i)).ToArray()); //出力 // Console.WriteLine(ans); // Console.WriteLine("Yes"); // Console.WriteLine("No"); // Console.WriteLine(cnt);
using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Security.Cryptography.X509Certificates; namespace atcorder { class Program { static void Main(string[] args) { double N = double.Parse(Console.ReadLine()); if (N % 2 == 0) { Console.WriteLine((N / 2) / N); } else { Console.WriteLine(((int)N / 2 + 1) / N); } } } } //文字配列の取得 // string[] strArray = Console.ReadLine().Split(' ').ToArray(); // string S = strArray[0]; // string T = strArray[1]; //文字列の取得 // string s = Console.ReadLine(); //数字配列の取得 // int[] intArray = Console.ReadLine().Split(' ').Select(i => // int.Parse(i)).ToArray(); int H = intArray[0]; int W = intArray[1]; //数字の取得 // int N = int.Parse(Console.ReadLine()); //リスト型(int)の取得 // List<int> A = new List<int>(Console.ReadLine().Split(' ').Select(i => // int.Parse(i)).ToArray()); //出力 // Console.WriteLine(ans); // Console.WriteLine("Yes"); // Console.WriteLine("No"); // Console.WriteLine(cnt);
[["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 25], ["+", 31, 23, 0, 16, 31, 16, 31, 74, 0, 24], ["+", 31, 23, 0, 16, 31, 16, 31, 74, 39, 199], ["+", 31, 23, 0, 16, 31, 16, 31, 74, 0, 25]]
8
120
5
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { const int MOD = 1000000007; const int INF = 1 << 30; static void Main(string[] args) { Solve(); } static void Solve() { Scaner cin = new Scaner(); write wr = new write(); Math2 mmath = new Math2(); //ここから var o = cin.IntM; int N = o[0]; int NN = N / 2; if (N == 1) wr.wri(1.0); if (N % 2 == 0) wr.wri(1.0 / 2.0); else wr.wri((double)(NN + 1) / N); } } // b-aで降順 class PriorityQueue<T> { private readonly List<T> heap; private readonly Comparison<T> compare; private int size; public PriorityQueue() : this(Comparer<T>.Default) {} public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) {} public PriorityQueue(Comparison<T> comparison) : this(16, comparison) {} public PriorityQueue(int capacity, Comparison<T> comparison) { this.heap = new List<T>(capacity); this.compare = comparison; } public void Enqueue(T item) { this.heap.Add(item); var i = size++; while (i > 0) { var p = (i - 1) >> 1; if (compare(this.heap[p], item) <= 0) break; this.heap[i] = heap[p]; i = p; } this.heap[i] = item; } public T Dequeue() { var ret = this.heap[0]; var x = this.heap[--size]; var i = 0; while ((i << 1) + 1 < size) { var a = (i << 1) + 1; var b = (i << 1) + 2; if (b < size && compare(heap[b], heap[a]) < 0) a = b; if (compare(heap[a], x) >= 0) break; heap[i] = heap[a]; i = a; } heap[i] = x; heap.RemoveAt(size); return ret; } public T Peek() { return heap[0]; } public int Count { get { return size; } } public bool Any() { return size > 0; } } class Math2 { public long[] Fact; // Nまでの階乗 public long[] invFact; //階乗の逆元 // Nまでの階乗をセットしておく public void FactoricalModSet(long N, long MOD = 1000000007) { Fact = new long[N + 1]; invFact = new long[N + 1]; Fact[0] = 1; for (long i = 1; i <= N; ++i) { Fact[i] = Fact[i - 1] * i % MOD; } invFact[N] = powMOD(Fact[N], MOD - 2); for (long i = N; i > 0; --i) { invFact[i - 1] = invFact[i] * i % MOD; } } public long nCrMod(long n, long r, long MOD = 1000000007) { return (Fact[n] * invFact[r] % MOD * invFact[n - r] % MOD); } public long powMOD(long x, long n, long MOD = 1000000007) { long res = 1; while (n > 0) { if ((n & 1) != 0) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } static long GCD(long x, long y) { if (y == 0) return x; return GCD(y, x % y); } static long LCM(long x, long y) { return x * y / GCD(x, y); } } class Scaner { public string Str => Console.ReadLine(); public string[] StrM => Str.Split(' '); public long[] LongM => StrM.Select(long.Parse).ToArray(); public int[] IntM => StrM.Select(int.Parse).ToArray(); public char[] CharM => Str.ToArray(); public long Long1 => LongM[0]; public int Int1 => IntM[0]; } class write { public void wri<T>(T x) { Console.WriteLine(x); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { const int MOD = 1000000007; const int INF = 1 << 30; static void Main(string[] args) { Solve(); } static void Solve() { Scaner cin = new Scaner(); write wr = new write(); Math2 mmath = new Math2(); //ここから var o = cin.IntM; int N = o[0]; int NN = N / 2; if (N == 1) wr.wri(1.0); else if (N % 2 == 0) wr.wri(1.0 / 2.0); else wr.wri((double)(NN + 1) / N); } } // b-aで降順 class PriorityQueue<T> { private readonly List<T> heap; private readonly Comparison<T> compare; private int size; public PriorityQueue() : this(Comparer<T>.Default) {} public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) {} public PriorityQueue(Comparison<T> comparison) : this(16, comparison) {} public PriorityQueue(int capacity, Comparison<T> comparison) { this.heap = new List<T>(capacity); this.compare = comparison; } public void Enqueue(T item) { this.heap.Add(item); var i = size++; while (i > 0) { var p = (i - 1) >> 1; if (compare(this.heap[p], item) <= 0) break; this.heap[i] = heap[p]; i = p; } this.heap[i] = item; } public T Dequeue() { var ret = this.heap[0]; var x = this.heap[--size]; var i = 0; while ((i << 1) + 1 < size) { var a = (i << 1) + 1; var b = (i << 1) + 2; if (b < size && compare(heap[b], heap[a]) < 0) a = b; if (compare(heap[a], x) >= 0) break; heap[i] = heap[a]; i = a; } heap[i] = x; heap.RemoveAt(size); return ret; } public T Peek() { return heap[0]; } public int Count { get { return size; } } public bool Any() { return size > 0; } } class Math2 { public long[] Fact; // Nまでの階乗 public long[] invFact; //階乗の逆元 // Nまでの階乗をセットしておく public void FactoricalModSet(long N, long MOD = 1000000007) { Fact = new long[N + 1]; invFact = new long[N + 1]; Fact[0] = 1; for (long i = 1; i <= N; ++i) { Fact[i] = Fact[i - 1] * i % MOD; } invFact[N] = powMOD(Fact[N], MOD - 2); for (long i = N; i > 0; --i) { invFact[i - 1] = invFact[i] * i % MOD; } } public long nCrMod(long n, long r, long MOD = 1000000007) { return (Fact[n] * invFact[r] % MOD * invFact[n - r] % MOD); } public long powMOD(long x, long n, long MOD = 1000000007) { long res = 1; while (n > 0) { if ((n & 1) != 0) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } static long GCD(long x, long y) { if (y == 0) return x; return GCD(y, x % y); } static long LCM(long x, long y) { return x * y / GCD(x, y); } } class Scaner { public string Str => Console.ReadLine(); public string[] StrM => Str.Split(' '); public long[] LongM => StrM.Select(long.Parse).ToArray(); public int[] IntM => StrM.Select(int.Parse).ToArray(); public char[] CharM => Str.ToArray(); public long Long1 => LongM[0]; public int Int1 => IntM[0]; } class write { public void wri<T>(T x) { Console.WriteLine(x); } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 95]]
8
959
1
using System; using static System.Console; using static System.Math; namespace Main { class Program { static void Main(string[] args) { decimal N = decimal.Parse(ReadLine()); Write(N % 2 == 0 ? Floor(N / 2) / N : (Floor(N / 2) + 1) / 2); } } }
using System; using static System.Console; using static System.Math; namespace Main { class Program { static void Main(string[] args) { decimal N = decimal.Parse(ReadLine()); Write(N % 2 == 0 ? Floor(N / 2) / N : (Floor(N / 2) + 1) / N); } } }
[["-", 3, 4, 0, 28, 0, 41, 75, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 41, 75, 16, 12, 22]]
8
77
2
using System; class P { static void Main() { double a = double.Parse(Console.ReadLine()); double odd = 0; for (int i = 1; i < a; i++) { if (i % 2 == 1) { odd++; } } Console.WriteLine(odd / a); } }
using System; class P { static void Main() { double a = double.Parse(Console.ReadLine()); double odd = 0; for (int i = 1; i <= a; i++) { if (i % 2 == 1) { odd++; } } Console.WriteLine(odd / a); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
71
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Contest20191017 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int Counter = 0; for (int i = 0; i < N; i++) { if (i % 2 != 0) { Counter++; } } double a = (double)Counter / N; Console.WriteLine(a); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Contest20191017 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int Counter = 0; for (int i = 1; i <= N; i++) { if (i % 2 != 0) { Counter++; } } double a = (double)Counter / N; Console.WriteLine(a); } } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
111
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A { class Program { static void Main(string[] args) { int input = int.Parse(Console.ReadLine()); int ret = (int)Math.Ceiling(input / 2.0); Console.WriteLine(ret); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace A { class Program { static void Main(string[] args) { int input = int.Parse(Console.ReadLine()); int ret = (int)Math.Ceiling(input / 2.0); Console.WriteLine(ret / (double)input); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 85], ["+", 3, 4, 0, 28, 0, 16, 12, 74, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 12, 74, 39, 199], ["+", 3, 4, 0, 28, 0, 16, 12, 74, 51, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25]]
8
82
5
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder_20190707_1 { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); string OK = "No"; for (int i = 0; i < 10; i++) { if (N % i == 0 && N / i < 10) { OK = "Yes"; } } Console.WriteLine(OK); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder_20190707_1 { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); string OK = "No"; for (int i = 1; i < 10; i++) { if (N % i == 0 && N / i < 10) { OK = "Yes"; } } Console.WriteLine(OK); } } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203]]
8
112
2
using System; using System.Collections; using System.Collections.Generic; using System.Linq; class EightyONe { static void Main(string[] args) { var input = int.Parse(Console.ReadLine()); var ans = ""; bool flag = false; if (input > 81) { ans = "No"; } else { for (var i = 2; i < 10; i++) { if (input % i == 0 && input / i <= 9) { ans = "Yes"; flag = true; break; } } if (!flag) { ans = "No"; } } Console.WriteLine(ans); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; class EightyONe { static void Main(string[] args) { var input = int.Parse(Console.ReadLine()); var ans = ""; bool flag = false; if (input > 81) { ans = "No"; } else { for (var i = 1; i < 10; i++) { if (input % i == 0 && input / i <= 9) { ans = "Yes"; flag = true; break; } } if (!flag) { ans = "No"; } } Console.WriteLine(ans); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203]]
8
141
2
using System; namespace Sample { class Sample { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int flg = 0; for (int i = 1; i <= 9; i++) { if ((N / i < 10) && (N % 1 == 0)) { flg = 1; } } if (flg == 1) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } }
using System; namespace Sample { class Sample { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int flg = 0; for (int i = 1; i <= 9; i++) { if (((N / i) < 10) && ((N % i) == 0)) { flg = 1; } } if (flg == 1) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } }
[["+", 15, 16, 31, 23, 0, 16, 31, 23, 0, 24], ["+", 15, 16, 31, 23, 0, 16, 31, 23, 0, 25], ["+", 8, 196, 0, 57, 15, 16, 12, 23, 0, 24], ["-", 15, 16, 12, 23, 0, 16, 31, 16, 12, 203], ["+", 12, 23, 0, 16, 31, 23, 0, 16, 12, 22], ["+", 15, 16, 12, 23, 0, 16, 31, 23, 0, 25]]
8
110
6
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static void Main(string[] args) { try { Solve.Answer(); } catch (Exception ex) { WriteLine(ex.ToString()); } } } public class Solve { public static void Answer() { var n = IO.ReadInt; bool f = false; for (int i = 1; i <= 9; i++) { for (int j = 1; j < 9; j++) { if (i * j == n) { f = true; break; } } } IO.WriteLineYes_or_No(f); } public class IO { #region Input public static string ReadString => ReadLine(); public static int ReadInt => int.Parse(ReadLine()); public static long ReadLong => long.Parse(ReadLine()); public static double ReadDouble => double.Parse(ReadLine()); public static List<string> ReadSplitString => ReadLine().Split().ToList(); public static List<int> ReadSplitInt => ReadLine().Split().Select(s => int.Parse(s)).ToList(); public static List<long> ReadSplitLong => ReadLine().Split().Select(s => long.Parse(s)).ToList(); public static List<double> ReadSplitdouble => ReadLine().Split().Select(s => double.Parse(s)).ToList(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); #endregion } /// <summary> /// 正の数の四則演算のみ対応 /// </summary> public class Mod { // 割る数の定義 private readonly static long M = 1000000007; // 足し算 public static long Add(long a, long b) => (a * b) % M; // 引き算 public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; // 掛け算 public static long Multi(long a, long b) => (a * b) % M; } public class Scanner { public string Str => Console.ReadLine().Trim(); public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray(); public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray(); // public char[][] Grid(int n) => Create(n, () => Str.ToCharArray()); // public int[] ArrInt1D(int n) => Create(n, () => Int); // public long[] ArrLong1D(int n) => Create(n, () => Long); // public int[][] ArrInt2D(int n) => Create(n, () => ArrInt); // public long[][] ArrLong2D(int n) => Create(n, () => ArrLong); private Queue<string> q = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] // インライン化を推奨する public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); } public void Make<T1>(out T1 v1) => v1 = Next<T1>(); public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); } } } public class Combination { public static long Choice2(long n) { if (n <= 1) return 0; if (n == 2) return 1; else return n * (n - 1) / 2; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static void Main(string[] args) { try { Solve.Answer(); } catch (Exception ex) { WriteLine(ex.ToString()); } } } public class Solve { public static void Answer() { var n = IO.ReadInt; bool f = false; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { f = true; break; } } } IO.WriteLineYes_or_No(f); } public class IO { #region Input public static string ReadString => ReadLine(); public static int ReadInt => int.Parse(ReadLine()); public static long ReadLong => long.Parse(ReadLine()); public static double ReadDouble => double.Parse(ReadLine()); public static List<string> ReadSplitString => ReadLine().Split().ToList(); public static List<int> ReadSplitInt => ReadLine().Split().Select(s => int.Parse(s)).ToList(); public static List<long> ReadSplitLong => ReadLine().Split().Select(s => long.Parse(s)).ToList(); public static List<double> ReadSplitdouble => ReadLine().Split().Select(s => double.Parse(s)).ToList(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); #endregion } /// <summary> /// 正の数の四則演算のみ対応 /// </summary> public class Mod { // 割る数の定義 private readonly static long M = 1000000007; // 足し算 public static long Add(long a, long b) => (a * b) % M; // 引き算 public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; // 掛け算 public static long Multi(long a, long b) => (a * b) % M; } public class Scanner { public string Str => Console.ReadLine().Trim(); public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray(); public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray(); // public char[][] Grid(int n) => Create(n, () => Str.ToCharArray()); // public int[] ArrInt1D(int n) => Create(n, () => Int); // public long[] ArrLong1D(int n) => Create(n, () => Long); // public int[][] ArrInt2D(int n) => Create(n, () => ArrInt); // public long[][] ArrLong2D(int n) => Create(n, () => ArrLong); private Queue<string> q = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] // インライン化を推奨する public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); } public void Make<T1>(out T1 v1) => v1 = Next<T1>(); public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); } } } public class Combination { public static long Choice2(long n) { if (n <= 1) return 0; if (n == 2) return 1; else return n * (n - 1) / 2; } } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
8
1,017
2
using System; using System.Text; using System.Collections.Generic; using static System.Math; using System.Linq; using static System.Array; namespace ConsoleApp2 { class Program { static long[,] A; static bool[,] map; static long count = 0; static long N; static long[] used; static long ans = 0; static void Main(string[] args) { N = long.Parse(Console.ReadLine()); if (Solve(N) && N <= 81) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } static bool Solve(long N) { for (int i = 1; i <= 9; i++) { if (N % i == 0 && N / i <= 9) { return true; } } return true; } } }
using System; using System.Text; using System.Collections.Generic; using static System.Math; using System.Linq; using static System.Array; namespace ConsoleApp2 { class Program { static long[,] A; static bool[,] map; static long count = 0; static long N; static long[] used; static long ans = 0; static void Main(string[] args) { N = long.Parse(Console.ReadLine()); if (Solve(N) && N <= 81) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } static bool Solve(long N) { for (int i = 1; i <= 9; i++) { if (N % i == 0 && N / i <= 9) { return true; } } return false; } } }
[["-", 0, 195, 8, 196, 0, 37, 0, 211, 0, 146], ["+", 0, 195, 8, 196, 0, 37, 0, 211, 0, 147]]
8
181
2
using System; using System.Collections.Generic; using System.Linq; public static class Program { public static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (i * j == n) { Console.WriteLine("Yes"); } } } Console.WriteLine("No"); } }
using System; using System.Collections.Generic; using System.Linq; public static class Program { public static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); for (int i = 1; i < 10; i++) for (int j = 1; j < 10; j++) if (i * j == n) { Console.WriteLine("Yes"); return; } Console.WriteLine("No"); } }
[["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 45], ["-", 0, 7, 8, 196, 0, 7, 8, 196, 0, 45], ["-", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46], ["-", 0, 7, 8, 196, 0, 7, 8, 196, 0, 46], ["+", 8, 7, 8, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 7, 8, 57, 64, 196, 0, 37, 0, 35]]
8
107
2
using System; using System.Collections.Generic; using System.Linq; public static class Program { public static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (i * j == n) { Console.WriteLine("Yes"); } } } Console.WriteLine("No"); } }
using System; using System.Collections.Generic; using System.Linq; public static class Program { public static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (i * j == n) { Console.WriteLine("Yes"); return; } } } Console.WriteLine("No"); } }
[["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35]]
8
107
2
using System; class Program { static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); //標準入力 for (int i = 1; i < 10; i++) { int b = a / i; if (a % i > 0) { continue; } if (b < 10) { Console.WriteLine("yes"); return; } } Console.WriteLine("no"); } }
using System; class Program { static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); //標準入力 for (int i = 1; i < 10; i++) { int b = a / i; if (a % i > 0) { continue; } if (b < 10) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
96
4
using System; namespace B_81 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string ans = "NO"; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { ans = "YES"; break; } } } Console.WriteLine(ans); } } }
using System; namespace B_81 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string ans = "No"; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { ans = "Yes"; break; } } } Console.WriteLine(ans); } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["+", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["-", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222], ["+", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222]]
8
100
4
using System; class P { static void Main() { int val = int.Parse(Console.ReadLine()); int kekka; string s = "NO"; for (int i = 1; i <= 9; i++) { if (val % i == 0) { if (val / i <= 9) { s = "YES"; break; } } } Console.WriteLine(s); } }
using System; class P { static void Main() { int val = int.Parse(Console.ReadLine()); int kekka; string s = "No"; for (int i = 1; i <= 9; i++) { if (val % i == 0) { if (val / i <= 9) { s = "Yes"; break; } } } Console.WriteLine(s); } }
[["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["+", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["-", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222], ["+", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222]]
8
89
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _81 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string ans = "no"; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (n == i * j) { ans = "yes"; break; } } } Console.WriteLine(ans); Console.ReadKey(true); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _81 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string ans = "No"; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (n == i * j) { ans = "Yes"; break; } } } Console.WriteLine(ans); Console.ReadKey(true); } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["+", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["-", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222], ["+", 64, 196, 0, 1, 0, 11, 12, 5, 0, 222]]
8
131
4
using System; class Program { static void Main(string[] args) { // 整数の入力 int a = int.Parse(Console.ReadLine()); bool result = false; //計算 for (int i = 1; i > 10; i++) { for (int j = 1; j > 10; j++) { if (i * j == a) { result = true; } } } //判定 if (result) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } }
using System; class Program { static void Main(string[] args) { // 整数の入力 int a = int.Parse(Console.ReadLine()); bool result = false; //計算 for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (i * j == a) { result = true; } } } //判定 if (result) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 47], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18]]
8
113
4
using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { Console.WriteLine("YES"); return; } } } Console.WriteLine("NO"); } }
using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { Console.WriteLine("Yes"); return; } } } Console.WriteLine("No"); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
94
4
using System; public class Program { private static void Main() { int N = int.Parse(Console.ReadLine()); bool result = false; for (int i = 2; i <= 9; i++) { if (N % i == 0 && N / i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
using System; public class Program { private static void Main() { int N = int.Parse(Console.ReadLine()); bool result = false; for (int i = 1; i <= 9; i++) { if (N % i == 0 && N / i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203]]
8
87
2
using System; class program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); float z; for (int k = 1; k <= 9; k++) { z = (float)num / (float)k; int y; y = (int)z * k; if (y == num && (int)z <= 9 && num != 1) { Console.WriteLine("Yes"); Environment.Exit(0); } } Console.WriteLine("No"); } }
using System; class program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); float z; for (int k = 1; k <= 9; k++) { z = (float)num / (float)k; int y; y = (int)z * k; if (y == num && (int)z <= 9) { Console.WriteLine("Yes"); Environment.Exit(0); } } Console.WriteLine("No"); } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 79], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203]]
8
119
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { static int N; static void INPUT() { N = NextInt(); } static void OUT() { var b = false; for (int i = 2; i <= 9; i++) { if (N % i == 0) { if (N / i <= 9) { b = true; break; } } } Console.WriteLine(b ? "Yes" : "No"); } static void Main(string[] args) { #if DEBUG TESTINPUT(); // INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() { N = 50; } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util {}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { static int N; static void INPUT() { N = NextInt(); } static void OUT() { var b = false; for (int i = 1; i <= 9; i++) { if (N % i == 0) { if (N / i <= 9) { b = true; break; } } } Console.WriteLine(b ? "Yes" : "No"); } static void Main(string[] args) { #if DEBUG TESTINPUT(); // INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() { N = 1; } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util {}
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["+", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203]]
8
476
4
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // string[] str = Console.ReadLine().Split(' // ');//2つ以上のスペース区切り入力の取得 string str = Console.ReadLine(); int A = int.Parse(str); // int B = int.Parse(str[1]); // string[] strHeight = Console.ReadLine().Split(' // ');//2つ以上のスペース区切り入力の取得 string[] results = new string[N]; for (int i = 1; i < 10; i++) { if (A % i == 0) { int B = A / i; if (B >= 1 && B < 10) { Console.WriteLine("Yes"); } } } Console.WriteLine("No"); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // string[] str = Console.ReadLine().Split(' // ');//2つ以上のスペース区切り入力の取得 string str = Console.ReadLine(); int A = int.Parse(str); // int B = int.Parse(str[1]); // string[] strHeight = Console.ReadLine().Split(' // ');//2つ以上のスペース区切り入力の取得 string[] results = new string[N]; for (int i = 1; i < 10; i++) { if (A % i == 0) { int B = A / i; if (B >= 1 && B < 10) { Console.WriteLine("Yes"); return; } } } Console.WriteLine("No"); } }
[["+", 64, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 64, 196, 0, 57, 64, 196, 0, 37, 0, 35]]
8
112
2
using System; namespace B { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { if (N % i != 0 && N / i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } } }
using System; namespace B { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { if (N % i == 0 && N / i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 79], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 60]]
8
88
2
using System; public class Program { public static int maxM; public static int[] shinamonotachi; public static void Main(string[] args) { var input = int.Parse(Console.ReadLine()); var answers = new int[81]; var index = 0; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { answers[index] = i * j; } } foreach (var copyAnswer in answers) { if (copyAnswer == input) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
using System; public class Program { public static int maxM; public static int[] shinamonotachi; public static void Main(string[] args) { var input = int.Parse(Console.ReadLine()); var answers = new int[81]; var index = 0; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { answers[index] = i * j; index++; } } foreach (var copyAnswer in answers) { if (copyAnswer == input) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
[["+", 0, 7, 8, 196, 0, 1, 0, 223, 0, 22], ["+", 0, 7, 8, 196, 0, 1, 0, 223, 0, 29], ["+", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
138
3
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Globalization; using System.Numerics; using System.Text.RegularExpressions; // byte 255 int<2147483647 ≒ 2 * 10^9 long < 9223372036854775807 ≒ 9 * 10^18 // decimal 29桁 class Myon { // chokudaiさんのコードをコピペ static Scanner cin; public Myon() {} public static int Main() { // Console.SetOut(new Printer(Console.OpenStandardOutput())); cin = new Scanner(); new Myon().calc(); // test(); return 0; } public void calc() { int n = cin.nextInt(); for (int i = 9; i >= 2; i--) { if (n % i == 0) { int b = n / i; if (b <= 9) { ShowL("Yes"); return; } } } ShowL("No"); } static long nibuntansaku(long[] a, long searchingNumber, long iLast) { // sが挿入されてソートされるaのindex long f = 0, l = iLast; long len = a.Length; long index = -1; while (l - f >= 1) { // index = (f + l) / 2; if (a[index] == searchingNumber) { f = index; break; } else if (a[index] > searchingNumber) l = index; else f = index + 1; } return f; } #region utilities public static void ShowL() { Console.WriteLine(""); } public static void ShowL<T>(T s) { Console.WriteLine(s); } public static void Show<T>(T s) { Console.Write(s); } public static void Yorn(bool isOK, string ifOK = "Yes", string ifNOTOK = "No") { if (isOK) ShowL(ifOK); else ShowL(ifNOTOK); } #endregion utilities } class Printer : StreamWriter { public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) {} public Printer(Stream stream, Encoding encoding) : base(stream, encoding) {} } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { while (i >= s.Length) { string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; } return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] nextIntArr(long n) { int[] output = new int[n]; for (long i = 0; i < n; i++) output[i] = nextInt(); return output; } public long nextLong() { return long.Parse(next()); } public long[] nextLongArr(long n) { long[] output = new long[n]; for (long i = 0; i < n; i++) output[i] = nextLong(); return output; } public double nextDouble() { return double.Parse(next()); } } static class SMath { /// <summary> /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。 /// 12 なら 2,2,3 です。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static List<long> Factorization(long n) { if (n <= 0) return null; List<long> output = new List<long>(); int i = 2; long max = (long)Math.Sqrt(n); ; while (n > 1) { if (i > max) { output.Add(n); break; } while (n % i == 0) { output.Add(i); n /= i; } i++; } return output; } /// <summary> /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。 /// 84 = (2^2 * 3 * 7) なら 2, 1, 0, 1  remainder = 1です。(打ち切られない) /// 13 = 0,0,...0 remainder = 2019 /// ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる) /// </summary> /// <param name="n"></param> /// <param name="remainder"></param> /// <returns></returns> public static List<long> Factorization(long n, out long remainder) { // output[i] が i-1番目の素数の数を示す //ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる) remainder = 1; if (n <= 0) return null; List<long> output = new List<long>(); long tempn = 2; long max = (long)Math.Sqrt(n); bool[] isNOTPrime = new bool[max + 1]; while (n > 1) { if (tempn > max) { remainder = n; break; } if (!isNOTPrime[tempn]) { output.Add(0); while (n % tempn == 0) { output[output.Count - 1]++; n /= tempn; } for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true; } tempn++; } return output; } /// <summary> /// i番目の素数をoutput[i-1]に返します。 /// ただしnより大きい素数については判定しません。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static List<long> GetPrimeNumbers(long n) { if (n <= 0) return null; List<long> output = new List<long>(); long tempn = 2; bool[] isNOTPrime = new bool[n + 1]; while (n > 1) { if (tempn > n) { break; } if (!isNOTPrime[tempn]) { output.Add(tempn); for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true; } tempn++; } return output; } private static long[][] combMemo; private static int maxn_combination = -1; public static long Combination(int n, int k) { if (n < 0 || k > n) return 0; if (n == k || k == 0) return 1; if (k > n / 2) return Combination(n, n - k); if (maxn_combination < n) { maxn_combination = n; combMemo = new long [n + 1][]; } if (combMemo[n] == null) combMemo[n] = new long[n / 2 + 1]; if (combMemo[n][k] != 0) return combMemo[n][k]; else { combMemo[n][k] = Combination(n - 1, k) + Combination(n - 1, k - 1); return combMemo[n][k]; } } public static long Gcd(long a, long b) { if (a <= 0) return -1; // if (a > b) return gcd(b, a); if (b == 0) return a; return Gcd(b, a % b); } public static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } public class pairComparer : IComparer<int[]> { int index = 0; public pairComparer(int n) { index = n; } public pairComparer() { index = 0; } public int Compare(int[] x, int[] y) { return y[index] - y[index]; } } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Globalization; using System.Numerics; using System.Text.RegularExpressions; // byte 255 int<2147483647 ≒ 2 * 10^9 long < 9223372036854775807 ≒ 9 * 10^18 // decimal 29桁 class Myon { // chokudaiさんのコードをコピペ static Scanner cin; public Myon() {} public static int Main() { // Console.SetOut(new Printer(Console.OpenStandardOutput())); cin = new Scanner(); new Myon().calc(); // test(); return 0; } public void calc() { int n = cin.nextInt(); for (int i = 9; i >= 1; i--) { if (n % i == 0) { int b = n / i; if (b <= 9) { ShowL("Yes"); return; } } } ShowL("No"); } static long nibuntansaku(long[] a, long searchingNumber, long iLast) { // sが挿入されてソートされるaのindex long f = 0, l = iLast; long len = a.Length; long index = -1; while (l - f >= 1) { // index = (f + l) / 2; if (a[index] == searchingNumber) { f = index; break; } else if (a[index] > searchingNumber) l = index; else f = index + 1; } return f; } #region utilities public static void ShowL() { Console.WriteLine(""); } public static void ShowL<T>(T s) { Console.WriteLine(s); } public static void Show<T>(T s) { Console.Write(s); } public static void Yorn(bool isOK, string ifOK = "Yes", string ifNOTOK = "No") { if (isOK) ShowL(ifOK); else ShowL(ifNOTOK); } #endregion utilities } class Printer : StreamWriter { public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) {} public Printer(Stream stream, Encoding encoding) : base(stream, encoding) {} } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { while (i >= s.Length) { string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; } return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] nextIntArr(long n) { int[] output = new int[n]; for (long i = 0; i < n; i++) output[i] = nextInt(); return output; } public long nextLong() { return long.Parse(next()); } public long[] nextLongArr(long n) { long[] output = new long[n]; for (long i = 0; i < n; i++) output[i] = nextLong(); return output; } public double nextDouble() { return double.Parse(next()); } } static class SMath { /// <summary> /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。 /// 12 なら 2,2,3 です。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static List<long> Factorization(long n) { if (n <= 0) return null; List<long> output = new List<long>(); int i = 2; long max = (long)Math.Sqrt(n); ; while (n > 1) { if (i > max) { output.Add(n); break; } while (n % i == 0) { output.Add(i); n /= i; } i++; } return output; } /// <summary> /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。 /// 84 = (2^2 * 3 * 7) なら 2, 1, 0, 1  remainder = 1です。(打ち切られない) /// 13 = 0,0,...0 remainder = 2019 /// ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる) /// </summary> /// <param name="n"></param> /// <param name="remainder"></param> /// <returns></returns> public static List<long> Factorization(long n, out long remainder) { // output[i] が i-1番目の素数の数を示す //ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる) remainder = 1; if (n <= 0) return null; List<long> output = new List<long>(); long tempn = 2; long max = (long)Math.Sqrt(n); bool[] isNOTPrime = new bool[max + 1]; while (n > 1) { if (tempn > max) { remainder = n; break; } if (!isNOTPrime[tempn]) { output.Add(0); while (n % tempn == 0) { output[output.Count - 1]++; n /= tempn; } for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true; } tempn++; } return output; } /// <summary> /// i番目の素数をoutput[i-1]に返します。 /// ただしnより大きい素数については判定しません。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static List<long> GetPrimeNumbers(long n) { if (n <= 0) return null; List<long> output = new List<long>(); long tempn = 2; bool[] isNOTPrime = new bool[n + 1]; while (n > 1) { if (tempn > n) { break; } if (!isNOTPrime[tempn]) { output.Add(tempn); for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true; } tempn++; } return output; } private static long[][] combMemo; private static int maxn_combination = -1; public static long Combination(int n, int k) { if (n < 0 || k > n) return 0; if (n == k || k == 0) return 1; if (k > n / 2) return Combination(n, n - k); if (maxn_combination < n) { maxn_combination = n; combMemo = new long [n + 1][]; } if (combMemo[n] == null) combMemo[n] = new long[n / 2 + 1]; if (combMemo[n][k] != 0) return combMemo[n][k]; else { combMemo[n][k] = Combination(n - 1, k) + Combination(n - 1, k - 1); return combMemo[n][k]; } } public static long Gcd(long a, long b) { if (a <= 0) return -1; // if (a > b) return gcd(b, a); if (b == 0) return a; return Gcd(b, a % b); } public static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } public class pairComparer : IComparer<int[]> { int index = 0; public pairComparer(int n) { index = n; } public pairComparer() { index = 0; } public int Compare(int[] x, int[] y) { return y[index] - y[index]; } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203]]
8
1,428
2
using System; using System.Collections.Generic; using System.Linq; public class Solution { public static void Main() { var n = int.Parse(Console.ReadLine()); var ok = false; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { ok = true; } } } Console.WriteLine(ok ? "Yes" : "Mo"); } }
using System; using System.Collections.Generic; using System.Linq; public class Solution { public static void Main() { var n = int.Parse(Console.ReadLine()); var ok = false; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { if (i * j == n) { ok = true; } } } Console.WriteLine(ok ? "Yes" : "No"); } }
[["-", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222]]
8
108
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _1 { class Program { static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); for (int i = 9; i >= 1; i--) { if (a % i == 0) { if (i < 9 && a / i < 9) { Console.WriteLine("Yes"); return; } } } Console.WriteLine("No"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _1 { class Program { static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); for (int i = 9; i >= 1; i--) { if (a % i == 0) { if (i <= 9 && a / i <= 9) { Console.WriteLine("Yes"); return; } } } Console.WriteLine("No"); } } }
[["-", 64, 196, 0, 57, 15, 16, 31, 16, 17, 18], ["+", 64, 196, 0, 57, 15, 16, 31, 16, 17, 19], ["-", 64, 196, 0, 57, 15, 16, 12, 16, 17, 18], ["+", 64, 196, 0, 57, 15, 16, 12, 16, 17, 19]]
8
120
4
using System; using System.IO; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { public static void Main() { int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { if (n % i == 0 && n % i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
using System; using System.IO; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; public static class P { public static void Main() { int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= 9; i++) { if (n % i == 0 && n / i <= 9) { Console.WriteLine("Yes"); return; } } Console.WriteLine("No"); } }
[["-", 0, 57, 15, 16, 12, 16, 31, 16, 17, 109], ["+", 0, 57, 15, 16, 12, 16, 31, 16, 17, 85]]
8
152
2
using System; namespace C { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); long n = (long)Math.Sqrt((double)N); if ((long)Math.Pow((double)n, 2.0) == N) { Console.WriteLine((long)n - 1); return; } for (long i = n; i > 1; i--) { if (N % i == 0) { long j = N / i; Console.WriteLine(j + i - 2); return; } } Console.WriteLine(N - 1); } } }
using System; namespace C { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); long n = (long)Math.Sqrt((double)N); if ((long)Math.Pow((double)n, 2.0) == N) { Console.WriteLine((long)(n * 2) - 2); return; } for (long i = n; i > 1; i--) { if (N % i == 0) { long j = N / i; Console.WriteLine(j + i - 2); return; } } Console.WriteLine(N - 1); } } }
[["+", 0, 28, 0, 16, 31, 74, 51, 23, 0, 24], ["+", 0, 16, 31, 74, 51, 23, 0, 16, 17, 48], ["+", 0, 16, 31, 74, 51, 23, 0, 16, 12, 203], ["+", 0, 28, 0, 16, 31, 74, 51, 23, 0, 25], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
142
6
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; static void Main(string[] args) { try { SetOut(sw); Solve.Answer(); Out.Flush(); } catch (Exception ex) { WriteLine(ex.ToString()); } } } public class Solve { public static void Answer() { long n = IO.RLong; double root = Sqrt(n); // O(10^6) int j = 0; int k = 0; for (int i = (int)root; i > 0; i--) { if (n % i == 0) { k = i; j = (int)n / i; break; } } WriteLine(k + j - 2); } public class IO { #region Input public static int RInt => int.Parse(ReadLine()); public static long RLong => long.Parse(ReadLine()); public static double RDouble => double.Parse(ReadLine()); public static string[] RSString => ReadLine().Split(); public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray(); public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray(); public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO"); #endregion } public class Mod { #region private readonly static long M = 1000000007; public static long Add(long a, long b) => (a * b) % M; public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; public static long Multi(long a, long b) => (a * b) % M; #endregion } public static int GCD(int x, int y) { if (x < y) Swap(ref x, ref y); if (y == 0) return x; else return GCD(x % y, y); } public static void Swap(ref int x, ref int y) { int tmp = x; x = y; y = tmp; } public static long Choice2(long n) { #region if (n <= 1) return 0; if (n == 2) return 1; else return n * (n - 1) / 2; #endregion } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; static void Main(string[] args) { try { SetOut(sw); Solve.Answer(); Out.Flush(); } catch (Exception ex) { WriteLine(ex.ToString()); } } } public class Solve { public static void Answer() { long n = IO.RLong; double root = Sqrt(n); // O(10^6) long j = 0; long k = 0; for (long i = (long)root; i > 0; i--) { if (n % i == 0) { k = i; j = (long)n / i; break; } } WriteLine(k + j - 2); } public class IO { #region Input public static int RInt => int.Parse(ReadLine()); public static long RLong => long.Parse(ReadLine()); public static double RDouble => double.Parse(ReadLine()); public static string[] RSString => ReadLine().Split(); public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray(); public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray(); public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO"); #endregion } public class Mod { #region private readonly static long M = 1000000007; public static long Add(long a, long b) => (a * b) % M; public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; public static long Multi(long a, long b) => (a * b) % M; #endregion } public static int GCD(int x, int y) { if (x < y) Swap(ref x, ref y); if (y == 0) return x; else return GCD(x % y, y); } public static void Swap(ref int x, ref int y) { int tmp = x; x = y; y = tmp; } public static long Choice2(long n) { #region if (n <= 1) return 0; if (n == 2) return 1; else return n * (n - 1) / 2; #endregion } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["-", 10, 198, 0, 200, 0, 212, 0, 74, 39, 199], ["+", 10, 198, 0, 200, 0, 212, 0, 74, 39, 199], ["-", 0, 1, 0, 11, 12, 16, 31, 74, 39, 199], ["+", 0, 1, 0, 11, 12, 16, 31, 74, 39, 199]]
8
663
10
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { static long N; static void INPUT() { N = NextLong(); } static void OUT() { var a = long.MaxValue; for (var n = 1; n < Math.Pow(N, 0.5); n++) if (N % n == 0) { a = Math.Min(n + N / n - 2, a); } Console.WriteLine(a); } static void Main(string[] args) { #if DEBUG // TESTINPUT(); INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() {} } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util {}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using static Input; using static Util; class Program { static long N; static void INPUT() { N = NextLong(); } static void OUT() { var a = long.MaxValue; for (var n = 1L; n <= Math.Pow(N, 0.5); n++) if (N % n == 0) { a = Math.Min(n + N / n - 2, a); } Console.WriteLine(a); } static void Main(string[] args) { #if DEBUG // TESTINPUT(); INPUT(); #else INPUT(); #endif OUT(); } static void TESTINPUT() {} } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } class Util {}
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
472
4
using System; namespace C { class Program { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); double rootn = Math.Sqrt(n); int max = 0; for (int i = 1; i < rootn + 1; i++) { if (n % i == 0 & i > max) { max = i; } } Console.WriteLine(max + n / max - 2); } } }
using System; namespace C { class Program { static void Main(string[] args) { string input = Console.ReadLine(); long n = long.Parse(input); double rootn = Math.Sqrt(n); long max = 0; for (long i = 1; i < rootn + 1; i++) { if (n % i == 0 & i > max) { max = i; } } Console.WriteLine(max + n / max - 2); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["-", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199]]
8
105
8
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; public static class P { public static void Main() { var n = long.Parse(Console.ReadLine()); var min = long.MaxValue; for (long i = 1; i * i <= n; i++) { if (n % i != 0) continue; min = Min(min, n / i + i - 1); } Console.WriteLine(min); } }
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; public static class P { public static void Main() { var n = long.Parse(Console.ReadLine()); var min = long.MaxValue; for (long i = 1; i * i <= n; i++) { if (n % i != 0) continue; min = Min(min, n / i + i - 2); } Console.WriteLine(min); } }
[["-", 12, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
145
2
using System; using System.Collections.Generic; using System.Linq; namespace c { class Program { static void Main(string[] args) { var n = long.Parse(Console.ReadLine()); var l = new List<long>(); var count = long.MaxValue; for (var i = 1; i < Math.Sqrt(n); i++) { if (n % i == 0) { count = Math.Min(n / i + i - 2, count); } } Console.WriteLine(count); } } }
using System; using System.Collections.Generic; using System.Linq; namespace c { class Program { static void Main(string[] args) { var n = long.Parse(Console.ReadLine()); var l = new List<long>(); var count = long.MaxValue; for (var i = 1; i < Math.Sqrt(n) + 1; i++) { if (n % i == 0) { count = Math.Min(n / i + i - 2, count); } } Console.WriteLine(count); } } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203]]
8
121
2
using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; public static class Program { private static readonly long INF = long.MaxValue / 2; private static readonly int MOD = 1000000007; struct Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T first; public U second; private bool CompWithFirst; public Pair(T f, U s, int comp) { first = f; second = s; CompWithFirst = comp == 1; } public int CompareTo(Pair<T, U> other) { return CompWithFirst ? first.CompareTo(other.first) : second.CompareTo(other.second); } } public class PriorityQueue<T> where T : IComparable<T> { private List<T> Q; private readonly int r; public PriorityQueue(bool ascending) { if (ascending) { r = 1; } else { r = -1; } Q = new List<T>(); } private void PushHeap(List<T> list, T item) { int n = list.Count(); list.Add(item); while (n != 0) { int pIndex = (n - 1) / 2; if (list[n].CompareTo(list[pIndex]) * r > 0) { Swap(Q, n, pIndex); } else { break; } n = pIndex; } } private void PopHeap(List<T> list) { int n = list.Count() - 1; list[0] = list[n]; list.RemoveAt(n); int cur = 0; int comp; while (2 * cur + 1 <= n - 1) { int c1 = 2 * cur + 1; int c2 = 2 * (cur + 1); if (c1 == n - 1) { comp = c1; } else { comp = list[c1].CompareTo(list[c2]) * r > 0 ? c1 : c2; } if (list[cur].CompareTo(list[comp]) * r < 0) { Swap(Q, cur, comp); } else { break; } cur = comp; } } private void Swap(List<T> list, int a, int b) { T keep = list[a]; list[a] = list[b]; list[b] = keep; } public void Enqueue(T value) { PushHeap(Q, value); } public T Dequeue() { T ret = Q[0]; PopHeap(Q); return ret; } public T Peek() { return Q[0]; } public int CountItem() { return Q.Count(); } } static long ModInv(long a, long m) { long b = m, u = 1, v = 0; while (b != 0) { long t = a / b; a -= t * b; long tmp = a; a = b; b = tmp; u -= t * v; tmp = u; u = v; v = tmp; } u %= m; if (u < 0) u += m; return u; } public static long CalcComb(long n, long m) { //条件チェック if (n < m) return 0; if (n < 0 || m < 0) return 0; // nCm = nCm-1 ×(n-m+1)/ m // return CalcFact(n) / (CalcFact(m) * CalcFact(n - m)); // MODで割ってbの逆元を求める return CalcFact(n) * ModInv((CalcFact(m) * CalcFact(n - m)) % MOD, MOD) % MOD; } /// <summary> /// 階乗を計算する。 /// </summary> /// <param name="n">n</param> /// <returns>nの階乗(n!)</returns> public static long CalcFact(long n) { if (n <= 0) { return 1; } return n * CalcFact(n - 1) % MOD; } struct Pos { public int x; public int y; public Pos(int x, int y) { this.x = x; this.y = y; } public static Pos operator +(Pos p1, Pos p2) { var result = new Pos(); result.x = p2.x + p1.x; result.y = p2.y + p1.y; return result; } public static Pos operator -(Pos p1, Pos p2) { var result = new Pos(); result.x = p2.x - p1.x; result.y = p2.y - p1.y; return result; } } class Modular { private const int M = 1000000007; private long value; public Modular(long value) { this.value = value; } public static implicit operator Modular(long a) { var m = a % M; return new Modular((m < 0) ? m + M : m); } public static Modular operator +(Modular a, Modular b) { return a.value + b.value; } public static Modular operator -(Modular a, Modular b) { return a.value - b.value; } public static Modular operator *(Modular a, Modular b) { return a.value * b.value; } private static Modular Pow(Modular a, int n) { switch (n) { case 0: return 1; case 1: return a; default: var p = Pow(a, n / 2); return p * p * Pow(a, n % 2); } } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, M - 2); } private static readonly List<int> facs = new List<int> { 1 }; private static Modular Fac(int n) { for (int i = facs.Count; i <= n; ++i) { facs.Add((int)(Math.BigMul(facs.Last(), i) % M)); } return facs[n]; } public static Modular Ncr(int n, int r) { return (n < r) ? 0 : (n == r) ? 1 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(Modular a) { return (int)a.value; } } static class Permutation<T> { private static void Search(List<T[]> perms, Stack<T> stack, T[] a) { int N = a.Length; if (N == 0) { perms.Add(stack.Reverse().ToArray()); } else { var b = new T[N - 1]; Array.Copy(a, 1, b, 0, N - 1); for (int i = 0; i < a.Length; ++i) { stack.Push(a[i]); Search(perms, stack, b); if (i < b.Length) { b[i] = a[i]; } stack.Pop(); } } } public static IEnumerable<T[]> All(IEnumerable<T> src) { var perms = new List<T[]>(); Search(perms, new Stack<T>(), src.ToArray()); return perms; } } public static int ReadLineToInt() { return int.Parse(ReadLine()); } public static long ReadLineToLong() { return long.Parse(ReadLine()); } public static int[] ReadLineToInts() { return ReadLine().Split(' ').Select(int.Parse).ToArray(); } static int nibunn(int[] array, int target) { int L = 0; int R = array.Length; while (R - L > 1) { int mid = (R + L) / 2; if (array[mid] > target) L = mid; else R = mid; } return L; } public static void Main(string[] args) { long n = ReadLineToLong(); // pos(i,j) i*j=n i+j-2 = ans a <= b long ans = INF; for (long i = 1; i * i < n; i++) { if (n % i != 0) continue; long j = n / i; ans = Math.Min(ans, i + j - 2); } WriteLine(ans); } }
using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; public static class Program { private static readonly long INF = long.MaxValue / 2; private static readonly int MOD = 1000000007; struct Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T first; public U second; private bool CompWithFirst; public Pair(T f, U s, int comp) { first = f; second = s; CompWithFirst = comp == 1; } public int CompareTo(Pair<T, U> other) { return CompWithFirst ? first.CompareTo(other.first) : second.CompareTo(other.second); } } public class PriorityQueue<T> where T : IComparable<T> { private List<T> Q; private readonly int r; public PriorityQueue(bool ascending) { if (ascending) { r = 1; } else { r = -1; } Q = new List<T>(); } private void PushHeap(List<T> list, T item) { int n = list.Count(); list.Add(item); while (n != 0) { int pIndex = (n - 1) / 2; if (list[n].CompareTo(list[pIndex]) * r > 0) { Swap(Q, n, pIndex); } else { break; } n = pIndex; } } private void PopHeap(List<T> list) { int n = list.Count() - 1; list[0] = list[n]; list.RemoveAt(n); int cur = 0; int comp; while (2 * cur + 1 <= n - 1) { int c1 = 2 * cur + 1; int c2 = 2 * (cur + 1); if (c1 == n - 1) { comp = c1; } else { comp = list[c1].CompareTo(list[c2]) * r > 0 ? c1 : c2; } if (list[cur].CompareTo(list[comp]) * r < 0) { Swap(Q, cur, comp); } else { break; } cur = comp; } } private void Swap(List<T> list, int a, int b) { T keep = list[a]; list[a] = list[b]; list[b] = keep; } public void Enqueue(T value) { PushHeap(Q, value); } public T Dequeue() { T ret = Q[0]; PopHeap(Q); return ret; } public T Peek() { return Q[0]; } public int CountItem() { return Q.Count(); } } static long ModInv(long a, long m) { long b = m, u = 1, v = 0; while (b != 0) { long t = a / b; a -= t * b; long tmp = a; a = b; b = tmp; u -= t * v; tmp = u; u = v; v = tmp; } u %= m; if (u < 0) u += m; return u; } public static long CalcComb(long n, long m) { //条件チェック if (n < m) return 0; if (n < 0 || m < 0) return 0; // nCm = nCm-1 ×(n-m+1)/ m // return CalcFact(n) / (CalcFact(m) * CalcFact(n - m)); // MODで割ってbの逆元を求める return CalcFact(n) * ModInv((CalcFact(m) * CalcFact(n - m)) % MOD, MOD) % MOD; } /// <summary> /// 階乗を計算する。 /// </summary> /// <param name="n">n</param> /// <returns>nの階乗(n!)</returns> public static long CalcFact(long n) { if (n <= 0) { return 1; } return n * CalcFact(n - 1) % MOD; } struct Pos { public int x; public int y; public Pos(int x, int y) { this.x = x; this.y = y; } public static Pos operator +(Pos p1, Pos p2) { var result = new Pos(); result.x = p2.x + p1.x; result.y = p2.y + p1.y; return result; } public static Pos operator -(Pos p1, Pos p2) { var result = new Pos(); result.x = p2.x - p1.x; result.y = p2.y - p1.y; return result; } } class Modular { private const int M = 1000000007; private long value; public Modular(long value) { this.value = value; } public static implicit operator Modular(long a) { var m = a % M; return new Modular((m < 0) ? m + M : m); } public static Modular operator +(Modular a, Modular b) { return a.value + b.value; } public static Modular operator -(Modular a, Modular b) { return a.value - b.value; } public static Modular operator *(Modular a, Modular b) { return a.value * b.value; } private static Modular Pow(Modular a, int n) { switch (n) { case 0: return 1; case 1: return a; default: var p = Pow(a, n / 2); return p * p * Pow(a, n % 2); } } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, M - 2); } private static readonly List<int> facs = new List<int> { 1 }; private static Modular Fac(int n) { for (int i = facs.Count; i <= n; ++i) { facs.Add((int)(Math.BigMul(facs.Last(), i) % M)); } return facs[n]; } public static Modular Ncr(int n, int r) { return (n < r) ? 0 : (n == r) ? 1 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(Modular a) { return (int)a.value; } } static class Permutation<T> { private static void Search(List<T[]> perms, Stack<T> stack, T[] a) { int N = a.Length; if (N == 0) { perms.Add(stack.Reverse().ToArray()); } else { var b = new T[N - 1]; Array.Copy(a, 1, b, 0, N - 1); for (int i = 0; i < a.Length; ++i) { stack.Push(a[i]); Search(perms, stack, b); if (i < b.Length) { b[i] = a[i]; } stack.Pop(); } } } public static IEnumerable<T[]> All(IEnumerable<T> src) { var perms = new List<T[]>(); Search(perms, new Stack<T>(), src.ToArray()); return perms; } } public static int ReadLineToInt() { return int.Parse(ReadLine()); } public static long ReadLineToLong() { return long.Parse(ReadLine()); } public static int[] ReadLineToInts() { return ReadLine().Split(' ').Select(int.Parse).ToArray(); } static int nibunn(int[] array, int target) { int L = 0; int R = array.Length; while (R - L > 1) { int mid = (R + L) / 2; if (array[mid] > target) L = mid; else R = mid; } return L; } public static void Main(string[] args) { long n = ReadLineToLong(); // pos(i,j) i*j=n i+j-2 = ans a <= b long ans = INF; for (long i = 1; i * i <= n; i++) { if (n % i != 0) continue; long j = n / i; ans = Math.Min(ans, i + j - 2); } WriteLine(ans); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
1,720
2
using System; using System.Linq; using System.Collections.Generic; using static System.Console; using static System.Math; class Solver { Scanner sc = new Scanner(); public void Solve() { var N = sc.nextLong(); long min = long.MaxValue; for (long i = 0; i * i < N; i++) { if (i == 0) continue; if (N % i != 0) continue; long j = N / i; min = Min(min, i + j - 2); } if (min == long.MaxValue) { min = N - 1; } WriteLine(min); } IEnumerable<long> PrimeFactors(long n) { long i = 2; long tmp = n; while (i * i <= n) { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class Program { static void Main(string[] args) { Solver s = new Solver(); s.Solve(); } }
using System; using System.Linq; using System.Collections.Generic; using static System.Console; using static System.Math; class Solver { Scanner sc = new Scanner(); public void Solve() { var N = sc.nextLong(); long min = long.MaxValue; for (long i = 0; i * i <= N; i++) { if (i == 0) continue; if (N % i != 0) continue; long j = N / i; min = Min(min, i + j - 2); } if (min == long.MaxValue) { min = N - 1; } WriteLine(min); } IEnumerable<long> PrimeFactors(long n) { long i = 2; long tmp = n; while (i * i <= n) { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class Program { static void Main(string[] args) { Solver s = new Solver(); s.Solve(); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
583
2
using System; using System.Collections.Generic; using static System.Math; using System.Linq; using System.Text; class DandA { static void Main(string[] args) { long n = ReadLineLong(); long i = 1; long rtn_a = 0; long rtn_b = 0; while (i * i < n) { if (n % i == 0) { rtn_a = n / i; rtn_b = i; } i++; } Console.WriteLine((rtn_a + rtn_b) - 2); } static string[] ary = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; static private string ReadLineStr() { return Console.ReadLine(); } static private int ReadLineInt() { return int.Parse(Console.ReadLine()); } static private long ReadLineLong() { return long.Parse(Console.ReadLine()); } static private string[] ReadLineStrArray() { return Console.ReadLine().Trim().Split(); } static private int[] ReadLineIntArray() { return Console.ReadLine() .Trim() .Split() .Select(a => int.Parse(a)) .ToArray(); } static private long[] ReadLineLongArray() { return Console.ReadLine() .Trim() .Split() .Select(a => long.Parse(a)) .ToArray(); } static private double[] ReadLineDoubleArray() { return Console.ReadLine() .Trim() .Split() .Select(a => double.Parse(a)) .ToArray(); } static private decimal[] ReadLineDecimalArray() { return Console.ReadLine() .Trim() .Split() .Select(a => decimal.Parse(a)) .ToArray(); } static private string[][] ReadMLineStrArray(int arg) { return new int [arg].Select(_ => Console.ReadLine().Split()).ToArray(); } static private int[][] ReadMLineIntArray(int arg) { return new int [arg] .Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()) .ToArray(); } static private bool IsPrime(int num) { if (num < 2) return false; if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = Math.Sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } }
using System; using System.Collections.Generic; using static System.Math; using System.Linq; using System.Text; class DandA { static void Main(string[] args) { long n = ReadLineLong(); long i = 1; long rtn_a = 0; long rtn_b = 0; while (i * i <= n) { if (n % i == 0) { rtn_a = n / i; rtn_b = i; } i++; } Console.WriteLine((rtn_a + rtn_b) - 2); } static string[] ary = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; static private string ReadLineStr() { return Console.ReadLine(); } static private int ReadLineInt() { return int.Parse(Console.ReadLine()); } static private long ReadLineLong() { return long.Parse(Console.ReadLine()); } static private string[] ReadLineStrArray() { return Console.ReadLine().Trim().Split(); } static private int[] ReadLineIntArray() { return Console.ReadLine() .Trim() .Split() .Select(a => int.Parse(a)) .ToArray(); } static private long[] ReadLineLongArray() { return Console.ReadLine() .Trim() .Split() .Select(a => long.Parse(a)) .ToArray(); } static private double[] ReadLineDoubleArray() { return Console.ReadLine() .Trim() .Split() .Select(a => double.Parse(a)) .ToArray(); } static private decimal[] ReadLineDecimalArray() { return Console.ReadLine() .Trim() .Split() .Select(a => decimal.Parse(a)) .ToArray(); } static private string[][] ReadMLineStrArray(int arg) { return new int [arg].Select(_ => Console.ReadLine().Split()).ToArray(); } static private int[][] ReadMLineIntArray(int arg) { return new int [arg] .Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()) .ToArray(); } static private bool IsPrime(int num) { if (num < 2) return false; if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = Math.Sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } }
[["-", 0, 195, 8, 196, 0, 52, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 17, 19]]
8
745
2
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); long i = 1; var xys = new List<long[]>(); while (i < Math.Sqrt(n)) { if (n % i == 0) { var xy = new long[] { i, n / i }; xys.Add(xy); } i++; } long[] sums = xys.Select(e => (e[0] - 1) + (e[1] - 1)).ToArray(); Console.WriteLine(sums.Min()); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); long i = 1; var xys = new List<long[]>(); while (i <= Math.Sqrt(n)) { if (n % i == 0) { var xy = new long[] { i, n / i }; xys.Add(xy); } i++; } long[] sums = xys.Select(e => (e[0] - 1) + (e[1] - 1)).ToArray(); Console.WriteLine(sums.Min()); } } }
[["-", 0, 195, 8, 196, 0, 52, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 17, 19]]
8
159
2