Odd Grasshopper 奇偶找规律

在这里插入图片描述
思路 :

  • 首先根据输入的奇偶性分别找规律,发现与输入无关,都满足4个一循环的规律
  • 如果次数n刚好是4的倍数,那么就是x0,否则,找到小于n中最靠近n的一个4的倍数,从它开始模拟到n,就是答案
#include <iostream>
#include <algorithm>
#include <math.h>
 
using namespace std;
 
typedef long long ll;
 
ll td(ll start, ll end, ll ans)
{
    
    
    for (ll i = start + 1; i <= end; i ++ )
    {
    
    
        if (ans % 2 == 0) ans = ans - i;
        else ans = ans + i;
    }
    return ans;
}
 
int main()
{
    
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int _;
    cin >> _;
    
    while (_ -- )
    {
    
    
        ll x0, n;
        cin >> x0 >> n;
        
        if (n % 4 == 0)
        {
    
    
            cout << x0 << endl;
            continue;
        }
        
        ll bc = 0;
        for (ll i = n - 1; ; i -- )
            if (i % 4 == 0)
            {
    
    
                bc = i;
                break;
            }
        
        cout << td(bc, n, x0) << endl;
    }
    
    return 0;
}

おすすめ

転載: blog.csdn.net/m0_51448653/article/details/121193541