lightoj 1294 - Positive Negative Sign 【基础计数】
来源:互联网 发布:韩剧网软件下载 编辑:程序博客网 时间:2024/06/07 05:55
Given two integers: n and m and n is divisible by 2m, you have to write down the first n natural numbers in the following form. At first take first m integers and make their sign negative, then take next m integers and make their sign positive, the next m integers should have negative signs and continue this procedure until all the n integers have been assigned a sign. For example, let n be 12 and m be 3. Then we have
-1 -2 -3 +4 +5 +6 -7 -8 -9 +10 +11 +12
If n = 4 and m = 1, then we have
-1 +2 -3 +4
Now your task is to find the summation of the numbers considering their signs.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing two integers: n and m (2 ≤ n ≤ 109, 1 ≤ m). And you can assume that n is divisible by 2*m.
Output
For each case, print the case number and the summation.
Sample Input
Output for Sample Input
2
12 3
4 1
Case 1: 18
Case 2: 2
题意:给你1-n共n个数,给你一个数m(n % (2*m) == 0),让你每次截取m个数,按 [- + - +...]的顺序给它们添加符号,最后得到一个表达式,问表达式的结果是多少。
思路:首先n为10^9,不能模拟。
考虑 [- +] 临近2*m个数做出的贡献,求得值为m*m。同样若考虑[+ -] 临近2*m个数做出的贡献,求得值为-m*m。
这样,只需求出n分成多少个m,设temp = n / m,则相应的[- +] 或 [+ -]个数为temp / 2。
若temp为偶数,结果为所有[- +]做出的贡献。反之结果为所有[+ -]做出的贡献 + 前m个数之和。
AC代码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN 500000+10#define MAXM 50000000#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long longusing namespace std;int main(){ int t, kcase = 1; Ri(t); W(t) { LL n, m; Rl(n); Rl(m); LL temp = n / m; LL sum; if(temp & 1) { temp--; sum = -m * (m + 1)/2; sum += -m * m * temp / 2; } else sum = m * m * temp / 2; printf("Case %d: %lld\n", kcase++, sum); } return 0;}
- lightoj 1294 - Positive Negative Sign 【基础计数】
- LightOJ 1294:Positive Negative Sign
- lightoj 1294 - Positive Negative Sign
- lightoj 1294 Positive Negative Sign
- LIGHTOJ-1294 - Positive Negative Sign
- LightOJ 1294 Positive Negative Sign
- LightOJ 1294:Positive Negative Sign
- LightOJ 1294:Positive Negative Sign
- 【lightoj】-1294 Positive Negative Sign
- 【LightOJ】1294 - Positive Negative Sign
- LightOJ 1294 - Positive Negative Sign
- LightOJ 1294 Positive Negative Sign
- 【LightOJ 1294 Positive Negative Sign】
- LightOJ 1294 Positive Negative Sign【大水】
- lightOJ 1294 - Positive Negative Sign 【规律题】
- LightOJ 1294 - Positive Negative Sign (规律)
- 【水】LightOJ Positive Negative Sign 1294
- LightOJ 1294 - Positive Negative Sign【数学】
- 兄弟连专注IT教育 九周年再创辉煌
- window下连接mysql 错误代码 1045
- 计蒜客——难题库——“爬梯子”问题
- StringBuffer
- C#索引器与属性
- lightoj 1294 - Positive Negative Sign 【基础计数】
- Linux Programing -- ch14-- 信号量、共享内存、消息队列
- 矩阵覆盖那点事儿
- Java:按值传递还是按引用传递详细解说
- linux mysql
- Mysql主要配置文件知多少
- 创建Maven子项目时出现“Unable to read parent POM”的问题
- git内容整理--版本控制工具的高级用法
- Mysql实现实时备份