UVALive - 5798
/**
题意:B,P,L,N,分别表示进制,mod,数组的个数,操作数
做法:树状数组 欧几里得 每个数加入到数组Tree的数是 B^(L-i)
用树状数组进行维护前缀和,然后求一段区间的数,除以B^(L-j)
因为(前缀和/B^(L-j)) 很大不好计算,所以就用乘法逆元
(k是a关于p的乘法的逆元) a*k≡1 (mod p) === (a/b)mod p (b关于p的乘法的逆元)
PS(当我们要求(a/b) mod p的值,且a很大,无法直接求得a/b的值时,我们就要用到乘法逆元。
我们可以通过求b关于p的乘法逆元k,将a乘上k再模p,即(a*k) mod p。其结果与(a/b) mod p等价。)
**/
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <stdio.h>
#define maxn 200000 + 10
using namespace std;
long long Tree[maxn];
long long mmap[maxn]; ///逆元
long long _next[maxn]; /// 次方
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
if(a == && b == ) return -;
if(b == )
{
x = ;
y = ;
return a;
}
long long d = extend_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
}
long long mod_reverse(long long a,long long n)
{
long long x,y;
long long d = extend_gcd(a,n,x,y);
if(d == ) return (x%n+n)%n;
else return -;
}
long long B,P,L,N;
int lowbit(int x)
{
return x&(-x);
}
void add(int x, long long value)
{
for(int i = x; i <= L; i += lowbit(i))
{
Tree[i] = ((Tree[i] + value) % P + P) % P;
}
}
long long getsum(int x)
{
long long sum =;
for(int i=x; i; i -= lowbit(i))
sum = ((sum + Tree[i])%P + P)%P;
return sum;
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%lld %lld %lld %lld",&B,&P,&L,&N))
{
if(B == && P == && L == &&N == ) break;
memset(Tree,,sizeof(Tree));
memset(mmap,,sizeof(mmap));
memset(_next,,sizeof(_next));
int tmp = ;
mmap[L] = ;
_next[L] = ;
for(int i=L-; i>=; i--)
{
tmp = (tmp *B) %P;
mmap[i] = mod_reverse(tmp,P);
_next[i] = tmp;
} int u,v;
char ch[];
for(int i=; i<=N; i++)
{
scanf("%s %d %d",ch,&u,&v);
//cout<<ch<<" "<<u<<" "<<v<<endl;
if(ch[] == 'E')
{
long long ans = (getsum(u) - getsum(u-));
ans -= v*_next[u];
add(u,-ans);
}
else if(ch[] == 'H')
{
long long ans = ((getsum(v) - getsum(u-))%P +P)%P;
//cout<<"ans = "<<ans<<endl;
ans = (ans *mmap[v])%P;
printf("%lld\n",ans);
}
}
printf("-\n");
}
return ;
}
UVALive - 5798的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- VirtualBox上安装ubuntu
当安装完成,重启后,在启动界面出现Please remove the installation medium,then press ENTER.问题? 解决方法:在VirtualBox里面通过iso文 ...
- MQTT在平台中的应用【本文摘自智车芯官网】
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分.该协议支持所有平台,几乎可以把所有联 ...
- Jquery 跨域请求JSON数据问题
制作网站时,我们有时候为了方便快捷会调用别人写好的API接口,或者是调用一些免费的API接口获得JSON数据.比如天气,农历,网站备案信息查询等. 但是,这些API接口都是别人自己服务器上的,我们要调 ...
- css实现div一直旋转
看到音乐播放器播放界面的唱片一直旋转,突然想到在网页中的一直旋转效果,所有特地查了一下分享出来 这是一个静态的div,然后把它旋转动起来.效果请看右上角的音乐播放按键一样. 代码如下: <htm ...
- kkpager的改进,Ajax数据变化但是页码不变的问题,kkpagerajax
最近做项目用到了kkpager来做分页,在一个页面只是调用一次的时候不会出现问题,但是在一个页面多次调用就出现问题了. 在网上搜集了好久,终于找到了解决方法,记录下来方便以后使用.希望也可以方便需要的 ...
- Storm之tickTuple
tickTuple是Storm中引入的一种定时机制,利用tickTuple能够实现间隔一段时间进行某种处理的逻辑. 在boltA中实现tickTuple注册的方法如下 @Override public ...
- Java中输入输出流
InputStream:所有字节输入流的所有类的超类. read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲数组b中 reset()将此流重新定位到最后一次对此流调用mark方法 ...
- 大数据Hadoop-2
大数据Hadoop学习之搭建Hadoop平台(2.1) 关于大数据,一看就懂,一懂就懵. 大数据的发展也有些年头了,如今正走在风口浪尖上,作为小白,我也来凑一份热闹. 大数据经过多年的发展,有着不同的 ...
- [NOI.AC省选模拟赛3.23] 集合 [数学]
题面 传送门 一句话题意: 给定$n\leq 1e9,k\leq 1e7,T\leq 1e9$ 设全集$U=\lbrace 1,2,3,...n\rbrace $,求$(min_{x\in S}\lb ...
- 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)
读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...