JDOJ 2782: 和之和
JDOJ 2782: 和之和
Description
给出数n,求ans=(n+1)+(n+2)+...+(n+n)
Input
一行,一个整数n
Output
一行,一个整数ans%23333333333333333(2后面16个3)
Sample Input
1
Sample Output
2
HINT
0<=n<=1012,实际上可能还会更小点
最优解声明及解题背景:
(一道困扰了我半年的题)果然本蒟蒻还是太菜了/
很多学弟和比我后学的都比我先切了这道题,但是我还迟迟没有切。。
前几天补了快速幂和快速乘,想重新A这道题,没想到又WA......
然后经过各种玄学推导及修正了一堆小错误之后。。。
还是卡到了C语言的最优解。

题解:
一开始的思路是裸的n*n+一个1-n的等差数列。
后来被卡了百分之9,因为等差数列的公式在本题的数据范围会爆,而加模之后又不能保证除法式的正确性。
所以我们想到了另一种做法:快速乘。
如果对快速乘不太了解的小伙伴请参考以下的博客:
代码:
#include<cstdio>
#define ll long long
#define mod 23333333333333333ll
using namespace std;
ll n,ans;
ll qmult(ll a,ll b)
{
ll ret=0;
while(b>0)
{
if(b&1)
ret=(ret+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return ret;
}
int main()
{
scanf("%lld",&n);
if(n&1)
ans=qmult(n,(3*n+1)/2)%mod;
else
ans=qmult((3*n+1),n/2)%mod;
printf("%lld",ans);
return 0;
}
JDOJ 2782: 和之和的更多相关文章
- JDOJ 2785: 商之和 数论分块
Code: #include <iostream> #include <cstdio> #define setIO(s) freopen(s".in",&q ...
- JDOJ 1140: 完数
JDOJ 1140: 完数 题目传送门 Description 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是&qu ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- Spring Boot 2.2.1 正式发布,需特别注意这个注解的使用!
Spring Boot 2.2.1 已于2019年11月7日正式发布. 该版本内容包含110项修复.改进和依赖升级. 如果开发者要从Spring Boot 2.2.0升级到2.2.1的话,这里要特别注 ...
- hadoop自带RPC的使用 代码demo
引入的三方包 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...
- 为什么MySQL数据库要用B+树存储索引?
问题:MySQL中存储索引用到的数据结构是B+树,B+树的查询时间跟树的高度有关,是log(n),如果用hash存储,那么查询时间是O(1).既然hash比B+树更快,为什么mysql用B+树来存储索 ...
- hdu6494 dp
http://acm.hdu.edu.cn/showproblem.php?pid=6494 题意 一个长n字符串(n,1e4),'A'代表A得分,'B'代表B得分,'?'代表不确定,一局比赛先得11 ...
- 【2019.7.22 NOIP模拟赛 T1】麦克斯韦妖(demon)(质因数分解+DP)
暴力\(DP\) 先考虑暴力\(DP\)该怎么写. 因为每个序列之后是否能加上新的节点只与其结尾有关,因此我们设\(f_i\)为以\(i\)为结尾的最长序列长度. 每次枚举一个前置状态,判断是否合法之 ...
- 第三方系统平台如何对接gooflow2.0
第一步,参与者数据源配置 目前提供3种参与者数据源(员工,角色,部门),还有一种sql语句 XML配置如下 <?xml version="1.0" encoding=&quo ...
- 游戏引擎架构 (Jason Gregory 著)
第一部分 基础 第1章 导论 (已看) 第2章 专业工具 (已看) 第3章 游戏软件工程基础 (已看) 第4章 游戏所需的三维数学 (已看) 第二部分 低阶引擎系统 第5章 游戏支持系统 (已看) 第 ...
- Note | 论文写作笔记
目录 1. 规范 2. 语法 3. 其他 4. 好图好表 5. 好表达 我们的工作很重要 我们的工作有意义 我们的工作细节 我们怎么组织这篇文章 最终效果出类拔萃 怎么解释我们的成功 写完逐条核对吧. ...
- 深度解密Go语言之context
目录 什么是 context 为什么有 context context 底层实现原理 整体概览 接口 Context canceler 结构体 emptyCtx cancelCtx timerCtx ...
- springboot2 中Druid和ibatis(baomidou) 遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.iflytek.pandaai.service.multi.mapper.TanancyMapper
调用mapper中任何方法都会出现类似的错误 org.apache.ibatis.binding.BindingException: Invalid bound statement (not foun ...