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 ...
随机推荐
- memcached简单介绍及在django中的使用
什么是memcached? Memcached是一个高性能的分布式的内存对象缓存系统,全世界有不少公司采用这个缓存项目来构建大负载的网站,来分担数据库的压力.Memcached是通过在内存里维护一个统 ...
- Linux SPI总线和设备驱动架构之二:SPI通用接口层
通过上一篇文章的介绍,我们知道,SPI通用接口层用于把具体SPI设备的协议驱动和SPI控制器驱动联接在一起,通用接口层除了为协议驱动和控制器驱动提供一系列的标准接口API,同时还为这些接口API定义了 ...
- C++陷阱系列:让面试官倒掉的题
http://blog.chinaunix.net/uid-22754909-id-3969535.html 今天和几位同仁一起探讨了一下C++的一些基础知识,在座的同仁都是行家了,有的多次当过C++ ...
- epc笔记
http://wenku.baidu.com/view/5e921520dd36a32d7375812a.html 1. 先注册, EPC注册EPS业务或non-EPS服务 ?? HSS做什么? 2 ...
- delphi保存文件的命名规则
没有固定的标准.自己可以定义 .你可以参考PASCAL命名法则.查一下PASCAL命名. 我习惯用UMain,FMain,UDM,DM,UAboutBox,AboutBox.....程序相关内容都放在 ...
- 【bzoj1878】[SDOI2009]HH的项链 树状数组
题目描述 HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变得越来越长.有一 ...
- How to Create a Perl Based Custom Monitor on NetScaler
How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...
- SQL语言:结构化查询语言
SQL语言:结构化查询语言 程序员或者DBA(数据库管理员)使用SQL和DBBSM进行交互,操纵数据库中的资源 分类: 1.DDL 数据定义语言 结构 create 创建 database ta ...
- 【ZJ选讲·压缩】
给一个由小写字母组成的字符串(len<=50) 我们可以用一种简单的方法来压缩其中的重复信息. 用M,R两个大写字母表示压缩信息 M标记重复串的开始, R表示后面的一段字符串重复从上一个 ...
- JSOI2004 平衡点 / 吊打XXX [模拟退火]
题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到 ...