牛客练习赛44 小y的线段【思维】

传送门

题目描述:
给出n条线段,第i条线段的长度为 ai ,每次可以从第 i 条线段的j位置跳到第 i + 1 条线段的 j+1 位置。如果第 i+1 条线段长度不到j+1,那么就会回到第 i 条线段的 0 位置,然后继续跳。问从第 i 条线段的 0 位置跳到第 n 条线段需要跳多少次。

解题思路:

题目上的 n 的范围 (n ≤ 2e7 ),我们推断出只能用 O( n ) 的复杂度来解决。我们知道从 i 条线段跳到 n 条线段至少需要(n - i)次。我们可以倒着推,如果从 i 线段的 0 会跳跃到一条线段的 0 位置,那么 0 到 i-1 的所有线段都会 跳跃到该条线段的0位置。

代码:

///#include<bits/stdc++.h>
///#include<unordered_maps>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>

#define MT(a, b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai = acos(-1.0);
const double E = 2.718281828459;
//const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;

int a[20000010];
unsigned int SA, SB, SC;
int mod;

unsigned int Rand() {
    SA ^= SA << 16;
    SA ^= SA >> 5;
    SA ^= SA << 1;
    unsigned int t = SA;
    SA = SB;
    SB = SC;
    SC ^= t ^ SA;
    return SC;
}

int main() {
    ll n;
    cin >> n >> mod >> SA >> SB >> SC;
    for (int i = 1; i <= n; ++i) a[i] = Rand() % mod + 1;
    ll ans = n * (n - 1) / 2;
    int x = a[n];
    for (ll i = n - 1; i >= 1; --i) {
        x--;
        if (x > a[i])   ///取小的值作为限制条件
            x = a[i];
        if (x == 0) {   ///这个位置前面的每个线段都会多跳跃一次
            ans = ans + (i - 1);
            x = a[i];
        }
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42211531/article/details/89416296