杭电ACM 解题报告 - neuxxm's Blog - 本为贵公子,平生实爱才。感时思报国,拔剑起蒿莱。

杭电ACM 1028 Ignatius and the Princess III

Ignatius and the Princess III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4193    Accepted Submission(s): 2931



 

Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
 


 

Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
 


 

Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
 


 

Sample Input

	
4
10
20
 
Sample Output

	
5
42
627

 

#include <stdio.h>
#include <string.h>
#define N 120
int c1[N+1], c2[N+1];
int main(){
    int i, j, k, n;
    for (i=0; i<=N; ++i){
        c1[i] = 1;
    }
    for (k=2; k<=N; ++k){
        memset(c2, 0, sizeof(c2));
        for (i=0; i<=N; ++i){
            for (j=i; j<=N; j+=k){
                c2[j] += c1[i];
            }
        }
        memcpy(c1, c2, sizeof(c2));
    }
    while (scanf("%d", &n) == 1){
        printf("%d\n", c1[n]);
    }
    return 0;
}

杭电ACM 1021 Fibonacci Again

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14170    Accepted Submission(s): 6581



 

Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
 


 

Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
 


 

Output
Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.
 


 

Sample Input

	
0
1
2
3
4
5
 
Sample Output

	
no
no
yes
no
no
no

 

#include <stdio.h>
int main(){
    int n, t;
    while (scanf("%d", &n) == 1){
        t = n&7;
        printf("%s\n", t==2 || t==6 ? "yes" : "no");
    }
    return 0;
}

杭电ACM 1019 Least Common Multiple

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10956    Accepted Submission(s): 3953



 

Problem Description
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.
 
 


 

Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
 


 

Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
 


 

Sample Input

	
2
3 5 7 15
6 4 10296 936 1287 792 1
 
Sample Output

	
105
10296

 

#include <stdio.h>
int gcd(int x, int y){
    if (y == 0){
        return x;
    }
    int i = x&1, j = y&1;
    return i ? j ? (x>y ? gcd(y, x-y) : gcd(x, y-x)) : gcd(x, y>>1): j ? gcd(x>>1, y) : gcd(x>>1, y>>1)<<1;
}
int lcm(int x, int y){
    return x/gcd(x, y)*y;
}
int main(){
    int z, n, t, r;
    scanf("%d", &z);
    while (z-- != 0){
        scanf("%d", &n);
        scanf("%d", &r);
        while (--n != 0){
            scanf("%d", &t);
            r = lcm(r, t);
        }
        printf("%d\n", r);
    }
    return 0;
}

杭电ACM 1017 A Mathematical Curiosity

A Mathematical Curiosity

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10170    Accepted Submission(s): 3078



 

Problem Description
Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a^2+b^2 +m)/(ab) is an integer.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 


 

Input
You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.
 


 

Output
For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case on one line in the format as shown below.
 


 

Sample Input

	
1

10 1
20 3
30 4
0 0
 
Sample Output

	
Case 1: 2
Case 2: 4
Case 3: 5

 

#include <stdio.h>
int a2b2[101][101];
int ab[101][101];
int a[101];
int main(){
    int zz, z, n, m, i, j, b, r;
    for (i=1; i<=100; ++i){
        a[i] = i*i;
    }
    for (i=1; i<=100; ++i){
        for (j=1; j<=100; ++j){
            a2b2[i][j] = a[i]+a[j];
            ab[i][j] = i*j;
        }
    }
    b = 0;
    scanf("%d", &zz);
    while (zz-- != 0){
        if (b){
            printf("\n");
        }
        b = 1;
        z = 0;
        for (;;){
            scanf("%d %d", &n, &m);
            if (n==0 && m==0){
                break;
            }
            r = 0;
            for (i=1; i<n; ++i){
                for (j=i+1; j<n; ++j){
                    if ((a2b2[i][j]+m)%ab[i][j] == 0){
                        ++r;
                    }
                }
            }
            printf("Case %d: %d\n", ++z, r);
        }
    }
    return 0;
}

杭电ACM 1014 Uniform Generator

Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5668    Accepted Submission(s): 2252



 

Problem Description
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

seed(x+1) = [seed(x) + STEP] % MOD

where '%' is the modulus operator.

Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.

For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.

If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.

Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.
 


 

Input
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).
 


 

Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line.
 


 

Sample Input

	
3 5
15 20
63923 99999
 
Sample Output

	
         3         5    Good Choice

        15        20    Bad Choice

     63923     99999    Good Choice

 

#include <stdio.h>
int gcd(int x, int y){
    if (y == 0){
        return x;
    }
    int i = x&1, j = y&1;
    return i ? j ? (x>y ? gcd(y, x-y) : gcd(x, y-x)) : gcd(x, y>>1) : j ? gcd(x>>1, y) : gcd(x>>1, y>>1)<<1;
}
int main(){
    int x, y;
    while (scanf("%d %d", &x, &y) == 2){
        printf("%10d%10d    %s\n\n", x, y, gcd(x, y)==1 ? "Good Choice" : "Bad Choice");
    }
    return 0;
}

