n%i之和
题目: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之和的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- SYNATXAHIGHLIGHTER IN WLW HAS PROBLEMS
System.Reflection.TargetInvocationException: 调用的目标发生了异常. ---> System.ArgumentException: 字体“Consol ...
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...
- Log4Qt 使用(二)
本文基于上一篇<Log4Qt 使用(一)>来继续完善一下关于Log4Qt的使用. 在讲解之前,我们首先来看一个例子: int main(int argc, char *argv[]) { ...
- PLSQL Developer安装(Oracle11g+win7_64bit)
1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...
- SVN服务器的本地搭建和使用
用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 详细步骤如下 http://www.2cto.com/os/201412/361931.html
- 广播接收者 BroadcastReceiver 示例-1
广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者.广播作为Android组件间的通信方式,可以使用的场景如下: 1 ...
- .net web api 的route理解
.NET web api 的特性是和MVC一样,通过Route 来控制action的访问方式.Route匹配规则是个奇特的方式,首先看一段Route的模板 Routes.MapHttpRoute( n ...
- shell获取日期(昨天,明天,上月,下月)
今天 sh-4.1$ echo `date +%Y-%m-%d` 2016-08-17 昨天 sh-4.1$ echo `date -d "last day" +%Y-%m-%d` ...
- SignalR小计
微软官方例子地址:http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-a ...
- java对像序列化
package cn.stat.p2.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; impor ...