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 ...
随机推荐
- latex编译过程-关于嵌入所有字体
我们的初始目的是想在编译的过程中嵌入所有字体 参考 我们进行了设置,但是不起作用,后发现使用pdflatex编译时是不会调用 ps2pdf的 然后,我们就需要了解编译过程 1. 通常,我们使用texs ...
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...
- L91
Make Healthy Choices Easier Options Telling people to change unhealthy behaviors doesn't work. Other ...
- 第六章-jQuery
jQuery的理念是: 写更少的代码, 完成更多的工作 jQuery有两个版本1.x和2.x, 版本2.x不再支持IE678 jQuery最明显的标志就是$, jQuery把所有的功能都封装在了jQu ...
- QT(2)项目文件介绍
一.项目创建 二.文件说明 三.QT模块
- Http客户端跳转和服务器端跳转的区别
服务器端跳转: 服务器转发全程是没有客户端参与的,都在web container容器内部进行,没有任何服务器和客户端的通信,实际就是服务器内部的跳转. 这次forward, 服务器没有构建H ...
- VS2008 查找失效怎么办
按Ctrl+F没有反应? visual studio 里 查找替换 显示不出来; 还能用 让他查找个不存在的文本还会弹出找不到的提示; 就是看不到 查找替换的操作框了; 问题解决方法: ...
- Python之文件输入输出,
文件输入与输出 • 打开文件返回文件对象 – file_object=open(file_name,access_mode='r') • 关闭文件对象 – file_object.close() ...
- 1、在 Windows 上安装 OpenCV-Python & ubuntu16.04安装 opencv
Goals In this tutorial We will learn to setup OpenCV-Python in your Windows system. Below steps are ...
- SQL中的drop,truncate和delete的区别
(1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作.TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把 ...