题目: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. writev/readv

    ```cpp#include <sys/uio.h>ssize_t readv(int fd, const struct iovec *iov, int iovcnt);ssize_t w ...

  2. 关于kendo ui的使用改变颜色方式

    1.概述: 在网上kendo ui教程中示例在演示的时候引用的css样式为kendo.common.min.css与kendo.default.min.css这两个外部样式,大家有没有发现,这两个样式 ...

  3. Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)

    解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 ...

  4. [Git] Automatically running tests before commits with ghooks

    Wouldn't it be nice if everyone ran the tests before committing code? With ghooks, you can automatic ...

  5. DES加解密算法Qt实现

      算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...

  6. Qt QString to char*

    QString转换成char * 的时候,一定要定义一个QBateArray的变量.不能连写 How can I convert a QString to char* and vice versa ? ...

  7. DOS环境下MySQL使用及不同字符集之间的转换

    mysql -uroot -p; show databses; 创建数据库\c; create database webclass; use webclass; 创建表并设置好各字段的属性\c cre ...

  8. 自定义控件 闪烁效果的TextView

    使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android ...

  9. vs2013+EF6+Mysql

    1.首先需要在整个项目中添加一个Model类库,在类库中引用EF 我需要在该项目下添加EF的MYSQL对象实体 首先需要引入几个相关引用,我通过NuGet来添加,如下图 接下来我需要通过ADO.NET ...

  10. MongoVUE查询备忘

    用了一段时间的MongoVUE,把一些在MongoVUE中常用的查询记录一下,以便查阅.1.and查询    查询date等于2016-01-08,并且page_url等于shouye.html   ...