NOI 2009 Solutions to Contest Tasks

Similar documents
Cristina Nita-Rotaru. CS355: Cryptography. Lecture 4: Enigma.

Cryptography A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering. Apr Cryptography Slide 1

Algebra for Cryptology

Cryptography A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering. Apr Cryptography Slide 1

Attacks on RSA & Using Asymmetric Crypto

CS/COE

Number Theory in Cryptography

The Hill Cipher A Linear Algebra Perspective

1 Recommended Reading 1. 2 Public Key/Private Key Cryptography Overview RSA Algorithm... 2

Can a Machine Think?

Cryptography. P. Danziger. Transmit...Bob...

Facts & Myths of Enigma

RSA RSA public key cryptosystem

CS 282A/MATH 209A: Foundations of Cryptography Prof. Rafail Ostrosky. Lecture 4

Cryptography: A Fairy Tale for Mathematicians and Starring Mathematicians!

6.080/6.089 GITCS April 8, Lecture 15

Lecture 1: Introduction to Public key cryptography

Number theory (Chapter 4)

last name ID 1 c/cmaker/cbreaker 2012 exam version a 6 pages 3 hours 40 marks no electronic devices SHOW ALL WORK

Cryptography. pieces from work by Gordon Royle

QUANTUM CRYPTOGRAPHY. BCS, Plymouth University, December 1, Professor Kurt Langfeld Centre for Mathematical Sciences, Plymouth University

1 Indistinguishability for multiple encryptions

9 Knapsack Cryptography

What is the computational cost of automating brilliance or serendipity? (P vs NP question and related musings) COS 116: 4/12/2006 Adam Finkelstein

L7. Diffie-Hellman (Key Exchange) Protocol. Rocky K. C. Chang, 5 March 2015

Hamiltonian Cycle. Zero Knowledge Proof

Introduction to Cryptography. Lecture 8

Specialized Cryptanalytic Machines: Two examples, 60 years apart. Patrick Schaumont ECE Department Virginia Tech

Clock Arithmetic and Euclid s Algorithm

CRYPTOGRAPHY AND LARGE PRIMES *

Data Structures in Java

19 Lorenz Cipher Machine

Topics in Complexity

Cryptography and Number Theory

Mathematics: Modeling Our World Unit 2: SECRET CODES SUPPLEMENTAL ACTIVITY THE GOLD BUG S2.1

3.6.1 Building Functions from Context. Warm Up

CSA E0 235: Cryptography March 16, (Extra) Lecture 3

Ciphers: Making and Breaking

Introduction to Cryptographic Engineering. Steven M. Bellovin

CHALMERS GÖTEBORGS UNIVERSITET. TDA352 (Chalmers) - DIT250 (GU) 11 April 2017, 8:30-12:30

On the Legacy of Quantum Computation and Communication to Cryptography

Tractable & Intractable Problems

Systems of Equations and Inequalities

Lecture 38: Secure Multi-party Computation MPC

What is the computational cost of automating brilliance or serendipity? (Computational complexity & P vs NP) COS 116, Spring 2010 Adam Finkelstein

Quantum Entanglement and Cryptography. Deepthi Gopal, Caltech

Historical cryptography. cryptography encryption main applications: military and diplomacy

Notes 10: Public-key cryptography

CRYPTOGRAPHY AND NUMBER THEORY

Introduction to Elliptic Curve Cryptography. Anupam Datta

Grade 6 Math Circles October 20/21, Formalism and Languages: Beyond Regular Languages

Cryptography. Lecture 2: Perfect Secrecy and its Limitations. Gil Segev

Solutions for week 1, Cryptography Course - TDA 352/DIT 250

Systems of Equations and Inequalities

Cryptography and RSA. Group (1854, Cayley) Upcoming Interview? Outline. Commutative or Abelian Groups

Network Security Technology Spring, 2018 Tutorial 3, Week 4 (March 23) Due Date: March 30

Logic gates. Quantum logic gates. α β 0 1 X = 1 0. Quantum NOT gate (X gate) Classical NOT gate NOT A. Matrix form representation

Private Key Cryptography. Fermat s Little Theorem. One Time Pads. Public Key Cryptography

8 Elliptic Curve Cryptography

