ETF - Euler Totient Function SPOJ: - http://www.spoj.com/problems/ETF/ CodeChef :- Rational Numbers (it is sum of phi upto n) In number theory, Euler's totient function (or Euler's phi function), denoted as φ(n) or ϕ(n), is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n. (These integers are sometimes referred to as totatives of n.) This is very common in number theory the formula for finding the number of numbers less than and co-prime with n is N(1-1/p1)(1-1/p2).... where p1,p2.... refers to the distinct prime divisors of the number. A example always makes it clear ,lets take n=20 hence the prime divisors are 2,5 hence by formula the number of numbers less than and prime to 20 should be 20*(1-1/2)(1-1/2)=20(1/2)(4/5)=8 Now we can count also 1,3,7,9,11,14,17...
http://codeforces.com/contest/476/problem/B #include < bits / stdc ++. h > using namespace std ; string s1 , s2 ; int val = 0 , n ; int dp [ 15 ][ 15 ]; int solve ( int s , int l , int f ) { int & ret = dp [ s ][ l ]; if ( s == l ) { if ( f == val ) return 1 ; else return 0 ; } int ct = 0 ; ///number of ways in starting is 0 if ( s2 [ s ]== '+' ) ct += solve ( s + 1 , l , f + 1 ); if ( s2 [ s ]== '-' ) ct += solve ( s + 1 , l , f - 1 ); if ( s2 [ s ]== '?' ) ct += solve ( s + 1 , l , f + 1 )+ solve ( s + 1 , l , f - 1 ); return ret = ct ; /// In the last return this value } int main () { int i , j , k , l ; double ans ; cin >> s1 >> s2 ; n = s1 . size (); for ( i = 0 ; i < n ; i ++) { if ...
Divide Intervals Into Minimum Number of Groups Leetcode Problem Divide Intervals Into Minimum Number of Groups is very much similar to the number-of-the-smallest-unoccupied-chair Here we have to divide the given intervals into minimum number of groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other. Solution: We will think each interval as combination of two events , startTime and endTime. 1) We create a list of events and then sort that list. 2) After that, we Initialise a min heap. which can be thinked like different baskets or groups. 3) We iterate through the events one by one, and assign each events to groups (picked from minheap). While iterating starting event we pick a group from min heap and keep track in an array. While iterating ending event through our tracking array, we release the group again in min...
Comments
Post a Comment