Giter Club home page Giter Club logo

ac-library's Introduction

AC(AtCoder) Library

AC Library is the official library of AtCoder. This repository manages the contents of AC Library.

You may refer to the following links for more details:

The documents of master branch are as follows:

Policy

Our goal of this project is to achieve that

  • Enable every AtCoder users to use this library with minimum efforts of studying about PC
  • Maximize convenience for the usage in competitive programming. We completely ignore other usages.
  • No bugs. This is a fantasy, but we pursue this.

By this policy, we ignore some manners of C++ intentionally. For example,

  • we don't use size_t, but use int.
  • Segtree handles function pointers, not functional objects.
  • and so on...

Direction of this project

We haven't decided whether we should increase this library's contents or not because there are pros and cons. If you are interested in this topic, please join the discussion in The Announcement on Codeforces.

For now, we are not planning to add new features, and we use this repository only for

  • collecting issues
  • recording the changelogs
  • versioning our releases

Contributing

We accept issues/PRs only from AtCoder users.

We would appreciate it if you would report our mistakes like a typo, or, more importantly, bugs!

As mentioned above, we haven't decided which way to go. Therefore we are not accepting feature requests for now, and issues will be closed.

Releases

You can view the newest version of AC Library in The Announcement on AtCoder page.

You can also see all versions in The Release page.

License

This library is released under the CC0, except for the third-party libraries that are located under /document_(en|ja)/lib directory. Please refer /document_en/lib/LICENSE.md for details.

ac-library's People

Contributors

beet-aizu avatar cupro29 avatar hitonanode avatar keiichihirobe avatar koba-e964 avatar misawa avatar monkukui avatar polyomino24 avatar rsk0315 avatar rururia-kyopro avatar seastar105 avatar ssrs-cp avatar tockrock avatar tonalidadehidrica avatar tumoiyorozu avatar yosupo06 avatar yu-yama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ac-library's Issues

Declaring several methods in atcoder::segtree as const

I want to append const to several method declarations in atcoder::segtree.

Some complicated DP problems require to maintain two segtrees to improve time complexity. In such situations, coders always have to pay attention to them. If one of them is not modifed at all (used like (disjoint) sparse tables), I want to declare it as const to reduce the risk of updating the wrong segtree.

In current ACL, all atcoder::segtree methods are not declared as const, so we cannot declare the instances as const when there is no update query. On the other hand, it seems that the methods get(), prod(), all_prod(), max_right() and min_left() do not affect any member variables. I suggest that these methods have const like this to enable such usage of atcoder::segtree.

What do you think of this modification?

Incorrect comment in math?

// -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1)

I think this should be:

x*u0*g % (u1*g) = (r1-r0) % (u1*g) (u0*g = m0, u1*g = m1)

For example, when (r0, r1) = (4, 0) and (m0, m1) = (5, 3), we have (x, g, u0, u1) = (-2, 1, 5, 3). So x*u0*g % (u1*g) is -1 (or 2 if we consider non-negative mod), but (r1-r0) is -4.

Anyway, the code itself seems correct.

Documentation for MacOS (Visual Studio specifically)

The documentation currently have instructions for Visual Studio on Windows (see path in the doc with C drive, I don't know where the include folder is in macOS case). I guess most of the programmers use Windows and hence the lack of documentation on MacOS. I had tried to set it up a few weeks back and had a great trouble in getting it compiled in Visual studio. (Command line compile is fine but visual studio doesn't see the library).

It will be helpful for macOS users.

Add explicit for some constructors?

The following code works because we don't add explicit for the constructor.

segtree<...> seg = 10;

Should we add explicit for some constructors for more intuitive behaviors?

UIkit's url is wrong

    <link rel="stylesheet" href="https://judge.yosupo.jp/public/css/uikit.min.css" />
    <script src="https://judge.yosupo.jp/public/js/uikit.min.js"></script>
    <script src="https://judge.yosupo.jp/public/js/uikit-icons.min.js"></script>

I just copy-pasted those lines and forgot to fix...

Max Flow doesn't handle v-v flow as documented

By definition in the document appendix.html, it should return 0 I believe.

g(v, f) = Sum{...} - Sum{...}

This implies the sum of g(v, f) over all vertices is zero.

