### Egor's blog

By Egor, 7 years ago, translation,

Hi!

I can't solve this зproblem. My code:

#include <iostream>

using namespace std;

int main() {
int a, b;
cin >> a >> b;
int ans = 0;
for (int i = 0; i < a; i++) ans++;
for (int i = 0; i < b; i++) ans++;
cout << ans << endl;
return 0;
}


• +1170

 » 7 years ago, # | ← Rev. 2 →   +512 A simple 3D segment tree does the job.
•  » » 7 years ago, # ^ |   +16 I think the judge should have problem I tried segment tree but couldn't accept.
•  » » 7 years ago, # ^ | ← Rev. 2 →   +31 It took me 4D segment tree to solve this. How did you solve it with 3D :o ?
•  » » 7 years ago, # ^ | ← Rev. 2 →   +7 Egor please don't cry :( You can solve it by using Z-rule or Pythagoran theorem.
•  » » 7 years ago, # ^ |   +8 424 .Record?
•  » » » 7 years ago, # ^ |   +6
•  » » » » 7 years ago, # ^ | ← Rev. 2 →   0 but yet (over 450)
•  » » » » » 7 years ago, # ^ | ← Rev. 2 →   +13 EDIT: Now it is! Congratulations!
•  » » » 7 years ago, # ^ | ← Rev. 2 →   0 Sorry. Double post
•  » » » » 7 years ago, # ^ |   -38 Not yet
•  » » » » » 7 years ago, # ^ | ← Rev. 2 →   -36 Not yet
•  » » » » » » 7 years ago, # ^ |   -20 Not yet
•  » » 7 years ago, # ^ |   0 -1
•  » » 6 years ago, # ^ |   0 I did the same thing. Got 13 WA bcoz of '<' instead of '<=' on line 2374. Finally AC!!! :)
 » 7 years ago, # |   +30 change the ints to long long and it will pass i guarantee it !!! says this with thumbs up
 » 7 years ago, # |   +91 Look, you are a newbie. This world of Codeforces is really very tough for newbies. Sorry to say that :(Work hard. You will succeed here. Though it is gonna be tough. :P
•  » » 7 years ago, # ^ |   +26
 » 7 years ago, # |   +151 I think you should read my new book: How to Get 2700+ Rating on Codeforces in 10 Days
 » 7 years ago, # |   +39 You must read this :)
 » 7 years ago, # | ← Rev. 2 →   +86 ugh newbies these days....theres an easier way to do this #include #include #include int mein(){ int tequila,vodka; int drunk==0; f>>tequila>>vodka; drunk=tequila-vodka; cout<
•  » » 7 years ago, # ^ |   0 Not compiled!
•  » » 7 years ago, # ^ |   +23 Go drunk , you're home...
 » 7 years ago, # | ← Rev. 5 →   +35 Egor it seems that you need to make training like cgy4ever I expect you will be better in the near future I discovered a lot of guys but this time really I believe that you someday will be good and I'm sure you will be red. anyway If you need any kind of help in any hard problems just tell me. I will make training soon for some Beginners if you are interested you can join :P
•  » » 7 years ago, # ^ |   +2 I want to join to.Do you grantee to get a good result after 10 days?;;)
•  » » 7 years ago, # ^ |   +5 after training two days you become Green, Congrats
 » 7 years ago, # |   +86 My solution also gives TLE, I think I should try memoization: #include using namespace std; int compute(int n){ if(n<3) return n; return 2*compute(n-1) - compute(n-2); } int main() { int a,b; cin >> a >> b; cout << compute(a)+compute(b) << endl; } 
 » 7 years ago, # |   +4 I like your sense of humor, Egor. It sure makes me laugh (although I am a poor newbie as well :P)
 » 7 years ago, # | ← Rev. 10 →   +119 Indeed, it's a tough problem for your level. I think it can be solved by defining the addition operation through Lambda Calculus.First, let's define Natural Numbers. Let's say that a number x is a function that given another function y and a variable z, applies y on z for x times.0 ::== λx.λy.z 1 ::== λx.λy.x y 2 ::== λx.λy.x x y ...So, let's define the addition operator. It would simply be: add a b ::== a λx.λy.x b x y (Remember: a, b are natural numbers, so they are just functions.)I'll leave the easy work of handling negative numbers, thinking about how to relate that to code, .. etc, to you as a kind of practice.One last note: I really didn't try to learn these things, so I'm probably speaking nonsense. But a newbie like you wouldn't tell.
 » 7 years ago, # | ← Rev. 2 →   0 I have this problem: http://www.spoj.com/problems/TESTI'm thinking about an approach using Suffix Tree, Splay Tree, Dynamic convex hull, Max flow using pre-flow push with global heuristic. But I still don't have any good solution. Can anyone help me please :(
