Skip to content

Commit

Permalink
commit 2012-04-20 18:49:43
Browse files Browse the repository at this point in the history
  • Loading branch information
zobayer1 committed Apr 20, 2012
1 parent 63fa5e0 commit 2169dff
Show file tree
Hide file tree
Showing 331 changed files with 21,352 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ABCDEF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
USER: zobayer
TASK: ABCDEF
ALGO: subset, binary search
*/

#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX 101

int v[2000000];

int main() {
int n, a[MAX];
int i, j, k, cnt, p, val;
scanf("%d", &n);
cnt = p = 0;
for(i=0; i < n; i++) scanf("%d",&a[i]);
for(i=0; i<n; i++) if( a[i] )
for(j=0 ; j<n; j++)
for(k=0 ; k<n; k++)
v[p++] = a[i]*(a[j]+a[k]);
sort(v, v+p);
for(i=0 ; i<n; i++) {
for(j=0 ; j<n; j++) {
for(k=0 ; k<n; k++) {
val = a[i]*a[j]+a[k];
cnt += upper_bound( v, v+p, val) - lower_bound( v, v+p, val );
}
}
}
printf("%d\n", cnt);
return 0;
}
43 changes: 43 additions & 0 deletions ABSYS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
USER: zobayer
TASK: ABSYS
ALGO: ad-hoc
*/
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;

int getint(char *s)
{
int i, n = 0;
for(i=0;s[i];i++)
{
if(!isdigit(s[i])) return -1;
n = n*10+s[i]-'0';
}
return n;
}

int main()
{
char str[100], *p;
int t, i, a[3];
scanf("%d", &t);
for(gets(str); t; t--)
{
gets(str);
gets(str);
i = 0;
p = strtok(str," +=");
while(p)
{
a[i++] = getint(p);
p = strtok(0," +=");
}
if(a[0]==-1) printf("%d + %d = %d\n",a[2]-a[1], a[1], a[2]);
else if(a[1]==-1) printf("%d + %d = %d\n",a[0], a[2]-a[0], a[2]);
else printf("%d + %d = %d\n",a[0], a[1], a[0]+a[1]);
}
return 0;
}
39 changes: 39 additions & 0 deletions ACODE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
USER: zobayer
TASK: ACODE
ALGO: dp
*/
#include <stdio.h>
#include <string.h>

#define mset(x,v) memset(x,v,sizeof(x))
#define MAX 5005
#define i64 long long
#define FS "%lld"

i64 dp[MAX];
int len;
char str[MAX];

i64 solve(int i)
{
if(i>=len) return 1;
if(dp[i]) return dp[i];
if(str[i]=='0') return 0;
i64 ret=0;
ret += solve(i+1);
if(i+1<len && 10*(str[i]-'0')+str[i+1]-'0' <= 26) ret += solve(i+2);
return dp[i]=ret;
}

int main()
{
while(scanf("%s",str)==1)
{
if(str[0]=='0') break;
len = strlen(str);
mset(dp,0);
printf(FS"\n",solve(0));
}
return 0;
}
15 changes: 15 additions & 0 deletions ACPC10A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
USER: zobayer
TASK: ACPC10A
ALGO: ad-hoc
*/
#include <stdio.h>

int main() {
int a, b, c;
while(scanf("%d%d%d", &a, &b, &c)==3 && (a|b|c)) {
if(b - a == c - b) printf("AP %d\n", c + c - b);
else printf("GP %d\n", c * c / b);
}
return 0;
}
35 changes: 35 additions & 0 deletions ACPC10D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
USER: zobayer
TASK: ACPC10D
ALGO: dp
*/
#include <stdio.h>

const int inf = 0x7f7f7f7f;
int tri[100000][3];

inline int min(int a, int b) {
return a < b ? a : b;
}

int main() {
int n, i, j, a, b, c, d, cs = 1;
while(scanf("%d", &n)==1 && n) {
for(i = 0; i < n; i++)
for(j = 0; j < 3; j++)
scanf("%d", &tri[i][j]);
tri[0][0] = inf;
tri[0][2] += tri[0][1];
for(i = 1; i < n; i++) {
for(j = 0; j < 3; j++) {
a = (j==2)? inf : tri[i-1][j+1];
b = tri[i-1][j];
c = (j==0)? inf : tri[i-1][j-1];
d = (j==0)? inf : tri[i][j-1];
tri[i][j] += min(min(a, b), min(c, d));
}
}
printf("%d. %d\n", cs++, tri[n-1][1]);
}
return 0;
}
19 changes: 19 additions & 0 deletions ACPC10E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
USER: zobayer
TASK: ACPC10E
ALGO: math
*/
#include <cstdio>
using namespace std;

