Showing posts with label Primes. Show all posts
Showing posts with label Primes. Show all posts

Wednesday, August 24, 2016

Codeforces Round #365 (Div. 2) - E. Mishka and Divisors

Problem Statement:
http://codeforces.com/contest/703/problem/E

Summary:
Given an array of n <= 1000 integers a[i] <= 10^12, and an integer k <= 10^12. Find subset of the array which product of the elements is divisible by k, such that the size of the subset is minimised. If more than one such subsets exist, return the one with smallest sum over the elements.

Monday, July 18, 2016

Codeforces Round #360 (Div. 1) - B. Remainders Game

Problem Statement:
http://codeforces.com/contest/687/problem/B

Summary:
Given integer n, k < 1M, and n numbers c[1], ..., c[n] < 1M, decide whether we can always guess for all unknown integer x, what x mod k is if x mod c[i] is known for all i.

Wednesday, July 22, 2015

Codeforces 547C - Mike and Foam

Problem Statement:
547C - Mike and Foam

Solution:
The most important observation: since \( a_i <= 5 \times 10^5 \), the maximum number of distinct prime factors \(a_i\) can have is 6, since \(2 \times 3 \times 5 \times 7 \times 11 \times 13 = 30030 < 5 \times 10^5\) while \( 2 \times 3 \times 5 \times 7 \times 11 \times 13 \times 17 = 510510 > 5 \times 10^5 \). This allows us to perform a fundamental counting principle called inclusion-exclusion principle.

Thursday, February 5, 2015

Codeforces Round #290 (Div. 1 C / Div. 2 E) - Fox and Dinner

Problem Statement:
512C - Fox and Dinner

Solution:
I've always been fascinated with maximum flow problems. The author to this problem is brilliant, and his official editorial is very well written, succinct and sweet. This post is mostly for my future reference, but feel free to read through the discussion :)

We can certainly identify that this problem is a matching problem, but what exactly is the type of matching we are interested in? On the first glance it seems to require some notion of tables, in which each table must contain at least 3 elements. That sounds like a random constraint, but as it turns out, a pretty fundamental observation to this problem can leads to a very clever representation of the problem that can easily enforce the constraints described in the problem statement.

Tuesday, February 3, 2015

Codeforces Round #290 (Div. 1 B / Div. 2 D) - Fox and Jumping

Problem Statement:
512B - Fox and Jumping

Solution:
The idea to solve this problem is pretty simple (but the DP is not :P): Find the lowest cost combination of cards for which their greatest common divisor is 1. The official editorial has succinctly explained the DP approach in merely a few lines, check it out!

The proper DP approach is to do a bitmask on prime factors, which will give a running time of \(O(2^k N^2)\), where k is at most 9, since there can at most be 9 different prime factors in any integer less than \(10^9\). How does it work?

Sunday, June 22, 2014

a bit of codechef: Prime Polindromes

Problem Statement:
CodeChef - Prime Polindromes

Summary:
Given an integer \(N\), find the smallest integer \(M \geq N\) such that \(M\) is a palindrome and prime number. Limit: \(1\leq N \leq 10^6\).

Solution:
It is considered an easy problem in CodeChef (Oh my God..) and actually it's not that easy. At first it is tempting to try one by one palindromes \(P\) which is bigger than \(N\) by checking for each whether it is prime. That means for each \(P\), we have to check that \(2,3,4,5,\ldots ,P-1\) do not divide \(P\), this will take \(O(P^2)\) time, and it is bad since the limit for \(N\) is \(10^6\). Even with the fact that we just need to check until \(\sqrt{P}\), this approach will definitely result in a TLE (Time Limit Exceeded). So?

Certainly we have to choose different strategy, a different perspective. What if, we generate all primes we need beforehand? We can construct an array \(A\) consisting of around 1 million elements of 0 or 1, such that if \(i\) is a prime number, \(A[i] = 1\), and 0 otherwise. As such, if we want to check whether \(P\) is a prime, we just need to get the value of \(A[P]\), hence \(O(1)\) for the primality check. This is a dramatic decrease from our initial \(O(P^2)\)!

