message
stringlengths
2
59.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
37
108k
cluster
float64
20
20
__index_level_0__
int64
74
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` from math import gcd from functools import reduce n, k = map(int, input().split()) st = reduce(gcd, {int(e) % k for e in input().split()}) st = gcd(st, k) print(k // st) print(' '.join(map(str, range(0, k, st)))) ```
instruction
0
65,620
20
131,240
Yes
output
1
65,620
20
131,241
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` from math import gcd n, k = map(int, input().split()) a = list(map(int, input().split())) a_ = [el % k for el in a] g = gcd(k, a_[0]) for i in range(1, n): g = gcd(g, a_[i]) print(k // g) print(' '.join(map(str, [g * i for i in range(k // g)]))) ```
instruction
0
65,621
20
131,242
Yes
output
1
65,621
20
131,243
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` import math n,k=map(int,input().split()) c=[] b=list(map(int,input().split())) g=b[0] for j in range(1,n): g=math.gcd(g,b[j]) l=0 ans=[] j=0 while(j<k): ans.append(l%k) l+=g j+=1 ans=list(set(ans)) print(len(ans)) print(*ans) ```
instruction
0
65,622
20
131,244
No
output
1
65,622
20
131,245
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` import math n, k = map(int, input().split()) a = list(map(int, input().split())) GCD = 0 for x in a: GCD = math.gcd(GCD, x) ans = {GCD%k} for i in range(k): ans.add((i*GCD)%k) print(*sorted(list(ans))) ```
instruction
0
65,623
20
131,246
No
output
1
65,623
20
131,247
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` n, k = map(int,input().split()) v = list(map(int,input().split())) def gcd(a,b): if a < b: return gcd(b,a) if b == 0: return a else: return gcd(b, a%b) g = v[0] for i in range(1,n): g = gcd(g, v[i]) lst = [] i = 0 while g * i < k: lst.append(g*i % k) i += 1 lst.sort() print(len(lst)) print(' '.join(map(str,lst))) ```
instruction
0
65,624
20
131,248
No
output
1
65,624
20
131,249
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars. There are n banknote denominations on Mars: the value of i-th banknote is a_i. Natasha has an infinite number of banknotes of each denomination. Martians have k fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base k is d, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet. Determine for which values d Natasha can make the Martians happy. Natasha can use only her banknotes. Martians don't give her change. Input The first line contains two integers n and k (1 ≤ n ≤ 100 000, 2 ≤ k ≤ 100 000) — the number of denominations of banknotes and the base of the number system on Mars. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — denominations of banknotes on Mars. All numbers are given in decimal notation. Output On the first line output the number of values d for which Natasha can make the Martians happy. In the second line, output all these values in increasing order. Print all numbers in decimal notation. Examples Input 2 8 12 20 Output 2 0 4 Input 3 10 10 20 30 Output 1 0 Note Consider the first test case. It uses the octal number system. If you take one banknote with the value of 12, you will get 14_8 in octal system. The last digit is 4_8. If you take one banknote with the value of 12 and one banknote with the value of 20, the total value will be 32. In the octal system, it is 40_8. The last digit is 0_8. If you take two banknotes with the value of 20, the total value will be 40, this is 50_8 in the octal system. The last digit is 0_8. No other digits other than 0_8 and 4_8 can be obtained. Digits 0_8 and 4_8 could also be obtained in other ways. The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. Submitted Solution: ``` n, k = [int(aa) for aa in input().split()] a = [0] * k for aa in input().split(): a[int(aa)%k] = 1 answ = a.copy() for i in range(len(a)): if a[i]==1: pos = (i+i)%k a[pos] = 0 while answ[pos]==0: answ[pos]=1 pos = (pos+i)%k a[pos] = 0 string = '' size = 0 for i in range(len(answ)): if answ[i]==1: size+=1 string+= str(i) + ' ' print(size) print(string) ```
instruction
0
65,625
20
131,250
No
output
1
65,625
20
131,251
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,871
20
131,742
Tags: brute force Correct Solution: ``` a=int(input()) b=list(map(int,input().strip().split())) e,o=[],[] for i in range(len(b)): if b[i]%2==0: e.append(b[i]) else: o.append(b[i]) if len(e)==1: print(b.index(e[0])+1) else: print(b.index(o[0])+1) ```
output
1
65,871
20
131,743
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,872
20
131,744
Tags: brute force Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) e=0 o=0 c=0 b=0 for i in range(n): if a[i]%2==0: e=e+1 c=i if a[i]%2!=0: o=o+1 b=i if e>o: print(b+1) else: print(c+1) ```
output
1
65,872
20
131,745
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,873
20
131,746
Tags: brute force Correct Solution: ``` n = int(input()) t = [int(x) for x in input().split()] k = [] l = [] for i in range(n): if t[i] % 2 == 0: k.append(i+1) else: l.append(i+1) if len(k) > len(l): for p in range(len(l)): print(l[p],end = '') else: for q in range(len(k)): print(k[q],end = '') ```
output
1
65,873
20
131,747
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,874
20
131,748
Tags: brute force Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) even = odd = 0 for i in range(len(l)): if(l[i]%2==0): even+=1 even_num = i+1 else: odd+=1 odd_num = i+1 if(even>odd): print(odd_num) else: print(even_num) ```
output
1
65,874
20
131,749
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,875
20
131,750
Tags: brute force Correct Solution: ``` n=int(input()) a=list(input().split()) ce=[] co=[] for i in a: if int(i)%2==0: ce.append(int(i)) else: co.append(int(i)) if len(co)==1: for i in range(len(a)): if co[0]==int(a[i]): print(i+1) else: for i in range(len(a)): if ce[0]==int(a[i]): print(i+1) ```
output
1
65,875
20
131,751
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,876
20
131,752
Tags: brute force Correct Solution: ``` a=int(input()) b=[int(x) for x in input().split()] c=0 d=0 for i in b: if i%2==0: c=c+1 else: d=d+1 if c<d: for i in range(len(b)): if b[i]%2==0: print(i+1) break elif d<c: for i in range(len(b)): if b[i]%2!=0: print(i+1) break ```
output
1
65,876
20
131,753
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,877
20
131,754
Tags: brute force Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) v=0 b=0 c=0 d=0 for i in range(n): if(a[i]%2==0): v+=1 b=i else: c+=1 d=i if(v==1): print(b+1) else: print(d+1) ```
output
1
65,877
20
131,755
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,888
20
131,776
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` import sys def main(): n = int(sys.stdin.readline()) arr = [0] * (n + 1) cnt = [0] * (n + 1) curr_len = 1 curr_sum = 0 for _ in range(n): t = list(map(int, sys.stdin.readline().split())) if t[0] == 1: curr_sum += t[1] * t[2] cnt[t[1] - 1] += t[2] elif t[0] == 2: curr_sum += t[1] arr[curr_len] = t[1] curr_len += 1 else: curr_len -= 1 curr_sum -= arr[curr_len] + cnt[curr_len] cnt[curr_len - 1] += cnt[curr_len] cnt[curr_len] = 0 print(curr_sum / curr_len) main() ```
output
1
65,888
20
131,777
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,889
20
131,778
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` from sys import stdin input=stdin.readline n=int(input()) s=0 l=1 cnt=[0]*(n+1) arr=[0] for _ in range(n): t=list(map(int,input().split())) if t[0]==1: a,x=t[1:] cnt[a]+=x s+=min(a,l)*x elif t[0]==2: k=t[1] arr.append(k) s+=k l+=1 else: cnt[l-1]+=cnt[l] s-=arr[-1]+cnt[l] cnt[l]=0 arr.pop() l-=1 print(s/l) ```
output
1
65,889
20
131,779
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,890
20
131,780
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` from sys import stdin input = stdin.readline n = int(input()) arr, cnt, curr_len, curr_sum = [0] * (n+1), [0] * (n+1), 1, 0 for _ in range(n): s = input() if s[0] == '1': _, x, y = map(int, s.split()) curr_sum += x*y cnt[x-1] += y elif s[0] == '2': _, x = map(int, s.split()) curr_sum += x arr[curr_len] = x curr_len += 1 else: curr_len -= 1 curr_sum -= arr[curr_len] + cnt[curr_len] cnt[curr_len-1] += cnt[curr_len] cnt[curr_len] = 0 print('%.9f' % (curr_sum / curr_len)) ```
output
1
65,890
20
131,781
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,891
20
131,782
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` ###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction from collections import * from sys import stdin import sys # from bisect import * from heapq import * from math import log2, ceil, sqrt, gcd, log g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] rr = lambda x : reversed(range(x)) mod = int(1e9)+7 inf = float("inf") # range = xrange t, = gil() one, rang = [0], [0] sm = 0 while t: t -= 1 res = gil() if len(res) == 3: ai, xi = res[1:] sm += ai*xi rang[ai-1] += xi elif len(res) == 2: one.append(res[1]) rang.append(0) sm += res[1] else: rang[-2] += rang[-1] sm -= rang.pop() + one.pop() # print(one, rang, sm) print(round(sm/len(one), 6)) ```
output
1
65,891
20
131,783
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,893
20
131,786
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` import sys import math as mt input=sys.stdin.buffer.readline #t=int(input()) t=1 for __ in range(t): n=int(input()) #n,h=map(int,input().split()) suma=0 last=0 length=1 ans=[] ans.append(0) pref=[0] for ____ in range(n): l=list(map(int,input().split())) if len(l)==1: last=ans[-1] suma-=last length-=1 pref[-2]+=pref[-1] suma-=pref[-1] pref.pop() ans.pop() print((suma)/length) elif len(l)==2: suma+=l[1] length+=1 last=l[1] ans.append(last) pref.append(0) print(suma/length) else: suma+=(l[2] * l[1]) #for i in range(l[1]): # ans[i]+=l[2] pref[l[1]-1]+=l[2] print(suma/length) ```
output
1
65,893
20
131,787
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image> Submitted Solution: ``` import sys,os,io import math,bisect,operator inf,mod = float('inf'),10**9+7 # sys.setrecursionlimit(10 ** 6) from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,defaultdict input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ Neo = lambda : list(map(int,input().split())) # test, = Neo() n, = Neo() arr, cnt, curr_len, curr_sum = [0] * (n+1), [0] * (n+1), 1, 0 for _ in range(n): s = input() if s[0] == '1': _, x, y = map(int, s.split()) curr_sum += x*y cnt[x-1] += y elif s[0] == '2': _, x = map(int, s.split()) curr_sum += x arr[curr_len] = x curr_len += 1 else: curr_len -= 1 curr_sum -= arr[curr_len] + cnt[curr_len] cnt[curr_len-1] += cnt[curr_len] cnt[curr_len] = 0 print('%.9f' % (curr_sum / curr_len)) ```
instruction
0
65,898
20
131,796
Yes
output
1
65,898
20
131,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image> Submitted Solution: ``` from __future__ import division lst = [0] n = 1 total = 0 avg = 0 q = int(input()) for i in range(q): Input = input().split() a = int(Input[0]) if(a==1): # print("CASE1") total = (n*avg + int(Input[1])*int(Input[2])) avg = float(total)/n print(avg) elif(a==2): # print("CASE2") lst.append(int(Input[1])) total = n*avg + int(Input[1]) #print("totsl",total,n) n+=1 avg = float(avg)/n print(avg) else: # print("CASE3") total = n*avg - lst.pop() n-=1 avg = float(avg)/n print(avg) ```
instruction
0
65,900
20
131,800
No
output
1
65,900
20
131,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image> Submitted Solution: ``` import sys input=sys.stdin.readline su=0 ans=[0] from collections import defaultdict al=defaultdict(int) a=int(input()) for i in range(a): z=list(map(int,input().split())) if(z[0]==2): ans.append(z[1]) su+=z[1] print(su/len(ans)) if(z[0]==3): su-=(al[len(ans)-1]+ans[-1]) r=al[len(ans)-1] al[len(ans)-2]+=r ans.pop() print(su/len(ans)) if(z[0]==1): su+=z[1]*z[2] print(su/len(ans)) al[z[1]-1]+=z[2] ```
instruction
0
65,901
20
131,802
No
output
1
65,901
20
131,803
Provide tags and a correct Python 3 solution for this coding contest problem. People in the Tomskaya region like magic formulas very much. You can see some of them below. Imagine you are given a sequence of positive integer numbers p1, p2, ..., pn. Lets write down some magic formulas: <image><image> Here, "mod" means the operation of taking the residue after dividing. The expression <image> means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor". People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q. Input The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers: p1, p2, ..., pn (0 ≤ pi ≤ 2·109). Output The only line of output should contain a single integer — the value of Q. Examples Input 3 1 2 3 Output 3
instruction
0
65,945
20
131,890
Tags: math Correct Solution: ``` # eople in the Tomskaya region like magic formulas very much. You can see some of them below. # Imagine you are given a sequence of positive integer numbers p1, p2, ..., pn. Lets write down some magic formulas: # Here, "mod" means the operation of taking the residue after dividing. # The expression means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor". # People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q. # Input # The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers: p1, p2, ..., pn (0 ≤ pi ≤ 2·109). # Output # The only line of output should contain a single integer — the value of Q. # Examples # inputCopy # 3 # 1 2 3 # outputCopy # 3 # Because the XOR operation satisfies the law of exchange, # So formula 2 = p1 ^ p2 ^ ...... ^ pn ^ (1% 1) ^ (1% 2) ^ ...... (1% n) ^ (2% 1)% (2% 2) ^ ...... ^ (2 % n) ^ ...... ^ (n% 1) ^ (n% 2) ^ ...... ^ (n% n) # =p1 ^ p2 ^ ...... ^ pn ^ (1% 1) ^ (2% 1) ^ ... (n% 1) ^ (1% 2) ^ (2% 2) ^ ...... ^ (n% 2 ) ^ ...... ^ (1% n)% (2% n) ^ ...... ^ (n% n) # And for any positive integer k, (1 ~ n)% k will only have k results of 0 ~ (k-1), so (1% k), (2% k) ... (n% k) It is regular, that is (1% k) ^ (2% k) ^ ... ^ (n% k) is regular. # For example, when n = 15 and k = 7, the results of (1% k), (2% k) ... (n% k) are shown in the table below. # According to the XOR principle, a ^ a = 0, 0 ^ a = a; so the XOR results in the two red lines in the figure are 0. So ( 1% 7) ^ (2% 7) ^ ...... ^ (15% 7) = 1; # Another example is when n = 25 and k = 7. The results of ( 1% 7), (2% 7) ... (25% 7) are shown in the table below. # So ( 1% 7) ^ (2% 7) ^ ...... ^ (25% 7) = 5 ^ 6 ^ 0 = 3 # Therefore, when dealing with this problem, you can use an array a to save the result of 1 XOR to n, and then enumerate k = 1 ~ n, find out how many entire rows have the remainder 0 ~ (k-1), and finally Calculate the part that is less than one full line. # Meaning of the questions: # Give N (1 ~ 10 ^ 6) the number of representatives of N, given p1 ...... pn (0 ~ 2 x 10 ^ 9). Seeking and given two formulas. # Ideas: # Mathematics. 0 to any number of different or all the same (0 ^ x == x), or any number of different time itself is 0 (x ^ x == 0), the XOR two times itself. (X ^ x ^ x == x). So-column table to observe the law: # Row # % # Row # 1 2 3 4 5 6 7 8 9 10 # 1 0 1 1 1 1 1 1 1 1 1 # 2 0 0 2 2 2 2 2 2 2 2 # 3 0 1 0 3 3 3 3 3 3 3 # 4 0 0 1 0 4 4 4 4 4 4 # 5 0 1 2 1 0 5 5 5 5 5 # 6 0 0 0 2 1 0 6 6 6 6 # 7 0 1 1 3 2 1 0 7 7 7 # 8 0 0 2 0 3 2 1 0 8 8 # 9 0 1 0 1 4 3 2 1 0 9 # 10 0 0 1 2 0 4 3 2 1 0 # Can be found, a value of 0 on the diagonal upper triangular elements ranks number. Row to see not see anything, but in a column view, you can see that each module corresponds in cycles. And this period is (n / i), if the cycle is an even number, it becomes 0 can be ignored, if it is odd, the exclusive OR result is 0 ^ 1 ^ ...... ^ i - 1. In addition, if the (n% i) is not equal to 0, also need to be described XOR 1 ^ ...... ^ (n% i) ^ (0) (0 XOR after optional, does not change because the XOR 0 its value, so that the back of the pretreatment polyisocyanate 0 or it is not affected), so a good continuous pretreatment 0 XOR values ​​corresponding to each of n. # After the deal, according to the period and the remainder of the XOR can be the answer. #include <cstdio> #include <cmath> #include <algorithm> # using namespace std; # const int MAX = 1000001; # int num[MAX]; # void solve () { # num[0] = 0; # for (int i = 1; i < MAX; ++i) # num[i] = num[i - 1] ^ i; # } # int main() { # int n, ans = 0; # solve(); # scanf("%d", &n); # for (int i = 1; i <= n; ++i) { # int a; # scanf("%d", &a); # ans ^= a; # if ((n / i) % 2) ans ^= num[i - 1]; # if (n % i) ans ^= num[n % i]; # } # printf("%d\n", ans); # return 0; # } import sys, math from copy import deepcopy as dc from bisect import bisect_left, bisect_right def findXOR(n): remainder = n%4 if remainder ==0: return n elif remainder == 1: return 1 elif remainder ==2: return n+1 else: return 0 def solution(list1): answer = 0 length = len(list1) for i in range(length): answer ^=list1[i] modded = i+1 place = length//modded if place%2==0: answer ^= findXOR(length%modded) else: answer ^= findXOR(modded-1) ^ findXOR(length%modded) return answer n = int(input()) list1 = [int(x) for x in input().split()] print(solution(list1)) ```
output
1
65,945
20
131,891
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,162
20
132,324
Tags: brute force, combinatorics, dp Correct Solution: ``` dpc=[1] dpci=[] for i in range(1,2000): dpc.append((dpc[i-1]*i)%1000000007) def modInverse(a, m) : m0 = m y = 0 x = 1 if (m == 1) : return 0 while (a > 1) : q = a // m t = m m = a % m a = t t = y y = x - q * y x = t if (x < 0) : x = x + m0 return x for i in dpc: dpci.append(modInverse(i,1000000007)) def c(n,r): if(r<0): return 0 if(n<r): return 0 if(n<0): return 0 return dpc[n]*dpci[r]*dpci[n-r] dp={1:0} def f(n): x=n b=0 while x!=0: x=x&(x-1) b+=1 dp[n]=dp[b]+1 for i in range(2,1001): f(i) a=[[],[],[],[],[]] for i in dp: a[dp[i]].append(i) n=input() l=len(n) x=[] for i in range(l): if n[i]=='1': x.append(i) #print(l) k=int(input()) if k>5: print(0) elif k==0: print(1) else: ans=0 for i in a[k-1]: r=0 for j in x: if i-r<0: break xy=c(l-j-1,i-r) #print(l-j-1,i-r,xy) ans=(ans+xy)%1000000007 r+=1 if len(x) in a[k-1] and len(x)!=1: ans+=1 if len(x)>1 and k==1: ans-=1 print(ans) ```
output
1
66,162
20
132,325
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,163
20
132,326
Tags: brute force, combinatorics, dp Correct Solution: ``` def reduce(n): n=bin(n)[2:] n=list(n) n=map(int,n) return sum(n) def a(n,bits,dp): if(bits==0): return 1 if(n=='0' or n==''): return 0 if(bits>len(n)): return 0 if(bits==len(n)): if(n=='1'*len(n)): #print(n,bits,1) return 1 else: return 0 """ i=1 while(i<len(n) and n[i]=='0'): i+=1 x=dp[len(n)-1][bits] + a(n[i:],bits-1,dp) #print(n,bits,dp[len(n)-1][bits],x) return x%1000000007 """ ans=0 while(bits!=0 and (n!='' and n!='0') and bits<len(n)): ans+=dp[len(n)-1][bits] i=1 while(i<len(n) and n[i]=='0'): i+=1 n=n[i:] bits-=1 if(bits==0): ans+=1 elif(bits==len(n) and n=='1'*len(n)): ans+=1 return ans n=(input()) k=int(input()) if(k>5): print(0) elif(k==0): print(1) elif(k==1): print(len(n)-1) else: dp=[] dp.append([0,]) for i in range(1,1002): dp.append([]) for j in range(i): if(j==0): dp[i].append(1) else: dp[i].append((dp[i-1][j-1]+dp[i-1][j])%1000000007) dp[i].append(1) pos=[] for i in range(1,len(n)+1): count=0 I=i while(i!=1): i=reduce(i) count+=1 if(count==k-1): pos.append(I) ans=0 for i in pos: ans+=a(n,i,dp) #print(dp) #print(a('10000',3,dp)) #print(pos) print(ans%1000000007) ```
output
1
66,163
20
132,327
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,164
20
132,328
Tags: brute force, combinatorics, dp Correct Solution: ``` def Numb(a,k): if a == 0: return 0 m = len(bin(a))-3 if m + 1 < k: return 0 if k == 1: return m+1 if m + 1 == k: return Numb(a & ((1<<m)-1), k-1) return C[m][k]+Numb(a & ((1<<m)-1), k-1) s = input() nDec = int(s,2) n = len(s) k = int(input()) C = [[1],[1,1]] for i in range(n): tmp = [1] for j in range(1,i+2): tmp.append(C[-1][j-1]+C[-1][j]) tmp.append(1) C.append(tmp) if k == 0: print(1) else: NumOfOp = [0 for i in range(n+1)] for i in range(2,n+1): NumOfOp[i] = NumOfOp[bin(i).count('1')] + 1 res = 0 for i in range(1,n+1): if NumOfOp[i] == k-1: res += Numb(nDec,i) if k == 1: res -= 1 print(res%(10**9+7)) ```
output
1
66,164
20
132,329
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,165
20
132,330
Tags: brute force, combinatorics, dp Correct Solution: ``` s0=input() k=int(input()) s1=s0[::-1] lens1=len(s1) maxnum=1005 mod=1000000007 dp=[[0]*maxnum for tmpi in range(maxnum)] f=[0]*maxnum c=[[0]*maxnum for tmpi in range(maxnum)] def cntone(num): tmps=bin(num)[2:] cnt=0 for i in range(len(tmps)): if(tmps[i]=='1'): cnt+=1 return cnt for i in range(1,maxnum): if(i==1): f[i]=0 else: f[i]=f[cntone(i)]+1 for i in range(maxnum): if(i==0): c[i][0]=1 continue for j in range(i+1): if(j==0): c[i][j]=1 else: c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod for i in range(lens1): if(i==0): dp[i][0] = 1 if(s1[i]=='1'): dp[i][1]=1 else: dp[i][1]=0 continue else: for j in range(0,i+2): if(j==0): dp[i][j]=1 continue if(s1[i]=='1'): dp[i][j]=(dp[i-1][j-1]+c[i][j])%mod else: dp[i][j]=dp[i-1][j]%mod ans=0 for i in range(1,lens1+1): if(f[i]==k-1): ans=(ans+dp[lens1-1][i])%mod if(k==0): ans=1 elif(k==1): ans-=1 else: ans=ans print(ans) ```
output
1
66,165
20
132,331
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,166
20
132,332
Tags: brute force, combinatorics, dp Correct Solution: ``` # -*- encoding:utf-8 -*- import sys from functools import reduce #sys.stdin = open('C.in', 'r') def debug(*args): return print('debug ',end='') for x in args: print(x,end=' ') print('') n = input() k = int(input()) mod = 1000000007 #n = '1'*1000 #k = 5 a = [0] * 1001 def bitnum(x): ans = 0 while x: if x & 1: ans += 1 x >>= 1 return ans for i in range(1,1001): bn = bitnum(i) a[i] = a[bn] + 1 #debug([i for i in range(10)]) #debug(a[:10]) ''' number of bits that needs k operations to reduce to 1 ''' a = [i for i in range(len(a)) if a[i]==k] a = list(filter(lambda x: x <= len(n), a)) #debug(a[:10]) C = [None for i in range(1001)] C[0] = [1, 0] for i in range(1, len(C)): C[i] = [1] + [(C[i-1][j]+C[i-1][j-1])%mod for j in range(1,i)] + [1] answer = 0 def cal2(x, arr): global answer while True: ln = len(arr) if x == 0: answer += 1 return if x > ln: return if x == ln: if '0' in arr:return else: answer += 1 return answer += C[ln-1][x] answer %= mod #debug(ans, x) i = arr.find('1', arr.index('1')+1) if i < 0: if x == 1: answer += 1 return #debug("***") x -= 1 arr = arr[i:] #cal2(x-1, arr[i:]) def cal(x): global answer answer = 0 cal2(x, n) debug(answer) return answer % mod def solve(): if k == 0:return 1 if len(a) == 0:return 0 tmp = map(cal,a) debug(list(tmp)) ans = reduce(lambda x,y:(x+y)%mod, map(cal, a)) return ans if k != 1 else ans-1 try: print(solve()) except Exception as e: print(k, n[-30:]) #print(solve()) ```
output
1
66,166
20
132,333
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,167
20
132,334
Tags: brute force, combinatorics, dp Correct Solution: ``` n = input() k = int(input()) MOD = int(1e9 + 7) a = {0: 0, 1: 0, 2: 1} for i in range(3, 1001): s = bin(i)[2:] ones_count = 0 for x in s: if x == "1": ones_count += 1 a[i] = a[ones_count] + 1 answer = 0 table = {} def bin_coeff(N, K): if K > N: raise Exception if K > N // 2: K = N - K if K == 0: table[(N, 0)] = 1 return 1 if (N, K) not in table: table[(N, K)] = (N * bin_coeff(N - 1, K - 1) // K) return table[(N, K)] ones = 0 for i, x in enumerate(n): if x == "1": good = [] N = len(n) - i - 1 min = 0 if i > 0 else 1 for j in range(min, N + 1): if a[j + ones] == k - 1: good.append(j) for x in good: answer = (answer + bin_coeff(N, x)) % MOD if i == 0 and x == 1: answer -= 1 ones += 1 if a[ones] == k - 1: answer += 1 if k == 0: answer = 1 if k == 1: answer = len(n) - 1 answer = int(answer) print(answer) ```
output
1
66,167
20
132,335
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,168
20
132,336
Tags: brute force, combinatorics, dp Correct Solution: ``` def main(): s, k = input(), int(input()) n = len(s) if k < 2: print((1, n - 1)[k]) return tais = [n - i - 1 for i, c in enumerate(s) if c == '1'] tlen, cache, res = len(tais), {}, 0 def c(n, m): if n < m * 2: m = n - m if 0 < m: if (n, m) not in cache: cache[n, m] = (c(n - 1, m - 1) + c(n - 1, m)) % 1000000007 return cache[n, m] return not m for m in range(n, 0, -1): x, t = m, k while x != 1: y = 0 t -= 1 while x: y += x & 1 x //= 2 x = y if t == 1: if len(tais) > m: del tais[m:] res += sum(c(t, m - i) for i, t in enumerate(tais)) + (m <= tlen) print(res % 1000000007) if __name__ == '__main__': from sys import setrecursionlimit setrecursionlimit(1050) main() ```
output
1
66,168
20
132,337
Provide tags and a correct Python 3 solution for this coding contest problem. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
instruction
0
66,169
20
132,338
Tags: brute force, combinatorics, dp Correct Solution: ``` R = lambda: map(int, input().split()) mod = 10 ** 9 + 7 maxn = 1001 c = [[0 for j in range(maxn)] for i in range(maxn)] for i in range(maxn): c[i][0] = 1 for i in range(1, maxn): for j in range(i + 1): c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod arr = list(map(int, input())) k = int(input()) if k == 0: print(1 if arr.count(1) else 0) exit(0) ops = [0] * (maxn + 1) ans = 0 for i in range(2, maxn): cnt = bin(i).count('1') ops[i] = ops[cnt] + 1 for i in range(1, maxn): if ops[i] == k - 1: oc = i for j, x in enumerate(arr): if x and oc >= 0: ans = (ans + c[len(arr) - j - 1][oc]) % mod oc -= 1 ans = (ans + 1) % mod if arr.count(1) == i else ans if k == 1: ans = (ans + mod - 1) % mod print(ans) ```
output
1
66,169
20
132,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` def fact(n): if(n==0): return 1; return(n*fact(n-1)) mod=1000000007 ncrtable=[[0 for i in range(1001)] for j in range(1001)]; ncrtable[0][0]=1; for row in range(1,1001): for col in range(row+1): if(col==0): ncrtable[row][col]=1; else: ncrtable[row][col]=ncrtable[row-1][col]+ncrtable[row-1][col-1]; def ncr(n,r): if(n<r): return 0; if(r<0): return 0; return ncrtable[n][r]; def compute(n,x): tot=0; #cnt=0; y=x; for i in range(1000,-1,-1): if(1<<i & n): tot+=ncr(i,x)%mod; tot%=mod x-=1; #cnt+=1; if(x==0): return((tot+1)%mod); return(tot%mod); arr=[0 for i in range(1002)]; arr[1]=1; for i in range(2,1001): ct=0 for j in range(32,-1,-1): if(1<<j & i): ct+=1; arr[i]=arr[ct]+1 n=int(input(),2); #print(n); k=int(input()) ans=0 for i in range(0,1001): if(arr[i]==k): ans=compute(n,i)+ans; ans%=mod; if(k==1): ans-=1; print(ans); exit(0) if(n>=1001): for i in range(0,1001): if(arr[i]==k-1): ans=compute(n,i)+ans; #ans-=1; ans%=mod; #for i in range(1,1001): ## for j in range(32): # if(1<<j &i): # c+=1; # if(c==k-1): # ans+=(mod-1); # ans%=mod; if(k!=1): print(ans); else: print(ans-1); ```
instruction
0
66,170
20
132,340
Yes
output
1
66,170
20
132,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` def main(): s, k = input(), int(input()) n = len(s) if k < 2: print((1, n - 1)[k]) return tais = [n - i - 1 for i, c in enumerate(s) if c == '1'] tlen, mod, res = len(tais), 1000000007, 0 fac, ifac, f = [1], [1], 1 for i in range(1, n + 1): f = f * i % mod fac.append(f) x = v = 0 y = u = 1 a, b = f, mod while a: q, r = divmod(b, a) a, b, x, y, u, v = r, a, u, v, x - u * q, y - v * q ifac.append(x) def c(n, m): return fac[n] * ifac[m] * ifac[n - m] % mod if 0 <= m <= n else 0 for m in range(n, 0, -1): x, t = m, k while x != 1: y = 0 t -= 1 while x: y += x & 1 x //= 2 x = y if t == 1: if len(tais) > m: del tais[m:] res += sum(c(t, m - i) for i, t in enumerate(tais)) + (m <= tlen) print(res % mod) if __name__ == '__main__': main() ```
instruction
0
66,171
20
132,342
Yes
output
1
66,171
20
132,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` from math import factorial from functools import lru_cache mod = 1000000007 def num_iters(x): ans = 0 while x > 1: x = bin(x).count('1') ans += 1 return ans @lru_cache(maxsize=1001) def fact(n): return factorial(n) % mod def egcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y) def modinv(a, m): g, x, y = egcd(a, m) if g != 1: raise Exception('modular inverse does not exist') else: return x % m @lru_cache(maxsize=1001) def inv(a): return modinv(a, mod) def binom(n, k): return (fact(n) * inv(fact(k)) * inv(fact(n - k))) % mod def num_numbers(n, i): # number of numbers in [1..n] that have exactly i 1s # in their binary representation # n is a str ans = 0 num_ones_in_prefix = 0 for j in range(len(n) - 1): pos_right = len(n) - j - 1 if num_ones_in_prefix + pos_right < i or num_ones_in_prefix > i: break if n[j] == '1': ans = (ans + binom(pos_right, i - num_ones_in_prefix)) % mod num_ones_in_prefix += 1 cnt1 = n.count('1') if (n[-1] == '0' and cnt1 == i) or (n[-1] == '1' and cnt1 in (i, i + 1)): ans = (ans + 1) % mod return ans precalc = [num_iters(x) for x in range(1001)] def solve(n, k): if k == 0: return 1 elif k == 1: return len(n) - 1 else: ans = 0 for i in range(1, 1001): if precalc[i] == k - 1: ans = (ans + num_numbers(n, i)) % mod return ans n = input() k = int(input()) print(solve(n, k)) ```
instruction
0
66,172
20
132,344
Yes
output
1
66,172
20
132,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` import sys import math from collections import defaultdict,deque import heapq fact=[1] mod=10**9+7 for i in range(1,1005): fact.append((i*fact[-1])%mod) #print(fact,'fact') def comb(n,k,fact): #print(n,'n',k,'k') x=fact[n] y=pow(fact[k],mod-2,mod) z=pow(fact[n-k],mod-2,mod) #print(x,'x',y,'y',z,'z') return (x*(y*z))%mod def getset(num): cnt=0 while num>0: cnt+=num%2 num=num>>1 return cnt def get(s,bits): #print(s,'s',bits,'bits') n=len(s) #print(n,'n') if n!=0 and bits==0: return 1 if n==0 and bits==0: return 1 if n==0: return 0 if s[0]=='1': ans=0 if n-1>=bits: ans+=comb(n-1,bits,fact) #print(comb(n-1,bits,fact),'comb') x=get(s[1:],bits-1) #print(x,'x',s,'s',bits-1,'bitts') ans+=x ans%=mod #print(s,'s',ans,bits) return ans x=get(s[1:],bits) x%=mod #print(s,'s',x,bits) return x #t=int(sys.stdin.readline()) num=sys.stdin.readline()[:-1] m=len(num) n=0 #print(m,'m') for i in range(m): if num[i]=='1': n=(n<<1)+1 else: n=n<<1 #print(n,'nnnn') k=int(sys.stdin.readline()[:-1]) if k==0: print(1) sys.exit() dic=defaultdict(list) for i in range(2,1001): x=getset(i) cnt=0 while x!=1: y=getset(x) cnt+=1 x=y dic[cnt+1].append(i) #print(dic,'dic') ans=0 '''for i in dic[k]: if i<=n: ans+=1''' #ans+=len(dic[k]) #print(n,'n') #print(num,'num') s=num #print(s,'s') #print(len(s)) dic[0]=[1] #print(dic[k-1]) for j in dic[k-1]: if j<=n: #print(s,'s') x=get(s,j) #print(x,'xxxxxxxx') #print(j,'j') #print(x,'x',j,'j',k-1,'k-1') ans+=x #print(x,'x',k-1,'j',j,'j') ans%=mod #ans=get(s,k-1) if s=='1' and k==1: print(0) sys.exit() if k==1: ans-=1 print(ans) ```
instruction
0
66,173
20
132,346
Yes
output
1
66,173
20
132,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` # int(input()) # [int(i) for i in input().split()] def count_oper(x): if x == 1: return(0) res = 0 y = x while y > 0: if y % 2 == 1: res += 1 y = y//2 return(count_oper(res) + 1) def solve(s,k): a = [] ones = [] for i in range(len(s)): c= s[i] a.append(int(c == '1')) if c == '1': ones.append(i) nones = len(ones) n = len(a) if k == 0 : print(1) return if k == 1 and n != 1: print(n) return if n == 1: if k > 0: print(0) else: print(1) return #print("main") # compute binomial coeff-s: c = [] c.append([0]*(n+1)) for n1 in range(1,n+1): tmp = [0]*(n+1) for m in range(n1+1): #print(n1,m) if m == 0 or m == n1: tmp[m] = 1 else: tmp[m] = (c[n1-1][m-1] + c[n1-1][m]) % modulo c.append(tmp) ans = 0 for m in range(1,n+1): # how many 1's should be in a special number? if count_oper(m) == k-1: # m ones! for j in range(min(nones,m)): # loop over 1's and add corrsponding bin coef # print(j, ones[j]) ans += (c[n - ones[j] - 1][m - j ]) % modulo if nones >= m: ans += 1 print(ans % modulo) s = input() k = int(input()) modulo = 10**9 + 7 solve(s,k) ```
instruction
0
66,174
20
132,348
No
output
1
66,174
20
132,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` n = input() k = int(input()) MOD = int(1e9 + 7) a = {0:0, 1: 0, 2: 1} for i in range(3, 1001): s = bin(i)[2:] ones_count = 0 for x in s: if x == "1": ones_count += 1 a[i] = a[ones_count] + 1 answer = 0 table = {} def bin_coeff(N, K): if K > N: raise Exception if K > N // 2: K = N - K if K == 0: table[(N, 0)] = 1 return 1 if (N, K) not in table: table[(N, K)] = (N * bin_coeff(N - 1, K - 1) / K) % MOD return table[(N, K)] ones = 0 for i, x in enumerate(n): if x == "1": good = [] N = len(n) - i - 1 min = 0 if i > 0 else 1 for j in range(min, N + 1): if a[j + ones] == k - 1: good.append(j) for x in good: answer = (answer + bin_coeff(N, x)) % MOD ones += 1 if a[ones] == k - 1: answer += 1 if n == "11" and k == 1: answer = 1 if k == 0: answer = 1 answer = int(answer) print(answer) ```
instruction
0
66,175
20
132,350
No
output
1
66,175
20
132,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` def Main(): n = int(input()) c = [x for x in range(n+1) if list(bin(list(bin(x)).count('1'))).count('1') == 2] return c if __name__== '__main__': print(Main().__len__()) ```
instruction
0
66,176
20
132,352
No
output
1
66,176
20
132,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109 + 7. Input The first line contains integer n (1 ≤ n < 21000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. Output Output a single integer — the number of special numbers not greater than n, modulo 109 + 7. Examples Input 110 2 Output 3 Input 111111011 2 Output 169 Note In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). Submitted Solution: ``` #l=[None,0,1,2,1,2,2,3,1,2,2,3,2,3,3,2,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,3,3,4,3,4,4,2,3,4,4,2,4,2,2,3,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,3,3,4,3,4,4,2,3,4,4,2,4,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,3,3,4,3,4,4,2,3,4,4,2,4,2,2,3,3,2,2,3,2,3,3,3,2,3,3,3,3,3,3,4,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,3,3,4,3,4,4,2,3,4,4,2,4,2,2,3,2,3,3,3,3,3,3,4,3,3,3,4,3,4,4,2,3,3,3,4,3,4,4,2,3,4,4,2,4,2,2,3,3,3,3,4,3,4,4,2,3] invs=[1,1,500000004,333333336,250000002,400000003,166666668,142857144,125000001,111111112,700000005,818181824,83333334,153846155,71428572,466666670,562500004,352941179,55555556,157894738,850000006,47619048,409090912,739130440,41666667,280000002,576923081,370370373,35714286,758620695,233333335,129032259,281250002,939393946,676470593,628571433,27777778,621621626,78947369,717948723,425000003,658536590,23809524,395348840,204545456,822222228,369565220,404255322,520833337,448979595,140000001,784313731,788461544,56603774,685185190,763636369,17857143,385964915,879310351,50847458,616666671,688524595,564516133,15873016,140625001,30769231,469696973,686567169,838235300,579710149,814285720,98591550,13888889,410958907,310810813,93333334,539473688,831168837,858974365,202531647,712500005,123456791,329268295,84337350,11904762,670588240,197674420,252873565,102272728,415730340,411111114,164835166,184782610,43010753,202127661,231578949,760416672,268041239,724489801,646464651,570000004,940594066,892156869,572815538,394230772,209523811,28301887,224299067,342592595,9174312,881818188,873873880,508928575,893805316,692982461,147826088,939655179,239316241,25423729,478991600,808333339,438016532,844262301,886178868,782258070,856000006,7936508,480314964,570312504,798449618,515384619,190839696,734848490,165413535,843283588,274074076,419117650,58394161,789855078,604316551,407142860,134751774,49295775,832167838,506944448,151724139,705479457,149659865,655405410,530201346,46666667,483443712,269736844,594771246,915584422,625806456,929487186,343949047,601265827,685534596,856250006,962732926,561728399,116564418,664634151,587878792,42168675,5988024,5952381,242603552,335294120,795321643,98837210,791907520,626436786,325714288,51136364,683615824,207865170,435754193,205555557,933701664,82417583,562841534,92391305,524324328,521505380,577540111,601063834,338624341,615789478,439790579,380208336,694300523,634020623,343589746,862244904,969543154,823232329,507537692,285000002,228855723,470297033,108374385,946078438,131707318,286407769,526570052,197115386,832535891,604761909,90047394,514150947,32863850,612149537,79069768,671296301,875576043,4587156,470319638,440909094,950226251,436936940,125560539,754464291,364444447,446902658,35242291,846491234,711790398,73913044,277056279,969827593,90128756,619658124,880851070,512711868,67510549,239495800,280334730,904166673,406639007,219008266,707818935,922131154,89795919,443089434,165991904,391129035,28112450,428000003,912350604,3968254,339920951,240157482,556862749,285156252,70038911,399224809,517374521,757692313,417624524,95419848,836501907,367424245,611320759,582706771,138576780,421641794,743494429,137037038,450184505,209558825,388278391,529197084,752727278,394927539,252707583,802158279,681003589,203571430,718861215,67375887,650176683,524647891,77192983,416083919,808362375,253472224,961937723,575862073,756013751,852739732,522184304,574829936,210169493,327702705,215488217,265100673,441471575,523333337,770764125,241721856,646864691,134868422,137704919,297385623,749185673,457792211,857605184,312903228,787781356,464743593,492012783,671974527,403174606,800632917,652996850,342767298,614420067,428125003,741433027,481366463,597523224,780864203,406153849,58282209,3058104,832317079,343465048,293939396,631419944,521084341,624624629,2994012,737313438,502976194,41543027,121301776,631268441,167647060,284457480,897660825,206997086,49418605,715942034,395953760,985590785,313218393,521489975,162857144,413105416,25568182,175637395,341807912,19718310,103932585,826330538,717877100,713091927,602777782,113573408,466850832,812672182,541208795,882191787,281420767,376021801,546195656,295392956,262162164,436657685,260752690,16085791,788770059,618666671,300531917,212201593,669312174,195250661,307894739,160104988,719895293,172323761,190104168,966233773,847150265,932816544,817010315,951156819,171794873,102301791,431122452,63613232,484771577,840506335,911616168,760705295,253768846,55137845,142500001,855361602,614427865,779156333,735148520,424691361,554187196,238329240,473039219,188264060,65853659,352798056,643203888,578692498,263285026,16867470,98557693,534772186,916267949,844868741,802380958,966745850,45023697,44917258,757075477,134117648,16431925,526932088,806074772,610722615,39534884,761020887,835648154,614318711,937788025,50574713,2293578,354691078,235159819,642369025,220454547,716553293,975113129,88036118,218468470,83146068,562780273,176733782,877232149,285077953,682222227,605321512,223451329,161147904,517621149,432967036,423245617,172866522,355895199,198257082,36956522,321041217,638528143,820734347,984913800,208602152,45064378,775160605,309829062,240938168,440425535,447983018,256355934,763213536,533755278,646315794,119747900,228511532,140167365,206680586,952083340,355509358,703319507,654244311,109504133,653608252,853909471,607802879,461065577,38854806,544897963,641547866,221544717,632860045,82995952,529292933,695564521,156941651,14056225,266533068,714000005,1996008,456175302,282306165,1984127,588118816,669960479,747534522,120078741,491159139,778431378,344422703,142578126,598440550,535019459,314563109,699612408,400386850,758687264,597302509,878846160,191938581,208812262,921606125,47709924,441904765,918250957,301707782,683712126,510396979,805660383,561205277,791353389,589118203,69288390,844859819,210820897,811918069,871747218,404452693,68518519,60998152,725092256,311233888,604779416,801834868,694139199,541133459,264598542,854280516,376363639,39927405,697463773,457504524,626353795,174774776,901079143,457809698,840501798,491949914,101785715,525846706,859430611,433392543,533687947,578761066,825088345,446208116,762323949,732864680,538596495,334500878,708041963,813263531,904181191,229565219,126736112,771230508,980968865,898100179,787931040,869191056,878006879,732418530,426369866,447863251,261092152,724020448,287414968,585738544,605084750,656514387,663851356,160202362,607744112,95798320,632550340,835845902,720735791,410684477,761666672,296173047,885382066,76285241,120860928,887603312,823432349,729818786,67434211,36124795,568852463,492635028,648692815,696574230,874592840,377235775,728896109,716369535,428802592,953150249,156451614,842190022,393890678,630818624,732371800,571200004,746006395,944178635,835987267,36565978,201587303,438985740,900316462,30015798,326498425,696062997,171383649,880690744,807210037,677621288,714062505,720748835,870716517,43545879,740683235,359689925,298761612,119010820,890432105,640986137,703076928,291858681,529141108,29096478,1529052,438167942,916158543,823439884,171732524,698027319,146969698,216338882,315709972,983408755,760542174,33082707,812312318,163418292,1497006,41853513,368656719,62593145,251488097,493313525,520771517,454814818,60650888,320531760,815634224,609720181,83823530,345080766,142228740,530014645,948830416,411678835,103498543,237263466,524709306,927431066,357971017,444283650,197976880,92352093,992795396,520863313,656609200,862266864,760744991,696709590,81428572,489301002,206552708,85348507,12784091,626950359,587818701,991513444,170903956,385049368,9859155,355836852,551966296,701262277,413165269,366433569,358938550,93444910,856545967,318497916,301388891,224687935,56786704,802213007,233425416,630344832,406336091,696011009,770604401,235939645,941095897,729138172,640710387,66848568,688010904,29931973,273097828,698778838,147696478,538565633,131081082,721997306,718328846,590847918,130376345,506040272,508042899,676037488,894385033,889185587,809333339,215712385,650265962,304116868,606100800,896688748,334656087,952443864,597625334,779973655,653947373,582128782,80052494,1310616,859947650,518954252,586161884,850065195,95052084,855656703,983116890,690012975,923575136,648124196,466408272,525161294,908505161,839124845,975578413,613607193,585897440,645326509,551150899,805874846,215561226,868789815,31806616,419313853,742385792,278833969,920253171,841972193,455808084,822194205,880352651,537106922,126884423,877038902,527568926,964956202,571250004,46192260,427680801,764632633,807213936,592546588,889578170,581164812,367574260,102595798,712345684,755856972,277093598,816728173,119164620,223312885,736519613,73439413,94132030,462759466,532926833,618757617,176399028,52247874,321601944,917575764,289346249,617896014,131642513,712907122,8433735,84235861,549278850,496998803,267386093,601197609,958133978,560334532,922434374,696066751,401190479,853745547,483372925,239620405,522511852,848520716,22458629,348288078,878537742,216725561,67058824,722679206,508215966,690504108,263466044,359064330,403037386,792298722,805361311,67520373,19767442,269454125,880510447,718424107,417824077,158381504,807159359,987312579,968894016,200230151,525287360,360505169,1146789,918671255,177345539,265142859,617579913,891676175,821184516,840728106,610227277,759364364,858276650,612684036,987556568,736723169,44018059,559188279,109234235,497187855,41573034,738496077,781390140,705487127,88366891,287150840,938616078,813823863,642538980,314794218,841111117,591564932,302660756,256921375,611725668,786740337,80573952,324145537,758810578,882288235,216483518,963776077,711622812,371303398,86433261,712568311,677947603,741548533,99128541,41349293,18478261,916395229,660520612,776814740,819264075,304864867,910367177,952535066,492456900,252960174,104301076,23630505,22532189,595927121,887580306,515508025,154914531,16008538,120469084,164004261,720212771,568544106,223991509,115588548,128177967,467724871,381606768,551214365,266877639,262381456,323157897,884332288,59873950,383001052,114255766,687958120,570083686,204806689,103340293,8342023,476041670,746097820,177754679,580477678,851659757,338860106,827122159,260599795,554752070,199174408,326804126,669412981,926954739,943473799,803901443,468717952,730532792,584442174,19427403,492339125,772448985,1019368,320773933,399796544,610772362,793908635,816430026,447821685,41497976,17189080,764646470,172552978,847782264,877139986,578470829,901507544,507028116,911735212,133266534,874874881,857000006] facts=[None,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,227020758,178290591,674358851,789741546,425606191,660911389,557316307,146326063,72847302,602640637,860734560,657629300,440732388,459042011,394134213,35757887,36978716,109361473,390205642,486580460,57155068,943272305,14530444,523095984,354551275,472948359,444985875,799434881,776829897,626855450,954784168,10503098,472639410,741412713,846397273,627068824,726372166,318608048,249010336,948537388,272481214,713985458,269199917,75195247,286129051,595484846,133605669,16340084,996745124,798197261,286427093,331333826,536698543,422103593,280940535,103956247,172980994,108669496,715534167,518459667,847555432,719101534,932614679,878715114,661063309,562937745,472081547,766523501,88403147,249058005,671814275,432398708,753889928,834533360,604401816,187359437,674989781,749079870,166267694,296627743,586379910,119711155,372559648,765725963,275417893,990953332,104379182,437918130,229730822,432543683,551999041,407899865,829485531,925465677,24826746,681288554,260451868,649705284,117286020,136034149,371858732,391895154,67942395,881317771,114178486,473061257,294289191,314702675,79023409,640855835,825267159,333127002,640874963,750244778,281086141,979025803,294327705,262601384,400781066,903100348,112345444,54289391,329067736,753211788,190014235,221964248,853030262,424235847,817254014,50069176,159892119,24464975,547421354,923517131,757017312,38561392,745647373,847105173,912880234,757794602,942573301,156287339,224537377,27830567,369398991,365040172,41386942,621910678,127618458,674190056,892978365,448450838,994387759,68366839,417262036,100021558,903643190,619341229,907349424,64099836,89271551,533249769,318708924,92770232,420330952,818908938,584698880,245797665,489377057,66623751,192146349,354927971,661674180,71396619,351167662,19519994,689278845,962979640,929109959,389110882,98399701,89541861,460662776,289903466,110982403,974515647,928612402,722479105,218299090,96415872,572421883,774063320,682979494,693774784,611379287,166890807,880178425,837467962,705738750,616613957,338771924,497191232,896114138,560652457,661582322,224945188,262995829,859081981,857116478,279856786,408062844,406076419,367193638,985761614,767884817,77737051,801784560,410447512,813374614,702909132,777826615,11426636,685259446,721228129,931065383,593559607,860745086,578819198,495425745,893029457,6156532,502193801,37480384,220174401,383076669,3013247,750298503,574624441,230733683,144887710,656590378,773954850,358485371,772254339,469363737,95843299,823414273,87709482,892174648,749756145,185864756,68295241,98238739,131504392,111672419,928208089,687974198,753032165,71715287,506557931,290314197,546089425,174590825,187067364,817659471,309331349,303445769,964814732,112937795,848457973,113604679,263728612,162653895,519013648,956915940,591788795,26960558,818561771,201473695,830318534,283328761,298655153,103269519,567777414,629890782,707451727,528064896,419467694,259775012,452053078,972081682,512829263,412924123,354780756,917691336,648929514,519218426,957710940,848100261,607279584,78508462,651656900,271922065,927371945,976904514,655633282,147015495,44958071,431540693,956102180,821001984,4640954,508310043,709072863,866824584,318461564,773853828,371761455,53040744,609526889,972452623,799173814,723225821,3874155,305590228,289496343,139259591,348260611,756867525,848691744,101266155,835557082,267191274,448180160,518514435,443022120,614718802,151579195,204297074,912569551,137049249,515433810,979001276,524451820,229298431,88837724,892742699,387369393,840349900,206661672,18186411,619853562,246548548,236767938,893832644,930410696,321544423,971435684,402636244,780681725,194281388,661238608,964476271,643075362,439409780,96895678,723461710,915447882,785640606,114709392,933696835,539582134,739120141,300372431,244129985,722433522,26638091,388855420,42468156,647517040,474194942,832805846,958306874,489519451,339220689,9833277,923477502,390998217,790283925,694135631,736657340,609563281,873127083,489593220,264439147,891171227,489029295,502009550,325923608,280525558,857054649,820622208,558213940,216997416,487921842,951328535,606653379,794417402,449723904,783486165,414645478,809681447,114612567,824953206,255016498,147060381,88903008,228293174,394357308,362355866,900088886,638573794,779598451,904922263,451026166,549459329,212643744,563246709,391796933,174243175,189725986,238337196,60051478,782959006,982673239,237607992,685987666,694447544,195840153,519748540,446086975,523485236,185780714,716004996,214280883,140643728,555470704,516522055,116665689,899547947,490696549,683197147,686671136,988747143,744912554,619072836,345158054,224284246,637879131,78947725,342273666,237716550,915360466,711578771,423071394,228124918,271834959,480779410,254894593,859192972,990202578,258044399,151532640,644862529,48049425,448119239,130306338,850105179,401639970,606863861,183881380,837401090,513536652,714177614,946271680,243293343,403377310,688653593,15447678,754734307,631353768,202296846,159906516,912696536,737140518,467380526,896686075,309895051,356369955,461415686,706245266,10064183,183054210,455971702,737368289,956771035,564163693,365118309,226637659,304857172,440299843,717116122,485961418,615704083,476049473,354119987,329471814,620060202,251964959,45357250,175414082,671119137,48735782,122378970,717506435,18459328,949577729,771970076,635808197,608040366,165916428,258536202,902229110,617090616,548564593,613394864,753777984,577888302,416452176,881599549,524547188,599140122,522765386,657552586,256787840,287613719,776067801,597965522,458655497,764387515,350167935,494713961,513386012,576480762,864589772,86987059,495636228,512647986,721997962,982831380,162376799,204281975,462134806,189646394,425968575,209834628,494248765,664281698,947663843,540352769,25662122,986679150,207298711,477043799,24708053,528335066,189351697,717500453,42764755,316734785,823726196,293357001,547414377,258966410,602945692,561521296,351253952,752369730,174204566,871148004,302242737,554611874,540181425,349941261,414343943,921115587,959388563,227019335,708812719,793380997,342547759,324322556,458370547,356254978,809319893,159690374,848340820,971304725,180230004,103061704,207441144,443272953,45593686,541647240,612817107,849140508,109375794,906749744,159084460,541378020,692284266,908221578,720697998,363923522,819281897,701846632,479994712,196613531,29272489,792937812,859009553,202148261,385627435,115321267,612859231,132778909,173511339,782369566,322583903,324703286,31244274,433755056,109559692,871157455,350443931,592104988,197184362,141678010,649163959,746537855,954594407,850681817,703404350,467293824,684978431,565588709,378843675,825260479,749777538,850502015,387852091,412307507,307565279,914127155,864079609,845970807,414173935,638273833,664477235,173471099,480759791,839694748,190898355,956270620,957911348,43002811,628936576,966234409,667971950,236586166,954211897,223051884,21058295,656573222,631532535,809706350,984734695,314281677,311454037,640732448,434907794,175084834,434807109,973816812,488481268,844735329,917344075,314288693,459259162,992521062,667512257,603748166,679935673,833938466,933875943,522922384,981191471,457854178,112860028,484939649,611363777,627371454,844300972,962501388,738504183,631041465,29224765,334078303,211237785,626057542,900175080,728504100,450509755,575177363,905713570,416609984,874776027,334255451,683287462,999293262,474888472,317020697,180417613,591538360,879151833,605566485,569294094,970567518,896200922,943088633,145735679,884701203,949403596,749113557,78958680,850679027,665376978,686499745,426302291,842343474,708066168,962548572,349652428,833757979,492365420,136639914,76093131,591710464,208764552,166233017,498121245,545840935,26721664,736011124,880639351,137410283,42609708,235572009,981737748,718913567,909319027,906112184,298059463,274736280,217450848,351267027,149682364,249066734,11785215,333890217,774940233,302540697,519852435,802535369,620684620,306323295,752310997,848793393,883503040,569433124,254795373,855478464,660158704,87911700,944741410,351053939,2634663,134077016,736459220,4882454,969435081,120150411,922584286,828772112,106810765,371205161,17024731,960279329,389323593,23991206,744762405,684217429,479374977,963728237,3246420,688035746,381629444,752436308,274567573,440219140,702541058,919238277,563955926,467150839,5249506,399086000,833151662,847391187,655983283,337920422,866913758,675206635,549602585,963783662,324756002,393087771,731515248,787956453,550936813,398161393,631665856,442637251,454846959,348994181,88011024,513458067,60476466,9760396,403700900,990173371,519613195,945797344,114696834,327457551,905694736,143025346,289024806,451579463,325709522,18701196,326143996,49850509,619195074,414881030,850660769,880149960,651809429,592293509,810577782,929598726,835669318,731671946,529667681,285562083,293565850,686472980,274474950,282703792,889076915,56602629,546147347,255724802,873696194,831784350,110556728,279941051,667003092,302778600,803516696,772054724,165410893,531446229,958833885,703493734,68812272,481542542,722167619,172528691,173636402,356397518,390931659,311533827,53449710,959934024,259493848,215350798,907381983,791418522,896453666,530274270,443147787,468552325,410897594,491169384,314015783,406644587,772818684,721371094,596483817,922913559,78344520,173781169,485391881,326797438,209197264,227032260,183290649,293208856,909531571,778733890,346053132,674154326,75833611,738595509,449942130,545136258,334305223,589959631,51605154,128106265,85269691,347284647,656835568,934798619,602272125,976691718,647351010,456965253,143605060,148066754,588283108,104912143,240217288,49898584,251930392,868617755,690598708,880742077,200550782,935358746,104053488,348096605,394187502,726999264,278275958,153885020,653433530,364854920,922674021,65882280,762280792,84294078,29666249,250921311,659332228,420236707,614100318,959310571,676769211,355052615,567244231,840761673,557858783,627343983,461946676,22779421,756641425,641419708] facts_inv=[1,1,500000004,166666668,41666667,808333339,301388891,900198419,487524805,831947206,283194722,571199524,380933296,490841026,320774361,821384963,738836565,514049213,639669405,402087866,120104394,862862120,130130097,179570875,799148792,791965957,761229465,917082579,282752951,699405279,123313510,649139150,957785605,604781386,958964165,170256120,671396008,261389083,243720767,211377457,230284438,469031331,654024560,317535457,393580354,297635121,202122504,366002609,861791727,854322286,57086446,138374245,98814890,662241795,956708188,762849245,960050886,16842998,379600744,752196628,279203279,250478744,971781922,888440989,92006891,262953954,367620517,856233148,659650492,328400734,261834298,102279357,626420551,159266036,785936033,757145819,470488764,58058296,590487931,361904913,517023815,623666965,775898383,33444559,512302915,711909451,833859418,262458156,412073391,746203077,108291146,902288920,444590100,843490222,455781814,720587182,986672790,267903845,992529638,545379091,875453797,751242122,732855320,463425783,917917562,427789694,494601793,929856098,360461633,462022587,631472937,708391653,979539218,150261410,966230370,704054182,781931507,664802838,73430533,244314544,735369293,204424541,157413317,90710678,403957347,35231659,341549460,435760235,26841877,287029784,832977158,494908226,18900820,428713543,234542640,890626248,477136961,324650637,415396022,556945299,311121040,775256183,97008847,909769299,860484515,247313688,97584341,197942752,622959077,816261476,218775078,61051491,388559552,270513463,930328016,535034378,939327150,802161325,87355452,76021104,300475134,921120970,141488402,117432445,836081911,411127772,279585109,331015482,251970332,113916985,971258342,929656488,889125916,178549862,909072132,142337556,23536009,73579300,595918989,327351505,474040845,682177026,267484491,181789534,164031466,579265039,169780996,589143218,907389066,354007352,970284256,753771127,915384231,880390598,520002017,689846169,600458403,815230759,706137534,732191651,538660962,47456025,599244836,111326330,657408467,412962971,375791085,784424117,527809735,930755077,237765502,688330647,526831752,551769167,745569020,26723577,264012611,623336468,599189621,669402697,589406380,862395510,571452236,262652254,206529699,414251246,621302001,764851556,933179180,287917815,979512693,56188367,340759435,756827299,7507809,544712803,794680992,674239165,82664871,130052992,267208556,113141945,12864223,559723725,842457890,921805957,885861006,35975146,40467642,554379392,778217523,182383338,405485651,139942632,890314741,678001239,811242198,707436745,425222626,905116696,126558142,441097927,852828624,326436613,232297110,604650182,671446058,10005416,522425397,518670730,38958040,830402065,800847072,376559882,110863358,113130413,279395402,246496014,986498194,953356631,314119133,22470175,961072596,215410152,155688065,70721713,297450078,812883107,541016959,1872031,644834046,476442732,755056316,91314186,326841207,285853701,396235995,476081606,270053967,311939982,581039804,985319076,135712978,964144274,904487323,678375373,515288812,705261532,807484621,122354320,419749533,972410777,211450036,949557355,18947635,222282375,48171780,378700860,623832397,478444619,510870143,876980910,810176965,748638324,113421724,440348993,863313960,855851119,438584915,828080811,720691159,71663720,48408626,843989221,837856261,23396586,696498209,699396142,558282241,447074582,748373754,670816350,949329879,230172975,253576086,513778485,518825953,716192587,608379868,932975307,856951364,238908695,952383270,413462845,749755550,255633116,371504590,575270325,736243777,7621849,202798951,842667039,502327813,12403107,546737375,494648599,151624724,395508517,384226928,242233679,35789821,275029085,239986639,890723831,812542048,364833448,745651158,442296160,329212426,335960985,66673582,971303612,458040065,136966163,279002518,756568843,351700958,866541869,396563255,517728958,965430081,680218496,838469951,144627151,576509210,798927877,640906388,538138307,199844570,847618664,622119051,373122494,767097325,617288087,395092300,433074305,67569149,815890839,568176207,776450314,850674275,408395804,136913583,801784300,794207214,262154718,34284026,400561835,433972639,573828101,184699592,812790266,404769648,582517190,392883298,497395023,656097176,460552924,608552698,486267026,331363414,476406879,202491684,432338321,344314146,993894982,649068571,532377735,925873015,756095388,83536581,782502356,467833719,201958994,113067476,982276563,417000623,683259514,595275137,339855849,540755239,602085936,629650637,694546695,783468170,990732904,300418276,153830238,781995268,886235290,762796169,704474617,834858176,120593647,291208178,185572492,867351020,704212748,633983365,436319797,679651749,574691409,884692149,583265738,954817023,25168036,437027667,13494817,926806481,260807531,473460019,215121540,398786562,665421923,247242609,357210812,375220601,951489166,260146496,562904189,803189606,115688778,402674167,664102792,62073083,931438538,989781133,54305395,845490579,408508000,817016,770460718,252530800,193344993,18240764,451521272,550299453,675641621,648967803,128976362,498292114,586102337,268722858,813389329,711699206,345071263,549118359,498160774,12544712,899831499,7499676,159323417,860458480,925163407,526574743,431479193,124394448,736478932,827152428,468482333,191449968,570982019,630772527,6811956,460686917,587776989,189529436,618602480,213045731,31935150,166725807,46518902,361709447,728106284,141044314,106680816,648547039,81624401,345039463,428679492,444415784,871949944,552304261,263204892,812749473,93356306,615275825,658196191,99745872,808765204,980015659,506203240,798053748,875307384,490913669,395559142,537801345,254916759,26857248,323421544,786532324,111710215,362083412,98363148,686582519,232498406,816375871,302974657,320593384,541140925,837139904,84057040,831759554,589762885,489023570,342716282,966455154,672855976,456926629,511811424,370359005,435482844,480465346,749545478,742002607,688642026,635383632,287496457,895129599,639223927,732732045,883082755,597812434,281256739,243843142,932634458,133552203,978803223,605228299,76527469,923076281,353392926,363322540,770576388,900277818,11219964,45472760,708339507,491437446,743928014,675393437,584018351,629556304,639854830,305512590,408488823,110876181,497784495,617034693,851537422,102938949,385266150,668331122,814641919,630622468,978945869,28268783,357972167,146329111,973624935,149958790,433931296,25598024,447940280,354732829,764891064,685394573,585294277,465409408,469130064,920721745,182673921,284022508,597678446,416816025,588422624,98457962,240636924,36839874,300511140,182273503,581213731,455560749,864940520,856724314,261438685,740632796,674273817,942626165,174802133,374887767,194299387,742848665,907493095,441999251,492506669,611675310,290416066,970929824,883609623,223358251,807963821,422005815,659475855,461490465,664907290,263359925,173600234,665950005,716496304,824226812,899890349,390028746,771125588,145203352,452007490,325362082,146808268,736600017,245688986,146065271,957412368,898799739,278661168,704941285,610928999,81601883,483849511,959722958,846205539,512459448,477514011,94771790,785546670,950680044,61469483,942823288,676349828,399270685,202224300,83614201,27855221,178708942,736070142,898806733,147446631,252269212,554679879,911476215,87670064,942585856,185967971,834953514,366759830,422843000,170643325,93981853,572719111,985870900,598086433,218375794,859943833,859649526,91331965,695015236,607644320,732718027,917982226,747216559,979635810,561306185,34036360,223449518,94586255,423202372,442944642,611697021,200279654,277309078,618283679,820550379,353246455,521460957,68835467,668938271,630939793,166620027,751195077,59571869,663276431,490471791,292464946,386389206,728831039,100424847,231097324,77617394,253639149,723976405,560621283,219949516,322944880,693507607,78791198,119998459,265121019,385833490,175839688,127126701,357575575,462477947,523972795,85257542,474256318,882209397,465260644,945308123,444097002,744917420,375150087,199218939,962795536,153320195,574288074,366385933,970641480,460261345,165378268,54660122,796112070,75056929,684432874,318576890,508386937,736496795,933419021,301388996,795962538,981413164,518902828,825023058,764707707,274652931,632168477,715573025,516018880,296024238,427201967,100757491,425935778,453525227,714143838,325377579,154052075,164453301,661274799,281891479,854578132,707463702,54478503,560779145,542878457,842687510,21165703,595996646,257510057,793448599,603061927,924060221,392136705,100461338,471328395,891398279,205030949,652464912,835850842,87425060,823905986,380913646,278673941,927068232,520240501,876473603,136589194,511732167,687296805,683241687,350269024,968145478,892943787,218267753,631708693,575265725,844874308,490669196,539989340,283721451,422216333,634877244,154305891,30857166,70409600,192823595,593649861,218997343,408157062,510618691,46798894,349151803,787794327,112121118,212247051,734542883,558493333,135971470,553224553,555304942,877988083,353984399,287379295,128097089,213238733,686489183,584370424,478522537,386164116,728903056,854166382,496535429,200766267,471649197,622910706,125683017,956326056,20739963,301662013,197927580,561829805,470110929,409651919,25445274,530972257,693634465,812235796,433779479,246955439,273484835,533196859,434841810,150091327,794785050,966482053,130865325,820075960,424860898,894572050,601383094,45465724,85336318,672082365,48587322,356055887,859189025,162098823,439790362,893587086,428005909,694221764,21829348,71677376,928496509,456286540,290395261,680262749,924193154,292067220,171853628,296940288,150623112,455840069,180683168,811842548,508120422,642272196,544234726,650304911,229451663,921643700,798472778,749018037,226545381,765423842,718894474,717080061,749196186,489999179,806854513,378512648,701818525,88561613,592947516,719258871,280773178,426531817,513644853,265496088,511425456,854621511,704306302,284837521,431600849,280960245,932742910,291976581,677356118,77062670,110519140,832608350,207247104,180387636,52180388] import sys sys.setrecursionlimit(10000) def combi(n,r): if r==0 or r==n: return 1 if r<0 or n<r: return 0 return (((facts[n]*facts_inv[r])%1000000007)*facts_inv[n-r])%1000000007 """ def f(i,n): #2進数で1をi個含む、n以下の数の個数 keta=len(bin(n)[2:]) if n<(2**i-1): return 0 if n==1: return 1 if (i==0 or i==1) else 0 if n==0: return 1 if i==0 else 0 return (f(i-1,n-2**(keta-1))+combi(keta-1,i))%1000000007 """ def solve(n,k,flag=True): if n=="0": return 0 if n=="1": return 1 if k==0 else 0 if k<0: return 0 if k==0: return 1 ind=n.find("1",1) if ind==-1: a=1 if (k==1 and not flag) else 0 return combi(len(n)-1,k)+a else: a=-1 if (k==1 and flag) else 0 return solve(n[ind:],k-1,False)+(combi(len(n)-1,k)+a) n=input() k=int(input()) """ if k==0: print(1) elif k==1: i=0 while n>1: i+=1 n//=2 print(i) else: res=0 for i in range(1,1001): if l[i]==k-1: res+=f(i,n) res%=1000000007 print(res) """ print(solve(n,k)) ```
instruction
0
66,177
20
132,354
No
output
1
66,177
20
132,355
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,194
20
132,388
Tags: brute force, implementation, math Correct Solution: ``` from math import sqrt num = int(input()) def inString(i, num): i = str(i) num = str(num) cur_term = 0 for j in range(len(num)): if num[j] == i[cur_term]: cur_term += 1 if cur_term == len (i): return True return False ansNotFound = True for i in range(int(sqrt(num)) + 1, 0, -1): if inString(i*i, num): print(len(str(num)) - len(str(i*i))) ansNotFound = False break if ansNotFound: print(-1) ```
output
1
66,194
20
132,389
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,195
20
132,390
Tags: brute force, implementation, math Correct Solution: ``` import math as ma from sys import exit def li(): return list(map(int,input().split())) def num(): return map(int,input().split()) def nu(): return int(input()) import math def perfectSquare(s): # size of the string n = len(s) # our final answer ans = -1 # to store string which is # perfect square. num = "" # We make all possible subsequences for i in range(1 , (1 << n)): str = "" for j in range(0 , n): # to check jth bit is # set or not. if ((i >> j) & 1): str = str + s[j] # we do not consider a number # with leading zeros if (str[0] != '0'): # convert our temporary # string into integer temp = 0; for j in range(0 , len(str)): temp = (temp * 10 + (ord(str[j]) - ord('0'))) k = int(math.sqrt(temp)) # checking temp is perfect # square or not. if (k * k == temp): # taking maximum sized # string if (ans < len(str)): ans = len(str) num = str if (ans == -1): return ans else: # print PerfectSquare #print("{} ".format(num) , end="") return n - ans print(perfectSquare(input())) ```
output
1
66,195
20
132,391
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,196
20
132,392
Tags: brute force, implementation, math Correct Solution: ``` def Slove(): n = input() sq=int(int(n)**(0.5)) while sq: tmp = str(sq*sq) idx=0 for i in n: if(i==tmp[idx]): idx=idx+1 if(idx==len(tmp)): return len(n)-idx sq-=1 return -1 print(Slove()) ```
output
1
66,196
20
132,393
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,197
20
132,394
Tags: brute force, implementation, math Correct Solution: ``` nm = input() if(nm == '1') | (nm == '0'): print(0) raise SystemExit a = [] pos = 0 for i in range(44722): a.append(i * i) # for i in range(44722): # print(a[i]) i = 1 max = 0 for i in range(44721, 1, -1): if (len(str(a[i])) != len(str(nm))): i = i * 10 else: pos = i pn = a[i] # print(pos) # print(pn) break flag1 = False flag2 = False for i in range(pos, 0, -1): spn = str(a[i]) snm = str(nm) if (snm == '1'): # print('Нужный квадрат:') # # print(a[i], '=', i, '*', i) # print() # print('Наше число:') # print(nm) # print() # print('Кол-во символов для удаления:') print(len(snm) - len(spn)) exit() for j in range(len(spn)): for k in range(len(snm)): if (len(snm) - k >= len(spn) - j): if (spn[j] == snm[k]): if (j + 1 == len(spn)): # print('Нужный квадрат:') # print(a[i], '=', i, '*', i) # print() # print('Наше число:') # print(nm) # print() # print('Кол-во символов для удаления:') print(len(snm) - len(spn)) exit() j = j + 1 continue else: if (k + 1 == len(snm)): flag1 = True continue else: flag1 = True break if (flag1 == True): break if (flag1 == True): flag1 = False continue # print('Нужный квадрат:') # print(a[i], '=', i, '*', i) # print() # print('Наше число:') # print(nm) # print() # print('Кол-во символов для удаления:') print(len(snm) - len(spn)) exit() print(-1) ```
output
1
66,197
20
132,395
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,198
20
132,396
Tags: brute force, implementation, math Correct Solution: ``` import math def isSubString(str_s, str_t): i = 0 j = 0 while i < len(str_s) and j < len(str_t): if (str_s[i] == str_t[j]): i += 1 j += 1 else: i += 1 if j == len(str_t): return True return False str_s = input() for i in range(int(math.sqrt(int(str_s))), 0, -1): now = i ** 2 if isSubString(str_s, str(now)): print(len(str_s) - len(str(now))) exit() print(-1) ```
output
1
66,198
20
132,397
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,199
20
132,398
Tags: brute force, implementation, math Correct Solution: ``` #codeforces_962C_live def isSubSequence(subString, string, m=-1, n=-1): if m == -1: m = len(subString); if n == -1: n = len(string); if m == 0: return True if n == 0: return False if subString[m-1] == string[n-1]: return isSubSequence(subString, string, m-1, n-1) return isSubSequence(subString, string, m, n-1) n = input() L = [str(e*e) for e in range(1,100000)] l = len(L) for k in range(l): if isSubSequence(L[-k-1],n): print(len(n)-len(L[-k-1])) exit(); print(-1) ```
output
1
66,199
20
132,399
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,200
20
132,400
Tags: brute force, implementation, math Correct Solution: ``` import math def read_data(): n = int(input().strip()) return n def compare(s): mpos = 0 spos = 0 count = 0 while spos < len(s): npos = m.find(s[spos],mpos) if npos == -1: return -1 count += npos - mpos mpos = npos + 1 spos += 1 count += len(m) - mpos return count def solve(): start = int(math.sqrt(n)) for i in range(start,0,-1): c = compare(str(i*i)) if c > -1: return c return -1 n = read_data() m = str(n) print(solve()) ```
output
1
66,200
20
132,401
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1.
instruction
0
66,201
20
132,402
Tags: brute force, implementation, math Correct Solution: ``` n=int(input()) s=str(n) x=int(n**0.5) b=0 while True: k=str(x*x) q=0 b=0 for i in k: if i not in s[q:]: b=1 break else: q=q+s[q:].find(i)+1 if b==0: print(len(s)-len(k)) b=2 break x-=1 if x<1: break if b<2: print(-1) ```
output
1
66,201
20
132,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1. Submitted Solution: ``` import math ans = [''] def solve(pos: int): global n sz = len(ans) for i in range(sz): if ans[i] != '' or n[pos] != '0': ans.append(ans[i] + n[pos]) if pos < len(n) - 1: solve(pos + 1) n = input() if int(math.sqrt(int(n))) ** 2 == int(n): print(0) exit() solve(0) ans.sort(key=lambda x: len(x), reverse=True) for i in ans: if i != '' and int(math.sqrt(int(i))) ** 2 == int(i): print(len(n) - len(i)) exit() print(-1) ```
instruction
0
66,202
20
132,404
Yes
output
1
66,202
20
132,405
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1. Submitted Solution: ``` from math import sqrt def permutate(x): ll = [] if len(x) <= 0: ll.append([]) else: ll = [] candi = permutate(x[1:]) for l in candi: ll.append([x[0]] + l) ll += candi return ll def main(): x = input() n = len(x) perm = [''.join(i) for i in permutate(x) if len(i) > 0] perm = [i for i in perm if not i.startswith('0')] ans = [i for i in [int(j) for j in perm] if sqrt(i) == int(sqrt(i))] if len(ans) > 0: it = max(ans) print(n - len(str(it))) else: print(-1) if __name__ == '__main__': main() ```
instruction
0
66,203
20
132,406
Yes
output
1
66,203
20
132,407
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1. Submitted Solution: ``` from collections import deque d = set() l = dict() for i in range(1, 100000): d.add(str(i ** 2)) n = input() l[n] = 0 q = deque() q.append(n) while len(q) > 0: x = q.popleft() if x in d: print(l[x]) exit(0) for i in range(len(x)): res = x[:i] + x[i + 1:] if len(res) > 0 and res[0] != '0' and res not in l: l[res] = l[x] + 1 q.append(res) print(-1) ```
instruction
0
66,204
20
132,408
Yes
output
1
66,204
20
132,409
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive integer n, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer n to make from it the square of some positive integer or report that it is impossible. An integer x is the square of some positive integer if and only if x=y^2 for some positive integer y. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^{9}). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from n, print -1. In the other case, print the minimal number of operations required to do it. Examples Input 8314 Output 2 Input 625 Output 0 Input 333 Output -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9. In the second example the given 625 is the square of the integer 25, so you should not delete anything. In the third example it is impossible to make the square from 333, so the answer is -1. Submitted Solution: ``` import math def isSquare(x): return int(math.sqrt(x))**2==x def dp(ch,i=0): #print(ch) if(ch=="0"): return "" if len(ch)==1: if isSquare(int(ch)) and ch[0]!="0": return ch else: return "" if ch[0]=='0': return dp(ch[1:],max(i-1,0)) if i>=len(ch): if isSquare(int(ch)): return ch else: return "" if isSquare(int(ch)): return ch else: c=list(ch[::]) c[i]="" c=''.join(c) return max(dp(ch,i+1),dp(c,i),key=len) ch=input() sh=dp(ch) if len(sh)==0: print(-1) else: #print(sh) print(len(ch)-len(sh)) ```
instruction
0
66,205
20
132,410
Yes
output
1
66,205
20
132,411