[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. 删除表中一个字段的SQL语句

    1.删除没有默认值的列:alter table Test drop COLUMN BazaarType 2.删除有默认值的列:先删除约束(默认值)alter table Test DROP CONST ...

  2. Mandelbrot图像

      using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication3{    ...

  3. Android(java)学习笔记133:Eclipse中的控制台不停报错Can't bind to local 8700 for debugger

    [DDMS] Can't bind to local 8600 for debugger 改成 Under Window -> Preferences -> Android -> D ...

  4. 使用FontDialog组件设置字体

    实现效果: 知识运用: FontDialog组件的Font属性 //获取或设置选定的字体 public Font Font  { get;set; } 实现代码: private void butto ...

  5. 2018.4.12 各个系统安装MyEclipse过程(包括Mac、Linux、Windows)

    首先下载MyEclipse 最新官网在这里http://www.myeclipsecn.com/ mac 安装 . 在安装第一步会显示 "安装myeclipse显示更低版本javase6&q ...

  6. 跑superpixel的程序

    知乎上对superpixel的讲解还不错:https://www.zhihu.com/question/27623988 superpixel的算法有很多,opencv中也包含了很多,我找了一个比较经 ...

  7. cocos2dx 加密spine文件遇到的问题(暂时没有解决方法)

    今天我研究了一下加密spine动画的加密的方法,图片肯定要加密的,所以我只选择加密图片,另外的一个altas文件和json文件就不做加密打算. 我的思路是通过TexturePacker打包成加密的文件 ...

  8. Java微信公众号开发----关键字自动回复消息

    在配置好开发者配置后,本人第一个想要实现的是自动回复消息的功能,说明以下几点: 1. url 仍然不变,还是开发配置里的url 2. 微信采用 xml 格式传输数据 3.微信服务器传给我们的参数主要有 ...

  9. logback写日志

    https://blog.csdn.net/u010128608/article/details/76618263 https://blog.csdn.net/zhuyucheng123/articl ...

  10. 在VMware上安装centos7

    1. 下载centos7 64位镜像 linux官网下载:https://www.centos.org/download/ 2. 在VMware上安装centos7 2.1 新建虚拟机 打开虚拟机主页 ...