zhengqingyuan's blog

By zhengqingyuan, history, 21 hour(s) ago, In English

Here are some useful conclution for naive algorithms to solve number theory problem,I hope you can know something about it and solve number theory problems more easily.

1.The number of prime factors of an intelliger

It's sure that the number of prime factors of an intelliger is very small,and an intelliger $$$v$$$ can be divided into $$$\log_2(v)$$$ primes.This can be used for bruteforce and State compression.

example:510D.

2.The number of factors of an intelliger

First of all,$$$\sum_{i = 1} ^ n \operatorname{d}(n) = \sum_{i = 1} ^ n [\frac{n}{i}] \approx n \ln n$$$.

Then I've found out that the number of factors of an intelliger($$$\operatorname{d}(n)$$$) is usually small,and to make sure,I made a code to get the maxinum number of the number of factors,and get:

  1. For $$$n \le 10 ^ 4,\max \operatorname{d}(n) <= 68$$$;
  2. For $$$n \le 5 \times 10 ^ 4,\max \operatorname{d}(n) <= 100$$$;
  3. For $$$n \le 10 ^ 5,\max \operatorname{d}(n) <= 128$$$;
  4. For $$$n \le 2 \times 10 ^ 5,\max \operatorname{d}(n) <= 160$$$;
  5. For $$$n \le 3 \times 10 ^ 5,\max \operatorname{d}(n) <= 180$$$;
  6. For $$$n \le 5 \times 10 ^ 5,\max \operatorname{d}(n) <= 200$$$;
  7. For $$$n \le 10 ^ 6,\max \operatorname{d}(n) <= 240$$$;
  8. For $$$n \le 5 \times 10 ^ 6,\max \operatorname{d}(n) <= 384$$$;
  9. For $$$n \le 10 ^ 7,\max \operatorname{d}(n) <= 448$$$;

So if your solution of a problem is $$$\operatorname{O}(n\max \operatorname{d}(a_i))$$$ or $$$\operatorname{O}(\sum \operatorname{d}(a_i))$$$,it might be correct because for $$$a_i \le 10 ^ 7$$$,it's sure that $$$\operatorname{d}(a_i) \le 500$$$.

examples:

3.Euler's Function: $$$\operatorname{O}(\log_2 n)$$$ times to $$$1$$$.

It's sure that $$$\phi(n) \le \frac{n}{2}$$$ for $$$2 | n$$$,and $$$2 | \phi(n)$$$ for $$$n > 1$$$.So if you use operation $$$x = \phi(x)$$$ for $$$x = n$$$ initially,it will become $$$1$$$ in $$$\operatorname{O}(\log_2 n)$$$ times.

example:906D.

Full text and comments »

  • Vote: I like it
  • +18
  • Vote: I do not like it

By zhengqingyuan, history, 4 days ago, In English

There are some bugs and features exists while compiling a c++ code,it looks weird but really exists on standard c++ compiler.I'll write down the bugs and features here to remind you to aware of them.

1.stl::vector:fail to use operator "=".

I was trying to implent the merge function of segment tree while solving the task,and the initial code is

here

and found that it will get WA on test #6 using the language C++14(GCC 6-32).Then I found out that the

code

might got wrong.That because the location of lc[u] changed after using lc.push_back() and the same as rc[u],so the operator "=" might not work.

The bug have been fixed since C++17(GCC 7-32),but if you compile your code using language C++ and the previous version before C++17(GCC 7-32),the bug still exists.

2.vector:Can vector be compiled by head file ?

I've participated on a school contest and found that a code with stl :: vector ran successfully without head file <vector>,after that,I made some testing and found something miraculous:

If the compiler is on Windows system (on codeforces,Dev c++ on Windows,etc),you can use <algorithm> instead of <vector> to compile stl :: vector,but if the compiler is on Linux system (Luogu,and so on),<algorithm> is unable to compile stl :: vector.Also, in Windows <iostream> is able to compile stl :: vector and it's invalid to use <iostream> to compile stl :: vector in Linux.So,for participants,especially for Chinese who is willing to engage on Olympics in informatics,please be aware of this.

For more such bugs and features,you can reply to the comment and I'll put some of them that sometimes exists but not well-known here.

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it

By zhengqingyuan, history, 11 days ago, In English

1.Introduction

Binary grouping is a way to implent dynamic insertion.If you found that the query for a data structure constructed by inserted elements from set $$$s$$$ can be answered online and be solved by uniting querys from a set of data structures built on a partition of $$$S$$$ in $$$O(h(n,m))$$$ as $$$m$$$ is the number of subsets in the partition and you can construct a data structure in $$$O(\operatorname{f}(n))$$$,It means that you can divide the elements in groups,for $$$n = \sum 2 ^ {a_i}(a_i > a_{i + 1})$$$,We can divide $$$n$$$ elements into groups,each group has $$$2^{a_k}$$$ elements,in this way we divided elements to $$$\log_2{n}$$$ groups.Then we build a data structure for each group of insertions using the construct algorithm.

When we insert a new element,we divide it in a group,and when we found a group that its size is the same as the new group,we merge the two groups into one group and rebuild a data structure with the elements in the group.Using the method,we get a way to implent dynamic insertion and the method to construct is $$$O(\sum {f(x)})(\sum x = n \log_2 n) \approx O(f(n)\log_2 n)$$$ in total and $$$O(h(n,\log_2 n))$$$ for each query.

2.Examples

It can be used for kd-tree,but I haven't found an example on the site,so please leave a comment if you found the example.

The example of using the method on kd-tree is P4148 on luogu,in the problem,the elements is the points in the first operation and we can use kd-tree to deal with the elements and answer the query online.But we have to implent dynamic insertion,so we use this method.

code

In CF710F,we use the method on AC-Automation.In the task,we use a group to deal with the inserted string and an another group for deleted strings.In both of the groups,we devide the strings into groups and deal with insertions using the method and make ACAM for each group. When we insert a string,we insert it to the group of inserted strings and insert it to the group of deleted string when we delete it.For querys,if $$$x_i$$$ means that How many string is concluded if we arrive index $$$i$$$ while running the ACAM,$$$y_i$$$ is how many string ends here in the trie,we get $$$x_i = y_i + x_{fail_i}$$$,and the way to get answer is:

  1. Run all the ACAMs in the group of inserted strings and add $$$x_i$$$ to answer while arriving index $$$i$$$.
  2. Run all the ACAMs in the group of deleted strings and minus answer by $$$x_i$$$ while arriving index $$$i$$$.

Here is the Accepted submission.

written at the end of the blog

We would like to thank:

  1. Masters who invent the method;
  2. zhengqingyuan edit the blog;
  3. unalive to give advice and make the blog better.
  4. adamant suggest a good problem as an example of using the trick.

Full text and comments »

  • Vote: I like it
  • +21
  • Vote: I do not like it