F:牛牛的Link Power I

传送门

题意:

在这里插入图片描述

分析:

先把字符为1的位置放到一个p数组里 p 1 , p 2 , p 3 . . . . . . p c n t p_1,p_2,p_3......p_{cnt}
然后我们发现数组的Link值为:
p c n t p c n t 1 + p c n t p c n t 2 + p c n t p c n t 3 + . . . . . . + p c n t p 1 + p c n t 1 p c n t 2 + . . . . . . + p c n t 1 p 1 + . . . . . . + p 2 p 1 p_{cnt}-p_{cnt-1}+p_{cnt}-p_{cnt-2}+p_{cnt}-p_{cnt-3}+......+p_{cnt}-p_{1}+p_{cnt-1}-p_{cnt-2}+......+p_{cnt-1}-p_{1}+......+p_2-p_1
从后往前看,他实际就是第k位的值乘以k-1减去前面k-1个的和
处理一个前缀和即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#define Pll make_pair
using namespace std;
typedef long long ll;
const int MAXN=2e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int phi=1e9+6;
char ch[MAXN];
ll p[MAXN];
ll sum[MAXN];
int main()
{
    int n;
    scanf("%d ",&n);
    int cnt=0;
    for(int i=1;i<=n;i++){
        scanf("%c",&ch[i]);
        if(ch[i]=='1')
            p[++cnt]=i;
    }
    for(int i=1;i<=cnt;i++)sum[i]=(sum[i-1]+p[i])%mod;
    ll ans=0;
    for(int i=cnt;i>=2;i--){
        ans=(ans+(i-1)*p[i]%mod-sum[i-1]+mod)%mod;
    }
    printf("%lld\n",ans%mod);
    return 0;
}
/*
 
 */
发布了142 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104227553