CPSC 467b: Cryptography and Computer Security

Optimization Prof. A. Goswami Department of Mathematics Indian Institute of Technology, Kharagpur. Lecture - 20 Travelling Salesman Problem

Chapter 2 Classical Cryptosystems

quantum distribution of a sudoku key Sian K. Jones University of South Wales

1999 version 2001 simplified version

Question 1. The Chinese University of Hong Kong, Spring 2018

An Introduction to Cryptography

In fact, 3 2. It is not known whether 3 1. All three problems seem hard, although Shor showed that one can solve 3 quickly on a quantum computer.

SAT, Coloring, Hamiltonian Cycle, TSP

CIS 6930/4930 Computer and Network Security. Topic 5.2 Public Key Cryptography

Elliptic curves: Theory and Applications. Day 4: The discrete logarithm problem.

COS433/Math 473: Cryptography. Mark Zhandry Princeton University Spring 2017

Linear Equations and Inequalities

Chapter 7: Systems of Linear Equations

Theory of Computation CS3102 Spring 2014 A tale of computers, math, problem solving, life, love and tragic death

CPSC 467: Cryptography and Computer Security

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Practice Assignment 2 Discussion 24/02/ /02/2018

Class 24: Computability

ENEE 457: Computer Systems Security 09/19/16. Lecture 6 Message Authentication Codes and Hash Functions

Security Implications of Quantum Technologies

Analysis of Algorithms. Unit 5 - Intractable Problems

Lecture 5: Pseudorandom functions from pseudorandom generators

Algorithms and Data Structures Final Lesson

Public Key Cryptography

2016 King s College Math Competition. Instructions

17.1 Binary Codes Normal numbers we use are in base 10, which are called decimal numbers. Each digit can be 10 possible numbers: 0, 1, 2, 9.

Chapter 2. A Look Back. 2.1 Substitution ciphers

Lecture 11: Hash Functions, Merkle-Damgaard, Random Oracle

Exam Security January 19, :30 11:30

CPSC 467b: Cryptography and Computer Security

P, NP, NP-Complete. Ruth Anderson

CSE 105 Theory of Computation

2016 King s College Math Competition. Instructions

Public-Key Cryptosystems CHAPTER 4

(Updated February 17, 2006) Laszlo B. Kish.

Winter 2008 Introduction to Modern Cryptography Benny Chor and Rani Hod. Assignment #2

Unit 5 Test Review Systems of Linear Equations Name Class Date

CS3233 Competitive i Programming

Chapter 2 : Perfectly-Secret Encryption

Lecture th January 2009 Fall 2008 Scribes: D. Widder, E. Widder Today s lecture topics

Transcription:

NOI 2009 Solutions to Contest Tasks Martin Henz March 14, 2009 Martin Henz NOI 2009 Solutions to Contest Tasks 1

The Scientific Committee of NOI 2009 Ben Leong Chang Ee-Chien Colin Tan Frank Stephan Martin Henz Sung Wing Kin Martin Henz NOI 2009 Solutions to Contest Tasks 2

1 XMAS 2 3 4 Martin Henz NOI 2009 Solutions to Contest Tasks 3

Program in Pascal 1 XMAS Program in Pascal 2 3 4 Martin Henz NOI 2009 Solutions to Contest Tasks 4

XMAS Program in Pascal At a Secret Santa Christmas party, each guest brings a gift and places it under the Christmas tree. Each guest gets exactly one gift from under the tree. Martin Henz NOI 2009 Solutions to Contest Tasks 5

XMAS Program in Pascal At a Secret Santa Christmas party, each guest brings a gift and places it under the Christmas tree. Each guest gets exactly one gift from under the tree. You are given a list that says whose gift each person receives, and need to compile a list of guests that pick each person s gift. Martin Henz NOI 2009 Solutions to Contest Tasks 6

XMAS Program in Pascal Alice brings an apple, Bob brings a balloon, and Carol brings a cake. Given: Alice gets the balloon, Bob gets the cake, and Carol gets the apple. Required list: Apple goes to Carol, balloon goes to Alice, cake goes to Bob. Martin Henz NOI 2009 Solutions to Contest Tasks 7

