HDU 4655 2013多校联合赛第6场 Cut Pieces
2013-08-10 14:20
459 查看
Cut Pieces
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 717 Accepted Submission(s): 297
Problem Description
Suppose we have a sequence of n blocks. Then we paint the blocks. Each block should be painted a single color and block i can have color 1 to color ai. So there are a total of prod(ai) different ways to color the blocks.
Consider one way to color the blocks. We call a consecutive sequence of blocks with the same color a "piece". For example, sequence "Yellow Yellow Red" has two pieces and sequence "Yellow Red Blue Blue Yellow" has four pieces. What is S, the total number of
pieces of all possible ways to color the blocks?
This is not your task. Your task is to permute the blocks (together with its corresponding ai) so that S is maximized.
Input
First line, number of test cases, T.
Following are 2*T lines. For every two lines, the first line is n, length of sequence; the second line contains n numbers, a1, ..., an.
Sum of all n <= 106.
All numbers in the input are positive integers no larger than 109.
Output
Output contains T lines.
Each line contains one number, the answer to the corresponding test case.
Since the answers can be very large, you should output them modulo 109+7.
Sample Input
1 3 1 2 3
Sample Output
14 Hint Both sequence 1 3 2 and sequence 2 3 1 result in an S of 14.
题意 给你n个数的数列,让你确定一个排列a1~an,使得所有b1~bn满足bi<=ai的序列组成的块数的和最大。其中连续相同的数共同组成一个块。
思路 这题最优的排列是一大一小的排,这个是队友想出来的,我也没有深究。(事后发现他也只是找规律出来的= =、所以说这题A的有点开挂)。如何算一个排列的块数。最后我们是通过样例2 5 4还有其他自己YY的几组数据推出来的。对于任意一个序列a1~an,若其所有项的乘积为s,则该序列总共的块数和为:s+sum{s-s/max(a[i],a[i-1])}(i=2,3,4,5,6,...,n)。另外注意取模运算中遇到除法需要转化为乘以其逆元。
下面两份代码,除法逆元是我写的,还有一种不用除法而保存前缀后缀积的方法是队友写的。
AC代码1:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[1000006]; #define mod 1000000007 #define LL long long LL b[1000006]; LL exgcd(LL a, LL b, LL &x, LL &y) { if (b == 0) { x = 1; y = 0; return a; } int r = exgcd(b, a % b, y, x); y -= x * (a / b); return r; } LL Inv(LL a) { LL r, x, y; r = exgcd(a, mod, x, y); if (r == 1) return (x % mod + mod) % mod; return -1; } int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } sort(a + 1, a + 1 + n); int k = 0; for (int i = 1, j = n; i <= j; i++, j--) { b[++k] = a[i]; b[++k] = a[j]; } LL s = 1; for (int i = 1; i <= n; i++) { s = (s * b[i]) % mod; } LL ans = s; for (int i = 2; i <= n; i++) { LL tmp = Inv(max(b[i], b[i - 1]));//求逆元 LL tmp2 = s - ((s % mod) * (tmp % mod)) % mod; ans = (ans + tmp2) % mod; } while (ans < 0) ans += mod; ans = ans % mod; printf("%lld\n", ans); } }
AC代码2:
#include<iostream> #include<stdio.h> #include<algorithm> #include<stdlib.h> #include<math.h> #include<string.h> #define LL long long #define mod 1000000007 #define maxn 1000010 long long le[maxn]; long long rig[maxn]; using namespace std; LL val[maxn]; LL a[maxn]; int main(){ int i,j; int cas; cin>>cas; int n; while(cas--){ cin>>n; for(i=1;i<=n;i++){ scanf("%d",&val[i]); } sort(val+1,val+1+n); int l,r; for(l=1,r=n,i=1;i<=n;i++) { if(i%2==1) a[i]=val[l++]; else a[i]=val[r--]; } le[0]=1; for(i=1;i<=n;i++){ le[i]=(le[i-1]*a[i]+mod)%mod; } rig[n+1]=1; for(i=n;i>=1;i--){ rig[i]=(rig[i+1]*a[i]+mod)%mod; } LL ans=(n*le +mod)%mod; LL divv=0; for(i=1;i<n;i++){ divv=((divv+(le[i-1]*rig[i+2]+mod)%mod*min(a[i],a[i+1])+mod)%mod); } ans=(ans-divv+mod)%mod; cout<<ans<<endl; } }
相关文章推荐
- HDU 4658 2013 多校联合赛第6场 Integer Partition
- 2013 多校第六场 hdu 4655 Cut Pieces
- HDU 4655 Cut Pieces(2013多校6 1001题 简单数学题)
- HDU 4686 Arc of Dream (2013多校联合9 1001)
- hdu 4562 Dice 求期望 推数学公式 (2013多校联合)
- hdu 4677 并查集+分块算法 好题 (2013多校联合)
- HDU 4637 Rain on your Fat brother 线段与半圆和线段交 简单题 (2013多校联合)
- hdu 4607 park visit 2013多校联合训练第一场
- 2013 多校联合4 1011 Fliping game (hdu 4642)
- 2013 多校联合4 1007 Group(hdu 4638)
- HDU 4632 2013多校联合第4场 A - Palindrome subsequence
- 2013 多校联合 H Park Visit (hdu 4607)
- 2013 多校联合 F Magic Ball Game (hdu 4605)
- 2013 多校联合2 D Vases and Flowers (hdu 4614)
- 2013 多校联合 2 A Balls Rearrangement (hdu 4611)
- 2013多校联合4 1001 Palindrome subsequence(hdu 4632)
- 2013多校联合3 1010 No Pain No Game(hdu 4630)
- HDU 4638 2013多校联合第4场 G-Group
- 2013 多校联合 F Magic Ball Game (hdu 4605)
- hdu 4602 partition 2013多校联合训练第一场