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. Pycharm2019.2.4专业版激活

    Pycharm2019.2.4专业版激活 IDE是开发者创建程序时使用的的软件包,它通过简单的用户界面集成多个高度关联的组件,从而最大化提升编程体验和生产效率:本质上,IDE是一种改进代码创建.测试和 ...

  2. Ubuntu 安装git及git命令

    1.检查git是否已经安装,输入git version命令即可,如果没有显示版本号表示没有安装git 2.安装git sudo apt-get install git 3.配置git全局环境git c ...

  3. (十八)golang--defer关键字

    在函数中,程序员经常需要创建资源(比如,数据库连接,文件句柄,锁等),为了在函数执行完毕后,及时释放资源,go设计者提供defer(延时机制) 用defer申明的语句不会立即执行,而是被存入到defe ...

  4. 02Shell变量

    Shell变量 什么是 shell 变量 shell变量就是 用一个固定的字符串去表示不固定的内容 变量的类型 自定义变量 定义变量 变量名=变量值 (显式赋值) 变量名必须以字母或下划线开头,区分大 ...

  5. kmp算法笔记(简单易懂)

    一般字符串比较长串m短串为n,那么用暴力方法复杂度为O(m*n) 但是kmp却可以达到O(m+n)!!!!!! 对于这个神奇的算法,我也是似懂非懂, 下面介绍一个简单的方法求kmp 1.求next数组 ...

  6. 明解C语言 中级篇 第二章答案

    练习2-1 /* 倒计时后显示程序运行时间 */ #include <time.h> #include <stdio.h> /*--- 等待x毫秒 ---*/ int slee ...

  7. perf性能调优

    工具准备 ubuntu:sudo apt-get install perf 嵌入式平台:下载源码编译 注意:编译过程中会有Auto-detectiing system feature的依赖库打印,注意 ...

  8. python 统计使用技巧

    python 统计使用技巧 # 1.不输入回车获取值 注:需要tty模块配合. fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ...

  9. Mybatis传递多个参数的几种方式

    顺序传参法 public User selectUser(String name, int deptId); <select id="selectUser" resultMa ...

  10. Java向服务器上传图片

    在比较绚丽多彩的网站或者业务逻辑比较丰富的程序设计过程中,图片的相关操作时必不少的,尤其时图片的上传.还没有彻底摆脱纸质办公可能需要将纸质的文件备份上传,网站的建设可能需要上传用户头像.图片描述等等, ...