Posts

Showing posts with the label SPOJ

SPOJ Maximum Sum

//I am struggling //Just Not Good at it // Accepted //I am struggling //Just Not Good at it #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; const int mod = 1000007; const int high = 100010; const int inf = 1 << 30; int ar[high]; struct info {     LL pos, maxi; }tree[high * 4] , base; void update(int node, int b , int e , int i ,  LL x) {     if(i>e or i<b) return;     if(b == e)     {         tree[node].maxi = x;         tree[node].pos = i;         return;     }     int ...

SPOJ HORRIBLE - Horrible Queries

//Next Codeforces Round #354 (Div. 2) // Accepted // Segment Tree (Lazy Propagation) #include<bits/stdc++.h> //#include<cstdio> //#include<iostream> //#include<algorithm> //#include<vector> //#include<cstring> //#include<cmath> //#include<map> using namespace std; #define fast ios_base::sync_with_stdio(false) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back //#define i64 long long typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; const int high = 1 << 18; struct info {     LL sum, prop; }; info tree[high]; void update(int node, int b , int e , int i , int j , LL x) {     if(i == b and j == e)     {         tree[node].sum+=(x * (e - b + 1));         tree[node...

SPOJ Prime Intervals

// Accepted // Time: 0.74 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define sf scanf #define pf printf #define DIFF 1000010 #define limit 46400 #define l_limit 216 #define mset(c,v) memset(c , v , sizeof(c)) typedef long long LL; LL prm[DIFF], plen=0; bool mark[DIFF], primenow[DIFF]; void sieve(LL n) {     LL i,j;     mset(mark, true);     for(i=2; i*i<limit; i++)     {         if(mark[i])         {             for(j=i*i; j<limit; j+=i)             {                 mark[j] = false;             }    ...

SPOJ Prime Generator

// Accepted // Time: 0.01 // Process: Segmented Seieve #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define DIFF 100010 #define mset(c,v) memset(c,v,sizeof(c)) #define sf scanf #define pf printf LL prm[DIFF], plen=0; void sieve(int n) {     LL i,j;     bool mark[DIFF];     //mset(mark, true);     for(i=2; i<DIFF; i++) mark[i] = true;     LL qrt = floor(sqrt(double(n)));     LL k = floor(sqrt (double(qrt)));     for(i=2; i<=k; i++)     {         if(mark[i])         {             for(j=i*i; j<=qrt; j+=i)          ...

SPOJ Prime Time

#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #include <cmath> #include <cctype> #include <sstream> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #define sf scanf #define pf printf #define sfint scanf ("%d %d",&a,&b) #define sfl scanf ("%ld %ld",&a,&b) #define sfll scanf ("%lld %lld",&a,&b) #define sfd scanf ("%lf %lf",&a,&b) #define sff scanf ("%f %f",&a,&b) #define loop(i,n) for(i=0;i<n;i++) #define LL long long #define L long #define nl puts("") #define MX 10100 #define N 100 #define MOD 10000000007 #define pb push_back #define pi acos(-1.0) #define sz size() #define gc getchar () #define ps push #define clr clear #define bn begin() #define ed end() using namespace std; int prime[(MX >> 6) + ...

SPOJ Finding the Kth Prime

// Header file begin #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<string> #include<vector> #include<cmath> #include<cctype> #include<sstream> #include<set> #include<list> #include<stack> #include<utility> #include<queue> #include<algorithm> #include<cstdlib> #include<bitset> // End //.......... // Macro #define sf scanf #define pf printf #define lp1(i,n) for(i=0;i<n;i++) #define lp2(i,n) for(i=1;i<=n;i++) #define mset(c,v) memset(c,v,sizeof(c)) #define outs(a) cout<<" "<<a<<" " #define nl puts("") #define sq(x) ((x)*(x)) #define all(x) x.begin(),x.end() #define reall(x) x.rbegin(),x.rend() #define s_wap(x,y) x^=y;y^=x;x^=y; #define sz size() #define gc getchar() #define psb push_back #define fre...

SPOJ-AMR11E - Distinct Primes

#include<iostream> #include<cstdio> #include<algorithm> #include<bitset> #include<map> #define N 3010 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef map<int,int> mpii; typedef map<LL,bool>mpbll; int prm[N], plen=0 , psz = N; bitset<N>bs; void sieve() {     LL i,j;     bs.set();     bs[0] = bs[1] = 0;     for(i=2; i<N; i++)     {         if(bs[i])         {             prm[plen++] = i;             for(j=i*i;j<N;j+=i)             {                 bs[j] = 0;           ...

