C++——USACO Section 1.4 题解

来源:互联网 发布:centos mirror 163 编辑:程序博客网 时间:2024/04/30 09:09
Arithmetic Progressions

An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.

Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).

TIME LIMIT: 5 secs

PROGRAM NAME: ariprog

INPUT FORMAT

Line 1:N (3 <= N <= 25), the length of progressions for which to searchLine 2:M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M.

SAMPLE INPUT (file ariprog.in)

57

OUTPUT FORMAT

If no sequence is found, a single line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.

There will be no more than 10,000 sequences.

SAMPLE OUTPUT (file ariprog.out)

1 437 42 829 81 125 1213 1217 125 202 24

/*ID: mcdonne1PROG: ariprogLANG: C++*/#include <stdio.h>#include <assert.h>#include <string>#include <string.h>#include <algorithm>using namespace std;// open filesFILE *fin = fopen ("ariprog.in", "r");FILE *fout = fopen ("ariprog.out", "w");// global variablesunsigned int N, M, maxMM;unsigned int numbers [65000];unsigned int number_size = 0;unsigned char num_available [125001];unsigned char dist_available [125001];int have_res = 0;int skipstep = 1;// read the inputint read_input () {    fscanf (fin, "%d %d", &N, &M);    return 0;}int cmp_int (const void *a, const void *b) {    return (*(int *)a - *(int *)b);}void asm_num (int a, int b) {    for (unsigned int n = 1; n < N; n++)        if (num_available [a + n * b] == 0) return;    fprintf (fout, "%d %d\n", a, b);    have_res ++;    if (have_res==1)        skipstep = b;}void asm_num () {    for (unsigned int b = 1; b < maxMM; b+=skipstep) {        if (dist_available [b]) {            for (unsigned int p = 0; p < number_size && numbers [p] + (N -1) * b <= maxMM; p++)                asm_num (numbers [p], b);        }    }}int process () {    memset (num_available, 0, sizeof (unsigned char) * 125001);    memset (dist_available, 0, sizeof (unsigned char) * 125001);    for (unsigned int m1 = 0; m1 <= M; m1++) {        for (unsigned int m2 = m1; m2 <= M; m2++) {            int n = m1 * m1 + m2 * m2;            if (!num_available [n]) {                num_available [n] = 1;                numbers [number_size++] = n;            }        }    }    qsort (numbers, number_size, sizeof (unsigned int), cmp_int);    maxMM = M * M + M * M;    for (unsigned int n1 = 0; n1 < number_size; n1++) {        unsigned int _n1 = numbers [n1];        for (unsigned int n2 = n1 + 1; n2 < number_size && _n1 + (numbers[n2] - _n1) * (N - 1) <= maxMM; n2++) {            assert (numbers [n2] - _n1 >= 0 && numbers [n2] - _n1 < 125001);            if (num_available [_n1 + (numbers [n2] - _n1) * (N - 1)] &&                num_available [_n1 + (numbers [n2] - _n1) * (N - 2)])                dist_available [numbers [n2] - _n1] = true;        }    }    asm_num ();    if (!have_res) fprintf (fout, "NONE\n");    return 0;}int main () {    read_input ();    process ();    fclose (fin);    fclose (fout);    return 0;}
/*ID: mcdonne1PROG: ariprogLANG: C++*/#include<iostream>#include<cstdio>bool mp[125001];int n,m,k;inline bool dc(int a,int b){for(int i=1;i<n;++i)if(a+b*i>k||!mp[a+b*i])return false;for(int i=n;a+b*i<=m;++i)if(a+b*i>k||mp[a+b*i])return false;return true;}inline void put(int a,int b)    {        int num=0;char c[10]; if(a==0) putchar('0');else{while(a) c[++num]=(a%10)+48,a/=10;      while(num) putchar(c[num--]);  }    putchar(' ');    if(b==0) putchar('0');    else    {    while(b) c[++num]=(b%10)+48,b/=10;      while(num) putchar(c[num--]);    }    putchar('\n');}int main(){freopen("ariprog.in","r",stdin);freopen("ariprog.out","w",stdout);scanf("%d%d",&n,&m);for(int i=0;i<=m;++i)for(int j=0;j<=m;++j)mp[i*i+j*j]=true;k=2*m*m;bool out=false;for(int i=1;i<=k/(n-1);++i)for(int j=0;j+(n-1)*i<=k;++j)if(mp[j]&&dc(j,i)){put(j,i);out=true;}if(!out) puts("NONE");return 0;}

Mother's Milk

Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.

Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.

PROGRAM NAME: milk3

INPUT FORMAT

A single line with the three integers A, B, and C.

SAMPLE INPUT (file milk3.in)

8 9 10

OUTPUT FORMAT

A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.

SAMPLE OUTPUT (file milk3.out)

1 2 8 9 10

SAMPLE INPUT (file milk3.in)

2 5 10

SAMPLE OUTPUT (file milk3.out)

5 6 7 8 9 10

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <ctype.h>#define MAX 20typedef struct StateState;struct State {    int a[3];};int seen[MAX+1][MAX+1][MAX+1];int canget[MAX+1];Statestate(int a, int b, int c){    State s;    s.a[0] = a;    s.a[1] = b;    s.a[2] = c;    return s;}int cap[3];/* pour from bucket "from" to bucket "to" */Statepour(State s, int from, int to){    int amt;    amt = s.a[from];    if(s.a[to]+amt > cap[to])amt = cap[to] - s.a[to];    s.a[from] -= amt;    s.a[to] += amt;    return s;}voidsearch(State s){    int i, j;    if(seen[s.a[0]][s.a[1]][s.a[2]])return;    seen[s.a[0]][s.a[1]][s.a[2]] = 1;    if(s.a[0] == 0)/* bucket A empty */canget[s.a[2]] = 1;    for(i=0; i<3; i++)    for(j=0; j<3; j++)search(pour(s, i, j));}voidmain(void){    int i;    FILE *fin, *fout;    char *sep;    fin = fopen("milk3.in", "r");    fout = fopen("milk3.out", "w");    assert(fin != NULL && fout != NULL);    fscanf(fin, "%d %d %d", &cap[0], &cap[1], &cap[2]);    search(state(0, 0, cap[2]));    sep = "";    for(i=0; i<=cap[2]; i++) {if(canget[i]) {    fprintf(fout, "%s%d", sep, i);    sep = " ";}    }    fprintf(fout, "\n");    exit(0);}


原创粉丝点击