abc310e <公式递推(dp?)>

发布时间 2023-07-17 09:45:27作者: O2iginal

题目

E - NAND repeatedly

思路

image

总结

  • 公式递推, 找到相邻两项间的关系;

代码

Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e6 + 10;
LL a[N];
LL b[N];
int n;
string s;

// LL cal(LL x, int n, bool y)
// {
//     if (y) return n - x;
//     else return n;
// }

void solv()
{
    cin >> n >> s; 
    int n = s.size();
    s = " " + s; 

    for (int i = 1; i <= n; i ++)
    {
        // b[i] = cal(b[i-1], i-1, s[i] - '0') + (s[i] - '0');
        int Ai = s[i] - '0';
        b[i] = (Ai ? (i-1 - b[i-1]) : i-1) + Ai;
        a[i] = a[i-1] + b[i];
    }
    cout << a[n] << endl;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T = 1;
	// cin >> T;
    while (T --)
    {
        solv();
    }
    return 0;
}