Posts

Showing posts with the label Maths questions

use of sscanf , takes interger from messy string R23C55.. or R2323434C44554

                                          http://codeforces.com/contest/1/problem/B                                                       B. Spreadsheets 55 = BC 27 = AA 28 = AB sscanf takes out number from a messy string input : for example : if the input is R5564C757 , it takes (sscanf(s,"  %*c   %d    %*c     %d  ",&x,&y);  x=5564  y=757 #include<cstdio> #include<bits/stdc++.h> using namespace std; void g(int t) {  if(t)  {  g((t-1)/26);  putchar(65+(t-1)%26);  } } int main() {   int n,x,y;   char s[64],*p;   for(scanf("%d ",&n);n--;)   {     gets(s);     if(sscanf(s,"  ...

wilson's theorm + fermat's theorem :---BORING Factorials

                                   SPOJ :-  BORING - Boring Factorials  According to wilson's theorem   for prime numbers  ((n-1)!) mod n = n-1 In the question Boring factorials we hv to find :---   N! mod P (1*2*..*(n-1)*(n)*..*(p-1) )mod P= P-1  (wilson's) (1*2*..*(n-1)*(n) )mod P  (this is the question) =(P-1) / ((n+1)*(n+2)*..*(P-2)*(P-1)) mod P #include<bits/stdc++.h> using namespace std; #define ll long long int long long int read_int(){ char r; bool start=false,neg=false; long long int ret=0; while(true){ r=getchar(); if((r-'0'<0 || r-'0'>9) && r!='-' && !start){ continue; } if((r-'0'<0 || r-'0'>9) && r!='-' && start){ break; } if(start)ret*=10; start=true; if(r=='-')neg=true; else ret+=r-'0'; } if(!neg) return ret; ...

ETF - Euler Totient Function

                                      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...

Union_find_application_ (finding minimum cost's component among all disconnected components)

In this question ,firstly there are n disconnected components ,with different costs. One -by -one, these disconnected componets are getting attached. we have to find the smallest cost component after each attachment.   https://www.hackerrank.com/contests/morgan-stanley-2015/challenges/min-con-com #include<bits/stdc++.h> using namespace std; multiset<int> s; multiset<int>::iterator it; int cost[500005]; int parent[500005]; int rankk[500005]; int find(int nod) {    if(parent[nod]==nod)         return nod;    else      return find(parent[nod]); } void merge(int nod1,int nod2) {     int p1=find(nod1);     int p2=find(nod2);     if(rankk[p1]>rankk[p2])     {         parent[p2]=p1;                   ///those rankk is more ,will become parent        ...

Equation solving in Programming

ans=(k*g)/(g1*g2);  (If the answer is an integer). In these types of Equation solving program ,try to avoid multiplication . Always do division to find the answer ,Otherwise there is a very possibilty of getting WA . So, in 1st step:       g1=g1/g;           2nd step :     ans=k/g1;           3rd step :      ans=ans/g2;

Diophantine equation

           https://en.wikipedia.org/wiki/Diophantine_equation             Diophantine equation is  ax + by = c give an integer solution only if  gcd(a,b) divides c . down vote Solving Linear Diophantine equations ax + by = c  and  gcd(a, b)  divides  c . Divide a, b and c by gcd(a,b). Now gcd(a,b) == 1 Find solution to aU + bV = 1 using  Extended Euclidean algorithm Multiply equation by c. Now you have a(Uc) + b (Vc) = c You found solution  x = U*c  and  y = V * c problem realted :https://www.codechef.com/APRIL12/problems/DUMPLING/ EXPLANATION Given two integers A and B, let us try to figure out all the reachable integers. Consider jumping X units to the right as adding X and jumping X units to the left as subtracting X. Working out an example always helps. If A = 4 and B = 6, observe that we can only reach every in...

a/b mod p

Image
 =   ( making use of  Fermat's little theorem ).
Finding how many numbers have odd number divisers in th range [a,b] Given two number a and b.Count how many numbers in the range a to b(including both a and b) has odd number of divisors. Input First line of input contains number of test cases T.Each of the next T lines contains two number a and b. Output For each test case output a single line containing resulting count. Constraints T<=100 1<=a<=b<=10^12 Sample Input 1 2 5 Sample Output 1 Explanation Divisors of 2:1,2 Divisors of 3:1,3 Divisors of 4:1,2,4 Divisors of 5:1,5 So only 4 has odd number of divisors which gives count as 1. Solution :: #include<stdio.h> #include<math.h> int main() { int t; scanf("%d",&t); // Test cases while(t--) { long long a,b,ans=0; scanf("%lld%lld",&a,&b); long long first_root=ceil(sqrt(a));//sqrt of first perfect square in [a,b] long long last_root=sqrt(b); //sqrt of ...
nCr  find  karne  ka  best  example. #include<bits/stdc++.h> using namespace std; int fast_pow(long long base, long long n,long long M) {     if(n==0)        return 1;     if(n==1)     return base;     long long halfn=fast_pow(base,n/2,M);     if(n%2==0)         return ( halfn * halfn ) % M;     else         return ( ( ( halfn * halfn ) % M ) * base ) % M; } int findMMI_fermat(int n,int M) {     return fast_pow(n,M-2,M); } int main() {     long long fact[100001];     fact[0]=1;     int i=1;     int MOD=1000000007;     while(i<=100000)     {         fact[i]=(fact[i-1]*i)%MOD;         i++;     }     int t;     cin>>t;     while(t--)   ...