iop


私信TA

用户名:uq_96393607689

访问量:3421

签 名:

等  级
排  名 2572
经  验 2154
参赛次数 0
文章发表 13
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:举例: 

(2+5+7+9)%3=2

转换》》 

2%3=2,   5%3=2,   7%3=1,  9%3=0

2+2+1+0=5%3=2  

都能得到2,但是,万一2+2+1+0加起来的数超过了long所能存储的范围了呢? 行不通,继续转换


转换》》

2%3=2,   5%3=2,   7%3=1,  9%3=0

(2+2)%3=1   (1+1)%3=2    (2+0)%3=2

行了!这不都保证小于3了嘛!


注意事项:double的范围大。强转有损失
参考代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {        
    	Scanner sc=new Scanner(System.in);
    	int n=sc.nextInt();
    	long sum=0L;
    	for(int i=1;i<=n;i++){
    		long s1=i*1L;
    		//long s2=s1;
    		for(int j=1;j<=3;j++){
    			s1=(s1*s1)%123456789L;  			
    			//s2=(long)Math.pow(s2,2)%123456789;
    		}
    		//sum+=(long)Math.pow((long)Math.pow((long)Math.pow(i,2)%123456789L,2)%123456789L,2)%123456789L;
    		sum+=s1;
    		sum%=123456789L;
    	}
    	System.out.print(sum);
    }
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区