Sereja and Subsequences [CodeForces - 314C]

http://codeforces.com/problemset/problem/314/C

题意

给出一个长度为n的串,求里面所有非降子序列中所有数字的乘积的和。答案对1e9+7取模。

分析

感觉是神奇的计数题。
一开始的想法是算所有数字的贡献,一拍脑袋发现并不是单纯的计数。
然后其实就是考虑以某一个数字结尾的前面的所有方案乘积总和。
但是可以想到如果在一个段后面有两个相同的数字,那么就会有一部分计算重复,这个重复的部分专门记在数组里,之后再算的时候减去(这里所说的重复部分在代码中为num数组)。

code

#include<bits/stdc++.h>
#define ll long long
#define M 1000005
#define mo 1000000007
#define lowbit(p) (p&(-p))
using namespace std;
void read(int &x){
    x=0; char c=getchar();
    for (;c<48;c=getchar());
    for (;c>47;c=getchar())x=(x<<1)+(x<<3)+(c^48);
}
struct Tree{
    ll a[M];
    void add(int p,ll num){
        for (;p<M;p+=lowbit(p))a[p]=(a[p]+num)%mo;
    }
    ll qu(int p){
        ll res=0;
        for (;p;p-=lowbit(p))res+=a[p];
        return res%mo;
    }
}T;
int num[M];
int main(){
//  freopen("1.in","r",stdin);
    int n,x;
    ll res=0;
    read(n);
    for (int i=1;i<=n;i++){
        read(x);
        ll tmp=T.qu(x);
        res=(res+1ll*(1+tmp-num[x])*x%mo)%mo;
        T.add(x,(1+tmp-num[x])*x%mo);
        num[x]=1+tmp;
    }
    printf("%lld\n",(res%mo+mo)%mo);
    return 0;
}

总结

居然1A了…吃惊。
好了看题解看题解。这题想的感觉很玄妙啊。
感觉看了看站内别的题解写的好不走心啊怎么只有代码…orz
不过看起来都是树状数组瞎搞的样子。
暂定为是容斥一样的东西吧。

看了看cf上跑得最快的大佬,感觉有好多黑科技的样子…强啊。

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值