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 ...
随机推荐
- Manthan, Codefest 18 (rated, Div. 1 + Div. 2) F 单调栈 + 贡献 + 计数
https://codeforces.com/contest/1037/problem/F 题意 function z(array a, integer k): if length(a) < k ...
- Paper | Non-Local ConvLSTM for Video Compression Artifact Reduction
目录 1. 方法 1.1 框图 1.2 NL流程 1.3 加速版NL 2. 实验 3. 总结 [这是MFQE 2.0的第一篇引用,也是博主学术生涯的第一篇引用.最重要的是,这篇文章确实抓住了MFQE方 ...
- 【新特性速递】树控件结构由单层 TR 改为 TR-TD-TABLE 层级嵌套
FineUIPro/Mvc/Core的下个版本(v6.1.0),我们对树控件进行了优化,由原来的单层 TR 改为 TR-TD-TABLE 层级嵌套,从而做到表里如一. 上个版本(v6.0.0),我们对 ...
- python xpath图片爬取
import requests from urllib.request import urlretrieve from lxml import etree headers = { 'User-Agen ...
- Java 银联云闪付对接记录
一开始盲目找资料走了弯路: 还是从银联给的官方文档入手最高效: 附件3:云闪付业务商户入网服务指引.pdf http://tomas.test.upcdn.net/pay/%E9%99%84%E4%B ...
- datalab (原发布 csdn 2018年09月21日 20:42:54)
首先声明datalab本人未完成,有4道题目没有做出来.本文博客记录下自己的解析,以便以后回忆.如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵. /* * bitAnd - x& ...
- asp.net 获取当前,相对,绝对路径
一.C#获取当前路径的方法: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. ...
- python3之利用字典和列表实现城市多级菜单
利用字典和列表实现城市多级菜单 #coding:utf-8 #利用字典和列表实现城市多级菜单 addrIndex = {":"福建"} addrDict = {" ...
- Python【day 14-2】递归遍历文件夹
#需求 遍历文件夹中所有的子文件夹及子文件--用递归实现 '''''' ''' 伪代码 1.遍历根目录--listdir for 得到第一级子文件夹(不包含子文件夹的子文件)和文件 2.判断是文件还是 ...
- java9模块不可见问题
问题描述 jdk.internal.reflect包不可见 问题原因 java9模块化之后,java.base只把jdk.internal.reflect暴露给了少数几个内部包而没有向当前模块暴露. ...