"Simulation 20180307" Triple exclaim enumeration + tree array, 20180307 exclaim
Description
Given $ n, k $, how many tuples are required $ (a, B, c) $ meet $1 ≤ a ≤ B ≤ c ≤ n $ and $ a + B ^ 2 running c ^ 3 \ (mod \ k) $.
Input
The number of data groups in the first row $ T $.
There are two integers for each group of data: $ n $ and $ k $.
Output
$ T $ row, an integer in each line, indicates the number of three tuples that meet the condition.
Sample Input
1
10 7
Sample output
27
Data range
$1 ≤ n, k ≤ 10 ^ 5 $
$ T ≤ 400 $
Time limit $4 s $
Question
It's unfriendly to test with other schools and then do the questions ......
The data in this question is very characteristic (where is it very characteristic), $10 ^ 5 $ is obviously a symbolic number, it means $ O (n \ log \ n) $ is acceptable (such a large $ T $ is ignored ).
Naturally, this formula does not have any regularity (I am also helpless). We can consider enumerating $1 $ in $ a, B, and c $.
But which one should we choose? It is easy to think of, it should be $ c $, which has the highest number of times and is not easy to calculate.
Next, let's consider a simplified question. What should I do if I don't get the remaining $ k $?
For a number $ B $, because $1 ≤ a ≤ B $, obviously $ c ^ 3 $ is only available in $ [B ^ 2 + 1, B ^ 2 + B] $ is the only solution.
Therefore, each $ B $ can provide a solution for $ c ^ 3 $ in $ [B ^ 2 + 1, B ^ 2 + B] $, isn't that how to add a value to the interval? Tree array.
When you consider getting the remaining $ k $, you can do the same thing if you find the same situation. The only problem is that $ [B ^ 2 + 1, B ^ 2 + B] $ may exceed $ k $.
At this time, we can find that the length exceeds $ k $ and all regions are fully covered. Any $ c $ can use this $ B $. We only need one counter $ count $, $ \ left \ lfloor \ frac {B} {k} \ right \ rfloor $ is added each time.
Now, the solution to this question is coming soon. We enumerate $ c $ from small to large, first in the tree array $ (tree) $ [c ^ 2 + 1, c ^ 2 + c] $ is added with $1 $ and $ count $ is updated. The answer is equivalent to $ tree [c ^ 3 \ % k] + count $. Time Complexity: $ O (Tn \ log \ n) $ (ignore the size of $ T $ again)
$ Code: $
1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 # include <algorithm> 5 using namespace std; 6 # define N 100005 7 # define ll long 8 int T, n, mod; 9 ll ans, now, t [N]; 10 void update (int x, int v) 11 {12 for (int I = x; I <= mod; I + = I &-I) 13 t [I] + = v; 14} 15 ll getsum (int x) 16 {17 ll ans = 0; 18 for (int I = x; I-= I &-I) 19 ans + = t [I]; 20 return ans; 21} 22 int main () 23 {24 freopen ("exclaim. in "," r ", stdin); 25 freopen (" exclaim. out "," w ", stdout); 26 scanf (" % d ", & T); 27 for (int cas = 1; cas <= T; cas ++) 28 {29 scanf ("% d", & n, & mod); 30 ans = now = 0; 31 memset (t, 0, sizeof (t )); 32 for (int I = 1; I <= n; I ++) 33 {34 int l = (1ll * I + 1) % mod + 1, r = (1ll * I + I) % mod + 1; 35 if (l <= r) 36 update (l, 1), update (r + 1, -1); 37 else38 update (1, 1), update (r + 1,-1), update (l, 1 ); 39 int c = 1ll * I % mod; 40 now + = (I-1)/mod; 41 ans + = getsum (c + 1) + now; 42} 43 printf ("Case % d:", cas); 44 cout <ans; 45 putchar (10); 46} 47}View Code
Finally, I spoke about it: $ exclaim $ is not a triples, but it is a scream ...... I don't know why ......