XMAS Program in Pascal Picking a gift corresponds to a permutation of the list of guests. Martin Henz NOI 2009 Solutions to Contest Tasks 8

XMAS Program in Pascal Picking a gift corresponds to a permutation of the list of guests. Task compute the inverse of the permutation. Martin Henz NOI 2009 Solutions to Contest Tasks 9

XMAS Program in Pascal Idea For each gift, remember its receiver. Then output the receivers in a loop. Martin Henz NOI 2009 Solutions to Contest Tasks 10

Program in Pascal XMAS Program in Pascal program xmas ; var R e c e i v e r : array [ 1.. 1 0 0 0 0 0 ] of i n t e g e r ; g u e s t s, g i f t s, guest, g i f t : i n t e g e r ; begin readln ( g u e s t s ) ; g i f t s := g u e s t s ; f o r g u e s t := 1 to g u e s t s do begin readln ( g i f t ) ; R e c e i v e r [ g i f t ] := g u e s t ; end ; f o r g i f t := 1 to g i f t s do w r i t e l n ( R e c e i v e r [ g i f t ] ) ; end. Martin Henz NOI 2009 Solutions to Contest Tasks 11

Complexity XMAS Program in Pascal There is a fixed amount of work done in each loop body. Martin Henz NOI 2009 Solutions to Contest Tasks 12

Complexity XMAS Program in Pascal There is a fixed amount of work done in each loop body. Each loop runs through all guests (gifts). Martin Henz NOI 2009 Solutions to Contest Tasks 13

Complexity XMAS Program in Pascal There is a fixed amount of work done in each loop body. Each loop runs through all guests (gifts). Overall: the runtime grows proportional to the number of guests. Martin Henz NOI 2009 Solutions to Contest Tasks 14

1 XMAS 2 3 4 Martin Henz NOI 2009 Solutions to Contest Tasks 15

Investment options: oil, shares, steel, silver and gold. Investor has to commit to an investment plan over m months. Investor has to commit to a sum of $2520 which is invested each month. Each month, the investor has to buy one product for $2520. Investor has to buy the same product in the first 15 months of the plan (or in all months if it is shorter than 15 months). Two consecutive changes of the product bought must be separated by at least 14 months, in which the product remains unchanged. Martin Henz NOI 2009 Solutions to Contest Tasks 16

XMAS For a 36 months plan it is possible to buy gold the first 18 months, then silver the next 15 months and then gold again the remaining three months. Martin Henz NOI 2009 Solutions to Contest Tasks 17

Input XMAS 6 7 2 2 6 6 3 1 7 3 3 8 4 3 2 7 5 5 7 6 6 6 4 7 2 7 1 6 6 2 2 3 4 7 7 8 First six rows contain buy price in given month; last row contains sell price after last month. Martin Henz NOI 2009 Solutions to Contest Tasks 18

Desired Output XMAS Best returns: The amount of money that can be earned from selling all investments at the end of the period, after investing each month in the best possible way. Martin Henz NOI 2009 Solutions to Contest Tasks 19

A Naive Approach Consider all possible combinations of investments. : G-G-O-O-O-S For each combination, check that it meets the rules. If it meets the rules, compute the returns. Output the highest return. Martin Henz NOI 2009 Solutions to Contest Tasks 20

XMAS Complexity! We have to go through 5 months combinations! Martin Henz NOI 2009 Solutions to Contest Tasks 21

First Idea XMAS Observation We are allowed to change the investment product only after at least 15 months Martin Henz NOI 2009 Solutions to Contest Tasks 22

First Idea XMAS Observation We are allowed to change the investment product only after at least 15 months Reduce combinations Only consider months in which a change of product happens. Martin Henz NOI 2009 Solutions to Contest Tasks 23

First Idea XMAS Observation We are allowed to change the investment product only after at least 15 months Reduce combinations Only consider months in which a change of product happens. Complexity reduces to 5 months/15 combinations! Martin Henz NOI 2009 Solutions to Contest Tasks 24

Second Idea XMAS Memoization Once we have computed the best return when starting with a certain product on a given month, we don t need to compute it any more; we simply remember the computed value in a table. Martin Henz NOI 2009 Solutions to Contest Tasks 25

