problem
stringlengths
1.25k
8.96k
answer
stringlengths
54
1k
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Field of Wonders</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.</div><h2>Input Format</h2><div>The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.</div><h2>Output Format</h2><div>Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.</div><h2>Example</h2><h3>Input</h3><div>4 a**d 2 abcd acbd</div><h3>Output</h3><div>2</div><h2>Example</h2><h3>Input</h3><div>5 lo*er 2 lover loser</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 a*a 2 aaa aba</div><h3>Output</h3><div>1</div><h2>Note</h2><div>In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.</div></body></html>
```python input() w = input() ans = set("abcdefghijklmnopqrstuvwxyz") for _ in range(int(input())): q = input() if any(a!='*' and a!=b or a=='*' and b in w for a, b in zip(w, q)): continue ans &= set(q)-set(w) print(len(ans))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Field of Wonders</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.</div><h2>Input Format</h2><div>The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.</div><h2>Output Format</h2><div>Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.</div><h2>Example</h2><h3>Input</h3><div>4 a**d 2 abcd acbd</div><h3>Output</h3><div>2</div><h2>Example</h2><h3>Input</h3><div>5 lo*er 2 lover loser</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 a*a 2 aaa aba</div><h3>Output</h3><div>1</div><h2>Note</h2><div>In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.</div></body></html>
```python def is_good_guess(guess, original_string, pos_not_reveal, pos_reveal, reveal): for i in pos_not_reveal: if guess[i] in reveal: return False for i in pos_reveal: if guess[i] != original_string[i]: return False return True n = int(input()) l2 = list(input()) reveal = [] pos_reveal = [] pos_not_reveal = [] for i in range(n): if l2[i] != '*': pos_reveal.append(i) reveal.append(l2[i]) else: pos_not_reveal.append(i) reveal = set(reveal) m = int(input()) option = set('abcdefghijklmnopqrstuvwxyz') for i in range(m): guess = list(input()) guess_char = set(guess) if is_good_guess(guess,l2,pos_not_reveal,pos_reveal,reveal): option = (guess_char - reveal) & option print(len(option))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Field of Wonders</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.</div><h2>Input Format</h2><div>The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.</div><h2>Output Format</h2><div>Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.</div><h2>Example</h2><h3>Input</h3><div>4 a**d 2 abcd acbd</div><h3>Output</h3><div>2</div><h2>Example</h2><h3>Input</h3><div>5 lo*er 2 lover loser</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 a*a 2 aaa aba</div><h3>Output</h3><div>1</div><h2>Note</h2><div>In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.</div></body></html>
```python n, word = int(input()), input() stars_index, lttr_index = [], [] lttrs = set() for i in range(len(word)): if word[i] == '*': stars_index.append(i) else: lttr_index.append(i) lttrs.add(word[i]) m = int(input()) pssbl_lttrs = None for i in range(m): p_word = input() f = False for j in stars_index: if p_word[j] in lttrs: f = True break for j in lttr_index: if p_word[j] != word[j]: f = True break if f: continue if pssbl_lttrs is None: pssbl_lttrs = set(p_word[i] for i in stars_index) else: u = set(p_word[i] for i in stars_index) pssbl_lttrs = pssbl_lttrs.intersection(u) print(len(pssbl_lttrs))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Field of Wonders</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.</div><h2>Input Format</h2><div>The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.</div><h2>Output Format</h2><div>Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.</div><h2>Example</h2><h3>Input</h3><div>4 a**d 2 abcd acbd</div><h3>Output</h3><div>2</div><h2>Example</h2><h3>Input</h3><div>5 lo*er 2 lover loser</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 a*a 2 aaa aba</div><h3>Output</h3><div>1</div><h2>Note</h2><div>In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.</div></body></html>
```python from collections import * input() w, m, c, n = input(), int(input()), Counter(), 0 for _ in range(m): q = input() if any(a!='*' and a!=b or a=='*' and b in w for a, b in zip(w, q)): continue c.update(set(l for i, l in enumerate(q) if w[i]=='*')) n += 1 print(len([l for l, x in c.items() if x==n]))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Field of Wonders</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.</div><h2>Input Format</h2><div>The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.</div><h2>Output Format</h2><div>Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.</div><h2>Example</h2><h3>Input</h3><div>4 a**d 2 abcd acbd</div><h3>Output</h3><div>2</div><h2>Example</h2><h3>Input</h3><div>5 lo*er 2 lover loser</div><h3>Output</h3><div>0</div><h2>Example</h2><h3>Input</h3><div>3 a*a 2 aaa aba</div><h3>Output</h3><div>1</div><h2>Note</h2><div>In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.</div></body></html>
```python n = int(input()) s = input() already = set() for i in s: if i != '*': already.add(i) ans = [chr(ord('a') + i) for i in range(26)] ans = set(ans) for i in range(int(input())): cur = input() can = True clos = set() #should be closed for j in range(n): if s[j] == '*': clos.add(cur[j]) elif s[j] != cur[j]: can = False break if can: if (already & clos) != set(): can = False if can: ans = (ans & clos) print(len(ans))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Lost in Transliteration</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.</div><h2>Input Format</h2><div>The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.</div><h2>Output Format</h2><div>Print the minimal number of groups where the words in each group denote the same name.</div><h2>Example</h2><h3>Input</h3><div>10 mihail oolyana kooooper hoon ulyana koouper mikhail khun kuooper kkkhoon</div><h3>Output</h3><div>4</div><h2>Example</h2><h3>Input</h3><div>9 hariton hkariton buoi kkkhariton boooi bui khariton boui boi</div><h3>Output</h3><div>5</div><h2>Example</h2><h3>Input</h3><div>2 alex alex</div><h3>Output</h3><div>1</div><h2>Note</h2><div>There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.</div></body></html>
```python import math n=int(input()) l=[] for i in range(n): s=input() while("kh" in s): s=s.replace("kh","h") while("u" in s): s=s.replace("u","oo") l.append(s) print(len(set(l)))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Lost in Transliteration</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.</div><h2>Input Format</h2><div>The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.</div><h2>Output Format</h2><div>Print the minimal number of groups where the words in each group denote the same name.</div><h2>Example</h2><h3>Input</h3><div>10 mihail oolyana kooooper hoon ulyana koouper mikhail khun kuooper kkkhoon</div><h3>Output</h3><div>4</div><h2>Example</h2><h3>Input</h3><div>9 hariton hkariton buoi kkkhariton boooi bui khariton boui boi</div><h3>Output</h3><div>5</div><h2>Example</h2><h3>Input</h3><div>2 alex alex</div><h3>Output</h3><div>1</div><h2>Note</h2><div>There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.</div></body></html>
```python n=eval(input()) ans=set() for i in range(n): s=input() # print(s) t=0 while t<=100: s=s.replace("u","oo") s=s.replace("kh","h") t+=1 # print(s) ans.add(s) print(len(ans))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Lost in Transliteration</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.</div><h2>Input Format</h2><div>The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.</div><h2>Output Format</h2><div>Print the minimal number of groups where the words in each group denote the same name.</div><h2>Example</h2><h3>Input</h3><div>10 mihail oolyana kooooper hoon ulyana koouper mikhail khun kuooper kkkhoon</div><h3>Output</h3><div>4</div><h2>Example</h2><h3>Input</h3><div>9 hariton hkariton buoi kkkhariton boooi bui khariton boui boi</div><h3>Output</h3><div>5</div><h2>Example</h2><h3>Input</h3><div>2 alex alex</div><h3>Output</h3><div>1</div><h2>Note</h2><div>There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.</div></body></html>
```python n = int(input()) st = set() for i in range(n): word = input() word = word.replace('u', 'oo') while word.replace('kh', 'h') != word: word = word.replace('kh', 'h') st.add(word) print(len(st))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Lost in Transliteration</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.</div><h2>Input Format</h2><div>The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.</div><h2>Output Format</h2><div>Print the minimal number of groups where the words in each group denote the same name.</div><h2>Example</h2><h3>Input</h3><div>10 mihail oolyana kooooper hoon ulyana koouper mikhail khun kuooper kkkhoon</div><h3>Output</h3><div>4</div><h2>Example</h2><h3>Input</h3><div>9 hariton hkariton buoi kkkhariton boooi bui khariton boui boi</div><h3>Output</h3><div>5</div><h2>Example</h2><h3>Input</h3><div>2 alex alex</div><h3>Output</h3><div>1</div><h2>Note</h2><div>There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.</div></body></html>
```python n = int(input()) words = {} for i in range(n): w = input() while 'kh' in w or 'u' in w: w = w.replace('u','oo').replace('kh','h') try: words[w] += 1 except KeyError: words[w] = 1 print(len(words.keys()))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Lost in Transliteration</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>There are some ambiguities when one writes Berland names with the letters of the Latin alphabet. For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name. The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name. There are n users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account? Formally, we assume that two words denote the same name, if using the replacements "u" $$\text{The formula used to produce the text is not provided in the image.}$$ "oo" and "h" $$\text{The formula used to produce the text is not provided in the image.}$$ "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements. For example, the following pairs of words denote the same name: - "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" $$\rightarrow$$ "kuuper" and "kuooper" $$\rightarrow$$ "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" $$\rightarrow$$ "khoon" and "kkkhoon" $$\rightarrow$$ "kkhoon" $$\rightarrow$$ "khoon". For a given list of words, find the minimal number of groups where the words in each group denote the same name.</div><h2>Input Format</h2><div>The first line contains integer number n (2 ≤ n ≤ 400) — number of the words in the list. The following n lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive.</div><h2>Output Format</h2><div>Print the minimal number of groups where the words in each group denote the same name.</div><h2>Example</h2><h3>Input</h3><div>10 mihail oolyana kooooper hoon ulyana koouper mikhail khun kuooper kkkhoon</div><h3>Output</h3><div>4</div><h2>Example</h2><h3>Input</h3><div>9 hariton hkariton buoi kkkhariton boooi bui khariton boui boi</div><h3>Output</h3><div>5</div><h2>Example</h2><h3>Input</h3><div>2 alex alex</div><h3>Output</h3><div>1</div><h2>Note</h2><div>There are four groups of words in the first example. Words in each group denote same name: 1. "mihail", "mikhail" 2. "oolyana", "ulyana" 3. "kooooper", "koouper" 4. "hoon", "khun", "kkkhoon" There are five groups of words in the second example. Words in each group denote same name: 1. "hariton", "kkkhariton", "khariton" 2. "hkariton" 3. "buoi", "boooi", "boui" 4. "bui" 5. "boi" In the third example the words are equal, so they denote the same name.</div></body></html>
```python #!/usr/bin/env python3 names = int(input()) def simplify_name(s): s_orig = s[:] s = s.replace('u', 'oo') s = s.replace('kh', 'h') if s == s_orig: return s else: return simplify_name(s) name_set = set() for idx in range(names): name_set.add(simplify_name(input())) print(len(name_set))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Road Widening</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi. For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan.</div><h2>Input Format</h2><div>The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.</div><h2>Output Format</h2><div>In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.</div><h2>Example</h2><h3>Input</h3><div>3 4 5 4 5 4 10</div><h3>Output</h3><div>16 9 9 10</div><h2>Example</h2><h3>Input</h3><div>4 1 100 100 1 1 100 100 1</div><h3>Output</h3><div>202 101 101 101 101</div><h2>Example</h2><h3>Input</h3><div>3 1 1 100 100 1 1</div><h3>Output</h3><div>-1</div></body></html>
```python n = int(input()) ss = [0] * (n + 1) gg = [0] * (n + 1) #mins = [0] * n maxs = [0] * n curMin = -10 ** 10 curMax = -curMin for i in range(n): s, g = map(int, input().split(' ')) ss[i] = s gg[i] = g curMin = max(curMin - 1, s) curMax = min(curMax + 1, s + g) if curMin > curMax: print(-1) exit(0) #mins[i] = curMin maxs[i] = curMax res = [0] * n cur = 10 ** 10 for i in range(n - 1, -1, -1): res[i] = cur = min(cur + 1, maxs[i]) print(sum(res) - sum(ss)) print(*res)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Road Widening</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi. For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan.</div><h2>Input Format</h2><div>The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.</div><h2>Output Format</h2><div>In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.</div><h2>Example</h2><h3>Input</h3><div>3 4 5 4 5 4 10</div><h3>Output</h3><div>16 9 9 10</div><h2>Example</h2><h3>Input</h3><div>4 1 100 100 1 1 100 100 1</div><h3>Output</h3><div>202 101 101 101 101</div><h2>Example</h2><h3>Input</h3><div>3 1 1 100 100 1 1</div><h3>Output</h3><div>-1</div></body></html>
```python n = int(input()) s = [0]*n g = [0]*n for i in range(n): a,b = map(int,input().split()) s[i] = a g[i] = a+b for i in range(1,n): g[i] = min(g[i],g[i-1]+1) for i in range(n-2,-1,-1): g[i] = min(g[i],g[i+1]+1) ans = 0 for i in range(n): if s[i] <= g[i]: ans += g[i] - s[i] else: print(-1) break else: print(ans) print(*g)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Road Widening</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi. For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan.</div><h2>Input Format</h2><div>The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.</div><h2>Output Format</h2><div>In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.</div><h2>Example</h2><h3>Input</h3><div>3 4 5 4 5 4 10</div><h3>Output</h3><div>16 9 9 10</div><h2>Example</h2><h3>Input</h3><div>4 1 100 100 1 1 100 100 1</div><h3>Output</h3><div>202 101 101 101 101</div><h2>Example</h2><h3>Input</h3><div>3 1 1 100 100 1 1</div><h3>Output</h3><div>-1</div></body></html>
```python n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) a[i][1]+=a[i][0] for i in range(1,n): if a[i][1]>a[i-1][1]+1: a[i][1]=a[i-1][1]+1 if a[i][1]<a[i][0]:exit(print(-1)) s=a[-1][1]-a[-1][0] for i in range(n-2,-1,-1): if a[i][1] > a[i + 1][1] + 1: a[i][1] = a[i + 1][1] + 1 if a[i][1] < a[i][0]: exit(print(-1)) s+=a[i][1]-a[i][0] print(s) for i in a: print(i[1],end=' ')```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Quadcopter Competition</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is situated (x2, y2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x1, y1) and containing the point (x2, y2) strictly inside. The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path?</div><h2>Input Format</h2><div>The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.</div><h2>Output Format</h2><div>Print the length of minimal path of the quadcopter to surround the flag and return back.</div><h2>Example</h2><h3>Input</h3><div>1 5 5 2</div><h3>Output</h3><div>18</div><h2>Example</h2><h3>Input</h3><div>0 1 0 0</div><h3>Output</h3><div>8</div></body></html>
```python I=lambda:map(int,input().split()) a,b=I() c,d=I() print((max(1,abs(a-c))+max(1,abs(b-d))<<1)+4)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Quadcopter Competition</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is situated (x2, y2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x1, y1) and containing the point (x2, y2) strictly inside. The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path?</div><h2>Input Format</h2><div>The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.</div><h2>Output Format</h2><div>Print the length of minimal path of the quadcopter to surround the flag and return back.</div><h2>Example</h2><h3>Input</h3><div>1 5 5 2</div><h3>Output</h3><div>18</div><h2>Example</h2><h3>Input</h3><div>0 1 0 0</div><h3>Output</h3><div>8</div></body></html>
```python c_x,c_y = map(int,input().split()) f_x,f_y = map(int,input().split()) if c_x == f_x: print(2*(abs(c_y-f_y)+1)+4) elif c_y == f_y: print(2*(abs(c_x-f_x)+1)+4) else: p_x = f_x+1 if c_x < f_x else f_x-1 p_y = f_y+1 if c_y < f_y else f_y-1 print(2*(abs(p_x-c_x)+abs(p_y-c_y)))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Quadcopter Competition</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is situated (x2, y2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x1, y1) and containing the point (x2, y2) strictly inside. The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path?</div><h2>Input Format</h2><div>The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.</div><h2>Output Format</h2><div>Print the length of minimal path of the quadcopter to surround the flag and return back.</div><h2>Example</h2><h3>Input</h3><div>1 5 5 2</div><h3>Output</h3><div>18</div><h2>Example</h2><h3>Input</h3><div>0 1 0 0</div><h3>Output</h3><div>8</div></body></html>
```python x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) a, b = abs(x2-x1), abs(y2-y1) if a == 0: print(4 + 2 * (b + 1)) elif b == 0: print(2 * (a + 1) + 4) else: print(2*(a+1)+2*(b+1))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Quadcopter Competition</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is situated (x2, y2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x1, y1) and containing the point (x2, y2) strictly inside. The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path?</div><h2>Input Format</h2><div>The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.</div><h2>Output Format</h2><div>Print the length of minimal path of the quadcopter to surround the flag and return back.</div><h2>Example</h2><h3>Input</h3><div>1 5 5 2</div><h3>Output</h3><div>18</div><h2>Example</h2><h3>Input</h3><div>0 1 0 0</div><h3>Output</h3><div>8</div></body></html>
```python import sys input = sys.stdin.readline x, y = map(int, input().split()) z, w = map(int, input().split()) print(2*(max(1, abs(z-x)) + max(1, abs(w-y))) + 4)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Quadcopter Competition</h1><div>Time limit per test: 3.0 s</div><h2>Description</h2><div>Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (x1, y1) and the coordinates of the point where the flag is situated (x2, y2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1). Thus the quadcopter path is a closed cycle starting and finishing in (x1, y1) and containing the point (x2, y2) strictly inside. The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). What is the minimal length of the quadcopter path?</div><h2>Input Format</h2><div>The first line contains two integer numbers x1 and y1 ( - 100 ≤ x1, y1 ≤ 100) — coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers x2 and y2 ( - 100 ≤ x2, y2 ≤ 100) — coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.</div><h2>Output Format</h2><div>Print the length of minimal path of the quadcopter to surround the flag and return back.</div><h2>Example</h2><h3>Input</h3><div>1 5 5 2</div><h3>Output</h3><div>18</div><h2>Example</h2><h3>Input</h3><div>0 1 0 0</div><h3>Output</h3><div>8</div></body></html>
```python x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) if x1 == x2 or y1 == y2: z = abs(x1 - x2) + abs(y1 - y2) - 1 print(z * 2 + 8) else: mas = [[x2 - 1, y2 - 1], [x2 + 1, y2 - 1], [x2 - 1, y2 + 1], [x2 + 1, y2 + 1]] mx = 0 for i in mas: a = i[0] b = i[1] #print(a, b) mx = max(mx, abs(x1 - a) + abs(y1 - b)) print(mx * 2)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>New Year and Original Order</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Let S(n) denote the number that represents the digits of n in sorted order. For example, S(1) = 1, S(5) = 5, S(50394) = 3459, S(353535) = 333555. Given a number X, compute $$\sum_{1 \leq k \leq X} S(k)$$ modulo 109 + 7.</div><h2>Input Format</h2><div>The first line of input will contain the integer X (1 ≤ X ≤ 10700).</div><h2>Output Format</h2><div>Print a single integer, the answer to the question.</div><h2>Example</h2><h3>Input</h3><div>21</div><h3>Output</h3><div>195</div><h2>Example</h2><h3>Input</h3><div>345342</div><h3>Output</h3><div>390548434</div><h2>Note</h2><div>The first few values of S are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 12. The sum of these values is 195.</div><h2>Editorial</h2><div>This is a digit dp problem. Let's try to solve the subproblem "How many ways can the i-th digit be at least j?". Let's fix j, and solve this with dp. We have a dp state dp[a][b][c] = number of ways given we've considered the first a digits of X, we need b more occurrences of digits at least j, and c is a boolean saying whether or not we are strictly less than X or not yet. For a fixed digit, we can compute this dp table in O(n2) time, and then compute the answers to our subproblem for each i (i.e. by varying b in our table).</div></body></html>
```python # Problem G num = input() num_list = [] for i in range(len(num)): num_list.append(int(num[i])) myMod = (10 ** 9) + 7 length = len(num_list) f = [0] * (length + 1) t = [1] * (length + 1) for i in range(length): f[i+1] = (f[i] * 10 + 1) % myMod t[i+1] = (t[i] * 10) % myMod ans = 0 for i in range(1, 10): dp = [0] * (length + 1) for j in range(length): dp[j+1] = (dp[j] * i + (10 - i) * (dp[j] * 10 + t[j])) % myMod c = 0 ctr = 0 for k in num_list: z = min(i, k) o = k - z ans += o * (dp[length-1-ctr] * t[c+1] + f[c+1] * t[length-1-ctr]) % myMod ans += z * (dp[length-1-ctr] * t[c] + f[c] * t[length-1-ctr]) % myMod ans %= myMod c += k >= i ctr += 1 ans += f[c] if ans >= myMod: ans -= myMod print(ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Finite or not?</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction. A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.</div><h2>Output Format</h2><div>For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.</div><h2>Example</h2><h3>Input</h3><div>2 6 12 10 4 3 10</div><h3>Output</h3><div>Finite Infinite</div><h2>Example</h2><h3>Input</h3><div>4 1 1 2 9 36 2 4 12 3 3 5 4</div><h3>Output</h3><div>Finite Finite Finite Infinite</div><h2>Note</h2><div>$$$\frac{6}{12} = \frac{1}{2} = 0,5_{10}$$$ $$$\frac{4}{3} = 1,(3)_{10}$$$ $$$\frac{9}{36} = \frac{1}{4} = 0,01_2$$$ $$$\frac{4}{12} = \frac{1}{3} = 0,1_3$$$</div><h2>Editorial</h2><div>First, if $$$p$$$ and $$$q$$$ are not coprime, divide them on $$$\gcd(p,q)$$$. Fraction is finite if and only if there is integer $$$k$$$ such that $$$q \mid p \cdot b^k$$$. Since $$$p$$$ and $$$q$$$ are being coprime now, $$$q \mid b^k \Rightarrow$$$ all prime factors of $$$q$$$ are prime factors of $$$b$$$. Now we can do iterations $$$q = q \div \gcd(b,q)$$$ while $$$\gcd(q,b) \ne 1$$$. If $$$q \ne 1$$$ after iterations, there are prime factors of $$$q$$$ which are not prime factors of $$$b \Rightarrow$$$ fraction is Infinite, else fraction is Finite. But this solution works in $$$O(nlog^210^{18})$$$. Let's add $$$b=\gcd(b,q)$$$ in iterations and name iterations when $$$\gcd(b,q)$$$ changes iterations of the first type and when it doesn't change — iterations of the second type. Iterations of second type works summary in $$$O(\log10^{18})$$$. Number of iterations of the first type is $$$O(\log10^{18})$$$ too but on each iteration $$$b$$$ decreases twice. Note that number of iterations in Euclid's algorithm is equal to number of this decreases. So iterations of first type works in $$$O(\log10^{18})$$$ summary. Total time complexity is $$$O(n\log10^{18})$$$ Solution</div></body></html>
```python from sys import stdin _, *l = stdin.read().splitlines() for i, s in enumerate(l): p, q, b = map(int, s.split()) l[i] = 'Infinite' if p * pow(b, 64, q) % q else 'Finite' print('\n'.join(l))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Finite or not?</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction. A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.</div><h2>Output Format</h2><div>For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.</div><h2>Example</h2><h3>Input</h3><div>2 6 12 10 4 3 10</div><h3>Output</h3><div>Finite Infinite</div><h2>Example</h2><h3>Input</h3><div>4 1 1 2 9 36 2 4 12 3 3 5 4</div><h3>Output</h3><div>Finite Finite Finite Infinite</div><h2>Note</h2><div>$$$\frac{6}{12} = \frac{1}{2} = 0,5_{10}$$$ $$$\frac{4}{3} = 1,(3)_{10}$$$ $$$\frac{9}{36} = \frac{1}{4} = 0,01_2$$$ $$$\frac{4}{12} = \frac{1}{3} = 0,1_3$$$</div><h2>Editorial</h2><div>First, if $$$p$$$ and $$$q$$$ are not coprime, divide them on $$$\gcd(p,q)$$$. Fraction is finite if and only if there is integer $$$k$$$ such that $$$q \mid p \cdot b^k$$$. Since $$$p$$$ and $$$q$$$ are being coprime now, $$$q \mid b^k \Rightarrow$$$ all prime factors of $$$q$$$ are prime factors of $$$b$$$. Now we can do iterations $$$q = q \div \gcd(b,q)$$$ while $$$\gcd(q,b) \ne 1$$$. If $$$q \ne 1$$$ after iterations, there are prime factors of $$$q$$$ which are not prime factors of $$$b \Rightarrow$$$ fraction is Infinite, else fraction is Finite. But this solution works in $$$O(nlog^210^{18})$$$. Let's add $$$b=\gcd(b,q)$$$ in iterations and name iterations when $$$\gcd(b,q)$$$ changes iterations of the first type and when it doesn't change — iterations of the second type. Iterations of second type works summary in $$$O(\log10^{18})$$$. Number of iterations of the first type is $$$O(\log10^{18})$$$ too but on each iteration $$$b$$$ decreases twice. Note that number of iterations in Euclid's algorithm is equal to number of this decreases. So iterations of first type works in $$$O(\log10^{18})$$$ summary. Total time complexity is $$$O(n\log10^{18})$$$ Solution</div></body></html>
```python # python3 import sys from math import gcd import sys sys.set_int_max_str_digits(0) def is_finite(p, q, b): return not p * pow(b, 64, q) % q input() print("\n".join("Finite" if is_finite(*map(int, line.split())) else "Infinite" for line in sys.stdin.readlines()))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Finite or not?</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction. A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.</div><h2>Output Format</h2><div>For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.</div><h2>Example</h2><h3>Input</h3><div>2 6 12 10 4 3 10</div><h3>Output</h3><div>Finite Infinite</div><h2>Example</h2><h3>Input</h3><div>4 1 1 2 9 36 2 4 12 3 3 5 4</div><h3>Output</h3><div>Finite Finite Finite Infinite</div><h2>Note</h2><div>$$$\frac{6}{12} = \frac{1}{2} = 0,5_{10}$$$ $$$\frac{4}{3} = 1,(3)_{10}$$$ $$$\frac{9}{36} = \frac{1}{4} = 0,01_2$$$ $$$\frac{4}{12} = \frac{1}{3} = 0,1_3$$$</div><h2>Editorial</h2><div>First, if $$$p$$$ and $$$q$$$ are not coprime, divide them on $$$\gcd(p,q)$$$. Fraction is finite if and only if there is integer $$$k$$$ such that $$$q \mid p \cdot b^k$$$. Since $$$p$$$ and $$$q$$$ are being coprime now, $$$q \mid b^k \Rightarrow$$$ all prime factors of $$$q$$$ are prime factors of $$$b$$$. Now we can do iterations $$$q = q \div \gcd(b,q)$$$ while $$$\gcd(q,b) \ne 1$$$. If $$$q \ne 1$$$ after iterations, there are prime factors of $$$q$$$ which are not prime factors of $$$b \Rightarrow$$$ fraction is Infinite, else fraction is Finite. But this solution works in $$$O(nlog^210^{18})$$$. Let's add $$$b=\gcd(b,q)$$$ in iterations and name iterations when $$$\gcd(b,q)$$$ changes iterations of the first type and when it doesn't change — iterations of the second type. Iterations of second type works summary in $$$O(\log10^{18})$$$. Number of iterations of the first type is $$$O(\log10^{18})$$$ too but on each iteration $$$b$$$ decreases twice. Note that number of iterations in Euclid's algorithm is equal to number of this decreases. So iterations of first type works in $$$O(\log10^{18})$$$ summary. Total time complexity is $$$O(n\log10^{18})$$$ Solution</div></body></html>
```python input() print('\n'.join(['Infinite' if p * pow(b, 99, q) % q else 'Finite' for p, q, b in map(lambda l: map(int, l.split()), __import__('sys').stdin.readlines())]))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Finite or not?</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction. A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.</div><h2>Output Format</h2><div>For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.</div><h2>Example</h2><h3>Input</h3><div>2 6 12 10 4 3 10</div><h3>Output</h3><div>Finite Infinite</div><h2>Example</h2><h3>Input</h3><div>4 1 1 2 9 36 2 4 12 3 3 5 4</div><h3>Output</h3><div>Finite Finite Finite Infinite</div><h2>Note</h2><div>$$$\frac{6}{12} = \frac{1}{2} = 0,5_{10}$$$ $$$\frac{4}{3} = 1,(3)_{10}$$$ $$$\frac{9}{36} = \frac{1}{4} = 0,01_2$$$ $$$\frac{4}{12} = \frac{1}{3} = 0,1_3$$$</div><h2>Editorial</h2><div>First, if $$$p$$$ and $$$q$$$ are not coprime, divide them on $$$\gcd(p,q)$$$. Fraction is finite if and only if there is integer $$$k$$$ such that $$$q \mid p \cdot b^k$$$. Since $$$p$$$ and $$$q$$$ are being coprime now, $$$q \mid b^k \Rightarrow$$$ all prime factors of $$$q$$$ are prime factors of $$$b$$$. Now we can do iterations $$$q = q \div \gcd(b,q)$$$ while $$$\gcd(q,b) \ne 1$$$. If $$$q \ne 1$$$ after iterations, there are prime factors of $$$q$$$ which are not prime factors of $$$b \Rightarrow$$$ fraction is Infinite, else fraction is Finite. But this solution works in $$$O(nlog^210^{18})$$$. Let's add $$$b=\gcd(b,q)$$$ in iterations and name iterations when $$$\gcd(b,q)$$$ changes iterations of the first type and when it doesn't change — iterations of the second type. Iterations of second type works summary in $$$O(\log10^{18})$$$. Number of iterations of the first type is $$$O(\log10^{18})$$$ too but on each iteration $$$b$$$ decreases twice. Note that number of iterations in Euclid's algorithm is equal to number of this decreases. So iterations of first type works in $$$O(\log10^{18})$$$ summary. Total time complexity is $$$O(n\log10^{18})$$$ Solution</div></body></html>
```python input() print('\n'.join(map(lambda x: (lambda p, q, b: 'Infinite' if p * pow(b, 60, q) % q else 'Finite')(*x), map(lambda l:map(int, l.split()), __import__('sys').stdin.readlines()))))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Finite or not?</h1><div>Time limit per test: 1.0 s</div><h2>Description</h2><div>You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction. A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.</div><h2>Input Format</h2><div>The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.</div><h2>Output Format</h2><div>For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.</div><h2>Example</h2><h3>Input</h3><div>2 6 12 10 4 3 10</div><h3>Output</h3><div>Finite Infinite</div><h2>Example</h2><h3>Input</h3><div>4 1 1 2 9 36 2 4 12 3 3 5 4</div><h3>Output</h3><div>Finite Finite Finite Infinite</div><h2>Note</h2><div>$$$\frac{6}{12} = \frac{1}{2} = 0,5_{10}$$$ $$$\frac{4}{3} = 1,(3)_{10}$$$ $$$\frac{9}{36} = \frac{1}{4} = 0,01_2$$$ $$$\frac{4}{12} = \frac{1}{3} = 0,1_3$$$</div><h2>Editorial</h2><div>First, if $$$p$$$ and $$$q$$$ are not coprime, divide them on $$$\gcd(p,q)$$$. Fraction is finite if and only if there is integer $$$k$$$ such that $$$q \mid p \cdot b^k$$$. Since $$$p$$$ and $$$q$$$ are being coprime now, $$$q \mid b^k \Rightarrow$$$ all prime factors of $$$q$$$ are prime factors of $$$b$$$. Now we can do iterations $$$q = q \div \gcd(b,q)$$$ while $$$\gcd(q,b) \ne 1$$$. If $$$q \ne 1$$$ after iterations, there are prime factors of $$$q$$$ which are not prime factors of $$$b \Rightarrow$$$ fraction is Infinite, else fraction is Finite. But this solution works in $$$O(nlog^210^{18})$$$. Let's add $$$b=\gcd(b,q)$$$ in iterations and name iterations when $$$\gcd(b,q)$$$ changes iterations of the first type and when it doesn't change — iterations of the second type. Iterations of second type works summary in $$$O(\log10^{18})$$$. Number of iterations of the first type is $$$O(\log10^{18})$$$ too but on each iteration $$$b$$$ decreases twice. Note that number of iterations in Euclid's algorithm is equal to number of this decreases. So iterations of first type works in $$$O(\log10^{18})$$$ summary. Total time complexity is $$$O(n\log10^{18})$$$ Solution</div></body></html>
```python n = int(input()) ans = '' while n > 0: p, q, b = map(int, input().split(' ')) for i in range(6): b = b * b % q if b * p % q == 0: ans += 'Finite\n' else: ans += 'Infinite\n' n -= 1 print (ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Perfect Encoding</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>You are working as an analyst in a company working on a new system for big data storage. This system will store $$$n$$$ different objects. Each object should have a unique ID. To create the system, you choose the parameters of the system — integers $$$m \ge 1$$$ and $$$b_{1}, b_{2}, \ldots, b_{m}$$$. With these parameters an ID of some object in the system is an array of integers $$$[a_{1}, a_{2}, \ldots, a_{m}]$$$ where $$$1 \le a_{i} \le b_{i}$$$ holds for every $$$1 \le i \le m$$$. Developers say that production costs are proportional to $$$\sum_{i=1}^{m} b_{i}$$$. You are asked to choose parameters $$$m$$$ and $$$b_{i}$$$ so that the system will be able to assign unique IDs to $$$n$$$ different objects and production costs are minimized. Note that you don't have to use all available IDs.</div><h2>Input Format</h2><div>In the only line of input there is one positive integer $$$n$$$. The length of the decimal representation of $$$n$$$ is no greater than $$$1.5 \cdot 10^{6}$$$. The integer does not contain leading zeros.</div><h2>Output Format</h2><div>Print one number — minimal value of $$$\sum_{i=1}^{m} b_{i}$$$.</div><h2>Example</h2><h3>Input</h3><div>36</div><h3>Output</h3><div>10</div><h2>Example</h2><h3>Input</h3><div>37</div><h3>Output</h3><div>11</div><h2>Example</h2><h3>Input</h3><div>12345678901234567890123456789</div><h3>Output</h3><div>177</div><h2>Editorial</h2><div>The problem asks to find integers $$$b_i$$$ such that $$$\prod b_i \ge n$$$ and $$$\sum b_i$$$ is minimized. Let's suppose that in optimal solution there is $$$x \ge 4$$$ among $$$b_i$$$. It is better to split it to $$$2$$$ and $$$(x-2)$$$: the sum remains the same and the product is increased (or stays the same). So we will use only $$$2$$$ and $$$3$$$ as our $$$b_i$$$. If there are at least three $$$2$$$ among $$$b_i$$$, we can replace them with two $$$3$$$: the sum remains the same, the product is increased. So, optimal solution looks like this: zero, one or two $$$2$$$s and some $$$3$$$s. For now let's say that we try all three possibilities for the number of $$$2$$$s. The problem now looks like "find $$$\left\lceil \log_{3} n \right\rceil$$$". The trick here is that we can estimate the answer very accurately. Let's say that the length of decimal form of $$$n$$$ is $$$L$$$. Then $$$\log_{3} n$$$ is very close to $$$L \frac{\log 10}{\log 3}$$$, the difference is not greater than $$$3$$$. So it is easy to calculate the number $$$p$$$ such that $$$3^p < n / 4 < n < 3^{p+6}$$$. If we will calculate $$$3^p$$$, then we should adjust this a little bit by multiplying by $$$3$$$ a few number of times. Multiplying by $$$3$$$ can be done in linear time, comparing two numbers also in linear time. Let's now remember that we have tried all the possibilities for the number of $$$2$$$s. We will do it not beforehand, but only now, because now each option can be checked in linear time. To calculate $$$3^p$$$ we will use binary exponentiation with FFT. If the length of the result is $$$L$$$, then the running time will be $$$O(L \log L + \frac{L}{2} \log L + \frac{L}{4} \log L + \ldots) = O(L \log L)$$$. To reduce the running time you should store the numbers in base $$$1000$$$, not in base $$$10$$$. This will reduce the length of the number $$$3$$$ times, and the numbers we are getting in FFT will be at most $$$5 \cdot 10^{11}$$$ which is good enough to avoid precision issues. In the end, running time is roughly equivalent to 4 FFT calls of size $$$2^{19}$$$ which is not that big. Total complexity — $$$O(L \log L)$$$.</div></body></html>
```python import decimal from math import ceil, floor, inf, log n = input() if n=='1': print(1) exit() decimal.getcontext().prec = len(n)+6 decimal.getcontext().Emax = len(n)+6 log3 = log(10)*(len(n)-1)/log(3) pref = n if len(pref)>20: pref = pref[:20] pref = pref[0] + '.' + pref[1:] log3 += log(float(pref))/log(3) log3+=1e-8 full = max(0, floor(log3)) small=0 if full>=1 and log3-full<=log(2)/log(3)*2-1: small=2 full-=1 elif log3-full<=log(2)/log(3): small = 1 else: full+=1 n = decimal.Decimal(n) ans = full*3 + small*2 def check(f,s): global ans res = decimal.Decimal(3)**f * (2**s) if res>=n: ans = min(ans,f*3+s*2) if small==0 and full>=1: full-=1 small+=1 check(full,small) elif small==1 and full>=1: full-=1 small+=1 check(full,small) elif small==2: small-=2 full+=1 check(full,small) print(ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Perfect Encoding</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>You are working as an analyst in a company working on a new system for big data storage. This system will store $$$n$$$ different objects. Each object should have a unique ID. To create the system, you choose the parameters of the system — integers $$$m \ge 1$$$ and $$$b_{1}, b_{2}, \ldots, b_{m}$$$. With these parameters an ID of some object in the system is an array of integers $$$[a_{1}, a_{2}, \ldots, a_{m}]$$$ where $$$1 \le a_{i} \le b_{i}$$$ holds for every $$$1 \le i \le m$$$. Developers say that production costs are proportional to $$$\sum_{i=1}^{m} b_{i}$$$. You are asked to choose parameters $$$m$$$ and $$$b_{i}$$$ so that the system will be able to assign unique IDs to $$$n$$$ different objects and production costs are minimized. Note that you don't have to use all available IDs.</div><h2>Input Format</h2><div>In the only line of input there is one positive integer $$$n$$$. The length of the decimal representation of $$$n$$$ is no greater than $$$1.5 \cdot 10^{6}$$$. The integer does not contain leading zeros.</div><h2>Output Format</h2><div>Print one number — minimal value of $$$\sum_{i=1}^{m} b_{i}$$$.</div><h2>Example</h2><h3>Input</h3><div>36</div><h3>Output</h3><div>10</div><h2>Example</h2><h3>Input</h3><div>37</div><h3>Output</h3><div>11</div><h2>Example</h2><h3>Input</h3><div>12345678901234567890123456789</div><h3>Output</h3><div>177</div><h2>Editorial</h2><div>The problem asks to find integers $$$b_i$$$ such that $$$\prod b_i \ge n$$$ and $$$\sum b_i$$$ is minimized. Let's suppose that in optimal solution there is $$$x \ge 4$$$ among $$$b_i$$$. It is better to split it to $$$2$$$ and $$$(x-2)$$$: the sum remains the same and the product is increased (or stays the same). So we will use only $$$2$$$ and $$$3$$$ as our $$$b_i$$$. If there are at least three $$$2$$$ among $$$b_i$$$, we can replace them with two $$$3$$$: the sum remains the same, the product is increased. So, optimal solution looks like this: zero, one or two $$$2$$$s and some $$$3$$$s. For now let's say that we try all three possibilities for the number of $$$2$$$s. The problem now looks like "find $$$\left\lceil \log_{3} n \right\rceil$$$". The trick here is that we can estimate the answer very accurately. Let's say that the length of decimal form of $$$n$$$ is $$$L$$$. Then $$$\log_{3} n$$$ is very close to $$$L \frac{\log 10}{\log 3}$$$, the difference is not greater than $$$3$$$. So it is easy to calculate the number $$$p$$$ such that $$$3^p < n / 4 < n < 3^{p+6}$$$. If we will calculate $$$3^p$$$, then we should adjust this a little bit by multiplying by $$$3$$$ a few number of times. Multiplying by $$$3$$$ can be done in linear time, comparing two numbers also in linear time. Let's now remember that we have tried all the possibilities for the number of $$$2$$$s. We will do it not beforehand, but only now, because now each option can be checked in linear time. To calculate $$$3^p$$$ we will use binary exponentiation with FFT. If the length of the result is $$$L$$$, then the running time will be $$$O(L \log L + \frac{L}{2} \log L + \frac{L}{4} \log L + \ldots) = O(L \log L)$$$. To reduce the running time you should store the numbers in base $$$1000$$$, not in base $$$10$$$. This will reduce the length of the number $$$3$$$ times, and the numbers we are getting in FFT will be at most $$$5 \cdot 10^{11}$$$ which is good enough to avoid precision issues. In the end, running time is roughly equivalent to 4 FFT calls of size $$$2^{19}$$$ which is not that big. Total complexity — $$$O(L \log L)$$$.</div></body></html>
```python from decimal import * from math import * getcontext().Emax = 1500010 getcontext().prec = 1500010 a = Decimal(input()) mi = int(log(float(a.scaleb(-a.logb())),3) + log(10,3) * (float(a.logb()) - 1) - 1) an = mi * 3 A = Decimal(3) ** mi while(A < a): A *= 3 an += 3 ans = an for i in range(2): A = A * 2 / 3 an -= 1 if(A > a): A /= 3 an -= 3 while(A < a): A *= 3 an += 3 if(an < ans): ans = an print(ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Nastya and a Game</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $$\frac{p}{s} = k$$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.</div><h2>Input Format</h2><div>The first line contains two integers n and k (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 105), where n is the length of the array and k is the constant described above. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 108) — the elements of the array.</div><h2>Output Format</h2><div>In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.</div><h2>Example</h2><h3>Input</h3><div>1 1 1</div><h3>Output</h3><div>1</div><h2>Example</h2><h3>Input</h3><div>4 2 6 3 8 1</div><h3>Output</h3><div>2</div><h2>Note</h2><div>In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $${ \frac { 1 } { 1 } } = 1$$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $$\frac{18}{9}=2$$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $$\frac{24}{12}=2$$.</div><h2>Editorial</h2><div>Let's call numbers which are more than 1 as good. Notice the following fact: If a subsegment is suitable, it contains not more than 60 good numbers. Indeed, let's assume that a subsegment contains more than 60 good numbers. In this subsegment $$p \geq 2^{61} > 2 \cdot 10^{18}$$. At the same time, as $$k \leq 10^5$$, and $$s \leq n \cdot 10^{8} = 2 \cdot 10^{13}$$, there is $$s \cdot k \leq 2 \cdot 10^{18}$$. Therefore, this subsegment can't be suitable due to $$s \cdot k < p$$. Let's keep all positions of good numbers in a sorted array. We sort out possible left border of a subsegment and then with binary search we find the next good number to the right of this left border. Then let's iterate from this found number to the right by the good numbers(that is we sort out the rightmost good number in a subsegment), until product of all numbers in the subsegment becomes more than $$2 \cdot 10^{18}$$ (it's flag which shows us, that product is too big for a suitable subsgment and we need to finish to iterate). We have shown above the number of iterations isn't more than 60. Now for sorted out the left border and the rightmost good number we only need to know the number of 1's which needs to be added to the right of the rightmost good number, as we can easily maintain sum and product in the subsegment during iterating. Then we need to check whether found number of 1's exists to the right of the rightmost good number. It can be checked if we look at the next good number's position. Complexity is $${\mathcal {O}}(n\cdot \log(10^{18}))$$. In order to check that $$a \cdot b$$ is more than $$2 \cdot 10^{18}$$, you shouldn't calculate $$\alpha$$ multiply by $$\beta$$, due to overflow. You must only check that $${ \frac { 2 \cdot 1 0 ^ { 1 8 } } { a } } \geq b$$.</div></body></html>
```python n,k=map(int, input().split()) v=list(map(int, input().split())) pos, pref=[-1], [] ans=0 for i in range(n): if v[i]!=1: pos.append(i) if v[i]==1 and k==1: ans+=1 if i: pref.append(pref[-1]+v[i]) else: pref.append(v[i]) pos.append(n) m=len(pos) #print("m",m) inf=int(2e18+10) for i in range(1,m-1): p=1 for j in range(i, m-1): p=p*v[pos[j]] if p>inf: break s = pref[pos[j]]; if pos[i]: s-=pref[pos[i]-1] if s*k==p: ans+=1 d=p-s*k if d>0 and d%k==0: w= d//k f=min(pos[i]-pos[i-1]-1,w) b=min(pos[j+1]-pos[j]-1, w) if f+b<w: continue ans+=f+b-w+1 #print(v[pos[i]], v[pos[j]]) print(ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Nastya and a Game</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $$\frac{p}{s} = k$$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.</div><h2>Input Format</h2><div>The first line contains two integers n and k (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 105), where n is the length of the array and k is the constant described above. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 108) — the elements of the array.</div><h2>Output Format</h2><div>In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.</div><h2>Example</h2><h3>Input</h3><div>1 1 1</div><h3>Output</h3><div>1</div><h2>Example</h2><h3>Input</h3><div>4 2 6 3 8 1</div><h3>Output</h3><div>2</div><h2>Note</h2><div>In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $${ \frac { 1 } { 1 } } = 1$$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $$\frac{18}{9}=2$$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $$\frac{24}{12}=2$$.</div><h2>Editorial</h2><div>Let's call numbers which are more than 1 as good. Notice the following fact: If a subsegment is suitable, it contains not more than 60 good numbers. Indeed, let's assume that a subsegment contains more than 60 good numbers. In this subsegment $$p \geq 2^{61} > 2 \cdot 10^{18}$$. At the same time, as $$k \leq 10^5$$, and $$s \leq n \cdot 10^{8} = 2 \cdot 10^{13}$$, there is $$s \cdot k \leq 2 \cdot 10^{18}$$. Therefore, this subsegment can't be suitable due to $$s \cdot k < p$$. Let's keep all positions of good numbers in a sorted array. We sort out possible left border of a subsegment and then with binary search we find the next good number to the right of this left border. Then let's iterate from this found number to the right by the good numbers(that is we sort out the rightmost good number in a subsegment), until product of all numbers in the subsegment becomes more than $$2 \cdot 10^{18}$$ (it's flag which shows us, that product is too big for a suitable subsgment and we need to finish to iterate). We have shown above the number of iterations isn't more than 60. Now for sorted out the left border and the rightmost good number we only need to know the number of 1's which needs to be added to the right of the rightmost good number, as we can easily maintain sum and product in the subsegment during iterating. Then we need to check whether found number of 1's exists to the right of the rightmost good number. It can be checked if we look at the next good number's position. Complexity is $${\mathcal {O}}(n\cdot \log(10^{18}))$$. In order to check that $$a \cdot b$$ is more than $$2 \cdot 10^{18}$$, you shouldn't calculate $$\alpha$$ multiply by $$\beta$$, due to overflow. You must only check that $${ \frac { 2 \cdot 1 0 ^ { 1 8 } } { a } } \geq b$$.</div></body></html>
```python n, k = map(int, input().split()) A = list(map(int, input().split())) from itertools import accumulate C = [0]+A C = list(accumulate(C)) A = [0]+A P = [0]*(n+1) x = 0 # P[i]: iの直前にある2以上の項のindex for i in range(1, n+1): P[i] = x if A[i] > 1: x = i INF = 2*10**18+1 ans = 0 for i in range(1, n+1): p = 1 j = i while j: if p*A[j] < INF: s = C[i]-C[j-1] p *= A[j] if p%k == 0: d = p//k-s # j-P[j]-1: P[j]とjの間にある1の個数 if 0 <= d <= j-P[j]-1: ans += 1 else: break j = P[j] print(ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program should be a Python function generated from the given prompt. Simply call the function after the definition. <html><body><h1>Nastya and a Game</h1><div>Time limit per test: 2.0 s</div><h2>Description</h2><div>Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $$\frac{p}{s} = k$$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.</div><h2>Input Format</h2><div>The first line contains two integers n and k (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 105), where n is the length of the array and k is the constant described above. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 108) — the elements of the array.</div><h2>Output Format</h2><div>In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.</div><h2>Example</h2><h3>Input</h3><div>1 1 1</div><h3>Output</h3><div>1</div><h2>Example</h2><h3>Input</h3><div>4 2 6 3 8 1</div><h3>Output</h3><div>2</div><h2>Note</h2><div>In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $${ \frac { 1 } { 1 } } = 1$$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $$\frac{18}{9}=2$$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $$\frac{24}{12}=2$$.</div><h2>Editorial</h2><div>Let's call numbers which are more than 1 as good. Notice the following fact: If a subsegment is suitable, it contains not more than 60 good numbers. Indeed, let's assume that a subsegment contains more than 60 good numbers. In this subsegment $$p \geq 2^{61} > 2 \cdot 10^{18}$$. At the same time, as $$k \leq 10^5$$, and $$s \leq n \cdot 10^{8} = 2 \cdot 10^{13}$$, there is $$s \cdot k \leq 2 \cdot 10^{18}$$. Therefore, this subsegment can't be suitable due to $$s \cdot k < p$$. Let's keep all positions of good numbers in a sorted array. We sort out possible left border of a subsegment and then with binary search we find the next good number to the right of this left border. Then let's iterate from this found number to the right by the good numbers(that is we sort out the rightmost good number in a subsegment), until product of all numbers in the subsegment becomes more than $$2 \cdot 10^{18}$$ (it's flag which shows us, that product is too big for a suitable subsgment and we need to finish to iterate). We have shown above the number of iterations isn't more than 60. Now for sorted out the left border and the rightmost good number we only need to know the number of 1's which needs to be added to the right of the rightmost good number, as we can easily maintain sum and product in the subsegment during iterating. Then we need to check whether found number of 1's exists to the right of the rightmost good number. It can be checked if we look at the next good number's position. Complexity is $${\mathcal {O}}(n\cdot \log(10^{18}))$$. In order to check that $$a \cdot b$$ is more than $$2 \cdot 10^{18}$$, you shouldn't calculate $$\alpha$$ multiply by $$\beta$$, due to overflow. You must only check that $${ \frac { 2 \cdot 1 0 ^ { 1 8 } } { a } } \geq b$$.</div></body></html>
```python n,k = map(int,input().split()) A = list(map(int,input().split())) A = [0]+A; x = 0 prev = [0 for i in range(n+1)] sm = [0 for i in range(n+1)] for i in range(1,n+1): prev[i] = x if A[i]>1: x = i sm[i] = A[i]+sm[i-1] lim = int(2*(10**18)) ans = 0 for i in range(1,n+1): p = 1 j = i while j: if lim//A[j]>p: s = sm[i]-sm[j-1] p *= A[j] if p%k == 0 and p//k>=s and j-1-prev[j]>=p/k-s: ans += 1 else: break j = prev[j] print(ans)```