#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<bitset> #include<set> usingnamespace std; #define int long long typedeflonglong ll; #define MAXN 200006 #define MAXM 450 #define pb push_back #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define inf 0x3f3f3f3f #define cmx( a , b ) a = max( a , b ) #define cmn( a , b ) a = min( a , b ) #define upd( a , b ) ( a = ( a + b ) % P ) #define P 1000000007 voidread( signed& x ){ scanf("%d",&x); } voidread( ll& x ){ scanf("%lld",&x); } int n , m , x0; #define swap( a , b ) a=a^b,b=a^b,a=a^b int f[MAXN]; signedmain(){ f[0] = f[1] = 1; cin >> n >> m; for( int i = 2 ; i <= max( n , m ) ; ++ i ) f[i] = ( f[i - 1] + f[i - 2] ) % P; cout << ( ( f[n] * 2 ) % P + ( f[m] * 2 ) % P - 2 + P ) % P << endl; }
B. The World Is Just a Programming Task (Hard Version)
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<bitset> #include<set> usingnamespace std; //#define int long long typedeflonglong ll; #define MAXN 600006 #define MAXM 450 #define pb push_back #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define inf 0x3f3f3f3f #define cmx( a , b ) a = max( a , b ) #define cmn( a , b ) a = min( a , b ) #define upd( a , b ) ( a = ( a + b ) % P ) #define swap( a , b ) a = a ^ b , b = a ^ b , a = a ^ b #define P 1000000007 voidread( signed& x ){ scanf("%d",&x); } voidread( ll& x ){ scanf("%lld",&x); } int n , p , x0; char s[MAXN]; int S[MAXN]; stack<pair<int,char>> stk; vector<int> x , y; signedmain(){ read( n ); scanf("%s",s + 1); int cc = 0; for( int i = 1 ; i <= n ; ++ i ) cc += ( s[i] == '(' ? 1 : -1 ); if( cc ) returnputs("0\n1 1"); for( int i = 1 ; i <= n ; ++ i ) { if( !stk.empty() && s[i] == ')' && stk.top().second == '(' ) stk.pop(); else stk.push( mp( i , s[i] ) ); } int las = 1; while( !stk.empty() && stk.top().se == '(' ) las = stk.top().fi , stk.pop(); -- las; for( int i = 1 ; i <= n ; ++ i ) S[i] = S[i - 1] + (s[(i + las - 1 + n) % n + 1] == '(' ? 1 : -1); int res0 = 0; for( int i = 0 ; i <= n ; ++ i ) if( S[i] == 0 ) x.pb( i ) , ++ res0; elseif( S[i] == 1 ) y.pb( i ); -- res0; int ans = res0 , cur = 0; pii ANS = mp(1 , 1); for( int i = 0 ; i < x.size() - 1 ; ++ i ) { cur = 0; for( int j = x[i] + 1 ; j < x[i + 1] ; ++ j ) if( S[j] == 1 ) ++ cur; if( cur > ans ) ans = cur , ANS = mp( x[i] + 1 , x[i + 1] ); ans = max( ans , cur ); } for( int i = 0 ; i < y.size() - 1 ; ++ i ) { cur = 0; for( int j = y[i] + 1 ; j < y[i + 1] ; ++ j ) if( S[j] == 2 ) ++ cur; if( cur + res0 > ans ) ans = cur + res0 , ANS = mp( y[i] + 1 , y[i + 1] ); } cout << ans << endl; ANS.fi += las - 1 , ANS.fi %= n , ANS.fi ++; ANS.se += las - 1 , ANS.se %= n , ANS.se ++; cout << ANS.fi << ' ' << ANS.se; }
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<bitset> #include<set> usingnamespace std; //#define int long long typedeflonglong ll; #define MAXN 1000006 #define MAXM 450 #define pb push_back #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define inf 0x3f3f3f3f #define cmx( a , b ) a = max( a , b ) #define cmn( a , b ) a = min( a , b ) #define upd( a , b ) ( a = ( a + b ) % P ) #define swap( a , b ) a = a ^ b , b = a ^ b , a = a ^ b #define P 1000000007 voidread( signed& x ){ scanf("%d",&x); } voidread( ll& x ){ scanf("%lld",&x); } int n , m; vector<int> G[MAXN]; int ind[MAXN]; int dfn[MAXN] , low[MAXN] , clo , ins[MAXN] , stk[MAXN] , tp = 0; int indg[MAXN] , bl[MAXN] , scc; vector<int> sc[MAXN]; voidtarjan( int u ){ dfn[u] = low[u] = ++ clo , ins[u] = 1 , stk[++ tp] = u; for( int v : G[u] ) { if( !dfn[v] ) tarjan( v ) , low[u] = min( low[u] , low[v] ); elseif( ins[v] ) low[u] = min( low[u] , dfn[v] ); } if( dfn[u] == low[u] ) { int x; ++ scc; do { x = stk[tp--] , bl[x] = scc , ins[x] = 0 , sc[scc].pb( x ); } while( x != u ); } } vector<int> ans1 , ans2; intmain(){ int t; cin >> t; while( t-- ) { read( n ) , read( m ); for( int i = 1 ; i <= n ; ++ i ) G[i].clear() , low[i] = dfn[i] = ins[i] = stk[i] = bl[i] = indg[i] = 0; for( int i = 1 , u , v ; i <= m ; ++ i ) { read( u ) , read( v ); G[u].pb( v ); } for( int i = 1 ; i <= scc ; ++ i ) sc[i].clear(); scc = 0; for( int i = 1 ; i <= n ; ++ i ) if( !dfn[i] ) tarjan( i ); for( int i = 1 ; i <= n ; ++ i ) for( int v : G[i] ) if( bl[v] != bl[i] ) ++ indg[bl[v]]; if( scc == 1 ) { puts("NO"); continue; } puts("YES"); int flg = 0; ans1.clear() , ans2.clear(); for( int i = 1 ; i <= scc ; ++ i ) if( flg || indg[i] ) for( int v : sc[i] ) ans1.pb( v ); else { for( int v : sc[i] ) ans2.pb( v ); flg = 1; } printf("%d %d\n",ans1.size() , ans2.size( )); for( int v : ans1 ) printf("%d ",v); puts(""); for( int v : ans2 ) printf("%d ",v); puts(""); } }