here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.
Showing posts with label simple maths. Show all posts
Showing posts with label simple maths. Show all posts

Tuesday, 9 June 2015

Playing with isosceles triangle

problem statement is here


#include<stdio.h>
#include<math.h>
 int main() {
long int n,t,s,flag,i,c;
scanf("%ld", &t);
while(t--) {
scanf("%ld",&s);
flag=0;
if(s%2==0){
while(s%2==0)
s=s/2;
}
n=(sqrt(s));
for(i=3;i<=n;i+=2){
if(s%i==0){
if(i%4==1)
flag = 1;
while(s%i==0)
s=s/i;
}
if(s==1)
break;
}
if(s!=1&&s%4==1)
flag=1;
if(flag==0){
printf("NO\n");
} else {
printf("YES\n");
}
}
return 0;
}

Counting Triangles

problem statement is here



#include<stdio.h>
int main(){
        long long int totel,x,y;
        scanf("%lld",&x);
        while(x--){
                scanf("%lld",&y);
                totel=(y*(y+2)*(2*y+1))/8;
                printf("%lld\n",totel);
        }
        return 0;
}

A Famous ICPC Team


problem statement is here


#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
   long long int a[4],p;
   int j,i=1;
   while(scanf("%lld ",&a[0])!=EOF)
    {
       
        for(j=1;j<4;j++)
        {  
            scanf("%lld",&a[j]);
           
        }
        sort(a,a+4);
        p=a[3]+a[2];
        printf("Case %d: %lld\n",i,p);
        i++;
    }
    return 0;
}


Balanced base-3


problem statement is here


#include<stdio.h>
int main() {
int t,y,z,n,ar[10000],i;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
z=0;
while(n){
ar[z]=n%3;
z++;
n/=3;
}
ar[z]=0;
for(i=0;i<=z;i++){
if(ar[i]==2){
ar[i]=-1;
ar[i+1]++;
}else if(ar[i]==3){
ar[i]=0;
ar[i+1]++;
}
}
y=0;
for(i=z;i>=0;i--){
if(ar[i]!=0){
y=1;
if(ar[i]==1)
printf("+");
else if(ar[i]==-1)
printf("-");
}
else if(y==1)
printf("0");
}
printf("\n");
}
return 0;
}

WHAT A CO-ACCIDENT


problem statement is here


#include<stdio.h>
int main(){
long long int a,b,t;
scanf("%lld",&t);
while(t--){
scanf("%lld %lld",&a,&b);
if(a%2==0 || b%2==0){
printf("Suresh\n");
}else{
printf("Ramesh\n");
}
}
return 0;
}

Saturday, 24 January 2015

Faridi and Yadav

problem statement is here


#include<stdio.h>
#include<math.h>

int main(){
double x,y,r,t;
scanf("%lf",&t);
while(t--){
scanf("%lf%lf",&x,&y);
r=2*(sqrt((x*x)-(y*y)));
printf("%.3lf\n",r );
}
return 0;
}

Black Widow Rings

problem statement is here


#include<stdio.h>
int main(){
long int max,t,n,ar[10000],br[10000],i,j,p,m;
scanf("%ld",&t);
while(t--){
max=0;m=0;
scanf("%ld",&n);
for(i=0;i<n;i++){
scanf("%ld %ld",&ar[i],&br[i]);
if(ar[i]>max){
max=ar[i];
p=i;
}
}
br[p]=0;
for(i=0;i<n;i++){
if(m<br[i]){
m=br[i];
}
}
if(max>m){
printf("%ld\n",p+1);
}else{
printf("-1\n");
}
}
return 0;
}

Wednesday, 31 December 2014

Playing with GCD

problem statement is here


