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. NO11——01背包

    # include <stdio.h> # include <stdlib.h> # include <string.h> # define max(x,y) x& ...

  2. 查看lwjgl常用状态的值

    在遇到状态值较多较复杂的情况,可以选择使用GL11.glIsEnabled()或者GL11.glGet()函数来查看状态机值,以下是常用值: public static void printOpenG ...

  3. Jboss6内存修改

    1.启动脚本:/home/jboss/jboss-eap-6.2/bin/standalone.sh -Djboss.bind.address.management=192.168.0.62 -Djb ...

  4. 实现AJAX跨域访问方式一

    1.添加pom依赖 <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId ...

  5. [Leetcode] text justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. MySQL 创建一个简单的成绩管理系统

    操作过程使用实验楼. 首先是创建一个数据库studentsystem,使用语句是: CREATE DATABASE studentsystem;  查看创建好的数据库的命令还是SHOW DATABAS ...

  7. 【BZOJ 2753 滑雪与时间胶囊】

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2843  Solved: 993[Submit][Status][Discuss] Descripti ...

  8. [学习笔记]LCT进阶操作

    LCT总结——应用篇(附题单)(LCT) 一般都是维护链的操作.split即可搞定. 进阶操作的话,处理好辅助树和原树的关系即可搞定. 其实,最大的区别就是,splay随便转,辅助树形态变了,但是原树 ...

  9. 【bzoj2141】排队 [国家集训队2011]排队(魏铭) 树套树 线段树套替罪羊树

    这个题就是动态偏序对,每次操作做两个删除两个插入就好了. #include<cstdio> #include<iostream> #include<cstring> ...

  10. SDOI 2009 学校食堂 状压dp

    这个题的关键处1 紧跟着他的bi个人 —— 由此得出任意一个状态都可以表示为 有第一个人没吃到饭做分隔的前面所有人已吃饭,并用1<<8表示之后的(包括他)的八个人的状态2 信息仍然是上一个 ...