Try not to use double, probably there is something like ceil(log(n)) = ceil(1.000000000000000000000001) = 2.0 Also in test 6 N is greater than 1.000.000. I used Int and Float data types and got AC Hint: we start from kk = 1, then kk += kk (2), kk += kk (4) but we cannot advance when kk > k. get to the maximum kk with this and the remaining n - kk can be easily calculated with (n - kk) / k + (((n - kk) % k) > 0) #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> int main() { int n, k; scanf("%d %d", &n, &k); if (n < 1 || n > pow(10, 9) || k < 1 || k > pow(10, 9)) { return 0; } unsigned long long result = 0; unsigned long long buffer = 1; n--; for (int i = 0; n >= 0; i++) { n -= buffer; result++; buffer *= 2; if (n <= 0) { printf("%llu\n", result); return 0; } if (buffer >= k) { buffer = k; double count = (double)n / buffer; if (count != (long long)count) { count++; } result += count; printf("%llu\n", result); return 0; } } return 0; } Edited by author 21.01.2020 01:57 Edited by author 23.06.2021 07:43 Edited by author 23.06.2021 07:43 Edited by author 23.06.2021 07:43 The program is already installed in one computer. Now, we need to install it in (N-1) computers. if k > n my program worked with n < 0 you should to consider the case after cycle: if n < 0 then n = 0 Edited by author 03.04.2020 21:51 #include<stdio.h> #include<math.h> int main() { int n,k,i=1,s=1,t=0,temp=0; scanf("%d %d",&n,&k);
while(i<k) { s=s+i; i=2*i; t++;
if(s>=n) break; } while(s<n) { s=s+k; t++; } printf("%d",t); return 0; } while(s<n) { s=s+k; t++; } Isn't here (n-s)/k iterations? #pragma optimize( "g", on ) import sys n,k=map(int,input().split()) ans=0 nn=1 on=1 if n==1: print(0) sys.exit() for i in range(100000000000000000000): if on<=k: nn+=on on+=on ans+=1 elif k<n: nn+=k on+=k ans+=1 if nn>=n: print(ans) sys.exit() i can not understand why optimization my cod if i got ac with you help i give you 1000 rubles Edited by author 17.10.2018 22:35 The reason why you got time limit is that you don't use optimal algorithm. Your algorithm can be even correct, but to pass you need downgrade your algorithm. Maybe you could find some formula that would describe the whole process. Python is not the case here. You would get the same time limit issue with C. I launched your script with N = 10000000 and K = 2 and it took 1.833s on my machine to get the correct answer when the time limit is 0.25s. Of course it will grow up nonlinearly with bigger N. So it would take ages with N = 10^9 (the edge value). $ time echo "10000000 2" | python 1131_bruteforce.py real 0m1.833s user 0m1.824s sys 0m0.007s if i got ac with you help i give you 1000 rubles Edited by author 17.10.2018 22:35 P.S. I don't need your money if you finally solve it :) Edited by author 13.02.2020 06:12 Edited by author 13.02.2020 06:12var n,k,ch,im:int64; begin read(n,k); n:=n-1; ch:=0; im:=1; while n>0 do begin if im<=k then begin n:=n-im; im:=im*2; ch:=ch+1; end; if im>k then begin n:=n-k; im:=im+k; ch:=ch+1; end; end; writeln(ch); end. well, i've also had WA 5, i tried to enter 4 2 and got 3 instead of 2. i found the mistake but then i got WA 14. i tried 6 4 and got 4 instead of 3 and understood that i had corrected the code wrong and only then i got AC. Edited by author 16.01.2020 21:20 Edited by author 16.01.2020 21:20 #include <iostream> using namespace std; int main() { long int n, k; cin >> n >> k; long int a = 1; long int h = 0; if(n == 1){ cout << 0; } else if(n == 2){ cout << 1; } else{ while(n != 0){ if(a > k && a < n){ a = k; n -= a; h++; } else if(a > n){ n = 0; h++; } else{ n -= a; a *= 2; if(a > k){ a = k; } h++; } } cout << h; } } I tried to pick up to my occasion all data types ,but there was some problem ;Then i tried using data type "auto" and it's worked! n, k =map(int, input().split(' ')) z=0 time=0 num = 1 progress = 2 if k==1 and n==1: print(n) elif k ==1: print(n-1) elif k>=n: time+=1 while num<n: num+=progress progress*=2 time+=1 print(time) else: time+=1 while z==0: if progress<=k and num<n: num+=progress progress*=2 time+=1 else: z+=1 progress=k if num<n: while num<n: if num<n: time+=1 num+=progress print(time) Edited by author 16.04.2019 01:34 I can't find mistake, help!! #include <iostream> #include <cmath> using namespace std; int main() { __int64 n,k; int s = 0; cin>>n>>k; int step; step = log(k)/log(2); if(n-pow(2,step+1)>=0) { n -= pow(2,step+1); s = (step+1)+ceil(n*1./k); } else { s = ceil(log(n)/log(2)); } cout<<s; } Formula: ceil(log2(min(n, k))) + ceil((double)(n - (1 << (int)(ceil(log2(min(n, k))))))/ k) Edited by author 29.07.2018 13:01 Edited by author 09.09.2018 17:50 Here is my code: int main() { int N, K; cin >> N >> K; int connect = 1; int temp = N; int largepow = 0; // calculate the largest power of 2 less than K while(temp > 0) { if(pow(2, largepow) < K && pow(2, largepow + 1) < K) largepow++; temp /= 2; } N--; int count = 0; // count the hours while(N > 0) { N -= connect; count++; if(connect < pow(2, largepow)) connect *= 2; else if(connect >= pow(2, largepow) && connect < K) connect++; } cout << count; return 0; } Got WA4, then I tried something else: int main () { long long n, k, exp; cin >> n >> k; for (exp = 0; (1 << exp) < n && (1 << exp) < k; exp++) if ((1 << exp) < n) exp += ((n - (1 << exp) - 1) / k) + 1; cout << exp; return 0; } Test case 4 again. What is wrong? where is my mistake? #include <cstdio> #include <cmath> inline int lb(int n) { return ceil(log(double(n))/log(2.0)); } int main() { int n,k; scanf("%d%d",&n,&k); if (n<=k) printf("%d",lb(n)); else { int ans=lb(k); n-=1l << ans; ans+=n/k; if (n%k) ++ans; printf("%d",ans); } } I got WA6 too...can you please tell me the case? Whoever gets Wrong Answer at 6th test, try to change your way of finding the power of two. My solution got WA#6 when I calculated power of two this way: power = ceil(log(x)/log(2)); Better calculate it by multiplying it. Could you explain, why it is better ? What is a test 6? Whoever gets Wrong Answer at 6th test, try to change your way of finding the power of two. My solution got WA#6 when I calculated power of two this way: power = ceil(log(x)/log(2)); Better calculate it by multiplying it. If n = 1 answer is 0, but accepted solution with answer 1. Why??? |
|