468A - 24 Game - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

import sys
import threading
from random import randint
import os
from io import BytesIO, IOBase

from collections import defaultdict, Counter, deque
from math import inf, sqrt, floor
from functools import lru_cache
from heapq import heappop, heappush, heapify
import math
from bisect import bisect_left, bisect_right
 
nextInts = lambda: list(map(int, input().split()))
nextInt = lambda: int(input())
Mod = 10**9 + 7

def remove(n):
    print(str(n), "-", str(n-1), "=", "1")
    print(1, "*", 1, "=", 1)

def solution():
    n = nextInt()
    if n < 4:
        print("NO")
        return

    print("YES")
    for i in range(5 + n%2, n,2)[::-1]:
        remove(i+1)

    if n % 2:
        print("2 - 1 = 1")
        print("4 * 5 = 20")
        print("1 + 3 = 4")
        print("4 + 20 = 24")
    else:
        print("1 * 2 = 2")
        print("2 * 3 = 6")
        print("6 * 4 = 24")

def main():

    t = 1
        for _ in range(t):
        solution()



BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


sys.setrecursionlimit(10**6)
threading.stack_size(1 << 27)
thread = threading.Thread(target=main)
thread.start(); thread.join()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
template<class T>inline T Min(register T x,register T y){
	return x<y?x:y;
}
template<class T>inline T Max(register T x,register T y){
	return x>y?x:y;
}
template<class T>inline void Cmin(register T &x,register T y){
	x=Min(x,y);
}
template<class T>inline void Cmax(register T &x,register T y){
	x=Max(x,y);
}
inline long long read(){
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9'){
        if(ch=='-'){
            f=-1;
        }
        ch=getchar();
    }
    while(ch>='0' && ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=getchar();
    }
    return x*f;
}
inline bool write(long long x,char ch='\n'){
    static char buf[64];
    static long long len=-1;
    if(x<0){
        putchar('-');
        x=-x;
    }
    do{
        buf[++len]=x%10;
        x/=10;
    }
    while(x);
    while(len>=0){
        putchar(buf[len--]+'0');
    }
    putchar(ch);
    return true;
}
long long n;
int main(){
	n=read();
    if(n<=3){
        return !printf("NO\n");
    }
    else if(n>=4){
    	printf("YES\n");
	    if(n%2==1){
	    	printf("5 * 4 = 20\n20 + 3 = 23\n23 + 2 = 25\n25 - 1 = 24\n");
		    for(register int i=n;i>=6;i-=2){
		    	printf("%d - %d = 1\n24 * 1 = 24\n",i,i-1);
		    }
	    }
	    else{
	    	printf("1 + 2 = 3\n3 + 3 = 6\n4 * 6 = 24\n");
		    for(register int i=n;i>=5;i-=2){
		    	printf("%d - %d = 1\n24 * 1 = 24\n",i,i-1);
		    }
		}
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64