The ? 1 ? 2 ? ... ? n = k problem ——求和规律

本文介绍了一种算法,用于解决通过在连续整数序列1到n之间插入加号或减号来达到特定目标值的问题。该算法首先确定一个足够大的n值,使序列之和超过目标值,然后调整序列直至差值为偶数,从而实现目标。

 The ? 1 ? 2 ? ... ? n = k problem 

The problem

Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k

For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12
with n = 7

The Input

The first line is the number of test cases, followed by a blank line.

Each test case of the input contains integer k (0<=|k|<=1000000000).

Each test case will be separated by a single line.

The Output

For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

2

12

-3646397

Sample Output

7

2701

Alex Gevak
September 15, 2000 (Revised 4-10-00, Antonio Sanchez)



题意:给出最后的结果,在1,2,3,4,。。。。n的前面添加+或者-,使得式子运算结果等于给出的结果
解析:如果给定的结果是一个正数,那么我们需要找到一个前N项和大于给定结果的n,然后在此基础上添加减号。这里需要注意的是,在一个数前面添加一个减号,就相当于在总和里减去这个数的2倍,所以总和和给定的结果之间的差值一定要是偶数才可以,如果不是偶数,则无从添加减号。所以找到的n不是最小的,需要继续添加。

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int main()
{
    int i,j,n,t;
    long long k,sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&k);
        if(k < 0)
            k = -k;
        for(i = 1;i < 100000;i++)
        {
            if(i * (i + 1) >= 2 * k)
                break;
        }
        sum = (i * (i + 1))/2;
        while((sum - k)%2 != 0)
        {
            i++;
            sum += i;
        }
        if(t > 0)
        printf("%d\n\n",i);
        else
            printf("%d\n",i);
    }
    return 0;
}

F. Attraction Theory time limit per test2 seconds memory limit per test256 megabytes Taylor Swift - Love Story (Taylor's Version) ⠀ There are 𝑛 people in positions 𝑝1,𝑝2,…,𝑝𝑛 on a one-dimensional coordinate axis. Initially, 𝑝𝑖=𝑖 for each 1≤𝑖≤𝑛. You can introduce an attraction at some integer coordinate 𝑥 (1≤𝑥≤𝑛), and then all the people will move closer to the attraction to look at it. Formally, if you put an attraction in position 𝑥 (1≤𝑥≤𝑛), the following changes happen for each person 𝑖 (1≤𝑖≤𝑛): if 𝑝𝑖=𝑥, no change; if 𝑝𝑖<𝑥, the person moves in the positive direction, and 𝑝𝑖 is incremented by 1; if 𝑝𝑖>𝑥, the person moves in the negative direction, and 𝑝𝑖 is decremented by 1. You can put attractions any finite number of times, and in any order you want. It can be proven that all positions of a person always stays within the range [1,𝑛], i.e. 1≤𝑝𝑖≤𝑛 at all times. Each position 𝑥 (1≤𝑥≤𝑛), has a value 𝑎𝑥 associated with it. The score of a position array [𝑝1,𝑝2,…,𝑝𝑛], denoted by 𝑠𝑐𝑜𝑟𝑒(𝑝), is ∑𝑛𝑖=1𝑎𝑝𝑖, i.e. your score increases by 𝑎𝑥 for every person standing at 𝑥 in the end. Over all possible distinct position arrays 𝑝 that are possible with placing attractions, find the sum of 𝑠𝑐𝑜𝑟𝑒(𝑝). Since the answer may be large, print it modulo 998244353. Input Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). The description of the test cases follows. The first line of each test case contains a single integer 𝑛 (1≤𝑛≤2105). The second line of each test case contains 𝑛 integers — 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2105. Output For each test case, output a single line containing an integer: the sum of 𝑠𝑐𝑜𝑟𝑒(𝑝) over all possible distinct position arrays 𝑝 that are possible with placing attractions, modulo 998244353. Example inputCopy 7 1 1 2 5 10 3 1 1 1 4 1 1 1 1 4 10 2 9 7 5 1000000000 1000000000 1000000000 1000000000 1000000000 8 100 2 34 59 34 27 5 6 outputCopy 1 45 24 72 480 333572930 69365 Note In the first test case, the only possible result is that person 1 stays at 1. The score of that is 𝑎1=1. In the second test case, the following position arrays [𝑝1,𝑝2] are possible: [1,2], score 15; [1,1], score 10; [2,2], score 20. The sum of scores is 15+10+20=45. In the third test case, the following position arrays [𝑝1,𝑝2,𝑝3] are possible: [1,1,1]; [1,1,2]; [1,2,2]; [1,2,3]; [2,2,2]; [2,2,3]; [2,3,3]; [3,3,3]. Each has a score of 3, and thus the total sum is 24.解决这道题,用c++
09-25
c++14 ## 题目描述 小 L 给你出了一道简单数学题。 对于一个长度为 $n$ 的排列 $p_n$,我们在 $i$ 与 $p_i$ 之间连无向边,设形成的连通块个数为 $C(p_n)$。 对于一个连通块 $S$,如果他包含的点的编号为 $x_1,x_2\dots x_k$,那么他的权值为 $w(S)=\sum\limits_{i=1}^k 2^{x_i}$。 如果连通块分别为 $S_1,S_2\dots S_{C(p_n)},那么$记该排列的权值为 $\sum\limits_{i=1}^{C(p_n)}2^{w(S_i)}$。 记 $F(n,m)$ 表示 $C(p_n)=m$ 的 $p_n$ 个数。 记 $G(n,m)$ 为 $C(p_n)=m$ 的 $p_n$ 的不同权值数量。 这里认为 $F(0,0)=G(0,0)=1$。 现在有 $Q$ 次询问,每次给出 $op,n,m$: - 如果 $op=1$,你需要输出 $F(n,m)\bmod 2$。 - 如果 $op=2$,你需要输出 $G(n,m)\bmod 2$。 ## 输入格式 第一行一个整数 $Q$ 代表询问次数。 接下来 $Q$ 行,每行三个整数 $op,n,m$ 代表小 L 的一次询问。 - 如果 $op=1$,你需要输出 $F(n,m)\bmod 2$。 - 如果 $op=2$,你需要输出 $G(n,m)\bmod 2$。 ## 输出格式 一行一个长度为 $Q$ 的 $01$ 字符串代表答案序列。 ## 输入输出样例 #1 ### 输入 #1 ``` 20 1 3 8 2 10 2 2 8 2 1 3 1 2 3 3 1 1 5 1 5 10 2 9 8 1 9 5 2 6 6 2 1 0 2 6 4 1 5 3 1 2 5 2 4 7 2 0 2 2 0 5 2 8 1 2 7 1 1 8 8 ``` ### 输出 #1 ``` 01101000110110000111 ``` ## 说明/提示 - 对于 $20\%$ 的数据,$0\leq n,m\leq 10$。 - 对于 $40\%$ 的数据,$0\leq n,m\leq 5000$。 - 对于另外 $15\%$ 的数据,$op=1$。 - 对于另外 $15\%$ 的数据,$op=2$。 - 对于另外 $15\%$ 的数据,$0\leq n,m\leq 10^5$。 - 对于 $100\%$ 的数据,$1\leq Q\leq 10^6,0\leq n,m\leq 10^9,1\leq op\leq 2$。
最新发布
12-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值