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. Java的 StringBuffer 和 StringBuilder 类

    https://www.runoob.com/java/java-stringbuffer.html 返回值是它本身的类, 所以可以链式调用! 总结就是可以直接在对象上使用 , 可以链式使用, buf ...

  2. POJ2976Dropping tests(分数规划)

    传送门 题目大意:n个二元组a[i],b[i],去掉k个,求sigma a[i]/ sigma b[i]的最大值 代码: #include<iostream> #include<cs ...

  3. 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)

    点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...

  4. 【2019.7.20 NOIP模拟赛 T2】B(B)(数位DP)

    数位\(DP\) 首先考虑二进制数\(G(i)\)的一些性质: \(G(i)\)不可能有连续两位第\(x\)位和第\(x+1\)位都是\(1\).因为这样就可以进位到第\(x+2\)位.其余情况下,这 ...

  5. 数据仓库006 - MySQL 5.6.x - Linux最佳生产环境离线部署

    一.离线安装包 文件准备 这里以mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz为例,记一次MySQL 5.6.x 的生产环境离线部署过程.使用SecureCRT连接 ...

  6. 国内Java面试总是问StringBuffer,StringBuilder区别是啥?档次为什么这么低?

    GitHub 6.6k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 6.6k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 6.6k Star 的 ...

  7. Appium移动自动化测试-----(三)Intellij IDEA + Android SDK + Genymotion Emulator

    下载安装Intellij IDEA 略 下载Android SDK http://tools.android-studio.org/index.php/sdk    下载后解压 http://www. ...

  8. JMS简介与入门

    1:JMS引入 如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去 ...

  9. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  10. Python 多进程池

    def get_html(n): time.sleep(n) print("sub_progress success") return n # 多进程池 pool = multip ...