•  » » 7 years ago, # ^ |   +8 Poor ! You could have done it with Heavy Light Decomposition. :p
•  » » » 7 years ago, # ^ |   +8 Uff these newbies are always irritating us international grandmaster ( I always wanted yo say this ) :D
•  » » » » 7 years ago, # ^ |   0 Y u so cocky!:)
•  » » » » » 7 years ago, # ^ |   +8 Performed poorly in last contest :( straight newbie from international grandmaster :(
•  » » 7 years ago, # ^ | ← Rev. 2 →   +11 Let IGM help you :)  while(cin>>n){ if(n==42)break; else cout<
•  » » » 7 years ago, # ^ |   +21 I submitted your code with out any changes but get compilation error. :|
 » 7 years ago, # |   0 It's so hard for you to solve this problem... :|You should practice hard to do that... :|
 » 7 years ago, # | ← Rev. 2 →   0 Once mathlover told me this problem can be solved by using network flow. I think the approach newbie mathlover told us can be understood easily by us poor newbies.
 » 7 years ago, # | ← Rev. 3 →   0 Interesting... I really enjoy this problem but since I am only an expert, I am looking forward to opinions from these explosions of International Grandmasters, (I honestly don't know how).Your code runs in linear time, with a constant factor of one. The constant factor however can be reduced to half with two simple base cases by doing the following:int main() { int a, b; cin >> a >> b; int ans = 0; if (a == 1) { cout << b + 1 << endl; } else { cout << a + 1 << endl; } for (int i = 0; i < a; i += 2) ans += 2; for (int i = 0; i < b; i += 2) ans += 2; cout << ans << endl; return 0; }
 » 7 years ago, # |   +18 It requires complex bit manipulation. So, I think it might be too tough for your level. First work on easier problems and build your level up to this. Anyway, here is my AC solution for your reference. int add(int a, int b) { if (!b) return a; return add(a^b, (a&b) << 1); } int main() { clock_t startTime = clock(); ios_base::sync_with_stdio(false); int a, b; cin>>a>>b; cout<
 » 7 years ago, # |   +45 Your solution is O(a+b) which is quite slow , you can use a technique similar to Exponentiation by squaring so it will become O(log(a)+log(b)), also it's better if you use fast input/output methodshere's the code #include #include using namespace std; int a,b,ans=0; int main(){ scanf("%d %d",&a,&b); int p=1; while(a>0){ if(a%2){ ans+=p; } a/=2; p*=2; } p=1; while(b>0){ if(b%2){ ans+=p; } b/=2; p*=2; } printf("%d\n",ans); } 
 » 7 years ago, # |   +27 Your algorithm is O(a+b). You can easily make it O(min(a,b)). Here is how to do it: #include #include using namespace std; int main() { int a, b; cin >> a >> b; if(a>b) swap(a,b); int ans = b; for (int i = 0; i < a; i++) ans++; cout << ans << endl; return 0; } Also, you should use faster IO methods.
•  » » 7 years ago, # ^ | ← Rev. 2 →   +13 It's also provable that O(a+b) and O(min(a,b)) are both the same thing, but this requires some deep analysis. For the problem, it's a classic application of FFT. Maybe you may want to chose an easier problem to get you started.
 » 7 years ago, # |   +10 Did you try to solve it for small a and b? It must be a pattern there, and then precalc does the matter.
 » 7 years ago, # |   +12 The answer is trivial . 1- Swap a and b2- find the largest a*b digit prime that can be expressed only by powers of 2 ;) eg 7 = 2^3 — 2^0 3- Now multiply that prime number by a|b 4- print a+b
 » 7 years ago, # |   0 I have been searching for a solution to this problem for over a year.. And today I found a solution thanks to google. However it was too complicated and I couldnt prove it. Please help, heres the code: #include using namespace std; int main() { int a, b; cin >> a >> b; cout << a+b << endl; return 0; } What does this '+' magical symbol does?
•  » » 7 years ago, # ^ |   +28 This code uses a special greedy algorithm that fails for large cases; In that case we must build complex data structure (the code is too long for me to post here) to fix it.
•  » » » 7 years ago, # ^ |   0 But i tried to submit it. It got accepted
•  » » » » 7 years ago, # ^ |   +57 yes probably test cases are weak to promote hacks
 » 7 years ago, # | ← Rev. 2 →   0 No no. You're wrong. You should solve it in a clear recursively way like this: #include using namespace std; typedef long long ll; int sum(int x,int y) { if (x==0) return y; else return sum(x-1,y)+1; } int main() { ios_base::sync_with_stdio(0); int a,b; cin >> a >> b; cout<< sum(a,b) <
•  » » 7 years ago, # ^ |   +8 I getted compail errer with youre code.  File "main.py", line 2 using namespace std; ^ SyntaxError: invalid syntax Do youre code is rially collect? Pleeze fix me:)
•  » » » 7 years ago, # ^ | ← Rev. 2 →   0 Oh! Thanks for reminding me!Pay attention to this point:You should notice you can add two values recursively only with C++. In other languages, it is much more harder!
 » 7 years ago, # | ← Rev. 4 →   +12 your code is too complicated to understand . as a newbie coder i suggest you to follow the way you actually do computation in mind . how do you add in mind ? obviously by memorizing the last state. so here is the best thing for newbies :D #include using namespace std; int mem[1000][1000]; int a,b; int dp(int x,int y) { if(x<0 || y<0) return 0; if(x==0) return y; if(y==0) return x; int &ret=mem[x][y]; if(ret!=-1) return ret; ret=0; ret+=dp(x-1,y-1)+2; return ret; } int main() { cin>>a>>b; memset(mem,-1,sizeof(mem)); cout<
 » 7 years ago, # | ← Rev. 3 →   +15 Egor Lol :P
•  » » 7 years ago, # ^ | ← Rev. 4 →   0 del
•  » » 7 years ago, # ^ |   0 congrats karan :D
•  » » » 7 years ago, # ^ |   0 lol ayush :P :P jokes apart,i have a lot to learn :)
•  » » » » 7 years ago, # ^ |   -8 You have the best rating I've ever seen(1001 !! That's great!!) Seriously, why do you want to be red??
•  » » » » » 7 years ago, # ^ |   0 He will be red On 11th January
•  » » » » 7 years ago, # ^ |   +3 I guess we have some misunderstandings here. I wasn't joking in my last comment... I LOVE palindromes and 1001 is the first non-obvious palindrome number! (Contains even number of digits and has at least two different digits.) From my point of view, it's one of the best ratings! :)
 » 7 years ago, # | ← Rev. 3 →   +6 there is my code for this problem, it works for 1<=a,b <=1000 RGHOSTMEGAi will try to type code solving for constraints up to 10000!
 » 7 years ago, # |   +148 I think the hint is misleading, you shouldn't use '+'. This code gets AC and has no '+' at all: #include int main() { long long a, b; std::cin >> a >> b; for (int i = 10000 ; i ; i--) { long long x = 1LL << (rand() % 33); if (a & x) if (b & x) if (!(a & (2*x))) { a ^= x ; b ^= x ; a ^= 2 * x ; continue; } if (a & x) if (b & x) if (!(b & (2*x))) { a ^= x ; b ^= x ; b ^= 2 * x ; continue; } if (b & x) if (!(a & x)) { a |= x; b ^= x ; continue; } } std::cout << a; } 
•  » » 7 years ago, # ^ |   +172 #include int main() { long long a, b; std::cin >> a >> b; std::cout << a - -b; } But yours much easier to understand
 » 7 years ago, # |   0 You're asked to use the + operator. This is why I think the following link may be useful:http://stackoverflow.com/a/19412942You may need to make a few insignificant changes if this program wouldn't strictly conform to the required input specification.
 » 7 years ago, # | ← Rev. 2 →   +116 I watched some lectures about randomized algorithms from Burunduk1 recently, and i tried his usual recipe — as everybody knows, it works for every problem... #include using namespace std; int main(){ int a,b,res; cin>>a>>b; while (true) { res=rand(); if (res-b==a) { cout<
