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 ...
随机推荐
- matlab程序计时
t1=datetime(); %程序 t2=datetime() totaltime=t2-t1; disp(t2-t1); 或者: tic %代码块 toc disp(['运行时间: ',num2s ...
- matplotlib中文显示-微软雅黑
网上有很多方法,但是基本的是片面的. 参考1 https://tracholar.github.io/wiki/python/matplotlib-chinese-font.html 参考2 http ...
- Java_HTTP_01_HttpClient
一. 二.参考文档 1. HttpClient官方文档 HttpClient官方文档中文翻译 1.HttpClient 4 实现文件下载 2.httpclient 上传文件.下载文件 3.httpcl ...
- less语言特性
作为CSS的一种扩展,LESSCSS不仅向下兼容CSS的语法,而且连新增的特性也是使用CSS的语法.这样的设计使得学习LESS很轻松,而且你可以在任何时候回退到CSS. 变量 很容易理解: @nice ...
- linux ssh 命令使用解析
前一阵远程维护Linux服务器,使用的是SSH,传说中的secure shell. 登陆:ssh [hostname] -u user 输入密码:***** 登 陆以后就可以像控制自己的机器一样控制它 ...
- zero to one(1)
How to google 这两天把之前的过期的账号重新弄了一下,在vutrl上的账号,普通的话现在2.5$只提供ipv6地址,如果是想google我觉得这个应该没有什么问题,或者可以买***的账号, ...
- Ubuntu——Python3.x——scikit-learn 安装
修改默认Python (默认的是Python2.7) rm -rf /usr/bin/python ln -s /usr/bin/ptyhon3 /usr/bin/python 安装所需依赖: apt ...
- session.write类型引发的思考---Mina Session.write流程探索.doc--zhengli
基于Mina开发网络通信程序,在传感器数据接入领域应用的很广泛,今天我无意中发现一个问题,那就是我在前端session.write(msg)数据出去之后,却没有经过Filter的Encoder方法,同 ...
- MySql介绍及优化(1)
mysql 数据库 1.是关系型数据库: 2.体积小.支持多处理器.开源.免费 3.在 Internet 中小型网站中的使用率尤其高 mysql 数据库开发常见问题及优化 一.库表设计 1.1 引擎选 ...
- Java 日志记录规则
Java 日志记录规则 规则一:日志是面向读者的 我们不应该让无价值的信息使日志文件变得乱糟糟,比如说完整打印所有的实体字段. 通常,实体名字和其逻辑关键字足以识别在表格中的一条记录了. 规则二:匹配 ...