首页 > 其他分享 >Light oj 1245 【数学题】

Light oj 1245 【数学题】

时间:2023-05-22 14:03:28浏览次数:50  
标签:Case oj int Light long 1245 sqrt res include


1245 - Harmonic Number (II)



PDF (English)

Statistics

Forum


Time Limit: 3 second(s)

Memory Limit: 32 MB


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

Output for Sample Input

11
1
2
3
4
5
6
7
8
9
10
2147483647
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

 

运算到sqrt(n)



#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <queue>
#include <iterator>

using namespace std;

int t,cases = 1;
int n;
long long ans;

long long solve(long long  n)
{
	long long res = 0;
	for (int i = 1; i <= sqrt(n); i++)
	{
		res += n / i;
		if (n / i > n / (i + 1))
			res += (n / i - n / (i + 1))*i;
	}
	if ((long long)sqrt(n) == n/(long long)sqrt(n))
		res -= (int)sqrt(n);
	return res;
}

int main()
{
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		ans = solve(n);
		printf("Case %d: %lld\n",cases++,ans);
	}
	return 0;
}



标签:Case,oj,int,Light,long,1245,sqrt,res,include
From: https://blog.51cto.com/u_15990681/6323575

相关文章

  • csproj OutputType
    csproj<?xmlversion="1.0"encoding="utf-8"?><ProjectToolsVersion="4.0"DefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup><C......
  • mysql与lightdb中的insert on duplicate/replace
    最近看pg中insert的实现源码,看到onconflict的excluded优点疑惑,顺带总结下mysql和pg中已存在更新、不存在插入的差异(注:oracle是mergeinto实现)。在mysql中的insertonduplicate和lightdb的onconflict是等价的。逻辑都是基于唯一约束进行已存在则更新,否则插入。insertrecordIF......
  • POJ1737 Connected Graph ( n点无向连通图计数
    题意说明:求\(n\)个点的无向连通图个数据说已经非常典了,但是我太菜了不会组合数学,最近补档时看到这道题,决定记录下来理理思路......
  • 全新的照片后期处理软件Lightroom Classic 2022(lrc2022)Mac版
    LightroomClassic2022是一款全新的照片后期处理。通过使用其卓越的图形处理功能,该软件不仅可以帮助您轻松地制作出创意的照片,而且还可以为您带来意想不到的效果。对照片,视频和图像编辑工具进行优化。这些新功能使您可以在处理图像时使用编辑器之外的方法进行编辑,从而节省时间并......
  • Sonarqube---You're not authorized to run analysis. Please contact the project ad
    问题:sonarqube执行时报错:You'renotauthorizedtorunanalysis.Pleasecontacttheprojectadministrator原因:项目开始执行是好的,因为需要做项目的权限控制,所以将project从public修改为private后,再执行就报You'renotauthorizedtorunanalysis.Pleasecontacttheproject......
  • 使用增强版 singleflight 合并事件推送,效果炸裂!
    hello,大家好啊,我是小楼。最近在工作中对Go的singleflight包做了下增强,解决了一个性能问题,这里记录下,希望对你也有所帮助。singleflight是什么singleflight直接翻译为”单(次)飞(行)“,它是对同一种请求的抑制,保证同一时刻相同的请求只有一个在执行,且在它执行期间的相同请求都......
  • 「解题报告」UOJ671 [UNR #5] 诡异操作
    这题怎么这么多差评啊?哦卡常啊,没事了。发现两个操作都是只增不减,显然势能线段树。考虑维护区间按位与,线段树上维护每一位上有多少个\(1\),按位与就是区间赋\(0\),对于区间除法暴力重构。直接暴力维护即可做到\(O((n+q)\logn\logV)\)的复杂度。但是\(\logV=128\),顶两......
  • [BZOJ4407]于神之怒加强版 CODE
    #include<bits/stdc++.h>#definelllonglong#defineFor(i,a,b)for(lli=(a);i<=(b);++i)#defineRep(i,a,b)for(lli=(a);i>=(b);--i)constllN=1e6+10;usingnamespacestd;constllmod=1e9+7;llvis[N],tot,p[N];voidinit(lln){//质数筛For......
  • 蒲公英(Loj 分块模板9)
    [Violet]蒲公英题目背景亲爱的哥哥:你在那个城市里面过得好吗?我在家里面最近很开心呢。昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多小朋友也被它杀掉了。我觉得把那么可怕的怪物召唤出来的那个坏蛋也很坏呢。不过奶奶说他是很难受......
  • Loj分块模板1
    #include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>usingnamespacestd;intn,m,len;intpos[5211314],add[5211314],num[5211314];inlinevoidAdd(intLiftPos,intRightPos,intval......