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. [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?

    原文来源:https://stackoverflow.com/questions/1257592/how-do-i-remove-files-saying-old-mode-100755-new-mo ...

  2. 什么是http?

    http请求流程: http课程链接:http://www.imooc.com/video/6712/0

  3. 【bzoj4750】密码安全 单调栈

    题目描述 模10^9+61 输入 第一行包含一个正整数 T ,表示有 T 组测试数据. 接下来依次给出每组测试数据.对于每组测试数据: 第一行包含一个正整数 n . 第二行包含 n 个非负整数,表示 ...

  4. hdu 1053 Entropy (哈夫曼树)

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. [CF1066C]Books Queries

    题目大意:维护一个数列,要求在左边插入一个数,在右边插入一个数,查询一个数的排名 题解:可以双指针,开个数组存每个数的位置 卡点:无 C++ Code: #include <cstdio> ...

  6. nc用法小记

    By francis_hao    Jun 30,2017   ncat:连接和重定向套接字 概要 ncat [OPTIONS...] [hostname] [port]   描述 ncat 是一个集 ...

  7. B. Minimum Ternary String (这个B有点狠)

    B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. HDU1828 Picture 线段树+扫描线模板题

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. oracle的sequece的使用(主键自增长)

    在Oracle数据库中,sequence等同于序列号,每次取的时候sequence会自动增加,一般会作用于需要按序列号排序的地方. 1.Create Sequence (注释:你需要有CREATE S ...

  10. 前端面试:js闭包,为什么要使用闭包

    要理解闭包,首先理解javascript特殊的变量作用域,变量的作用于无非就是两种:全局变量,局部变量. javascript语言的特殊处就是函数内部可以读取全局变量. 1.如何从外部读取局部变量? ...