JDOJ 2782: 和之和

JDOJ传送门

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: 和之和的更多相关文章

  1. JDOJ 2785: 商之和 数论分块

    Code: #include <iostream> #include <cstdio> #define setIO(s) freopen(s".in",&q ...

  2. JDOJ 1140: 完数

    JDOJ 1140: 完数 题目传送门 Description 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是&qu ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. [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 ...

随机推荐

  1. IronPython

    当时做FitnesseTest的时候,写了很多和硬件交互的代码,但是后来发现每次都通过启动进程的方式运行python脚本,很费时间. 既然要运行python脚本,在.net平台下可以用IronPyth ...

  2. Pencil 基于Electron的GUI原型工具之菜单再探

    为什么要重试呢? 主要是觉得Pencil这个工具还是比较有价值.就像Linus对Linux下分发版的态度"让用户有选择"一样,在现在这个Sass服务.Web服务化越来越普遍越便利的 ...

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

  4. Vue.js 源码分析(二十九) 高级应用 transition-group组件 详解

    对于过度动画如果要同时渲染整个列表时,可以使用transition-group组件. transition-group组件的props和transition组件类似,不同点是transition-gr ...

  5. JVM的监控工具之jconsole

    JConsole(Java Monitoring and Management Console)是一种基于JMX的可视化监视.管理工具.管理的是什么?管理的是监控信息.永久代的使用信息.类加载等等 如 ...

  6. Docker 镜像-管理-导入-导出

    目录 Docker 镜像基本概念 Docker 镜像加速 Docker 镜像 常用命令 Docker 镜像的创建和导出导入 Docker 镜像基本概念 我们使用的容器都是基于镜像的,镜像是由多层组成的 ...

  7. mysql error 1364 Field doesn't have a default values

    https://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values. us ...

  8. 2019 物易云通java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.物易云通等公司offer,岗位是Java后端开发,因为发展原因最终选择去了物易云通,入职一年时间了,也成为了面 ...

  9. 前端页面 title keyword description的添加

    用法:<title>网站标题</title> 用法:<meta name=”Keywords” Content=”关键词1,关键词2,关键词3,关键词4″> 用法: ...

  10. Eclipse 笔记

    2004年前的开发架构:(操作系统+数据库+中间件+开发工具) AIX + DB2 + WAS + WSAD  ===  皇家级 UNIX/Linux + Oracle +BEA WebLogic + ...