杭电ACM 1013 Digital Roots

Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19299    Accepted Submission(s): 5632



 

Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
 


 

Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
 


 

Output
For each integer in the input, output its digital root on a separate line of the output.
 


 

Sample Input

	
24
39
0
 
Sample Output

	
6
3

 

#include <stdio.h>
int f(int n){
    if (n <= 9){
        return n;
    }
    int r = 0;
    while (n > 0){
        r += n%10;
        n /= 10;
    }
    return f(r);
}
int main(){
    char s[1025];
    int i, n;
    while (scanf("%s", s) == 1){
        n = 0;
        for (i=0; s[i]; ++i){
            n += s[i]-'0';
        }
        if (n == 0){
            break;
        }
        printf("%d\n", f(n));
    }
    return 0;
}

杭电ACM 1012 u Calculate e

u Calculate e

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11752    Accepted Submission(s): 5028



 

Problem Description
A simple mathematical formula for e is



where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
 


 

Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
 


 

Sample Output

	
n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

 

#include <stdio.h>
int main(){
    double sum;
    int n, i, l;
    printf("n e\n");
    printf("- -----------\n");
    printf("0 1\n");
    sum = 1;
    l = 1;
    for (n=1; n<=9; ++n){
        l *= n;
        sum += 1.0/l;
        if (n <= 2){
            printf("%d %g\n", n, sum);
        }
        else{
            printf("%d %.9lf\n", n, sum);
        }
    }
    return 0;
}

杭电ACM 1008 Elevator

Elevator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15522    Accepted Submission(s): 8168



 

Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
 


 

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
 


 

Output
Print the total time on a single line for each test case.
 


 

Sample Input

	
1 2
3 2 3 1
0
 
Sample Output

	
17
41

 

#include <stdio.h>
int main(){
    int n, t, l, r;
    while (scanf("%d", &n) == 1){
        if (n == 0){
            break;
        }
        r = 0;
        l = 0;
        while (n-- != 0){
            scanf("%d", &t);
            if (t > l){
                r += (t-l)*6;
            }
            else{
                r += (l-t)*4;
            }
            r += 5;
            l = t;
        }
        printf("%d\n", r);
    }
    return 0;
}

杭电ACM 1005 Number Sequence

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38553    Accepted Submission(s): 8150



 

Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).
 


 

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
 


 

Output
For each test case, print the value of f(n) on a single line.
 


 

Sample Input

	
1 1 3
1 2 10
0 0 0
 
Sample Output

	
2
5

 

#include <stdio.h>
#include <string.h>
int main(){
    int b[7][7];
    int a[100];
    int n;
    int x, y, m, st, len;
    while (scanf("%d %d %d", &x, &y, &m) == 3){
        if (x==0 && y==0 && m==0){
            break;
        }
        a[1] = 1;
        a[2] = 1;
        memset(b, 0, sizeof(b));
        b[1][1] = 1;
        n = 3;
        for (;;){
            a[n] = (a[n-1]*x+a[n-2]*y)%7;
            if (b[a[n-1]][a[n]] != 0){
                break;
            }
            b[a[n-1]][a[n]] = n-1;
            ++n;
        }
        st = b[a[n-1]][a[n]];
        len = n-1-st;
        if (m < st){
            printf("%d\n", a[m]);
        }
        else{
            printf("%d\n", a[st+(m-st)%len]);
        }
    }
    return 0;
}

杭电ACM 1004 Let the Balloon Rise

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28869    Accepted Submission(s): 9266



 

Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
 


 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 


 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 


 

Sample Input

	
5
green
red
blue
red
red
3
pink
orange
pink
0
 
Sample Output

	
red
pink

 

#include <stdio.h>
#include <string.h>
#define N 390
const char C = 'a'-1;
struct bal_t{
    char color[16];
    int fre;
}bal[N+1];
int hash(char s[]){
    int r = 0;
    char *p = s;
    char c;
    while ((c=*p++) != '\0'){
        r += c-C;
    }
    return r;
}
int main(){
    char s[16];
    int n, i, t, max, max_i;
    while (scanf("%d", &n) == 1){
        if (n == 0){
            break;
        }
        for (i=0; i<=N; ++i){
            bal[i].fre = 0;
        }
        while (n-- != 0){
            scanf("%s", s);
            t = hash(s);
            if (bal[t].fre != 0){
                ++bal[t].fre;
            }
            else{
                strcpy(bal[t].color, s);
                bal[t].fre = 1;
            }
        }
        max = 0;
        for (i=0; i<=N; ++i){
            if (bal[i].fre > max){
                max = bal[i].fre;
                max_i = i;
            }
        }
        printf("%s\n", bal[max_i].color);
    }
    return 0;
}




Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee