Case 1:1case 2:72
Calculate all cases First
(1+2+...+n) ^3 = (n (n+1)/2) ^3
Exclude two or more of the same value
(1^2+2^2+3^2+...+n^2) * (1+2+...+n)
And because 1^2+2^2+3^3+...+n^2=n* (n+1) * (2*n+1)/6,
SO (1^2+2^2+3^2+...+n^2) * (1+2+...+n) *3=n* (n+1) * (2*n+1)/6*n* (n+1)/2*3
Put three of the same add back
1^3+2^3+...+n^3 = (1+2+...+n) ^2 = (n (n+1)/2) ^2
But note that these are counted 3 times in the 2nd case!
So multiply by three!
After simplification for (n (n+1)/2) ^3-n*n* (n+1) * (n+1) * (n-1)/2
And then think of is how to calculate this formula, multiplication when there is the denominator, general processing such cases will use inverse, but this topic is a bit special n max for 10^18, then will not explode long long,n*n will explode unsigned long long, according to the above formula You can think of odd and even discussions.
1#include <iostream>2#include <algorithm>3#include <queue>4#include <cstring>5#include <cstdio>6#include <vector>7#include <string>8#include <iterator>9#include <cmath>Ten#include <deque> One#include <stack> A#include <cctype> - using namespacestd; - thetypedefLong Longll; -typedefLong Doubleld; - - Const intN = -+Ten; + Const intINF =0xFFFFFFF; - Const DoubleEPS = 1e-8; + Constll MOD = 1e9 +7; A at #defineINFL 0X7FFFFFFFFFFFFFFFLL - #defineMet (A, b) memset (A, B, sizeof (a)) - #defineRep (c, a, b) for (ll C = A; C < b; C + +) - #defineNRE (c, a, b) for (int c = A; C > b; c--) - //(n (n+1)/2) ^3-n*n* (n+1) * (n+1) * (n-1)/2 - intMain () in { - intT, k =1; toCIN >>T; + while(t--) - { the ll A, b, N; *CIN >>N; $cout <<" Case"<< k++ <<": ";Panax Notoginseng if(N &1) A = n MOD, B = (n +1) /2%MOD; - ElseA = N/2% MOD, B = (n +1) %MOD; thell tmp = A * b%MOD; +ll ans = tmp * tmp% MOD *tmp; ATMP = tmp * (n MoD)% mod * ((n +1)% MoD)% mod * ((N-1)% MOD)%MOD; thecout << (ans-tmp + MoD)% mod <<Endl; + } - return 0; $}
View Code
Multiplication Inverse definition:
The k value satisfying a*k≡1 (mod p) is a multiplicative inverse of p.
Why do we have to multiply the inverse element?
When we ask for (A/b) mod P's value, and a is large, and cannot directly obtain a A/b value, we will use the multiplication inverse.
We can use the B to multiply the inverse k of p, multiply a by the K-mode p, ie (a*k) mod p. The result is equivalent to (A/b) mod p.
Card: (actually very simple ...) )
According to B*k≡1 (mod p) there is b*k=p*x+1.
k= (p*x+1)/b.
Put K into (a*k) mod p, get:
(* (p*x+1)/b) MoD p
= ((a*p*x)/b+a/b) mod p
=[((a*p*x)/b) mod p + (A/b)] mod p
=[(p* (a*x)/b) mod p + (A/b)] mod p
p*[(a*x)/b] MoD p=0
So the original equals: (A/b) mod p
The multiplication inverse is used when calculating a large number of MODP. That is, the/a becomes * (f (a)), where F (a) is a multiplicative inverse of a in modulo P meaning, i.e. a*f (a) mod p=1
There are two methods for calculating the multiplication inverse, extending the GCD or the fast power modulo based on Euler's formula.
------------------------------------------------------------------------------------------------------
The extended GCD is the smallest integer solution to solve the equation Ax=1 (mod P).
Set ax=1+y*p, i.e. a*f (a,p) =1+p*g (a,p), to consider the solution of X and Y as a function of a,p.
When A>=p, set a=p*k+r, the equation becomes:
P*k*f (a,p) +r*f (a,p) =1+p*g (a,p), move item, get R*f (a,p) =1+p* (g (a,p)-k*f (a,p))
Then get f (a mod p,p) =f (a,p), g (a mod p,p) =g (a,p)-[a/p]*f (a,p);
P>=a is almost a meaning.
So the f,g is set to a global variable, like ordinary gcd do, instant update f,g can.
----------------------------------------------------------------------------------------------------
Set Euler function phi (x) = number of digits in [1,x-1] with x coprime. So Euler's formula:
A^phi (P) =1 (mod p)
Both sides with a, you can get a^ (Phi (P)-1) mod p is a in the meaning of modulo p multiplication inverse.
Exception:
If P is a prime number, then Phi (p) =p-1. That is, the multiplication inverse of A is a^ (P-2), the fast power can be.