[shkola] Polska NOI - Zadachi + Resheniq
- From: Ivaylo Riskov <ivaylo_riskov@xxxxxxx>
- To: shkola@xxxxxxxxxxxxx
- Date: Wed, 2 Oct 2002 00:42:33 +0300
Towa sa zadachite ot Polskata olimpiada, za koito goworih dnes!
Chetiri zadachi + 3 resheniq na C.
Chetwxrtoto(towa na "Game") reshih da ne wi go izprashtam ponezhe
hwashta samo 4 ot 11 testa, a i poluchih predloveniq za podobrqwane.
Slovih dopxlnitelno nqkoi komentari kxm sorsowete. Wsichki tezi
resheniq rabotqt i hwashtat wsichki testowa za sxotwetnata zadacha!
Ako ima wxprosi pishete!
Pozdrawi,
Ivo
P.S. Ako nqkoi zhelae mozhe da mu pratq link kxm testowete na
zadachite!
P.S.S. Shte wi pratq i zadachite ot proletniq turnir Shumen 2001 za
9-10 klas kogato napisha i ostanalite dwe
==============================================================
Ivaylo Riskov <ivaylo_riskov@xxxxxxx>
"Stenderup's Law:
The sooner you fall behind, the more time you will have to
catch up"
==============================================================
/* Polish National Olympiad 2000/2001 Stage 1 */
/* Antiprime Numbers */
#include <stdio.h>
#define NUMBER(x, y) number[x]*primes[y]
int primes[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
long long number[100];
int num_divs[100];
int powers[100][10];
int found;
int main() {
FILE *f;
int i, j, besti, bestj, first;
int new_divs, n, best_divs;
f = fopen("ant.in", "rt");
fscanf(f, "%i", &n);
fclose(f);
//first anti-prime number is 1
found = 0;
number[0] = 1;
num_divs[0] = 1;
for (i = 0; i < 10; i++)
powers[0][i] = 0;
//find next anti-prime number
do {
first = 1;
//for each number found so far try to increase each one of its
powers
for (i = 0; i <= found; i++)
for (j = 0; j < 10; j++) {
//get new number of dividers
new_divs = num_divs[i]*(powers[i][j] +
2)/(powers[i][j] + 1);
//should be greater then the number of dividers
of the
//last anti-prime and it should be the lowest
number
//with that property
if (new_divs > num_divs[found] &&
(first || NUMBER(i, j) < NUMBER(besti,
bestj)))
besti = i,
bestj = j,
first = 0,
best_divs = new_divs;
}
//we've found it - add it to the list
number[++found] = NUMBER(besti, bestj);
num_divs[found] = best_divs;
for (i = 0; i < 10; i++)
powers[found][i] = powers[besti][i];
powers[found][bestj]++;
//if our number is greater than n - stop
} while (number[found] <= n);
f = fopen("ant.out", "wt");
fprintf(f, "%Li\n", number[found - 1]);
fclose(f);
return 0;
}
/* Polish National Olympiad 2000/2001 Stage 1 */
/* Density Map */
#include <stdio.h>
int map[250][250];
int n, r;
int main() {
FILE *f;
int i, j;
int left[250];
int right[250];
f = fopen("map.in", "rt");
fscanf(f, "%i %i", &n, &r);
for (j = 0; j < n; j++)
for (i = 0; i < n; i++)
fscanf(f, "%i", map[i] + j);
fclose(f);
n--;
for (j = 0; j <= n; j++) {
//fill left values for the given row
for (left[0] = 0, i = 1; i <= n; i++) {
left[i] = left[i - 1] + map[i - 1][j];
if (i > r)
left[i] -= map[i - r - 1][j];
}
//fill right values for the given row
for (right[n] = 0, i = n - 1; i >= 0; i--) {
right[i] = right[i + 1] + map[i + 1][j];
if (n - i > r)
right[i] -= map[i + r + 1][j];
}
//add up these values to our initial values
for (i = 0; i <= n; i++)
map[i][j] += right[i] + left[i];
}
//do this vertically as well
for (i = 0; i <= n; i++) {
//fill up values for a column
for (left[0] = 0, j = 1; j <= n; j++) {
left[j] = left[j - 1] + map[i][j - 1];
if (j > r)
left[j] -= map[i][j - r - 1];
}
//fill down values for a column
for (right[n] = 0, j = n - 1; j >= 0; j--) {
right[j] = right[j + 1] + map[i][j + 1];
if (n - j > r)
right[j] -= map[i][j + r + 1];
}
//add up and down values
for (j = 0; j <= n; j++)
map[i][j] += right[j] + left[j];
}
f = fopen("map.out", "wt");
for (j = 0; j <= n; j++)
for (i = 0; i <= n; i++)
fprintf(f, "%i%s", map[i][j], (i == n ? "\n" : " "));
fclose(f);
return 0;
}
/* Polish National Olympiad 2000/2001 Stage 1 */
/* Intervals */
#include <stdio.h>
#include <stdlib.h>
int arr[50000][2];
int n;
int fcmp(const void *e1, const void *e2) {
int diff;
diff = *(int *)e1 - *(int *)e2;
if (!diff)
diff = *((int *)e1 + 1) - *((int *)e2 + 1);
return diff;
}
int main() {
int i, j;
FILE *f;
f = fopen("prz.in", "rt");
fscanf(f, "%i", &n);
for (i = 0; i < n; i++)
fscanf(f, "%i %i", arr[i], arr[i] + 1);
fclose(f);
//sort it by first value
qsort(arr, n, sizeof(int)*2, fcmp);
//i - index of the interval being processed
//j - index of the interval we process against
for (j = 0, i = 1; i < n; i++)
if (arr[i][0] > arr[j][1]) {
j++;
arr[j][0] = arr[i][0];
arr[j][1] = arr[i][1];
}
else
if (arr[i][1] > arr[j][1])
arr[j][1] = arr[i][1];
f = fopen("prz.out", "wt");
for (i = 0; i <= j; i++)
fprintf(f, "%i %i\n", arr[i][0], arr[i][1]);
fclose(f);
return 0;
}
Title: VIII OI, Intervals
Intervals
TaskWrite a program which:
InputIn the first line of the text file PRZ.IN there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)-st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval, 1 <= ai <= bi <= 1000000. OutputThe text file PRZ.IN should contain descriptions of all computed pairwise non-intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output file in ascending order. ExamlpeFor the input file PRZ.IN: 5 5 6 1 4 10 10 6 9 8 10 the correct answer is the output file PRZ.OUT: 1 4 5 10 |
||||||
Antiprime numbers
A positive integer n is an antiprime number, when it has more divisors than any positive integer, that is less thann. These are examples of antiprime numbers: 1, 2, 4, 6, 12 and 24. TaskWrite a program which:
InputIn the only line of the input file ANT.IN there is one integer n, 1 <= n <= 2 000 000000. OutputIn the only line of the output file ANT.OUT your program should write exactly one integer - the greatest antiprime number not greater than n. ExampleFor the input file ANT.IN: 1000 the correct answer is the output file ANT.OUT: 840 |
||||||
Green game
"Green game" is a game for two players, say Ann and Billy. Their task is to shift a pawn on the board. At the beginning of the game we put a pawn on the arbitrarily chosen start fieldP, then players shiftthe pawn by turns from their field to any successor of this field (we knowit belongs to the opponent). The game is started by an owner of the start field P. The game is finished whenthe pawn stays for the second time on the same field, say the fieldQ. If in the sequence of moves from the field Q to the field Q taken for the second time, the pawn was put at least once on the green field, Ann wins the game,otherwise Billy wins. We say that Ann has a winning strategy for the given start field P in case when there is such a method, which guarantees that she wins the game beginning from thisfield, no matter what movesBilly makes. TaskWrite a program which:
InputIn the first line of the text file GRA.IN there are written two positive integersa, b, separated by a single space, meaning respectively: the number of fields belonging to Ann, the number of fields belonging to Billy. Integersa, b satisfy the condition: 1 <= a+b <= 3000. In the followinga+b lines there are descriptions of the fields of the board: first,descriptions of fields belonging to Ann, and then, of ones belonging to Billy. The(i+1)-st line, for 1 <= i <= a+b, begins withintegers z, k meaning respectively the colour of the field i (0 means white, 1 - green) and the number of successors of this field. Then k integers (1 <= k < a+b) are written (stillin the same line). They are the numbers denoting the successors of the i-th field. Theintegers in each line are separated by single spaces. The number of green fields on the board is not greater than 100.The total number of successors of all the fields on the board is not grater than 30000. OutputThe first line of the text file GRA.OUT should contain exactly one integer l, which indicates the number of fields for which Ann hasa winning strategy. The following l lines should contain numbers of these fields written in ascending order - each integer should be written in a separate line. ExampleFor the input file GRA.IN: 5 3 0 2 6 7 0 3 6 7 8 0 1 8 1 1 7 1 1 8 1 2 1 2 0 2 1 2 0 2 3 4 the correct answer is the output file GRA.OUT: 5 1 2 4 6 7 |
||||||
Density map
There are given:
If [i, j] and [i', j'] are two positions in the tableF, thedistance between them is max(|i - i'|, |j -j'|). The table W, n x n, should be computed, where W[i, j](the number in i-th column and j-th line of the table W) is equalto the sum of all the numbers F[x,y], such that the distance between[x,y] and [i,j] is not greater than r. TaskWrite a program, which:
InputIn the first line of the text file MAP.IN there are two positive integersseparated by a single space: n and r, where 0 <= r < n <= 250. In the following n lines the table F is described. Each of these lines contains n integers from the set {0,1}, separated by single spaces. The i-th number written in(j+1)-st line is equal to F[i, j]. OutputThe text file MAP.OUT should contain exactly n lines. In the j-th linethe values W[1, j]...W[n, j] should be written respectively;they should be separated by single spaces. ExampleFor the input file MAP.IN: 5 1 1 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 The correct answer is the output file MAP.OUT: 3 4 2 2 1 4 5 2 2 1 3 4 3 3 2 2 2 2 2 2 1 1 2 2 2 |
||||||
Other related posts:
- » [shkola] Polska NOI - Zadachi + Resheniq