这是第一题简单的数据在
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
BigInteger m;
int Max = (int) Math.pow(2, 32);
int sum;
System.out.println( (Math.pow(10, 9)*Math.pow(10, 9))>Math.pow(2, 32));
while (scanner.hasNext()) {
n = scanner.nextInt();
m = new BigInteger(scanner.nextInt()+"");
sum = 0;
BigInteger a[] = new BigInteger[n+1];
a[0] = m;
for (int i = 1; i < a.length; i++) {
if(i%2 == 1){
if(i==1){
a[i] = 1*m;
}else{
a[i] = a[i-1]*m<0?((a[i-1]%Max)*(m%Max))%Max:a[i-1]*m;
}
}else{
a[i] = (a[i-1]*m-a[i-2])<0?((a[i-1]%Max)*(m%Max))%Max:(a[i-1]*m-a[i-2]);
}
}
for (int i = 1; i < a.length; i++) {
sum = (sum+a[i])<0?(sum%Max+a[i]%Max)%Max:(sum+a[i]);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println(sum);
}
}
还有中等的和困难的都只过了一组数据。。
不知为什么错了,希望有人能指出来。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
BigInteger m;
BigInteger Max = new BigInteger(Integer.MAX_VALUE+"");
BigInteger sum;
while (scanner.hasNext()) {
n = scanner.nextInt();
m = scanner.nextBigInteger();
sum = new BigInteger("0");
BigInteger a[] = new BigInteger[n+1];
a[0] = m;
for (int i = 1; i < a.length; i++) {
if(i%2 == 1){
if(i==1){
a[i] = m;
}else{
a[i] = a[i-1].multiply(m);
}
}else{
a[i] = a[i-1].multiply(m).subtract(a[i-2]);
}
}
for (int i = 1; i < a.length; i++) {
sum = sum.add(a[i]).mod(Max);
}
/*for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}*/
System.out.println(sum);
}
}
}
只过了一组。。。