[BZOJ3817]Sum
[BZOJ3817]Sum
试题描述
输入
输出
输入示例
输出示例
-
数据规模及约定
对于 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的更多相关文章
- BZOJ3817 Sum(类欧几里得算法)
设$t=\sqrt r$,原题转化为$\sum_{x=1}^n(4*\lfloor\frac{tx}2\rfloor-2*\lfloor tx\rfloor+1)$考虑如何求$\sum_{x=1}^n ...
- 【BZOJ3817/UOJ42】Sum(类欧)
[BZOJ3817/UOJ42]Sum(类欧) 题面 BZOJ UOJ 题解 令\(x=\sqrt r\),那么要求的式子是\[\sum_{d=1}^n(-1)^{[dx]}\] 不难发现,对于每个\ ...
- BZOJ3817 清华集训2014 Sum 类欧几里得
传送门 令\(\sqrt r = x\) 考虑将\(-1^{\lfloor d \sqrt r \rfloor}\)魔改一下 它等于\(1-2 \times (\lfloor dx \rfloor \ ...
- 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 ...
随机推荐
- HDU 5091 Beam Cannon (扫描线思想)
题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...
- groupadd - 建 立 新 群 组
总览 SYNOPSIS groupadd [-g gid [-o]] [-r] [-f] group 描述 DESCRIPTION groupadd 可 指 定 群 组 名 称 来 建 立 新 的 群 ...
- 01_4_SERVLET声明周期
01_4_SERVLET声明周期 1. Servlet的生命周期 生命全过程 加载ClassLoader 实例化 new //客户端第一次请求的时候,只new一次 初始化init(ServletCon ...
- POP简单动画简单使用 (入门级别)
动画可以让APP“更友好”的与用户交互,苹果提供很多的好看的动画供开发者使用,不过简单的平移.旋转.缩放.......使用起来很简单,但是想要进行一些比较复杂的动画效果,使用起来就比较难以实现,俗话说 ...
- NSOperation、NSOperationQueue
NSOperation.NSOperationQueue NSOperation 和 NSOperationQueue 配合使用也能实现多线程. NSOperation 继承于 NSObject,是一 ...
- php代码压缩
php代码压缩,除可以使用token_get_all进行压缩之外,还可以使用系统自带的函数 php_strip_whitespace (PHP 5) php_strip_whitespace — ...
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(index-parameter style with registered outputs)
case语句中,对于state/next 矢量仅仅做了1-bit比较. parameter 值不是表示FSM的状态编码,而是表示state/next变量的索引.
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals
1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...
- ThinkPHP5 高级查询之构建分组条件
ThinkPHP5 高级查询之构建分组条件 一.在tp5中通过where方法如何构建分组条件, 例如:where user_id=$this->user_id and (status in (4 ...
- 第3-5课 填充左侧菜单/品牌的添加 Thinkphp5商城第四季
目录 左侧菜单的填充 品牌的添加 form标签里要加上method="post" enctype="multipart/form-data" form标签里如果 ...