ZOJ 3872 计算对答案的贡献

                                               D - Beauty of Array


Description

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output

For each case, print the answer in one line.

Sample Input

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

Sample Output

105
21
38

题意:给你一个数组,找出所有任意连续子序列中美数和,一段子序列美数和定义为:互异数的和题解:计算对答案的贡献

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1010;
const int MAX=151;
const int MOD=1000000007;
const int INF=1000000000;
const double EPS=0.00000001;
typedef long long ll;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int num[1000100];
int main()
{
    int a,i,n,T;
    ll ans;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        ans=0;
        memset(num,0,sizeof(num));
        for (i=1;i<=n;i++) {
            scanf("%d", &a);
            ans+=(ll)(i-num[a])*(n-i+1)*a;
            num[a]=i;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

代码

时间: 2024-07-31 14:22:07

ZOJ 3872 计算对答案的贡献的相关文章

UVA 11076 Add Again 计算对答案的贡献+组合数学

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of di?erent integer pairs with LCM is equal to N

UVA 11038 - How Many O&#39;s? 计算对答案的贡献

题意: 求[n, m]之间包含0的数字的个数题解:转化为求solve(n) - solve(m-1)的前缀问题 对于求0到n的解,我们举例 n = 25789 对于8这位,让其为0对答案的贡献是 (0~257)*(0~9) 假设是 n = 25709 那么让这位为0的答案贡献是 (0~256) * (0~9) + (257)* (0~9) //meek///#include<bits/stdc++.h> #include <cstdio> #include <cmath>

zoj 3872

D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3872 Appoint description:  System Crawler  (2015-04-30) Description Edward has an array A with N integers. He defines the beauty

poj 2006 Litmus Test 【即zoj 2351:计算酸的PH】

公式:  pH = -log10 [H+]  PH值根据氢离子浓度求出 ,[这里的 [H+] 浓度是摩尔每升为单位的] Ka = [H+] [acid ions] / [acid]   平衡常数K等于分解的氢离子和酸根离子乘积与未分解的酸分子的比值 输入格式:   Ka :常数   ori :初始酸浓度    m :1摩酸分子完全溶解 分解出氢离子数    n :1摩酸分子完全溶解 分解出氢离子数[注意是完全溶解] [这题需要特别注意的是]指数形式是可以直接输入的.如用scanf 或者 prin

DP ZOJ 3872 Beauty of Array

题目传送门 1 /* 2 DP:dp 表示当前输入的x前的包含x的子序列的和, 3 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: 4 sum 表示当前输入x前的所有和: 5 a[x] 表示id: 6 详细解释:http://blog.csdn.net/u013050857/article/details/45285515 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cmath>

ZOJ - 3872 Beauty of Array

题意:给定一个含有N个数的序列S,定义序列的魅力值为序列中不同数字之和,求出该序列所有子序列的魅力值之和. 分析:每个数乘以它出现的次数,求和即可. 如何求每个数出现的次数? 1.对于一个各数字完全不同的序列, eg:3 5 2 6 8 对于5来说,确定其存在于的子序列 (1)其右面,可选0个数字---5  可选1个数字---3 5 (2)其右面,可选0个数字---5  可选1个数字---5 2  可选2个数字---5 2 6        可选3个数字---5 2 6 8 因此,2 * 4 =

ZOJ 3872 Beauty of Array&amp;&amp;ZOJ 3870 Team Formation

3872链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 题目大意:给你n个数,问所有的连续的子序列中的所有元素的和(子序列中有相同元素只计算一次)(n<100000). 即若序列为1  2  3,则组成1,2,3,1 2,2 3,1 2 3,和为20: 若序列为1 2 2,则组成1,2,2,1 2,2 2,1 2 2,和为13: 解题思路:求不重复的序列和很简单,关键是去重. 现在看一个序列: 3  4 

ZOJ 3872 (思维)

题意: 给你一个串,  串和为所有不相同数字之和,  然后让你求该串所有连续子串总和为多少. 题解: 从前往后扫一次   ,  记录当前数字出现次数,  计算和的时候减去该数字乘出现次数 代码: #include<stdio.h> #include<iostream> #include<map> using namespace std; map<int, long long int> mark; int main() { int T, a, n; scanf

ZOJ 3872 Beauty of Array(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty