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 ...
随机推荐
- 这一次,彻底弄懂 JavaScript 执行机制
本文转自https://juejin.im/post/59e85eebf265da430d571f89#heading-4 本文的目的就是要保证你彻底弄懂javascript的执行机制,如果读完本文还 ...
- [转]numpy的getA()/getA1()/getH()/getI()函数
转自https://blog.csdn.net/weixin_42906066/article/details/82625779 1.mat.getA() 将自身矩阵变量转化为ndarray类型的变量 ...
- 公式推导【IoUNet//ECCV2018】
Jiang B, Luo R, Mao J, Xiao T, Jiang Y. Acquisition of localization confidence for accurate object d ...
- HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode 与 Server.HtmlDecode ,Server.HtmlEncode 与 HttpServerUtility.HtmlDecode , HttpServerUtility.HtmlEncode
HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码,通常是编码以下字符"<".">"."&" 等. ...
- 简明了解apply()和call()
apply()和call()都是ES6语法的,并且都是函数的方法. function foo() { alert(this.name) } var obj = { name: '小明' } foo() ...
- JDBC的安装与使用
JDBC的安装 首先在登录MySQL的官网下载JDBC-MySQL数据库驱动,或者去www.mysql.com/products/connector直接下载. 因为jdbc包属于第三方包,因此要自己导 ...
- sequelize-auto生成sequelize所有模型
sequelize是node最受欢迎的orm库,普遍使用 Promise. 意味着所有异步调用可以使用 ES2017 async/await 语法. 快速入门地址:https://github.com ...
- 【目标检测】关于如何在 PyTorch1.x + Cuda10 + Ubuntu18.0 运行 CenterNet 源码
这几天一直在尝试运行CenterNet的源码,但是出现各种问题,本已经打算放弃,中午吃完饭又不甘心,打算重新安装环境再来一遍,没想到竟然成功了.所以,坚持下去,黑夜过后便是黎明. 注意:gcc/g++ ...
- 什么是code-Behind技术?
code-Behind技术就是代码隐藏(代码后置),在ASP.NET中通过ASPX页面指向CS文件的方法实现显示逻辑和处理逻辑的分离,这样有助于web应用程序的创建. 比如分工,美工和编程的可以个干各 ...
- WPF MVVM框架(5)
前面几章节所讲到的内容, 基本上属于前端XAML的使用方法, 那么本章及后面的章节, 则会侧重于UI与业务分离如何分离 . UI与业务逻辑之间的互操作性,, 下面将介绍WPF中, 比较主流的MVVM框 ...