标签:void ret oom miss 求逆 strong str content integer
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1155 Accepted Submission(s): 401
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<queue> 7 #include<stack> 8 using namespace std; 9 typedef long long LL; 10 const LL mod = 1e9+7; 11 LL quick(LL n,LL m); 12 typedef struct node 13 { 14 int l; 15 int r; 16 int id; 17 } ss; 18 ss ask[300005]; 19 LL cnt[300005]; 20 int ans[300005]; 21 LL N[300005]; 22 LL Ni[300005]; 23 LL ck[300005]; 24 bool cmp(node p,node q) 25 { 26 return p.l < q.l; 27 } 28 bool cmp1(node p,node q) 29 { 30 return p.r < q.r; 31 } 32 void slove_mo(int n,int m); 33 int main(void) 34 { 35 int T; 36 int i,j; 37 N[0] = 1; 38 Ni[0] = 1; 39 for(i = 1; i <= 300000; i++) 40 { 41 N[i] = N[i-1]*(LL)i%mod; 42 Ni[i] = quick(N[i],mod-2); 43 } 44 scanf("%d",&T); 45 while(T--) 46 { 47 int n,m; 48 scanf("%d %d",&n,&m); 49 memset(cnt,0,sizeof(cnt)); 50 for(i = 1; i <= n; i++) 51 { 52 scanf("%d",&ans[i]); 53 } 54 for(i = 0; i < m; i++) 55 { 56 scanf("%d %d",&ask[i].l,&ask[i].r); 57 ask[i].id = i; 58 } 59 sort(ask,ask+m,cmp); 60 int ak = sqrt(1.0*n)+1; 61 int v = ak; 62 int id = 0; 63 for(i = 0; i < m; i++) 64 { 65 if(ask[i].l > v) 66 { 67 v+=ak; 68 sort(ask+id,ask+i,cmp1); 69 id = i; 70 } 71 } 72 sort(ask+id,ask+m,cmp1); 73 slove_mo(n,m); 74 for(i = 0; i < m; i++) 75 { 76 printf("%lld\n",ck[i]); 77 } 78 } 79 return 0; 80 } 81 void slove_mo(int n,int m) 82 { 83 int xl = ask[0].l; 84 int xr = ask[0].r; 85 int i,j; 86 LL sum = xr-xl+1; 87 LL ak = 1; 88 for(i = xl; i <= xr; i++) 89 { 90 ak = ak*Ni[cnt[ans[i]]]%mod; 91 cnt[ans[i]]++; 92 ak = ak*N[cnt[ans[i]]]%mod; 93 } 94 sum = N[sum]*quick(ak,mod-2)%mod; 95 ck[ask[0].id] = sum; 96 for(i = 1; i < m; i++) 97 { 98 sum = ask[i].r-ask[i].l+1; 99 while(xr > ask[i].r) 100 { 101 ak = ak*Ni[cnt[ans[xr]]]%mod; 102 cnt[ans[xr]]--; 103 ak = ak*N[cnt[ans[xr]]]%mod; 104 xr--; 105 } 106 while(xr < ask[i].r) 107 { 108 xr++; 109 ak = ak*Ni[cnt[ans[xr]]]%mod; 110 cnt[ans[xr]]++; 111 ak = ak*N[cnt[ans[xr]]]%mod; 112 } 113 while(xl < ask[i].l) 114 { 115 ak = ak*Ni[cnt[ans[xl]]]%mod; 116 cnt[ans[xl]]--; 117 ak = ak*N[cnt[ans[xl]]]%mod; 118 xl++; 119 } 120 while(xl > ask[i].l) 121 { 122 xl--; 123 ak = ak*Ni[cnt[ans[xl]]]%mod; 124 cnt[ans[xl]]++; 125 ak = ak*N[cnt[ans[xl]]]%mod; 126 } 127 ck[ask[i].id] = N[sum]*quick(ak,mod-2)%mod; 128 }return ; 129 } 130 LL quick(LL n,LL m) 131 { 132 LL ask = 1; 133 n%=mod; 134 while(m) 135 { 136 if(m&1) 137 ask = ask*n%mod; 138 n = n*n%mod; 139 m>>=1; 140 } 141 return ask; 142 }
标签:void ret oom miss 求逆 strong str content integer
原文地址:http://www.cnblogs.com/zzuli2sjy/p/6029790.html