Luogu 2671 求和 NOIP2015T3
题解
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$个)
- #include <bits/stdc++.h>
- #define ll long long
- #define inf 0x3f3f3f3f
- #define il inline
- namespace io {
- #define in(a) a=read()
- #define out(a) write(a)
- #define outn(a) out(a),putchar('\n')
- #define I_int int
- inline I_int read() {
- I_int x = , f = ; char c = getchar() ;
- while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
- while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
- return x * f ;
- }
- char F[ ] ;
- inline void write( I_int x ) {
- I_int tmp = x > ? x : -x ;
- if( x < ) putchar( '-' ) ;
- int cnt = ;
- while( tmp > ) {
- F[ cnt ++ ] = tmp % + '' ;
- tmp /= ;
- }
- while( cnt > ) putchar( F[ -- cnt ] ) ;
- }
- #undef I_int
- }
- using namespace io ;
- using namespace std ;
- #define N 100010
- const int mod = ;
- int n = read() , m = read() ;
- struct node {
- int col , val , id ;
- } a[ N ] ;
- bool cmp( node a , node b ) {
- return a.col < b.col ;
- }
- int main() {
- for( int i = ; i <= n ; i ++ ) a[ i ].val = read() , a[ i ].id = i ;
- for( int i = ; i <= n ; i ++ ) a[ i ].col = read() ;
- sort( a + , a + n + , cmp ) ;
- int cur = ;
- ll ans = ;
- for( int i = ; i <= n ; i ++ ) {
- ll sum = ;
- for( cur = i + ; a[ i ].col == a[ cur ].col && cur <= n ; cur ++ ) {
- if( ( a[ i ].id + a[ cur ].id ) % == )
- sum = ( sum + ( 1ll * ( a[ i ].id + a[ cur ].id ) % mod * 1ll * ( a[ i ].val + a[ cur ].val ) % mod ) % mod ) % mod ;
- }
- ans = ( ans + sum ) % mod ;
- }
- printf( "%lld\n" , ( ans + mod ) % mod ) ;
- return ;
- }
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)$
- #include <bits/stdc++.h>
- #define ll long long
- #define inf 0x3f3f3f3f
- #define il inline
- #define int long long
- namespace io {
- #define in(a) a=read()
- #define out(a) write(a)
- #define outn(a) out(a),putchar('\n')
- #define I_int int
- inline I_int read() {
- I_int x = , f = ; char c = getchar() ;
- while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
- while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
- return x * f ;
- }
- char F[ ] ;
- inline void write( I_int x ) {
- if( x == ) { putchar( '' ) ; return ; }
- I_int tmp = x > ? x : -x ;
- if( x < ) putchar( '-' ) ;
- int cnt = ;
- while( tmp > ) {
- F[ cnt ++ ] = tmp % + '' ;
- tmp /= ;
- }
- while( cnt > ) putchar( F[ -- cnt ] ) ;
- }
- #undef I_int
- }
- using namespace io ;
- using namespace std ;
- #define N 100010
- const int mod = ;
- int n , m ;
- struct node {
- int col , val ;
- } a[ N ] ;
- int cnt[ N ][ ] , sum[ N ][ ] ;
- signed main() {
- n = read() , m = read() ;
- for( int i = ; i <= n ; i ++ ) a[ i ].val = read() ;
- for( int i = ; i <= n ; i ++ ) a[ i ].col = read() ;
- for( int i = ; i <= n ; i ++ ) {
- cnt[ a[ i ].col ][ i& ] ++ ;
- sum[ a[ i ].col ][ i& ] = ( sum[ a[ i ].col ][ i& ] + a[ i ].val ) % mod ;
- }
- ll ans = ;
- for( int i = ; i <= n ; i ++ ) {
- ll num = cnt[ a[ i ].col ][ i& ] , s = sum[ a[ i ].col ][ i& ] ;
- ans = ( ans + ( i*(s-a[i].val) + (num-)*a[i].val*i ) % mod ) % mod ;
- }
- printf( "%lld\n" , ans % mod ) ;
- return ;
- }
Luogu 2671 求和 NOIP2015T3的更多相关文章
- luogu 4427 求和
bjoi 2018 求和 唯一一道可能切的题一个数组还没开long long就成0分了 题目大意: 一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k次方和,而且每次的k可能是不同的 此处 ...
- [洛谷2671]求和<前缀和&模拟>
题目链接:https://www.luogu.org/problemnew/show/P2671 这是noip2015普及组的第三题,谁说的普及组的题就一定水的不行,这道题就比较有意思的 这道题的暴力 ...
- Luogu P1625 求和
题意 给定两个整数 \(n,m\),求 \[\sum\limits_{i=1}^{n}\frac{1}{\prod\limits_{j=i}^{i+m-1}j} \] \(\texttt{Data R ...
- luogu P1630 求和(枚举暴力)
题意 题解 可以发现当a=10001时, 和1是等价的. 所以这题就水了. #include<iostream> #include<cstring> #include<c ...
- [Luogu] 余数求和
question: $$\sum_{i=1}^{n} k \bmod i$$$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$$$\sum_{i= ...
- [Luogu 2261] CQOI2007 余数求和
[Luogu 2261] CQOI2007 余数求和 这一定是我迄今为止见过最短小精悍的省选题了,核心代码 \(4\) 行,总代码 \(12\) 行,堪比小凯的疑惑啊. 这题一看暴力很好打,然而 \( ...
- BZOJ 4555 Luogu P4091 [HEOI2016/TJOI2016]求和 (第二类斯特林数)
题目链接 (luogu) https://www.luogu.org/problem/P4091 (bzoj) https://www.lydsy.com/JudgeOnline/problem.ph ...
- [Luogu P2261] [CQOI2007]余数求和 (取模计算)
题面 传送门:https://www.luogu.org/problemnew/show/P2261 Solution 这题显然有一个O(n)的直接计算法,60分到手. 接下来我们就可以拿出草稿纸推一 ...
- 【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 ...
随机推荐
- Memory consumption of popular Java data types
http://java-performance.info/memory-consumption-of-java-data-types-2/ 如何在Java中分配超过-Xmx限制的内存 http://i ...
- 《深入理解Android内核设计思想》已陆续在全国各大书店及网上书店上市,感谢大家一直以来的支持~~
<深入理解Android内核设计思想>已陆续在全国各大书店上市,电子书店也在陆续上架中(不断添加): 1. China-Pub 2. 京东 3. s=books&ie=UTF8&a ...
- (转)Kangle配置文件
kangle配置文件 (重定向自Kangle配置文件) 目录 [隐藏] 1配置文件介绍 2重新加载配置文件 3config 3.1request和response(配置访问控制) 3.2listen( ...
- Ubuntu搭建solr搜索服务器
参考:http://blog.csdn.net/makang110/article/details/50971705 一:搭建solr服务器 1:安装jdk1.7,并配置环境变量 2:下载tomcat ...
- 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 ...
- PAT Radix[二分][进制转换][难]
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- oracle怎么恢复被覆盖的存储过程
在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...
- 禁止F12与右键
实践项目的代码哦,给大家分享下,如何屏蔽右键与F12. 应用网站 www.empiretreasure.vip 与 www.MineBook.vip.可以去逛逛哦. 不多说了,上代码 ...
- redis windows版本下载
https://github.com/dmajkic/redis/downloads http://windows.php.net/downloads/pecl/snaps/redis/3.1.4rc ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...