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-技巧分块的更多相关文章

  1. Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题

    Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] ​ 总共两次询 ...

  2. 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 ...

  3. Educational Codeforces Round 71 (Rated for Div. 2)E. XOR Guessing

    一道容斥题 如果直接做就是找到所有出现过递减的不同排列,当时硬钢到自闭,然后在凯妹毁人不倦的教导下想到可以容斥做,就是:所有的排列设为a,只考虑第一个非递减设为b,第二个非递减设为c+两个都非递减的情 ...

  4. 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 ...

  5. [暴力] 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 ...

  6. [贪心,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 ...

  7. Educational Codeforces Round 71 (Rated for Div. 2)

    传送门 A.There Are Two Types Of Burgers 签到. B.Square Filling 签到 C.Gas Pipeline 每个位置只有"高.低"两种状 ...

  8. Educational Codeforces Round 71 (Rated for Div. 2) Solution

    A. There Are Two Types Of Burgers 题意: 给一些面包,鸡肉,牛肉,你可以做成鸡肉汉堡或者牛肉汉堡并卖掉 一个鸡肉汉堡需要两个面包和一个鸡肉,牛肉汉堡需要两个面包和一个 ...

  9. Remainder Problem(分块) Educational Codeforces Round 71 (Rated for Div. 2)

    引用:https://blog.csdn.net/qq_41879343/article/details/100565031 下面代码写错了,注意要上面这种.查:2  800  0,下面代码就错了. ...

随机推荐

  1. mybatis:字符串转成数组拼接成SQL

    <foreach item="item" index="index" collection="str.split(',')" open ...

  2. linux awk的用法

    linux awk的用法 <pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb 12 42 30 b 03 43 25 a 08 10 16 ...

  3. spring跨重定向传递数据

    spring跨重定向传递数据 为何要重定向? 作用之一:防止表单重复提交 如何重定向? // 在控制器方法返回的视图名称中,以redirect:开头的String不是用来查找视图的,而是用来指导浏览器 ...

  4. LeetCode 788. 旋转数字(Rotated Digits) 36

    788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...

  5. Netty--索引

    Netty 入门示例 Netty原理架构解析 Netty 基本原理 Netty面试题 阿里的Netty知识点你又了解多少

  6. C++ 读取 MATLAB 的 .m 文件,并发送到 MATLAB 运行

    本代码是由「Microsoft Visual Studio 2015 Enterprise」编写. 想要了解更多 C++ 与 MATLAB 混合编程的知识,可以参考我的另一篇博客:C++ 与 MATL ...

  7. Linux基础系统优化(二)

    SELinux功能 SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,这个功能管理员又爱又恨,大多数生产环境也是关闭的做法,安全手段使 ...

  8. 14 IO流(十一)——装换流InputStreamReader与OutputStreamWriter

    什么是转换流 首先,这里的转换流指的是InputstreamReader与OutputStreamWriter. 正如它们的名字,它的作用是将字节流转换为字符流. 为什么要转换为字符流呢?因为对于获取 ...

  9. Linux 环境安装 Node、nginx、docker、vsftpd、gitlab

    Linux 环境安装 centos7 # 更新yum yum update -y 0. 防火墙 firewalld 新入的JD云服务器,发现防火墙默认是关闭的. # 查看防火墙状态 systemctl ...

  10. GC收集器

    新生代收集器 Serial New 单线程收集器,工作时必须暂停其他线程: 简单高效,没有线程交互开销: 基于复制算法: Parallel New 对Serial的改进,多线程: CPU数量<4 ...