http://acm.hdu.edu.cn/showproblem.php?pid=5239
转:http://blog.csdn.net/zava_1087/article/details/46040747
任意数的平方,最多29次模上p,都会不变
#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 110000;
using namespace std;
typedef unsigned long long LL;
const LL mod = 9223372034707292160LL;
int n,q,cnt[maxn<<2];
LL sum[maxn<<2];
LL mulmod(LL x,LL y,LL mod)
{
LL ret = 0;
__asm__("movq %1,%%rax\n imulq %2\n idivq %3\n":"=d"(ret):"m"(x),"m"(y),"m"(mod):"%rax");
return ret;
}
void push_up(int rt)
{
sum[rt]=(sum[rt<<1]+sum[rt<<1|1])%mod;
cnt[rt]=min(cnt[rt<<1],cnt[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r){
scanf("%I64d",&sum[rt]);
cnt[rt]=0;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int L,int R,int l,int r,int rt)
{
if(cnt[rt]==30) return ;
if(l==r){
sum[rt]=mulmod(sum[rt],sum[rt],mod);
// sum[rt]=(sum[rt]*sum[rt])%mod;
cnt[rt]++;
return ;
}
int m=(l+r)>>1;
if(L<=m) update(L,R,lson);
if(m<R) update(L,R,rson);
push_up(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return sum[rt];
}
int m=(l+r)>>1;
LL ret=0;
if(L<=m)
ret=(ret+query(L,R,lson))%mod;
if(m<R)
ret=(ret+query(L,R,rson))%mod;
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
int T,cas=1;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
build(1,n,1);
LL sum=0;
printf("Case #%d:\n", cas++);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
sum=(sum+query(l,r,1,n,1))%mod;
update(l,r,1,n,1);
printf("%I64d\n",sum);
}
}
return 0;
}