打表或者画个图可以看出i>根号n时每个i的贡献值相差很小,可以利用公式优化(函数C)

但是注意不能一整段使用公式,否则复杂度还是会劣化到O(n)(显然对gongxian只能逐步递减)

网上看了不少代码,但是都没有对贡献值边界问题给定明确的判断

所以还是加多一个while循环确定贡献值的开端是前面的n/i没有的

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
typedef long long ll;
ll H(ll n,ll j){
ll ans=0;
for(int i = 1; i <= j; i++) ans+=n/i;
return ans;
}
ll C(ll n,ll gongxian){
return gongxian*(n/gongxian-n/(gongxian+1));
}
int main(){
int T,kase=0; scanf("%d",&T);
while(T--){
ll n; scanf("%lld",&n);
if(n<=20){
printf("Case %d: %lld\n",++kase,H(n,n));
continue;
}
ll gen = sqrt(n);
ll ans1 = H(n,gen);
ll tmp = n/gen;
ll cnt=gen;
while(1){
if(n/gen==n/(cnt+1)){
ans1+=n/gen;
cnt++;
}
else{
cnt++;//start
break;
}
}
ll gongxian = n/cnt;
ll ans2=0;
while(gongxian){
ans2+=C(n,gongxian);
gongxian--;
}
printf("Case %d: %lld\n",++kase,ans1+ans2);
}
return 0;
}

顺便附加对规律观察用的代码

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define scan(a) scanf("%d",&a)
using namespace std;
typedef pair<int,int> P;
const int maxn = 1e6+11;
typedef long long ll;
int cnt[maxn],n;
bool C(int n){
memset(cnt,0,sizeof cnt);
rep(i,1,n) cnt[n/i]++;
int maxzero=0,num=0;
rep(i,1,n) if(i>=sqrt(n)&&cnt[i]==1) num++;
// cout<<(int)sqrt(n+0.5)+1<<" "<<cnt[(int)sqrt(n+0.5)+1]<<endl;
// return cnt[(int)sqrt(n+0.5)+1];
cout<<num<<endl;
}
int main(){
// rep(i,1,10000) if(C(i)) cout<<i<<endl;
srand(time(NULL));
int n=rand()%1000000;
C(n);cout<<sqrt(n)<<endl;
}

可以看出[根号n,n]对答案的贡献基本等于根号n

#include<bits/stdc++.h>
using namespace std;
int main(){
int cnt=0;
for(int i = 100; i <= 1e6; i++){
int a=i/(int)sqrt(i),b=sqrt(i);
if(a-b<0) cnt++;
// cout<<i<<" "<<a-b<<endl; }
cout<<cnt<<endl;
}

可以看出a(应该)都是大于等于b

待做同类题目:1257 2956

//BACKUP
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
typedef long long ll;
ll cal(int n,int k){
ll ans=0;
for(int i = 1; i <= n; i++) ans+=k%i;//k-(k/i)*i
return ans;
}
ll G(int n,int k){
ll ans=0;
for(int i = 1; i <= n; i++) ans+=(k/i)*i;
return ans;
}
ll con(ll k,ll i){return i*(k/i-k/(i+1));}
ll sum(ll a1,ll n,ll d){return a1+(n-1)*d;}
int main(){
int n,k;
while(cin>>n>>k){
if(n<=100){
cout<<cal(n,k)<<endl;
continue;
}
ll root=sqrt(n);
// cout<<"root"<<root<<endl;
ll ans1=1ll*k*n;
ll ans2=G(root,k); //
ll ans3=0;
int cnt=root+1; if(k/cnt==0){//防止越界处理
cout<<ans1-G(root,k)<<endl;
continue;
}
while(1){
if(k/root==k/(cnt+1)){
ans3+=(k/(cnt+1))*(cnt+1);
cnt++;
// cout<<k/root<<" "<<k/cnt<<endl;
}
else{
cnt++;
break;
}
}
bool flag=0;
ll lim=k/n; if(lim==0) lim++;
ll val=k/cnt; if(val==0) flag=1;
ll now=cnt;
ll ans4=0;
while(!flag&&val>=lim){
cout<<"NOW "<<now<<endl;
ll LEN = k/val-k/(val+1);
cout<<"LEN "<<LEN<<endl;
cout<<"VAL "<<val<<endl;
ans4+=con(k,val)*(LEN?sum(now,LEN,1):0);
now+=LEN;
val--;
}
cout<<ans1-(ans2-ans3-ans4)<<endl;
}
return 0;
}

LightOJ - 1245 根号n暴力的更多相关文章

  1. LightOJ 1245 数学题,找规律

    1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...

  2. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  3. lightoj 1245

    lightoj 1245 Harmonic Number (II) 题意:给定一个 n ,求 n/1 + n/2 + …… + n/n 的值(这里的 "/" 是计算机的整数除法,向 ...

  4. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  5. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  6. LightOj 1245 --- Harmonic Number (II)找规律

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...

  7. Harmonic Number (II) LightOJ - 1245 (找规律?。。。)

    题意: 求前n项的n/i  的和 只取整数部分 暴力肯定超时...然后 ...现在的人真聪明...我真蠢 觉得还是别人的题意比较清晰 比如n=100的话,i=4时n/i等于25,i=5时n/i等于20 ...

  8. lightoj 1245 Harmonic Number (II)(简单数论)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...

  9. LightOJ 1245 Harmonic Number (II) 水题

    分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #inclu ...

随机推荐

  1. 使用 Sentry集中处理错误

    Sentry的简介 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将程序的所有 exception 自动记录下来,处理 exception 是每个程 ...

  2. c++基础之struct

    就是让用户自己自定义一个要往里面放各种东西的抽屉 // 声明一个结构体类型 Books struct Books { ]; ]; ]; int book_id; }; int main( ) { Bo ...

  3. 关于VBS的一个怪现象

    今天一个同学让我帮忙写一个程序,要求是: 输入一个n,返回从0到n中任意个数的组合,返回取异或结果为0的组合.来看VBS代码 n = p = "" ^n - s = s = s ) ...

  4. 浅谈delphi创建Windows服务程序与窗体实现交互

    我想实现的功能是创建一个服务程序,然后在服务Start时动态创建一个窗体Form,然后把Form缩小时变成TrayIcon放在Windows托盘上. 我在服务程序的OnStart事件中写到 Start ...

  5. FTP无法获取目录及文件

    在防火墙打开的情况下 如果不能获取,尝试将C:\windows\system32\svchost.exe 加入防火墙允许通道中. 并且设置为 主动模式

  6. webrequest、httpwebrequest、webclient、HttpClient 四个类的区别

    一.在 framework 开发环境下: webrequest.httpwebreques  都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...

  7. CentOS 系统管理与yum软件仓库搭建

    重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...

  8. C++内存管理之shared_ptr

     ----------------------------------------shared_ptr--------------------------------------- 引子 c++中动态 ...

  9. CH收藏的书

    论语 道德经 墨子

  10. Docker Community Edition 镜像使用帮助

    1.什么是Docker 容器技术 在计算机的世界中,容器拥有一段漫长且传奇的历史.容器与管理程序虚拟化 (hypervisor virtualization,HV)有所不同,管理程序虚拟化通过中间层将 ...