“类欧几里得算法”第一题 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\)。 那么原式可以改写为:

\[\sum_{d=1}^n 1-2\times(\lfloor d\sqrt r\rfloor\bmod 2)=\sum_{d=1}^n 1-2\times(\lfloor d\sqrt r\rfloor-2\times\lfloor\frac{d\sqrt r}{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\),那么原式即为

\[n-2\times f(1,0,1,n)+4\times f(1,0,2,n)
\]

考虑关于\(f(a,b,c,n)\)的算法,(开始扣题了),设\(k=\frac{a\sqrt r +b}{c}\)。

当\(k\ge1\)时,\(k\)可以拆为一个正数+小于1的非负实数,即

\[f(a,b,c,n)=\sum_{d=1}^n \lfloor d\times k\rfloor=\sum_{d=1}^n d\times\lfloor k\rfloor+\lfloor d\times(k-\lfloor k\rfloor)\rfloor\\
=\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\)时,(比如上面递归的那层),可以看作是满足

\[\begin{cases}
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}\)。

\[f(a,b,c,n)=n\times\lfloor n\times k\rfloor-f(ac,-bc,a^2r-b^2,\lfloor n\times k\rfloor)
\]

可以发现,情况一二交替递归,且每轮的总的变化与欧几里得算法相似,故知其复杂度为\(\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的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

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

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

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

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [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 ...

随机推荐

  1. ABP 设置默认为中文

    把资源文件 的zh-cn去掉就可以,改成默认文件

  2. vscode+MinGW+cmake设置轻量ide

    本地随手写一些题目的时候,发现visual studio非常庞大emmm vscodevscode是一个轻量编辑器 (1)vscode插件与设置自动同步 在两个电脑上,用vscode可以同步插件 ,利 ...

  3. win10在Pycharm中安装scrapy

    查看官网说明 发现推荐是安装Anaconda 或 Miniconda,这东西有点大而全,感觉目前用不上.所以没这样做. 直接安装scrapy 如果直接装会报错的,参考文章就可以解决. 这里记一下组件下 ...

  4. redis多实例

    1.首先在发布系统: 2.安装多实例利用cmd命令安装,切换目录到redis下 (1)首先拷贝一个redis的conf文件(如redis_6380.conf),并且修改里面的服务端口号.日志端口号,以 ...

  5. mysql 主从库同步

    #主库修改my.ini [mysqld] server log-bin=mysql-bin binlog-do-db=demo #从库修改my.ini [mysqld] server replicat ...

  6. "hello,world"———C++入门有感

    刚进入这所学校时,编程对于我来说应该算得上一个既熟悉又陌生的词语.虽然曾经耳边不断有人不断提到编程语言,C语言,程序猿等词语,但是作为一个外行人在来到这所学校之前,对于其中的奥秘还是没什么特别了解,仅 ...

  7. sqlserver 分割字符串和调用

    传入某种规则拼接字符串获得数组(表) /*功能说明:传入字符串跟分割符('''SGHE00000003'',''SGHE00000004'',''SGHE00000005'''),返回一个Table* ...

  8. 解决更新ssh后在/etc/init.d下无sshd的问题

    1.将远程服务器的/etc/init.d/ssd  文件拷贝到本地 scp /etc/init.d/ssh  root@IP地址:/etc/init.d 2.vi /etc/init.d/sshd 3 ...

  9. Python if __name__ == '__main__':

    python属于脚本语言,只能逐行运行, if __name__ == '__main__':这句相当于main(),即首先执行这条语句.

  10. bootstrap中的dropdown组件扩展hover事件

    bootstrap的下拉组件,需要点击click时,方可展示下拉列表.因此对于喜欢简单少操作的大家来说,点击一下多少带来不便,因此,引入hover监听,鼠标经过自动展示下拉框.其实在bootstrap ...