Sum BZOJ 3944
Sum
【问题描述】
给定一个正整数 N ( N <= 231 - 1 )
求:

【输入格式】
【输出格式】
6
1
2
8
13
30
2333
【样例输出】
1 1
2 0
22 -2
58 -3
278 -3
1655470 2
题解:
首先推一波式子

上式就是杜教筛的原理

:


:


对于
的求解方式在上面已经给出了
那么求出前
项答案并记忆化状态,就能达到
的时间复杂度
由于
,那么我们求的每一项的参数都是
形式的
对于
小于等于
的答案我们已经预处理出了
所以我们只需要记忆大于
的答案
首先提出一个命题:对于
,
都不相同
证明:
假设
,且
那么
mi , mj 表示两者的余数,它们的差为
因为
,所以
那么
,而
,假设不成立
所以不存在
,使得
证毕
所以在
时,
成立
那么
,我们就能直接使用数组存,对于每一个参数,我们将其除k的结果作为下标储存答案
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxm = 2e6 + ;
const int maxn = 1e4 + ;
int n;
int pri[maxm];
bool vis[maxm];
struct couple
{
long long miu, phi;
};
couple ans[maxn], ori[maxm];
inline void Scan(int &x)
{
char c;
bool o = false;
while(!isdigit(c = getchar())) o = (c != '-') ? o : true;
x = c - '';
while(isdigit(c = getchar())) x = x * + c - '';
if(o) x = -x;
}
int m;
inline void Sieve()
{
int tot = ;
m = maxm - ;
ori[] = (couple) {, };
for(int i = ; i <= m; ++i)
{
if(!vis[i])
{
pri[++tot] = i;
ori[i] = (couple) {-, i - };
}
for(int j = ; j <= tot; ++j)
{
int k = pri[j];
long long s = (long long) i * k;
if(s > m) break;
vis[s] = true;
if(!(i % k))
{
ori[s].miu = ;
ori[s].phi = ori[i].phi * k;
break;
}
else
{
ori[s].miu = -ori[i].miu;
ori[s].phi = ori[i].phi * ori[k].phi;
}
}
}
for(int i = ; i <= m; ++i)
{
ori[i].miu += ori[i - ].miu;
ori[i].phi += ori[i - ].phi;
}
}
couple Solve(int x)
{
if(x <= m) return ori[x];
int e = n / x;
if(vis[e]) return ans[e];
int last;
couple c, s;
s.miu = , s.phi = x * ((long long) x + ) >> ;
for(long long i = ; i <= x; i = (long long) last + )
{
last = x / (x / i);
c = Solve(x / i);
s.miu -= c.miu * (long long) (last - i + ), s.phi -= c.phi * (long long) (last - i + );
}
vis[e] = true, ans[e] = s;
return s;
}
int main()
{
int T;
Scan(T);
Sieve();
while(T--)
{
Scan(n);
memset(vis, false, sizeof(vis));
if(n <= m) printf("%lld %lld\n", ori[n].phi, ori[n].miu);
else
{
couple answer = Solve(n);
printf("%lld %lld\n", answer.phi, answer.miu);
}
}
Sum BZOJ 3944的更多相关文章
- ●杜教筛入门(BZOJ 3944 Sum)
入门杜教筛啦. http://blog.csdn.net/skywalkert/article/details/50500009(好文!) 可以在$O(N^{\frac{2}{3}})或O(N^{\f ...
- BZOJ 3944: Sum [杜教筛]
3944: Sum 贴模板 总结见学习笔记(现在还没写23333) #include <iostream> #include <cstdio> #include <cst ...
- bzoj 3944: Sum(杜教筛)
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4930 Solved: 1313[Submit][Status][Discuss ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- BZOJ.3944.Sum(Min_25筛)
BZOJ 洛谷 不得不再次吐槽洛谷数据好水(连\(n=0,2^{31}-1\)都没有). \(Description\) 给定\(n\),分别求\[\sum_{i=1}^n\varphi(i),\qu ...
- bzoj 3944 Sum —— 杜教筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3944 杜教筛入门题! 看博客:https://www.cnblogs.com/zjp-sha ...
- BZOJ 3944 Sum 解题报告
我们考虑令: \[F_n = \sum_{d|n}\varphi(d)\] 那么,有: \[\sum_{i=1}^{n}F_i = \sum_{i=1}^{n}\sum_{d|i}\varphi(d) ...
- 【刷题】BZOJ 3944 Sum
Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1,ans ...
- 「bzoj 3944: Sum」
题目 杜教筛板子了 #include<iostream> #include<cstring> #include<cstdio> #include<cmath& ...
随机推荐
- VueJS坎坷之路111---_self.$scopedSlots.default is not a function
VueJs + Element 话不多说,直接贴错: _self.$scopedSlots.default is not a function <el-table stripe border r ...
- 为什么要在函数内部声明 var that = this 呢
看一个例子 $('#conten').click(function(){ //this是被点击的#conten var that =this; $('.conten').each(function() ...
- NOIP模拟赛 麻将
[题目描述] 众所周知,麻将是我们国家的国粹.这段时间,小D也迷上了麻将这个老少皆宜的游戏. 小D觉得这些不同规则的麻将太麻烦了,所以他集合了很多种麻将规则创造出了一套D麻将.下面是D麻将的几个特点: ...
- Unbuntu18.04如何备份
以后可能用的到:https://blog.csdn.net/qq_35523593/article/details/78545530
- phpExcel使用方法二
require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new PHPExcel(); ...
- 【android】安卓开发apk列表
- 谷歌的Zxing框架的扫码软件 (目前国内的应用商店很少此种类型的扫码app) - 解析IP地址功能,从IP地址(子网掩码)自动解析出网段,广播地址
- apply 与 lambda
Python中的lambda和apply用法 https://blog.csdn.net/anshuai_aw1/article/details/82347016
- 带权并查集:HDU3172-Virtual Friends
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- MySQL使用yum安装
1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...