Sicily 2506. 绝对值序列

2506. 绝对值序列

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

  给你一个序列 , S1=a, S2=b, Sn=|Sn-1-Sn-2|(n>=3).
现在给你 a,b 和一些询问 K,  输出这个序列的第 K 项是多少 .

Input

输入文件第一行是 2 个整数 , a,b.
第二行一个数 n, 表示询问的个数 .

接下来n,每行一个正整数K,表示询问这个序列的第K项是多少.

n<=50
0<=a,b,K<=10^18

Output

输出文件有 n n 个询问进行回答 .

Sample Input

21 12
5
1
2
3
4
5

Sample Output

21
12
9
3
6

Problem Source

黄金周赛——省赛最后的冲刺

// Problem#: 2506
// Submission#: 3275461
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
using namespace std;

int main() {

    std::ios::sync_with_stdio(false);

    int caseNum;
    long long int Oria, Orib;

    cin >> Oria >> Orib >> caseNum;

    while (caseNum--) {

        long long int a = Oria, b = Orib;
        long long int counter = 1;
        long long int ans;
        long long int k;
        cin >> k;

        while (1) {

            if (counter == k) {
                ans = a;
                break;
            }

            if (a == b) {

                if ((k - counter + 1) % 3 == 0) {
                    ans = 0;
                    break;
                } else {
                    ans = a;
                    break;
                }

            } else if (b != 0 && a / b >= 2) {

                long long int sureNum = a / b / 2 * 3;

                if (counter <= k && k <= counter + sureNum) {
                    if ((k - counter) % 3 == 0) {
                        ans = a - (k - counter) / 3 * b * 2;
                        break;
                    } else if ((k - counter) % 3 == 1) {
                        ans = b;
                        break;
                    } else {
                        if (a - (k - counter) / 3 * b * 2 > b) ans = a - (k - counter) / 3 * b * 2 - b;
                        else ans = b - (a - (k - counter) / 3 * b * 2);
                        break;
                    }

                } else {

                    counter += a / b / 2 * 3 + 1;
                    long long int temp = b;
                    if (a - sureNum / 3 * b * 2 > temp) b = a - sureNum / 3 * b * 2 - temp;
                    else b = temp - (a - sureNum / 3 * b * 2);
                    a = temp;
                }
        
            } else {
            
                long long int next;
                if (a > b) next = a - b;
                else next = b - a;
                a = b;
                b = next;
                counter++;

            }

        }

        cout << ans << endl;

    }

    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值