How can we generate all primes up to 1 million++? This is where this discussion about Sieve of Eratosthenes becomes useful. By running the algorithm to an appropriate value of \(i\), we will generate all primes needed. This will take at most \(N \times (\frac{1}{2}+\frac{1}{3}+\ldots + \frac{1}{N}) = O(N \log{N})\), which means there are around \(10^6 \times \log{10^6} = 6\times 10^6\) computations to generate the primes, quite manageable.

Finally, we just need to observe that a palindrome with even-numbered digit will never be prime, which can be proven quite easily:
Let \(P = a_0a_1...a_{k-1}a_ka_ka_{k-1}...a_1a_0\) be a palindrome with an even total digit. From here we have quite a few ways to proceed, but the easiest would be to take \(\bmod{11}\) of the palindrome, and you'll observe that \(a_i\) will have alternating sign \(\bmod{11}\), which eventually will cancel out and leads to \(P \equiv 0 \bmod{11}\).

Making use of all the information, here is one possible, and rather messy implementation of our solution:

#include <cstdio>
#include <cmath>
//N must have odd digit, if even digit then it's divisible by 11
//N should be less than 10^7
//assume there exist prime of form 1.....1
//generate all primality from 1 to <1020^2
bool prime[1009050];

int pow_10(int n){
   int ret=1;
   for(int i=0;i<n;++i)
      ret*=10;
   return ret;
}

int main(){
   int N;
   scanf("%d",&N);
   for(int i=0;i<=1009050;++i)prime[i]=1;//initialization
   for(int i=2;i<=1020;++i)
      for(int j=2;j<=1009050/i;++j){
         prime[i*j]=0;
      }
   int temp=N,d=0;
   while(temp!=0){ //finding num of digit of N
      ++d;
      temp/=10;
   }

   int s; //starting point of trial and error
   if(d%2==1)s=N/pow_10(d/2);
   else s=pow_10(d/2);
   while(1){ //generating palindromes... messy but correct
      int t=s,e=0;
      while(t!=0){++e;t/=10;}
      int K=s*pow_10(e-1);
      for(int i=1;i<e;++i){
         K+=(s/pow_10(i))%10*pow_10(e-1-i);
      }
      if(prime[K] && K>=N){
         printf("%d\n",K);
         break;
      }
      ++s;
   }
   return 0;
}

Wednesday, May 14, 2014

a bit of array

Algorithm attempts to solve problems through manipulation of data structures, hence data structure and algorithm go hand in hand. They are like Batman and Robin, only less gay (just saying).

One of the most fundamental data structure in any programming platform is array. When we initialize an array, especially in case of static array, we are allocating a fixed amount of blocks of memory to a certain data type, which are accessible by indicating the specific index of the array. The strength of an array structure is that by knowing the index, access operation on arrays takes O(1) time. However, since the size of arrays have to be known before use, there are chances of excessive unused memory allocated which results in lower performance.

Here is one interesting manipulation of array. Consider a problem of printing all prime numbers less than 50,000. A prime number is defined as a number which can only be divided by itself and 1. (Interestingly, 1 is not a prime ladies and gentlemen!)

A simple approach to this problem can be described as follows. For any number between 0 to 50,000, test whether it is prime, then print it. If a number is divisible by any number less than itself (excluding 1), than it is not a prime. Otherwise, we have a prime number.

Here is an interesting implementation of this idea, leveraging on the fact that if a number is a product of two other numbers (excluding 1), then it is not a prime.

#include <stdio.h>
#define N 10000

int main(){
  bool a[N]; //for number 1 to 49,999,a[i]=1 if prime, 0 otherwise
  for(int i=2;i<N;++i)a[i]=1; //initialize a[i]
  for(int i=2;i<=N/2;++i){
    for(int j=2;j<=N/i;++j){ //leveraging on symmetry of i*j
      a[i*j]=0; //not prime!
    }
  printf("Primes less than 10,000 are: ");
  for(int i=2;i<=N;++i){
    if(a[i])printf("%d ",a[i]);
  }
  printf("\n");
  return 0;
}

Leveraging on symmetry of i*j allows us to only check the value of i from 2 to N/2. j must be less than N/i since we are only interested in values of  i*j that are less than N.