题目: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. chrome devtools 实用快捷键

    Ctrl + O:查找资源,非常使用 Ctrl + Shift + C:切换审查元素模式与浏览器窗口模式 Ctrl + Shift + F:在源代码中搜索 Ctrl + G:跳转到指定行

  2. c\c++复习基础要点08--c++单例模式

    单例模式有许多种实现方法,在c++中,甚至可以直接用一个全局变量做到这一点,但是这样的代码显得不优雅.使用全局对象能够保证方便地访问实例,但是不能保证只声明一个对象——也就是说除了一个全局实例外,仍然 ...

  3. Linux下配置免安装版Tomcat

    一.下载免安装版Tomcat 打开Tomcat主页http://tomcat.apache.org/,下载Tomcat免安装版,我这里下载的是apache-tomcat-7.0.2.tar.gz. 二 ...

  4. [Polymer] Introduction

    install Polymer and explore creating our first custom element: bower install polymer index.html: < ...

  5. Slider( 滑动条) 组件

    本节课重点了解 EasyUI 中 Slider(滑动条)组件的使用方法,这个组件依赖于Draggable(拖动)组件. 一. 加载方式//class 加载方式<input class=" ...

  6. Linq 学习(1) 概述

    本篇简单回顾C#语言集合操作的变化,通过与Linq对等的面向对象的语法来认识Linq.Linq是Language Integrated Query, 初识Linq感觉跟SQL Server的Tsql很 ...

  7. 工厂方法模式(java 设计模式)

    1.工厂方法模式的定义 工厂方法模式使用的频率非常高, 在我们日常的开发中总能见到它的身影. 其定义为:Define an interface for creating an object,but l ...

  8. Android view 小总结

    android 中, view 的绘制包含三步: 1. onMeasure(), 对view进行测量: 2. onLayout(),对view进行布局: 3.onDraw(),对view进行绘制. v ...

  9. Android-为何以及如何保存Fragment实例

    在安卓开发中,由于旋转设备会造成配置改变进而导致Activity实例被摧毁(当然也包括Activity托管的Fragment).Activity或Fragment实例被摧毁自然也就让Model被摧毁, ...

  10. SqlServer 不同服务器之间数据库连接、数据库登录、数据传递

    需求:我是本地数据库想纯SQL访问其它服务器上的数据库,而不使用数据库客户端的连接.这里面就想到了数据库link,通过下面的代码进行创建以后,就可以在本地对链接的服务器数据库进行操作了--添加SQLS ...