Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2 problems from recruit coders, BigInt library way faster
- Loading branch information
Showing
4 changed files
with
177 additions
and
176 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
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,66 @@ | ||
#include <stdio.h> | ||
#define FOR(i,n) for(i = 0; i < n; ++i) | ||
#define DIGITS 10000 | ||
#define BASE 1000 | ||
#define BP10 3 | ||
|
||
int num1[DIGITS], num2[DIGITS]; | ||
int res[DIGITS]; | ||
int i, j; | ||
int get_last(int* a){ | ||
int i; | ||
for(i = DIGITS; i-- && !a[i]; ); | ||
return i + 1 + (i == -1); | ||
} | ||
void carry(int* a){ | ||
int c = 0; | ||
FOR(i, DIGITS){ | ||
c = (a[i] += c) / BASE; | ||
a[i] %= BASE; | ||
} | ||
} | ||
void add(int* a, int *b){ | ||
FOR(i, DIGITS) a[i] += b[i]; | ||
carry(a); | ||
} | ||
void mul(int *a, int *b){ | ||
int lim_i = get_last(a), lim_j = get_last(b); | ||
FOR(i, lim_i) FOR(j, lim_j) res[i+j] += a[i] * b[j]; | ||
carry(res); | ||
} | ||
void read(char *n, int *d){ | ||
char *last = n; | ||
int j = 0, base = 1, x; | ||
while(*last) ++last; --last; | ||
while(last >= n){ | ||
base = 1, x = 0; | ||
FOR(i, 3){ | ||
x += (*last-- - '0') * base; | ||
base *= 10; | ||
if(last < n) break; | ||
} | ||
d[j++] = x; | ||
} | ||
} | ||
void show(int *n){ | ||
i = get_last(n); | ||
printf("%d", n[--i]); | ||
while(i--) printf("%03d", n[i]); | ||
puts(""); | ||
} | ||
char n1[DIGITS], n2[DIGITS]; | ||
int main(void){ | ||
int t; scanf("%d", &t); | ||
while(t--){ | ||
for(i = 0; i < DIGITS; ++i) num1[i] = num2[i] = res[i] = 0; | ||
scanf("%s %s", n1, n2); | ||
read(n1, num1); | ||
read(n2, num2); | ||
//show(num1); puts("*"); show(num2); | ||
mul(num1, num2); | ||
show(res); | ||
//puts("-------------------"); | ||
} | ||
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,42 @@ | ||
#include <cmath> | ||
#include <cstdio> | ||
#define FOR(i,n) for(int i = 0; i < n; ++i) | ||
#define FORI(i,f,n) for(int i = f; i < n; ++i) | ||
#define FORS(i,f,n,s) for(int i = f; i < n; i += s) | ||
#define MAX(a,b) ((a) > (b) ? (a) : (b)) | ||
const int MAXP = 1<<16, ROOT = 1<<8; | ||
int pl[MAXP/10], pc = 0; | ||
bool prime[MAXP]; | ||
bool segprime[1000048]; | ||
int main(void){ | ||
FOR(i, MAXP) prime[i] = i > 1; | ||
FORS(i, 4, MAXP, 2) prime[i] = false; | ||
FORS(i, 3, ROOT, 2) if(prime[i]){ | ||
FORS(j, i*i, MAXP, i<<1) prime[j] = false; | ||
} | ||
FOR(i, MAXP) if(prime[i]) pl[pc++] = i;// printf("%d\n", i); | ||
int t; scanf("%d", &t); | ||
while(t--){ | ||
int a, b, first, from, limit, range, step, to; | ||
scanf("%d %d", &from, &to); | ||
range = to - from + 1; | ||
limit = int(floor(sqrt(to))); | ||
FOR(i, range) segprime[i] = true; | ||
if(from <= 2 && to >= 2) puts("2"); | ||
FORI(i, 1, pc){ | ||
if(pl[i] > limit) break; | ||
a = pl[i] * pl[i]; | ||
b = ((from + pl[i] - 1)/ pl[i]) * pl[i]; | ||
if(!(b & 1)) b += pl[i]; | ||
first = MAX(a, b); | ||
step = pl[i] << 1; | ||
FORS(j, first - from, range, step) segprime[j] = false; | ||
//printf("%d isnt prime due to %d\n", j + from, pl[i]); | ||
} | ||
FORS(i, !(from & 1), range, 2) if(segprime[i]) printf("%d\n", i + from); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
Empty file.