forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03ac758
Showing
422 changed files
with
22,303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <http://unlicense.org> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
SPOJ | ||
==== | ||
|
||
This repository contains solutions to almost all problems I have solved on | ||
SPOJ. They're intended as a last resort in case you've tried very hard to solve | ||
a problem but you just haven't been able to figure it out. When I was trying to | ||
solve <a href="http://www.spoj.com/problems/GSS2/">GSS2</a>, I mostly found | ||
vague hints on online forums, which weren't helpful. I wasn't able to solve | ||
the problem until after I looked at some code. So obviously the code helps, and | ||
there can be good reasons to just look at code rather than being stuck | ||
indefinitely. | ||
|
||
The copyright notice (LICENSE) applies to all code except for a few algorithms | ||
copied from the Stanford ACM team notebook, which are indicated as such. There | ||
is technically no license for that code that I'm aware of, but it's unlikely | ||
you'll get into any trouble for using it or redistributing it freely. A recent | ||
version of the Stanford ACM team notebook can be found <a | ||
href="http://stanford.edu/~liszt90/acm/notebook.html">here</a>. | ||
|
||
There is a comment at the beginning of each file that gives the date on which | ||
the solution was submitted. Caution: Many of the older solutions have bad | ||
coding style and poor mastery of C++. | ||
|
||
For a few problems I have multiple solutions, usually because after my first | ||
successful submission I discovered there was a much better way to solve the | ||
problem. Those are indicated as e.g. niceday-1.cpp and niceday-2.cpp. There is | ||
no possible ambiguity as SPOJ problem names aren't allowed to contain dashes | ||
(as far as I know, anyway). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 2014-09-22 | ||
#include <vector> | ||
#include <algorithm> | ||
#include <cstdio> | ||
using namespace std; | ||
bool is_knumber[1000001]; | ||
int knumber_count[1000001]; | ||
bool is_prime(int x) { | ||
if (x < 2) return false; | ||
for (int i = 2; i*i <= x; i++) { | ||
if (x%i == 0) return false; | ||
} | ||
return true; | ||
} | ||
int main() { | ||
is_knumber[2] = true; | ||
for (int i = 2; i <= 1000; i++) { | ||
if (is_prime(i)) { | ||
int pwr = i*i; | ||
int sod = 1 + i + i*i; | ||
while (pwr <= 1000000) { | ||
if (is_prime(sod)) { | ||
is_knumber[pwr] = true; | ||
} | ||
pwr *= i; | ||
sod += pwr; | ||
} | ||
} | ||
} | ||
knumber_count[0] = 0; | ||
for (int i = 1; i <= 1000000; i++) { | ||
knumber_count[i] = knumber_count[i-1] + int(is_knumber[i]); | ||
} | ||
int T; scanf("%d", &T); | ||
while (T--) { | ||
int A, B; scanf("%d %d", &A, &B); | ||
printf("%d\n", knumber_count[B] - knumber_count[A-1]); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 2014-05-02 | ||
#include <cstdio> | ||
char s[100001]; | ||
char t[100001]; | ||
char other(char a, char b, char c) { | ||
for (char d = 'A';; d++) { | ||
if (d != a && d != b && d != c) return d; | ||
} | ||
} | ||
int main() { | ||
int N; scanf("%d\n", &N); | ||
gets(s); | ||
for (int i = 0; i < N; i++) { | ||
t[2*i] = other(s[2*i], s[2*i+1], i == 0 ? 'A' : t[2*i-1]); | ||
t[2*i+1] = other(t[2*i], s[2*i], s[2*i+1]); | ||
} | ||
t[2*N] = 0; | ||
puts(t); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 2014-04-25 | ||
#include <cstdio> | ||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
int a[100]; | ||
int main() { | ||
int N; | ||
scanf("%d", &N); | ||
for (int i = 0; i < N; i++) { | ||
scanf("%d", a+i); | ||
} | ||
vector<int> s1, s2; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
for (int k = 0; k < N; k++) { | ||
s1.push_back(a[i]*a[j]+a[k]); | ||
if (a[i] != 0) { | ||
s2.push_back(a[i]*(a[j]+a[k])); | ||
} | ||
} | ||
} | ||
} | ||
sort(s1.begin(), s1.end()); | ||
sort(s2.begin(), s2.end()); | ||
long long res = 0; int i = 0, j = 0; | ||
while (i < s1.size() && j < s2.size()) { | ||
if (s1[i] < s2[j]) { | ||
i++; | ||
} else if (s1[i] > s2[j]) { | ||
j++; | ||
} else { | ||
int k, m; | ||
for (k = i; k < s1.size() && s1[k] == s1[i]; k++); | ||
for (m = j; m < s2.size() && s2[m] == s2[j]; m++); | ||
res += (k-i)*(long long)(m-j); i = k; j = m; | ||
} | ||
} | ||
printf("%lld\n", res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 2014-05-01 | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
#include <algorithm> | ||
using namespace std; | ||
const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}; | ||
const int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1}; | ||
int main() { | ||
for (int cs = 1;; cs++) { | ||
int R, C; cin >> R >> C; if (R==0) return 0; | ||
vector<string> V(R); | ||
vector<vector<int> > dp(R, vector<int>(C, -1e9)); | ||
for (int i = 0; i < R; i++) { | ||
cin >> V[i]; | ||
} | ||
int res = 0; | ||
for (char c = 'A'; c <= 'Z'; c++) { | ||
for (int i = 0; i < R; i++) { | ||
for (int j = 0; j < C; j++) { | ||
if (V[i][j] != c) continue; | ||
if (c == 'A') dp[i][j] = 1; | ||
for (int k = 0; k < 8; k++) { | ||
int r = i + dx[k], c = j + dy[k]; | ||
if (r >= 0 && c >= 0 && r < R && c < C && | ||
V[r][c] == V[i][j] - 1) { | ||
dp[i][j] = max(dp[i][j], dp[r][c] + 1); | ||
} | ||
} | ||
res = max(res, dp[i][j]); | ||
} | ||
} | ||
} | ||
cout << "Case " << cs << ": " << res << endl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 2014-10-07 | ||
#include <cstdio> | ||
int a[10000]; | ||
int main() { | ||
int T; scanf("%d", &T); | ||
while (T--) { | ||
int N; scanf("%d", &N); | ||
long long res = 0; | ||
long long sum = 0; | ||
for (int i = 0; i < N; i++) { | ||
scanf("%d", a + i); | ||
if (i > 0) { | ||
res += i*(long long)a[i] - sum; | ||
} | ||
sum += a[i]; | ||
} | ||
printf("%lld\n", res); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 2008-06-16 | ||
#include <iostream> | ||
#include <cstring> | ||
#define MAX(a,b) ((a)>(b)?(a):(b)) | ||
#define MIN(a,b) ((a)<(b)?(a):(b)) | ||
#define MAXS 10000 | ||
using namespace std; | ||
struct bignum | ||
{ | ||
char* digits; | ||
int length; | ||
bignum(int size) | ||
{ | ||
length=1; | ||
digits=new char[size]; | ||
digits[0]=0; | ||
} | ||
bignum(int size,const bignum& x) //Copy constructor | ||
{ | ||
length=x.length; | ||
digits=new char[size]; | ||
memcpy(digits,x.digits,x.length); | ||
} | ||
bignum(int size,int x) | ||
{ | ||
length=0; | ||
digits=new char[size]; | ||
while (x>0) | ||
{ | ||
digits[length++]=x%10; | ||
x/=10; | ||
} | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
~bignum() | ||
{ | ||
delete digits; | ||
} | ||
void add(bignum& x) | ||
{ | ||
int l=MAX(length,x.length); | ||
int d; | ||
int carry=0; | ||
x.digits[x.length]=0; | ||
memset(digits+length,0,l-length+1); | ||
for (d=0; d<=l; d++) | ||
{ | ||
int sum=carry; | ||
if (d<length) | ||
sum+=digits[d]; | ||
if (d<x.length) | ||
sum+=x.digits[d]; | ||
digits[d]=sum; | ||
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0; | ||
} | ||
length=l; | ||
if (digits[length]) length++; | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
void sub(bignum& x) | ||
{ | ||
int d; | ||
int borrow=0; | ||
digits[length]=0; | ||
for (d=0; d<=length; d++) | ||
{ | ||
digits[d]-=borrow; | ||
if (d<x.length) | ||
digits[d]-=x.digits[d]; | ||
if (digits[d]<0) {digits[d]+=10;borrow=1;} else borrow=0; | ||
} | ||
while (length>0&&!digits[length-1]) length--; | ||
if (length==0) length=1; | ||
} | ||
void print() | ||
{ | ||
int i; | ||
for (i=length-1; i>=0; i--) | ||
putchar(digits[i]+48); | ||
} | ||
void operator=(const bignum& y) | ||
{ | ||
length=y.length; | ||
memcpy(digits,y.digits,y.length); | ||
} | ||
void operator=(const char* s) | ||
{ | ||
length=strlen(s); | ||
int i=length; | ||
while (i--) | ||
digits[i]=s[length-i-1]-48; | ||
} | ||
}; | ||
int main() | ||
{ | ||
int i,j,T; | ||
scanf("%d",&T); | ||
char s1[MAXS],s2[MAXS],s3[MAXS]; | ||
bignum n1(MAXS),n2(MAXS),n3(MAXS); | ||
for (j=0; j<T; j++) | ||
{ | ||
scanf("%s + %s = %s",&s1,&s2,&s3); | ||
//one of the numbers has a "machula" | ||
for (i=0; i<strlen(s1); i++) | ||
if (s1[i]=='m') | ||
{ | ||
n2=s2; | ||
n3=s3; | ||
n1=n3; | ||
n1.sub(n2); | ||
} | ||
for (i=0; i<strlen(s2); i++) | ||
if (s2[i]=='m') | ||
{ | ||
n1=s1; | ||
n3=s3; | ||
n2=n3; | ||
n2.sub(n1); | ||
} | ||
for (i=0; i<strlen(s3); i++) | ||
if (s3[i]=='m') | ||
{ | ||
n1=s1; | ||
n2=s2; | ||
n3=n1; | ||
n3.add(n2); | ||
} | ||
n1.print(); printf(" + "); n2.print(); printf(" = "); n3.print(); printf("\n"); | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.