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 ...
随机推荐
- 【oracle】INSERT INTO SELECT
- 使用Python拆分数据量大的CSV文件(亲测有效)
转载:https://www.cnblogs.com/FYZHANG/p/11629075.html 一次就运行成功了,感谢博主分享 #!/usr/bin/env python3 # -*- # @F ...
- django 报错Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: ['$post/(?P<pk>[0-9]+)/$']
Django报错:Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: [' ...
- IronPython
当时做FitnesseTest的时候,写了很多和硬件交互的代码,但是后来发现每次都通过启动进程的方式运行python脚本,很费时间. 既然要运行python脚本,在.net平台下可以用IronPyth ...
- 动手学深度学习11- 多层感知机pytorch简洁实现
多层感知机的简洁实现 定义模型 读取数据并训练数据 损失函数 定义优化算法 小结 多层感知机的简洁实现 import torch from torch import nn from torch.nn ...
- 自已开发IM有那么难吗?手把手教你自撸一个Andriod版简易IM (有源码)
本文由作者FreddyChen原创分享,为了更好的体现文章价值,引用时有少许改动,感谢原作者. 1.写在前面 一直想写一篇关于im即时通讯分享的文章,无奈工作太忙,很难抽出时间.今天终于从公司离职了, ...
- virsh 查看信息
获取域网络接口信息 virsh domiflist debian8 获取vcpu信息 virsh vcpuinfo debian8 设定内存最大内存 virsh setmaxmem debian8 9 ...
- 下载安装office2019
Hello,大家好,我是小喵. 支付宝搜索“321994”,领红包喽! 前几天答应给大家写一篇关于安装激活Office2019的文章.一直在准备,准备制作GIF动图,制作图片等,把我电脑上的Offic ...
- 2019-11-29-WPF-模拟触摸设备
原文:2019-11-29-WPF-模拟触摸设备 title author date CreateTime categories WPF 模拟触摸设备 lindexi 2019-11-29 08:47 ...
- 简单ALV得演示(用到了ALV可编辑及保存后修改数据库)
*&---------------------------------------------------------------------* *& Report YPMRP010_ ...