2016 pku campusD/OpenJ_POJ - C16D(规律+大数)
- 总时间限制:
- 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.#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]);
}
}
}
}
}