Second Idea Memoization Once we have computed the best return when starting with a certain product on a given month, we don t need to compute it any more; we simply remember the computed value in a table. Complexity The overall runtime reduces to numberofproducts numberofmonths Martin Henz NOI 2009 Solutions to Contest Tasks 26

1 XMAS 2 3 4 Martin Henz NOI 2009 Solutions to Contest Tasks 27

XMAS The map of a house is given by a grid like this one: B F X X F F X X F S A lazy cat at a S tarting point wants to pick up all F ood items and then go to B ed. Martin Henz NOI 2009 Solutions to Contest Tasks 28

Rules B F X X F F X X F S Only step left, right, up or down Do not step on walls marked with X Martin Henz NOI 2009 Solutions to Contest Tasks 29

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Martin Henz NOI 2009 Solutions to Contest Tasks 30

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Martin Henz NOI 2009 Solutions to Contest Tasks 31

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Optimal tour visiting Germany s 15 largest cities Martin Henz NOI 2009 Solutions to Contest Tasks 32

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Optimal tour visiting Germany s 15 largest cities Shortest among 43,589,145,600 possible tours Martin Henz NOI 2009 Solutions to Contest Tasks 33

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Optimal tour visiting Germany s 15 largest cities Shortest among 43,589,145,600 possible tours There is no known algorithm that can solve this problem efficiently Martin Henz NOI 2009 Solutions to Contest Tasks 34

Traveling Salesman (TSP) The problem Given a list of cities and their pairwise distances, the task is to find a shortest possible tour that visits each city exactly once. Optimal tour visiting Germany s 15 largest cities Shortest among 43,589,145,600 possible tours There is no known algorithm that can solve this problem efficiently Runtime of best algorithm grows exponentially with number of cities Martin Henz NOI 2009 Solutions to Contest Tasks 35

TSP With A Twist XMAS B F X X F F X X F S Martin Henz NOI 2009 Solutions to Contest Tasks 36

TSP With A Twist XMAS B F X X F F X X F S What is the distance between F ood items? We need to compute the shortest path between two food items, considering the walls marked with X Martin Henz NOI 2009 Solutions to Contest Tasks 37

Overall Strategy XMAS Compute the distances between each pair of food items and between S and B and the food items using a shortest path algorithm Martin Henz NOI 2009 Solutions to Contest Tasks 38

Overall Strategy Compute the distances between each pair of food items and between S and B and the food items using a shortest path algorithm Try all possible combinations starting with S, going through all F, and ending in S and compute the travel distance Martin Henz NOI 2009 Solutions to Contest Tasks 39

Overall Strategy Compute the distances between each pair of food items and between S and B and the food items using a shortest path algorithm Try all possible combinations starting with S, going through all F, and ending in S and compute the travel distance Remember the shortest distance obtained so far, and at the end, output that number Martin Henz NOI 2009 Solutions to Contest Tasks 40

: Cryptography Some History Breaking a Symmetric Key Cipher Alice sends a message m to Bob. The message may be intercepted by Eve along the way. Martin Henz NOI 2009 Solutions to Contest Tasks 41

: Cryptography Some History Breaking a Symmetric Key Cipher Alice sends a message m to Bob. The message may be intercepted by Eve along the way. Solution Alice encrypts m using a key k that is only known to Bob. Martin Henz NOI 2009 Solutions to Contest Tasks 42

Some History Breaking a Symmetric Key Cipher : Cryptography Alice sends a message m to Bob. The message may be intercepted by Eve along the way. Solution Alice encrypts m using a key k that is only known to Bob. Symmetric key cipher c = E(m, k) m = D(c, k) Martin Henz NOI 2009 Solutions to Contest Tasks 43

Some History Breaking a Symmetric Key Cipher Engima Cryptography in WW-I and WW-II German encryption machine The most common encryption technique in WW-II employed by the German military was based on an encryption machine called Enigma. Martin Henz NOI 2009 Solutions to Contest Tasks 44

Some History Breaking a Symmetric Key Cipher Breaking Enigma Weaknesses Enigma had cryptographic weaknesses, but ultimately the following factors led to the breaking of codes: procedural flaws, operator mistakes, occasional captured machines and key tables Martin Henz NOI 2009 Solutions to Contest Tasks 45

