Translate

Showing posts with label number theory. Show all posts
Showing posts with label number theory. Show all posts

Tuesday, 3 February 2015

ASSIST(Assistance Required) spoj problem solution

HII GUYS this is also one of the easy problem
here simply apply the brute force logic
but just do that in precompute

i think there is no need to explain in this
if still u have doubt u can go through my solution

HERE is my ac c++ solution is::::



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
#define lim 50000
vector<int>dev;
vector<bool>check(lim,false);
void pre()
{
 int i,j;
 for(i=2;i<=34000;i++)
 {
  if(check[i]==false)
  {
   int x=0;
   for(j=i+1;j<=34000;j++)
   {
    if(check[j]==false)
    x++;
    if(x==i)
    {
     check[j]=true;
     x=0;
    }
   }
  }
 }
 for(i=2;i<=34000;i++)
 {
  if(check[i]==false)
  {
   dev.push_back(i);
  }
 }
 //printf("%d",dev.size());
}
int main()
{
 pre();
 while(1)
 {
  int n;
  scanf("%d",&n);
  if(n==0)
  break;
  printf("%d\n",dev[n-1]);
 }
}

AMR11E(Distinct Primes) spoj problem solution

HII GUYS this is one of the easy problem
just compute small prime number and then check which numbers are lucky number and store them
in ur ans array...then for every testcase just print ans in o(1)

Here is my ac c++ solution is::::



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define lim 3600
vector<int>prime;
vector<bool>check(lim,false);
vector<int>ans(lim);
void pre()
{
 int i,j;
 for(i=3;i<=55;i+=2)
 {
  if(check[i]==false)
  {
   for(j=i*i;j<=3025;j+=2*i)
   check[j]=true;
  }
 }
 prime.push_back(2);
 for(i=3;i<=2700;i+=2)
 {
  if(check[i]==false)
  prime.push_back(i);
 }
}
void dev()
{
 int i,j,x,count=0;
 for(i=30;i<=2664;i++)
 {
  x=0;
  for(j=0;prime[j]<=i;j++)
  {
   if(i%prime[j]==0)
    x++;
   if(x==3) {
    ans[count++]=i;
    break;}
  }
 }
}
int main()
{
 pre();
 dev();
 int t,i,n;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d",&n);
  printf("%d\n",ans[n-1]);
 }
 return 0;
}

Friday, 30 January 2015

PR003004(Digit Sum) SPOJ PROBLEM SOLUTION

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
HII GUYS THIS IS MY AC C++ SOLUTION

if u want to know the logic behind this u can rerfer my
another post for explanation
http://spojdev.blogspot.in/2015/01/formula-for-calculating-sum-of-digits.html

#include<iostream>
#include<cstdio>
#include<cmath>
#define LL long long int
using namespace std;
LL sum(LL N)
{
if(N/10==0) return N*(N+1)/2;
int i=0;
LL n=N;
while(n/10!=0){
i++;
n/=10;
}
     LL p=pow(10,i);
     return ((n*45*i*p/10)+ n*(n-1)*p/2+ n*(N%p+1) + sum(N%p));
}
int main()
{
LL a,b,t;
scanf("%lld",&t);
while(t--)
{
cin>>a>>b;
cout<<sum(b)-sum(a-1)<<endl;
}
return 0;
}

CPCRC1C(Sum of Digits) spoj problem solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
HII GUYS I JUST EXPLAIN HOW I DERIVED THIS FORMULA I JUST MENTION 
IN MY SEPERATE POST.............HERE IS MY 


AC C++ SOLUTION IS::::::::::


#include<iostream>
#include<cmath>
using namespace std;
long long sum(long long N)
{
if(N/10==0) return N*(N+1)/2;
int i=0;
long long n=N;
while(n/10!=0){
i++;
n/=10;
}
     int p=pow(10,i);
     return ((n*45*i*p/10)+ n*(n-1)*p/2+ n*(N%p+1) + sum(N%p));
}
int main()
{
long long a,b;
cin>>a>>b;
while(a!=-1 || b!=-1)
{
cout<<sum(b)-sum(a-1)<<endl;
cin>>a>>b;
}
return 0;
}

Formula for calculating the sum of Digits from 1 to n

HII GUYS>>>>>>>>

First of all i just explain u by one example.........
1.one important thing is the sum of 1+2+3+.........+9=45
2.now u have to find out that how many times this sum(45) will occur......
3.example::
      let u want to find out sum of digits of 1 to 52.
      then just notice how i m reaching to 92.
     i.e 1,2,3..........9
         11,12,13,...........19
         21,22,23,,,,,,,,,,,,,,,,29
         31,32,,,,,,,,,,,,,,,,,,,,,,39
         41,42,,,,,,,,,,,,,,,,,,,,,49
         10,20,30,40,50
         51,52
        
         just think how i m making sets....
        1. now observe how many sum(1+2+3+......+9=45)  occuring
         5 times sum(45) occuring
         
        2.10times 1,10 times 2,10 times 3,10 times 4
           so i can write this sum as (1+2+3+4) *10 times
                                                 formula for this=4*5*10/2
    
        3.for 50,51 ,52 i.e how many times 5 occuring...
           i.e 3 times i can write this as 5*(2+1) or 5*(52%10+1)
        
         4.now for 0+1+2=2*3/2

so finally formula for this is

int sumofDigit(int N)
{
if(N<10)
return N*(N+1)/2;
int n=N,i;
while(n/10!=0)
{
i++;
n/=10;
}
int p=pow(10,i);
return ((n*i*45*p/10)+(n*(n-1)*p/2)+n*(N%p+1)+sumofDigit(N%p));
}
                    
         
   PRACTICE PROBLEM::

  http://www.spoj.com/problems/CPCRC1C/
  http://www.spoj.com/problems/PR003004/      
         

Working With Java Collections