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 ...
随机推荐
- 在CI框架中的配置整合amfphp
之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1. 搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...
- leetcode 110 Balanced Binary Tree(DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- perl 语言学习总结
.#!/usr/bin/perl -w 内建警告信息,Perl发出警告 .字符串 . 连接符 .重复次数 .字符串与数字之间的自动转换 .; + += *= .= not and or xor .pr ...
- 如何加快建 index 索引 的时间
朋友在500w的表上建索引,半个小时都没有结束.所以就讨论如何提速. 一.先来看一下创建索引要做哪些操作:1. 把index key的data 读到内存==>如果data 没在db_cache ...
- Wmware Player中Linux挂载U盘
菜单(Player)中有一项是可移动设备,中选择U盘,然后选择连接(断开主机), 然后在命令行中敲入 fdisk -l 正常情况下是sda是硬盘的信息,然后将会看到一个单蹦的sdb4的信息(sdb4可 ...
- try-catch-finally中return的执行情况
在try中没有异常的情况下try.catch.finally的执行顺序 try--- finally 如果try中有异常,执行顺序是try--- catch --- finally 如果try中没有异 ...
- [51nod1065]最小正子段和
题意:求一个序列中大于0的最小子段和. 解题关键: 先求出前缀和和,对于每个位置求某个位置到当前位置和大于1的和的最小值.然而这是复杂度是O(n^2)的.其实可以通过排序优化到O(nlogn).对前缀 ...
- [hdu3530]Subsequence (单调队列)
题意:求在一段序列中满足m<=max-min<=k的最大长度. 解题关键:单调队列+dp,维护前缀序列的最大最小值,一旦大于k,则移动左端点,取max即可. #include<cst ...
- Eclipse中一个开发AspectJ的插件安…
eclipse4.2 EE版本插件安装 Eclipse最新版本Juno版本发布.部分插件版本跟不上. 选择自己需要的插件安装. eclipse http://www.eclipse.org/downl ...
- CSP 的有用资料
具体请参考: http://software-security.sans.org/downloads/appsec-2014-files/building-a-content-security-pol ...