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,下面代码就错了. ...
随机推荐
- knuth洗牌算法
首先来思考一个问题: 设计一个公平的洗牌算法 1. 看问题,洗牌,显然是一个随机算法了.随机算法还不简单?随机呗.把所有牌放到一个数组中,每次取两张牌交换位置,随机 k 次即可. 如果你的答案是这样, ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Python进阶之面向对象
新式类与旧式类 区别: 在2.2版本之前所有的类都是旧式类,3.x版本已取消旧式类 旧式类一般的写法,不继承任何父类 class Person: def __init__(self, name): s ...
- win10查看激活到期时间
我们知道Windows系统需要激活后才可以使用全部功能,那么你的Windows10激活了吗?如何查看激活时间呢?是不是永久激活的?带着这些问题,下面我们就一个一个逐一查看一下吧. 工具/原料 Wi ...
- docker笔记2--镜像容器基本使用
1 docker的安装 系统:centos7 (1)配置好yum (2)yum -y install docker (3)查看状态 systemctl status docker 2 docker镜像 ...
- 【LeetCode】无重复字符的最长子串【滑动窗口法】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- [转帖]Socat 入门教程
https://www.hi-linux.com/posts/61543.html 现在安装k8s 必须带 socat 今天看一下socat 到底是啥东西呢. Socat 是 Linux 下的一个多功 ...