What is wrong in my code regarding Divisors and prime factors? - primes

We have to check if there exists an integer A such that it has exactly X positive integer divisors and exactly K of them are prime numbers
This is my code but it is showing wrong answer after submitting

In codeChef you need to take input in stdin format. Try replacing map(int, input.split()) by
import sys
map(int, sys.stdin.split())

because your code is wrong in case of 5 2
r = 4
then x>4
your code shows ans 1
but real ans is 0

Related

How to store all the inputs in a list using a single command?

I was doing some ds and algo problems from faceprep and a question came:
Amy Farrah Fowler asks her students to give 3 examples for positive odd numbers. When the student specifies a correct answer, his/her score is incremented by 1. When the student specifies a positive even number, his/her score is decremented by 0.5. When the student specifies a negative number, he/she will not be given any more chances to correct his or her mistake and his/her score will be decremented by 1. So a student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number. Some students didn't know the difference between odd numbers and even numbers and they made many mistakes and so it was difficult for her to maintain the scores. She asks for your help. Can you please help her by writing a program to calculate the score?
INPUT & OUTPUT FORMAT:
Input consists of a list of integers.
The output consists of a single line. The score needs to be corrected to 1 decimal place.
[For this exercise, don't worry about duplicate odd numbers. Even if the students specify the same odd number thrice, it is accepted].
input is given as:
1
2
3
4
5
Now How to take input and store it in a variable by single command?
till now I was doing but now number of inputs are different for each case study.
:
a=int(input())
b=int(input())
c=int(input())
I want to do this using a single command so that each input will store in a list.
Try this code snippet to store the input in the list "List_w".
List_w = []
try:
while(1):
List_w.append(int(input()))
except:
print(List_w)
if your input is separated by white space, you can use:
arr = list(map(int, input().split()))
print(arr) # input 1 2 3 4 # output [1,2,3,4]
if your input is based on given range use:
for _ in range(int(input())):
arr = list(map(int, input().split()))
print(arr)
if the inputs separated by new line you can use append method or one liner:
arr = [int(input()) for _ in range(int(input()))]
print(arr)
# input
3
4
5
6 # output [4, 5, 6]
know more about input here https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/

Output division result including digits after decimal - Qt

I am sorry if it's already answered in here, but I couldn't find the exact solution I am looking for.
I am trying to output the result of a division of two variable which is continuously changing. The result may vary from 0.00 to 100.12314235234523 (not exactly this specific, I just wanted to give you an idea). I want to print the result only 2 digits after the decimal point, if there is nothing after decimal, it should print 2 zeroes after decimal.
For example:
10 / 5 = 2.00
23 / 6 = 3.83
I don't need to round up the result for example, if the output is: 73.4869999, I don't need it to be 73.49, 73.48 is fine with me.
What I have written so far is:
packet_loss[2]->setText(QString("%1%2%3").arg(((data.packet_loss_tx) * 100) / data.packets_tx).
arg(locale.decimalPoint()).
arg((((data.packet_loss_tx) * 100) % data.packets_tx), 2, 10, QChar('0')));
But this prints all the values after the decimal point. I can divide this part arg((((data.packet_loss_tx) * 100) % data.packets_tx) with 10, 100 or 1000 to reduce the number of decimals after the decimal point but this is a variable which changes every seconds. So if the output is 3 digits after decimal and I divide it by 10, I will get proper output, but the next value may be 5 digits after decimal and division by 10 will give me 4 digits after decimal. I want the final output to show only 2 digits after decimal.
You could try to use QString::number() function with specific formatting options (two digits precision). For example:
auto s = QString::number(100.12914235234523, 'f', 2); // Gives "100.13"
Besides, if you use floating point numbers, it's better to multiply them with floating point numbers too. I.e. you need to perform your calculations using 100.0 instead of integer value 100.

Regular Expression for Binary Numbers Divisible by 5

I want to write a regular expression for Binary Numbers Divisible by 5.
I have already done the regular expressions for Binary Numbers Divisible by 2 and for 3 but I couldn't find one for 5.
Any suggestions?
(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$ to test it with regexp. See it working here.
You can build a DFA and convert it to regular expression. The DFA was already built in another answer. You can read it, it is very well explained.
The general idea is to remove nodes, adding edges.
Becomes:
Using this transformation and the DFA from the answer I linked, here are the steps to get the regular expression:
(EDIT: Note that the labels "Q3" and "Q4" have been mistakenly swapped in the diagram. These states represent the remainder after modulus 5.)
2^0 = 1 = 1 mod 5
2^1 = 2 = 2 mod 5
2^2 = 4 = -1 mod 5
2^3 = 8 = -2 mod 5
2^4 = 16 = 1 mod 5
2^5 = 32 = 2 mod 5
... -1 mod 5
... -2 mod 5
So we have a 1, 2, -1, -2 pattern. There are two subpatterns where only the sign of the number alternates: Let n is the digit number and the number of the least significant digit is 0; odd pattern is
(-1)^(n)
and even pattern is
2x((-1)^(n))
So, how to use this?
Let the original number be 100011, divide the numbers digits into two parts, even and odd. Sum each parts digits separately. Multiply sum of the odd digits by 2. Now, if the result is divisible by sum of the even digits, then the original number is divisible by 5, else it is not divisible. Example:
100011
1_0_1_ 1+0+1 = 2
_0_0_1 0+0+1 = 1; 1x2 = 2
2 mod(2) equals 0? Yes. Therefore, original number is divisible.
How to apply it within a regex? Using callout functions within a regex it can be applied. Callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching.
However, ndn's answer is more appropriate and easier, therefore I recommend to use his answer.
However, "^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))1)$" matches empty string.

Python Range() for positive and negative numbers

Okay, so I'm trying to write a simple program that gives me both the positive
and negative range of the number given by the user.
For example, if the user gives number 3 then the program should prints out
-3 -2 -1 0 1 2 3
I've tried thinking but just can't think how to get the negative range outputs.
But the code I got below only gives me the positive range outputs, so I was
thinking may I need to do to make it gives me both the positive and negative
outputs.
s = int(input("Enter a number "))
for i in range(s+1):
print i+1
Range() can take two parameters: range(start, end), where start is inclusive, and end is exclusive. So the range you want for 3 is: range(-3, 4). To make it general:
s = int(input("Enter a number "))
for i in range(-s, s+1):
print i

Answering Queries on Binary array

Given a binary array of length <=10^5 and almost equal number of queries. Each query is given by two integers (l,r) for each query we have to computer the total number of consecutive 0's and 1's in the range [l,r].
If n is the length of the array then 1 <= l < r <= n.
For example:
if the binary array (1-indexed) is "011000" and say there are 5 queries:
1 3
5 6
1 5
3 6
3 4
Then the required answer is
1
1
2
2
0
I am aware that this can be solved by a linear time (worst case) algorithm for each query but due to the large number of queries it's not feasible.
Just wondering which is the most efficient way to achieve this?
You can do it with O(n) space complexity and O(log(n)) search time for each query. Calculate the counts for windows of size 1, 2, 4, .... For a given query you can find O(log(n)) windows (at most 2 windows of a particular size), summing which you can find your answer.
As Dukeling said in the comments, you can preprocess in O(n) to compute an array B where B[x] contains the total number of consecutive digits seen in [1..r].
This allows a query in O(1) to find the number of consecutive digits in the range [l,r] by using the array to count the total number in the range [1,r] and subtracting the number in the range [1,l].
Python code:
def preprocess(A):
last=A[0]
B=[0,0]
num_consecutive=0
for a in A[1:]:
if a==last:
num_consecutive+=1
B.append(num_consecutive)
last=a
return B
def query(B,l,r):
return B[r]-B[l]
A=[0,1,1,0,0,0]
B=preprocess(A)
print query(B,1,3)
print query(B,5,6)
print query(B,1,5)
print query(B,3,6)
print query(B,3,4)