int main() {
int g, a, t, d, i;
long long p, k;
while(scanf("%d%d%d%d", &g, &t, &a, &d)==4 && g > 0) {
k = (long long) g * a + d;
for(i = 0; (1LL<<i) < k; i++);
p = (long long) t * (t-1) / 2 * g + (1LL<<i) - 1;
printf("%d*%d/%d+%d=%lld+%lld\n", g, a, t, d, p, (1LL<<i) - k);
}
return 0;
}
61 changes: 61 additions & 0 deletions ACS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
USER: zobayer
TASK: ACS
ALGO: optimization, simulation
*/
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAXR 1234
#define MAXC 5678

int R[MAXR+1], C[MAXC+1], cR[MAXR+1], cC[MAXC+1];

inline int m(int x, int y)
{
return (R[x]-1)*MAXC + C[y];
}

inline void getxy(int w, int &x, int &y)
{
int p = (w+MAXC-1) / MAXC;
int q = (w-1) % MAXC + 1;
x = cR[p];
y = cC[q];
}

int main()
{
int i, x, y, w;
char s[5];
for(i=1; i<=1234; i++) R[i] = cR[i] = i;
for(i=1; i<=5678; i++) C[i] = cC[i] = i;
while(scanf("%s", s)==1)
{
switch(s[0])
{
case 'R':
scanf("%d %d", &x, &y);
swap(R[x], R[y]);
cR[R[x]] = x;
cR[R[y]] = y;
break;
case 'C':
scanf("%d %d", &x, &y);
swap(C[x], C[y]);
cC[C[x]] = x;
cC[C[y]] = y;
break;
case 'Q':
scanf("%d %d", &x, &y);
printf("%d\n", m(x, y));
break;
case 'W':
scanf("%d", &w);
getxy(w, x, y);
printf("%d %d\n", x, y);
}
}
return 0;
}
20 changes: 20 additions & 0 deletions ACT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
USER: zobayer
TASK: ACT
ALGO: ad-hoc
*/
#include <stdio.h>
#include <string.h>

int main()
{
char str[50005];
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d %s", &n, str);
printf("%c\n", str[strlen(str)-1]);
}
return 0;
}
118 changes: 118 additions & 0 deletions ADDREV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
USER: zobayer
TASK: ADDREV
ALGO: big integer
*/
#include <cstdio>
#include <cstring>
using namespace std;

char num1[222], num2[222], res[222];

void cutzero_in(int len1, int len2);
void cutzero_out(int k);
void reverse_in(int len1, int len2);
void rev_add();

int main()
{
int t, len1, len2;
scanf("%d",&t);
for(;t;t--)
{
scanf("%s%s",num1,num2);
len1 = strlen(num1);
len2 = strlen(num2);
reverse_in(len1, len2); // As the numbers to be reversed
rev_add();
printf("%s\n",res); // In case of normal addition, res is to be reversed
}
return 0;
}

void cutzero_in(int len1, int len2)
{
int i, j;
//Eleminating leading zeroes in reversed num1
for(i=0;i<len1-1;i++)
{
if(num1[i]!='0')
break;
}
if(i!=0)
for(j=0;i<=len1;i++,j++)
num1[j] = num1[i];
//Eleminating leading zeroes in reversed num2
for(i=0;i<len2-1;i++)
{
if(num2[i]!='0')
break;
}
if(i!=0)
for(j=0;i<=len2;i++,j++)
num2[j] = num2[i];
}

void cutzero_out(int k)
{
int i, j;
for(i=0;i<k-1;i++)
{
if(res[i]!='0')
break;
}
if(i!=0)
for(j=0;i<=k;i++,j++)
res[j] = res[i];
}

void reverse_in(int len1, int len2)
{
int i, j;
for(i=0,j=len1-1;i<j;i++,j--)
{
num1[i] ^= num1[j];
num1[j] ^= num1[i];
num1[i] ^= num1[j];
}
for(i=0,j=len2-1;i<j;i++,j--)
{
num2[i] ^= num2[j];
num2[j] ^= num2[i];
num2[i] ^= num2[j];
}
cutzero_in(len1,len2);
}

void rev_add()
{
int sum, carry = 0, k = 0, i, j,len1 = strlen(num1), len2 = strlen(num2);
for(i=len1-1,j=len2-1;i>=0&&j>=0;i--,j--)
{
sum = (num1[i]-'0')+(num2[j]-'0')+carry;
res[k++] = sum%10 + '0';
carry = sum/10;
}
if(j==-1)
while(i>=0)
{
sum = carry+num1[i--]-'0';
res[k++] = sum%10+'0';
carry = sum/10;
}
else if(i==-1)
while(j>=0)
{
sum = carry+num2[j--]-'0';
res[k++] = sum%10+'0';
carry = sum/10;
}
while(carry)
{
res[k++] = carry%10+'0';
carry /=10;
}
res[k] = '\0';
// The reverse of res is the normal sum, so no need to reverse it again
cutzero_out(k);
}
Loading

0 comments on commit 2169dff

Please sign in to comment.