2016 pku campusD/OpenJ_POJ

题面:

总时间限制:
1000ms
内存限制:
262144kB
描述

As we all know, to pass PE exams, students need to do extracurricular sports, especially jogging. As the result, the Jogging Association in the university is very popular.

There are N students in the Jogging Association. Some run faster while the others run slower. The time it takes for a student to run exactly one lap is called his/her ''lap time'', which is always a positive integer. The lap times of students are distinct from each other. However, running too slow is not allowed for students of the Jogging Association, so each lap time is at most 100 digits.

One day, they make an appointment to jog together. They begin at the same time as well as the same starting point, jog along the circular runway at their own steady speed round and round, not stop until the first time when all of them meet again at the starting point. When they stop, they accidently find that the time they have spent is precisely equal to the time they need if everyone runs a lap one by one, as a relay race.

Now we are curious about the lap times of all the students. Can you guess out any possible solution?

输入The first line contains an integer T (1 ≤ T ≤ 30), indicating the number of test cases.

For each test case:

A line contains an integer N (2 ≤ N ≤ 200), indicating the number of students.输出For each test case:

If the solution does not exist, output -1 on a single line;

Otherwise, output one solution with n different integers t1,t2,...,tn, one per line, where ti (1 ≤ ti < 10^100) indicates the lap time of the i-th student. If there are several solutions, output any one.样例输入

13

样例输出

102030

提示For the three students in the Sample Output, they will stop at time 60, which is precisely equal to the time they need if they run as a relay race.

    题面描述:有n个学生,询问你是否存在n个数,使得n个数的最小公倍数等于n个数的和。
    题目分析:毫无疑问,这道题就是一个找规律的构造题。因此这题的解法就是通过打表寻找规律。
    我们可以发现,当n等于2的时候是不存在的,而 n=3 有解 1 2 3 正好是 6, n=4 有解 1 2 6 9 和为 18, 假设 n=k-2 有解且和为 a, 这令 b=2a, c=3a, 则有 a+b+c=6a, 而 lcm(a,b,c)=lcm(a,2a,3a)=a*lcm(1,2,3)=6a, 故 n=k 有解, 由此数学归纳法可知 n>=3 均有解。
    因此我们可以发现,这道题的规律就是:以1 2 3 为底每次将1 2
    下面附上Java和C++的两份高精度代码:
    C++:
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=207;
int a[maxn][maxn],len[maxn];
int ans[maxn][maxn],len2[maxn];
int main(){
    len[3]=1;a[3][1]=6;
    for (int i=4;i<maxn;i++){
        for (int j=1;j<=len[i-1];j++){
            a[i][j]=a[i-1][j]*3;
        }
        int l=1;
        while (!(a[i][l]==0&&l>len[i-1])){
            if (a[i][l]>=10){
                a[i][l+1]+=a[i][l]/10;
                a[i][l]%=10;
            }
            l++;
            len[i]=l;
        }
        len[i]--;
    }
    len2[3]=1;ans[3][1]=3;
    for (int i=4;i<maxn;i++){
        for (int j=1;j<=len[i-1];j++){
            ans[i][j]=ans[i-1][j]*3;
        }
        int l=1;
        while (!(ans[i][l]==0&&l>len2[i-1])){
             if (ans[i][l]>=10){
                ans[i][l+1]+=ans[i][l]/10;
                ans[i][l]%=10;
             }
            l++;
            len2[i]=l;
        }
        len2[i]--;
    }
    int t;
    cin>>t;
    while (t--){
        int n;
        cin>>n;
        if (n<=2) {
            cout<<-1<<endl;
            continue;
        }
        cout<<1<<endl<<2<<endl;
        for (int i=3;i<n;i++){
            for (int j=len[i];j>=1;j--) cout<<a[i][j];
            cout<<endl;
        }
        for (int i=len2[n];i>=1;i--) cout<<ans[n][i];
        cout<<endl;
    }
    return 0;
}
    Java:
import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sca=new Scanner(System.in);
		BigInteger s[]=new BigInteger[300];
		int t;
		t=sca.nextInt();
		while(t-->0) {
			int n=sca.nextInt();
			int cnt=3;
			if(n==2) {
				System.out.println(-1);
				continue;
			}
			else {
				s[1]=BigInteger.valueOf(1);
				s[2]=BigInteger.valueOf(2);
				s[3]=BigInteger.valueOf(3);
				for(int i=4;i<=n;i++) {
					for(int j=1;j<=cnt;j++) {
						s[j]=s[j].multiply(BigInteger.valueOf(3));
					}
					s[++cnt]=BigInteger.valueOf(2);
					s[1]=BigInteger.valueOf(1);
				}
				for(int i=1;i<=cnt;i++) {
					System.out.println(s[i]);
				}
			}
		}
	}
}

原文地址:https://www.cnblogs.com/Chen-Jr/p/11007293.html