•  » » 7 years ago, # ^ |   -20 Chuck Norris can get AC with this code
•  » » » 7 years ago, # ^ |   0
•  » » » » 7 years ago, # ^ |   0 - - -
•  » » 7 years ago, # ^ |   0 Wow such an elegant solution!
•  » » 7 years ago, # ^ |   +70 You just forgot to shuffle the input.
 » 7 years ago, # |   0 Same problem hereit took 2 hrs of coding but nothing at the end :(I'll keep trying, I'll teach you if i got accepted
 » 7 years ago, # | ← Rev. 2 →   +65 It is easy to guarantee constant time lookup: #include int main() { int a,b; cin >> a >> b; if(a==0 && b==0){ cout << 0 << endl; } if(a==0 && b==1){ cout << 1 << endl; } if(a==0 && b==2){ cout << 2 << endl; } if(a==0 && b==3){ cout << 3 << endl; } if(a==0 && b==4){ cout << 4 << endl; } if(a==1 && b==0){ cout << 1 << endl; } if(a==1 && b==1){ cout << 2 << endl; } if(a==1 && b==2){ cout << 3 << endl; } if(a==1 && b==3){ cout << 4 << endl; } if(a==1 && b==4){ cout << 5 << endl; } if(a==2 && b==0){ cout << 2 << endl; } if(a==2 && b==1){ cout << 3 << endl; } if(a==2 && b==2){ cout << 4 << endl; } if(a==2 && b==3){ cout << 5 << endl; } if(a==2 && b==4){ cout << 6 << endl; } if(a==3 && b==0){ cout << 3 << endl; } if(a==3 && b==1){ cout << 4 << endl; } if(a==3 && b==2){ cout << 5 << endl; } if(a==3 && b==3){ cout << 6 << endl; } if(a==3 && b==4){ cout << 7 << endl; } if(a==4 && b==0){ cout << 4 << endl; } if(a==4 && b==1){ cout << 5 << endl; } if(a==4 && b==2){ cout << 6 << endl; } if(a==4 && b==3){ cout << 7 << endl; } if(a==4 && b==4){ cout << 8 << endl; } } I think you can easily see how to extend this :)
•  » » 7 years ago, # ^ |   0
•  » » 7 years ago, # ^ |   +3 brilliant! In fact you can extend this to negative numbers by adding in a simple if statement! Still constant time lookup and constant memory! Best if all only logic operaters!
•  » » 7 years ago, # ^ |   +8 Calm down dude ... He's still a newbie ... ! you should explain more ... !
•  » » 7 years ago, # ^ |   0 Hey VeniVidiVici, did you generate this code?? or simply you write it?? because it looks like a lot of implementation, and it is highly probably that red coders hack this solution, even when the test cases are hard to see...
•  » » 7 years ago, # ^ | ← Rev. 2 →   +5 An IGM told me this is not O(1) but O(ab). However I'm a poor newbie and don't know why.
 » 7 years ago, # | ← Rev. 2 →   0 It seems you have chosen a very hard problem. You must try another problem easier than this... You are good enough this problem is so hard don't cry.
 » 7 years ago, # | ← Rev. 2 →   +26 anyway, try to solve it before participating in TCO 2015 Final Round because it's a very important classic problem :D
 » 7 years ago, # |   -15 How would I solve this hard problem!?!?!?! I am beginner to coding.
 » 7 years ago, # | ← Rev. 2 →   0 Wow this problem is quite impressive because I have seen in all online judge I have been. I don't know any aproach to attack this kind of problem, good luck Egor
 » 7 years ago, # |   0 Change your rating back, maybe it's the problem.
 » 7 years ago, # | ← Rev. 2 →   +25 After a day of thinking, I realized that this was a simple binary search exercise: #include using namespace std; int main() { int a, b; int ans; cin >> a >> b; int l = 0, r = 1 << 30; while (l + 1 < r) { int mid = (l + r - 1) / 2; if (mid == a + b) ans = mid; else if (mid < a + b) l = mid + 1; else r = mid + 1; } } Of course to be safe, at the end you can add a check to see if this is the right sum by doing if (ans == a + b) ... Oh wait a minute.
