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 ...
随机推荐
- 状态模式(State) 笔记
让一个对象随着内部的状态改变而发生改变. 状态的两种切换方式: 1) 完全交给Context类切换, 2) 给Context初始化状态,其他的切换根据每一个State类进行切换,Context对象不再 ...
- Horizontal,vertical,Input_Mouse,Input_Key
鼠标获取 using UnityEngine; using System.Collections; public class Input_Mouse : MonoBehaviour { void Up ...
- volatile-java关键字
volatile的作用: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序: XBYTE[2]=0x55; XBYTE[2 ...
- Android Paint之 setXfermode PorterDuffXfermode 讲解
setXfermodePorterDuffXfermode图层混合模式android图像混合模式AvoidXfermode 尊重原创,欢迎转载,转载请注明: FROM GA_studio htt ...
- asp.net(C#)利用QRCode生成二维码---.NET菜鸟的成长之路
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs&q ...
- 获取scrollTop兼容各浏览器的方法,以及body和documentElement
1.各浏览器下 scrollTop的差异 IE6/7/8: 对于没有doctype声明的页面里可以使用 document.body.scrollTop 来获取 scrollTop高度 : 对于有do ...
- 127.0.0.1与localhost与ip的区别
127.0.0.1与localhost与ip的区别 May 18, 2014 localhost 不联网不使用网卡,不受防火墙和网卡限制本机访问 一般使用 本地套接字文件AF_UNIX 应用程序一般约 ...
- osg添加纹理示例
转自http://www.cnblogs.com/ylwn817/articles/1976851.html #include <osgDB/ReadFile>#include <o ...
- 基于mAppWidget实现手绘地图(十五)–如何控制放大缩小
一般来说,可以使用以下几种方式来控制地图的放大/缩小 : 使用控件底部的缩放按钮 双击控件 pinch手势 物理按键 :I键标识缩小 :O键表示放大.(只有设备具有物理按键才行) 当然, ...
- MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
公司线上在用partition,有一个表的分区字段错了,需要重建,结果发现没有办法像修改主键字段或者修改索引字段那样直接一条sql搞定.而是需要建临时表,有down time,所以去仔细看了文档,研究 ...