Java Blue Bridge Cup Group B Test Problem G: Eight Summations

Java Blue Bridge Cup Group B Test Problem G: Eight Summations

Time limit: 1.0s Memory limit: 512.0MB Total score for this question: 20 points

【Problem Description】

Given a positive integer n, find the eighth power of 1, the eighth power of 2, the eighth power of 3, the eighth power of 4, ,, and the result of modulo 123456789. Among them, mod means take
remainder.
[Input format]
The first line of input contains an integer n.
[Output format]
Output one line, including an integer, representing the answer.
[Sample input]
2
[Sample output]
257
[Sample input]
987654
[Sample output]
43636805
[Evaluation use case scale and conventions]
For 20% of evaluation use cases, 1 ≤ n ≤ 20.
For 60% of the evaluation use cases, 1 ≤ n ≤ 1000.
For all evaluation use cases, 1 ≤ n ≤ 1000000.

/**		试题 G:  八次求和 时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分 
【问题描述】 给定正整数 n, 求 1^8 + 2^8 + · · · + n^8 mod 123456789 。其中 mod 表示取 余。 
【输入格式】 输入的第一行包含一个整数 n。 
【输出格式】 输出一行,包含一个整数,表示答案。 
【样例输入】 2 
【样例输出】 257 
【样例输入】 987654 
【样例输出】 43636805 
【评测用例规模与约定】 
对于 20% 的评测用例,1 ≤ n ≤ 20。 
对于 60% 的评测用例,1 ≤ n ≤ 1000。 
对于所有评测用例,1 ≤ n ≤ 1000000。*/ 
import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		int mod=123456789;
		Scanner s = new Scanner(System.in);
		int n=s.nextInt();long result=0;
		for(long i=1;i<=n;i++){
    
    //遍历1~n
			long temp=1;
			for(int j=1;j<=4;j++){
    
    //对每一个 i 进行八次乘方,四次循环,每次循环里面计算一次平方
				temp =(temp*((i*i) %mod))%mod; //计算平方并取模,防止溢出			}
			result=(result+temp)%mod;//把每一次  i 循环得到的八次方结果汇总到变量  result 中。
		}
		System.out.println(result);//打印输出  resualt
	}
}

Friendship connection, please click

Guess you like

Origin blog.csdn.net/DAurora/article/details/109066639