湘潭oj1203/邀请赛A称号 数论+java睑板

乞讨 n%1+n%2+n%3+n%4+.........n%n=,n<=10^12次要。

一味的找规律之初。没有发现。后来,前辈执教后,人才平淡,所以,现在唯一明确的。


首先在地图上:


对于该题,在求区间(根号n,n),因为n%i=n-i*x(这里x是从1枚举到根号n,每一个k 相应n/(x+1)~n/x区间内。因为是等差数列(还是递减)。直接用公式求和)。

哎(根号n,n)区间是被切割来求得。分成根号n次。

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
public class Main{

	public static void main(String[] args) {
		int T;
	    Scanner in=new Scanner(System.in);
	    T=in.nextInt();
	    for(int ii=1;ii<=T;ii++)                
	    {
	          long n;                                    
	          BigInteger sum=new BigInteger("0");
	           n=in.nextLong();
	           long lasta=2*n;
	        for(long i=1;i*i<n;i++)
	        {
	        	 sum=sum.add(BigInteger.valueOf(n%i));
	        	 if((i+1)>=lasta){break;}              //边界推断
	        	  long b=n/i;
	              long a=n/(i+1)+1;
	              BigInteger temp1=BigInteger.valueOf(n);         //将一个 long型数据转为biginteger
	              temp1=temp1.multiply(BigInteger.valueOf((b-a+1)));   
	              BigInteger temp2=BigInteger.valueOf(b+a);
	              temp2=temp2.multiply(BigInteger.valueOf(i));    //*
	              temp2=temp2.multiply(BigInteger.valueOf(b-a+1));   
	              temp2=temp2.divide(BigInteger.valueOf(2));   //除以
	              temp1=temp1.subtract(temp2);                 // -
	            sum=sum.add(temp1);                             //+
	           lasta=a; 
	        }
	         System.out.println("Case "+ii+": "+sum);         //Java的println自己主动带换行。注意!
	    }

	}

}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mengfanrong/p/4742344.html