Codeforces - 466C. Number of Ways - 思维、暴力

Number of Ways

题目链接

分类:思维、暴力

1.题意概述

  • 给你n个数a[1…n],要你找符合条件的 i,j(2ijn1) ,满足 k=1i1ak=k=ijak=k=j+1nak ,求符合条件的 i,j 的组合数?

2.解题思路

  • 本意就是要你找三段使得他们和相等,那么数列首先得能被分成三段,也就是 sum%3=0 才行,然后再按组合统计即可,细节可以参见代码。

3.AC代码

#include 
#define INF 0x3f3f3f3f
#define maxn 500010
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn];
ll sum[maxn];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    int n;
    scanf("%d", &n);
    sum[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        sum[i] = sum[i - 1] + a[i];
    }
    ll cnt = 0;
    if (sum[n] % 3 == 0)
    {
        ll tmp1 = sum[n] / 3;
        ll tmp2 = tmp1 << 1;
        int count = 0;
        for (int i = 1; i < n; i++)
        {   
            if (sum[i] == tmp2)
                cnt += count;
            if (sum[i] == tmp1)
                count++;
        }
    }
    printf("%I64d\n", cnt);
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}

你可能感兴趣的:(Codeforces,思维,暴力)