#include<stdio.h>
long long ar[100004];
void etf(){
     long long k,i,z;
     ar[0]=0;
     for(k=1;k<100001;k++){
      long long n=k;
      long long r=n;
        for(i=2;i*i<=n;i++){ 
          if (n%i==0) 
          r-=r/i; 
          while(n%i==0) 
          n/=i; 
       } 
       if (n>1)
        r-=r / n; 
       z=k-r;
       ar[k]=ar[k-1]+z;
   } 
}
int main(){
    long long t,num,c=1;
    etf();
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&num);
        printf("Case %lld: %lld\n",c,ar[num]);
        c++;
    }
    return 0;
}

Tuesday, 30 December 2014

Game of chocolate

problem statement is here


#include<stdio.h>
long long gcd(long long a,long long b){
    if(b==0){
        return a;
    }
return gcd(b,a%b);
}
int main(){
long long a,b,c,d,e,f,g,i,j,cc=1,t;
scanf("%lld",&t);
while(t--){
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
e=a*(c+1)+b*(d+1);
        f=(a+b)*(c+1+d);
        g=gcd(e,f);
        if(g>0){
        e/=g;
        f/=g;
        }
        if(e==0){
        printf("Case %lld: 0\n",cc);
        }else{
        printf("Case %lld: %lld/%lld\n",cc,e,f);
        }
        cc++;
}
return 0;
}

Rivals

problam statement is here


#include<stdio.h>
#define MOD 1000000007

long long ar[2000010];
void fact(){
long long i;
    ar[0]=1;
    ar[1]=1;
    for(i=2;i<=2000000;i++)
        ar[i]=(ar[i-1]*i)%MOD;
}
long long fermet(long long x){
    long long a=1,p=x,n=MOD-2;
    while(n){
        if(n&1)
            a=(a*p)%MOD;
        p=(p*p)%MOD;
        n>>=1;
    }
    return a;
}
int main(){
    int t,a,b;
    long long c,d;
    fact() ;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&a,&b);
        c=(fermet(ar[a])*fermet(ar[b]))%MOD;
        d=(ar[a+b]*c)%MOD;
        printf("%lld\n",d);
    }
    return 0;
}

Monday, 29 December 2014

Balanced base-3

problem statement is here

#include<stdio.h>

int main() {
int t,y,z,n,ar[10000],i;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
z=0;
while(n){
ar[z]=n%3;
z++;
n/=3;
}
ar[z]=0;
for(i=0;i<=z;i++){
if(ar[i]==2){
ar[i]=-1;
ar[i+1]++;
}else if(ar[i]==3){
ar[i]=0;
ar[i+1]++;
}
}
y=0;
for(i=z;i>=0;i--){
if(ar[i]!=0){
y=1;
if(ar[i]==1) 
printf("+");
else if(ar[i]==-1)
printf("-");
}
else if(y==1) 
printf("0");
}
printf("\n");
}
return 0;
}

Friday, 14 November 2014

D - Playing with Marbles

problem statement is here


#include<stdio.h>
int main()
{
    long long int s,n,j,x;
    while (1)
    {
                    s=0;
                    scanf("%lld",&n);
                    if(n==0)
                    break;
                    else
                    {
                        x=n+1;
                        s=(3*x*x-x)/2;
                                     }
                                     printf("%lld\n",s);
                                     }
                                     return 0;
                                     }

Atoms in the Lab

problem statement is here


#include <stdio.h>
 int main(){
    int p;
    long long n,k,m,count;
    double mul;
    scanf("%d",&p);
    while(p--){
        scanf("%lld %lld %lld",&n,&k,&m);
        mul=n;
        count=0;
        while(mul<=m){
            count++;
            mul*=k;
        }
        if (count>0)
            printf("%lld\n",count-1);
        else
            printf("0\n");
    }

    return 0;
}

Monday, 3 November 2014

What’s Next

problem statement is here



#include<stdio.h>
int main()
{
    int a,b,c,n,d,r,p,l;
    while(1)
    {
            scanf("%d %d %d",&a,&b,&c);
            if(a==0&&b==0&&c==0)
            break;
            else
            {
            l=(c-b)/(b-a);
            if(l==1)
            {
                    d=c-b;
                    n=c+d;
                    printf("AP %d\n",n);
                    }
                    else
                    {
                        r=c/b;
                        p=c*r;
                        printf("GP %d\n",p);
                        }
                        }
                        }
                        return 0;
                        }

