LightOj 1245 --- Harmonic Number (II)找规律
题目链接:http://lightoj.com/volume_showproblem.php?problem=1245
题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题,
i 1 2 3 4 5 6 7 8
a 8 4 2 2 1 1 1 1
你可以多写几组你会发现
有8-4个1;4-2个2;。。。其他例子也是这样;
当n = 10时
n/1 = 10, n/2 = 5说明(5, 10]这个前开后闭的区间中对应的结果都是 1 ,共有10-5 = 5个 ans += 5*1;
n/2 = 5, n/3 = 3说明(3, 5]这个前开后闭的区间中对应的结果都是 2 ,共有5-3 = 2个 ans += 2*2;
n/3 = 3, n/4 = 2说明(2, 3]这个前开后闭的区间中对应的结果都是 3 ,共有3-2 = 1个 ans += 1*3;
n/4 = 2, n/5 = 2说明(2, 2]这个前开后闭的区间中对应的结果都是 4 ,共有2-2 = 0个 ans += 0*4;
n/5 = 2, n/6 = 1说明(1, 2]这个前开后闭的区间中对应的结果都是 5 ,共有2-1 = 1个 ans += 1*5;
对于一个比较大的数n,直接循环一定会错,所以要优化,对于 1 - (int)sqrt(n)所对应的另一半即[n/(int)sqrt(n), n/1]这些数一定只出现了一次,所以加上,剩下的那些就是出现多次的了,按照规律进行处理,注意会加多的那个部分;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 20100 int main()
{
int T, n,t=;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int i=, cnt=;
long long ans=;
if(n==)
{
printf("Case %d: 1\n", t++);
continue;
}
while()
{
int a=n/i;
int b=n/(i+);
ans=ans+a+(a-b)*i;///结果;
cnt=cnt+(a-b)+;///表示已经加了几个数了;
i++;
if(cnt==n-)///当还剩一个数时就结束就行了,并且还要把这个数加上;
{
ans+=n/i;
break;
}
if(b<=i)///当后面那个数<=i事就说明已经加完了;
break;
}
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
简单的写法
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = 1e7+;
const double eps = 1e-; int main()
{
int T, t = ; scanf("%d", &T); while(T--)
{
LL n; scanf("%lld", &n); LL ans = , a = , b = , k = (int)sqrt(n); for(int i=; i<=k; i++)
{
a = n/i, b = n/(i+);
ans += a + (a-b)*i;
}
if(k == n/k)
ans -= k;
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
LightOj 1245 --- Harmonic Number (II)找规律的更多相关文章
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;
/** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- lightoj 1245 Harmonic Number (II)(简单数论)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...
- LightOJ 1245 - Harmonic Number (II)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...
- LightOJ 1245 Harmonic Number (II) 水题
分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #inclu ...
- LightOJ - 1245 Harmonic Number (II) 求同值区间的和
题目大意:对下列代码进行优化 long long H( int n ) { long long res = 0; for( int i = 1; i <= n; i++ ) ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
随机推荐
- 微软职位内部推荐-SW Engineer II for Cloud Servi
微软近期Open的职位: Do you have a passion for embedded devices and services?   Does the following m ...
- android实现左右滑动菜单
直接看效果图: 主要实现代码: package com.way.view; import android.content.Context; import android.media.Den ...
- Log4j2 配置笔记(Eclipse+maven+SpringMVC)
Log4j2相关介绍可以百度看下,这里只注重配置Log4j2 能够马上跑起来: 1.pom.xml文件中添加Log4j2的相关Maven配置信息 <!-- log4j2 --> <d ...
- 本地wordpress博客系统安装搭建实践
我们按步骤来, (1)安装XAMPP集成软件包 wordpress 的运行要求是在 php + MySQL + Apache的服务器环境,所以要先搭建该环境,我用的是XAMPP软件包,安装很方便. 下 ...
- iOS网络传输Delegate不被触发的本质原因
NSURLSession一共有四种Delegate (文后附表有Session和SessionTask分类表格) NSURLSessionDelegate, NSURLSessionDownloadD ...
- UVALive - 6955 Finding Lines 随机算法
题目链接: http://acm.hust.edu.cn/vjudge/contest/126968#problem/F 题意 给你n个点,问是否有>=p/100*n个点共线(p>=20& ...
- SqlServer Split函数
Create FUNCTION [dbo].[SplitToTable] ( @SplitString nvarchar(max), @Separator nvarchar(10)=' ' ) RET ...
- Rich Text Editor for MVC
在网站开发中难免会用到富文本编辑器,本文将介绍一款富文本编辑器(在线HTML编辑器) Rich Text Editor ,简要说明一下其在MVC中的使用. 具体使用情况和下载地址请参考:http:// ...
- ios 流媒体 资料
(1)“Real-Time Streaming Protocol (RTSP)”比较官方的资料(链接). (2)“ffmpeg”在百度百科的简介(链接). (3)“ffmpeg”的官网:http:// ...
- thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法
随手记录下今天在thinkphp3.2.3中遇到的错误SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.file_info ...