1051: Glamor Sequence

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 16  Solved: 5
[Submit][Status][Web
Board
]

Description

you have a sequence +1, +2, +3, ... +m, -(m + 1), -(m + 2), ..., -(2m), (2m + 1), (2m + 2), ..., (3m), .....
and you need calculator sum of the first n items.

Input

For each test case, there is a line contains two integers n and m, (1 <= m <= n <= 1000000000).

Output

For each test case, print sum of the first n items.

Sample Input

8 2
10 3

Sample Output

-8
5

HINT

For the first sample, the sequence is 1, 2, -3, -4, 5, 6, -7, -8, so the answer is -8.

For the second sample, the sequence is 1, 2, 3, -4, -5, -6, 7, 8, 9, -10, so the answer is 5.

想了很久还是不会。问了同学大概思路,确是用等差数列求和公式,但是先前的做法效率比较低,用了for循环,一旦出现n比较大而m非常小的话就会超时,比如n=1000000000,m=1,test测试一下5秒多。

因此一种思路是这样:

可以发现从1开始前2*m项的和的绝对值是一个常数即m,e=n-n%(2*m)那么只需处理后面e+1~n的数字即可。因此可以有如下规律

设剩下的区间为e+1~n(也可能是e+m)。

1、若e+m<=n——即数轴分布情况为e+1~e+m~n。只需将之前的所有完整段的和加上Sum[e+1,e+m]即可,且每一项都是正数。

2、若e+m>n——即数轴分布情况为e+1~n~e+m。那么要计算S1[e+1,n]与S2[n+1,e+m],ans=ans+S1-S2。

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void)
{
long long n,m,e;
long long ans;
while (~scanf("%lld%lld",&n,&m))
{
e=n-n%(2*m);
ans=(e)*(-m)/2;//所有完整段求和
if(e+m<n)//分类讨论
{
ans=ans+(2*e+1+m)*m/2;
ans=ans-(e+m+1+n)*(n-e-m)/2;
}
else if(e+m>n)
{
ans=ans+(e+1+n)*(n-e)/2;
}
else
{
ans=ans+(2*e+1+m)*m/2;
}
printf("%lld\n",ans);//这题要用long long来储存答案
}
return 0;
}

ACM程序设计选修课——1051: Glamor Sequence(YY+求和公式)的更多相关文章

  1. ACM程序设计选修课——1058: Lucky Sequence(思考)

    1058: Lucky Sequence Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 52  Solved: 6 [Submit][Status][ ...

  2. ACM程序设计选修课——1043: Radical loves integer sequences(YY)

    1043: Radical loves integer sequences Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 36  Solved: 4 ...

  3. ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)

    1040: Alex and Asd fight for two pieces of cake Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 27   ...

  4. ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)

    1036: Hungar的菜鸟赛季 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 20  Solved: 14 [Submit][Status][Web ...

  5. ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence 时间限制: 1 Sec  内存限制: 32 MB 提交: 70  解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...

  6. ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)

    问题 A: (ds:队列)打印队列 时间限制: 1 Sec  内存限制: 128 MB 提交: 25  解决: 4 [提交][状态][讨论版] 题目描述 网络工程实验室只有一台打印机,它承担了非常繁重 ...

  7. ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)

    问题 E: (ds:图)公路村村通 时间限制: 1 Sec  内存限制: 128 MB 提交: 9  解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...

  8. ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)

    问题 F: (ds:图)旅游规划 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ...

  9. ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

随机推荐

  1. WPF知识点全攻略11- 命令(Command)

    先看一下命令的简单使用: <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Cut ...

  2. python_106_创建类的两种方式

    class Foo(object): def __init__(self, name): self.name = name f = Foo("alex") print(type(f ...

  3. Spring boot 集成Kafka

    搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...

  4. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fundService': Injection of resource dependencies failed;

    在进行SSM的Controller的编写, 从浏览器访问后端Controller的时候遇到了这个问题. 这个问题的描述: 创建Bean的对象失败 错误代码如下: @Service("fund ...

  5. React入门教程(二)

    前言 距离上次我写 React 入门教程已经快2个月了,年头年尾总是比较忙哈,在React 入门教程(一)我大概介绍了 React 的使用和一些注意事项,这次让我们来继续学习 React 一. Rea ...

  6. ipvsadm启动报错解决方法

    Centos7 yum -y install ipvadm 安装后,启动ipvsadm却报错. Redirecting to /bin/systemctl start ipvsadm.service ...

  7. Confluence 导出为 PDF 格式 - 导出多个页面或者整个空间

    使用 Confluence 的空间导出功能,你可以将多个页面或者整个 Confluence 站点转换为 PDF 文件. 希望使用空间导出功能,你需要 导出空间(Export Space)权限.请查看 ...

  8. Decorator——Python初级函数装饰器

    最近想整一整数据分析,在看一本关于数据分析的书中提到了(1)if __name__ == '__main__' (2)列表解析式 (3)装饰器. 先简单描述一下前两点,再详细解说Python初级的函数 ...

  9. usb hub 设备流程图

    在此处负责而来:http://blog.csdn.net/xuelin273/article/details/38646851  下面的转载于:http://blog.csdn.net/qianguo ...

  10. LeetCode(189) Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...