[P5172] Sum
“类欧几里得算法”第一题 sum
【题意】
给入\(n,r\),求\(\sum_{d=1}^n(-1)^{\lfloor d\sqrt r \rfloor}\)。
【分析】
只需要考虑所有\(d\)中,\(\lfloor d\sqrt r\rfloor\)为偶数的个数。显然\(\lfloor x\rfloor\)为偶数\(\Leftrightarrow \lfloor x\rfloor=2\times\lfloor\frac{x}{2}\rfloor\)。 那么原式可以改写为:
=n-2\times\sum_{d=1}^n\lfloor d\sqrt r\rfloor+4\times\sum_{d=1}^n\lfloor\frac{d\sqrt r}{2}\rfloor
\]
不妨设\(f(a,b,c,n)=\sum_{d=1}^n\lfloor d\times\frac{a\sqrt r+b}{c}\rfloor\),那么原式即为
\]
考虑关于\(f(a,b,c,n)\)的算法,(开始扣题了),设\(k=\frac{a\sqrt r +b}{c}\)。
当\(k\ge1\)时,\(k\)可以拆为一个正数+小于1的非负实数,即
=\frac{n(n+1)}{2}\lfloor k\rfloor+\sum_{d=1}^n\lfloor d\times\frac{a\sqrt r+b-\lfloor k\rfloor\times c}{c}\rfloor\\
=\frac{n(n+1)}{2}\lfloor k\rfloor+f(a,b-\lfloor k\rfloor\times c,c,n)
\]
当\(k<1\)时,(比如上面递归的那层),可以看作是满足
0<x\le n\\
0<y\le x\times k\\
x\in Z\\
y\in Z
\end{cases}
\]
的点数。考虑再矩形\((1,1)(n,\lfloor n\times k\rfloor)\)内容斥可得个数为\(n\times\lfloor n\times k\rfloor\) 减去左上三角的部分,而那部分可当作是\(y=x\times k,\ x\in[1,n]\)的反函数\(y=x\times \dfrac{1}{k}, x\in[1,\lfloor n\times k\rfloor]\)。
其中\(\dfrac{1}{k}=\dfrac{c}{a\sqrt r+b}=\dfrac{c(a\sqrt r-b)}{(a\sqrt r+b)(a\sqrt r-b)}=\dfrac{ac\sqrt r-bc}{a^2r-b^2}\)。
\]
可以发现,情况一二交替递归,且每轮的总的变化与欧几里得算法相似,故知其复杂度为\(\log\)级的。
难度海星。。
然而当\(r\)为完全平方数时需要单独算,大概会爆LL吧。。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL n,r;
double x;
LL f(LL a,LL b,LL c,LL n) {
if(!n) return 0;
LL d=__gcd(__gcd(a,b),c);
a/=d, b/=d, c/=d;
double k=(a*x+b)/c;
if(k>=1) return n*(n+1)/2*(LL)(k)+f(a,b-(LL)(k)*c,c,n);
else return n*(LL)(n*k)-f(a*c,-b*c,a*a*r-b*b,(LL)(n*k));
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%lld%lld",&n,&r);
x=sqrt(r);
LL c=x;
if(c*c==r) {
if(c&1) printf("%lld\n",n-2*((n+1)/2));
else printf("%lld\n",n);
}
else printf("%lld\n",n-2*f(1,0,1,n)+4*f(1,0,2,n));
}
return 0;
}
[P5172] Sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- java中的 java.util.concurrent.locks.ReentrantLock类中的lockInterruptibly()方法介绍
在java的 java.util.concurrent.locks包中,ReentrantLock类实现了lock接口,lock接口用于加锁和解锁限制,加锁后必须释放锁,其他的线程才能进入到里面执行, ...
- sql中varchar(n),nvarchar(n) 长度性能及所占空间分析
sql中varchar(n),nvarchar(n) 长度性能及所占空间分析 1.varchar(n),nvarchar(n) 中的n怎么解释: nvarchar(n)最多能存n个字符,不区分中英文. ...
- csu1804
/* csu 1804 因为define和const总出错,到现在也不明白为什么 不知道define 和 const 出来的 mod 有什么区别 */ #include<iostream> ...
- spring深入学习(三)-----spring容器内幕
之前都是说了怎么配置bean以及用法之类的,这篇博文来介绍下spring容器内幕. 内部容器工作机制 Spring中AbstractApplicationContext抽象类的refresh()方法是 ...
- 【js jQuery】map集合 循环迭代取值---以及 map、json对象、list、array循环迭代的方法和区别
后台给前台传来一个map @ResponseBody @RequestMapping(value = "getSys") public Map<Long,String> ...
- 微信小程序的自定义插件
第一步,创建一个页面和普通页面一样 第二不,在这个页面上进行对json配置, "component":true 第三不在需要插入的页面中进行设置 插入标签 <dialog i ...
- MAC帧和PPP帧区别
- Redis sentinel之集群搭建
环境 由于不太熟悉docker,所以,把docker当虚拟机来用,服务器环境如下: Redis Server 环境搭建 Redis Server 01 搭建 并且制作Redis镜像 容器建立 # do ...
- mpvue 初体验之改写车标速查小程序
前文 说到我开发了一个简单的小程序叫做 车标速查(代码以及二维码详见 这里),本文简单讲讲如何将这个小程序转为 mpvue 开发(最终 成果 ) mpvue 官网的 文档 真的是非常简单,不,应该说是 ...
- 针对zstack虚拟机导出的问题的解决办法!
1. nfs 首先定位image所在位置: 云主机--〉云盘操作--〉配置信息--〉更多信息--安装路径 /opt/zstack/nfsprimarystorage/prim-aab63dc6284f ...