Showing posts with label Math. Show all posts
Showing posts with label Math. Show all posts

Tuesday, 20 March 2018

Codeforces Problem 950D - A Leapfrog in the Array (C++)

Welcome:
The question to the problem can be found here:

Explanation:

1. It is a tricky one but easy to code if you understand it.
2. using the index of the array we can solve it.
3. First lets test your attention skill. Look at the IV Figure and then the I figure.
4. If the array index number is a odd number(see input) if you add one and divide by 2, you get the required answer(the value at that index)
5. On the other hand as long as the index number is even, do x+=n- x/2.
6. Do them in Bitwise so it will reduce a lot of time to run.
7. Happy Coding.


Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
long long int n,q,x;
cin>>n>>q;
while(q--){
    cin>>x;
    while(!(x&1)){
        x+=n-x/2;
    }
    cout<<(x+1>>1)<<endl;

}



return 0;}

Tuesday, 2 January 2018

SPOJ Problem ABSP1 Solution in (C++)

Welcome:

The question to the problem can be found here:


Explanation:
1.Notice the array is sorted already.
2. It can be solved with simple Math.
3. There are two logic only.
4. Multiply each element  i -th times(i-th times: position of element in the array)
5. Multiply each element n-1-i -th times.
6. Subtract 4 and 5.
7.Happy Coding !

Code:

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

long long ABS(long long a[],long long n){
    long long sum=0;
rep(i,n){
sum += (a[i]*i) - (a[i]*(n-1-i));
}
return sum;
}
int main(){
 //   freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
int t;
long long arr[10000];
cin>>t;
while(t-->0){
long long n;
cin>>n;
rep(i,n){
    cin>>arr[i];
}
cout<<ABS(arr,n)<<endl;

}

return 0;}

Thursday, 7 September 2017

Uva Problem 11936 - The Lazy Lumberjacks Solution (C++)

Problem:

11936 - The Lazy Lumberjacks


Please find the problem here.

Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:


#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
int x,y,z;
cin>>t;
while(t--){
cin>>x>>y>>z;
if(x+y>z && y+z>x && z+x>y){
cout<<"OK"<<endl;
}
else if(x==y && y ==z && z==x)
    cout<<"OK"<<endl;
else cout<<"Wrong!!"<<endl;
}

return 0;}

OjLight Problem 1294 - Positive Negetive Number (C++)

Problem:

Solution:

This is the one of the simplest problem I ever had, just implement using the given formulas! However, it does take me some time to write.

Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
long long t;
long long n,m,result;
cin>>t;
for(int i=1;i<=t;i++){
    cin>>n>>m;
    result = m*(n/2);
    cout<<"Case "<<i<<": "<<result<<endl;

}
return 0;

}








OjLight Problem 1053 - Higher Math Solution (C++)

Problem: OjLight Problem 1053 - Higher Math

Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:

#include <bits/stdc++.h>
using namespace std;
main(){
long long n,a,b,c,f,g;
cin>>n;
for(int i=1;i<=n;i++){
        cin>>a>>b>>c;
if(a>b && a>c)
{
    f= a*a;
    g=(b*b)+(c*c);

}
else if(b>a&&b>c){
    f= (b*b);
    g= a*a+c*c;
}
else if(c>a&& c>b){
    f=c*c;
    g=a*a+b*b;
}
(f==g?cout<<"Case "<<i<<":"<<" yes"<<endl:cout<<"Case "<<i<<":"<<" no"<<endl);
}
return 0;
}



Sunday, 3 September 2017

CodeForce Problem 318A - Even Odds (C++)

Problem:

Please find the problem here
.

Solution:

This is one of the easiest problem I ever had, just implement the given formulas! However, it does take me a few minutes to write.

Code:
//Even Odds Solution
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
int main(){
ll n, k;
cin>>n>>k;
//Suppose n=10.
//Odd series then Even series 1,3,5,7,9,2,4,6,8,10.
n=(n+1)/2; //Finding the mid point.In above case it is 9.
//Condition 1.greater than 9 Even and vice-versa(Odd).
cout<<(k>n? 2*(k-n):2*k-1)<<endl;//if and else condition

return 0;
}

CodeForce Problem 849A - Odds and Ends Solution (C++)

Problem:

You can see the question to the problem here.


Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:

#include <bits/stdc++.h>
using namespace std;
main(){
int n;
int s[101];
cin>>n;
for(int i=0;i<n;i++){
    cin>>s[i];   //Numbers are stored into array
}

/*Algorithm explanation
//when n is divided by 2 remainder is not 0, result is always odd.
//Plus First element and last element.. both of them must always be odd for this Algorithm to work as mentioned in question. */

(n%2 && s[0]%2 && s[n-1]%2)?cout<<"Yes":cout<<"No";  //Another form of if else.

}

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...