Some History Breaking a Symmetric Key Cipher Breaking Enigma Weaknesses Enigma had cryptographic weaknesses, but ultimately the following factors led to the breaking of codes: procedural flaws, operator mistakes, occasional captured machines and key tables Guessing messages Keine besonderen Vorkommnisse (in English: No particular events ) Martin Henz NOI 2009 Solutions to Contest Tasks 46

Some History Breaking a Symmetric Key Cipher Alan Turing Turing s role in breaking German ciphers Alan Turing worked at Bletchley Park, Britain s codebreaking centre. Martin Henz NOI 2009 Solutions to Contest Tasks 47

Some History Breaking a Symmetric Key Cipher Alan Turing Turing s role in breaking German ciphers Alan Turing worked at Bletchley Park, Britain s codebreaking centre. Bombe Turing devised a number of techniques for breaking German ciphers, including the method of the bombe, an electromechanical machine that could find settings for the Enigma machine. Martin Henz NOI 2009 Solutions to Contest Tasks 48

Some History Breaking a Symmetric Key Cipher Alan Turing Turing s role in breaking German ciphers Alan Turing worked at Bletchley Park, Britain s codebreaking centre. Bombe Turing devised a number of techniques for breaking German ciphers, including the method of the bombe, an electromechanical machine that could find settings for the Enigma machine. Father of computer science Turing provided an influential formalisation of the concept of the algorithm and computation with the Turing machine. Martin Henz NOI 2009 Solutions to Contest Tasks 49

Some History Breaking a Symmetric Key Cipher Breaking a Symmetric Key Cipher Symmetric key cipher c = E(m, k) m = D(c, k) Martin Henz NOI 2009 Solutions to Contest Tasks 50

Some History Breaking a Symmetric Key Cipher Breaking a Symmetric Key Cipher Symmetric key cipher c = E(m, k) m = D(c, k) Attacker stituation Eve gets hold of c, m, E and D, and wants to find k. Martin Henz NOI 2009 Solutions to Contest Tasks 51

Some History Breaking a Symmetric Key Cipher Breaking a Symmetric Key Cipher Symmetric key cipher c = E(m, k) m = D(c, k) Attacker stituation Eve gets hold of c, m, E and D, and wants to find k. Strategy Try all possible keys k i and test: c = E(m, k i )? Martin Henz NOI 2009 Solutions to Contest Tasks 52

Some History Breaking a Symmetric Key Cipher Double Encoding Idea c = E(E(m, k 1 ), k 2 ) m = D(D(c, k 2 ), k 1 ) Martin Henz NOI 2009 Solutions to Contest Tasks 53

Some History Breaking a Symmetric Key Cipher Double Encoding Idea c = E(E(m, k 1 ), k 2 ) m = D(D(c, k 2 ), k 1 ) Attacker stituation Eve gets hold of c, m, E and D, and wants to find k 1 and k 2. Martin Henz NOI 2009 Solutions to Contest Tasks 54

Some History Breaking a Symmetric Key Cipher Double Encoding Idea c = E(E(m, k 1 ), k 2 ) m = D(D(c, k 2 ), k 1 ) Attacker stituation Eve gets hold of c, m, E and D, and wants to find k 1 and k 2. Naive strategy Try all possible keys k1 i and kj 2, an compute: c = E(E(m, k1 i ), kj 2 ) Martin Henz NOI 2009 Solutions to Contest Tasks 55

Better Approach XMAS Some History Breaking a Symmetric Key Cipher Idea Work forward and backward, and check if results match Martin Henz NOI 2009 Solutions to Contest Tasks 56

Better Approach XMAS Some History Breaking a Symmetric Key Cipher Idea Work forward and backward, and check if results match Implementation Try all possible keys k1 i x = E(m, k1 i ) and x = D(c, k2 i ) and compute: Martin Henz NOI 2009 Solutions to Contest Tasks 57

Better Approach XMAS Some History Breaking a Symmetric Key Cipher Idea Work forward and backward, and check if results match Implementation Try all possible keys k1 i x = E(m, k1 i ) and x = D(c, k2 i ) and compute: Hash table Use a hash table to remember all x values, and for each x value, look in the hash table for a match. Martin Henz NOI 2009 Solutions to Contest Tasks 58