LightOJ 1245

来源:互联网 发布:淘宝卖家售假申诉 编辑:程序博客网 时间:2024/05/21 19:42

解题思路:

先暴力解决sqrt(n)内的数,因为他们连续的数除的商相差较大,之后根号后的数连续除后得到的商不是相等就是差1就可以用公式跳跃式前进(这名字不是很好)


代码:

#include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include <algorithm> #include<vector>using namespace std;typedef long long ll;ll n,m;int main(){int t;scanf("%d",&t);int cas = 1;while(t--){ll ans = 0;scanf("%lld",&n);ll sq = sqrt(n);for(int i=1;i<=sq;i++) ans += n/i;ll to = sq+1,c,d;while(to<=n) {c = n / to; d = n / c + 1;   ans += c*(d-to);to = d;}printf("Case %d: %lld\n",cas++,ans);} return 0; } 


原创粉丝点击