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& ...
随机推荐
- SDWebImage解析
SDWebImage托管在github上.https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下 ...
- CentOS7安装配置VSFTP
#是否开启匿名用户,匿名都不安全,不要开 anonymous_enable=NO #允许本机账号登录FTP local_enable=YES #允许账号都有写操作 write_enable=YES # ...
- 单例模式的几种实现-Java版
目录 关键点 饿汉式 懒汉式 双检锁 静态内部类单例模式 枚举方式 关键点 私有化构造器 通过静态方法或枚举返回单例类对象 确保单例类对象只有一个,尤其在多线程环境下. 确保每个类被序列化不会重新创建 ...
- laravel中使用PHPQuery实现网页采集
由于没有PHPQuery的composer包安装所以需要我们手动在我们的laravel项目中安装加载PHPQuery,这里需要设置laravel的autoload->class map. 1.首 ...
- 2.什么是composer与packgist,composer的安装
目录 学习地址: composer与packgist关系图片 composer的安装; 配置composer 修改国内镜像 用composer安装与卸载插件 composer插件升级后报错 学习地址: ...
- OverflowError:django signed integer is greater than maximum
在学习一对一查询的时候,打印作者的电话时报了这个错 alex = Author.objects.filter(name='alex').first() print(alex.authordetail. ...
- POJ:2109-Power of Cryptography(关于double的误差)
Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Description Current work in cryptograp ...
- 树状数组:CDOJ1583-曜酱的心意(树状数组心得)
曜酱的心意 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others) Description ...
- jmeter jdbc各字段的含义
JDBC采样器各选项的含义如下: 1.Variable Name 其中的Variable Name和上面JDBC Connection Configuration中的Variable Name相同,这 ...
- 对java多线程的一些浅浅的理解
作为一名JAVA初学者,前几天刚刚接触多线程这个东西,有了些微微的理解想写下来(不对的地方请多多包涵并指教哈). 多线程怎么写代码就不说了,一搜一大堆.说说多线程我认为最难搞的地方,就是来回释放锁以及 ...