B.Icebound and Sequence
链接:https://ac.nowcoder.com/acm/contest/903/B
题意:
Icebound hates math. But Imp loves math. One day, Imp gave icebound a problem.
The problem is as follows.
S=(∑ni=1qi) mod pS=(∑i=1nqi) mod p
For given q,n,p, you need to help icebound to calculate the value of S.
思路:
等比数列求和。
因为考虑到取余,所以不能直接算。
令S(n) 为等比数列前n项和。
若n为偶数:
S(n) = S(n/2) + S(n/2)*a^(n/2) (因为第i(i <= n/2)项和i+n/2项存在第i项乘a^(n/2)等以第i+n/2项的值。
若n为奇数:
S(n) = S(n/2) + S(n/2)*a^(n/2) + a^n
代码:
#include <bits/stdc++.h>
using namespace std; typedef long long LL; LL n, q, p; LL QM(LL a, LL b, LL m)
{
LL res = 1;
while (b)
{
if (b&1)
res = (res*a)%m;
a = (a*a)%m;
b >>= 1;
}
return res;
} LL GetR(int t)
{
if (t == 1)
return n%p;
if (t%2 == 0)
return (GetR(t/2)+(GetR(t/2)*QM(n, t/2, p))%p)%p;
else
return ((GetR(t/2)+(GetR(t/2)*QM(n, t/2, p))%p)%p+QM(n, t, p))%p;
} int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> q >> p;
cout << GetR(q) << endl;
} return 0;
}
B.Icebound and Sequence的更多相关文章
- 2019河北省大学生程序设计竞赛(重现赛)B 题 -Icebound and Sequence ( 等比数列求和的快速幂取模)
题目链接:https://ac.nowcoder.com/acm/contest/903/B 题意: 给你 q,n,p,求 q1+q2+...+qn 的和 模 p. 思路:一开始不会做,后面查了下发现 ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- tensorflow knn mnist
# MNIST Digit Prediction with k-Nearest Neighbors #----------------------------------------------- # ...
- hdu 6109 数据分割
/** * 题目描述有点坑,勉强能读懂,大致意思,有多组约束条件.原本每组数据之间是有分界符号的 * 现在分界符号没了,让你找出原来每组数据多少个条件,并且告诉,每组的最后一个条件会使得与前面的 * ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
py文件直接在cmd窗口用python命令执行时正常:代码逐句在ipython中也正常:但是, 在wingIDE中运行报错“UnicodeEncodeError: 'ascii' codec can' ...
- Vue 中数据流组件
好久不见呀,这两年写了很多很多东西,也学到很多很多东西,没有时常分享是因为大多都是我独自思考.明年我想出去与更多的大神交流,再修筑自己构建的内容. 有时候我会想:我们遇到的问题,碰到的界限,是别人给的 ...
- Scrapy,终端startproject,显示错误TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
F:\python_project\test>scrapy startproject spz Traceback (most recent call last): File "d:\p ...
- css 跳转电脑分辨率
因为我们经常在项目中要适配各种屏幕,为了方便前端的开发和测试.我们可以直接把电脑的分辨率调整到需要适配的最小的分辨率,其实还有一种更直接粗暴的方法.直接按F12打开控制台,在收拉浏览器就能看到目前的分 ...
- MongoDB中的一些坑(最好不要用)
MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless,大部分情况下性能也很棒.但是薄荷 ...
- 使用webdriver出现的问题:[18796:1808:0730/131103.313:ERROR:install_util.cc(600)] Failed to read HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken: 系统找不到指定的文件。 (0x2) DevTools lis
1.注册表导致 [5956:4996:0710/155156.898:ERROR:install_util.cc(589)] Unable to create registry key HKLM\SO ...
- 基于Html5的移动端APP开发框架
快速增长的APP应用软件市场,以及智能手机的普及,手机应用:Native(原生)APP快速占领了APP市场,成为了APP开发的主流,但其平台的不通用性,开发成本高,多版本开发等问题,一直困扰着专业AP ...
- a possible low-level optimization
http://www.1point3acres.com/bbs/thread-212960-1-1.html 位的vector<int> counts,遍历nums,然后counts[nu ...