[hdu5439 aggregated counting] formulation simple, pre-treatment

Source: Internet
Author: User

Test instructions: Generates a set of sequences according to the following rules, so that f (n) is n the last position in the sequence where the number appears, and the value of F (f (n)) is obtained.

1. First, write down 1, 2 on a paper.
2. The 2nd number is 2 and write down 2 2 ' s (including the one originally on the paper). The paper thus have 1, 2, 2 written on it.
3. The 3rd number is 2 and write down 2 3 ' s. 1, 2, 2, 3, 3 are now shown on the paper.
4. The 4th number is 3 and write down 3 4 ' s. 1, 2, 2, 3, 3, 4, 4, 4 are now shown on the paper.
5. The procedure continues indefinitely as you can imagine. 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, ....

Idea: Make the original sequence a, according to the definition and sequence of the generation rules can be introduced:

    • F (n) equals the first n of a and
    • F (n) is the last position where n is the number that appears in a

F (f (n)) means: The first m term of a and, M is n in the last occurrence of a position. So the formula F (f (n)) can be written as:

F (f (n)) =1 + (2+3) * + (4+5) + (6+7+8) + + ... + (... +n) *t

T is far less than N, about hundreds of thousands of of the appearance, the rest will not say more ...

#pragma COMMENT (linker, "/stack:10240000")#include <bits/stdc++.h>using namespace std;#define X First#define Y Second#define PB Push_back#define MP Make_pair#define ALL (a) (a). Begin (), (a). End ()#define FILLCHAR (A, X) memset (A, X, sizeof (a))typedef Long Long ll;typedef pair<int, int> PII;namespace Debug {void print () {cout<<Endl;}Template<TypeName T>void Print (Const T t) {cout<<T<<Endl;}Template<TypeName F,TypeName... R>void Print (Const F F,Const R...R) {cout<<F<<" ";p rint (R ...);}Template<TypeName T>void Print (T*p, T*q) {int D=P<Q?1:-1; while(P!=q) {cout<<*P<<", ";p+=D;} cout<<Endl;}}Template<TypeName T>BOOL Umax (T&A, Const T&b) {return b<=a?false:(A=b,true);}Template<TypeName T>BOOL Umin (T&A, Const T&b) {return b>=a?false:(A=b,true);}/* -------------------------------------------------------------------------------- */Const int MAXN = 1e9 + 7;Const int MoD = MAXN;int seq[600000] = {0, 1, 2, 2};ll sum[600000] = {0, 1, 3, 5}, ans[600000];int  Total = 3;void Init() {     for (int I = 3; ; I ++) {         for (int J = 0; J < Seq[i]; J ++) {            seq[++ Total ] = i;            Sum[total] = Sum[total - 1] + Seq[total];        }        if (Sum[total] > MAXN)  Break;    }    ans[1] = 1;     for (int I = 2; I < Total ; I ++) {        Ans[i] = (Ans[i - 1] + (Sum[i] + Sum[i] - Seq[i] + 1) * Seq[i] / 2 % MoD * i) % MoD;    }}int Main() {#ifndef Online_judge    Freopen ("In.txt", "R", stdin);    //freopen ("OUT.txt", "w", stdout);#endif//Online_judge    int T, N;    init ();    Cin >> T;     while (T --) {        Cin >> N;        int P = upper_bound (Sum + 1, sum + Total , N) - sum - 1;        cout << (Ans[p] + (Sum[p] + 1 + N) * (n - Sum[p]) / 2 % MoD * (P + 1)) % MoD << Endl;    }}

[hdu5439 aggregated counting] formulation simple, pre-treatment

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.