题目链接

题解

20pts

$O(n^3)$枚举$x,y,z$,根据题目要求判断

40pts

$O(n^2)$枚举$x,z$,需要满足$x,z$奇偶相同

20~40pts的代码我都没有写过...就不贴了

70~90pts

尝试对40pts的暴力进行优化

题目对三元组的两个限制我们在40pts的时候已经用来优化过了,还有一个限制是颜色相同

我们可以以颜色为关键字对数组进行排序,对每个数算答案的时候,只需要枚举相同颜色的这一段即可(因为三元组要求有序,所以要从$x+2$开始枚举)

这样的复杂度是$O(n*cnt_{col})$(这里的$cnt_{col}$为出现次数最多的颜色的出现次数)

使用$scanf$将会得到70pts

使用快读或$scanf+O2$将会得到80pts

使用快读+$O2$将会得到90pts

($fread$在这里的效果和快读是差不多的,因为数只有$1e5$个)

  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define inf 0x3f3f3f3f
  5. #define il inline
  6.  
  7. namespace io {
  8.  
  9. #define in(a) a=read()
  10. #define out(a) write(a)
  11. #define outn(a) out(a),putchar('\n')
  12.  
  13. #define I_int int
  14. inline I_int read() {
  15. I_int x = , f = ; char c = getchar() ;
  16. while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
  17. while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
  18. return x * f ;
  19. }
  20. char F[ ] ;
  21. inline void write( I_int x ) {
  22. I_int tmp = x > ? x : -x ;
  23. if( x < ) putchar( '-' ) ;
  24. int cnt = ;
  25. while( tmp > ) {
  26. F[ cnt ++ ] = tmp % + '' ;
  27. tmp /= ;
  28. }
  29. while( cnt > ) putchar( F[ -- cnt ] ) ;
  30. }
  31. #undef I_int
  32.  
  33. }
  34. using namespace io ;
  35.  
  36. using namespace std ;
  37.  
  38. #define N 100010
  39. const int mod = ;
  40.  
  41. int n = read() , m = read() ;
  42. struct node {
  43. int col , val , id ;
  44. } a[ N ] ;
  45.  
  46. bool cmp( node a , node b ) {
  47. return a.col < b.col ;
  48. }
  49.  
  50. int main() {
  51. for( int i = ; i <= n ; i ++ ) a[ i ].val = read() , a[ i ].id = i ;
  52. for( int i = ; i <= n ; i ++ ) a[ i ].col = read() ;
  53. sort( a + , a + n + , cmp ) ;
  54. int cur = ;
  55. ll ans = ;
  56. for( int i = ; i <= n ; i ++ ) {
  57. ll sum = ;
  58. for( cur = i + ; a[ i ].col == a[ cur ].col && cur <= n ; cur ++ ) {
  59. if( ( a[ i ].id + a[ cur ].id ) % == )
  60. sum = ( sum + ( 1ll * ( a[ i ].id + a[ cur ].id ) % mod * 1ll * ( a[ i ].val + a[ cur ].val ) % mod ) % mod ) % mod ;
  61. }
  62. ans = ( ans + sum ) % mod ;
  63. }
  64. printf( "%lld\n" , ( ans + mod ) % mod ) ;
  65. return ;
  66. }

100pts

考虑使用数学方法优化以上做法

我们现在有什么条件呢,列举一下

1.$x$和$z$的奇偶性相同且颜色相同

2.求和公式为$(x+z)(num_x+num_z)$

从求和公式入手,把括号拆掉,式子变成$x*num_x+x*num_z+z*num_x+z*num_z$

设$x1,x2,x3$的奇偶性相同且颜色相同(那么他们组成的二元组就等同于符合条件的三元组)

考虑$x1$对答案的贡献:

对于$(x1,x2)$,$x1$的贡献为$x1*num_{x1}+x1*num_{x2}$

对于$(x1,x3)$,$x1$的贡献为$x1*num_{x1}+x1*num_{x3}$

那么$x_1$对答案的贡献就是

$x1*(num_{x2}+num_{x3})+2*x1*num_{x1}$

然后多加入一个数$x4$也可以得到类似的贡献(多代几个也就看出来规律了)

推广到$x_n$结论也是一样的

结论:

设$s=\sum num_{xi}$(这里的$xi$均满足条件1)$cnt=cnt_{col}$,$cnt_{col}$为当前颜色奇偶相同的数的个数

则$xi$对答案的贡献为$xi*(s-num_{xi})+(cnt-1)*xi*num_{xi}$($cnt$要$-1$,因为$xi$不能和自己组成二元组)

所以将$s$和$cnt$统计出来就行了,注意要分奇偶统计

