Sum

【问题描述】

给定一个正整数 N ( N <= 231 - 1 )

求:

【输入格式】

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

【输出格式】

一共T行,每行两个用空格分隔的数ans1,ans2
【样例输入】

6
1
2
8
13
30
2333

【样例输出】

1 1
2 0
22 -2
58 -3
278 -3
1655470 2


题解:

首先推一波式子

上式就是杜教筛的原理

:

:

对于的求解方式在上面已经给出了

那么求出前项答案并记忆化状态,就能达到的时间复杂度

由于  ,那么我们求的每一项的参数都是  形式的

对于  小于等于的答案我们已经预处理出了

所以我们只需要记忆大于的答案

首先提出一个命题:对于  都不相同

证明:

假设 ,且

那么

mi , m表示两者的余数,它们的差为

因为,所以

那么,而,假设不成立

所以不存在,使得

证毕

所以在 时,成立

那么,我们就能直接使用数组存,对于每一个参数,我们将其除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的更多相关文章

  1. ●杜教筛入门(BZOJ 3944 Sum)

    入门杜教筛啦. http://blog.csdn.net/skywalkert/article/details/50500009(好文!) 可以在$O(N^{\frac{2}{3}})或O(N^{\f ...

  2. BZOJ 3944: Sum [杜教筛]

    3944: Sum 贴模板 总结见学习笔记(现在还没写23333) #include <iostream> #include <cstdio> #include <cst ...

  3. bzoj 3944: Sum(杜教筛)

    3944: Sum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4930  Solved: 1313[Submit][Status][Discuss ...

  4. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  5. BZOJ.3944.Sum(Min_25筛)

    BZOJ 洛谷 不得不再次吐槽洛谷数据好水(连\(n=0,2^{31}-1\)都没有). \(Description\) 给定\(n\),分别求\[\sum_{i=1}^n\varphi(i),\qu ...

  6. bzoj 3944 Sum —— 杜教筛

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3944 杜教筛入门题! 看博客:https://www.cnblogs.com/zjp-sha ...

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

  8. 【刷题】BZOJ 3944 Sum

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

  9. 「bzoj 3944: Sum」

    题目 杜教筛板子了 #include<iostream> #include<cstring> #include<cstdio> #include<cmath& ...

随机推荐

  1. VueX源码分析(2)

    VueX源码分析(2) 剩余内容 /module /plugins helpers.js store.js helpers要从底部开始分析比较好.也即先从辅助函数开始再分析那4个map函数mapSta ...

  2. Unity学习之路——C#相关

    1.C#数组数组定义 int[] number; float[] score; string[] names;动态初始化,借助new运算符为数组元素分配空间int[] Array = new int[ ...

  3. mongo 副本集+分片 配置

    服务器规划如下: 副本集名称|服务器IP 192.168.56.111 192.168.56.112 192.168.56.113 shard1 3201 3201 3201 shard2 3202 ...

  4. 批量ping IP并检测IP延迟率和丢包率脚本

    脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...

  5. html页面简单访问限制

    PS:突然发现博客园有密码保护功能,已经可以满足基本需求了.博客园还能备份自己的所有数据,做到了数据归用户所有,平台只是展示,真是良心网站,大赞. 想要通过一个站点放一些东西给一些人看,但是又不想让所 ...

  6. 数字内置方法详解(int/long/float/complex)

    一.常用方法 1.1.int 以下是Python2.7的int内置函数: 序号 函数名 作用 举例 1 int.bit_length() 二进制存储这个整数至少需要多少bit(位). >> ...

  7. 【mysql】mysql存储过程实例

    ```mysql DELIMITER $$   DROP PROCEDURE IF EXISTS `system_number_update` $$   CREATE DEFINER=`root`@` ...

  8. 最长公共子序列(LCS)问题

    最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序 ...

  9. Linux学习-Linux的账号与群组

    使用者识别码: UID 与 GID Linux 主机并不会直接认识 你的"帐号名称"的,他仅认识 ID 啊 (ID 就是一组号码啦). 由于计算机仅认识 0 与 1,所 以主机对于 ...

  10. HDU 1561 The more, The Better(树形背包)

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...