题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1168

题意:给定一个n,注意这里n小于10^12,求

分析:早些时候就做过一道题,在这里:http://blog.csdn.net/acdreamers/article/details/8809517

由于当时这题数据小,直接计算步长更方便,但是对于本题由于10^12就不合适了,那么我们就采用第二种方法,现在我就分别

简略说说这两种方法,其实大体思路差不多,只是处理不一样。

首先是不可能直接枚举的,那么我们先把写成,所以问题变为求:

由于在一定范围内是保持不变的,所以我们接下来可以把时间复杂度降到

现在我们简略模拟一下:比如n=30,那么我们可以划分等价类:

可以看出,我们第一个和最后一个组合,第二个和倒数第二个组合,等等,如此进行下去,注意如果是奇数项,中间的那个只

被加一次,这样我们只需要枚举到就行了。这个方法很好,其实还有一个相对还算可以方法,那么就是记录每个等价类的

首元素和尾元素,每次跳一段长度,这样对于10^9数据完全没有问题。

本题由于数据很大,所以用Java大数:

import java.math.BigInteger;
import java.util.Scanner;
import java.math.*; public class Main
{
public static BigInteger Solve(long n)
{
BigInteger ans=BigInteger.ZERO;
long i,t=(long) Math.sqrt(n*1.0);
for(i=1L;i<=t;i++)
{
BigInteger a=BigInteger.valueOf(n/i+n/(i+1)+1);
BigInteger b=BigInteger.valueOf(n/i-n/(i+1));
BigInteger temp=BigInteger.ZERO;
if(i!=(n/i))
temp= (a.multiply(b)).divide(BigInteger.valueOf(2));
BigInteger c=BigInteger.valueOf(n/i);
BigInteger ret=c.add(temp);
ret=ret.multiply(BigInteger.valueOf(i));
ans=ans.add(ret);
}
return ans;
} public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
while(cin.hasNextLong())
{
long n=cin.nextLong();
BigInteger x=BigInteger.valueOf(n);
System.out.println((x.multiply(x)).subtract(Solve(n)));
}
}
}

n%i之和的更多相关文章

  1. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  2. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  3. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  4. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  5. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  10. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. SQL Profile 总结(一)

    一.前提概述 在介绍SQL Profile之前,不得不说的一个工具就是SQL Tuning Advisor:这个工具是从Oracle 10g開始引入,它的任务就是分析一个指定的SQL语句,并建议怎样使 ...

  2. 苹果的HomeKit协议

    苹果的HomeKit协议非常底层,其作用仅限于让iOS平台和家居设备能够相互“握手”,但“认识”之后,想要继续控制灯.空调等设备,仍然需要家电厂商在HomeKit的基础上进行二次开发.

  3. 服务 IntentService 前台服务 定时后台服务

    Activity public class MainActivity extends ListActivity {     private int intentNumber = 0;     @Ove ...

  4. C# 数学运算符

    运算符大致分为如下3类: 一元运算符,处理一个操作符 二元运算符,处理两个操作数 三元运算符,处理三个操作数 大多数运算符都是二元运算符,只有几个一元运算符和一个三元运算符,即条件运算符(条件运算符是 ...

  5. ASP.net 前台页面通过ID获取控件

    asp.net的服务器控件的ID通常只能在服务器端很好的识别,客户端需要通过ClientID获得控件 1.通过js获得   var controlID = "<%=controlID. ...

  6. MYSQL显示数据库内每个表拥有的触发器

    一  所有数据库->所有触发器: SELECT * FROM information_schema.triggers; 二  当前数据库->当前所有触发器(假设当前数据库为gmvcs_ba ...

  7. OSG调试信息显示

    调试信息显示 OSG 可以将各式各样的调试信息输出到std:cout.这在开发OSG 程序时十分有用,你可以借此观察OSG 的执行的各种操作.环境变量OSG_NOTIFY_LEVEL用于控制OSG调试 ...

  8. js ||与&&

    ||:找到结果为true的分项就停止,并返回该分项的值,否则继续执行,如果都没有为true的分项则返回最后分项的值(注意每个分项先转成bool与true进行比较). //例如下面的例子: // &qu ...

  9. Spring MVC异常处理

    Spring Mvc 中异常处理,一般有两种解决办法: 一.利用org.springframework.web.servlet.handler.SimpleMappingExceptionResolv ...

  10. 3月23日html(五) 格式与布局练习:360浏览器布局

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...