一:O(n)

计算贡献:前n项中,能被i(1~n)整除的数的个数为(n/i)个,,也就是 i 给前n项中(n/i)个数做了余数

#include<iostream>

using namespace std;

int main ()
{
int n;
cin>>n;
long long ans=0;
for(int i=1;i<=n;i++)
ans+=n/i;
cout<<ans<<endl; return 0;
}

将前n项加和即可

二:(倍数法)O(nlogn)

时间复杂度=O(n+n/2+n/3+……1)n个=nlogn

#include<iostream>

using namespace std;
int a[(int)1e5+5];
int main ()
{
int n;
cin>>n;
long long ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j+=i)
a[j]++;
ans+=a[i];//当i为步长完全遍历完时,i的约数也已经遍历完了
}
cout<<ans<<endl;
return 0;
}

前n项余数个数和的更多相关文章

  1. c - 2/1, 3/2, 5/3, 8/5, 13/8...前20项的和

    double pres(const int n) { ; //分子. ; //分母. ; double tmp; ; i <= n; i++) { sum += (numerator / den ...

  2. HDU_2011——求多项式的前n项和

    Problem Description 多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...现在请你求出该多项式的前n项的和.   Input 输入数据由2行组 ...

  3. Fibonacci数列前n项值的输出(运用递归算法)

    1.斐波那契数列: 又称黄金分割数列,指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 在数学上,斐波纳契数列以如下被以递归的方法 ...

  4. 打印Fibonacci数列方法汇总(前20项,每行5个)

    NO.1 迭代法 标签:通俗.易懂 思路:先打印第一项.再在循环里面执行fib=fib1+fib2,把fib2赋给fib1,把fib赋给fib2,每行5个可使用if函数(循环次数对5取余). #inc ...

  5. Java例题_20 前20项之和!

    1 /*20 [程序 20 求前 20 项之和] 2 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和. 3 程序分析:请抓住分子与分母的变 ...

  6. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  7. 39. 求分数序列前N项和

    求分数序列前N项和 #include <stdio.h> int main() { int i, n; double numerator, denominator, item, sum, ...

  8. 20. 求阶乘序列前N项和

    求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...

  9. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

随机推荐

  1. MATLAB在读取excel文件是发生错误,怎么解决?

    转载:https://blog.csdn.net/qq_38712026/article/details/78783422?utm_source=blogxgwz4

  2. 算法历练之路——传纸条(JAVA)

    传纸条 时间限制: 1Sec 内存限制: 128MB 提交: 36 解决: 16 题目描述小渊和小轩是好朋友也是同班同学,他们在一起 总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列 ...

  3. 新蜂商城的mybatis plus版本,添加了秒杀专区、优惠卷领取以及后台搜索功能

    本项目是在newbee-mall项目的基础上改造而来,将orm层由mybatis替换为mybatis-plus,添加了秒杀功能.优惠劵功能以及后台搜索功能,喜欢的话麻烦给我个star 后台管理模块添加 ...

  4. 【Linux】snmp在message中报错: /etc/snmp/snmpd.conf: line 311: Error: ERROR: This output format has been de

    Apr 17 17:36:17 localhost snmpd[2810]: /etc/snmp/snmpd.conf: line 311: Error: ERROR: This output for ...

  5. 【Linux】Linux下如何分区及如何格式化

    环境:CentOS7.1 磁盘大小是1.8T 将磁盘/dev/sda分一个分区,分区类型为xfs fdisk /dev/sda n    --创建新分区 p  --创建分区类型为主分区 1  --主分 ...

  6. 如何查看U盘的VID和PID

    1.将USB插入电脑 2.右键单击[此电脑],选择[管理] 3.在弹出的对话框中选择[设备管理器],选择[USB大容量存储设备] 4.右键单击[USB大容量存储设备],选择[属性],在弹出的对话框中选 ...

  7. 1.5V转5V的最少电路的芯片电路图

    PW5100满足1.5V转5V的很简洁芯片电路,同时达到了最少的元件即可组成DC-DC电路1.5V转5V的升压转换器系统. PW5100在1.5V转5V输出无负载时,输入效率电流极低,典型值10uA. ...

  8. Centos7 虚拟机优化

    配置yum源 rm -f /etc/yum.repos.d/* curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/ ...

  9. 使用 gitlab-runner 持续集成

    gitlab-runner 是 Gitlab 推出的与 Gitlab CI 配合使用的持续集成工具.当开发人员在 Gitlab 上更新代码之后,Gitlab CI 服务能够检测到代码更新,此时可以触发 ...

  10. mysqlG基于TID模式同步报错 (Last_IO_Errno: 1236)

    mysqlG基于TID模式同步报错Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading da ...