您的位置:首页 > 大数据 > 人工智能

zoj 3175 Number of Containers(数论~)

2011-01-23 18:38 387 查看
算是数论吧。 = =



就是求i从1到n的 n/i-1的累和。



开始我用最简单的累和,算20Y要好几秒,肯定过不去,也想了到n的平方根,思路是对的,可是没想明白 = =就去看别人题解了,破习惯。



结果人家就是用的到n的平方根 = =。。让姐情何以堪。。。



看懂了。大家画个函数图,y = n/x,以 y = x对称,下面的部分计算的时候,上半部分y-根号n为下面曲线上点的纵坐标。。。下面也有根号n个点,所以总共减去根号n*根号n。



#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
	int ncases,i,n;
	long long ans;
	scanf("%d",&ncases);
	while( ncases-- )
	{
		ans = 0;
		scanf("%d",&n);
		for(i=1; i<=sqrt(n); i++)
			ans += (n/i);
		ans *= 2;
		i--;
		ans = ans - i*i - n;
		printf("%lld/n",ans);
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: