Description

Input

一共T+1行
第1行为数据组数T(T<=10)
第2~T+1行每行一个非负整数N,代表一组询问

Output

一共T行,每行两个用空格分隔的数ans1,ans2

Sample Input

6
1
2
8
13
30
2333

Sample Output

1 1
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的更多相关文章

  1. 【BZOJ3944】Sum(杜教筛)

    [BZOJ3944]Sum(杜教筛) 题面 求\[\sum_{i=1}^n\mu(i)和\sum_{i=1}^n\phi(i)\] 范围:\(n<2^{31}\) 令\[S(n)=\sum_{i ...

  2. 【BZOJ3944】Sum

    题面 Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1, ...

  3. 【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 ...

  4. 【BZOJ4262】Sum 单调栈+线段树

    [BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...

  5. 【POJ1707】【伯努利数】Sum of powers

    Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...

  6. 【loj6059】Sum

    Portal --> loj6059 Solution ​​ 看过去第一反应是..大力数位dp!然后看了一眼数据范围... ​ 但是这没有什么关系!注意到我们不需要考虑前导零了,可以直接快乐dp ...

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

  8. 【LeetCode】Sum of Two Integers

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  9. 【POJ2739】Sum of Consecutive Prime Numbers

    简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...

随机推荐

  1. Mybatis(三) 动态SQL

    if + where 用法 1. if 元素来实现多条件查询 1.1 UserMapper.xml配置文件 <!--查询用户列表 (if)--> <select id="g ...

  2. 有人向你扔了一个bug,哈哈哈哈

    有人向你扔了一个bug. "26楼会议室的灯亮着.它应该是熄灭着的." bug的备注里写道"你应该能在5分钟内搞定,只要按一下开关就好了."你去了26楼的会议室 ...

  3. 学习python基础规则

    前面应该是记流水账的方式,毕竟学习的内容不多无法产出什么有效的内容. 这两天从开始下载Python开始学习,一路顺畅冒的问题,直到开始学习python的游戏规则,严格缩进.注释及‘’的使用等感觉还不错 ...

  4. redis 学习(8)-- redis 客户端 -- Jedis

    redis 客户端 -- Jedis 1. Jedis 直连 本质是 TCP 连接. 执行流程 创建Jedis对象 通过Jedis执行命令 返回Jedis执行结果 关闭Jedis连接 demo 要使用 ...

  5. [Next] 服务端渲染知识补充

    渲染 渲染:就是将数据和模版组装成 html 客户端渲染 客户端渲染模式下,服务端把渲染的静态文件给到客户端,客户端拿到服务端发送过来的文件自己跑一遍 js,根据 JS 运行结果,生成相应 DOM,然 ...

  6. 导入excel-uploadify+npoi

    总结: 导入文件时一定要给database设置栏位 虚拟表的栏位名称可以与数据库表不一致,但顺序一定要一致,因为它是按照顺序依次插入的 sheet.FirstRowNum = 0; sheet.Fir ...

  7. php过滤微信昵称中的表情

    function filterNickname($nickname) { $nickname = preg_replace('/[\x{1F600}-\x{1F64F}]/u', '', $nickn ...

  8. openlayers之全屏控件的使用

    import { FullScreen } from 'ol/control' map.addControl(new FullScreen())

  9. 使用pagehelper分页工具page警告问题

    警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader java.lang. ...

  10. oracle重置dba用户密码

    1.进入sqlplus里面: [oracle@master ~]$ sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Tue ...