g(v, f) = g(v, f') holds for all vertices v other than s and t.

Since s = t, this also implies g(t, f) = g(t, f'). (∵ everywhere except t is fixed, and the sum is fixed)

g(t,f′)−g(t,f) is maximized under these conditions. It returns this g(t,f′)−g(t,f)

Since g(t, f) = g(t, f'), the returned value should be 0.
Though, the actual implementation returns inf or the third argument (limit),

The current return value also makes sense if you view the problem as an instance of MaxFlow formulated as LP. You'll most likely get a primal unbounded, dual infeasible instance, depending on the choose of formulation.

Since there are multiple return values that makes some sense, both fix (fix documentation / fix implementation) would still leave confusion. So it might be better to assert(s != t) and document it.

https://github.com/rust-lang-ja/ac-library-rs/pull/24/files#r485363476

docs: add summarized (human-friendly) description to "crt"

Mathのcrtにある説明から常人が「**の剰余定理」という名称(あるいはCRTが何の略か)に辿り着くには(コンテストの成績を左右するには十分に長い)時間が掛かるのではないかと思います。

crt

pair<ll, ll> crt(vector<ll> r, vector<ll> m)

同じ長さの配列 $r, m$ を渡します。この配列の長さを $n$ とした時、

$$x \equiv r[i] \pmod{m[i]}, \forall i \in \lbrace 0,1,\cdots, n - 1 \rbrace$$

を解きます。答えは(存在するならば) $y, z (0 \leq y &lt; z = \mathrm{lcm}(m[i]))$ を用いて $x \equiv y \pmod z$ の形で書けることが知られており、この $(y, z)$ をpairとして返します。答えがない場合は $(0, 0)$ を返します。$n=0$ の時は $(0, 1)$ を返します。

wrong links in index.md

All #include <atcoder/...> links in the index.md lead to something.html, while it should be something.md I guess.

Deduplicate segments with the same tangent properly on mcf_graph::slope

This line doesn't seem to be correct me. Instead, we should update this variable by d.
Also, it would be better to name it cost_per_flow or something like that to avoid confusion.

prev_cost = cost;

A test case would be

TEST(MincostflowTest, SameCostPaths) {
    mcf_graph<int, int> g(3);
    ASSERT_EQ(0, g.add_edge(0, 1, 1, 1));
    ASSERT_EQ(1, g.add_edge(1, 2, 1, 0));
    ASSERT_EQ(2, g.add_edge(0, 2, 2, 1));
    auto expected = std::vector<std::pair<int, int>>{{0, 0}, {3, 3}};
    ASSERT_EQ(expected, g.slope(0, 2));
}

License of Documentations

Can we use the documents under document_ja/ and document_en/ library in ports to other languages? If possible, under which license shall we use?

A constructor, static_modint(bool), isn't needed

In now, we can compile the following code, but it is confusing.

using mint = modint998244353;

mint x = 2.0; // static_modint(bool) is called
assert(x == mint(1)); // because bool(2.0) -> true -> 1

License of expander.py

May I use/modify the expander.py for my personal competitive programming library? If so, under which license shall I use it?

Add BigInt

Please consider adding a BigInt library containing basic arithmetic operations to work with BigInts.

Its implementation is already present in many other languages like Python and Java. So I believe, C++ users also deserve easy access to BigInts.

Time complexity of floor_sum

The document says O(log(m+a)), but now (by #92) m+a may be negative or zero. I suggest O(log(a mod m)).

By the way, maybe, its complexity could be bounded by O(log(min(n, a mod m))). It might be wrong because I guessed just intuitively (and empirically). So I'm glad if anyone can analyze this.

Minimum cost flow algorithm

Hello.

Is there any place I can learn about the minimum cost flow algorithm which is in the library?

Edit: Actually, it seems to be the same algorithm in this topcoder tutorial. However, they claim the complexity is O(nmFlogn) instead of O(F(n+m)logn). Which one should I trust? xD

Provide everything combined file

We investigated reactions for this library.

It is appeared that includuding header files and running expander.py needs the knowledge about PC.

We should provide the everything combined header file so that we can use AC Library by just copy-paste it to the top of main.cpp.

scc_graph may cause stack overflow

#include <atcoder/all>
#include <iostream>

using namespace std;
using namespace atcoder;

const int BIG = 9'000'000; // Constraints 0 <= n <= 10^8

int main() {
    scc_graph g(BIG);
    for (int i = 0; i < BIG - 1; i++)
    {
        g.add_edge(i, i + 1);
    }
    auto scc = g.scc();
    cout << scc.size() << endl;

    return 0;
}

English:
When Run the code on Custom Test, it exit with exit code 139, which probably a segmentation fault due to stack overflow.

日本語:
コードテストで上記のコードを実行すると終了コード139(おそらくスタックオーバーフローによるセグメンテーション違反)で終了します。

Convolution(empty, long_array) take O(N) time

vector<modint998244353> a(0), b(1_000_000);

atcoder::convolution(a, b); // it consumes long time!

We shouldn't run copy-constructor when convolution(empty, long_array) is called

License of test cases

Can we use the test cases in the test/ folder as the tests of foreign-language port of this library? If possible, under what license shall we use?

Unclear comment in math?

m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b

If this comment is about the range of m0, I think this would be suitable:

|m0 - (m1 * u)| <= |m0| + |m1 * u|
<= |m0|*t +|m1|*s  (since t >= 1)
<= b

Otherwise, I might not have understood the comment correctly, sorry.

Self loop handling

Reporter: Mi_Sawa (Japanese tweet: https://twitter.com/Mi_Sawa/status/1303170874938331137)

max_flow / min_cost_flow cannot handle self-loops "correctly".

This is the code of max_flow.add_edge(). We can notice that we cannot handle an id of reverse edge correctly.
In now, in spite of this code wrong, everything goes well. However, cleary we should fix this.

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        g[from].push_back(_edge{to, int(g[to].size()), cap});
        g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});
        return m;
    }

documents of segtree.min_left and lazysegtree.min_left are wrong

https://github.com/atcoder/ac-library/blame/master/document_en/segtree.md#L155
https://github.com/atcoder/ac-library/blame/master/document_ja/segtree.md#L159
https://github.com/atcoder/ac-library/blame/master/document_en/lazysegtree.md#L165
https://github.com/atcoder/ac-library/blame/master/document_ja/lazysegtree.md#L166

English:
I guess g(op(a[l - 1], a[l + 1], ..., a[r - 1])) = false should be g(op(a[l - 1], a[l], ..., a[r - 1])) = false.

Japanese:
g(op(a[l - 1], a[l + 1], ..., a[r - 1])) = falseg(op(a[l - 1], a[l], ..., a[r - 1])) = falseの誤りではないでしょうか。

`#include <intrin.h>` is missing from internal_math.hpp

Visual Studio causes an error '_umul128': 識別子が見つかりませんでした when using #include<atcoder/math>.
The following three lines may be needed in internal_math.hpp.

#ifdef _MSC_VER
#include <intrin.h>
#endif

generate_document.py breaking in python version 3.9

I had cloned the ACL repository and I tried to convert all the markdown files into HTML files using the generate_document.py script which is given in the /tools directory when I ran into this error:

❯ ./generate_document.py
12:45:37 [INFO] start converting, lang=en
12:45:37 [INFO] convert ../document_en/twosat.md
Traceback (most recent call last):
File "/home/tusky/Tushar/ac-library/tools/./generate_document.py", line 89, in
statement = convert(open(md_file).read(), base_dir, opts.tag)
File "/home/tusky/Tushar/ac-library/tools/./generate_document.py", line 50, in convert
if tag == 'production' or re.match(r'v[0-9].[0-9]', tag):
File "/usr/lib/python3.9/re.py", line 191, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object

After a little bit of googling, I was brought to an answer on Stack Overflow which urged me to wrap the variable tag with the str function which seemed to do the trick.

Thus changing line 50 of generate_document.py to

if tag == 'production' or re.match(r'v[0-9]\.[0-9]', str(tag)):

seems to do the trick and I can now see the desired output:

❯ ./generate_document.py
12:50:49 [INFO] start converting, lang=en
12:50:49 [INFO] convert ../document_en/twosat.md
12:50:49 [INFO] read example: twosat_practice
12:50:49 [INFO] convert ../document_en/dsu.md
12:50:49 [INFO] read example: dsu_practice
12:50:49 [INFO] convert ../document_en/maxflow.md
12:50:49 [INFO] read example: maxflow_practice
12:50:49 [INFO] convert ../document_en/appendix.md
12:50:49 [INFO] convert ../document_en/scc.md
12:50:49 [INFO] read example: scc_practice
12:50:49 [INFO] convert ../document_en/index.md
12:50:49 [INFO] convert ../document_en/convolution.md
12:50:49 [INFO] read example: convolution_int_practice
12:50:49 [INFO] read example: convolution_practice
12:50:49 [INFO] convert ../document_en/lazysegtree.md
12:50:49 [INFO] read example: lazyseg_practice1
12:50:49 [INFO] read example: lazyseg_practice2
12:50:49 [INFO] convert ../document_en/modint.md
12:50:49 [INFO] read example: modint_usage
12:50:49 [INFO] convert ../document_en/fenwicktree.md
12:50:49 [INFO] read example: fenwick_practice
12:50:49 [INFO] convert ../document_en/string.md
12:50:49 [INFO] read example: sa_usage
12:50:49 [INFO] read example: sa_practice
12:50:49 [INFO] convert ../document_en/segtree.md
12:50:49 [INFO] read example: segtree_practice
12:50:49 [INFO] convert ../document_en/mincostflow.md
12:50:49 [INFO] read example: mincostflow_practice
12:50:49 [INFO] convert ../document_en/math.md
12:50:49 [INFO] read example: floor_sum_practice
12:50:49 [INFO] start converting, lang=ja
12:50:49 [INFO] convert ../document_ja/twosat.md
12:50:49 [INFO] read example: twosat_practice
12:50:49 [INFO] convert ../document_ja/dsu.md
12:50:49 [INFO] read example: dsu_practice
12:50:49 [INFO] convert ../document_ja/maxflow.md
12:50:49 [INFO] read example: maxflow_practice
12:50:49 [INFO] convert ../document_ja/appendix.md
12:50:49 [INFO] convert ../document_ja/scc.md
12:50:49 [INFO] read example: scc_practice
12:50:49 [INFO] convert ../document_ja/index.md
12:50:49 [INFO] convert ../document_ja/convolution.md
12:50:49 [INFO] read example: convolution_int_practice
12:50:49 [INFO] read example: convolution_practice
12:50:49 [INFO] convert ../document_ja/lazysegtree.md
12:50:49 [INFO] read example: lazyseg_practice1
12:50:49 [INFO] read example: lazyseg_practice2
12:50:49 [INFO] convert ../document_ja/modint.md
12:50:49 [INFO] read example: modint_usage
12:50:49 [INFO] convert ../document_ja/fenwicktree.md
12:50:49 [INFO] read example: fenwick_practice
12:50:49 [INFO] convert ../document_ja/string.md
12:50:49 [INFO] read example: sa_usage
12:50:49 [INFO] read example: sa_practice
12:50:49 [INFO] convert ../document_ja/segtree.md
12:50:49 [INFO] read example: segtree_practice
12:50:49 [INFO] convert ../document_ja/mincostflow.md
12:50:49 [INFO] read example: mincostflow_practice
12:50:49 [INFO] convert ../document_ja/math.md
12:50:49 [INFO] read example: floor_sum_practice

Use #include "..." instead of #include <...> to include internal

To include headers from other headers in the same library, we should use #include "...". This is a common manner (examples: 1, 2, 3).

However, the current AtCoder Library uses #include <...> (e.g. https://github.com/atcoder/ac-library/blob/114e690ade7fe839db3ea0e5f169207672ef0886/atcoder/all).
#include <...> is for headers in the standard system directories (see https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html), but AtCoder Library is not system's one.

IIUC, if you use #include <...>, a problem will happen when multiple versions of the this library are installed.
For example, let say we have /usr/include/atcoder/segtree.hpp, /home/user/kimiyuki/GitHub/ac-library/atcoder/segtree.hpp, and /home/user/kimiyuki/GitHub/ac-library/atcoder/all. Under such situation, even when you include /home/user/kimiyuki/GitHub/ac-library/atcoder/all with #include "atcoder/all", this atcoder/all ignores /home/user/kimiyuki/GitHub/ac-library/atcoder/segtree.hpp and includes /usr/include/atcoder/segtree.hpp. This will cause mysterious bugs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.