Cattle-off practice match 53 B delicious jelly

Links: https://ac.nowcoder.com/acm/contest/1114/B
Source: cattle off

Time limit: C / C ++ 2 seconds and 4 seconds languages other
space restrictions: C / C ++ 524288K, other languages 1048576K
64bit the IO the Format: LLD%

Title Description

 Due to the larger n tasty jelly, where n <= 3000000, only the value of this formula 1e9 + 7 modulo needs.

Enter a description:

A first line of input integer n. 1 <= n <= 3000000.

Output Description:

Output a integer answer.
Example 1

Entry

copy
3

Export

copy
22 

ideas:
This question is mainly to start the conversion process of this formula, which is more difficult to think, maybe we will do it directly with this formula, which would be difficult to do
the conversion process:
transform into
 
Reason: Why do you want to convert, if not converted, then i / j j as the denominator, and there are power j and j and inside the inner loop, so very easy to write programs, ideally j is outside in the loop variable,
Next, we have to analyze why this is so transformed
For I = . 1 I = . 1, there J = . 1 J = . 1
for I = 2 I = 2, there   J = 1,2
. . .
For I = n- I = n-, there   j = 1,2, ..., n
We found that i is greater than or equal j, then the conversion can be obtained of formula

Code:
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 3e6+10;
const ll mod = 1e9+7;
ll a[maxn];
ll div2 = 5e8+4;//2对于1e9+7的逆元
void init1(int k){
    for(int i=1;i<=k;i++)
        a[i] = 1;
} 

void init2(int k){
    for(int i=1;i<=k;i++){
        a[i] = (a[i]*i)%mod;
    }
}

ll f(ll n){
    ll sum = 0;
    init1(n);
    for(ll j = 1 ; j<=n;j++){
        init2(n/j);
        for(ll k = 1;k<=n/j;k++){
            ll mm = min(n,(k+1)*j-1);
            sum = (sum+((((mm-k*j+1+mod)%mod)*(k*j+mm)%mod)*div2%mod)*a[k]%mod)%mod;
            
        }
    }
    return sum%mod;
}
int main(){
    int n;
    cin>>n;
    cout<<f(n)<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lusiqi/p/11717742.html