#include <bits/stdc .h>
#define LL long long
using namespace std;
const int AX = 1e6 66 ;
int a[AX];
int main(){
int n ;
cin >> n ;
for( int i = 1 ; i <= n ; i ){
cin >> a[i] ;
}
sort( a 1 , a n 1 ) ;
LL res = 0 ;
for( int i = 2 ; i <= n ; i ){
res = ( a[1] a[i] ) ;
}
cout << res << endl;
return 0 ;
}
G
思路:沒推公式,只是找了規(guī)律,整個(gè)式子其實(shí)就是求所有區(qū)間的和乘以w[區(qū)間長度]
,然后我們考慮相同區(qū)間長度i,發(fā)現(xiàn)加到最后前i-1個(gè)數(shù)依次加了1,2,3…i-1次,后i-1個(gè)數(shù)也是如此,中間的數(shù)都加了i次。
那么預(yù)處理出前綴和,以及各項(xiàng)與所在位置的乘積即可。
Code:
#include <bits/stdc .h>
#define LL long long
using namespace std;
const int AX = 3e5 66 ;
const LL MOD = 1e9 7 ;
LL a[AX];
LL w[AX];
LL sum1[AX];
LL f[AX];
LL g[AX];
int main(){
LL n ;
cin >> n ;
for( int i = 1 ; i <= n ; i ){
cin >> a[i] ;
}
for( int i = 1 ; i <= n ; i ){
cin >> w[i] ;
}
for( int i = 1 ; i <= n ; i ){
sum1[i] = sum1[i-1] a[i] ;
sum1[i] %= MOD ;
}
for( int i = 1 ; i <= n ; i ){
f[i] = f[i-1] 1LL * i * a[i] % MOD ;
f[i] %= MOD ;
}
for( int i = n ; i >= 1 ; i-- ){
g[i] = g[i 1] 1LL * ( n - i 1 ) * a[i] % MOD ;
g[i] %= MOD;
}
LL res = 0LL;
for( int i = 1 ; i <= n ; i ){
res = ( 1LL * i * ( ( sum1[n-i 1] MOD - sum1[i-1] ) % MOD ) % MOD f[i-1] %MOD g[n-i 2] % MOD ) * 1LL * w[i] % MOD ;
res %= MOD ;
}
cout << res % MOD << endl;
return 0 ;
}
I
思路:一個(gè)根節(jié)點(diǎn)連接n條鏈,每條單獨(dú)的聯(lián)通子樹為( x 1 ) * x / 2
各個(gè)鏈之間一直累乘x 1.
Code:
#include <bits/stdc .h>
#define LL long long
using namespace std;
const int MOD = 998244353;
int main(){
int n ;
cin >> n ;
LL x ;
LL ans2 = 0LL;
LL ans1 = 1LL;
for( int i = 0 ; i < n ; i ){
cin >> x;
ans1 *= ( x 1 );
ans1 %= MOD ;
ans2 = 1LL*(x 1)*x/2LL ;
ans2 %= MOD ;
}
cout << ( ans1 ans2 ) % MOD << endl;
}
#include <bits/stdc .h>
#define LL long long
using namespace std;
const int AX = 1e3 66 ;
int mark[AX] ;
int main(){
int n , m ;
cin >> n >> m ;
string s[2] ;
int cur = 0 ;
int id ;
for( int i = 1 ; i <= n ; i ) mark[i] = 1 ;
for( int i = 0 ; i < m ; i ){
cin >> id >> s[cur] ;
if( !i ) { mark[id] = 0 ; cur = !cur ; continue; }
if( s[cur] != s[!cur] ) mark[id] = 0 ;
cur = !cur ;
}
for( int i = 1 ; i <= n ; i ){
if( mark[i] ) cout << i << ' ';
}cout << endl;
return 0 ;
}