复杂度$O(n)$

  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define inf 0x3f3f3f3f
  5. #define il inline
  6. #define int long long
  7.  
  8. namespace io {
  9.  
  10. #define in(a) a=read()
  11. #define out(a) write(a)
  12. #define outn(a) out(a),putchar('\n')
  13.  
  14. #define I_int int
  15. inline I_int read() {
  16. I_int x = , f = ; char c = getchar() ;
  17. while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
  18. while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
  19. return x * f ;
  20. }
  21. char F[ ] ;
  22. inline void write( I_int x ) {
  23. if( x == ) { putchar( '' ) ; return ; }
  24. I_int tmp = x > ? x : -x ;
  25. if( x < ) putchar( '-' ) ;
  26. int cnt = ;
  27. while( tmp > ) {
  28. F[ cnt ++ ] = tmp % + '' ;
  29. tmp /= ;
  30. }
  31. while( cnt > ) putchar( F[ -- cnt ] ) ;
  32. }
  33. #undef I_int
  34.  
  35. }
  36. using namespace io ;
  37.  
  38. using namespace std ;
  39.  
  40. #define N 100010
  41. const int mod = ;
  42.  
  43. int n , m ;
  44. struct node {
  45. int col , val ;
  46. } a[ N ] ;
  47. int cnt[ N ][ ] , sum[ N ][ ] ;
  48.  
  49. signed main() {
  50. n = read() , m = read() ;
  51. for( int i = ; i <= n ; i ++ ) a[ i ].val = read() ;
  52. for( int i = ; i <= n ; i ++ ) a[ i ].col = read() ;
  53. for( int i = ; i <= n ; i ++ ) {
  54. cnt[ a[ i ].col ][ i& ] ++ ;
  55. sum[ a[ i ].col ][ i& ] = ( sum[ a[ i ].col ][ i& ] + a[ i ].val ) % mod ;
  56. }
  57. ll ans = ;
  58. for( int i = ; i <= n ; i ++ ) {
  59. ll num = cnt[ a[ i ].col ][ i& ] , s = sum[ a[ i ].col ][ i& ] ;
  60. ans = ( ans + ( i*(s-a[i].val) + (num-)*a[i].val*i ) % mod ) % mod ;
  61. }
  62. printf( "%lld\n" , ans % mod ) ;
  63. return ;
  64. }

Luogu 2671 求和 NOIP2015T3的更多相关文章

  1. luogu 4427 求和

    bjoi 2018 求和 唯一一道可能切的题一个数组还没开long long就成0分了 题目大意: 一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k次方和,而且每次的k可能是不同的 此处 ...

  2. [洛谷2671]求和<前缀和&模拟>

    题目链接:https://www.luogu.org/problemnew/show/P2671 这是noip2015普及组的第三题,谁说的普及组的题就一定水的不行,这道题就比较有意思的 这道题的暴力 ...

  3. Luogu P1625 求和

    题意 给定两个整数 \(n,m\),求 \[\sum\limits_{i=1}^{n}\frac{1}{\prod\limits_{j=i}^{i+m-1}j} \] \(\texttt{Data R ...

  4. luogu P1630 求和(枚举暴力)

    题意 题解 可以发现当a=10001时, 和1是等价的. 所以这题就水了. #include<iostream> #include<cstring> #include<c ...

  5. [Luogu] 余数求和

    question: $$\sum_{i=1}^{n} k \bmod i$$$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$$$\sum_{i= ...

  6. [Luogu 2261] CQOI2007 余数求和

    [Luogu 2261] CQOI2007 余数求和 这一定是我迄今为止见过最短小精悍的省选题了,核心代码 \(4\) 行,总代码 \(12\) 行,堪比小凯的疑惑啊. 这题一看暴力很好打,然而 \( ...

  7. BZOJ 4555 Luogu P4091 [HEOI2016/TJOI2016]求和 (第二类斯特林数)

    题目链接 (luogu) https://www.luogu.org/problem/P4091 (bzoj) https://www.lydsy.com/JudgeOnline/problem.ph ...

  8. [Luogu P2261] [CQOI2007]余数求和 (取模计算)

    题面 传送门:https://www.luogu.org/problemnew/show/P2261 Solution 这题显然有一个O(n)的直接计算法,60分到手. 接下来我们就可以拿出草稿纸推一 ...

  9. 【Bzoj4555】【Luogu P4091】求和(NTT)

    题面 Bzoj Luogu 题解 先来颓柿子 $$ \sum_{i=0}^n\sum_{j=0}^iS(i,j)2^jj! \\ =\sum_{j=0}^n2^jj!\sum_{i=0}^nS(i,j ...

随机推荐

  1. Memory consumption of popular Java data types

    http://java-performance.info/memory-consumption-of-java-data-types-2/ 如何在Java中分配超过-Xmx限制的内存 http://i ...

  2. 《深入理解Android内核设计思想》已陆续在全国各大书店及网上书店上市,感谢大家一直以来的支持~~

    <深入理解Android内核设计思想>已陆续在全国各大书店上市,电子书店也在陆续上架中(不断添加): 1. China-Pub 2. 京东 3. s=books&ie=UTF8&a ...

  3. (转)Kangle配置文件

    kangle配置文件 (重定向自Kangle配置文件) 目录 [隐藏] 1配置文件介绍 2重新加载配置文件 3config 3.1request和response(配置访问控制) 3.2listen( ...

  4. Ubuntu搭建solr搜索服务器

    参考:http://blog.csdn.net/makang110/article/details/50971705 一:搭建solr服务器 1:安装jdk1.7,并配置环境变量 2:下载tomcat ...

  5. There are 2 missing blocks. The following files may be corrupted

    There are 2 missing blocks. The following files may be corrupted: 步骤1,检查文件缺失情况 可以看到, blk_1074785806 ...

  6. PAT Radix[二分][进制转换][难]

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  7. oracle怎么恢复被覆盖的存储过程

    在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...

  8. 禁止F12与右键

    实践项目的代码哦,给大家分享下,如何屏蔽右键与F12. 应用网站  www.empiretreasure.vip   与       www.MineBook.vip.可以去逛逛哦. 不多说了,上代码 ...

  9. redis windows版本下载

    https://github.com/dmajkic/redis/downloads http://windows.php.net/downloads/pecl/snaps/redis/3.1.4rc ...

  10. js dom 操作技巧

    1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...