【BZOJ3944】 Sum
Description
Input
Output
Sample Input
1
2
8
13
30
2333
Sample Output
2 0
22 -2
58 -3
278 -3
1655470 2
Solution
裸的杜教筛。具体的内容详见洲阁2015年的集训队论文。这里我就大致口胡一下,求一个积性函数的前缀和可以把它狄利克雷卷积上另一个函数,同时卷上的这个函数和卷完得到的这个函数如果都很好求前缀和的话,那么就可以用杜教筛来求。phi函数大致的推导如下:

Code
#include <cstdio>
#include <map> #define R register
#define maxn 2000010
typedef long long ll;
int phi[maxn], miu[maxn], pr[maxn / ], prcnt;
ll sph[maxn], smi[maxn];
bool vis[maxn];
const int moha = ;
struct Hash {
Hash *next;
int ps; ll ans;
} *last1[moha], *last2[moha], mem[moha], *tot = mem;
inline ll S1(R int n)
{
if (n < maxn) return sph[n];
for (R Hash *iter = last1[n % moha]; iter; iter = iter -> next)
if (iter -> ps == n) return iter -> ans; R ll ret = 1ll * n * (n + 1ll) / ;
for (R ll i = , j; i <= n; i = j + )
{
j = n / (n / i);
ret -= S1(n / i) * (j - i + );
}
*++tot = (Hash) {last1[n % moha], n, ret}; last1[n % moha] = tot;
return ret;
}
inline ll S2(R int n)
{
if (n < maxn) return smi[n];
for (R Hash *iter = last2[n % moha]; iter; iter = iter -> next)
if (iter -> ps == n) return iter -> ans; R ll ret = ;
for (R ll i = , j; i <= n; i = j + )
{
j = n / (n / i);
ret -= (j - i + ) * S2(n / i);
}
*++tot = (Hash) {last2[n % moha], n, ret}; last2[n % moha] = tot;
return ret;
}
int main()
{
R int T; scanf("%d", &T);
phi[] = sph[] = ;
miu[] = smi[] = ;
for (R int i = ; i < maxn; ++i)
{
if (!vis[i]) pr[++prcnt] = i, phi[i] = i - , miu[i] = -;
sph[i] = sph[i - ] + phi[i];
smi[i] = smi[i - ] + miu[i];
for (R int j = ; j <= prcnt && 1ll * i * pr[j] < maxn; ++j)
{
vis[i * pr[j]] = ;
if (i % pr[j])
{
phi[i * pr[j]] = phi[i] * (pr[j] - );
miu[i * pr[j]] = -miu[i];
}
else
{
phi[i * pr[j]] = phi[i] * pr[j];
miu[i * pr[j]] = ;
break;
}
}
}
for (; T; --T)
{
R int N; scanf("%d", &N);
// printf("%d\n", N);
printf("%lld %lld\n", S1(N), S2(N));
}
return ;
}
/*
6
1
2
8
13
30
2333
*/
【BZOJ3944】 Sum的更多相关文章
- 【BZOJ3944】Sum(杜教筛)
[BZOJ3944]Sum(杜教筛) 题面 求\[\sum_{i=1}^n\mu(i)和\sum_{i=1}^n\phi(i)\] 范围:\(n<2^{31}\) 令\[S(n)=\sum_{i ...
- 【BZOJ3944】Sum
题面 Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1, ...
- 【CF914G】Sum the Fibonacci 快速??变换模板
[CF914G]Sum the Fibonacci 题解:给你一个长度为n的数组s.定义五元组(a,b,c,d,e)是合法的当且仅当: 1. $1\le a,b,c,d,e\le n$2. $(s_a ...
- 【BZOJ4262】Sum 单调栈+线段树
[BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- 【loj6059】Sum
Portal --> loj6059 Solution 看过去第一反应是..大力数位dp!然后看了一眼数据范围... 但是这没有什么关系!注意到我们不需要考虑前导零了,可以直接快乐dp ...
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...
随机推荐
- SSM框架中表单提交出现400错误
在jsp页面中: 在controller里传递参数: 由于参数sonTime时String类型,要把String类型的时间转换成Date类型的时间,在过程无法自动完成转换故出现400错误,可以使用@D ...
- jsp文件
新建的jsp文件比新建的html文件多了一行内容<%@ page contentType="text/html; charset=utf-8" %>,这样不会显示乱码 ...
- vue : 无法加载文件 C:\Users\lihongjie\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 htt ps:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1 + vue init webpack vue_p
以管理员方式打开powershell 运行命令:set-ExecutionPolicy RemoteSigned 出现: 执行策略更改执行策略可帮助你防止执行不信任的脚本.更改执行策略可能会产生安全风 ...
- cmd_操作MySQL数据库
建议用 TXT 文档来写代码 然后粘贴至cmd命令直接运行创建students库,表名为student,表包含以下字段: id(主键) name(姓名) age(年龄) sex(性别) sc(综合积分 ...
- JavaEE--JavaWeb三大组件Servlet、Filter、Listener
Servlet.Filter.Listener是JavaEE Web服务规定的服务器动态组件,由开发者编写由Web容器创建,并保证单例以及线程安全性,其中加载顺序为Linstener -> Fl ...
- sql server join联结
join学习起来有点乱,现做如下整理: table A id abc 1 a 2 b 3 c 4 d table B id abc 1 e 2 a 3 f 4 c --join或者inner join ...
- 首个springboot项目总结
项目说明:采用分布式架构的基于restfulApi的后端微服务api项目. 用到的技术栈:springboot.mybatis.swagger.eureka注册中心.gateway.awss3.doc ...
- JS实现table表格在鼠标移动出现一行变色或者十字叉变色
1,一行变色 <script> function trBg(){ var tab=document.getElementById("table"); var tr=ta ...
- php函数之substr()
问题: 希望从字符串的某个特定位置开始抽取这个字符串的一部分.例如,对于输入到一个表单的用户名,想要得到这个用户名的前8个字符. 解决: 使用substr()选择子串 $substring = sub ...
- Nginx实用整理
1. nginx 简述 1.1Nginx是轻量级高并发HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:Nginx可以作为一个HTTP服务器进行网站的发布处理,另外N ...