Problem description
  令f(x)为x的全部约数之和,x的约数即能够被x整除的数。如f(24)=1+2+3+4+6+8+12+24=60),求 f(l) + f(l + 1) + …… + f(r)
Input
  第一行为一个整数T(T<=100000),表示数据的组数。

接下来T行,每行有两个整数l。r(1 <= l <= r <= 200000)

Output
  对每组数据,输出f(l)+f(l+1)+……+f(r) 的和
Sample Input
2
3 5
10 20
Sample Output
17
270
Problem Source
  HUNNU Contest 

開始用这样的方法来求

 for(i = 3; i<=200000; i++)
{
a[i] = 1+i;
for(j = 2; j*j<=i; j++)
{
if(i%j==0)
{
a[i]+=j;
if(i/j!=j)
a[i]+=(i/j);
}
}
}

超时。改成

    for(i = 1; i<=200000; i++)
{
for(j = 1; i*j<=200000; j++)
{
a[i*j]+=i;
}
}

才AC

开了一个变量去级数,发现上面的运算了5千多W次

以下的才两百多W次

看来区别还是蛮大的

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <time.h>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8 LL a[N];
LL sum[N]; void init()
{
LL i,j,k;
MEM(a,0);
MEM(sum,0);
for(i = 1; i<=200000; i++)
{
for(j = 1; i*j<=200000; j++)
{
a[i*j]+=i;
}
}
for(i = 1; i<=200000; i++)
sum[i] = sum[i-1]+a[i];
}
int main()
{
LL i,j,k;
int l,r;
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&l,&r);
printf("%I64d\n",sum[r]-sum[l-1]);
} return 0;
}

hunnu11546:Sum of f(x)的更多相关文章

  1. hunnu Sum of f(x)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11546&courseid=0 Sum of f(x) ...

  2. hnnu 11546 Sum of f(x) (求一个数的全部约数和)

    代码: #include<cstdio> #include<cstring> #define N 200000 using namespace std; long long f ...

  3. three Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  4. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  5. geeksforgeeks@ Find sum of different corresponding bits for all pairs (Bit manipulation)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=387 Find sum of different corresponding b ...

  6. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...

  7. hunnu-11546--Sum of f(x)

    Sum of f(x) Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users:  ...

  8. 牛客练习赛19 E和F(签到就走系列)托米的饮料+托米搭积木

    E题传送门:点我 F题传送门:点我 可爱的小托米得到了n瓶饮料. 但他不小心把开盖的工具弄丢了,所以他只能利用饮料瓶来开盖. 已知第i个瓶子的品牌为ai,且其能打开bi品牌的瓶子. 问有几瓶饮料托米无 ...

  9. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...

随机推荐

  1. scanf输入字符串相关

    http://blog.csdn.net/liuhui_8989/article/details/13398793   补充..输入s的时候不要把变量设置成string类型,设置成char数组类型.. ...

  2. [BZOJ 4071] 巴邻旁之桥

    Link: BZOJ 4071传送门 Solution: 首先算出能提前算的贡献 $K=1$:肯定选中间的点,小学数学 $K=2$:对于每对$(x,y)$一定选离$(x+y)/2$近的桥 也就是说将$ ...

  3. 最短路径:我的理解--SPFA算法

    SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...

  4. MS SQL语句优化

    MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计 ...

  5. Shell基础学习(六) 流程控制

    1.if if的语法格式 if conditon then command1 command2 ``` commandn fi 2.if else if conditon then command1 ...

  6. xcode的ios工程目录结构复习

    目录结构: a.supporting files: main.m和资源文件 xxx-info.plist:包含应用程序相关属性列表,如版本,程序名等 .pch文件:预编译头文件,相当于MFC里的std ...

  7. HDU 4681 String(2013多校8 1006题 DP)

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  8. 【转】2012年6月25 – 某欧美上市企业PHP工程师最新面试题

    笔试: 尼玛,连页眉页脚都是英文!不过都还能读懂.题目很简单.印象深刻的有几题. 具体题目忘了,主要知识点考点是,建立的视图,实现有自增字段. 答:之前还真没考虑过这个问题.当时条件发射,给了一个用户 ...

  9. Source Insight常用快捷键及注释快捷键设置

    转:http://blog.csdn.net/tlaff/article/details/6536610 在使用SI过程中,我根据自己的使用习惯修改了它的默认快捷键,并且在配置文件中添加了一些人性化功 ...

  10. 【Linux编程】进程标识符与fork函数

    ID为0的进程一般是调度进程.常被称为交换进程(swapper),是内核中的系统进程. ID为1的进程叫做init进程,是一个普通用户进程,不属于内核,由内核调用. 一个现有进程能够调用fork函数创 ...