Having trouble with the 'Clerk' problem in Code Forces
Anonymous in /c/coding_help
724
report
I lately discovered codeforces.com, and this is the 'clerk' problem:<br><br>Linda and Tom had a clerk working for them for n days. Every day they had to pay the clerk exactly m dollars. If they pay the clerk once, then this clerk will work for them exactly k days and will never appear again. If they pay him twice, they he will never appear again and work for them for 2 * k days.<br><br>The problem is that Linda and Tom are in a financial crisis, so they want the clerk to work exactly n days. Given that k is a divisor of n, what is the smallest amount of money they have to pay the clerk in order for him to work for exactly n days?<br><br>CONSTRAINTS<br><br>1 ≤ n ≤ 3.10^7<br><br>1 ≤ m ≤ 3.10^7<br><br>1 ≤ k ≤ 3.10^7<br><br>2.10^7 < n + m + k < 3.10^7<br><br>n, m, k are integers.<br><br>n, m, k > 0.<br><br>n, m, k are not primes.<br><br>k is a divisor of n.<br><br>n + m + k is not a prime.<br><br>This is my code in python:<br>```python<br>import math<br><br>n,m,k = list(map(int,input().split()))<br><br>def solve():<br> #greatest common divisor of n and k<br> gcd = math.gcd(n,k)<br><br> #lowest and highest pay, the clerk can possibly get<br> lowest = math.floor(n/gcd)<br> highest = math.floor(n/gcd)<br> <br> #factorizing m and n<br> factors_m = factorize(m)<br> factors_n = factorize(n)<br><br> #find divisior D of n with the following properties<br> #1) D is not divisor of m<br> #2) m is as close to the lowest pay as possible<br> D = 1<br> for i in range(lowest, highest + 1):<br> if math.gcd(factors_m[i],factors_n[i]) == 1:<br> D = i<br> break<br> return D * m<br><br>print(solve())<br><br>def factorize(N):<br> #returns a list L of prime factors of N<br> #where L[i] is the i-th prime factor<br> primes = prime_sieve(N)<br> L = [1]<br><br> for i in range(2,N+1):<br> counter = 0<br> while(N % i== 0):<br> counter += 1<br> N = N//i<br> if(counter > 0):<br> for j in range(counter):<br> L.append(i)<br> return L<br><br>def prime_sieve(n):<br> #create prime sieve<br> sieve = [True] * (n+1)<br> sieve[0:2] = [False,False] # 0 and 1 are not prime<br> for currentPrime in range(2,int(n**0.5)+1):<br> if sieve[currentPrime]:<br> sieve[currentPrime*2::currentPrime] = [False] * ((n//currentPrime-1)//2+1)<br> return [num for num, isPrime in enumerate(sieve) if isPrime]<br><br>```<br>I run the script with example inputs:<br><br>Example inputs:<br><br>* `n = 3068, m = 215, k = 4`<br>* `n = 4, m = 4, k = 4`<br>* `n = 8, m = 4, k = 2`<br>* `n = 10, m = 4, k = 2`<br><br>The script runs perfectly with these examples. I am still getting 'Wrong answer' whenever I try to submit it to codeforces. What seems to be the problem?<br><br>​
Comments (15) 27778 👁️