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. C getchar()

    C getchar() #include <stdio.h> int main() { ; char str[size]; ; char ch; printf("Enter wh ...

  2. unique_ptr的实现原理

    在C++11中有两个智能指针类型来管理动态对象,share_ptr允许多个指针指向同一个对象,unique_ptr则“独占”所指对象. 我们知道指针或引用在离开作用域时是不会进行析构的,但是类在离开作 ...

  3. 【2019.8.11上午 慈溪模拟赛 T3】欢迎回来(back)(设阈值+莫队)

    设阈值 考虑对于询问的\(d\)设阈值进行分别处理. 对于\(d\le\sqrt{max\ d}\)的询问,我们可以\(O(n\sqrt{max\ d})\)预处理答案,\(O(1)\)输出. 对于\ ...

  4. 360安全浏览器右击不显示审查元素 或按F12不弹出开发人员工具的原因和解决方法:设为极速模式

    IE兼容模式  会显示 IE的开发人员工具 极速模式 才会显示谷歌的那种方式 IE调试模式不怎么习惯,如下图 正常调试模式如下图

  5. GC原理---对象可达判断

    对象可达判断 引用计数算法(Reference Counting) 给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值加1:当引用失效时,计数器减1:任何时刻计数器都为0的对象就是不可能再被 ...

  6. HTTP协议第一篇:三握手解读

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  7. 在ASP.Net Core 中使用枚举类而不是枚举

    前言:我相信大家在编写代码时经常会遇到各种状态值,而且为了避免硬编码和代码中出现魔法数,通常我们都会定义一个枚举,来表示各种状态值,直到我看到Java中这样使用枚举,我再想C# 中可不可以这样写,今天 ...

  8. 物联网架构成长之路(32)-SpringBoot集成MQTT客户端

    一.前言 这里虽然是说MQTT客户端.其实对于服务器来说,这里的一个具有超级权限的MQTT客户端,就可以做很多事情.比如手机APP或者网页或者第三方服务需要发送数据到设备,但是这些又不是设备,又不能让 ...

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

  10. ELK 框架整体流程编写 以及logstash脚本编写

    Elasticsearch Elasticsearch 是一个实时的分布式搜索和分析引擎,它可以用于全文搜索,结构化搜索以及分析.它是一个建立在全文搜索引擎 Apache Lucene 基础上的搜索引 ...