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 ...
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...
Below is the important Graph Algorithms for problem solving Algorithms Cycle Detection Algorithm For Directed graph Cycle can be detected using : 1) DFS-Based Cycle Detection using color marking (visited array) void dfs(node): mark visited for neighbor: if not visited: dfs(neighbor) stack.push(node) 2) Kahn’s Algorithm : Build the Graph → Create an adjacency list from the prerequisites array. Compute In-Degree → Count the number of prerequisites (incoming edges) for each course. Start BFS with Zero In-Degree Nodes → Add all courses with zero prerequisites to a queue. Process Courses in BFS Order → Remove edges and reduce in-degrees. Check if All Courses Were Taken → If all courses were processed, return true ; otherwise, there’s a cycle. // Simple idea of Kahn's: while (queue is n...
Comments
Post a Comment