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 ...
随机推荐
- 输出redis cluster集群所有节点指定的参数的配置
需要:实现类似redis-trib.rb call 命令的功能,输出redis cluster集群所有节点指定的参数的配置 redis-trib.rb的输出 [redis@lxd-vm3 ~]$ re ...
- Spark学习之路 (十)SparkCore的调优之Shuffle调优[转]
概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作.因此,如果要让作业的性能更上一层楼,就有必要对shuffle过程进行调优 ...
- vue 学习2
模板指令.属性总结 html 中的标签属性 1. :class 值是对象,key为class 的值,值为boolean类型 html标签任意属性都可以:属性,表示动态值(值是变化的,不是固定不变的) ...
- lasso-ridge
线性回归 线性回归很简单,用线性函数拟合数据,用 mean square error (mse) 计算损失(cost),然后用梯度下降法找到一组使 mse 最小的权重. lasso 回归和岭回归(ri ...
- centos7 下 安装GeoIP2,在nginx中根据ip地址对应的国家转发请求
最近有个需求是根据用户的地理位置,访问不同的服务器,比如国外用户访问国外的服务器,国内的用户访问国内的服务器,实现的思路主要两种: 智能dns,这个需要在阿里云中注册为企业版才有提供 nginx中使用 ...
- Leetcode Week5 Maximum Sum Circular Subarray
Question Given a circular array C of integers represented by A, find the maximum possible sum of a n ...
- 牛客寒假6-C汉诺塔
链接:https://ac.nowcoder.com/acm/contest/3007/C来源:牛客网 题目描述 现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔 ...
- Chrome 浏览器相关
********* 问题 ********* localhost 通常会使用加密技术来保护您的信息.Google Chrome 此次尝试连接到 localhost 时,此网站发回了异常的错误凭据.这可 ...
- BZOJ 2467: [中山市选2010]生成树
有一种图形叫做五角形圈.一个五角形圈的中心有1个由n个顶点和n条边组成的圈.在中心的这个n边圈的每一条边同时也是某一个五角形的一条边,一共有n个不同的五角形.这些五角形只在五角形圈的中心的圈上有公共的 ...
- base(根URL)
指定用于一个文档中包含的所有相对 URL 的根 URL.一份中只能有一个 <base> 元素. 可以通过使用 document.baseURI 的 JS 脚本查询 属性 包含全局属性 hr ...