Showing posts with label intra aust. Show all posts
Showing posts with label intra aust. Show all posts

Thursday, January 18, 2018

Even the Odds!

Contest link: Intra AUST Preliminary Fall - 17

Problem name: Even the Odds!

Required knowledge: Modular arithmetic, big multiplication.
Editorial: Firstly, try reading this Modular Arithmetic or you can google about modular arithmatic and it's properties. Now let's analyze the problem.

Given and M, we need to find summation of first even numbers modulo and summation of first odd numbers modulo M.
We can observe that the first N odd numbers summation can be found by calculating N * N and first N even numbers summation can be found by calculating N * (N+1).
let, N = 4,

Odd : 1 + 3 + 5 + 7 = 16 = 4 * 4
Even : 2 + 4 + 6 + 8 = 20 = 4 * 5

As the modulo M is really big we can't simply calculate ( (N % M) * ( (N+1)%M) )%M, because the N can be as large as 1018 - 1 and M can be 1018, then at the time of multiplying, it will overflow. How can we do this without overflow? We will simply use a divide and conquer technique to calculate N * N by adding N with N, N times, instead of multiplying them. Let N = 1018 - 1, M = 1018.


Now, from modular arithmatic we know that
- (a * b) % m = (( a%m ) * ( b%m ))%m
- (a + b) % m = (( a%m ) + ( b%m ))%m
Say, = 1018 and b = 10 and m = 1018 - 1.
So, ( (1018 - 1) %1018 ) * (1018 - 1) %1018 ) ) % 1018= ( (1018 - 1) * (1018 - 1) ) % 1018 = 0 ; here (1018 - 1) * (1018 - 1) will cause overflow.
But,(((1018 - 1)%1018) + ((1018 - 1)%1018) + ....

We can easily add (1018 - 1 + 1018 - 1) and then mod it with 1018. We will do this 1018 times, and by using modulo operation each time, there won't be any overflow.
As 1018 is also a very big number, we can do this is O (log10(N)) times with folowing divide and conquer algorithm.


We will use this bigmul function to calculate ( a * b ) % c. We will simply add the number a, b times each with modulo operation. As the range is too big, we can't simply use loop, so we will use this recursion technique of calculating big multiplication in logN time.

Intra AUST Preliminary Round (Senior group) Editorial

Contest link : Intra AUST Preliminary Spring - 17

Diamond lover : Just implement the diamond shape and carefully calculate the spaces between the diamonds.

Easy to solve 1 : From 0 to N, if we X-OR all the integers, we get a binary representation which length is exactly the same length as N. As N is large, using brute force to generate result won't help. We just need to find the binary representation of N and replace every digit with "1". The decimal format of that is the answer.

Strength check :  Just check all the information about the password is whether present in the string or not.

Can you solve it? : It's a bit manipulation problem. If you are familiar with the concept of bit handling it should be easy for you. However, I found a pattern and solved it accordingly. Here is how : Can you solve it?

Longest String : A simple brute force will be enough for the solution. Just implement the code and keep in mind about the alphabetical order.

Can you solve it?

Link: https://www.hackerrank.com/contests/intra-aust-preliminary-round-senior-group/challenges




আমাকে N দেয়া হবে এবং আমার ১ থেকে N পর্যন্ত সব ইন্টিজার এর বাইনারি রিপ্রেসেন্টেশন এ কতটি ১ আছে, তা বলতে হবে। ধরি N = ৮। উপরের চিত্র তে আমি ০ থেকে ৮ পর্যন্ত সংখ্যাগুলির বাইনারি লিখেছি। লক্ষ্য করলে দেখা যায়, এই বাইনারি এর মাঝে একটি নির্দিষ্ট সময় পরপর ১ আসে। প্রথম ঘর এর জন্য একটি ০, একটি ১, এভাবে, দ্বিতীয় ঘরের জন্য দু'টি ০, দু'টি ১, এভাবে... খেয়াল করলে দেখা যায় ব্যাপারটা আসলে ২ এর পাওয়ার হিসেবে আসতে থাকে। আমরা বাইনারি নম্বর এর এই ব্যাপারগুলি অবশ্যি জানি। এখন আমি এই জিনিসটি থেকে একটি প্যাটার্ন বানানোর চেষ্টা করবো।

যেহেতু আমার N জানা আছে, কাজেই প্রথমে N এর বাইনারি বের করে ফেলি। ৮ এর ক্ষেত্রে ১০০০। এখন খেয়াল করি, (ডান দিক থেকে ০ - ৮ সকল নম্বর) প্রথম ঘরে, ০ থেকে ৮ এর মাঝে ১ আছে মোট ৪ টি। দ্বিতীয় ঘরেও ৪ টি, তৃতীয় ঘরেও ৪ টি, চতুর্থ ঘরে ১ টি। মোট ১৩ টি ১ আছে। প্রতি ঘরের জন্য এই ১ এর কাউন্ট টা কিভাবে করবো ? কিছু টেস্টকেস চিন্তা করলে ব্যাপারটা সবার ধরতে পারার কথা। প্যাটার্ন বের করতে পারলে ভালো, নাহলে চিত্রের বামদিকে আমি আমার প্যাটার্ন এর ফরমুলা লিখে দিয়েছি। এখানে xtra জিনিসটি একটু ইম্পরটেন্ট। নরমালি আমরা ১,২,৪,৮,১৬... ২ এর পাওয়ার এর ব্যাপারগুলি সহজে প্যাটার্ন এ ফেলেতে পারি, কিন্তু যদি ৯,১৩,২১,...এরকম নম্বর থাকে, যেটা আসলে কমপ্লিট প্যাটার্ন এর মধ্যে পরে না, তাদের জন্য বারতি কিছু যোগ এর দরকার হয়। ৯ এর জন্য চিন্তা করলে এমনটা দেখা যাবে। যাহোক, এখানে ব্যাপারটা হল N এর বাইনারির length পর্যন্ত আমাকে লুপ চালায়ে আর কিছু ক্যালকুলেশন করে টোটাল কতটি ১ আছে তা বের করতে হবে।