Suggested Solutions of Sharif Internet Contest 2010 Shayan Ehsani Saeed Seddighin.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Lecture Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Mental Mind Gym coming …. 30 Second Challenge - Advanced Additive.
Du Prel, J; Röhrig, B; Hommel, G; Blettner, M Choosing Statistical TestsPart 12 of a Series on Evaluation of Scientific Publikations Dtsch Arztebl Int.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 13 Recursion. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Recursive void Functions Tracing recursive.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 3.1 Chapter 3.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 6.1 Chapter 6.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
3 Logic The Study of What’s True or False or Somewhere in Between.
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
© S Haughton more than 3?
For(int i = 1; i
NONO. NONO NONO NONO 34 Companies produced one film NONO.
Addition 1’s to 20.
EOC Practice #19 SPI
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input , but it outputs another different number.
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
XII CBSE Previous Year Question Paper QUESTION NO 1 (C) 2 Marks.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Functions Prototypes, parameter passing, return values, activation frams.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Given Connections Solution
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
ORKUT OR GMAIL PASSWORD HACKING. EVEN A CHILD CAN HACK THE ORKUT OR GMAIL ID ORKUT OR GMAIL ID.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
The Basics of Arrays Problem: How can the rancher easily catalog all of his cattle?
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Thermodynamics I Introduction CHAPTER
Sequences 9/18/ :20 PM C201: Linked List.
C++ Arrays.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
BACK SOLUTION:
پروتكل آموزش سلامت به مددجو
إعداد المشرفة التربوية نجلاء الجارد
Conditional Construct
CS150 Introduction to Computer Science 1
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
The Run-Time Stack and Reference Parameters
Reaction time زمن الرجع.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Function Overloading.
Тархи ба оюун \Brain and Mind\
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science

Pointers & Functions.

Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Suggested Solutions of Sharif Internet Contest 2010 Shayan Ehsani Saeed Seddighin

Problem A: Compression! So Simple! Do the first thing that comes to your mind!

Problem A: Compression! cin >> n; while (n>9){ int temp=0; while(n>0){ temp+=n%10; n/=10; } n=temp; } cout << n << endl;

Problem B: Nimper Nim is a Chinese game. Nimm in German means Take. Idea: XOR!

Problem B: Nimper Task : Given a set of numbers, you have to remove minimal sum of numbers to obtain a Lose-state free set. A Lose-state is a subset which the binary-exclusive-or of its numbers is equal to zero. Method : Greedy !!! Solution : Do the following steps as long as your set contains a Loose-state : 1- Find a loose state. 2- Remove its minimum number from set.

Problem C: Jangalestan Task: Find the number of connected components in a grid. Solution: Any search method on the graphs! DFS,BFS,…

Problem C: Jangalestan Trick: int move[8][2]={{0,1},{0,-1}, {1,0},{-1,0}, {1,-1},{1,1}, {-1,1},{-1,-1}};

Problem D: Prime Numbers…Again

How to find prime numbers? vector make_prime(int max) { Not_Prime[2]=false; vector Primes; Primes.clear(); for(int i=2;i<=max;i++){ if (Not_Prime[i]==false){ Primes.push_back(i); for(int j=2*i;j<=maxn;j+=i) Not_Prime[j]=true; } return Primes; }

Problem E: Word Maker Task: You are given a set of polyhedrons and words. Determine how many of the words can be made by juxtaposing these polyhedrons ?

Problem E: Word Maker Idea: Maximum Matching! {a,b,t} {x,y,y} {x,a,b} a b x

Problem E: Word Maker Idea: Maximum Matching! {a,b,t} {x,y,y} {x,a,b} a b x

Problem F: Weird Coloring Task: Find the minimum Edge Roman Domination Function

Problem F: Weird Coloring Idea: This problem is NP-Hard, So the solution is: Branch & Bound! Branches: First assign 2 to the most effective edges. Bounds: There are never two adjacent edge uv and vw such that α(uv)>0 and α(vw)>0.

Problem G :Trundling Object Class : Graph algorithms Idea : BFS (breadth first search) or DFS (depth first search) Solution : Consider the following graph: Each vertex of graph is mapped to a possible situation of cube. How many vertices? At most 3 X N X M. Why? - 3 possible ways to rotate the cube - N X M possible ways to put the cube on the table If we can trundle the cube in order to move from one state to the other, we draw an edge between pertinent vertices of the graph. How many edges? At most 9 X N X M Why? Degree of each vertex is at most 6 so there are at most 6 X 3 X N X M / 2 edges in the graph.

Some vertices of the graph are blocked (Which ones?) Find all vertices that are reachable from the vertex mapped to the initial situation. How? BFS or DFS Now What? Which cells of table have ability to become black? Problem G :Trundling Object

Problem H: Remainder Calculator viewer discretion is advised

How to calculate a! Mod m? Simple: If (a > m) return(0); Otherwise: u=1 for (int i=1;i<=a;i++) u=u*i Mod m; Problem H: Remainder Calculator

First Step: Let simplify the problem: Assume that we want to calculate a!^v mod m. Firstly, calculate u=a! mod m. So, we want to calculate u^v mod m. Notice, that value of u i mod m will be in range [0, m - 1] for any non- negative integer i. Imagine we are iterating i starting from 0. We'll receive following values: u 0 mod m, u 1 mod m, and so on. According to the pigeonhole principle, one of the values will repeat in no more than (m + 1) iterations. Consider the following example. Let u = 2, m = 56. The first 7 iterations are shown in the table :

Problem H: Remainder Calculator i u^i mod m Let P=Size of precycle and L=Size of cycle. For any v to calculate u^v mod m, we should see the column: i= P + ( L + v mod L - P mod L ) mod L Of our table. Thus, our problem will reduce to calculating v mod L.

Consider a_1!^a_2!^…^a_n! as v The task is to find a_0! ^ v Mod m: We can compute a_0! as mentioned so the task is reduced to computation of p^v Mod m. It is known that for every p and m there exist two numbers T and L satisfying the following condition: If v_1 and v_2 are two numbers greater than p and v_1 Mod L = v_2 Mod l then p^v_1 Mod m = p^v_2 Mod m. Problem H: Remainder Calculator

So after finding value of p, L and T: if (a_2!^a_3^…^a_n Is less than T) compute p^(a_2!^a_3^…^a_n) Mod m otherwise solve a_2!^a_3^…^a_n Mod L and compute p^(a_2!^a_3^…^a_n Mod L) Mod m. Problem H: Remainder Calculator

Problem I: Number Convertor Problem I: Number Convertor Task: Find the minimum cost way to convert a to b with given numbers. Idea: Shortest Path Algorithms Solution Put a vertex for each number between 0 and 10 5 For each two numbers x and y if x can be convert to y with one operation, then put a direct edge from x to y with weight of the cost of operation. Now, find the shortest path between a and b