abs(a-b) I

problem statement is here



#include<stdio.h>
int main(){
long long int a,b,ar[100000],i,j,u,p,t,sum;
scanf("%lld",&t);
while(t--){
sum=0;
scanf("%lld",&a);
scanf("%lld",&ar[0]);
u=0;
for(i=1;i<a;i++){
scanf("%lld",&ar[i]);
p=ar[i]-ar[i-1];
u+=(i*p);
sum+=u;
}
printf("%lld\n",sum);
}
return 0;
}

Wednesday, 29 October 2014

A Summatory

problem statement is here


#include <stdio.h>
#define M 1000010

int t,c;
long long int sum[M];

int main(){
      long long int i, a = 0;
           for (i = 1; i < M; i++){
                  a = (a + i*i*i) % 1000000003;
                  sum[i] = (sum[i-1] + a) % 1000000003;
                   }
           scanf("%d", &t);
           while (t--){
              scanf("%d", &c);
              printf("%lld\n", sum[c]);
               }
           return 0;
}

Monday, 20 October 2014

Crucial Equation

problem statement is here


#include<stdio.h>
gcd(int m,int n){
if(n==0)
return m;
else 
return gcd(n,m%n);
}
int main(){
int a,b,c,t,g,e=1;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&a,&b,&c);
g=gcd(abs(a),abs(b));
if(c%g==0)
printf("Case %d: Yes\n",e);
else 
printf("Case %d: No\n",e);
e++;
}
return 0;
}

Pigeonhole Tower

problem statement is here

#include<stdio.h>
#include<math.h>
int main() {
long long a,b,c=1,d,i,j,t,n;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
if(n<3){
a=0;
}else{
b=sqrt(n);
while(1){
if(b*(b+2)<=n){
a=b;
break;
}else{
b--;
}
}
}
printf("Case %lld: %lld\n",c,a);
c++;
}
return 0;
}

Monday, 6 October 2014

Boring factorials

problem is here


This can be solved using Wilson theorem
   (1). if n>=p  ans would be 0
   (2). then we have to use Wilson's theorem
             (p-1)!     -1 (mod p)
             1*2*3*.........*(n-1)*(n)*..............*(p-1)     -1 (mod p)
             n!*(n+1)*...........*(p-1)      -1 (mod p)
             n!      -1*[(n+1)*...............(p-2)*(p-1)]^-1 (mod p)


#include<stdio.h>
long long power(long long a,long long b,long long m){
  long long x=1,y=a;
  while(b>0){
  if(b%2!=0){
  x=(x*y)%m;
  }
  y=(y*y)%m;
  b>>=1;
  }
  return x;
}
int main(){
  int t;
  long long n,p,i,result,z,x;
  scanf("%d",&t);
  while(t--){
  result=-1;
  scanf("%lld %lld",&n,&p);
  if(n>=p){
    printf("0\n");
    continue;
  }
  x=1;
  for(i=n+1;i<p;i++){
  x=(x*i)%p;
  }
    z=power(x,p-2,p);
    result=(result*z)%p;
  printf("%lld\n",p+result);
  }
  return 0;

}

Saturday, 4 October 2014

Sum


#include<stdio.h>
int main(){
long a,b,n,i,j,ar[1002],u,m;
scanf("%ld",&n);
for(i=0;i<n;i++){
scanf("%ld",&ar[i]);
}
for(i=1;i<n;i++){
for(j=0;j<n;j++){
scanf("%ld",&u);
if(i==1 && j==2){
m=u;
}
}
}
if(n==2){
printf("1 %ld\n",ar[1]-1);
}else{
a=(ar[1]+ar[2]-m)/2;
printf("%ld",a);
for(i=1;i<n;i++){
b=ar[i]-a;
printf(" %ld",b);
}
printf("\n");
}
return 0;
}