51nod - 1586 - 约数和 - 打表 - 思维
https://www.51nod.com/Challenge/Problem.html#problemId=1586
一眼看过去居然一点思路都没有的,一言不合就打表,打贡献表。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n = 25;
int a[200];
void update_b(int id) {
for(int i = 1; i <= id; ++i) {
if(id % i == 0) {
a[i]++;
}
}
}
void show_c(int id) {
memset(a, 0, sizeof(a));
for(int i = 1; i <= id; ++i) {
if(id % i == 0) {
update_b(i);
}
}
printf("c[%d]=\n ", id);
for(int i = 1; i <= n; ++i) {
printf("%d%c", a[i], " \n"[i == n]);
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
for(int i = 1; i <= n; ++i) {
show_c(i);
}
return 0;
}
好像a[i]位置对c[j]的贡献就是d(j/i)的样子,所以就预处理出d表就完事了?
数字不大,d表有nlogn的出法。但是暴力分解因子果断T,预处理所有数的因子也是T。原因在于因子是根号级别的,但是正解是直接更新a的倍数,是log级别的。
考虑a本身,它对应的另一个因子是1,2a对应的因子是2……这样可以用一个循环直接更新。期望复杂度是O(nlogn)的,而分解质因子暴力的期望复杂度则是O(n*n^{3/4})。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6;
int d[MAXN + 5], a[MAXN + 5];
int p[MAXN + 5], ptop;
bool np[MAXN + 5];
void sieve(int n = MAXN) {
np[1] = d[1] = 1;
for(int i = 2; i <= n; ++i) {
if(!np[i])
p[++ptop] = i, d[i] = 2, a[i] = 1;
for(int j = 1, t; j <= ptop && (t = i * p[j]) <= n; ++j) {
np[t] = 1;
if(i % p[j])
d[t] = d[i] * d[p[j]], a[t] = 1;
else {
d[t] = d[i] / (a[i] + 1) * (a[i] + 2);
a[t] = a[i] + 1;
break;
}
}
}
}
const int BufferSize = 1 << 16;
char buffer[BufferSize], *head, *tail;
inline char Getchar() {
if(head == tail) {
int l = fread(buffer, 1, BufferSize, stdin);
tail = (head = buffer) + l;
}
return *head++;
}
inline int read() {
int x = 0;
char c = Getchar();
for(; c < '0' || c > '9'; c = Getchar())
;
for(; c >= '0' && c <= '9'; c = Getchar())
x = (x << 3) + (x << 1) + c - '0';
return x ;
}
inline void _write(ll x) {
if(x > 9)
_write(x / 10);
putchar(x % 10 + '0');
}
inline void write(ll x) {
_write(x);
putchar('\n');
}
ll c[MAXN + 5] = {};
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
sieve();
int n = read(), q = read();
for(int i = 1; i <= q; ++i) {
int op = read();
if(op == 1) {
int x = read();
int y = read();
for(int j = x, k = 1; j <= n; j += x, ++k)
c[j] += (ll)d[k] * y;
} else {
int x = read();
write(c[x]);
}
}
return 0;
}
文件快读是真的快啊。
51nod - 1586 - 约数和 - 打表 - 思维的更多相关文章
- [多校联考2019(Round 4 T2)][51nod 1288]汽油补给(ST表+单调栈)
[51nod 1288]汽油补给(ST表+单调栈) 题面 有(N+1)个城市,0是起点N是终点,开车从0 -> 1 - > 2...... -> N,车每走1个单位距离消耗1个单位的 ...
- 51NOD 1220 约数之和 [杜教筛]
1220 约数之和 题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_1(ij)\) \[ \sigma_0(ij) = \sum_{x\mid i}\sum_{y\mi ...
- Codeforces 914 C 数位DP+暴力打表+思维
题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...
- 51nod 1218 最长递增子序列 | 思维题
51nod 1218 最长递增子序列 题面 给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中. 题解 YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它 ...
- [51Nod 1220] - 约数之和 (杜教筛)
题面 令d(n)d(n)d(n)表示nnn的约数之和求 ∑i=1n∑j=1nd(ij)\large\sum_{i=1}^n\sum_{j=1}^nd(ij)i=1∑nj=1∑nd(ij) 题目分析 ...
- 51nod 1674 区间的价值V2(思维+拆位+尺取法)
最近被四区题暴虐... 题意:lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值 ...
- 51Nod 1305 Pairwise Sum and Divide | 思维 数学
Output 输出fun(A)的计算结果. Input示例 3 1 4 1 Output示例 4 first try: #include "bits/stdc++.h" using ...
- 51nod 1220 约数之和【莫比乌斯反演+杜教筛】
首先由这样一个式子:\( d(ij)=\sum_{p|i}\sum_{q|j}[gcd(p,q)==1]\frac{pj}{q} \)大概感性证明一下吧我不会证 然后开始推: \[ \sum_{i=1 ...
- 51nod1586 约数和
果然我自己写的读入优化naive!...换题目给的读入优化就A了...话说用visual交快了好多啊... const int BufferSize=1<<16; char buffer[ ...
随机推荐
- nginx图片过滤处理模块http_image_filter_module
nginx图片过滤处理模块http_image_filter_module安装配置笔记 http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- Spring Boot关于layui的通用返回类
1.关于layui的通用返回类 code.count.data.msg public class Msg { private long code = 0; private long count = 0 ...
- springboot自定义错误页
静态错误页放在 动态可以放在freemaker或者thymeleaf 匹配规则: 先找动态页面再找静态页面 先找精确错误页面再找模糊页面 注:精确错误页面=50 ...
- 安卓手机通过有线连接PC上网
因手机wifi坏了,速度上限为2.5m/s.无法发挥出100m带宽的威力. 这里探索各大神的指导,记录下其中一种方式. :链接 主题:使用openvpn和手机的USB共享网络 通过mico ...
- 接口返回buffer的16进制数据如何转换
我们请求接口数据经常会看到buffer数据,这是我们可以使用data.toString()就可以啦~
- Linux内核设计与实现 总结笔记(第十三章)虚拟文件系统
一.通用文件系统接口 Linux通过虚拟文件系统,使得用户可以直接使用open().read().write()访问文件系统,这种协作性和泛型存取成为可能. 不管文件系统是什么,也不管文件系统位于何种 ...
- SQL语句之-简单查询
SQL 语句的语法顺序是: SELECT[DISTINCT] FROM WHERE GROUP BY HAVING UNION ORDER BY 一.查询SELECT 1.查询全部列:SELEC ...
- [CSP-S模拟测试]:排列组合(数学 or 找规律)
题目描述 $T$组数据,每次给定$n$,请求出下式的值,对$10^9+7$取模: $$C_n^0\times C_n^0+C_n^1\times C_n^1+C_n^2\times C_n^2+... ...
- 模拟vue实现简单的webpack打包
一.安装nodejs,查看是否安装成功 二.package.json项目初始化 npm init 电脑有node环境,在根目录下运行命令npm init初始化项目,根据提示输入项目相关信息,然后运行. ...