Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.*; import java.io.*; import java.math.BigInteger; /** * * @author rohan */ public class Main { static int mod = (int)1e9+7; static void solve() { char[] c = sc.nextLine().toCharArray(); int n = sc.nextInt(); int x=0,y=0; if(c[0]==94) x=0; else if(c[0]==62) x=1; else if(c[0]==118) x=2; else x=3; if(c[2]==94) y=0; else if(c[2]==62) y=1; else if(c[2]==118) y=2; else y=3; int dif = x-y; if((n&1)==0) out.println("undefined"); else { n=n%4; if(((x+n)%4)==y) { out.println("cw"); } else out.println("ccw"); } out.close(); } static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { new Thread(null,new Runnable() { @Override public void run() { try{ solve(); } catch(Exception e){ e.printStackTrace(); } } },"1",1<<26).start(); } static class Pair implements Comparable<Pair>{ int x,y; Pair (int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair o) { return -Integer.compare(this.x,o.x); } @Override public String toString() { return x + " "+ y ; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
d = {} d[chr(118)] = 0 d[chr(60)] = 1 d[chr(94)] = 2 d[chr(62)] = 3 s = input() i = d[s[0]] f = d[s[2]] delta = int(input())%4 if (i+1)%4 == f or (i-1)%4 == f: if (i+delta)%4 == f: print("cw") else: print("ccw") else: print("undefined")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.*; import java.util.Scanner; public class Codeforces2 { public static void main(String[] args) { // TODO Auto-generated method stub Character a[]=new Character[4]; a[0]='^'; a[1]='>'; a[2]='v'; a[3]='<'; Scanner scn=new Scanner(System.in); Character start=scn.next().charAt(0); Character end=scn.next().charAt(0); int n=scn.nextInt(); int rem=n%4; int index=0; for(int i=0;i<4;i++){ if(a[i]==start){ index=i; break; } } int cw=(index+rem)%4; int ccw=(index-rem+4)%4; String ans=""; if(end==a[cw]){ ans="cw"; } if(end==a[ccw]){ ans="ccw"; } if(end==a[cw] && end==a[ccw]){ ans="undefined"; } System.out.println(ans); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
# https://codeforces.com/problemset/problem/834/A # 900 s, e = map(ord, input().split()) n = int(input()) c = [94, 62, 118, 60] if n % 2 == 0: print("undefined") else: si = c.index(s) ei = c.index(e) d = ei - si if d == -3 or d == 1: if n == 1: print("cw") else: if int((n-1) / 2) % 2 == 1: print("ccw") else: print("cw") elif d == 3 or d == -1: if n == 1: print("ccw") else: if int((n-1) / 2) % 2 == 1: print("cw") else: print("ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; const int INF = 1234567890; string sa, sb; int cnt; map<char, int> arrows; int main() { cin >> sa >> sb; cin >> cnt; char ca = sa[0], cb = sb[0]; arrows['^'] = 0; arrows['>'] = 1; arrows['v'] = 2; arrows['<'] = 3; int bgn = arrows[ca], ed = arrows[cb]; int cw = (bgn + cnt) % 4; int ccw = (bgn + (cnt * 3)) % 4; if ((cnt % 4) % 2 == 0) { cout << "undefined" << endl; } else if (cw == ed) { cout << "cw" << endl; } else { cout << "ccw" << endl; } }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
a, b = input().split(' ') n = int(input()) d = {'v': 0, '>': 1, '^': 2, '<': 3} a, b = d[a], d[b] ccw = bool((a + n) % 4 == b) cw = bool((a - n) % 4 == b) if cw and not ccw: print('cw') elif ccw and not cw: print('ccw') else: print('undefined')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
def ri(): return int(input()) def rli(): return list(map(int, input().split())) def ris(): return list(input()) def pli(a): return "".join(list(map(str, a))) def plis(a): return " ".join(list(map(str, a))) di = ["v", "<", "^", ">"] s, e = input().split() n = ri() % 4 s = di.index(s) e = di.index(e) iscw = True if(n == 0 or n == 2): print("undefined") elif(n == 1): if(s-e == 1 or s-e == -3): iscw = False print("cw" if iscw else "ccw") else: if(e-s == 1 or e-s == -3): iscw = False print("cw" if iscw else "ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } using namespace std; const int maxn = 1e6 + 5; int main() { char a, b; scanf("%c %c", &a, &b); int ans1; if (a == '^') { if (b == '^') ans1 = 0; else if (b == '>') ans1 = 1; else if (b == 'v') ans1 = 2; else if (b == '<') ans1 = 3; } else if (a == '>') { if (b == '>') ans1 = 0; else if (b == 'v') ans1 = 1; else if (b == '<') ans1 = 2; else if (b == '^') ans1 = 3; } else if (a == 'v') { if (b == 'v') ans1 = 0; else if (b == '<') ans1 = 1; else if (b == '^') ans1 = 2; else if (b == '>') ans1 = 3; } else if (a == '<') { if (b == '<') ans1 = 0; else if (b == '^') ans1 = 1; else if (b == '>') ans1 = 2; else if (b == 'v') ans1 = 3; } int ans2; scanf("%d", &ans2); ans2 %= 4; if (ans1 == 1 || ans1 == 3) { if (ans1 == ans2) cout << "cw" << endl; else if (abs(ans1 - ans2) == 2) cout << "ccw" << endl; else cout << "undefined" << endl; } else { cout << "undefined" << "\n"; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.*; public class A834 { public static void main(String[] args) { char[] sym = {'v', '<', '^', '>'}; Scanner scan = new Scanner(System.in); String str = scan.nextLine(); int num = scan.nextInt(), index = 0; num %= 4; if(num % 2 == 0) System.out.println("undefined"); else{ for(int i = 0; i < sym.length; i++) if(str.charAt(0) == sym[i]) index = i; System.out.println(sym[(index+num)%4] == str.charAt(2) ? "cw" : "ccw"); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; void rotate(void) { long long n, i, j, k, m; string toy = "v<^>"; char c1, c2, cw, ccw; cin >> c1 >> c2 >> n; if (n % 4 == 0 || n % 4 == 2) { cout << "undefined\n"; } else { n = n % 4; for (i = 0; i < 4; i++) { if (toy[i] == c1) { j = i; break; } } m = n; k = i; while (m--) { k++; k = k % 4; } cw = toy[k]; m = n; k = i; while (m--) { k--; if (k < 0) { k = 3; } } ccw = toy[k]; if (cw == c2) { cout << "cw\n"; } else if (ccw == c2) { cout << "ccw\n"; } else { cout << "undefined\n"; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); rotate(); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
a,b = raw_input().split() n = input() if a == '^' : if b == '>' : if n%4 == 1 : print 'cw' elif n%4 == 3 : print 'ccw' elif b == '<' : if n%4 == 1 : print 'ccw' elif n%4 == 3 : print 'cw' elif b == 'v' or b == '^': print 'undefined' elif a == '>' : if b == '^' : if n%4 == 1 : print 'ccw' elif n%4 == 3 : print 'cw' elif b == '<' or b == '>' : print 'undefined' elif b == 'v' : if n%4 == 1 : print 'cw' elif n%4 == 3 : print 'ccw' elif a == '<' : if b == '^' : if n%4 == 1 : print 'cw' elif n%4 == 3 : print 'ccw' elif b == '>' or b == '<' : print 'undefined' elif b == 'v' : if n%4 == 1 : print 'ccw' elif n%4 == 3 : print 'cw' elif a == 'v' : if b == '^' or b == 'v' : print 'undefined' elif b == '>' : if n%4 == 1 : print 'ccw' elif n%4 == 3 : print 'cw' elif b == '<' : if n%4 == 1 : print 'cw' elif n%4 == 3 : print 'ccw'
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import sys #read in information from std input ls = ['v', '<', '^', '>'] answer = '' hw = [] seconds = 0 #################################################### # When submitting for real, comment the next line, # uncomment when developing. #sys.stdin = open("../input/input3") #################################################### for i in sys.stdin.readline().split(' '): hw.append(i.strip()) seconds = int(sys.stdin.readline().strip())%4 pos = 0 #Get index of first position for x in xrange(4): if ls[x] == hw[0]: pos = x right = False left = False #check left if hw[1] == ls[pos-seconds]: answer = 'ccw' left = True #check right if hw[1] == ls[(pos+seconds)%4]: answer = 'cw' right = True if right and left: answer = 'undefined' print answer,
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
k, m = input().split() n = int(input()) n %= 4 a = ['v', '<', '^', '>', 'v', '<', '^', '>'] if n == 2 or n==0: print('undefined') if n == 1: if m == a[a.index(k) + 1]: print('cw') else: print('ccw') if n == 3: if m == a[a.index(k) + 3]: print('cw') else: print('ccw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
s=input() n=int(input()) d={} d['^']=0 d['>']=1 d['v']=2 d['<']=3 if n%4==2 or n%4==0: print("undefined") else: sum=(d[s[2]]-d[s[0]]+4)%4; if(sum==n%4): print("cw") else: print("ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { string a = "<^>v"; string b = "<v>^"; char st, en; int n; cin >> st >> en >> n; int i1, i2, i; n %= 4; for (i = 0; i < 4; i++) { if (a[i] == st) { i1 = i + n; i1 %= 4; } if (b[i] == st) { i2 = i + n; i2 %= 4; } } if (a[i1] == en && b[i2] == en) cout << "undefined"; else if (a[i1] == en) cout << "cw"; else cout << "ccw"; return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
keys = ['v', '<', '^', '>'] start, end = raw_input().split() n = int(raw_input()) % 4 start_ind = keys.index(start) ans = '' foo = (start_ind + n) % 4 if (keys[foo] == end and (keys[foo] != keys[start_ind - n])): ans = 'cw' elif (keys[start_ind - n] == end and (keys[foo] != keys[start_ind - n])): ans = 'ccw' elif (keys[foo] == keys[start_ind - n]): ans = 'undefined' print ans
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> int direcCharToInt(char c) { switch (c) { case '^': return 0; case '>': return 1; case 'v': return 2; case '<': return 3; } return -1; } int main() { int start, end, n; char buf[4]; gets(buf); scanf("%d", &n); start = direcCharToInt(buf[0]); end = direcCharToInt(buf[2]); n %= 4; if (start == end || (end - start + 4) % 4 == 2) { puts("undefined"); } else if ((end - start + 4) % 4 == n) { puts("cw"); } else if ((start - end + 4) % 4 == n) { puts("ccw"); } else { puts("undefined"); } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
s=input().split() d={'^':1,'>':2,'v':3,'<':4} x=int(input())%4 a=(4+d[s[1]]-d[s[0]])%4 b=(4-a)%4 if a==x and b!=x: print("cw") elif b==x and a!=x: print("ccw") else: print("undefined")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
class CodeforcesTask834ASolution: def __init__(self): self.result = '' self.positions = '' self.time = 0 def read_input(self): self.positions = input() self.time = int(input()) def process_task(self): shift = self.time % 4 if not shift: self.result = "undefined" else: spinner = ["^", ">", "v", "<"] start_pos = spinner.index(self.positions[0]) stop_pos = spinner.index(self.positions[2]) cw_result = (start_pos + shift) % 4 ccw_result = (start_pos - shift) % 4 if cw_result == stop_pos and ccw_result == stop_pos: self.result = "undefined" elif cw_result == stop_pos: self.result = "cw" else: self.result = "ccw" def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask834ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#!/usr/bin/python # coding: utf-8 cw=['v','<','^','>','v','<','^','>'] ccw=['v','>','^','<','v','>','^','<'] (s,e)=map(str,raw_input().split(' ')) n=int(raw_input()) n=n%4 tmp1=tmp2=0 cwind=cw.index(s)+n ccwind=ccw.index(s)+n if(cw[cwind]==e): tmp1=1 if(ccw[ccwind]==e): tmp2=1 if(tmp1==1 and tmp2==1): print "undefined" elif(tmp1): print "cw" else: print "ccw" ''' Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): The_Useless_Toy.png After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 10^9) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ > 1 Output cw Input < ^ 3 Output ccw Input ^ v 6 Output undefined '''
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char s, e; long long n; cin >> s >> e; cin >> n; n = n % 4; int ss, ee; if (s == 118) ss = 0; if (s == 60) ss = 1; if (s == 94) ss = 2; if (s == 62) ss = 3; if (e == 118) ee = 0; if (e == 60) ee = 1; if (e == 94) ee = 2; if (e == 62) ee = 3; if ((abs(ss + n) % 4 == ee) && (abs(ss - n + 4) % 4 == ee)) cout << "undefined"; else if (abs(ss + n) % 4 == ee) cout << "cw"; else if (abs(ss - n + 4) % 4 == ee) cout << "ccw"; return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int fx[] = {-1, 1, 0, 0}; int fy[] = {0, 0, -1, 1}; int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; int kx[] = {1, 1, -1, -1, 2, 2, -2, -2}; int ky[] = {2, -2, 2, -2, 1, -1, 1, -1}; bool cmp(const pair<int, int> &a, const pair<int, int> &b) { return a.first > b.first; } int main() { map<char, int> Map; Map['v'] = 0; Map['<'] = 1; Map['^'] = 2; Map['>'] = 3; char a, b; scanf("%c %c", &a, &b); int n; scanf("%d", &n); int start = Map[a], _end = Map[b]; int clkWise, cntClkWise; clkWise = (start + n) % 4; cntClkWise = ((start - n) % 4 + 4) % 4; if (_end == clkWise && _end == cntClkWise) { printf("undefined"); } else if (_end == clkWise) { printf("cw"); } else if (_end == cntClkWise) { printf("ccw"); } else printf("undifined\n"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; long long pwr(long long base, long long p, long long mod) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(0); char a, b; cin >> a >> b; long long n; cin >> n; int i, f; n = n % 4; if (a == '^') i = 0; else if (a == '>') i = 1; else if (a == 'v') i = 2; else if (a == '<') i = 3; if (b == '^') f = 0; else if (b == '>') f = 1; else if (b == 'v') f = 2; else if (b == '<') f = 3; if (n == 0 || n == 2) cout << "undefined"; else if ((i + n) % 4 == f) cout << "cw"; else cout << "ccw"; return (0); }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char cc[300], x[300]; cc['<'] = 0; cc['^'] = 1; cc['>'] = 2; cc['v'] = 3; x[0] = '<'; x[1] = '^'; x[2] = '>'; x[3] = 'v'; char c1, c2; int n; cin >> c1 >> c2 >> n; int start = cc[c1]; int finish = x[(start + n) % 4]; int finish2 = x[(1500000000 + start - n) % 4]; if (finish == finish2) cout << "undefined" << endl; else cout << (c2 == finish ? "cw" : "ccw") << endl; return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub FastScanner sc = new FastScanner(); HashMap<Integer, Character> angles = new HashMap<Integer, Character>(); angles.put(0,'v'); angles.put(1,'<'); angles.put(2,'^'); angles.put(3,'>'); HashMap<Character,Integer> angles2 = new HashMap<Character,Integer>(); angles2.put('v',0); angles2.put('<',1); angles2.put('^',2); angles2.put('>',3); boolean cw_check = false; boolean ccw_check = false; char start_pos = sc.nextToken().charAt(0); char end_pos = sc.nextToken().charAt(0); int sec = sc.nextInt(); sec = sec%4; if((angles2.get(start_pos)+sec)%4<=3 && (angles2.get(start_pos)+sec)%4>=0 && end_pos == angles.get((angles2.get(start_pos)+sec)%4)){ cw_check = true; } if((angles2.get(start_pos)-sec+4)%4>=0 && (angles2.get(start_pos)-sec+4)%4<=3 && end_pos == angles.get((angles2.get(start_pos)-sec+4)%4)){ ccw_check = true; } if((cw_check && ccw_check) || (!cw_check && !ccw_check)){ System.out.println("undefined"); } else if(cw_check){ System.out.println("cw"); } else System.out.println("ccw"); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p1 = (int) sc.next().charAt(0); int i1 = p1 == 118 ? 1 : p1 == 60 ? 2 : p1 == 94 ? 3 : p1 == 62 ? 0 : 5; int p2 = (int) sc.next().charAt(0); int i2 = p2 == 118 ? 1 : p2 == 60 ? 2 : p2 == 94 ? 3 : p2 == 62 ? 0 : 5; int d = sc.nextInt() % 4; if (d == 0 || d == 2) { System.out.println("undefined"); } else if (d == 1) { if (i1 == 0 && i2 == 1) { System.out.println("cw"); } else if (i1 == 1 && i2 == 2) { System.out.println("cw"); } else if (i1 == 2 && i2 == 3) { System.out.println("cw"); } else if (i1 == 3 && i2 == 0) { System.out.println("cw"); } else { System.out.println("ccw"); } } else if (d == 3) { if (i1 == 0 && i2 == 3) { System.out.println("cw"); } else if (i1 == 1 && i2 == 0) { System.out.println("cw"); } else if (i1 == 2 && i2 == 1) { System.out.println("cw"); } else if (i1 == 3 && i2 == 2) { System.out.println("cw"); } else { System.out.println("ccw"); } } sc.close(); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class b{ public static void main(String[] args) { Scanner s=new Scanner(System.in); char c1=s.next().charAt(0); char c2=s.next().charAt(0); int n=s.nextInt(); String ccw="^<v>^<v>"; String cw="^>v<^>v<"; int rem=n%4; if(rem==2||rem==0) { System.out.println("undefined"); }else { int index=-1; for(int i=0;i<cw.length();i++) { if(cw.charAt(i)==c1) { index=i; break; } } if(cw.charAt(index+rem)==c2) { System.out.println("cw"); }else { System.out.println("ccw"); } } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
ip=input().split() sec=int(input())%4 if(sec==0 or sec == 2): print("undefined") elif(sec==1): if(ip[0]=='^'): if(ip[1]=='>'): print("cw") else: print('ccw') elif(ip[0]=='>'): if (ip[1] == 'v'): print("cw") else: print('ccw') elif (ip[0] == 'v'): if (ip[1] == '<'): print("cw") else: print('ccw') else: if (ip[1] == '^'): print("cw") else: print('ccw') else: if(ip[0]=='^'): if(ip[1]=='>'): print("ccw") else: print('cw') elif(ip[0]=='>'): if (ip[1] == 'v'): print("ccw") else: print('cw') elif (ip[0] == 'v'): if (ip[1] == '<'): print("ccw") else: print('cw') else: if (ip[1] == '^'): print("ccw") else: print('cw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
clockwise = ['^', '>', 'v', '<'] counterclockwise = ['^', '<', 'v', '>'] start , end = raw_input().split() n = int(raw_input()) n = n % 4 x = clockwise.index(end) - clockwise.index(start) y = counterclockwise.index(end) - counterclockwise.index(start) if x<0: x += 4 if y<0: y += 4 if x == y: print "undefined" elif x == n: print "cw" else: print "ccw"
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.*; public class UselessToy{ public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] spun = new String[] { "^", ">", "v", "<" }; String[] line = input.nextLine().split(" "); int N = input.nextInt(); int startIndex = 0; for (int i = 0; i < spun.length; i++) if (line[0].equals(spun[i])) startIndex = i; boolean right = false; boolean left = false; if (spun[((N % 4) + startIndex) % 4].equals(line[1])) right = true; if (spun[(startIndex - (N % 4)) < 0 ? 4 + (startIndex - (N % 4)) : (startIndex - (N % 4)) % 4].equals(line[1])) left = true; if ((!right && !left) || (right && left)) System.out.print("undefined"); else if (right) System.out.print("cw"); else System.out.print("ccw"); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
st, en = map(str, input().split()) n = int(input()) n %= 4 m = {"^":1, ">":2, "v":3, "<":4} if (m[st] + m[en]) % 4 == 2 or (m[st] + m[en]) % 4 == 0: print("undefined") elif (m[st] + n) % 4 == m[en] or (m[st] + n) == m[en]: print("cw") else: print("ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; import java.util.StringTokenizer; public class A{ public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); String one= in.next(); String two= in.next(); int n= in.nextInt(); String key= "^>v<"; n%=4; int start= key.indexOf(one); int nex=start; for (int i = 1; i <= n; i++) { nex= (nex+1)%4; } boolean cw= false; if(nex==key.indexOf(two)){ cw= true; } nex=start; for (int i = 1; i <= n; i++) { nex= (nex-1)%4; if(nex<0) nex+=4; nex%=4; } boolean ccw= false; if(nex==key.indexOf(two)){ ccw= true; } if(ccw&&cw){ System.out.println("undefined"); } else if(cw){ System.out.println("cw"); } else if(ccw){ System.out.println("ccw"); } else{ System.out.println("undefined"); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public String next() throws IOException { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); return next(); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
a,b = input().split(" ") n = int(input()) if (a == b) or (a == '<' and b == '>') or (a == '<' and b == '<') or (a == '^' and b == 'v') or (a == 'v' and b == '^'): print('undefined') if (a == '^' and b == '>'): if ((n - 1)%4 == 0) or n==0: print('cw') if ((n-3)%4 == 0) or n == 0: print('ccw') if (a == '^' and b == '<'): if ((n - 1)%4 == 0) or n==0: print('ccw') if ((n-3)%4 == 0) or n == 0: print('cw') if (a == '>' and b == '^'): if ((n - 1)%4 == 0) or n==0: print('ccw') if ((n-3)%4 == 0) or n == 0: print('cw') if (a == '<' and b == '^'): if ((n - 1)%4 == 0) or n==0: print('cw') if ((n-3)%4 == 0) or n == 0: print('ccw') if (a == 'v' and b == '>'): if ((n - 1)%4 == 0) or n==0: print('ccw') if ((n-3)%4 == 0) or n == 0: print('cw') if (a == 'v' and b == '<'): if ((n - 1)%4 == 0) or n==0: print('cw') if ((n-3)%4 == 0) or n == 0: print('ccw') if (a == '>' and b == 'v'): if ((n - 1)%4 == 0) or n==0: print('cw') if ((n-3)%4 == 0) or n == 0: print('ccw') if (a == '<' and b == 'v'): if ((n - 1)%4 == 0) or n==0: print('ccw') if ((n-3)%4 == 0) or n == 0: print('cw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * public class Main { public static void main(String[] args) { FastReader * fastReader = new FastReader(); int TC = fastReader.nextInt(); StringBuffer sb * = new StringBuffer(); while (TC-- > 0) { int count =0; int n = * fastReader.nextInt(); int k = fastReader.nextInt(); int c = * fastReader.nextInt(); String line = fastReader.nextLine(); String arr[] = * line.split(" "); long arrInLong[] = new long[arr.length]; for(int * i=0;i<arr.length;i++){ arrInLong[i] = Long.parseLong(arr[i]); } * Arrays.sort(arrInLong); sb.append(count+"\n"); } * System.out.print(sb.toString()); } } */ public class Main { public static void main(String[] args) { FastReader fastReader = new FastReader(); char a = fastReader.next().charAt(0); char b = fastReader.next().charAt(0); int c = Integer.parseInt(fastReader.next()); //System.out.println(a+" "+b+" "+c); //StringBuffer sb = new StringBuffer(); if(a == b){ if(c%4 == 0){ System.out.println("undefined"); return; } } if(a == '<'){ if(b == '^'){ if(c%4 == 1){ System.out.println("cw"); return; } if(c%4 == 3){ System.out.println("ccw"); return; } System.out.println("undefined"); return; } if(b == '>'){ if(c%4 == 2){ System.out.println("undefined"); return; } System.out.println("undefined"); return; } if(b == 'v'){ if(c%4 == 1){ System.out.println("ccw"); return; } if(c%4 == 3){ System.out.println("cw"); return; } System.out.println("undefined"); return; } } if(a == 'v'){ if(b == '<'){ if(c%4 == 1){ System.out.println("cw"); return; } if(c%4 == 3){ System.out.println("ccw"); return; } System.out.println("undefined"); return; } if(b == '^'){ if(c%4 == 2){ System.out.println("undefined"); return; } System.out.println("undefined"); return; } if(b == '>'){ if(c%4 == 1){ System.out.println("ccw"); return; } if(c%4 == 3){ System.out.println("cw"); return; } System.out.println("undefined"); return; } } if(a == '>'){ if(b == '^'){ if(c%4 == 1){ System.out.println("ccw"); return; } if(c%4 == 3){ System.out.println("cw"); return; } System.out.println("undefined"); return; } if(b == '<'){ if(c%4 == 2){ System.out.println("undefined"); return; } System.out.println("undefined"); return; } if(b == 'v'){ if(c%4 == 1){ System.out.println("cw"); return; } if(c%4 == 3){ System.out.println("ccw"); return; } System.out.println("undefined"); return; } } if(a == '^'){ if(b == '>'){ if(c%4 == 1){ System.out.println("cw"); return; } if(c%4 == 3){ System.out.println("ccw"); return; } System.out.println("undefined"); return; } if(b == 'v'){ if(c%4 == 2){ System.out.println("undefined"); return; } System.out.println("undefined"); return; } if(b == '<'){ if(c%4 == 1){ System.out.println("ccw"); return; } if(c%4 == 3){ System.out.println("cw"); return; } System.out.println("undefined"); return; } } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(" "); } /** get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { // TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import sys import math import itertools import collections def getdict(n): d = {} if type(n) is list or type(n) is str: for i in n: if i in d: d[i] += 1 else: d[i] = 1 else: for i in range(n): t = ii() if t in d: d[t] += 1 else: d[t] = 1 return d def cdiv(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a*b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return int(str(n)[::-1]) def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True arr = ['^', '>', 'v', '<'] p = list(input().split()) t = ii() if (arr.index(p[0]) - arr.index(p[1])) % 4 == 2 or (arr.index(p[0]) - arr.index(p[1])) % 4 == 0: print('undefined') elif (arr.index(p[0]) - arr.index(p[1])) % 4 == t % 4: print('ccw') elif (arr.index(p[0]) - arr.index(p[1])) % 4 == 4 - t % 4: print('cw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
dir1, dir2 = raw_input().split() rotations = int(raw_input()) def isCW(dir1, dir2, rotations): cw_rotation = ["^", ">", "v", "<"] actual_rotations = rotations % 4 for i in xrange(len(cw_rotation)): if cw_rotation[i] == dir1: if cw_rotation[(i + actual_rotations) % 4] == dir2: return True return False def isCCW(dir1, dir2, rotations): return isCW(dir2, dir1, rotations) if isCW(dir1, dir2, rotations): if isCCW(dir1, dir2, rotations): print "undefined" else: print "cw" else: print "ccw"
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main(int argc, const char *argv[]) { string s, t; int n = 0, start = 0, end = 0, cw = 0, ccw = 0; cin >> s >> t >> n; if (s[0] == '^') { start = 0; } else if (s[0] == '>') { start = 1; } else if (s[0] == 'v') { start = 2; } else { start = 3; } if (t[0] == '^') { end = 0; } else if (t[0] == '>') { end = 1; } else if (t[0] == 'v') { end = 2; } else { end = 3; } n = n % 4; cw = (start + n) % 4; ccw = (start + 4 - n) % 4; if (cw == end && ccw == end) { cout << "undefined" << endl; } else if (cw == end && ccw != end) { cout << "cw" << endl; } else if (cw != end && ccw == end) { cout << "ccw" << endl; } else { cout << "undefined" << endl; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
cw = [60,94,62,118] ccw = [60,118,62,94] i,f = map(ord,input().split()) n = int(input()) ans = 'undefined' index_i = cw.index(i) index_f = cw.index(f) if (index_i+n)%4==index_f: ans='cw' index_i = ccw.index(i) index_f = ccw.index(f) if (index_i+n)%4==index_f: if ans=='undefined': ans='ccw' else: ans= 'undefined' print(ans)
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
pos1, pos2 = input().split() turns_nr = int(input()) if turns_nr % 2 == 0: print('undefined') exit() elif turns_nr % 4 == 1: lst = '<^>v<^>v' for idx1, sym1 in enumerate(lst): if sym1 == pos1: if lst[idx1 - 1] == pos2: print('ccw') exit() elif lst[idx1 + 1] == pos2: print('cw') exit() elif turns_nr % 4 == 3: lst = '<^>v<^>v' for idx1, sym1 in enumerate(lst): if sym1 == pos1: if lst[idx1 - 1] == pos2: print('cw') exit() elif lst[idx1 + 1] == pos2: print('ccw') exit()
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char f, s; cin >> s >> f; int n; cin >> n; n %= 4; int ff, ss; switch (f) { case 'v': ff = 0; break; case '<': ff = 1; break; case '^': ff = 2; break; case '>': ff = 3; break; default: break; } switch (s) { case 'v': ss = 0; break; case '<': ss = 1; break; case '^': ss = 2; break; case '>': ss = 3; break; default: break; } int tt = ss; ss += n; ss %= 4; bool con = false; if (ss == ff) con = true; while (n--) { tt--; if (tt == -1) tt = 3; } if (tt == ff) { if (con) cout << "undefined"; else cout << "ccw"; } else { if (con) cout << "cw"; else cout << "undefined"; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { private static FastScanner sc = new FastScanner(); public static void main(String[] args) { String ss = sc.next(); String ts = sc.next(); int n = sc.nextInt(); int s = 0; int t = 0; if(ss.equals("^")) { s = 0; } else if(ss.equals(">")) { s = 1; } else if(ss.equals("v")) { s = 2; } else { s = 3; } if(ts.equals("^")) { t = 0; } else if(ts.equals(">")) { t = 1; } else if(ts.equals("v")) { t = 2; } else { t = 3; } if((t - s + 4) % 4 == 2 || (t - s + 4) % 4 == 0) { System.out.println("undefined"); } else if((t - s + 4) % 4 == 1) { if(n % 4 == 1) { System.out.println("cw"); } else { System.out.println("ccw"); } } else { if(n % 4 == 3) { System.out.println("cw"); } else { System.out.println("ccw"); } } } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if(ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch(IOException e) { e.printStackTrace(); } if(buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt(){ return Integer.parseInt(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
[start, end] = [x for x in input().split()] duration = int(input()) cw = ['v', '<', '^', '>'] ccw = ['v', '>', '^', '<'] cw_bool = end == cw[((cw.index(start) + duration) % 4)] ccw_bool = end == ccw[((ccw.index(start) + duration) % 4)] if cw_bool and ccw_bool: print("undefined") elif cw_bool: print("cw") else: print("ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
from sys import stdin, stdout line = stdin.readline().strip().split(' ') n = int(stdin.readline().strip()) rot = n % 4 cw_array = ['v', '<', '^', '>'] ccw_array = ['v', '>', '^', '<'] first = line[0] last = line[1] cw_flag = False ccw_flag = False ci = cw_array.index(first) ci = (ci+rot) % 4 if(cw_array[ci]==last): cw_flag = True cci = ccw_array.index(first) cci = (cci+rot) % 4 if(ccw_array[cci]==last): ccw_flag = True if(cw_flag==True and ccw_flag==True): stdout.write("undefined") else: if(cw_flag==False and ccw_flag==False): stdout.write("undefined") else: if(cw_flag==True): stdout.write("cw") else: stdout.write("ccw")
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#!/usr/bin/env python # -*- coding: utf-8 -*- import array import bisect import collections import copy import fractions import functools import heapq import itertools import math import operator import os import re import random import string import subprocess import sys import time import unittest from io import StringIO from pprint import pprint def main(): iterator = iter(sys.stdin.read().split()) a = next(iterator) b = next(iterator) n = int(next(iterator)) n %= 4 s = 'v<^>' p1 = s.find(a) p2 = s.find(b) if (p2 - p1) % 4 == n and (p1 - p2) % 4 == n: print('undefined') elif (p2 - p1) % 4 == n: print('cw') elif (p1 - p2) % 4 == n: print('ccw') else: print('undefined') if __name__ == '__main__': main()
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> const char s[] = {'^', '>', 'v', '<'}; char a[5], b[5]; int main() { scanf("%s%s", a, b); int k; for (int i = 0; i < 4; i++) if (a[0] == s[i]) k = i; int n; scanf("%d", &n); n %= 4; if (s[(k + n) % 4] == b[0] && s[(k + 4 - n) % 4] != b[0]) printf("cw\n"); else if (s[(k + 4 - n) % 4] == b[0] && s[(k + n) % 4] != b[0]) printf("ccw\n"); else printf("undefined\n"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; bool first = true; long long gcd(long long a, long long b) { while (a != 0 && b != 0) { if (a < b) swap(a, b); a %= b; } return a + b; } string lowreg(string s) { string ret; int len = s.length(); for (int i = 0; i < len; i++) if ((int)s[i] >= (int)'A' && (int)s[i] <= (int)'Z') ret += (char)((int)'a' + (int)s[i] - (int)'A'); else ret += s[i]; return ret; } int main() { char c1, c2, a[4] = {'v', '<', '^', '>'}; scanf("%c %c\n", &c1, &c2); int n, i1, i2, k1, k2; scanf("%d", &n); n %= 4; for (int i = 0; i < 4; i++) { if (c1 == a[i]) i1 = i; if (c2 == a[i]) i2 = i; } i1++; i2++; if (i1 + n > 4) k1 = (i1 + n) % 4; else k1 = i1 + n; if (i2 + n > 4) k2 = (i2 + n) % 4; else k2 = i2 + n; if (k1 == i2 && k2 == i1) printf("undefined"); else if (k1 == i2) printf("cw"); else printf("ccw"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char c1, c2; cin >> c1 >> c2; int n; cin >> n; if (n % 2 == 0) { cout << "undefined" << endl; } else { int n1, n2; switch (c1) { case '^': n1 = 1; break; case '>': n1 = 2; break; case 'v': n1 = 3; break; case '<': n1 = 4; break; } switch (c2) { case '^': n2 = 1; break; case '>': n2 = 2; break; case 'v': n2 = 3; break; case '<': n2 = 4; break; } n %= 4; if (n1 != 4) { if (n2 - n1 == 1) { if (n == 1) cout << "cw" << endl; else cout << "ccw" << endl; } else { if (n == 1) cout << "ccw" << endl; else cout << "cw" << endl; } } else { if (n1 - n2 == 3) { if (n == 1) cout << "cw" << endl; else cout << "ccw" << endl; } else { if (n == 1) cout << "ccw" << endl; else cout << "cw" << endl; } } } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; public class MainY { public static void main(String[] args) { List<String> input = getRawInput(); int start = (int)(input.get(0).trim().split(" ")[0]).charAt(0); int ends = (int)(input.get(0).trim().split(" ")[1]).charAt(0); BigInteger big = new BigInteger(input.get(1).trim()); int []completeAcii ={94, 62, 118, 60}; BigInteger rest = big.mod(new BigInteger("4")); int pos = getPosition(start, completeAcii); int []aux = createAuxExcludePos(pos, completeAcii); int []aux2 = createAux(aux); if (rest.equals(new BigInteger("3")) && aux2[0]==ends){ System.out.println("ccw"); } else if (rest.equals(new BigInteger("1")) && aux2[0]==ends){ System.out.println("cw"); }else if (rest.equals(new BigInteger("3")) && aux2[2]==ends){ System.out.println("cw"); } else if (rest.equals(new BigInteger("1")) && aux2[2]==ends){ System.out.println("ccw"); }else{ System.out.println("undefined"); } } private static int[] createAux(int[] aux) { List<Integer>auxList = new ArrayList<>(); for (int i=0;i<aux.length;i++){ if (aux[i]!=0){ auxList.add(aux[i]); } } int[]a = new int [auxList.size()]; for (int i=0;i<auxList.size();i++){ a[i]=auxList.get(i); } return a; } private static int[] createAuxExcludePos(int pos, int[] completeAcii) { int result[] = new int[completeAcii.length]; int k = 0; if (pos+1<completeAcii.length){ for (int i=(pos+1); i<completeAcii.length;i++){ result[k++] = completeAcii[i]; } for (int i=0;i<pos;i++){ result[k++] = completeAcii[i]; } }else{ for (int i=0;i<pos;i++){ result[i] = completeAcii[i]; } } return result; } private static int getPosition(int start, int[] completeAcii) { int i=0; for (i=0;i<completeAcii.length;i++){ if (completeAcii[i]==start){ return i; } } return 0; } public static List<String> getRawInput() { List<String> line = new ArrayList<>(); try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); line.add(br.readLine().trim()); line.add(br.readLine().trim()); } catch (IOException e) { e.printStackTrace(); } return line; } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int n; void panduan(char c1, char c2) { if (c1 == '^') { if (c2 == '>') { if (n % 4 == 1) { printf("cw\n"); return; } else if (n % 4 == 3) { printf("ccw\n"); return; } } else if (c2 == '<') { if (n % 4 == 3) { printf("cw\n"); return; } else if (n % 4 == 1) { printf("ccw\n"); return; } } } else if (c1 == '>') { if (c2 == 'v') { if (n % 4 == 1) { printf("cw\n"); return; } else if (n % 4 == 3) { printf("ccw\n"); return; } } else if (c2 == '^') { if (n % 4 == 3) { printf("cw\n"); return; } else if (n % 4 == 1) { printf("ccw\n"); return; } } } else if (c1 == 'v') { if (c2 == '<') { if (n % 4 == 1) { printf("cw\n"); return; } else if (n % 4 == 3) { printf("ccw\n"); return; } } else if (c2 == '>') { if (n % 4 == 3) { printf("cw\n"); return; } else if (n % 4 == 1) { printf("ccw\n"); return; } } } if (c1 == '<') { if (c2 == '^') { if (n % 4 == 1) { printf("cw\n"); return; } else if (n % 4 == 3) { printf("ccw\n"); return; } } else if (c2 == 'v') { if (n % 4 == 3) { printf("cw\n"); return; } else if (n % 4 == 1) { printf("ccw\n"); return; } } } } int main() { char ch; char s[10]; scanf("%c", &s[0]); getchar(); scanf("%c", &s[1]); scanf("%d", &n); if (s[0] == s[1]) printf("undefined\n"); else if ((s[0] == 118 && s[1] == 94) || (s[0] == 94 && s[1] == 118) || (s[0] == 60 && s[1] == 62) || (s[0] == 62 && s[1] == 60)) printf("undefined\n"); else { panduan(s[0], s[1]); } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
pos = [None, None] pos[0], pos[1] = input().split() sec = int(input()) if pos[0] == '^': now_pos = 1 elif pos[0] == '>': now_pos = 2 elif pos[0] == 'v': now_pos = 3 else: now_pos = 4 start = now_pos if pos[1] == '^': end = 1 elif pos[1] == '>': end = 2 elif pos[1] == 'v': end = 3 else: end = 4 b1 = False b2 = False h1 = -100 h2 = -100 if start <= end: h1 = end - start else: h1 = 4 - start + end if start >= end: h2 = start - end else: h2 = start + 4 - end if h1 == h2 == (sec % 4): b1 = b2 = True elif h1 == (sec % 4): b1 = True elif h2 == (sec % 4): b2 = True if b1 and b2: print('undefined') elif b1: print('cw') elif b2: print('ccw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<Integer,Character> cw = new HashMap<Integer,Character>(); HashMap<Integer,Character> ccw = new HashMap<Integer,Character>(); cw.put(0, '^'); cw.put(1, '>'); cw.put(2,'?'); cw.put(3, '<'); ccw.put(0,'^'); ccw.put(1, '<'); ccw.put(2, '?'); ccw.put(3,'>'); String s = sc.next(); char start = s.charAt(0); start = ((int)(start-'^') == 24)?'?':start; String e = sc.next(); char end = e.charAt(0); end = ((int)(end-'^') == 24)?'?':end; int n = sc.nextInt(); int pos = n%4; int cw_start=0,ccw_start=0; for(int i=0;i<4;i++){ if(cw.get(i) == start){ //System.out.println(i); cw_start = i; break; } } boolean cw_flag = false; cw_start = (cw_start+pos)%4; cw_flag = (cw.get(cw_start) == end)?true:false; //System.out.println(cw_start+" "+cw_flag); for(int i=0;i<4;i++){ if(ccw.get(i)==start){ ccw_start = i; break; } } boolean ccw_flag = false; ccw_start = (ccw_start+pos)%4; ccw_flag = (ccw.get(ccw_start) == end)?true:false; //System.out.println(ccw_start+" "+ccw_flag); if(ccw_flag && cw_flag){ System.out.println("undefined"); }else if(ccw_flag){ System.out.println("ccw"); }else{ System.out.println("cw"); } sc.close(); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; const int sz = 1e5 + 10; char c[] = {'^', '>', 'v', '<'}; int main() { char a, b; int n, st, na, nb; while (cin >> a >> b >> n) { for (st = 0; c[st] != a; st++) { } na = (st + n) % 4; nb = ((st - n) % 4 + 4) % 4; if (b == c[na] && b == c[nb]) cout << "undefined\n"; else if (b == c[na]) cout << "cw\n"; else if (b == c[nb]) cout << "ccw\n"; else cout << "undefined\n"; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char a, b; int t; cin >> a >> b >> t; int x = (int)a; int y = (int)b; int r = t % 4; if (r == 0) cout << "undefined" << endl; else if (x == 94) { if (r == 1 && y == 62) cout << "cw" << endl; else if (r == 1 && y == 60) cout << "ccw" << endl; else if (r == 1 && y != 60 && y != 62) cout << "undefined" << endl; else if (r == 2) cout << "undefined" << endl; else if (r == 3 && y == 60) cout << "cw" << endl; else if (r == 3 && y == 62) cout << "ccw" << endl; else if (r == 3 && y != 60 && y != 62) cout << "undefined" << endl; } else if (x == 62) { if (r == 1 && y == 118) cout << "cw" << endl; else if (r == 1 && y == 94) cout << "ccw" << endl; else if (r == 1 && y != 118 && y != 94) cout << "undefined" << endl; else if (r == 2) cout << "undefined" << endl; else if (r == 3 && y == 94) cout << "cw" << endl; else if (r == 3 && y == 118) cout << "ccw" << endl; else if (r == 3 && y != 94 && y != 118) cout << "undefined" << endl; } else if (x == 118) { if (r == 1 && y == 60) cout << "cw" << endl; else if (r == 1 && y == 62) cout << "ccw" << endl; else if (r == 1 && y != 60 && y != 62) cout << "undefined" << endl; else if (r == 2) cout << "undefined" << endl; else if (r == 3 && y == 62) cout << "cw" << endl; else if (r == 3 && y == 60) cout << "ccw" << endl; else if (r == 3 && y != 62 && y != 60) cout << "undefined" << endl; } else if (x == 60) { if (r == 1 && y == 94) cout << "cw" << endl; else if (r == 1 && y == 118) cout << "ccw" << endl; else if (r == 1 && y != 94 && y != 118) cout << "undefined" << endl; else if (r == 2) cout << "undefined" << endl; else if (r == 3 && y == 118) cout << "cw" << endl; else if (r == 3 && y == 94) cout << "ccw" << endl; else if (r == 3 && y != 118 && y != 94) cout << "undefined" << endl; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.util.HashMap; import java.util.Scanner; public class TheUselessToy { public static void main(String[] args) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); map.put('^', 0); map.put('>', 1); map.put('v', 2); map.put('<', 3); Scanner sc = new Scanner(System.in); String shapes = sc.nextLine(); char fir = shapes.split(" ")[0].charAt(0); char second = shapes.split(" ")[1].charAt(0); int distanceCW = map.get(second) - map.get(fir); if (distanceCW < 0) { distanceCW += 4; } int distanceCCW = Math.abs(map.get(second) - map.get(fir) - 4) % 4; int n = Integer.parseInt(sc.nextLine()) % 4; if (distanceCCW == distanceCW) { System.out.println("undefined "); } else if (distanceCCW == n) { System.out.println("ccw"); } else if(distanceCW == n) { System.out.println("cw"); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
startpos,endpos=str(input()).split() n=int(input()) startnum=0 endnum=0 if(startpos=='^'): startnum=0 elif(startpos=='>'): startnum=1 elif(startpos=='v'): startnum=2 elif(startpos=='<'): startnum=3 if(endpos=='^'): endnum=0 elif(endpos=='>'): endnum=1 elif(endpos=='v'): endnum=2 elif(endpos=='<'): endnum=3 if((startnum+n)%4==endnum and (startnum-n)%4==endnum): print("undefined") elif((startnum+n)%4==endnum): print("cw") elif((startnum-n)%4==endnum): print("ccw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); String w=sc.nextLine(); int n=sc.nextInt(); char p=w.charAt(0); char q=w.charAt(2); char a[]=new char[4]; a[0]='<'; a[1]='^'; a[2]='>'; a[3]='v'; char b[]=new char[4]; b[0]='<'; b[1]='v'; b[2]='>'; b[3]='^'; n=n%4; int startA=0,startB=0,endA=0,endB=0; for(int i=0;i<4;i++){ if(a[i]==p) startA=i; if(a[i]==q) endA=i; if(b[i]==p) startB=i; if(b[i]==q) endB=i; } int ansA=0,ansB=0; if(startA>endA){ ansA=(4-startA)+endA; } else { ansA=endA-startA; } if(startB>endB){ ansB=(4-startB)+endB; } else { ansB=endB-startB; } if(ansA==n && ansB==n) System.out.println("undefined"); else if(ansA==n) System.out.println("cw"); else if(ansB==n) System.out.println("ccw"); else System.out.println("undefined"); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = new Integer(br.readLine()); br.close(); int a = (int)st.nextToken().charAt(0); int b = (int)st.nextToken().charAt(0); if(a==94&&b==62){ if(n%4==1){ System.out.println("cw"); return; } else if(n%4==3){ System.out.println("ccw"); } } else if(a==94&&b==60){ if(n%4==1){ System.out.println("ccw"); return; } else if(n%4==3){ System.out.println("cw"); return; } } else if(a==118&&b==62){ if(n%4==1){ System.out.println("ccw"); return; } else if(n%4==3){ System.out.println("cw"); return; } } else if(a==118&&b==60){ if(n%4==1){ System.out.println("cw"); return; } else if(n%4==3){ System.out.println("ccw"); return; } } else if(a==62&&b==118){ if(n%4==1){ System.out.println("cw"); return; } else if(n%4==3){ System.out.println("ccw"); return; } } else if(a==62&&b==94){ if(n%4==1){ System.out.println("ccw"); return; } else if(n%4==3){ System.out.println("cw"); return; } } else if(a==60&&b==94){ if(n%4==1){ System.out.println("cw"); return; } else if(n%4==3){ System.out.println("ccw"); return; } } else if(a==60&&b==118){ if(n%4==1){ System.out.println("ccw"); return; } else if(n%4==3){ System.out.println("cw"); return; } } else{ System.out.println("undefined"); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
//package codeforces; import java.util.Scanner; /** * Created by nitin.s on 30/07/17. */ public class TheUselessToy { static String undefined = "undefined"; static String counter = "ccw"; static String clock = "cw"; public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); char start = s.charAt(0); char end = s.charAt(2); int rotation = in.nextInt(); int rem = rotation % 4; String res = process(start, end, rem); System.out.println(res); } private static String process(char start, char end, int rem) { if(start == '^') { if(rem == 0 || rem == 2) return undefined; else if(rem == 1) { if(end == '>') return clock; if(end == '<') return counter; else return undefined; } else if(rem == 3) { if(end == '<') return clock; if(end == '>') return counter; else return undefined; } } else if(start == '>') { if(rem == 0 || rem == 2) return undefined; else if(rem == 1) { if(end == 'v') return clock; if(end == '^') return counter; else return undefined; } else if(rem == 3) { if(end == 'v') return counter; if(end == '^') return clock; else return undefined; } } else if(start == '<') { if(rem == 0 || rem == 2) return undefined; else if(rem == 1) { if(end == '^') return clock; if(end == 'v') return counter; else return undefined; } else if(rem == 3) { if(end == '^') return counter; if(end == 'v') return clock; else return undefined; } } else if(start == 'v') { if(rem == 0 || rem == 2) return undefined; else if(rem == 1) { if(end == '<') return clock; if(end == '>') return counter; else return undefined; } else if(rem == 3) { if(end == '<') return counter; if(end == '>') return clock; else return undefined; } } return undefined; } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
a=input().split() n=int(input()) b=["v",">","^","<"] if n%2==0: print("undefined") else: if (b.index(a[0])+n)%4==b.index(a[1]): print("ccw") else: print("cw")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; map<string, int> cmp, ccmp; int main() { ios::sync_with_stdio(false); cmp["^ >"] = 1; cmp["^ v"] = 2; cmp["^ <"] = 3; cmp["> v"] = 1; cmp["> <"] = 2; cmp["> ^"] = 3; cmp["v <"] = 1; cmp["v ^"] = 2; cmp["v >"] = 3; cmp["< ^"] = 1; cmp["< >"] = 2; cmp["< v"] = 3; ccmp["^ >"] = 3; ccmp["^ v"] = 2; ccmp["^ <"] = 1; ccmp["> v"] = 3; ccmp["> <"] = 2; ccmp["> ^"] = 1; ccmp["v <"] = 3; ccmp["v ^"] = 2; ccmp["v >"] = 1; ccmp["< ^"] = 3; ccmp["< >"] = 2; ccmp["< v"] = 1; string str; int num; getline(cin, str); cin >> num; num %= 4; if (num == cmp[str] && num == ccmp[str]) cout << "undefined" << endl; else if (num == cmp[str]) cout << "cw" << endl; else if (num == ccmp[str]) cout << "ccw" << endl; else cout << "undefined" << endl; return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
a=input().split() ## n=int(input()) m=['^','>','v','<'] a1=a[0] a2=a[1] no=m.index(a1) m1=[m[no],m[(no+1)%4],m[(no+2)%4],m[(no+3)%4]] n1=n%4 n2=4-n1 if n1==n2 or n==0 or a1==a2: print('undefined') else: if m1[n1]==a2: print('cw') else: print('ccw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
dirs = ['v','<','^','>'] a,b = map(dirs.index, input().split()) k = (b-a+4)%4 n = int(input()) if k==0 or k==2: print('undefined') elif k==n%4: print('cw') else: print('ccw')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> mp; char c1, c2; long long n; cin >> c1 >> c2; cin >> n; n = n % 4; if (c1 == '^') { if (n == 0 || n == 2) { puts("undefined"); } else if (n == 1) { if (c2 == '>') puts("cw"); else if (c2 == '<') { puts("ccw"); } else puts("undefined"); } else if (n == 3) { if (c2 == '<') puts("cw"); else if (c2 == '>') { puts("ccw"); } else puts("undefined"); } } else if (c1 == '>') { if (n == 0 || n == 2) { puts("undefined"); } else if (n == 1) { if (c2 == 'v') puts("cw"); else if (c2 == '^') { puts("ccw"); } else puts("undefined"); } else if (n == 3) { if (c2 == '^') puts("cw"); else if (c2 == 'v') { puts("ccw"); } else puts("undefined"); } } else if (c1 == 'v') { if (n == 0 || n == 2) { puts("undefined"); } else if (n == 1) { if (c2 == '<') puts("cw"); else if (c2 == '>') { puts("ccw"); } else puts("undefined"); } else if (n == 3) { if (c2 == '>') puts("cw"); else if (c2 == '<') { puts("ccw"); } else puts("undefined"); } } else if (c1 == '<') { if (n == 0 || n == 2) { puts("undefined"); } else if (n == 1) { if (c2 == '^') puts("cw"); else if (c2 == 'v') { puts("ccw"); } else puts("undefined"); } else if (n == 3) { if (c2 == 'v') puts("cw"); else if (c2 == '^') { puts("ccw"); } else puts("undefined"); } } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class A { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(1, in, out); out.close(); } static class Solver { public void solve(int testNumber, InputReader in, PrintWriter out) { char s1 = in.next().charAt(0); char s2 = in.next().charAt(0); int n = in.nextInt(); n %= 4; char c1 = s1; char c2 = s1; for (int i = 0; i < n; i++) { c1 = nextSpin(c1); c2 = prevSpin(c2); } if (c1 == s2 && c2 == s2) { out.println("undefined"); } else if (c1 == s2) { out.println("cw"); } else if (c2 == s2) { out.println("ccw"); } else { out.println("undefined"); } } public char nextSpin(char c) { if (c == '<') { return '^'; } if (c == '^') { return '>'; } if (c == '>') { return 'v'; } return '<'; } public char prevSpin(char c) { if (c == '<') { return 'v'; } if (c == 'v') { return '>'; } if (c == '>') { return '^'; } return '<'; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
def pos_to_int(c): if c == '^': return 0 if c == '>': return 1 if c == 'v': return 2 if c == '<': return 3 assert(False) a, b = map(pos_to_int, raw_input().split()) n = int(raw_input()) ok1 = (a + n)%4 == b ok2 = (a - n)%4 == b if ok1 and ok2: print 'undefined' elif ok1: print 'cw' elif ok2: print 'ccw' else: assert(False)
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> int main() { char c1, c2, x, a; int n, p, i, m; scanf("%s", &c1); scanf("%s", &c2); scanf("%d", &n); a = c1; x = c1; if (n % 4 == 2 || n % 4 == 0) { printf("undefined"); return 0; } for (i = 1; i <= n % 4; i++) { if (a == '^') a = '<'; else if (a == '>') a = '^'; else if (a == 'v') a = '>'; else if (a == '<') a = 'v'; } if (a == c2) { printf("ccw"); return 0; } for (i = 1; i <= n % 4; i++) { if (x == '^') x = '>'; else if (x == '>') x = 'v'; else if (x == 'v') x = '<'; else if (x == '<') x = '^'; } if (x == c2) { printf("cw"); return 0; } printf("undefined"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; char a[] = {'v', '<', '^', '>'}; int main() { char t1, t2; scanf("%c %c", &t1, &t2); int pos, n; scanf("%d", &n); for (int i = 0; i < 4; i++) { if (a[i] == t1) { pos = i; break; } } int tt = n % 4; int a1 = (tt + pos) % 4, a2 = (pos - tt + 4) % 4; if (a[a1] == t2 && a1 != a2) puts("cw"); else if (a[a2] == t2 && a1 != a2) puts("ccw"); else puts("undefined"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char ch, ch1; long long int n, x, y, z; cin >> ch >> ch1 >> n; x = (int)ch; y = (int)ch1; z = (x - y); if ((z == 58) || (z == -34) || (z == 32) || (z == -56)) { if (((3 + n) % 4) == 0) { cout << "cw" << endl; } else if (((n + 1) % 4) == 0) { cout << "ccw" << endl; } else { cout << "undefined" << endl; } } else if ((z == 56) || (z == -58) || (z == 34) || (z == -32)) { if (((n + 1) % 4) == 0) { cout << "cw" << endl; } else if (((n + 3) % 4) == 0) { cout << "ccw" << endl; } else { cout << "undefined" << endl; } } else { cout << "undefined" << endl; } return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 1; const int K = 200; const int M = 1e9 + 7; const double eps = 1e-6; char f, t, k; map<char, int> mp; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); mp['v'] = 0; mp['<'] = 1; mp['^'] = 2; mp['>'] = 3; cin >> f >> t; int time; cin >> time; time %= 4; if (f == t) { cout << "undefined\n"; } else if (abs(mp[t] - mp[f]) & 1) { if (time == 1) { if ((mp[t] - mp[f] + 4) % 4 == 1) cout << "cw\n"; else cout << "ccw\n"; } else { if ((mp[t] - mp[f] + 4) % 4 == 3) cout << "cw\n"; else cout << "ccw\n"; } } else { cout << "undefined\n"; } }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.*; import java.util.*; public class A1008 { public static void main(String [] args) /*throws Exception*/ { InputStream inputReader = System.in; OutputStream outputReader = System.out; InputReader in = new InputReader(inputReader);//new InputReader(new FileInputStream(new File("input.txt")));new InputReader(inputReader); PrintWriter out = new PrintWriter(outputReader);//new PrintWriter(new FileOutputStream(new File("output.txt"))); Algorithm solver = new Algorithm(); solver.solve(in, out); out.close(); } } class Algorithm { void solve(InputReader ir, PrintWriter pw) { char first = ir.next().charAt(0), second = ir.next().charAt(0); int n = ir.nextInt(); if (n % 2 == 0) { pw.print("undefined"); } else { if (first == '^') { if (second == '>') { if ((n - 1) / 2 % 2 == 0) pw.print("cw"); else pw.print("ccw"); } else { if ((n - 1) / 2 % 2 == 0) pw.print("ccw"); else pw.print("cw"); } } else if (first == 'v') { if (second == '>') { if ((n - 1) / 2 % 2 == 0) pw.print("ccw"); else pw.print("cw"); } else { if ((n - 1) / 2 % 2 == 0) pw.print("cw"); else pw.print("ccw"); } } else if (first == '>') { if (second == '^') { if ((n - 1) / 2 % 2 == 0) pw.print("ccw"); else pw.print("cw"); } else { if ((n - 1) / 2 % 2 == 0) pw.print("cw"); else pw.print("ccw"); } } else if (first == '<') { if (second == '^') { if ((n - 1) / 2 % 2 == 0) pw.print("cw"); else pw.print("ccw"); } else { if ((n - 1) / 2 % 2 == 0) pw.print("ccw"); else pw.print("cw"); } } } } private static void Qsort(int[] array, int low, int high) { int i = low; int j = high; int x = array[low + (high - low) / 2]; do { while (array[i] < x) ++i; while (array[j] > x) --j; if (i <= j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; i++; j--; } } while (i <= j); if (low < j) Qsort(array, low, j); if (i < high) Qsort(array, i, high); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine(){ String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } String [] toArray() { return nextLine().split(" "); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
s=raw_input().split() k={'^':0,'>':1,'v':2,'<':3} n=input() if k[s[1]]==(k[s[0]]+n%4)%4 and k[s[0]]!=(k[s[1]]+n%4)%4: print 'cw' elif k[s[0]]==(k[s[1]]+n%4)%4 and k[s[1]]!=(k[s[0]]+n%4)%4: print 'ccw' else: print 'undefined'
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int f(char a) { if (a == '>') return 0; if (a == 'v') return 1; if (a == '<') return 2; if (a == '^') return 3; } int main() { char s1, s2; cin >> s1 >> s2; int n; cin >> n; int o = f(s2) - f(s1); int x = (o - n) % 4; int y = (n + o) % 4; if (x == y) cout << "undefined"; else if (x == 0 && y != 0) cout << "cw"; else if (y == 0 && x != 0) cout << "ccw"; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; char c, s; cin >> c >> s >> n; char t[4], v[4]; t[0] = '^'; t[1] = '<'; t[2] = 'v'; t[3] = '>'; v[0] = '^'; v[1] = '>'; v[2] = 'v'; v[3] = '<'; int i, r, r1; for (i = 0; i < 4; i++) { if (t[i] == c) r = i; if (v[i] == c) r1 = i; } if ((t[(n + r) % 4] == s) && (v[(n + r1) % 4] == s)) cout << "undefined"; else if (t[(n + r) % 4] == s) cout << "ccw"; else if (v[(n + r1) % 4] == s) cout << "cw"; else cout << "undefined"; return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; char s[100], s1[5] = {'v', '<', '^', '>'}; int n; int main() { int i, j, k = 0; gets(s); scanf("%d", &n); if (s[0] == s[2]) { printf("undefined"); return 0; } if (s[0] == '>' && s[2] == '<') { printf("undefined"); return 0; } if (s[2] == '>' && s[0] == '<') { printf("undefined"); return 0; } if (s[0] == '^' && s[2] == 'v') { printf("undefined"); return 0; } if (s[2] == '^' && s[0] == 'v') { printf("undefined"); return 0; } n = n % 4; for (i = 0; i < 4; i++) if (s1[i] == s[0]) { k = i; break; } k += n; k = k % 4; if (s[2] == s1[k]) printf("cw"); else printf("ccw"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
def read(func=int, is_list=False): if is_list: return map(func, raw_input().split()) else: return func(raw_input()) x, y = read(str, True) def get(s): if s == '^': return 0 if s == '>': return 1 if s == 'v': return 2 return 3 x = get(x) y = get(y) n = read(int, False) if n % 4 == 2 or n % 4 == 0: print 'undefined' elif (x + n) % 4 == y: print 'cw' else: print 'ccw'
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
s, t = map(lambda x: "v<^>".index(x), input().split()) n = int(input()) cw = (n + s - t) % 4 == 0 ccw = (n + t - s) % 4 == 0 if cw and not ccw: print("cw") elif not cw and ccw: print("ccw") else: print("undefined")
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; char a, b; cin >> a >> b >> n; if (n % 2 == 0) cout << "undefined"; else if (n % 4 == 1) { if (a == '<' && b == '^') cout << "cw"; else if (a == '^' && b == '>') cout << "cw"; else if (a == '>' && b == 'v') cout << "cw"; else if (a == 'v' && b == '<') cout << "cw"; else cout << "ccw"; } else { if (a == '<' && b == '^') cout << "ccw"; else if (a == '^' && b == '>') cout << "ccw"; else if (a == '>' && b == 'v') cout << "ccw"; else if (a == 'v' && b == '<') cout << "ccw"; else cout << "cw"; } }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; int main() { char start, end; cin >> start >> end; int n; cin >> n; int mapa[120]; mapa[0] = 'v'; mapa[1] = '<'; mapa[2] = '^'; mapa[3] = '>'; mapa['v'] = 0; mapa['<'] = 1; mapa['^'] = 2; mapa['>'] = 3; int ans = 0; if ((mapa[start] + n) % 4 == mapa[end]) ans++; if ((mapa[start] + 4 - (n % 4)) % 4 == mapa[end]) ans--; if (ans < 0) printf("ccw\n"); if (ans > 0) printf("cw\n"); if (!ans) printf("undefined\n"); return 0; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { String clock = "^>v<^>v<"; String counter = "^<v>^<v>"; public void solve(int testNumber, FastReader in, PrintWriter out) { char[] s = in.nextCharacterArray(2); int len = s.length; int n = in.nextInt(); boolean undefined = false; boolean cw = false; boolean ccw = false; int ind1cw = clock.indexOf(s[0]); int ind2cw = clock.indexOf(s[1], ind1cw); int ind1ccw = counter.indexOf(s[0]); int ind2ccw = counter.indexOf(s[1], ind1ccw); if (Math.abs(ind1ccw - ind2ccw) == n % 4 && Math.abs(ind1cw - ind2cw) == n % 4) { undefined = true; } else if (Math.abs(ind1ccw - ind2ccw) == n % 4) { ccw = true; } else if (Math.abs(ind1cw - ind2cw) == n % 4) { cw = true; } out.println(undefined ? "undefined" : (cw ? "cw" : "ccw")); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char nextCharacter() { int c = pread(); while (isSpaceChar(c)) c = pread(); return (char) c; } public char[] nextCharacterArray(int n) { char[] chars = new char[n]; for (int i = 0; i < n; i++) { chars[i] = nextCharacter(); } return chars; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
states = ['v', '<', '^', '>'] start, end = input().split() n = int(input()) cw = False ccw = False if states[(states.index(start)+n)%4] == end: cw = True if states[(states.index(start)-n)%4] == end: ccw = True print(['cw','ccw','undefined'][cw+ccw-(1-ccw)])
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; public class tenth { public static void main(String args[]) throws Exception { String sp=br.readLine().trim(); String ss[]=sp.split(" "); int t=input()%4,j=0,k=0; if(sp.charAt(0)=='^') j=0; if(sp.charAt(0)=='>') j=1; if(sp.charAt(0)=='v') j=2; if(sp.charAt(0)=='<') j=3; if(sp.charAt(2)=='^') k=0; if(sp.charAt(2)=='>') k=1; if(sp.charAt(2)=='v') k=2; if(sp.charAt(2)=='<') k=3; if(t==0||t==2){ System.out.print("undefined"); } else if((j+t)%4==k){ System.out.print("cw"); } else{ System.out.print("ccw"); } } static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); private static String s[]; public static void input(int a[], int p) throws IOException { s = br.readLine().split(" "); int i; for (i = 0; i < p; i++) { a[i] = Integer.parseInt(s[i]); } } public static void input(long a[], int p) throws IOException { s = br.readLine().split(" "); int i; for (i = 0; i < p; i++) { a[i] = Long.parseLong(s[i]); } } public static void input(double a[], int p) throws IOException { s = br.readLine().split(" "); int i; for (i = 0; i < p; i++) { a[i] = Double.parseDouble(s[i]); } } public static int input() throws IOException { int a; a = Integer.parseInt(br.readLine()); return a; } }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; const long long MOD = 1e9 + 7; const long long INF = 1e9; long long n, l, k; long long array[1 << 18]; int main() { map<char, int> ref = {{'^', 0}, {'>', 1}, {'v', 2}, {'<', 3}}; char s, e; cin >> s >> e; int n; cin >> n; int tot = (n / 4 + 1) * 4; bool a = (tot + (ref[s] - n)) % 4 == ref[e]; bool b = (ref[s] + n) % 4 == ref[e]; if (a && b) { cout << "undefined" << '\n'; return 0; } if (a) cout << "ccw" << '\n'; else if (b) cout << "cw" << '\n'; }
CPP
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import java.io.*; import java.util.*; public class Main { static ContestScanner in; static Writer out; public static void main(String[] args) { Main main = new Main(); try { in = new ContestScanner(); out = new Writer(); main.solve(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } void solve() throws IOException { char[] cw = {'^', '>', 'v', '<'}; char[] s = {in.nextToken().charAt(0), in.nextToken().charAt(0)}; int d = in.nextInt() % 4; if(d % 2 == 0) { System.out.println("undefined"); return; } int st = 0; for (int i = 0; i < 4; i++) { if(s[0] == cw[i]) { st = i; break; } } int dir = d == 3 ? -1 : 1; if(s[1] == cw[(st + 1) % 4]) dir *= 1; else dir *= -1; System.out.println(dir > 0 ? "cw" : "ccw"); } } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException{super(System.out);} } class ContestScanner implements Closeable{ private BufferedReader in;private int c=-2; public ContestScanner()throws IOException {in=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws IOException {in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} public void close() throws IOException {in.close();} }
JAVA
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
s, n, f, sp= input(), int(input()), 0, 'v<^>' l = sp.index(s[0]) if sp[(l + n) % 4] == s[2]: f += 1 if sp[(l - n) % 4] == s[2]: f += 2 if f == 1: print('cw') elif f == 2: print('ccw') else: print('undefined')
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
l = raw_input() n = int(raw_input()) % 4 alpha = "v<^>" if n == 0 or n == 2: print "undefined" else: orig = 0 while alpha[orig] != l[0]: orig += 1 now = 0 while alpha[now] != l[2]: now += 1 if (orig+n)%4 == now: print "cw" elif (orig-n+8)%4 == now: print "ccw" else: print "undefined"
PYTHON
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
d, s, n = '^>v<', input(), int(input()) if n % 2 == 0: print("undefined") elif (d.find(s[0])+n)%4 == d.find(s[2]): print("cw") else: print("ccw") # Made By Mostafa_Khaled
PYTHON3
834_A. The Useless Toy
<image> Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): <image> After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number n is given (0 ≤ n ≤ 109) – the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise. Examples Input ^ &gt; 1 Output cw Input &lt; ^ 3 Output ccw Input ^ v 6 Output undefined
2
7
import sys sys.setrecursionlimit=2000000 init,end=raw_input().strip().split() t=int(raw_input()) pos={'^':0,'>':1,'v':2,'<':3} ninit=pos[init] nend=pos[end] if (ninit+t)%4==nend: if (ninit-t)%4==nend: print 'undefined' else: print 'cw' else: if (ninit-t)%4==nend: print 'ccw' else: print 'undefined'
PYTHON
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; import java.text.*; import javax.swing.plaf.basic.BasicScrollPaneUI.HSBChangeListener; import java.io.*; import java.math.*; public class code5 { InputStream is; PrintWriter out; static long mod=pow(10,9)+7; static int dx[]={0,0,1,-1},dy[]={1,-1,0,0}; void solve() throws Exception { int n=ni(); int m=ni(); int k=ni(); int size=1000000; ArrayList<Pair> alarrive[]=new ArrayList[size+1]; ArrayList<Pair> aldep[]=new ArrayList[size+1]; for(int i=0;i<=size;i++) { alarrive[i]=new ArrayList<Pair>(); aldep[i]=new ArrayList<Pair>(); } for(int i=0;i<m;i++) { int d=ni(); int f=ni(); int t=ni(); int c=ni(); if(f==0) aldep[d].add(new Pair(t,c)); else alarrive[d].add(new Pair(f,c)); } long prefix[]=new long[size+1]; Arrays.fill(prefix,Long.MAX_VALUE); long suffix[]=new long[size+2]; Arrays.fill(suffix,Long.MAX_VALUE); long sta[]=new long[n+1]; Arrays.fill(sta,-1); long std[]=new long[n+1]; int c1=0,c2=0; Arrays.fill(std,-1); long p1=0,p2=0; for(int i=1;i<=size;i++) { for(int j=0;j<alarrive[i].size();j++) { if(sta[alarrive[i].get(j).x]==-1) { c1++; p1+=alarrive[i].get(j).y; sta[alarrive[i].get(j).x]=alarrive[i].get(j).y; }else if(sta[alarrive[i].get(j).x]==-1||sta[alarrive[i].get(j).x]>alarrive[i].get(j).y){ p1-=sta[alarrive[i].get(j).x]; sta[alarrive[i].get(j).x]=alarrive[i].get(j).y; p1+=alarrive[i].get(j).y; } } if(c1==n) { prefix[i]=Math.min(prefix[i-1],p1); } } for(int i=size;i>=1;i--) { for(int j=0;j<aldep[i].size();j++) { if(std[aldep[i].get(j).x]==-1) { c2++; p2+=aldep[i].get(j).y; std[aldep[i].get(j).x]=aldep[i].get(j).y; }else if(std[aldep[i].get(j).x]>aldep[i].get(j).y) { p2-=std[aldep[i].get(j).x]; std[aldep[i].get(j).x]=aldep[i].get(j).y; p2+=aldep[i].get(j).y; } } if(c2==n) { suffix[i]=Math.min(suffix[i+1],p2); } } long ans=Long.MAX_VALUE; for(int i=1;i+k<=size;i++) { long q1=prefix[i-1]; long q2=suffix[i+k]; if(q1==Long.MAX_VALUE||q2==Long.MAX_VALUE) continue; ans=Math.min(q1+q2,ans); } out.println(ans==Long.MAX_VALUE?-1:ans); } public static int count(int x) { int num=0; while(x!=0) { x=x&(x-1); num++; } return num; } static long d, x, y; void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } long modInverse(long A,long M) //A and M are coprime { extendedEuclid(A,M); return (x%M+M)%M; //x may be negative } public static void mergeSort(int[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(int arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); int left[] = new int[n1]; int right[] = new int[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } public static void mergeSort(long[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(long arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); long left[] = new long[n1]; long right[] = new long[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } static class Pair implements Comparable<Pair>{ int x,y,k; int i,dir; long cost; Pair (int x,int y){ this.x=x; this.y=y; } public int compareTo(Pair o) { if(o.x!=this.x) return o.x-this.x; else return this.y-o.y; } public String toString(){ return x+" "+y+" "+k+" "+i;} public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y == y ; } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode(); } } public static boolean isPal(String s){ for(int i=0, j=s.length()-1;i<=j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } public static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x,long y){ if(y==0) return x; else return gcd(y,x%y); } public static int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } public static long gcdExtended(long a,long b,long[] x){ if(a==0){ x[0]=0; x[1]=1; return b; } long[] y=new long[2]; long gcd=gcdExtended(b%a, a, y); x[0]=y[1]-(b/a)*y[0]; x[1]=y[0]; return gcd; } public static int abs(int a,int b){ return (int)Math.abs(a-b); } public static long abs(long a,long b){ return (long)Math.abs(a-b); } public static int max(int a,int b){ if(a>b) return a; else return b; } public static int min(int a,int b){ if(a>b) return b; else return a; } public static long max(long a,long b){ if(a>b) return a; else return b; } public static long min(long a,long b){ if(a>b) return b; else return a; } public static long pow(long n,long p,long m){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } public static long pow(long n,long p){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void run() throws Exception { is = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new code5().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); //new code5().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.util.*; public class B853 { public static void main(String args[])throws IOException { Reader sc=new Reader(); int n=sc.nextInt(); int m=sc.nextInt(); int k=sc.nextInt(); List<flight> pln[]=new ArrayList[1000001]; int arr[][]=new int[n+1][2];int minday=0,maxday=10000000; for(int i=1;i<=1000000;i++) { pln[i]=new ArrayList<>(); } for(int i=0;i<m;i++) { int day=sc.nextInt(); int ind=sc.nextInt(); int aa=sc.nextInt(); int pri=sc.nextInt(); if(aa==0) { pln[day].add(new flight(ind,pri,0)); if(arr[ind][0]==0) arr[ind][0]=day; else if(day<arr[ind][0]) arr[ind][0]=day; } else { pln[day].add(new flight(aa,pri,1)); if(day>arr[aa][1]) arr[aa][1]=day; } } for(int i=1;i<=n;i++) { minday=Math.max(minday,arr[i][0]); maxday=Math.min(maxday,arr[i][1]); } if(maxday<=minday+k) { System.out.println(-1); System.exit(0); } long price[][]=new long[1000001][2];long inf=100000000000000L; arr=new int[n+1][2];long p1=0,p2=0;long res=inf; for(int i=1;i<=minday;i++) { for(flight ob: pln[i]) { if(ob.ad==0) { if(arr[ob.ind][0]==0) { p1+=ob.pri; arr[ob.ind][0]=ob.pri; } else if(ob.pri<arr[ob.ind][0]) { p1+=ob.pri-arr[ob.ind][0]; arr[ob.ind][0]=ob.pri; } } } } price[minday+k][0]=p1; //System.out.println(p1); for(int i=minday+k+1;i<maxday;i++) { for(flight ob:pln[i-k]) { if(ob.ad==0) { if(arr[ob.ind][0]==0) { p1+=ob.pri; arr[ob.ind][0]=ob.pri; } else if(ob.pri<arr[ob.ind][0]) { p1+=ob.pri-arr[ob.ind][0]; arr[ob.ind][0]=ob.pri; } } } price[i][0]=p1; } for(int i=maxday;i<=1000000;i++) { for(flight ob: pln[i]) { if(ob.ad==1) { if(arr[ob.ind][1]==0) { p2+=ob.pri; arr[ob.ind][1]=ob.pri; } else if(ob.pri<arr[ob.ind][1]) { p2+=ob.pri-arr[ob.ind][1]; arr[ob.ind][1]=ob.pri; } } } } price[maxday][1]=p2; res=Math.min(res,p2+price[maxday-1][0]); for(int i=maxday-1;i>minday+k;i--) { for(flight ob:pln[i]) { if(ob.ad==1) { if(arr[ob.ind][1]==0) { p2+=ob.pri; arr[ob.ind][1]=ob.pri; } else if(ob.pri<arr[ob.ind][1]) { p2+=ob.pri-arr[ob.ind][1]; arr[ob.ind][1]=ob.pri; } } } price[i][1]=p2; res=Math.min(res,p2+price[i-1][0]); } System.out.println(res); /*for(int i=1;i<=maxday;i++) { System.out.println(i+" "+price[i][0]+" "+price[i][1]); }*/ } } class flight { int ind,pri,ad; public flight(int i,int p,int a) { this.ind=i; this.pri=p; this.ad=a; } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte [] buffer; private int bufferPointer, bytesRead; public Reader () { din = new DataInputStream (System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader (String file_name) throws IOException { din = new DataInputStream (new FileInputStream (file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine () throws IOException { byte [] buf = new byte[1024]; int cnt = 0, c; while ((c = read ()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String (buf, 0, cnt); } public int nextInt () throws IOException { int ret = 0; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong () throws IOException { long ret = 0; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble () throws IOException { double ret = 0, div = 1; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (c == '.') while ((c = read ()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer () throws IOException { bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read () throws IOException { if (bufferPointer == bytesRead) fillBuffer (); return buffer[bufferPointer++]; } public void close () throws IOException { if (din == null) return; din.close (); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
R=lambda :map(int,input().split()) n,m,k=R() F,T=[],[] ans=int(1e12) for i in range(m): d,f,t,c=R() if f:F.append((d,f,c)) else:T.append((-d,t,c)) for p in [F,T]: cost=[ans]*(n+1) s=n*ans q=[] p.sort() for d,t,c in p: #print(p) if c<cost[t]: #print(c,cost[t]) s+=c-cost[t] #print(s) cost[t]=c if s<ans: q.append((s,d)) p.clear() #print(q) p+=q #print(p) s,t=ans,(0,0) #print(F,T) for f in F: while f: if f[1]+t[1]+k<0:s=min(s,f[0]+t[0]) elif T: #print(T) t=T.pop() #print(T) # print(t) continue #print(f) f=0 #print(f) print(s if s<ans else -1)
PYTHON3
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct nod { int d, f, t, c; }; long long ans1[1000005], ans2[1000005]; int cmp(nod x, nod y) { return x.d < y.d; } nod arr[100005]; int n, m, k; int vis[100005]; long long sum, num; int main() { while (cin >> n >> m >> k) { for (int i = 1; i <= m; i++) scanf("%d %d %d %d", &arr[i].d, &arr[i].f, &arr[i].t, &arr[i].c); sort(arr + 1, arr + m + 1, cmp); memset(vis, 0, sizeof(vis)); num = 0; sum = 0; int now = 1; for (int day = 1; day <= 1000000; day++) { for (now; now <= m && arr[now].d <= day; now++) { if (arr[now].f == 0) continue; if (vis[arr[now].f] != 0 && vis[arr[now].f] < arr[now].c) continue; if (vis[arr[now].f] == 0) num++; sum = sum - vis[arr[now].f] + arr[now].c; vis[arr[now].f] = arr[now].c; } if (num != n) ans1[day] = 9999999999999999LL; else ans1[day] = sum; } long long best = 9999999999999999LL; num = 0; sum = 0; now = m; memset(vis, 0, sizeof(vis)); for (int day = 1000000; day - k - 1 >= 1; day--) { for (now; now >= 1 && arr[now].d >= day; now--) { if (arr[now].t == 0) continue; if (vis[arr[now].t] != 0 && vis[arr[now].t] < arr[now].c) continue; if (vis[arr[now].t] == 0) num++; sum = sum - vis[arr[now].t] + arr[now].c; vis[arr[now].t] = arr[now].c; } if (num == n) best = min(best, sum + ans1[day - k - 1]); } if (best == 9999999999999999LL) best = -1; cout << best << endl; } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 8; struct node { long long d, s, e, c; } A[N]; struct val { long long E, C; }; int cmp(node A, node B) { return A.d < B.d; } stack<val> sta; long long S[N], E[N], INF = 1000005, llen, rrlen, L, R, n, m, k, idl, idr, cal; void find_ppl() { int knum = 12; if (!knum) puts("yes"); val T, QQ; for (; idl < m; idl++) { if (A[idl].e == 0) { if (A[idl].s == 0) continue; if (S[A[idl].s] > A[idl].c) { if (S[A[idl].s] == INF) L++; llen -= S[A[idl].s]; S[A[idl].s] = A[idl].c; llen += S[A[idl].s]; if (L == n) { int pre = A[idl].d; T.E = A[idl].d; T.C = llen; sta.push(T); } } if (knum < 10) puts("ptr"); } } } void find_ppr() { val T, QQ; int pp = 5; for (int j = 1; j <= 10000; j++) pp *= 1; for (; idr >= 0 && A[idr].d - A[idl].d - 1 >= k; idr--) { if (A[idr].s == 0) { if (A[idr].e == 0) continue; if (E[A[idr].e] > A[idr].c) { if (E[A[idr].e] == INF) R++; rrlen -= E[A[idr].e]; if (pp < 0) return; E[A[idr].e] = A[idr].c; rrlen += E[A[idr].e]; if (R == n) { while (!sta.empty()) { T = sta.top(); if (A[idr].d - T.E - 1 >= k) { if (cal == -1) { cal = T.C + rrlen; } else { cal = min(T.C + rrlen, cal); } break; } else { sta.pop(); } } } } } } } int main() { bool ok = false; if (ok) printf("1111111\n"); scanf("%lld %lld %lld", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%lld%lld%lld%lld", &A[i].d, &A[i].s, &A[i].e, &A[i].c); } string str; sort(A, A + m, cmp); S[0] = E[0] = 0; for (int i = 1; i <= n; i++) { S[i] = E[i] = INF; } llen = rrlen = 1LL * INF * n; idl = L = R = 0; cal = L - 1; idr = m - 1; find_ppl(); set<int> pq; for (int i = 1; i <= 55; i++) pq.insert(i + 2); find_ppr(); printf("%lld\n", cal); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct piii { int d, f, t, c; int i; friend int operator<(const piii &a, const piii &b) { if (a.d != b.d) return a.d < b.d; if (a.f != b.f) return a.f < b.f; if (a.t != b.t) return a.t < b.t; return a.c < b.c; } } a[100008]; struct pii { int a; int i; friend int operator<(const pii &a, const pii &b) { return a.a < b.a; } } ps[100008]; priority_queue<pii> quea; int mina[100008]; int minb[100008]; vector<piii> veca[100008]; vector<piii> vecb[100008]; set<piii> setb; int lastia[100008]; int lastib[100008]; int n, m, k; long long ansa; long long nowa; int main(int argv, char *args[]) { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%d", &a[i].d); scanf("%d", &a[i].f); scanf("%d", &a[i].t); scanf("%d", &a[i].c); } sort(a, a + m); for (int i = 1; i <= n; i++) { mina[i] = 1 << 30; minb[i] = 1 << 30; lastia[i] = -1; lastib[i] = -1; } for (int i = 0; i < m; i++) { if (a[i].f == 0) { continue; } a[i].c = min(a[i].c, mina[a[i].f]); mina[a[i].f] = a[i].c; } for (int i = m - 1; i >= 0; i--) { if (a[i].t == 0) { continue; } a[i].c = min(a[i].c, minb[a[i].t]); minb[a[i].t] = a[i].c; } for (int i = 0; i < m; i++) { if (a[i].f != 0) { veca[a[i].f].push_back(a[i]); } else { vecb[a[i].t].push_back(a[i]); } } for (int i = 1; i <= n; i++) { if (vecb[i].size()) { setb.insert(vecb[i][0]); lastib[i] = 0; nowa += vecb[i][0].c; } else { puts("-1"); exit(0); ; } } ansa = 1ll << 62; int stpos = 0; for (int i = 1; i <= n; i++) { if (veca[i].size() == 0 || vecb[i].size() == 0) { puts("-1"); exit(0); ; } stpos = max(stpos, veca[i][0].d); stpos = max(stpos, vecb[i][0].d - k - 1); } int cnta = 0; for (int i = 0; i < m; i++) { if (a[i].f != 0) { int st = a[i].d; if (lastia[a[i].f] == -1) { cnta++; } else { nowa -= a[lastia[a[i].f]].c; } nowa += a[i].c; lastia[a[i].f] = i; if (cnta == n && setb.size() == n) { while (1) { auto it = setb.begin(); if (it->d - st - 1 < k) { lastib[it->t]++; if (lastib[it->t] >= vecb[it->t].size()) { goto outa; } nowa -= it->c; int tt = it->t; setb.erase(it); setb.insert(vecb[tt][lastib[tt]]); nowa += vecb[tt][lastib[tt]].c; } else { break; } } ansa = min(ansa, nowa); } } else { continue; } } outa: 0; if (ansa == (1ll << 62)) { puts("-1"); exit(0); ; } else { printf("%I64d\n", ansa); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; pair<int, int> flight[200010]; int N, M, K; int D[200010], F[200010], T[200010], C[200010]; long long go[1000000 + 1], back[1000000 + 1], sumGo, sumBack, tmp[200010]; int main() { scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < M; i++) { scanf("%d%d%d%d", D + i, F + i, T + i, C + i); flight[i] = {D[i], i}; } for (int i = 0; i <= 1000000; i++) go[i] = back[i] = 2000000000000LL; sumGo = 2000000000000LL * N; sumBack = 2000000000000LL * N; sort(flight, flight + M); for (int i = 0; i <= N; i++) tmp[i] = 2000000000000LL; for (int i = 0; i < M; i++) { int idx = flight[i].second; if (T[idx] == 0) { sumGo -= tmp[F[idx]]; tmp[F[idx]] = min(tmp[F[idx]], (long long)C[idx]); sumGo += tmp[F[idx]]; } go[D[idx]] = sumGo; } for (int i = 0; i < 1000000; i++) go[i + 1] = min(go[i + 1], go[i]); sort(flight, flight + M, greater<pair<int, int>>()); for (int i = 0; i <= N; i++) tmp[i] = 2000000000000LL; for (int i = 0; i < M; i++) { int idx = flight[i].second; if (F[idx] == 0) { sumBack -= tmp[T[idx]]; tmp[T[idx]] = min(tmp[T[idx]], (long long)C[idx]); sumBack += tmp[T[idx]]; } back[D[idx]] = sumBack; } for (int i = 1000000; i > 0; i--) back[i - 1] = min(back[i - 1], back[i]); long long ans = 2000000000000LL; for (int i = 0; i <= 1000000 - (K + 1); i++) { ans = min(ans, go[i] + back[i + K + 1]); } if (ans == 2000000000000LL) ans = -1; printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; pair<int, int> a[maxn]; int t[maxn], u[maxn], v[maxn], c[maxn], w[maxn], num; long long L[maxn], R[maxn], sum, ans = 1LL << 60; int main() { int N, M, K, pos = 0; scanf("%d%d%d", &N, &M, &K); for (int i = 1; i <= M; i++) scanf("%d%d%d%d", &t[i], &u[i], &v[i], &c[i]); for (int i = 1; i <= M; i++) a[i] = make_pair(t[i], i); sort(a + 1, a + M + 1); for (int i = 1; i <= 1000000; i++) { L[i] = L[i - 1]; while (pos + 1 <= M && a[pos + 1].first <= i) { pos++; int e = a[pos].second; if (v[e] == 0) { if (!w[u[e]]) { num++; sum += c[e]; w[u[e]] = c[e]; if (num == N) L[i] = sum; } else if (c[e] < w[u[e]]) { if (num == N) L[i] -= (w[u[e]] - c[e]); else sum -= (w[u[e]] - c[e]); w[u[e]] = c[e]; } } } } num = 0; sum = 0; pos = M + 1; memset(w, 0, sizeof(w)); for (int i = 1000000; i >= 1; i--) { R[i] = R[i + 1]; while (pos - 1 >= 1 && a[pos - 1].first >= i) { pos--; int e = a[pos].second; if (u[e] == 0) { if (!w[v[e]]) { num++; sum += c[e]; w[v[e]] = c[e]; if (num == N) R[i] = sum; } else if (c[e] < w[v[e]]) { if (num == N) R[i] -= (w[v[e]] - c[e]); else sum -= (w[v[e]] - c[e]); w[v[e]] = c[e]; } } } } for (int i = 1; i <= 1000000 - K - 1; i++) { if (L[i] && R[i + K + 1]) ans = min(ans, L[i] + R[i + K + 1]); } if (ans == 1LL << 60) puts("-1"); else printf("%I64d\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 6.66; const int MAXK = 1e6 + 6.66; const long long inf = 1e16 + 6.66; struct Flight { int day; int in; int out; int price; bool operator<(const Flight& f) const { return day < f.day; } } flights[MAXN]; long long price[MAXN]; long long inc[MAXK]; long long outc[MAXK]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) cin >> flights[i].day >> flights[i].in >> flights[i].out >> flights[i].price; sort(flights, flights + m); fill(price, price + n + 1, inf); int inf_cnt = n; long long ans = 0; for (int lday = 1e6, w = m - 1; lday > -1; lday--) { while (w > -1 && flights[w].day >= lday) { if (flights[w].out == 0) { w--; continue; }; if (price[flights[w].out] == inf) { ans += flights[w].price; price[flights[w].out] = flights[w].price; inf_cnt--; } else if (price[flights[w].out] > flights[w].price) { ans += -price[flights[w].out] + flights[w].price; price[flights[w].out] = flights[w].price; } w--; } if (inf_cnt == 0) outc[lday] = ans; else outc[lday] = inf; } ans = 0, inf_cnt = n; fill(price, price + n + 1, inf); for (int lday = 0, w = 0; lday <= 1e6; lday++) { while (w < m && flights[w].day <= lday) { if (flights[w].in == 0) { w++; continue; }; if (price[flights[w].in] == inf) { ans += flights[w].price; price[flights[w].in] = flights[w].price; inf_cnt--; } else if (price[flights[w].in] > flights[w].price) { ans += -price[flights[w].in] + flights[w].price; price[flights[w].in] = flights[w].price; } w++; } if (inf_cnt == 0) inc[lday] = ans; else inc[lday] = inf; } ans = inf; for (int i = 0; i < 1e6 - k - 1; i++) { ans = min(ans, inc[i] + outc[i + k + 1]); } if (ans >= inf) cout << -1 << endl; else cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, k; long long acc[2000100][2]; int cnt[2000100][2], last[2000100]; vector<pair<int, pair<int, int> > > a, b; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int t, x, y, cost; cin >> t >> x >> y >> cost; if (y == 0) a.push_back(make_pair(t + k + 1, make_pair(x, cost))); if (x == 0) b.push_back(make_pair(t, make_pair(y, cost))); } sort((a).begin(), (a).end()); sort((b).begin(), (b).end()); reverse((b).begin(), (b).end()); memset(last, -1, sizeof(last)); for (int i = 0; i < ((int)(a).size()); i++) { int t = a[i].first; int x = a[i].second.first; int cost = a[i].second.second; if (last[x] != -1) { acc[t][0] -= last[x]; last[x] = min(last[x], cost); } else { last[x] = cost; cnt[t][0]++; } acc[t][0] += last[x]; } for (int i = 1; i < 2000100; i++) cnt[i][0] += cnt[i - 1][0], acc[i][0] += acc[i - 1][0]; memset(last, -1, sizeof(last)); for (int i = 0; i < ((int)(b).size()); i++) { int t = b[i].first; int x = b[i].second.first; int cost = b[i].second.second; if (last[x] != -1) { acc[t][1] -= last[x]; last[x] = min(last[x], cost); } else { last[x] = cost; cnt[t][1]++; } acc[t][1] += last[x]; } long long ans = 10000000000000000LL; for (int i = 2000100 - 2; i >= 0; i--) { acc[i][1] += acc[i + 1][1]; cnt[i][1] += cnt[i + 1][1]; if (cnt[i][1] == n && cnt[i][0] == n) { ans = min(ans, acc[i][1] + acc[i][0]); } } if (ans == 10000000000000000LL) ans = -1; cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int read() { char ch = getchar(); int f = 0; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') { f = f * 10 + (ch ^ 48); ch = getchar(); } return f; } struct data { int cost; int pla; int tim; } come[100005], leave[100005]; int tot1, tot2, val[100005], finish_time, cou, val2[100005], cou2, finish_time2; long long sum, finish[1000005], sum2, ans = 900000000000000000LL; bool cmp(data x, data y) { return x.tim < y.tim; } bool cmp2(data x, data y) { return x.tim > y.tim; } int main() { int n = read(), m = read(), k = read(); for (int i = 1; i <= m; i++) { int d = read(), f = read(), t = read(), c = read(); if (f == 0) { leave[++tot1].pla = t; leave[tot1].cost = c; leave[tot1].tim = d; } else if (t == 0) { come[++tot2].pla = f; come[tot2].cost = c; come[tot2].tim = d; } } sort(come + 1, come + tot2 + 1, cmp); sort(leave + 1, leave + tot1 + 1, cmp2); for (int i = 1; i <= tot2; i++) { if (!val[come[i].pla]) { val[come[i].pla] = come[i].cost; sum += come[i].cost; cou++; if (cou == n) { finish_time = come[i].tim; finish[come[i].tim] = sum; } } else { if (come[i].cost >= val[come[i].pla]) continue; sum += come[i].cost; sum -= val[come[i].pla]; val[come[i].pla] = come[i].cost; if (cou == n) { finish[come[i].tim] = sum; } } } for (int i = finish_time; i <= 1000000; i++) { if (finish[i] == 0) finish[i] = finish[i - 1]; } for (int i = 1; i <= tot1; i++) { if (finish_time + k + 1 > leave[i].tim) break; if (!val2[leave[i].pla]) { cou2++; val2[leave[i].pla] = leave[i].cost; sum2 += leave[i].cost; if (cou2 == n && finish_time + k <= leave[i].tim) { ans = min(ans, sum2 + finish[leave[i].tim - k - 1]); } } else { if (leave[i].cost >= val2[leave[i].pla]) continue; sum2 += leave[i].cost; sum2 -= val2[leave[i].pla]; val2[leave[i].pla] = leave[i].cost; if (cou2 == n) { ans = min(ans, sum2 + finish[leave[i].tim - k - 1]); } } } if (ans == 900000000000000000LL || !finish_time || cou2 != n || cou != n) puts("-1"); else cout << ans; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 100002; int n, m, k, currentOutCost[N], cntOut, cntIn; struct flight { int i, d, c; } in[N], out[N]; multiset<int> a[N]; bool cmp(flight x, flight y) { return x.d < y.d; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = (1); i <= (int)(m); ++i) { int d, first, t, c; scanf("%d%d", &d, &first); scanf("%d%d", &t, &c); if (first) out[++cntOut] = {first, d, c}; else in[++cntIn] = {t, d, c}; } sort(out + 1, out + 1 + cntOut, cmp); sort(in + 1, in + 1 + cntIn, cmp); long long ans = 1000000000000000000ll, currOut = 0, currInSum = 0, currOutSum = 0, j = 1; for (int i = (1); i <= (int)(cntIn); ++i) a[in[i].i].insert(in[i].c); for (int i = (1); i <= (int)(n); ++i) if (((int)(a[i]).size())) currInSum += *a[i].begin(); else return cout << -1, 0; for (int i = (1); i <= (int)(cntOut); ++i) { int node = out[i].i, d = out[i].d, c = out[i].c; while (j <= cntIn and in[j].d <= d + k) { currInSum -= *a[in[j].i].begin(); a[in[j].i].erase(a[in[j].i].find(in[j].c)); if (!((int)(a[in[j].i]).size())) goto fn; currInSum += *a[in[j].i].begin(); ++j; } if (!currentOutCost[node]) { ++currOut; currInSum += currentOutCost[node] = c; } else { currInSum -= currentOutCost[node]; currentOutCost[node] = min(currentOutCost[node], c); currInSum += currentOutCost[node]; } if (currOut == n) ans = min(ans, currOutSum + currInSum); } fn: cout << (ans == 1000000000000000000ll ? -1 : ans); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 1000 + 5; vector<pair<int, int>> from[N]; vector<pair<int, int>> to[N]; long long inf = 1e13; long long best_from[N]; long long best_to[N]; long long cost[N]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (t == 0) { from[d].push_back({f, c}); } if (f == 0) { to[d].push_back({t, c}); } } long long sum = inf * n; for (int i = 1; i <= n; i++) { cost[i] = inf; } for (int i = 1; i < N; i++) { for (auto p : from[i]) { int x = p.first; int e = p.second; if (cost[x] > e) { sum -= cost[x] - e; cost[x] = e; } } best_from[i] = sum; } sum = inf * n; for (int i = 1; i <= n; i++) { cost[i] = inf; } for (int i = N - 1; i > 0; i--) { for (auto p : to[i]) { int x = p.first; int e = p.second; if (cost[x] > e) { sum -= cost[x] - e; cost[x] = e; } } best_to[i] = sum; } long long res = n * inf; for (int i = 1; i < N - k - 1; i++) { res = min(res, best_from[i] + best_to[i + k + 1]); } printf("%lld\n", res < inf ? res : -1); }
CPP