在hackerrank网站上,有一个名为“在数学下总结N系列”的任务。这是同一https://www.hackerrank.com/challenges/summing-the-n-series/problem
的链接我尝试了很多事情。终于到了我的一些测试用例通过的原因,不是由于超时异常。
这是完整的代码。请让我知道什么是解决方案。
public class Solution {
static int mod = 1000000007;
static int summingSeries(long t) {
long sum = 0;
for (int i = 0; i < t; i++) {
sum = ((t%mod)*(t%mod))%mod;
}
return (int)sum;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(scanner.nextLine().trim());
for (int tItr = 0; tItr < t; tItr++) {
long n = Long.parseLong(scanner.nextLine().trim());
int result = summingSeries(n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}
答案 0 :(得分:1)
终于解决了。看看。
public class Solution {
static int mod = 1000000007;
static int summingSeries(long n) {
return (int)(((n % mod) * (n % mod)) % mod);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(scanner.nextLine().trim());
for (int tItr = 0; tItr < t; tItr++) {
long n = Long.parseLong(scanner.nextLine().trim());
int result = summingSeries(n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}