•  » » 7 years ago, # ^ |   0 You only forgot to compile it!
•  » » » 7 years ago, # ^ |   0 Well that is left as an excersise to the reader!
 » 7 years ago, # | ← Rev. 4 →   +11 you could solve it with binary index trees in o(log(a+b)) #include #define ll long long unsigned int using namespace std; ll maxn=100000; ll bt[100000]; void update(ll ind,ll v){ while(ind0){ ans+=bt[ind]; ind-=(ind&-ind); } return ans; } int main(){ ll a,b; cin>>a>>b; update(a,a); update(b,b); cout<
 » 7 years ago, # |   +58 finally accepted with this code. :D
•  » » 7 years ago, # ^ |   +3 great work the easiest solution I have ever seen :D
•  » » 7 years ago, # ^ |   +10 Tears of joy in my eyes...The future of Earth is on your shoulders
 » 7 years ago, # |   +20 I know all of you are alien machine of gods' from 57Ds outspace ,Gods wants all of you to maitain the society of 3Ds,not disturb the 3Ds world,just keep it,not doing crime!!!!!!
 » 7 years ago, # |   +22 Congratulation for being Specialist from Newbie. No doubt that, you have tried hard for this rating change. Try more. If you rightly do your work, sure, you will be able to solve this hard problem in 8 days. :P
•  » » 7 years ago, # ^ |   +78 I bet he will become International Grandmaster after 10th of January
•  » » » 7 years ago, # ^ |   +6 Ah yes, in that case he must read my book
•  » » » 7 years ago, # ^ | ← Rev. 2 →   0 Yep... He is working hard. I'm sure he will continue this & he is gonna attend in Round 285 being International Grandmaster . Great inspiration for new coders !!!
•  » » » 7 years ago, # ^ |   0 It turned out to be true... How did you know?
 » 7 years ago, # |   -10 Finally I did it: #include #include #include using namespace std; int main() { long long int first,second; char a[128],b[128],c[128]; int asz,bsz,curr,t=0,i; scanf("%lld %lld", &first, &second); sprintf(a,"%lld",first); sprintf(b,"%lld",second); reverse(a,a+strlen(a)); reverse(b,b+strlen(b)); while(strlen(a)
 » 7 years ago, # |   -10 Wow... You improved... You are specialist now...
• »
»
7 years ago, # ^ |
-70

try this one :

# include <bits/stdc++.h>

typedef long long ll;

# define pb push_back

using namespace std; vector v; int ans=0; int main() { int a,b; cin>>a>>b; for(int i=0;i<a;i++) v.pb(1); for(int i=0;i<b;i++) v.pb(2); for(int i=0;i<v.size();i++) { if(v[i]==1) ans++; if(v[i]==2) ans++; } cout<<ans; return 0; }

 » 7 years ago, # | ← Rev. 2 →   0 Egor, you must be enjoying this :D Plot twist: he can't solve the problem for real (:ok je sors:).
 » 7 years ago, # |   -12 Now Egor is green! Did you solve this problem?
 » 7 years ago, # |   +102 Hi Egor. I think your code is ok for nonnegative values, but it fails for negative ones!
 » 7 years ago, # | ← Rev. 2 →   +56 ur problem looks very similar to this problem. u should just submit the same solution 5 times, it will give u WA. when u submit 6th time, u will get AC! :)
 » 7 years ago, # | ← Rev. 5 →   -14 .-. LOL very hard problem.This is a very complex MST problem. It took me two days to reach the conclusion that I can do it like this: Nodes A and B are connected with a total weight of A+B This is my AC code: #include #include #include #include using namespace std; int a,b; vector >EdgeList; vector pset(1000000); void initSet(int N) { for(int i=0;i<=N;i++) pset[i] = i; } int findSet(int i) {return (pset[i] == i) ? i : (pset[i]=findSet(pset[i])); } void unionSet(int i, int j) {pset[findSet(i)] = findSet(j); } int isSameSet(int i, int j) {return findSet(i) == findSet(j); } int main() { ios::sync_with_stdio(false);cin.tie(0); while(cin>>a>>b){ initSet(max(a,b)+1); EdgeList.clear(); EdgeList.push_back(make_tuple(a+b,a,b)); sort(EdgeList.begin(),EdgeList.end()); int mst=0; for(tuple u:EdgeList){ int w,a,b; tie(w,a,b)=u; if(!isSameSet(a,b)){ unionSet(a,b); mst+=w; } } cout<
 » 7 years ago, # |   +5 Congrats Egor, now you're "Expert" :)) I think you have solved this problem finally, have you? :D
 » 7 years ago, # |   +79 Egor must be truly great rising star of competitive programming since he became blue from grey in such a short period of time! Even lack of contests wasn't an obstacle for him!
 » 7 years ago, # |   -21 One thousand upvotes. Wow.
