hunnu11546:Sum of f(x)
| 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 |
| Sample Output |
17 |
| 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)的更多相关文章
- hunnu Sum of f(x)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11546&courseid=0 Sum of f(x) ...
- hnnu 11546 Sum of f(x) (求一个数的全部约数和)
代码: #include<cstdio> #include<cstring> #define N 200000 using namespace std; long long f ...
- 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 ...
- hdu 4389 X mod f(x) 数位DP
思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...
- 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 ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- hunnu-11546--Sum of f(x)
Sum of f(x) Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: ...
- 牛客练习赛19 E和F(签到就走系列)托米的饮料+托米搭积木
E题传送门:点我 F题传送门:点我 可爱的小托米得到了n瓶饮料. 但他不小心把开盖的工具弄丢了,所以他只能利用饮料瓶来开盖. 已知第i个瓶子的品牌为ai,且其能打开bi品牌的瓶子. 问有几瓶饮料托米无 ...
- 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号结 ...
随机推荐
- Android签名有什么作用?
应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名,系统才会允许安装升级的应用程序.如果你采用了不同的证书,那么系统会要求你的应用程序采用不同 ...
- 不按装mysql情况下,php安装pdo_mysql
安装pdo时遇到 --with-pdo-mysql 这个要指向mysql安装目录:可是我这台机器不安装mysql; 解决方法: [root@localhost app]# yum install ...
- Python,JAVA中子类的构造函数与父类构造函数的关系
Python: 子类不重载.覆盖父类的构造函数(子类不自己定义构造函数),则构造子类时会调用父类构造函数 若子类覆盖了父类的构造函数,则构造子类时不执行父类的构造函数,但仍继承了父类,如需调用父类构造 ...
- Html基础知识详解
一定要做的符合客户要求,不是自己认为对的. 一.基础标签 1.1 大小颜色位置 <!DOCTYPE HTML> <html> <head> <meta htt ...
- visual studio 2008试用版的评估期(万能破解)
教程 http://jingyan.baidu.com/article/a3a3f811ee87268da2eb8ae7.html 参考: http://blog.chinaunix.net/uid- ...
- jQuery向父辈遍历的方法
通过DOM树可以可容易的访问到html文档中的所有元素 例如向上访问父辈的元素有以下方法 1.parent()方法可以得到所定元素的直接父元素 $("span").parent ...
- FAQ:Domain Event 和 C# 中的 Event 有啥区别?
问: Domain Event 和 C# 中的 Event 有啥区别? 答: C# 中的 Event,事件.监听者列表和事件发布器是由一个类型承担,事件源和监听者之间的生命周期耦合在一起,C# 帮你提 ...
- pytest文档12-skip跳过用例
前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是 ...
- Android 工程报错解决 Unable to resolve target 'android-17'
转自:http://www.cnblogs.com/csulennon/p/3705177.html 换了系统后,重新安装了Android SDK和ADT插件,导入之前的工作空间.居然发现所有的And ...
- Android屏幕尺寸适配注意事项
1 基本设置 1.1 AndroidManifest.xml设置 在中添加子元素 android:anyDensity="true"时,应用程序安装在不同密度的终端上时,程序会分别 ...