Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code
long long H( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
res = res + n / i;
return res;
}
Yes, my error was that I was using the integer divisions only. However, you are given n, you have to find H(n) as in my code.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n < 231).
Output
For each case, print the case number and H(n) calculated by the code.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
2147483647
Sample Output
Case 1: 1
Case 2: 3
Case 3: 5
Case 4: 8
Case 5: 10
Case 6: 14
Case 7: 16
Case 8: 20
Case 9: 23
Case 10: 27
Case 11: 46475828386
思路: 找规律这件事,emmm.....
注意sqrt(n)这个数,数之间的差与后面数的个数。。。。。
写几个完整的例子,努力寻找规律!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <deque>
#include <iostream>
using namespace std;
typedef long long LL;
const LL N = ; map<int, bool> check;
int prime[]; long long H( int n ) {
long long res = ;
for( int i = ; i <= n; i++ )
res = res + n / i;
return res;
} int main()
{
LL i, p, j, n, t, cnt = ;
LL sum; scanf("%lld", &t);
while(t--) {
sum = ;
scanf("%lld", &n);
for(i = ; i <= (LL)sqrt(n); i++) {
// cout << "i: " << i << endl;
sum += (n / i - n / (i + )) * i;
sum += n / i;
}
if(n / (LL)sqrt(n) == (LL)sqrt(n)) {
// sum -= (n / (LL)(sqrt(n)) - n / (LL)(sqrt(n) + 1)) * (i - 1);
sum -= (LL)sqrt(n);
}
printf("Case %lld: %lld\n", cnt ++, sum);
}
return ; //2 147 483 648
}
Harmonic Number (II) 数学找规律的更多相关文章
- 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个数 ...
- LightOJ1245 Harmonic Number (II) —— 规律
题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II) PDF (English) Statistics ...
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- Harmonic Number (II)
Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
- LightOj 1245 --- Harmonic Number (II)找规律
题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...
- HDU 1391 number steps(找规律,数学)
Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,... as shown ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
随机推荐
- Unity3D制作3D虚拟漫游场景(一)
开始前先说一些题外话,本来这个工程是已经完成了超过一半了,然而由于手残重装了系统不小心删除了,现在只好再做一遍了.顺便写一下博供今后写代码参考. 这是一款使用unity3D开发的虚拟城市漫游游戏,实际 ...
- PHP0008:PHP基础-数组
- lvs基础
lvs类型 lvs-nat: 上下文为masquerade 多目标的DNAT(iptables): 它通过修改请求报文的目标IP地址(同时可能会修改目标端口)至挑选出某RS的RIP地址实现转发: 特性 ...
- echarts地图
<html> <body> <div id="cbar" style="width: 100%; height: 360px"&g ...
- openlayers画区域
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 【巨杉数据库SequoiaDB】社区分享 | SequoiaDB + JanusGraph 实践
本文来自社区用户投稿,感谢小伙伴的技术分享 项目背景 大家好!在春节这段时间里,由于一直在家,所以花时间捣鼓了一下代码,自己做了 SequoiaDB 和 JanusGraph 的兼容扩展工作. 自己觉 ...
- Cloudera Manager和CDH版本的对应关系
来源:https://www.cloudera.com/documentation/enterprise/release-notes/topics/rn_consolidated_pcm.html#c ...
- vConsole 让你在手机上也能轻松调试网页
有时候为了想在手机真机上对网页进行 Debug,可手机上没有 F12,用 Chrome DevTools 连接手机操作又太过复杂.VConsole 的出现,正好解决了这一痛点! (下列内容照搬一下官方 ...
- Linux虚拟文件系统–VFS简介
http://www.embeddedlinux.org.cn/emb-linux/file-system/201712/20-7907.html 导读 Linux中可以支持多种文件系统,而且支持各种 ...
- Linux安装U盘启动报错Failed to load ldlinux.c32
报错信息 使用U盘安装linux无法正常启动 Start booting from USB device... SYSLINUX 5.10 EDD 2013-06-04 Copyright (C) 1 ...