SPOJ-CMG - Collecting Mango

#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<stack> #include<map> #include<cmath> #include<algorithm> #define psb push_back #define ppb pop_back #define all(x) x.begin(),x.end() #define N 1000100 #define sf scanf #define pf printf using namespace std; typedef long long LL; typedef vector<LL>vll; LL ar[N] , num[N]; int main() {     int t , tc=0;     //cin >> t;     sf("%d",&t);     while(t--)     {         LL n, x, arlen=-1, nmlen=-1 , i , maxi=-1 , zar;         char c;         //cin >> n;         sf("%lld",&n);         //cout << "Case " << ++tc << ":\n";       ...

Printing some primes

#include<bits/stdc++.h> using namespace std; #define mx 100001000 int prime[(mx>>6) + 1] , prm[(mx>>1)+9], plen=1; #define setbit(n) (prime[n>>6] |= (1 << ((n>>1)&31))) #define checkbit(n) (prime[n>>6] & (1 << ((n>>1)&31))) typedef long long LL; LL ans[60000+6]; void sieve() {     LL i,j;     for(i=3;i*i<=mx; i+=2)     {         if(!checkbit(i))         {             for(j=i*i;j<=mx;j+=i+i)             {                 setbit(j);             }         }     }     prm[plen++]=2;     for(i...

SPOJ Adding Reversed Numbers

#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #define sf scanf #define pf printf using namespace std; typedef unsigned long long ull; typedef long L; ull reverse_num(ull n) { ull a=n,add=0,b=n; while(a>0) { a%=10; add = a+(add*10); b/=10; a=b; } return add; } int main() { L t; sf("%ld",&t); while(t--) { ull x,y; sf("%llu %llu",&x,&y); ull result = reverse_num(x) + reverse_num(y); pf("%llu\n",reverse_num(result)); } return 0; }

SPOJ Life, the Universe, and Everything

#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #include <cmath> #include <cctype> #include <sstream> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #define sf scanf #define pf printf #define sfint scanf ("%d %d",&a,&b) #define sfl scanf ("%ld %ld",&a,&b) #define sfll scanf ("%lld %lld",&a,&b) #define sfd scanf ("%lf %lf",&a,&b) #define sff scanf ("%f %f",&a,&b) #define loop(i,n) for(i=0;i<n;i++) #define LL long long #define L long #define nl puts("") #define MX 1000005 #define N 100 #define MOD 10000000007 #define pb push_back #define pi acos(-1.0) #define sz size() #define gc getchar () #define ps push #define clr clear #define bn begin() #define ed end() using namespace std; int main() { int x...

SPOJ Adding Reversed Numbers

import java.util.*; import java.math.*; class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int t,tc,n= 10 ; tc = in. nextInt ( ) ; for ( t= 1 ;t<=tc;t++ ) { BigInteger a,b,x,y,s1= BigInteger . valueOf ( 0 ) ,s2= BigInteger . valueOf ( 0 ) ,p= BigInteger . valueOf ( 10 ) ,s,r= BigInteger . valueOf ( 0 ) ,m; x=in. nextBigInteger ( ) ; y = in. nextBigInteger ( ) ; a=x;b=x; while ( a!= BigInteger . valueOf ( 0 ) ) { a = a. mod ( p ) ; s1 = a. add ( s1. multiply ( p ) ) ; b = b. divide ( p ) ; a=b; } //System.out.print(s1); a=y;b=y; while ( a!= BigInteger . valueOf ( 0 ) ) { a = a. mod ( p ) ; s2 = a. add ( s2. multiply ( p ) ) ; b = b. divide ( p ) ; a=b; } s = s1. add ( s2 ) ; //System.out.print(s); a = s ; b = s; while ( a!= BigInteger . valueOf ( 0 ) ) { a=a. mod ( p ) ; r = a. add (...

SPOJ Digit Sum

#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #include <cmath> #include <cctype> #include <sstream> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #define sf scanf #define pf printf #define sfint scanf ("%d %d",&a,&b) #define sfl scanf ("%ld %ld",&a,&b) #define sfll scanf ("%lld %lld",&a,&b) #define sfd scanf ("%lf %lf",&a,&b) #define sff scanf ("%f %f",&a,&b) #define loop(i,n) for(i=0;i<n;i++) #define LL long long #define L long #define nl puts("") #define MX 1000005 #define N 100 #define MOD 10000000007 #define pb push_back #define pi acos(-1.0) #define sz size() #define gc getchar () #define ps push ...