[BZOJ3817]Sum

试题描述

给定正整数N,R。求

输入

第一行一个数 T,表示有 T 组测试数据。
接下来 T 行,每行两个正整数 n,r。

输出

输出 T 行,每行一个整数表示答案。

输入示例


输出示例


-

数据规模及约定

对于 100% 的数据,满足 n≤10^9,r≤10^4,T≤10^4。

题解

新技能:类欧几里得算法。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define LL long long
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int r;
double x;
int f(int a, int b, int c, int n) {
if(!n) return 0;
int t = gcd(gcd(a, b), c); a /= t; b /= t; c /= t;
int c1 = c - (LL)((b * x + c) / a) * a;
LL s = (LL)((b * x + c) / a) * ((LL)n * (n + 1) >> 1) + n * (LL)((b * x + c1) / a * n);
return s - f(b * b * r - c1 * c1, a * b, -a * c1, (b * x + c1) / a * n);
} int main() {
int T = read();
while(T--) {
int n = read(); r = read();
x = sqrt(r);
if(x == (int)x && (r & 1)) printf("%d\n", (n & 1) ? -1 : 0);
else printf("%d\n", n + (f(2, 1, 0, n) << 2) - (f(1, 1, 0, n) << 1));
} return 0;
}

记得特判 r 是完全平方奇数的情况。(我也不知道为啥。。。)并且记得在函数 f(a, b, c, n) 中要给 a, b, c 约分。

[BZOJ3817]Sum的更多相关文章

  1. BZOJ3817 Sum(类欧几里得算法)

    设$t=\sqrt r$,原题转化为$\sum_{x=1}^n(4*\lfloor\frac{tx}2\rfloor-2*\lfloor tx\rfloor+1)$考虑如何求$\sum_{x=1}^n ...

  2. 【BZOJ3817/UOJ42】Sum(类欧)

    [BZOJ3817/UOJ42]Sum(类欧) 题面 BZOJ UOJ 题解 令\(x=\sqrt r\),那么要求的式子是\[\sum_{d=1}^n(-1)^{[dx]}\] 不难发现,对于每个\ ...

  3. BZOJ3817 清华集训2014 Sum 类欧几里得

    传送门 令\(\sqrt r = x\) 考虑将\(-1^{\lfloor d \sqrt r \rfloor}\)魔改一下 它等于\(1-2 \times (\lfloor dx \rfloor \ ...

  4. LeetCode - Two Sum

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

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

  6. Leetcode 笔记 112 - Path Sum

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

  7. POJ 2739. Sum of Consecutive Prime Numbers

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

  8. BZOJ 3944 Sum

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

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

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

随机推荐

  1. UVA208 Firetruck 消防车(并查集,dfs)

    要输出所有路径,又要字典序,dfs最适合了,用并查集判断1和目的地是否连通即可 #include<bits/stdc++.h> using namespace std; ; int p[m ...

  2. eclipse中代码注释及其他常用快捷键

    html代码注释/取消注释             Ctrl + Shift + c php代码注释/取消注释                 Ctrl + / (4)Ctrl+Shift+/ 说明: ...

  3. 修改deeplabv3的test的输出的label颜色

    deeplab.py是拿来做test的,其中的postprecess函数中的palette = pascal_palette_invert()是给每个类别加颜色 这个是通过import utils获得 ...

  4. Opencascade术语笔记。

    1. chamfer 倒角 vs fillet  圆角: 2.boolean operatiron(布尔操作): common(相加),fuse(相交),cut(相减): 3.depressions( ...

  5. CPP-基础:C/C++数组名与指针的区别

    2005-08-23 08:36 来源:天极网 作者:宋宝华 责任编辑:方舟·yesky 引言 指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很 ...

  6. tensorflow目标检测API之建立自己的数据集

    1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...

  7. nginx下配置laravel+rewrite重写

    server { listen ; server_name ha.d51v.cn; #access_log /data/wwwlogs/access_nginx.log combined; root ...

  8. FastJsonUtils工具类

    fastjson是由alibaba开源的一套json处理器.与其他json处理器(如Gson,Jackson等)和其他的Java对象序列化反序列化方式相比,有比较明显的性能优势. 版权声明:本文为博主 ...

  9. 22.Yii2.0框架多表关联一对一查询之hasOne

    思路: 通过文章查它对应的分类信息 一对一的关系 控制器里 //一对一关联查询 public function actionRelatesone() { //方法一,hasOne() 用查一条文章的结 ...

  10. python3 连接 mysql和mariadb的模块

    是pymysql python2中是MySQL-python sudo pip install pymysql 参考博客https://www.jb51.net/article/140387.htm