UVA - 11526 - H(n)(思路,减少枚举量)
Posted TianTengtt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 11526 - H(n)(思路,减少枚举量)相关的知识,希望对你有一定的参考价值。
题意:给定在32位带符号整数范围内的n,求n/1+n/2+n/3+n/4+……+n/n = ?
因为损失精度,所以算出来的有些连续的项是相同的数值,故想办法找出对于某个值,哪一段范围均是这个值。
详见代码注释
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl #define Min(a, b) a < b ? a : b #define Max(a, b) a < b ? b : a typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const ll MOD = 1e9 + 7; using namespace std; #define NDEBUG #include<cassert> const int MAXN = 100 + 10; const int MAXT = 10000 + 10; int main(){ ll n; int T; scanf("%d", &T); while(T--){ scanf("%lld", &n); ll sum = 0; for(ll i = 1; i <= n;){ ll value = n / i; //此时需要找的值为value ll lur = n / value; //n中最多包含lur个value,向下取整(因为不能是小数) sum += (lur - i + 1) * value; // i ~ lur 范围内的值均是value i = lur + 1; //减少枚举量 } printf("%lld\n", sum); } return 0; }
以上是关于UVA - 11526 - H(n)(思路,减少枚举量)的主要内容,如果未能解决你的问题,请参考以下文章
UVA 1602 Lattice Animals解题思路(打表+set)
Uva 11300 Spreading the Wealth(贪心)