Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

【Problem Description】
初始\([1,500000]\)都为0,后续有两种操作:
\(1\)、将\(a[x]\)的值加上\(y\)。
\(2\)、求所有满足\(i\ mod\ x=y\)的\(a[i]\)的和。
【Solution】
具体做法就是,对于前\(\sqrt{500000}=708\)个数,定义\(dp[j][k]\)表示所有满足\(i\ mod\ j=k\)的\(a[i]\)的和。每次进行\(1\)操作的时候,\(O(\sqrt{500000})\)预处理一下。查询时可\(O(1)\)查询。
对于大于\(O(\sqrt{500000})\)的数,可暴力求解:\(i\ mod\ x=y\Leftrightarrow i+x\cdot t=y\)。所以只需要枚举\(\frac{500000}{x}\)次即可。而\(x\)不小于\(\sqrt{500000})=708\),所以枚举次数不大于\(708\)次,所以总复杂度为\(O(500000^{\frac{3}{2}})\)。
【Code】
/*
* @Author: Simon
* @Date: 2019-08-28 14:34:47
* @Last Modified by: Simon
* @Last Modified time: 2019-08-28 15:02:25
*/
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1005
#define maxm 500005
int dp[maxn][maxn]/*下标满足模i余数为j的 值的和*/,a[maxm];
int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int q,m=ceil(sqrt(maxm));cin>>q;
while(q--){
int p,x,y;cin>>p>>x>>y;
if(p==1){
a[x]+=y;
for(int i=1;i<=m;i++) dp[i][x%i]+=y;
}
else{
if(x<=m) cout<<dp[x][y]<<endl;
else{
int ans=0;
for(int i=y;i<maxm;i+=x) ans+=a[i];
cout<<ans<<endl;
}
}
}
#ifndef ONLINE_JUDGE
cout<<endl;system("pause");
#endif
return 0;
}
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块的更多相关文章
- Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题
Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] 总共两次询 ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 71 (Rated for Div. 2)E. XOR Guessing
一道容斥题 如果直接做就是找到所有出现过递减的不同排列,当时硬钢到自闭,然后在凯妹毁人不倦的教导下想到可以容斥做,就是:所有的排列设为a,只考虑第一个非递减设为b,第二个非递减设为c+两个都非递减的情 ...
- Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)
E. XOR Guessing time limit per test1 second memory limit per test256 megabytes inputstandard input o ...
- [暴力] Educational Codeforces Round 71 (Rated for Div. 2) B. Square Filling (1207B)
题目:http://codeforces.com/contest/1207/problem/B B. Square Filling time limit per test 1 second mem ...
- [贪心,dp] Educational Codeforces Round 71 (Rated for Div. 2) C. Gas Pipeline (1207C)
题目:http://codeforces.com/contest/1207/problem/C C. Gas Pipeline time limit per test 2 seconds memo ...
- Educational Codeforces Round 71 (Rated for Div. 2)
传送门 A.There Are Two Types Of Burgers 签到. B.Square Filling 签到 C.Gas Pipeline 每个位置只有"高.低"两种状 ...
- Educational Codeforces Round 71 (Rated for Div. 2) Solution
A. There Are Two Types Of Burgers 题意: 给一些面包,鸡肉,牛肉,你可以做成鸡肉汉堡或者牛肉汉堡并卖掉 一个鸡肉汉堡需要两个面包和一个鸡肉,牛肉汉堡需要两个面包和一个 ...
- Remainder Problem(分块) Educational Codeforces Round 71 (Rated for Div. 2)
引用:https://blog.csdn.net/qq_41879343/article/details/100565031 下面代码写错了,注意要上面这种.查:2 800 0,下面代码就错了. ...
随机推荐
- LODOP打印超文本有边距不居中的情况2
之前的博文:LODOP打印项水平居中.之前的博文有介绍超文本和纯文本的居中方式,设置超文本打印项居中时,注意打印内容要在打印项本身宽度里居中.之前的博文超文本用的是个表格,而且表格本身没有margin ...
- 说一说ORACLE的监听
1.讲在前面 本文档仅适用于如下范围:Oracle以dedicate (专有)连接模式通过TCP/IP协议连接的场景. 2.监听的作用 在谈监听的作用之前,有必要先看看监听的工作原理图: 客户端进程发 ...
- Maven中解决jar包冲突的三种方式
首先我们在idea中创建一个maven工程,我们只关注pom.xml以及External Libraries中导入的jar包 导入spring-beans.jar <dependency> ...
- Mysql中的读锁,写锁,乐观锁及事务隔离级别和并发问题
mysql读锁,写锁,乐观锁 读锁,也叫共享锁(shared lock) SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE 写锁,也叫排他 ...
- js玩命加载……
在请求数据加载的过程中,经常需要显示请求等待,写了一个简单的请求等待—- html代码如下 <!--页面载入显示--> <div id="dataLoad" st ...
- jQuery的基础总结
**本篇只列出零碎的jQuery基础知识点,个人记录自己的学习进度,无序排列,谨慎查看.** 1.jQuery入口函数的四种写法2.jQuery与JS遍历数组的区别3.jQuery符号冲突问题4.jQ ...
- 自定义 Word 默认的 Normal.dotm 模板、更改 Word 默认字体、更改 Word 默认样式(16)
1. 引言 以Office 2016为例. 有没有遇见这样的问题: 每次新建一个 Word 空白文档打开后字体默认是等线,段落默认是单倍行距,默认标题也不是自己想要的样式,等一系列问题.每次打开都要调 ...
- Go语言(基本数据类型)
Go语言中有丰富的数据类型,除了基本的整型.浮点型.布尔型.字符串外,还有数组.切片.结构体.函数.map.通道(channel)等.Go 语言的基本类型和其他语言大同小异. 基本数据类型 整型 整型 ...
- hadoop 异常
2019-09-20 22:49:51,955 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Problem connecting to ...
- shiro源码解析
一.web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name>为啥与Spring文件中配置的ShiroFilterFactoryBean的Be ...