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; class ABC_132_D { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); PascalsTriangle pascal = new PascalsTriangle(2000); for (int i = 1; i < K + 1; i++) { long a = pascal.ncr(N - K + 1, i) % 1000000007; long b = pascal.ncr(K - 1, i - 1) % 1000000007; long ans = a * b % 1000000007; Console.WriteLine(ans); } } class PascalsTriangle { int m = 1000000007; int[,] table; public PascalsTriangle(int depth) { Calc(depth); } public PascalsTriangle(int depth, int mod) { m = mod; Calc(depth); } private void Calc(int depth) { table = new int[depth, depth]; table[0, 0] = 1; for (int d = 1; d < depth; d++) { for (int i = 0; i < d + 1; i++) { int left = (i == 0) ? 0 : table[d - 1, i - 1]; int right = (i == d) ? 0 : table[d - 1, i]; table[d, i] = (left + right) % m; } } } public int ncr(int n, int r) { return table[n, r]; } } }
using System; class ABC_132_D { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); PascalsTriangle pascal = new PascalsTriangle(2010); for (int i = 1; i < K + 1; i++) { long a = pascal.ncr(N - K + 1, i) % 1000000007; long b = pascal.ncr(K - 1, i - 1) % 1000000007; long ans = a * b % 1000000007; Console.WriteLine(ans); } } class PascalsTriangle { int m = 1000000007; int[,] table; public PascalsTriangle(int depth) { Calc(depth); } public PascalsTriangle(int depth, int mod) { m = mod; Calc(depth); } private void Calc(int depth) { table = new int[depth, depth]; table[0, 0] = 1; for (int d = 1; d < depth; d++) { for (int i = 0; i < d + 1; i++) { int left = (i == 0) ? 0 : table[d - 1, i - 1]; int right = (i == d) ? 0 : table[d - 1, i]; table[d, i] = (left + right) % m; } } } public int ncr(int n, int r) { return table[n, r]; } } }
[["-", 0, 212, 0, 230, 3, 4, 0, 28, 0, 203], ["+", 0, 212, 0, 230, 3, 4, 0, 28, 0, 203]]
8
328
2
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using Extensions; using static System.Math; using static Extensions.MathExtension; using static Extensions.ConsoleInputExtension; using static Extensions.ConsoleOutputExtension; class Solver { public void Solve() { // Solve Code Here int N = Cin, K = Cin; const long mod = 1000000007; long red = N - K, blue = K; long x, k; for (int i = 1; i <= K; i++) { if (red - i + 1 < 0) { Coutln(1); continue; } long pattern = 1; long n = blue - 1; long r = blue - i; for (int j = 0; j < r; j++) { pattern *= n - j; pattern %= mod; pattern += mod; pattern %= mod; } for (int j = 2; j <= r; j++) { extGCD(j, mod, out x, out k); while (x < 0) x += mod; pattern *= x; pattern %= mod; } n = red + 1; r = red - (i - 1); for (int j = 0; j < r; j++) { pattern *= n - j; pattern %= mod; pattern += mod; pattern %= mod; } for (int j = 2; j <= r; j++) { extGCD(j, mod, out x, out k); while (x < 0) x += mod; pattern *= x; pattern %= mod; } Coutln(pattern); } } // Other Functions Here long extGCD(long a, long b, out long x, out long y) { if (b == 0) { x = 1; y = 0; return a; } long d = extGCD(b, a % b, out y, out x); y -= a / b * x; return d; } } class EntryPoint { static void Main(string[] args) { new Solver().Solve(); Flush(); } } class PriorityQueue<T> { private readonly List<Tuple<int, T>> _list = new List<Tuple<int, T>>(); public PriorityQueue() {} public void Push(T item, int priority) { var itemIndex = _list.Count; _list.Add(Tuple.Create(priority, item)); int parentIndex; while ((parentIndex = GetParentIndex(itemIndex)) != -1 && priority > _list[parentIndex].Item1) { Swap(itemIndex, parentIndex); itemIndex = parentIndex; } } private int GetParentIndex(int index) { if (index == 0) return -1; return ((index + 1) >> 1) - 1; } private Tuple<int, int> GetChildrenIndex(int index) { var item2 = (index + 1) << 1; var item1 = item2 - 1; return item2 >= _list.Count ? null : Tuple.Create(item1, item2); } public T Pop() { var item = _list[0].Item2; Tuple<int, int> childrenIndex; int index = 0; while ((childrenIndex = GetChildrenIndex(index)) != null) { if (_list[childrenIndex.Item1].Item1 > _list[childrenIndex.Item2].Item1) _list[index] = _list[childrenIndex.Item1]; else _list[index] = _list[childrenIndex.Item2]; } return item; } public T Peek() { return _list[0].Item2; } private void Swap(int index1, int index2) { var tmp = _list[index1]; _list[index1] = _list[index2]; _list[index2] = tmp; } } struct Rational { private long _numerator; private long _denominator; public long Numerator => _numerator; public long Denominator => _denominator; public Rational(long numerator, long denominator) { var gcd = GCD(numerator, denominator); _numerator = numerator / gcd; _denominator = denominator / gcd; } public static Rational operator +(Rational a, Rational b) { return new Rational(a._numerator * b._denominator + b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator -(Rational a, Rational b) { return new Rational(a._numerator * b._denominator - b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator *(Rational a, Rational b) { return new Rational(a._numerator * b._numerator, a._denominator * b._denominator); } public static Rational operator /(Rational a, Rational b) { return new Rational(a._numerator * b._denominator, a._denominator * b._numerator); } public static explicit operator Rational(int i) { return new Rational(i, 1); } public static explicit operator Rational(long l) { return new Rational(l, 1); } public static explicit operator Rational(double d) { long denominator = 1; while (d % 1 != 0) { denominator *= 2; d *= 2; } return new Rational((long)d, denominator); } public static explicit operator Rational(float f) { long denominator = 1; while (f % 1 != 0) { denominator *= 2; f *= 2; } return new Rational((long)f, denominator); } } class UnionFind { private readonly int[] _array; public UnionFind(int N) { _array = new int[N]; for (int i = 0; i < N; i++) { _array[i] = i; } } public int GetRootNode(int n) { if (_array[n] == n) return n; return _array[n] = GetRootNode(_array[n]); } public void UnionGroup(int a, int b) { var rootA = GetRootNode(a); var rootB = GetRootNode(b); if (rootA == rootB) return; _array[rootA] = rootB; } public bool IsSameGroup(int a, int b) => GetRootNode(a) == GetRootNode(b); public bool IsRoot(int n) => _array[n] == n; } namespace Extensions { public class ConsoleInputExtension { private static readonly ConsoleInputExtension _cin = new ConsoleInputExtension(); public static ConsoleInputExtension Cin => _cin; private static readonly Queue<string> _inputQueue = new Queue<string>(); private ConsoleInputExtension() {} public static implicit operator string(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return _inputQueue.Dequeue(); } public static implicit operator int(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return int.Parse(_inputQueue.Dequeue()); } public static implicit operator long(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return long.Parse(_inputQueue.Dequeue()); } public static implicit operator float(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return float.Parse(_inputQueue.Dequeue()); } public static implicit operator double(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return double.Parse(_inputQueue.Dequeue()); } } public static class ConsoleOutputExtension { private static StreamWriter _writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; public static void Cout(object o) { _writer.Write(o); } public static void CoutF(object o) { _writer.Write(o); _writer.Flush(); } public static void Coutln(object o) { _writer.WriteLine(o); } public static void CoutlnF(object o) { _writer.WriteLine(o); _writer.Flush(); } public static void Flush() { _writer.Flush(); } } public static class MathExtension { //最小公倍数 public static int LCM(int num1, int num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } public static long LCM(long num1, long num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } //最大公約数 public static int GCD(int num1, int num2) { int a = Math.Max(num1, num2); int b = Math.Min(num1, num2); int mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } public static long GCD(long num1, long num2) { long a = Math.Max(num1, num2); long b = Math.Min(num1, num2); long mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } } public static class EnumerableExtension { public delegate void Function<in T>(T val); public static void ForEach<T>(this IEnumerable<T> enumerable, Function<T> function) { foreach (var x in enumerable) { function(x); } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using Extensions; using static System.Math; using static Extensions.MathExtension; using static Extensions.ConsoleInputExtension; using static Extensions.ConsoleOutputExtension; class Solver { public void Solve() { // Solve Code Here int N = Cin, K = Cin; const long mod = 1000000007; long red = N - K, blue = K; long x, k; for (int i = 1; i <= K; i++) { if (red - i + 1 < 0) { Coutln(0); continue; } long pattern = 1; long n = blue - 1; long r = blue - i; for (int j = 0; j < r; j++) { pattern *= n - j; pattern %= mod; pattern += mod; pattern %= mod; } for (int j = 2; j <= r; j++) { extGCD(j, mod, out x, out k); while (x < 0) x += mod; pattern *= x; pattern %= mod; } n = red + 1; r = red - (i - 1); for (int j = 0; j < r; j++) { pattern *= n - j; pattern %= mod; pattern += mod; pattern %= mod; } for (int j = 2; j <= r; j++) { extGCD(j, mod, out x, out k); while (x < 0) x += mod; pattern *= x; pattern %= mod; } Coutln(pattern); } } // Other Functions Here long extGCD(long a, long b, out long x, out long y) { if (b == 0) { x = 1; y = 0; return a; } long d = extGCD(b, a % b, out y, out x); y -= a / b * x; return d; } } class EntryPoint { static void Main(string[] args) { new Solver().Solve(); Flush(); } } class PriorityQueue<T> { private readonly List<Tuple<int, T>> _list = new List<Tuple<int, T>>(); public PriorityQueue() {} public void Push(T item, int priority) { var itemIndex = _list.Count; _list.Add(Tuple.Create(priority, item)); int parentIndex; while ((parentIndex = GetParentIndex(itemIndex)) != -1 && priority > _list[parentIndex].Item1) { Swap(itemIndex, parentIndex); itemIndex = parentIndex; } } private int GetParentIndex(int index) { if (index == 0) return -1; return ((index + 1) >> 1) - 1; } private Tuple<int, int> GetChildrenIndex(int index) { var item2 = (index + 1) << 1; var item1 = item2 - 1; return item2 >= _list.Count ? null : Tuple.Create(item1, item2); } public T Pop() { var item = _list[0].Item2; Tuple<int, int> childrenIndex; int index = 0; while ((childrenIndex = GetChildrenIndex(index)) != null) { if (_list[childrenIndex.Item1].Item1 > _list[childrenIndex.Item2].Item1) _list[index] = _list[childrenIndex.Item1]; else _list[index] = _list[childrenIndex.Item2]; } return item; } public T Peek() { return _list[0].Item2; } private void Swap(int index1, int index2) { var tmp = _list[index1]; _list[index1] = _list[index2]; _list[index2] = tmp; } } struct Rational { private long _numerator; private long _denominator; public long Numerator => _numerator; public long Denominator => _denominator; public Rational(long numerator, long denominator) { var gcd = GCD(numerator, denominator); _numerator = numerator / gcd; _denominator = denominator / gcd; } public static Rational operator +(Rational a, Rational b) { return new Rational(a._numerator * b._denominator + b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator -(Rational a, Rational b) { return new Rational(a._numerator * b._denominator - b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator *(Rational a, Rational b) { return new Rational(a._numerator * b._numerator, a._denominator * b._denominator); } public static Rational operator /(Rational a, Rational b) { return new Rational(a._numerator * b._denominator, a._denominator * b._numerator); } public static explicit operator Rational(int i) { return new Rational(i, 1); } public static explicit operator Rational(long l) { return new Rational(l, 1); } public static explicit operator Rational(double d) { long denominator = 1; while (d % 1 != 0) { denominator *= 2; d *= 2; } return new Rational((long)d, denominator); } public static explicit operator Rational(float f) { long denominator = 1; while (f % 1 != 0) { denominator *= 2; f *= 2; } return new Rational((long)f, denominator); } } class UnionFind { private readonly int[] _array; public UnionFind(int N) { _array = new int[N]; for (int i = 0; i < N; i++) { _array[i] = i; } } public int GetRootNode(int n) { if (_array[n] == n) return n; return _array[n] = GetRootNode(_array[n]); } public void UnionGroup(int a, int b) { var rootA = GetRootNode(a); var rootB = GetRootNode(b); if (rootA == rootB) return; _array[rootA] = rootB; } public bool IsSameGroup(int a, int b) => GetRootNode(a) == GetRootNode(b); public bool IsRoot(int n) => _array[n] == n; } namespace Extensions { public class ConsoleInputExtension { private static readonly ConsoleInputExtension _cin = new ConsoleInputExtension(); public static ConsoleInputExtension Cin => _cin; private static readonly Queue<string> _inputQueue = new Queue<string>(); private ConsoleInputExtension() {} public static implicit operator string(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return _inputQueue.Dequeue(); } public static implicit operator int(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return int.Parse(_inputQueue.Dequeue()); } public static implicit operator long(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return long.Parse(_inputQueue.Dequeue()); } public static implicit operator float(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return float.Parse(_inputQueue.Dequeue()); } public static implicit operator double(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return double.Parse(_inputQueue.Dequeue()); } } public static class ConsoleOutputExtension { private static StreamWriter _writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; public static void Cout(object o) { _writer.Write(o); } public static void CoutF(object o) { _writer.Write(o); _writer.Flush(); } public static void Coutln(object o) { _writer.WriteLine(o); } public static void CoutlnF(object o) { _writer.WriteLine(o); _writer.Flush(); } public static void Flush() { _writer.Flush(); } } public static class MathExtension { //最小公倍数 public static int LCM(int num1, int num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } public static long LCM(long num1, long num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } //最大公約数 public static int GCD(int num1, int num2) { int a = Math.Max(num1, num2); int b = Math.Min(num1, num2); int mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } public static long GCD(long num1, long num2) { long a = Math.Max(num1, num2); long b = Math.Min(num1, num2); long mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } } public static class EnumerableExtension { public delegate void Function<in T>(T val); public static void ForEach<T>(this IEnumerable<T> enumerable, Function<T> function) { foreach (var x in enumerable) { function(x); } } } }
[["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203]]
8
1,980
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.ABC_132 { class E { static void Main(string[] args) { Method(args); } static void Method(string[] args) { int[] nm = ReadInts(); int n = nm[0]; int m = nm[1]; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) { graph[i] = new List<int>(); } for (int i = 0; i < m; i++) { int[] uv = ReadInts(); int u = uv[0] - 1; int v = uv[1] - 1; graph[u].Add(v); } int[] st = ReadInts(); int s = st[0] - 1; int t = st[1] - 1; long[,] dists = Dijkstra(s, graph); if (dists[t, 0] == long.MaxValue) WriteLine(-1); else WriteLine(dists[t, 0]); } static long[,] Dijkstra(int startIndex, List<int>[] graph) { var pQueue = new PriorityQueue<int>(); var distances = new long[graph.Length, 3]; // Initialize nodes for (int i = 0; i < graph.Length; i++) { for (int j = 0; j < 3; j++) { distances[i, j] = long.MaxValue; } } pQueue.Enqueue(0, startIndex); while (pQueue.Exist()) { KeyValuePair<long, int> pair = pQueue.Dequeue(); long distance = pair.Key; int index = pair.Value; if (distances[index, distance % 3] <= distance) continue; distances[index, distance % 3] = distance; // Update priority queue for (int i = 0; i < graph[index].Count; i++) { int nextIndex = graph[index][i]; long nextDistance = distance + 1; if (distances[nextIndex, nextDistance % 3] <= nextDistance) { continue; } pQueue.Enqueue(nextDistance, nextIndex); } } return distances; } class PriorityQueue<T> { private readonly List<KeyValuePair<long, T>> list; private int count; public PriorityQueue() { list = new List<KeyValuePair<long, T>>(); count = 0; } private void Add(KeyValuePair<long, T> pair) { if (count == list.Count) { list.Add(pair); } else { list[count] = pair; } count++; } private void Swap(int a, int b) { KeyValuePair<long, T> tmp = list[a]; list[a] = list[b]; list[b] = tmp; } public void Enqueue(long key, T value) { Add(new KeyValuePair<long, T>(key, value)); int c = count - 1; while (c > 0) { int p = (c - 1) / 2; if (list[c].Key >= list[p].Key) break; Swap(p, c); c = p; } } public KeyValuePair<long, T> Dequeue() { KeyValuePair<long, T> pair = list[0]; count--; if (count == 0) return pair; list[0] = list[count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= count) break; int c = (c2 >= count || list[c1].Key < list[c2].Key) ? c1 : c2; if (list[c].Key >= list[p].Key) break; Swap(p, c); p = c; } return pair; } public bool Exist() { return count > 0; } } 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.ABC_132 { class E { static void Main(string[] args) { Method(args); } static void Method(string[] args) { int[] nm = ReadInts(); int n = nm[0]; int m = nm[1]; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) { graph[i] = new List<int>(); } for (int i = 0; i < m; i++) { int[] uv = ReadInts(); int u = uv[0] - 1; int v = uv[1] - 1; graph[u].Add(v); } int[] st = ReadInts(); int s = st[0] - 1; int t = st[1] - 1; long[,] dists = Dijkstra(s, graph); if (dists[t, 0] == long.MaxValue) WriteLine(-1); else WriteLine(dists[t, 0] / 3); } static long[,] Dijkstra(int startIndex, List<int>[] graph) { var pQueue = new PriorityQueue<int>(); var distances = new long[graph.Length, 3]; // Initialize nodes for (int i = 0; i < graph.Length; i++) { for (int j = 0; j < 3; j++) { distances[i, j] = long.MaxValue; } } pQueue.Enqueue(0, startIndex); while (pQueue.Exist()) { KeyValuePair<long, int> pair = pQueue.Dequeue(); long distance = pair.Key; int index = pair.Value; if (distances[index, distance % 3] <= distance) continue; distances[index, distance % 3] = distance; // Update priority queue for (int i = 0; i < graph[index].Count; i++) { int nextIndex = graph[index][i]; long nextDistance = distance + 1; if (distances[nextIndex, nextDistance % 3] <= nextDistance) { continue; } pQueue.Enqueue(nextDistance, nextIndex); } } return distances; } class PriorityQueue<T> { private readonly List<KeyValuePair<long, T>> list; private int count; public PriorityQueue() { list = new List<KeyValuePair<long, T>>(); count = 0; } private void Add(KeyValuePair<long, T> pair) { if (count == list.Count) { list.Add(pair); } else { list[count] = pair; } count++; } private void Swap(int a, int b) { KeyValuePair<long, T> tmp = list[a]; list[a] = list[b]; list[b] = tmp; } public void Enqueue(long key, T value) { Add(new KeyValuePair<long, T>(key, value)); int c = count - 1; while (c > 0) { int p = (c - 1) / 2; if (list[c].Key >= list[p].Key) break; Swap(p, c); c = p; } } public KeyValuePair<long, T> Dequeue() { KeyValuePair<long, T> pair = list[0]; count--; if (count == 0) return pair; list[0] = list[count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= count) break; int c = (c2 >= count || list[c1].Key < list[c2].Key) ? c1 : c2; if (list[c].Key >= list[p].Key) break; Swap(p, c); p = c; } return pair; } public bool Exist() { return count > 0; } } 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, 213, 3, 4, 0, 28, 0, 16, 17, 85], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
1,054
2
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime.CompilerServices; using System.Text; using System.Diagnostics; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; using static Template; using Pi = Pair<int, int>; class Solver { public void Solve(Scanner sc) { int N, K; sc.Make(out N, out K); var l = new List<int>(); var cnt = new List<int>(); for (int i = 1; i * i < N; i++) cnt.Add(1); var lhalf = new List<int>(); for (int i = 1; i * i < N; i++) { if (N / i <= i) break; lhalf.Add(N / i - N / (i + 1)); } lhalf.Reverse(); cnt.AddRange(lhalf); var dp = new ModInt[cnt.Count + 1]; var newdp = new ModInt[cnt.Count + 1]; dp[0] = 1; for (int i = 0; i < K; i++) { for (int j = 0; j < cnt.Count; j++) dp[j + 1] += dp[j]; for (int j = 0; j < cnt.Count; j++) newdp[j] = dp[cnt.Count - j - 1] * cnt[j]; swap(ref dp, ref newdp); } ModInt res = 0; for (var i = 0; i < cnt.Count; i++) res += dp[i]; WriteLine(res); } } public struct ModInt { public const long MOD = (int)1e9 + 7; // public const long MOD = 998244353; public long Value { get; set; } public ModInt(long n = 0) { Value = n; } private static ModInt[] fac; //階乗 private static ModInt[] inv; //逆数 private static ModInt[] facinv; // 1/(i!) public override string ToString() => Value.ToString(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator +(ModInt l, ModInt r) { l.Value += r.Value; if (l.Value >= MOD) l.Value -= MOD; return l; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator -(ModInt l, ModInt r) { l.Value -= r.Value; if (l.Value < 0) l.Value += MOD; return l; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator *(ModInt l, ModInt r) => new ModInt(l.Value * r.Value % MOD); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator /(ModInt l, ModInt r) => l * Pow(r, MOD - 2); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator long(ModInt l) => l.Value; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ModInt(long n) { n %= MOD; if (n < 0) n += MOD; return new ModInt(n); } public static ModInt Pow(ModInt m, long n) { if (n == 0) return 1; if (n % 2 == 0) return Pow(m * m, n >> 1); else return Pow(m * m, n >> 1) * m; } public static void Build(int n) { fac = new ModInt[n + 1]; facinv = new ModInt[n + 1]; inv = new ModInt[n + 1]; inv[1] = 1; fac[0] = fac[1] = 1; facinv[0] = facinv[1] = 1; for (var i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i; inv[i] = MOD - inv[MOD % i] * (MOD / i); facinv[i] = facinv[i - 1] * inv[i]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Fac(ModInt n) => fac[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Inv(ModInt n) => inv[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt FacInv(ModInt n) => facinv[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Comb(ModInt n, ModInt r) { if (n < r) return 0; if (n == r) return 1; var calc = fac[n]; calc = calc * facinv[r]; calc = calc * facinv[n - r]; return calc; } } #region Template public static class Template { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver().Solve(new Scanner()); Console.Out.Flush(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == 1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == -1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; } public static T[] Shuffle<T>(this IList<T> A) { T[] rt = A.ToArray(); Random rnd = new Random(); for (int i = rt.Length - 1; i >= 1; i--) swap(ref rt[i], ref rt[rnd.Next(i + 1)]); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; } public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); } } 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 (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, // v2); } public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, // out T3 v3); return (v1, v2, v3); } public (T1, T2, T3, T4) Make<T1, T2, T3, // T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, // v3, v4); } } public class Pair<T1, T2> : IComparable<Pair<T1, T2>> { public T1 v1; public T2 v2; public Pair() {} public Pair(T1 v1, T2 v2) { this.v1 = v1; this.v2 = v2; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2> p) { var c = Comparer<T1>.Default.Compare(v1, p.v1); if (c == 0) c = Comparer<T2>.Default.Compare(v2, p.v2); return c; } public override string ToString() => $"{v1.ToString()} {v2.ToString()}"; public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; } } public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>> { public T3 v3; public Pair() : base() {} public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2) { this.v3 = v3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2, T3> p) { var c = base.CompareTo(p); if (c == 0) c = Comparer<T3>.Default.Compare(v3, p.v3); return c; } public override string ToString() => $"{base.ToString()} {v3.ToString()}"; public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; } } #endregion
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime.CompilerServices; using System.Text; using System.Diagnostics; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; using static Template; using Pi = Pair<int, int>; class Solver { public void Solve(Scanner sc) { int N, K; sc.Make(out N, out K); var l = new List<int>(); var cnt = new List<int>(); for (int i = 1; i * i <= N; i++) cnt.Add(1); var lhalf = new List<int>(); for (int i = 1; i * i < N; i++) { if (N / i <= i) break; lhalf.Add(N / i - N / (i + 1)); } lhalf.Reverse(); cnt.AddRange(lhalf); var dp = new ModInt[cnt.Count + 1]; var newdp = new ModInt[cnt.Count + 1]; dp[0] = 1; for (int i = 0; i < K; i++) { for (int j = 0; j < cnt.Count; j++) dp[j + 1] += dp[j]; for (int j = 0; j < cnt.Count; j++) newdp[j] = dp[cnt.Count - j - 1] * cnt[j]; swap(ref dp, ref newdp); } ModInt res = 0; for (var i = 0; i < cnt.Count; i++) res += dp[i]; WriteLine(res); } } public struct ModInt { public const long MOD = (int)1e9 + 7; // public const long MOD = 998244353; public long Value { get; set; } public ModInt(long n = 0) { Value = n; } private static ModInt[] fac; //階乗 private static ModInt[] inv; //逆数 private static ModInt[] facinv; // 1/(i!) public override string ToString() => Value.ToString(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator +(ModInt l, ModInt r) { l.Value += r.Value; if (l.Value >= MOD) l.Value -= MOD; return l; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator -(ModInt l, ModInt r) { l.Value -= r.Value; if (l.Value < 0) l.Value += MOD; return l; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator *(ModInt l, ModInt r) => new ModInt(l.Value * r.Value % MOD); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator /(ModInt l, ModInt r) => l * Pow(r, MOD - 2); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator long(ModInt l) => l.Value; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ModInt(long n) { n %= MOD; if (n < 0) n += MOD; return new ModInt(n); } public static ModInt Pow(ModInt m, long n) { if (n == 0) return 1; if (n % 2 == 0) return Pow(m * m, n >> 1); else return Pow(m * m, n >> 1) * m; } public static void Build(int n) { fac = new ModInt[n + 1]; facinv = new ModInt[n + 1]; inv = new ModInt[n + 1]; inv[1] = 1; fac[0] = fac[1] = 1; facinv[0] = facinv[1] = 1; for (var i = 2; i <= n; i++) { fac[i] = fac[i - 1] * i; inv[i] = MOD - inv[MOD % i] * (MOD / i); facinv[i] = facinv[i - 1] * inv[i]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Fac(ModInt n) => fac[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Inv(ModInt n) => inv[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt FacInv(ModInt n) => facinv[n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Comb(ModInt n, ModInt r) { if (n < r) return 0; if (n == r) return 1; var calc = fac[n]; calc = calc * facinv[r]; calc = calc * facinv[n - r]; return calc; } } #region Template public static class Template { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver().Solve(new Scanner()); Console.Out.Flush(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == 1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == -1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; } public static T[] Shuffle<T>(this IList<T> A) { T[] rt = A.ToArray(); Random rnd = new Random(); for (int i = rt.Length - 1; i >= 1; i--) swap(ref rt[i], ref rt[rnd.Next(i + 1)]); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; } public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); } } 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 (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, // v2); } public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, // out T3 v3); return (v1, v2, v3); } public (T1, T2, T3, T4) Make<T1, T2, T3, // T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, // v3, v4); } } public class Pair<T1, T2> : IComparable<Pair<T1, T2>> { public T1 v1; public T2 v2; public Pair() {} public Pair(T1 v1, T2 v2) { this.v1 = v1; this.v2 = v2; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2> p) { var c = Comparer<T1>.Default.Compare(v1, p.v1); if (c == 0) c = Comparer<T2>.Default.Compare(v2, p.v2); return c; } public override string ToString() => $"{v1.ToString()} {v2.ToString()}"; public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; } } public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>> { public T3 v3; public Pair() : base() {} public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2) { this.v3 = v3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2, T3> p) { var c = base.CompareTo(p); if (c == 0) c = Comparer<T3>.Default.Compare(v3, p.v3); return c; } public override string ToString() => $"{base.ToString()} {v3.ToString()}"; public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; } } #endregion
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
2,471
2
using System; using System.Text; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; class Solve { const int mod = 1000000007; static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split().Select(int.Parse).ToArray(); var B = Console.ReadLine().Split().Select(int.Parse).ToArray(); int ans = 0; for (int i = 0; i < N; i++) { if (A[i] >= B[i]) { ans += B[i]; } else { if (B[i] - A[i] <= A[i + 1]) { ans += B[i]; A[i + 1] -= B[i] - A[i]; } else { ans += A[i] + A[i + 1]; A[i + 1] = 0; } } } Console.WriteLine(ans); } }
using System; using System.Text; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; class Solve { const int mod = 1000000007; static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split().Select(int.Parse).ToArray(); var B = Console.ReadLine().Split().Select(int.Parse).ToArray(); long ans = 0; for (int i = 0; i < N; i++) { if (A[i] >= B[i]) { ans += B[i]; } else { if (B[i] - A[i] <= A[i + 1]) { ans += B[i]; A[i + 1] -= B[i] - A[i]; } else { ans += A[i] + A[i + 1]; A[i + 1] = 0; } } } Console.WriteLine(ans); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
238
2
using System; using System.Linq; using System.Collections.Generic; using static System.Console; using System.Text; using System.IO; namespace AtCoder_Beginner_Contest_135 { class Program { static public int[] Sarray() { return ReadLine().Split().Select(int.Parse).ToArray(); } static public List<int> Slist() { return ReadLine().Split().Select(int.Parse).ToList(); } static void Main(string[] args) { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); Solve(); Out.Flush(); } static void Solve() { var N = Sarray()[0]; var Ai = Sarray(); var Bi = Sarray(); var cnt = 0; for (var i = N - 1; 0 <= i; --i) { var Bid = Math.Max(0, Bi[i] - Ai[i + 1]); // Ai+1を退治して残り戦力 cnt += Bi[i] - Bid; // Ai+1での討伐数 var Aid = Math.Max(0, Ai[i] - Bid); // Aiを退治 cnt += Ai[i] - Aid; // Aiでの討伐数 Ai[i] = Aid; // Ai残り } WriteLine(cnt); } } }
using System; using System.Linq; using System.Collections.Generic; using static System.Console; using System.Text; using System.IO; namespace AtCoder_Beginner_Contest_135 { class Program { static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); } static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); } static void Main(string[] args) { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); Solve(); Out.Flush(); } static void Solve() { var N = Sarray()[0]; var Ai = Sarray(); var Bi = Sarray(); var cnt = 0L; for (var i = N - 1; 0 <= i; --i) { var Bid = Math.Max(0, Bi[i] - Ai[i + 1]); // Ai+1を退治して残り戦力 cnt += Bi[i] - Bid; // Ai+1での討伐数 var Aid = Math.Max(0, Ai[i] - Bid); // Aiを退治 cnt += Ai[i] - Aid; // Aiでの討伐数 Ai[i] = Aid; // Ai残り } WriteLine(cnt); } } }
[["-", 0, 235, 8, 201, 0, 195, 39, 224, 39, 199], ["+", 0, 235, 8, 201, 0, 195, 39, 224, 39, 199], ["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 8, 201, 0, 195, 39, 236, 237, 238, 0, 199], ["+", 8, 201, 0, 195, 39, 236, 237, 238, 0, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
265
10
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { static class CCitySavers { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var aList = Console.ReadLine().Split(' ').Select(y => int.Parse(y)).ToList(); var bList = Console.ReadLine().Split(' ').Select(y => int.Parse(y)).ToList(); int enemySum = aList.Sum(); for (int i = 0; i < n; i++) { int attack = bList[i]; if (aList[i] < attack) { attack -= aList[i]; aList[i] = 0; if (aList[i + 1] < attack) { aList[i + 1] = 0; } else { aList[i + 1] -= attack; } } else { aList[i] -= attack; } } Console.WriteLine(enemySum - aList.Sum()); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { static class CCitySavers { static void Main(string[] args) { var n = long.Parse(Console.ReadLine()); var aList = Console.ReadLine().Split(' ').Select(y => long.Parse(y)).ToList(); var bList = Console.ReadLine().Split(' ').Select(y => long.Parse(y)).ToList(); long enemySum = aList.Sum(); for (int i = 0; i < n; i++) { long attack = bList[i]; if (aList[i] < attack) { attack -= aList[i]; aList[i] = 0; if (aList[i + 1] < attack) { aList[i + 1] = 0; } else { aList[i + 1] -= attack; } } else { aList[i] -= attack; } } Console.WriteLine(enemySum - aList.Sum()); Console.ReadLine(); } } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 199], ["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199]]
8
246
10
using System; using System.Collections.Generic; using System.Linq; namespace Kanazawa { public static class Kaga { public static void Main() { var n = Cint(); var a = CintA(); var b = CintA(); var count = 0; Loop(n, i => { if (b[i] >= a[i]) { count += a[i]; b[i] -= a[i]; a[i] = 0; } else { count += b[i]; a[i] -= b[i]; b[i] = 0; } if (b[i] >= a[i + 1]) { count += a[i + 1]; b[i] -= a[i + 1]; a[i + 1] = 0; } else { count += b[i]; a[i + 1] -= b[i]; b[i] = 0; } }); Cout(count); } #region Input and Output private static readonly List<string> Buffer = new List<string>(); private static string Cin() { ReadBuffer(); var str = Buffer.First(); Buffer.RemoveAt(0); return str; } private static IEnumerable<string> CinA() => Console.ReadLine()?.Split(); private static int Cint() => int.Parse(Cin()); private static int[] CintA() => CinA().Select(int.Parse).ToArray(); private static void Cout(object obj) => Console.WriteLine(obj); private static void ReadBuffer() { if (Buffer.Any()) return; var line = Console.ReadLine(); if (line != null && line.Contains(' ')) { Buffer.AddRange(line.Split()); } else { Buffer.Add(line); } } #endregion #region Loops private static void Loop(int count, Action<int> action) => Loop(0, count, action); private static void Loop(int init, int count, Action<int> action, int step = 1) { for (var i = init; i < count + init; i += step) { action(i); } } #endregion } }
using System; using System.Collections.Generic; using System.Linq; namespace Kanazawa { public static class Kaga { public static void Main() { var n = Cint(); var a = CintA(); var b = CintA(); var count = 0L; Loop(n, i => { if (b[i] >= a[i]) { count += a[i]; b[i] -= a[i]; a[i] = 0; } else { count += b[i]; a[i] -= b[i]; b[i] = 0; } if (b[i] >= a[i + 1]) { count += a[i + 1]; b[i] -= a[i + 1]; a[i + 1] = 0; } else { count += b[i]; a[i + 1] -= b[i]; b[i] = 0; } }); Cout(count); } #region Input and Output private static readonly List<string> Buffer = new List<string>(); private static string Cin() { ReadBuffer(); var str = Buffer.First(); Buffer.RemoveAt(0); return str; } private static IEnumerable<string> CinA() => Console.ReadLine()?.Split(); private static int Cint() => int.Parse(Cin()); private static int[] CintA() => CinA().Select(int.Parse).ToArray(); private static void Cout(object obj) => Console.WriteLine(obj); private static void ReadBuffer() { if (Buffer.Any()) return; var line = Console.ReadLine(); if (line != null && line.Contains(' ')) { Buffer.AddRange(line.Split()); } else { Buffer.Add(line); } } #endregion #region Loops private static void Loop(int count, Action<int> action) => Loop(0, count, action); private static void Loop(int init, int count, Action<int> action, int step = 1) { for (var i = init; i < count + init; i += step) { action(i); } } #endregion } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
486
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // using ProCon.Util; namespace ProCon { class Program { static void Main(string[] args) { AC135B(); } static void AC135B() { // https://atcoder.jp/contests/abc135/tasks var n = int.Parse(Console.ReadLine()); //勇者の数 var listA = Console.ReadLine() .Split() .Select(int.Parse) .ToArray(); //街にいるモンスターの数 var listB = Console.ReadLine().Split().Select(int.Parse).ToArray(); //勇者が倒せる数 var sum = listA.Sum(); for (int i = 0; i < n; i++) { //まずはiの街を倒す if (listB[i] - listA[i] >= 0) { listB[i] = listB[i] - listA[i]; listA[i] = 0; } else { listA[i] = listA[i] - listB[i]; listB[i] = 0; } //まだ余力があればi +1の街を倒す if (listB[i] > 0) { if (listB[i] - listA[i + 1] >= 0) { listB[i] = listB[i] - listA[i + 1]; listA[i + 1] = 0; } else { listA[i + 1] = listA[i + 1] - listB[i]; listB[i] = 0; } } } Console.WriteLine(sum - listA.Sum()); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // using ProCon.Util; namespace ProCon { class Program { static void Main(string[] args) { AC135B(); } static void AC135B() { // https://atcoder.jp/contests/abc135/tasks var n = int.Parse(Console.ReadLine()); //勇者の数 var listA = Console.ReadLine() .Split() .Select(long.Parse) .ToArray(); //街にいるモンスターの数 var listB = Console.ReadLine() .Split() .Select(long.Parse) .ToArray(); //勇者が倒せる数 var sum = listA.Sum(); for (int i = 0; i < n; i++) { //まずはiの街を倒す if (listB[i] - listA[i] >= 0) { listB[i] = listB[i] - listA[i]; listA[i] = 0; } else { listA[i] = listA[i] - listB[i]; listB[i] = 0; } //まだ余力があればi +1の街を倒す if (listB[i] > 0) { if (listB[i] - listA[i + 1] >= 0) { listB[i] = listB[i] - listA[i + 1]; listA[i + 1] = 0; } else { listA[i + 1] = listA[i + 1] - listB[i]; listB[i] = 0; } } } Console.WriteLine(sum - listA.Sum()); Console.ReadKey(); } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199]]
8
317
4
using System; using System.Linq; namespace abc135_c { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); var B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); var a1 = A.Sum(); if (N == 1) { int a = A[0] + A[1]; if (a <= B[0]) Console.WriteLine(a); else Console.WriteLine(B[0]); } else { for (int i = N; i > 0; i--) foreach (var j in new int[] { 0, 1 }) if (A[i - j] <= B[i - 1]) { B[i - 1] -= A[i - j]; A[i - j] = 0; } else { A[i - j] -= B[i - 1]; B[i - 1] = 0; } Console.WriteLine(a1 - A.Sum()); } } } }
using System; using System.Linq; namespace abc135_c { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); var a1 = A.Sum(); if (N == 1) { var a = A[0] + A[1]; if (a <= B[0]) Console.WriteLine(a); else Console.WriteLine(B[0]); } else { for (int i = N; i > 0; i--) foreach (var j in new int[] { 0, 1 }) if (A[i - j] <= B[i - 1]) { B[i - 1] -= A[i - j]; A[i - j] = 0; } else { A[i - j] -= B[i - 1]; B[i - 1] = 0; } Console.WriteLine(a1 - A.Sum()); } } } }
[["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 0, 57, 64, 196, 0, 197, 0, 198, 39, 199], ["+", 64, 196, 0, 197, 0, 198, 39, 216, 0, 217]]
8
274
6
using System; using System.Linq; using System.Collections.Generic; using static System.Math; namespace C { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToArray(); var b = Console.ReadLine().Split().Select(int.Parse).ToArray(); var monsterCount = 0; for (int i = 0; i < b.Count(); i++) { if (a[i] >= b[i]) { monsterCount += b[i]; } else { monsterCount += a[i]; b[i] -= a[i]; monsterCount += Min(a[i + 1], b[i]); a[i + 1] -= Min(a[i + 1], b[i]); } } Console.WriteLine(monsterCount); } } }
using System; using System.Linq; using System.Collections.Generic; using static System.Math; namespace C { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(long.Parse).ToArray(); var b = Console.ReadLine().Split().Select(long.Parse).ToArray(); long monsterCount = 0; for (int i = 0; i < b.Count(); i++) { if (a[i] >= b[i]) { monsterCount += b[i]; } else { monsterCount += a[i]; b[i] -= a[i]; monsterCount += Min(a[i + 1], b[i]); a[i + 1] -= Min(a[i + 1], b[i]); } } Console.WriteLine(monsterCount); } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
214
6
using System; using System.Linq; namespace CSharp { class Program { public static void Main() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var B = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var result = 0; for (int i = 0; i < N; i++) { if (B[i] > A[i]) { B[i] -= A[i]; result += A[i]; A[i] = 0; } else { A[i] -= B[i]; result += B[i]; B[i] = 0; } if (B[i] > A[i + 1]) { B[i] -= A[i + 1]; result += A[i + 1]; A[i + 1] = 0; } else { A[i + 1] -= B[i]; result += B[i]; B[i] = 0; } } Console.WriteLine(result); } } }
using System; using System.Linq; namespace CSharp { class Program { public static void Main() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var B = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var result = 0L; for (int i = 0; i < N; i++) { if (B[i] > A[i]) { B[i] -= A[i]; result += A[i]; A[i] = 0; } else { A[i] -= B[i]; result += B[i]; B[i] = 0; } if (B[i] > A[i + 1]) { B[i] -= A[i + 1]; result += A[i + 1]; A[i + 1] = 0; } else { A[i + 1] -= B[i]; result += B[i]; B[i] = 0; } } Console.WriteLine(result); } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
260
6
using System; using System.Linq; namespace ConsoleApp6 { class Program { static void Main(string[] args) { Console.ReadLine(); var A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int ans = 0; for (int i = 0; i < B.Count(); i++) { int d = Math.Min(A[i], B[i]); ans += d; A[i] -= d; B[i] -= d; d = Math.Min(A[i + 1], B[i]); ans += d; A[i + 1] -= d; B[i] -= d; } Console.WriteLine(ans); } } }
using System; using System.Linq; namespace ConsoleApp6 { class Program { static void Main(string[] args) { Console.ReadLine(); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long ans = 0; for (long i = 0; i < B.Count(); i++) { long d = Math.Min(A[i], B[i]); ans += d; A[i] -= d; B[i] -= d; d = Math.Min(A[i + 1], B[i]); ans += d; A[i + 1] -= d; B[i] -= d; } Console.WriteLine(ans); } } }
[["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 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], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199]]
8
204
10
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AtCoder { static class Program { static void Main() { //ここから int N = IO.ReadLineToInt(); var A = IO.ReadLineToIntList(); var B = IO.ReadLineToIntList(); var sum = 0; for (int i = 0; i < N; i++) { var num = Math.Min(A[i], B[i]); sum += num; B[i] -= num; num = Math.Min(A[i + 1], B[i]); sum += num; A[i + 1] -= num; } //ここまで Console.WriteLine(sum); Console.ReadKey(); } } static class Func { static long mod = 1000000007; static public void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; } static public long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; } static public long LCM(long a, long b) { return (long)a * b / GCD(a, b); } static public int Factorial(int a) { return a == 1 ? a : a * Factorial(a - 1); } public static double ToAngle(this double radian) { return (double)(radian * 180 / Math.PI); } public static double ToRadian(this double angle) { return (double)(angle * Math.PI / 180); } static public long Conbination(int n, int m) { if (m == 0) return 1; if (n == 0) return 0; return n * Conbination(n - 1, m - 1) / m; } static public void Factoring(long num, ref long[] vs) { if (num == 1) { vs[1]++; } for (long i = 2; i < Math.Sqrt(num); i++) { if (num % i == 0) { vs[i]++; Factoring(num / i, ref vs); } } vs[1]++; } } static class IO { static public string ReadLineToString() { return Console.ReadLine(); } static public int ReadLineToInt() { return int.Parse(Console.ReadLine()); } static public long ReadLineToLong() { return long.Parse(Console.ReadLine()); } static public List<string> ReadLineToStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> ReadLineToIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> ReadLineToLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> ReadLineToDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings(object[] strings) { foreach (var o in strings) { Console.Write(o + " "); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AtCoder { static class Program { static void Main() { //ここから int N = IO.ReadLineToInt(); var A = IO.ReadLineToIntList(); var B = IO.ReadLineToIntList(); long sum = 0; for (int i = 0; i < N; i++) { var num = Math.Min(A[i], B[i]); sum += num; B[i] -= num; num = Math.Min(A[i + 1], B[i]); sum += num; A[i + 1] -= num; } //ここまで Console.WriteLine(sum); Console.ReadKey(); } } static class Func { static long mod = 1000000007; static public void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; } static public long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; } static public long LCM(long a, long b) { return (long)a * b / GCD(a, b); } static public int Factorial(int a) { return a == 1 ? a : a * Factorial(a - 1); } public static double ToAngle(this double radian) { return (double)(radian * 180 / Math.PI); } public static double ToRadian(this double angle) { return (double)(angle * Math.PI / 180); } static public long Conbination(int n, int m) { if (m == 0) return 1; if (n == 0) return 0; return n * Conbination(n - 1, m - 1) / m; } static public void Factoring(long num, ref long[] vs) { if (num == 1) { vs[1]++; } for (long i = 2; i < Math.Sqrt(num); i++) { if (num % i == 0) { vs[i]++; Factoring(num / i, ref vs); } } vs[1]++; } } static class IO { static public string ReadLineToString() { return Console.ReadLine(); } static public int ReadLineToInt() { return int.Parse(Console.ReadLine()); } static public long ReadLineToLong() { return long.Parse(Console.ReadLine()); } static public List<string> ReadLineToStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> ReadLineToIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> ReadLineToLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> ReadLineToDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings(object[] strings) { foreach (var o in strings) { Console.Write(o + " "); } } } }
[["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
706
2
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static long[] a; static long N; static long K; static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); var destroyed = 0; var amari = 0; for (int i = 0; i < N; i++) { amari = 0; //最小値を求める var x = Math.Min(A[i], B[i]); A[i] -= x; B[i] -= x; destroyed += x; var y = Math.Min(A[i + 1], B[i]); A[i + 1] -= y; B[i] -= y; destroyed += y; } // Console.WriteLine(String.Join(" ",A)); Console.WriteLine(destroyed); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static long[] a; static long N; static long K; static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); var destroyed = 0L; var amari = 0; for (int i = 0; i < N; i++) { amari = 0; //最小値を求める var x = Math.Min(A[i], B[i]); A[i] -= x; B[i] -= x; destroyed += x; var y = Math.Min(A[i + 1], B[i]); A[i + 1] -= y; B[i] -= y; destroyed += y; } // Console.WriteLine(String.Join(" ",A)); Console.WriteLine(destroyed); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
256
2
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput cin) { var N = cin.ReadInt; var A = cin.ReadIntArray(N + 1); var B = cin.ReadIntArray(N); var count = new int[N]; for (int i = 0; i < N; i++) count[i] = 0; for (int i = N - 1; i >= 0; i--) { var c = Min(B[i], A[i + 1]); count[i] += c; A[i + 1] -= c; B[i] -= c; var c2 = Min(B[i], A[i]); count[i] += c2; A[i] -= c2; } Console.WriteLine(count.Sum()); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput cin) { var N = cin.ReadInt; var A = cin.ReadLongArray(N + 1); var B = cin.ReadLongArray(N); var count = new long[N]; for (int i = 0; i < N; i++) count[i] = 0; for (int i = N - 1; i >= 0; i--) { var c = Min(B[i], A[i + 1]); count[i] += c; A[i + 1] -= c; B[i] -= c; var c2 = Min(B[i], A[i]); count[i] += c2; A[i] -= c2; } Console.WriteLine(count.Sum()); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
583
6
using System; class Program { static void Main() { int N = int.Parse(Console.ReadLine()); string[] A_ = Console.ReadLine().Split(' '); string[] B = Console.ReadLine().Split(' '); // A の方は int にしておく int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(A_[i]); } // int count = 0; for (int i = 0; i < N; i++) { int b = int.Parse(B[i]); if (b <= A[i]) { count += b; } else if (b <= A[i] + A[i + 1]) { count += b; A[i + 1] = A[i + 1] - (b - A[i]); } else { count += A[i] + A[i + 1]; A[i + 1] = 0; } } Console.WriteLine(count); } }
using System; class Program { static void Main() { int N = int.Parse(Console.ReadLine()); string[] A_ = Console.ReadLine().Split(' '); string[] B = Console.ReadLine().Split(' '); // A の方は int にしておく long[] A = new long[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = long.Parse(A_[i]); } // long count = 0; for (int i = 0; i < N; i++) { long b = long.Parse(B[i]); if (b <= A[i]) { count += b; } else if (b <= A[i] + A[i + 1]) { count += b; A[i + 1] = A[i + 1] - (b - A[i]); } else { count += A[i] + A[i + 1]; A[i + 1] = 0; } } Console.WriteLine(count); } }
[["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 1, 0, 11, 12, 213, 63, 214, 205, 199], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 205, 199], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 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
240
12
using System; namespace pro { class MainClass { public static void Main() { int n = int.Parse(Console.ReadLine()); string[] str0 = Console.ReadLine().Split(' '); string[] str1 = Console.ReadLine().Split(' '); int[] a = new int[n + 1]; int[] b = new int[n]; for (int i = 0; i < n + 1; i++) { a[i] = int.Parse(str0[i]); } for (int i = 0; i < n; i++) { b[i] = int.Parse(str1[i]); } int count = 0; for (int i = 0; i < n; i++) { if (b[i] > a[i]) { b[i] -= a[i]; count += a[i]; if (b[i] > a[i + 1]) { count += a[i + 1]; a[i + 1] = 0; } else { a[i + 1] -= b[i]; count += b[i]; } } else { count += b[i]; } } Console.WriteLine(count); } } }
using System; namespace pro { class MainClass { public static void Main() { int n = int.Parse(Console.ReadLine()); string[] str0 = Console.ReadLine().Split(' '); string[] str1 = Console.ReadLine().Split(' '); int[] a = new int[n + 1]; int[] b = new int[n]; for (int i = 0; i < n + 1; i++) { a[i] = int.Parse(str0[i]); } for (int i = 0; i < n; i++) { b[i] = int.Parse(str1[i]); } long count = 0; for (int i = 0; i < n; i++) { if (b[i] > a[i]) { b[i] -= a[i]; count += a[i]; if (b[i] > a[i + 1]) { count += a[i + 1]; a[i + 1] = 0; } else { a[i + 1] -= b[i]; count += b[i]; } } else { count += b[i]; } } Console.WriteLine(count); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
282
2
using System; using System.Linq; namespace ABC135 { class C { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(input[i]); } input = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(input[i]); } int beforeSum = 0; int afterSum = 0; for (int i = 0; i < N; i++) { beforeSum += B[i]; } int tmp = 0; for (int i = 0; i < N; i++) { tmp = B[i] - A[i]; if (tmp < 0) { B[i] = 0; A[i] = Math.Abs(tmp); } if (tmp == 0) { B[i] = 0; A[i] = 0; } if (tmp > 0) { B[i] = Math.Abs(tmp); A[i] = 0; } tmp = B[i] - A[i + 1]; if (tmp < 0) { B[i] = 0; A[i + 1] = Math.Abs(tmp); } if (tmp == 0) { B[i] = 0; A[i + 1] = 0; } if (tmp > 0) { B[i] = Math.Abs(tmp); A[i + 1] = 0; } } for (int i = 0; i < N; i++) { afterSum += B[i]; } Console.WriteLine(beforeSum - afterSum); } } }
using System; using System.Linq; namespace ABC135 { class C { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(input[i]); } input = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(input[i]); } long beforeSum = 0; long afterSum = 0; for (int i = 0; i < N; i++) { beforeSum += B[i]; } int tmp = 0; for (int i = 0; i < N; i++) { tmp = B[i] - A[i]; if (tmp < 0) { B[i] = 0; A[i] = Math.Abs(tmp); } if (tmp == 0) { B[i] = 0; A[i] = 0; } if (tmp > 0) { B[i] = Math.Abs(tmp); A[i] = 0; } tmp = B[i] - A[i + 1]; if (tmp < 0) { B[i] = 0; A[i + 1] = Math.Abs(tmp); } if (tmp == 0) { B[i] = 0; A[i + 1] = 0; } if (tmp > 0) { B[i] = Math.Abs(tmp); A[i + 1] = 0; } } for (int i = 0; i < N; i++) { afterSum += B[i]; } Console.WriteLine(beforeSum - afterSum); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
432
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoderPre { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var B = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); long count = 0; for (int i = 0; i <= N; i++) { if (A[i] >= B[i]) { count += B[i]; A[i] -= B[i]; // Console.WriteLine("大きい"); // Console.WriteLine(count); } else { count += A[i]; if (B[i] - A[i] >= A[i + 1]) { count += A[i + 1]; A[i + 1] = 0; // Console.WriteLine("小さい大きい"); // Console.WriteLine(A[i + 1]); // Console.WriteLine(count); } else { count += B[i] - A[i]; A[i + 1] -= (B[i] - A[i]); // Console.WriteLine("小さい小さい"); // Console.WriteLine(A[i+1]); // Console.WriteLine(count); } } } Console.WriteLine(count); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoderPre { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var B = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); long count = 0; for (int i = 0; i < N; i++) { if (A[i] >= B[i]) { count += B[i]; A[i] -= B[i]; // Console.WriteLine("大きい"); // Console.WriteLine(count); } else { count += A[i]; if (B[i] - A[i] >= A[i + 1]) { count += A[i + 1]; A[i + 1] = 0; // Console.WriteLine("小さい大きい"); // Console.WriteLine(count); } else { count += B[i] - A[i]; A[i + 1] -= (B[i] - A[i]); // Console.WriteLine("小さい小さい"); // Console.WriteLine(count); } } } Console.WriteLine(count); } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18]]
8
260
2
using System; using System.Linq; using System.Collections.Generic; class Program { public static void Main() { var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long sum = 0; for (long i = 0; i < N; i++) { if (A[i] < B[i]) { long a = A[i + 1]; A[i + 1] -= (B[i] - A[i]); if (A[i + 1] < 0) { sum += A[i] + a; A[i + 1] = 0; } else if (A[i + 1] >= 0) { sum += B[i]; } } else { sum = +B[i]; } } Console.WriteLine(sum); } }
using System; using System.Linq; using System.Collections.Generic; class Program { public static void Main() { var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long sum = 0; for (long i = 0; i < N; i++) { if (A[i] < B[i]) { long a = A[i + 1]; A[i + 1] -= (B[i] - A[i]); if (A[i + 1] < 0) { sum += A[i] + a; A[i + 1] = 0; } else if (A[i + 1] >= 0) { sum += B[i]; } } else { sum += B[i]; } } Console.WriteLine(sum); } }
[["-", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 75, 196, 0, 1, 0, 11, 12, 241, 0, 72], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 107]]
8
239
3
using System; class Program { public static void Main() { // スペース区切りの整数の入力 int n = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), i => int.Parse(i)); int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), i => int.Parse(i)); long sum = 0; for (long i = 0; i < n; i++) { if (a[i] >= b[i]) { sum = +a[i]; } else { sum += a[i]; b[i] -= a[i]; if (a[i + 1] >= b[i]) { sum += b[i]; a[i + 1] -= b[i]; } else { sum += a[i + 1]; a[i + 1] = 0; } } } Console.WriteLine(sum); } }
using System; class Program { public static void Main() { // スペース区切りの整数の入力 int n = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), i => int.Parse(i)); int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), i => int.Parse(i)); long sum = 0; for (long i = 0; i < n; i++) { if (a[i] >= b[i]) { sum += b[i]; } else { sum += a[i]; b[i] -= a[i]; if (a[i + 1] >= b[i]) { sum += b[i]; a[i + 1] -= b[i]; } else { sum += a[i + 1]; a[i + 1] = 0; } } } Console.WriteLine(sum); } }
[["-", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 64, 196, 0, 1, 0, 11, 12, 241, 0, 72], ["-", 0, 1, 0, 11, 12, 241, 0, 204, 205, 22], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 107], ["+", 64, 196, 0, 1, 0, 11, 12, 204, 205, 22]]
8
220
5
using System; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] arr = Console.ReadLine() .Trim() .Split(' ') .Select(tmp => int.Parse(tmp)) .ToArray<int>(); int[] hero = Console.ReadLine() .Trim() .Split(' ') .Select(tmp => int.Parse(tmp)) .ToArray<int>(); int num = 0; for (int i = 0; i < n; i++) { if (hero[i] > arr[i]) { int diff = hero[i] - arr[i]; // heroが大きすぎる if (diff > arr[i + 1]) { num += arr[i] + arr[i + 1]; arr[i + 1] = 0; } else { num += hero[i]; arr[i + 1] -= diff; } } else { num += hero[i]; } } Console.WriteLine(num.ToString()); } } }
using System; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] arr = Console.ReadLine() .Trim() .Split(' ') .Select(tmp => int.Parse(tmp)) .ToArray<int>(); int[] hero = Console.ReadLine() .Trim() .Split(' ') .Select(tmp => int.Parse(tmp)) .ToArray<int>(); long num = 0; for (int i = 0; i < n; i++) { if (hero[i] > arr[i]) { int diff = hero[i] - arr[i]; // heroが大きすぎる if (diff > arr[i + 1]) { num += arr[i] + arr[i + 1]; arr[i + 1] = 0; } else { num += hero[i]; arr[i + 1] -= diff; } } else { num += hero[i]; } } Console.WriteLine(num.ToString()); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
248
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp24 { class Program { static void Main(string[] args) { //入力 int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); //回答となる数値 long ans = 0; //検証 for (int i = 0; i < N; i++) { if (A[i] > B[i]) { //自分の持ち場のモンスターを倒すので精一杯なので、答えにB[i]を追加するだけでその他の操作は何もしなくてよい ans += B[i]; } else if (A[i] <= B[i]) { if (A[i] + A[i + 1] > B[i]) { ans += B[i]; A[i + 1] -= B[i] - A[i]; } else if (B[i] > A[i] + A[i + 1]) { ans += A[i] + A[i + 1]; A[i + 1] = 0; } } } //出力 Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp24 { class Program { static void Main(string[] args) { //入力 int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); //回答となる数値 long ans = 0; //検証 for (int i = 0; i < N; i++) { if (A[i] >= B[i]) { //自分の持ち場のモンスターを倒すので精一杯なので、答えにB[i]を追加するだけでその他の操作は何もしなくてよい ans += B[i]; } else if (A[i] < B[i]) { if (A[i] + A[i + 1] > B[i]) { ans += B[i]; A[i + 1] -= B[i] - A[i]; } else if (B[i] >= A[i] + A[i + 1]) { ans += A[i] + A[i + 1]; A[i + 1] = 0; } } } //出力 Console.WriteLine(ans); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20], ["-", 8, 196, 0, 57, 75, 57, 15, 16, 17, 19], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 17, 18], ["-", 64, 196, 0, 57, 75, 57, 15, 16, 17, 47], ["+", 64, 196, 0, 57, 75, 57, 15, 16, 17, 20]]
8
287
6
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) {   int N; N = int.Parse(Console.ReadLine()); //標準入力 int[] A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int count = 0; for (int i = 0; i < N; i++) { int diffFirst = B[i] - A[i]; if (diffFirst > 0) { int diffSecond = A[i + 1] - diffFirst; if (diffSecond > 0) { count += B[i]; A[i + 1] = diffSecond; } else { count += A[i] + A[i + 1]; A[i + 1] = 0; } } else { count += B[i]; } } Console.Write(count); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) {   int N; N = int.Parse(Console.ReadLine()); //標準入力 int[] A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int[] B = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); long count = 0; for (int i = 0; i < N; i++) { int diffFirst = B[i] - A[i]; if (diffFirst > 0) { int diffSecond = A[i + 1] - diffFirst; if (diffSecond > 0) { count += B[i]; A[i + 1] = diffSecond; } else { count += A[i] + A[i + 1]; A[i + 1] = 0; } } else { count += B[i]; } } Console.Write(count); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
227
2
#if true //#if false #define UTAKA_DEBUG #endif using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using System.IO; using static System.Math; namespace UtakaApp { public class Program { public const string ContestName = "abc135"; public const string ProblemName = "C"; public static void Main(string[] args) { #if UTAKA_LOCAL new TestCaseChecker().TestProblems(); #else new Program().Solve(Console.In, new ConsoleWriter()); #endif } public void Solve(TextReader textReader, IConsoleWriter consoleWriter) { var cin = new ConsoleInput(textReader, ' '); var n = cin.ReadInt; var a = cin.ReadIntArray(n + 1); var b = cin.ReadIntArray(n); var total = 0; var killedByFormer = Min(b[0], a[0]); total += killedByFormer; var remainPowerOfFormer = b[0] - killedByFormer; Dbg.WriteLine($"{0} {0} {killedByFormer}"); for (int i = 1; i < n; i++) { killedByFormer = Min(remainPowerOfFormer, a[i]); total += killedByFormer; var rest = a[i] - killedByFormer; var killedByLatter = Min(rest, b[i]); total += killedByLatter; remainPowerOfFormer = b[i] - killedByLatter; Dbg.WriteLine($"{i} {killedByFormer} {killedByLatter}"); } var lastKill = Min(remainPowerOfFormer, a[n]); Dbg.WriteLine($"{n} {lastKill} -"); total += lastKill; consoleWriter.WriteLine(total.ToString()); } } public class ConsoleInput { private readonly TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) { inputStream.Enqueue(tmp[i]); } return inputStream.Dequeue(); } } public string ReadLine => _stream.ReadLine(); public int ReadInt => int.Parse(Read); public long ReadLong => long.Parse(Read); public double ReadDouble => double.Parse(Read); public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } public interface IConsoleWriter { void Write(string str); void WriteLine(string str); } public class ConsoleWriter : IConsoleWriter { public void Write(string str) { Console.Write(str); } public void WriteLine(string str) { Console.WriteLine(str); } } #if UTAKA_LOCAL public class DebugConsoleWriter : IConsoleWriter { private StringBuilder mSb; public DebugConsoleWriter() { mSb = new StringBuilder(); } public void Write(string str) { mSb.Append(str); } public void WriteLine(string str) { mSb.AppendLine(str); } public string GetAllOutput() { return mSb.ToString(); } } public class TestCaseChecker { private string DirectoryPath => $"../../../../{Program.ContestName}/{Program.ProblemName}"; private string GetInputFilePath(int testCaseNumber) { return $"{DirectoryPath}/in_{testCaseNumber}.txt"; } private string GetOutputFilePath(int testCaseNumber) { return $"{DirectoryPath}/out_{testCaseNumber}.txt"; } public bool TestProblem(int testCaseNumber) { var inputFilePath = GetInputFilePath(testCaseNumber); var outputFilePath = GetOutputFilePath(testCaseNumber); TextReader inputStream = new StreamReader(inputFilePath); var debugConsoleWriter = new DebugConsoleWriter(); new Program().Solve(inputStream, debugConsoleWriter); var output = debugConsoleWriter.GetAllOutput(); TextReader outputStream = new StreamReader(outputFilePath); var outputAnswer = outputStream.ReadToEnd(); #if UTAKA_DEBUG Console.Write(output); Console.Write(outputAnswer); #endif var isCorrect = output == outputAnswer; return isCorrect; } public void TestProblems() { var isSuccessAll = true; var testCaseNumber = 1; while (true) { var inputFileName = GetInputFilePath(testCaseNumber); if (!File.Exists(inputFileName)) { break; } Console.WriteLine( $"TestCase {testCaseNumber} ====================================================="); var result = TestProblem(testCaseNumber); if (result) { Console.WriteLine($"Success"); } else { isSuccessAll = false; Console.WriteLine($"Failure *****"); } testCaseNumber++; } if (isSuccessAll) { Console.WriteLine($"!!!!!!!!! All Success !!!!!!!!!"); } } } public static class Dbg { public static void WriteLine(string str) { Console.WriteLine(str); } public static void Write(string str) { Console.Write(str); } } #else public static class Dbg { public static void WriteLine(string str) {} public static void Write(string str) {} } #endif }
#if true //#if false #define UTAKA_DEBUG #endif using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using System.IO; using static System.Math; namespace UtakaApp { public class Program { public const string ContestName = "abc135"; public const string ProblemName = "C"; public static void Main(string[] args) { #if UTAKA_LOCAL new TestCaseChecker().TestProblems(); #else new Program().Solve(Console.In, new ConsoleWriter()); #endif } public void Solve(TextReader textReader, IConsoleWriter consoleWriter) { var cin = new ConsoleInput(textReader, ' '); var n = cin.ReadInt; var a = cin.ReadIntArray(n + 1); var b = cin.ReadIntArray(n); long total = 0; var killedByFormer = Min(b[0], a[0]); total += killedByFormer; var remainPowerOfFormer = b[0] - killedByFormer; Dbg.WriteLine($"{0} {0} {killedByFormer}"); for (int i = 1; i < n; i++) { killedByFormer = Min(remainPowerOfFormer, a[i]); total += killedByFormer; var rest = a[i] - killedByFormer; var killedByLatter = Min(rest, b[i]); total += killedByLatter; remainPowerOfFormer = b[i] - killedByLatter; Dbg.WriteLine($"{i} {killedByFormer} {killedByLatter}"); } var lastKill = Min(remainPowerOfFormer, a[n]); Dbg.WriteLine($"{n} {lastKill} -"); total += lastKill; consoleWriter.WriteLine(total.ToString()); } } public class ConsoleInput { private readonly TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) { inputStream.Enqueue(tmp[i]); } return inputStream.Dequeue(); } } public string ReadLine => _stream.ReadLine(); public int ReadInt => int.Parse(Read); public long ReadLong => long.Parse(Read); public double ReadDouble => double.Parse(Read); public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } public interface IConsoleWriter { void Write(string str); void WriteLine(string str); } public class ConsoleWriter : IConsoleWriter { public void Write(string str) { Console.Write(str); } public void WriteLine(string str) { Console.WriteLine(str); } } #if UTAKA_LOCAL public class DebugConsoleWriter : IConsoleWriter { private StringBuilder mSb; public DebugConsoleWriter() { mSb = new StringBuilder(); } public void Write(string str) { mSb.Append(str); } public void WriteLine(string str) { mSb.AppendLine(str); } public string GetAllOutput() { return mSb.ToString(); } } public class TestCaseChecker { private string DirectoryPath => $"../../../../{Program.ContestName}/{Program.ProblemName}"; private string GetInputFilePath(int testCaseNumber) { return $"{DirectoryPath}/in_{testCaseNumber}.txt"; } private string GetOutputFilePath(int testCaseNumber) { return $"{DirectoryPath}/out_{testCaseNumber}.txt"; } public bool TestProblem(int testCaseNumber) { var inputFilePath = GetInputFilePath(testCaseNumber); var outputFilePath = GetOutputFilePath(testCaseNumber); TextReader inputStream = new StreamReader(inputFilePath); var debugConsoleWriter = new DebugConsoleWriter(); new Program().Solve(inputStream, debugConsoleWriter); var output = debugConsoleWriter.GetAllOutput(); TextReader outputStream = new StreamReader(outputFilePath); var outputAnswer = outputStream.ReadToEnd(); #if UTAKA_DEBUG Console.Write(output); Console.Write(outputAnswer); #endif var isCorrect = output == outputAnswer; return isCorrect; } public void TestProblems() { var isSuccessAll = true; var testCaseNumber = 1; while (true) { var inputFileName = GetInputFilePath(testCaseNumber); if (!File.Exists(inputFileName)) { break; } Console.WriteLine( $"TestCase {testCaseNumber} ====================================================="); var result = TestProblem(testCaseNumber); if (result) { Console.WriteLine($"Success"); } else { isSuccessAll = false; Console.WriteLine($"Failure *****"); } testCaseNumber++; } if (isSuccessAll) { Console.WriteLine($"!!!!!!!!! All Success !!!!!!!!!"); } } } public static class Dbg { public static void WriteLine(string str) { Console.WriteLine(str); } public static void Write(string str) { Console.Write(str); } } #else public static class Dbg { public static void WriteLine(string str) {} public static void Write(string str) {} } #endif }
[["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
1,159
2
using System; using System.Linq; class C { static void Main() { Console.ReadLine(); var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var b = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var x = 0; for (var i = 0; i < b.Length; i++) { var m = Math.Min(a[i], b[i]); x += m; b[i] -= m; var n = Math.Min(a[i + 1], b[i]); x += n; a[i + 1] -= n; } Console.WriteLine(x); } }
using System; using System.Linq; class C { static void Main() { Console.ReadLine(); var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var b = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var x = 0L; for (var i = 0; i < b.Length; i++) { var m = Math.Min(a[i], b[i]); x += m; b[i] -= m; var n = Math.Min(a[i + 1], b[i]); x += n; a[i + 1] -= n; } Console.WriteLine(x); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
171
2
using System; using System.Collections.Generic; using System.Linq; public class Contest { public static void Main() { /* int n = a[0]; int[][] e = new int[n][]; for(int i=0; i<n; i++) e[i] = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(zzz => int.Parse(zzz))); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(zzz => int.Parse(zzz))); */ // long a,b,c,d,e,f; // MyRead.Multi(out a, out b, out c, out d, out e, out f); int n; MyRead.Multi(out n); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, int>(zzz => int.Parse(zzz))); int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, int>(zzz => int.Parse(zzz))); int cnt = 0; for (int i = 0; i < n; i++) { if (a[i] < b[i]) { cnt += a[i]; int t = a[i + 1]; a[i + 1] -= b[i] - a[i]; if (a[i + 1] < 0) a[i + 1] = 0; cnt += t - a[i + 1]; } else { cnt += b[i]; } } string s = cnt.ToString(); Console.WriteLine(s); } private static int calc_main(int[] a, int[] b, int[] c, int[] d) { return 0; } } public static class MyRead { private static bool eq<T, U>() => typeof(T).Equals(typeof(U)); private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); private static 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); public static string[] Multi<T>(out T a) { var ar = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); a = cv<T>(ar[0]); return ar; } public static string[] Multi<T, U>(out T a, out U b) { var ar = Multi<T>(out a); b = cv<U>(ar[1]); return ar; } public static string[] Multi<T, U, V>(out T a, out U b, out V c) { var ar = Multi<T, U>(out a, out b); c = cv<V>(ar[2]); return ar; } public static string[] Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = Multi<T, U, V>(out a, out b, out c); d = cv<W>(ar[3]); return ar; } public static string[] Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = Multi<T, U, V, W>(out a, out b, out c, out d); e = cv<X>(ar[4]); return ar; } public static string[] 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 = Multi<T, U, V, W, X>(out a, out b, out c, out d, out e); f = cv<Y>(ar[5]); return ar; } }
using System; using System.Collections.Generic; using System.Linq; public class Contest { public static void Main() { /* int n = a[0]; int[][] e = new int[n][]; for(int i=0; i<n; i++) e[i] = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(zzz => int.Parse(zzz))); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(zzz => int.Parse(zzz))); */ // long a,b,c,d,e,f; // MyRead.Multi(out a, out b, out c, out d, out e, out f); int n; MyRead.Multi(out n); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, int>(zzz => int.Parse(zzz))); int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, int>(zzz => int.Parse(zzz))); long cnt = 0; for (int i = 0; i < n; i++) { if (a[i] < b[i]) { cnt += a[i]; int t = a[i + 1]; a[i + 1] -= b[i] - a[i]; if (a[i + 1] < 0) a[i + 1] = 0; cnt += t - a[i + 1]; } else { cnt += b[i]; } } string s = cnt.ToString(); Console.WriteLine(s); } private static int calc_main(int[] a, int[] b, int[] c, int[] d) { return 0; } } public static class MyRead { private static bool eq<T, U>() => typeof(T).Equals(typeof(U)); private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); private static 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); public static string[] Multi<T>(out T a) { var ar = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); a = cv<T>(ar[0]); return ar; } public static string[] Multi<T, U>(out T a, out U b) { var ar = Multi<T>(out a); b = cv<U>(ar[1]); return ar; } public static string[] Multi<T, U, V>(out T a, out U b, out V c) { var ar = Multi<T, U>(out a, out b); c = cv<V>(ar[2]); return ar; } public static string[] Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = Multi<T, U, V>(out a, out b, out c); d = cv<W>(ar[3]); return ar; } public static string[] Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = Multi<T, U, V, W>(out a, out b, out c, out d); e = cv<X>(ar[4]); return ar; } public static string[] 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 = Multi<T, U, V, W, X>(out a, out b, out c, out d, out e); f = cv<Y>(ar[5]); return ar; } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
874
2
using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str1 = Console.ReadLine().Split(' '); int[] a = new int[n + 1]; for (int i = 0; i < n + 1; i++) { a[i] = int.Parse(str1[i]); } string[] str2 = Console.ReadLine().Split(' '); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = int.Parse(str2[i]); } int ans = 0; for (int i = 0; i < n; i++) { ans += Math.Min(a[i], b[i]); b[i] -= Math.Min(a[i], b[i]); ans += Math.Min(a[i + 1], b[i]); a[i + 1] -= Math.Min(a[i + 1], b[i]); } Console.WriteLine(ans.ToString()); } }
using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str1 = Console.ReadLine().Split(' '); int[] a = new int[n + 1]; for (int i = 0; i < n + 1; i++) { a[i] = int.Parse(str1[i]); } string[] str2 = Console.ReadLine().Split(' '); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = int.Parse(str2[i]); } long ans = 0; for (int i = 0; i < n; i++) { ans += Math.Min(a[i], b[i]); b[i] -= Math.Min(a[i], b[i]); ans += Math.Min(a[i + 1], b[i]); a[i + 1] -= Math.Min(a[i + 1], b[i]); } Console.WriteLine(ans.ToString()); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
268
2
using System; namespace AtCoderABC135C { class Program { static void Main(string[] args) { int ans = 0; int N = int.Parse(Console.ReadLine()); string[] s = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(s[i]); } s = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(s[i]); } for (int i = 0; i < N; i++) { int M = A[i] + A[i + 1]; if (M > B[i]) { ans += B[i]; M -= B[i]; if (M < A[i + 1]) A[i + 1] = M; } else { ans += M; A[i + 1] = 0; } } Console.WriteLine(ans.ToString()); } } }
using System; namespace AtCoderABC135C { class Program { static void Main(string[] args) { long ans = 0; int N = int.Parse(Console.ReadLine()); string[] s = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(s[i]); } s = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(s[i]); } for (int i = 0; i < N; i++) { int M = A[i] + A[i + 1]; if (M > B[i]) { ans += B[i]; M -= B[i]; if (M < A[i + 1]) A[i + 1] = M; } else { ans += M; A[i + 1] = 0; } } Console.WriteLine(ans.ToString()); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
265
2
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { static void Main(string[] args) { MainStream(); } static void MainStream() { Scan sc = new Scan(); write wr = new write(); int n = sc.intarr[0]; int[] a = sc.intarr; int[] b = sc.intarr; int monster_all_count = 0; for (int i = 0; i <= n; i++) { monster_all_count += a[i]; } // int remain_brave = 0; // int remain_monster = 0; for (int i = 0; i < n; i++) { int brave = b[i]; if (brave > a[i]) { a[i + 1] -= (brave - a[i]); if (a[i + 1] < 0) { a[i + 1] = 0; } a[i] = 0; } else { a[i] -= brave; } } int after_monster = 0; for (int i = 0; i <= n; i++) { after_monster += a[i]; } wr.wri(monster_all_count - after_monster); } } class Scan { public string str => Console.ReadLine(); public string[] strarr => str.Split(' '); public long[] longarr => strarr.Select(long.Parse).ToArray(); public int[] intarr => strarr.Select(int.Parse).ToArray(); public char[] chararr => str.ToArray(); } class write { public void wri<Type>(Type x) { Console.WriteLine(x); } } class Method { static public int max(params int[] nums) { int maxx = nums[0]; for (int i = 1; i < nums.Length; i++) { maxx = Math.Max(maxx, nums[i]); } return maxx; } } }
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { static void Main(string[] args) { MainStream(); } static void MainStream() { Scan sc = new Scan(); write wr = new write(); int n = sc.intarr[0]; int[] a = sc.intarr; int[] b = sc.intarr; long monster_all_count = 0; for (int i = 0; i <= n; i++) { monster_all_count += a[i]; } // int remain_brave = 0; // int remain_monster = 0; for (int i = 0; i < n; i++) { int brave = b[i]; if (brave > a[i]) { a[i + 1] -= (brave - a[i]); if (a[i + 1] < 0) { a[i + 1] = 0; } a[i] = 0; } else { a[i] -= brave; } } long after_monster = 0; for (int i = 0; i <= n; i++) { after_monster += a[i]; } wr.wri(monster_all_count - after_monster); } } class Scan { public string str => Console.ReadLine(); public string[] strarr => str.Split(' '); public long[] longarr => strarr.Select(long.Parse).ToArray(); public int[] intarr => strarr.Select(int.Parse).ToArray(); public char[] chararr => str.ToArray(); } class write { public void wri<Type>(Type x) { Console.WriteLine(x); } } class Method { static public int max(params int[] nums) { int maxx = nums[0]; for (int i = 1; i < nums.Length; i++) { maxx = Math.Max(maxx, nums[i]); } return maxx; } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
408
4
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string m = Console.ReadLine(); string y = Console.ReadLine(); string[] monster = m.Split(' '); string[] yusha = y.Split(' '); int[] monsterList = monster.Select(x => int.Parse(x)).ToArray(); int[] yushaList = yusha.Select(x => int.Parse(x)).ToArray(); int result = 0; for (int i = 0; i < n; i++) { if (yushaList[i] <= monsterList[i]) { result += yushaList[i]; } else { result += monsterList[i]; int a = yushaList[i] - monsterList[i]; if (monsterList[i + 1] - a >= 0) { monsterList[i + 1] -= a; result += a; } else { result += (monsterList[i + 1]); monsterList[i + 1] = 0; } } } Console.WriteLine(result); } } }
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string m = Console.ReadLine(); string y = Console.ReadLine(); string[] monster = m.Split(' '); string[] yusha = y.Split(' '); int[] monsterList = monster.Select(x => int.Parse(x)).ToArray(); int[] yushaList = yusha.Select(x => int.Parse(x)).ToArray(); long result = 0; for (int i = 0; i < n; i++) { if (yushaList[i] <= monsterList[i]) { result += yushaList[i]; } else { result += monsterList[i]; int yushaNextTown = yushaList[i] - monsterList[i]; if (monsterList[i + 1] - yushaNextTown >= 0) { monsterList[i + 1] -= yushaNextTown; result += yushaNextTown; } else { result += (monsterList[i + 1]); monsterList[i + 1] = 0; } } } Console.WriteLine(result); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 75, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 75, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 75, 196, 0, 57, 15, 16, 31, 16, 12, 22], ["+", 75, 196, 0, 57, 15, 16, 31, 16, 12, 22], ["-", 0, 57, 64, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 22]]
8
256
10
using System; using System.Collections.Generic; using System.Linq; using static Input; public class Prog { public struct Pair { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void Solve() { int N = NextInt(); int[] A = LineInt(); int[] B = LineInt(); int ans = A.Sum(); for (int i = 0; i < N; i++) { int F_m = Math.Min(A[i], B[i]); A[i] -= F_m; B[i] -= F_m; if (B[i] > 0) { F_m = Math.Min(A[i + 1], B[i]); A[i + 1] -= F_m; B[i] -= F_m; } } Console.WriteLine(ans - A.Sum()); } public static void Main() { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } private static int UpperBound(int[] list, int n) { int a = 0, b = list.Count(); if (list[a] >= n) return a; else if (list[b - 1] < n) return b; while (b - a != 1) { int x = (a + b) / 2; if (list[x] >= n) b = x; else a = x; } return b; } private static int LowerBound(int[] list, int n) { int a = 0, b = list.Count(); if (list[b - 1] <= n) return b - 1; else if (list[a] > n) return a - 1; while (b - a != 1) { int x = (a + b) / 2; if (list[x] <= n) a = x; else b = x; } return a; } } public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable { private List<Tkey> keys = new List<Tkey>(); private List<Tvalue> values = new List<Tvalue>(); private int size = 0; private static bool Descending; private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); } public PriorityQueue(bool b = true) { Clear(); Descending = b; } public int Count() { return size; } public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; } public void Add(Tkey e, Tvalue v) { keys.Add(e); values.Add(v); int x = size; size += 1; while (x != 0) { int par = (x % 2 == 0 ? x - 2 : x - 1) / 2; if (Compare(keys[par], e) <= 0) break; Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a; Tvalue b = values[x]; values[x] = values[par]; values[par] = b; x = par; } } public void Poll() { Tkey k = keys[size - 1]; Tvalue v = values[size - 1]; int x = 0; size--; if (size == 0) { Clear(); return; } keys.RemoveAt(size); values.RemoveAt(size); while (true) { int l = x * 2 + 1; int r = x * 2 + 2; if (size - 1 < l) break; int y = 0; if (size - 1 == l) { y = l; } else if (Compare(keys[l], keys[r]) == 1) { y = r; } else { y = l; } if (Compare(keys[y], k) > 0) break; keys[x] = keys[y]; values[x] = values[y]; x = y; } keys[x] = k; values[x] = v; } public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; } public Tvalue Peek() { return values[0]; } public void Debug() { Console.Write("key : "); foreach (var k in keys) Console.Write(k + " "); Console.WriteLine(); Console.Write("value : "); foreach (var k in values) Console.Write(k + " "); Console.WriteLine(); } } public class Node { public List<Edge> relate; public bool visit = false; public long cost = long.MaxValue; public long plus_cost = 0; public Node() { relate = new List<Edge>(); } } public class Edge { public int to; public long cost; public long T; public Edge(int to, long cost, long T) { this.to = to; this.cost = cost; this.T = T; } } 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; public class Prog { public struct Pair { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void Solve() { int N = NextInt(); long[] A = LineLong(); long[] B = LineLong(); long ans = A.Sum(); for (int i = 0; i < N; i++) { long F_m = Math.Min(A[i], B[i]); A[i] -= F_m; B[i] -= F_m; if (B[i] > 0) { F_m = Math.Min(A[i + 1], B[i]); A[i + 1] -= F_m; B[i] -= F_m; } } Console.WriteLine(ans - A.Sum()); } public static void Main() { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } private static int UpperBound(int[] list, int n) { int a = 0, b = list.Count(); if (list[a] >= n) return a; else if (list[b - 1] < n) return b; while (b - a != 1) { int x = (a + b) / 2; if (list[x] >= n) b = x; else a = x; } return b; } private static int LowerBound(int[] list, int n) { int a = 0, b = list.Count(); if (list[b - 1] <= n) return b - 1; else if (list[a] > n) return a - 1; while (b - a != 1) { int x = (a + b) / 2; if (list[x] <= n) a = x; else b = x; } return a; } } public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable { private List<Tkey> keys = new List<Tkey>(); private List<Tvalue> values = new List<Tvalue>(); private int size = 0; private static bool Descending; private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); } public PriorityQueue(bool b = true) { Clear(); Descending = b; } public int Count() { return size; } public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; } public void Add(Tkey e, Tvalue v) { keys.Add(e); values.Add(v); int x = size; size += 1; while (x != 0) { int par = (x % 2 == 0 ? x - 2 : x - 1) / 2; if (Compare(keys[par], e) <= 0) break; Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a; Tvalue b = values[x]; values[x] = values[par]; values[par] = b; x = par; } } public void Poll() { Tkey k = keys[size - 1]; Tvalue v = values[size - 1]; int x = 0; size--; if (size == 0) { Clear(); return; } keys.RemoveAt(size); values.RemoveAt(size); while (true) { int l = x * 2 + 1; int r = x * 2 + 2; if (size - 1 < l) break; int y = 0; if (size - 1 == l) { y = l; } else if (Compare(keys[l], keys[r]) == 1) { y = r; } else { y = l; } if (Compare(keys[y], k) > 0) break; keys[x] = keys[y]; values[x] = values[y]; x = y; } keys[x] = k; values[x] = v; } public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; } public Tvalue Peek() { return values[0]; } public void Debug() { Console.Write("key : "); foreach (var k in keys) Console.Write(k + " "); Console.WriteLine(); Console.Write("value : "); foreach (var k in values) Console.Write(k + " "); Console.WriteLine(); } } public class Node { public List<Edge> relate; public bool visit = false; public long cost = long.MaxValue; public long plus_cost = 0; public Node() { relate = new List<Edge>(); } } public class Edge { public int to; public long cost; public long T; public Edge(int to, long cost, long T) { this.to = to; this.cost = cost; this.T = T; } } 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); }
[["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 198, 0, 200, 0, 212, 0, 213, 63, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 213, 63, 22], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199]]
8
1,460
12
using System; class atcoder { public static void Main() { int cnt = 0; int left = 0; int n = int.Parse(Console.ReadLine()); string[] a = Console.ReadLine().Split(' '); string[] b = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { int b1 = int.Parse(b[i]); int a1 = int.Parse(a[i]); if (a1 <= b1 + left) { cnt += a1; left += b1 - a1; if (left >= b1) { left = b1; } } else { cnt += b1 + left; left = 0; } } int an = int.Parse(a[n]); if (an <= left) { cnt += an; } else { cnt += left; } Console.WriteLine(cnt); } }
using System; class atcoder { public static void Main() { long cnt = 0; int left = 0; int n = int.Parse(Console.ReadLine()); string[] a = Console.ReadLine().Split(' '); string[] b = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { int b1 = int.Parse(b[i]); int a1 = int.Parse(a[i]); if (a1 <= (b1 + left)) { cnt += a1; left += b1 - a1; if (left >= b1) { left = b1; } } else { cnt += b1 + left; left = 0; } } int an = int.Parse(a[n]); if (an <= left) { cnt += an; } else { cnt += left; } Console.WriteLine(cnt); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 8, 196, 0, 57, 15, 16, 12, 23, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 12, 23, 0, 25]]
8
201
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 N = Int32.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(__ => Int32.Parse(__)).ToArray(); var B = Console.ReadLine().Split(' ').Select(__ => Int32.Parse(__)).ToArray(); var count = 0; for (var i = 0; i < N; i++) { var a = Math.Min(A[i], B[i]); count += a; A[i] -= a; B[i] -= a; a = Math.Min(A[i + 1], B[i]); count += a; A[i + 1] -= a; B[i] -= a; } Console.WriteLine(count); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { var N = Int32.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(__ => Int32.Parse(__)).ToArray(); var B = Console.ReadLine().Split(' ').Select(__ => Int32.Parse(__)).ToArray(); var count = 0L; for (var i = 0; i < N; i++) { var a = Math.Min(A[i], B[i]); count += a; A[i] -= a; B[i] -= a; a = Math.Min(A[i + 1], B[i]); count += a; A[i + 1] -= a; B[i] -= a; } Console.WriteLine(count); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
223
2
using System; public class Hello { public static void Main() { // Your code here! int n = int.Parse(System.Console.ReadLine()); string[] s = System.Console.ReadLine().Split(' '); string[] s2 = System.Console.ReadLine().Split(' '); int[] a = new int[n + 1]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = int.Parse(s[i]); b[i] = int.Parse(s2[i]); } a[n] = int.Parse(s[n]); int x = 0; for (int i = 0; i < n; i++) { if (a[i] - b[i] >= 0) { a[i] -= b[i]; x += b[i]; b[i] = 0; } else { b[i] -= a[i]; x += a[i]; a[i] = 0; } if (b[i] > 0) { if (a[i + 1] - b[i] >= 0) { a[i + 1] -= b[i]; x += b[i]; b[i] = 0; } else { b[i] -= a[i + 1]; x += a[i + 1]; a[i + 1] = 0; } } } System.Console.WriteLine(x); } }
using System; public class Hello { public static void Main() { // Your code here! long n = int.Parse(System.Console.ReadLine()); string[] s = System.Console.ReadLine().Split(' '); string[] s2 = System.Console.ReadLine().Split(' '); long[] a = new long[n + 1]; long[] b = new long[n]; for (int i = 0; i < n; i++) { a[i] = int.Parse(s[i]); b[i] = int.Parse(s2[i]); } a[n] = int.Parse(s[n]); long x = 0; for (int i = 0; i < n; i++) { if (a[i] - b[i] >= 0) { a[i] -= b[i]; x += b[i]; b[i] = 0; } else { b[i] -= a[i]; x += a[i]; a[i] = 0; } if (b[i] > 0) { if (a[i + 1] - b[i] >= 0) { a[i + 1] -= b[i]; x += b[i]; b[i] = 0; } else { b[i] -= a[i + 1]; x += a[i + 1]; a[i + 1] = 0; } } } System.Console.WriteLine(x); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
343
12
using System; using System.Linq; using static System.Console; class Program { static void Main(string[] args) { int N = int.Parse(ReadLine()); string[] strA = ReadLine().Split(' '); string[] strB = ReadLine().Split(' '); int[] A = strA.Select(int.Parse).ToArray(); int[] B = strB.Select(int.Parse).ToArray(); int tmp; int ans = 0; for (int i = 0; i < N; i++) { tmp = 0; if (A[i] >= B[i]) { ans += B[i]; } else { ans += A[i]; tmp = B[i] - A[i]; if (A[i + 1] >= tmp) { ans += tmp; A[i + 1] -= tmp; } else { ans += A[i + 1]; A[i + 1] = 0; } } } WriteLine(ans); } }
using System; using System.Linq; using static System.Console; class Program { static void Main(string[] args) { int N = int.Parse(ReadLine()); string[] strA = ReadLine().Split(' '); string[] strB = ReadLine().Split(' '); int[] A = strA.Select(int.Parse).ToArray(); int[] B = strB.Select(int.Parse).ToArray(); int tmp; long ans = 0L; for (int i = 0; i < N; i++) { tmp = 0; if (A[i] >= B[i]) { ans += B[i]; } else { ans += A[i]; tmp = B[i] - A[i]; if (A[i + 1] >= tmp) { ans += tmp; A[i + 1] -= tmp; } else { ans += A[i + 1]; A[i + 1] = 0; } } } WriteLine(ans); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
232
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ABC135 { class Program3 { static void Main() { var N = int.Parse(Console.ReadLine()); var str = Console.ReadLine(); var A = str.Split(' ').Select(int.Parse).ToArray(); str = Console.ReadLine(); var B = str.Split(' ').Select(int.Parse).ToArray(); if (A.Length != N + 1 || B.Length != N) A[N + 2] = 1; var ans = 0; var remainder = 0; for (int i = 0; i < N; i++) { ans += Math.Min(A[i], B[i] + remainder); var a = Math.Max(A[i] - remainder, 0); remainder = Math.Max(B[i] - a, 0); // Console.WriteLine(remainder); } ans += Math.Min(A[N], remainder); Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ABC135 { class Program3 { static void Main() { var N = int.Parse(Console.ReadLine()); var str = Console.ReadLine(); var A = str.Split(' ').Select(int.Parse).ToArray(); str = Console.ReadLine(); var B = str.Split(' ').Select(int.Parse).ToArray(); if (A.Length != N + 1 || B.Length != N) A[N + 2] = 1; Decimal ans = 0; var remainder = 0; for (int i = 0; i < N; i++) { ans += Math.Min(A[i], B[i] + remainder); var a = Math.Max(A[i] - remainder, 0); remainder = Math.Max(B[i] - a, 0); // Console.WriteLine(remainder); } ans += Math.Min(A[N], remainder); Console.WriteLine(ans); } } }
[["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 22]]
8
237
2
using System; using System.Collections.Generic; using System.Linq; namespace Welcome_to_AtCoder { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var monsterArray = Console.ReadLine().Split().Select(_ => int.Parse(_)).ToArray(); var heroArray = Console.ReadLine().Split().Select(_ => int.Parse(_)).ToArray(); var killedMonsterCount = 0; for (int i = 0; i < heroArray.Length; i++) { var monsterCount = monsterArray[i]; var heroKillableCount = heroArray[i]; if (monsterCount < heroKillableCount) { killedMonsterCount += monsterCount; heroKillableCount -= monsterCount; var nextMonsterCount = monsterArray[i + 1]; if (nextMonsterCount < heroKillableCount) { killedMonsterCount += nextMonsterCount; monsterArray[i + 1] = 0; } else { killedMonsterCount += heroKillableCount; monsterArray[i + 1] -= heroKillableCount; } } else { killedMonsterCount += heroKillableCount; } } Console.WriteLine(killedMonsterCount); } } }
using System; using System.Collections.Generic; using System.Linq; namespace Welcome_to_AtCoder { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var monsterArray = Console.ReadLine().Split().Select(_ => decimal.Parse(_)).ToArray(); var heroArray = Console.ReadLine().Split().Select(_ => decimal.Parse(_)).ToArray(); var killedMonsterCount = 0M; for (int i = 0; i < heroArray.Length; i++) { var monsterCount = monsterArray[i]; var heroKillableCount = heroArray[i]; if (monsterCount < heroKillableCount) { killedMonsterCount += monsterCount; heroKillableCount -= monsterCount; var nextMonsterCount = monsterArray[i + 1]; if (nextMonsterCount < heroKillableCount) { killedMonsterCount += nextMonsterCount; monsterArray[i + 1] = 0; } else { killedMonsterCount += heroKillableCount; monsterArray[i + 1] -= heroKillableCount; } } else { killedMonsterCount += heroKillableCount; } } Console.WriteLine(killedMonsterCount); } } }
[["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 250]]
8
222
6
#if DEBUG using Microsoft.VisualStudio.TestTools.UnitTesting; #endif using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Numerics; // cannot be used in GCJ using System.Text; using System.Threading.Tasks; namespace Contest { public class Program { public static IO.StreamScanner scanner = new IO.StreamScanner(Console.OpenStandardInput()); public static void Main() { var n = scanner.Integer(); var a = scanner.ScanLine().Split().Select(v => int.Parse(v)).ToList(); var b = scanner.ScanLine().Split().Select(v => int.Parse(v)).ToList(); var ans = GetAns(n, a, b); IO.Printer.Out.WriteLine(ans); IO.Printer.Out.Flush(); } public static int GetAns(int n, List<int> a, List<int> b) { var ret = 0; for (int i = 0; i < n; i++) { if (a[i] < b[i]) { ret += a[i]; b[i] -= a[i]; a[i] = 0; } else { ret += b[i]; a[i] -= b[i]; b[i] = 0; } if (a[i + 1] < b[i]) { ret += a[i + 1]; b[i] -= a[i + 1]; a[i + 1] = 0; } else { ret += b[i]; a[i + 1] -= b[i]; b[i] = 0; } } return ret; } } #if DEBUG [TestClass] public class Test { [TestMethod] public void Test1() {} public static bool IsEq(IEnumerable<IEnumerable<int>> l1, IEnumerable<IEnumerable<int>> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (!IsEq(l1.ElementAt(i), l2.ElementAt(i))) return false; } return true; } public static bool IsEq(IEnumerable<int> l1, IEnumerable<int> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (l1.ElementAt(i) != l2.ElementAt(i)) return false; } return true; } public static bool IsEq(IEnumerable<double> l1, IEnumerable<double> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (!IsEq(l1.ElementAt(i), l2.ElementAt(i))) return false; } return true; } public static bool IsEq(IEnumerable<bool> l1, IEnumerable<bool> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (l1.ElementAt(i) != l2.ElementAt(i)) return false; } return true; } public static bool IsEq(double a, double b) { return Math.Abs(a - b) < 1E-5; } } #endif // Used in LeetCode public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } } public static class LeetCode { public static TreeNode ParseBinaryTree(string str) { var ar = str.Replace("[", "").Replace("]", "").Split(','); var nodes = new List<TreeNode>(); for (int i = 0; i < ar.Length; i++) { nodes.Add(ar[i] == "null" ? null : new TreeNode(int.Parse(ar[i]))); var parent = (i + 1) / 2 - 1; // if the node class parent property // if(parent >= 0) //{ // nodes.Last().Parent = nodes[parent]; //} } for (int i = 0; i < nodes.Count(); i++) { var l = (i + 1) * 2 - 1; var r = (i + 1) * 2; if (l >= 0 && l < nodes.Count() && nodes[i] != null) nodes[i].left = nodes[l]; if (r >= 0 && r < nodes.Count() && nodes[i] != null) nodes[i].right = nodes[r]; } return nodes.First(); } public static IList<IList<int>> ParseLLI(string str) { var ret = new List<IList<int>>(); var split = str.Split(']'); foreach (var s in split) { var v = s.Replace(",[", "").Replace(", [", "").Replace("[", ""); if (string.IsNullOrWhiteSpace(v)) { ret.Add(new List<int>()); continue; } ret.Add(v.Split(',').Select(i => int.Parse(i)).ToList()); } return (IList<IList<int>>)ret; } public static IList<int> ParseLI(string str) { str = str.Replace("[", "").Replace("]", ""); return str.Split(',').Select(v => int.Parse(v)).ToList(); } } namespace IO { using System.IO; using System.Text; using System.Globalization; public class Printer : StreamWriter { static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; } public static Printer Out { get; set; } 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) {} public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); } public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr < len) return buf[ptr++]; ptr = 0; if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++]; isEof = true; return 0; } public char Char() { byte b; do b = read(); while ((b < 33 || 126 < b) && !isEof); 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 string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n'; b = (char)read()) if (b == 0) break; else if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (;; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; } static T[] enumerate<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(); return a; } public char[] Char(int n) { return enumerate(n, Char); } public string[] Scan(int n) { return enumerate(n, Scan); } public double[] Double(int n) { return enumerate(n, Double); } public int[] Integer(int n) { return enumerate(n, Integer); } public long[] Long(int n) { return enumerate(n, Long); } } } }
#if DEBUG using Microsoft.VisualStudio.TestTools.UnitTesting; #endif using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Numerics; // cannot be used in GCJ using System.Text; using System.Threading.Tasks; namespace Contest { public class Program { public static IO.StreamScanner scanner = new IO.StreamScanner(Console.OpenStandardInput()); public static void Main() { var n = scanner.Integer(); var a = scanner.ScanLine().Split().Select(v => long.Parse(v)).ToList(); var b = scanner.ScanLine().Split().Select(v => long.Parse(v)).ToList(); var ans = GetAns(n, a, b); IO.Printer.Out.WriteLine(ans); IO.Printer.Out.Flush(); } public static long GetAns(int n, List<long> a, List<long> b) { var ret = 0L; for (int i = 0; i < n; i++) { if (a[i] < b[i]) { ret += a[i]; b[i] -= a[i]; a[i] = 0; } else { ret += b[i]; a[i] -= b[i]; b[i] = 0; } if (a[i + 1] < b[i]) { ret += a[i + 1]; b[i] -= a[i + 1]; a[i + 1] = 0; } else { ret += b[i]; a[i + 1] -= b[i]; b[i] = 0; } } return ret; } } #if DEBUG [TestClass] public class Test { [TestMethod] public void Test1() {} public static bool IsEq(IEnumerable<IEnumerable<int>> l1, IEnumerable<IEnumerable<int>> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (!IsEq(l1.ElementAt(i), l2.ElementAt(i))) return false; } return true; } public static bool IsEq(IEnumerable<int> l1, IEnumerable<int> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (l1.ElementAt(i) != l2.ElementAt(i)) return false; } return true; } public static bool IsEq(IEnumerable<double> l1, IEnumerable<double> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (!IsEq(l1.ElementAt(i), l2.ElementAt(i))) return false; } return true; } public static bool IsEq(IEnumerable<bool> l1, IEnumerable<bool> l2) { if (l1.Count() != l2.Count()) return false; for (int i = 0; i < l1.Count(); i++) { if (l1.ElementAt(i) != l2.ElementAt(i)) return false; } return true; } public static bool IsEq(double a, double b) { return Math.Abs(a - b) < 1E-5; } } #endif // Used in LeetCode public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } } public static class LeetCode { public static TreeNode ParseBinaryTree(string str) { var ar = str.Replace("[", "").Replace("]", "").Split(','); var nodes = new List<TreeNode>(); for (int i = 0; i < ar.Length; i++) { nodes.Add(ar[i] == "null" ? null : new TreeNode(int.Parse(ar[i]))); var parent = (i + 1) / 2 - 1; // if the node class parent property // if(parent >= 0) //{ // nodes.Last().Parent = nodes[parent]; //} } for (int i = 0; i < nodes.Count(); i++) { var l = (i + 1) * 2 - 1; var r = (i + 1) * 2; if (l >= 0 && l < nodes.Count() && nodes[i] != null) nodes[i].left = nodes[l]; if (r >= 0 && r < nodes.Count() && nodes[i] != null) nodes[i].right = nodes[r]; } return nodes.First(); } public static IList<IList<int>> ParseLLI(string str) { var ret = new List<IList<int>>(); var split = str.Split(']'); foreach (var s in split) { var v = s.Replace(",[", "").Replace(", [", "").Replace("[", ""); if (string.IsNullOrWhiteSpace(v)) { ret.Add(new List<int>()); continue; } ret.Add(v.Split(',').Select(i => int.Parse(i)).ToList()); } return (IList<IList<int>>)ret; } public static IList<int> ParseLI(string str) { str = str.Replace("[", "").Replace("]", ""); return str.Split(',').Select(v => int.Parse(v)).ToList(); } } namespace IO { using System.IO; using System.Text; using System.Globalization; public class Printer : StreamWriter { static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; } public static Printer Out { get; set; } 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) {} public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); } public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr < len) return buf[ptr++]; ptr = 0; if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++]; isEof = true; return 0; } public char Char() { byte b; do b = read(); while ((b < 33 || 126 < b) && !isEof); 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 string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n'; b = (char)read()) if (b == 0) break; else if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (;; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; } static T[] enumerate<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(); return a; } public char[] Char(int n) { return enumerate(n, Char); } public string[] Scan(int n) { return enumerate(n, Scan); } public double[] Double(int n) { return enumerate(n, Double); } public int[] Integer(int n) { return enumerate(n, Integer); } public long[] Long(int n) { return enumerate(n, Long); } } } }
[["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 8, 201, 0, 235, 8, 201, 0, 195, 39, 199], ["+", 8, 201, 0, 235, 8, 201, 0, 195, 39, 199], ["-", 54, 55, 0, 220, 39, 236, 237, 238, 0, 199], ["+", 54, 55, 0, 220, 39, 236, 237, 238, 0, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
2,055
12
using System; using System.Linq; namespace atcoder135 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] ct = Console.ReadLine().Split().Select(int.Parse).ToArray(); int[] h = Console.ReadLine().Split().Select(int.Parse).ToArray(); int sm = 0; for (int i = 0; i < n; ++i) { int tp = M(ct[i], h[i]); ct[i] -= tp; h[i] -= tp; sm += tp; tp = M(ct[i + 1], h[i]); ct[i + 1] -= tp; h[i] -= tp; sm += tp; } Console.WriteLine(sm); } static int M(int ct, int h) { if (ct <= h) return ct; return h; } } }
using System; using System.Linq; namespace atcoder135 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] ct = Console.ReadLine().Split().Select(int.Parse).ToArray(); int[] h = Console.ReadLine().Split().Select(int.Parse).ToArray(); long sm = 0; for (int i = 0; i < n; ++i) { int tp = M(ct[i], h[i]); ct[i] -= tp; h[i] -= tp; sm += tp; tp = M(ct[i + 1], h[i]); ct[i + 1] -= tp; h[i] -= tp; sm += tp; } Console.WriteLine(sm); } static int M(int ct, int h) { if (ct <= h) return ct; return h; } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
216
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { void Solve() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(); var B = Console.ReadLine().Split(); var AA = new int[N + 1]; var BB = new int[N]; for (int i = 0; i < N + 1; i++) { AA[i] = int.Parse(A[i]); } for (int i = 0; i < N; i++) { BB[i] = int.Parse(B[i]); } var r = 0; for (int i = 0; i < N; i++) { var a = Math.Min(AA[i], BB[i]); AA[i] -= a; BB[i] -= a; r += a; a = Math.Min(AA[i + 1], BB[i]); AA[i + 1] -= a; BB[i] -= a; r += a; } Console.WriteLine(r); } static void Main(string[] args) { new Program().Solve(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { void Solve() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(); var B = Console.ReadLine().Split(); var AA = new int[N + 1]; var BB = new int[N]; for (int i = 0; i < N + 1; i++) { AA[i] = int.Parse(A[i]); } for (int i = 0; i < N; i++) { BB[i] = int.Parse(B[i]); } long r = 0; // N for (int i = 0; i < N; i++) { var a = Math.Min(AA[i], BB[i]); AA[i] -= a; BB[i] -= a; r += a; a = Math.Min(AA[i + 1], BB[i]); AA[i + 1] -= a; BB[i] -= a; r += a; } Console.WriteLine(r); } static void Main(string[] args) { new Program().Solve(); } }
[["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
284
2
using System; using System.Text; using System.Collections.Generic; using System.Linq; using System.Numerics; class AtCoder { const int MOD = 1000000007; public void Slove() { var n = int.Parse(Console.ReadLine()); var mons = Console.ReadLine().Split().Select(int.Parse).ToArray(); var unit = Console.ReadLine().Split().Select(int.Parse).ToArray(); long ans = 0; for (int i = 0; i < n; i++) { var temp = Math.Min(mons[i], unit[i]); mons[i] -= temp; unit[i] -= temp; ans += temp; if (unit[i] > 0) { temp = Math.Min(mons[i + 1], unit[i]); mons[i] -= temp; ans += temp; } } Console.WriteLine(ans); } static void Main() { var ac = new AtCoder(); ac.Slove(); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; using System.Numerics; class AtCoder { const int MOD = 1000000007; public void Slove() { var n = int.Parse(Console.ReadLine()); var mons = Console.ReadLine().Split().Select(int.Parse).ToArray(); var unit = Console.ReadLine().Split().Select(int.Parse).ToArray(); long ans = 0; for (int i = 0; i < n; i++) { var temp = Math.Min(mons[i], unit[i]); mons[i] -= temp; unit[i] -= temp; ans += temp; if (unit[i] > 0) { temp = Math.Min(mons[i + 1], unit[i]); mons[i + 1] -= temp; ans += temp; } } Console.WriteLine(ans); } static void Main() { var ac = new AtCoder(); ac.Slove(); } }
[["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
230
2
using System; class ABC_135_C { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(input[i]); } string[] input2 = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(input2[i]); } int count = 0; for (int i = 0; i < N; i++) { if (A[i] <= B[i]) { count += A[i]; B[i] -= A[i]; A[i] = 0; } else { count += B[i]; A[i] -= B[i]; B[i] = 0; } if (A[i + 1] <= B[i]) { count += A[i + 1]; B[i] -= A[i + 1]; A[i + 1] = 0; } else { count += B[i]; A[i + 1] -= B[i]; B[i] = 0; } } Console.WriteLine(count); } }
using System; class ABC_135_C { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); int[] A = new int[N + 1]; for (int i = 0; i < N + 1; i++) { A[i] = int.Parse(input[i]); } string[] input2 = Console.ReadLine().Split(' '); int[] B = new int[N]; for (int i = 0; i < N; i++) { B[i] = int.Parse(input2[i]); } long count = 0; for (int i = 0; i < N; i++) { if (A[i] <= B[i]) { count += A[i]; B[i] -= A[i]; A[i] = 0; } else { count += B[i]; A[i] -= B[i]; B[i] = 0; } if (A[i + 1] <= B[i]) { count += A[i + 1]; B[i] -= A[i + 1]; A[i + 1] = 0; } else { count += B[i]; A[i + 1] -= B[i]; B[i] = 0; } } Console.WriteLine(count); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
324
2
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Globalization; using System.Diagnostics; using System.Threading.Tasks; class Sobaya { static void Main() { new Sobaya().calc(); return; } Scanner cin; void calc() { cin = new Scanner(); int n = cin.nextInt(); int i1; int[] a = new int[n + 1]; int[] b = new int[n]; for (i1 = 0; i1 < n + 1; i1++) a[i1] = cin.nextInt(); for (i1 = 0; i1 < n; i1++) b[i1] = cin.nextInt(); int remain = 0; int ans = 0; for (i1 = 0; i1 < n; i1++) { remain = a[i1] + a[i1 + 1]; if (remain < b[i1]) { a[i1] = 0; a[i1 + 1] = 0; ans += remain; } else if (a[i1] < b[i1]) { a[i1 + 1] -= b[i1] - a[i1]; a[i1] = 0; ans += b[i1]; // Console.WriteLine(a[i1+1]+"a"); } else { ans += b[i1]; } // Console.WriteLine(ans); } 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; } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Globalization; using System.Diagnostics; using System.Threading.Tasks; class Sobaya { static void Main() { new Sobaya().calc(); return; } Scanner cin; void calc() { cin = new Scanner(); int n = cin.nextInt(); int i1; long[] a = new long[n + 1]; long[] b = new long[n]; for (i1 = 0; i1 < n + 1; i1++) a[i1] = cin.nextInt(); for (i1 = 0; i1 < n; i1++) b[i1] = cin.nextInt(); long remain = 0; long ans = 0; for (i1 = 0; i1 < n; i1++) { remain = a[i1] + a[i1 + 1]; if (remain < b[i1]) { a[i1] = 0; a[i1 + 1] = 0; ans += remain; } else if (a[i1] < b[i1]) { a[i1 + 1] -= b[i1] - a[i1]; a[i1] = 0; ans += b[i1]; // Console.WriteLine(a[i1+1]+"a"); } else { ans += b[i1]; } // Console.WriteLine(ans); } 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; } }
[["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
656
12
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var S = sc.ReadStr(); var dp = new long[13]; dp[0] = 1; long k = 1; for (int i = S.Length - 1; i >= 0; i--) { var nextDP = new long[13]; if (S[i] == '?') { for (int x = 0; x <= 9; x++) { var d = x * k; for (int j = 0; j < dp.Length; j++) { nextDP[(d + j) % 13] += dp[j]; nextDP[(d + j) % 13] %= MOD; } } } else { var d = ((int)S[i] - '0') * k; for (int j = 0; j < dp.Length; j++) { nextDP[(d + j) % 13] += dp[j]; nextDP[(d + j) % 13] %= MOD; } } dp = nextDP; k *= 10; k %= MOD; } var ans = dp[5]; Console.WriteLine(ans); } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Random Rand = new Random(); static Scanner sc = new Scanner(); static long Gcd(long a, long b) { var v = new[] { a, b }; while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; } return v[0]; } static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } static bool IsPrime(long num) { if (num < 2) return false; else 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; } static List<long> PrimeFactors(long n) { var res = new List<long>(); var tmp = n; for (long x = 2; x * x <= n; x++) { while (tmp % x == 0) { tmp /= x; res.Add(x); } } if (tmp != 1) res.Add(tmp); return res; } static long[] Divisor(long n) { var res = new List<long>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res.Add(i); if (i * i != n) res.Add(n / i); } } return res.ToArray(); } static Result[] ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f) { Result[] res = new Result[a.Count() * b.Count()]; var i = 0; foreach (var v in a) foreach (var w in b) res[i++] = f(v, w); return res; } static IEnumerable<Result> ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f, Func<T1, T2, bool> s) { foreach (var v in a) { foreach (var w in b) if (s(v, w)) yield return f(v, w); } } static List<long> Facs = new List<long>() { 1 }; static long Fac(int n) { for (int i = Facs.Count; i <= n; i++) { Facs.Add(i * Facs[i - 1]); } return Facs[n]; } static long nCr(int n, int r) { if (n < r) return 0; if (n == r) return 1; var res = Fac(n); res /= Fac(r); res /= Fac(n - r); return res; } } 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 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 Inverse(ModInt v) { return Pow(v, Mod - 2); } } public class BinomialCoefficient { public ModInt[] fact, ifact; public BinomialCoefficient(int n) { fact = new ModInt[n + 1]; ifact = new ModInt[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = ModInt.Inverse(fact[n]); for (int i = n - 1; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); ifact[0] = ifact[1]; } public ModInt this[int n, int r] { get { if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0; return fact[n] * ifact[n - r] * ifact[r]; } } public ModInt RepeatedCombination(int n, int k) { if (k == 0) return 1; return this[n + k - 1, k]; } } 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; } } 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 List<T[]> All(params T[] source) { var perms = new List<T[]>(); Search(perms, new Stack<T>(), source); return perms; } } 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; } } } class Vector<T> : List<T> where T : new() { public Vector() {} public Vector(int count, T defaultValue) { for (int i = 0; i < count; i++) { this.Add(defaultValue); } } } class PriorityQueue<T> where T : IComparable<T> { public readonly T[] Array; public readonly bool Greater; public int Count { get; private set; } = 0; public PriorityQueue(int capacity, bool greater = true) { this.Array = new T[capacity]; this.Greater = greater; } public void Enqueue(T item) { this.Array[this.Count] = item; this.Count += 1; this.UpHeap(); } public T Dequeue() { this.Swap(0, this.Count - 1); this.Count -= 1; this.DownHeap(); return this.Array[this.Count]; } public T Peek() { return this.Array[0]; } private void UpHeap() { var n = this.Count - 1; while (n != 0) { var parent = (n - 1) / 2; if (this.Array[n].CompareTo(this.Array[parent]) > 0) { this.Swap(n, parent); n = parent; } else { break; } } } private void DownHeap() { var parent = 0; while (true) { var child = 2 * parent + 1; if (child > this.Count - 1) break; if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0) { child += 1; } if (this.Compare(this.Array[parent], this.Array[child]) < 0) { this.Swap(parent, child); parent = child; } else { break; } } } private int Compare(T a, T b) { var c = a.CompareTo(b); if (!this.Greater) { c = -c; } return c; } private void Swap(int a, int b) { var tmp = this.Array[a]; this.Array[a] = this.Array[b]; this.Array[b] = tmp; } } class UnionFind { public int[] Parents { get; set; } public UnionFind(int n) { this.Parents = new int[n]; for (int i = 0; i < n; i++) { this.Parents[i] = i; } } public int Find(int x) { if (this.Parents[x] != x) { this.Parents[x] = Find(this.Parents[x]); } return this.Parents[x]; } public bool Same(int x, int y) { return this.Find(x) == this.Find(y); } public int Union(int x, int y) { var xroot = this.Find(x); var yroot = this.Find(y); if (xroot != yroot) this.Parents[xroot] = yroot; return yroot; } } static class Algorithm { static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb) { var l = 0; var r = a.Count - 1; while (l <= r) { var m = (l + r) / 2; var res = cmp.Compare(a[m], v); if (res < 0 || (res == 0 && !islb)) l = m + 1; else r = m - 1; } return l; } public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); } public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); } public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); } public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var S = sc.ReadStr(); var dp = new long[13]; dp[0] = 1; long k = 1; for (int i = S.Length - 1; i >= 0; i--) { var nextDP = new long[13]; if (S[i] == '?') { for (int x = 0; x <= 9; x++) { var d = x * k; for (int j = 0; j < dp.Length; j++) { nextDP[(d + j) % 13] += dp[j]; nextDP[(d + j) % 13] %= MOD; } } } else { var d = ((int)S[i] - '0') * k; for (int j = 0; j < dp.Length; j++) { nextDP[(d + j) % 13] += dp[j]; nextDP[(d + j) % 13] %= MOD; } } dp = nextDP; k *= 10; k %= 13; } var ans = dp[5]; Console.WriteLine(ans); } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Random Rand = new Random(); static Scanner sc = new Scanner(); static long Gcd(long a, long b) { var v = new[] { a, b }; while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; } return v[0]; } static long Lcm(long a, long b) { return a / Gcd(a, b) * b; } static bool IsPrime(long num) { if (num < 2) return false; else 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; } static List<long> PrimeFactors(long n) { var res = new List<long>(); var tmp = n; for (long x = 2; x * x <= n; x++) { while (tmp % x == 0) { tmp /= x; res.Add(x); } } if (tmp != 1) res.Add(tmp); return res; } static long[] Divisor(long n) { var res = new List<long>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res.Add(i); if (i * i != n) res.Add(n / i); } } return res.ToArray(); } static Result[] ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f) { Result[] res = new Result[a.Count() * b.Count()]; var i = 0; foreach (var v in a) foreach (var w in b) res[i++] = f(v, w); return res; } static IEnumerable<Result> ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f, Func<T1, T2, bool> s) { foreach (var v in a) { foreach (var w in b) if (s(v, w)) yield return f(v, w); } } static List<long> Facs = new List<long>() { 1 }; static long Fac(int n) { for (int i = Facs.Count; i <= n; i++) { Facs.Add(i * Facs[i - 1]); } return Facs[n]; } static long nCr(int n, int r) { if (n < r) return 0; if (n == r) return 1; var res = Fac(n); res /= Fac(r); res /= Fac(n - r); return res; } } 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 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 Inverse(ModInt v) { return Pow(v, Mod - 2); } } public class BinomialCoefficient { public ModInt[] fact, ifact; public BinomialCoefficient(int n) { fact = new ModInt[n + 1]; ifact = new ModInt[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = ModInt.Inverse(fact[n]); for (int i = n - 1; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); ifact[0] = ifact[1]; } public ModInt this[int n, int r] { get { if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0; return fact[n] * ifact[n - r] * ifact[r]; } } public ModInt RepeatedCombination(int n, int k) { if (k == 0) return 1; return this[n + k - 1, k]; } } 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; } } 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 List<T[]> All(params T[] source) { var perms = new List<T[]>(); Search(perms, new Stack<T>(), source); return perms; } } 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; } } } class Vector<T> : List<T> where T : new() { public Vector() {} public Vector(int count, T defaultValue) { for (int i = 0; i < count; i++) { this.Add(defaultValue); } } } class PriorityQueue<T> where T : IComparable<T> { public readonly T[] Array; public readonly bool Greater; public int Count { get; private set; } = 0; public PriorityQueue(int capacity, bool greater = true) { this.Array = new T[capacity]; this.Greater = greater; } public void Enqueue(T item) { this.Array[this.Count] = item; this.Count += 1; this.UpHeap(); } public T Dequeue() { this.Swap(0, this.Count - 1); this.Count -= 1; this.DownHeap(); return this.Array[this.Count]; } public T Peek() { return this.Array[0]; } private void UpHeap() { var n = this.Count - 1; while (n != 0) { var parent = (n - 1) / 2; if (this.Array[n].CompareTo(this.Array[parent]) > 0) { this.Swap(n, parent); n = parent; } else { break; } } } private void DownHeap() { var parent = 0; while (true) { var child = 2 * parent + 1; if (child > this.Count - 1) break; if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0) { child += 1; } if (this.Compare(this.Array[parent], this.Array[child]) < 0) { this.Swap(parent, child); parent = child; } else { break; } } } private int Compare(T a, T b) { var c = a.CompareTo(b); if (!this.Greater) { c = -c; } return c; } private void Swap(int a, int b) { var tmp = this.Array[a]; this.Array[a] = this.Array[b]; this.Array[b] = tmp; } } class UnionFind { public int[] Parents { get; set; } public UnionFind(int n) { this.Parents = new int[n]; for (int i = 0; i < n; i++) { this.Parents[i] = i; } } public int Find(int x) { if (this.Parents[x] != x) { this.Parents[x] = Find(this.Parents[x]); } return this.Parents[x]; } public bool Same(int x, int y) { return this.Find(x) == this.Find(y); } public int Union(int x, int y) { var xroot = this.Find(x); var yroot = this.Find(y); if (xroot != yroot) this.Parents[xroot] = yroot; return yroot; } } static class Algorithm { static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb) { var l = 0; var r = a.Count - 1; while (l <= r) { var m = (l + r) / 2; var res = cmp.Compare(a[m], v); if (res < 0 || (res == 0 && !islb)) l = m + 1; else r = m - 1; } return l; } public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); } public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); } public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); } public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); } }
[["-", 0, 7, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 7, 8, 196, 0, 1, 0, 11, 12, 203]]
8
3,320
2
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() { var ca = S.ToCharArray(); Array.Reverse(ca); int M = 13; int N = S.Length; mlong[] dp = new mlong[M]; int dig = 1; dp[0] = 1; for (int i = 0; i < N; i++) { var ndp = new mlong[M]; if (S[i] == '?') { for (int j = 0; j < M; j++) { for (int k = 0; k < 10; k++) { int nj = (dig * k + j) % M; ndp[nj] += dp[j]; } } } else { for (int j = 0; j < M; j++) { for (int k = 0; k < 10; k++) { if (S[i] - '0' != k) continue; int nj = (dig * k + j) % M; ndp[nj] += dp[j]; } } } dig *= 10; dig %= M; dp = ndp; } Console.WriteLine(dp[5]); } String S; public Sol() { S = rs(); } 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)); } } struct mlong { const long mod = (long)1e9 + 7; long V; public mlong(long _v = 0) { V = _v; } public static mlong operator +(mlong a, mlong b) { var v0 = a.V + b.V; if (v0 >= mod) v0 -= mod; return new mlong(v0); } public static mlong operator -(mlong a, mlong b) { var v0 = mod + a.V - b.V; if (v0 >= mod) v0 -= mod; return new mlong(v0); } public static mlong operator *(mlong a, mlong b) { var v0 = a.V * b.V; if (v0 >= mod) v0 %= mod; return new mlong(v0); } public static mlong operator /(mlong a, mlong b) { var v0 = a.V * inv(b.V).V; if (v0 >= mod) v0 %= mod; return new mlong(v0); } public static mlong inv(long x) { long a = 0, b = 0, c = 0; ExtGCD(x, mod, ref a, ref b, ref c); return (mlong)((a + mod) % mod); } public static void ExtGCD(long x, long y, ref long a, ref long b, ref long c) { long r0 = x; long r1 = y; long a0 = 1; long a1 = 0; long b0 = 0; long b1 = 1; long q1, r2, a2, b2; while (r1 > 0) { q1 = r0 / r1; r2 = r0 % r1; a2 = a0 - q1 * a1; b2 = b0 - q1 * b1; r0 = r1; r1 = r2; a0 = a1; a1 = a2; b0 = b1; b1 = b2; } c = r0; a = a0; b = b0; } public static mlong ModPow(mlong a, long k) { if (k == 0) return (mlong)1; if (a == 0) return (mlong)0; mlong x = a; mlong ret = 1; while (k > 0) { if (k % 2 == 1) ret *= x; x *= x; k >>= 1; } return ret; } public static bool operator ==(mlong a, mlong b) { return a.Equals(b); } public static bool operator !=(mlong a, mlong b) { return !(a == b); } public override bool Equals(System.Object obj) { if (obj == null) return false; mlong p = (mlong)obj; if ((System.Object)p == null) return false; return p.V == V; } public override int GetHashCode() { return V.GetHashCode(); } public static implicit operator mlong(long n) { long v = n % mod; if (v < 0) v += mod; return new mlong(v); } public static implicit operator mlong(int n) { long v = n % mod; if (v < 0) v += mod; return new mlong(v); } public static explicit operator long(mlong n) { return n.V; } public override String ToString() { return V.ToString(); } }
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() { var ca = S.ToCharArray(); Array.Reverse(ca); int M = 13; int N = S.Length; mlong[] dp = new mlong[M]; int dig = 1; dp[0] = 1; for (int i = 0; i < N; i++) { var ndp = new mlong[M]; if (ca[i] == '?') { for (int j = 0; j < M; j++) { for (int k = 0; k < 10; k++) { int nj = (dig * k + j) % M; ndp[nj] += dp[j]; } } } else { for (int j = 0; j < M; j++) { for (int k = 0; k < 10; k++) { if (ca[i] - '0' != k) continue; int nj = (dig * k + j) % M; ndp[nj] += dp[j]; } } } dig *= 10; dig %= M; dp = ndp; // Console.WriteLine(String.Join(" ",dp)); } Console.WriteLine(dp[5]); } String S; public Sol() { S = rs(); } 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)); } } struct mlong { const long mod = (long)1e9 + 7; long V; public mlong(long _v = 0) { V = _v; } public static mlong operator +(mlong a, mlong b) { var v0 = a.V + b.V; if (v0 >= mod) v0 -= mod; return new mlong(v0); } public static mlong operator -(mlong a, mlong b) { var v0 = mod + a.V - b.V; if (v0 >= mod) v0 -= mod; return new mlong(v0); } public static mlong operator *(mlong a, mlong b) { var v0 = a.V * b.V; if (v0 >= mod) v0 %= mod; return new mlong(v0); } public static mlong operator /(mlong a, mlong b) { var v0 = a.V * inv(b.V).V; if (v0 >= mod) v0 %= mod; return new mlong(v0); } public static mlong inv(long x) { long a = 0, b = 0, c = 0; ExtGCD(x, mod, ref a, ref b, ref c); return (mlong)((a + mod) % mod); } public static void ExtGCD(long x, long y, ref long a, ref long b, ref long c) { long r0 = x; long r1 = y; long a0 = 1; long a1 = 0; long b0 = 0; long b1 = 1; long q1, r2, a2, b2; while (r1 > 0) { q1 = r0 / r1; r2 = r0 % r1; a2 = a0 - q1 * a1; b2 = b0 - q1 * b1; r0 = r1; r1 = r2; a0 = a1; a1 = a2; b0 = b1; b1 = b2; } c = r0; a = a0; b = b0; } public static mlong ModPow(mlong a, long k) { if (k == 0) return (mlong)1; if (a == 0) return (mlong)0; mlong x = a; mlong ret = 1; while (k > 0) { if (k % 2 == 1) ret *= x; x *= x; k >>= 1; } return ret; } public static bool operator ==(mlong a, mlong b) { return a.Equals(b); } public static bool operator !=(mlong a, mlong b) { return !(a == b); } public override bool Equals(System.Object obj) { if (obj == null) return false; mlong p = (mlong)obj; if ((System.Object)p == null) return false; return p.V == V; } public override int GetHashCode() { return V.GetHashCode(); } public static implicit operator mlong(long n) { long v = n % mod; if (v < 0) v += mod; return new mlong(v); } public static implicit operator mlong(int n) { long v = n % mod; if (v < 0) v += mod; return new mlong(v); } public static explicit operator long(mlong n) { return n.V; } public override String ToString() { return V.ToString(); } }
[["-", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["-", 0, 57, 15, 16, 31, 16, 31, 204, 205, 22], ["+", 0, 57, 15, 16, 31, 16, 31, 204, 205, 22]]
8
1,220
4
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Convert; using static System.Math; //using static System.Globalization.CultureInfo; using System.Text; class Program { private static void chmin<T>(ref T num, T val) where T : IComparable<T> => num = num.CompareTo(val) == 1 ? val : num; private static void chmax<T>(ref T num, T val) where T : IComparable<T> => num = num.CompareTo(val) == -1 ? val : num; static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); //WriteLine(Solve()); } static void Solve() { var str = new string(ReadLine().Reverse().ToArray()); var dp = new ModInt[13]; dp[0] = 1; var p=(ModInt)1; for(var i=0;i<str.Length;i++) { var newdp = new ModInt[13]; if (str[i] != '?') { for (var j = 0; j < 13; j++) newdp[(j + p * (str[i] - '0') % 13) % 13] += dp[j]; dp = newdp; p *= 10;continue; } for (var k = 0; k < 10; k++) for (var j = 0; j < 13; j++) newdp[(j + k * p % 13) % 13] += dp[j]; p *= 10; p %= 13; dp = newdp; } WriteLine(dp[5]); } } public struct ModInt { public const long MOD = (int)1e9 + 7; //public const long MOD = 998244353; public long num { get; set; } public ModInt(long n = 0) { num = n; } private static ModInt[] _fac;//階乗 private static ModInt[] _inv;//逆数 private static ModInt[] _facrev;//1/(i!) public override string ToString() => 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) => new ModInt(l.num * r.num % MOD); public static ModInt operator /(ModInt l, ModInt r) => l * Pow(r, MOD - 2); public static implicit operator long(ModInt l) => l.num; public static implicit operator ModInt(long n) { n %= MOD; if (n < 0) n += MOD; return new ModInt(n); } public static ModInt Pow(ModInt m,long n) { if (n == 0) return 1; if (n % 2 == 0) return Pow(m * m, n >> 1); else return Pow(m * m, n >> 1) * m; } public static void CombBuild(int n) { _fac = new ModInt[n + 1]; _facrev = new ModInt[n + 1]; _inv = new ModInt[n + 1]; _inv[1] = 1; _fac[0] = _fac[1] = 1; _facrev[0] = _facrev[1] = 1; for (var i = 2; i <= n; i++) { _fac[i] = _fac[i - 1] * i; _inv[i] = MOD - _inv[MOD % i] * (MOD / i); _facrev[i] = _facrev[i - 1] * _inv[i]; } } public static ModInt Fac(ModInt n) => _fac[n]; public static ModInt Div(ModInt n) => _inv[n]; public static ModInt FacRev(ModInt n) => _facrev[n]; public static ModInt Part(ModInt n,ModInt r) { if (n < r) return 0; if (n == r) return 1; return _fac[n] * _facrev[r]; } public static ModInt Comb(ModInt n,ModInt r) { if (n < r) return 0; if (n == r) return 1; var calc = _fac[n]; calc = calc * _facrev[r]; calc = calc * _facrev[n - r]; return calc; } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Convert; using static System.Math; //using static System.Globalization.CultureInfo; using System.Text; class Program { private static void chmin<T>(ref T num, T val) where T : IComparable<T> => num = num.CompareTo(val) == 1 ? val : num; private static void chmax<T>(ref T num, T val) where T : IComparable<T> => num = num.CompareTo(val) == -1 ? val : num; static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); //WriteLine(Solve()); } static void Solve() { var str = new string(ReadLine().Reverse().ToArray()); var dp = new ModInt[13]; dp[0] = 1; var p=(ModInt)1; for(var i=0;i<str.Length;i++) { var newdp = new ModInt[13]; if (str[i] != '?') { for (var j = 0; j < 13; j++) newdp[(j + p * (str[i] - '0') % 13) % 13] += dp[j]; dp = newdp; p *= 10;p%=13;continue; } for (var k = 0; k < 10; k++) for (var j = 0; j < 13; j++) newdp[(j + k * p % 13) % 13] += dp[j]; p *= 10; p %= 13; dp = newdp; } WriteLine(dp[5]); } } public struct ModInt { public const long MOD = (int)1e9 + 7; //public const long MOD = 998244353; public long num { get; set; } public ModInt(long n = 0) { num = n; } private static ModInt[] _fac;//階乗 private static ModInt[] _inv;//逆数 private static ModInt[] _facrev;//1/(i!) public override string ToString() => 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) => new ModInt(l.num * r.num % MOD); public static ModInt operator /(ModInt l, ModInt r) => l * Pow(r, MOD - 2); public static implicit operator long(ModInt l) => l.num; public static implicit operator ModInt(long n) { n %= MOD; if (n < 0) n += MOD; return new ModInt(n); } public static ModInt Pow(ModInt m,long n) { if (n == 0) return 1; if (n % 2 == 0) return Pow(m * m, n >> 1); else return Pow(m * m, n >> 1) * m; } public static void CombBuild(int n) { _fac = new ModInt[n + 1]; _facrev = new ModInt[n + 1]; _inv = new ModInt[n + 1]; _inv[1] = 1; _fac[0] = _fac[1] = 1; _facrev[0] = _facrev[1] = 1; for (var i = 2; i <= n; i++) { _fac[i] = _fac[i - 1] * i; _inv[i] = MOD - _inv[MOD % i] * (MOD / i); _facrev[i] = _facrev[i - 1] * _inv[i]; } } public static ModInt Fac(ModInt n) => _fac[n]; public static ModInt Div(ModInt n) => _inv[n]; public static ModInt FacRev(ModInt n) => _facrev[n]; public static ModInt Part(ModInt n,ModInt r) { if (n < r) return 0; if (n == r) return 1; return _fac[n] * _facrev[r]; } public static ModInt Comb(ModInt n,ModInt r) { if (n < r) return 0; if (n == r) return 1; var calc = _fac[n]; calc = calc * _facrev[r]; calc = calc * _facrev[n - r]; return calc; } }
[["+", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 132], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 203], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35]]
8
979
4
using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; // using System.Threading.Tasks; // using System.Text.RegularExpressions; // using System.Globalization; // using System.Diagnostics; using static System.Console; using System.Numerics; using static System.Math; using pair = Pair<long, long>; class Program { static void Main() { // SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); new Program().solve(); Out.Flush(); } readonly Scanner cin = new Scanner(); readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑ readonly int mod = 1000000007; readonly int dom = 998244353; bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; } void solve() { var S = ReadLine(); var T = ReadLine(); int N = S.Length; int M = T.Length; while (!(N + M - 1 <= S.Length)) S += S; var B = new RollingHash(T); var hash = new pair(B[0, M, 0], B[0, M, 1]); var A = new RollingHash(S); var ok = new bool[N]; for (int i = 0; i < N; i++) { ok[i] = new pair(A[i, i + M, 0], A[i, i + M, 1]) == hash; } // ok.join(); var U = new DisjointSet(N); long ans = 0; for (int i = 0; i < N; i++) { if (ok[i]) { chmax(ref ans, 1); } if (ok[i] && ok[(i + M) % N]) { if (!U.Unite(i, (i + M) % N)) { WriteLine(-1); return; } chmax(ref ans, U.Size(i)); } } WriteLine(ans); } } class RollingHash { const int BASE = 9973; readonly int[] mod = new[] { 999999937, 1000000007 }; readonly long[][] hash, power; public RollingHash(string s) { hash = new long [2][]; power = new long [2][]; for (int iter = 0; iter < mod.Length; iter++) { hash[iter] = new long[s.Length + 1]; power[iter] = new long[s.Length + 1]; power[iter][0] = 1; for (int i = 0; i < s.Length; i++) { hash[iter][i + 1] = (hash[iter][i] * BASE + s[i]) % mod[iter]; power[iter][i + 1] = power[iter][i] * BASE % mod[iter]; } } } /// <summary> /// [l, r)のハッシュ値 0-indexed /// </summary> /// <param name="l"></param> /// <param name="r"></param> /// <param name="id"></param> /// <returns></returns> public long this[int l, int r, int id = 0] { get { long ret = hash[id][r] - hash[id][l] * power[id][r - l] % mod[id]; if (ret < 0) ret += mod[id]; return ret; } } } /// <summary> /// 高速だが、悪意のあるケースでハッシュ衝突 /// </summary> class RollingHash64 { const int BASE = 9973; readonly long[] hash, power; public RollingHash64(string s) { hash = new long[s.Length + 1]; power = new long[s.Length + 1]; power[0] = 1; for (int i = 0; i < s.Length; i++) { hash[i + 1] = hash[i] * BASE + s[i]; power[i + 1] = power[i] * BASE; } } /// <summary> /// [l, r)のハッシュ値 0-indexed /// </summary> /// <param name="l"></param> /// <param name="r"></param> /// <returns></returns> public long this[int l, int r] => hash[r] - hash[l] * power[r - l]; } /// <OriginalAuthor>camypaper</OriginalAuthor> class DisjointSet { readonly int[] par; readonly byte[] rank; /// <summary> /// 0 から <paramref name="N"/>-1 までの番号がついた <paramref name="N"/> /// 個の要素からなる集合を作成します. /// </summary> /// <param name="N">要素数</param> /// <remarks>このコンストラクタは O(N) で実行されます.</remarks> public DisjointSet(int N) { par = new int[N]; rank = new byte[N]; for (int i = 0; i < N; i++) par[i] = -1; } /// <summary> /// 指定した要素が属する集合の代表値を取得します. /// </summary> /// <param name="id">調べたい要素の 0-indexed での番号</param> /// <returns>指定した要素が属する集合の代表値</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public int this[int id] { get { if (par[id] < 0) return id; return par[id] = this[par[id]]; } } /// <summary> /// 指定した 2 つの要素が属する集合同士を 1 つに統合することを試みます. /// </summary> /// <param name="x">最初の要素の 0-indexed での番号</param> /// <param name="y">2 つ目の要素の 0-indexed での番号</param> /// <returns>統合に成功したならば true,そうでなければ false.</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public bool Unite(int x, int y) { x = this[x]; y = this[y]; if (x == y) return false; if (rank[x] < rank[y]) { var tmp = x; x = y; y = tmp; } par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; return true; } /// <summary> /// 指定した要素が属する集合のサイズを取得します. /// </summary> /// <param name="x">指定する要素の 0-indexed での番号</param> /// <returns>集合のサイズ</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public int Size(int x) => -par[this[x]]; /// <summary> /// 指定した 2 つの要素が属する集合が同じかどうかを判定する.計算量 O(α(N)). /// </summary> /// <param name="x">最初の要素の 0-indexed での番号</param> /// <param name="y">2 つ目の要素の 0-indexed での番号</param> /// <returns>同じならば true,そうでなければ false.</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public bool Same(int x, int y) => this[x] == this[y]; } static class Ex { public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values)); public static string concat<T>(this IEnumerable<T> values) => string.Concat(values); public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); } public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) < 0) low = mid + 1; else high = mid; } return low; } public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) <= 0) low = mid + 1; else high = mid; } return low; } } class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T f; public U s; public Pair(T f, U s) { this.f = f; this.s = s; } public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s); public override string ToString() => $"{f} {s}"; } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan => ReadLine().Split(); public int[] scanint => Array.ConvertAll(scan, int.Parse); public long[] scanlong => Array.ConvertAll(scan, long.Parse); public double[] scandouble => Array.ConvertAll(scan, double.Parse); public string next { get { if (i < s.Length) return s[i++]; string st = ReadLine(); while (st == "") st = ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint => int.Parse(next); public long nextlong => long.Parse(next); public double nextdouble => double.Parse(next); }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; // using System.Threading.Tasks; // using System.Text.RegularExpressions; // using System.Globalization; // using System.Diagnostics; using static System.Console; using System.Numerics; using static System.Math; using pair = Pair<long, long>; class Program { static void Main() { // SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); new Program().solve(); Out.Flush(); } readonly Scanner cin = new Scanner(); readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑ readonly int mod = 1000000007; readonly int dom = 998244353; bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; } void solve() { var S = ReadLine(); var T = ReadLine(); int N = S.Length; int M = T.Length; while (!(N + M - 1 <= S.Length)) S += S; var B = new RollingHash(T); var hash = new pair(B[0, M, 0], B[0, M, 1]); var A = new RollingHash(S); var ok = new bool[N]; for (int i = 0; i < N; i++) { ok[i] = new pair(A[i, i + M, 0], A[i, i + M, 1]).CompareTo(hash) == 0; } // ok.join(); var U = new DisjointSet(N); long ans = 0; for (int i = 0; i < N; i++) { if (ok[i]) { chmax(ref ans, 1); } if (ok[i] && ok[(i + M) % N]) { if (!U.Unite(i, (i + M) % N)) { WriteLine(-1); return; } chmax(ref ans, U.Size(i)); } } WriteLine(ans); } } class RollingHash { const int BASE = 9973; readonly int[] mod = new[] { 999999937, 1000000007 }; readonly long[][] hash, power; public RollingHash(string s) { hash = new long [2][]; power = new long [2][]; for (int iter = 0; iter < mod.Length; iter++) { hash[iter] = new long[s.Length + 1]; power[iter] = new long[s.Length + 1]; power[iter][0] = 1; for (int i = 0; i < s.Length; i++) { hash[iter][i + 1] = (hash[iter][i] * BASE + s[i]) % mod[iter]; power[iter][i + 1] = power[iter][i] * BASE % mod[iter]; } } } /// <summary> /// [l, r)のハッシュ値 0-indexed /// </summary> /// <param name="l"></param> /// <param name="r"></param> /// <param name="id"></param> /// <returns></returns> public long this[int l, int r, int id = 0] { get { long ret = hash[id][r] - hash[id][l] * power[id][r - l] % mod[id]; if (ret < 0) ret += mod[id]; return ret; } } } /// <summary> /// 高速だが、悪意のあるケースでハッシュ衝突 /// </summary> class RollingHash64 { const int BASE = 9973; readonly long[] hash, power; public RollingHash64(string s) { hash = new long[s.Length + 1]; power = new long[s.Length + 1]; power[0] = 1; for (int i = 0; i < s.Length; i++) { hash[i + 1] = hash[i] * BASE + s[i]; power[i + 1] = power[i] * BASE; } } /// <summary> /// [l, r)のハッシュ値 0-indexed /// </summary> /// <param name="l"></param> /// <param name="r"></param> /// <returns></returns> public long this[int l, int r] => hash[r] - hash[l] * power[r - l]; } /// <OriginalAuthor>camypaper</OriginalAuthor> class DisjointSet { readonly int[] par; readonly byte[] rank; /// <summary> /// 0 から <paramref name="N"/>-1 までの番号がついた <paramref name="N"/> /// 個の要素からなる集合を作成します. /// </summary> /// <param name="N">要素数</param> /// <remarks>このコンストラクタは O(N) で実行されます.</remarks> public DisjointSet(int N) { par = new int[N]; rank = new byte[N]; for (int i = 0; i < N; i++) par[i] = -1; } /// <summary> /// 指定した要素が属する集合の代表値を取得します. /// </summary> /// <param name="id">調べたい要素の 0-indexed での番号</param> /// <returns>指定した要素が属する集合の代表値</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public int this[int id] { get { if (par[id] < 0) return id; return par[id] = this[par[id]]; } } /// <summary> /// 指定した 2 つの要素が属する集合同士を 1 つに統合することを試みます. /// </summary> /// <param name="x">最初の要素の 0-indexed での番号</param> /// <param name="y">2 つ目の要素の 0-indexed での番号</param> /// <returns>統合に成功したならば true,そうでなければ false.</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public bool Unite(int x, int y) { x = this[x]; y = this[y]; if (x == y) return false; if (rank[x] < rank[y]) { var tmp = x; x = y; y = tmp; } par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; return true; } /// <summary> /// 指定した要素が属する集合のサイズを取得します. /// </summary> /// <param name="x">指定する要素の 0-indexed での番号</param> /// <returns>集合のサイズ</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public int Size(int x) => -par[this[x]]; /// <summary> /// 指定した 2 つの要素が属する集合が同じかどうかを判定する.計算量 O(α(N)). /// </summary> /// <param name="x">最初の要素の 0-indexed での番号</param> /// <param name="y">2 つ目の要素の 0-indexed での番号</param> /// <returns>同じならば true,そうでなければ false.</returns> /// <remarks>最悪計算量 O(α(N)) で実行されます.</remarks> public bool Same(int x, int y) => this[x] == this[y]; } static class Ex { public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values)); public static string concat<T>(this IEnumerable<T> values) => string.Concat(values); public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); } public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) < 0) low = mid + 1; else high = mid; } return low; } public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) <= 0) low = mid + 1; else high = mid; } return low; } } class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T f; public U s; public Pair(T f, U s) { this.f = f; this.s = s; } public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s); public override string ToString() => $"{f} {s}"; } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan => ReadLine().Split(); public int[] scanint => Array.ConvertAll(scan, int.Parse); public long[] scanlong => Array.ConvertAll(scan, long.Parse); public double[] scandouble => Array.ConvertAll(scan, double.Parse); public string next { get { if (i < s.Length) return s[i++]; string st = ReadLine(); while (st == "") st = ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint => int.Parse(next); public long nextlong => long.Parse(next); public double nextdouble => double.Parse(next); }
[["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 60], ["+", 0, 11, 12, 16, 31, 213, 63, 214, 0, 131], ["+", 0, 11, 12, 16, 31, 213, 63, 214, 141, 22], ["+", 0, 11, 12, 16, 31, 213, 3, 4, 0, 24], ["+", 0, 11, 12, 16, 31, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 60], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
1,784
7
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program { const int mod = (int)1e9 + 7; static void Main() { Sc sc = new Sc(); var s = sc.La; int n = (int)s[0]; int[] a = sc.Ia; int[] k = new int[200001]; int[] g = Enumerable.Repeat(-1, n).ToArray(); int[] h = Enumerable.Repeat(-1, 200001).ToArray(); for (int i = n - 1; i >= 0; i--) { if (h[a[i]] != -1) { g[i] = h[a[i]]; } h[a[i]] = i; } for (int i = n - 1; i >= 0; i--) { if (g[i] == -1) { g[i] = h[a[i]]; } } int p = 0, b = 0; while (p < s[1]) { if (g[b] <= b) { p++; } b = (g[b] + 1) % n; if (b == 0) { p++; } k[p] = b; if (b == 0) { break; } } s[1]--; if (p != s[1]) { s[1] %= p; } var avl = new Avl(); var bb = new bool[200001]; return; for (int i = k[s[1]]; i < n; i++) { if (!bb[a[i]]) { avl.Ud(i, a[i]); bb[a[i]] = true; } else { var q = avl.Ra(avl.cnt); while (q != null) { bb[q.d] = false; avl.Dl(q.n); if (q.d == a[i]) { break; } q = avl.Ra(avl.cnt); } } } StringBuilder sb = new StringBuilder(); while (avl.cnt > 0) { var q = avl.Ra(1); sb.Append(avl.Ra(1).d); if (avl.cnt != 1) { sb.Append(" "); } else { sb.Append("\n"); } avl.Dl(q.n); } Console.Write(sb); } } public class Avl { public class Nd { public int h = 1, c = 1; public readonly int n; public int d, ds; public Nd l, r; public Nd(int n, int d, Nd l, Nd r) { this.n = n; this.d = d; ds = d; this.l = l; this.r = r; } } public Nd root, du; public int cnt = 0; public Avl() { du = new Nd(int.MinValue, 0, du, du); du.h = 0; du.c = 0; root = du; } public void Ud(int n, int d) { Nd mn = new Nd(n, d, du, du); if (root == du) { root = mn; } else { Fu(root, n, mn); if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } } cnt++; } private void Fu(Nd t, int n, Nd mn) { t.c++; t.ds += mn.d; if (t.n > n) { if (t.l != du) { Fu(t.l, n, mn); if (Abs(t.l.l.h - t.l.r.h) > 1) { t.l = Rotate(t.l); } } else { t.l = mn; } } else { if (t.r != du) { Fu(t.r, n, mn); if (Abs(t.r.l.h - t.r.r.h) > 1) { t.r = Rotate(t.r); } } else { t.r = mn; } } t.h = Max(t.l.h, t.r.h) + 1; } private Nd Rotate(Nd t) { Nd nd = du; if (t.l.h > t.r.h) { if (t.l.l.h > t.l.r.h) { nd = t.l; t.l = t.l.r; nd.r = t; Ft(t); } else { nd = t.l.r; t.l.r = nd.l; nd.l = t.l; Ft(t.l); t.l = nd.r; nd.r = t; Ft(t); } } else { if (t.r.l.h > t.r.r.h) { nd = t.r.l; t.r.l = nd.r; nd.r = t.r; Ft(t.r); t.r = nd.l; nd.l = t; Ft(t); } else { nd = t.r; t.r = t.r.l; nd.l = t; Ft(t); } } Ft(nd); return nd; } private void Ft(Nd t) { t.h = Max(t.l.h, t.r.h) + 1; t.c = t.l.c + t.r.c + 1; t.ds = t.l.ds + t.r.ds + t.d; } public bool Dl(int n) { Nd t = root; if (t.n == n) { root = Fd2(root); if (cnt == 0) { return true; } else if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } else { Ft(root); } return true; } bool bo = Fd1(root, n); if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } else { Ft(root); } return bo; } private bool Fd1(Nd t, int n) { if (t.n > n) { if (t.l == du) { return false; } else if (t.l.n != n) { bool bo = Fd1(t.l, n); if (Abs(t.l.l.h - t.l.r.h) > 1) { t.l = Rotate(t.l); } else { Ft(t.l); } return bo; } else { t.l = Fd2(t.l); Ft(t); return true; } } else { if (t.r == du) { return false; } else if (t.r.n != n) { bool bo = Fd1(t.r, n); if (Abs(t.r.l.h - t.r.r.h) > 1) { t.r = Rotate(t.r); } else { Ft(t.r); } return bo; } else { t.r = Fd2(t.r); Ft(t); return true; } } } private Nd Fd2(Nd dn) { cnt--; if (dn.l == du) { return dn.r; } if (dn.r == du) { return dn.l; } Nd u = dn.l; if (u.r == du) { u.r = dn.r; Ft(u); return u; } u = Fd3(u, dn); if (Abs(dn.l.l.h - dn.l.r.h) > 1) { u.l = Rotate(dn.l); } else { Ft(dn.l); } Ft(u); return u; } private Nd Fd3(Nd u, Nd dn) { if (u.r.r != du) { Nd v = Fd3(u.r, dn); if (Abs(u.r.l.h - u.r.r.h) > 1) { u.r = Rotate(u.r); } else { Ft(u.r); } return v; } else { Nd v = u.r; u.r = u.r.l; v.l = dn.l; v.r = dn.r; return v; } } public Nd Ra(int n) { return cnt >= n ? Fr(root, n) : null; } private Nd Fr(Nd t, int n) { if (t.l.c > n - 1) { return Fr(t.l, n); } if (t.l.c < n - 1) { return Fr(t.r, n - t.l.c - 1); } return t; } } public class Sc { public int I { get { return int.Parse(Console.ReadLine()); } } public long L { get { return long.Parse(Console.ReadLine()); } } public double D { get { return double.Parse(Console.ReadLine()); } } public string S { get { return Console.ReadLine(); } } public int[] Ia { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } public long[] La { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } } public double[] Da { get { return Array.ConvertAll(Console.ReadLine().Split(), double.Parse); } } public string[] Sa { get { return Console.ReadLine().Split(); } } public object[] Oa { get { return Console.ReadLine().Split(); } } public int[] Ia2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), int.Parse); } } public int[] Ia3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), int.Parse); } public int[] Ia3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), int.Parse); } public long[] La2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), long.Parse); } } public long[] La3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), long.Parse); } public long[] La3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), long.Parse); } public T[] Arr<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(); } return a; } public T[] Arr<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i); } return a; } public T[] Arr<T>(int n, Func<string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(Console.ReadLine().Split()); } return a; } public T[] Arr<T>(int n, Func<int, string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i, Console.ReadLine().Split()); } return a; } }
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program { const int mod = (int)1e9 + 7; static void Main() { Sc sc = new Sc(); var s = sc.La; int n = (int)s[0]; int[] a = sc.Ia; int[] k = new int[200002]; int[] g = Enumerable.Repeat(-1, n).ToArray(); int[] h = Enumerable.Repeat(-1, 200001).ToArray(); for (int i = n - 1; i >= 0; i--) { if (h[a[i]] != -1) { g[i] = h[a[i]]; } h[a[i]] = i; } for (int i = n - 1; i >= 0; i--) { if (g[i] == -1) { g[i] = h[a[i]]; } } int p = 0, b = 0; while (p < s[1]) { if (g[b] <= b) { p++; } b = (g[b] + 1) % n; if (b == 0) { p++; } k[p] = b; if (b == 0) { break; } } s[1]--; if (p != s[1]) { s[1] %= p; } var avl = new Avl(); var bb = new bool[200001]; for (int i = k[s[1]]; i < n; i++) { if (!bb[a[i]]) { avl.Ud(i, a[i]); bb[a[i]] = true; } else { var q = avl.Ra(avl.cnt); while (q != null) { bb[q.d] = false; avl.Dl(q.n); if (q.d == a[i]) { break; } q = avl.Ra(avl.cnt); } } } StringBuilder sb = new StringBuilder(); while (avl.cnt > 0) { var q = avl.Ra(1); sb.Append(avl.Ra(1).d); if (avl.cnt != 1) { sb.Append(" "); } else { sb.Append("\n"); } avl.Dl(q.n); } Console.Write(sb); } } public class Avl { public class Nd { public int h = 1, c = 1; public readonly int n; public int d, ds; public Nd l, r; public Nd(int n, int d, Nd l, Nd r) { this.n = n; this.d = d; ds = d; this.l = l; this.r = r; } } public Nd root, du; public int cnt = 0; public Avl() { du = new Nd(int.MinValue, 0, du, du); du.h = 0; du.c = 0; root = du; } public void Ud(int n, int d) { Nd mn = new Nd(n, d, du, du); if (root == du) { root = mn; } else { Fu(root, n, mn); if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } } cnt++; } private void Fu(Nd t, int n, Nd mn) { t.c++; t.ds += mn.d; if (t.n > n) { if (t.l != du) { Fu(t.l, n, mn); if (Abs(t.l.l.h - t.l.r.h) > 1) { t.l = Rotate(t.l); } } else { t.l = mn; } } else { if (t.r != du) { Fu(t.r, n, mn); if (Abs(t.r.l.h - t.r.r.h) > 1) { t.r = Rotate(t.r); } } else { t.r = mn; } } t.h = Max(t.l.h, t.r.h) + 1; } private Nd Rotate(Nd t) { Nd nd = du; if (t.l.h > t.r.h) { if (t.l.l.h > t.l.r.h) { nd = t.l; t.l = t.l.r; nd.r = t; Ft(t); } else { nd = t.l.r; t.l.r = nd.l; nd.l = t.l; Ft(t.l); t.l = nd.r; nd.r = t; Ft(t); } } else { if (t.r.l.h > t.r.r.h) { nd = t.r.l; t.r.l = nd.r; nd.r = t.r; Ft(t.r); t.r = nd.l; nd.l = t; Ft(t); } else { nd = t.r; t.r = t.r.l; nd.l = t; Ft(t); } } Ft(nd); return nd; } private void Ft(Nd t) { t.h = Max(t.l.h, t.r.h) + 1; t.c = t.l.c + t.r.c + 1; t.ds = t.l.ds + t.r.ds + t.d; } public bool Dl(int n) { Nd t = root; if (t.n == n) { root = Fd2(root); if (cnt == 0) { return true; } else if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } else { Ft(root); } return true; } bool bo = Fd1(root, n); if (Abs(root.l.h - root.r.h) > 1) { root = Rotate(root); } else { Ft(root); } return bo; } private bool Fd1(Nd t, int n) { if (t.n > n) { if (t.l == du) { return false; } else if (t.l.n != n) { bool bo = Fd1(t.l, n); if (Abs(t.l.l.h - t.l.r.h) > 1) { t.l = Rotate(t.l); } else { Ft(t.l); } return bo; } else { t.l = Fd2(t.l); Ft(t); return true; } } else { if (t.r == du) { return false; } else if (t.r.n != n) { bool bo = Fd1(t.r, n); if (Abs(t.r.l.h - t.r.r.h) > 1) { t.r = Rotate(t.r); } else { Ft(t.r); } return bo; } else { t.r = Fd2(t.r); Ft(t); return true; } } } private Nd Fd2(Nd dn) { cnt--; if (dn.l == du) { return dn.r; } if (dn.r == du) { return dn.l; } Nd u = dn.l; if (u.r == du) { u.r = dn.r; Ft(u); return u; } u = Fd3(u, dn); if (Abs(dn.l.l.h - dn.l.r.h) > 1) { u.l = Rotate(dn.l); } else { Ft(dn.l); } Ft(u); return u; } private Nd Fd3(Nd u, Nd dn) { if (u.r.r != du) { Nd v = Fd3(u.r, dn); if (Abs(u.r.l.h - u.r.r.h) > 1) { u.r = Rotate(u.r); } else { Ft(u.r); } return v; } else { Nd v = u.r; u.r = u.r.l; v.l = dn.l; v.r = dn.r; return v; } } public Nd Ra(int n) { return cnt >= n ? Fr(root, n) : null; } private Nd Fr(Nd t, int n) { if (t.l.c > n - 1) { return Fr(t.l, n); } if (t.l.c < n - 1) { return Fr(t.r, n - t.l.c - 1); } return t; } } public class Sc { public int I { get { return int.Parse(Console.ReadLine()); } } public long L { get { return long.Parse(Console.ReadLine()); } } public double D { get { return double.Parse(Console.ReadLine()); } } public string S { get { return Console.ReadLine(); } } public int[] Ia { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } public long[] La { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } } public double[] Da { get { return Array.ConvertAll(Console.ReadLine().Split(), double.Parse); } } public string[] Sa { get { return Console.ReadLine().Split(); } } public object[] Oa { get { return Console.ReadLine().Split(); } } public int[] Ia2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), int.Parse); } } public int[] Ia3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), int.Parse); } public int[] Ia3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), int.Parse); } public long[] La2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), long.Parse); } } public long[] La3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), long.Parse); } public long[] La3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), long.Parse); } public T[] Arr<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(); } return a; } public T[] Arr<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i); } return a; } public T[] Arr<T>(int n, Func<string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(Console.ReadLine().Split()); } return a; } public T[] Arr<T>(int n, Func<int, string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i, Console.ReadLine().Split()); } return a; } }
[["-", 0, 212, 0, 227, 39, 224, 225, 226, 0, 203], ["+", 0, 212, 0, 227, 39, 224, 225, 226, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 37, 0, 38]]
8
2,832
4
using System; using CompLib.Mathematics; using CompLib.Util; public class Program { private int N, M; private ModInt[] F; public void Solve() { var sc = new Scanner(); F = new ModInt[1000001]; F[0] = 1; for (int i = 1; i <= 1000000; i++) { F[i] = F[i - 1] * i; } N = sc.NextInt(); M = sc.NextInt(); // 総和 3M -a // 最大 2M以下 -b // 奇数の個数 M以下 パリティ一致 -c // -aは自明 // M=0のとき、b,c満たす // M=kのとき満たしている、 // M=k+1でも満たす // a,cのみのパターン ModInt ans = 0; for (int i = M; i >= 0; i -= 2) { // i 奇数の個数 // 奇数のところにはあらかじめ1を置いておく if (i > N) continue; // 並べ方 N C i var a = C(N, i); // 残り 3M - i個 偶数になるように分ける var b = H(N, (3 * M - i) / 2); ans += a * b; } // bを満たさないパターン // 最大が2M超過 for (int max = 2 * M + 1; max <= 3 * M; max++) { // 残り3M-max並べ方 // M以下なのでcは満たす var c = H(N - 1, 3 * M - max); ans -= c * N; } Console.WriteLine(ans); } ModInt C(int n, int m) { return F[n] * ModInt.Inverse(F[n - m] * F[m]); } // n種類、r個選ぶ ModInt H(int n, int r) { return C(n + r - 1, r); } public static void Main(string[] args) => new Program().Solve(); } // https://bitbucket.org/camypaper/complib namespace CompLib.Mathematics { #region ModInt /// <summary> /// [0,<see cref="Mod"/>) までの値を取るような数 /// </summary> public struct ModInt { /// <summary> /// 剰余を取る値. /// </summary> // public const long Mod = (int) 1e9 + 7; public const long Mod = 998244353; /// <summary> /// 実際の数値. /// </summary> public long num; /// <summary> /// 値が <paramref name="n"/> であるようなインスタンスを構築します. /// </summary> /// <param name="n">インスタンスが持つ値</param> /// <remarks>パフォーマンスの問題上,コンストラクタ内では剰余を取りません.そのため,<paramref /// name="n"/> ∈ [0,<see cref="Mod"/>) を満たすような <paramref name="n"/> /// を渡してください.このコンストラクタは O(1) で実行されます.</remarks> public ModInt(long n) { num = n; } /// <summary> /// このインスタンスの数値を文字列に変換します. /// </summary> /// <returns>[0,<see cref="Mod"/>) の範囲内の整数を 10 /// 進表記したもの.</returns> 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 implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } /// <summary> /// 与えられた 2 つの数値からべき剰余を計算します. /// </summary> /// <param name="v">べき乗の底</param> /// <param name="k">べき指数</param> /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns> public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } /// <summary> /// 与えられた 2 つの数値からべき剰余を計算します. /// </summary> /// <param name="v">べき乗の底</param> /// <param name="k">べき指数</param> /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns> 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); } /// <summary> /// 与えられた数の逆元を計算します. /// </summary> /// <param name="v">逆元を取る対象となる数</param> /// <returns>逆元となるような値</returns> /// <remarks>法が素数であることを仮定して,フェルマーの小定理に従って逆元を /// O(log N) で計算します.</remarks> public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } #endregion #region Binomial Coefficient public class BinomialCoefficient { public ModInt[] fact, ifact; public BinomialCoefficient(int n) { fact = new ModInt[n + 1]; ifact = new ModInt[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = ModInt.Inverse(fact[n]); for (int i = n - 1; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); ifact[0] = ifact[1]; } public ModInt this[int n, int r] { get { if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0; return fact[n] * ifact[n - r] * ifact[r]; } } public ModInt RepeatedCombination(int n, int k) { if (k == 0) return 1; return this[n + k - 1, k]; } } #endregion } namespace CompLib.Util { using System; using System.Linq; class Scanner { private string[] _line; private int _index; private const char Separator = ' '; public Scanner() { _line = new string[0]; _index = 0; } public string Next() { if (_index >= _line.Length) { string s; do { s = Console.ReadLine(); } while (s.Length == 0); _line = s.Split(Separator); _index = 0; } return _line[_index++]; } public string ReadLine() { _index = _line.Length; return Console.ReadLine(); } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public decimal NextDecimal() => decimal.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { string s = Console.ReadLine(); _line = s.Length == 0 ? new string[0] : s.Split(Separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray(); } }
using System; using CompLib.Mathematics; using CompLib.Util; public class Program { private int N, M; private ModInt[] F; public void Solve() { var sc = new Scanner(); F = new ModInt[10000001]; F[0] = 1; for (int i = 1; i <= 10000000; i++) { F[i] = F[i - 1] * i; } N = sc.NextInt(); M = sc.NextInt(); // 総和 3M -a // 最大 2M以下 -b // 奇数の個数 M以下 パリティ一致 -c // -aは自明 // M=0のとき、b,c満たす // M=kのとき満たしている、 // M=k+1でも満たす // a,cのみのパターン ModInt ans = 0; for (int i = M; i >= 0; i -= 2) { // i 奇数の個数 // 奇数のところにはあらかじめ1を置いておく if (i > N) continue; // 並べ方 N C i var a = C(N, i); // 残り 3M - i個 偶数になるように分ける var b = H(N, (3 * M - i) / 2); ans += a * b; } // bを満たさないパターン // 最大が2M超過 for (int max = 2 * M + 1; max <= 3 * M; max++) { // 残り3M-max並べ方 // M以下なのでcは満たす var c = H(N - 1, 3 * M - max); ans -= c * N; } Console.WriteLine(ans); } ModInt C(int n, int m) { return F[n] * ModInt.Inverse(F[n - m] * F[m]); } // n種類、r個選ぶ ModInt H(int n, int r) { return C(n + r - 1, r); } public static void Main(string[] args) => new Program().Solve(); } // https://bitbucket.org/camypaper/complib namespace CompLib.Mathematics { #region ModInt /// <summary> /// [0,<see cref="Mod"/>) までの値を取るような数 /// </summary> public struct ModInt { /// <summary> /// 剰余を取る値. /// </summary> // public const long Mod = (int) 1e9 + 7; public const long Mod = 998244353; /// <summary> /// 実際の数値. /// </summary> public long num; /// <summary> /// 値が <paramref name="n"/> であるようなインスタンスを構築します. /// </summary> /// <param name="n">インスタンスが持つ値</param> /// <remarks>パフォーマンスの問題上,コンストラクタ内では剰余を取りません.そのため,<paramref /// name="n"/> ∈ [0,<see cref="Mod"/>) を満たすような <paramref name="n"/> /// を渡してください.このコンストラクタは O(1) で実行されます.</remarks> public ModInt(long n) { num = n; } /// <summary> /// このインスタンスの数値を文字列に変換します. /// </summary> /// <returns>[0,<see cref="Mod"/>) の範囲内の整数を 10 /// 進表記したもの.</returns> 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 implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } /// <summary> /// 与えられた 2 つの数値からべき剰余を計算します. /// </summary> /// <param name="v">べき乗の底</param> /// <param name="k">べき指数</param> /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns> public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } /// <summary> /// 与えられた 2 つの数値からべき剰余を計算します. /// </summary> /// <param name="v">べき乗の底</param> /// <param name="k">べき指数</param> /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns> 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); } /// <summary> /// 与えられた数の逆元を計算します. /// </summary> /// <param name="v">逆元を取る対象となる数</param> /// <returns>逆元となるような値</returns> /// <remarks>法が素数であることを仮定して,フェルマーの小定理に従って逆元を /// O(log N) で計算します.</remarks> public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } #endregion #region Binomial Coefficient public class BinomialCoefficient { public ModInt[] fact, ifact; public BinomialCoefficient(int n) { fact = new ModInt[n + 1]; ifact = new ModInt[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = ModInt.Inverse(fact[n]); for (int i = n - 1; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); ifact[0] = ifact[1]; } public ModInt this[int n, int r] { get { if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0; return fact[n] * ifact[n - r] * ifact[r]; } } public ModInt RepeatedCombination(int n, int k) { if (k == 0) return 1; return this[n + k - 1, k]; } } #endregion } namespace CompLib.Util { using System; using System.Linq; class Scanner { private string[] _line; private int _index; private const char Separator = ' '; public Scanner() { _line = new string[0]; _index = 0; } public string Next() { if (_index >= _line.Length) { string s; do { s = Console.ReadLine(); } while (s.Length == 0); _line = s.Split(Separator); _index = 0; } return _line[_index++]; } public string ReadLine() { _index = _line.Length; return Console.ReadLine(); } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public decimal NextDecimal() => decimal.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { string s = Console.ReadLine(); _line = s.Length == 0 ? new string[0] : s.Split(Separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray(); } }
[["-", 0, 11, 12, 227, 39, 224, 225, 226, 0, 203], ["+", 0, 11, 12, 227, 39, 224, 225, 226, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203]]
8
1,253
4
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; using static System.Math; using static System.Array; using static AtCoder.Cout; using static AtCoder.Tool; using static AtCoder.ModInt; namespace AtCoder { class AC { const int MOD = 1000000007; // const int MOD = 998244353; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 3; const double EPS = 1e-8; static readonly int[] dI = { 0, 1, 0, -1, 1, -1, -1, 1 }; static readonly int[] dJ = { 1, 0, -1, 0, 1, 1, -1, -1 }; // static List<List<int>> G = new List<List<int>>(); // static List<List<Edge>> G = new List<List<Edge>>(); // static List<Edge> E = new List<Edge>(); static void Main(string[] args) { // var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = // false }; Console.SetOut(sw); var th = new Thread(Run, 1 << 26); th.Start(); th.Join(); // Run(); Console.Out.Flush(); } static void Run() { int Testcase = 1; // Testcase = Cin.Int; for (var _ = 0; _ < Testcase; _++) Solve(); } static void Solve() { Cin.Input(out int N, out int M); ModInt ans = 0; for (var i = M; i >= 0; i -= 2) { int s = (3 * M - i) / 2; ans += nCr(N, i) * (nCr(s + N - 1, s) - i * nCr(s - M + N - 1, s - M) - (N - i) * nCr(s - M - 1 + N - 1, s - M - 1)); } OutL(ans.value); } public struct Edge { public int from; public int to; public long dist; public Edge(int t, long c) { from = -1; to = t; dist = c; } public Edge(int f, int t, long c) { from = f; to = t; dist = c; } } } public class Priority_Queue<T> { private List<T> Q; private readonly Comparison<T> Func_Compare; public Priority_Queue(Comparison<T> comp) { Func_Compare = comp; 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 (Func_Compare(list[n], list[pIndex]) < 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 = Func_Compare(list[c1], list[c2]) < 0 ? c1 : c2; } if (Func_Compare(list[cur], list[comp]) > 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 Count() { return Q.Count(); } public bool Any() { return Q.Any(); } } public class SegmentTree<T> { // 1-indexed type int n; T[] Tree; Func<T, T, T> f; T ex; int L; [MethodImpl(MethodImplOptions.AggressiveInlining)] public SegmentTree(int size, Func<T, T, T> fun, T exvalue) { ex = exvalue; f = fun; n = 1; while (n < size) n <<= 1; Tree = new T[n << 1]; L = (n << 1) - 1; for (var i = 0; i <= L; i++) Tree[i] = ex; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public SegmentTree(int size, Func<T, T, T> fun, T exvalue, T[] initial) { ex = exvalue; n = 1; while (n < size) n <<= 1; f = fun; Tree = new T[n << 1]; L = (n << 1) - 1; for (var i = 0; i <= L; i++) Tree[i] = (n <= i && i <= n + initial.Length - 1) ? initial[i - n] : ex; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Set_All() { for (var i = n - 1; i >= 1; i--) Tree[i] = f(Tree[i << 1], Tree[(i << 1) | 1]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Assign(int idx, T nxt) => Tree[idx + n] = nxt; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int idx) { int now = idx + n; while (now > 1) { now >>= 1; Tree[now] = f(Tree[now << 1], Tree[now << 1 | 1]); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Query_Update(int idx, T nxt) { Assign(idx, nxt); Update(idx); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Query_Update_func(int idx, T y) { Assign(idx, f(Peek(idx), y)); Update(idx); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query_Fold(int l, int r) { int L = n + l; int R = n + r; T vL = ex, vR = ex; while (L < R) { if (L % 2 == 1) { vL = f(vL, Tree[L]); L++; } if (R % 2 == 1) { vR = f(Tree[R - 1], vR); R--; } L >>= 1; R >>= 1; } return f(vL, vR); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Peek(int idx) => Tree[idx + n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Display(int len) { for (var i = 0; i < len; i++) Console.Write($"{Tree[i + n]} "); Console.WriteLine(); } } public class UnionFind { private int[] parent; private int[] rank; private int[] size; public UnionFind(int n) { parent = new int[n]; rank = new int[n]; size = new int[n]; for (var i = 0; i < n; i++) { parent[i] = i; rank[i] = 0; size[i] = 1; } } public int Root(int x) { return parent[x] == x ? x : parent[x] = Root(parent[x]); } public bool SameRoot(int x, int y) { return Root(x) == Root(y); } public void Unite(int x, int y) { x = Root(x); y = Root(y); if (x == y) { return; } if (rank[x] < rank[y]) { parent[x] = y; size[y] += size[x]; size[x] = 0; } else { parent[y] = x; if (rank[x] == rank[y]) { rank[x]++; } size[x] += size[y]; size[y] = 0; } } public int SizeOf(int x) { return size[Root(x)]; } } struct ModInt { public long value; private const int MOD = 1000000007; // private const int MOD = 998244353; public ModInt(long value) { this.value = value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ModInt(long a) { var ret = a % MOD; return new ModInt(ret < 0 ? (ret + MOD) : ret); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator +(ModInt a, ModInt b) => (a.value + b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator -(ModInt a, ModInt b) => (a.value - b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator *(ModInt a, ModInt b) => (a.value * b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator /(ModInt a, ModInt b) => a * Modpow(b, MOD - 2); public static ModInt operator <<(ModInt a, int n) => (a.value << n); public static ModInt operator>>(ModInt a, int n) => (a.value >> n); public static ModInt operator ++(ModInt a) => a.value + 1; public static ModInt operator --(ModInt a) => a.value - 1; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Modpow(ModInt a, long n) { var k = a; ModInt ret = 1; while (n > 0) { if ((n & 1) != 0) ret *= k; k *= k; n >>= 1; } return ret; } private static readonly List<long> Factorials = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Fac(long n) { for (var i = Factorials.Count(); i <= n; i++) { Factorials.Add((Factorials[i - 1] * i) % MOD); } return Factorials[(int)n]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt nCr(long n, long r) { if (n <= 0 || r < 0) return 0; return n < r ? 0 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(ModInt a) => (int)a.value; } static class Cin { public static string[] ReadSplit => Console.ReadLine().Split(); public static int[] ReadSplitInt => ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] ReadSplitLong => ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] ReadSplit_Double => ConvertAll(Console.ReadLine().Split(), double.Parse); public static string Str => Console.ReadLine(); public static int Int => int.Parse(Console.ReadLine()); public static long Long => long.Parse(Console.ReadLine()); public static double Double => double.Parse(Console.ReadLine()); public static T Conv<T>(string input) { if (typeof(T).Equals(typeof(ModInt))) { return (T)(dynamic)(long.Parse(input)); } return (T)Convert.ChangeType(input, typeof(T)); } public static void Input<T>(out T a) => a = Conv<T>(Console.ReadLine()); public static void Input<T, U>(out T a, out U b) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); } public static void Input<T, U, V>(out T a, out U b, out V c) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); } public static void Input<T, U, V, W>(out T a, out U b, out V c, out W d) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); } public static void Input<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); e = Conv<X>(q[4]); } } static class Cout { public static void OutL(object s) => Console.WriteLine(s); public static void Out_Sep<T>(IEnumerable<T> s) => Console.WriteLine(string.Join(" ", s)); public static void Out_Sep<T>(IEnumerable<T> s, string sep) => Console.WriteLine(string.Join($"{sep}", s)); public static void Out_Sep(params object[] s) => Console.WriteLine(string.Join(" ", s)); public static void Out_One(object s) => Console.Write($"{s} "); public static void Out_One(object s, string sep) => Console.Write($"{s}{sep}"); public static void Endl() => Console.WriteLine(); } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { array = ConvertAll(array, x => initialvalue); } static public void Swap<T>(ref T a, ref T b) { T keep = a; a = b; b = keep; } static public void Display<T>(T[,] array2d, int n, int m) { for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { Console.Write($"{array2d[i, j]} "); } Console.WriteLine(); } } static public long Gcd(long a, long b) { if (a == 0 || b == 0) return Max(a, b); return a % b == 0 ? b : Gcd(b, a % b); } static public long LPow(int a, int b) => (long)Pow(a, b); static public bool Bit(long x, int dig) => ((1L << dig) & x) != 0; static public int Sig(long a) => a == 0 ? 0 : (int)(a / Abs(a)); } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; using static System.Math; using static System.Array; using static AtCoder.Cout; using static AtCoder.Tool; using static AtCoder.ModInt; namespace AtCoder { class AC { // const int MOD = 1000000007; const int MOD = 998244353; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 3; const double EPS = 1e-8; static readonly int[] dI = { 0, 1, 0, -1, 1, -1, -1, 1 }; static readonly int[] dJ = { 1, 0, -1, 0, 1, 1, -1, -1 }; // static List<List<int>> G = new List<List<int>>(); // static List<List<Edge>> G = new List<List<Edge>>(); // static List<Edge> E = new List<Edge>(); static void Main(string[] args) { // var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = // false }; Console.SetOut(sw); var th = new Thread(Run, 1 << 26); th.Start(); th.Join(); // Run(); Console.Out.Flush(); } static void Run() { int Testcase = 1; // Testcase = Cin.Int; for (var _ = 0; _ < Testcase; _++) Solve(); } static void Solve() { Cin.Input(out int N, out int M); ModInt ans = 0; for (var i = M; i >= 0; i -= 2) { int s = (3 * M - i) / 2; ans += nCr(N, i) * (nCr(s + N - 1, s) - i * nCr(s - M + N - 1, s - M) - (N - i) * nCr(s - M - 1 + N - 1, s - M - 1)); } OutL(ans.value); } public struct Edge { public int from; public int to; public long dist; public Edge(int t, long c) { from = -1; to = t; dist = c; } public Edge(int f, int t, long c) { from = f; to = t; dist = c; } } } public class Priority_Queue<T> { private List<T> Q; private readonly Comparison<T> Func_Compare; public Priority_Queue(Comparison<T> comp) { Func_Compare = comp; 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 (Func_Compare(list[n], list[pIndex]) < 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 = Func_Compare(list[c1], list[c2]) < 0 ? c1 : c2; } if (Func_Compare(list[cur], list[comp]) > 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 Count() { return Q.Count(); } public bool Any() { return Q.Any(); } } public class SegmentTree<T> { // 1-indexed type int n; T[] Tree; Func<T, T, T> f; T ex; int L; [MethodImpl(MethodImplOptions.AggressiveInlining)] public SegmentTree(int size, Func<T, T, T> fun, T exvalue) { ex = exvalue; f = fun; n = 1; while (n < size) n <<= 1; Tree = new T[n << 1]; L = (n << 1) - 1; for (var i = 0; i <= L; i++) Tree[i] = ex; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public SegmentTree(int size, Func<T, T, T> fun, T exvalue, T[] initial) { ex = exvalue; n = 1; while (n < size) n <<= 1; f = fun; Tree = new T[n << 1]; L = (n << 1) - 1; for (var i = 0; i <= L; i++) Tree[i] = (n <= i && i <= n + initial.Length - 1) ? initial[i - n] : ex; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Set_All() { for (var i = n - 1; i >= 1; i--) Tree[i] = f(Tree[i << 1], Tree[(i << 1) | 1]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Assign(int idx, T nxt) => Tree[idx + n] = nxt; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int idx) { int now = idx + n; while (now > 1) { now >>= 1; Tree[now] = f(Tree[now << 1], Tree[now << 1 | 1]); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Query_Update(int idx, T nxt) { Assign(idx, nxt); Update(idx); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Query_Update_func(int idx, T y) { Assign(idx, f(Peek(idx), y)); Update(idx); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query_Fold(int l, int r) { int L = n + l; int R = n + r; T vL = ex, vR = ex; while (L < R) { if (L % 2 == 1) { vL = f(vL, Tree[L]); L++; } if (R % 2 == 1) { vR = f(Tree[R - 1], vR); R--; } L >>= 1; R >>= 1; } return f(vL, vR); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Peek(int idx) => Tree[idx + n]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Display(int len) { for (var i = 0; i < len; i++) Console.Write($"{Tree[i + n]} "); Console.WriteLine(); } } public class UnionFind { private int[] parent; private int[] rank; private int[] size; public UnionFind(int n) { parent = new int[n]; rank = new int[n]; size = new int[n]; for (var i = 0; i < n; i++) { parent[i] = i; rank[i] = 0; size[i] = 1; } } public int Root(int x) { return parent[x] == x ? x : parent[x] = Root(parent[x]); } public bool SameRoot(int x, int y) { return Root(x) == Root(y); } public void Unite(int x, int y) { x = Root(x); y = Root(y); if (x == y) { return; } if (rank[x] < rank[y]) { parent[x] = y; size[y] += size[x]; size[x] = 0; } else { parent[y] = x; if (rank[x] == rank[y]) { rank[x]++; } size[x] += size[y]; size[y] = 0; } } public int SizeOf(int x) { return size[Root(x)]; } } struct ModInt { public long value; // private const int MOD = 1000000007; private const int MOD = 998244353; public ModInt(long value) { this.value = value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator ModInt(long a) { var ret = a % MOD; return new ModInt(ret < 0 ? (ret + MOD) : ret); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator +(ModInt a, ModInt b) => (a.value + b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator -(ModInt a, ModInt b) => (a.value - b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator *(ModInt a, ModInt b) => (a.value * b.value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt operator /(ModInt a, ModInt b) => a * Modpow(b, MOD - 2); public static ModInt operator <<(ModInt a, int n) => (a.value << n); public static ModInt operator>>(ModInt a, int n) => (a.value >> n); public static ModInt operator ++(ModInt a) => a.value + 1; public static ModInt operator --(ModInt a) => a.value - 1; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Modpow(ModInt a, long n) { var k = a; ModInt ret = 1; while (n > 0) { if ((n & 1) != 0) ret *= k; k *= k; n >>= 1; } return ret; } private static readonly List<long> Factorials = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt Fac(long n) { for (var i = Factorials.Count(); i <= n; i++) { Factorials.Add((Factorials[i - 1] * i) % MOD); } return Factorials[(int)n]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ModInt nCr(long n, long r) { if (n <= 0 || r < 0) return 0; return n < r ? 0 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(ModInt a) => (int)a.value; } static class Cin { public static string[] ReadSplit => Console.ReadLine().Split(); public static int[] ReadSplitInt => ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] ReadSplitLong => ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] ReadSplit_Double => ConvertAll(Console.ReadLine().Split(), double.Parse); public static string Str => Console.ReadLine(); public static int Int => int.Parse(Console.ReadLine()); public static long Long => long.Parse(Console.ReadLine()); public static double Double => double.Parse(Console.ReadLine()); public static T Conv<T>(string input) { if (typeof(T).Equals(typeof(ModInt))) { return (T)(dynamic)(long.Parse(input)); } return (T)Convert.ChangeType(input, typeof(T)); } public static void Input<T>(out T a) => a = Conv<T>(Console.ReadLine()); public static void Input<T, U>(out T a, out U b) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); } public static void Input<T, U, V>(out T a, out U b, out V c) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); } public static void Input<T, U, V, W>(out T a, out U b, out V c, out W d) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); } public static void Input<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var q = ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); e = Conv<X>(q[4]); } } static class Cout { public static void OutL(object s) => Console.WriteLine(s); public static void Out_Sep<T>(IEnumerable<T> s) => Console.WriteLine(string.Join(" ", s)); public static void Out_Sep<T>(IEnumerable<T> s, string sep) => Console.WriteLine(string.Join($"{sep}", s)); public static void Out_Sep(params object[] s) => Console.WriteLine(string.Join(" ", s)); public static void Out_One(object s) => Console.Write($"{s} "); public static void Out_One(object s, string sep) => Console.Write($"{s}{sep}"); public static void Endl() => Console.WriteLine(); } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { array = ConvertAll(array, x => initialvalue); } static public void Swap<T>(ref T a, ref T b) { T keep = a; a = b; b = keep; } static public void Display<T>(T[,] array2d, int n, int m) { for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { Console.Write($"{array2d[i, j]} "); } Console.WriteLine(); } } static public long Gcd(long a, long b) { if (a == 0 || b == 0) return Max(a, b); return a % b == 0 ? b : Gcd(b, a % b); } static public long LPow(int a, int b) => (long)Pow(a, b); static public bool Bit(long x, int dig) => ((1L << dig) & x) != 0; static public int Sig(long a) => a == 0 ? 0 : (int)(a / Abs(a)); } }
[["-", 0, 124, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 124, 0, 198, 0, 200, 0, 212, 0, 203]]
8
3,307
4
using System; using System.Collections.Generic; public class CSharp { public static void Main() { Kyopuro chokudai = new Kyopuro(); chokudai.AtCoder(); } } public class Kyopuro { int n, m; public void AtCoder() { string[] input; input = Console.ReadLine().Split(' '); n = int.Parse(input[0]); m = int.Parse(input[1]); int p = 998244353; Mod mod = new Mod(2000000); long ans = p - n; ans *= mod.Ncr(m + n - 2, n - 1); ans %= p; ans += mod.Ncr(m * 3 + n - 1, n - 1); ans %= p; /* if (n > m + 1) { ans += p; ans -= mod.Ncr(n + m - 3, n - m - 2); ans %= p; } */ if (n > m + 1) { int c = 0; long ans2; for (int i = 0; m + 2 + i <= n; i = i + 2) { if (m - 1 - i / 2 < 0) break; ans2 = mod.Ncr(n, m + 2 + i); ans2 *= mod.Ncr(n + m - 2 - i / 2, n - 1); ans2 %= p; ans += p; ans -= ans2; ans %= p; } } Console.WriteLine(ans); } public static void DebugWL(object x) { System.Diagnostics.Debug.WriteLine(x); } } // Mod(int n) // int[] fac: factors (1-indexed) // int n: the length of fac // int .Ncr(n,r): nCr // int .Fac(n): n!%p // int .Pow(n,m): n^m%p public class Mod { const int p = 998244353; int[] fac; public Mod(int n) { fac = new int[n + 1]; fac[0] = 1; long tmp = 0; for (int i = 1; i <= n; i++) { tmp = fac[i - 1]; tmp *= i; tmp %= p; fac[i] = (int)tmp; } } public int Ncr(int n, int r) { if (n < r) return 0; if (r == 0 | n == r) return 1; long a = Fac(n); a = a * Pow(Fac(n - r), p - 2) % p; a = a * Pow(Fac(r), p - 2) % p; return (int)a; } public int Fac(int n) { if (fac[n] >= 0) return fac[n]; long k = 1; for (long i = 1; i <= n; i++) { k = k * i % p; } fac[n] = (int)k; return fac[n]; } public int Pow(int n, int m) { if (m == 0) return 1; if (m == 1) return n; long a = 1; if (m % 2 == 0) { a = Pow(n, m / 2); a = a * a % p; } else { a = Pow(n, m / 2); a = a * a % p; a = a * n % p; } return (int)a; } }
using System; using System.Collections.Generic; public class CSharp { public static void Main() { Kyopuro chokudai = new Kyopuro(); chokudai.AtCoder(); } } public class Kyopuro { int n, m; public void AtCoder() { string[] input; input = Console.ReadLine().Split(' '); n = int.Parse(input[0]); m = int.Parse(input[1]); int p = 998244353; Mod mod = new Mod(3000000); long ans = p - n; ans *= mod.Ncr(m + n - 2, n - 1); ans %= p; ans += mod.Ncr(m * 3 + n - 1, n - 1); ans %= p; /* if (n > m + 1) { ans += p; ans -= mod.Ncr(n + m - 3, n - m - 2); ans %= p; } */ if (n > m + 1) { int c = 0; long ans2; for (int i = 0; m + 2 + i <= n; i = i + 2) { if (m - 1 - i / 2 < 0) break; ans2 = mod.Ncr(n, m + 2 + i); ans2 *= mod.Ncr(n + m - 2 - i / 2, n - 1); ans2 %= p; ans += p; ans -= ans2; ans %= p; } } Console.WriteLine(ans); } public static void DebugWL(object x) { System.Diagnostics.Debug.WriteLine(x); } } // Mod(int n) // int[] fac: factors (1-indexed) // int n: the length of fac // int .Ncr(n,r): nCr // int .Fac(n): n!%p // int .Pow(n,m): n^m%p public class Mod { const int p = 998244353; int[] fac; public Mod(int n) { fac = new int[n + 1]; fac[0] = 1; long tmp = 0; for (int i = 1; i <= n; i++) { tmp = fac[i - 1]; tmp *= i; tmp %= p; fac[i] = (int)tmp; } } public int Ncr(int n, int r) { if (n < r) return 0; if (r == 0 | n == r) return 1; long a = Fac(n); a = a * Pow(Fac(n - r), p - 2) % p; a = a * Pow(Fac(r), p - 2) % p; return (int)a; } public int Fac(int n) { if (fac[n] >= 0) return fac[n]; long k = 1; for (long i = 1; i <= n; i++) { k = k * i % p; } fac[n] = (int)k; return fac[n]; } public int Pow(int n, int m) { if (m == 0) return 1; if (m == 1) return n; long a = 1; if (m % 2 == 0) { a = Pow(n, m / 2); a = a * a % p; } else { a = Pow(n, m / 2); a = a * a % p; a = a * n % p; } return (int)a; } }
[["-", 0, 212, 0, 230, 3, 4, 0, 28, 0, 203], ["+", 0, 212, 0, 230, 3, 4, 0, 28, 0, 203]]
8
649
2
using System; using static System.Console; class Test { public static void Main() { int a = int.Parse(ReadLine()); int s = 3 * a * a; } }
using System; using static System.Console; class Test { public static void Main() { int a = int.Parse(ReadLine()); int s = 3 * a * a; WriteLine(s); } }
[["+", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
42
5
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC134A { class Program { static void Main(string[] args) { int r = int.Parse(Console.ReadLine()); Console.WriteLine(r * r * r * 3); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC134A { class Program { static void Main(string[] args) { int r = int.Parse(Console.ReadLine()); Console.WriteLine(r * r * 3); Console.ReadKey(); } } }
[["-", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48]]
8
79
2
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace ABC133D { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); return; } public void Solve(ConsoleInput cin) { var N = cin.ReadInt; var A = cin.ReadIntArray(N); var sumA = A.Sum(); var ans = new int[N]; ans[0] = sumA - 2 * A.Where((value, index) => index % 2 == 1).Sum(); for (int i = 1; i < N; i++) { ans[i] = 2 * A[i - 1] - ans[i - 1]; } Console.WriteLine(string.Join(" ", ans)); } } public static class Extensions { public static string Reverse(this string sourse) { char[] chrAry = sourse.ToCharArray(); Array.Reverse(chrAry); return new string(chrAry); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace ABC133D { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); return; } public void Solve(ConsoleInput cin) { var N = cin.ReadInt; var A = cin.ReadLongArray(N); var sumA = A.Sum(); var ans = new long[N]; ans[0] = sumA - 2 * A.Where((value, index) => index % 2 == 1).Sum(); for (int i = 1; i < N; i++) { ans[i] = 2 * A[i - 1] - ans[i - 1]; } Console.WriteLine(string.Join(" ", ans)); } } public static class Extensions { public static string Reverse(this string sourse) { char[] chrAry = sourse.ToCharArray(); Array.Reverse(chrAry); return new string(chrAry); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
586
4
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); long[] A = new long[N]; long[] x = new long[N]; long s = 0; string[] str = Console.ReadLine().Split(); for (var i = 0; i < N; ++i) { A[i] = long.Parse(str[i]); s += A[i]; } x[0] = s; for (var i = 1; i < N; i += 2) { x[0] += A[i]; } for (var i = 1; i < N; ++i) { x[i] = x[i - 1] + 2 * A[i - 1]; } Console.WriteLine(string.Join(" ", x)); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); long[] A = new long[N]; long[] x = new long[N]; long s = 0; string[] str = Console.ReadLine().Split(); for (var i = 0; i < N; ++i) { A[i] = long.Parse(str[i]); s += A[i]; } x[0] = s; for (var i = 1; i < N; i += 2) { x[0] -= 2 * A[i]; } for (var i = 1; i < N; ++i) { x[i] = -x[i - 1] + 2 * A[i - 1]; } Console.WriteLine(string.Join(" ", x)); } }
[["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 107], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 110], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 31, 203], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 31, 241, 0, 33]]
8
206
5
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); #if DEBUG ReadLine(); #endif } void Solve(ConsoleInput cin) { int n = cin.ReadInt; int[] a = cin.ReadIntArray(n); int[] x = new int[n]; // x_0 x[0] = a.Sum(); for (int i = 1; i < n - 1; i += 2) { x[0] -= 2 * a[i]; } // x_i for (int i = 1; i < n; i++) { x[i] = 2 * a[i - 1] - x[i - 1]; } Console.WriteLine(String.Join(" ", x)); } } class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); #if DEBUG ReadLine(); #endif } void Solve(ConsoleInput cin) { int n = cin.ReadInt; long[] a = cin.ReadLongArray(n); long[] x = new long[n]; // x_0 x[0] = a.Sum(); for (int i = 1; i < n - 1; i += 2) { x[0] -= 2 * a[i]; } // x_i for (int i = 1; i < n; i++) { x[i] = 2 * a[i - 1] - x[i - 1]; } Console.WriteLine(String.Join(" ", x)); } } class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
556
8
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp4 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var q = from a in Console.ReadLine().Split(' ') select int.Parse(a); var aList = q.ToList(); // X(i) + X(i+1) = 2A(i) // X(1) + X(2) = 2A(1), X(3) + X(4) = 2A(3), X(5) + X(6) = 2A(5), ... // A(0) + ... + A(N-1) = X(0) + ... X(N-1) = S // X(0) = S - ((X(1) + X(2)) + (X(3) + X(4)) + ... (X(N-2) + X(N-1))) // X(0) = S - (2A(1) + 2A(3) + ... 2A(N-2)) // X(0) = S - 2(A(1) + A(3) + ... A(N-2)) var xList = new List<int>(n); int x1 = aList.Sum(); for (int i = 1; i < n - 1; i++) { if ((i % 2) == 1) { x1 -= (2 * aList[i]); } } xList.Add(x1); // X(i+1) = 2A(i) - X(i) for (int i = 0; i < n - 1; i++) { xList.Add(2 * aList[i] - xList[i]); Console.Write(xList[i] + " "); } Console.WriteLine(xList.Last()); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp4 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var q = from a in Console.ReadLine().Split(' ') select long.Parse(a); var aList = q.ToList(); // X(i) + X(i+1) = 2A(i) // X(1) + X(2) = 2A(1), X(3) + X(4) = 2A(3), X(5) + X(6) = 2A(5), ... // A(0) + ... + A(N-1) = X(0) + ... X(N-1) = S // X(0) = S - ((X(1) + X(2)) + (X(3) + X(4)) + ... (X(N-2) + X(N-1))) // X(0) = S - (2A(1) + 2A(3) + ... 2A(N-2)) // X(0) = S - 2(A(1) + A(3) + ... A(N-2)) var xList = new List<long>(n); long x1 = aList.Sum(); for (int i = 1; i < n - 1; i++) { if ((i % 2) == 1) { x1 -= (2 * aList[i]); } } xList.Add(x1); // X(i+1) = 2A(i) - X(i) for (int i = 0; i < n - 1; i++) { xList.Add(2 * aList[i] - xList[i]); Console.Write(xList[i] + " "); } Console.WriteLine(xList.Last()); } } }
[["-", 0, 242, 0, 244, 0, 213, 63, 214, 205, 199], ["+", 0, 242, 0, 244, 0, 213, 63, 214, 205, 199], ["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
219
6
using System; using System.Linq; using System.Collections.Generic; using static System.Console; class Solver { Scanner sc = new Scanner(); public void Solve() { var N = sc.nextInt(); var A = sc.ArrayInt(N); var ans = new long[N]; var sum = 0L; for (int i = 1; i < N; i += 2) { sum += A[i] * 2; } ans[0] = A.Sum() - sum; for (int i = 1; i < N; i++) { ans[i] = (2 * A[i - 1]) - ans[i - 1]; } WriteLine(String.Join(" ", 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 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; class Solver { Scanner sc = new Scanner(); public void Solve() { var N = sc.nextInt(); var A = sc.ArrayLong(N); var ans = new long[N]; var sum = 0L; for (int i = 1; i < N; i += 2) { sum += A[i] * 2; } ans[0] = A.Sum() - sum; for (int i = 1; i < N; i++) { ans[i] = (2 * A[i - 1]) - ans[i - 1]; } WriteLine(string.Join(" ", 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 Program { static void Main(string[] args) { Solver s = new Solver(); s.Solve(); } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 199]]
8
544
4
using System; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; class Program { static void Main() { var sc = new StreamScanner(OpenStandardOutput()); var N = sc.Integer(); var A = new int[N]; var s = 0L; for (int i = 0; i < A.Length; i++) { var k = sc.Integer(); A[i] = k; if ((i & 1) == 0) s += k; else s -= k; } var ans = new long[N]; ans[0] = s; for (int i = 1; i < ans.Length; i++) { ans[i] = (A[i - 1] - (ans[i - 1] >> 1)) * 2; } SetOut( new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(string.Join(" ", ans)); Out.Flush(); } } public class StreamScanner { public StreamScanner(System.IO.Stream stream) { str = stream; } public readonly System.IO.Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr < len) return buf[ptr++]; ptr = 0; if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++]; isEof = true; return 0; } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (;; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } }
using System; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; class Program { static void Main() { var sc = new StreamScanner(OpenStandardInput()); var N = sc.Integer(); var A = new int[N]; var s = 0L; for (int i = 0; i < A.Length; i++) { var k = sc.Integer(); A[i] = k; if ((i & 1) == 0) s += k; else s -= k; } var ans = new long[N]; ans[0] = s; for (int i = 1; i < ans.Length; i++) { ans[i] = (A[i - 1] - (ans[i - 1] >> 1)) * 2; } SetOut( new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(string.Join(" ", ans)); Out.Flush(); } } public class StreamScanner { public StreamScanner(System.IO.Stream stream) { str = stream; } public readonly System.IO.Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr < len) return buf[ptr++]; ptr = 0; if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++]; isEof = true; return 0; } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (;; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } }
[["-", 0, 230, 3, 4, 0, 28, 0, 213, 63, 22], ["+", 0, 230, 3, 4, 0, 28, 0, 213, 63, 22]]
8
517
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AtCoder { public class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray(); int[] b = new int[N]; long s = 0; long[] sum = new long[2]; for (int i = 0; i < N; i++) { s += A[i]; sum[i % 2] += A[i]; } b[0] = (int)(s - 2 * sum[1 % 2]); for (int i = 1; i < N; i++) { b[i] = 2 * A[i - 1] - b[i - 1]; } for (int i = 0; i < N; i++) { Console.Write(b[i]); } Console.WriteLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AtCoder { public class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] A = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray(); int[] b = new int[N]; long s = 0; long[] sum = new long[2]; for (int i = 0; i < N; i++) { s += A[i]; sum[i % 2] += A[i]; } b[0] = (int)(s - 2 * sum[1 % 2]); for (int i = 1; i < N; i++) { b[i] = 2 * A[i - 1] - b[i - 1]; } for (int i = 0; i < N; i++) { Console.Write(b[i] + " "); } Console.WriteLine(); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 3, 4, 0, 28, 0, 16, 12, 5, 0, 62], ["+", 3, 4, 0, 28, 0, 16, 12, 5, 0, 222]]
8
237
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC133D { class Program { static void Main(string[] args) { var input = Console.ReadLine(); var n = int.Parse(input); var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var total = a.Sum(); var result = new int[n]; result[0] = total - 2 * (a.Select((x, i) => new { x, i }) .Where(x => x.i % 2 == 1) .Select(x => x.x) .Sum()); for (int i = 1; i < n; i++) { result[i] = 2 * a[i - 1] - result[i - 1]; } Console.WriteLine(string.Join(" ", result)); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC133D { class Program { static void Main(string[] args) { var input = Console.ReadLine(); var n = int.Parse(input); var a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var total = a.Sum(); var result = new long[n]; result[0] = total - 2 * (a.Select((x, i) => new { x, i }) .Where(x => x.i % 2 == 1) .Select(x => x.x) .Sum()); for (int i = 1; i < n; i++) { result[i] = 2 * a[i - 1] - result[i - 1]; } Console.WriteLine(string.Join(" ", result)); } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
218
4
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Threading; using Extensions; using static System.Math; using static Extensions.MathExtension; using static Extensions.ConsoleInputExtension; using static Extensions.ConsoleOutputExtension; class Solver { public void Solve() { // Solve Code Here int N = Cin; var A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var aSum = A.Sum(); for (var i = 0; i < A.Length; i++) A[i] <<= 1; var B = new int[N]; for (int i = 1; i < N; i += 2) aSum -= A[i]; B[0] = aSum; for (int i = 1; i < N; i++) { B[i] = A[i - 1] - B[i - 1]; } foreach (var i in B) Cout(i + " "); Coutln(""); } // Other Functions Here } #if !DEBUG class EntryPoint { static void Main(string[] args) { new Solver().Solve(); Flush(); } } #endif class PriorityQueue<T> { private readonly List<Tuple<int, T>> _list = new List<Tuple<int, T>>(); public int Count { get; private set; } = 0; public PriorityQueue() { _list.Add(null); _list.Add(null); _list.Add(null); } public void Push(T item, int priority) { var itemIndex = Count++; _list[itemIndex] = Tuple.Create(priority, item); while (_list.Count < Count + 2) _list.Add(null); int parentIndex; while ((parentIndex = GetParentIndex(itemIndex)) != -1 && priority > _list[parentIndex].Item1) { Swap(itemIndex, parentIndex); itemIndex = parentIndex; } } private int GetParentIndex(int index) { if (index == 0) return -1; return ((index + 1) >> 1) - 1; } private Tuple<int, int> GetChildrenIndex(int index) { var item2 = (index + 1) << 1; var item1 = item2 - 1; return item2 >= _list.Count ? null : Tuple.Create(item1, item2); } public T Pop() { if (_list.Count <= 0 || _list[0] == null) throw new IndexOutOfRangeException(); var item = _list[0].Item2; _list[0] = null; Tuple<int, int> childrenIndex; int index = 0; while ((childrenIndex = GetChildrenIndex(index)) != null && (_list[childrenIndex.Item1] != null || _list[childrenIndex.Item2] != null)) { if ((_list[childrenIndex.Item1] != null && _list[childrenIndex.Item2] != null && _list[childrenIndex.Item1].Item1 > _list[childrenIndex.Item2].Item1) || _list[childrenIndex.Item2] == null) { _list[index] = _list[childrenIndex.Item1]; _list[childrenIndex.Item1] = null; index = childrenIndex.Item1; } else { two: _list[index] = _list[childrenIndex.Item2]; _list[childrenIndex.Item2] = null; index = childrenIndex.Item2; } } Count--; return item; } public T Peek() { return _list[0].Item2; } private void Swap(int index1, int index2) { var tmp = _list[index1]; _list[index1] = _list[index2]; _list[index2] = tmp; } } struct Rational { private long _numerator; private long _denominator; public long Numerator => _numerator; public long Denominator => _denominator; public Rational(long numerator, long denominator) { var gcd = GCD(numerator, denominator); _numerator = numerator / gcd; _denominator = denominator / gcd; } public static Rational operator +(Rational a, Rational b) { return new Rational(a._numerator * b._denominator + b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator -(Rational a, Rational b) { return new Rational(a._numerator * b._denominator - b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator *(Rational a, Rational b) { return new Rational(a._numerator * b._numerator, a._denominator * b._denominator); } public static Rational operator /(Rational a, Rational b) { return new Rational(a._numerator * b._denominator, a._denominator * b._numerator); } public static explicit operator Rational(int i) { return new Rational(i, 1); } public static explicit operator Rational(long l) { return new Rational(l, 1); } public static explicit operator Rational(double d) { long denominator = 1; while (d % 1 != 0) { denominator *= 2; d *= 2; } return new Rational((long)d, denominator); } public static explicit operator Rational(float f) { long denominator = 1; while (f % 1 != 0) { denominator *= 2; f *= 2; } return new Rational((long)f, denominator); } } struct ModInt { public long Value { get; } public static long Mod { get; set; } = 1000000000L + 7; public ModInt(long l) { long value = l % Mod; while (value < 0) value += Mod; Value = value % Mod; } public static implicit operator long(ModInt m) { return m.Value; } public static explicit operator ModInt(long l) { return new ModInt(l); } public static ModInt operator +(ModInt a, ModInt b) { return new ModInt(a.Value + b.Value); } public static ModInt operator -(ModInt a, ModInt b) { return new ModInt(a.Value - b.Value); } public static ModInt operator *(ModInt a, ModInt b) { return new ModInt(a.Value * b.Value); } public static ModInt operator /(ModInt a, ModInt b) { return new ModInt(a * b.Inverse()); } public override string ToString() { return Value.ToString(); } } static class ModIntMath { public static ModInt Pow(this ModInt a, ModInt b) { var pow = b.Value; var m = a; ModInt ans = new ModInt(1); while (pow != 0) { if ((pow & 1) == 1) { ans *= m; } pow >>= 1; m *= m; } return ans; } public static ModInt Inverse(this ModInt m) { return m.Pow((ModInt)(ModInt.Mod - 2)); } } class UnionFind { private readonly int[] _array; public UnionFind(int N) { _array = new int[N]; for (int i = 0; i < N; i++) { _array[i] = i; } } public int GetRootNode(int n) { if (_array[n] == n) return n; return _array[n] = GetRootNode(_array[n]); } public void UnionGroup(int a, int b) { var rootA = GetRootNode(a); var rootB = GetRootNode(b); if (rootA == rootB) return; _array[rootA] = rootB; } public bool IsSameGroup(int a, int b) => GetRootNode(a) == GetRootNode(b); public bool IsRoot(int n) => _array[n] == n; } namespace Extensions { public class ConsoleInputExtension { private static readonly ConsoleInputExtension _cin = new ConsoleInputExtension(); public static ConsoleInputExtension Cin => _cin; private static readonly Queue<string> _inputQueue = new Queue<string>(); private ConsoleInputExtension() {} public static implicit operator string(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return _inputQueue.Dequeue(); } public static implicit operator int(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return int.Parse(_inputQueue.Dequeue()); } public static implicit operator long(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return long.Parse(_inputQueue.Dequeue()); } public static implicit operator float(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return float.Parse(_inputQueue.Dequeue()); } public static implicit operator double(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return double.Parse(_inputQueue.Dequeue()); } } public static class ConsoleOutputExtension { private static StreamWriter _writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; public static void Cout(object o) { _writer.Write(o); } public static void CoutF(object o) { _writer.Write(o); _writer.Flush(); } public static void Coutln(object o) { _writer.WriteLine(o); } public static void CoutlnF(object o) { _writer.WriteLine(o); _writer.Flush(); } public static void Flush() { _writer.Flush(); } } public static class MathExtension { //最小公倍数 public static int LCM(int num1, int num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } public static long LCM(long num1, long num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } //最大公約数 public static int GCD(int num1, int num2) { int a = Math.Max(num1, num2); int b = Math.Min(num1, num2); int mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } public static long GCD(long num1, long num2) { long a = Math.Max(num1, num2); long b = Math.Min(num1, num2); long mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } } public static class EnumerableExtension { public delegate void Function<in T>(T val); public static void ForEach<T>(this IEnumerable<T> enumerable, Function<T> function) { foreach (var x in enumerable) { function(x); } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Threading; using Extensions; using static System.Math; using static Extensions.MathExtension; using static Extensions.ConsoleInputExtension; using static Extensions.ConsoleOutputExtension; class Solver { public void Solve() { // Solve Code Here int N = Cin; var A = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var aSum = A.Sum(); for (var i = 0; i < A.Length; i++) A[i] <<= 1; var B = new long[N]; for (int i = 1; i < N; i += 2) aSum -= A[i]; B[0] = aSum; for (int i = 1; i < N; i++) { B[i] = A[i - 1] - B[i - 1]; } foreach (var i in B) Cout(i + " "); Coutln(""); } // Other Functions Here } #if !DEBUG class EntryPoint { static void Main(string[] args) { new Solver().Solve(); Flush(); } } #endif class PriorityQueue<T> { private readonly List<Tuple<int, T>> _list = new List<Tuple<int, T>>(); public int Count { get; private set; } = 0; public PriorityQueue() { _list.Add(null); _list.Add(null); _list.Add(null); } public void Push(T item, int priority) { var itemIndex = Count++; _list[itemIndex] = Tuple.Create(priority, item); while (_list.Count < Count + 2) _list.Add(null); int parentIndex; while ((parentIndex = GetParentIndex(itemIndex)) != -1 && priority > _list[parentIndex].Item1) { Swap(itemIndex, parentIndex); itemIndex = parentIndex; } } private int GetParentIndex(int index) { if (index == 0) return -1; return ((index + 1) >> 1) - 1; } private Tuple<int, int> GetChildrenIndex(int index) { var item2 = (index + 1) << 1; var item1 = item2 - 1; return item2 >= _list.Count ? null : Tuple.Create(item1, item2); } public T Pop() { if (_list.Count <= 0 || _list[0] == null) throw new IndexOutOfRangeException(); var item = _list[0].Item2; _list[0] = null; Tuple<int, int> childrenIndex; int index = 0; while ((childrenIndex = GetChildrenIndex(index)) != null && (_list[childrenIndex.Item1] != null || _list[childrenIndex.Item2] != null)) { if ((_list[childrenIndex.Item1] != null && _list[childrenIndex.Item2] != null && _list[childrenIndex.Item1].Item1 > _list[childrenIndex.Item2].Item1) || _list[childrenIndex.Item2] == null) { _list[index] = _list[childrenIndex.Item1]; _list[childrenIndex.Item1] = null; index = childrenIndex.Item1; } else { two: _list[index] = _list[childrenIndex.Item2]; _list[childrenIndex.Item2] = null; index = childrenIndex.Item2; } } Count--; return item; } public T Peek() { return _list[0].Item2; } private void Swap(int index1, int index2) { var tmp = _list[index1]; _list[index1] = _list[index2]; _list[index2] = tmp; } } struct Rational { private long _numerator; private long _denominator; public long Numerator => _numerator; public long Denominator => _denominator; public Rational(long numerator, long denominator) { var gcd = GCD(numerator, denominator); _numerator = numerator / gcd; _denominator = denominator / gcd; } public static Rational operator +(Rational a, Rational b) { return new Rational(a._numerator * b._denominator + b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator -(Rational a, Rational b) { return new Rational(a._numerator * b._denominator - b._numerator * a._denominator, a._denominator * b._denominator); } public static Rational operator *(Rational a, Rational b) { return new Rational(a._numerator * b._numerator, a._denominator * b._denominator); } public static Rational operator /(Rational a, Rational b) { return new Rational(a._numerator * b._denominator, a._denominator * b._numerator); } public static explicit operator Rational(int i) { return new Rational(i, 1); } public static explicit operator Rational(long l) { return new Rational(l, 1); } public static explicit operator Rational(double d) { long denominator = 1; while (d % 1 != 0) { denominator *= 2; d *= 2; } return new Rational((long)d, denominator); } public static explicit operator Rational(float f) { long denominator = 1; while (f % 1 != 0) { denominator *= 2; f *= 2; } return new Rational((long)f, denominator); } } struct ModInt { public long Value { get; } public static long Mod { get; set; } = 1000000000L + 7; public ModInt(long l) { long value = l % Mod; while (value < 0) value += Mod; Value = value % Mod; } public static implicit operator long(ModInt m) { return m.Value; } public static explicit operator ModInt(long l) { return new ModInt(l); } public static ModInt operator +(ModInt a, ModInt b) { return new ModInt(a.Value + b.Value); } public static ModInt operator -(ModInt a, ModInt b) { return new ModInt(a.Value - b.Value); } public static ModInt operator *(ModInt a, ModInt b) { return new ModInt(a.Value * b.Value); } public static ModInt operator /(ModInt a, ModInt b) { return new ModInt(a * b.Inverse()); } public override string ToString() { return Value.ToString(); } } static class ModIntMath { public static ModInt Pow(this ModInt a, ModInt b) { var pow = b.Value; var m = a; ModInt ans = new ModInt(1); while (pow != 0) { if ((pow & 1) == 1) { ans *= m; } pow >>= 1; m *= m; } return ans; } public static ModInt Inverse(this ModInt m) { return m.Pow((ModInt)(ModInt.Mod - 2)); } } class UnionFind { private readonly int[] _array; public UnionFind(int N) { _array = new int[N]; for (int i = 0; i < N; i++) { _array[i] = i; } } public int GetRootNode(int n) { if (_array[n] == n) return n; return _array[n] = GetRootNode(_array[n]); } public void UnionGroup(int a, int b) { var rootA = GetRootNode(a); var rootB = GetRootNode(b); if (rootA == rootB) return; _array[rootA] = rootB; } public bool IsSameGroup(int a, int b) => GetRootNode(a) == GetRootNode(b); public bool IsRoot(int n) => _array[n] == n; } namespace Extensions { public class ConsoleInputExtension { private static readonly ConsoleInputExtension _cin = new ConsoleInputExtension(); public static ConsoleInputExtension Cin => _cin; private static readonly Queue<string> _inputQueue = new Queue<string>(); private ConsoleInputExtension() {} public static implicit operator string(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return _inputQueue.Dequeue(); } public static implicit operator int(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return int.Parse(_inputQueue.Dequeue()); } public static implicit operator long(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return long.Parse(_inputQueue.Dequeue()); } public static implicit operator float(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return float.Parse(_inputQueue.Dequeue()); } public static implicit operator double(ConsoleInputExtension _) { if (_inputQueue.Count == 0) Console.ReadLine().Split(' ').ForEach(val => _inputQueue.Enqueue(val)); return double.Parse(_inputQueue.Dequeue()); } } public static class ConsoleOutputExtension { private static StreamWriter _writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; public static void Cout(object o) { _writer.Write(o); } public static void CoutF(object o) { _writer.Write(o); _writer.Flush(); } public static void Coutln(object o) { _writer.WriteLine(o); } public static void CoutlnF(object o) { _writer.WriteLine(o); _writer.Flush(); } public static void Flush() { _writer.Flush(); } } public static class MathExtension { //最小公倍数 public static int LCM(int num1, int num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } public static long LCM(long num1, long num2) { var gcd = GCD(num1, num2); return num1 * (num2 / gcd); } //最大公約数 public static int GCD(int num1, int num2) { int a = Math.Max(num1, num2); int b = Math.Min(num1, num2); int mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } public static long GCD(long num1, long num2) { long a = Math.Max(num1, num2); long b = Math.Min(num1, num2); long mod; while ((mod = a % b) != 0) { a = b; b = mod; } return b; } } public static class EnumerableExtension { public delegate void Function<in T>(T val); public static void ForEach<T>(this IEnumerable<T> enumerable, Function<T> function) { foreach (var x in enumerable) { function(x); } } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199]]
8
2,305
4
using System; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; class MainClass : Scanner { static Graph<int> G; static int N; static int K; static bool[] used; static void Main() { (N, K) = ReadTuple<int, int>(); G = new Graph<int>(N); for (int i = 0; i < N - 1; i++) { var r = RInts(_ => int.Parse(_) - 1); G.Add(r[0], r[1]); G.Add(r[1], r[0]); } Modular ans = K; used = new bool[N]; used[0] = true; var que = new Queue<int>(); que.Enqueue(0); while (que.Count > 0) { var now = que.Dequeue(); int ncnt = G[now].Count; if (now == 0) { if (ncnt < 0 || K - 1 < ncnt) ans *= 0; else ans *= Modular.Npr(K - 1, ncnt); } else { if (K - 2 < ncnt) ans *= 0; else ans *= Modular.Npr(K - 2, ncnt - 1); } foreach (var next in G[now]) { if (used[next]) continue; used[next] = true; que.Enqueue(next); } } WriteLine(ans); } } class Modular { private const int M = 1000000007; private const int arysize = 2000001; private long value; public Modular(long value = 0) { this.value = value; } public override string ToString() { return value.ToString(); } 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) => a.value + b.value; public static Modular operator -(Modular a, Modular b) => a.value - b.value; public static Modular operator *(Modular a, Modular b) => a.value * b.value; public static Modular Pow(Modular a, long n) { Modular ans = 1; for (; n > 0; n >>= 1, a *= a) { if ((n & 1) == 1) ans *= a; } return ans; } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, M - 2); } private static int[] facs = new int[arysize]; private static int facscount = -1; public static Modular Fac(int n) //階乗 { facs[0] = 1; while (++facscount <= n) { facs[facscount + 1] = (int)(Math.BigMul(facs[facscount], facscount + 1) % M); } return facs[n]; } public static Modular Fac(int r, int n) //記録しない階乗 { int temp = 1; for (int i = r; i <= n; i++) { temp = (int)(Math.BigMul(temp, i) % M); } return temp; } public static Modular Ncr(int n, int r) // nCr { return (n < r) ? 0 : (n == r) ? 1 : (Math.Max(n, r) <= arysize) ? Fac(n) / (Fac(r) * Fac(n - r)) : Fac(n - r + 1, n) / Fac(r); } public static Modular Npr(int n, int r) { r = n - r; return Fac(n) / Fac(r); } public static explicit operator int(Modular a) { return (int)a.value; } } class Graph<T> { private List<T>[] G; public Graph(int size) { G = new List<T>[size]; for (int i = 0; i < size; i++) { G[i] = new List<T>(); } } public List<T> this[int i] => G[i]; public void Add(int from, T to) => G[from].Add(to); public List<T>[] ToListArray() => G; public int Length => G.Length; } class Scanner { public static string RString() => Console.ReadLine(); public static int RInt() => ReadTuple<int>(); public static long RLong() => ReadTuple<long>(); public static double RDouble() => ReadTuple<double>(); public static string[] RStrings() => Console.ReadLine().Split(); public static int[] RInts() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] RLongs() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] RDoubles() => Array.ConvertAll(Console.ReadLine().Split(), double.Parse); public static int[] RInts(Func<string, int> func) => Console.ReadLine().Split().Select(func).ToArray(); public static long[] RLongs(Func<string, long> func) => Console.ReadLine().Split().Select(func).ToArray(); public static double[] RDoubles(Func<string, double> func) => Console.ReadLine().Split().Select(func).ToArray(); // public static string[] RStrings(Func<string, string> func) => // Console.ReadLine().Split().Select(func).ToArray(); public static T1 ReadTuple<T1>() { var r = RString(); var r1 = (T1)Convert.ChangeType(r, typeof(T1)); return r1; } public static (T1, T2) ReadTuple<T1, T2>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); return (r1, r2); } public static (T1, T2, T3) ReadTuple<T1, T2, T3>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); var r3 = (T3)Convert.ChangeType(r[2], typeof(T3)); return (r1, r2, r3); } public static (T1, T2, T3, T4) ReadTuple<T1, T2, T3, T4>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); var r3 = (T3)Convert.ChangeType(r[2], typeof(T3)); var r4 = (T4)Convert.ChangeType(r[3], typeof(T4)); return (r1, r2, r3, r4); } }
using System; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; class MainClass : Scanner { static Graph<int> G; static int N; static int K; static bool[] used; static void Main() { (N, K) = ReadTuple<int, int>(); G = new Graph<int>(N); for (int i = 0; i < N - 1; i++) { var r = RInts(_ => int.Parse(_) - 1); G.Add(r[0], r[1]); G.Add(r[1], r[0]); } Modular ans = K; used = new bool[N]; used[0] = true; var que = new Queue<int>(); que.Enqueue(0); while (que.Count > 0) { var now = que.Dequeue(); int ncnt = G[now].Count; if (now == 0) { if (ncnt < 0 || K - 1 < ncnt) ans *= 0; else ans *= Modular.Npr(K - 1, ncnt); } else { if (K - 2 < ncnt - 1) ans *= 0; else ans *= Modular.Npr(K - 2, ncnt - 1); } foreach (var next in G[now]) { if (used[next]) continue; used[next] = true; que.Enqueue(next); } } WriteLine(ans); } } class Modular { private const int M = 1000000007; private const int arysize = 2000001; private long value; public Modular(long value = 0) { this.value = value; } public override string ToString() { return value.ToString(); } 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) => a.value + b.value; public static Modular operator -(Modular a, Modular b) => a.value - b.value; public static Modular operator *(Modular a, Modular b) => a.value * b.value; public static Modular Pow(Modular a, long n) { Modular ans = 1; for (; n > 0; n >>= 1, a *= a) { if ((n & 1) == 1) ans *= a; } return ans; } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, M - 2); } private static int[] facs = new int[arysize]; private static int facscount = -1; public static Modular Fac(int n) //階乗 { facs[0] = 1; while (++facscount <= n) { facs[facscount + 1] = (int)(Math.BigMul(facs[facscount], facscount + 1) % M); } return facs[n]; } public static Modular Fac(int r, int n) //記録しない階乗 { int temp = 1; for (int i = r; i <= n; i++) { temp = (int)(Math.BigMul(temp, i) % M); } return temp; } public static Modular Ncr(int n, int r) // nCr { return (n < r) ? 0 : (n == r) ? 1 : (Math.Max(n, r) <= arysize) ? Fac(n) / (Fac(r) * Fac(n - r)) : Fac(n - r + 1, n) / Fac(r); } public static Modular Npr(int n, int r) { r = n - r; return Fac(n) / Fac(r); } public static explicit operator int(Modular a) { return (int)a.value; } } class Graph<T> { private List<T>[] G; public Graph(int size) { G = new List<T>[size]; for (int i = 0; i < size; i++) { G[i] = new List<T>(); } } public List<T> this[int i] => G[i]; public void Add(int from, T to) => G[from].Add(to); public List<T>[] ToListArray() => G; public int Length => G.Length; } class Scanner { public static string RString() => Console.ReadLine(); public static int RInt() => ReadTuple<int>(); public static long RLong() => ReadTuple<long>(); public static double RDouble() => ReadTuple<double>(); public static string[] RStrings() => Console.ReadLine().Split(); public static int[] RInts() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] RLongs() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] RDoubles() => Array.ConvertAll(Console.ReadLine().Split(), double.Parse); public static int[] RInts(Func<string, int> func) => Console.ReadLine().Split().Select(func).ToArray(); public static long[] RLongs(Func<string, long> func) => Console.ReadLine().Split().Select(func).ToArray(); public static double[] RDoubles(Func<string, double> func) => Console.ReadLine().Split().Select(func).ToArray(); // public static string[] RStrings(Func<string, string> func) => // Console.ReadLine().Split().Select(func).ToArray(); public static T1 ReadTuple<T1>() { var r = RString(); var r1 = (T1)Convert.ChangeType(r, typeof(T1)); return r1; } public static (T1, T2) ReadTuple<T1, T2>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); return (r1, r2); } public static (T1, T2, T3) ReadTuple<T1, T2, T3>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); var r3 = (T3)Convert.ChangeType(r[2], typeof(T3)); return (r1, r2, r3); } public static (T1, T2, T3, T4) ReadTuple<T1, T2, T3, T4>() { var r = RStrings(); var r1 = (T1)Convert.ChangeType(r[0], typeof(T1)); var r2 = (T2)Convert.ChangeType(r[1], typeof(T2)); var r3 = (T3)Convert.ChangeType(r[2], typeof(T3)); var r4 = (T4)Convert.ChangeType(r[3], typeof(T4)); return (r1, r2, r3, r4); } }
[["+", 75, 196, 0, 57, 15, 16, 12, 16, 17, 33], ["+", 75, 196, 0, 57, 15, 16, 12, 16, 12, 203]]
8
1,553
2
using System; using static System.Console; using static System.Math; using System.IO; using System.Collections.Generic; namespace I_am_the_green_coder { class Inonoa { static long[] fac = new long[100001]; static long[] ifac = new long[100001]; // なんか入力用の関数作ったけどまだテストしてないので使う前に確かめて static void Main() { fac[0] = 1; ifac[0] = 1; for (long i = 0; i < 100000; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M) ifac[i + 1] = ifac[i] * Mpow(i + 1, MOD - 2) % MOD; } long[] nk = ILongs(); long[][] ab = ILongLongs(nk[0] - 1); List<long>[] tonari = new List<long>[nk[0]]; for (int i = 0; i < nk[0]; i++) { tonari[i] = new List<long>(); } bool[] moumita = new bool[nk[0]]; for (int i = 0; i < nk[0] - 1; i++) { tonari[ab[i][0] - 1].Add(ab[i][1] - 1); tonari[ab[i][1] - 1].Add(ab[i][0] - 1); } long ans = nk[1]; Queue<long> q = new Queue<long>(); q.Enqueue(0); if (nk[0] == 1) { Console.WriteLine(nk[1]); return; } while (q.Count > 0) { long ima = q.Dequeue(); moumita[ima] = true; if (tonari[ima].Count - 1 > nk[1] - 2) { Console.WriteLine(0); return; } if (ima == 0) { ans *= Perm(nk[1] - 1, tonari[ima].Count); ans %= MOD; } else if (tonari[ima].Count != 1) { ans *= Perm(nk[1] - 2, tonari[ima].Count - 1); ans %= MOD; } foreach (long tnr in tonari[ima]) { if (!moumita[tnr]) { q.Enqueue(tnr); } } } Console.WriteLine(ans); } static long Comb(long a, long b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; long tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } static long Mpow(long x, long n) { long ans = 1; while (n != 0) { if (n % 2 == 1) ans = ans * x % MOD; x = x * x % MOD; n = n / 2; } return ans; } static long Perm(long a, long b) { if (a < b) return 0; long tmp = ifac[a - b] % MOD; return tmp * fac[a] + MOD; } static readonly long MOD = 1000000007; ///< summary>a_1, a_2, a_3, ...みたいなの</summary> static long[] ILongs() { string[] s = ReadLine().Split(); long[] re = new long[s.Length]; for (long i = 0; i < s.Length; i++) { re[i] = long.Parse(s[i]); } return re; } ///< summary>xooxoxoxxoxo...みたいなの</summary> static char[] IChars() { return ReadLine().ToCharArray(); } ///< summary>n m とかをちょっとはやくかくだけ</summary> static string[] IStrings() { return ReadLine().Split(); } ///< summary>n行の入力を取ったり</summary> static long[][] ILongLongs(long n) { long[][] re = new long [n][]; for (long i = 0; i < n; i++) { string[] s = ReadLine().Split(); re[i] = new long[s.Length]; for (long j = 0; j < s.Length; j++) { re[i][j] = long.Parse(s[j]); } } return re; } ///< summary>n行の文字列を取ったり</summary> static char[][] ICharsChars(long n) { char[][] re = new char [n][]; for (long i = 0; i < n; i++) { re[i] = ReadLine().ToCharArray(); } return re; } } }
using System; using static System.Console; using static System.Math; using System.IO; using System.Collections.Generic; namespace I_am_the_green_coder { class Inonoa { static long[] fac = new long[100001]; static long[] ifac = new long[100001]; // なんか入力用の関数作ったけどまだテストしてないので使う前に確かめて static void Main() { fac[0] = 1; ifac[0] = 1; for (long i = 0; i < 100000; i++) { fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M) ifac[i + 1] = ifac[i] * Mpow(i + 1, MOD - 2) % MOD; } long[] nk = ILongs(); long[][] ab = ILongLongs(nk[0] - 1); List<long>[] tonari = new List<long>[nk[0]]; for (int i = 0; i < nk[0]; i++) { tonari[i] = new List<long>(); } bool[] moumita = new bool[nk[0]]; for (int i = 0; i < nk[0] - 1; i++) { tonari[ab[i][0] - 1].Add(ab[i][1] - 1); tonari[ab[i][1] - 1].Add(ab[i][0] - 1); } long ans = nk[1]; Queue<long> q = new Queue<long>(); q.Enqueue(0); if (nk[0] == 1) { Console.WriteLine(nk[1]); return; } while (q.Count > 0) { long ima = q.Dequeue(); moumita[ima] = true; if (tonari[ima].Count - 1 > nk[1] - 2) { Console.WriteLine(0); return; } if (ima == 0) { ans *= Perm(nk[1] - 1, tonari[ima].Count); ans %= MOD; } else if (tonari[ima].Count != 1) { ans *= Perm(nk[1] - 2, tonari[ima].Count - 1); ans %= MOD; } foreach (long tnr in tonari[ima]) { if (!moumita[tnr]) { q.Enqueue(tnr); } } } Console.WriteLine(ans); } static long Comb(long a, long b) { if (a == 0 && b == 0) return 1; if (a < b || a < 0) return 0; long tmp = ifac[a - b] * ifac[b] % MOD; return tmp * fac[a] % MOD; } static long Mpow(long x, long n) { long ans = 1; while (n != 0) { if (n % 2 == 1) ans = ans * x % MOD; x = x * x % MOD; n = n / 2; } return ans; } static long Perm(long a, long b) { if (a < b) return 0; long tmp = ifac[a - b] % MOD; return tmp * fac[a] % MOD; } static readonly long MOD = 1000000007; ///< summary>a_1, a_2, a_3, ...みたいなの</summary> static long[] ILongs() { string[] s = ReadLine().Split(); long[] re = new long[s.Length]; for (long i = 0; i < s.Length; i++) { re[i] = long.Parse(s[i]); } return re; } ///< summary>xooxoxoxxoxo...みたいなの</summary> static char[] IChars() { return ReadLine().ToCharArray(); } ///< summary>n m とかをちょっとはやくかくだけ</summary> static string[] IStrings() { return ReadLine().Split(); } ///< summary>n行の入力を取ったり</summary> static long[][] ILongLongs(long n) { long[][] re = new long [n][]; for (long i = 0; i < n; i++) { string[] s = ReadLine().Split(); re[i] = new long[s.Length]; for (long j = 0; j < s.Length; j++) { re[i][j] = long.Parse(s[j]); } } return re; } ///< summary>n行の文字列を取ったり</summary> static char[][] ICharsChars(long n) { char[][] re = new char [n][]; for (long i = 0; i < n; i++) { re[i] = ReadLine().ToCharArray(); } return re; } } }
[["-", 0, 195, 8, 196, 0, 37, 0, 16, 17, 72], ["+", 0, 195, 8, 196, 0, 37, 0, 16, 17, 109]]
8
975
2
using CompLib.Util; using System; using System.Collections.Generic; using System.Linq; public class Program { int N, Q; List<Edge>[] E; int[] X, U, V; long[] Y; // LCA[i] ノードiが最近共通祖先になるクエリ List<int>[] LCAList; // ノードiがU,Vになるクエリ List<int>[] UVList; // 子に向く辺 List<Edge>[] Tree; // 2^i個上の祖先 List<int>[] _ancestor; int[] Depth; long[] Ans; void Scan() { var sc = new Scanner(); N = sc.NextInt(); Q = sc.NextInt(); E = new List<Edge>[N]; for (int i = 0; i < N; i++) { E[i] = new List<Edge>(); } for (int i = 0; i < N - 1; i++) { int a = sc.NextInt() - 1; int b = sc.NextInt() - 1; int c = sc.NextInt() - 1; long d = sc.NextLong(); E[a].Add(new Edge(b, c, d)); E[b].Add(new Edge(a, c, d)); } X = new int[Q]; Y = new long[Q]; U = new int[Q]; V = new int[Q]; UVList = new List<int>[N]; for (int i = 0; i < N; i++) { UVList[i] = new List<int>(); } for (int i = 0; i < Q; i++) { X[i] = sc.NextInt() - 1; Y[i] = sc.NextLong(); int u = sc.NextInt() - 1; int v = sc.NextInt() - 1; U[i] = u; V[i] = v; UVList[u].Add(i); UVList[v].Add(i); } } void Build() { Tree = new List<Edge>[N]; _ancestor = new List<int>[N]; for (int i = 0; i < N; i++) { Tree[i] = new List<Edge>(); _ancestor[i] = new List<int>(); } Depth = new int[N]; for (int i = 0; i < N; i++) { Depth[i] = -1; } var q = new Queue<int>(); q.Enqueue(0); Depth[0] = 0; while (q.Count > 0) { var d = q.Dequeue(); foreach (var e in E[d]) { if (Depth[e.To] != -1) continue; Tree[d].Add(e); Depth[e.To] = Depth[d] + 1; q.Enqueue(e.To); _ancestor[e.To].Add(d); } int t = Depth[d] / 2; while (t > 0) { int count = _ancestor[d].Count; int last = _ancestor[d][count - 1]; _ancestor[d].Add(_ancestor[last][count - 1]); t /= 2; } } } // ノードlのn個上の祖先 int Ancestor(int l, int n, int index = 0) { if (n == 0) return l; for (int i = index;; i++) { if ((n & (1 << i)) > 0) { return Ancestor(_ancestor[l][i], n - (1 << i), i + 1); } } } int LCA(int l, int r) { // 高さを合わせる int dl = Depth[l]; int dr = Depth[r]; if (dl < dr) { r = Ancestor(r, dr - dl); } else { l = Ancestor(l, dl - dr); } if (l == r) return l; for (int i = _ancestor[l].Count - 1; i >= 0; i--) { if (i >= _ancestor[l].Count) continue; if (_ancestor[l][i] != _ancestor[r][i]) { l = _ancestor[l][i]; r = _ancestor[r][i]; } } return _ancestor[l][0]; } void CalcLCA() { LCAList = new List<int>[N]; for (int i = 0; i < N; i++) { LCAList[i] = new List<int>(); } for (int i = 0; i < Q; i++) { LCAList[LCA(U[i], V[i])].Add(i); } } void Query() { Ans = new long[Q]; var stack = new int[N]; var index = new int[N]; for (int i = 0; i < N; i++) { index[i] = -1; } int ptr = 0; long dist = 0; int[] cnt = new int[N - 1]; long[] sum = new long[N - 1]; while (ptr >= 0) { index[ptr]++; if (index[ptr] < Tree[stack[ptr]].Count) { var e = Tree[stack[ptr]][index[ptr]]; dist += e.D; cnt[e.C]++; sum[e.C] += e.D; stack[ptr + 1] = e.To; ptr++; } else { index[ptr] = -1; foreach (int q in UVList[stack[ptr]]) { Ans[q] += dist - sum[X[q]] + Y[q] * cnt[X[q]]; } foreach (int q in LCAList[stack[ptr]]) { Ans[q] -= dist - sum[X[q]] + Y[q] * cnt[X[q]]; } ptr--; if (ptr >= 0) { var e = Tree[stack[ptr]][index[ptr]]; dist -= e.D; cnt[e.C]--; sum[e.C] -= e.D; } } } } public void Solve() { Scan(); Build(); CalcLCA(); Query(); Console.WriteLine(string.Join("\n", Ans)); } public static void Main(string[] args) => new Program().Solve(); } struct Edge { public int To, C; public long D; public Edge(int to, int c, long d) { To = to; C = c; D = d; } } namespace CompLib.Util { using System; using System.Linq; class Scanner { private int _index; private string[] _line; const char separator = ' '; public Scanner() { _index = 0; _line = new string[0]; } public string Next() { while (_index >= _line.Length) { _line = Console.ReadLine().Split(separator); _index = 0; } return _line[_index++]; } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { _line = Console.ReadLine().Split(separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); } }
using CompLib.Util; using System; using System.Collections.Generic; using System.Linq; public class Program { int N, Q; List<Edge>[] E; int[] X, U, V; long[] Y; // LCA[i] ノードiが最近共通祖先になるクエリ List<int>[] LCAList; // ノードiがU,Vになるクエリ List<int>[] UVList; // 子に向く辺 List<Edge>[] Tree; // 2^i個上の祖先 List<int>[] _ancestor; int[] Depth; long[] Ans; void Scan() { var sc = new Scanner(); N = sc.NextInt(); Q = sc.NextInt(); E = new List<Edge>[N]; for (int i = 0; i < N; i++) { E[i] = new List<Edge>(); } for (int i = 0; i < N - 1; i++) { int a = sc.NextInt() - 1; int b = sc.NextInt() - 1; int c = sc.NextInt() - 1; long d = sc.NextLong(); E[a].Add(new Edge(b, c, d)); E[b].Add(new Edge(a, c, d)); } X = new int[Q]; Y = new long[Q]; U = new int[Q]; V = new int[Q]; UVList = new List<int>[N]; for (int i = 0; i < N; i++) { UVList[i] = new List<int>(); } for (int i = 0; i < Q; i++) { X[i] = sc.NextInt() - 1; Y[i] = sc.NextLong(); int u = sc.NextInt() - 1; int v = sc.NextInt() - 1; U[i] = u; V[i] = v; UVList[u].Add(i); UVList[v].Add(i); } } void Build() { Tree = new List<Edge>[N]; _ancestor = new List<int>[N]; for (int i = 0; i < N; i++) { Tree[i] = new List<Edge>(); _ancestor[i] = new List<int>(); } Depth = new int[N]; for (int i = 0; i < N; i++) { Depth[i] = -1; } var q = new Queue<int>(); q.Enqueue(0); Depth[0] = 0; while (q.Count > 0) { var d = q.Dequeue(); foreach (var e in E[d]) { if (Depth[e.To] != -1) continue; Tree[d].Add(e); Depth[e.To] = Depth[d] + 1; q.Enqueue(e.To); _ancestor[e.To].Add(d); } int t = Depth[d] / 2; while (t > 0) { int count = _ancestor[d].Count; int last = _ancestor[d][count - 1]; _ancestor[d].Add(_ancestor[last][count - 1]); t /= 2; } } } // ノードlのn個上の祖先 int Ancestor(int l, int n, int index = 0) { if (n == 0) return l; for (int i = index;; i++) { if ((n & (1 << i)) > 0) { return Ancestor(_ancestor[l][i], n - (1 << i), i + 1); } } } int LCA(int l, int r) { // 高さを合わせる int dl = Depth[l]; int dr = Depth[r]; if (dl < dr) { r = Ancestor(r, dr - dl); } else { l = Ancestor(l, dl - dr); } if (l == r) return l; for (int i = _ancestor[l].Count - 1; i >= 0; i--) { if (i >= _ancestor[l].Count) continue; if (_ancestor[l][i] != _ancestor[r][i]) { l = _ancestor[l][i]; r = _ancestor[r][i]; } } return _ancestor[l][0]; } void CalcLCA() { LCAList = new List<int>[N]; for (int i = 0; i < N; i++) { LCAList[i] = new List<int>(); } for (int i = 0; i < Q; i++) { LCAList[LCA(U[i], V[i])].Add(i); } } void Query() { Ans = new long[Q]; var stack = new int[N]; var index = new int[N]; for (int i = 0; i < N; i++) { index[i] = -1; } int ptr = 0; long dist = 0; int[] cnt = new int[N - 1]; long[] sum = new long[N - 1]; while (ptr >= 0) { index[ptr]++; if (index[ptr] < Tree[stack[ptr]].Count) { var e = Tree[stack[ptr]][index[ptr]]; dist += e.D; cnt[e.C]++; sum[e.C] += e.D; stack[ptr + 1] = e.To; ptr++; } else { index[ptr] = -1; foreach (int q in UVList[stack[ptr]]) { Ans[q] += dist - sum[X[q]] + Y[q] * cnt[X[q]]; } foreach (int q in LCAList[stack[ptr]]) { Ans[q] -= (dist - sum[X[q]] + Y[q] * cnt[X[q]]) * 2; } ptr--; if (ptr >= 0) { var e = Tree[stack[ptr]][index[ptr]]; dist -= e.D; cnt[e.C]--; sum[e.C] -= e.D; } } } } public void Solve() { Scan(); Build(); CalcLCA(); Query(); Console.WriteLine(string.Join("\n", Ans)); } public static void Main(string[] args) => new Program().Solve(); } struct Edge { public int To, C; public long D; public Edge(int to, int c, long d) { To = to; C = c; D = d; } } namespace CompLib.Util { using System; using System.Linq; class Scanner { private int _index; private string[] _line; const char separator = ' '; public Scanner() { _index = 0; _line = new string[0]; } public string Next() { while (_index >= _line.Length) { _line = Console.ReadLine().Split(separator); _index = 0; } return _line[_index++]; } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { _line = Console.ReadLine().Split(separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); } }
[["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
1,728
4
using System; using System.Collections.Generic; using System.Linq; public class AtCoderA { static void Main() { Dictionary<string, int> dic = new Dictionary<string, int>(); { var line = Console.ReadLine(); for (int i = 0; i < 4; i++) { string str = line.Substring(i, 1); if (dic.ContainsKey(str)) { dic.Add(str, 1); } else { dic[str] = dic[str] + 1; } } } foreach (var item in dic) { if (item.Value != 2) { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } }
using System; using System.Collections.Generic; using System.Linq; public class AtCoderA { static void Main() { Dictionary<string, int> dic = new Dictionary<string, int>(); { var line = Console.ReadLine(); for (int i = 0; i < 4; i++) { string str = line.Substring(i, 1); if (!dic.ContainsKey(str)) { dic.Add(str, 1); } else { dic[str] = dic[str] + 1; } } } foreach (var item in dic) { if (item.Value != 2) { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } }
[["+", 0, 7, 8, 196, 0, 57, 15, 241, 0, 111]]
8
158
1
using System; namespace abc132A { class MainClass { public static void Main(string[] args) { string A = Console.ReadLine(); string[] B = new string[4]; int C = 0; for (int i = 0; i < 4; i++) { B[i] = A.Substring(i, 1); for (int r = 0; r < 4; r++) { B[r] = A.Substring(r, 1); if (B[i] == B[r]) C = C + 1; } } C = (C - 4) / 2; if (C == 2) Console.WriteLine(C); else Console.WriteLine("No"); } } }
using System; namespace abc132A { class MainClass { public static void Main(string[] args) { string A = Console.ReadLine(); string[] B = new string[4]; int C = 0; for (int i = 0; i < 4; i++) { B[i] = A.Substring(i, 1); for (int r = 0; r < 4; r++) { B[r] = A.Substring(r, 1); if (B[i] == B[r]) C = C + 1; } } C = (C - 4) / 2; if (C == 2) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
[["-", 64, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 62], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
159
4
// A - Fifty-Fifty // 20190912 using System; using System.Linq; using System.Collections.Generic; namespace A { class Program { static void Main(string[] args) { // string S = Console.ReadLine(); var S = "HELO"; string ans = "Yes"; for (int i = 0; i < 4; i++) { if (S.Where(s => s == S[i]).Count() != 2) { ans = "No"; } } Console.WriteLine(ans); } } }
// A - Fifty-Fifty // 20190912 using System; using System.Linq; using System.Collections.Generic; namespace A { class Program { static void Main(string[] args) { string S = Console.ReadLine(); string ans = "Yes"; for (int i = 0; i < 4; i++) { if (S.Where(s => s == S[i]).Count() != 2) { ans = "No"; } } Console.WriteLine(ans); } } }
[["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 62], ["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 0, 131], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 3, 4, 0, 24], ["+", 0, 200, 0, 212, 0, 213, 3, 4, 0, 25]]
8
104
10
using System; class fifty { public static void Main() { String str = Console.ReadLine(); checklist c = new checklist(); int kind = 0, count = 0, p = 0; char[] list = new char[str.Length + 1]; for (int i = 0; i < str.Length; i++) { if (!c.check(list, str[i])) { kind++; list[p] = str[i]; p++; } } for (int i = 0; i < str.Length; i++) { if (str[0].Equals(str[i])) { count++; } } if (count == 2 && kind == 2) { Console.WriteLine("YES"); } else { Console.WriteLine("NO"); } } } class checklist { public bool check(char[] list, char c) { for (int i = 0; i < list.Length; i++) { if (list[i].Equals(c)) { return true; } } return false; } }
using System; class fifty { public static void Main() { String str = Console.ReadLine(); checklist c = new checklist(); int kind = 0, count = 0, p = 0; char[] list = new char[str.Length + 1]; for (int i = 0; i < str.Length; i++) { if (!c.check(list, str[i])) { kind++; list[p] = str[i]; p++; } } for (int i = 0; i < str.Length; i++) { if (str[0].Equals(str[i])) { count++; } } if (count == 2 && kind == 2) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } class checklist { public bool check(char[] list, char c) { for (int i = 0; i < list.Length; i++) { if (list[i].Equals(c)) { return true; } } return false; } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
238
4
using System; using System.IO; public class Program { static void Main(string[] args) { string s = Console.ReadLine(); if ((s[0] == s[1]) && (s[0] == s[2]) && (s[0] == s[3])) Console.WriteLine("Yes"); else if (((s[0] == s[1]) && (s[2] == s[3])) || ((s[0] == s[2]) && (s[1] == s[3])) || ((s[0] == s[3]) && (s[1] == s[2]))) Console.WriteLine("Yes"); else Console.WriteLine("No"); } }
using System; using System.IO; public class Program { static void Main(string[] args) { string s = Console.ReadLine(); if ((s[0] == s[1]) && (s[0] == s[2]) && (s[0] == s[3])) Console.WriteLine("No"); else if (((s[0] == s[1]) && (s[2] == s[3])) || ((s[0] == s[2]) && (s[1] == s[3])) || ((s[0] == s[3]) && (s[1] == s[2]))) Console.WriteLine("Yes"); else Console.WriteLine("No"); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
180
2
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { const string ANSUCCESS = "No"; const string SUCCESS = "Yes"; var input = Console.ReadLine(); if (input.Distinct().Count() == 2) { Console.WriteLine(SUCCESS); } Console.WriteLine(ANSUCCESS); } } }
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { const string ANSUCCESS = "No"; const string SUCCESS = "Yes"; var input = Console.ReadLine(); if (input.Distinct().Count() == 2) { Console.WriteLine(SUCCESS); return; } Console.WriteLine(ANSUCCESS); } } }
[["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35]]
8
89
2
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { const string ANSUCCESS = "No"; const string SUCCESS = "Yes"; var input = Console.ReadLine(); var first = input.First(); var input2 = input.Replace(first, ' ').Replace(" ", ""); if (input2.Length != 2) { Console.WriteLine(ANSUCCESS); return; } var input3 = input.Replace(input2.First(), ' ').Replace(" ", ""); if (input3.Length != 0) { Console.WriteLine(ANSUCCESS); return; } Console.WriteLine(SUCCESS); } } }
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { const string ANSUCCESS = "No"; const string SUCCESS = "Yes"; var input = Console.ReadLine(); var first = input.First(); var input2 = input.Replace(first, ' ').Replace(" ", ""); if (input2.Length != 2) { Console.WriteLine(ANSUCCESS); return; } var input3 = input2.Replace(input2.First(), ' ').Replace(" ", ""); if (input3.Length != 0) { Console.WriteLine(ANSUCCESS); return; } Console.WriteLine(SUCCESS); } } }
[["-", 0, 213, 63, 214, 205, 213, 63, 214, 205, 22], ["+", 0, 213, 63, 214, 205, 213, 63, 214, 205, 22]]
8
165
2
using System; class Program { static void Main(string[] args) { // 整数の入力 string S = Console.ReadLine(); // スペース区切りの整数の入力 //出力 if (S[0] == S[1] && S[2] == S[3] && S[0] == S[2]) { Console.WriteLine("no"); } else if (S[0] == S[1] && S[2] == S[3]) { Console.WriteLine("yes"); } else if (S[0] == S[2] && S[1] == S[3]) { Console.WriteLine("yes"); } else if (S[0] == S[3] && S[1] == S[2]) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } } }
using System; class Program { static void Main(string[] args) { // 整数の入力 string S = Console.ReadLine(); // スペース区切りの整数の入力 //出力 if (S[0] == S[1] && S[2] == S[3] && S[0] == S[2]) { Console.WriteLine("No"); } else if (S[0] == S[1] && S[2] == S[3]) { Console.WriteLine("Yes"); } else if (S[0] == S[2] && S[1] == S[3]) { Console.WriteLine("Yes"); } else if (S[0] == S[3] && S[1] == S[2]) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
187
10
using System; using System.Linq; namespace ABC132A { class Program { static void Main(string[] args) { Console.WriteLine( Console.ReadLine().GroupBy(x => x).Any(x => x.Count() == 2) ? "Yes" : "No"); } } }
using System; using System.Linq; namespace ABC132A { class Program { static void Main(string[] args) { Console.WriteLine( Console.ReadLine().GroupBy(x => x).All(x => x.Count() == 2) ? "Yes" : "No"); } } }
[["-", 0, 28, 0, 41, 15, 213, 63, 214, 141, 22], ["+", 0, 28, 0, 41, 15, 213, 63, 214, 141, 22]]
8
66
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main(string[] args) { string s; bool result = false; s = Console.ReadLine(); Dictionary<char, int> dic = new Dictionary<char, int>(); char key = (char)0; foreach (char c in s) { if (dic.ContainsKey(c)) { dic.Add(c, 1); key = c; } else { dic[c]++; } if (dic.Count > 2) { Console.WriteLine("No"); return; } } if (dic[key] == 2) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main(string[] args) { string s; bool result = false; s = Console.ReadLine(); Dictionary<char, int> dic = new Dictionary<char, int>(); char key = (char)0; foreach (char c in s) { if (!dic.ContainsKey(c)) { dic.Add(c, 1); key = c; } else { dic[c]++; } if (dic.Count > 2) { Console.WriteLine("No"); return; } } if (dic[key] == 2) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
[["+", 0, 210, 8, 196, 0, 57, 15, 241, 0, 111]]
8
179
1
using System; using System.Collections.Generic; namespace atCoder { class Program { static void Main(string[] args) { var input = Console.ReadLine(); int dup = 0; List<char> chars = new List<char>(); foreach (char c in input) { if (chars.Contains(c) == false) chars.Add(c); else dup++; } if (dup == 2 && chars.Count == 2) Console.WriteLine("Yes"); else Console.WriteLine("no"); } } }
using System; using System.Collections.Generic; namespace atCoder { class Program { static void Main(string[] args) { var input = Console.ReadLine(); int dup = 0; List<char> chars = new List<char>(); foreach (char c in input) { if (chars.Contains(c) == false) chars.Add(c); else dup++; } if (dup == 2 && chars.Count == 2) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
119
2
using System; using System.Linq; namespace A { class Program { static void Main(string[] args) { // 入力の取得 var s = Console.ReadLine().ToCharArray(); var grouped = s.GroupBy(x => x).Select(x => x.Count()).ToArray(); var answer = grouped.Length == 2 && grouped.All(x => x == 2); // 解答の出力 Console.WriteLine(answer ? "YES" : "NO"); } } }
using System; using System.Linq; namespace A { class Program { static void Main(string[] args) { // 入力の取得 var s = Console.ReadLine().ToCharArray(); var grouped = s.GroupBy(x => x).Select(x => x.Count()).ToArray(); var answer = grouped.Length == 2 && grouped.All(x => x == 2); // 解答の出力 Console.WriteLine(answer ? "Yes" : "No"); } } }
[["-", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["-", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222]]
8
104
4
using System; using System.Linq; namespace testing { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split().Select(int.Parse).ToArray(); int counter = 0; for (int i = 0; i < N - 3; i++) { int[] vs = new int[] { a[i], a[i + 1], a[i + 2] }; Array.Sort(vs); if (vs[1] == a[i + 1]) { counter++; } } Console.WriteLine(counter); } } }
using System; using System.Linq; namespace testing { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split().Select(int.Parse).ToArray(); int counter = 0; for (int i = 0; i < N - 2; i++) { int[] vs = new int[] { a[i], a[i + 1], a[i + 2] }; Array.Sort(vs); if (vs[1] == a[i + 1]) { counter++; } } Console.WriteLine(counter); } } }
[["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203]]
8
153
2
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); long[] pArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long cnt = 0; for (int j = 0; j < n - 2; j++) { if (pArray[j] + 1 != Math.Min(pArray[j], Math.Min(pArray[j + 1], pArray[j + 2])) && pArray[j + 1] != Math.Max(pArray[j], Math.Max(pArray[j + 1], pArray[j + 2]))) { cnt++; } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); long[] pArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long cnt = 0; for (int j = 0; j < n - 2; j++) { if (pArray[j + 1] != Math.Min(pArray[j], Math.Min(pArray[j + 1], pArray[j + 2])) && pArray[j + 1] != Math.Max(pArray[j], Math.Max(pArray[j + 1], pArray[j + 2]))) { cnt++; } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
[["-", 31, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 73]]
8
219
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp15 { class Program { static void Main(string[] args) { Console.ReadLine(); //読み飛ばし string line = Console.ReadLine(); int[] nums = line.Split(',').Select(c => int.Parse(c)).ToArray(); int count = 0; for (int i = 1; i < nums.Length - 1; i++) { if (Math.Min(nums[i], Math.Min(nums[i - 1], nums[i + 1])) != nums[i] && Math.Max(nums[i], Math.Max(nums[i - 1], nums[i + 1])) != nums[i]) { count++; } } Console.WriteLine(count); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp15 { class Program { static void Main(string[] args) { Console.ReadLine(); //読み飛ばし string line = Console.ReadLine(); int[] nums = line.Split(' ').Select(c => int.Parse(c)).ToArray(); int count = 0; for (int i = 1; i < nums.Length - 1; i++) { if (Math.Min(nums[i], Math.Min(nums[i - 1], nums[i + 1])) != nums[i] && Math.Max(nums[i], Math.Max(nums[i - 1], nums[i + 1])) != nums[i]) { count++; } } Console.WriteLine(count); } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 252, 0, 253], ["+", 205, 213, 3, 4, 0, 28, 0, 252, 0, 253]]
8
199
2
using System; using System.Linq; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); int[] p = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int count = 0; for (int i = 1; i <= n - 2; i++) { if ((p[i - 1] - p[i]) * (p[i + 1] - p[i]) > 0) count++; } Console.WriteLine(count); } }
using System; using System.Linq; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); int[] p = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); int count = 0; for (int i = 1; i <= n - 2; i++) { if ((p[i - 1] - p[i]) * (p[i] - p[i + 1]) > 0) count++; } Console.WriteLine(count); } }
[["-", 31, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["-", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
132
4
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(); int N = cin.nextInt(); int[] a = cin.ArrayInt(N, 0); int count = 0; for (int i = 1; i < N - 1; i++) { if (a[i - 1] <= a[i] && a[i] <= a[i + 1]) count++; else if (a[i * 1] <= a[i] && a[i] <= a[i - 1]) count++; } Console.WriteLine(count); } } 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(); int N = cin.nextInt(); int[] a = cin.ArrayInt(N, 0); int count = 0; for (int i = 1; i < N - 1; i++) { if (a[i - 1] < a[i] && a[i] < a[i + 1]) count++; else if (a[i + 1] <= a[i] && a[i] <= a[i - 1]) count++; } Console.WriteLine(count); } } 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; } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 19], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 18], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 19], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 18], ["-", 31, 204, 206, 207, 0, 28, 0, 16, 17, 48], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 72]]
8
795
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC132 { class Program { static void Main(string[] args) { string Str = Console.ReadLine().Trim(); int n = int.Parse(Str); Str = Console.ReadLine().Trim(); string[] StrArr = Str.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); int[] p = StrArr.Select(int.Parse).ToArray(); int cnt = 0; for (int i = 1; i < n - 1; i++) { if ((p[i] > p[i - 1]) || (p[i] > p[i + 1])) { } else if ((p[i] < p[i - 1]) || (p[i] < p[i + 1])) { } else { cnt++; } } Console.WriteLine(cnt); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC132 { class Program { static void Main(string[] args) { string Str = Console.ReadLine().Trim(); int n = int.Parse(Str); Str = Console.ReadLine().Trim(); string[] StrArr = Str.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); int[] p = StrArr.Select(int.Parse).ToArray(); int cnt = 0; for (int i = 1; i < n - 1; i++) { if ((p[i] > p[i - 1]) && (p[i] > p[i + 1])) { } else if ((p[i] < p[i - 1]) && (p[i] < p[i + 1])) { } else { cnt++; } } Console.WriteLine(cnt); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 106], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 8, 196, 0, 57, 75, 57, 15, 16, 17, 106], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 17, 98]]
8
225
4
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { var maxIndex = int.Parse(Console.ReadLine()); var list = Console.ReadLine().Split().Select(x => int.Parse(x)).ToList(); var index = 3; int cnt = 0; while (index < maxIndex) { if (IsMedian(list[index - 1], new int[3] { list[index], list[index], list[index] })) { cnt++; } index++; } Console.WriteLine(cnt); } private static bool IsMedian(int medianVal, int[] list) { var isMax = list.Max() != medianVal; var isMin = list.Min() != medianVal; return isMax && isMin; } } }
using System; using System.Collections.Generic; using System.Linq; namespace AtCoder { class Program { static void Main(string[] args) { var maxIndex = int.Parse(Console.ReadLine()); var list = Console.ReadLine().Split().Select(x => int.Parse(x)).ToList(); var index = 2; int cnt = 0; while (index < maxIndex) { if (IsMedian( list[index - 1], new int[3] { list[index - 2], list[index - 1], list[index] })) { cnt++; } index++; } Console.WriteLine(cnt); } private static bool IsMedian(int medianVal, int[] list) { var isMax = list.Max() != medianVal; var isMin = list.Min() != medianVal; return isMax && isMin; } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
186
6
using System; using System.Linq; using System.Collections.Generic; namespace agc030_a { class Program { static void Main(string[] args) { var text = Console.ReadLine(); var list = text.Split(' ').Select(x => int.Parse(x)).ToList(); var n = list[0]; var a = list[1]; var b = list[2]; var taxi = n * a; if (a < b) { Console.WriteLine(a.ToString()); } else { Console.WriteLine(b.ToString()); } } } }
using System; using System.Linq; using System.Collections.Generic; namespace agc030_a { class Program { static void Main(string[] args) { var text = Console.ReadLine(); var list = text.Split(' ').Select(x => int.Parse(x)).ToList(); var n = list[0]; var a = list[1]; var b = list[2]; var taxi = n * a; if (taxi < b) { Console.WriteLine(taxi.ToString()); } else { Console.WriteLine(b.ToString()); } } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22]]
8
135
4
using System; namespace Atcoder_program_test_Csharp { using System; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]); int A = int.Parse(str[1]); int B = int.Parse(str[2]); int A2 = A * N; if (B >= A) { Console.Write(A); } else Console.Write(B); Console.ReadKey(); } } }
using System; namespace Atcoder_program_test_Csharp { using System; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(' '); // 2つ以上のスペース区切り入力の取得 int N = int.Parse(str[0]); //数値で受け取りたい場合は変換する int A = int.Parse(str[1]); int B = int.Parse(str[2]); int A2 = A * N; if (B >= A2) { Console.Write(A2); } else Console.Write(B); Console.ReadKey(); } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 12, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 22], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22]]
8
118
4
using System; using System.Linq; using System.Collections.Generic; namespace program { class Program { static void Main() { int[] n = Input.ScanIntArray(); Out(Math.Min(n[0], n[1] * n[2])); } static public void Out(object s) { Console.WriteLine(s); } } static class Input { static public int ScanInt() { return int.Parse(Console.ReadLine()); } static public uint ScanUint() { return uint.Parse(Console.ReadLine()); } static public double ScanDouble() { return double.Parse(Console.ReadLine()); } static public long ScanLong() { return long.Parse(Console.ReadLine()); } static public ulong ScanUlong() { return ulong.Parse(Console.ReadLine()); } static public int[] ScanIntArray() { return Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray(); } static public uint[] ScanUintArray() { return Console.ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); } static public double[] ScanDoubleArray() { return Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); } static public long[] ScanLongArray() { return Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); } static public ulong[] ScanUlongArray() { return Console.ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); } static public string ScanStr() { return Console.ReadLine(); } static public string[] ScanStrArray() { return ScanStr().Split(); } } static class MyMath { 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 ulong GCD(ulong a, ulong b) { Func<ulong, ulong, ulong> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static int LCM(int a, int b) { return a * b / GCD(a, b); } public static ulong LCM(ulong a, ulong b) { return a * b / GCD(a, b); } } }
using System; using System.Linq; using System.Collections.Generic; namespace program { class Program { static void Main() { int[] n = Input.ScanIntArray(); Out(Math.Min(n[2], n[0] * n[1])); } static public void Out(object s) { Console.WriteLine(s); } } static class Input { static public int ScanInt() { return int.Parse(Console.ReadLine()); } static public uint ScanUint() { return uint.Parse(Console.ReadLine()); } static public double ScanDouble() { return double.Parse(Console.ReadLine()); } static public long ScanLong() { return long.Parse(Console.ReadLine()); } static public ulong ScanUlong() { return ulong.Parse(Console.ReadLine()); } static public int[] ScanIntArray() { return Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray(); } static public uint[] ScanUintArray() { return Console.ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); } static public double[] ScanDoubleArray() { return Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); } static public long[] ScanLongArray() { return Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); } static public ulong[] ScanUlongArray() { return Console.ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); } static public string ScanStr() { return Console.ReadLine(); } static public string[] ScanStrArray() { return ScanStr().Split(); } } static class MyMath { 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 ulong GCD(ulong a, ulong b) { Func<ulong, ulong, ulong> gcd = null; gcd = (x, y) => y == 0 ? x : gcd(y, x % y); return a > b ? gcd(a, b) : gcd(b, a); } public static int LCM(int a, int b) { return a * b / GCD(a, b); } public static ulong LCM(ulong a, ulong b) { return a * b / GCD(a, b); } } }
[["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["-", 0, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["-", 0, 16, 12, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 16, 12, 204, 206, 207, 0, 28, 0, 203]]
8
594
6
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long[] iArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long N = iArray[0]; long D = iArray[1]; long[,] point = new long[N, D]; int cnt = 0; for (int p = 0; p < N; p++) { long[] tmpArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); for (int t = 0; t < D; t++) { point[p, t] = tmpArray[t]; } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { long sum = 0; for (int z = 0; z < D; z++) { sum += (long)Math.Pow(point[i, z] - point[j, z], 2); } long sqr = (long)Math.Sqrt(sum); if (sum % sqr == 0) { cnt++; } } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long[] iArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long N = iArray[0]; long D = iArray[1]; long[,] point = new long[N, D]; int cnt = 0; for (int p = 0; p < N; p++) { long[] tmpArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); for (int t = 0; t < D; t++) { point[p, t] = tmpArray[t]; } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { long sum = 0; for (int z = 0; z < D; z++) { sum += (long)Math.Pow(point[i, z] - point[j, z], 2); } long b = (long)Math.Sqrt(sum); if (sum == b * b) { cnt++; } } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
[["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 109], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 22], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 48], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 12, 22]]
8
323
8
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long[] iArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long N = iArray[0]; long D = iArray[1]; long[,] point = new long[N, D]; int cnt = 0; for (int p = 0; p < N; p++) { long[] tmpArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); for (int t = 0; t < D; t++) { point[p, t] = tmpArray[t]; } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { long sum = 0; for (int z = 0; z < D; z++) { sum += (long)Math.Pow(point[i, z] - point[j, z], 2); } for (long y = 1; y < sum; y++) { if (y * y == sum) { cnt++; } } // long sqr = (long)Math.Sqrt(sum); // if (sum % sqr == 0) //{ // cnt++; //} } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long[] iArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); long N = iArray[0]; long D = iArray[1]; long[,] point = new long[N, D]; int cnt = 0; for (int p = 0; p < N; p++) { long[] tmpArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray(); for (int t = 0; t < D; t++) { point[p, t] = tmpArray[t]; } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { long sum = 0; for (int z = 0; z < D; z++) { sum += (long)Math.Pow(point[i, z] - point[j, z], 2); } for (long y = 1; y <= sum; y++) { if (y * y == sum) { cnt++; } } // long sqr = (long)Math.Sqrt(sum); // if (sum % sqr == 0) //{ // cnt++; //} } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
8
331
2
using System; using System.Collections.Generic; public class AtCoderB { static void Main() { int n; int d; { var line = Console.ReadLine(); var words = line.Split(' '); n = int.Parse(words[0]); d = int.Parse(words[1]); } int[,] map = new int[n, d]; for (int i = 0; i < n; i++) { var line = Console.ReadLine(); var words = line.Split(' '); for (int j = 0; j < d; i++) { map[i, j] = int.Parse(words[j]); } } int answer = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sum = 0; for (int k = 0; k < d; k++) { sum += (map[i, k] - map[j, k]) * (map[i, k] - map[j, k]); } double sqrt = Math.Sqrt(sum); int sq = (int)Math.Floor(sqrt); if (sq * sq == sum) { answer++; } } } Console.WriteLine(answer); } }
using System; using System.Collections.Generic; public class AtCoderB { static void Main() { int n; int d; { var line = Console.ReadLine(); var words = line.Split(' '); n = int.Parse(words[0]); d = int.Parse(words[1]); } int[,] map = new int[n, d]; for (int i = 0; i < n; i++) { var line = Console.ReadLine(); var words = line.Split(' '); for (int j = 0; j < d; j++) { map[i, j] = int.Parse(words[j]); } } int answer = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sum = 0; for (int k = 0; k < d; k++) { sum += (map[i, k] - map[j, k]) * (map[i, k] - map[j, k]); } double sqrt = Math.Sqrt(sum); int sq = (int)Math.Floor(sqrt); if (sq * sq == sum) { answer++; } } } Console.WriteLine(answer); } }
[["-", 0, 7, 8, 196, 0, 7, 26, 223, 0, 22], ["+", 0, 7, 8, 196, 0, 7, 26, 223, 0, 22]]
8
296
2
using System; class a { public static void Main() { string[] str = Console.ReadLine().Split(' '); int n = int.Parse(str[0]); int d = int.Parse(str[1]); int[,] x = new int[n, d]; for (int i = 0; i < n; i++) { string[] str0 = Console.ReadLine().Split(' '); for (int j = 0; j < d; j++) { x[i, j] = int.Parse(str0[j]); } } int count1 = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int count = 0; for (int k = 0; k < d; k++) { count += (x[i, k] - x[j, k]) * (x[i, k] - x[j, k]); } for (int l = 1; l < 70; l++) { if (count == l * l) { count1++; } } } } Console.WriteLine(count1); } }
using System; class a { public static void Main() { string[] str = Console.ReadLine().Split(' '); int n = int.Parse(str[0]); int d = int.Parse(str[1]); int[,] x = new int[n, d]; for (int i = 0; i < n; i++) { string[] str0 = Console.ReadLine().Split(' '); for (int j = 0; j < d; j++) { x[i, j] = int.Parse(str0[j]); } } int count1 = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int count = 0; for (int k = 0; k < d; k++) { count += (x[i, k] - x[j, k]) * (x[i, k] - x[j, k]); } for (int l = 1; l < 130; l++) { if (count == l * l) { count1++; } } } } Console.WriteLine(count1); } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203]]
8
272
2
using System; class Program { static void Main() { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int D = int.Parse(input[1]); // 入力の格納 int[,] X = new int[N, D]; for (int n = 0; n < N; n++) { input = Console.ReadLine().Split(' '); for (int d = 0; d < D; d++) { X[n, d] = int.Parse(input[d]); } } // 全探索 int count = 0; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { long dist = 0; for (int e = 0; e < D; e++) { dist += (long)Math.Pow(X[i, e] - X[j, e], 2); } if (Math.Pow(Math.Sqrt(dist), 2) == dist) count++; } } Console.WriteLine(count); } }
using System; class Program { static void Main() { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int D = int.Parse(input[1]); // 入力の格納 int[,] X = new int[N, D]; for (int n = 0; n < N; n++) { input = Console.ReadLine().Split(' '); for (int d = 0; d < D; d++) { X[n, d] = int.Parse(input[d]); } } // 全探索 int count = 0; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { long dist = 0; for (int e = 0; e < D; e++) { dist += (long)Math.Pow(X[i, e] - X[j, e], 2); } if (Math.Pow((long)Math.Sqrt(dist), 2) == dist) count++; } } Console.WriteLine(count); } }
[["+", 31, 213, 3, 4, 0, 28, 0, 74, 0, 24], ["+", 31, 213, 3, 4, 0, 28, 0, 74, 39, 199], ["+", 31, 213, 3, 4, 0, 28, 0, 74, 0, 25]]
8
254
3
using System; using System.Collections.Generic; // list用 using System.Text; // StringBuilder用 using System.Collections; using System.Linq; class Program { static decimal result; static List<int>[] s; static int N, D; static void Main() { // N=int.Parse(Console.ReadLine()); string[] tmp = Console.ReadLine().Split(' '); N = int.Parse(tmp[0]); D = int.Parse(tmp[1]); decimal kyori; s = new List<int>[N]; for (int i = 0; i < N; i++) { s[i] = new List<int>(); } for (int j = 0; j < N; j++) { string[] tmp2 = Console.ReadLine().Split(' '); for (int i = 0; i < D; i++) { s[j].Add(int.Parse(tmp2[i])); } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { kyori = 0; for (int k = 0; k < D; k++) { kyori += (s[i][k] - s[j][k]) * (s[i][k] - s[j][k]); } for (int k = 1; k < 21; k++) { if (kyori == k * k) { result++; } } } } Console.WriteLine(result.ToString()); } static void Read() { string[] tmp = Console.ReadLine().Split(' '); // for(int i=0;i<N;i++){s.Add(int.Parse(tmp[i]));} } }
using System; using System.Collections.Generic; // list用 using System.Text; // StringBuilder用 using System.Collections; using System.Linq; class Program { static decimal result; static List<int>[] s; static int N, D; static void Main() { // N=int.Parse(Console.ReadLine()); string[] tmp = Console.ReadLine().Split(' '); N = int.Parse(tmp[0]); D = int.Parse(tmp[1]); decimal kyori; s = new List<int>[N]; for (int i = 0; i < N; i++) { s[i] = new List<int>(); } for (int j = 0; j < N; j++) { string[] tmp2 = Console.ReadLine().Split(' '); for (int i = 0; i < D; i++) { s[j].Add(int.Parse(tmp2[i])); } } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { kyori = 0; for (int k = 0; k < D; k++) { kyori += (s[i][k] - s[j][k]) * (s[i][k] - s[j][k]); } for (int k = 1; k < 100000; k++) { if (kyori == k * k) { result++; } } } } Console.WriteLine(result.ToString()); } static void Read() { string[] tmp = Console.ReadLine().Split(' '); // for(int i=0;i<N;i++){s.Add(int.Parse(tmp[i]));} } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203]]
8
369
2
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace Practice { class Program { static void Main(string[] args) { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } private const int MOD = 1000000007; private const int MAX = 2001; static long[] fac = new long[MAX]; static long[] finv = new long[MAX]; static long[] inv = new long[MAX]; static void Solve() { var n = Input.NextInt(); var d = Input.NextInt(); var x = new int [n][]; for (int i = 0; i < n; ++i) { x[i] = Input.NextInt(d).ToArray(); } var ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; ++j) { int dist = CalcDist(x[i], x[j]); for (int k = 1; k <= 20; ++k) { if (dist == k * k) { ans++; break; } } } } Console.WriteLine(ans); } private static int CalcDist(int[] a, int[] b) { int ans = 0; for (int i = 0; i < a.Length; ++i) { var t = (a[i] - b[i]); ans += t * t; } return ans; } #region よく使う便利関数 static void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 static long COM(long n, long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } static class Input { static char[] separator = { ' ' }; public static bool IsEof { get; set; } static Queue<string> q { get; set; } static Input() { IsEof = false; q = new Queue<string>(); } /// <summary> /// 入力予約 /// </summary> /// <param name="items"></param> public static void SetText(IEnumerable<string> items) { foreach (var item in items) { SetText(item); } } /// <summary> /// 入力予約 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool SetText(string s) { if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } return true; } /// <summary> /// 内部queueに入力からの値をsplitして格納する /// </summary> /// <returns></returns> static bool read() { var s = Console.ReadLine(); if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } if (!q.Any()) return read(); return true; } /// <summary> /// 次のstringを一つ読み込む /// </summary> /// <returns></returns> public static string Next() { if (!q.Any()) { if (!read()) { IsEof = true; return ""; } } return q.Dequeue(); } public static int NextInt() => int.Parse(Next()); public static long NextLong() => long.Parse(Next()); public static double NextDouble() => double.Parse(Next()); public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList(); public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList(); public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList(); public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList(); public static List<string> NextLine() => Console.ReadLine() .Trim() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .ToList(); public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList(); public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList(); public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList(); } private static bool isPrime(long x) { if (x == 2) { return true; } if (x < 2 || x % 2 == 0) { return false; } long i = 3; while (i * i <= x) { if (x % i == 0) { return false; } i = i + 2; } return true; } private static long lcm(long x, long y) { return x / gcd(x, y) * y; } private static long gcd(long x, long y) { return y > 0 ? gcd(y, x % y) : x; } private static long pow(long x, long n) { if (n == 0) { return 1; } long res = pow(x * x % MOD, n / 2); if (n % 2 == 1) { res = res * x % MOD; } return res; } private static void Swap<T>(ref T x, ref T y) { T t = x; x = y; y = t; } /// <summary> /// 指定した値以上の先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値以上の先頭のインデクス</returns> public static int LowerBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) < 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int LowerBound<T>(IReadOnlyList<T> arr, T value) where T : IComparable { return LowerBound(arr, 0, arr.Count, value, Comparer<T>.Default); } /// <summary> /// 指定した値より大きい先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値より大きい先頭のインデクス</returns> public static int UpperBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) <= 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int UpperBound<T>(IReadOnlyList<T> arr, T value) { return UpperBound(arr, 0, arr.Count, value, Comparer<T>.Default); } #endregion } public class DisjointSet { private int[] rank; private int[] p; private int[] size; public DisjointSet(int n) { rank = new int[n]; p = new int[n]; size = new int[n]; for (int i = 0; i < n; ++i) { MakeSet(i); } } public void MakeSet(int x) { p[x] = x; rank[x] = 0; size[x] = 1; } public bool IsSame(int x, int y) { return FindSet(x) == FindSet(y); } public void Unite(int x, int y) { Link(FindSet(x), FindSet(y)); } public void Link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; size[x] += size[y]; size[y] = 0; } else { p[x] = y; size[y] += size[x]; size[x] = 0; if (rank[x] == rank[y]) { rank[y]++; } } } public int FindSet(int x) { if (x != p[x]) { p[x] = FindSet(p[x]); } return p[x]; } public int Size(int x) { int root = FindSet(x); return size[root]; } } }
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace Practice { class Program { static void Main(string[] args) { var sw = new System.IO.StreamWriter( Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } private const int MOD = 1000000007; private const int MAX = 2001; static long[] fac = new long[MAX]; static long[] finv = new long[MAX]; static long[] inv = new long[MAX]; static void Solve() { var n = Input.NextInt(); var d = Input.NextInt(); var x = new int [n][]; for (int i = 0; i < n; ++i) { x[i] = Input.NextInt(d).ToArray(); } var ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; ++j) { int dist = CalcDist(x[i], x[j]); for (int k = 1; k <= 160; ++k) { if (dist == k * k) { ans++; break; } } } } Console.WriteLine(ans); } private static int CalcDist(int[] a, int[] b) { int ans = 0; for (int i = 0; i < a.Length; ++i) { var t = (a[i] - b[i]); ans += t * t; } return ans; } #region よく使う便利関数 static void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 static long COM(long n, long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } static class Input { static char[] separator = { ' ' }; public static bool IsEof { get; set; } static Queue<string> q { get; set; } static Input() { IsEof = false; q = new Queue<string>(); } /// <summary> /// 入力予約 /// </summary> /// <param name="items"></param> public static void SetText(IEnumerable<string> items) { foreach (var item in items) { SetText(item); } } /// <summary> /// 入力予約 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool SetText(string s) { if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } return true; } /// <summary> /// 内部queueに入力からの値をsplitして格納する /// </summary> /// <returns></returns> static bool read() { var s = Console.ReadLine(); if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } if (!q.Any()) return read(); return true; } /// <summary> /// 次のstringを一つ読み込む /// </summary> /// <returns></returns> public static string Next() { if (!q.Any()) { if (!read()) { IsEof = true; return ""; } } return q.Dequeue(); } public static int NextInt() => int.Parse(Next()); public static long NextLong() => long.Parse(Next()); public static double NextDouble() => double.Parse(Next()); public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList(); public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList(); public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList(); public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList(); public static List<string> NextLine() => Console.ReadLine() .Trim() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .ToList(); public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList(); public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList(); public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList(); } private static bool isPrime(long x) { if (x == 2) { return true; } if (x < 2 || x % 2 == 0) { return false; } long i = 3; while (i * i <= x) { if (x % i == 0) { return false; } i = i + 2; } return true; } private static long lcm(long x, long y) { return x / gcd(x, y) * y; } private static long gcd(long x, long y) { return y > 0 ? gcd(y, x % y) : x; } private static long pow(long x, long n) { if (n == 0) { return 1; } long res = pow(x * x % MOD, n / 2); if (n % 2 == 1) { res = res * x % MOD; } return res; } private static void Swap<T>(ref T x, ref T y) { T t = x; x = y; y = t; } /// <summary> /// 指定した値以上の先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値以上の先頭のインデクス</returns> public static int LowerBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) < 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int LowerBound<T>(IReadOnlyList<T> arr, T value) where T : IComparable { return LowerBound(arr, 0, arr.Count, value, Comparer<T>.Default); } /// <summary> /// 指定した値より大きい先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値より大きい先頭のインデクス</returns> public static int UpperBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) <= 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int UpperBound<T>(IReadOnlyList<T> arr, T value) { return UpperBound(arr, 0, arr.Count, value, Comparer<T>.Default); } #endregion } public class DisjointSet { private int[] rank; private int[] p; private int[] size; public DisjointSet(int n) { rank = new int[n]; p = new int[n]; size = new int[n]; for (int i = 0; i < n; ++i) { MakeSet(i); } } public void MakeSet(int x) { p[x] = x; rank[x] = 0; size[x] = 1; } public bool IsSame(int x, int y) { return FindSet(x) == FindSet(y); } public void Unite(int x, int y) { Link(FindSet(x), FindSet(y)); } public void Link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; size[x] += size[y]; size[y] = 0; } else { p[x] = y; size[y] += size[x]; size[x] = 0; if (rank[x] == rank[y]) { rank[y]++; } } } public int FindSet(int x) { if (x != p[x]) { p[x] = FindSet(p[x]); } return p[x]; } public int Size(int x) { int root = FindSet(x); return size[root]; } } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203]]
8
1,934
2
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using static System.Math; using System.Text; namespace Program { public static class ABC133B { static public void Solve() { var N = NN; var D = NN; var XList = new long[N, D]; for (var i = 0; i < N; i++) { for (var j = 0; j < D; j++) { XList[i, j] = NN; } } var ans = 0L; var seisuList = new List<long>(); for (var i = 0; i < 41; i++) { seisuList.Add(i * i); } for (var i = 0; i < N; i++) { for (var j = i + 1; j < N; j++) { var sum = 0L; for (var k = 0; k < D; k++) { sum += (XList[i, k] - XList[j, k]) * (XList[i, k] - XList[j, k]); } if (seisuList.Contains(sum)) ++ans; } } Console.WriteLine(ans); } //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // TEST _/ //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ class BT<T> where T : IComparable { private class Node { public Node left; public Node right; public T val; public bool isBlack; } private Comparison<T> c; private Node root; private bool change; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} private bool Red(Node n) => n != null && !n.isBlack; private bool Black(Node n) => n != null && n.isBlack; private Node RotateL(Node n) { Node m = n.right, t = m.left; m.left = n; n.right = t; return m; } private Node RotateR(Node n) { Node m = n.left, t = m.right; m.right = n; n.left = t; return m; } private Node RotateLR(Node n) { n.left = RotateL(n.left); return RotateR(n); } private Node RotateRL(Node n) { n.right = RotateR(n.right); return RotateL(n); } public void Add(T x) { root = Add(root, x); root.isBlack = true; } private Node Add(Node n, T x) { if (n == null) { change = true; return new Node() { val = x }; } var r = c(x, n.val); if (r < 0) { n.left = Add(n.left, x); return Balance(n); } if (r > 0) { n.right = Add(n.right, x); return Balance(n); } change = false; return n; } private Node Balance(Node n) { if (!change) return n; if (!Black(n)) return n; if (Red(n.left) && Red(n.left.left)) { n = RotateR(n); n.left.isBlack = true; } else if (Red(n.left) && Red(n.left.right)) { n = RotateLR(n); n.left.isBlack = true; } else if (Red(n.right) && Red(n.right.left)) { n = RotateRL(n); n.right.isBlack = true; } else if (Red(n.right) && Red(n.right.right)) { n = RotateL(n); n.right.isBlack = true; } else { change = false; } return n; } public void Remove(T x) { root = Remove(root, x); if (root != null) root.isBlack = true; } private Node Remove(Node n, T x) { if (n == null) { change = false; return n; } var r = c(x, n.val); if (r < 0) { n.left = Remove(n.left, x); return BalanceL(n); } if (r > 0) { n.right = Remove(n.right, x); return BalanceR(n); } if (n.left == null) { change = n.isBlack; return n.right; } n.left = RemoveMax(n.left); n.val = lmax; return BalanceL(n); } private T lmax; private Node RemoveMax(Node n) { if (n.right != null) { n.right = RemoveMax(n.right); return BalanceR(n); } lmax = n.val; change = n.isBlack; return n.left; } private Node BalanceL(Node n) { if (!change) return n; if (Black(n.right) && Red(n.right.left)) { var b = n.isBlack; n = RotateRL(n); n.isBlack = b; n.left.isBlack = true; change = false; } else if (Black(n.right) && Red(n.right.right)) { var b = n.isBlack; n = RotateL(n); n.isBlack = b; n.right.isBlack = true; n.left.isBlack = true; change = false; } else if (Black(n.right)) { change = n.isBlack; n.isBlack = true; n.right.isBlack = false; } else { n = RotateL(n); n.isBlack = true; n.left.isBlack = false; n.left = BalanceL(n.left); change = false; } return n; } private Node BalanceR(Node n) { if (!change) return n; if (Black(n.left) && Red(n.left.right)) { var b = n.isBlack; n = RotateLR(n); n.isBlack = b; n.right.isBlack = true; change = false; } else if (Black(n.left) && Red(n.left.left)) { var b = n.isBlack; n = RotateR(n); n.isBlack = b; n.left.isBlack = true; n.right.isBlack = true; change = false; } else if (Black(n.left)) { change = n.isBlack; n.isBlack = true; n.left.isBlack = false; } else { n = RotateR(n); n.isBlack = true; n.right.isBlack = false; n.right = BalanceR(n.right); change = false; } return n; } public bool Have(T x) { var t = FindUpper(x); return t.Item1 && t.Item2.CompareTo(x) == 0; } public Tuple<bool, T> FindUpper(T x) { var v = FindUpper(root, x); if (v == null) return Tuple.Create(false, default(T)); return v; } private Tuple<bool, T> FindUpper(Node n, T x) { if (n == null) return null; var r = c(x, n.val); if (r < 0) { var v = FindUpper(n.left, x); if (v == null) return Tuple.Create(true, n.val); return v; } else if (r > 0) return FindUpper(n.right, x); else return Tuple.Create(true, x); } public Tuple<bool, T> FindLower(T x) { var v = FindLower(root, x); if (v == null) return Tuple.Create(false, default(T)); return v; } private Tuple<bool, T> FindLower(Node n, T x) { if (n == null) return null; var r = c(x, n.val); if (r < 0) return FindLower(n.left, x); else if (r > 0) { var v = FindLower(n.right, x); if (v == null) return Tuple.Create(true, n.val); return v; } else return Tuple.Create(true, x); } public T Min() { Node n = root, p = null; while (n != null) { p = n; n = n.left; } return p == null ? default(T) : p.val; } public T Max() { Node n = root, p = null; while (n != null) { p = n; n = n.right; } return p == null ? default(T) : p.val; } public bool Any() => root != null; public int CountSlow() => List(root).Count(); public IEnumerable<T> List() => List(root); private IEnumerable<T> List(Node n) { if (n == null) yield break; foreach (var i in List(n.left)) yield return i; yield return n.val; foreach (var i in List(n.right)) yield return i; } } //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // TEST END _/ //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } Solve(); Console.Out.Flush(); } static Random rand = new Random(); static class Console_ { private static Queue<string> param = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => rand.Next()); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); class PQ<T> where T : IComparable { private List<T> h; private Comparison<T> c; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 Peek => h[0]; public int Count => h.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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<TKey, TValue> where TKey : IComparable { private PQ<Tuple<TKey, TValue>> q; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>((x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Push(TKey k, TValue v) => q.Push(Tuple.Create(k, v)); public Tuple<TKey, TValue> Peek => q.Peek; public int Count => q.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<TKey, TValue> Pop() => q.Pop(); } public class UF { private long[] d; [MethodImpl(MethodImplOptions.AggressiveInlining)] public UF(long s) { d = Repeat(-1L, s).ToArray(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsSame(long x, long y) => Root(x) == Root(y); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Count(long x) => -d[Root(d[x])]; } struct Mod : IEquatable<object> { static public long _mod = 1000000007; private long _val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mod(long x) { if (x < _mod && x >= 0) _val = x; else if ((_val = x % _mod) < 0) _val += _mod; } static public implicit operator Mod(long x) => new Mod(x); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator long(Mod x) => x._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator +(Mod x, Mod y) => x._val + y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator -(Mod x, Mod y) => x._val - y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator *(Mod x, Mod y) => x._val * y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator /(Mod x, Mod y) => x._val * Inverse(y._val); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator ==(Mod x, Mod y) => x._val == y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator !=(Mod x, Mod y) => x._val != y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] bool IEquatable<object>.Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Mod obj) => obj == null ? false : _val == obj._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _val.GetHashCode(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => _val.ToString(); static private List<Mod> _fact = new List<Mod>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static private void Build(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod Comb(long n, long k) { Build(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]; } } struct Mat<T> { private T[,] m; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mat(T[,] v) { m = (T[,])v.Clone(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); [MethodImpl(MethodImplOptions.AggressiveInlining)] static T Pow<T>(T x, long y) { T a = (dynamic)1; while (y != 0) { if ((y & 1) == 1) a *= (dynamic)x; x *= (dynamic)x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void _Build(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static long Comb(long n, long k) { _Build(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]; } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using static System.Math; using System.Text; namespace Program { public static class ABC133B { static public void Solve() { var N = NN; var D = NN; var XList = new long[N, D]; for (var i = 0; i < N; i++) { for (var j = 0; j < D; j++) { XList[i, j] = NN; } } var ans = 0L; var seisuList = new List<long>(); for (var i = 0; i < 10000; i++) { seisuList.Add(i * i); } for (var i = 0; i < N; i++) { for (var j = i + 1; j < N; j++) { var sum = 0L; for (var k = 0; k < D; k++) { sum += (XList[i, k] - XList[j, k]) * (XList[i, k] - XList[j, k]); } if (seisuList.Contains(sum)) ++ans; } } Console.WriteLine(ans); } //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // TEST _/ //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ class BT<T> where T : IComparable { private class Node { public Node left; public Node right; public T val; public bool isBlack; } private Comparison<T> c; private Node root; private bool change; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} private bool Red(Node n) => n != null && !n.isBlack; private bool Black(Node n) => n != null && n.isBlack; private Node RotateL(Node n) { Node m = n.right, t = m.left; m.left = n; n.right = t; return m; } private Node RotateR(Node n) { Node m = n.left, t = m.right; m.right = n; n.left = t; return m; } private Node RotateLR(Node n) { n.left = RotateL(n.left); return RotateR(n); } private Node RotateRL(Node n) { n.right = RotateR(n.right); return RotateL(n); } public void Add(T x) { root = Add(root, x); root.isBlack = true; } private Node Add(Node n, T x) { if (n == null) { change = true; return new Node() { val = x }; } var r = c(x, n.val); if (r < 0) { n.left = Add(n.left, x); return Balance(n); } if (r > 0) { n.right = Add(n.right, x); return Balance(n); } change = false; return n; } private Node Balance(Node n) { if (!change) return n; if (!Black(n)) return n; if (Red(n.left) && Red(n.left.left)) { n = RotateR(n); n.left.isBlack = true; } else if (Red(n.left) && Red(n.left.right)) { n = RotateLR(n); n.left.isBlack = true; } else if (Red(n.right) && Red(n.right.left)) { n = RotateRL(n); n.right.isBlack = true; } else if (Red(n.right) && Red(n.right.right)) { n = RotateL(n); n.right.isBlack = true; } else { change = false; } return n; } public void Remove(T x) { root = Remove(root, x); if (root != null) root.isBlack = true; } private Node Remove(Node n, T x) { if (n == null) { change = false; return n; } var r = c(x, n.val); if (r < 0) { n.left = Remove(n.left, x); return BalanceL(n); } if (r > 0) { n.right = Remove(n.right, x); return BalanceR(n); } if (n.left == null) { change = n.isBlack; return n.right; } n.left = RemoveMax(n.left); n.val = lmax; return BalanceL(n); } private T lmax; private Node RemoveMax(Node n) { if (n.right != null) { n.right = RemoveMax(n.right); return BalanceR(n); } lmax = n.val; change = n.isBlack; return n.left; } private Node BalanceL(Node n) { if (!change) return n; if (Black(n.right) && Red(n.right.left)) { var b = n.isBlack; n = RotateRL(n); n.isBlack = b; n.left.isBlack = true; change = false; } else if (Black(n.right) && Red(n.right.right)) { var b = n.isBlack; n = RotateL(n); n.isBlack = b; n.right.isBlack = true; n.left.isBlack = true; change = false; } else if (Black(n.right)) { change = n.isBlack; n.isBlack = true; n.right.isBlack = false; } else { n = RotateL(n); n.isBlack = true; n.left.isBlack = false; n.left = BalanceL(n.left); change = false; } return n; } private Node BalanceR(Node n) { if (!change) return n; if (Black(n.left) && Red(n.left.right)) { var b = n.isBlack; n = RotateLR(n); n.isBlack = b; n.right.isBlack = true; change = false; } else if (Black(n.left) && Red(n.left.left)) { var b = n.isBlack; n = RotateR(n); n.isBlack = b; n.left.isBlack = true; n.right.isBlack = true; change = false; } else if (Black(n.left)) { change = n.isBlack; n.isBlack = true; n.left.isBlack = false; } else { n = RotateR(n); n.isBlack = true; n.right.isBlack = false; n.right = BalanceR(n.right); change = false; } return n; } public bool Have(T x) { var t = FindUpper(x); return t.Item1 && t.Item2.CompareTo(x) == 0; } public Tuple<bool, T> FindUpper(T x) { var v = FindUpper(root, x); if (v == null) return Tuple.Create(false, default(T)); return v; } private Tuple<bool, T> FindUpper(Node n, T x) { if (n == null) return null; var r = c(x, n.val); if (r < 0) { var v = FindUpper(n.left, x); if (v == null) return Tuple.Create(true, n.val); return v; } else if (r > 0) return FindUpper(n.right, x); else return Tuple.Create(true, x); } public Tuple<bool, T> FindLower(T x) { var v = FindLower(root, x); if (v == null) return Tuple.Create(false, default(T)); return v; } private Tuple<bool, T> FindLower(Node n, T x) { if (n == null) return null; var r = c(x, n.val); if (r < 0) return FindLower(n.left, x); else if (r > 0) { var v = FindLower(n.right, x); if (v == null) return Tuple.Create(true, n.val); return v; } else return Tuple.Create(true, x); } public T Min() { Node n = root, p = null; while (n != null) { p = n; n = n.left; } return p == null ? default(T) : p.val; } public T Max() { Node n = root, p = null; while (n != null) { p = n; n = n.right; } return p == null ? default(T) : p.val; } public bool Any() => root != null; public int CountSlow() => List(root).Count(); public IEnumerable<T> List() => List(root); private IEnumerable<T> List(Node n) { if (n == null) yield break; foreach (var i in List(n.left)) yield return i; yield return n.val; foreach (var i in List(n.right)) yield return i; } } //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // TEST END _/ //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } Solve(); Console.Out.Flush(); } static Random rand = new Random(); static class Console_ { private static Queue<string> param = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => rand.Next()); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); class PQ<T> where T : IComparable { private List<T> h; private Comparison<T> c; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 Peek => h[0]; public int Count => h.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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<TKey, TValue> where TKey : IComparable { private PQ<Tuple<TKey, TValue>> q; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<TKey> c, bool asc = true) { q = new PQ<Tuple<TKey, TValue>>((x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Push(TKey k, TValue v) => q.Push(Tuple.Create(k, v)); public Tuple<TKey, TValue> Peek => q.Peek; public int Count => q.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<TKey, TValue> Pop() => q.Pop(); } public class UF { private long[] d; [MethodImpl(MethodImplOptions.AggressiveInlining)] public UF(long s) { d = Repeat(-1L, s).ToArray(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsSame(long x, long y) => Root(x) == Root(y); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Count(long x) => -d[Root(d[x])]; } struct Mod : IEquatable<object> { static public long _mod = 1000000007; private long _val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mod(long x) { if (x < _mod && x >= 0) _val = x; else if ((_val = x % _mod) < 0) _val += _mod; } static public implicit operator Mod(long x) => new Mod(x); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator long(Mod x) => x._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator +(Mod x, Mod y) => x._val + y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator -(Mod x, Mod y) => x._val - y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator *(Mod x, Mod y) => x._val * y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator /(Mod x, Mod y) => x._val * Inverse(y._val); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator ==(Mod x, Mod y) => x._val == y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator !=(Mod x, Mod y) => x._val != y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] bool IEquatable<object>.Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Mod obj) => obj == null ? false : _val == obj._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _val.GetHashCode(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => _val.ToString(); static private List<Mod> _fact = new List<Mod>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static private void Build(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod Comb(long n, long k) { Build(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]; } } struct Mat<T> { private T[,] m; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mat(T[,] v) { m = (T[,])v.Clone(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); [MethodImpl(MethodImplOptions.AggressiveInlining)] static T Pow<T>(T x, long y) { T a = (dynamic)1; while (y != 0) { if ((y & 1) == 1) a *= (dynamic)x; x *= (dynamic)x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void _Build(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static long Comb(long n, long k) { _Build(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]; } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203]]
8
5,811
2
using System; using System.Linq; using System.Collections.Generic; using System.IO; using static System.Console; namespace atcorder2 { class Program { public static long Read() { return long.Parse(ReadLine()); } public static long[] Reads() { return ReadLine().Split().Select(long.Parse).ToArray(); } public static long gcd(long x, long y) { if (y < x) gcd(y, x); while (y != 0) { var z = x % y; x = y; y = z; } return x; } public static long C(long n, long k) { long x = 1; for (long i = 1; i <= k; i++) { x *= n - i + 1; x /= i; } return x; } static void Main(string[] args) { var sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); var q = Reads(); var l = q[0]; var r = q[1]; long ans = long.MaxValue; if (r - l <= 2019) { for (long i = l; i <= r; i++) { for (long j = l + 1; j <= r; j++) { ans = Math.Min(ans, i * j % 2019); } } WriteLine(ans); } else WriteLine(0); Console.Out.Flush(); } } }
using System; using System.Linq; using System.Collections.Generic; using System.IO; using static System.Console; namespace atcorder2 { class Program { public static long Read() { return long.Parse(ReadLine()); } public static long[] Reads() { return ReadLine().Split().Select(long.Parse).ToArray(); } public static long gcd(long x, long y) { if (y < x) gcd(y, x); while (y != 0) { var z = x % y; x = y; y = z; } return x; } public static long C(long n, long k) { long x = 1; for (long i = 1; i <= k; i++) { x *= n - i + 1; x /= i; } return x; } static void Main(string[] args) { var sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); var q = Reads(); var l = q[0]; var r = q[1]; long ans = long.MaxValue; if (r - l <= 2019) { for (long i = l; i <= r; i++) { for (long j = i + 1; j <= r; j++) { ans = Math.Min(ans, i * j % 2019); } } WriteLine(ans); } else WriteLine(0); Console.Out.Flush(); } } }
[["-", 10, 198, 0, 200, 0, 212, 0, 16, 31, 22], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 31, 22]]
8
325
2
using System; namespace C { class Program { static void Main(string[] args) { string[] In = Console.ReadLine().Split(' '); int l = int.Parse(In[0]); int r = int.Parse(In[1]); r = (int)Math.Min(r, l + 4038); //これでl~rの範囲が大きくても絞れる long ans = 2018; for (long i = l; i <= r; i++) { for (long j = i + 1; j <= r; j++) { long x = (int)(i * j) % 2019; ans = (int)Math.Min(ans, x); } } Console.WriteLine(ans); } } }
using System; namespace C { class Program { static void Main(string[] args) { string[] In = Console.ReadLine().Split(' '); int l = int.Parse(In[0]); int r = int.Parse(In[1]); r = (int)Math.Min(r, l + 4038); //これでl~rの範囲が大きくても絞れる long ans = 2018; for (long i = l; i <= r; i++) { for (long j = i + 1; j <= r; j++) { long x = (i * j) % 2019; ans = (int)Math.Min(ans, x); } } Console.WriteLine(ans); } } }
[["-", 0, 200, 0, 212, 0, 16, 31, 74, 39, 199], ["-", 0, 200, 0, 212, 0, 16, 31, 74, 0, 25], ["-", 0, 212, 0, 16, 31, 74, 51, 23, 0, 24]]
8
157
3
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; using static Math2; using static io; using static Utils; public class CP { int[] d = new int[] { 1, 0, -1, 0, 1 }; public void Solve() { var o = Ints; var l = o[0]; var r = o[1]; if (r - l > 2020) { Put("0"); return; } int ans = INF; for (int i = l; i <= r; ++i) { for (int j = i + 1; j <= r; ++j) { ans = Min(ans, (i * j) % 2019); } } Put(ans); } } public class BinarySearch { public static int Lower_Bound<T>(IList<T> Array, T Target) { return Lower_Bound<T>(Array, Target, Comparer<T>.Default.Compare); } public static int Lower_Bound<T>(IList<T> Array, T Target, Comparison<T> Comp) { var cmp = Comparer<T>.Create(Comp); var l = -1; // always ng var r = Array.Count(); // always ok while (r - l > 1) { var mid = l + (r - l) / 2; var res = cmp.Compare(Array[mid], Target); if (res >= 0) r = mid; else l = mid; } return r; } public static int Upper_Bound<T>(IList<T> Array, T Target) { return Upper_Bound<T>(Array, Target, Comparer<T>.Default.Compare); } public static int Upper_Bound<T>(IList<T> Array, T Target, Comparison<T> Comp) { var cmp = Comparer<T>.Create(Comp); var l = -1; var r = Array.Count; while (r - l > 1) { var mid = l + (r - l) / 2; var res = cmp.Compare(Array[mid], Target); if (res > 0) r = mid; else l = mid; } return r; } } 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()); } } public class PriorityQueue<T> { List<T> _item; public int Count { get { return _item.Count; } } bool _isascend { get; set; } public T Peek { get { return _item[0]; } } Comparison<T> Comp; public PriorityQueue(bool IsAscend = true, IEnumerable<T> list = null) : this(Comparer<T>.Default.Compare, IsAscend, list) {} public PriorityQueue(Comparison<T> cmp, bool IsAscend = true, IEnumerable<T> list = null) { _item = new List<T>(); _isascend = IsAscend; this.Comp = cmp; if (list != null) { _item.AddRange(list); Build(); } } private int Compare(int i, int j) => (_isascend ? -1 : 1) * Comp(_item[i], _item[j]); private void Swap(int i, int j) { var t = _item[i]; _item[i] = _item[j]; _item[j] = t; } private int Parent(int i) => (i - 1) >> 1; private int Left(int i) => (i << 1) + 1; public T Enqueue(T val) { int i = _item.Count; _item.Add(val); while (i > 0) { int p = Parent(i); if (Compare(i, p) > 0) Swap(i, p); i = p; } return val; } private void Heapify(int index) { for (int i = index, j; (j = Left(i)) < _item.Count; i = j) { if (j != _item.Count - 1 && Compare(j, j + 1) < 0) j++; if (Compare(i, j) < 0) Swap(i, j); } } public T Dequeue() { T val = _item[0]; _item[0] = _item[_item.Count - 1]; _item.RemoveAt(_item.Count - 1); Heapify(0); return val; } private void Build() { for (var i = (_item.Count >> 1) - 1; i >= 0; i--) Heapify(i); } } class Program { static void Main(string[] args) { var CP = new CP(); CP.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.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; using static Math2; using static io; using static Utils; public class CP { int[] d = new int[] { 1, 0, -1, 0, 1 }; public void Solve() { var o = Longs; var l = o[0]; var r = o[1]; if (r - l >= 2020) { Put("0"); return; } long ans = INF; for (long i = l; i <= r; ++i) { for (long j = i + 1; j <= r; ++j) { ans = Min(ans, (i * j) % 2019); } } Put(ans); } } public class BinarySearch { public static int Lower_Bound<T>(IList<T> Array, T Target) { return Lower_Bound<T>(Array, Target, Comparer<T>.Default.Compare); } public static int Lower_Bound<T>(IList<T> Array, T Target, Comparison<T> Comp) { var cmp = Comparer<T>.Create(Comp); var l = -1; // always ng var r = Array.Count(); // always ok while (r - l > 1) { var mid = l + (r - l) / 2; var res = cmp.Compare(Array[mid], Target); if (res >= 0) r = mid; else l = mid; } return r; } public static int Upper_Bound<T>(IList<T> Array, T Target) { return Upper_Bound<T>(Array, Target, Comparer<T>.Default.Compare); } public static int Upper_Bound<T>(IList<T> Array, T Target, Comparison<T> Comp) { var cmp = Comparer<T>.Create(Comp); var l = -1; var r = Array.Count; while (r - l > 1) { var mid = l + (r - l) / 2; var res = cmp.Compare(Array[mid], Target); if (res > 0) r = mid; else l = mid; } return r; } } 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()); } } public class PriorityQueue<T> { List<T> _item; public int Count { get { return _item.Count; } } bool _isascend { get; set; } public T Peek { get { return _item[0]; } } Comparison<T> Comp; public PriorityQueue(bool IsAscend = true, IEnumerable<T> list = null) : this(Comparer<T>.Default.Compare, IsAscend, list) {} public PriorityQueue(Comparison<T> cmp, bool IsAscend = true, IEnumerable<T> list = null) { _item = new List<T>(); _isascend = IsAscend; this.Comp = cmp; if (list != null) { _item.AddRange(list); Build(); } } private int Compare(int i, int j) => (_isascend ? -1 : 1) * Comp(_item[i], _item[j]); private void Swap(int i, int j) { var t = _item[i]; _item[i] = _item[j]; _item[j] = t; } private int Parent(int i) => (i - 1) >> 1; private int Left(int i) => (i << 1) + 1; public T Enqueue(T val) { int i = _item.Count; _item.Add(val); while (i > 0) { int p = Parent(i); if (Compare(i, p) > 0) Swap(i, p); i = p; } return val; } private void Heapify(int index) { for (int i = index, j; (j = Left(i)) < _item.Count; i = j) { if (j != _item.Count - 1 && Compare(j, j + 1) < 0) j++; if (Compare(i, j) < 0) Swap(i, j); } } public T Dequeue() { T val = _item[0]; _item[0] = _item[_item.Count - 1]; _item.RemoveAt(_item.Count - 1); Heapify(0); return val; } private void Build() { for (var i = (_item.Count >> 1) - 1; i >= 0; i--) Heapify(i); } } class Program { static void Main(string[] args) { var CP = new CP(); CP.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, 197, 0, 198, 0, 200, 0, 212, 0, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 22], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 20], ["-", 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], ["-", 0, 7, 8, 196, 0, 7, 10, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 7, 10, 198, 39, 199]]
8
1,931
10