Jupiter Atacks!

/**
题意: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的更多相关文章

  1. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  2. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  5. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  8. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  9. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

随机推荐

  1. ng2 搭建本地开发环境

    git clone https://github.com/angular/quickstart.git quickstart cd quickstart npm install npm start h ...

  2. Python两个内置函数——locals 和globals (学习笔记)

    这两个函数主要提供,基于字典的访问局部和全局变量的方式.在理解这两个函数时,首先来理解一下python中的名字空间概念.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个字典,它的键 ...

  3. Mac下使用Charles抓包Android

    原文地址:http://fanjiajia.cn/2018/11/21/Mac%E4%B8%8B%E4%BD%BF%E7%94%A8Charles%E6%8A%93%E5%8C%85Android/ ...

  4. C#中的Stack的Peek操作,曝出异常

    我们在遍历一个栈的时候,有时候需要判断栈顶元素,用到了Peek元素,然后再用Pop元素,但是这个时候会出现一个逻辑错误, 当用Pop删除全部栈的元素时,再用Peek就会报错, InvalidOpera ...

  5. 项目常用解决方案之SystemSetting.xml文件的修改与读取

    Winform及WPF项目中经常会用到类似SystemSetting.xml等类似的文件用于保存CLIENT的数据,比如登录过的用户名或密码以及其他设置.所以就想到一个解决方法,可以用到所有有此需求的 ...

  6. css 给body设置背景图片

  7. sql语句编写 有时候一个子查询可以拆分成多个子查询

    sql语句编写 有时候一个子查询可以拆分成多个子查询

  8. error C3872: '0x3000': this character is not allowed in an identifier 解决方法

    文章参考地址:http://blog.csdn.net/danxuezx/article/details/5096497 编译时遇到这个错误多半是从网上拷贝一段代码到VS里然后编译时产生的,这是因为拷 ...

  9. Codeforces Round #526 (Div. 2) C. The Fair Nut and String

    C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...

  10. c++ fstream用法(2)

    一> #include "stdafx.h" #include<iostream> #include<string> #include<fstr ...