•  » » 7 years ago, # ^ |   -7 Now a good number of votes
 » 7 years ago, # |   +16 Solution in 20+ languages https://www.hackerrank.com/challenges/solve-me-first :D
•  » » 7 years ago, # ^ |   +3 Brainfuck language :)))
•  » » » 7 years ago, # ^ |   +3 if u think that's big, try solving the problem in Whitespace! :)
•  » » » » 7 years ago, # ^ |   +8 There's a whole bunch of them.My personal favourite is ArnoldC :D IT'S SHOWTIME TALK TO THE HAND "hello world" YOU HAVE BEEN TERMINATED 
 » 7 years ago, # | ← Rev. 7 →   -13 //You can Use this! #include using namespace std; #define ll long long int main() { ll a, b; cin >> a >> b; ll ans = 0; for (ll i = 0; i < a; i++) ans++; for (ll i = 0; i < b; i++) ans++; cout << ans << endl; return 0; } 
 » 7 years ago, # |   +56
 » 7 years ago, # |   +34 it prints the answer for x , y printf("%d",printf("%*c%*c", x,'\r', y, '\r')); 
•  » » 7 years ago, # ^ |   -11 include typedef long long ll; define mp make_pair define pb push_back using namespace std; vector v; int ans=0; int main() { int a,b; cin>>a>>b; for(int i=0;i
 » 7 years ago, # |   +3 It Seems like This post gonna be on Top till next Christmas :D
 » 7 years ago, # |   +8 Use ++i and ++ans.
 » 7 years ago, # | ← Rev. 3 →   +9 Congratulation Egor !!! You are in div 1 now !!! Wish you best luck.
•  » » 7 years ago, # ^ |   +8 He's improving too soon too fast ... ! :D
•  » » 7 years ago, # ^ |   +10 I was so confused! His rating is increasing without any contest!!!
•  » » » 7 years ago, # ^ |   +11 this is the magic :P
•  » » » » 7 years ago, # ^ | ← Rev. 2 →   +16 New year's miracle ... ! \:D/
 » 7 years ago, # |   +23 you are Candidate master now.Now try once again.
 » 7 years ago, # |   +11 International master ... ! way to go Egor ... ! :D
•  » » 7 years ago, # ^ |   -15 Egor You are crazy !
 » 7 years ago, # |   -35 Why you posting a blog for vain Threads.:| you are crazy !
•  » » 7 years ago, # ^ |   -45 Я хочу поздравить всех пользователей интеллектуальной игры "Clash Of Clans" стем что вы нашли и играете лучшую игру в мире! Поздравляю
 » 7 years ago, # | ← Rev. 2 →   -33 .
 » 7 years ago, # |   -28 I'm egor ! #include using namespace std; int main() { int a, b; cin >> a >> b; int ans = 0; for (int i = 0; i < a; i++) ans++; for (int i = 0; i < b; i++) ans++; cout << ans << endl; return 0; } 
»
7 years ago, # |
-22

 » 6 years ago, # |   +113 #good_old_times 
 » 19 months ago, # |   0 It is work!!! var a,b,c:string; a2,b2,c2:array[0..1000] of longint; i,j:longint; begin readln(c); a:=c.Split(' ')[0]; b:=c.Split(' ')[1]; for i:=0 to length(a)-1 do a2[i]:=ord(a[length(a)-i])-ord('0'); for i:=0 to length(b)-1 do b2[i]:=ord(b[length(b)-i])-ord('0'); for i:=0 to 999 do begin c2[i]:=c2[i]+a2[i]+b2[i]; c2[i+1]:=c2[i] div 10; c2[i]:=c2[i] mod 10; end; i:=1000; while(c2[i]=0)do i:=i-1; for j:=i downto 0 do write(c2[j]); end. `