HDU 4059 the Boss on Mars (Repulsion principle)

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=4059


Test instructions

Given a number n, the number of four-square and N-mutually exclusive numbers that are less than n are obtained.


Analysis:

We can find out all the number of 1~n from the four of the square and the sum1, and then to find out 1~n all and n is not mutually exclusive number of four-square and sum2;

Ans =sum1-sum2;

Set f (n) to denote all the numbers from the 1~n four-th square and F (n) =1/30*n* (n+1) (2n+1) (3n^2+3n-1);

Tear down as follows:

(n+1) ^5-n^5=5n^4+10n^3+10n^2+5n+1

n^5-(n-1) ^5=5 (n-1) ^4+10 (n-1) ^3+10 (n-1) ^2+5 (n-1) +1

(n-1) ^5-(n-2) ^5=5 (n-2) ^4+10 (n-2) ^3+10 (n-2) ^2+5 (n-2) +1

...

...

...

2^5-1^5=5*1^4+10*1^3+10*1^2+5*1+1

The formula that needs to be used is

1^2+2^2+...+n^2=n* (n+1) * (2*n+1)/6;

1^3+2^3+...+n^3=n^2* (n+1) ^2/4;

And then they all add up, and by doing this, you can get f (n).


Because we're going to modulo, and there's division, so we need to find the inverse.

A^b=1 (mod m)

Here are two ways of doing this

1)

Since 30 and 1000000007 are mutually exclusive, it is known by the Fermat theorem that this is the inverse of (30^ (mod-2))% (mod);

2)

A/b mod m = (a mod (m*b))/m;


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int mod = 1000000007;typedef long long ll;int cnt=0;int a[50];    LL n,inv;void Fen (int n)//factor decomposition {cnt=0;            for (int i=2;i*i<=n;i++) {if (n%i==0) {a[cnt++]=i;        while (n%i==0) n/=i; }} if (n>1) a[cnt++]=n;}    ll multi (ll a,ll b,ll m) {ll ans = 0;            while (b) {if (b&1) {ans = (ans + a)%m;        b--;        } b>>=1;        a=a*2;    if (a>n) a%=m; } return ans;    ll Quick_mod (ll a,ll b,ll m) {ll ans = 1;            while (b) {if (b&1) {ans=multi (ans,a,m);        b--;        } b>>=1;    A=multi (A,A,M); } return ans;    ll F (ll N)//four times Square and formula: 1/30*n* (n+1) (2n+1) (3n^2+3n-1) {ll ans=n;    Ans= (ans* (n+1))%mod;    Ans= (ans* (2*n+1))%mod;    Ans= (ans* ((3*n*n+3*n-1)%mod))%mod;    Ans= (ANS*INV)%mod; return ans;}    ll solve (ll N) {Fen (n);    LL ans = 0;        for (int i=1;i< (1<<cnt); i++) {LL tmp = 1;        int tt = 0;                for (int j=0;j<cnt;j++) {if ((1<<j) &i) {tmp=tmp*a[j];            tt++;        }} LL q=n/tmp;        LL T = multi (multi (TMP,TMP,MOD), multi (tmp,tmp,mod), MoD);        if (tt&1) ans= (Ans+multi (f (q), t,mod))%mod;    Else ans= (Ans-multi (f (q), t,mod) +mod)%mod; } return ans;    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%i64d", &n);            if (n==1) {puts ("0");        Continue        INV = Quick_mod (30,MOD-2,MOD);        LL tmp = f (n);        LL ans = solve (n);        Ans= (tmp-ans+mod)%mod;    printf ("%i64d\n", ans); } return 0;}




HDU 4059 the Boss on Mars (Repulsion principle)

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.