UV 11610 Reverse Prime (number theory + tree array + binary)

Source: Internet
Author: User

Returns a reverse_prime, which is a 7-digit number and a prime number <= 1e6 after inversion.
First, find all such numbers.
Two operations
Q k: deletes the number K.
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 2657

First, all the reverse_prime values are pre-processed. In this case, all the prime numbers of <= 1e6 are obtained first.
Then we will reverse it. We can see that all prime numbers are 6 digits, but the question requires 7 digits. It can be seen that the first digit of the original number is 0. You can ignore this 0, and the efficiency may be faster.
Reverse all prime numbers and merge them into six digits. Calculate the number of prime factors for each number. Do not forget the value 0 at the end of the earlier calculation, that is, factor 2 and Factor 5.
For the statistical part, two tree arrays are created. The first one indicates the number of remaining values in the interval, and the second one indicates the number of factors in the interval and.
For the D operation, use map to record the subscript of a reverse_prime, and then update two tree arrays.
For the q operation, use the binary position and the first tree array to know the number of values in the interval. Finally, sum the values with the second tree array.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <map>
# Include <cstring>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <set>
# Include <string>
# Include <queue>
# Define inf 1600005
# Define M 40
# Define N 1000000
# Define maxn300005
# Define eps 1e-12
# Define zero (a) fabs (a) <eps
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mp (a, B) make_pair (a, B)
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL unsigned long
# Define MOD 1000000007.
# Define lson step <1
# Define rson step <1 | 1
# Define sqr (a) * ())
# Define Key_value ch [ch [root] [1] [0]
# Define test puts ("OK ");
# Define pi acos (-1.0)
# Define lowbit (x) (-(x) & (x ))
# Pragma comment (linker, "/STACK: 1024000000,1024000000 ")
Using namespace std;
Int flag [N] = {0}, prime [N], cnt = 0;
Int fac [N], a [N], tot = 0, p [N];
LL s1 [N], s2 [N];
Map <int, int> m;
Int slove (int num ){
Int len = 0, ret = 0, bit [20];
While (num ){
Bit [len ++] = num % 10;
Num/= 10;
}
For (int I = 0; I <len; I ++)
Ret = ret * 10 + bit [I];
While (ret <100000) ret * = 10;
Return ret;
}
Void Init (){
For (int I = 2; I <N; I ++ ){
If (flag [I]) continue;
Prime [++ cnt] = I;
For (int j = 2; j * I <N; j ++)
Flag [I * j] = 1;
}
For (int I = 1; I <= cnt; I ++ ){
A [I] = slove (prime [I]);
}
Sort (a + 1, a + 1 + cnt );
For (int I = 1; I <= cnt; I ++ ){
M [a [I] = I;
}
For (int I = 1; I <= cnt; I ++ ){
Fac [I] = 2; // removes the last 0, which must include two factors: 2, 5.
Int tmp = a [I];
For (int j = 1; j <= cnt & prime [j] * prime [j] <= tmp; j ++)
While (tmp % prime [j] = 0 ){
Tmp/= prime [j];
Fac [I] ++;
}
If (tmp> 1) fac [I] ++;
}
}
Void Update (LL * s, int x, int val ){
For (int I = x; I <= cnt; I + = lowbit (I ))
S [I] + = val;
}
LL sum (LL * s, int x ){
LL ret = 0;
For (int I = x; I> 0; I-= lowbit (I ))
Ret + = s [I];
Return ret;
}
Int main (){
// Freopen ("in.txt", "r", stdin );
Init ();
Char str [5]; int k;
Mem (s1, 0); mem (s2, 0 );
For (int I = 1; I <= cnt; I ++) s1 [I] = lowbit (I );
For (int I = 1; I <= cnt; I ++) Update (s2, I, fac [I]);
While (scanf ("% s % d", str, & k )! = EOF ){
If (str [0] = 'D '){
Int pos = m [k/10];
Update (s1, pos,-1 );
Update (s2, pos,-fac [pos]);
}
Else {
K ++; www.2cto.com
Int low = 1, high = cnt, mid;
While (low <= high ){
Mid = (low + high)> 1;
LL tmp = sum (s1, mid );
If (tmp = k) break;
If (tmp <k) low = mid + 1;
Else high = mid-1;
}
Printf ("% lld \ n", sum (s2, mid ));
}
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.