{"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\nint main()\n{\n int n;\n int a;\n int b=0;\n\tcin>>n;\n\ta=n;\n\tfor (int i=1;i<=n;i++)\n\t{\n\t\tb+=4*(a-1);\n\t\ta--;\n\t}\n\tcout<\nusing namespace std;\nint main()\n{\n\tlong long t1,t2,x1,x2,t0,X,Y;\n\tcin>>t1>>t2>>x1>>x2>>t0;\n\tdouble t,T=1e100;\n\twhile(x1>=0&&x2>=0)\n\t{\n\t\tt=(t1*x1+t2*x2*1.0)/(x1+x2);\n\t \tif(t\nusing namespace std;\nint main()\n{\nint a;\ncin>>a;\na=a*3;\na=a/2;\ncout<\n#include \ntypedef long long int ll;\ntypedef long double ld;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//#define N 4\n//const ll INF = 0x3f3f3f3f;\n//const ll LL_INF = 0x3f3f3f3f3f3f3f3f;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define PI 3.14159265358979323846264338327950\n#define mp make_pair\n#define inf 1e9\n#define M 1000000007\n#define N 1000000\n\nint main() {\n\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\n\tvector a(3);\n\tfor (ll i = 0; i < 3; i++) {\n\t\tcin >> a[i];\n\t}\n\tsort(a.begin(), a.end());\n\n\tif (a[2] >= 2 * (a[0] + a[1])) {\n\t\tcout << (a[0]+a[1]) << endl;\n\t} else {\n\t\tcout << (a[0]+a[1]+a[2])/3 << endl;\n\t}\n\t\n}\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "261c30b7d7eb92a4362ee7cb6e6e66e6", "src_uid": "bae7cbcde19114451b8712d6361d2b01", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define int long long\n\n\nint32_t main()\n{\n int x,y,n;\n cin>>x>>y>>n;\n if(x==0 and y==0 and n==2)\n cout<<\"Yes\\n\";\n else if((n-abs(x)-abs(y))%2==0 and (n-abs(x)-abs(y))>=0)\n cout<<\"Yes\\n\";\n else\n cout<<\"No\\n\";\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "41effa523ac928a9132d31de707fa57d", "src_uid": "9a955ce0775018ff4e5825700c13ed36", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\nusing namespace std;\nstring s;\nint main(){\n\tcin>>s;\n\tint pre=-1;\n\tint ans=0;\n\tfor(int i=0;i\n\n\n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n int N, val, numar = 0 , special = 0, specialmax = -1;\n\n cin >> N;\n\n while (N > 0)\n\n {\n\n cin >> val;\n\n if (val == 1)\n\n {\n\n numar = numar + 1;\n\n if (special > 0)\n\n {\n\n special = special - 1;\n\n }\n\n }\n\n else\n\n {\n\n special = special + 1;\n\n if (special > specialmax)\n\n {\n\n specialmax = special;\n\n }\n\n }\n N--;\n }\n\n cout << numar + specialmax ;\n\n return 0;\n\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp", "implementation"], "code_uid": "f5879fc1f672a5cc4691e25eacdfbc37", "src_uid": "9b543e07e805fe1dd8fa869d5d7c8b99", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define SET(a) memset(a,-1,sizeof(a))\n#define ALL(a) a.begin(),a.end()\n#define CLR(a) memset(a,0,sizeof(a))\n#define PB push_back\n#define PI acos(-1.0)\n#define max3(a,b,c) max(a,max(b,c))\n#define min3(a,b,c) min(a,min(b,c))\n#define READ freopen(\"input.txt\", \"r\", stdin)\n#define WRITE freopen(\"output.txt\", \"w\", stdout)\n#define LL long long\n#define MOD 1000000007\n#define MX 100010\n#define pii pair\n#define mx 20005\n#define INF 99999999\n\nint main()\n{\n\tLL n, m, x, y, i, j, sum, flag,k;\n\tchar ch;\n\tstring str;\n\tint test_case = 0, min = 999999999, first, last, a, b;\n\t//freopen (\"d:/codejam_input/A-large-practice.out\",\"w\",stdout);\n\t\n\t\n\twhile (cin>>n>>k)\n\t{\n\t\tsum = 0;\n\t\tfor (i = 1; i <= n; i++) {\n\t\t\tsum = (i * (i + 1)) / 2 - (n-i);\n\t\t\tif (sum == k) {\n\t\t\t\tcout << n - i << endl;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "binary search"], "code_uid": "33d304d922d6354dd43bbacdbb3f4d47", "src_uid": "17b5ec1c6263ef63c668c2b903db1d77", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "Clang++17 Diagnostics", "source_code": "#include \nusing namespace std;\n\nint main()\n{\n int ntests;\n cin >> ntests;\n for(int i=0; i> query;\n if(query == 1)\n cout << 1 << '\\n';\n else\n {\n int r = 1;\n for(int j=0; j\n#include \n#include \n\n#define ull unsigned long long\nusing namespace std;\n\n//map prime_map;\n//multimap dictionary;\n\null potence( ull base, ull exp ){\n if (exp == 0) return 1;\n if (exp == 1) return base;\n ull half = potence(base, exp/2);\n return half*half*potence(base, exp%2); \n}\n\nint main() { \n ios_base::sync_with_stdio(0);\n int a, c;\n cin >> a >> c;\n ull exp = 0;\n ull result = 0;\n static ull base = 3;\n while ( !(a == 0 && c == 0) ){\n int b = (( c%base - a%base )+ base)%base;\n result += b*potence(base,exp++);\n a /= base;\n c /= base;\n } \n cout << result;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "6504b59e2cf45260a8e4dcfa586cb294", "src_uid": "5fb635d52ddccf6a4d5103805da02a88", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n int n;\n cin>>n;\n long long a[n],i;\n for(i=0;i>a[i];\n sort(a,a+n);\n long long s = a[n-1],curr = a[n-1];\n for(i=n-2;i>=0;i--)\n {\n if(a[i]\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef unsigned int UI;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef long double LD;\nconst double PI = 3.14159265;\nconst double E = 2.71828182846;\n\nint gcd(int a, int b);\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tint n;\n\tint num[100];\n\twhile (cin >> n)\n\t{\n\t\tfor (int i=0; i> num[i];\n\t\tint ans = 0;\n\t\tfor (int i=0; i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define INF 0x3f3f3f3f\n#define eps 1e-6\ntypedef long long ll;\nconst double pi=acos(-1.0);\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin>>n;\n\tint ans=n/2+1;\n cout<\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint x1[100000];\n\nint main()\n{\n long long a1,a2,b1,b2,k,s=0,l1,l2;\n cin >> a1 >> a2 >> b1 >> b2 >> k;\n\n l1=max(a1,b1);\n l2=min(a2,b2);\n\n s+=l2-l1+1;\n\n if (k>=l1 and k<=l2) s--;\n\n if (s<0) s=0;\n cout << s << endl;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "3dcf67d2cb320426fd16d63c12af956c", "src_uid": "9a74b3b0e9f3a351f2136842e9565a82", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nint main ()\n{\n\tlong long n;\n\n\tcin >> n;\n\n\tif (n % 2 == 0)\n\t\tcout << (n/2) << endl;\n\telse\n\t\tcout << (-(n+1))/2 << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "a3ecd67633bbf59e04372edcf1a5fd7b", "src_uid": "689e7876048ee4eb7479e838c981f068", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nbool da(string x)\n{\n\tfor(int i=0;i'Z')\n\t\t\treturn false;\n\t}\n\treturn true;\n}\nbool xiao(string x)\n{\n\tif(x[0]<'a'||x[0]>'z') return false;\n\tfor(int i=1;i'Z')\n\t\t\treturn false;\n\t}\n\treturn true;\n}\nint main()\n{ \n\tstring a;\n\tcin>>a; \n\tif(da(a)||xiao(a))\n\t{\n\t\tfor(int i=0;i='a'&&a[i]<='z')\n\t\t\t{\n\t\t\t\ta[i]-=' ';\n\t\t\t}\n\t\t\telse\n\t\t\t\ta[i]+=' ';\n\t\t}\n\t\tcout<\nusing namespace std;\ntypedef long long ll;\n#define INF 1e8\nint pd[120][10010];\nint vi,vf,tempo,d;\n\nint go(int t, int v){\n\t//~ if(v > 100*d) return -INF;\n\tif(t == 1) \treturn v==vi?v:-INF; \n\n\tint &ref = pd[t][v];\n\tif(ref != -1) return ref;\n\tref = v+go(t-1,v);\n\tfor(int i = 1 ; i <= d ; ++i){\n\t\tif(v-i>=0) ref = max(ref,v+go(t-1,v-i));\n\t\tref = max(ref,v+go(t-1,v+i));\n\t}\n\treturn ref;\n}\n\nint main(){\n\t//~ freopen(\"in\",\"r\",stdin);\n\tcin>>vi>>vf>>tempo>>d;\n\tmemset(pd,-1,sizeof pd);\n\tcout << go(tempo,vf) << '\\n'; \t\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "dp"], "code_uid": "d312421af4baa1d027b6d687923701ff", "src_uid": "9246aa2f506fcbcb47ad24793d09f2cf", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\n\nint main() {\n int a[8], b[8];\n int as[8], bs[8];\n for (int i = 0; i < 8; i++) {\n cin >> a[i] >> b[i];\n as[i] = a[i];\n bs[i] = b[i];\n }\n sort(as, as + 8);\n sort(bs, bs + 8);\n\n int x[3], y[3];\n x[0] = as[0];\n x[1] = as[3];\n x[2] = as[7];\n\n y[0] = bs[0];\n y[1] = bs[3];\n y[2] = bs[7];\n if (!(x[0] < x[1] && x[1] < x[2] && y[0] < y[1] && y[1] < y[2])) {\n cout << \"ugly\";\n return 0;\n }\n\n for (int i = 0; i <= 2; i++) {\n for (int j = 0; j <= 2; j++) {\n if (i == 1 && j == 1) {\n continue;\n }\n bool found = false;\n for (int k = 0; k < 8; k++) {\n if (x[i] == a[k] && y[j] == b[k]) {\n found = true;\n break;\n }\n }\n\n if (!found) {\n cout << \"ugly\";\n return 0;\n }\n }\n }\n\n cout << \"respectable\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["sortings"], "code_uid": "b454b2a35d57dacc99680ffe81e10f11", "src_uid": "f3c96123334534056f26b96f90886807", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \nusing namespace std;\nlong long n,k,m;\nlong long dp[1005][10][100][2];\nlong long dfs(int L,int be,int mod,bool flag,int mul)\n{\n long long ans=0;\n if(L==n)return flag&&be!=0;\n if(dp[L][be][mod][flag]!=-1)return dp[L][be][mod][flag];\n for(int i=0;i<=9;i++)\n {\n int next=(mul*i+mod)%k;\n ans=(ans+dfs(L+1,i,next,flag||(next==0&&i>0),mul*10%k))%m;\n }\n return dp[L][be][mod][flag]=ans;\n}\nint main()\n{\n memset(dp,-1,sizeof(dp));\n cin>>n>>k>>m;\n cout<\nusing namespace std;\n\nchar a[2001]=\"0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400\";\nint n;\nint main()\n{\n\tcin>>n;\n\tcout<\nusing namespace std;\n\nint main()\n{\n int m,n;\n cin>>n>>m;\n if(n==1&&m==1)\n {\n cout<<1;\n return 0;\n }\n if(n%2!=0)\n {\n if(m<(n+1)/2)cout<\n\nusing namespace std;\n\ntypedef long long LL;\nint main()\n{\n\tios::sync_with_stdio(false);\n\tLL m, b,max=0;\n\tcin >>m>>b;\n\tfor(LL y=b,x=0;y>=0;y--,x+=m)\n\t{\n\t\tLL a=x*(x+1)/2*(y+1) + y*(y+1)/2*(x+1);\n\t\tif(max\nusing namespace std;\nint main()\n{\n int a,b,n;\n cin>>a>>b>>n;\n int k=n;\n for(int i=0;i<=k;i++)\n {\n if(i%2==0)\n {\n int d=__gcd(n,a);\n if(n!=0)\n {\n n=n-d;\n }\n else\n {\n cout<<\"1\";\n break;\n }\n \n }\n else\n {\n int d=__gcd(n,b);\n if(n!=0)\n {\n n=n-d;\n }\n else\n {\n cout<<\"0\";\n break;\n }\n }\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "cca390263ee44d0fe731cba3fe067472", "src_uid": "0bd6fbb6b0a2e7e5f080a70553149ac2", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/// -Legolas_Otaku- ///\n#include \n#define hentai ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define File freopen(\".txt\", \"r\", stdin);freopen(\".txt\", \"w\", stdout); \n#define ll long long\n#define Yeah for(;;)\nusing namespace std;\n\nint main(){\n\t//File;\n\thentai;\n\t\n\tstring a,b;\n\tcin>>a>>b;\n\tif(a == b){\n\t\tcout<\n#define ll long long\nusing namespace std;\nll stat[100005];\n\nint main()\n{\n string a;\n cin>>a;\n ll r=0,b=0,y=0,g=0,kr=0,kb=0,ky=0,kg=0;\n\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define INF 1011111111\n#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)\n#define rep(i,n) FOR(i,0,n)\n#define CL(a,v) memset((a),(v),sizeof(a))\n#define mp make_pair\n#define X first\n#define Y second\n#define all(c) (c).begin(), (c).end()\n#define SORT(c) sort(all(c))\n\ntypedef long long ll;\ntypedef vector VI;\ntypedef pair pii;\n\n/*** TEMPLATE CODE ENDS HERE */\n\nint p[1000100];\nint st[1000100];\n\nint main() {\n#ifdef LOCAL_HOST\n freopen(\"input.txt\",\"r\",stdin);\n //freopen(\"output.txt\",\"w\",stdout);\n#endif\n\n int n,m;\n cin >> n;\n rep(i,n) cin >> p[i];\n cin >> m;\n rep(i,m) {\n int j;\n cin >> j;\n p[j-1] *= -1;\n }\n\n reverse(p,p+n);\n int sz = 0;\n\n rep(i,n) {\n int v = p[i];\n if(v>0 && (sz==0 || st[sz-1] != v)) v = -v;\n p[i] = v;\n if(v>0)\n sz--;\n else\n st[sz++] = -v;\n }\n assert(sz>=0);\n\n if(sz>0) {\n cout << \"NO\\n\";\n }\n else {\n reverse(p, p+n);\n cout << \"YES\\n\";\n rep(i,n) cout << p[i] << \" \";\n cout << endl;\n }\n\n\n#ifdef LOCAL_HOST\n printf(\"TIME: %.3lf\\n\",double(clock())/CLOCKS_PER_SEC);\n#endif\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "data structures"], "code_uid": "2382b56d50d34cbfe9abc02af384e62e", "src_uid": "be82b8f209217875221ebe5de8675971", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\nusing namespace std;\nint main(void)\n{ \n\tlong long b = 0;\n\tlong long n;\n\tcin >> n;\n\twhile ((n % 10) == 0)\n\t{\n\t\tn /= 10;\n\t}\n\tlong long x = n;\n\twhile (x != 0)\n\t{\n\t\tint a = x % 10;\n\t\tb = b * 10 + a;\n\t\tx /= 10;\n\n\t}\n\tif (b == n) cout << \"YES\";\n\telse cout << \"NO\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "0c61fff4a85593595daceb18dd6f65ad", "src_uid": "d82278932881e3aa997086c909f29051", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define L(s) (int)((s).size())\n#define pb push_back\n#define pii pair\n#define mp make_pair\n#define x first\n#define y second\n#define inf 1000000000\n#define all(s) (s).begin(), (s).end()\n#define ll long long\n#define VI vector\n#define ull unsigned ll\nint x;\nint main() {\n\tcin >> x;\n\tif (x == 1) { cout << \"1\\n\"; return 0; } else\n\t\tif (x == 3) { cout << \"5\\n\"; return 0; }\n\tfor(int n = 3; ; ++n) {\n\t\tif (n % 2) {\n\t\t\tint val = n * n / 2 + 1;\n\t\t\tif (x % 4 == 0) val -= val % 4;\n\t\t\tif (val >= x) {\n\t\t\t\tcout << n << endl;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t} else {\n\t\t\tint val = ((n / 2 - 1) * (n / 2 - 1) / 2 + (n / 2 - 1) % 2) * 4;\n\t\t\tif (val >= x) {\n\t\t\t\tcout << n << endl;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "constructive algorithms"], "code_uid": "dc10c6d75aed6beb8ed2c252a6c01920", "src_uid": "01eccb722b09a0474903b7e5abc4c47a", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\n\n#define N 505\nint n , K , Q = 1e9 + 7;\nvoid add(int& A , int B) {\n A += B;\n if (A >= Q) {\n A -= Q;\n }\n}\nint f[2][N][N];\nint power[N];\nvoid work() {\n int i , j , k ;\n cin >> n >> K;\n power[0] = 1;\n for (i = 1 ; i <= n ; ++ i)\n power[i] = power[i - 1] << 1 , power[i] %= Q;\n int cur = 0 , nxt = 1;\n f[cur][0][0] = 1;\n for (i = 1 ; i <= n ; ++ i) {\n memset(f[nxt] , 0 , sizeof(f[nxt]));\n for (j = 0 ; j <= K ; ++ j) {\n for (k = 0 ; k < i ; ++ k) {\n if (!f[cur][j][k]) continue;\n add(f[nxt][j][k] , (int)((LL)f[cur][j][k] * power[k] % Q));\n if (j < K) {\n add(f[nxt][j + 1][i] , (int)((LL)f[cur][j][k] * ((power[i] - power[k] + Q) % Q) % Q));\n }\n }\n }\n swap(cur , nxt);\n }\n int ans = 0;\n for (i = 0 ; i <= n ; ++ i)\n add(ans , f[cur][K][i]);\n printf(\"%d\\n\" , ans);\n}\n\nint main(){\n work();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "9b5e6a2cc248743d033550c248e30e68", "src_uid": "111673158df2e37ac6c019bb99225ccb", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\nusing namespace std;\nlong long t,x,s;\nint main()\n{\n\tcin>>t>>s>>x;\n\tif(((x-t)%s==0 && x-t>=0) || ((x-t-1)%s==0 && x-t>=0 && x-t-1!=0))\n\t\tcout<<\"YES\";\n\telse\n\t\tcout<<\"NO\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "0f2230ebaaae4e417af192a3e52ea6e1", "src_uid": "3baf9d841ff7208c66f6de1b47b0f952", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nint main(){std::cout<<\"25\\n\";return 0;}\n//3122", "lang_cluster": "C++", "tags": ["number theory"], "code_uid": "c835c0e92ce6d22dd9c148258535bb86", "src_uid": "dcaff75492eafaf61d598779d6202c9d", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n \ntypedef long long int ll;\n \nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n int x1, x2, y1, y2, x, y;\n cin>>x1>>y1>>x2>>y2;\n \n if(y2==y1){\n y1++;\n }\n \n if(x2==x1){\n x1++;\n }\n \n if(y2x1)\n x=x2+1;\n }\n \n if(y2>y1){\n y=y2+1;\n \n if(x2x1)\n x=x2+1;\n }\n \n cout<<(abs(x-x1) + abs(y-y1)) * 2; \n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "af971f2fb3596f4eb1967646fba64fca", "src_uid": "f54ce13fb92e51ebd5e82ffbdd1acbed", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n \nint main() {\n\tstring s;\n\tcin >> s;\n\tcout << 26 + 25 * s.length() << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "strings"], "code_uid": "0667ff55b3094b3ff74547269123fd01", "src_uid": "556684d96d78264ad07c0cdd3b784bc9", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint nu_f[1000001]={0};\nvoid Num_Factors()\n{\n\tfor (long long i=1 ; i<1000001;i++)\n\t{\n\t for (long long j=i; j<1000001;j+=i)\n\t {\n\t nu_f[j]++;\n\t }\n\t}\n}\n\nint main()\n{\n \n\tint n, m, c;\n\tNum_Factors();\n\tcin >> n >> m >> c;\n\tlong long res = 0;\n\tfor (int i = 1; i <= n; i++)\n\t{\n\t\tfor (int j = 1; j <= m; j++)\n\t\t{\n\t\t\tfor (int k = 1; k <= c; k++)\n\t\t\t{\n\t\t\t\tres += nu_f[i*j*k];\n\t\t\t\tres%=1073741824;\n\t\t\t}\n\t\t}\n\t}\n\tcout << res;\n\tcout << endl;\n//\tsystem(\"pause\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation", "number theory"], "code_uid": "857890af747d867516e94963e9756e7d", "src_uid": "4fdd4027dd9cd688fcc70e1076c9b401", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint a,b,c,d,e,f;\nint main()\n{\n\tscanf(\"%d%d%d%d%d%d\",&a,&b,&c,&d,&e,&f);\n\tif((b*d*f>a*c*e ||(!c && d)||(!a && b && d))&& d)\n\t\tputs(\"Ron\");\n\telse\n\t\tputs(\"Hermione\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "0568440aa37cbb4cdccc027c4acd89a8", "src_uid": "44d608de3e1447f89070e707ba550150", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nint main(){\n\tint n,m;\n\tcin>>n>>m;\n\tif(m>n) swap(n,m);\n\tif((n%3==1 && m%4==0 && n>1) || (m%3==1 && n%4==0 && m>1)) cout<<(n*m-(n*m/4))<1))<\n#include \nusing namespace std;\nint digits(int number)\n{\n int digits;\n\n return digits;\n}\nint main()\n{\n string numbers;\n string temp;\n int count=0;\n int te;\n int x=0;\n int k=0;\n cin>>x;\n for(int i=1; i<=x; i++)\n {\n //k = digits(i);\n te=i;\n if (te<0)\n {\n k=1;\n }\n while(te)\n {\n te/=10;\n k++;\n\n }\n\n count=i;\n for (int j = 0; j < k; j++)\n {\n temp += (char) ((count%10)+48);\n count=count/10;\n\n }\n k=0;\n reverse(temp.begin(), temp.end());\n\n numbers+=temp;\n temp=\"\";\n }\n\n cout<\n\nusing namespace std;\n\nconst int MAX_N=10+5,MAX_C=10+5,MAX_R=10+5;\nbool a[MAX_R][MAX_C];\nint x[MAX_N],y[MAX_N];\n\nint main()\n{\n int r,c;\n cin >> r >> c;\n int n,k;\n cin >> n >> k;\n for(int i=0;i> x[i];\n cin >> y[i];\n }\n //SOLVE\n for(int i=0;i=k)\n {\n ans++;\n }\n }\n }\n }\n }\n //OUTPUT\n cout << ans;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "0904589812bae8baddb5b82c2ab37e84", "src_uid": "9c766881f6415e2f53fb43b61f8f40b4", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint main()\n{\n int n;\n cin >> n;\n int num = 0;\n int num_e = 0;\n string s;\n cin >> s;\n for(int i = 0; i < n; ++i)\n {\n if(s[i] == '8')\n {\n ++num_e;\n }\n }\n num = n / 11;\n if(num <= num_e)\n {\n cout << num << endl;\n }\n else\n {\n cout << num_e << endl;\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "7b96ddd035830325d1d64444b9d5a2c7", "src_uid": "259d01b81bef5536b969247ff2c2d776", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint n;\nint a[11][11];\nint f[11][11];\nint main(){\n\tcin>>n;\n\tfor(int i=1;i<=n;i++){\n\t\ta[i][1]=1;a[1][i]=1;}\n\tfor(int i=2;i<=n;i++)\n\tfor(int j=2;j<=n;j++){\n\t\ta[i][j]=a[i-1][j]+a[i][j-1];\n\t}\n\tcout<\nusing namespace std; \nint main()\n{\n\tlong long n;\n\tcin >> n;\n\tif (n % 2 == 0 && n != 2) cout << \"YES\" << endl;\n\telse cout << \"NO\" << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "c4c6c530bc07aaf9df1eb98fe54b6555", "src_uid": "230a3c4d7090401e5fa3c6b9d994cdf2", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 Diagnostics", "source_code": "#include \nusing namespace std;\n\nint main (){\n\t//freopen(\"input.txt\" , \"r\" , stdin);\n\t\n\tlong long a ,b; scanf(\"%lld%lld\",&a,&b);\n\tfor ( long long i=1 ; i<=b ; i++){\n\t\tif ( a%i != i-1){\n\t\t\tcout<<\"No\"; return 0;\n\t\t}\n\t}\n\tcout<<\"Yes\";\n}", "lang_cluster": "C++", "tags": ["brute force", "number theory"], "code_uid": "26f38cf38025a9a1319c7347a4f21a10", "src_uid": "5271c707c9c72ef021a0baf762bf3eb2", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint a,b;\n\tcin>>a;\n\tfor(int i=1;i<=a;i++)\n\t{\n\t\tcin>>b;\n\t\tif(b==1||b==2||b==4||b==5||b==8||b==11)\n\t\t{\n\t\t\tcout<<\"NO\"<\n\nusing namespace std;\n\n#define maxn 8000005\n#define ll long long int\n#define db(x) cout<<#x<<\" -> \"< \"< \"< \"< \"< \"< > vt;\nll power(ll a,ll n){\n ll ret = 1LL;\n for(int i=1; i<=n; i++){\n ret*=a;\n }\n return ret;\n}\n\nbool vis[maxn+5];\n\nvoid seive(){\n for(int i=2; i*i<=maxn; i++){\n if(!vis[i]){\n for(int j=(2*i); j<=maxn; j+=i){\n vis[ j ] = true;\n }\n }\n }\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n ll n, b;\n seive();\n // IN;\n cin>>n>>b;\n ll ans = n+5000;\n ll t = b;\n vector< pair< ll , ll > > vt;\n for(ll i=2; 1LL * i * i <= b ; i++){\n bool flag = false;\n if( b % i ){ continue; }\n // if ( vis[ i ] ) { flag = true; }\n // if( !flag ){\n ll c = 0;\n while( b % i == 0 ){\n b/=i;\n c++;\n }\n vt.push_back({1LL*i,1LL*c});\n // }\n }\n if( b > 1){\n vt.push_back({b,1LL});\n }\n for(auto it: vt){\n ll x = it.first;\n ll y = it.second;\n unsigned ll t = 1ULL * x;\n ll res = 0LL;\n while( t <= n ){\n res+=(n/t);\n if( x > (n/t) ) { break; }\n t*=1ULL*x;\n // db3(t,res,n/t);\n }\n res/=y;\n ans = min(ans,res);\n }\n cout<\nusing namespace std;\n\nint main()\n{\n\tint r,g,b,i;\n\tint ans=29;\n\tcin>>r>>g>>b;\n\t\n\twhile(1)\n\t{\n\t\tfor(i=1;i<=3;i++)\n\t\t{\n\t\t\tif(r<=0&&g<=0&&b<=0)\n\t\t\t\tbreak;\n\t\t\tans++;\n\t\t\tif(i==1)\n\t\t\t\tr-=2;\n\t\t\tif(i==2)\n\t\t\t\tg-=2;\n\t\t\tif(i==3)\n\t\t\t\tb-=2;\n\t\t}\n\t\tif(r<=0&&g<=0&&b<=0)\n\t\t\tbreak;\n\t}\n\t\n\tcout<\n\nusing namespace std;\n\nint main()\n{\n long long int d1,d2,d3,sum,d;\n cin>>d1>>d2>>d3;\n sum=d1+d2+d3;\n d=2*(d1+d2);\n if(sum>=d)\n cout<\n#include\n#include\nusing namespace std;\nint main()\n{\n string s;\n char arr[100]={0};\n int j=0;\n cin>>s;\n sort(s.begin(),s.end());\n for(int i=0;i\n\ntypedef bool bo;\ntypedef short sh;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef double de;\ntypedef long double lde;\ntypedef char ch;\n\n#define spd ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n#define file(x, y) freopen((x), \"rt\", stdin); freopen((y), \"wt\", stdout)\n#define pr pair\n#define str string\n#define vec vector\n#define vint vec \n#define vll vec \n#define vull vec \n#define vlde vec \n#define vch vec \n#define vstr vec \n#define vbo vec \n#define ph push\n#define phf push_front\n#define phb push_back\n#define ins insert\n#define sub substr\n#define re reverse\n#define bn begin()\n#define ed end()\n#define rbn rbegin()\n#define red rend()\n#define se size()\n#define ey empty()\n#define tp top()\n#define pp pop()\n#define ft front()\n#define ppf pop_front()\n#define bk back()\n#define ppb pop_back()\n#define cr clear()\n#define ee erase\n#define rse resize\n#define te true\n#define fe false\n#define st first\n#define nd second\n#define antihype signed main()\n#define all(x) (x).bn, (x).ed\n#define allc(x) (x).bn, (x).ed, cmp\n#define rall(x) (x).rbn, (x).red\n#define rallc(x) (x).rbn, (x).red, cmp\n#define coutf(x) cout << fixed << setprecision(x)\n#define gle(x) getline(cin, (x))\n#define loop(i, n) (int i = 0; i < (n); ++i)\n#define rloop(i, n) (int i = (n - 1); i > -1; --i)\n#define loopll(i, n) (ll i = 0; i < (n); ++i)\n#define rloopll(i, n) (ll i = (n - 1); i > -1; --i)\n#define incloop(i, x, y) (int i = (x); i < (y); ++i)\n#define decloop(i, x, y) (int i = (x); i > (y); --i)\n#define incloopll(i, x, y) (ll i = (x); i < (y); ++i)\n#define decloopll(i, x, y) (ll i = (x); i > (y); --i)\n#define steploop(i, x, y, z) (int i = (x); i < (y); i += (z))\n#define steploopll(i, x, y, z) (ll i = (x); i < (y); i += (z))\n#define loopst(i, n) (size_t i = 0; i < (n); ++i)\n#define cinvint(v, n) vint v(n); for loopst(i, n) cin >> v[i]\n#define cinvll(v, n) vll v(n); for loopst(i, n) cin >> v[i]\n#define cinvull(v, n) vull v(n); for loopst(i, n) cin >> v[i]\n#define cinvlde(v, n) vlde v(n); for loopst(i, n) cin >> v[i]\n#define cinvch(v, n) vch v(n); for loopst(i, n) cin >> v[i]\n#define cinvstr(v, n) vstr v(n); for loopst(i, n) cin >> v[i]\n#define sqr(a) (a) * (a)\n\nusing namespace std;\n\nantihype {\n\tspd;\n\t//file(\"input.in\", \"output.out\");\n\tint v[60] = { 0, 2, 0, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256, 0, 512, 0, 1024, 0, 2048, 0, 4096, 0, 8192, 0, 16384, 0, 32768, 0, 65536, 0, 131072, 0, 262144, 0, 524288, 0, 1048576, 0, 2097152, 0, 4194304, 0, 8388608, 0, 16777216, 0, 33554432, 0, 67108864, 0, 134217728, 0, 268435456, 0, 536870912, 0, 1073741824 }, n;\n\tcin >> n;\n\tcout << v[n - 1];\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp"], "code_uid": "525270f8396918b5c1712e15d669b8c2", "src_uid": "4b7ff467ed5907e32fd529fb39b708db", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define ll unsigned long long\n#define pi 3.1415926535897932384626433832795\n#define fin(a,n) for(ll i=0;i>a[i];\n#define fout(a,n) for(ll i=0;i>n>>m;\n\twhile(n)\n\t{\n\t\tans += n;\n\t\tint tmp = (n+prev)%m;\n\t\tn = (n+(prev))/m;\n\t\tprev = tmp;\n\t}\n\tcout<\n#include \n#include \n#include \n#include \n#define F(i,a,b) for(int i=a;i<=b;i++)\n#define D(i,a,b) for(int i=a;i>=b;i--)\n#define LL long long\n#define INF 0X3F3F3F3F\n#define MINF 999999999999999999\n#define MAXN 110\nusing namespace std;\nint n;\nint res;\nint main()\n{\n\tcin>>n;\n\tif(n)\n\t\tres+=n/100,n%=100;\n//\tcout<\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint main()\n{\n int a,b;\n\twhile(scanf(\"%d%d\",&a,&b)!=EOF)\n\t{\n\t int c,d,e,f;\n\t\tscanf(\"%d%d%d%d\",&c,&d,&e,&f);\n\t\tint flag = 0;\n\t\tif(c+e<=a && max(d,f)<=b)\n\t\t\tflag = 1;\n\t\tif(c+f<=a && max(d,e)<=b)\n\t\t\tflag = 1;\n\t\tif(d+e<=a && max(c,f)<=b)\n\t\t\tflag = 1;\n\t\tif(d+f<=a && max(c,e)<=b)\n\t\t\tflag = 1;\n\t\tif(c+e<=b && max(d,f)<=a)\n\t\t\tflag = 1;\n\t\tif(c+f<=b && max(d,e)<=a)\n\t\t\tflag = 1;\n\t\tif(d+e<=b && max(c,f)<=a)\n\t\t\tflag = 1;\n\t\tif(d+f<=b && max(c,e)<=a)\n\t\t\tflag = 1;\n\t\tif(flag)\n\t\t\tcout<<\"YES\"<\nusing namespace std;\nint main()\n{\n char a[110];\n long long b=0,s=0,c=0,nb,ns,nc,pb,ps,pc,r,sum=0,i=0,t;\n cin>>a>>nb>>ns>>nc>>pb>>ps>>pc>>r;\n while(a[i]!='\\0')\n {\n switch(a[i])\n {\n case 'B':b+=1;break;\n case 'S':s+=1;break;\n case 'C':c+=1;break;\n }\n i++;\n }\n while(1)\n {\n if(nb>=b&&ns>=s&&nc>=c)\n {\n sum+=1;\n nb-=b;\n ns-=s;\n nc-=c;\n }\n else\n {\n if(nb\n#define ll long long\n#define endl '\\n'\nusing namespace std;\n\nint main(){\n\n\tios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n\tint buy[35], sell[35];\n\tint n, m, r, ans = 0;\n\tint *minBuy, *maxSell;\n\n\tscanf(\"%d %d %d\", &n, &m, &r);\n\tfor(int i = 0; i < n; i++) scanf(\"%d\",&buy[i]);\n\tfor(int i = 0; i < m; i++) scanf(\"%d\",&sell[i]);\n\t\n\tminBuy = min_element(buy, buy+n);\n\tmaxSell = max_element(sell, sell+m);\n\n\tif(*minBuy < *maxSell){\n\t\t\tans = r / (*minBuy);\t\t\n\t\t\tr = r % (*minBuy);\t\t\t\n\t\t\tr = r + ans *(*maxSell);\n\t}\n\n\tprintf(\"%d\\n\",r);\t\n\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "ea1fc43464df7bee806847cc5098d124", "src_uid": "42f25d492bddc12d3d89d39315d63cb9", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#pragma warning(disable:4996) \n \ntypedef long long ll;\n#define MIN(a, b) ((a)>(b)? (b): (a))\n#define MAX(a, b) ((a)<(b)? (b): (a))\n#define LINF 9223300000000000000\n#define INF 2140000000\nconst long long MOD = 1000000007;\nusing namespace std;\n\n\nconst int max_comb=300;\nvector fac(max_comb+1); //n! (mod M)\nvector ifac(max_comb+1); //k!^(-1) (mod M)\n\nll mpow(ll x, ll n){ //x^n(mod M)\n ll ans = 1;\n while(n != 0){\n if(n&1) ans = ans*x % MOD;\n x = x*x % MOD;\n n = n >> 1;\n }\n return ans;\n}\n\nll minv(ll x){\n return mpow( x, MOD-2 );\n}\n\nll comb(int a, int b){ // C(a,b) = a! * b!^(-1) * (a-b)^(-1)\n if(a == 0 && b == 0)return 1;\n if(a < b || a < 0)return 0;\n ll tmp = ifac[a-b]* ifac[b] % MOD;\n return tmp * fac[a] % MOD;\n}\n\nll perm(int a, int b){ // P(a,b) = a! * (a-b)!^(-1)\n if(b == 0)return 1;\n if(a < b || a < 0)return 0;\n ll tmp = ifac[a-b] % MOD;\n return tmp * fac[a] % MOD;\n}\n\nvoid pre_comb()\n{\n fac[0] = 1;\n ifac[0] = 1;\n for(int i = 0; i\n#include\n#include\nusing namespace std;\nint main(){\nint w1,h1,w2,h2,a,b,c,x;\ncin>>w1>>h1>>w2>>h2;\na=(w1*2)+(h1*2);\nb=(w2*2)+(h2*2);\nx=min(w1,w2);\nc=a+b-(x*2);\ncout<<4+c;\nreturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "4fe16825eef971bb15c8b421ac946237", "src_uid": "b5d44e0041053c996938aadd1b3865f6", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n,k;\n\tcin>>n>>k;\n\tint z;\n\tz=240-k;\n\tint j=1;\n\tint sum=0;\n\twhile(z>=0)\n\t{\n\t\tz=z-(j*5);\n\t\tsum++;\n\t\tj++;\n\t}\n\tif(sum==n)\n\t{\n\t\tcout<n)\n\t{\n\t\tcout<\nusing namespace std;\nint main()\n{\n\tint k, l, m, n, d, count = 0;\n\tcin >> k >> l >> m >> n >> d;\n\tfor (unsigned int i = 1; i <=d; i++)\n\t{\n\t\tif (i%k&&i%l&&i%m&&i%n)\n\t\t\tcount++;\n\t}\n\tcout << d - count<\n#include\n#include\n#include\nusing namespace std;\nint main()\n{\nint hh1,hh2,mm1,mm2,hh,mm;\nwhile(cin>>hh1)\n{\n getchar();\n cin>>mm1;\n getchar();\n cin>>hh2;\n getchar();\n cin>>mm2;\n getchar();\n\n\n mm=mm1-mm2;\n if(mm<0)\n {\n mm+=60;\n hh1--;\n }\n hh=hh1-hh2;\n if(hh<0)\n hh+=24;\n if(hh/10)\n cout<\n#include \nusing namespace std;\nint prime(int n);\nint next(int n);\nint main() {\n int num1,num2;\n cin >> num1 >> num2;\n if (num2== next(num1))\n cout << \"YES\";\n else\n cout << \"NO\";\n return 0;\n}\nint prime(int n) {\n for (int i = 2; i <= n/2; i++)\n if (n%i==0)\n return 0;\n return 1;\n}\nint next(int n) {\n for (n++; prime(n)==0; n++);\n return n;\n}\n", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "72d9fc992b72fadee946287003c51f87", "src_uid": "9d52ff51d747bb59aa463b6358258865", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \nusing namespace std;\nvoid swap (int &x,int &y){\n int temp=x;\nx=y;\ny=temp;\n}\n\nint main(){\n int n,c;\n cin>>n>>c;\n int *day= new int[n];\n for (int j=0;j>day[j];}\n \n \n int *prob=new int[n];\n int u=0;\n for (int i=0;i=0&&day[i]- day[i+1]>=c){\n \n prob[u] =day[i]-day[i+1]-c;\n \n u++; \n \n \n }\n \n }\n \n \n if (u==0){\n cout <<0;}\n else {\n \n for (int l=u;l>0;l--){\n for (int k=0;k\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\n#define all(v) v.begin(),v.end()\n#define sz(V) ((ll)((v).size()))\n#define pp(x) push_back(x)\n#define ck(a) (a<1 || a>9)\n#define lop(i,n) for(ll i=0;i>v[i];\ntypedef string ss;\ntypedef long long ll;\ntypedef pair ii;\ntypedef pair si;\ntypedef pair is;\ntypedef pair chi;\ntypedef vector vii;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef vector vs;\ntypedef vector vll;\n//READ(\"input.txt\");WRITE(\"output.txt\");\nll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }\nll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }\nll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }\ndouble sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }\n\nint main()\n{\n ll x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0;\n vector < vector> v(3, vector(3));\n int k = 0;\n lop(i, 3)\n {\n\n ll t; cin >> t;\n v[k][i] = t;\n x1 += t;\n if (i == 1)\n y2 += t;\n if (i == 2)\n y3 += t;\n }\n k++;\n lop(i, 3)\n {\n ll t; cin >> t;\n v[k][i] = t;\n x2 += t;\n if (i == 0)\n y1 += t;\n if (i == 2)\n y3 += t;\n }\n k++;\n lop(i, 3)\n {\n ll t; cin >> t;\n v[k][i] = t;\n x3 += t;\n if (i == 0)\n y1 += t;\n if (i == 1)\n y2 += t;\n }\n ll temp = max(max(max(x1, x2), x3), max(max(y1, y2), y3));\n if (temp - x1 == 0 || temp - x2 == 0 || temp - x3 == 0)\n temp++;\n ll ans1 = temp - x1;\n ll ans2 = temp - x2;\n ll ans3 = temp - x3;\n while (ans1 + ans2 + ans3 != temp)\n {\n temp++;\n ans1 = temp - x1;\n ans2 = temp - x2;\n ans3 = temp - x3;\n\n }\n cout << ans1 << \" \" << v[0][1] << \" \" << v[0][2] << endl;\n cout << v[1][0] << \" \" << ans2 << \" \" << v[1][2] << endl;\n cout << v[2][0] << \" \" << v[2][1] << \" \" << ans3 << endl;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "6156088d8da027114c0f6cf14227afbd", "src_uid": "0c42eafb73d1e30f168958a06a0f9bca", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": " #include \n using namespace std;\n int main()\n { \n long long a,k2=0,k3=0;\n cin>>a;\n for (;;)\n {\n if ((a%10)==4) k2++;\n if ((a%10)==7) k3++;\n if (a<=10) break;\n a=a/10;\n }\n if (k2+k3==7 || k2+k3==4)\n cout<<\"YES\";\n else cout<<\"NO\";\n return 0;\n }\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "2764e81ed5cfbf7ebceb53622ac9baec", "src_uid": "33b73fd9e7f19894ea08e98b790d07f1", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main()\n{\n double ha,ma;\n int h,m;\n char c;\n scanf(\"%d%c%d\",&h,&c,&m);\n h=h%12;\n/* if(h > 12)\n ha=(h*15.0);\n else*/ \n ha=(h*30.0); \n //if(ha == 360 || ha == 180)\n // ha=0;\n \n/* if(h > 12)\n ha+=(m*0.245);\n else */\n ha+=(m*0.5); \n ma=(6.0*m);\n printf(\"%.1lf %.lf\\n\",ha,ma);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "38b20b50df845efe0dbe54977dd27941", "src_uid": "175dc0bdb5c9513feb49be6644d0d150", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint n,m;\nint main()\n{\n int ans=0;\n scanf(\"%d %d\",&n,&m);\n if(n==1 || m==1)\n {\n printf(\"%d\\n\",max(n,m));\n return 0;\n }\n if(n==2 && m==2)\n {\n printf(\"4\\n\");\n return 0;\n }\n if(n>2 && m>2)\n {\n printf(\"%d\\n\",(n*m+1)/2);\n return 0;\n }\n if(n==2)\n {\n ans=m/4;\n ans*=4;\n m%=4;\n if(m>=2)\n ans+=4;\n else if(m==1)\n ans+=2;\n printf(\"%d\\n\",ans);\n return 0;\n }\n if(m==2)\n {\n ans=n/4;\n ans*=4;\n n%=4;\n if(n>=2)\n ans+=4;\n else if(n==1)\n ans+=2;\n printf(\"%d\\n\",ans);\n return 0;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "constructive algorithms", "implementation"], "code_uid": "e49d424a337ea6a8158a98f8e3a36e25", "src_uid": "e858e7f22d91aaadd7a48a174d7b2dc9", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define ll long long\nint main()\n{\n ll n,k;\n cin>>n>>k;\n ll rounding= powl(10,k);\n int cntfive=0;\n ll temp=n;\n if(n%rounding==0)\n {\n cout<cntfive)\n needfive=k-cntfive;\n //cout<\n#include\n#include\nusing namespace std;\nbool isdistict(string x)\n{\n for (int i = 0; i < x.length(); i++)\n {\n for (int j = i + 1; j < x.length(); j++)\n {\n if (x[i] == x[j])return false;\n }\n }\n return true;\n}\nint main()\n{\n int n;\n cin >> n;\n while (true)\n {\n n = n + 1;\n stringstream ss;\n ss << n;\n string x = ss.str();\n\n if (isdistict(x))\n {\n cout << n;\n break;\n }\n }\n}", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "32396b63f8eeb6f5d2dce98ad3a8225d", "src_uid": "d62dabfbec52675b7ed7b582ad133acd", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main(){\nint k,n,a[100],i;\ncin>>n>>k;\nfor(i=0;i>a[i];\nfor(i=0;i\nusing namespace std;\ntypedef long long ll;\nll INF=1000000000010;\nchar oper[10];\nll func2(ll a,ll b) {\n\tif(oper[2]=='+') return a+b;\n\treturn a*b;\n} \nll func3(ll a,ll b,ll c) {\n\tll ans=INF;\n\tif(oper[1]=='+') {\n\t\tans=min(ans,func2(a+b,c));\n\t\tans=min(ans,func2(b+c,a));\n\t\tans=min(ans,func2(a+c,b));\n\t}\n\telse {\n\t\tans=min(ans,func2(a*b,c));\n\t\tans=min(ans,func2(b*c,a));\n\t\tans=min(ans,func2(a*c,b));\n\t}\n\treturn ans;\n}\nll func4(ll a,ll b,ll c,ll d) {\n\tll ans=INF;\n\tif(oper[0]=='+') {\n\t\tans=min(ans,func3(a+b,c,d));\n\t\tans=min(ans,func3(b+c,a,d));\n\t\tans=min(ans,func3(a+c,b,d));\n\t\tans=min(ans,func3(a+d,c,b));\n\t\tans=min(ans,func3(b+d,a,c));\n\t\tans=min(ans,func3(c+d,a,b));\n\t}\n\telse {\n\t\tans=min(ans,func3(a*b,c,d));\n\t\tans=min(ans,func3(b*c,a,d));\n\t\tans=min(ans,func3(a*c,b,d));\n\t\tans=min(ans,func3(a*d,b,c));\n\t\tans=min(ans,func3(b*d,a,c));\n\t\tans=min(ans,func3(c*d,a,b));\n\t}\n\treturn ans;\n}\nint main() {\n\tll a,b,c,d;\n\tcin>>a>>b>>c>>d;\n\tfor(int i=0;i<3;i++) cin>>oper[i];\n\tll ans=func4(a,b,c,d);\n\tcout<\n#define watch(x) cout << (#x) << \" is \" << (x) << endl\ntypedef long long ll;\nusing namespace std;\nint main() {\n ll n, L;\n cin >> n >> L;\n vector cost(34, 4e18);\n for(int i = 0; i < n; i++)\n cin >> cost[i];\n for(int i = 1; i < 34; i++) {\n cost[i] = min(cost[i], cost[i-1]*2);\n }\n auto compute = [&] (ll x) {\n ll idx = 0, res = 0;\n while(x > 0) {\n if (x % 2 == 1)\n res += cost[idx];\n x /= 2;\n idx += 1;\n }\n return res;\n };\n ll L2 = L;\n ll ans = compute(L2);\n while(L2 <= max(L*2, 1LL<<(n+1))) {\n L2 += L2 & (-L2);\n // cout << \"res \" << compute(L2)\n ans = min(ans, compute(L2));\n\n }\n cout << ans << endl;\n return 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["greedy", "dp", "bitmasks"], "code_uid": "8d1080740ffd0dbd578d7e7f8a916bdb", "src_uid": "04ca137d0383c03944e3ce1c502c635b", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n int maxa=0,n,a;\n cin>>n;\n while(n--)\n {\n cin>>a;\n maxa=max(maxa,a);\n }\n if(maxa<25) cout<<0;\n else cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#ifdef __GNUC__\ntypedef long long ll;typedef unsigned long long ull;\n#else\ntypedef __int64 ll; typedef unsigned __int64 ull;\n#endif\n\n#define INF 1<<28\n#define SIZE 100\n#define PI 2*acos(0)\n#define REP(i,n) for (i=0;i=0;i--)\n#define FOR(i,p,k) for (i=p; i\" <<#x<<\": \"< pii;\ntypedef pair pdd;\ntypedef vector vi;\ntypedef vectorvd;\ntypedef vectorvll;\ntypedef vector vs;\ntypedef vectorvvi;\ntypedef vectorvvll;\ntypedef vectorvvd;\ntypedef vectorvpii;\ntypedef map msi;\ntypedef mapmii;\ntypedef mapmpi;\n\ntemplate inline T euclide(T a,T b,T &x,T &y)\n{if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}\nif(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}\nif(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}\ntemplate inline void checkmin(T &a,T b){if(b inline void checkmax(T &a,T b){if(b>a) a=b;}\ntemplate T Abs(T x) {return x > 0 ? x : -x;}\ntemplate inline T sqr(T x){return x*x;}\ntemplate inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}\ntemplate inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.\ntemplate string toString(T n){ostringstream oss;oss<>r;return r;}\nll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}\ntemplate void debug(const T& e){cout< void debug(const T1& e1,const T2& e2){cout< void debug(const T1& e1,const T2& e2,const T3& e3){cout< void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout< void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout< void debug(vector& e){int i;REP(i,SZ(e)) cout< void debug(vector< basic_string >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout< void debug(vector< vector >& e,int row,int col){int i,j;REP(i,row) {REP(j,col) cout< void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout< void debug(T e[],int row){int i;REP(i,row) cout<0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;}\n//compute b^p%m\nint BigMod(ll B,ll P,ll M){ ll R=1; while(P>0) {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (int)R;}\n/*calculates (a*b)%c taking into account that a*b might overflow */\nll mulmod(ll a,ll b,ll c){ ll x = 0,y=a%c; while(b>0){ if(b%2 == 1){x=(x+y)%c;} y=(y*2)%c;b/= 2;}return x%c;}\n//print a number in binary format with n length\nvoid binprint(int mask,int n){int i;string s=\"\";do{s+=(mask%2+'0');mask/=2;}while(mask);Reverse(s);s=string(max(n-SZ(s),0),'0')+s;for(i=SZ(s)-n;ib.cost;}};// Min Priority Queue\n//bool comp(pq a,pq b){ return a.cost < b.cost;} //Asc Sort by cost\n\n//int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year\n\n//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction\n//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction\n//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction\n//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction\n\nint convert(int num,int base)\n{\n //debug(num);\n string str=\"\";\n\n while(num)\n {\n str+=num%base+'0';\n num/=base;\n }\n Reverse(str);\n //debug(str,num);\n return toInt(str);\n}\n\nint main()\n{\n //freopen(\"A.in\",\"r\",stdin);\n //freopen(\"A.out\",\"w\",stdout);\n\n int i,j,test,Case=1,k;\n\n while(scanf(\"%d\",&k)==1)\n {\n FOR(i,1,k)\n {\n FOR(j,1,k)\n printf(\"%d \",convert(i*j,k));\n printf(\"\\n\");\n }\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5d7e15399f9ab57e996d226e9e218710", "src_uid": "a705144ace798d6b41068aa284d99050", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ndouble f[1005][1005];\nint main()\n{\n \tint w,b,i,j;\n \tcin>>w>>b;\n \tfor(i=1;i<=w;i++)\n \t\tf[i][0]=1;\n \tfor(j=0;j<=b;j++)\n \t\tf[0][j]=0;\n \tfor(i=1;i<=w;i++)\n\t{\n for(j=1;j<=b;j++)\n\t\t{\n f[i][j]=i*1.0/(i+j)*1.0;\n if(j>=3)\n\t\t\t\tf[i][j]+=f[i][j-3]*(j*1.0/(i+j)*1.0)*((j-1)*1.0/(i+j-1)*1.0)*((j-2)*1.0/(i+j-2)*1.0);\n if(j>=2)\n\t\t\t\tf[i][j]+=f[i-1][j-2]*(j*1.0/(i+j)*1.0)*((j-1)*1.0/(i+j-1)*1.0)*(i*1.0/(i+j-2)*1.0);\n }\n }\n printf(\"%.9lf\",f[w][b]);\n\treturn 0;\n} ", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "games"], "code_uid": "18913f622e371ebbe3821447cbd73601", "src_uid": "7adb8bf6879925955bf187c3d05fde8c", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n\nusing std::string;\n\nstring n;\nint len, sum, temp;\n\nvoid solve(){\n\tn.assign(len, '0');\n\ttemp = sum;\n\tsum -= 1;\n\tn[0] = '1';\n\tfor(int i = len - 1; i >= 0 && sum >= 0; --i){\n\t\tif(sum >= 9){\n\t\t\tsum -= 9;\n\t\t\tn[i] += 9;\n\t\t}else{\n\t\t\tn[i] += sum;\n\t\t\tsum = 0;\n\t\t}\n\t}\n\tprintf(\"%s \", n.c_str());\n\n\tn.assign(len, '0');\n\tsum = temp;\n\tfor(int i = 0; i < len && sum >= 0; ++i){\n\t\tif(sum >= 9){\n\t\t\tsum -= 9;\n\t\t\tn[i] += 9;\n\t\t}else{\n\t\t\tn[i] += sum;\n\t\t\tsum = 0;\n\t\t}\n\t}\n\tprintf(\"%s\\n\", n.c_str());\n\t\n\treturn ;\n}\n\nint main(int argc, char const *argv[])\n{\n\t// freopen(\"input.in\", \"r\", stdin);\n\n\tscanf(\"%d %d\", &len, &sum);\n\n\tif(sum == 0 && len == 1)\n\t\tprintf(\"0 0\\n\");\n\telse if(sum < 1 || sum > 9 * len)\n\t\tprintf(\"-1 -1\\n\");\n\telse\n\t\tsolve();\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "dp", "implementation"], "code_uid": "fe3da483dbb42abf40baa60f0f770a80", "src_uid": "75d062cece5a2402920d6706c655cad7", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\ntypedef long long int ll;\n\nint main(){\n\tll l,r,x,y,k;cin>>l>>r>>x>>y>>k;\n\tfor(ll i=x;i<=y;++i){\n\t\tlong long int ch=i*k;\n\t\tif(l<=ch and ch<=r){\n\t\t\tcout<<\"YES\";\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout<<\"NO\";\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "brute force"], "code_uid": "911f681368b488ba7ca5564b4aa9a69e", "src_uid": "1110d3671e9f77fd8d66dca6e74d2048", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define int long long\n#define all(x) (x).begin(), (x).end()\n#define sz(x) (int)((x).size())\n#define f first\n#define s second\n#define pii pair\n\nusing namespace std;\n\nvoid solve(){\n int n, m;\n cin >> n >> m;\n vector> grt(n), grr(n);\n for(int i = 0; i < m; ++i){\n int a, b;\n cin >> a >> b;\n a--; b--;\n grt[a].push_back(b);\n grr[b].push_back(a);\n }\n int k;\n cin>> k;\n vector p(k);\n for(int i = 0; i < k; ++i) {cin >> p[i]; p[i]--;}\n reverse(all(p));\n vector dist(n, 1e9);\n queue q;\n q.push(p[0]);\n dist[p[0]] = 0;\n while(!q.empty()){\n int v = q.front();\n q.pop();\n for(int u : grr[v]) {\n if(dist[u] == 1e9){\n dist[u] = dist[v] + 1;\n q.push(u);\n }\n }\n }\n int minans = 0, maxans = 0;\n for(int i = 1; i < k; ++i){\n int md = 1e9;\n for(int j : grt[p[i]]){\n if(j == p[i - 1]) continue;\n md = min(md, dist[j] + 1);\n }\n int d = dist[p[i - 1]] + 1;\n if(md < d) minans++, maxans++;\n if(md == d) maxans++;\n\n }\n cout << minans << ' ' << maxans;\n}\n\nsigned main(){\n ios::sync_with_stdio(0); cin.tie(0);\n //freopen(\"input.txt\", \"r\", stdin);\n int t;\n t = 1;\n while(t--) solve();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["shortest paths", "graphs"], "code_uid": "33ca9da04fa4527cad15de9245c74172", "src_uid": "19a0c05eb2d1559ccfe60e210c6fcd6a", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include ;\nusing namespace std;\n\nint main(){\n int a,b,c,x,y,z;\n\n cin>>x>>y>>z;\n a = (x+y-z)/2;\n b = (y+z-x)/2;\n c = (x+z-y)/2;\n\tif(a==0 &&b==0 &&c==0) \n\t\t{\n\t\t\tcout<<\"Impossible\"<=0 && b>=0 && c>=0 && (x+y+z) %2==0) \n cout<\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int mod=1000000007;\n\nll fp(ll num,int pow){\n if (!pow)\n return 1;\n if (pow&1)\n return (fp((num*num)%mod,pow/2)*num)%mod;\n return fp((num*num)%mod,pow/2);\n}\n\nint main()\n{\n string s;\n cin>>s;\n ll ans=0,tmp;\n for (int i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\n\nvoid ff()\n{\n\tstd::ios_base::sync_with_stdio(0);\n\tcin.tie(NULL); cout.tie(NULL);\n}\n\n//#pragma warning (disable : 4996)\nvoid in_out_txt()\n{\n\tfreopen(\"jumping.in\", \"r\", stdin);\n\t//freopen(\"dup.txt\", \"w\", stdout);\n}\n\nint main(){\n\t\n\tff();\n\tlong long x,y,z,low,mid,high,left,right,temp;\n\tcin>>y>>x;\n\tz=(x*(x-1))/2+1;\n\tif(y==1)\n\t{\n\t\tcout<<0;\n\t\treturn 0;\n\t}\n\tif(y==1||y>z)\n\t\tcout<<-1;\n\telse{\n\t\tlow=1,high=x-1;\n\t\t\n\t\twhile(low=right)\n\t\t\t\tlow=mid;\n\t\t\telse \n\t\t\t\thigh=mid;\n\t\t\tif(low==high-1){\n\t\t\t\tif(y>right&&mid==low){\n\t\t\t\t\tmid=high;\n\t\t\t\t\tlow=high;\n\t\t\t\t}\n\t\t\t\telse if(mid==high){\n\t\t\t\t\t\n\t\t\t\t\tmid=low;\n\t\t\t\t\tleft=(mid*(mid+1))/2+1;\n\t\t\t\t\tright=(x*(x-1))/2+1;\n\t\t\t\t\ttemp=x-mid-1;\n\t\t\t\t\tright-=(temp*(temp+1))/2;\n\n\t\t\t\t\tif(y>right)\n\t\t\t\t\t\tlow=high;\n\n\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t\t\n\n\t\t}\n\t\tcout<\nusing namespace std;\n\n#define int long long\n\nsigned main(){\n\tcin.tie(nullptr)->sync_with_stdio(false);\n\tint x,y,m;cin>>x>>y>>m;\n\tif(x>=m||y>=m){\n\t\tcout<<0<<\"\\n\";\n\t\treturn 0;\n\t}\n\tif(x<=0&&y<=0){\n\t\tcout<<-1<<\"\\n\";\n\t\treturn 0;\n\t}\n\tint ans=0;\n\t//fix the negative number x\n\tif(x<0||y<0){\n\t\tif(y\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\n\nconst int INF = 1 << 30;\nconst int N = (int)1e6;\nconst int MODULO = (int)1e9 + 7;\n\n\n\nvoid task() {\n int hy, ay, dy, hm, am, dm, h, d, a;\n scanf(\"%d %d %d\", &hy, &ay, &dy);\n scanf(\"%d %d %d\", &hm, &am, &dm);\n scanf(\"%d %d %d\", &h, &a, &d);\n int ans = (int)1e6;\n for (int ia = ay; ia <= hm + dm; ++ia) {\n for (int id = dy; id <= am; ++id) {\n if (ia <= dm) {\n continue;\n }\n int cnt = hm / (ia - dm) + (hm % (ia - dm) != 0);\n int need = (id >= am ? 0 : max(0, (am - id)*cnt - hy + 1));\n int t_ans = need*h + (ia - ay)*a + (id - dy)*d;\n ans = min(ans, t_ans);\n //cout << ia << ' ' << id << ' ' << cnt << ' ' << need << ' ' << t_ans << endl;\n }\n }\n if (ans == (int)1e6){\n if (ay > hm + dm){\n if (am - dy >= hy) {\n ans = (am - dy - hy + 1)*min(h, d);\n } else\n ans = 0;\n } else {//dy > am\n if (ay - dm <= 0)\n ans = (dm - ay + 1)*a;\n else\n ans = 0;\n }\n }\n printf(\"%d\", ans);\n}\n\nint main() {\n /*\n ios_base::sync_with_stdio(0);\n freopen(\"input.txt\", \"w\", stdout);\n cout << 20000 << endl;\n //code here\n\n for (int i = 0; i < 20000; ++i) {\n cout << 10 << ' '<< 1000 << endl;\n }\n return 0;\n // */\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#else\n //freopen(\"rmq.in\", \"r\", stdin);\n //freopen(\"rmq.out\", \"w\", stdout);\n#endif\n\n\n\n task();\n\n#ifndef ONLINE_JUDGE\n cout << \"\\n---------------------\\n\";\n cout << clock() * 1000 / CLOCKS_PER_SEC << \"ms\";\n#endif\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "421087a968dcfcae37e13846588528c9", "src_uid": "bf8a133154745e64a547de6f31ddc884", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define N 15\nint a[N],b[N];\nint main()\n{\n int n,m;\n cin>>n>>m;\n int i,j;\n for (i=0;i>a[i];\n }\n for (i=0;i>b[i];\n }\n for (i=0;i\n#include \nusing namespace std;\n#define SZ 100000\nint a[SZ + 5], u[SZ + 5], p[SZ + 5], r[SZ + 5];\nmain()\n{\n int n, b;\n cin >> n;\n a[1] = 0;\n for(int i = 2;i <= n; i++) \n {\n for(int k = 2;2 * i - k * (k - 1) > 0; k++)\n if ((2 * i - k * (k - 1)) % (2 * k) == 0) \n {\n b = (2 * i- k * (k - 1)) / (2 * k);\n int e = p[b + k - 1] ^ p[b - 1];\n u[e] = i;\n if (e == 0 && r[i] == 0) r[i] = k;\n }\n int m = 0;\n while (u[m] == i) m++;\n a[i] = m;\n p[i] = p[i - 1] ^ m;\n }\n cout << (a[n] == 0? -1:r[n]);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "games"], "code_uid": "2fb8f611f0d814d90e38c1c55e4c1bee", "src_uid": "63262317ba572d78163c91b853c05506", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint t;\n\tcin>>t;\n\twhile(t--)\n\t{\n\t\tint a,b=1;\n\t\tcin>>a;\n\t\tfor(int i=2;i<=sqrt(a);i++)\n\t\t{\n\t\t\tif(a%i==0)\n\t\t\t{\n\t\t\t\tb=a/i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout<\n#include \n\nusing namespace std;\n\nint n, t, k, d;\nint main(){\n\n cin >> n >> t >> k >> d;\n\n int c1 = ceil((double) n/k);\n double c2 = d/t;\n\n\n if(c1 - c2 > 1){\n cout << \"YES\" << endl;\n } else {\n cout << \"NO\" << endl;\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "bb0014cb38615d25f9fae65d6e17135d", "src_uid": "32c866d3d394e269724b4930df5e4407", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\n\nint main()\n{\n int n;\n double t1 = 0 , k , a[105] , x;\n cin >> n >> k;\n for(int i=0;i> a[i];\n t1 +=a[i];\n }\n t1 /= n;\n x = 2 * n *(k - t1 - 0.5);\n if(x > 0)\n cout << x << endl;\n else\n cout << '0';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "aa7a82a775590de3209fcc6606ec7fca", "src_uid": "f22267bf3fad0bf342ecf4c27ad3a900", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main(){\n\tint n;\n\tcin>>n;\n\tstring str;\n\tcin>>str;\n\tint ans1=0,ans2=0,len=0;\n\t\n\tfor(int i=0;i\n#define int long long\nusing namespace std;\nconst int N=54;\nconst int mod=1e9+7;\nint f[N][N],g[N][N],nf[N][N],inv[N];\nint binpow(int x,int y){\n\tint tich=1;\n\twhile(y){\n\t\tif(y&1){\n\t\t\ttich*=x;\n\t\t\ttich%=mod;\n\t\t}\n\t\tx*=x;\n\t\tx%=mod;\n\t\ty>>=1;\n\t}\n\treturn tich;\n}\nsigned main(){\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tint n,i,j,k,l,m,x,cnt,mul;\n\tcin>>n>>m;\n\tinv[0]=1;\n\tfor(i=1;i<=n;i++){\n\t\tinv[i]=binpow(i,mod-2);\n\t}\n\tmemset(f,0,sizeof(f));\n\tmemset(g,0,sizeof(g));\n\tf[0][1]=1;\n\tfor(i=1;i<=n;i++){\n\t\tfor(j=1;j<=i;j++){\n\t\t\tfor(k=0;k=mod){\n\t\t\t\t\t\tf[k][l]-=mod;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n//\tfor(i=1;i<=n;i++){\n//\t\tfor(j=1;j<=i+1;j++){\n//\t\t\tcout<\n#include \n#include \n#include \n#define ld long double\nusing namespace std;\n\nconst int MAXN = 5001;\nconst long long MODULE = 1000 * 1000 * 1000 + 7;\nconst int N\t= 7;\nconst int M = 1 << N;\nlong long f1[M][M], fc[M][M], nfc[M][M];\nlong long f[M], f2[M];\nconst int LG = 17;\nint msk;\n\nvoid merg(long long a[M][M], long long b[M][M])\n{\n\tfor (int i = 0; i < msk; i++)\n\t{\n\t\tfor (int j = 0; j < msk; j++)\n\t\t{\n\t\t\tfc[i][j] = 0;\n\t\t\tfor (int l = 0; l < msk; l++)\n\t\t\t{\n\t\t\t\tfc[i][j] = (fc[i][j] + a[i][l] * b[l][j]) % MODULE;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nvoid copyf()\n{\n\tfor (int j = 0; j < msk; j++)\n\t{\n\t\tfor (int l = 0; l < msk; l++)\n\t\t{\n\t\t\tnfc[j][l] = fc[j][l];\n\t\t}\n\t}\n}\n\nint main()\n{\n\tfor (int i = 0; i < M; i++) f[i] = 0;\n\tf[0] = 1;\n\tmsk = 1;\n\tfor (int i = 1; i <= N; i++)\n\t{\n\t\tint w;\n\t\tcin >> w;\n\t\tfor (int j = 0; j < msk; j++)\n\t\t{\n\t\t\tf[j + msk] = f[j];\n\t\t\tf[j] = 0;\n\t\t}\n\t\tmsk *= 2;\n\t\tfor (int j = 0; j < msk; j++)\n\t\t{\n\t\t\tfor (int l = 0; l < msk; l++)\n\t\t\t{\n\t\t\t\tlong long n0 = 0, n1 = 1;\n\t\t\t\tfor (int k = 0; k < i; k++)\n\t\t\t\t{\n\t\t\t\t\tlong long v0 = n0 + n1, v1 = n0;\n\t\t\t\t\tif (((j & (1 << k)) == 0) || ((l & (1 << k)) == 0)) v1 += n1;\n\t\t\t\t\tn0 = v0, n1 = v1;\n\t\t\t\t}\n\t\t\t\tf1[j][l] = n1;\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < msk; j++)\n\t\t{\n\t\t\tfor (int l = 0; l < msk; l++)\n\t\t\t{\n\t\t\t\tfc[j][l] = 0;\n\t\t\t}\n\t\t\tfc[j][j] = 1;\n\t\t}\n\t\tfor (int pp = LG; pp >= 0; pp--)\n\t\t{\n\t\t\tcopyf();\n\t\t\tmerg(nfc, nfc);\n\t\t\tif ((1 << pp) & w)\n\t\t\t{\n\t\t\t\tcopyf();\n\t\t\t\tmerg(nfc, f1);\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < msk; j++)\n\t\t{\n\t\t\tf2[j] = 0;\n\t\t\tfor (int l = 0; l < msk; l++)\n\t\t\t{\n\t\t\t\tf2[j] = (f2[j] + f[l] * fc[l][j]) % MODULE;\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < msk; j++) f[j] = f2[j];\n\t}\n\tcout << f[(1 << N) - 1] << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "8a8cfceb1d45921106f3bd8c77e6db42", "src_uid": "a4bda63b95dc14185c47a08652fe41bd", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\n\nint main()\n{\n\tint n ;\n\tstring s ;\n\tcin >> n ;\n\tgetline(cin,s);\n\tint week[] = {52,52,52,52,53,53,52} ;\n\tif(s == \" of week\")\n\t\tcout << week[n-1] << endl ;\n\telse\n\t{\n\t\tif(n == 30)\n\t\t\tcout << 11<\n#include\n#include\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate \nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n#define rnd(x, y) uniform_int_distribution(x, y)(rng)\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n // http://xorshift.di.unimi.it/splitmix64.c\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n};\n#define boost ios_base::sync_with_stdio(false);cin.tie(nullptr);\n#define rep(i, x, n) for(ll i = x; i < n; i++)\n#define rrep(i, x, n) for(ll i = x; i >= n; i--)\n#define all(v) v.begin(),v.end()\n#define rall(v) v.rbegin(),v.rend()\n#define ff first\n#define ss second\n#define pb push_back\n#define eb emplace_back\n#define e emplace\n#define mp make_pair\n#define debug(this) cout<<\"> \"<<#this<<\" : \"< pii;\ntypedef vector< ll > vi;\ntypedef vector< vi > vvi;\nconst ll MOD = 1e9+7;\nld PI=3.1415926535897932384626;\nld EPS = 1e-9;\n#define endl '\\n'\n\nint main() {\n\tboost;\n\tll n, x, y;\n\tcin >> n;\n\tll mx = -1, my = -1, mnx = 100, mny = 100;\n\tvector cord;\n\tmap x_cord, y_cord;\n\trep(i, 0, (4*n+1)) {\n\t\tcin >> x >> y;\n\t\tcord.eb(x, y);\n\t\tif(++y_cord[y] > 1) my = max(my, y), mny = min(mny, y);\n\t\tif(++x_cord[x] > 1) mnx = min(mnx, x), mx = max(mx, x);\n\t}\n\tfor(pii &p : cord) {\n\t\tif(p.ff > mx or p.ff < mnx or p.ss > my or p.ss < mny or (p.ff != mnx and p.ff != mx and p.ss != mny and p.ss != my)) {\n\t\t\tcout << p.ff << \" \" << p.ss << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "51d8fd02f5f19a047ee973adfec510f7", "src_uid": "1f9153088dcba9383b1a2dbe592e4d06", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n\nusing namespace std;\n\n\nint main() {\n\tint n;\n\tcin >> n;\n\tint first,last;\n\tfor(int i=0;i> x;\n\t\tif(i==0)\n\t\t\tfirst=x;\n\t\tif(i==n-1)\n\t\t\tlast=x;\n\t}\n\tif((first%2)*(last%2)*(n%2)!=0)\n\t\tcout << \"Yes\" << endl;\n\telse\n\t\tcout << \"No\" << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "7ae49b94d2a300fa59f3e14dc978b185", "src_uid": "2b8c2deb5d7e49e8e3ededabfd4427db", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\nsetst;\nstring str;\nint k;\nvectorperm;\n/*for combinations */\n\nvoid fun(string s,int pos)\n{ \n if(s.size()==2&&st.count(s)==0)\n return;\n \n \n \n int i=pos,c,flag=0;\n \n if(s.size()==k)\n {\n perm.push_back(s);return;\n }\n \n for(c=1;c<=str.size();c++)\n {\n \n fun(s+str[i],(i+1)%str.size());\n \n i++;\n i=i%str.size();\n }\n}\n\nint main() {\n\n\tstr=\"abcdef\";\n\tcin>>k;\n\tstring temp;\n\tint n,i;\n\tstring s1,s2;\n\tmapmp;\n\tmap::iterator it;\n\t\n\tcin>>n;\n\tfor(i=1;i<=n;i++)\n\t{\n\t cin>>s1>>s2;\n\t st.insert(s1);\n\t mp[s1]=s2;\n\t}\n\t\t fun(temp,0);\n\n\nstring s3;\nint count=0;\n\n for(i=0;i\nusing namespace std;\n\ntypedef long double db;\nconst db eps=1e-12;\n\nstruct Point\n{\n db x,y;\n Point(){}\n Point(db _x,db _y):x(_x),y(_y){}\n Point operator + (const Point &t)const\n {\n return Point(x+t.x,y+t.y);\n }\n Point operator - (const Point &t)const\n {\n return Point(x-t.x,y-t.y);\n }\n Point operator * (const db &t)const\n {\n return Point(x*t,y*t);\n }\n Point operator / (const db &t)const\n {\n return Point(x/t,y/t);\n }\n bool operator < (const Point &t)const\n {\n return fabs(x-t.x) operator & (const Circle &c1,const Circle &c2)\n {\n db d=(c1.o-c2.o).len();\n if(d>c1.r+c2.r+eps || d();\n db dt=(c1.r*c1.r-c2.r*c2.r)/d,d1=(d+dt)/2;\n Point dir=(c2.o-c1.o)/d,pcrs=c1.o+dir*d1;\n dt=sqrt(max(0.0L,c1.r*c1.r-d1*d1)),dir=dir.rot90();\n return vector{pcrs+dir*dt,pcrs-dir*dt};\n }\n}p[5];\n\nstruct DSU\n{\n int fa[5];\n void init(int n)\n {\n for(int i=1;i<=n;i++)\n fa[i]=i;\n }\n int find(int x)\n {\n return fa[x]==x ? x : fa[x]=find(fa[x]);\n }\n void merge(int x,int y)\n {\n x=find(x),y=find(y);\n if(x!=y)fa[x]=y;\n }\n}dsu;\n\nint main()\n{\n int n;\n scanf(\"%d\",&n);\n for(int i=1;i<=n;i++)\n cin>>p[i].o.x>>p[i].o.y>>p[i].r;\n vector all;\n dsu.init(n);\n int e=0;\n for(int i=1;i<=n;i++)\n {\n vector pat;\n for(int j=1;j<=n;j++)if(i!=j)\n {\n vector tmp=p[i]&p[j];\n if(!tmp.empty())dsu.merge(i,j);\n for(int k=0;k<(int)tmp.size();k++)\n all.push_back(tmp[k]),pat.push_back(tmp[k]);\n }\n sort(pat.begin(),pat.end());\n e+=unique(pat.begin(),pat.end())-pat.begin();\n }\n sort(all.begin(),all.end());\n int v=unique(all.begin(),all.end())-all.begin(),c=0;\n for(int i=1;i<=n;i++)\n c+=(dsu.find(i)==i);\n cout<\n#include \nusing namespace std;\n// priority_queue< ii, vector, greater > pq; pq.push pq.pop pq.top;\n// priority_queue por default ordena decrescente\n \n#define INF 0x3f3f3f3f\n#define LINF 0x3f3f3f3f3f3f3f3fLL\n#define NVI -1\n#define db if(0)\n#define pb push_back\n#define EPS 1e-9\n#define fst first\n#define scd second\n#define mp make_pair\n#define fr(a,b,c) for(int a=b;a vi;\ntypedef vector vll;\ntypedef vector > vvi;\ntypedef pair ii;\ntypedef vector vii;\ntypedef vector vvii;\ntypedef vector > > ve;\ntypedef pair dd;\n\nint n,cols[110], lines[110];\nint res[110];\n\nint main() {\n\tscanf(\"%d\", &n);\n\tfr(i,0,n) {\n\t\tres[i]=0;\n\t\tscanf(\"%d\", &cols[i]);\n\t\tfr(j,0,cols[i]) lines[j]++;\n\t}\n\t/*fr(i,0,n) {\n\t\tfr(j,0,)\n\t}*/\n\tint i = 0;\n\twhile(lines[i] > 0) {\n\t\tint col = n-1;\n\t\tfr(j,0,lines[i]) {\n\t\t\tres[col--]++;\n\t\t}\n\t\ti++;\n\t}\n\tcout << res[0];\n\tfr(i,1,n) cout << \" \" << res[i];\n\tcout << \"\\n\";\n}\n// 1445694154702", "lang_cluster": "C++", "tags": ["sortings", "implementation", "greedy"], "code_uid": "aabbad798f4d11ee8167136a47e822bd", "src_uid": "ae20712265d4adf293e75d016b4b82d8", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint main()\n{\n int a,x,y;\n cin>>a>>x>>y;\n if(x>=a||x<=-a)\n cout<<-1;\n else if(y==0||y%a==0)\n cout<<-1;\n else\n {\n if(y(-((a+1)/2)))\n cout<<1;\n else\n cout<<-1;\n }\n\n else\n {\n y-=a;\n int b=y/a;\n if(b%2==1)\n {\n if(x==0)\n cout<<-1;\n else if(x<0)\n cout<<3*(b-1)/2+2+1;\n else\n cout<<3*(b-1)/2+2+2;\n }\n else\n {\n if(x<(a+1)/2&&x>(-((a+1)/2)))\n cout<<3*b/2+2;\n else\n cout<<-1;\n }\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "6a42ede6b2f915872016059bdbee6f7a", "src_uid": "cf48ff6ba3e77ba5d4afccb8f775fb02", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS 1\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\nint gcd(int x, int y)\n{\n for (int i = min(x, y); i >= 1;i--)\n if (x%i == 0 && y%i == 0)\n return i;\n}\n\nint main()\n{\n int n,sum=0;\n cin >> n;\n \n for (int i = 1; i < n;i++)\n if (gcd(n - 1, i) == 1) sum++;\n\n cout << sum;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "number theory"], "code_uid": "0e522abf4f4d9d10401a85dec222fc6f", "src_uid": "3bed682b6813f1ddb54410218c233cff", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint main()\n{\n\tint y,w;\n\tscanf(\"%d%d\",&y,&w);\n\tswitch(max(y,w))\n\t{\n\t\tcase 1:printf(\"1/1\");break;\n\t\tcase 2:printf(\"5/6\");break;\n\t\tcase 3:printf(\"2/3\");break;\n\t\tcase 4:printf(\"1/2\");break;\n\t\tcase 5:printf(\"1/3\");break;\n\t\tcase 6:printf(\"1/6\");break;\n\t\tdefault:break;\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities"], "code_uid": "abd725998c23411cd0509423b5a037ea", "src_uid": "f97eb4ecffb6cbc8679f0c621fd59414", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;int n;\nint main(){cin>>n;cout<<(2*n+1)/3;}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "be61c3ac30e3a96d10e0502467321ff9", "src_uid": "a993069e35b35ae158d35d6fe166aaef", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define lli long long int\n#define v vector \n#define vv vector\n#define p pair\n#define arrayinput int n; int m; cin>>n>>m; v arr (n+1,-1); for (int i=0; i>temp;arr[i]=temp;}\nint z=1000000007;\nint main() {\n\tlli n,m;\n\tcin>>m>>n;\n\tlli count=0;\n\tif (n>m)\n\t\tswap(n,m);\n\tif (n==1)\n\t{\n\t\tif (m%6==1)\n\t\t\tcout<<(m-1);\n\t\telse if (m%6==2)\n\t\t\tcout<<(m-2);\n\t\telse if (m%6==3)\n\t\t\tcout<<(m-3);\n\t\telse if (m%6==4)\n\t\t\tcout<<(m-2);\n\t\telse if (m%6==5)\n\t\t\tcout<<(m-1);\n\t\telse cout<\n#include \n#include \n#include \n#include \n#include \n#define ull unsigned long long int\n#define ll long long int\nusing namespace std;\n\n\nint main()\n{\n\tll n,m;\n\tcin>>n>>m;//try distributing one each to all partitions first etc etc for the minimum case;\n\tll z=n;\n\t//int x=n-(int)ceil(n*1.0/m)*(m-1);\n\t\t\n\tll s=n%m*(n/m+1)*(n/m)/2+(m-n%m)*(n/m)*(n/m-1)/2;\n\t\n\t\n\tcout<\n\n#include \n#include \n\n#define sf scanf\n#define pf printf\n#define pb push_back\n#define mp make_pair\n#define PI ( acos(-1.0) )\n#define IN freopen(\"hard1.txt\",\"r\",stdin)\n#define OUT freopen(\"hard1.txt\",\"w\",stdout)\n#define FOR(i,a,b) for(i=a ; i<=b ; i++)\n#define DBG pf(\"Hi\\n\")\n#define i64 long long int\n#define eps (1e-8)\n#define xx first\n#define yy second\n#define ln 17\n#define off 1000005\n#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL)\n\n\nusing namespace __gnu_pbds;\nusing namespace std ;\n\ntypedef tree< i64, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\ntypedef pair pii ;\n\n#define INC 0\n#define DEC 1\n#define maxn 200005\n#define INF 1000000000\n\nint yes[505][505] ;\nvector g[maxn] ;\nint b[maxn] ;\n\nbool check(vector v)\n{\n for(int i=0 ; i>n>>m ;\n\n for(int i=1 ; i<=m ; i++)\n {\n int u , v ;\n cin>>u>>v ;\n// scanf(\"%d %d\",&u,&v) ;\n g[u].pb(v) ; g[v].pb(u) ;\n yes[u][v] = 1 ; yes[v][u] = 1 ;\n }\n\n for(int i=1 ; i<=n ; i++)\n {\n if( g[i].size() == n-1 ) b[i] = 1 ;\n }\n\n\n vector a ;\n\n for(int i=1 ; i<=n ; i++)\n {\n if( b[i] != 1 )\n {\n a.pb(i) ;\n b[i] = -1 ;\n for(int j=0 ; j c ;\n for(int i=1 ; i<=n ; i++)\n {\n if( b[i]==0 ) c.pb(i) ;\n }\n\n bool flag = 1 ;\n for(int i=0 ; i\nusing namespace std;\n\n#define sz(c) (c).size()\n#define pb push_back\n#define all(c) (c).begin(), (c).end()\n#define tr(c, i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define present(c, x) ((c).find(x) != (c).end())\n#define cpresent(c, x) (find(all(c), x) != (c).end())\n#define REP(i, n) for(int i = 0; i < (n); i++)\n#define REPD(i, n) for(int i = (n)-1; i >= 0; i--)\n#define For(i, a, b) for(int i = (a); i <= (b); i++)\n#define Ford(i, a, b) for(int i = (a); i >= (b); i--)\n#define mp make_pair\n#define ll long long\n#define ull unsigned long long\n#define modulo 1000000007\n#define boostio std::ios::sync_with_stdio(false);cin.tie(NULL);\n#define tt int n;cin>>n;while(n--)\n\nint main()\n{\nboostio;\n\nvector v1;\nll n;\n\ncin>>n;\nif(n==0)\n cout<<1<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ll long long\nusing namespace std;\n\nint main()\n{\n\tint x, y, z, a, b, c;\n\tcin >> x >> y >> z >> a >> b >> c;\n\ta -= x, x = 0;\n\twhile (y)\n\t{\n\t\tif (a > 0)\n\t\t\ta--;\n\t\telse if (b)\n\t\t\tb--;\n\t\telse\n\t\t\tbreak;\n\t\ty--;\n\t}\n\twhile (z)\n\t{\n\t\tif (a > 0)\n\t\t\ta--;\n\t\telse if (b)\n\t\t\tb--;\n\t\telse if (c)\n\t\t\tc--;\n\t\telse\n\t\t\tbreak;\n\t\tz--;\n\t}\n\tif (a >= 0 && b >= 0 && c >= 0 && !x && !y && !z)\n\t\tcout << \"YES\";\n\telse\n\t\tcout << \"NO\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "greedy", "implementation"], "code_uid": "0d973477ca58250a942e8853017f0cf5", "src_uid": "d54201591f7284da5e9ce18984439f4e", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \r\nusing namespace std;\r\nconst double PI = acos(-1.0);\r\nstruct Complex{\r\n double x,y;\r\n Complex(double _x = 0.0,double _y = 0.0){\r\n x = _x;\r\n y = _y;\r\n }\r\n Complex operator-(const Complex &b)const{\r\n return Complex(x - b.x,y - b.y);\r\n }\r\n Complex operator+(const Complex &b)const{\r\n return Complex(x + b.x,y + b.y);\r\n }\r\n Complex operator*(const Complex &b)const{\r\n return Complex(x*b.x - y*b.y,x*b.y + y*b.x);\r\n }\r\n};\r\nvoid change(Complex y[],int len){\r\n int i,j,k;\r\n for(int i = 1,j = len/2;i= k){\r\n j = j - k;\r\n k = k/2;\r\n }\r\n if(j < k) j+=k;\r\n }\r\n}\r\nvoid fft(Complex y[],int len,int on){\r\n change(y,len);\r\n for(int h = 2;h <= len;h<<=1){\r\n Complex wn(cos(on*2*PI/h),sin(on*2*PI/h));\r\n for(int j = 0;j < len;j += h){\r\n Complex w(1,0);\r\n for(int k = j;k < j + h/2;k++){\r\n Complex u = y[k];\r\n Complex t = w*y[k + h/2];\r\n y[k] = u + t;\r\n y[k + h/2] = u - t;\r\n w = w*wn;\r\n }\r\n }\r\n }\r\n if(on == -1){\r\n for(int i = 0;i < len;i++){\r\n y[i].x /= len;\r\n }\r\n }\r\n}\r\nclass BigInt\r\n{\r\n#define Value(x, nega) ((nega) ? -(x) : (x))\r\n#define At(vec, index) ((index) < vec.size() ? vec[(index)] : 0)\r\n static int absComp(const BigInt &lhs, const BigInt &rhs)\r\n {\r\n if (lhs.size() != rhs.size())\r\n return lhs.size() < rhs.size() ? -1 : 1;\r\n for (int i = lhs.size() - 1; i >= 0; --i)\r\n if (lhs[i] != rhs[i])\r\n return lhs[i] < rhs[i] ? -1 : 1;\r\n return 0;\r\n }\r\n using Long = long long;\r\n const static int Exp = 9;\r\n const static Long Mod = 1000000000;\r\n mutable std::vector val;\r\n mutable bool nega = false;\r\n void trim() const\r\n {\r\n while (val.size() && val.back() == 0)\r\n val.pop_back();\r\n if (val.empty())\r\n nega = false;\r\n }\r\n int size() const { return val.size(); }\r\n Long &operator[](int index) const { return val[index]; }\r\n Long &back() const { return val.back(); }\r\n BigInt(int size, bool nega) : val(size), nega(nega) {}\r\n BigInt(const std::vector &val, bool nega) : val(val), nega(nega) {}\r\n\r\npublic:\r\n friend std::ostream &operator<<(std::ostream &os, const BigInt &n)\r\n {\r\n if (n.size())\r\n {\r\n if (n.nega)\r\n putchar('-');\r\n for (int i = n.size() - 1; i >= 0; --i)\r\n {\r\n if (i == n.size() - 1)\r\n printf(\"%lld\", n[i]);\r\n else\r\n printf(\"%0*lld\", n.Exp, n[i]);\r\n }\r\n }\r\n else\r\n putchar('0');\r\n return os;\r\n }\r\n friend BigInt operator+(const BigInt &lhs, const BigInt &rhs)\r\n {\r\n BigInt ret(lhs);\r\n return ret += rhs;\r\n }\r\n friend BigInt operator-(const BigInt &lhs, const BigInt &rhs)\r\n {\r\n BigInt ret(lhs);\r\n return ret -= rhs;\r\n }\r\n BigInt(Long x = 0)\r\n {\r\n if (x < 0)\r\n x = -x, nega = true;\r\n while (x >= Mod)\r\n val.push_back(x % Mod), x /= Mod;\r\n if (x)\r\n val.push_back(x);\r\n }\r\n BigInt(const char *s)\r\n {\r\n int bound = 0, pos;\r\n if (s[0] == '-')\r\n nega = true, bound = 1;\r\n Long cur = 0, pow = 1;\r\n for (pos = strlen(s) - 1; pos >= Exp + bound - 1; pos -= Exp, val.push_back(cur), cur = 0, pow = 1)\r\n for (int i = pos; i > pos - Exp; --i)\r\n cur += (s[i] - '0') * pow, pow *= 10;\r\n for (cur = 0, pow = 1; pos >= bound; --pos)\r\n cur += (s[pos] - '0') * pow, pow *= 10;\r\n if (cur)\r\n val.push_back(cur);\r\n }\r\n BigInt &operator=(const char *s){\r\n BigInt n(s);\r\n *this = n;\r\n return n;\r\n }\r\n BigInt &operator=(const Long x){\r\n BigInt n(x);\r\n *this = n;\r\n return n;\r\n }\r\n friend std::istream &operator>>(std::istream &is, BigInt &n){\r\n string s;\r\n is >> s;\r\n n=(char*)s.data();\r\n return is;\r\n }\r\n BigInt &operator+=(const BigInt &rhs)\r\n {\r\n const int cap = std::max(size(), rhs.size()) + 1;\r\n val.resize(cap);\r\n int carry = 0;\r\n for (int i = 0; i < cap - 1; ++i)\r\n {\r\n val[i] = Value(val[i], nega) + Value(At(rhs, i), rhs.nega) + carry, carry = 0;\r\n if (val[i] >= Mod)\r\n val[i] -= Mod, carry = 1;\r\n else if (val[i] < 0)\r\n val[i] += Mod, carry = -1;\r\n }\r\n if ((val.back() = carry) == -1) //assert(val.back() == 1 or 0 or -1)\r\n {\r\n nega = true, val.pop_back();\r\n bool tailZero = true;\r\n for (int i = 0; i < cap - 1; ++i)\r\n {\r\n if (tailZero && val[i])\r\n val[i] = Mod - val[i], tailZero = false;\r\n else\r\n val[i] = Mod - 1 - val[i];\r\n }\r\n }\r\n trim();\r\n return *this;\r\n }\r\n friend BigInt operator-(const BigInt &rhs)\r\n {\r\n BigInt ret(rhs);\r\n ret.nega ^= 1;\r\n return ret;\r\n }\r\n BigInt &operator-=(const BigInt &rhs)\r\n {\r\n rhs.nega ^= 1;\r\n *this += rhs;\r\n rhs.nega ^= 1;\r\n return *this;\r\n }\r\n friend BigInt operator*(const BigInt &lhs, const BigInt &rhs)\r\n {\r\n int len=1;\r\n BigInt ll=lhs,rr=rhs;\r\n ll.nega = lhs.nega ^ rhs.nega;\r\n while(len<2*lhs.size()||len<2*rhs.size())len<<=1;\r\n ll.val.resize(len),rr.val.resize(len);\r\n Complex x1[len],x2[len];\r\n for(int i=0;i powTwo{BigInt(1)};\r\n static std::vector estimate;\r\n estimate.clear();\r\n if (absComp(lhs, rhs) < 0)\r\n return BigInt();\r\n BigInt cur = rhs;\r\n int cmp;\r\n while ((cmp = absComp(cur, lhs)) <= 0)\r\n {\r\n estimate.push_back(cur), cur += cur;\r\n if (estimate.size() >= powTwo.size())\r\n powTwo.push_back(powTwo.back() + powTwo.back());\r\n }\r\n if (cmp == 0)\r\n return BigInt(powTwo.back().val, lhs.nega ^ rhs.nega);\r\n BigInt ret = powTwo[estimate.size() - 1];\r\n cur = estimate[estimate.size() - 1];\r\n for (int i = estimate.size() - 1; i >= 0 && cmp != 0; --i)\r\n if ((cmp = absComp(cur + estimate[i], lhs)) <= 0)\r\n cur += estimate[i], ret += powTwo[i];\r\n ret.nega = lhs.nega ^ rhs.nega;\r\n return ret;\r\n }\r\n friend BigInt operator/(const BigInt &num,const Long &x){\r\n bool negat = ( x < 0 );\r\n Long xx = (negat) ? -x : x;\r\n BigInt ret;\r\n Long k = 0;\r\n ret.val.resize( num.size() );\r\n ret.nega = (num.nega ^ negat);\r\n for(int i = num.size() - 1 ;i >= 0; i--){\r\n ret[i] = ( k * Mod + num[i]) / xx;\r\n k = ( k * Mod + num[i]) % xx;\r\n }\r\n ret.trim();\r\n return ret;\r\n }\r\n bool operator==(const BigInt &rhs) const\r\n {\r\n return nega == rhs.nega && val == rhs.val;\r\n }\r\n bool operator!=(const BigInt &rhs) const { return nega != rhs.nega || val != rhs.val; }\r\n bool operator>=(const BigInt &rhs) const { return !(*this < rhs); }\r\n bool operator>(const BigInt &rhs) const { return !(*this <= rhs); }\r\n bool operator<=(const BigInt &rhs) const\r\n {\r\n if (nega && !rhs.nega)\r\n return true;\r\n if (!nega && rhs.nega)\r\n return false;\r\n int cmp = absComp(*this, rhs);\r\n return nega ? cmp >= 0 : cmp <= 0;\r\n }\r\n bool operator<(const BigInt &rhs) const\r\n {\r\n if (nega && !rhs.nega)\r\n return true;\r\n if (!nega && rhs.nega)\r\n return false;\r\n return (absComp(*this, rhs) < 0) ^ nega;\r\n }\r\n void swap(const BigInt &rhs) const\r\n {\r\n std::swap(val, rhs.val);\r\n std::swap(nega, rhs.nega);\r\n }\r\n};\r\nBigInt one[105][10],sb,len[105];\r\nint i,j,k;\r\n\r\nstruct dian{\r\n\tBigInt id;\r\n\tint dis,len;\r\n\tbool operator<(const dian x)const{return dis>x.dis;}\r\n}d,tmp1,tmp2,tmp3;\r\nchar ch[55];\r\n\r\nmapmp;\r\npriority_queue q;\r\n\r\nint main(){\r\n\tlen[1]=\"1\";\r\n\tfor(i=1;i<=9;i++){one[1][i]=one[1][i-1]+\"1\";}\r\n\tfor(i=2;i<=66;i++){\r\n\t\tlen[i]=len[i-1]*10;\r\n\t\tfor(j=1;j<=9;j++){\r\n\t\t\tone[i][j]=one[i-1][j]*10+one[1][j];\r\n\t\t}\r\n\t}\r\n\tcin>>ch+1;\r\n\td.id=ch+1;\r\n\td.len=strlen(ch+1);\r\n\tq.push(d);\r\n\twhile(1){\r\n\t\td=q.top();q.pop();\r\n\t\tif(mp[d.id]){continue;}\r\n\t\telse{mp[d.id]=1;}\r\n\t\tfor(i=1;i<=55;i++){\r\n\t\t\tif(i==55){goto aaa;}\r\n\t\t\tif(d.id=d.id){\r\n\t\t\t\tif(i==1){\r\n\t\t\t\t\ttmp1.id=one[d.len][i]-d.id;\r\n\t\t\t\t\ttmp1.dis=d.dis+d.len*i;\r\n\t\t\t\t\tq.push(tmp1);\r\n\t\t\t\t\tif(one[d.len][i]!=d.id){\r\n\t\t\t\t\t\ttmp2.id=d.id-one[d.len-1][9];\r\n\t\t\t\t\t\ttmp2.dis=d.dis+(d.len-1)*(9);\r\n\t\t\t\t\t\tq.push(tmp2);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\telse{\r\n\t\t\t\t\ttmp1.id=one[d.len][i]-d.id;\r\n\t\t\t\t\ttmp1.dis=d.dis+d.len*i;\r\n\t\t\t\t\tq.push(tmp1);\r\n\t\t\t\t\tif(one[d.len][i]!=d.id){\r\n\t\t\t\t\t\ttmp2.id=d.id-one[d.len][i-1];\r\n\t\t\t\t\t\ttmp2.dis=d.dis+d.len*(i-1);\r\n\t\t\t\t\t\tq.push(tmp2);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\taaa:;\r\n\t}\r\n}", "lang_cluster": "C++", "tags": ["greedy", "dp", "shortest paths"], "code_uid": "408e26997495aa372453562a315eb02e", "src_uid": "1961e7c9120ff652b15cad5dd5ca0907", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int MAXN = 1e5 + 113;\n\n\nint a[1000 + 113];\n\n\nsigned main()\n{\n\tint sum = 0;\n\tfor (int i = 1; i <= 6; ++i)\n\t{\n\t\tcin >> a[i];\n\t\tsum += a[i];\n\t}\n\tfor (int i = 1; i <= 6; ++i)\n\t{\n\t\tfor (int j = i+1; j <= 6; ++j)\n\t\t{\n\t\t\tfor (int k = j+1; k <= 6; ++k)\n\t\t\t{\n\t\t\t\tif ((a[i] + a[j] + a[k]) * 2 == sum)\n\t\t\t\t{\n\t\t\t\t\tcout << \"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << \"NO\";\n\t//system(\"pause\");\n\treturn 0;\n\n\t\n}", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "49b699f2a4e9fc0a950a35deffc78f33", "src_uid": "2acf686862a34c337d1d2cbc0ac3fd11", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*\nY Y # # # # # ############# # | # ############# # # # # #\n Y Y A # # # # # # # # # | # # # # # # # # #\n Y Y A # # # # # # # # # | # # # # # # # # #\n Y Y A # # # # # # # # # | # # # # # # # # #\n Y Y A # # # # # # # # # | # # # # # # # # #\n YAAAAAAAAAA # ########### ############# # ########### | ########### # ############# ########### #\n Y A # # # # # # # # | # # # # # # # #\n Y A # # # # # # # # | # # # # # # # #\n Y A # # # # # # # # | # # # # # # # #\n Y A # # # # # # # # | # # # # # # # #\nY A # # # # # ############# # # | # # ############# # # # # #\n###########################################################################################################\n##.#####.####.#######.######.#.......#######.################.#######.######.#.#######.#........#......####\n###.###.####.#.######.######.####.#########.#.##############.#.######.######.#..#####..#.########.#####.###\n####.#.####.###.#####.######.####.########.###.############.###.#####.######.#.#.###.#.#.########.######.##\n#####.####.......####........####.#######.......##########.......####........#.#.###.#.#........#.######.##\n#####.###.#######.###.######.####.######.#######.########.#######.###.######.#.##.#.##.#.########.######.##\n#####.##.#########.##.######.####.#####.#########.######.#########.##.######.#.##.#.##.#.########.#####.###\n#####.#.###########.#.######.#.......#.###########.####.###########.#.######.#.###.###.#........#......####\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n######################..##..###..#..#..#..#..##..###########...#########...###...........##################\n#####################..##..####..#..#..#..##..##..##########...#########...###...........##################\n####################..##..#####..#..#..#..###..##..#########...#########...#######...######################\n###################..##..######..#..#..#..####..##..########...#########...#######...######################\n##################..##..#######..#..#..#..#####..##..#######...#########...#######...######################\n##################..##..#..####...........#####..##..#######...............#######...######################\n##################..##..##..###...........#####..##..#######...............#######...######################\n##################..##..###..##...........#####..##..#######...#########...#######...######################\n###################..##..###..#...........####..##..########...#########...#######...######################\n####################..##..###.............###..##..#########...#########...#######...######################\n#####################..##..###............##..##..##########...#########...###...........##################\n######################..##..###..........##..##..###########...#########...###...........##################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n####..............######.............######...#########...###...........######............#################\n####..............#####...............######...#######...####............#####............#################\n####...################...#########...#######...#####...#####...#######...########....#####################\n####...################...#########...########...###...######...#######...########....#####################\n####...################...######################.....########...#######...########....#####################\n####..............#####...#######################...#########...#######...########....#####################\n####..............#####...####.......############...#########............#########....#####################\n####...################...####.......############...#########...........##########....#####################\n####...################...#########...###########...#########...##################....#####################\n####...################...#########...###########...#########...##################....#####################\n####..............#####...............###########...#########...##################....#####################\n####..............######.............############...#########...##################....#####################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n###########################################################################################################\n*/\n#pragma GCC optimize (\"O3\")\n#include \n#include \n#include \nusing namespace std;\nint dp[2001][2001];\nint n,k;\nint fun(int idx,int l){\n if(idx==k+1){\n return 1;\n }\n if(dp[l][idx]!=-1){\n return dp[l][idx];\n }\n int b=0;\n for(int i=1;i*l<=n;i++){\n b=(fun(idx+1,i*l)+b);\n if(b>int(1e9+7))\n b-=int(1e9+7);\n }\n dp[l][idx]=b;\n if(dp[l][idx]>int(1e9+7)){\n dp[l][idx]-=int(1e9+7);\n b-=int(1e9+7);\n }\n return b;\n}\nint b=n+1;\nint c=0;\nint main()\n{\n memset(dp,-1,sizeof dp);\n scanf(\"%d %d\",&n,&k);\n c=(fun(1,1)+c);\n printf(\"%d\",c);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "number theory"], "code_uid": "9eb14fd2af58466b5f4311039a8cb960", "src_uid": "c8cbd155d9f20563d37537ef68dde5aa", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main()\n{\n int s1,s2,min;\n int a[4];\n for(int i=0;i<4;i++)\n cin>>a[i];\n //int min=a[0];\n if(a[0]>=a[3]&&a[2]>=a[3])\n min=a[3];\n else if(a[0]>=a[2]&&a[2]<=a[3])\n min=a[2];\n else \n min=a[0];\n s1=min*256;\n a[0]=a[0]-min;\n if(a[0]>a[1])\n s2=a[1]*32;\n else\n s2=a[0]*32;\n cout< << Suirkhanov Meyirlan >>\n GL HF\n\n\n 42\n\n bless my code =)\n NEVER FOLLOW YOUR RULES :D HAHAHAHA\n*/\n\n\n///scanf (\"%d%d\", &x, &y);\n///printf(\"%d\", &a)\n\n#include \n\n#define fast ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0);\n#define randoperation srand(time(0));\n#define pii pair < int , int >\n#define pll pair < ll, ll >\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define fast ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0);\n#define randoperation srand(time(0));\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n\nconst ll N = 1e3 + 8;\nconst ld pi = 3.14159265359;\nconst int INF = 1e9 + 1;\nconst double eps = 1e-9;\n\nint ax, ay, bx, by, cx, cy;\n\nvoid fail(){\n printf(\"No\");\n exit(0);\n}\n\nll dist(int x1, int y1, int x2, int y2){\n return (x1 - x2) * 1ll * (x1 - x2) + (y1 - y2) * 1ll * (y1 - y2);\n}\n\nint main(){\n\n //freopen(\"inevit.in\", \"r\", stdin);\n //freopen(\"inevit.out\", \"w\", stdout);\n\n fast\n randoperation;\n\n /// ||\n ///start here \\/\n\n cin >> ax >> ay >> bx >> by >> cx >> cy;\n\n ld ab = dist(ax, ay, bx, by);\n ld bc = dist(bx, by, cx, cy);\n ld ca = dist(cx, cy, ax, ay);\n\n if(ab != bc){\n fail();\n }\n\n if(sqrt(ab) + sqrt(bc) <= sqrt(ca)){\n fail();\n }\n\n if(sqrt(ab) + sqrt(ca) <= sqrt(bc)){\n fail();\n }\n\n if(sqrt(bc) + sqrt(ca) <= sqrt(ab)){\n fail();\n }\n//\n printf(\"Yes\");\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "06358f4556a0b8929e0d9289caf22b2a", "src_uid": "05ec6ec3e9ffcc0e856dc0d461e6eeab", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define ll long long\nusing namespace std;\nint main()\n{\n\tll x,t,a,b,d1,d2;\n\tcin>>x>>t>>a>>b>>d1>>d2;\n\tll y=0;\n\tfor (int i = 0; i < t; ++i)\n\t{\n\t\tfor (int j = 0; j < t; ++j)\n\t\t{\n\t\t\tif((a-i*d1+b-j*d2)==x || b-j*d2==x || a-i*d1==x || x==0)\n\t\t\t{\n\t\t\t\ty=1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(y==1)\n\t\t\tbreak;\n\t}\n\tif(y==1)\n\t\tcout<<\"YES\\n\";\n\telse\n\t\tcout<<\"NO\\n\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "d106a8b1c4c5efc18922dd841545f3f0", "src_uid": "f98168cdd72369303b82b5a7ac45c3af", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\nusing namespace std;\n#define ll long long \n#define ull unsigned long long \n#define ld long double\n#define fi first\n#define se second\n#define pb push_back\n#define mp make_pair\n\n//vector> v;\n//ll a[200005],b[200005];\n//int a[200005],b[200005];\n//ll a[1001005];\n//pair a[1000005];\n//vector l;\n//pairp;\n//pair a[500005];\n//vector adj[100005];\n//unordered_set s;\n\nbool comp(const int a,const int b){\n return a>a>>b>>c;\n for(int i=0;i<=5000;i++){\n \tfor(int j=0;j<=5000;j++){\n \t\tif(i==0||j==0)ar[i][j]=1;\n \t\telse ar[i][j]=(ar[i-1][j]+j*ar[i-1][j-1])%mod;\n \t}\n }\n ll sum=(((ar[a][c]*ar[a][b])%mod)*ar[b][c])%mod;\n cout<\nusing namespace std;\nint main()\n{\n int a[3];\n cin >> a[0];\n cin >> a[1];\n cin >> a[2];\n sort(a,a+3);\n if ((a[0]+a[1])>a[2])\n {\n cout << 0 << '\\n';\n }\n else\n {\n cout << (a[2]-(a[1]+a[0])) +1<<'\\n';\n }\n \n\n\n\n\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "geometry"], "code_uid": "8aee486a6ae5862487b90fa603ed07a8", "src_uid": "3dc56bc08606a39dd9ca40a43c452f09", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define f(i, a, b, c) for(int i = a ; i < b ; i += c)\n#define fr(i, a, b, c) for(int i = a ; i >= b ; i -= c)\n#define fit(it, v) for(auto it = v.begin() ; it != v.end() ; ++it)\n#define frit(it, v) for(auto it = v.rbegin() ; it != v.rend() ; ++it)\n#define all(v) v.begin(), v.end()\n#define allr(v) v.rbegin(), v.rend()\n#define lli long long int\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define NL '\\n'\n\nusing namespace std;\n\ntypedef vector< int > vi;\ntypedef vector< vi > vvi;\ntypedef vector< lli > vli;\ntypedef pair< int, int > ii;\ntypedef vector< ii > vii;\ntypedef vector< char > vc;\ntypedef vector< string > vs;\ntypedef vector< bool > vb;\ntypedef set< int > si;\ntypedef set< ii > sii;\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\n\t#ifdef LOCAL_TEST\n\t\tifstream h(\"in\");\n\t\tofstream g(\"out\");\n\t\tclock_t z = clock();\n\t#else\n\t\t#define h cin\n\t\t#define g cout\n\t#endif//LOCAL_TEST\n\n\tint n;\n\th >> n;\n\tvector< vli > v(n, vli (4));\n\tlli x1 = INT_MAX, x2 = 0, y1 = INT_MAX, y2 = 0;\n\tf(i, 0, n, 1){\n\t\th >> v[i][0] >> v[i][1] >> v[i][2] >> v[i][3];\n\t\tx1 = min(x1, v[i][0]);\n\t\tx2 = max(x2, v[i][2]);\n\t\ty1 = min(y1, v[i][1]);\n\t\ty2 = max(y2, v[i][3]);\n\t}\n\tlli area = (y2 - y1) * (x2 - x1), sum = 0;\n\tf(i, 0, n, 1)\n\t\tsum += (v[i][2] - v[i][0]) * (v[i][3] - v[i][1]);\n\tg << (x2 - x1 == y2 - y1 && area == sum ? \"YES\" : \"NO\");\n\n\t#ifdef LOCAL_TEST\n\t\tcerr << \"Time taken is: \" << double(clock() - z) / CLOCKS_PER_SEC << NL;\n\t\th.close();\n\t\tg.close();\n\t#endif//LOCAL_TEST\n}\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5aa265bbfa500084d7a4a925326d556a", "src_uid": "f63fc2d97fd88273241fce206cc217f2", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n\nusing namespace std;\n\nconst int N = 2002;\n\ndouble dp [N][N];\n\nint main () {\n int n, t, T, i, j;\n double p, ans = 0.0;\n\n /* freopen (\"D.in\", \"r\", stdin);\n freopen (\"D.out\", \"w\", stdout);*/\n\n scanf (\"%d%lf%d\", &n, &p, &t);\n\n dp [1][1] = p;\n dp [1][0] = (1 - p);\n for (i = 1; i <= t; i ++) {\n for (j = 0; j < n; j ++) {\n dp [i + 1][j + 1] += dp [i][j] * p;\n dp [i + 1][j] += dp [i][j] * (1 - p);\n }\n dp [i + 1][j] += dp [i][j];\n }\n\n for (i = 1; i <= n; i ++)\n ans = ans + dp [t][i] * i;\n printf (\"%lf\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["probabilities", "math", "dp", "combinatorics"], "code_uid": "94e1a2beb769da7ca265e52b9bf5dbff", "src_uid": "20873b1e802c7aa0e409d9f430516c1e", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\r\n#include \r\n#include \r\nusing namespace std;\r\n#define int long long \r\n#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);\r\n#define double long double\r\n#define pb push_back\r\n#define pf push_front\r\n#define ft\tfirst\t\r\n#define sd\tsecond\t\r\n \r\nconst int mod = 1e9+7;\r\nconst int inf=1e18;\r\n//\tfreopen(\"input.txt\", \"r\", stdin);\r\n//\tfreopen(\"output.txt\", \"w\", stdout);\t \r\nbool isprime(int tot){\r\n\tif(tot==1) return false;\r\n\tfor(int i = 2;i*i<=tot;i++){\r\n\t\tif(tot%i==0) return false;\r\n\t}\r\n\treturn true;\r\n}\r\n\t\r\nint lcm(int a,int b){\r\n\treturn (a*b)/__gcd(a,b);\r\n}\r\n \r\nint gcd(int a, int b){ \r\n if (b == 0){\r\n \treturn a;\r\n\t} \r\n return gcd(b, a % b); \r\n \r\n}\r\n \r\nint power(int x, unsigned int y){ \r\n int res=1;\r\n if(x==0)return 0; \r\n while(y>0){ \r\n if(y&1){\r\n \tres=(res*x)%mod;\r\n } \r\n y=y>>1; \r\n x=(x*x)%mod;\r\n } \r\n return res%mod; \r\n}\r\n \r\nstruct node{\r\n\tint x,y;\r\n};\r\nint dp[1000005];\r\n\r\nsigned main(){\r\n FAST\t\r\n\tint n;\r\n\tcin>>n;\r\n\t\r\n\tfor(int i=1;i<=n;i++){\r\n\t\tfor(int j=i;j<=n;j+=i){\r\n\t\t\tdp[j] += 1;\r\n\t\t}\r\n\t}\r\n\t\r\n\tint sum=0;\r\n\tfor(int i=2;i<=n;i++){\r\n\t\tsum = (sum + dp[i-1])%998244353;\r\n\t\tdp[i] = (dp[i] + sum)%998244353;\r\n\t}\r\n\tcout<\nusing namespace std;\nint uz,topac,sayac,flag;\nchar A[200];\nint main()\n{\n\tscanf(\"%s\",A);\n\tuz=strlen(A);\n\tif(uz<5)\n\t{\n\t\tprintf(\"Too weak\");\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<=uz-1;i++)\n\t{\n\t\tif(A[i]>=48 && A[i]<=57)\n\t\t{\n\t\t\tflag++;\n\t\t}\n\t\tif(A[i]>=65 && A[i]<=90)\n\t\t{\n\t\t\tsayac++;\n\t\t}\n\t\tif(A[i]>=97 && A[i]<=122)\n\t\t{\n\t\t\ttopac++;\n\t\t}\n\t}\n\tif(sayac>=1 && topac>=1 && flag>=1)\n\t{\n\t\tprintf(\"Correct\");\n\t\treturn 0;\n\t}\n\telse\n\t{\n\t\tprintf(\"Too weak\");\n\t\treturn 0;\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "7389af449b00182f3efe303c21f238f8", "src_uid": "42a964b01e269491975965860ec92be7", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n//Optimisations\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"unroll-loops\")\n#pragma GCC optimize(\"O2\")\n//shortcuts for functions\n#define pb push_back\n#define mp make_pair\n#define ff first \n#define ss second\n#define endl \"\\n\"\n#define all(v) v.begin(),v.end()\n#define rall(v) v.rbegin(),v.rend()\n#define th(n) cout<\n#define vvl vector> \n#define vpll vector >\n#define pll pair\n#define ppl pair\n#define ull unsigned long long\n#define ll long long\n#define mll map< ll, ll > \n#define sll set< ll > \n#define ini(a, v) memset(a, v, sizeof(a))\n// Constants\nconstexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};\nconstexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};\nconstexpr ll INF = 1999999999999999997; \nconstexpr int inf= INT_MAX;\nconstexpr int MAXSIZE = int(1e6)+5;\nconstexpr auto PI = 3.14159265358979323846L;\nconstexpr auto oo = numeric_limits::max() / 2 - 2;\nconstexpr auto eps = 1e-6;\nconstexpr auto mod = 1000000007;\nconstexpr auto MOD = 1000000007;\nconstexpr auto MOD9 = 1000000009;\nconstexpr auto maxn = 100006;\n// Debugging \n// For reference: https://codeforces.com/blog/entry/65311\n#define dbg(...) cout << \"[\" << #__VA_ARGS__ << \"]: \"; cout << to_string(__VA_ARGS__) << endl\ntemplate int SIZE(const T (&t)[N]){ return N; } template int SIZE(const T &t){ return t.size(); } string to_string(string s, int x1=0, int x2=1e9){ return '\"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : \"\") + '\"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? \"true\" : \"false\"); } string to_string(char c){ return string({c}); } template string to_string(bitset &b, int x1=0, int x2=1e9){ string t = \"\"; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '\"' + t + '\"'; } template string to_string(A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template string to_string(pair &p) { l_v_l_v_l++; string res = \"(\" + to_string(p.first) + \", \" + to_string(p.second) + \")\"; l_v_l_v_l--; return res; } template string to_string(A (&v), int x1, int x2, C... coords) { int rnk = rank::value; string tab(t_a_b_s, ' '); string res = \"\"; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + \"[\"; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += \", \"; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += \"]\"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgs(){;} template void dbgs(Heads H, Tails... T){ cout << to_string(H) << \" | \"; dbgs(T...); }\n#define dbgm(...) cout << \"[\" << #__VA_ARGS__ << \"]: \"; dbgs(__VA_ARGS__); cout << endl;\n#define n_l '\\n'\nvoid fastio(){\nios_base::sync_with_stdio(false);\ncin.tie(NULL);\n}\nint main(){fastio();\n ll a,b,c;\n cin>>a>>b>>c;\n ll low=0,high=1e9;\n ll ans=0;\n while(low<=high){\n ll mid = (low+high)/2;\n ll flag=0;\n \n if(b*mid+c*(b-a)<0){\n flag=1;\n }\n \n if(flag!=1){\n ans=mid;\n high=mid-1;\n }else{\n low=mid+1;\n }\n }\n cout<\n#define lli long long int\nusing namespace std;\n\nint main(void)\n{\n lli a , b , c, mod;\n int cnt = 1;\n\n cin >> a >> b >> c;\n\n\n while( true && cnt <= 1e6)\n {\n if( a < b)\n {\n a *= 10;\n }\n\n mod = a % b;\n a /= b;\n\n if( a % 10 == c)\n {\n cout << cnt << endl;\n return 0;\n }\n\n cnt++;\n a = mod;\n }\n\n cout << -1 << endl;\n /*long double a,b;\n int i = 0, occ = 0;\n int check = 0;\n char c;\n long double ans = 0.0;\n string s;\n\n cin >> a >> b >> c;\n\n ans = a/b;\n stringstream ss;\n ss << fixed << setprecision( 20 ) << ans;\n s = ss.str();\n\n for(i = 0; i < s.length() - 1; i++)\n {\n if(s[i] == '.')\n {\n check = 1;\n }\n if(s[i] == c & check == 1)\n {\n occ = i - 1;\n break;\n }\n else\n {\n occ = -1;\n }\n }\n\n cout << occ << endl;*/\n}\n\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "55d5f83c7a63645bb95bab7921d6cba5", "src_uid": "0bc7bf67b96e2898cfd8d129ad486910", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n\nusing namespace std;\nint main()\n{\n\tlong long int x,y;\n\tint a,b,c,i,sum,k=0,v[80];\n\tcin>>a>>b>>c;\n\tfor(i=1;i<=81;i+=1)\n\t{\n\t\tx=b*pow((double)i,a)+c;\n\t\tif(x>=0&&x<1000000000)\n\t\t{\n\t\t\tsum=0;\n\t\t\ty=x;\n\t\t\twhile(x)\n\t\t\t{\n\t\t\t\tsum+=x%10;\n\t\t\t\tx/=10;\n\t\t\t}\n\t\t\tif(sum==i)\n\t\t\t\tv[k++]=y;\n\t\t}\n\t}\n\t/*if(c==0)\n\t\tcout<\n#include\n#include\n#include\nusing namespace std;\nint str[101],r[101];\nint Find(int a[],int n,int num)//二分查找,查找字母\n{ \n int l=0,r=n,mid=0; \n while(la[mid]) {l=mid+1;} \n } \n return -1; \n} \nvoid Gb(int s,int t)//因为二分查找必须有序,所以我打算练练归并\n{\n if(s==t) return;\n int mid=(s+t)/2;\n Gb(s,mid);\n Gb(mid+1,t);\n int i=s,j=mid+1,k=s;\n while((i<=mid)&&(j<=t))\n {\n if(str[i]<=str[j])\n {\n r[k]=str[i];k++;i++;\n }\n else\n {\n r[k]=str[j];k++;j++;\n }\n }\n while(i<=mid)\n {\n r[k]=str[i];k++;i++;\n }\n while(j<=t)\n {\n r[k]=str[j];k++;j++;\n }\n for(int i=s;i<=t;i++) str[i]=r[i];\n}\nint main()\n{\n int n;\n char ch;\n cin>>n;\n if(n<26) {printf(\"NO\");exit(0);}\n for(int i=0;i>ch,str[i]=int(toupper(ch));//将字符转换成数字\n Gb(0,n-1);\n /* 以下用来判断,如果有的话,find会传回下标。 */\n /* 但可能传回下标0,所以+1,如果没有,传回-1。 */\n /* ∵-1+1=0,∴大功告成,不喜勿喷。我只是个蒟蒻 */\n if((Find(str,n,int('A'))+1)&&(Find(str,n,int('B'))+1)&&(Find(str,n,int('C'))+1)&&(Find(str,n,int('D'))+1)&&(Find(str,n,int('E'))+1)&&(Find(str,n,int('F'))+1)&&(Find(str,n,int('G'))+1)&&(Find(str,n,int('H'))+1)&&(Find(str,n,int('I'))+1)&&(Find(str,n,int('J'))+1)&&(Find(str,n,int('K'))+1)&&(Find(str,n,int('L'))+1)&&(Find(str,n,int('M'))+1)&&(Find(str,n,int('N'))+1)&&(Find(str,n,int('O'))+1)&&(Find(str,n,int('P'))+1)&&(Find(str,n,int('Q'))+1)&&(Find(str,n,int('R'))+1)&&(Find(str,n,int('S'))+1)&&(Find(str,n,int('T'))+1)&&(Find(str,n,int('U'))+1)&&(Find(str,n,int('V'))+1)&&(Find(str,n,int('W'))+1)&&(Find(str,n,int('X'))+1)&&(Find(str,n,int('Y'))+1)&&(Find(str,n,int('Z'))+1)) cout<<\"YES\";\n else cout<<\"NO\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "6376f164a05bfcf24418f16bc3a63609", "src_uid": "f13eba0a0fb86e20495d218fc4ad532d", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\n#include\n#include\nusing namespace std;\n\nint Sort(int i, int j)\n{\n return i < j;\n}\n\nint main()\n{\n int N, M ,Temp , MinDif = 100000;\n vector IntVec;\n cin >> N >> M;\n for (int i = 0; i < M; i++)\n {\n cin >> Temp; \n IntVec.push_back(Temp);\n }\n\n sort(IntVec.begin(), IntVec.end(), Sort);\n\n\n for (int i = 0; i < M - N + 1; i++)\n {\n int Sum = IntVec[i + N-1] - IntVec[i];\n if (Sum < MinDif)\n MinDif = Sum;\n }\n\n cout << MinDif << \"\\n\";\n return 0;\n\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "9e0c9032dd56ad173ef7e430f8d8d8b8", "src_uid": "7830aabb0663e645d54004063746e47f", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": " #include \n using namespace std;\n int main(){\n int n,a[50];\n \n cin>>n;\n \n for (int i=0;i>a[i];\n \n for (int i=0;i\nusing namespace std;\nint a[4],w[4],x[4],y[4];\nint i,j,k,p=0;\nint main()\n{\n\tw[0]=0;w[1]=0;w[2]=0;w[3]=1;\n\tx[0]=1;x[1]=1;x[2]=2;x[3]=2;\n\ty[0]=2;y[1]=3;y[2]=3;y[3]=3;\n\tfor(i=0;i<4;i++)\n\t{\n\t\tscanf(\"%d\",&a[i]);\n\t}\n\tfor(i=0;i<4;i++)\n\t{\n\t\tif(a[w[i]]+a[x[i]]>a[y[i]]\n\t\t&&a[x[i]]+a[y[i]]>a[w[i]]\n\t\t&&a[w[i]]+a[y[i]]>a[x[i]])\n\t\t\tp=1;\n\t\telse\n\t\t\t{\n\t\t\t\tif(a[w[i]]+a[x[i]]>=a[y[i]]\n\t\t\t\t&&a[x[i]]+a[y[i]]>=a[w[i]]\n\t\t\t\t&&a[w[i]]+a[y[i]]>=a[x[i]])\n\t\t\t\t\tif(p!=1)p=2;\t\n\t\t\t}\t\n\t}\n\tif(p==1)\n\t\tprintf(\"TRIANGLE\");\n\telse\n\t\tif(p==2)\n\t\t\tprintf(\"SEGMENT\");\n\t\t\t\telse\n\t\t\t\t\tprintf(\"IMPOSSIBLE\");\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "geometry"], "code_uid": "8d2a4b124de975a1cae5f41b04755298", "src_uid": "8f5df9a41e6e100aa65b9fc1d26e447a", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n\nusing namespace std;\n\nint main()\n{\n long long res, n;\n cin >> n;\n res = n - (n/2) - (n/3) - (n/5) - (n/7) + (n/(2*3)) + (n/(2*7))\n +(n/(3*7)) + (n/(7*5)) + (n/(3*5)) + (n/(2*5)) - (n/(2*3*7))\n -(n/(2*5*7)) - (n/(3*5*7)) - (n/(2*3*5)) + (n/(2*3*5*7));\n cout << res;\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7ba09eb704fd162f0c9b0ba6434c8ff0", "src_uid": "e392be5411ffccc1df50e65ec1f5c589", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nstring s;\nint main()\n{\n cin>>s;\n for(int i=0;i='a' && s[i]<='z') cout<\n#include \nusing namespace std;\n\nint main()\n{\n long long h,n;\n while(~scanf(\"%I64d%I64d\",&h,&n))\n {\n long long cnt=0;\n n+=(1LL<\n#include\nusing namespace std;\nbool isprime(int x)\n{\n\tfor (int i = 2; i*i <= x; i++)\n\tif (x % i == 0)\n\t\treturn false;\n\treturn true;\n}\nint main()\n{\n\tint n,j,k, counter = 0, between = 0;\n\t//int*vis = new int[n];\n\t//int*primes = new int[n];\n\n\tcin >> n >> k;\n\tvector vis(n);\n\tvectorprimes(n);\n\tfor (int i = 0; i < n; i++)\n\t\tvis[i] = 0;\n\tvis[0] = vis[1] = 1;\n\tfor (int i = 2; i <= n / 2; i++) //mark the non-primes with a 1\n\t{\n\t\tif (vis[i]) continue;\n\t\tfor (int j = i + i; j < n; j += i)\n\t\t\tvis[j] = 1;\n\t}\n\tint p = 0;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (vis[i] == 0)\n\t\t{\n\t\t\tprimes[p] = i;\n\t\t\tp++;\n\t\t}\n\t}\n\n\t\n\t//for (int i = 0; i < p; ++i)\n\t//\tcout << primes[i] << endl;\n\tfor (int i = 0; i < p - 1; i++)\n\t{\n\t\tj = primes[i] + primes[i + 1] + 1;\n\t\tif (j>2 && j <= n&&isprime(j))\n\t\t{\n\t\t\tcounter++;\n\t\t}\n\t}\n\tif (counter >= k)\n\t\tcout << \"YES\";\n\telse cout << \"NO\";\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "number theory"], "code_uid": "108de9d22f1fe4ad14fc500fd6e0dd4c", "src_uid": "afd2b818ed3e2a931da9d682f6ad660d", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n\nconst int N = 2e5 + 5;\nconst int mod = 1e9 + 7;\nint n, l, r;\nint dp[N][3], divv[3];\nint main() {\n scanf(\"%d%d%d\", &n, &l, &r);\n\n // method 1\n// while(l%3 != r%3 && l < r) {\n// divv[l%3]++;\n// l++;\n// }\n//\n// divv[l%3]++;\n// if(l!=r) for(int i = 0; i < 3; ++i) divv[i] += (r-l+1)/3;\n\n // method 2\n divv[0] = r/3 - (l-1)/3;\n divv[1] = (r+2)/3 - (l-1+2)/3;\n divv[2] = (r+1)/3 - (l-1+1)/3;\n\n dp[0][0] = 1;\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < 3; ++j) {\n for(int k = 0; k < 3; ++k) {\n dp[i+1][(j+k)%3] = (dp[i+1][(j+k)%3] + (ll)dp[i][j] * divv[k]%mod)%mod;\n }\n }\n }\n printf(\"%d\\n\", dp[n][0]);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "f94139d94417aa08755da0080cb270c6", "src_uid": "4c4852df62fccb0a19ad8bc41145de61", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) a.begin(), a.end()\n\ntypedef long long ll;\n\ntemplate inline void chkmin(T1 &x, T2 y) { if (y < x) x = y; };\ntemplate inline void chkmax(T1 &x, T2 y) { if (y > x) x = y; };\n\nvoid go(vector &a, int v, int vl, int vr, int pos) {\n if (vl == vr) {\n a.pb(v);\n return;\n }\n\n a.pb(v);\n int vm = (vl + vr) / 2;\n if (pos <= vm)\n go(a, v + v, vl, vm, pos);\n else\n go(a, v + v + 1, vm + 1, vr, pos);\n\n}\n\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(NULL), cout.tie(NULL);\n\n int n, a, b;\n cin >> n >> a >> b;\n vector ansa, ansb;\n go(ansa, 1, 0, n - 1, a - 1);\n go(ansb, 1, 0, n - 1, b - 1);\n\n int i;\n for (i = 0; i < n; ++i) {\n if (ansa[i] != ansb[i]) break;\n }\n\n\n if (i == 1)\n cout << \"Final!\" << endl;\n else\n cout << (int)log2(n) - i + 1 << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "implementation"], "code_uid": "fec4aedd41c21514c46392f5953bf445", "src_uid": "a753bfa7bde157e108f34a28240f441f", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr andarash mazide nemat.\n___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat.\n___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb.\n\n___az dast o zabane ke barayad ?\n___kaz ohdeye shokrash be dar ayad !\n\n*/\n#include\n#define adelkhah main\n#define pb push_back\n//#define int long long\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\nint32_t adelkhah()\n{\n int n; cin >> n; int m = n; int ans = n;\n if(n < 10) return cout << 1,0;\n int d = 1; while(n) d *= 10,n/=10; d /= 10;\n m = m/d; cout << (m+1)*d - ans;\n}\n/**< WRITEN BY ALI ADELKHAH */\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "c83db973046f344fbfb1f6b764f0b1a7", "src_uid": "a3e15c0632e240a0ef6fe43a5ab3cc3e", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n\nusing namespace std;\n\n__int64 p, d;\n__int64 maxx = 0, ans;\n__int64 a[30];\n\nint main(){\n //freopen(\"input.txt\", \"rt\", stdin);\n // freopen(\"output.txt\", \"wt\", stdout);\n cin >> p >> d;\n __int64 pp = p, ppp = p;\n //a[0] = (pp % 10 + 1) % 10;\n //pp -= a[0];\n //pp /= 10;\n int i = 1;\n __int64 pow = 10;\n __int64 maxpow = 1;\n while (maxpow < p){\n maxpow *= 10;\n }\n while (p - pp <= d){\n if (pow >= maxpow && pow != 10)\n break;\n __int64 dig = (pp % pow) / (pow / 10);\n __int64 cur = (dig + 1) % 10;\n //a[i] = a[i - 1] + cur * pow;\n //pp -= a[i];\n if (p - (pp - cur * pow / 10) > d){\n cout << pp;\n return 0;\n }\n pp -= cur * pow / 10;\n pow *= 10;\n }\n cout << pp;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "06c0b4a8bae9e2516addbb5b0665a4ed", "src_uid": "c706cfcd4c37fbc1b1631aeeb2c02b6a", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nint main()\n{\n char p[101];\n scanf(\"%s\", p);\n int size(0);\n for (size_t i = 0; p[i] != 0; ++i)\n {\n size *= 16;\n switch (p[i])\n {\n case '>':\n size += 8;\n break;\n\n case '<':\n size += 9;\n break;\n\n case '+':\n size += 10;\n break;\n\n case '-':\n size += 11;\n break;\n\n case '.':\n size += 12;\n break;\n\n case ',':\n size += 13;\n break;\n\n case '[':\n size += 14;\n break;\n\n case ']':\n size += 15;\n break;\n\n default:\n break;\n }\n\n size %= 1000003;\n }\n printf(\"%d\\n\", size);\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "eac506a1373408d1d7e06c84e94c7a38", "src_uid": "04fc8dfb856056f35d296402ad1b2da1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main(){\n int a[20],b[20],ans=1e9;\n char c[100010],d[100010];\n memset(a,0,sizeof(a));\n memset(b,0,sizeof(b));\n cin>>c>>d;\n for(int i=0;i0&&i!=5&&i!=9)ans=min(ans,b[i]/a[i]);\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef double dbl;\ntypedef long long int lld;\n#define PI 3.14159265358979323\n#define foi(i,n) for(lld i = 0; i < n; i+=1)\n#define fod(i,n) for(lld i=n-1; i >=0; i-=1)\n#define MAX std::numeric_limits::max()\n#define MIN std::numeric_limits::min()\n#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\nvector getfact (lld x)\n{\n vector v;\n\twhile (x%2==0){\n x/=2;\n v.push_back(2);\n }\n for(lld i = 3; i <= sqrt(x); i += 2){\n while (x%i==0){\n x/=i;\n v.push_back(i);\n }\n }\n if (x > 2){\n v.push_back(x);\n }\n return v;\n}\nlld gcd_ (lld a, lld b)\n{\n\tlld max = a > b ? a : b;\n\tlld min = a < b ? a : b;\n\twhile(min){\n\t\tlld rem = max%min;\n\t\tmax = min;\n\t\tmin = rem;\n\t}\n\treturn max;\n}\nint main ()\n{\n #ifndef ONLINE_JUDGE\n freopen(\"inputf.in\", \"r\", stdin);\n freopen(\"outputf.in\", \"w\", stdout);\n #endif\n FastIO\n lld a, b;\n cin >> a >> b;\n lld g = gcd_(a, b);\n a/=g;\n b/=g;\n lld f = 1;\n map count1, count2;\n vector v = getfact(a);\n vector w = getfact(b);\n for(lld i = 0; i < v.size(); i+= 1){\n \tif (v[i] == 2 || v[i] == 3 || v[i] == 5){\n \t\tcount1[v[i]]+=1;\n \t}\n \telse{\n \t\tf = 0;\n \t}\n }\n for(lld i = 0; i < w.size(); i+= 1){\n \tif (w[i] == 2 || w[i] == 3 || w[i] == 5){\n \t\tcount2[w[i]]+=1;\n \t}\n \telse{\n \t\tf = 0;\n \t}\n }\n if (f == 0){\n \tcout << \"-1\";\n }\n else{\n \tlld count = abs(count2[2]-count1[2]) + abs(count2[3]-count1[3]) + abs(count2[5]-count1[5]);\n \tcout << count;\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "5ed04dce04efee01f5b9220253d2de54", "src_uid": "75a97f4d85d50ea0e1af0d46f7565b49", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "Clang++17 Diagnostics", "source_code": "\n#include\nusing namespace std;\nint main()\n{\n long long n;\n cin>>n;\n if(n%2==0)\n cout<<\"Mahmoud\";\n else\n cout<<\"Ehab\";\nreturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "games"], "code_uid": "4bf9d39007da312e56f1c37a34429475", "src_uid": "5e74750f44142624e6da41d4b35beb9a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n#define dd double\n#define ll long long int\n#define light ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\nll recur(ll index,ll moves,ll cnt,string &s,string &t,vector>>&dp){\n if(index==s.length())\n return 0;\n if(dp[index][moves][cnt]!=-1)\n return dp[index][moves][cnt];\n if(!moves){\n if(s[index]==t[0])\n return dp[index][moves][cnt]=recur(index+1,moves,cnt+1,s,t,dp);\n if(s[index]==t[1])\n return dp[index][moves][cnt]=cnt+recur(index+1,moves,cnt,s,t,dp);\n return dp[index][moves][cnt]=recur(index+1,moves,cnt,s,t,dp);\n }\n if(s[index]==t[0])\n return dp[index][moves][cnt]=max(recur(index+1,moves,cnt+1,s,t,dp),cnt+recur(index+1,moves-1,cnt,s,t,dp));\n if(s[index]==t[1])\n return dp[index][moves][cnt]=max(recur(index+1,moves-1,cnt+1,s,t,dp),cnt+recur(index+1,moves,cnt,s,t,dp));\n return dp[index][moves][cnt]=max({recur(index+1,moves-1,cnt+1,s,t,dp),cnt+recur(index+1,moves-1,cnt,s,t,dp),recur(index+1,moves,cnt,s,t,dp)});\n}\nint main()\n{\n light;\n ll n,k;\n string s,t;\n cin>>n>>k>>s>>t;\n if(t[0]!=t[1]){\n vector>>dp(n,vector>(k+1,vector(n+1,-1)));\n cout<>s1;\n\tcin>>s2;\n\t\n\tint l=s1.length();\n\tint m=l-1;\n\tfor(i=1;i=s2[0])\n\t\t\t{\n\t\t\t\tm=i-1;\n\t\t\t\tbreak;\n\t\t\t}\n\t}\n\tfor(i=0;i<=m;i++)\n\t cout<\n//}\n//{ Loops\n#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)\n#define rep(i,n) forab (i, 0, (n) - 1)\n#define For(i,n) forab (i, 1, n)\n#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)\n#define per(i,n) rofba (i, 0, (n) - 1)\n#define rof(i,n) rofba (i, 1, n)\n#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)\n//}\n//{ Floating-points\n#define EPS 1e-7\n#define abs(x) (((x) < 0) ? - (x) : (x))\n#define zero(x) (abs (x) < EPS)\n#define equal(a,b) (zero ((a) - (b)))\n#define PI 2*acos (0.0)\n//}\ntypedef long long int64;\ntypedef unsigned long long int64u;\n#define memo(a,v) memset(a,v,sizeof(a))\n#define all(a) a.begin(),a.end()\n#define db double\n#define pb push_back\n#define eb emplace_back\n#define pii pair\n#define NL puts(\"\")\n#define ff first\n#define ss second\n//{\n//Intput_Output\n#define gc getchar\n#define II ({ int a; read(a); a;})\n#define IL ({ int64 a; read(a); a;})\n#define ID ({ db a; scanf(\"%lf\",&a); a;})\n#define IC ({ char a; scanf(\"%c\",&a); a;})\n#define IS ({ string a; cin >> a; a;})\n#define OC printf(\"Case %d:\",cs);\n//}\n//}\n#define _stl(x) {__stl_print__(x);}\n#define __(args...) {dbg,args; cerr<inline bool read(T &x){int c=gc();int sgn=1;while(~c&&c<'0'|c>'9'){if(c=='-')sgn=-1;c=gc();}for(x=0;~c&&'0'<=c&&c<='9';c=gc())x=x*10+c-'0';x*=sgn;return ~c;}\nstruct debugger{template debugger& operator , (const T& v){cerr< void __stl_print__ (T &x) { // for all STL containers\n cerr << \"[\"; forstl (i, x) cerr << (i != x.begin () ? \", \" : \"\") << *i; cerr << \"]\" << endl;\n}\ntemplate inline T max (T &a, U &b)\n{\n return a > b ? a : b;\n}\ntemplate inline T min (T &a, U &b)\n{\n return a < b ? a : b;\n}\ntemplate inline T swap (T &a, U &b)\n{\n T tmp = a;\n a = b;\n b = tmp;\n}\n//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction\n//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction\n//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction\n//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction\n\nconst int64 INF = (1ll)<<50;\nconst int mx = 1e5 + 7;\nconst int mod = 1000000007 ;\nconst db pi = PI;\nint EQ(double d) {\n if ( fabs(d) < EPS ) return 0;\n return d > EPS ? 1 : -1 ;\n}\n\nchar ch[12];\n\nint64 call(int64 a,int len){\n int arr[12]= {0},cnt = 0;\n while(a){\n arr[cnt++] = a%2;\n a /= 2;\n }\n int64 num = 0;\n per(i,len){\n if(arr[i]) num = num*10 + 7;\n else num = num*10 + 4;\n }\n return num;\n}\n\nint64 bs(int64 len,int64 num){\n int64 low = 0,high = (1ll<>1;\n int64 tmp = call(mid,len);\n if(tmp >= num){\n high = mid - 1;\n ans = mid;\n }else low = mid + 1;\n }\n return ans;\n}\n\nint main() {\n#ifdef Sanim\n\t//geting WA check int64 & int64u\n freopen (\"in.txt\", \"r\", stdin);\n // freopen (\"output.txt\", \"w\", stdout);\n#endif\n int64 lf = IL,rt = IL;\n sprintf(ch,\"%lld\",lf);\n int l1 = strlen(ch);\n int64 a = bs(l1,lf);\n if(a == -1) {a = 0; ++l1;}\n sprintf(ch ,\"%lld\",rt);\n int l2 = strlen(ch);\n int64 b = bs(l2,rt);\n if(b == -1) {b = 0; ++l2;}\n int64 high = call(b,l2);\n int64 num = call(a,l1);\n\n int64 ans = 0,left = lf,tmp = (1ll<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n//typedef ll long long;\n//typedef ld long double;\n\n//const int maxn = 10000;\n//bool v[maxn];\n\nint main() {\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n\t\n\tint n, a, b, c;\n\tcin >> n >> a >> b >> c;\n\n\tint ans = 0;\n\n\tlong long tmp;\n\tfor (long long i1 = 0; i1 <= a; i1++)\n\t{\n\t\tfor (long long i2 = 0; i2 <= b; i2++)\n\t\t{\n\t\t\ttmp = i1 + 2 * i2;\n\n\t\t\tif ((2 * n - tmp) >= 0 && (2 * n - tmp) % 4 == 0 && (2 * n - tmp) / 4 <= c)\n\t\t\t\tans++;\n\t\t}\n\t}\n\t\n\tcout << ans;\n\t\t\n\treturn 0;\t\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "f68d6cfbbcad7d9054be42d014c9d7d8", "src_uid": "474e527d41040446a18186596e8bdd83", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\n\n\nusing namespace std;\n\n\n\nint main(){\n\n#ifndef ONLINE_JUDGE\n\n\tfreopen(\"in.in\", \"r\", stdin);\n\n\tfreopen(\"out.out\", \"w\", stdout);\n\n#endif\n\n\tint n, k, l, c, d, p, nl, np;\n\n\tint m[100];\n\n\tscanf(\"%d%d%d%d%d%d%d%d\", &n, &k, &l, &c, &d, &p, &nl, &np);\n\n\tm[0]=(k*l)/nl;\n\n\tm[1]=c*d;\n\n\tm[2]=p/np;\n\n\tsort(m, m+3);\n\n\tprintf(\"%d\\n\", m[0]/n);\n\n\treturn 0;\n\n\t\n\n}\n ", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "4a57803d4a7d9a30bb2155a7565c1858", "src_uid": "67410b7d36b9d2e6a97ca5c7cff317c1", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include\nusing namespace std;\n\nint main()\n{\n string s;\n cin>>s;\n int n=0;\n sort(s.begin(),s.end());\n for(int x=1;x\n#include\nusing namespace std;\n\nint main()\n{\n int n,m,a,b,nn,c1=0,c2=0,c3,c4;\n cin>>n>>m>>a>>b;\n nn=n;\n while(n-m>0)\n {\n c1=c1+b;\n n=n-m;\n }\n if(n>0)\n {\n c3=c1+(n*a);\n c4=c1+b;\n if(c3c1)\n c1=c1;\n else\n c1=c2;\ncout<\n\nusing namespace std;\n\nint main()\n{\n int n, m, mcopy, maximum, minimum, a[100];\n int i, j;\n\n cin >> n >> m;\n for (i = 0; i < n; i++)\n cin >> a[i];\n\n for (i = n - 1; i > 0; i--)\n for (j = 0; j < i; j++)\n {\n if (a[j] > a[j+1])\n {\n a[j] = a[j] + a[j+1];\n a[j+1] = a[j] - a[j+1];\n a[j] = a[j] - a[j+1];\n }\n }\n\n maximum = a[n-1];\n\n for (mcopy = m, i = 0; mcopy > 0 && i < n - 1;)\n {\n if (a[i] < a[n-1])\n {\n a[i]++;\n mcopy--;\n }\n else\n i++;\n }\n\n if (mcopy != 0)\n {\n if (mcopy % n == 0)\n cout << maximum + mcopy / n << ' ';\n else\n cout << maximum + 1 + mcopy / n << ' ';\n }\n else\n {\n minimum = a[0];\n for (i = 1; i < n; i++)\n if (a[i] > minimum)\n minimum = a[i];\n cout << minimum << ' ';\n }\n\n cout << maximum + m;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation", "binary search"], "code_uid": "1e7e7d7531f77834ea1de696293faef7", "src_uid": "78f696bd954c9f0f9bb502e515d85a8d", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n//#define REP(1,n) for(int i=1;i<=n;i++)\n#define mod 1000000007\n#define pb push_back\n#define ff first\n#define ss second\n#define ii pair\n#define vi vector\n#define vii vector\n//#define lli long long int\n#define inf 1000000000\n\nusing namespace std;\n\nint main()\n{\n\tint n,i;\n\tcin>>n;\n\tint A[n];\n\tvectorv;\n\tfor(int i=0;i>A[i];\n\t\tif(i!=0)\n\t\t{\n\t\t\tv.push_back(A[i]-A[i-1]);\n\t\t}\n\t}\n\tint m=*max_element(v.begin(),v.end());\n\tint a=INT_MAX;\n\tfor(int i=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define s(n)\t\t\t\t\tscanf(\"%d\",&n)\n#define sf(n) \t\t\t\t\tscanf(\"%lf\",&n)\n#define ss(n) \t\t\t\t\tscanf(\"%s\",n)\n#define maX(a,b)\t\t\t\t((a)>(b)?(a):(b))\n#define miN(a,b)\t\t\t\t((a)<(b)?(a):(b))\n#define abS(x)\t\t\t\t\t((x)<0?-(x):(x))\n#define pb\t\t\t\t\t\tpush_back\n#define mp\t \t\t\t\t\tmake_pair\n#define EPS\t\t\t\t\t\t1e-9\n#define fill(a,v) \t\t\t\tmemset(a, v, sizeof(a))\n#define SZ(v)\t\t\t\t\t((int)(v.size()))\n#define all(x)\t\t\t\t\tx.begin(), x.end()\n#define foreach(v,c) for( typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)\n#define INDEX(arr,ind)\t\t\t(lower_bound(all(arr),ind)-arr.begin())\n#define FF\t\t\t\t\t\tfirst\n#define SS\t\t\t\t\t\tsecond\n#define T(t) \t\t\tint t;scanf (\"%d\",&t);while (t--)\n#define INF\t\t\t\t\t\t(int)1e9\n#define LINF\t\t\t\t\t(long long)1e18\n#define FOR(i,a,b)\t\t\t\tfor(int i=a;i debugger& operator , (const T& v)\n\t{\n\t\tcerr< arr)\n{\n\tcout<<\"[\";\n\tfor(int i=0;i VI;\ntypedef vector > VVI;\ntypedef long long LL;\ntypedef vector VLL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector > VPII;\ntypedef pair PDD;\n\nint main()\n{\n\tint r1,r2,c1,c2,d1,d2;\n\tcin >> r1 >> r2 >>c1 >>c2 >> d1 >> d2;\n\tbool found = false;\n\tREP(i,10)\n\t{\n\t\tREP(j,10)\n\t\t{\n\t\t\tREP(k,10)\n\t\t\t{\n\t\t\t\tREP(l,10)\n\t\t\t\t{\n\t\t\t\t\tif(i==j||i==k||i==l||j==k||j==l||k==l) continue;\n\t\t\t\t\tif(i+j==r1&&k+l==r2&&i+k==c1&&j+l==c2&&i+l==d1&&j+k==d2)\n\t\t\t\t\t{\n\t\t\t\t\t\tcout << i << \" \" << j << endl <\nusing namespace std;\nint main(){\n int k , n , w;\n cin >> k >> n >> w;\n long long sum = 0 ;\n for (int i = 1; i <= w; i++)\n {\n sum += i * k;\n }\n cout << max(0LL,sum - n) << endl;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "21cdcb0d6c866b3280e0a382e3931c9d", "src_uid": "e87d9798107734a885fd8263e1431347", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nunsigned long long int n,k,a[111111],ans,j = 2;\nint main()\n{\n\tcin >> n >> k;\n\t/*if(n/2 <= k)\n\t{\n\t\tdouble x = (n-1)/2;\n\t\tcout << x*n;\n\t\treturn 0; \n\t}*/\n\tfor(int i = 1; i <= n; i++)\n\t{\n\t\ta[i] = i;\n\t}\n\tfor(int i = 1 ; i <= k; i++)\n\t{\n\t\tif(i < n+1-i)\n\t\t{\n\t\t\tswap(a[i],a[n+1-i]);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t}\n\tint x = 0;\n\tfor(int i = 1; i <= n; i++)\n\t{\n\t\tif(a[i] > a[i+1])\n\t\t{\n\t\t\tans += n-i;\n\t\t\tx++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tans += x;\n\t\t}\n\t}\n\tcout << ans;\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "56ff5f5575c0fa10469fa1f2a9b47fa6", "src_uid": "ea36ca0a3c336424d5b7e1b4c56947b0", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define INF 1e+7\n#define ll long long\n\nint check(char b[]){\n if(!strcmp(b,\"ABC\") || !strcmp(b,\"ACB\") || !strcmp(b,\"BAC\") || !strcmp(b,\"BCA\")\n || !strcmp(b,\"CAB\") || !strcmp(b,\"CBA\"))\n return 1;\n return 0;\n}\n\nint main(){\n char a[205],b[5],c[205],x = 0;\n gets(a);\n int i = 0;\n\n if(strlen(a) <= 3){\n if(check(a))//ԭ�����������\n printf(\"Yes\");\n else{\n printf(\"No\");\n }\n }else{\n i = 0;\n while(a[i+2] != '\\0'){\n if(a[i] != '.'){\n b[0] = a[i],b[1] = a[i+1],b[2] = a[i+2],b[3] = '\\0';\n if(check(b)){\n printf(\"Yes\");\n x = -1;\n break;\n }\n }\n i++;\n }\n if(x != -1){\n printf(\"No\");\n }\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "c9883b46fdf29f2fa467ef32fa49c6e2", "src_uid": "ba6ff507384570152118e2ab322dd11f", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n\nusing namespace std;\n\n#define ll long long int\n#define ld long double\n#define MAX 11\n#define MOD 1000000007\n#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)\n#define pi 3.14159265358979323846\n\nint main(){\n\n fast;\n\n ll i,n;\n cin >> n;\n ll n1 = n;\n\n ll tmp = 1;\n ll ans = 0;\n for(i=2;i*i<=n1;i++){\n while((n1%i)==0){\n tmp = n1/i;\n ans = ans + tmp;\n n1 = n1/i;\n }\n }\n\n if(n1>1){\n ans++;\n }\n\n ans = ans + n;\n\n cout << ans << endl;\n}\n\n\n// cout << fixed << setprecision(12);\n\n", "lang_cluster": "C++", "tags": ["number theory"], "code_uid": "a6451954671e7ae9e3e3e2ad4c12d525", "src_uid": "821c0e3b5fad197a47878bba5e520b6e", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nint main()\n{\n int x, y, z,p =0, m =0, u =0, up =0, down =0;\n cin >> x >> y >> z;\n\n up = x + z;\n down = y + z;\n for (int i=0; i < 3; i++)\n {\n if (up == down)\n {\n p++;\n m++;\n }\n if (up > down)\n {\n p++;\n }\n if (down > up)\n {\n m++;\n }\n }\n if (z == 0 && p == m)\n {\n cout << \"0\\n\";\n }\n\n else if ((up > y && down > x) || (down == x || up == y))\n {\n cout << \"?\\n\";\n }\n else if (p > m)\n {\n cout << \"+\\n\";\n }\n else if (p < m)\n {\n cout << \"-\\n\";\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "1908a3e840ed0218c393be3475a46e13", "src_uid": "66398694a4a142b4a4e709d059aca0fa", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint main(){\n string binary{};\n cin >> binary;\n int bin = binary.size()/2, size = binary.size(), i{};\n if((size-1)%2==0){\n for(i=1; i\nusing namespace std;\nusing ll=long long;\nvoid solveCP311()\n{\n ll n;\n cin>>n;\n string str;\n cin>>str;\n char arr1[n],arr2[n];\n ll j=0;\n for(ll i=0;i=arr1[i])\n {\n flag2=false;\n break;\n }\n }\n if(flag1||flag2) { cout<<\"YES\"; } else { cout<<\"NO\"; }\n}\n\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); cout.tie(NULL);\n\n ll t=1;\n //cin>>t;\n while(t--)\n {\n \tsolveCP311();\n }\n //cout<<\"time taken : \"<<(float)clock()/CLOCKS_PER_SEC<<\" sec\"<\n\nusing namespace std;\n\n#define DEBUG_ON 0\n\n#define INF 0x3f3f3f3f\n#define REP(i,n) for(int i = 0; i < (n); ++i)\n#define REPD(i,n) for(int i = (n)-1; i >= 0; --i)\n#define R(x) scanf(\" %d\",&x)\n#define RR(x,y) scanf(\" %d %d\",&x,&y)\n#define DBG(x) if(DEBUG_ON) cout << #x << \" == \" << x << endl\n\ntypedef long long ll;\ntypedef pair ii;\n\n#define MAXN 10010\n#define MAXL 35\n\ndouble pd[MAXL][MAXN][6][6];\nbool vis[MAXL][MAXN][6][6];\nint l[MAXN], r[MAXN];\ndouble p[MAXN];\n\n// probabilidade de equal iguais a need e o resto menor\ndouble solve(int mask, int need, int equal, int ind) {\n if (equal < 0) return 0.0;\n \n if (mask == 0) {\n if (equal == 0) return 1.0;\n else return 0.0;\n }\n \n double &ans = pd[mask][need][equal][ind];\n if (vis[mask][need][equal][ind]) return ans;\n vis[mask][need][equal][ind] = true;\n \n if (mask & (1 << ind)) {\n if (r[ind] < need) {\n return ans = solve(mask ^ (1 << ind), need, equal, ind); \n } else {\n double pe = 1.0/double(r[ind] - l[ind] + 1);\n double pl = double(need - l[ind])/double(r[ind] - l[ind] + 1);\n return ans = pe*solve(mask ^ (1 << ind), need, equal - 1, ind+1) + pl*solve(mask ^ (1 << ind), need, equal, ind+1); \n }\n } else {\n return ans = solve(mask, need, equal, ind+1);\n }\n}\n\n\n\nint main() {\n int n; R(n);\n REP(i,n) scanf(\" %d %d\", &l[i], &r[i]);\n memset(vis,0,sizeof(vis));\n \n int all = (1 << n) - 1;\n REP(i,n) {\n for (int j = 1; j <= r[i]; j++) {\n if (r[i] < j) continue;\n\n bool ok = true;\n REP(k, 5) {\n if (k == i) continue;\n if (l[k] > j) ok = false; \n } \n if (!ok) continue;\n double ans = 0; \n for (int win = 1; win < n; win++) {\n if (l[i] > j) {\n ans += solve(all ^ (1 << i), j, win, 0);\n } else {\n double x = solve(all ^ (1 << i), j, win, 0);\n double pe = 1.0/double(r[i] - l[i] + 1);\n double pg = double(r[i] - (j+1) +1)/double(r[i] - l[i] + 1);\n ans += pe*x/double(win + 1.0) + pg*x;\n }\n } \n p[j] += ans;\n }\n }\n\n double ans = 0;\n for (int i = 1; i <= 10000; i++) { \n ans += double(i)*p[i];\n }\n printf(\"%.10lf\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["probabilities", "bitmasks"], "code_uid": "d41ce18b294e30b8b07e40cbc521f77b", "src_uid": "5258ce738eb268b9750cfef309d265ef", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define r_0 return 0;\n#define ll long long\n#define ld long double\n#define pb push_back\n#define fr first\n#define se second\n#define in freopen(\"input.txt\",\"r\",stdin);\n#define out freopen(\"output.txt\",\"w\", stdout);\n#define TIME ios_base::sync_with_stdio(NULL);\n#define tcn cin.tie(NULL);\n#define tct cout.tie(NULL);\nusing namespace std;\n\nconst ll N = 1e7 + 7;\nll n,m,a,k,cnt,mx,b;\nstring s;\nint main()\n{\ncin >> n >> m;\ncnt=min(n-m,m-1);\ncout<<3*n+cnt;\n}\n", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "480e04d8f37c73b6a483b4f8710abff1", "src_uid": "24b02afe8d86314ec5f75a00c72af514", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \n#define ll long long\n#define mp make_pair\n#define pb push_back\n \nusing namespace std;\n\nconst int N = 300010;\nconst int MOD = 998244353; \n\nint a1, a2;\n\nint main() {\n cin >> a1 >> a2;\n int ans = 0;\n while (a1 > 0 && a2 > 0) {\n if (a1 > a2) swap(a1, a2);\n a1++;\n a2 -= 2;\n if (a2 >= 0) ans++;\n }\n cout << ans;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "dp", "implementation"], "code_uid": "cc69299bc081ae83790d318d1c82d0bc", "src_uid": "ba0f9f5f0ad4786b9274c829be587961", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define X first\n#define Y second\n#define pb push_back\n#define mp make_pair\n#define ppb pop_back\n#define sz(x) ((int)(x).size())\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\ntypedef vector vl;\ntypedef vector vb;\ntypedef vector vii;\ntypedef vector vvi;\ntypedef vector vvb;\n\nchar a[10][10];\nbool m[10][10][101];\n\nint main() {\n for(int i = 0; i < 8; i++) \n gets(a[i]);\n queue > Q;\n Q.push(mp(mp(7, 0), 0));\n m[7][0][0] = 1;\n while(!Q.empty()) {\n int x = Q.front().X.X;\n int y = Q.front().X.Y;\n int t = Q.front().Y;\n Q.pop();\n if(!x && y == 7) {\n puts(\"WIN\");\n return 0;\n }\n if(t >= 100) continue;\n for(int dx = -1; dx <= 1; dx++) \n for(int dy = -1; dy <= 1; dy++) {\n int xx = x+dx;\n int yy = y+dy;\n if(xx < 0 || xx > 7 || yy < 0 || yy > 7) continue;\n if(xx - t >= 0 && a[xx-t][yy] == 'S') continue;\n if(xx - t - 1 >= 0 && a[xx-t-1][yy] == 'S') continue;\n if(m[xx][yy][t+1]) continue;\n m[xx][yy][t+1] = 1;\n Q.push(mp(mp(xx, yy), t+1));\n }\n }\n puts(\"LOSE\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "implementation", "graphs"], "code_uid": "baa05a11e8e3f3f4863d8d825ceda368", "src_uid": "f47e4ab041288ba9567c19930eb9a090", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint n;\nint main()\n{\n\tcin>>n;\n\tfor(int i=1;i*(i+1)/2<=n;i++)\n\t{\n\t\tif(i*(i+1)/2==n)\n\t\t{\n\t\t\tcout<<\"YES\"<\n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\nconst ll MAXN = 40;\n\nll f[MAXN][12][12][6][6], N, t, ans;\n\nint main() {\n scanf(\"%lld%lld\", &N, &t);\n if (N <= 2) {\n \tputs(\"0\");\n \treturn 0;\t\n }\n for (ll i = 1; i <= 4; i++) {\n for (ll j = 1; j <= 4; j++) {\n if (i != j) f[2][0][0][i][j] = 1;\n \t// printf(\"%lld \", f[2][0][0][i][j]);\n }\n // puts(\"\");\n }\n for (ll i = 2; i <= N; i++) {\n for (ll j = t; j >= 0; j--) {\n for (ll k = t-1; k >= 0; k--) {\n for (ll p = 1; p <= 4; p++) {\n for (ll q = 1; q <= 4; q++) {\n for (ll m = 1; m <= 4; m++) {\n /* \n\t\t\t\t\t\t\tif (p < q && q > m && j) {\n f[i][j][k][q][m] += f[i-1][j-1][k][p][q];\n } else if (p > q && q < m && k) {\n f[i][j][k][q][m] += f[i-1][j][k-1][p][q];\n } else if (p != q && q != m) f[i][j][k][q][m] += f[i-1][j][k][p][q];\n \t*/\n \t \n\t\t\t\t\t\t\tif (p < q && q > m) {\n \t\tf[i+1][j+1][k][q][m] += f[i][j][k][p][q];\t\n \t} else if (p > q && q < m) {\n \t\tf[i+1][j][k+1][q][m] += f[i][j][k][p][q];\t\n \t} else if (p != q && q != m) f[i+1][j][k][q][m] += f[i][j][k][p][q];\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t}\n }\n }\n }\n }\n }\n for (ll i = 1; i <= 4; i++) {\n for (ll j = 1; j <= 4; j++) {\n \t// printf(\"%lld \", f[N][t][t-1][i][j]);\n if (i == j) continue;\n ans += f[N][t][t-1][i][j];\n }\n // puts(\"\");\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "b05776340abf455fc6653e45597f72db", "src_uid": "6d67559744583229455c5eafe68f7952", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define int long long\n\nstring s;\nint n;\nint change;int res=0;\nint moved=0;\nint dp[101][55][202]; //makes sure this state is visited\n\nvoid go(int pos, int change, int moved)\n{\n //Base Case\n if(pos == n)\n {\n res = max(res, abs(moved) - (change&1));\n return;\n }\n\n\n if(dp[pos][change][moved])\n return;\n\n dp[pos][change][moved] = 1;\n if(s[pos] == 'F')\n {\n go(pos+1, change, moved+1);\n if(change)\n {\n go(pos+1, change-1, -moved);\n }\n }\n else\n {\n go(pos+1, change, -moved);\n if(change)\n {\n go(pos+1, change-1, moved+1);\n }\n }\n}\n\n\nvoid solve()\n{\n memset(dp, 0, sizeof dp);\n cin>>s;\n n = (int)s.size();\n cin>>change;\n go(0, change, 0);\n cout<>t;\n while(t--)\n {\n solve();\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "c63f50d2a3a1e7217573f3b07ce834c1", "src_uid": "4a54971eb22e62b1d9e6b72f05ae361d", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector vi;\ntypedef vector vll;\ntypedef pair pii;\nconst int INF_int=100000000;\nconst ll INF_ll=1000000000000000;\n#define pb push_back\n#define FILE \"nenokku_easy\"\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n\tll n,m,k;\n\tcin >> n >> m>>k;\n\t/*if(k==n*m)\n {\n cout << k;\n return 0;\n }\n if(n==1 || m==1)\n {\n cout << k;\n return 0;\n }*/\n\tll l=1,r=m*n;\n\tint e=0;\n\tll ans=0;\n\twhile(r>=l)\n {\n ll mid=(l+r)/2;\n ll x=0;\n ll d=0;\n for(ll i=1;i<=n;i++)\n {\n\n if(mid%i==0)\n {\n if(mid/i>m)\n {\n x+=m;\n }\n else\n {\n x+=mid/i-1;\n d++;\n }\n\n }\n else\n {\n x+=min(mid/i,m);\n // if(mid==8)\n // cout << min(mid/i,m)<< \" \";\n }\n\n\n /* if(mid==8)\n cout << mid<<\": \" << i << \" \"<< x << \" \"<< d <\n#include \n\nusing namespace std;\n\nlong long a[4][2],b[2],c[2][2];\nlong long x[2],y[2],z[2];\n\nvoid init()\n{\n a[1][0]=a[0][1];a[1][1]=-a[0][0];\n a[2][0]=-a[0][0];a[2][1]=-a[0][1];\n a[3][0]=-a[0][1];a[3][1]=a[0][0];\n\n c[1][0]=c[0][1];c[1][1]=-c[0][0];\n}\n\nbool check()\n{\n long long det=x[0]*y[1]-x[1]*y[0];\n long long detx=z[0]*y[1]-z[1]*y[0];\n long long dety=x[0]*z[1]-x[1]*z[0];\n// cout<>a[0][0]>>a[0][1];\n cin>>b[0]>>b[1];\n cin>>c[0][0]>>c[0][1];\n init();\n bool ans=false;\n if(c[0][0]==0 && c[0][1]==0)\n {\n\t\tfor(int i=0;i<4;i++)\n\t\t{\n\t\t\tif(b[0]==a[i][0] && b[1]==a[i][1]){ans=true;break;}\n\t\t}\n }\n else\n {\n\t\tfor(int i=0;i<4;i++)\n\t\t{\n\t\t\tz[0]=b[0]-a[i][0];z[1]=b[1]-a[i][1];\n\t\t\tx[0]=c[0][0];x[1]=c[0][1];\n\t\t\ty[0]=c[1][0];y[1]=c[1][1];\n\t\t\tif(check()){ans=true;break;}\n\t\t}\n\n\t}\n\tif(ans)puts(\"YES\");\n\telse puts(\"NO\");\n}\n/****** PCCA -Tue Aug 09 12:36:45 GMT 2011 *******/", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "916aba2afe0091319b3aba5fe3811249", "src_uid": "cc8a8af1ba2b19bf081e379139542883", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define ld long double\n#define ll long long int\n#define mod 1000000007\n#define ll_inf 1000000000000000000\n#define int_inf 1000000000\n#define pb push_back\n#define endl '\\n'\n#define Endl '\\n'\n#define eps 1e-9\n#define PI acos(-1.0)\n#define ii pair\n#define fi first\n#define se second\n#define prout(x) cout.precision(x)\nusing namespace std;\nint a[5];\nint g[303][303];\nint cal(int x, int y){\n int &ret=g[x][y];\n if(g[x][y]!=-1)return g[x][y];\n ret=0;\n for(int i=1;i<=min(x, y);i++)ret|= !cal(x-i, y-i);\n for(int i=1;i<=x;i++)ret|=!cal(x-i, y);\n for(int i=1;i<=y;i++)ret|=!cal(x, y-i);\n return ret;\n}\nint main(){\n int n;\n /*\n memset(g,-1,sizeof g);\n for(int i=0;i<=100;i++){\n for(int j=0;j<=100;j++){\n for(int k=0;k<=100;k++){\n int x=cal(i, j, k);\n int z=(i^j);\n z^=k;\n if(x==0)cout<0)cout<<\"dssdf\\n\";\n }\n }\n }\n return 0;\n */\n cin>>n;\n for(int i=0;i>a[i];\n if(n==1){\n if(a[0]>0)cout<<\"BitLGM\"<\n#include\n#include\n#include\n#define eps 0.0001\nint x,y,xc,yc,r;\ndouble dist;\nint step;\n\nint main()\n{\n scanf(\"%d%d%d%d%d\",&r,&x,&y,&xc,&yc);\n \n dist=sqrt((long double)((long long)xc-x)*(xc-x)+((long long)yc-y)*(yc-y));\n \n step=dist/(r<<1);\n \n if(step*(r<<1)\r\nusing namespace std;\r\ntypedef long long LL;\r\nint i,j,n,m,k,l,o,p,mo;\r\ninline int read()\r\n{\r\n\tint f(1),x(0);\r\n\tchar ch=getchar();\r\n\twhile (!isdigit(ch)) {if (ch=='-') f*=-1;ch=getchar();}\r\n\twhile (isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();\r\n\treturn f*x;\r\n}\r\nint f[205][205][205];\r\nbool vis[205][205][205];\r\nint ny[205],jc;\r\nint dfs(int num,int sum,int now)\r\n{\r\n\tint rt(0);\r\n\tif (vis[num][sum][now]) return f[num][sum][now];\r\n\tvis[num][sum][now]=1;\r\n\tif (now<=i-1&&num>=1;\r\n\t}\r\n\treturn v;\r\n}\r\nint main()\r\n{\r\n\tn=read(),mo=read();\r\n\tint ans(0);\r\n\tfor (i=1,ny[0]=1;i<=n;i++) ny[i]=ny[i-1]*1ll*i%mo;\r\n\tfor (i=1,jc=ny[n];i<=n;i++) ny[i]=ksm(ny[i],mo-2);\r\n\tfor (i=0;i<=min(32,n);i++)\r\n\t{\r\n\t\tmemset(vis,0,sizeof vis);\r\n\t\tans=(ans+dfs(0,0,0))%mo;\r\n\t}\r\n\tprintf(\"%d\",1ll*ans*jc%mo);\r\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "1b1d53d1cf392191d24febcc2409b0fb", "src_uid": "cf57508de47d80bc983861f70bb5f3d6", "difficulty": 3200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define all(a) (a).begin(),(a).end()\n#define sqr(x) ((x) * (x))\n#define sz(a) int((a).size())\n#define FOR(i,a,b) for(int i=a;i vi;\ntypedef pair pii;\ntypedef pair pdd;\n#define eps 1e-8\n\nconst int maxn = 105;\n\n\nint main()\n{\n int yy1,yy2, mm1,mm2, dd1,dd2;\n scanf(\"%d:%d:%d\", &yy1, &mm1, &dd1);\n scanf(\"%d:%d:%d\", &yy2, &mm2, &dd2);\n\n int ans1 = 0;\n for(int i = 1900; i < yy1; ++i)\n {\n if(!(i % 4) &&( (i % 100) || !(i % 400) ))\n ans1 += 366;\n else \n ans1 += 365;\n }\n for(int i = 1; i < mm1; ++i)\n {\n if(i == 2) \n {\n if(!(yy1 % 4) &&( (yy1 % 100) || !(yy1 % 400) ))\n ans1 += 29;\n else\n ans1 += 28;\n }else if(i == 4 || i == 6 || i == 9 || i == 11)\n ans1 += 30;\n else\n ans1 += 31;\n }\n ans1 += dd1;\n\n int ans2 = 0;\n for(int i = 1900; i < yy2; ++i)\n {\n if(!(i % 4) &&( (i % 100) || !(i % 400) ))\n ans2 += 366;\n else \n ans2 += 365;\n }\n for(int i = 1; i < mm2; ++i)\n {\n if(i == 2) \n {\n if(!(yy2 % 4) &&( (yy2 % 100) || !(yy2 % 400) ))\n ans2 += 29;\n else\n ans2 += 28;\n }else if(i == 4 || i == 6 || i == 9 || i == 11)\n ans2 += 30;\n else\n ans2 += 31;\n }\n ans2 += dd2;\n\n cout << abs(ans1 - ans2);\n return 0;\n} ", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "237ca27a02ed824127dccd4cb7cc9b6c", "src_uid": "bdf99d78dc291758fa09ec133fff1e9c", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"Ofast,unroll-loops,no-stack-protector,fast-math\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n#pragma comment(linker, \"/STACK:16777216\")\n#include \n\nusing namespace std;\nusing matrix = vector>;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\n\n#define all(x) (x).begin(), (x).end()\n//#define int ll\n\nvoid FAST_IO() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n //cout.setf(ios::fixed);\n //cout.precision(20);\n #ifdef _offline\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif // _offline\n}\n\nconst int MAXN = 220;\nconst ll mod = 1e9 + 7;\nll dp[MAXN][MAXN], dp2[MAXN][MAXN];\nint z[MAXN];\nint n;\nstring s;\n\nll solve(int pos) {\n memset(dp, 0, sizeof(dp));\n dp[0][0] = 1;\n for (int i = 1; i <= 2 * n; ++i) {\n for (int j = 0; j <= i; ++j) {\n if (pos <= i && pos + s.length() - 1 >= i) {\n if (s[i - pos] == '(') {\n dp[i][j] = (dp[i][j] + (j ? dp[i - 1][j - 1] : 0)) % mod;\n }\n else {\n dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod;\n }\n if (pos + s.length() - 1 > i && z[s.length() - (i - pos) - 1] == i - pos + 1) {\n dp[i][j] = (dp[i][j] - dp2[i][j] + mod) % mod;\n }\n if (pos + s.length() - 1 == i) {\n dp2[i][j] = dp[i][j];\n }\n }\n else {\n dp[i][j] = (dp[i][j] + (j ? dp[i - 1][j - 1] : 0)) % mod;\n dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod;\n if (pos + s.length() - 1 > i) {\n dp[i][j] = (dp[i][j] - dp2[i][j] + mod) % mod;\n }\n }\n }\n }\n return dp[2 * n][0];\n}\n\nsigned main() {\n FAST_IO();\n cin >> n >> s;\n if (s.length() > 2 * n) {\n cout << 0;\n return 0;\n }\n int l = 0, r = 0;\n z[0] = 0;\n for (int i = 1; i < s.length(); ++i) {\n z[i] = max(0, min(z[i - l], r - i));\n while (s[z[i]] == s[i + z[i]]) {\n z[i]++;\n }\n if (i + z[i] > r) {\n l = i;\n r = i + z[i];\n }\n }\n ll ans = 0;\n for (int pos = 1; pos <= 2 * n - s.length() + 1; ++pos) {\n ans = (ans + solve(pos)) % mod;\n }\n cout << ans;\n}\n", "lang_cluster": "C++", "tags": ["strings", "dp"], "code_uid": "fb320b3276b5e0d70b95622fd1b6ac33", "src_uid": "590a49a7af0eb83376ed911ed488d7e5", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n \nint main()\n{\n int n;\n long long mod=1000000007;\n cin>>n;\n long long p=3, pow=3;\n if (n==1) {\n cout<<0<=mod) pow%=mod;\n p=pow-p;\n if (p<0) p=(p+mod)%mod;\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nconst long long mod = 1000000007;\n\nint main() {\n\tint n, m;\n\tcin >> n >> m;\n\tif (n < m)\n\t\tswap(n, m);\n\tif (n == 1 && m == 1) {\n\t\tcout << 2;\n\t\treturn 0;\n\t}\n\t\n\tif (m == 1) {\n\t\tvector d00(n);\n\t\tvector d01(n);\n\t\tvector d10(n);\n\t\tvector d11(n);\n\t\td00[1] = 1;\n\t\td01[1] = 1;\n\t\td10[1] = 1;\n\t\td11[1] = 1;\n\t\tfor (int i = 2; i < n; ++i) {\n\t\t\td00[i] = d10[i - 1];\n\t\t\td10[i] = (d01[i - 1] + d11[i - 1]) % mod;\n\t\t\td11[i] = d01[i - 1];\n\t\t\td01[i] = (d10[i - 1] + d00[i - 1]) % mod;\n\t\t}\n\t\t\n\t\tlong long cnt = (d00[n - 1] + d01[n - 1] + d10[n - 1] + d11[n - 1]) % mod;\n\t\t\n\t\tcout << cnt;\n\t\treturn 0;\n\t}\n\t\n\tlong long cnt1 = 0;\n\t{\n\t\tvector d00(n);\n\t\tvector d01(n);\n\t\tvector d10(n);\n\t\tvector d11(n);\n\t\td00[1] = 1;\n\t\td01[1] = 1;\n\t\td10[1] = 1;\n\t\td11[1] = 1;\n\t\tfor (int i = 2; i < n; ++i) {\n\t\t\td00[i] = d10[i - 1];\n\t\t\td10[i] = (d01[i - 1] + d11[i - 1]) % mod;\n\t\t\td11[i] = d01[i - 1];\n\t\t\td01[i] = (d10[i - 1] + d00[i - 1]) % mod;\n\t\t}\n\t\t\n\t\tcnt1 = (d00[n - 1] + d01[n - 1] + d10[n - 1] + d11[n - 1]) % mod;\n\t}\n\t\n\tlong long cnt2 = 0;\n\t{\n\t\tvector d00(m);\n\t\tvector d01(m);\n\t\tvector d10(m);\n\t\tvector d11(m);\n\t\td00[1] = 1;\n\t\td01[1] = 1;\n\t\td10[1] = 1;\n\t\td11[1] = 1;\n\t\tfor (int i = 2; i < m; ++i) {\n\t\t\td00[i] = d10[i - 1];\n\t\t\td10[i] = (d01[i - 1] + d11[i - 1]) % mod;\n\t\t\td11[i] = d01[i - 1];\n\t\t\td01[i] = (d10[i - 1] + d00[i - 1]) % mod;\n\t\t}\n\t\t\n\t\tcnt2 = (d00[m - 1] + d01[m - 1] + d10[m - 1] + d11[m - 1]) % mod;\n\t}\n\t\n\tcout << (cnt1 + cnt2 - 2 + mod) % mod;\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "d64aa08fe8f5fe03d653173d4317f562", "src_uid": "0f1ab296cbe0952faa904f2bebe0567b", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main() {\n\t// your code goes here\n\tint n;\n\tcin >> n;\n\tchar steps[n];\n\tcin >> steps;\n\tint LR = 0, UD = 0;\n\tfor(int i = 0; i < n; i++)\n\t{\n\t\tswitch( steps[i]) {\n\t\t\tcase 'L':\n\t\t\t\tLR--;\n\t\t\t\tbreak;\n\t\t\tcase 'U':\n\t\t\t\tUD++;\n\t\t\t\tbreak;\n\t\t\tcase 'D':\n\t\t\t\tUD--;\n\t\t\t\tbreak;\n\t\t\tcase 'R':\n\t\t\t\tLR++;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tcout << n -abs(LR) -abs( UD);\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "c55aa33312034feb3ebe55de6c9bd1db", "src_uid": "b9fa2bb8001bd064ede531a5281cfd8a", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#ifdef HOME_PC\n#include \n#endif\nusing namespace std;\n\n#pragma comment(linker,\"/stack:16000000\")\n#pragma warning (disable : 4996)\n\n#define ALL(v) v.begin(),v.end()\n#define SZ(v) (int)v.size()\n#define mset(A,x) memset((A),(x),sizeof(A))\n#define FOR(i,start,N) for(int i=(start);i<(N);++i)\n#define FORSZ(i,start,v) FOR(i,start,SZ(v))\n#define REPSZ(i,v) FORSZ(i,0,v)\n#define FORE(i,start,N) FOR(i,start,N+1)\n#define make_unique(v) v.resize(unique(ALL(v))-v.begin())\n#define debug(x) cout<<#x<<\" = \"< pii;\ntypedef long long i64;\ntypedef vector VI; typedef vector< vector > VVI;\ntypedef vector VS;\n\nconst int inf = 1<<25;\nconst double eps = 1e-9;\n\n\nstruct Matrix \n{\n static const int mod = 1000000007;\n static const int N = 8;\n\n int a[N][N];\n\n Matrix() { mset(a,0); }\n Matrix& I()\n {\n mset(a,0);\n FOR(i,0,N) a[i][i] = 1;\n return *this;\n }\n\n Matrix operator+(const Matrix& A) const \n {\n Matrix ret;\n FOR(i,0,N)\n FOR(j,0,N)\n ret(i,j) = (a[i][j] + A(i,j))%mod;\n return ret;\n }\n\n Matrix operator*(const Matrix& A) const \n {\n Matrix ret;\n\n FOR(k,0,N) FOR(i,0,N) FOR(j,0,N)\n ret(i,j) = (ret(i,j) + (i64)(a[i][k])*A(k,j)) % mod;\n return ret;\n }\n\n Matrix operator^(int n) const\n {\n Matrix ret;\n ret.I();\n Matrix A(*this);\n while(n)\n {\n if(n&1)\n ret = ret*A;\n A = A*A;\n n >>= 1;\n }\n return ret;\n }\n\n int sum() const \n {\n int ret = 0;\n FOR(i,0,N)\n FOR(j,0,N)\n ret = (ret+a[i][j]) % mod;\n return ret;\n }\n\n int& operator()(int i,int j)\n {\n return a[i][j];\n }\n\n const int& operator()(int i,int j) const\n {\n return a[i][j];\n }\n\n};\n\n\nMatrix calcGeom(const Matrix& A,int n)\n{\n Matrix ret;\n if(n == 0)\n return ret.I();\n if(n%2 == 0)\n ret = A^n, n--;\n\n int mid = (n+1)/2;\n return ret + (Matrix().I() + (A^mid))*calcGeom(A,mid-1);\n \n}\n\nint g(const Matrix& A,int L,int R)\n{\n \n \n int ret = 0;\n if(L<1)\n throw 0;\n if(L<2)\n ++L, ret = 4;\n if(L > R)\n return ret;\n L-=2, R-=2;\n \n\n Matrix X = (A^L)*calcGeom(A,R-L);\n return (ret + X.sum())%Matrix::mod;\n}\n\ni64 fastPow(i64 a,i64 n,i64 mod)\n{\n i64 ret = 1;\n while(n>0)\n {\n if(n&1)\n ret*=a, ret%=mod;\n a*=a, a%=mod;\n n>>=1;\n }\n return ret%mod;\n}\n\nint f(const Matrix& A,int L,int R)\n{\n if(L == 1 && R == 1)\n return 4;\n \n int ans = 0;\n if(L == 1)\n ++L, ans = 4;\n\n int all = g(A,L,R);\n if(L%2 == 0) ++L;\n if(R%2 == 0) --R;\n int pals = L<=R ? g(A,(L+1)/2,(R+1)/2):0;\n ans += ((i64)all+pals)%Matrix::mod * fastPow(2,Matrix::mod-2,Matrix::mod) % Matrix::mod;\n ans += Matrix::mod;\n ans %= Matrix::mod;\n return ans;\n}\n\n\nenum COLOR{WHITE,BLACK,RED,YELLOW};\n\nbool allowed[4][4];\n\n\nint main()\n{\n#ifdef HOME_PC\n //freopen (\"input.txt\",\"r\",stdin);\n //freopen (\"in.txt\",\"r\",stdin);\n //freopen (\"output.txt\",\"w\",stdout);\n#endif\n\n int L,R;\n cin>>L>>R;\n\n Matrix A;\n\n// cout<\n#define pb push_back\n#define ll long long\n#define MAX (ll)1000000\n#define mp make_pair\n\nusing namespace std;\n\nint a,b,c,d;\nbool f[10000000],s[10000000];\n\nint main()\n{\n cin>>a>>b>>c>>d;\n for(int i=0;i<=10000;i++)\n {\n f[b+i*a]=true;\n s[d+i*c]=true;\n }\n for(int i=0;i<10000000;i++){\n if(f[i]==true && s[i]==true) {cout<\n#include \nusing namespace std;\n\n#define int long long\n#define pb push_back\n#define in insert\n#define F first\n#define S second\n#define mod 1000000007\n#define endl '\\n'\n#define all(x) x.begin(),x.end()\n#define w(x) int x;cin>>x; while(x-->0)\n#define memset(a,b) memset(a,b,sizeof(a))\n#define debug(x) cout << \"[ \" << #x << \" : \" << x << \" ]\" << endl;\n\nvoid fast()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n}\n/*\nthink fast\nthink simple\n*/\nint n, k;\nbool check(int now)\n{\n\tint lines = 0;\n\twhile (now)\n\t{\n\t\tlines += now;\n\t\tnow /= k;\n\t}\n\treturn lines >= n;\n}\nvoid mysol()\n{\n\tcin >> n >> k;\n\tint l = 0;\n\tint r = 1e9 + 9;\n\twhile (l < r)\n\t{\n\t\tint mid = (l + r) >> 1;\n\t\tif (check(mid))\n\t\t\tr = mid;\n\t\telse\n\t\t\tl = mid + 1;\n\t}\n\tcout << l << endl;\n}\n\nint32_t main()\n{\n\tfast();\n\t// w(t)\n\t{\n\t\tmysol();\n\t}\n\t//cerr << sizeof(dp)*(1e-6) << \" megabytes\\n\";\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation", "binary search"], "code_uid": "82c26b0af63a5bb30909ece6d636f77d", "src_uid": "41dfc86d341082dd96e089ac5433dc04", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n int n;\n cin>>n;\n string s;\n cin>>s;\n int f=1;\n int a1=0,b1=0,a2=0,b2=0;\n for(int i=0;i\n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntemplate>\nclass Vector : public vector\n{\npublic:\n\tusing vector::vector;\n\tVector& operator()(size_t dummy_n) // additional init_val needs its own specification for Vector\n\t{\n\t\treturn *this;\n\t}\n};\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pdd;\ntypedef Vector vi; // Vector\ntypedef vector vll;\ntypedef vector vb;\ntypedef vector vd;\ntypedef vector vld;\ntypedef vector vpii;\ntypedef vector vpdd;\ntypedef vector vs;\ntypedef set si;\ntypedef multiset Si;\ntypedef map mi;\ntypedef multimap Mi;\ntemplate \nInt read()\n{\n\tInt flag = 1;\n\tchar c = getchar();\n\twhile ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();\n\tif (c == '-') flag = -1, c = getchar();\n\tInt init = c & 15;\n\twhile ((c = getchar()) >= '0' && c <= '9') init = (init << 3) + (init << 1) + (c & 15);\n\treturn init * flag;\n}\nvoid read(char& a) { do scanf(\"%c\", &a); while (isspace(a)); }\nvoid write(const char& a) { printf(\"%c\", a); }\nchar BUF[1234560];\nvoid read(string& a) { scanf(\"%s\", BUF); a = string(BUF); }\nvoid write(const string& a) { printf(\"%s\", a.c_str()); }\ninline void read(int& a) { /*scanf(\"%d\", &a);*/a = read(); }\nvoid write(const int& a) { printf(\"%d\", a); }\n//void write(const size_t& a) { printf(\"%u\", a); }\nvoid read(ll& a) { scanf(\"%I64d\", &a); }\nvoid write(const ll& a) { printf(\"%I64d\", a); }\nvoid read(double& a) { scanf(\"%lf\", &a); }\nvoid write(const double& a) { printf(\"%lf\", a); }\nvoid read(ld& a) { scanf(\"%Lf\", &a); }\nvoid write(const ld& a) { printf(\"%Lf\", a); }\nvoid read(pair& a) { scanf(\"%d%d\", &a.first, &a.second); }\nvoid write(const pair& a) { printf(\"%d %d\", a.first, a.second); }\ntemplate > class C, class T>\nvoid read(C& a) { for (auto i = a.begin(); i != a.end(); ++i) read(*i); }\ntemplate > class C, class T>\nvoid write(const C& a) { int sz = a.size(); if (sz != 0) write(a[0]); for (int i = 1; i < sz; ++i) { write(' '); write(a[i]); } }\ntemplate\nvoid read(T1& t1, T2& t2) { read(t1); read(t2); }\ntemplate\nvoid write(const T1& t1, const T2& t2) { write(t1); write(' '); write(t2); }\ntemplate\nvoid read(T1& t1, T2& t2, T3& t3) { read(t1); read(t2); read(t3); }\ntemplate\nvoid read(T1& t1, T2& t2, T3& t3, T4& t4) { read(t1); read(t2); read(t3); read(t4); }\ntemplate\nvoid write(const T1& t1, const T2& t2, const T3& t3) { write(t1); write(' '); write(t2); write(' '); write(t3); }\ntemplate > class C, class T>\nvoid sort(C& a) { sort(a.begin(), a.end()); }\nvoid sort(string& a) { sort(a.begin(), a.end()); }\ntemplate > class C, class T>\nvoid rsort(C& a) { sort(a.rbegin(), a.rend()); }\nvoid rsort(string& a) { sort(a.rbegin(), a.rend()); }\ntemplate\nint sz(const T& a) { return int(a.size()); }\n#define MX 1000000000\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define T(n) for(int _VARIABLE{(n) + 1}; --_VARIABLE;)\n#define F(b,e,i) for(auto i = (b); i != (e); (b) < decltype(b)(e) ? ++i : --i)\n#define G(f,l,i) for(auto i = (f) + ((f) < decltype(f)(l) ? -1 : 1); ((f) < decltype(f)(l) ? i++ : i--) != (l) ;)\n#define M(f,l,i,j) for(auto i = (f) , j = decltype(f)(l) ; i < j; ++i, --j)\n#define r(...) read(__VA_ARGS__)\n#define R(type, ...) type __VA_ARGS__; r(__VA_ARGS__)\n#define w(...) write(__VA_ARGS__)\n#define W(...) write(__VA_ARGS__), write('\\n')\n#define bgn begin()\n#define rbgn rbegin()\n#define nd end()\n#define rnd rend()\n#define pb push_back\n#define fnt front()\n#define bck back()\n#define x first\n#define y second\n\nint main()\n{\n#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tR(int, t);\n\tT(t) {\n\t\tR(int, n, k, d);\n\t\tR(vi, a(n));\n\t\tvi st(k + 1);\n\t\tint st_sz{}, i{}, mn_sz{ d };\n\t\tfor (; i < d; ++i)\n\t\t\tif (st[a[i]]++ == 0)\n\t\t\t\t++st_sz;\n\t\tfor (--i;;) {\n\t\t\tif (st_sz < mn_sz)\n\t\t\t\tmn_sz = st_sz;\n\t\t\tif (++i >= n)\n\t\t\t\tbreak;\n\t\t\tif (!--st[a[i - d]])// erase from set\n\t\t\t\t--st_sz;// update set size\n\t\t\tif (!st[a[i]]++)// insert into set\n\t\t\t\t++st_sz;// update set size;\n\t\t}\n\t\tW(mn_sz);\n\t}\n}\n//\n// ArtemParkhomets\n//", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "79cf11586543c5bc0b7f71f9bde870d2", "src_uid": "56da4ec7cd849c4330d188d8c9bd6094", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// In the name of God\n\n#include \n\nusing namespace std;\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\t\n\tlong long n;\n\tcin >> n;\n\n\tauto is_prime = [](long long x) -> bool {\n\t\tif(x <= 1) return false;\n\t\tfor(long long i = 2; i * i <= x; i++) if(x % i == 0) return false;\n\t\treturn true;\n\t};\n\n\tif(is_prime(n)) {\n\t\tcout << 1;\n\t\texit(0);\n\t}\n\n\tint primes[] = {2, 3, 5, 7, 11, 13, 17, 19};\n\t\n\tif(n % 2 == 0) cout << 2;\n\telse {\n\t\tif(is_prime(n - 2)) {\n\t\t\tcout << 2;\n\t\t\texit(0);\n\t\t}\n\t\tcout << 3;\n\t}\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "0fb1777abda2322e374d86d8d7348bee", "src_uid": "684ce84149d6a5f4776ecd1ea6cb455b", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "# include \nusing namespace std;\n\nconst int mod = 1e9 + 7;\n#define ll long long\n#define FOR(i,a,b) for(int i = a;i <= b;++i)\n#define _FOR(i,a,b) for(int i = a;i >= b;--i)\n\ntemplatevoid read(T &x)\n{\n x = 0;int f = 1;\n char c = getchar();\n for(;!isdigit(c);c = getchar()) if(c == '-') f = -1;\n for(;isdigit(c);c = getchar()) x = x * 10 + c - '0';\n x *= f;\n}\n\nmap ans;\nint x,y;\nll a[100010];\nint t;\n\nint poww(ll a,ll b)\n{\n\tll ans = 1ll;\n\twhile(b > 0)\n\t{\n\t\tif(b & 1) ans = ans * a % (1ll * mod);\n\t\tb /= 2ll;\n\t\ta = a * a % mod;\n\t}\n\treturn ans;\n}\n\nint main()\n{\n //freopen(\"1.in\",\"r\",stdin);\n //freopen(\"1.out\",\"w\",stdout);\n\tread(x),read(y);\n\tif(y % x) {puts(\"0\");return 0;}\n\tfor(int i = 1; i * i <= y / x; ++i)\n\t\tif((y / x) % i == 0)\n\t\t{\n\t\t\ta[++t] = i;\n\t\t\tif(i * i != (y / x)) a[++t] = (y / x) / i;\n\t\t}\n\tsort(a + 1,a + 1 + t);\n\t_FOR(i,t,1)\n\t{\n\t\tans[i] = poww(2,y / (x * a[i]) - 1);\n\t\tFOR(j,i + 1,t)\n\t\t\tif(a[j] % a[i] == 0)\n\t\t\t\tans[i] = (ans[i] - ans[j] + mod) % mod;\n\t}\n\tprintf(\"%d\\n\",ans[1]);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "bitmasks", "number theory"], "code_uid": "727dd27e05333d3e3266e66f35249e44", "src_uid": "de7731ce03735b962ee033613192f7bc", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n int n;\n cin>>n;\n if (n%2==0) cout<\nusing namespace std;\nint n,ans = 0;\nvoid dfs(int cur) {\n\tif(cur>n) return;\n\telse ans++;\n\tdfs(cur*10+1);\n\tdfs(cur*10);\n}\nint main() {\n\tcin>>n;\n\tdfs(1);\n cout<\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntemplate ostream& operator<<(ostream& o,const pair &p){\n\treturn o<<\"(\"< ostream& operator<<(ostream& o,const vector &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o< using V = vector;\ntemplate using VV = vector>;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) true\n#define dump(x) true\n#endif\n\ntemplate\nstruct ModInt{\n\tusing uint = unsigned int;\n\tusing ll = long long;\n\tusing ull = unsigned long long;\n\n\tconstexpr static uint mod = mod_;\n\n\tuint v;\n\tModInt():v(0){}\n\tModInt(ll _v):v(normS(_v%mod+mod)){}\n\texplicit operator bool() const {return v!=0;}\n\tstatic uint normS(const uint &x){return (x [0 , mod-1]\n\tstatic ModInt make(const uint &x){ModInt m; m.v=x; return m;}\n\tModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}\n\tModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}\n\tModInt operator-() const { return make(normS(mod-v)); }\n\tModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}\n\tModInt operator/(const ModInt& b) const { return *this*b.inv();}\n\tModInt& operator+=(const ModInt& b){ return *this=*this+b;}\n\tModInt& operator-=(const ModInt& b){ return *this=*this-b;}\n\tModInt& operator*=(const ModInt& b){ return *this=*this*b;}\n\tModInt& operator/=(const ModInt& b){ return *this=*this/b;}\n\tModInt& operator++(int){ return *this=*this+1;}\n\tModInt& operator--(int){ return *this=*this-1;}\n\tll extgcd(ll a,ll b,ll &x,ll &y) const{\n\t\tll p[]={a,1,0},q[]={b,0,1};\n\t\twhile(*q){\n\t\t\tll t=*p/ *q;\n\t\t\trep(i,3) swap(p[i]-=t*q[i],q[i]);\n\t\t}\n\t\tif(p[0]<0) rep(i,3) p[i]=-p[i];\n\t\tx=p[1],y=p[2];\n\t\treturn p[0];\n\t}\n\tModInt inv() const {\n\t\tll x,y;\n\t\textgcd(v,mod,x,y);\n\t\treturn make(normS(x+mod));\n\t}\n\tModInt pow(ll p) const {\n\t\tif(p<0) return inv().pow(-p);\n\t\tModInt a = 1;\n\t\tModInt x = *this;\n\t\twhile(p){\n\t\t\tif(p&1) a *= x;\n\t\t\tx *= x;\n\t\t\tp >>= 1;\n\t\t}\n\t\treturn a;\n\t}\n\tbool operator==(const ModInt& b) const { return v==b.v;}\n\tbool operator!=(const ModInt& b) const { return v!=b.v;}\n\tfriend istream& operator>>(istream &o,ModInt& x){\n\t\tll tmp;\n\t\to>>tmp;\n\t\tx=ModInt(tmp);\n\t\treturn o;\n\t}\n\tfriend ostream& operator<<(ostream &o,const ModInt& x){ return o<;\n\nint bsr(int x) { return 31 - __builtin_clz(x); }\nusing D = double;\nconst D pi = acos(-1);\nusing Pc = complex;\n\nvoid fft(bool type, vector &c){\t//multiply : false -> mult -> true\n\tstatic vector buf[30];\n\tint N = c.size();\n\tint s = bsr(N);\n\tassert(1<(N);\n\t\trep(i,N) buf[s][i] = polar(1,i*2*pi/N);\n\t}\n\tvector a(N),b(N);\n\tcopy(begin(c),end(c),begin(a));\n\trep1(i,s){\n\t\tint W = 1<<(s-i);\n\t\tfor(int y=0;y>1] = l-r;\n\t\t\t}\n\t\t}\n\t\tswap(a,b);\n\t}\n\tcopy(begin(a),end(a),begin(c));\n}\ntemplate\nvector multiply_fft2(const vector& x,const vector& y){\n\tconst int B = 15;\n\tint S = x.size()+y.size()-1;\n\tint N = 2< a[2],b[2];\n\trep(t,2){\n\t\ta[t] = vector(N);\n\t\tb[t] = vector(N);\n\t\trep(i,x.size()) a[t][i] = Pc( (x[i].v >> (t*B)) & ((1<> (t*B)) & ((1< z(S);\n\tvector c(N);\n\tMint base = 1;\n\trep(t,3){\n\t\tfill_n(begin(c),N,Pc(0,0));\n\t\tfor(int xt = max(t-1,0); xt<=min(1,t); xt++){\n\t\t\tint yt = t-xt;\n\t\t\trep(i,N) c[i] += a[xt][i]*b[yt][i];\n\t\t}\n\t\tfft(true,c);\n\t\trep(i,S){\n\t\t\tc[i] *= 1.0/N;\n\t\t\tz[i] += Mint(ll(round(c[i].real()))) * base;\n\t\t}\n\t\tbase *= 1<\nvector multiply_fft(const vector& x,const vector& y){\n\treturn multiply_fft2(x,y);\n}\n\n\ntemplate\nstruct Poly{\n\tvector v;\n\tint size() const{ return v.size();}\t//deg+1\n\tPoly(int N=0) : v(vector(N)){}\n\tPoly(vector v) : v(v){shrink();}\n\n\tPoly& shrink(){\n\t\twhile(!v.empty()&&v.back()==D(0)) v.pop_back();\t//double? iszeroをglobalに用意したほうがいいかな\n\t\treturn *this;\n\t}\n\tD at(int i) const{\n\t\treturn (i=size() && !x) return;\n\t\twhile(i>=size()) v.push_back(D(0));\n\t\tv[i]=x;\n\t\tshrink();\n\t\treturn;\n\t}\n\t\n\tPoly operator+(const Poly &r) const{\n\t\tint N=max(size(),r.size());\n\t\tvector ret(N);\n\t\trep(i,N) ret[i]=at(i)+r.at(i);\n\t\treturn Poly(ret);\n\t}\n\tPoly operator-(const Poly &r) const{\n\t\tint N=max(size(),r.size());\n\t\tvector ret(N);\n\t\trep(i,N) ret[i]=at(i)-r.at(i);\n\t\treturn Poly(ret);\n\t}\n\tPoly operator-() const{\n\t\tint N=size();\n\t\tvector ret(N);\n\t\trep(i,N) ret[i] = -at(i);\n\t\treturn Poly(ret);\n\t}\n\tPoly operator*(const Poly &r) const{\n\t\tif(size()==0||r.size()==0) return Poly();\n\t\treturn mul_fft(r);\n\t}\n\tPoly operator*(const D &r) const{\n\t\tint N=size();\n\t\tvector ret(N);\n\t\trep(i,N) ret[i]=v[i]*r;\n\t\treturn Poly(ret);\n\t}\n\tPoly operator/(const Poly &y) const{\n\t\treturn div_fast(y);\n\t}\n\tPoly operator%(const Poly &y) const{\n\t\treturn rem_fast(y);\n//\t\treturn rem_naive(y);\n\t}\n\tPoly operator<<(const int &n) const{\t// *=x^n\n\t\tassert(n>=0);\n\t\tint N=size();\n\t\tvector ret(N+n);\n\t\trep(i,N) ret[i+n]=v[i];\n\t\treturn Poly(ret);\n\t}\n\tPoly operator>>(const int &n) const{\t// /=x^n\n\t\tassert(n>=0);\n\t\tint N=size();\n\t\tif(N<=n) return Poly();\n\t\tvector ret(N-n);\n\t\trep(i,N-n) ret[i]=v[i+n];\n\t\treturn Poly(ret);\n\t}\n\tbool operator==(const Poly &y) const{\n\t\treturn v==y.v;\n\t}\n\tbool operator!=(const Poly &y) const{\n\t\treturn v!=y.v;\n\t}\n\n\tPoly& operator+=(const Poly &r) {return *this = *this+r;}\n\tPoly& operator-=(const Poly &r) {return *this = *this-r;}\n\tPoly& operator*=(const Poly &r) {return *this = *this*r;}\n\tPoly& operator*=(const D &r) {return *this = *this*r;}\n\tPoly& operator%=(const Poly &y) {return *this = *this%y;}\n\tPoly& operator<<=(const int &n) {return *this = *this<>=(const int &n) {return *this = *this>>n;}\n\n\n\tPoly mul_naive(const Poly &r) const{\n\t\tint N=size(),M=r.size();\n\t\tvector ret(N+M-1);\n\t\trep(i,N) rep(j,M) ret[i+j]+=at(i)*r.at(j);\n\t\treturn Poly(ret);\n\t}\n\tPoly mul_ntt(const Poly &r) const{\n\t\treturn Poly(multiply_ntt(this->v,r.v));\n\t}\n\tPoly mul_fft(const Poly &r) const{\n\t\tvector ret = multiply_fft(v,r.v);\n\t\treturn Poly(ret);\n\t}\n\n\tPoly div_fast_with_inv(const Poly &inv, int B) const {\n\t\treturn (*this * inv)>>(B-1);\n\t}\n\tPoly div_fast(const Poly &y) const{\n\t\tif(size() res = v;\n\t\tres.resize(min(n,size()));\n\t\treturn Poly(res);\n\t}\n\tPoly rev(int n = -1) const {\t//ignore x^n ~ -> return x^(n-1) * f(1/x)\n\t\tvector res = v;\n\t\tif(n!=-1) res.resize(n);\n\t\treverse(all(res));\n\t\treturn Poly(res);\n\t}\n\tPoly inv(int n) const{\t\t// f * f.inv() = x^B + r(x) (B>=n)\n\t\tint N = size();\n\t\tassert(N>=1);\t\t//f : non0\n\t\tassert(n>=N-1);\t\t//f = .. + c_{N-1}x^{N-1}\n\t\tD coef = D(1)/at(N-1);\n\t\tPoly a = rev();\n\t\tPoly g(vector(1,coef));\n\t\tfor(int i=1; i+N-2{2})-a*g).strip(2*i);\n\t\t}\n\t\treturn g.rev(n+1-N);\n\t}\n\n\tfriend ostream& operator<<(ostream &o,const Poly& x){\n\t\tif(x.size()==0) return o<<0;\n\t\trep(i,x.size()) if(x.v[i]!=D(0)){\n\t\t\to< fact,ifact;\nmint Choose(int a,int b){\n\tif(b<0 || a=0;i--) ifact[i] = ifact[i+1] * (i+1);\n}\n\ntemplate\nPoly berlekamp_massey(const vector &u){\n\tint N = u.size();\n\tvector b = {D(-1)}, c = {D(-1)};\n\tD y = D(1);\n\trep1(n,N){\n\t\tint L = c.size(), M = b.size();\n\t\tD x = 0;\n\t\trep(i,L) x += c[i]*u[n-L+i];\n\t\tb.pb(0);\n\t\tM++;\n\t\tif(!x) continue;\n\t\tD coef = x/y;\n\t\tif(L(c);\n}\n\nvector extended(int n, const vector< vector >& coeffs, const vector& terms) {\n\n\tvector ret(max(n + 1, terms.size()));\n\tcopy(terms.begin(), terms.end(), ret.begin());\n\tconst int order = coeffs.size() - 1;\n\tconst int deg = coeffs[0].size() - 1;\n\tassert((int) terms.size() >= order);\n\tfor (int m = terms.size(); m <= n; ++m) {\n\t\tmint s = 0;\n\t\tfor (int i = 1; i <= order; ++i) {\n\t\t\tint k = m - i;\n\t\t\tmint t = ret[k];\n\t\t\tfor (int d = 0; d <= deg; ++d) {\n\t\t\t\ts += t * coeffs[i][d];\n\t\t\t\tt *= k;\n\t\t\t}\n\t\t}\n\t\tmint denom = 0, mpow = 1;\n\t\tfor (int d = 0; d <= deg; ++d) {\n\t\t\tdenom += mpow * coeffs[0][d];\n\t\t\tmpow *= m;\n\t\t}\n\t\tret[m] = -s/denom;\n\t}\n\treturn ret;\n}\n\nvector< vector > find_recurrence_relation(vector terms, int deg, int ord = -1, bool verify=true) {\n\n\tif(ord != -1){\t\t//given order\n\t\tint n = (deg+1)*(ord+1)+ord-1;\n\t\twhile((int)terms.size()>n) terms.pop_back();\n\t}\n\n\tconst int n = terms.size();\n\tconst int B = (n + 2) / (deg + 2); // number of blocks\n\tconst int C = B * (deg + 1); // number of columns\n\tconst int R = n - (B - 1); // number of rows\n\tassert(B >= 2); assert(R >= C - 1);\n\n\tauto error = [] (int order, int deg) {\n\t\tfprintf(stderr, \n\t\t\t\"Error: Could not find a recurrence relation \"\n\t\t\t\"of order <= %d and degree <= %d.\\n\\n\", \n\t\t\torder, deg);\n\t\tassert(0);\n\t};\n\n\tvector< vector > mat(R, vector(C));\n\tfor (int y = 0; y < R; ++y) {\n\t\tfor (int b = 0; b < B; ++b) {\n\t\t\tmint v = terms[y+b];\n\t\t\tfor (int d = 0; d <= deg; ++d) {\n\t\t\t\tmat[y][b * (deg + 1) + d] = v;\n\t\t\t\tv *= y+b;\n\t\t\t}\n\t\t}\n\t}\n\n\tint rank = 0;\n\tfor (int x = 0; x < C; ++x) {\n\t\tint pivot = -1;\n\t\tfor (int y = rank; y < R; ++y) if (mat[y][x] != 0) {\n\t\t\tpivot = y; break;\n\t\t}\n\t\tif (pivot < 0) break;\n\t\tif (pivot != rank) swap(mat[rank], mat[pivot]);\n\t\tmint inv = mat[rank][x].inv();\n\t\tfor (int x2 = x; x2 < C; ++x2) mat[rank][x2] *= inv;\n\t\tfor (int y = rank + 1; y < R; ++y) if (mat[y][x]) {\n\t\t\tmint c = -mat[y][x];\n\t\t\tfor (int x2 = x; x2 < C; ++x2) {\n\t\t\t\tmat[y][x2] += c * mat[rank][x2];\n\t\t\t}\n\t\t}\n\t\t++rank;\n\t}\n\n\tif (rank == C) error(B - 1, deg);\n\n\tfor (int y = rank - 1; y >= 0; --y) if (mat[y][rank]) {\n\t\tassert(mat[y][y] == 1);\n\t\tmint c = -mat[y][rank];\n\t\tfor (int y2 = 0; y2 < y; ++y2) {\n\t\t\tmat[y2][rank] += c * mat[y2][y];\n\t\t}\n\t}\n\n\tint order = rank / (deg + 1);\n\n\tvector< vector > ret(order + 1, vector(deg + 1));\n\tret[0][rank % (deg + 1)] = 1;\n\tfor (int y = rank - 1; y >= 0; --y) {\n\t\tint k = order - y / (deg + 1), d = y % (deg + 1);\n\t\tret[k][d] = -mat[y][rank];\n\t}\n\n\tif (verify) {\n\t\tauto extended_terms = extended(n - 1, ret, \n\t\t\t\tvector(terms.begin(), terms.begin() + order));\n\t\tfor (int i = 0; i < (int) terms.size(); ++i) {\n\t\t\tif (terms[i] != extended_terms[i]) error(B - 1, deg);\n\t\t}\n\t}\n\n\tauto verbose = [&] {\n\t\tint last = verify ? n - 1 : order + R - 1;\n\t\tfprintf(stderr, \n\t\t\t\"[ Found a recurrence relation ]\\n\"\n\t\t\t\"- order %d\\n\"\n\t\t\t\"- degree %d\\n\"\n\t\t\t\"- verified up to a(%d) (number of non-trivial terms: %d)\\n\",\n\t\t\torder, deg, last, (last + 1) - ((deg + 2) * (order + 1) - 2)\n\t\t);\n\t\tfprintf(stderr, \"{\\n\");\n\t\tfor (int k = 0; k <= order; ++k) {\n\t\t\tfprintf(stderr, \" {\");\n\t\t\tfor (int d = 0; d <= deg; ++d) {\n\t\t\t\tif (d) fprintf(stderr, \", \");\n\t\t\t\tfprintf(stderr, \"%d\", ret[k][d].v);\n\t\t\t}\n\t\t\tfprintf(stderr, \"}%s\\n\", k == order ? \"\" : \",\");\n\t\t}\n\t\tfprintf(stderr, \"}\\n\\n\");\n\t};\n\tverbose();\n\n\treturn ret;\n}\n\nvoid show_extended_sequence(int n, const vector& terms, int degree, int order = -1) {\n\tauto coeffs = find_recurrence_relation(terms, degree, order);\n\tauto extended_terms = extended(n, coeffs, terms);\n\tfor (int i = 0; i < (int) extended_terms.size(); ++i) {\n\t\tprintf(\"%d %d\\n\", i, extended_terms[i].v);\n\t}\n\tputs(\"\");\n}\nV get_extended_sequence(int n, const vector& terms, int degree, int order = -1) {\n\tauto coeffs = find_recurrence_relation(terms, degree, order);\n\treturn extended(n, coeffs, terms);\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\tInitFact(2000010);\n\n\tauto brute = [&](int N){\n\t\tmint res = 0;\n\t\trep(i_,N+1) rep(j_,N+1){\n\t\t\tint i = i_, j = j_; if(i>j) swap(i,j);\n\t\t\tif(j == 0){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(i == 0){\n\t\t\t\tres += mint(-1).pow(i+j) * Choose(N,j) * mint(3).pow(j + N*(N-j));\n\t\t\t}else{\n\t\t\t\tres += mint(-1).pow(i+j) * Choose(N,i) * Choose(N,j) * 3 * mint(3).pow((N-i)*(N-j));\n\t\t\t}\n\t\t}\n\t\treturn -res;\n\t};\n\tauto solve = [&](ll N){\n\t\tmint res = 0;\n\t\trep(i,N+1){\n\t\t\tif(i == 0){\n\t\t\t\trep1(j,N){\n\t\t\t\t\tres += mint(-1).pow(i+j) * Choose(N,j) * mint(3).pow(j + N*(N-j));\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tres += mint(-1).pow(i) * Choose(N,i) * (( mint(3).pow(N-i) - 1 ).pow(N) - mint(3).pow((N-i)*N)) * 3;\n\t\t\t\tres += mint(-1).pow(i) * Choose(N,i) * mint(3).pow(i + N*(N-i));\n\t\t\t}\n\t\t}\n\t\treturn -res;\n\t};\n\tint N; cin >> N;\n\tcout << solve(N) << endl;\n//\tshow(brute(N));\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "1c35e3639a1765497e8b7f0b829606ba", "src_uid": "6e4b0ee2e1406041a961582ead299a3a", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\n#define ll long long\n#define re register int\n#define For(i,a,b) for(ll i=(a); i<=(b); i++)\nconst int INF=1<<30;\nconst int mod=1e9+3;\nusing namespace std;\ntemplate \ninline void read(T &x) { \n x = 0;\n ll f = 1;\n char ch = getchar();\n while (!isdigit(ch)) {\n if (ch == '-') f = -1;\n ch = getchar();\n }\n while (isdigit(ch)) {\n x = x * 10 + (ch ^ 48);\n ch = getchar();\n }\n x *= f;\n return;\n}\ntemplate \ninline void write(T x){\n if(x < 0) {\n putchar('-');\n x = -x;\n }\n if(x > 9)\n write(x/10); \n putchar(x % 10 + '0');\n return;\n}\ninline ll qpow(ll a,ll b){\n ll x=1,y=a;\n while(b>=1){\n if(b%2==1) x=x*y%mod;\n y=y*y%mod;\n b/=2;\n }\n return x;\n}\n\n\nconst int N = 5;\n\nbool v[N][N][50];\nll val[N][N], best[N][N], dp[N][N][50];\n\nll solve(int l, int r, int n) {\n if (v[l][r][n]) return dp[l][r][n];\n\n v[l][r][n] = 1;\n ll& ans = dp[l][r][n];\n int x = 6 - l - r;\n\n if (n == 1) return ans = best[l][r];\n ans = solve(l, x, n-1) + solve(x, r, n-1) + val[l][r];\n ll k = solve(l, r, n-1) * 2 + solve(r, l, n-1) + val[l][x]+ val[x][r];\n ans = min(ans, k);\n return ans;\n}\n\nint main () {\n #ifndef ONLINE_JUDGE\n freopen(\"a.in\",\"r\",stdin);\n freopen(\"a.out\",\"w\",stdout);\n #endif\n\n memset(v, 0, sizeof(v));\n for (int i = 1; i <= 3; i++)\n for (int j = 1; j <= 3; j++) {\n cin >> val[i][j];\n best[i][j] = val[i][j];\n }\n\n for (int i = 1; i <= 3; i++) {\n for (int j = 1; j <= 3; j++) {\n if (i == j) continue;\n for (int k = 1; k <= 3; k++) if (k != i && k != j) {\n best[i][j] = min(best[i][j], best[i][k] + best[k][j]);\n }\n }\n }\n int n;\n scanf(\"%d\", &n);\n cout << solve(1, 3, n) << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "c92b16c48818afbbb9deef4abc49dd4d", "src_uid": "c4c20228624365e39299d0a6e8fe7095", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include \nusing namespace std;\nstring s;\nint flag;\nint main(){\n\tcin>>s;\n\tfor (int i=0;i<=s.length()-1;i++)\n\t\tif (s[i]=='h')\n\t\t\tfor (int j=i+1;j<=s.length()-1;j++)\n\t\t\t\tif (s[j]=='e')\n\t\t\t\t\tfor (int l=j+1;l<=s.length()-1;l++)\n\t\t\t\t\t\tif (s[l]=='l')\n\t\t\t\t\t\t\tfor (int k=l+1;k<=s.length()-1;k++)\n\t\t\t\t\t\t\t\tif (s[k]=='l')\n\t\t\t\t\t\t\t\t\tfor (int p=k+1;p<=s.length()-1;p++)\n\t\t\t\t\t\t\t\t\t\tif (s[p]=='o')\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tflag=1;\n\t\t\t\t\t\t\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\t\t\t\t\t\t\treturn 0;\n\t\t\t\t\t\t\t\t\t\t}\n\tif (flag==0)\n\t{\n\t\tcout<<\"NO\";\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "strings"], "code_uid": "200b78299d21f9e192eceb7d102cca11", "src_uid": "c5d19dc8f2478ee8d9cba8cc2e4cd838", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\n\nint main(){\n\n// freopen(\"input.txt\", \"r\", stdin);\n// freopen(\"output.txt\", \"w\", stdout);\n\nint x , y;\nbool flag = true;\ncin >> x >> y;\n\nwhile(1){\nif(flag){\n if(x >= 2){\n x-=2;\n if(y >= 2){\n y-=2;\n }\n else{\n cout << \"Hanako\" << endl;\n return 0;\n }\n }\n else\n if(x > 0){\n x--;\n if(y>=12){\n y-=12;\n }\n else{\n cout << \"Hanako\" << endl;\n return 0;\n }\n }\n else\n if(y>=22){\n y-=22;\n }\n else{\n cout << \"Hanako\" << endl;\n return 0;\n }\n\n}\nelse{\n\n if(y >= 22){\n y-=22;\n }\n else\n if(y >= 12){\n y-=12;\n if(x>=1){\n x--;\n }\n else{\n cout << \"Ciel\" << endl;\n return 0;\n }\n }\n else\n if(y>=2){\n y-=2;\n if(x >= 2){\n x-=2;\n }\n else{\n cout << \"Ciel\" << endl;\n return 0;\n }\n }\n else{\n cout << \"Ciel\" << endl;\n return 0;\n }\n\n}\n\n\nflag = !flag;\n}\n return 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "c8e65fad0f72f6501a6205ebee8cfe32", "src_uid": "8ffee18bbc4bb281027f91193002b7f5", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n int n,pos,l,r,ans;\n while(cin>>n>>pos>>l>>r)\n {\n ans=0;\n if(l==1&&r==n)\n ans=0;\n else if(pos<=l&&r==n)\n ans+=l-pos+1;\n else if(pos>l&&r==n)\n ans+=pos-l+1;\n else if(pos>=r&&l==1)\n ans+=pos-r+1;\n else if(pos\n#include \n#include \n\nusing namespace std;\n\nint checkString(string str)\n{\n if (str == \"January\") return 1;\n else if (str == \"February\") return 2;\n else if (str == \"March\") return 3;\n else if (str == \"April\") return 4;\n else if (str == \"May\") return 5;\n else if (str == \"June\") return 6;\n else if (str == \"July\") return 7;\n else if (str == \"August\") return 8;\n else if (str == \"September\") return 9;\n else if (str == \"October\") return 10;\n else if (str == \"November\") return 11;\n else if (str == \"December\") return 12;\n else return 13;\n}\n\nstring toString(int n)\n{\n switch(n)\n {\n case 1 : return \"January\";\n case 2 : return \"February\";\n case 3 : return \"March\";\n case 4 : return \"April\";\n case 5 : return \"May\";\n case 6 : return \"June\";\n case 7 : return \"July\";\n case 8 : return \"August\";\n case 9 : return \"September\";\n case 10 : return \"October\";\n case 11 : return \"November\";\n case 12 : return \"December\";\n }\n return \"x\";\n}\n\nint main()\n{\n string tmp;\n cin >> tmp;\n int now = checkString(tmp);\n int plus;\n cin >> plus;\n now = (now + plus) % 12;\n if (now == 0) now = 12;\n cout << toString(now) << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "4ad8187d1baa96dc8c2faf4c91a43b76", "src_uid": "a307b402b20554ce177a73db07170691", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\"bits/stdc++.h\"\nusing namespace std;\n/*--------------------------------#defines------------------------------------------*/\n#define PI 3.14159265\n#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)\n/*-------------------------------CODE START-----------------------------------------*/\npriority_queue< pair >pq;\nvector< pair >vp;\nmapmp,m;\nconst int N=1e5+6;\nint dp[N];\ntypedef long long int ll;\nvectorv,v1;\nstacks,s1;\nint visit[N];\nstruct domingo{\nint val,left,right;\n}o[N];\nlong long int n,BIT[N]={};\nint main()\n{\n\n\t/*\n\tstring s;\n\tcin>>s;\n\n\twhile(next_permutation(s.begin(),s.end()))\n\t{\n\t\tcout<>n>>a>>b;\n\tint c[6]={a, a, a, a, b, b};\n\tif(a*4+2*b<=n)\n\t\treturn cout<<1, 0;\n\t\tint mi=1e5+5;\n\t\tsort(c,c+6);\n\t\tif(c[0]==c[5])\n\t\t{\n\t\t\tint sum=0,ct=1;\n\t\tfor(int i=0;i<6;i++){\n\t\t\tif((sum+c[i])<=n)\n\t\t\t{\n\t\t\t\tsum+=c[i];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tct++;\n\t\t\t\tsum=c[i];\n\t\t\t}\n\t\t}\n\t\treturn cout<\nusing namespace std;\nint c[2];\nint main()\n{\n string a;\n cin>>a;\n int i,n=a.size();\n for(i=0;i='a'&&a[i]<='z')c[1]++;\n else c[0]++;\n }\n if(c[0]>c[1])\n\t{\n for(i=0;i\n\nusing namespace std;\n\nlong long C, hr, hb, wr, wb;\n\nlong long lcm(long long a, long long b) {\n for(int i = 1; ; ++i) {\n if (i%a == 0 && i%b == 0)\n return i;\n }\n}\n\nlong long bruteForce(long long C, long long hr, long long hb, long long wr, long long wb) {\n long long ans = 0;\n for(int i = 0; i <= C / wb; ++i) {\n long long j = (C - i * wb) / wr;\n long long sum = hb * i + hr * j;\n ans = max(ans, sum);\n }\n return ans;\n}\n\nint main() {\n cin >> C >> hr >> hb >> wr >> wb;\n\n if (wr > wb) {\n swap(hr, hb);\n swap(wr, wb);\n }\n\n if (wb >= 10) {\n cout << bruteForce(C, hr, hb, wr, wb) << endl;\n } else {\n long long d = lcm(wr, wb);\n long long ans = 0;\n for(int i = 0; i < wb; ++i) {\n for(int j = 0; j < wr; ++j) {\n long long sumH = hr * i + hb * j;\n long long sumW = wr * i + wb * j;\n if (sumW > C)\n continue;\n long long sumRemain = (C - sumW) / d * max(d/wr*hr, d/wb*hb);\n ans = max(ans, sumH + sumRemain);\n }\n }\n cout << ans << endl;\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "brute force"], "code_uid": "6398dc8c7fdc8e4c52898756dbdb0b33", "src_uid": "eb052ca12ca293479992680581452399", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// 519A \n// Author: REET\n\n#include \nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n char tmp;\n int sum1 = 0;\n int sum2 = 0;\n vector> v(8);\n for(int i=0; i<8; i++) {\n for(int j=0; j<8; j++) {\n cin >> tmp;\n v[i].push_back(tmp);\n }\n }\n for(int i=0; i<8; i++) {\n for(int j=0; j<8; j++) {\n switch (v[i][j]) {\n case 'Q':\n sum1 += 9;\n break;\n case 'R':\n sum1 += 5;\n break;\n case 'B':\n sum1 += 3;\n break;\n case 'N':\n sum1 += 3;\n break;\n case 'P':\n sum1 += 1;\n break;\n default:\n break;\n }\n switch (v[i][j]) {\n case 'q':\n sum2 += 9;\n break;\n case 'r':\n sum2 += 5;\n break;\n case 'b':\n sum2 += 3;\n break;\n case 'n':\n sum2 += 3;\n break;\n case 'p':\n sum2 += 1;\n break;\n default:\n break;\n }\n }\n }\n if(sum1 == sum2) {\n cout << \"Draw\" << endl;\n return 0;\n }\n sum1 > sum2 ? cout << \"White\" << endl : cout << \"Black\" << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5217d90759ed6fcd91863b5a553b78c3", "src_uid": "44bed0ca7a8fb42fb72c1584d39a4442", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nusing ll = long long;\n#define endl \"\\n\"\n#define all(v) v.begin(),v.end()\n#define mp make_pair\n#define mm(a) memset(a,true,sizeof(a))\n#define rep(i,a,b) for(ll i = a; i < b; i++)\n#define repn(i,a,b) for(ll i = a; i >= b; i--)\n#define pb push_back\n#define ff first\n#define ss second\n#define fto(k,m) for(auto k:m)\n#define in_edges(m) repp(i, 1, m){ ll a, b; cin >> a >> b; adj[a].pb(b), adj[b].pb(a);}\n#define debug(this) cerr<<\"> \"<<#this<<\" : \"<> t;\n\t while(t--)\n\t\t{\n\t\t\tll n; cin >>n;\n\t\t\tstring s; cin >>s;\n\t\t\tll sum = 0;\n\t\t\trep(i,0,n)sum+=s[i]-'0';\n\t\t\tdebug(sum);\n\t\t\tll flag=0,ans=0;\n\t\t\tll kum = 0;\n\t\t\trep(i,2,n+1)\n\t\t\t{\n\t\t\t\tif(sum%i==0)\n\t\t\t\t{\n\t\t\t\t\tdebug(i);\n\t\t\t\t\tll j =0;\n\t\t\t\t\tkum=0;\n\t\t\t\t\twhile(j\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nstring s;\nint n,a[1001];\nint main(){\n\tcin>>n>>s;\n\tfor(int i=0;i\nusing namespace std;\nint main()\n{\n\tlong long int n;\n\tcin>>n;\n\tlong long int s;\n\tcin>>s;\n\t\n\tlong long int res=0;\n\tif(n==1 )\n\t{\n\t\tif(s==1)\n\t\tres++;\n\t}\n\telse if(s==1)\n\t{\n\t\tres++;\n\t}\n\telse if(s<=n*n)\n\t{\n\tlong long int p=sqrt(s);\n if(s<=n)\n {\n \tres=res+2;\n\t }\n\tfor(long long int i=2;i<=p;i++)\n\t{\n\t\n\t\tif(s%i==0 )\n\t\t{\n\t\t\tif(i*i==s)\n\t\t\tres++;\n\t\t\telse if((s/i)<=n)\n\t\t\t{\n\t\t\t\tres=res+2;\n\t\t\t}\n\t\t}\n\t}\n}\n\tcout<\n#include\nusing namespace std;\n int a[1000005];\nint main()\n{ \n int n,cnt=0,i,j,k;\n while(~scanf(\"%d\",&n)) \n {\n cnt=0;\n for(i=0;i<=n;i++)\n a[i]=i*i; \n for(i=2;i<=n;i++)\n {\n for(j=i-1,k=1;k\nusing namespace std;\nint a[4],b[4],c[4],d[4];\nint main()\n{\n\tfor(int i = 0; i < 4; i++)\n\t{\n\t\tcin >> a[i] >> b[i] >> c[i] >> d[i];\n\t}\n\tfor(int i = 0; i < 4; i++)\n\t{\n\t\t\n\t\tif((a[i]==1 && d[(i+3)%4]==1) || (a[i]==1 && d[i]==1)) {\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t} \n\t\tif((b[i]==1 && d[(i+2)%4]==1) || (b[i]==1 && d[i]==1)) {\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t} \n\t\tif((c[i]==1 && d[(i+1)%4]==1) || (c[i]==1 && d[i]==1)) {\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t} \n\t\t\n\t}\n\tcout << \"NO\";\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9cd23421b9a1f3ce9b8dfb41d3913bfd", "src_uid": "44fdf71d56bef949ec83f00d17c29127", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \nusing namespace std;\nint counts = 0;\nint main(){\n int n;\n cin >> n;\n int rest = n;\n int z = 1;\n int count = 0;\n while (rest >0){\n while (rest>=z)\n {\n z *= 2;\n }\n rest -= z /2;\n count++;\n z = 1;\n }\n cout << count;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["bitmasks"], "code_uid": "4009161e5d5115eb876fb9c4554f60cc", "src_uid": "03e4482d53a059134676f431be4c16d2", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ll long long\n#define mp make_pair\n#define pb push_back\n#define PI 3.1415926535897932384626433832\n#define MOD 1000000007\nusing namespace std;\nstring to(int a )\n{\n string s =\"\";\n while(a!=0)\n {\n s+=char(a%10+'0');\n a/=10;\n }\n string t = \"\";\n for(int i = s.size()-1; i>=0; i--)\n {\n t+=s[i];\n }\n return t;\n\n}\n/*\n\n for(int i = 0;i b;\nvector > a;\n\nvoid solve(){\n a[1][0]+=1;\nfor(int i = 1;i<=10;i++){\n bool is = true;\n for(int j = 0;jpow(2,i-1)){\n int k = a[i][j]-pow(2,i-1);\n a[i][j]=pow(2,i-1);\n\n a[i+1][j]+=k;\n a[i+1][j+1]+=k;\n is = false;\n }\n\n }\n if(is)return;\n\n\n}\n\n\n}\n\nint main(){\n\n ll n,t;\n cin>>n>>t;\na.push_back(b);\n\nfor(int i = 0;i<=11;i++){\n b.push_back(0);\n a.push_back(b);\n\n}\nfor(int i = 0;i\n#include\n\nusing namespace std;\n\nvoid solu(vectorn, int m) {\n\tfor (int i = 0; i < m ; i++) {\n\t\tif (n[i] + 15 < n[i + 1]) {\n\t\t\tcout << n[i] + 15;\n\t\t\treturn;\n\t\t}\n\t\t\n\t}\n\tcout << 90 << endl;\n}\n\nint main()\n{\n\tint size, n,a,b;\n\tcin >> size;\n\tvectorv;\n\tv.push_back(0);\n\tfor (int i = 0; i > n;\n\t\tv.push_back(n);\n\t}\n\tv.push_back(90);\n\n\tsolu(v, size+1);\n\n\t\n\t\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "4f08bcb5b3075411ec147b16dcb00581", "src_uid": "5031b15e220f0ff6cc1dd3731ecdbf27", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define ll long long\nusing namespace std;\ninline ll read()\n{\n\tll x=0,f=1;char ch=getchar();\n\twhile(ch<'0'||ch>'9')if(ch=='-')f=-1,ch=getchar();\n\twhile(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();\n\treturn x*f;\n}\nconst ll mod=1000000007;\nint main()\n{\n\tll a=read(),b=read();\n\tll val=((b*(b-1))/2)%mod,ans=0;\n\tfor(ll i=1;i<=a;++i){ll tmp=(1LL+1LL*i*b)%mod;ans=(ans+(val*tmp)%mod)%mod;}\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "d5ae454e48e301a0867ded524dd0562b", "src_uid": "cd351d1190a92d094b2d929bf1e5c44f", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \nusing namespace std;\n\nint k, r, ans, tmp;\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n //freopen(\"input.txt\" , \"r\", stdin );\n //freopen(\"output.txt\", \"w\", stdout);\n cin >> k >> r;\n for (tmp = k , ans = 1; tmp%10!=r && tmp%10!=0; tmp += k, ans++);\n cout << ans << '\\n';\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "math", "constructive algorithms", "implementation"], "code_uid": "162761cd23929056aec12fb7e55605d4", "src_uid": "18cd1cd809df4744bb7bcd7cad94e2d3", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\nstring s,s2=\"A\",s3=\"Q\";\nint k,ans,a,b,q1[101],q2[101];\ninline int read(){\n int f=1,x=0;char ch;\n do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');\n do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');\n return f*x;\n}\nint main()\n{\n cin>>s;\n int n=s.size();\n if (s[0]=='Q') q1[0]=1;\n for (int i=1;i=0;i--)\n\t{\n\t\tif (s[i]=='Q') q2[i]=q2[i+1]+1;\n\t\telse q2[i]=q2[i+1];\n\t}\n for (int i=0;i\n#include \n\nint main(){\n int n,m;\n scanf(\"%d%d\",&n,&m);\n if(n==0&&m !=0){\n printf(\"Impossible\\n\");\n return 0;\n }\n int maxn = n+m-1;\n if(m == 0) maxn++;\n int minn= 0;\n if(m <= n) minn = n;\n else minn = m;\n printf(\"%d %d\\n\",minn,maxn);\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "39783cb57971c93a12d45637fc846140", "src_uid": "1e865eda33afe09302bda9077d613763", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\n\nconst int maxn = 2e2 + 10;\nconst int INF = 0x3f3f3f3f;\nconst int eps = 1e-8;\n\nstruct node {\n\tint id;\n\tint s;\n\tint d;\n\tint c;\n}a[maxn];\nint n, m;\nint ans[maxn];\n\nint cmp(node x, node y)\n{\n\treturn x.d < y.d;\n}\n\nint main()\n{\n\twhile (cin >> n >> m)\n\t{\n\t\tmemset(ans, 0, sizeof(ans));\n\t\tint flag = 1;\n\t\tfor (int i = 0; i < m; ++i)\n\t\t{\n\t\t\tcin >> a[i].s >> a[i].d >> a[i].c;\n\t\t\ta[i].id = i + 1;\n\t\t\tif (ans[a[i].d])\n\t\t\t{\n\t\t\t\tflag = 0;\n\t\t\t}\n\t\t\tans[a[i].d] = m + 1;\n\t\t}\n\t\tif (!flag)\n\t\t{\n\t\t\tprintf(\"-1\\n\");\n\t\t\tcontinue;\n\t\t}\n\t\t/*for (int i = 1; i <= n; ++i)\n\t\t{\n\t\t\tif (i != 1) printf(\" \");\n\t\t\tprintf(\"%d\", ans[i]);\n\t\t}*/\n\t\t//printf(\"\\n\");\n\t\tsort(a, a + m, cmp);\n\t\tflag = 1;\n\t\tfor (int i = 1; i <= n; ++i)\n\t\t{\n\t\t\tif (ans[i]) continue;\n\t\t\tint j;\n\t\t\tfor (j = 0; j < m; ++j)\n\t\t\t{\n\t\t\t\tif (a[j].s <= i&&a[j].d > i)\n\t\t\t\t{\n\t\t\t\t\t//cout << (a[j].s < i) << endl;\n\t\t\t\t\tif (a[j].c)\n\t\t\t\t\t{\n\t\t\t\t\t\ta[j].c--;\n\t\t\t\t\t\tans[i] = a[j].id;\n\t\t\t\t\t\t//cout << a[j].s << \" \" << i << \" \" << a[j].id << endl;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < m; ++i)\n\t\t{\n\t\t\tif (a[i].c)\n\t\t\t{\n\t\t\t\tflag = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!flag)\n\t\t{\n\t\t\tprintf(\"-1\\n\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 1; i <= n; ++i)\n\t\t\t{\n\t\t\t\tif (i!=1) printf(\" \");\n\t\t\t\tprintf(\"%d\", ans[i]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\t\t}\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation", "greedy"], "code_uid": "24ff5dc68df949b21abd31541bee22d2", "src_uid": "02d8d403eb60ae77756ff96f71b662d3", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n\nusing namespace std;\n\nint remainders[100001]={0};\n\nint main()\n{\n int A, M, N = 0;\n bool existe = false;\n cin >> A >> M;\n remainders[N] = (A % M);\n N++;\n while( ((A % M) != 0) && !existe)\n {\n A = A + (A % M);\n existe = false;\n for(int i = 0 ; i < N ; ++i)\n {\n if(remainders[i] == (A%M))\n {\n existe = true;\n break;\n }\n }\n if(!existe)\n {\n remainders[N] = (A % M);\n N++;\n }\n }\n if(existe)\n {\n printf(\"No\");\n }\n else\n {\n printf(\"Yes\");\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "matrices", "implementation"], "code_uid": "80ed24fef46e037462039c5444e8fd15", "src_uid": "f726133018e2149ec57e113860ec498a", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": " #include\nusing namespace std;\nint a[100];\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.in\", \"r\", stdin);\n\t//freopen(\"out.out\", \"w\", stdout);\n#endif\n\t int n;\n\t scanf(\"%d\",&n);\n\t for (int i=0;i\n#include \n\nusing namespace std;\n\nint main()\n{\n int med[4];\n int cup[4];\n int n;\n scanf(\"%d %d %d\", &med[0], &med[1], &med[2]);\n\n scanf(\"%d %d %d\", &cup[0], &cup[1], &cup[2]);\n\n scanf(\"%d\", &n);\n\n med[3]=med[0]+med[1]+med[2];\n cup[3]=cup[0]+cup[1]+cup[2];\n\n int m=med[3]/5;\n if(med[3]%5!=0) m++;\n\n n=n-m;\n\n m=cup[3]/10;\n if(cup[3]%10!=0)m++;\n\n n=n-m;\n\n if(n<0)cout<<\"NO\";\n else cout<<\"YES\";\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "0b19494a409b071971282168935be920", "src_uid": "fe6301816dea7d9cea1c3a06a7d1ea7e", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \nusing namespace std;\n \ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector VI;\ntypedef vector< vector > VVI;\ntypedef pair PII;\ntypedef vector VPII;\n \n#define REP(i, n) for(int i = 0; i < n; ++i)\n#define RREP(i, n) for(int i = n - 1; i >= 0; --i)\n#define FOR(i, x, y) for(ll i = x; i <= y; ++i)\n#define RFOR(i, x, y) for(int i = x; i >= y; --i)\n#define SZ(a) (ll)(a).size()\n#define ALL(a) (a).begin(),(a).end()\n#define SORT(a) sort(ALL(a)) \n#define CLEAR(x) memset(x,0,sizeof x);\n#define COPY(FROM, TO) memcpy(TO, FROM, sizeof TO);\n#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())\n#define pb push_back\n#define sqr(x) (x)*(x)\n#define X first\n#define Y second\n#define y1 Y1\n#define y2 Y2\nconst long double pi=acos(-1.0);\nconst long double eps = 1e-9;\nconst int MAXN = 1010111;\nconst int INF = 1000000000;\nconst ll MOD = 1000000003;\n\nint n, m, mx, mn;\nint a[100000];\n\nint main()\n{ \n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n#endif\n cin >> n >> m >> mn >> mx;\n bool f = true;\n REP(i, m) \n {\n cin >> a[i];\n }\n sort(a, a + m);\n if (a[0] < mn) f = false;\n if (mx < a[m - 1]) f = false;\n int d = (mn < a[0]) + (a[m - 1] < mx);\n if (d > n - m) f = false;\n cout << (f ? \"Correct\" : \"Incorrect\") << endl;\n} ", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "572ce6e93a923065bd2474599a95512d", "src_uid": "99f9cdc85010bd89434f39b78f15b65e", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nstring s[3];\nint cntX,cnt0,cntd;\n\nint main(){\n\tfor (int i=0;i<3;i++){\n\t cin>>s[i];\n\t for (int j=0;j<3;j++){\n\t \tif (s[i][j]=='X') cntX++;\n\t \tif (s[i][j]=='0') cnt0++;\n\t \tif (s[i][j]=='.') cntd++;\n\t\t}\n\t}\n\tint bX=(s[0][0]=='X'&&s[0][1]=='X'&&s[0][2]=='X'||\n\t\ts[1][0]=='X'&&s[1][1]=='X'&&s[1][2]=='X'||\n\t\ts[2][0]=='X'&&s[2][1]=='X'&&s[2][2]=='X'||\n\t\ts[0][0]=='X'&&s[1][0]=='X'&&s[2][0]=='X'||\n\t\ts[0][1]=='X'&&s[1][1]=='X'&&s[2][1]=='X'||\n\t\ts[0][2]=='X'&&s[1][2]=='X'&&s[2][2]=='X'||\n\t\ts[0][0]=='X'&&s[1][1]=='X'&&s[2][2]=='X'||\n\t\ts[0][2]=='X'&&s[1][1]=='X'&&s[2][0]=='X');\n\n\tint b0=(s[0][0]=='0'&&s[0][1]=='0'&&s[0][2]=='0'||\n\t\ts[1][0]=='0'&&s[1][1]=='0'&&s[1][2]=='0'||\n\t\ts[2][0]=='0'&&s[2][1]=='0'&&s[2][2]=='0'||\n\t\ts[0][0]=='0'&&s[1][0]=='0'&&s[2][0]=='0'||\n\t\ts[0][1]=='0'&&s[1][1]=='0'&&s[2][1]=='0'||\n\t\ts[0][2]=='0'&&s[1][2]=='0'&&s[2][2]=='0'||\n\t\ts[0][0]=='0'&&s[1][1]=='0'&&s[2][2]=='0'||\n\t\ts[0][2]=='0'&&s[1][1]=='0'&&s[2][0]=='0');\n\tif (bX&&b0 || cntX-cnt0>=2 || cntXcnt0){\n\t\tcout<<\"illegal\"<0&&cntX==cnt0){\n\t\tcout<<\"first\"<0&&cntX-cnt0==1){\n\t\tcout<<\"second\"< \n#include \n#include \n#include \n#include \n#include \nusing namespace std; \nint main() \n{ \n#ifdef _DEBUG \nfreopen(\"input.txt\",\"rt\",stdin); \nfreopen(\"output.txt\",\"wt\",stdout); \n#endif \n\tint n;\n\tcin>>n;\n\tvector A(n,0);\n\tvector B(n,0);\n\tfor(int i=0;i>A[i];\n\tint k1=-1;\n\tint k2=-1;\n\tfor(int i=0;i\n\nint main(void){\n\tint n,m;\n\tint sum;\n\tscanf(\"%d%d\",&n,&m);\n\tif (n%2==0)\n\t\tsum=m*n/2;\n\telse if(m%2==0)\n\t\tsum=n*m/2;\n\telse \n\t\tsum=n*(m-1)/2+(n+1)/2-1;\n\tprintf(\"%d\",sum);\t\n\treturn 0;\n}\n\t\n\n\t \t \t\t \t \t\t\t \t \t\t\t\t\t\t \t \t", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "648c141b8bd2fb9ba8c064b565998f6a", "src_uid": "e840e7bfe83764bee6186fcf92a1b5cd", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define rep(i,l,r) for (int i=l; i<=r; i++)\n#define drep(i,r,l) for (int i=r; i>=l; i--)\ntypedef long long ll;\nconst int N = 3000000, INF = 0x7fffffff, limit = 100;\nint C[N], L, R, p, tot, dp[N], f[N], ans, P[30], len;\nbool flag[108];\nvoid dfs(ll cur, int last)\n{\n\tif (cur > R) return;\n\tC[++tot] = cur;\n\trep(i, last, len) dfs(cur * P[i], i); \n}\nint main()\n{\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n scanf(\"%d%d%d\", &L, &R, &p);\n rep(i, 2, limit)\n if (!flag[i])\n {\n \tP[++len] = i;\n for (int j = i * i; j <= limit; j += i) flag[j] = 1;\n }\n dfs(1, 1); \n sort(C + 1, C + tot + 1);\n dp[1] = 0, f[1] = 2;\n rep(i, 2, tot) dp[i] = INF, f[i] = INF;\n rep(Max, 2, p)\n {\n \tint i = 1, j;\n \tfor (j = 1; j <= tot; j++)\n \tif (dp[j] < INF)\n \t{\n \t if ((ll)C[j] * Max > R) break;\n while (C[i] < C[j] * Max) i++;\n dp[i] = min(dp[i], dp[j] + 1);\n f[i] = min(f[i], dp[i] + Max);\n }\n }\n rep(i, 1, tot) \n if (C[i] >= L && f[i] <= p) ans++;\n printf(\"%d\\n\", ans);\n\t//fclose(stdin); fclose(stdout);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "dp", "brute force"], "code_uid": "0b22f742ca6911ef1031e6ba5905c18d", "src_uid": "6d898638531e4713774bbd5d47e827bf", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\nconst __int64 INF = 1000000007;\nconst __int64 N = 1005;\n__int64 n, k, i, j, t, su, f1[N], c1[N][N], d1[N][N], dp1[3][3][N][N], f[N], c[N][N], d[N][N], dp[3][3][N][N];\nstruct coord\n{\n int x, y, g;\n};\nvoid checker();\nint main()\n{\n scanf(\"%I64d%I64d\", &n, &k);\n checker();\n return 0;\n}\nvoid checker()\n{\n //\n c1[0][0] = 1;\n for(i = 1; i <= n; ++ i)\n {\n c1[i][0] = 1;\n for(j = 1; j <= n; ++ j)\n {\n c1[i][j] = (c1[i-1][j]+c1[i-1][j-1])%INF;\n }\n }\n f1[0] = 1;\n for(i = 1; i <= n; ++ i)\n {\n f1[i] = (f1[i-1]*i)%INF;\n }\n dp1[0][0][1][0] = 1;\n d1[1][0] = 1;\n //\n dp1[0][0][2][0] = 1;\n dp1[0][1][2][1] = 1;\n dp1[1][0][2][1] = 1;\n dp1[1][1][2][2] = 1;\n for(i = 2; i <= n; ++ i)\n {\n if(i%2==0)\n {\n// d1[i][i] = 1;\n dp1[1][1][i][i] = 1;\n }\n for(j = i-1; j >= 0; -- j)\n {\n// d[i][j];\n if(i!=2)\n {\n dp1[0][0][i][j] = (dp1[0][1][i-1][j]+dp1[0][0][i-1][j]+dp1[1][0][i-1][j]+dp1[1][1][i-1][j])%INF;\n if(j >= 2)\n dp1[1][1][i][j] = dp1[0][0][i-1][j-2];\n if(j >= 1)\n {\n dp1[1][0][i][j] = (dp1[0][0][i-1][j-1]+dp1[1][0][i-1][j-1])%INF;\n dp1[0][1][i][j] = (dp1[0][0][i-1][j-1]+dp1[0][1][i-1][j-1])%INF;\n }\n }\n su = (dp1[0][0][i][j]+dp1[0][1][i][j]+dp1[1][0][i][j]+dp1[1][1][i][j])%INF;\n/* d1[i][j] = (f1[i-j]*su)%INF;\n for(t = j+1; t <= i; ++ t)\n {\n d1[i][j] = (d1[i][j]-c1[t][j]*d1[i][t])%INF;\n }*/\n }\n }\n i = n;\n if(i%2==0)\n {\n d1[i][i] = 1;\n }\n for(j = i-1; j >= 0; -- j)\n {\n// d[i][j];\n su = (dp1[0][0][i][j]+dp1[0][1][i][j]+dp1[1][0][i][j]+dp1[1][1][i][j])%INF;\n d1[i][j] = (f1[i-j]*su)%INF;\n for(t = j+1; t <= i; ++ t)\n {\n d1[i][j] = (d1[i][j]-c1[t][j]*d1[i][t])%INF;\n }\n }\n printf(\"%I64d\\n\", (d1[n][k]+INF)%INF);\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "ade719f07bda9b8b9b0f3a5b8f6fe2b5", "src_uid": "1243e98fe2ebd6e6d1de851984b96079", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "//bitw\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n int a,b,c;\n cin>>a>>b>>c;\n cout<\nint n;\nchar s[101];\nint main(){\n\tscanf(\"%d\", &n);\n\tscanf(\"%s\", s);\n\tif (n == 1 && s[0] == '0'){\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\tint cnt = 0;\n\tfor (int i = 0; i < n; i++)if (s[i] == '1')cnt++;\n\tprintf(\"1\");\n\tfor (int i = 0; i < n - cnt; i++)printf(\"0\");\n\tprintf(\"\\n\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "c466b6e2f6bee108488d1b4229026478", "src_uid": "ac244791f8b648d672ed3de32ce0074d", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ntypedef long long LL;\n\nLL n, K, l, r, mid;\n\nLL calc(LL len, LL x) {\n\tif (len <= 2*x) return 0;\n\tLL cnt[2] = {1, 0}, a[2] = {len, len-1};\n\tLL res = 0, tmp;\n\twhile (a[0] >= 2*x+1) {\n\t\tres += cnt[0]; res += (a[1] >= 2*x+1) * cnt[1];\n\t\ttmp = a[0] / 2;\n\t\tif (a[0]&1) cnt[0] = cnt[0] * 2 + cnt[1];\n\t\telse cnt[1] = cnt[1] * 2 + cnt[0];\n\t\ta[0] = tmp; a[1] = tmp - 1;\n\t}\n\treturn res;\n}\n\nLL solve(LL l, LL r, LL limit, LL K) {\n\tLL mid = (l + r) >> 1, tmp, tmp2;\n\tif (K == 1) return mid;\n\ttmp = calc(mid-l, limit);\n\ttmp2 = calc(r-mid, limit + 1);\n\tif (K <= tmp + tmp2 + 1) return solve(l, mid-1, limit, K-tmp2-1);\n\treturn solve(mid+1, r, limit, K-tmp-1);\n}\n\nint main() {\n\tscanf(\"%I64d%I64d\", &n, &K);\n\tif (K == 1) return puts(\"1\"), 0;\n\tif (K == 2) return printf(\"%I64d\\n\", n), 0;\n\tn -= 2; K -= 2;\n\tl = 0; r = (n + 1) / 2;\n\twhile (l < r) {\n\t\tmid = (l + r + 1) >> 1;\n\t\tif (calc(n, mid) >= K) l = mid;\n\t\telse r = mid - 1;\n\t}\n\tprintf(\"%I64d\\n\", solve(1, n, l, K) + 1);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "binary search", "implementation"], "code_uid": "e2f198e0f2378f946fe9c471fc19bd7d", "src_uid": "eb311bde6a0e3244d92fafbd4aa1e61f", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//Jay Shree Krishna\n#include\"bits/stdc++.h\"\n#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)\n#define ll long long int\n#define inf 1e18\n#define sr string\n#define mod 1000000007\n#define start int t;cin>>t;while(t--)\n#define db(x) cout<<#x<<\"=\"<>\n#define vi vector\n#define si set\n#define mlsi multiset\n#define vc vector\n#define vs vector\n#define vli vector\n#define vpii vector>\n#define pii pair\n#define mii map\n#define msi map\n#define mci map\n#define mpiii map,int>\n#define um unordered_map\n#define umii unordered_map\n#define umsi unordered_map\n#define umci unordered_map\n#define mkp make_pair\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(),x.end()\n#define sortv(x) sort(x.begin(),x.end())\n#define sorta(x,n) sort(x,x+n)\n#define sorted(x) is_sorted(x.begin(),x.end())\n#define print_all(x) for(auto t:x) cout<>x\n#define in2(x,y) cin>>x>>y\n#define in3(x,y,z) cin>>x>>y>>z\n#define in4(x,y,z,v) cin>>x>>y>>z>>v\n#define oo(x) cout< bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }\ntemplate bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }\n \nvoid read(vli &a,int n){\n a.resize(n);\n forr(i,n) cin>>a[i];\n}\n\n\nbool fine=0;\nvli a;\nll n,m;\nvoid solve(){\n \n in2(n,m);\n vector> a(m);\n forr(i,m){\n cin>>a[i].ff>>a[i].ss;\n swap(a[i].ff,a[i].ss);\n }\n sortv(a);\n reverse(all(a));\n ll ans=0;\n forr(i,m){\n if(n>=a[i].ss){\n ans+=a[i].ff*a[i].ss;\n n-=a[i].ss;\n }\n else{\n ans+=n*a[i].ff;\n oo(ans);\n rr;\n }\n }\n oo(ans);\n rr;\n\n\n\n\n\n\n}\n\nint main(){\n #ifndef ONLINE_JUDGE \n freopen(\"input1.txt\", \"r\", stdin); \n freopen(\"error.txt\", \"w\", stderr); \n freopen(\"output1.txt\", \"w\", stdout); \n #endif\n fast; \n if(fine){\n start{\n solve();\n }\n }\n else solve();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation", "greedy"], "code_uid": "95ae72b5c752f2ac1e37df9852e794ac", "src_uid": "c052d85e402691b05e494b5283d62679", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n\nlong long int min(long long int a, long long int b) {\n\treturn a < b ? a : b;\n}\n\nlong long int nCr(long long int n, long long int k) { \n long long C[k+1];\n memset(C, 0, sizeof(C));\n \n C[0] = 1; // nC0 is 1\n \n for (long long i = 1; i <= n; i++)\n {\n // Compute next row of pascal triangle using\n // the previous row\n for (long long j = min(i, k); j > 0; j--)\n C[j] = C[j] + C[j-1];\n }\n return C[k];\n}\n\nint main(void) {\n\tlong long int n, m, t;\n\tscanf(\"%lld%lld%lld\", &n, &m, &t);\n\tlong long int sum = 0;\n\tlong long int z = min(m, t-4);\n\tfor(long long int i = 1; i <= z; i++) {\n\t\tsum += nCr(m, i)*nCr(n, t-i);\n\t}\n\tprintf(\"%lld\", sum);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "5b65f2dbd8fd2b729b250352761a1c43", "src_uid": "489e69c7a2fba5fac34e89d7388ed4b8", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nstring s;\nint n,k,a[100009],ans;\nint main(){\n cin>>n>>k>>s;\n if(s[0]!='1' || s[n-1]!='1'){\n cout<<-1;\n return 0;\n }\n for(int i=0;i=n-1)cout<\n#define ll long long\n#define loop(i,n) for(ll i=0;i>ss;\n\tstring s=\"\";\n\ts=s+ss[0]+ss[1];\n\tll a=stoi(s);\n\ts=\"\";\n\ts=s+ss[3]+ss[4];\n\tll b=stoi(s);\n\tll sum=0;\n\tll mod1=60;\n\tll mod2=24;\n\twhile(!isPal(a,b)){\n\t\tsum++;\n\t\tb++;\n\t\ta+=b/60;\n\t\ta=a%24;\n\t\tb=b%60;\n\t}\n\tcout<\nusing namespace std;\nlong long int n;\nlong long int e;\nlong long int ans = 0;\nvoid p(long long int x)\n{\n\tfor(long long int i=3;i*i<=x;i++)\n\t{\n\t\tif(x%i==0) \n\t\t{\n\t\t\tans=i;\n\t\t\treturn;\n\t\t}\n\t}\n\tans=x;\n\treturn;\n}\nint main()\n{\n\tcin>>n;\n\tif(n%2==0) cout<\n#include\nusing namespace std; \nint main(){\nint a[4];\nfor(int i=0 ;i<4 ;i++){\n\tcin>>a[i];\n}\nsort(a,a+4);\nif(a[0]==a[3]){\n\tcout<<3;\n}\nelse if(a[0]==a[2]){\n\tcout<<2;\n}\nelse if(a[0]==a[1] ){\n\tif(a[2]==a[3]){\n\t\tcout<<2;\n\t}\n\telse{\n\t\tcout<<1;\n\t}\n}\nelse if(a[1]==a[3]){\n\tcout<<2;\n}\nelse if(a[1]==a[2]){\n\tcout<<1;\n}\nelse if(a[2]==a[3]){\n\tcout<<1;\n}\nelse {\n\tcout<<0;\n}\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "ad7bacd728b1c8ae777c0ce23675cc09", "src_uid": "38c4864937e57b35d3cce272f655e20f", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nstring tostring(int i){\n string s =\"\";\n while(i>0){\n s+= i%10 + '0';\n i/=10;\n }\n reverse(s.begin(),s.end());\n return s;\n}\nmain()\n{\n int a, b, j=0;\n string a1=\"\",a2 = \"\", b1 = \"\";\n cin >> a >> b;\n a++;\n while(1){\n a1 = tostring(a); b1=tostring(b);\n j = 0; a2 = \"\";\n for(int i=0; i\nusing namespace std;\nint a[15], b[15];\nint main(void)\n{\n\tint n, m, x;\n\tcin>> n >> m;\n\tfor (int i = 0; i> x;\n\t\ta[x] = 1;\n\t}\n\tfor (int i = 0; i> x;\n\t\tb[x] = 1;\n\t}\n\tint ans;\n\tfor (int i = 1; i<100; i++)\n\t{\n\t\tif (i>10)\n\t\t{\n\t\t\tint t1 = i / 10, t2 = i % 10;\n\t\t\tif ((a[t1] && b[t2]) || (a[t2] && b[t1]))\n\t\t\t{\n\t\t\t\tans = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (a[i] && b[i])\n\t\t\t{\n\t\t\t\tans = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tcout<< ans;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "58fe7e14506b3f3416ce68253e4d38de", "src_uid": "3a0c1b6d710fd8f0b6daf420255d76ee", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define M 1000000007\n#define pb push_back\n#define mp make_pair\n#define ff first\n#define ss second\n#define ll long long \n#define mem(x) memset(x,0,sizeof(x))\n#define setbits(x) __builtin_popcount(x)\n#define ull unsigned long long int\n#define rep(i,a,b) for(int i=a;i vi;\ntypedef vector< vi > vvi;\ntypedef pair< int,int > ii;\ntypedef pair li;\ninline void fast()\n{\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n}\nconst int N=(int)4010;\nint dp0[N][N],dps[N][N];\nint C[N][N];\n// c[n][k]=c[n-1][k-1]+c[n-1][k];\nconst int mod=998244853;\nvoid ncr(ll n)\n{\n\tfor(int i=0;i<=n;i++)\n\t{ \n\t\tfor(int j=0;j<=i;j++)\n\t\t{\n if(j==0||j==i)\n {\n \tC[i][j]=1;\n }\n else\n {\n \tC[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;\n \tC[i][j]=C[i][j]%mod;\n }\n\t\t}\n\t}\n}\nint main()\n{\n\tfast();\n\tint n,m;\n\tcin>>n>>m;\n\tll mx=max(n,m);\n\tncr(2*mx+2LL);\n\t\n\tfor(int i=0;i<=n;i++)\n\t{\n for(int j=i;j<=m;j++)\n { if(i==0)\n \tdp0[i][j]=1;\n \telse\n \tdp0[i][j]=dp0[i][j-1]+dp0[i-1][j];\n \tdp0[i][j]%=mod;\n }\n\t}\n\tfor(int i=0;i<=n;i++)\n\t{\n\t\tfor(int j=0;j<=m;j++)\n\t\t{ if(i==0)\n\t\t\tdps[i][j]=0;\n\t\t\telse if(j==0)\n\t\t\t\tdps[i][j]=i;\n\n\t\t\telse if(i>0&&j>0)\n\t\t\t{int a=(dps[i-1][j]+C[i+j-1][i-1])%mod;\n\t\t\tint b=(dps[i][j-1]+((mod-((mod+(C[i+j-1][j-1]-dp0[i][j-1])%mod)%mod))%mod))%mod;\n\t\t\tdps[i][j]=(a+b)%mod;\n\t\t\tdps[i][j]%=mod;\n\t\t }\n\t\t}\n\t}\n\tcout<\n#include \n\nusing namespace std;\n\nint S ( long long n );\n\nint main()\n{\n // freopen(\"in.txt\",\"r\",stdin);\n long long w, m, k;\n cin >> w >> m >> k;\n w /= k;\n\n int sm = S(m);\n long long ans = 0;\n long long cost = w;\n long long next10Pow = 1;\n for (int i = 0; i < sm; ++i) {\n next10Pow *= 10;\n }\n while (cost - (next10Pow - m) * sm >= 0) {\n cost -= (next10Pow - m) * sm;\n ans += next10Pow - m;\n m = next10Pow;\n ++sm;\n next10Pow *= 10;\n }\n\n ans += cost / sm;\n cout << ans << endl;\n\n return 0;\n}\n\nint S ( long long m )\n{\n int ans = 0;\n while (m) {\n ++ans;\n m /= 10;\n }\n return ans;\n}", "lang_cluster": "C++", "tags": ["math", "implementation", "binary search"], "code_uid": "bb6bfad586942693adacf025e6dcd8bc", "src_uid": "8ef8a09e33d38a2d7f023316bc38b6ea", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n#define ll long long\nll mod = 998244353;\n\nll binpow(ll a, ll p) {\n return p ? binpow(a * a % mod, p >> 1)* (p & 1 ? a : 1) % mod : 1;\n}\nll bininv(ll a) { return binpow(a, mod - 2); }\nset solved;\nmap > mp;\nvector > C;\nint k, kk;\nll m, invm;\nll fact[35000];\nll ifact[35000];\n\nstruct FFT {\n int n;\n vector w;\n vector inv;\n\n void set(int _n = (1 << 14)) {\n n = _n;\n inv.resize(n);\n inv[0] = 0;\n for (int i = 1, j = 0; i < n; i++) {\n int bit = (n >> 1);\n while (bit & j) {\n j ^= bit;\n bit >>= 1;\n }\n j ^= bit;\n inv[i] = j;\n }\n\n w.resize(n);\n ll step = binpow(3, (mod - 1) / n);\n w[0] = 1;\n for (int i = 1; i < n; i++) {\n w[i] = w[i - 1] * step % mod;\n }\n }\n\n void fft(vector& a) {\n for (int i = 0; i < n; i++) {\n if (i < inv[i]) {\n swap(a[i], a[inv[i]]);\n }\n }\n\n for (int len = 2; len <= n; len <<= 1) {\n for (int i = 0; i < n; i += len) {\n int i1 = i, i2 = i + (len >> 1);\n int wind = 0, wstep = (n / len);\n for (; i2 < i + len; i1++, i2++, wind += wstep) {\n ll x = a[i1], y = a[i2] * w[wind] % mod;\n a[i1] = (x + y) % mod;\n a[i2] = (x - y + mod) % mod;\n }\n }\n }\n }\n\n void ifft(vector& a) {\n fft(a);\n reverse(a.begin() + 1, a.end());\n ll invn = binpow(n, mod - 2);\n for (int i = 0; i < n; i++) {\n a[i] = (a[i] * invn) % mod;\n }\n }\n\n void do_calculation(vector a, vector b, vector& to) {\n fft(a);\n fft(b);\n for (int i = 0; i < n; i++) {\n to[i] = a[i] * b[i] % mod;\n }\n ifft(to);\n for (int i = k + 1; i < n; i++) {\n to[i] = 0;\n }\n }\n}fft;\n\nvoid prec() {\n fact[0] = ifact[0] = 1;\n for (int i = 1; i < 35000; i++) {\n fact[i] = fact[i - 1] * i % mod;\n ifact[i] = binpow(fact[i], mod - 2);\n }\n}\n\nvoid calc(ll n) {\n\n // (a+b)^k = SUM_0^k C_k^i a^i b^{k-i}\n if (solved.count(n))\n return;\n if (n == 1) {\n vector tmp(k + 1, invm);\n tmp[0] = 1;\n for (int i = 0; i < k + 1; i++) {\n tmp[i] = tmp[i] * ifact[i] % mod;\n }\n while (tmp.size() < kk) {\n tmp.push_back(0);\n }\n mp[1] = tmp;\n }\n else if (n & 1) {\n calc(n - 1);\n mp[n] = vector(kk, 0);\n fft.do_calculation(mp[1], mp[n - 1], mp[n]);\n mp[n][0] = 1;\n //for (int ii = 1; ii <= k; ++ii) {\n // for (int i = 0; i <= ii; ++i) {\n // (mp[n][ii] += C[ii][i] * mp[n - 1][i] % mod * mp[1][ii - i] % mod) %= mod;\n // }\n //}\n }\n else {\n calc(n / 2);\n mp[n] = vector(kk, 0);\n fft.do_calculation(mp[n / 2], mp[n / 2], mp[n]);\n mp[n][0] = 1;\n //for (int ii = 1; ii <= k; ++ii) {\n // for (int i = 0; i <= ii; ++i) {\n // (mp[n][ii] += C[ii][i] * mp[n / 2][i] % mod * mp[n / 2][ii - i] % mod) %= mod;\n // }\n //}\n }\n //do\n solved.insert(n);\n}\nvoid solve(int test_ind) {\n int st = clock();\n prec();\n ll n;\n cin >> n >> m >> k;\n kk = 1;\n while (kk < k) {\n kk <<= 1;\n }\n kk <<= 1;\n fft.set(kk);\n \n\n invm = bininv(m);\n calc(n);\n cout << mp[n][k] * fact[k] % mod << endl;\n}\n\nint main() {\n int t = 1;\n //cin >> t;\n for (int i = 0; i < t; i++) {\n solve(i);\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "probabilities", "number theory"], "code_uid": "5a27b5a286bd2f94451546a8576edd26", "src_uid": "e6b3e559b5fd4e05adf9f1cd1b22126b", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "Clang++17 Diagnostics", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main() {\n int n,k;cin>>n>>k;\n while(k--){\n if(n%10==0) n=n/10;\n else n-=1;\n \n }\n cout<\n#include \nusing namespace std;\nint main()\n{\n\tstring s;\n\tcin>>s;\n\tint dem=0;\n\tfor (int i=0;i<(s.size());i++)\n\t{\n\t\tif (s[i]=='a')\n\t\t\tdem++;\n\t}\n if (dem==0)\n cout<<0<(n/2))\n cout<(float)(n/2))\n cout<\n#include \nusing namespace std;\nint p[100];\nint t[100];\nint main(){\n\tint n,c,templ(0),tt(0),tempr(0);\n\tcin>>n>>c;\n\t\n\tfor(int i=0;i>p[i];}\n\t\tfor(int i=0;i>t[i];\n\tfor(int i=0;i=0;i--){\n\t\ttt+=t[i];\n\t\ttempr+=max(0,p[i]-c*tt);\n\t}\n\tif(templ>tempr) cout<<\"Limak\";\n\telse if(templ\n#include \n#include \n#include \n\nusing namespace std;\n\nconst int N = 3 * 1e8 + 10;\n\nbitset prime;\n\n\nint main() {\n\t\n\tint l, r;\n\n\twhile(~scanf(\"%d%d\", &l, &r)) {\n\n\t\tprime.set();\n\t\tint e = sqrt(r) + 1;\n\n\t\tfor (int i = 3; i <= e; i += 2)\n\t\t\tif (prime[i]) \n\t\t\t\tfor (int j = i * i, s = i * 2; j <= r; j += s) {\n\t\t\t\t\tprime[j] = false;\t\n\t\t\t\t}\n\n\t\tint cnt = (l <= 2 && 2 <= r);\n\t\tfor (int i = 5; i <= r; i += 4)\n\t\t\tif (i >= l && prime[i]) \n\t\t\t\tcnt ++;\n\n\n\t\tprintf(\"%d\\n\", cnt);\n\t\n\t}\n\n\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "5eb608450f8dd4511327eb6962b04666", "src_uid": "55a83526c10126ac3a6e6e5154b120d0", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\nThere's nothing to me now. An empty shell unfolded.\nHow, when we learn to pray inside our demons are laughing\nHow long will this go on? Are we a bit much stronger?\nDo you think you can save me from living this way?\n\nI don't know how to love. I just know how to live.\nAll I feel is hate. Will you forgive me?\n\nFor all those things I've done, they keep on creeping by me.\nAnd though we've changed our ways,\nstill all our demons are laughing.\nHow long will this go on? Aren't we a bit much stronger?\nI'd like to think you've came into my life to stay.\n\nI don't know how to love. I just know how to live.\nAll I feel is hate. Will you forgive me?\nI don't know how to breathe with you too far away.\nDon't know how to love. Will you forgive me?\nNo I can't live this way!\nI don't know how to love. I just know how to live.\nAll I feel is pain. Will you forgive me?\nI don't know how to breathe with you too far away.\nI don't know how to love.\n\nPast lives I've lived. Uncontrolled but sacred.\nYou've finally seen all that's left of me.\nSo hard to feel. So hard to breathe.\nWill you forgive me?\n*/\n\n#pragma GCC optimize(\"O3\")\n#define _CRT_SECURE_NO_WARNINGS\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n\n#define y0 sdkfaslhagaklsldk\n\n#define y1 aasdfasdfasdf\n#define yn askfhwqriuperikldjk\n#define j1 assdgsdgasghsf\n#define tm sdfjahlfasfh\n#define lr asgasgash\n#define norm asdfasdgasdgsd\n#define have adsgagshdshfhds\n#define ends asdgahhfdsfshdshfd\n#define prev asdgSHJsfgsdfhdsh\n#define hash asdgasdgasdgdfrywewery\n\n#define eps 1e-12\n#define M_PI 3.141592653589793\n#define bsize 300\n\n#define ldouble long double\nusing namespace std;\n\n#define bs 1000000007\n\nconst int N = 210031;\n\nint ar[N];\n\nint main(){\n// freopen(\"apache.in\",\"r\",stdin);\n// freopen(\"apache.out\",\"w\",stdout);\n// freopen(\"input.txt\", \"r\", stdin);\n// freopen(\"output.txt\", \"w\", stdout);\n ios_base::sync_with_stdio(0);\n //cin.tie(0);\n\n for (int i=0;i<4;i++){\n \tcin>>ar[i];\n }\n\n if (ar[0]!=ar[3]||ar[2]>0&&ar[0]==0)\n \tcout<<\"0\"<\r\nusing namespace std;\r\ntypedef long long ll;\r\nconst int mod=998244353;\r\nint n;\r\nll ans,m,d=1,tmp=1;\r\ninline ll qp(ll base,int p) {\r\n\tll res=1;\r\n\twhile(p) {\r\n\t\tif(p&1) res=res*base%mod;\r\n\t\tp>>=1;base=base*base%mod;\r\n\t}\r\n\treturn res;\r\n}\r\ninline ll Inv(ll base) {return qp(base%mod,mod-2);}\r\ninline ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}\r\nint main() {\r\n\tcin>>n>>m;\r\n\tif(m==1) {cout<m) break;\r\n\t\ttmp=1LL*(m/d)%mod*tmp%mod;\r\n\t\tans-=tmp;\r\n\t\tans%=mod;\r\n\t}\r\n\tcout<<(ans+mod)%mod;\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "number theory"], "code_uid": "2db27c0fd528e847316be780b46404c9", "src_uid": "0fdd91ed33431848614075ebe9d2ee68", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \nusing namespace std;\n\n/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */\n\nint main(int argc, char** argv) {\n\n\nint a=0,b=0,z,i,m,cc=0;\n\ncin>>i>>m>>z;\n\na=i;\nb=m;\n\nwhile (a<=z)\n{\n\twhile(b<=a && b<=z)\n\t{\n\t\tif(a==b)\n\t\tcc++;\n\t\tb+=m;\n\t}\n\ta+=i;\n}\ncout << cc;\n\n\n\t\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "9b3c22c7ed6bf3b76bdc5e16bd19bd93", "src_uid": "e7ad55ce26fc8610639323af1de36c2d", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#include \n#include \n#include \n#include \n#define ll long long\nusing namespace std;\n\nvector p;\nbool used[50];\nint n;\nll m;\n\nll pow(int x)\n{\n ll ans = 1;\n for (int i = 0; i < x; i++)\n ans *= 2;\n return ans;\n}\n\nvoid go(ll ost)\n{\n int last = 0;\n for (int i = 0; i < n; i++)\n {\n for (int j = last + 1; j <= n; j++)\n {\n if (ost > pow(n - j - 1))\n ost -= pow(n - j - 1);\n else\n {\n last = j;\n used[j] = true;\n p[i] = j;\n break;\n }\n }\n }\n bool flag = false;\n for (int i = 0; i < n; i++)\n {\n if (!p[i])\n flag = true;\n if (flag)\n {\n for (int j = n; j >= 1; j--)\n {\n if (!used[j])\n {\n used[j] = true;\n p[i] = j;\n break;\n }\n }\n }\n }\n for (int i = 0; i < n; i++)\n {\n printf(\"%d \", p[i]);\n }\n}\n\nint main()\n{\n //freopen(\"xxx.in\", \"r\", stdin);\n //freopen(\"xxx.out\", \"w\", stdout);\n cin >> n >> m;\n p.resize(n);\n \n go((ll)m);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "divide and conquer", "bitmasks"], "code_uid": "9723dc08e4a8a50e6aea29e030319c86", "src_uid": "a8da7cbd9ddaec8e0468c6cce884e7a2", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nint p, i, j, P[78777];\n\nint C(long long x){\n for (j = 0; ;j++){\n long long y = P[j];\n if ((y*y) > x) return 1;\n if (!(x%y)) return 0;\n }\n}\n\ninline int F(int i, long long a){\n if (a == 1) return 1;\n long long y = P[i];\n if ((y + 1) > a) return 0;\n if (((y*y) + 1) > a) return C(--a);\n\n int res = F(i + 1, a);\n for (; ;){\n if (++y > a) break;\n if (!(a%y)) res += F(i + 1, a/y);\n y--;\n y *= P[i];\n }\n return res;\n}\n\nint main(){\n P[p++] = 2;\n for (i=3; i<1000010; i+=2){\n if (C(i)) P[p++] = i;\n }\n\n long long a;\n scanf(\"%I64d\",&a);\n printf(\"%d\\n\", ((a < 5)?(a != 2):F(0, a)));\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "dp", "data structures", "number theory"], "code_uid": "abb5b04d4f6f8d371fdfb19ae46d8013", "src_uid": "1f68bd6f8b40e45a5bd360b03a264ef4", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint i,j,n,k;\nmain()\n{\n cin>>n;\n for(i=1;i<=n;i++)\n{\nj=n-i;\nif(j%i==0) k++;\n}\ncout<\n#include\n#include\n#include\n#include\nusing namespace std;\n#define SYN\t\t\t\t\tios_base::sync_with_stdio(0);cin.tie(0);\n#define FORab(i,a,b)\t\tfor(int i=int(a);i<=int(b);i++)\n#define FOR(i,n)\t\t\tFORab(i,0,(n)-1)\n#define FOR1(i,n)\t\t\tFORab(i,1,n)\n#define FORN(i,n)\t\t\tfor(int i=int(n)-1;i>=0;i--)\n#define FORN1(i,n)\t\t\tfor(int i=int(n);i>=1;i--)\n#define wh(n,m)\t\t\t\twhile(int(n)int(m))\n#define o(X)\t\t\t\tcout<>X)\n#define cii(X,Y)\t\t\t(cin>>X>>Y)\n#define ciii(X,Y,Z)\t\t\t(cin>>X>>Y>>Z)\ntypedef long long\t\t\tLL;\ntypedef unsigned long long\tLLU;\ntypedef unsigned long\t\tLU;\ntypedef long long int\t\tLLI;\ntypedef int\t\t\t\t\tI;\n\nint main()\n{\n\tSYN;\n\tfor(int i=0,mt;ci(mt);++i)\n\t{\n\t\t\n\t\tif (mt)\n\t\t{\n\t\t\too(abs(i % 5 - 2) + abs(i / 5 - 2),endl);\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9a7ea534b64c33c9dab51669d3d0a7b2", "src_uid": "8ba7cedc3f6ae478a0bb3f902440c8e9", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint main()\n{\n\t\tlong long a, b;\n\tcin >> a >> b;\n\tlong long i = 0;\n\twhile (a != b)\n\t{\n\t\tlong long temp = a;\n\t\tif (a > b)\n\t\t{\n\t\t\t\n\t\t\ti += a / b;\n\t\t\tif (a%b == 0)\n\t\t\t\tbreak;\n\t\t\ta = a%b;\n\t\t}\n\t\telse\n\t\t{\n\t\t\ti += b / a;\n\t\t\tif (b%a == 0)\n\t\t\t\tbreak;\n\t\t\ta = b%a;\n\t\t\tb = temp;\n\t\t}\n\t}\n\tcout << i;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "3df50185f6327f57cdfbb9831eca37f1", "src_uid": "ce698a0eb3f5b82de58feb177ce43b83", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n int main()\n {\n int n,m,b;\n cin>>n>>m;\n int a[n+1]={0};\n for(int i=0;i>b;\n int y=b;\n while(y<=n)\n {\n if(a[y]!=0)break;\n else a[y]=b;\n y++;\n }\n }\n for(int i=1;i<=n;i++)cout<\n#include\n#include\nusing namespace std;\ntypedef long long LL;\ntypedef pair pii;\n#define mp make_pair\n#define pb push_back\n#define f first\n#define s second\n\n\n//var \nstring s1,s2;\nint ar[105][2],n, dp[105][4], ans = 0; //mxcount ending in state\n\nbool vals(int g, int s){\n\tif(s == 1)\n\t\treturn (!ar[g][0] && !ar[g][1] && !ar[g+1][0]);\n\tif(s == 2) \n\t\treturn (!ar[g][0] && !ar[g+1][1] && !ar[g+1][0]);\n\tif(s == 3)\n\t\treturn (!ar[g+1][0] && !ar[g][1] && !ar[g+1][1]);\n\tif(s == 4)\n\t\treturn (!ar[g][0] && !ar[g][1] && !ar[g+1][1]);\n\treturn 0;\n}\n\nint main(){\n\tios_base::sync_with_stdio(0); \n\tcin.tie(0);\n\n\tcin >> s1 >> s2; n = s1.size();\n\tfor(int i = 0; i\n#include //ceil需要\nusing namespace std;\nint main()\n{\n int n,m,a;\n cin>>n>>m>>a;\n cout<<(long long)(ceil((double)n/a)*ceil((double)m/a));\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "8eb0aa74d5430948842ad655799780ea", "src_uid": "ef971874d8c4da37581336284b688517", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \r\n#include \r\n#include \r\n#define N 410\r\nusing namespace std;\r\ntypedef long long ll;\r\nint dp[N][N];\r\nint n,mod;\r\nvoid add(int &x,int y){\r\n x=(x+y)%mod;\r\n}\r\nint main()\r\n{\r\n scanf(\"%d%d\",&n,&mod);\r\n dp[1][1]=1;\r\n for(int i=1;i<=n;i++){\r\n for(int j=1;j<=i;j++){\r\n add(dp[i+1][j],1ll*dp[i][j]*j*2%mod);\r\n add(dp[i+2][j+1],1ll*dp[i][j]*(j+1)%mod);\r\n }\r\n }\r\n int ans=0;\r\n for(int i=1;i<=n;i++){\r\n add(ans,dp[n][i]);\r\n }\r\n printf(\"%d\",ans);\r\n return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "b2624aaec83f2670079ee083d17acbe1", "src_uid": "4f0e0d1deef0761a46b64de3eb98e774", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\nusing namespace std;\nlong long n,a,b;\nint main()\n{\n\tcin>>n>>a>>b;\n\tcout<\n#include \n#include \nusing namespace std;\n\nint main() {\n\t//freopen(\"in.txt\",\"r\",stdin);\n\tlong long n;\n\tlong long m;\n\tlong long a,b;\t\n\twhile(scanf(\"%lld %lld %lld %lld\",&n,&m,&a,&b)!=EOF) {\t\n\n\t\tif(n==0)\n\t\t\tprintf(\"0\\n\");\n\t\tif(n%m == 0) {\n\t\t\tprintf(\"0\\n\");\n\t\t}\n\t\telse {\t\t\n\t\t\tlong long mod = n%m;\n\t\t\tlong long div = n/m;\t\t\t\n\t\t\tlong long build = (m-mod)*a;\n\t\t\tlong long destroy = mod*b;\n\t\t\tif(build\nusing namespace std;\n#define ll long long\ninline ll read()\n{\n ll x=0,f=1;char ch=getchar();\n for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;\n for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';\n return x*f;\n}\ninline void write(ll x)\n{\n if(x<0)putchar('-'),x=-x;\n if(x>9)write(x/10);\n putchar(x%10+'0');\n}\ninline void writeln(ll x)\n{\n write(x);\n puts(\"\");\n}\nconst int N=21;\nll k,t,dep;\nll c[N][N],f[N][N];\nint a[23],num[N];\nll dp(int x){\n for(int i=0;i<=16;i++)\n f[0][i]=1;\n for(int i=1;i<=x;i++)\n for(int j=1;j<=16;j++){\n f[i][j]=0;\n for(int k=0;k<=min(i,a[j]);k++)\n f[i][j]+=f[i-k][j-1]*c[i][k];\n }\n return f[x][16];\n}\nvoid prt(int x){\n if(x<10)putchar(x+'0');\n else putchar(x-10+'a');\n}\nvoid dfs(int k,int x)\n{\n if(x==0)return;\n for(int i=(x==dep)?2:1;i<=16;i++){\n if(a[i]==0)continue;\n a[i]--;\n ll size=dp(x-1);\n if(size>=k){\n prt(i-1);\n dfs(k,x-1);\n break;\n }\n k=k-size;\n a[i]++;\n }\n}\nint main(){\n k=read();t=read();\n for(int i=1;i<=16;i++)\n a[i]=t;\n c[1][0]=c[1][1]=1;\n for(int i=2;i<=10;i++){\n c[i][0]=1;\n for(int j=1;j<=i;j++)\n c[i][j]=c[i-1][j-1]+c[i-1][j];\n }\n dep=1;\n while(1){\n a[2]--;\n ll size=dp(dep-1)*15;\n a[2]++;\n if(size>=k)break;\n dep++,k-=size;\n }\n dfs(k,dep);\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "combinatorics"], "code_uid": "5f30abc9d2057efbf92c0588fe1e458d", "src_uid": "cbfc354cfa392cd021d9fe899a745f0e", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n\nusing namespace std;\n\nint main() {\n int a1, a2, a3, a4;\n cin >> a1 >> a2 >> a3 >> a4;\n\n int sum = a1 + a2 + a3 + a4;\n if ((sum % 2) > 0) {\n cout << \"NO\\n\";\n return 0;\n }\n int half = sum / 2;\n if (a1 == half || a2 == half || a3 == half || a4 == half ||\n (a1 + a2 == half) || (a1 + a3 == half) || (a1 + a4 == half)) {\n cout << \"YES\\n\";\n } else {\n cout << \"NO\\n\";\n }\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "a8409b6d2fd419cd93b4f339b1fb71b6", "src_uid": "5a623c49cf7effacfb58bc82f8eaff37", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n\nmain()\n{\n int n=0,b=0;\n string s;\n\n cin>>s;\n\n for(int i=0;s[i]!='\\0';i++)\n {\n if(s[i]=='-')\n n++;\n else\n b++;\n }\n if(b==0||n%b==0)\n cout<<\"YES\";\n else\n cout<<\"NO\";\n }\n\n\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "f9635eb549b6d51bc26f1604ab596cb0", "src_uid": "6e006ae3df3bcd24755358a5f584ec03", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include\n#include\n \n#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)\n#define AfterDecimal(n) cout << fixed << setprecision(n);\n#define ll long long int\n#define ld long double\n#define all(a) a.begin(), a.end()\n#define pii pair\n#define pll pair\n#define bits(x) __builtin_popcount(x)\n\nusing namespace __gnu_pbds;\nusing namespace std;\n \n// #pragma GCC optimize(\"Ofast\")\n// #pragma GCC target(\"avx,avx2,fma\")\n// #pragma GCC optimization (\"unroll-loops\") \n \n//template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n \ntypedef tree, rb_tree_tag, \n tree_order_statistics_node_update> \n ordered_set; \n \n#ifdef LOCAL\n#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)\ntemplate\nvoid __f(const char* name, Arg1&& arg1){\n cout << \"# \"<< name << \" = \" << arg1 << '\\n';\n}\ntemplate\nvoid __f(const char* names, Arg1 && arg1, Args &&... args){\n const char* comma = strchr(names + 1 ,',');\n cout << \"# \";\n cout.write(names, comma - names)<< \" = \" << arg1 << \" | \";\n __f(comma + 1, args...);\n}\n#else\n#define debug(...) 36\n#endif\n\n \nstd::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\ninline int ri(int a, int b) {\n return uniform_int_distribution(a, b)(rng);\n}\n \nll power(ll x, ll y, ll p){ \n ll res = 1;\n x = x % p; \n while (y > 0) {\n if (y & 1) \n res = (res * x) % p; \n y = y >> 1;\n x = (x * x) % p; \n }\n return res;\n}\n \nconst ll INF = (ll)(8e18);\nconst ll MOD = (ll)(1e9 + 7);\nconst ll HMOD = (ll)4222234727;\nconst ll Hashprime = (ll)31;\n \nconst int maxn = 100100;\n\nstd::vector Brute(std::vector v, ll n){\n std::vector tv;\n for(ll i = 0; i < (ll)(1LL << n); i++){\n ll tval = 0;\n for(ll j = 0; j < n; j++){\n if((1LL << j) & i){\n tval += v[j];\n }\n }\n tv.push_back(tval);\n }\n sort(all(tv));\n return tv;\n}\n\nbool checkInOwn(std::vector vtot, ll m){\n map mp;\n for(auto i : vtot){\n if(mp.find(i - m) != mp.end()) return true;\n mp[i] = true;\n } \n return false;\n}\n\nbool solve(){\n ll w,m; cin >> w >> m;\n std::vector v;\n ll val = 1;\n while(val <= 3 * MOD){\n v.push_back(val);\n val = val * w;\n }\n ll n = (ll)v.size();\n if(w > 3){\n std::vector vtot = Brute(v, n);\n return checkInOwn(vtot, m);\n }\n return true; \n}\n\nint32_t main(){\n IOS;\n #ifndef ONLINE_JUDGE\n // freopen(\"input.txt\", \"r\", stdin);\n // freopen(\"output.txt\", \"w\", stdout);\n #endif\n int t = 1; // cin >> t;\n while(t--){\n cout << (solve() ? \"YES\\n\" : \"NO\\n\");\n }\n #ifdef LOCAL\n cerr << \"Time elapsed: \" << 1.0 * clock() / CLOCKS_PER_SEC << \" s.\\n\";\n #endif\n}\n//g++ -DLOCAL -std=c++17 -Wshadow -Wall -o \"c\" \"comp.cpp\" -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -g", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "meet-in-the-middle", "greedy", "number theory"], "code_uid": "47a64df8f1c819696ab596271ed68355", "src_uid": "a74adcf0314692f8ac95f54d165d9582", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\n\nOcean's apart day after day\nAnd I slowly go insane\nI hear your voice on the line\nBut it doesn't stop the pain\n\nIf I see you next to never\nHow can we say forever\n\nWherever you go\nWhatever you do\nI will be right here waiting for you\nWhatever it takes\nOr how my heart breaks\nI will be right here waiting for you\n\nI took for granted, all the times\nThat I thought would last somehow\nI hear the laughter, I taste the tears\nBut I can't get near you now\n\nOh, can't you see it baby\nYou've got me going crazy\n\nI wonder how we can survive\nThis romance\nBut in the end if I'm with you\nI'll take the chance\n\nOh, can't you see it baby\nYou've got me going crazy\n\n*/\n\n#include \nusing namespace std;\n\nint main()\n{\n int i,j,col=0,row=0,col1,row1;\n char cb[8][8];\n for(i=0;i<8;i++) {\n for(j=0;j<8;j++) {\n cin>>cb[i][j];\n }\n }\n for(i=0;i<8;i++) {\n if(cb[i][0]=='B') {\n col1=0;\n for(j=0;j<8;j++) {\n if(cb[i][j]=='B') col1++;\n }\n if(col1==8) col++;\n }\n }\n for(i=0;i<8;i++) {\n if(cb[0][i]=='B') {\n row1=0;\n for(j=0;j<8;j++) {\n if(cb[j][i]=='B') row1++;\n }\n if(row1==8) row++;\n }\n }\n if(col==8 && row==8) {\n cout<<8<<'\\n';\n return 0;\n }\n cout<\nusing namespace std;\n#define ll long long\nint main()\n{\n\tll x, y;\n\tcin>>x>>y;\n\tif(x>0 && y>0)\n\t{\n\t\tcout<<0<<\" \"<0)\n\t{\n\t\tcout<\n#define pb push_back\n#define sz(x) x.size()\n#define F first\n#define S second\n#define mp make_pair\n#define ll long long\n#define sqr(x) ((x)*(x))\n#define inf 1e18\nusing namespace std;\n ll a[1000010], b[1000], d[100010][26], p[10010];\n ll mn=inf, mx=-inf, ans, cnt, sm=1, n, m;\n bool ok, okk, u[1000010], uu[310][310], uk[1000010];\n char ck[510][510], ch[100010];\n int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};\n vector g[900010];\n \nint main(){\n \n //freopen (\"input.txt\",\"r\",stdin); freopen (\"output.txt\",\"w\",stdout);\n \n \n cin>>n;\n string s;\n cin>>s;\n int mid=sz(s)/2;\n for(int i=0;i=0;i--){\n \t\t\tif(a[i]==0){\n \t\t\t\tcnt=i;\n \t\t\t\tbreak;\n \t\t\t}\n \t\t}\n \t\tcnt=max(cnt, 0*1LL);\n \t\ts[mid]=char(cnt+'a');\n \t\ta[cnt]=1;\n \t}\n }\n mid--;\n for(int i=mid;i>=0;i--){\n \tif(s[i]=='?' && s[sz(s)-i-1]=='?'){\n \t\tfor(int j=n-1;j>=0;j--){\n \t\t\tif(a[j]==0){\n \t\t\t\ts[i]=char(j+'a');\n \t\t\t\ts[sz(s)-i-1]=char(j+'a');\n \t\t\t\ta[j]=1;\n \t\t\t\tbreak;\n \t\t\t}\t\n \t\t}\n \t}\n }\n for(int i=0;i<=mid;i++){\n \tif(s[i]=='?'){\n \t\ts[i]=s[sz(s)-i-1];\n \t}\n \tif(s[sz(s)-i-1]=='?'){\n \t\ts[sz(s)-i-1]=s[i];\n \t}\n }\n //cout<=n){\n \t\tcout<<\"IMPOSSIBLE\";\n \t\treturn 0;\n \t}\n }\n for(int i=0;i\nusing namespace std;\n#define ll long long\n\nll cal(ll n, ll r, ll d) {\n\tif (!r) return n / d;\n\tif (n < r) return 0;\n\treturn (n - r) / d + 1;\n}\n\nint main() {\n ll n, m, i, k, ret = 0;\n scanf(\"%lld %lld\", &n, &m);\n for (i = 0; i < m; i++)\n \tfor (k = 0; k < m; k++)\n \t\tif ((i * i + k * k) % m == 0)\n \t\t\tret += cal(n, i, m) * cal(n, k, m);\n printf(\"%lld\", ret);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "14fa345c9546b34e5d467ba304e9eadf", "src_uid": "2ec9e7cddc634d7830575e14363a4657", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nint qwq, n, m, s, ans = 1;\nint main(void) {\n scanf(\"%d %d\", &n, &m);\n for (int i = 1; i <= n; ++i) {\n std::cin >> qwq;\n if (s + qwq > m) {\n ++ans;\n s = 0 + 1 - 1;\n }\n s += qwq;\n }\n printf(\"%d\\n\\r\", ans);\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "1b0a91b36144dcf47675f2ab28423da4", "src_uid": "5c73d6e3770dff034d210cdd572ccf0f", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main(){\n int i,j,n,tmp,ans=0;\n cin>>n;\n int arr[105];\n for(i=0;i>arr[i];\n }\n for(i=0;i<=\n n;i++){\n tmp=0;\n for(j=0;j\nusing namespace std;\nint main(){\n int a,b,ans=0;\n cin>>a>>b;\n while(a<=b){\n a*=3;\n b*=2;\n ans++;\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long ll;\ntypedef pair P;\ntypedef pair PL;\nconst ll mod = 1000000007;\nconst ll MOD = 1000000007;\nconst ll INF = 1LL << 60;\n#define PI (acos(-1))\n#define ALL(c) (c).begin(), (c).end()\n\ntemplate \nistream &operator>>(istream &is, vector &vec)\n{\n for (auto &v : vec)\n is >> v;\n return is;\n}\ntemplate \nostream &operator<<(ostream &os, const vector &vec)\n{\n os << \"[\";\n for (auto v : vec)\n os << v << \",\";\n os << \"]\";\n return os;\n}\ntemplate \nostream &operator<<(ostream &os, const deque &vec)\n{\n os << \"deq[\";\n for (auto v : vec)\n os << v << \",\";\n os << \"]\";\n return os;\n}\ntemplate \nostream &operator<<(ostream &os, const set &vec)\n{\n os << \"{\";\n for (auto v : vec)\n os << v << \",\";\n os << \"}\";\n return os;\n}\ntemplate \nostream &operator<<(ostream &os, const multiset &vec)\n{\n os << \"{\";\n for (auto v : vec)\n os << v << \",\";\n os << \"}\";\n return os;\n}\ntemplate \nostream &operator<<(ostream &os, const pair &pa)\n{\n os << \"(\" << pa.first << \",\" << pa.second << \")\";\n return os;\n}\ntemplate \nostream &operator<<(ostream &os, const map &mp)\n{\n os << \"{\";\n for (auto v : mp)\n os << v.first << \"=>\" << v.second << \",\";\n os << \"}\";\n return os;\n}\n\n#define dbg(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \") \" << __FILE__ << endl;\ntemplate \n\nll lcm(ll a, ll b)\n{\n return a / __gcd(a, b) * b;\n}\n\nbool is_prime(ll x)\n{\n if (x == 1)\n {\n return false;\n }\n for (ll i = 2; i * i <= x; i++)\n {\n if (x % i == 0)\n return false;\n }\n return true;\n}\n\nvector divisor(ll n)\n{\n vector ret;\n for (ll i = 1; i * i <= n; i++)\n {\n if (n % i == 0)\n {\n ret.push_back(i);\n if (i * i != n)\n ret.push_back(n / i);\n }\n }\n sort(begin(ret), end(ret));\n return (ret);\n}\n\nlong long modpow(long long a, long long n, long long mod)\n{\n long long res = 1;\n while (n > 0)\n {\n if (n & 1)\n res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n}\n\n\nlong long modinv(long long a, long long mod)\n{\n return modpow(a, mod - 2, mod);\n}\n\nconst ll MAX = 510000;\nll fac[MAX], finv[MAX], inv[MAX];\n\nvoid COMinit()\n{\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (ll i = 2; i < MAX; i++)\n {\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n\nll COM(ll n, ll k)\n{\n if (n < k)\n return 0;\n if (n < 0 || k < 0)\n return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nstruct UnionFind\n{\n vector par;\n\n UnionFind(int n) : par(n, -1) {}\n void init(int n) { par.assign(n, -1); }\n\n int root(int x)\n {\n if (par[x] < 0)\n return x;\n else\n return par[x] = root(par[x]);\n }\n\n bool issame(int x, int y)\n {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y)\n {\n x = root(x);\n y = root(y);\n if (x == y)\n return false;\n if (par[x] > par[y])\n swap(x, y); // merge technique\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n\n int size(int x)\n {\n return -par[root(x)];\n }\n};\n\nstruct BIT\n{\n int n;\n vector bit;\n BIT()\n {\n init();\n }\n BIT(int n) : n(n)\n {\n init();\n }\n void init()\n {\n bit.clear();\n bit.resize(n + 1, 0);\n }\n int sum(int i)\n {\n int s = 0;\n while (i > 0)\n {\n s += bit[i];\n i -= i & -i;\n }\n return s;\n }\n int sum(int x, int y)\n {\n return sum(y) - sum(x - 1);\n }\n void add(int i, int x)\n {\n while (i <= n)\n {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lower_bound(int w)\n {\n if (w <= 0)\n return 0;\n int x = 0, r = 1;\n while (r < n)\n r <<= 1;\n for (int k = r; k > 0; k >>= 1)\n {\n if (x + k <= n && bit[x + k] < w)\n {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\n\n\nstruct LazySegmentTree\n{\n // private:\n ll n;\n vector node, lazy;\n\n // public:\n LazySegmentTree(vector v)\n {\n int sz = (int)v.size();\n n = 1;\n while (n < sz)\n n *= 2;\n node.resize(2 * n - 1);\n lazy.resize(2 * n - 1, 0);\n\n for (int i = 0; i < sz; i++)\n node[i + n - 1] = v[i];\n for (int i = n - 2; i >= 0; i--)\n node[i] = node[i * 2 + 1] + node[i * 2 + 2];\n }\n\n void eval(int k, int l, int r)\n {\n if (lazy[k] != 0)\n {\n node[k] += lazy[k];\n if (r - l > 1)\n {\n lazy[2 * k + 1] += lazy[k] / 2;\n lazy[2 * k + 2] += lazy[k] / 2;\n }\n lazy[k] = 0;\n }\n }\n\n void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1)\n {\n if (r < 0)\n r = n;\n eval(k, l, r);\n if (b <= l || r <= a)\n return;\n if (a <= l && r <= b)\n {\n lazy[k] += (r - l) * x;\n eval(k, l, r);\n }\n else\n {\n add(a, b, x, 2 * k + 1, l, (l + r) / 2);\n add(a, b, x, 2 * k + 2, (l + r) / 2, r);\n node[k] = node[2 * k + 1] + node[2 * k + 2];\n }\n }\n\n ll getsum(int a, int b, int k = 0, int l = 0, int r = -1)\n {\n if (r < 0)\n r = n;\n eval(k, l, r);\n if (b <= l || r <= a)\n return 0;\n if (a <= l && r <= b)\n return node[k];\n ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);\n ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);\n return vl + vr;\n }\n};\n\nll digit_sum(ll v)\n{\n ll ret = 0;\n while (v)\n {\n ret += (v % 10);\n v /= 10;\n }\n return ret;\n}\n\ntemplate \nstruct Kruskal\n{\n\n struct edge\n {\n ll from, to;\n T cost;\n ll used;\n edge() {}\n edge(ll from, ll to, T cost) : from(from), to(to), cost(cost), used(0) {}\n bool operator<(const edge &e) const\n {\n return cost < e.cost;\n }\n };\n\n ll n;\n vector p, r;\n vector edges;\n\n Kruskal() {}\n Kruskal(ll n) : n(n) {}\n\n void init(ll n)\n {\n r.assign(n, 1);\n p.resize(n);\n iota(p.begin(), p.end(), 0);\n }\n\n ll find(ll x)\n {\n return (x == p[x] ? x : p[x] = find(p[x]));\n }\n\n bool same(ll x, ll y)\n {\n return find(x) == find(y);\n }\n\n void unite(ll x, ll y)\n {\n x = find(x);\n y = find(y);\n if (x == y)\n return;\n if (r[x] < r[y])\n swap(x, y);\n r[x] += r[y];\n p[y] = x;\n }\n\n void add_edge(ll u, ll v, T c)\n {\n edges.emplace_back(u, v, c);\n }\n\n T build()\n {\n sort(edges.begin(), edges.end());\n init(n);\n T res = 0;\n for (auto &e : edges)\n {\n if (!same(e.from, e.to))\n {\n res += e.cost;\n unite(e.from, e.to);\n e.used = 1;\n }\n }\n return res;\n }\n\n T build(ll k)\n {\n sort(edges.begin(), edges.end());\n init(n);\n T res = 0;\n ll cnt = 0;\n for (auto &e : edges)\n {\n if (!same(e.from, e.to))\n {\n res += e.cost;\n unite(e.from, e.to);\n e.used = 1;\n cnt++;\n }\n if (cnt == k)\n {\n break;\n }\n }\n return res;\n }\n};\n\nint LIS(int a[], int n)\n{\n vector A(n, 0x3f3f3f3f);\n for (int i = 0; i < n; i++)\n *lower_bound(A.begin(), A.end(), a[i]) = a[i];\n return find(A.begin(), A.end(), 0x3f3f3f3f) - A.begin();\n}\n\n// string maze[1010];\nll maze[100][100];\nll grid_bfs(ll H, ll W, ll sx, ll sy, ll gx, ll gy)\n{\n\n int dx[4] = {1, 0, -1, 0};\n int dy[4] = {0, 1, 0, -1};\n\n vector> dist(H, vector(W, -1));\n dist[sy][sx] = 0;\n\n queue q;\n q.push({sy, sx});\n while (!q.empty())\n {\n ll x, y;\n tie(y, x) = q.front();\n q.pop();\n if (y == gy && x == gx)\n {\n break;\n }\n for (int t = 0; t < 4; t++)\n {\n ll nx = x + dx[t], ny = y + dy[t];\n if (nx < 0 || ny < 0 || nx >= W || ny >= H || dist[ny][nx] != -1 || maze[ny][nx] == '#')\n {\n continue;\n }\n\n dist[ny][nx] = dist[y][x] + 1;\n q.push({ny, nx});\n }\n }\n\n return dist[gy][gx];\n}\n\nvector> warshall_floyd(ll n, vector> g, ll INF)\n{\n //init vector> c(10,vector(10,0));\n\n for (int k = 0; k < n; k++)\n {\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n if (g[i][k] == INF || g[k][j] == INF)\n continue;\n g[i][j] = min(g[i][j], g[i][k] + g[k][j]);\n }\n }\n }\n\n return g;\n}\n\nstruct Dijkstra\n{\n\n ll n;\n vector>> Edges;\n vector Dist;\n vector Prev;\n vector PathNum;\n\n Dijkstra(ll n) : n(n), Edges(n), Dist(n), Prev(n), PathNum(n)\n {\n Prev.assign(n, -1);\n };\n\n void add_edge(ll a, ll b, ll c, bool directed = true)\n {\n if (directed)\n {\n Edges[a].emplace_back(make_pair(b, c));\n }\n else\n {\n Edges[a].emplace_back(make_pair(b, c));\n Edges[b].emplace_back(make_pair(a, c));\n }\n }\n // O((E+V)logV)\n void build(int start)\n {\n priority_queue, greater
VDD;\ntypedef vector VLL;\ntypedef vector VVLL;\n\n#define FOR(a, b, c) for (int (a) = (b); (a) < (c); (a)++)\n#define REP(a, b) FOR((a), 0, (b))\n#define FORD(a, b, c) for (int (a) = (b); (a) >= (c); (a)--)\n#define REPD(a, b) FORD((a), (b), 0)\n#define PB push_back\n#define ITER(it, c) for (auto (it) = (c).begin(); (it) != (c).end(); (it)++)\n#define ALL(c) (c).begin(), (c).end()\n#define MP make_pair\n#define MT(a, b, c) MP(MP((a), (b)), (c))\n#define X first\n#define Y second\n#define FST X.X\n#define SND X.Y\n#define TRD Y\n#define LINF 1e18\n#define INF 1e9\n#define EPS 1e-9\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tLL k, p;\n\tcin >> p >> k;\n\t//VI v (p);\n\t//REP(i, p) v[i] = (i*k)%p;\n\t//REP(i, p) cout << i << \": \" << (i*k)%p << endl;\n\n\tLL ans = 1;\n\tVI vis (p, 0);\n\tFOR(i, 1-(k==1), p){\n\t\tif (vis[i])\n\t\t\tcontinue;\n\t\tint j = i;\n\t\tdo {\n\t\t\tvis[j] = 1;\n\t\t\tj = (j*k)%p;\n\t\t}while(i != j && j != 0);\n\t\tans *= p; ans %= 1000000007;\n\t}\n\tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "617ab717109b1a96f83a92717b840961", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n#define INF 1'000'000'001\n#define f first\n#define s second\n#define pb push_back\n#define MAXN 1123456\n#define pii pair\n#define pdd pair\n#define one 1\n#define int long long\n#define mp make_pair\n#define ld long double\n#define double long double\n#define MOD 1000000007\n\nint p,k;\n\nint times_cycle(int n){\n\tint num=1;\n\tfor(int i=0;i>p>>k;\n\n if(k==0 || k==1){\n \tint resp=1;\n \tfor(int i=0;i\nusing namespace std;\n#define mp make_pair\n\nlong long p, k;\nconst int MOD = 1e9 + 7;\nlong long w[1000100];\n\nlong long pow_mod(long long a, long long b)\n{\n long long ret = 1;\n while(b)\n {\n if(b&1) ret = ret*a%MOD;\n b >>= 1;\n a = a*a %MOD;\n }\n return ret;\n}\n\nint main()\n{\n cin >> p >> k;\n if(k == 1)\n {\n cout << pow_mod(p, p) << endl;\n return 0;\n }\n for(long long i=1;i\nusing namespace std;\n\n#define PB push_back\n#define MP make_pair\n#define SZ size()\n#define all(v) v.begin(), v.end()\n#define REP(i, n) for(int i = 0; i < (int)n; i++)\n#define ITR(i, j, n) for(int i = j; i < (int)n; i++)\n#define mem(array, val) memset(array, val, sizeof(array))\n#define READ(filename) freopen(filename, \"r\", stdin)\n#define WRITE(filename) freopen(filename, \"w\", stdout)\n#define Pii pair \n#define Fr first\n#define Sc second\n#define Long long long\n#define si(a) scanf(\"%d\", &a)\n#define sl(a) scanf(\"%I64d\", &a)\n#define sd(a) scanf(\"%lf\", &a)\n#define sii(a, b) scanf(\"%d%d\", &a, &b)\n#define sll(a, b) scanf(\"%I64d%I64d\", &a, &b)\n#define sdd(a, b) scanf(\"%lf%lf\", &a, &b)\n#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)\n#define debug(x) cout << #x << \": \" << x << endl\n\n#define MAX 1000005\nconst Long mod = 1000000007;\nint P, K, cycle[MAX], mark[MAX];\nvector vec;\n\nint main() {\n Long ans = 1;\n sii(P, K);\n if(!K) {\n ITR(i, 1, P) ans = (ans*P) % mod;\n cout << ans << endl;\n return 0;\n }\n\n ITR(i, 1, P) if(!mark[i]) {\n Long nd = i, len = 0;\n while(!mark[nd]) {\n mark[nd] = 1;\n nd = (K * nd) % P;\n len++;\n }\n cycle[len]++;\n vec.PB(len);\n }\n REP(i, vec.SZ) {\n Long tmp = (vec[i] * cycle[vec[i]]) % mod;\n ans = (ans * (tmp + 1)) % mod;\n }\n if(K == 1) ans = (ans * P) % mod;\n printf(\"%I64d\\n\", ans);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "6f1fa847834fb24496b74678e3b3eabb", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\n#define MAXN 1100000\n#define MOD 1000000007\n\nusing namespace std;\n\ntypedef long long int LL;\n\nLL p,K;\n\nLL fastPow(LL base,LL pow)\n{\n LL ans=1;\n while(pow)\n {\n if(pow&1) ans=(ans*base)%MOD;\n base=(base*base)%MOD;\n pow>>=1;\n }\n return ans;\n}\n\nbool vis[MAXN];\n\nint main()\n{\n scanf(\"%I64d%I64d\",&p,&K);\n if(K==0)\n {\n printf(\"%I64d\\n\",fastPow(p,p-1));\n return 0;\n }\n else if(K==1)\n {\n printf(\"%I64d\\n\",fastPow(p,p));\n return 0;\n }\n int cnt=0;\n for(int i=0;i1) cnt++;\n }\n printf(\"%I64d\\n\",fastPow(p,cnt));\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "e1b84bae27d590532120e27f3a8c2149", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\ntypedef long long ll;\nconst ll N=1000000007;\nusing namespace std;\nll llppow(ll a,ll b,ll p)\n{\n\tll ans = 1;\n\tfor (ll i=0; i>n;\n\tint a[n];\n\tfor (int i=0;i>a[i];\n\t}\n\t*/\n\t/////////////////\n\tll p,k;\n\tcin>>p>>k;\n\tif (k%p==1)\n\t{\n\t\tcout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define X first\n#define Y second\n#define MP make_pair\n#define PB push_back\n#define LL long long\n#define pii pair\n#define pdd pair\n#define ok(a) printf(\"ok ___%d___\\n\", a)\n#define DEBUG(...) fprintf(stderr, __VA_ARGS__)\ntemplate inline bool Chkmin(T &x, T y) { return x > y ? x = y, true : false; }\ntemplate inline bool Chkmax(T &x, T y) { return x < y ? x = y, true : false; }\ntemplate inline T read(T &x) {\n\tstatic int f;\n\tstatic char c;\n\tfor (f = 1; !isdigit(c = getchar()); ) if (c == '-') f = -f;\n\tfor (x = 0; isdigit(c); c = getchar()) x = x * 10 + c - 48;\n\treturn x *= f;\n}\ntemplate inline void write(T x, char P = 0) {\n\tstatic char s[20];\n\tstatic int top = 0;\n\tif (x < 0) x = -x, putchar('-');\n\tdo {s[++top] = x % 10 + 48;} while (x /= 10);\n\tdo {putchar(s[top]);} while (--top);\n\tif (P) putchar(P);\n}\ninline void open() {\n\tfreopen(\".in\", \"r\", stdin);\n\tfreopen(\".out\", \"w\", stdout);\n}\nint $, _;\nconst int MO = 1e9 + 7;\nconst int oo = 0x3f3f3f3f;\nconst int MAXN = 1e5 + 5;\nconst int MAXM = 1e6 + 5;\n\n//EOT\n\nint K, P;\nbool vis[MAXM];\n\ninline int qpow(int x, int n) {\n\tint ans = 1;\n\twhile (n) {\n\t\tif (n & 1) ans = (LL) ans * x % MO;\n\t\tx = (LL) x * x % MO;\n\t\tn >>= 1;\n\t}\n\treturn ans;\n}\n\nint main() {\n\n\tread(P), read(K);\n\n\tif (K == 0) {\n\t\twrite(qpow(P, P - 1));\n\t\treturn 0;\n\t}\n\n\tif (K == 1) {\n\t\twrite(qpow(P, P));\n\t\treturn 0;\n\t}\n\n\tint p = 1;\n\twhile (!vis[p]) {\n\t\t_ ++;\n\t\tvis[p] = 1;\n\t\tp = (LL) K * p % P;\n\t}\n\n\twrite(qpow(P, P / _));\n\n\treturn 0;\n\n}\n\n\n ", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "7c14aacbe7abb38d0f56529b3916c469", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define ff first\n#define ss second\n#define fastio ios_base::sync_with_stdio(0); cin.tie(0)\ntypedef long long ll;\ntypedef long double ld;\nconst ld pi = 3.141592654;\nconst ll N = 1000500;\nconst ll mod = 1000000007;\nconst ll maxi2 = 1e9;\nusing namespace std;\n\n#define TRACE\n#ifdef TRACE\n#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)\ntemplate \nvoid __f(const char* name, Arg1&& arg1) {\n cerr << name << \" : \" << arg1 << endl;}\n \ntemplate \nvoid __f(const char* names, Arg1&& arg1, Args&&... args) {\n const char* comma = strchr(names + 1, ',');\n cerr.write(names, comma - names) << \" : \" << arg1<<\" | \";\n __f(comma+1, args...);}\n#else\n#define trace(...)\n#endif\n\nll repe[N];\nll used[N];\nll mult[N];\n\nll doit(ll i, ll k, ll n){\n\tll cant = 0;\n\n\twhile(1){\n\t\tif(used[i]) break;\n\t\tused[i] = true;\n\t\ti = (i * k) % n;\n\t\tcant++;\n\t}\n\n\treturn cant;\n}\n\nll bipow(ll a, ll b){\n\tif(b == 0) return 1;\n\n\tll ans = bipow(a, b / 2);\n\n\tif(b % 2 == 0) return (ans * ans) % mod;\n\telse return (a * ((ans * ans) % mod)) % mod;\n}\n\nint main(){\n\tfastio;\n\tll n, k;\n\tscanf(\"%lld %lld\",&n,&k);\n\n\tif(k == 0){\n\t\tcout << bipow(n, n - 1) << endl;\n\t\treturn 0;\n\t}\n\n\tfor(ll i = 0; i < n; i++){\n\t\tif(!used[i]) {\n\t\t\tll w = doit(i, k, n);\n\t\t\t//trace(w);\n\t\t\trepe[w]++;\n\t\t}\n\t}\n\n\tfor(ll i = 1; i <= n; i++){\n\t\tfor(ll j = i; j <= n; j += i){\n\t\t\tif(repe[j] != 0) mult[j] += repe[i] * i;\n\t\t}\n\t}\n\n\t//for(ll i = 1; i <= n; i++) trace(repe[i], mult[i]);\n\tll ans = 1;\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tif(mult[i] != 0) ans = (ans * bipow(mult[i], repe[i])) % mod;\n\t}\n\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "783582de0f27f9d2d61e427424c8b9a0", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define For(i,a,b) for(int i=a;i=0;i--)\n {\n res*=res;\n res%=mod;\n if((1ll<>p>>k;\n if(k==1)\n {\n cout<\nusing namespace std;\nconst int mod = 1e9 + 7;\nint p, k;\n\nint mu(int x, int y)\n{\n if (y == 1) return x;\n int tmp = mu(x, y / 2);\n tmp = (1ll * tmp * 1ll * tmp) % mod;\n if (y % 2 == 0) return tmp;\n return (1ll * tmp * 1ll * x) % mod;\n}\n\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(NULL);cout.tie(NULL);\n\n cin >> p >> k;\n\n if (k == 0)\n {\n cout << mu(p, p - 1);\n return 0;\n }\n\n if (k == 1)\n {\n cout << mu(p, p);\n return 0;\n }\n\n int m = 1;\n int t = k;\n while (1)\n {\n m++;\n t = (1ll * t * 1ll * k) % p;\n if (t == 1)\n {\n cout << mu(p, (p - 1) / m);\n return 0;\n }\n }\n\n return 0;\n\n}\n\n/*\n\nx + 2 * y = n\nx + y = k\n*/\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "8e1306b57298283d835cb71c65928a93", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\n#define PI 3.1415926535897932384626433832795028841971693992\n\n#define pb push_back\n#define mp make_pair \n#define fi first\n#define se second\n#define ZERO(a) memset(a, 0, sizeof(a))\n#define INIT(a) memset(a, 0xff, sizeof(a))\n#define FOR(i, n) for (i=0; i=0; i--)\n#define FOR2(i, a, b) for (i=a; i=a; i--)\n#define BIT(i, m) (m&(1LL<<(i)))\n#define RBIT(i, m) (m&~(1LL<<(i)))\n#define ABIT(i, m) (m|(1LL<<(i)))\n#define FORB(i, m) for (i=0; i<32; i++) if (BIT(i, m)>0)\n#define ALL(a) a.begin(), a.end()\n#define OUT(i, n) (i<0 || i>=n)\n#define AT(a, i) (a[(i+a.size())%a.size()])\n#define IT(v, i) (v.begin()+(i+v.size())%v.size())\n#define IT2(v, i, j) IT(v, i), IT(v, j)\n#define INSET(a, S) (S.find(a)!=S.end())\n\ntypedef vector vi;\ntypedef long long ll;\ntypedef vector vll;\ntypedef pair ii;\ntypedef vector vii;\ntypedef vector vs;\ntypedef list li;\ntypedef long double ld;\n\nconst int mod =1000000007;\n\nll pow(ll a, ll p)\n{\n ll res = 1;\n while(p>0) {\n if (p%2==1) res *= a, res %= mod;\n a *= a, a %= mod, p/=2;\n }\n return res;\n}\n\nint main() \n{\n ll p,k;\n cin>>p>>k;\n if (k==0) {\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define ll __int64\n#define ull unsigned __int64\n#define eps 1e-8\n#define NMAX 100000000\n#define MOD 1000000007\n#define lson l,mid,rt<<1\n#define rson mid+1,r,rt<<1|1\n#define PI acos(-1)\ntemplate\ninline void scan_d(T &ret)\n{\n char c;\n int flag = 0;\n ret=0;\n while(((c=getchar())<'0'||c>'9')&&c!='-');\n if(c == '-')\n {\n flag = 1;\n c = getchar();\n }\n while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();\n if(flag) ret = -ret;\n}\nconst int maxn = 1000000+10;\n\nbool flag[maxn];\nint p;\n\nint quick(int a, int b)\n{\n int ret = 1;\n while(b)\n {\n if(b&1) ret = (ll)ret*a%p;\n a = (ll)a*a%p;\n b >>= 1;\n }\n return ret;\n}\n\nint main()\n{\n#ifdef GLQ\n freopen(\"input.txt\",\"r\",stdin);\n// freopen(\"o.txt\",\"r\",stdin);\n// freopen(\"o1.txt\",\"w\",stdout);\n#endif\n int k;\n scanf(\"%d%d\",&p,&k);\n if(k == 0)\n {\n ll ans = 1;\n for(int i = 1; i < p; i++) ans = ans*p%MOD;\n printf(\"%I64d\\n\",ans);\n return 0;\n }\n else if(k == 1)\n {\n ll ans = 1;\n for(int i = 0; i < p; i++) ans = ans*p%MOD;\n printf(\"%I64d\\n\",ans);\n return 0;\n }\n ll ans = 1;\n for(int i = 1; i < p; i++) if(flag[i] == 0)\n {\n int x = i;\n while(flag[x] == 0)\n {\n flag[x] = 1;\n x = (ll)x*k%p;\n }\n ans = ans*p%MOD;\n }\n printf(\"%I64d\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "bd051f8defa796b5e1c79b45ffe1bfed", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\n__________________________68888.__________________,\n _______________________.8888888881________________,\n______________________8888888888888_______________,\n_____________________.88888888888888_.____________,\n_____________________88888888888888885____________,\n_____________________888888888888888888.__________,\n____________________8888888888888888888888._______,\n____________________8888888888888888888_.88588____,\n________________888888888888888888888888._________,\n_______________6888888888888888888888858._________,\n_______________88888888888888888888888868_________,\n______________8888888888888888888888888.__________,\n___________._888888888888888888888888885__________,\n____________8888888888888888888888888588._________,\n____________88888888888888888888888886881_________,\n___________68888_88888888888888888885___._________,\n__________.8888..8888888888888888888______________,\n__________.8888__8888888888888888888______________,\n___________88888_6888888888888888888______________,\n___________.8888._888888888888888888______________,\n____________:8888.8888888888888888888…__________,\n_____________688856888888888888888888886__________,\n______________6888888888888888888888888888________,\n_______________888888888888888888888888888________,\n________________8888888888888888888888888_________,\n______________..8888888888888888888888885_________,\n_______________88888888888888888888888888_________,\n______________688888888888888888888888888_________,\n______________88.88888888888___8888888881_________,\n________________88888888888_.__.88888888._________,\n_______________18888888888______68888888__________,\n______________.8888888888..______8888888__________,\n______________8888888888.________8888888._________,\n_____________.888888888__________.8888886_________,\n_____________.88888888____________8888888_________,\n_____________68888888____________.8888888_________,\n_____________8888888.____________._888888:________,\n____________8888888._______________8888888._______,\n___________.8888888________________:888888________,\n___________6888888__________________6888886_______,\n__________.888888.___________________888888_______,\n__________6888888____________________888888.______,\n__________8888885____________________.888886______,\n__________888888______________________888888______,\n__________888888______________________888888______,\n_________588888________________________88888______,\n_________88888_._______________________88888______,\n_________88885__________________________8888______,\n_________8888___________________________8888______,\n________.888.___________________________6888______,\n______..8888____________________________88888.____,\n_______.6888____________________________8888_.____,\n______._88888.__________________________8888:.____,\n________8888.__________________________88888______,\n________8888__________________________588888______,\n________88888________________________888888.______,\n\n*/\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ull long long\n#define ll long long\n#define pii pair\n#define p_queue priority_queue\n#define pb push_back\n#define mp make_pair\n#define all(v) (v).begin(),(v).end()\nusing namespace std;\nconst ll mod=(1e9)+7;\n\nll fun1(ll a, ll b)\n{\n ll tmp=1;\n while(b)\n {\n if(b&1)tmp=(tmp*a)%mod;\n a=(a*a)%mod;\n b>>=1;\n }\n return tmp;\n}\n\n\nint main()\n{\n ll p,k;\n while( cin>>p>>k)\n {\n if(k==0)\n {\n cout<\n\n\n\n#define NN 1000111\n\n#define mod 1000000007\n\nusing namespace std;\n\ntypedef long long INT;\n\n\n\nINT vst[NN], arr[NN];\n\n\n\nINT power(INT u, int n){\n\n\tif(n==0) return 1;\n\n\tINT ans=power(u, n/2);\n\n\tans=ans*ans%mod;\n\n\tif(n&1) ans=ans*u%mod;\n\n\treturn ans;\n\n}\n\n\n\nint main(){\n\n\tint n,k,i,j,p;\n\n\tcin>>p>>k;\n\n\tif(k==0) {cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define eps 1e-6\n#define LL long long\n#define pii pair\n//#pragma comment(linker, \"/STACK:1024000000,1024000000\")\nusing namespace std;\n\n//const int MAXN = 5000000 + 5;\n//const int INF = 0x3f3f3f3f;\nconst LL mod = 1e9 + 7;\nint n;\nLL p, k;\nset s;\nLL fa[1000100];\nint Find(LL x) {\n if(x != fa[x]) return fa[x] = Find(fa[x]);\n return x;\n} \n\nint cnt[1001000];\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n cin >> p >> k;\n s.insert(0);\n LL ans = 1;\n if(k==0) {\n for(int i = 1; i < p; i++) ans = ans * p % mod;\n cout << ans;\n return 0;\n }\n for(int i = 0; i < p; i++) fa[i] = i;\n for(int i = 0; i < p; i++) {\n LL shit = k * i % p;\n int x1 = Find(i), x2 = Find(shit);\n fa[x1] = x2;\n }\n for(int i = 0; i < p; i++) {\n int z = Find(i);\n cnt[z]++;\n }\n for(int i = 1; i < p; i++)\n if(cnt[i]) ans = ans * p % mod;\n if(k == 1) ans = ans * p % mod;\n cout << ans;\n return 0;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "2b525ae9236754c40d7a5821e66fc493", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \nusing namespace __gnu_pbds;\nusing namespace std;\n#define getbit(n, i) (((n) & (1LL << (i))) != 0) \n#define setbit0(n, i) ((n) & (~(1LL << (i)))) \n#define setbit1(n, i) ((n) | (1LL << (i))) \n#define togglebit(n, i) ((n) ^ (1LL << (i))) \n#define lastone(n) ((n) & (-(n))) \nchar gap = 32;\ntemplate \nostream& operator<<(ostream &os, const vector &v) { \n os << '{'; \n for (const auto &x : v) os << gap << x;\n return os << '}'; \n}\ntemplate \nostream& operator<<(ostream &os, const pair &p) {\n return os << '(' << p.first << gap << p.second << ')';\n}\n\ntemplate \nvoid read(T &x) {\n char c;\n for (c = getchar(); c < '0' || c > '9'; c = getchar())\n ;\n for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);\n}\n#define ll long long \n#define lll __int128_t\n#define pb push_back\n#define mp make_pair\ntypedef pair < int,int> ii;\ntypedef vector vii;\ntypedef vectorvl;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef tree<\nint,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>\nordered_set;\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\nll hashPrime = 1610612741;\n#define mod 1000000007\nll parent[2000005];\nll rating[2000005];\nll cnt[2000005];\nll findparent(ll root){\n while(parent[root]!=root){\n root = parent[root];\n }\n return root;\n}\nvoid findunion(ll root1,ll root2){\n ll par1 = findparent(root1);\n ll par2 = findparent(root2);\n if(rating[par1]>=rating[par2]){\n parent[par2] = par1;\n rating[par1]++;\n }\n else{\n parent[par1] = par2;\n rating[par2]++;\n }\n}\nvoid ini(ll n){\n for (ll i = 0; i <= n;i++){\n parent[i] = i;\n }\n}\nll modularExponentiation(ll x, ll n, ll M)\n{\n if (n == 0)\n return 1;\n else if (n % 2 == 0) //n is even\n return modularExponentiation((x * x) % M, n / 2, M);\n else //n is odd\n return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M; \n \n } \nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n ll i, j, k, l, n, m;\n cin >> n >> m;\n ini(n);\n if(m==0){\n cout << modularExponentiation(n, n - 1, mod) << \"\\n\";\n }\n else if(m==1){\n cout << modularExponentiation(n, n, mod) << \"\\n\";\n }\n else{\n for (i = 1; i < n;i++){\n ll bash = (i * m) % n;\n findunion(i, bash);\n }\n set s;\n for (i = 1; i < n;i++){\n parent[i] = findparent(i);\n s.insert(parent[i]);\n }\n ll sz = (ll)s.size();\n cout << modularExponentiation(n, sz, mod) << \"\\n\";\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "a3a4d7ce13cd1be57f9ff7a56583f55f", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\nCodeforces Contest #334, Div. 2, Problem D:\n*/\n\n/* theprof, Dec 1, 2015 */\n\n#include \n#include \n\n#define N 1000000\n#define MOD 1000000007\n\nusing namespace std;\n\nint main() {\n int p, k;\n long long i;\n long long num = 1;\n long long q;\n\n // read input parameters\n cin >> p >> k;\n\n if (k <= 1) {\n for (q=0; q\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#if !defined(__GNUC__)\n#else // !defined(__GNUC__)\n#define _CrtDbgBreak() __builtin_trap()\n#endif // !defined(__GNUC__)\n\n#if defined(ONLINE_JUDGE)\n#define LOCAL_TEST 0\n#else \n#define LOCAL_TEST 1\n#endif\n\n#if LOCAL_TEST\nstruct AssertsCounter\n{\n AssertsCounter(): counter(0) {}\n ~AssertsCounter() { std::cout << std::endl << \"DIAG: \" << (counter == 0 ? \"OK\" : \"FAIL!!!\") << \" Asserts count: \" << counter << std::endl; }\n void Increment() { counter++; }\n uint32_t counter;\n};\nAssertsCounter g_assertsCounter;\n#define LOCAL_ASSERT(expr) { if (!(expr)) {std::cout << \"ASSERT FAILED (\" << __LINE__ << \"): '\" << #expr << \"'\" << std::endl; g_assertsCounter.Increment(); _CrtDbgBreak(); } }\n#define LOCAL_ASSERT_EQ(expr1, expr2) { if ((expr1) != (expr2)) {std::cout << \"ASSERT FAILED (\" << __LINE__ << \"): '\" << #expr1 << \"' == '\" << #expr2 << \"' (\" << (expr1) << \" vs \" << (expr2) << \"')\" << std::endl; g_assertsCounter.Increment(); _CrtDbgBreak(); } }\n#else\nvolatile bool isLocalTestEnabled = 0;\n#define LOCAL_ASSERT(expr)\n#define LOCAL_ASSERT_EQ(expr1, expr2)\n#endif\n\nbool g_isLocalPrintEnabled = (bool)(LOCAL_TEST);\n#define LOCAL_PRINT() if (!g_isLocalPrintEnabled) { } else std::cout // << .. << ..\n\ntypedef std::string string8_t;\ntypedef long double ld_t;\n\ntypedef std::vector vector_size_t;\ntypedef std::vector vector_int32_t;\ntypedef std::vector vector_uint32_t;\ntypedef std::vector vector_int64_t;\ntypedef std::vector vector_uint64_t;\ntypedef std::vector vector_string8_t;\n\ntypedef std::vector vector_2d_size_t;\ntypedef std::vector vector_2d_int32_t;\ntypedef std::vector vector_2d_uint32_t;\ntypedef std::vector vector_2d_int64_t;\ntypedef std::vector vector_2d_uint64_t;\n\ntypedef std::set set_size_t;\ntypedef std::set set_int32_t;\ntypedef std::set set_uint32_t;\ntypedef std::set set_int64_t;\ntypedef std::set set_uint64_t;\ntypedef std::set set_string8_t;\n\ntypedef std::multiset multiset_size_t;\ntypedef std::multiset multiset_string8_t;\n\n// Auxiliary functions definition\n// \ntemplate void UpdateMin(T& a, const T b) {a = std::min(a, b);}\ntemplate void UpdateMax(T& a, const T b) {a = std::max(a, b);}\n\nconst ld_t Pi = std::atan(1.0L) * 4.0L;\nstatic const ld_t Eps = 1.0e-09;\ntemplate bool IsEqual(const T a, const T b) { return std::abs(a - b) < Eps; }\ntemplate bool IsGreater(const T a, const T b) { return a > b + Eps; }\ntemplate bool IsLess(const T a, const T b) { return a + Eps < b; }\ntemplate bool IsGreaterEqual(const T a, const T b) { return !IsLess(a, b); }\ntemplate bool IsLessEqual(const T a, const T b) { return !IsGreater(a, b); }\n\ntemplate string8_t ToStr(const T& val) { std::ostringstream ostr; ostr << val; return ostr.str(); }\ntemplate bool FromStr(const string8_t& str, T& val) {std::istringstream istr(str); istr >> val; return !!istr; }\n\ntemplate std::istream& operator>>(std::istream& ist, std::vector& data)\n{\n LOCAL_ASSERT(!!ist);\n for (size_t i = 0; i < data.size(); i++) { ist >> data[i]; }\n return ist;\n}\n\ntemplate T Read(std::istream& ist)\n{\n LOCAL_ASSERT(!!ist);\n T val; ist >> val; return val;\n}\n\ntemplate std::ostream& operator<<(std::ostream& ost, const std::vector& data)\n{\n for (size_t i = 0; i < data.size(); i++) { if (i != 0) { ost << ' '; } ost << data[i]; }\n return ost;\n}\n\n#if defined(ONLINE_JUDGE)\ntemplate class StopWatch { };\n#else\n#include \n#include \nlibrary::random::Rand g_rnd;\n#endif\n\nconst uint64_t Mod = 1000000007;\n\ntemplate\nstatic T FastPow(const T a, const T p)\n{\n T res = 1;\n\n T mask = 1;\n T powPart = a;\n\n const size_t bitsCount = sizeof(T) * 8;\n for (size_t i = 0; i < bitsCount; i++)\n {\n if (p & mask)\n {\n res *= powPart;\n res %= Mod;\n }\n mask <<= 1;\n powPart *= powPart;\n powPart %= Mod;\n }\n\n return res;\n}\n\nbool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode)\n{\n StopWatch<1> sw; (void)sw;\n\n // \n uint64_t p, k;\n ist >> p >> k;\n\n if (multipleTestMode && !ist)\n return false;\n\n LOCAL_PRINT() << std::endl << \"Next test: \" << p << \" \" << k << std::endl;\n //\n\n if (k == 0)\n {\n const uint64_t ans = FastPow(p, p - 1);\n ost << ans << std::endl;\n return multipleTestMode;\n }\n\n if (k == 1)\n {\n const uint64_t ans = FastPow(p, p);\n ost << ans << std::endl;\n return multipleTestMode;\n }\n\n uint64_t groups = 0;\n\n vector_size_t used(p + 1);\n uint64_t ans = 1;\n\n for (uint64_t x = 1; x < p; x++)\n {\n if (used[x])\n continue;\n\n groups += 1;\n ans *= p;\n ans %= Mod;\n\n used[x] = 1;\n\n uint64_t v = x;\n while (!used[v * k % p])\n {\n used[v * k % p] = 1;\n v = v * k % p;\n }\n }\n\n ost << ans << std::endl;\n\n return multipleTestMode;\n}\n\n\n#if LOCAL_TEST\nvoid Test()\n{\n using namespace library::random;\n const auto genVector = GenFactory::CreateGenVector(GenRange(2, 10), GenRange(-100, +200));\n\n for (size_t t = 0; t < 10; t++)\n {\n const vector_int32_t data = genVector();\n // size_t ans = GetAns(data);\n }\n}\n#endif\n\nint main(int argc, const char *argv[])\n{\n#if LOCAL_TEST\n Test();\n#endif\n\n std::ios_base::sync_with_stdio(false);\n std::istream* ist = &std::cin;\n std::ostream* ost = &std::cout;\n \n std::unique_ptr fileInput;\n \n if (argc > 1)\n {\n fileInput.reset(new std::ifstream(argv[1]));\n if (!(*fileInput))\n {\n std::cout << \"File not found: \" << argv[1] << std::endl;\n }\n ist = fileInput.get();\n } \n \n#if defined(ONLINE_JUDGE)\n Solve(*ist, *ost, false);\n#else\n while(Solve(*ist, *ost, true)) {};\n#endif\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "bb588f150b2fbb6c50ab60e4cbea737f", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nconst ll M = 1000000007;\n\nll p,k;\n\nconst int N = 1000005;\n\nint vis[N];\n\nint visit(int x){\n\tint cnt = 1;\n\tvis[x] = 1;\n\twhile(!vis[(x * k) % p]) {\n\t\tx = (x * k) % p;\n\t\tcnt++;\n\t\tvis[x] = 1;\n\t}\n\treturn cnt;\n}\n\nll powx(ll a,ll b){\n\tll ans = 1;\n\twhile(b){\n\t\tif (b&1)ans = (ans * a) % p;\n\t\tb >>= 1;\n\t\ta = (a * a) % p;\n\t}\n\treturn ans;\n}\n\nint main(){\n\tcin >> p >> k;\n\tll ans = 1;\n\tfor (int i = 1;i < p;i++)\n\t\tif (!vis[i]){\n\t\t\tll siz = visit(i);\n\t\t\t//cout <\nusing namespace std;\n\n/* macros */\n// memory\n#define zeroset(A) memset((A), 0, sizeof((A)))\n#define negset(A) memset((A), -1, sizeof((A)))\n#define all(c) (c).begin(), (c).end()\n#define pb push_back\n// for loops\n#define forlop(i,n) for (int i=0; i pii;\ntypedef pair pdd;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vll;\ntypedef vector > matrix;\n#define ff first\n#define ss second\n#define mp make_pair\n// constants\n#define EPS 1e-9\n#define MAXN 1000005\n#define INF INT_MAX\n#define NINF INT_MIN\n\nstring s;\nbool vis[MAXN];\nll k,p;\nconst ll MOD = 1000000007;\n\nvoid dfs(int i)\n{\n if (!vis[i]){\n vis[i] = true;\n dfs((k*i)%p);\n }\n}\n\nint main()\n{\n fast;\n int n;\n cin >> p >> k;\n ll ans = 1;\n for (int i=1; i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define fi first\n#define se second\n#define pb push_back\n#define mk make_pair\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\nll read()\n{\n char ch=getchar();ll x=0,f=1;\n while(ch<'0'||ch>'9')\n {\n if(ch=='-') f=-1;\n ch=getchar();\n }\n x=ch-'0';\n while(1)\n {\n ch=getchar();\n if(ch<'0'||ch>'9') break;\n x=x*10+ch-'0';\n }\n return x*f;\n}\nconst ll mod=1e9+7;\nll p,k,ret;\nll qmul(ll a,ll b,ll Mod)\n{\n ll ans=0;\n while(b)\n {\n if(b&1) ans=(ans+a)%Mod;\n b>>=1;a=(a+a)%Mod;\n }\n return ans;\n}\nll qpow(ll a,ll b,ll Mod)\n{\n ll ans=1;\n while(b)\n {\n if(b&1) ans=qmul(ans,a,Mod);\n b>>=1;a=qmul(a,a,Mod);\n }\n return ans;\n}\nint main()\n{\n // freopen(\"B.in\",\"r\",stdin);\n // freopen(\"B.out\",\"w\",stdout);\n p=read(),k=read();\n if(k==0) ret=qpow(p,p-1,mod);\n if(k==1) ret=qpow(p,p,mod);\n if(k>1)\n {\n ll l=1,cur=k;\n while(cur!=1)\n {\n cur=(ll) cur*k%p;\n l++;\n }\n ret=qpow(p,(p-1)/l,mod);\n }\n printf(\"%I64d\\n\",ret);\n // fclose(stdin);\n // fclose(stdout);\n return 0;\n}\n ", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "34c9d9ab537e89bf66b13ae32ed4cdb2", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\n#define ll long long\n#define F first\n#define S second\n#define mp make_pair\n#define pii pair\n#define vi vector\n#define pq priority_queue\n#define pb push_back\n#define watch(x) cout << (#x) << \" is \" << (x) << endl\n#define ALL(a) (a.begin()),(a.end())\n#define FOR(x,to) for(ll x=0;x<(to);x++)\n#define FORR(x,to) for(ll x=1;x<=(to);x++)\n#define fastio ios::sync_with_stdio(0); cin.tie(NULL);\nconst int INF = 0x3f3f3f3f;\nconst int MOD = 1e9+7;\n\n\nll expo(ll x, ll a){\n if(a==0) return 1;\n ll ans = expo(x,a/2);\n \n ans *= ans;\n ans %= MOD;\n if(a%2) {\n\n ans *= x;\n ans %= MOD;\n } \n return ans;\n}\n\n\nint main(){\n\tfastio;\n ll p, k;\n cin >> p >> k;\n\n if(k == 0){\n cout << expo(p,p-1) << \"\\n\";\n }\n else if(k == 1){\n cout << expo(p,p) << \"\\n\";\n }\n else{\n ll at = k;\n ll fator = 1;\n while(at!=1){\n \n at = (at*k)%p;\n fator++;\n }\n \n ll expoente = (p-1)/fator;\n \n cout << expo(p,expoente) << \"\\n\";\n\n }\n\n }", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "d7e01d0634f6c7c76fea1da59d6e7580", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nconst int mod = 1e9 + 7;\nint p, k, ans, vis[1 << 20];\nint solve() {\n\tans = 1;\n\tif(!k)\n {\n for(int i=1;i> p >> k;\n\tcout << solve();\n\n}\n\n\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "5b2eec6792e14903dea127e94dc92219", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\n#define mod 1000000007\n#define M 1000005\n#define ll long long\nll p,k;\nll pow(ll a,ll b){\n\tll res=1;\n\tll x=a;\n\twhile(b){\n\t\tif(b&1)res=(res*a)%mod;\n\t\tb>>=1;\n\t\ta=a*a%mod;\n\t}\n\treturn res;\n}\nint main(){\n\tcin>>p>>k;\n\tif(k==0){\n\t\tcout<\n#include \n#include \n#include \nusing namespace std;\nconst long long A=1e9+7;\n\nint main(int argc, const char * argv[]) {\n long long p,k;\n cin>>p>>k;\n if(k>1){\n long long i;\n long long a=1;\n for(i=1;i<=p-1;i++){\n a=a*k;\n a%=p;\n if(a%p==1) break;\n }\n long long j=(p-1)/i;\n long long ans=1;\n for(int l=1;l<=j;l++){\n ans*=p;\n ans%=A;\n }\n ans%=A;\n cout<\n\nusing namespace std;\n\nconst int MAXN = 1<<20;\nconst int MOD = 1000000007;\n\nbool bio[MAXN];\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\t\n\tint p, k, i;\n\tlong long inv;\n\tlong long sol = 1;\n\t\n\tcin>>p>>k;\n\t\n\tif (k == 0)\n\t{\n\t\tfor (i = 1; i < p; ++i)\n\t\t\tsol *= p,\n\t\t\tsol %= MOD;\n\t\tgoto end;\n\t}\n\t\n\tfor (inv = 1; inv * k % p != 1; ++inv);\n\t\n\tif (k == 1)\n\t\tsol = p;\n\t\n\tfor (i = 1; i < p; ++i)\n\t{\n\t\tif (!bio[i])\n\t\t{\n\t\t\tsol *= p;\n\t\t\tsol %= MOD;\n\t\t\t\n\t\t\tfor (long long j = i; !bio[j]; j = j * inv % p)\n\t\t\t\tbio[j] = true;\n\t\t}\n\t}\n\tend:\n\tcout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nconst int MOD = 1000000007;\n\nint p, k, ver[1001000], po[1001000];\n\nint main() {\n int i, r = 1;\n //freopen(\"ttt\", \"r\", stdin);\n\n cin >> p >> k;\n\n if(k == 0) {\n\n for(i = 1; i < p; ++i)\n r = (1LL * r * p) % MOD;\n\n cout << r;\n\n return 0;\n }\n if(k == 1) {\n for(i = 0; i < p; ++i)\n r = (1LL * r * p) % MOD;\n\n cout << r;\n\n return 0;\n }\n\n po[0] = 1;\n for(i = 1; i <= 1000000; ++i)\n po[i] = (1LL * po[i - 1] * k) % p;\n\n for(i = 1; i < p; ++i) if(!ver[i]) {\n int j = i, l = 0;\n\n do {\n ver[j] = 1;\n\n ++l;\n\n j = (1LL * k * j) % p;\n } while(!ver[j]);\n\n if(po[l] == 1)\n r = (1LL * r * p) % MOD;\n else {\n //r = (1LL * r * 2) % MOD;\n }\n\n }\n\n cout << r;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "5478cc7fe414f375f6cb5c46293fece1", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\n#define mp make_pair\n#define pb push_back\n#define ll long long\n\nusing namespace std;\n\nconst int MOD = 1E9+7;\n\nint parent[1111111];\nint nc;\n\nint find( int x ) {\n\tif ( x == parent[x] ) return x;\n\treturn parent[x] = find(parent[x]);\n}\n\nvoid merge(int a, int b) {\n\ta = find(a); b = find(b);\n\tif ( a == b ) return;\n\tnc --;\n\tparent[b] = a;\n}\n\nll fmp(ll x, int e) {\n\tif ( e == 0 ) return 1;\n\tll w = fmp(x, e/2);\n\tw = (w * w) % MOD;\n\tif ( e&1 ) w = (w * x) % MOD;\n\treturn w;\n}\n\nint main() {\n\n\tcin.tie(0);\n\tios_base::sync_with_stdio(0);\n\n\tll p, k;\n\t\n\tcin >> p >> k;\n\t\n\tif ( k == 0 ) {\n\t\tcout << fmp(p, p - 1) << '\\n';\n\t\treturn 0;\n\t}\n\t\n\tnc = p - (k > 1);\n\t\n\tfor ( ll i = 1; i < p; i ++ ) parent[i] = i;\n\t\t\n\tfor ( ll i = 1; i < p; i ++ )\n\t\tmerge ( i, (i * k) % p );\n\t\n\tcout << fmp ( p, nc ) << '\\n';\n\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "5a38e853cc6d3debee553de0435e2644", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#ifdef Adrian\n#include \"debug.h\"\n#else\n#define debug(...) 9999\n#endif\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef complex point;\n#define F first\n#define S second\n#define ii pair\ntemplate \nstruct triple{G F, S, T; };\n#define iii triple\n\nstruct DS\n{\n vector parent;\n vector cont;\n int cc;\n DS(int n) : parent(n), cont(n, 1), cc(0)\n {\n for(int i=0; i cont[y])\n swap(x, y);\n\n cc += (cont[x] == 1 && cont[y] == 1);\n cc -= (cont[x] > 1 && cont[y] > 1);\n\n parent[x] = y;\n cont[y] += cont[x];\n return true;\n }\n};\n\nint main()\n{\n\n#ifdef Adrian\n //freopen(\"test.txt\", \"r\", stdin);\n#else\n ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);\n#endif\n\n const int mod = 1e9 + 7;\n int p, k;\n cin>>p>>k;\n\n int ans = 1;\n if(k == 0)\n {\n for(int i=0; i

g(p);\n for(int i=0; i 1)\n cc = ds.cc;\n for(int i=0; i\n#include \n#include \n#include \n\nusing namespace std;\n\nint r()\n{\n int xy;\n scanf(\"%d\",&xy);\n return xy;\n}\n\nbool odw[1000007];\nconst int mod=1000000007;\nlong long ile[1000007];\nlong long cc[1000007];\nlong long dp[1000007];\n\nint main()\n{\n long long wyn=1;\n int p,k;\n scanf(\"%d%d\",&p,&k);\n //odw[0]=1;\n for(int i=0; i\n#include \n#include \n#include \nusing namespace std;\nconst int maxn=1000010,mod=1e9+7;\nlong long p,k,ans=1,vis[maxn],f[maxn];\nvoid dfs(int n,int l)\n{\n if(vis[n]==1)\n {\n f[l]++;\n return;\n }\n vis[n]=1;\n dfs(((long long)n*k)%p,l+1);\n}\nint main()\n{\n#ifdef sherco\n freopen(\"wtf.txt\",\"r\",stdin);\n freopen(\"a.txt\",\"w\",stdout);\n#endif\n scanf(\"%I64d%I64d\",&p,&k);\n if(k!=0&&k!=1)\n {\n for(int i=1;i\n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nint mod = 1e9 + 7;\n\nint add(int i, int j) { return ((i += j) < mod ? i : i -= mod); }\nint mult(int i, int j) { return (ll) i * j % mod; }\nint pwr(int i, int k) {\n int r = 1;\n while(k) {\n if(k & 1)\n r = mult(r, i);\n i = mult(i, i);\n k >>= 1;\n }\n \n return r;\n}\n\nint P[1000001];\nint beb(int a, int m) {\n memset(P, -1, sizeof P);\n int s = sqrt(m);\n mod = m;\n int r = 1;\n int d = pwr(a, m - 2);\n for(int i = 0; i <= s; ++i) {\n if(P[r] == -1) P[r] = i;\n r = mult(r, d);\n if(r == 1) { mod = 1e9 + 7; return i + 1; }\n }\n \n d = pwr(a, s);\n r = d;\n for(int i = 1; i <= s; ++i) {\n if(P[r] > -1) {\n mod = 1e9 + 7;\n return s * i + P[r];\n }\n \n r = mult(r, d);\n }\n\n return -1;\n}\n\nint main() {\n int p, k; cin >> p >> k;\n if(k == 1) cout << pwr(p, p);\n else if(k == 0) cout << pwr(p, p - 1);\n else cout << pwr(p, (p - 1) / beb(k, p));\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "f5b11b7f8a45e5d89529c7ce108caf27", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*************************************************************************\n\t> File Name: 334d.cpp\n\t> Author: \n\t> Mail: \n\t> Created Time: 2015年12月02日 星期三 14时01分02秒\n ************************************************************************/\n\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nconst int MOD=1000000007;\nconst int maxn=1000007;\nLL P,K;\nLL vis[maxn];\nLL num[maxn];\nLL getnum(int pos)\n{\n LL tep=(num[pos]*pos)%MOD;\n tep=(tep+1)%MOD;\n LL re=1;\n for(int i=1;i<=num[pos];i++)\n re=(re*tep)%MOD;\n return re;\n}\nint main()\n{\n cin >> P>>K;\n memset(vis,0,sizeof(vis));\n memset(num,0,sizeof(num));\n if(K==0){\n LL tepre=1;\n for(int i=1;i\n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nconst ll MOD = 1000000007;\n\n\nstruct DSU\n{\n\tvector parent;\n\n\tDSU(ll n)\n\t{\n\t\tparent = vector(n);\n\t\tfor (ll i = 0LL; i < n; i++) {\n\t\t\tparent[i] = i;\n\t\t}\n\t}\n\n\tll find_set(ll a)\n\t{\n\t\tif (a == parent[a]) {\n\t\t\treturn a;\n\t\t}\n\t\tll base = find_set(parent[a]);\n\t\tparent[a] = base;\n\t\treturn base;\n\t}\n\n\tvoid union_set(ll a, ll b)\n\t{\n\t\ta = find_set(a);\n\t\tb = find_set(b);\n\t\tif (a != b) {\n\t\t\tif (rand() % 2) {\n\t\t\t\tswap(a, b);\n\t\t\t}\n\t\t\tparent[b] = a;\n\t\t}\n\t}\n};\n\nll count_cycles(ll p, ll k)\n{\n\tDSU dsu(p);\n\tfor (ll i = 1LL; i < p; i++) {\n\t\tll ki = (k * i) % p;\n\t\tdsu.union_set(i, ki);\n\t}\n\n\tvector found(p, false);\n\tll count = 0LL;\n\tfor (ll i = 1LL; i < p; i++) {\n\t\tll base = dsu.find_set(i);\n\t\tif (!found[base]) {\n\t\t\tfound[base] = true;\n\t\t\tcount++;\n\t\t}\n\t}\n\treturn count;\n}\n\nll count_solutions(ll p, ll k)\n{\n\tll cycles;\n\tif (k == 0) {\n\t\tcycles = p - 1;\n\t}\n\telse if (k == 1) {\n\t\tcycles = p;\n\t}\n\telse {\n\t\tcycles = count_cycles(p, k);\n\t}\n\n\tll count = 1LL;\n\tfor (ll i = 0LL; i < cycles; i++) {\n\t\tcount *= (ll)p;\n\t\tcount %= MOD;\n\t}\n\treturn (ll)count;\n}\n\nint main()\n{\n\tll p, k;\n\tcin >> p >> k;\n\tll count = count_solutions(p, k);\n\tprintf(\"%lld\\n\", count);\n\n\tint end;\n\tcin >> end;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "cfbac577baa33ed3f19a9b8145649050", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define ppb pop_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define abs(a) ((a) < 0 ? -(a) : (a))\n#define sqr(a) ((a) * (a))\n\ntypedef unsigned int uint;\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nconst double eps = 1e-9;\nconst int mod = (int)1e+9 + 7;\nconst double pi = acos(-1.);\nconst int maxn = 1000100;\n\nint snm[maxn];\nint sz[maxn];\n\nint getroot(int u) {\n\treturn(snm[u] == u ? u : (snm[u] = getroot(snm[u])));\n}\n\nvoid merge(int u, int v) {\n\tu = getroot(u), v = getroot(v);\n\tif(u != v) {\n\t\tsz[u] += sz[v];\n\t\tsnm[v] = u;\n\t}\n}\n\nint isok(ll a, ll b, ll p) {\n\tll res = 1;\n\twhile(b) {\n\t\tif(b & 1) {\n\t\t\tres = res * a % p;\n\t\t}\n\t\ta = a * a % p;\n\t\tb >>= 1;\n\t}\n\treturn(res == 1 ? p : 1);\n}\n\nint binpow(ll a, ll b) {\n\tll res = 1;\n\twhile(b) {\n\t\tif(b & 1) {\n\t\t\tres = res * a % mod;\n\t\t}\n\t\ta = a * a % mod;\n\t\tb >>= 1;\n\t}\n\treturn(res);\n}\n\nint main() {\n\n\t#ifdef SOL\n\t{\n//\t\tfreopen(\"input.txt\", \"r\", stdin);\n//\t\tfreopen(\"output.txt\", \"w\", stdout);\n\t}\n\t#else\n\t{\n\t\tsrand(time(0));\n\t\tconst string file = \"\";\n\t\tif(!file.empty()) {\n\t\t\tfreopen((file + \".in\").c_str(), \"r\", stdin);\n\t\t\tfreopen((file + \".out\").c_str(), \"w\", stdout);\n\t\t}\n\t}\n\t#endif\n\n\tint p, k;\n\tscanf(\"%d%d\", &p, &k);\n\n\tif(k == 0) {\n\t\tcout << binpow(p, p - 1);\n\t\treturn(0);\n\t}\n\n\tfor(int i = 0; i < p; i++) {\n\t\tsnm[i] = i;\n\t\tsz[i] = 1;\n\t}\n\n\tfor(int i = 0; i < p; i++) {\n\t\tmerge((k * 1ll * i) % p, i);\n\t}\n\tll ans = 1;\n\tfor(int i = 0; i < p; i++) {\n\t\tif(getroot(i) == i) {\n\t\t\tans = (ans * 1ll * isok(k, sz[i], p)) % mod;\n\t\t}\n\t}\n\n\tprintf(\"%lld\", ans);\n\n\t#ifdef SOL\n\t{\n\t\tfflush(stdout);\n\t\tfprintf(stderr, \"%.3lf ms\\n\", 1000. * clock() / CLOCKS_PER_SEC);\n\t\tfflush(stderr);\n\t}\n\t#endif\n\treturn(0);\n}\n\n// by Andrey Kim\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "b72c16580f99f71a9dd02d71d76fdc49", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)\n#define forn(i, n) forr(i, 0, n)\n#define pb push_back\n#define fst first\n#define snd second\n#define SZ(x) ((int)x.size())\ntypedef long long ll;\n#define dforn(i, n) for(int i = n - 1; i >= 0; i--)\nconst int MAXN=-1;\nconst ll MOD=(ll)1e9+7;\n\nll p,k;\n\nint main() {\n //~ freopen(\"input.in\",\"r\",stdin);\n while(scanf(\"%lld%lld\",&p,&k)>=1){\n ll l=k==0?p-1:(k==1?p:-1);\n ll ans=1;\n if(l==-1){\n ll t=1,v=k;\n while(v!=1){\n v=(v*k)%p;t++;\n }\n l=p/t;\n }\n forn(i,l){\n ans=(ans*p)%MOD;\n }\n printf(\"%lld\\n\",ans);\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "b1e7ddff038ab0bb9b332f935841a2a8", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int N = 1000000 + 5;\nconst int MOD = (int)1e9 + 7;\n\nint a[N];\nbool vis[N];\nint p, k;\n\nint main()\n{\n cin >> p >> k;\n if (k == 0) {\n int ans = 1;\n for (int i = 1; i < p; ++i) {\n ans = (long long)ans * p % MOD;\n }\n cout << ans << endl;\n exit(0);\n }\n for (int i = 1; i < p; ++i) {\n a[i] = (long long)k * i % p;\n }\n int ans = 1;\n for (int i = 0; i < p; ++i) {\n if (!vis[i]) {\n int size = 0;\n int j = i;\n int tmp = 1;\n while (!vis[j]) {\n vis[j] = true;\n j = a[j];\n size++;\n tmp = (long long)tmp * k % p;\n }\n if (size == 1) {\n int cnt = k == 1 ? p : 1;\n ans = (long long)ans * cnt % MOD;\n } else {\n assert(tmp == 1);\n ans = (long long)ans * p % MOD;\n }\n }\n }\n cout << ans << endl;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "2bec2c7f9cf4e18e7d9b7cbd3ddf6916", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#define f first\n#define s second\n#define ll long long\n#define mp make_pair\n#define pb push_back\n\nusing namespace std;\n\ndouble eps = 1e-9;\nconst int INF = 1e9+7;\nconst int MAXN = int (2e6+7);\n\nint n, p, k, u[MAXN], ans = 1, mod = INF;\n\nvoid dfs(int v) {\n\tu[v] = 1;\n\tint x = (k * 1ll * v) % p;\n\tif(!u[x])\n\t\tdfs(x);\n}\n\nint main () {\n \t//freopen(\"input.in\", \"r\", stdin);\n \t//freopen(\"output.txt\", \"w\", stdout); \n \n cin >> p >> k;\n int i = 1;\n if(k == 1) {\n \ti = 0;\n }\n for(; i < p; i ++) {\n \tif(!u[i]) {\n \t\tdfs(i);\n \t\tans = (ans * 1ll * p) % mod;\n \t}\n }\n cout << ans;\n return 0;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "72e112f24709bd0c8b7417c9ef9f5ae0", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\nconst int mod7=1000000007;\n\nint main()\n{\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);\n #endif\n int mod,k;scanf(\"%d%d\",&mod,&k);\n int t=1,j;\n for (int i=1;i\n#include \n\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx\")\n\nusing namespace std;\n\n#define space ' '\n#define enter \"\\n\"\n#define fi first\n#define se second\n#define mp make_pair\n#define ALL(x) x.begin(), x.end()\n#define bits(x) __builtin_popcount(x)\n#define bitsll(x) __builtin_popcountll(x)\n#define crr(a) cerr << #a << \" = \" << a << \"\\n\"\n#define precision(a) fixed << setprecision(a)\n\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef set si;\ntypedef map mii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vii;\n\ninline bool isDigit (char c) {\n\treturn '0' <= c && c <= '9';\n}\n\ntemplate \ninline T sqr (T x) {\n\treturn x * x;\n}\n\ntemplate \ninline bool isSquare (T x) {\n\tT y = sqrt(x + 0.5);\n\treturn (y * y) == x;\n}\n\ntemplate \ninline T1 gcd (T1 a, T2 b) {\n\treturn b ? gcd(b, a % b) : a;\n}\n\ntemplate \ninline T1 eqMin (T1 & x, const T2 & y) {\n\tif (T1(y) < x)\n\t\treturn x = y;\n\treturn x;\n}\n\ntemplate \ninline T1 eqMax (T1 & x, const T2 & y) {\n\tif (T1(y) > x)\n\t\treturn x = y;\n\treturn x;\n}\n\ntemplate \ninline T1 min (const T1 & x, const T2 & y) {\n\treturn x < (T1) y ? x : (T1) y;\n}\n\ntemplate \ninline T1 max (T1 & x, const T2 & y) {\n\treturn x > (T1) y ? x : (T1) y;\n}\n\ntemplate \ninline T getint () {\n\tT x = 0, p = 1;\n\tchar ch;\n\tdo {\n\t\tch = getchar();\n\t} while (ch <= ' ');\n\tif (ch == '-')\n\t\tp = -1, ch = getchar();\n\twhile (ch >= '0' && ch <= '9')\n\t\tx = x * 10 + ch - '0', ch = getchar();\n\treturn x * p;\n}\n\ntemplate \nostream & operator << (ostream & os, const pair & p) {\n\treturn os << '(' << p.fi << \", \" << p.se << ')';\n}\n\ntemplate\nauto operator<<(ostream &os, const T &v) -> decltype(v.begin(), v.end(), declval()) {\n\tos << '{';\n\tbool was = false;\n\tfor (const auto &x : v) {\n\t\tif (was)\n\t\t\tos << \", \";\n\t\twas = true;\n\t\tos << x;\n\t}\n\tos << '}';\n\treturn os;\n}\n\ntemplate \nostream & operator << (ostream & os, const bitset & bit) {\n\tos << '/';\n\tfor (int i = 0; i < sz; i++)\n\t\tos << bit[i];\n\tos << '\\\\';\n\treturn os;\n}\n\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\nconst int INF = (int) 2e9;\nconst ll LINF = (ll) 2e18;\nconst int MOD = (int) 1e9 + 7;\nconst int MAXN = (int) 3e6 + 10;\n\nint p, k, t;\nbool used[MAXN];\n\nvoid dfs (int x) {\n\tused[x] = true;\n\tint to = ((ll) x * k) % p;\n\tif (!used[to]) {\n\t\tdfs(to);\n\t}\n}\n\nint main () {\n\tios_base::sync_with_stdio(false);\n\tcin >> p >> k;\n\tfor (int i = 0; i < p; i++) {\n\t\tif (!used[i]) {\n\t\t\tt++;\n\t\t\tdfs(i);\n\t\t}\n\t}\n\tll ans = 1;\n\tfor (int i = 1; i < t + (k == 1); i++) {\n\t\tans *= p;\n\t\tans %= MOD;\n\t}\n\tcout << ans;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "e518065ba3526672e23a432235328da2", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n/*断你妹。。*/\nusing namespace std;\ntypedef pair pr;\ntypedef long long ll;\n#define maxn 1000005\n#define mod 1000000007\n\nbool vis[maxn];\n\nll fastPow(ll a,int b){\n ll res=1;\n while(b){\n if (b&1) res=res*a%mod;\n b>>=1;\n a=a*a%mod;\n }\n return res;\n}\n\nint main(){\n ll p,k;\n scanf(\"%I64d%I64d\",&p,&k);\n if (k==0){\n printf(\"%I64d\\n\",fastPow(p,p-1));\n return 0;\n }\n if (k==1){\n printf(\"%I64d\\n\",fastPow(p,p));\n return 0;\n }\n int cnt=0;\n for (int i=1;i\n#include \n#include \nusing namespace std;\nint c[203], n, k[203][203], idx[203];\nbool co[203];\ninline bool done() {\n int i;\n for ( i = 1; i <= n; ++i ) {\n if ( !co[i] ) return 0;\n }\n return 1;\n}\nvoid complete( int x, int id[] ) {\n int i, kn = k[x][0];\n for ( i = 1; i <= kn; ++i ) {\n --id[k[x][i]];\n }\n}\nint deal( int s ) {\n memset( co, 0, sizeof( co ) );\n int idy[203];\n memcpy( idy, idx, sizeof( idx ) );\n int cnt = 0, i;\n do {\n bool flag = 1;\n while ( flag ) {\n flag = 0;\n for ( i = 1; i <= n; ++i ) {\n if ( c[i] == s ) {\n if ( !co[i] && !idy[i] ) {\n co[i] = 1;\n complete( i, idy );\n flag = 1;\n ++cnt;\n }\n }\n }\n }\n ++cnt;\n ++s;\n if ( s > 3 ) s = 1;\n } while ( !done() );\n return cnt - 1;\n}\nint main( int argc, char *argv[] )\n{\n int i;\n scanf( \"%d\", &n );\n for ( i = 1; i <= n; ++i ) scanf( \"%d\", c + i );\n int ki, j;\n memset( idx, 0, sizeof( idx ) );\n memset( k, 0, sizeof( k ) );\n for ( i = 1; i <= n; ++i ) {\n scanf( \"%d\", &ki );\n int t;\n for ( j = 1; j <= ki; ++j ) {\n scanf( \"%d\", &t );\n k[t][++k[t][0]] = i;\n ++idx[i];\n }\n }\n int tm, ma = 0x3f3f3f3f;\n //puts( \"##########\" );\n tm = deal( 1 );\n //printf( \"%d ++++++\\n\", tm );\n if ( ma > tm ) ma = tm;\n tm = deal( 2 );\n //printf( \"%d ########\\n\", tm );\n if ( ma > tm ) ma = tm;\n tm = deal( 3 );\n if ( ma > tm ) ma = tm;\n //printf( \"%d @@@@@@@@@\\n\", tm );\n cout<\nusing namespace std;\n\nint main()\n{\n string s;\n cin>>s;\n int n = s.size();\n int cnt4= 0, cnt7=0;\n int lucky = 1;\n for(int i = 0; i < n; ++i)\n {\n if(s[i] == '4')\n cnt4++;\n else if(s[i] == '7')\n cnt7++;\n else\n {\n lucky = 0;\n break;\n }\n }\n\n if(lucky && cnt4 == cnt7)\n {\n cout< s[i])\n {\n break;\n }\n }\n\n if(flag)\n {\n cout<\nusing namespace std;\n\n// #define multitest 1\n#ifdef WIN32\n#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__);\n#define pc(...) PC(#__VA_ARGS__, __VA_ARGS__);\ntemplate \nostream &operator<<(ostream &out, const pair &p)\n{\n\tout << '[' << p.first << \", \" << p.second << ']';\n\treturn out;\n}\ntemplate \nvoid PC(const char *name, Arg &&arg)\n{\n\twhile (*name == ',' || *name == ' ')\n\t\tname++;\n\tstd::cerr << name << \" { \";\n\tfor (const auto &v : arg)\n\t\tcerr << v << ' ';\n\tcerr << \" }\\n\";\n}\ntemplate \nvoid PC(const char *names, Arg1 &&arg1, Args &&... args)\n{\n\twhile (*names == ',' || *names == ' ')\n\t\tnames++;\n\tconst char *comma = strchr(names, ',');\n\tstd::cerr.write(names, comma - names) << \" { \";\n\tfor (const auto &v : arg1)\n\t\tcerr << v << ' ';\n\tcerr << \" }\\n\";\n\tPC(comma, args...);\n}\ntemplate \nvoid ZZ(const char *name, Arg1 &&arg1)\n{\n\tstd::cerr << name << \" = \" << arg1 << endl;\n}\ntemplate \nvoid ZZ(const char *names, Arg1 &&arg1, Args &&... args)\n{\n\tconst char *comma = strchr(names + 1, ',');\n\tstd::cerr.write(names, comma - names) << \" = \" << arg1;\n\tZZ(comma, args...);\n}\n#else\n#define db(...)\n#define pc(...)\n#endif\n\nusing ll = long long;\n#define f first\n#define s second\n#define pb push_back\nauto TimeStart = chrono::steady_clock::now();\n\nconst int nax = 2e5 + 10, mod = 1000000007;\n\nvoid solve(int caseNo)\n{\n\tvector> pos;\n\tint a, b;\n\tstring s;\n\tcin >> a >> b >> s;\n\tpos.pb({0, 0});\n\tfor (auto c : s)\n\t{\n\t\tauto b = pos.back();\n\t\tswitch (c)\n\t\t{\n\t\tcase 'U':\n\t\t\tb.s++;\n\t\t\tpos.pb(b);\n\t\t\tbreak;\n\t\tcase 'D':\n\t\t\tb.s--;\n\t\t\tpos.pb(b);\n\t\t\tbreak;\n\t\tcase 'L':\n\t\t\tb.f--;\n\t\t\tpos.pb(b);\n\t\t\tbreak;\n\t\tcase 'R':\n\t\t\tb.f++;\n\t\t\tpos.pb(b);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\t}\n\tauto back = pos.back();\n\tint x = back.f, y = back.s;\n\tdb(x, y);\n\tpos.pop_back();\n\tfor (auto elem : pos)\n\t{\n\t\tint a1 = a - elem.f;\n\t\tint b1 = b - elem.s;\n\t\tdb(a1, b1);\n\t\tif (x == 0)\n\t\t{\n\t\t\tif (y == 0)\n\t\t\t{\n\t\t\t\tif (a1 == 0 && b1 == 0)\n\t\t\t\t{\n\t\t\t\t\tcout << \"Yes\\n\";\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (a1 == 0 && b1 % y == 0)\n\t\t\t\t{\n\t\t\t\t\tif (b1 / y >= 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tcout << \"Yes\\n\";\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if (y == 0)\n\t\t{\n\t\t\tif (a1 % x == 0 && b1 == 0)\n\t\t\t{\n\t\t\t\tif (a1 / x >= 0)\n\t\t\t\t{\n\t\t\t\t\tcout << \"Yes\\n\";\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (a1 % x == 0 && b1 % y == 0 && ((a1 / x) == (b1 / y)))\n\t\t\t{\n\t\t\t\tif (a1 / x >= 0)\n\t\t\t\t{\n\t\t\t\t\tcout << \"Yes\\n\";\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << \"No\\n\";\n}\n\nint main()\n{\n#ifndef WIN32\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n#endif\n\tint t = 1;\n#ifdef multitest\n\tcin >> t;\n#endif\n\tfor (int i = 0; i < t; ++i)\n\t\tsolve(i);\n#ifdef TIME\n\tcerr << \"\\n\\nTime elapsed: \" << chrono::duration(chrono::steady_clock::now() - TimeStart).count() << \" seconds.\\n\";\n#endif\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation", "binary search"], "code_uid": "64007e321215e76936af0cd368c0f169", "src_uid": "5d6212e28c7942e9ff4d096938b782bf", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define ll long long\n#define pb push_back\n#define r_0 return 0\n#define fr first\n#define se second\n#define TIME ios_base::sync_with_stdio(0);\nusing namespace std;\nconst ll N=1e5 ;\nll n , b , c , d , ans , m , cnt , x , used[N];\nvector< string > v , v1 ;\nstring s ,s1 ;\nsetst;\nmap mp ;\nchar q ;\nint main()\n{\n cin >> s ;\n cin >> x ;\n for( ll i =1 ;i <= x; i ++ )\n {\n cin >> s1 ;\n v1.pb(s1) ;\n if( s1[1] == s[0] )\n ans ++ ;\n if( s == s1 )\n cnt ++ ;\n }\n if( cnt >= 1 ){\n cout << \"YES\" ;r_0 ; }\n if( ans >= 1 )\n for( ll i = 0 ;i < v1.size() ; i ++ )\n if( v1[i][0] == s[1])\n {\n cout << \"YES\" ; r_0 ;\n }\n cout << \"NO\" ;\n}\n\n\n", "lang_cluster": "C++", "tags": ["brute force", "strings", "implementation"], "code_uid": "8ed6759f49232ea058491d67d7a99503", "src_uid": "cad8283914da16bc41680857bd20fe9f", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\n * 429C. Guess the Tree.cpp\n *\n * Created on: Jul 20, 2015\n * Author: Osama Hatem\n */\n\n#include \n#include \n\nusing namespace std;\n\nint N, c[25];\nmap dp;\n\nbool solve(int activeMask) {\n int idx = N;\n for (int i = N - 1; !(activeMask & (1 << i)) && i >= 0; i--)\n idx = i;\n if (idx == N)\n return activeMask == 1 << (N - 1);\n if (dp.count(activeMask))\n return dp[activeMask];\n bool ret = false;\n vector active;\n for (int i = 0; i < N; i++)\n if ((1 << i) & activeMask)\n active.push_back(i);\n int sz = active.size();\n for (int i = 3; !ret && i < (1 << sz); i++) {\n if (!(i & (i - 1)))\n continue;\n int tempSum = 0, newMask = activeMask | (1 << idx);\n for (int j = 0; j < sz; j++)\n if (i & (1 << j))\n tempSum += c[active[j]], newMask ^= 1 << active[j];\n if (tempSum != c[idx] - 1)\n continue;\n ret |= solve(newMask);\n }\n return dp[activeMask] = ret;\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen(\"in.in\", \"r\", stdin);\n// freopen(\"out.out\", \"w\", stdout);\n#endif\n\n cin >> N;\n for (int i = 0; i < N; i++)\n cin >> c[i];\n sort(c, c + N);\n int mask = 0, cur = 0;\n for (; cur < N && c[cur] == 1; cur++)\n mask |= 1 << cur;\n cout << (solve(mask) ? \"YES\" : \"NO\") << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "dp", "bitmasks", "greedy", "trees"], "code_uid": "dc8cbb7985b3c57f6843f5b29933be82", "src_uid": "ed0925cfaee961a3ceebd13b3c96a15a", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\n#define maxN 100005\n\nusing namespace std;\n\nstring s;\nint dd[1000], n, b[20];\nchar ch[maxN], a[maxN];\n\n\nvoid process() {\n cin >> s; n = s.length();\n for(int i = 0; i < n; i++) ch[i + 1] = s[i];\n\n // dem\n int sl = 0, demquest = 0;\n for(int i = 1; i <= n; i++)\n if ('A' <= ch[i] && ch[i] <= 'J' && dd[ch[i] - 'A' + 1] == 0) {\n ++sl; dd[ch[i] - 'A' + 1] = 1;\n }\n else\n if (ch[i] == '?') ++demquest;\n\n for(int i = 1; i <= sl; i++) b[i] = 10 - i + 1;\n\n int mul = 1;\n if (ch[1] == '?') mul = 9, demquest--; else\n if ('A' <= ch[1] && ch[1] <= 'J') b[1] = 9;\n\n // tinh\n long long res = 1;\n for(int i = 1; i <= sl; i++) res *= b[i];\n res *= mul;\n cout << res;\n for(int i = 1; i <= demquest; i++) cout << 0;\n cout << endl;\n}\n\nint main() {\n //freopen(\"inp.txt\", \"r\", stdin); freopen(\"out.txt\", \"w\", stdout);\n process();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "b94c78e555b498b13aac870494feeb71", "src_uid": "d3c10d1b1a17ad018359e2dab80d2b82", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define modulo 1000000007\n#define pi 3.1415926535897932384\n\ntypedef long long int ll;\ntypedef double d;\ntypedef string str;\ntypedef char ch;\n\nvoid AWAD(){\n std::ios::sync_with_stdio(false);\n cin.tie(0);\n}\nint gcd(int a, int b) {\n return b == 0 ? a : gcd(b, a % b);\n}\nbool cmp (ll a, ll b){\n \n if(a==0 && b>a)\n return false;\n if(a> a >> b >> c;\n int a1[12] = {0};\n for(int i = 0; i < 12; i++){\n \n if(a==s[i] || b == s[i] || c == s[i]){\n a1[i] = 1;\n }\n }\n for(int i = 0; i < 12; i++){\n if(a1[i]){\n if(a1[(i+4)%12] && a1[(i+7)%12]){\n cout << \"major\\n\";\n return 0;\n }\n if(a1[(i+3)%12] && a1[(i+7)%12]){\n cout << \"minor\\n\";\n return 0;\n }\n }\n }\n \n cout << \"strange\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "c834f722705820ec219b5fdb6192b50c", "src_uid": "6aa83c2f6e095848bc63aba7d013aa58", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \nusing namespace std;\n#define TR(i,v) for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)\n#define DEBUG(x) cout << #x << \" = \" << x << endl;\n#define SIZE(p) (int)(p).size()\n#define MP(a, b) make_pair((a), (b))\n#define ALL(p) (p).begin(), (p).end()\n#define rep(i, n) for(int (i)=0; (i)<(int)(n); ++(i))\n#define REP(i, a, n) for(int (i)=(a); (i)<(int)(n); ++(i))\n#define FOR(i, a, b) for(int (i)=(int)(a); (i)<=(int)(b); ++(i))\n#define FORD(i, b, a) for(int (i)=(int)(b); (i)>=(int)(a); --(i))\n#define CLR(x, y) memset((x), (y), sizeof((x)))\ntypedef long long LL;\ntypedef pair pii;\nconst int mod=(int)1e9+7;\nint f[200005],f_inv[200005];\ninline int add(int x,int y) {\n x+=y;\n if(x>=mod) x-=mod;\n return x;\n}\ninline int sub(int x,int y) {\n x-=y,x%=mod;\n if(x<0) x+=mod;\n return x;\n}\ninline int mul(LL x,LL y) {\n x*=y,x%=mod;\n return x;\n}\nint pw(int a,int p) {\n int r=1;\n for(;p;p>>=1,a=mul(a,a)) if(p&1)\n r=mul(r,a);\n return r;\n}\nvoid pre() {\n f[0]=1;\n FOR(i,1,200000)\n f[i]=(LL)i*f[i-1]%mod;\n f_inv[200000]=pw(f[200000],mod-2);\n FORD(i,200000-1,0)\n f_inv[i]=(LL)(i+1)*f_inv[i+1]%mod; \n}\nint C(int n,int m) {\n int c=f[n];\n c=mul(c,f_inv[m]);\n c=mul(c,f_inv[n-m]);\n return c;\n}\nint g(int n,int a) { \n if(n==0) return 0;\n if(n==1) { \n return a==0; \n }\n if(a) return sub(C(n-1,a-1),g(n-1,a-1));\n return 0;\n}\nint main(int argc, char const *argv[]) {\n#ifndef ONLINE_JUDGE\n freopen(\"D.in\", \"r\", stdin);\n // freopen(\"out\", \"w\", stdout);\n#endif\n pre();\n int n,m,t; cin>>n>>m>>t;\n int a=n; n=n+m; \n int res=g(n,a); \n // DEBUG(res);\n // DEBUG(g(1,0));\n // DEBUG(res);\n if(t==0) res=sub(C(n,a),res);\n printf(\"%d\\n\",res);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "combinatorics", "number theory"], "code_uid": "72f964fe0c0a1915544ad08bca43e564", "src_uid": "066dd9e6091238edf2912a6af4d29e7f", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nstring temp = \"ACTG\";\n\nint func(string s) {\n\tint answer = int(1e6);\n\tfor(int i = 0; i < (int)s.size() - 3; i++) {\n\t\tstring new_s = \"\";\n\t\tnew_s += s[ i ];\n\t\tnew_s += s[ i + 1];\n\t\tnew_s += s[ i + 2 ];\n\t\tnew_s += s[ i + 3 ];\n\n\t\tint nedo = 0;\n\t\tfor(int j = 0; j < temp.size(); j++) {\n\t\t\tif(temp [ j ] == new_s[ j ]) continue;\n\n\t\t\tnedo += min(26 - abs(temp[ j ] - new_s[ j ]), abs(temp[ j ] - new_s[ j ]));\n\t\t}\n\t\tanswer = min(answer, nedo);\n\t}\n\n\treturn answer;\n}\n\nint main() {\n\tint n;\n\tscanf(\"%i\", &n);\n\n\tstring s;\n\tcin >> s;\n\n\tprintf(\"%i \", func(s));\n}", "lang_cluster": "C++", "tags": ["brute force", "strings"], "code_uid": "b49184c557d5638092d8a3dc5bb24359", "src_uid": "ee4f88abe4c9fa776abd15c5f3a94543", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define INF 100000007\n#define MP(a, b) make_pair(a, b)\ntypedef long long ll;\nconst int maxn = 100010;\nusing namespace std;\ntypedef pair P;\nint n, d, a[maxn], b[maxn], c[maxn], s[50], bb[maxn], num;\nll x;\nll getNextX()\n{\n x = (x * 37 + 10007) % 1000000007;\n return x;\n}\nvoid init()\n{\n ll i;\n for(i = 0; i < n;i++)\n {\n a[i] = i + 1;\n }\n for(i = 0; i < n; i++)\n {\n swap(a[i], a[getNextX() % (i + 1)]);\n }\n for(i = 0; i < n; i++)\n {\n if (i < d)\n b[i] = 1;\n else\n b[i] = 0;\n }\n for(i = 0; i < n; i++)\n {\n swap(b[i], b[getNextX() % (i + 1)]);\n }\n}\n#define NN 30\nint main()\n{\n// freopen(\"in.txt\", \"r\", stdin);\n// freopen(\"out.txt\", \"w\", stdout);\n int i, j, k;\n scanf(\"%d%d%I64d\", &n, &d, &x);\n init();\n for(i = 0; i < n; i++)\n if(b[i])\n bb[num++] = i;\n if(n < NN + 20)\n {\n for(i = 0; i < n; i++)\n {\n for(j = 0, k = 0; j < num && bb[j] <= i; j++)\n k = max(k, a[i - bb[j]]);\n printf(\"%d\\n\", k);\n }\n }\n else\n {\n for(i = 0; i < n; i++)\n if(n - a[i] <= NN)\n s[n - a[i]] = i;\n for(i = 0; i < n; i++)\n {\n for(j = 0; j <= NN; j++)\n if( s[j] <= i && b[i - s[j]] )\n {\n printf(\"%d\\n\", n - j);\n break;\n }\n if(j <= NN)\n continue;\n for(j = 0, k = 0; j < num&& bb[j] <= i; j++)\n k = max(k, a[i - bb[j]]);\n printf(\"%d\\n\", k);\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["probabilities"], "code_uid": "4ac21a70f773f90176899b79db4e4196", "src_uid": "948ae7a0189ada07c8c67a1757f691f0", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nconst int maxn = 200010;\nint col[maxn],k[maxn],cnt[maxn];//cnt 当前位置的颜色数量 \nset q;\nint main()\n{\n\tint n,m;\n\tcin>>n>>m;\n\tfor(int i=1;i<=n;i++){\n\t\tscanf(\"%d\",&col[i]);\n\t}\n\tint sum=0;\n\tfor(int i=1;i<=m;i++){\n\t\tscanf(\"%d\",&k[i]);\n\t\tif(k[i]>0)\n\t\tq.insert(i);\n\t\tsum+=k[i];\n\t}\n\tint l=1,r;\n\tint ans=1e9;\n\tfor(r=1;r<=n;r++){\n\t\tif(++cnt[col[r]]==k[col[r]]) q.erase(col[r]);\n\t\tif(q.empty()){//找到所有颜色且长度>=k[i] \n\t\t\twhile(l<=r&&cnt[col[l]]>k[col[l]])\tcnt[col[l++]]--;\n\t\t\tans=min(ans,r-l+1-sum);\n\t\t}\n\t}\n//\tcout<=1)\n\t\tprintf(\"NO\\n\");\n\telse\n\t\tprintf(\"YES\\n\");\n\t\n\t\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5d6a5ee2895b5e368672eaf434e2c978", "src_uid": "59f40d9f35e5fe402112214b42b682b5", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define R(a,b) for(i=a;i<=b;i++)\nlong long i,a,b,c,n,x,C,M=1e9+7;\nmain()\n{\n scanf(\"%d\",&n);\n R(1,n)scanf(\"%d\",&x),C+=x==1;\n a=b=c=1;\n R(2,C)c=(b+a*(i-1)%M)%M,a=b,b=c;\n R(C+1,n)c=c*i%M;\n printf(\"%d\\n\",c);\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp"], "code_uid": "846ef9b2f9c1540766faf1040ce17ff8", "src_uid": "91e8dbe94273e255182aca0f94117bb9", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// In the Name of Allah\n// Ya Ali!\n#include \n#pragma GCC optimize (\"O2\")\n#pragma GCC optimize (\"unroll-loops\")\n\nusing namespace std;\n\nconst int maxn = 402 ;\n\nint n , dp[maxn][maxn] , mod = 1e9 + 7 , c[maxn] , c2[maxn][maxn] ;\n\nint main()\n{\n//Let's TOF\nscanf(\"%d\" , &n) ;\n\nfor(int i = 1 ;i <= n+1 ; i ++)c[i] = i*(i-1) ;\n\nfor(int i = 1 ;i <= n+1 ; i ++)\n for(int j = 0 ; j <= i ; j ++)\n c2[i][j] = j*(i-j);\n\ndp[1][1] = 1;\ndp[1][0] = 1;\n\nfor(int i = 2 ; i <= n ; i ++){\n dp[i][0] = 1;\n for(int j = 1 ; j <= n ; j ++){\n int sum1 = 0 , sum2 = 0 , sum3 = 0;\n for(int k = 0 ; k < j ; k ++){\n dp[i][j] = (dp[i][j] + (dp[i-1][k]*1ll*dp[i-1][j-k-1])%mod)%mod ;\n }\n for(int k = 0 ; k <= j ; k ++){\n sum2 = (sum2 + (dp[i-1][k]*1ll*dp[i-1][j-k])%mod)%mod;\n }\n for(int k = 1 ; k <= j+1 ; k ++){\n sum1 = (sum1 + (((dp[i-1][k]*1ll*dp[i-1][j+1-k])%mod)*1ll*(c[k]+c2[j+1][k]))%mod)%mod ;\n }\n dp[i][j] = (dp[i][j] + (sum1*1ll*2)%mod)%mod;\n dp[i][j] = (dp[i][j] + (sum2*1ll*((2*j)+1))%mod)%mod;\n }\n}\n\nprintf(\"%d\" , dp[n][1]%mod) ;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "trees", "graphs"], "code_uid": "b11ab20c7bc429dcfd090feb1190b685", "src_uid": "fda761834f7b5800f540178ac1c79fca", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*====Corycle====*/\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ll long long\n#define ld long double\n#define ull unsigned ll\nusing namespace std;\nconst int inf=0x3f3f3f3f;\nconst int N=1e5+5;\nll read(){\n\tll s=0,f=1;char c=getchar();\n\twhile(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}\n\twhile(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}\n\treturn s*f;\n}\nll n,X,tot,cnt1,cnt2,num[N],p1[N],p2[N];\nll Multi(ll x,ll y,ll mod){return ((ull)x*y-(ull)((ld)x/mod*y)*mod+mod)%mod;}\nll Pow(ll x,ll y,ll mod){ll ans=1;x%=mod;while(y){if(y&1)ans=Multi(ans,x,mod);x=Multi(x,x,mod);y>>=1;}return ans;}\nvoid Divide(ll n){for(ll i=1;i*i<=n;i++)if(n%i==0){num[++tot]=i;if(i*i!=n)num[++tot]=n/i;};sort(num+1,num+tot+1);}\nvoid GetPrime(ll n,ll p[],ll &tot){for(ll i=2;i*i<=n;i++)if(n%i==0){p[++tot]=i;while(n%i==0)n/=i;}if(n!=1)p[++tot]=n;}\nll Phi(ll n){ll phi=n;for(int i=1;i<=cnt1;i++)if(n%p1[i]==0)phi=phi/p1[i]*(p1[i]-1);return phi;}\nll Solve(ll n){\n\tll phi=Phi(n),ans=0;\n\tif(phi!=1)GetPrime(phi,p2,cnt2);\n\tfor(int i=1;i<=tot;i++){\n\t\tll F=Phi(num[i]),L=F;\n\t\tfor(int j=1;j<=cnt2;j++){\n\t\t\twhile(L%p2[j]==0&&Pow(X,L/p2[j],num[i])==1)L/=p2[j];\n\t\t}\n\t\tans+=F/L;\n\t}\n\treturn ans;\n}\nint main(){\n//\tfreopen(\"_.in\",\"r\",stdin);\n//\tfreopen(\"_.out\",\"w\",stdout);\n\tn=read();X=read();Divide(n);GetPrime(n,p1,cnt1);printf(\"%lld\",Solve(n));\n\treturn 0;\n}\n/* \"https://blog.csdn.net/qq_34454069/article/details/82963027\" */", "lang_cluster": "C++", "tags": ["math", "number theory", "bitmasks"], "code_uid": "822323f7edf95ca772821c5a42ff2d77", "src_uid": "c2dd6de750812d6213c770b3587d8fcb", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define INF 999999999\n\nstruct el {\n int value;\n int ind;\n};\n\nbool cmp (el a, el b) {\n return a.value < b.value;\n}\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n \n \n int n, val; cin >> n;\n vector v(n);\n map m;\n \n for (int i = 0; i < n; i++) {\n cin >> val;\n el e = {val, i};\n v[i] = e;\n m[i] = val;\n }\n \n sort(v.begin(), v.end(), cmp);\n \n long long int ans = 0;\n for (int i = 0; i < n; i++) {\n map::const_iterator pos = m.find(v[i].ind);\n if (pos != m.begin() && pos != --m.end())\n ans += min(prev(pos)->second, next(pos)->second);\n else if (m.size() > 2) ans += v[i].value;\n m.erase(pos);\n }\n \n cout << ans;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "741dafc3baa9f8e823c41302a2175818", "src_uid": "e7e0f9069166fe992abe6f0e19caa6a1", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\nusing namespace std;\n#define ll long long int\n#define pb push_back\n#define mod 1000000007\n#define inarr(arr,n); for(ll i=0;i> arr[i];\n#define outarr(arr,n); for(ll i=0;i b; i--)\n#define mk make_pair\n#define setvector(vname,value,size); for(int i=0;in)\n {\n ini_sum-=(n-right)*(n-right);\n f1=1;\n }\n if(up<1)\n {\n ini_sum-=((up-1)*(up-1));\n if(f)\n {\n ll diff = abs(x-1)+abs(y-1)+1;\n if(diffn)\n {\n ini_sum-=(down-n)*(down-n);\n if(f)\n {\n ll diff = abs(x-n) + abs(y-1)+1;\n if(diff>n>>x>>y>>c;\n t = bin(0,100000000);\n cout<<(t==-1?ind:t)<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main()\n{\n long long ans = 0;\n long long w, h;\n cin >> w >> h;\n int a = 1, b = 1;\n while(b + b <= h)\n {\n long long z = (w - a * 2 + 1) * (h - b * 2 + 1);\n ans += z;\n// cout << \"a \" << a << \" b \" << b << \" z \" << z <<\" ans \"<< ans << endl;\n if(a + a + 2 <= w)\n {\n a += 1;\n }\n else\n {\n a = 1;\n b ++;\n }\n }\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "b2d5272d2dc60f45c6ae62253ceaa0a9", "src_uid": "42454dcf7d073bf12030367eb094eb8c", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nstring cube2[] = {\n\t\"012345\",\n\t\"452310\",\n\t\"014532\",\n\t\"231045\",\n};\n\nset s;\n\nstring rot(const string &str, short dir) {\n\tstring tmp(str);\n\tfor (int u=0; u<6; u++) {\n\t\ttmp[cube2[dir][u] - '0'] = str[u];\n\t}\n\treturn tmp;\n}\n\nvoid addAll2(const string &str) {\n\tfor (int i=0; i<4; i++) {\n\t\tfor (int u=0; u<4; u++) {\n\t\t\tfor (int t=0; t<4; t++) {\n\t\t\t\tfor (int y=0; y<4; y++) {\n\t\t\t\t\tstring tmp = rot(str, i);\n\t\t\t\t\ttmp = rot(tmp, u);\n\t\t\t\t\ttmp = rot(tmp, t);\n\t\t\t\t\ttmp = rot(tmp, y);\n\t\t\t\t\ts.insert(tmp);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nint main() {\n // freopen(\"input.txt\", \"r\", stdin);\n // freopen(\"output.txt\", \"w\", stdout);\n\n\tstring str; cin >> str;\n\n\tsort(str.begin(), str.end());\n\n\tint sum = 0;\n\tdo {\n\t\tif (s.find(str) == s.end())\n\t\t\t++sum;\n\n\t\taddAll2(str);\n\t} while (next_permutation(str.begin(), str.end()));\n\n\tcout << sum << endl;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "034d103cd54b1923552906ea45e748bd", "src_uid": "8176c709c774fa87ca0e45a5a502a409", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// https://codeforces.com/problemset/problem/1096/E\n#define DECIMAL_DIGITS 10\n#define STATIC_MOD 998244353\n\n#ifdef BTK\n/**/\n# include \"Template.hpp\"\n# include \"num/ModInt.hpp\"\n/**/\n#else\n/**/\n/* #region auto includes */\n/* #region stl */\n/**/\n# include \n# include \n# include \nusing namespace std;\n/**/\n/* #endregion */\n/* #region template/IncludeSTL.hpp*/\n/**\n * @file IncludeSTL.hpp\n * @author btk\n * @brief 標準ライブラリをincludeするだけ\n * @version 0.1\n * @date 2019-07-21\n *\n * @copyright Copyright (c) 2019\n *\n */\n\n/**/\n# pragma once\n/**/\n\n\n/**/\n# include \n# include \n# include \nusing namespace std;\n/**/\n/* #endregion */\n/* #region template/Macro.hpp*/\n/**\n * @file Macro.hpp\n * @author btk\n * @brief マクロとか,LLとか\n * @version 0.1\n * @date 2019-07-13\n *\n * @copyright Copyright (c) 2019\n *\n */\n\n\n//! LL\nusing LL = long long;\n\n/**\n * @def DEBUG\n * @brief デバッグ用のif文 提出時はif(0)で実行されない\n */\n\n/**/\n# ifdef BTK\n# define DEBUG if (1)\n# else\n# ifdef CIN_ONLY\n# define FAST_IO\n# endif\n# define DEBUG if (0)\n# endif\n/**\n * @def ALL(v)\n * @brief\n * ALLマクロ\n */\n# define ALL(v) (v).begin(), (v).end()\n\n/**\n * @def REC(ret, ...)\n * @brief\n * 再帰ラムダをするためのマクロ\n */\n# define REC(ret, ...) std::function\n\n/**\n * @def VAR_NAME(var)\n * @brief 変数名を取得する\n */\n# define VAR_NAME(var) # var\n\n/**\n * @brief\n * rangeで生まれる使わない変数を消す用(警告消し)\n */\ntemplate \ninline T& unused_var(T& v) {\n return v;\n}\n/* #endregion */\n/* #region template/IO.hpp*/\n/**\n * @file IO.hpp\n * @author btk\n * @brief cin高速化とか,出力の小数桁固定とか\n * @version 0.1\n * @date 2019-07-13\n *\n * @copyright Copyright (c) 2019\n */\n\n\n/**\n * @brief 入出力の設定を行うための構造体\n */\nstruct cww {\n /**\n * @brief Construct a new cww::cww object\n * @details\n * CIN_ONLYを定義すると,submit時にcinとscanfの同期を切る設定が走る\n * DECIMAL_DIGITSを定義すると,doubleの出力時指定した桁数分小数部を吐くようになる\n */\n cww() {\n# ifdef FAST_IO\n ios::sync_with_stdio(false);\n cin.tie(0);\n# endif\n# ifdef DECIMAL_DIGITS\n cout << fixed;\n cout << setprecision(DECIMAL_DIGITS);\n# endif\n }\n};\n\n//! 入出力設定構造体を実体化\ncww star;\n\n/**\n * @brief\n * vectorに直接cin流すためのやつ\n * @tparam T\n * @param is\n * @param v\n * @return istream&\n */\ntemplate \nstd::istream& operator>>(std::istream& is, std::vector& v) {\n for (auto& it : v) is >> it;\n return is;\n}\n/* #endregion */\n/* #region template/Loop.hpp*/\n/**\n * @file Loop.hpp\n * @author btk\n * @brief rangeとかループ系のクラス\n * @version 0.1\n * @date 2019-07-13\n *\n * @copyright Copyright (c) 2019\n *\n */\n\n\n/**\n * @brief\n * rangeを逆向きに操作したいとき用\n * @details\n * ループの範囲は[bg,ed)なので注意\n * @see range\n */\nclass reverse_range {\n private:\n struct I {\n int x;\n int operator*() { return x - 1; }\n bool operator!=(I& lhs) { return x > lhs.x; }\n void operator++() { --x; }\n };\n I i, n;\n\n public:\n /**\n * @brief Construct a new reverse range object\n *\n * @param n\n */\n reverse_range(int n) : i({0}), n({n}) {}\n /**\n * @brief Construct a new reverse range object\n *\n * @param i\n * @param n\n */\n reverse_range(int i, int n) : i({i}), n({n}) {}\n /**\n * @brief\n * begin iterator\n * @return I&\n */\n I& begin() { return n; }\n /**\n * @brief\n * end iterator\n * @return I&\n */\n I& end() { return i; }\n};\n/**\n * @brief\n * python みたいな range-based for を実現\n * @details\n * ループの範囲は[bg,ed)なので注意\n * !つけると逆向きにループが回る (reverse_range)\n * 空間計算量はO(1)\n * 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい\n */\nclass range {\n private:\n struct I {\n int x;\n int operator*() { return x; }\n bool operator!=(I& lhs) { return x < lhs.x; }\n void operator++() { ++x; }\n };\n I i, n;\n\n public:\n /**\n * @brief Construct a new range object\n *\n * @param n\n */\n range(int n) : i({0}), n({n}) {}\n /**\n * @brief Construct a new range object\n *\n * @param i\n * @param n\n */\n range(int i, int n) : i({i}), n({n}) {}\n /**\n * @brief\n * begin iterator\n * @return I&\n */\n I& begin() { return i; }\n /**\n * @brief\n * end iterator\n * @return I&\n */\n I& end() { return n; }\n /**\n * @brief\n * 逆向きに参照するrange(reverse_rangeを取得できるs)\n * @return reverse_range\n */\n reverse_range operator!() { return reverse_range(*i, *n); }\n};\n/* #endregion */\n/* #region template/MinMaxOperation.hpp*/\n/**\n * @file MinMaxOperation.hpp\n * @author btk\n * @brief 最大値とか最小値を求める\n * @version 0.1\n * @date 2019-07-04\n *\n * @copyright Copyright (c) 2019\n *\n */\n\n/**\n * @brief 2項の最小値求める\n *\n * @tparam T\n */\ntemplate \nstruct min_op {\n /**\n * @brief 本体\n *\n * @param l\n * @param r\n * @return T\n */\n static T exec(const T l, const T r) { return l < r ? l : r; }\n};\n\n/**\n * @brief 2項の最大値求める\n *\n * @tparam T\n */\ntemplate \nstruct max_op {\n /**\n * @brief 本体\n *\n * @param l\n * @param r\n * @return T\n */\n static T exec(const T l, const T r) { return l > r ? l : r; }\n};\n\n/**\n * @brief テンプレート再帰の末尾\n *\n * @tparam F 二項演算\n * @tparam T\n * @param v\n * @return T\n */\ntemplate \ninline T multi_op(T&& v) {\n return v;\n}\n\n/**\n * @brief 複数項における演算の結果を返す\n *\n * @tparam F\n * @tparam T\n * @tparam Ts\n * @param head\n * @param tail\n * @return T\n */\ntemplate \ninline T multi_op(const T head, Ts&&... tail) {\n return F::exec(head, multi_op(tail...));\n}\n\n/**\n * @brief 複数項の最小値\n * @see multi_op\n * @tparam T\n * @tparam Ts\n * @param head\n * @param tail\n * @return T\n */\ntemplate \ninline T multi_min(T&& head, Ts&&... tail) {\n return multi_op>(head, tail...);\n}\n\n/**\n * @brief 複数項の最大値\n * @see multi_op\n * @tparam T\n * @tparam Ts\n * @param head\n * @param tail\n * @return T\n */\ntemplate \ninline T multi_max(T&& head, Ts&&... tail) {\n return multi_op>(head, tail...);\n}\n\n/**\n * @brief\n * \b先頭の値をFで参照する関数に基づいて変更できたらする\n * @tparam F\n * @tparam T\n * @tparam Ts\n * @param target\n * @param candidates\n * @return true\n * @return false\n */\ntemplate \ninline bool ch_op(T& target, Ts&&... candidates) {\n const T old = target;\n target = multi_op(target, candidates...);\n return old != target;\n}\n\n/**\n * @brief change min\n * @tparam T 型\n * @param target 変更する値\n * @param candidates\n * @return 更新があればtrue\n */\ntemplate \ninline bool chmin(T& target, Ts&&... candidates) {\n return ch_op>(target, candidates...);\n}\n\n/**\n * @brief chminのmax版\n * @see chmin\n * @tparam T 型\n * @param target 変更する値\n * @param candidates\n * @return 更新があればtrue\n */\ntemplate \ninline bool chmax(T& target, Ts&&... candidates) {\n return ch_op>(target, candidates...);\n}\n/* #endregion */\n/* #region template/Random.hpp*/\n/**\n * @file Random.hpp\n * @author btk\n * @brief 乱数生成系\n * @version 0.1\n * @date 2019-07-13\n * @copyright Copyright (c) 2019\n */\n\n\n//! 乱数のシード値をプロセスIDとして取得\nconst pid_t pid = getpid();\n\n/**\n * @brief XorShift32の実装\n */\nclass XorShift32 {\n private:\n //! XorShiftの現在の値\n unsigned value;\n\n /**\n * @brief XorShift32のアルゴリズムに基づいて value を更新\n */\n inline void update() {\n value = value ^ (value << 13);\n value = value ^ (value >> 17);\n value = value ^ (value << 5);\n }\n\n /**\n * @brief 値を更新し,更新前の値を返却\n * @return unsigned 呼び出し時の value を用いる\n */\n inline unsigned get() {\n unsigned v = value;\n update();\n return v;\n }\n\n public:\n /**\n * @brief [0, 2^bit) の範囲の乱数値を取り出す\n * @tparam デフォルトは31\n * @return int\n */\n template \n inline int next_int() {\n return (int)(get() >> (32 - bit));\n }\n\n /**\n * @brief [-2^bit,2^bit)の範囲の乱数値を取り出す\n * @tparam デフォルトは31\n * @return int\n */\n template \n inline int next_signed() {\n unsigned v = get();\n return (int)((v >> (31 - bit)) - (1 << (bit)));\n }\n\n /**\n * @brief next_int呼び出し時の最大値を取得\n * @tparam 31\n * @return int\n */\n template \n inline int range_max() {\n return (int)((1u << bit) - 1);\n };\n\n /**\n * @brief Construct a new XorShift32 object\n * @param seed\n * @details 初期シードをvalueとするXorShift32のインスタンスを生成\n */\n XorShift32(const unsigned seed) {\n value = seed;\n update();\n }\n\n /**\n * @brief Construct a new XorShift 32 object\n * @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成\n */\n XorShift32() : XorShift32(pid) {}\n};\n/* #endregion */\n/* #region Template.hpp*/\n/**\n * @file Template.hpp\n * @brief 競技プログラミング用テンプレート\n * @author btk15049\n * @date 2019/05/02\n */\n\n\n/* #endregion */\n/* #region num/ModInt.hpp*/\n\n# include \n\n/**\n * @file ModInt.hpp\n * @brief mod構造体\n * @author btk15049\n * @date 2019/03/08\n * @details\n * \\todo verifyが足りない\n * verify: CSA12E,RUPC day3 F\n */\n\n//! [WARNING!] mod が入力で与えられる場合はconstexprを外す\n# ifdef STATIC_MOD\nconstexpr int mod = STATIC_MOD;\n# else\nint mod;\n# endif\n\n/**\n * @brief mod構造体\n * @details\n * 整数をラップして,常に保持されているデータがmodされた状態になるよう管理.\n */\nclass ModInt {\n private:\n //! 中身\n int x;\n\n public:\n /**\n * @brief ゲッター\n * @details 出力時などは \"cout << *ret << endl;\"のようにやるとよい.\n */\n long long operator*() const { return x; }\n /**\n * @brief デフォルトコンストラクタ.0で初期化される.\n */\n ModInt() { x = 0; }\n\n /**\n * @brief intからのコンストラクタ\n * @param[in] y 設定したい値\n * @details\n * modをとらないので高速.ただしmodより大きい値や負の数を入れると事故るので注意.\n */\n ModInt(const int y) { x = y; }\n\n /**\n * @brief long longからのコンストラクタ\n * @param[in] y 設定したい値\n * @details 毎回modを取るので低速.\n */\n ModInt(const long long y) { x = (int)((mod + y % mod) % mod); }\n\n /**\n * @brief ModIntからの代入演算子\n * @param[in] o 設定したい値\n * @details 高速\n */\n ModInt(const ModInt& o) { this->x = *o; }\n /**\n * @brief 整数から高速にModIntを作りたいときに使う\n * @param[in] x 設定したい値\n * @details xが[0,mod)であることが保証されてないと正しく動かない.\n */\n static inline ModInt raw(const long long x) {\n // assert(xx = o >= mod ? o - mod : o;\n return *this;\n }\n\n /**\n * @brief long longからの代入演算子\n * @param[in] o 設定したい値\n * @details mod2回取るから遅い.負数でもOK.\n */\n ModInt& operator=(const long long o) {\n this->x = (int)((mod + o % mod) % mod);\n return *this;\n }\n\n /**\n * @brief ModIntからの代入演算子\n * @param[in] o 設定したい値\n * @details 高速\n */\n ModInt& operator=(const ModInt o) {\n this->x = *o;\n return *this;\n }\n};\n\n/**\n * @param[in] l ModInt\n * @param[in] r ModInt\n * @details if文使って少し高速化.\n */\ninline ModInt add(const ModInt l, const ModInt r) {\n const long long x = *l + *r;\n return ModInt::raw(x >= mod ? x - mod : x);\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r ModInt.\n */\ninline ModInt mul(const ModInt l, const ModInt r) {\n return ModInt::raw(*l * *r % mod);\n}\n\n/**\n * @brief a^x %modを求める\n * @param[in] a ModInt\n * @param[in] x long long.\n */\ninline ModInt pow(ModInt a, long long x) {\n ModInt ret = ModInt::raw(1);\n while (x) {\n if (x & 1) {\n ret = mul(ret, a);\n }\n a = mul(a, a);\n x >>= 1;\n }\n return ret;\n}\n\n/**\n * @brief x^-1 %modを求める\n * @param[in] x ModInt.\n * @details\n * 内部ではユークリッドの拡張互助法を使っている.\n * O(log(mod))\n */\ninline ModInt inv(const ModInt x) {\n long long a = *x, b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n std::swap(a -= t * b, b);\n std::swap(u -= t * v, v);\n }\n return ModInt::get(u);\n}\n\n/**\n * @brief 負数を求める単項演算子\n * @param[in] x ModInt\n */\ninline ModInt operator-(const ModInt x) { return add(mod, -*x); }\n\n/**\n * @param[in] l ModInt\n * @param[in] r ModInt\n */\ninline ModInt operator+(const ModInt l, const ModInt r) { return add(l, r); }\n\n/**\n * @param[in] l ModInt\n * @param[in] r ModInt\n */\ninline ModInt operator*(const ModInt l, const ModInt r) { return mul(l, r); }\n\n/**\n * @param[in] l ModInt\n * @param[in] r ModInt\n */\ninline ModInt operator-(const ModInt l, const ModInt r) { return add(l, -r); }\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n * @details\n * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している.ただしmodより大きい値や負の数を入れると事故るので注意.\n */\ninline ModInt operator+(const ModInt l, const int r) {\n return add(l, ModInt::raw(r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n */\ninline ModInt operator+(const ModInt l, const long long r) {\n return add(l, ModInt::get(r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n * @details\n * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している.ただしmodより大きい値や負の数を入れると事故るので注意.\n */\ninline ModInt operator*(const ModInt l, const int r) {\n return mul(l, ModInt::raw(r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n */\ninline ModInt operator*(const ModInt l, const long long r) {\n return mul(l, ModInt::get(r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n * @details\n * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している.ただしmodより大きい値や負の数を入れると事故るので注意.\n */\ninline ModInt operator-(const ModInt l, const int r) {\n return add(l, ModInt::raw(mod - r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n */\ninline ModInt operator-(const ModInt l, const long long r) {\n return add(l, -ModInt::get(r));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n * @details\n * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している.ただしmodより大きい値や負の数を入れると事故るので注意.\n */\ninline ModInt operator/(const ModInt l, const int r) {\n return mul(l, inv(ModInt::raw(r)));\n}\n\n/**\n * @param[in] l ModInt\n * @param[in] r int\n */\ninline ModInt operator/(const ModInt l, const long long r) {\n return mul(l, inv(ModInt::get(r)));\n}\n\n/**\n* @param[in] l ModInt\n* @param[in] r long long\n* @details\n* pow(l,r)を呼び出すだけなのでpowを参照のこと.\n計算量はO(log mod)\n*/\ninline ModInt operator^(const ModInt l, const long long r) { return pow(l, r); }\n\n/**\n * @brief\n * +=の実装、各operator+を呼ぶだけ\n * @tparam T\n * @param l ModInt\n * @param r 足すやつ\n * @return ModInt&\n */\ntemplate \nModInt& operator+=(ModInt& l, T r) {\n l = l + r;\n return l;\n}\n\n/**\n * @brief\n * -=の実装、各operator-を呼ぶだけ\n * @tparam T\n * @param l ModInt\n * @param r 引くやつ\n * @return ModInt&\n */\ntemplate \nModInt& operator-=(ModInt& l, T r) {\n l = l - r;\n return l;\n}\n\n/**\n * @brief\n * *=の実装、各operator*を呼ぶだけ\n * @tparam T\n * @param l ModInt\n * @param r かけるやつ\n * @return ModInt&\n */\ntemplate \nModInt& operator*=(ModInt& l, T r) {\n l = l * r;\n return l;\n}\n\n/**\n * @namespace factorial\n * @brief 順列数関連の関数のまとめ\n * @details\n * - combination\n * - permutation\n * - multiChoose\n */\nnamespace factorial {\n //! 順列数を格納する配列のサイズ\n constexpr int size =\n# ifdef FACTORIAL_SIZE\n FACTORIAL_SIZE;\n# else\n 3123456;\n# endif\n //! 前計算ができているかどうかのフラグ\n bool is_build = false;\n\n //! 順列数を格納する配列\n ModInt factorial[size];\n //! (順列数)^-1を格納する配列\n ModInt inverse_factorial[size];\n\n /**\n * @brief 順列数の前計算\n * @details\n * 順列数と,その逆元を[0,size)まで求める.\n * 計算量は,O(size + log(mod))\n */\n void build() {\n is_build = true;\n factorial[0] = 1;\n for (int i = 1; i < size; i++) {\n factorial[i] = factorial[i - 1] * i;\n }\n inverse_factorial[size - 1] = inv(factorial[size - 1]);\n for (int i = size - 1; i >= 1; i--) {\n inverse_factorial[i - 1] = inverse_factorial[i] * i;\n }\n }\n\n /**\n * @brief nPkを求める\n * @details\n * 前計算がしてあれば O(1).前計算してない場合は is_build\n * を読み取って前計算をする.\n */\n inline ModInt permutation(int n, int k) {\n if (k < 0 || k > n) return ModInt::raw(0);\n if (!is_build) build();\n return factorial[n] * inverse_factorial[n - k];\n }\n\n /**\n * @brief nCkを求める\n * @details\n * 前計算がしてあれば O(1).前計算してない場合は is_build\n * を読み取って前計算をする.\n */\n inline ModInt combination(int n, int k) {\n if (k < 0 || k > n) return ModInt::raw(0);\n if (!is_build) build();\n return factorial[n] * inverse_factorial[k] * inverse_factorial[n - k];\n }\n} // namespace factorial\n /* #endregion */\n /* #endregion */\n /**/\n#endif\n\nnamespace factorial {\n /**\n * @brief 重複組合せ\n * @param n 何種類のものを\n * @param r いくつ並べるか\n * @return ModInt nHr\n */\n ModInt multiChoose(int n, int r) {\n if (n == 0) {\n return ModInt(r == 0 ? 1 : 0);\n }\n if (r == 0) {\n return ModInt(1);\n }\n return combination(n + r - 1, r);\n }\n\n /**\n * @brief 上限付き重複組合せ\n * @details 計算量 O(min(n, r / lim))\n * @param n 何種類のものを\n * @param r いくつ並べるか\n * @param lim 1種類のものを選べる上限\n * @return ModInt\n */\n ModInt multiChoose(int n, int r, int lim) {\n if (n == 0) {\n return ModInt(r == 0 ? 1 : 0);\n }\n if (lim <= 0 && r > 0) {\n return ModInt(0);\n }\n if (r == 0) {\n return ModInt(lim < 0 ? 0 : 1);\n }\n ModInt ret = 0;\n for (int i : range(n + 1)) {\n if (i * (lim + 1) > r) break;\n ret += ((i & 1) ? mod - 1 : 1) * combination(n, i)\n * multiChoose(n, r - i * (lim + 1));\n }\n return ret;\n }\n} // namespace factorial\n\n\nint main() {\n /* write here */\n int p, s, r;\n scanf(\"%d%d%d\", &p, &s, &r);\n ModInt ret = 0;\n for (int top : range(r, s + 1)) {\n ModInt tot = 0;\n for (int q : range(p)) {\n if (top * q > s - top) break;\n tot +=\n factorial::combination(p - 1, q)\n * factorial::multiChoose(p - q - 1, s - top - top * q, top - 1)\n * inv(q + 1);\n }\n ret += tot;\n }\n ret *= inv(factorial::multiChoose(p, s - r));\n printf(\"%lld\\n\", *ret);\n return 0;\n}", "lang_cluster": "C++", "tags": ["probabilities", "math", "dp", "combinatorics"], "code_uid": "e1e68be6e0ed98370439e1e660ed4c97", "src_uid": "609195ef4a970c62a8210dafe118580e", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#ifdef BUG\n #include \"debug.hpp\"\n#else\n #define DEBUG(var)\n #define EXPECT(expr)\n#endif\n#define NO_IO_TIE ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n\nusing namespace std;\ntemplate< class T1, class T2 > inline istream &\noperator>>( istream & fin, pair< T1, T2 > & pr )\n{ fin >> pr.first >> pr.second; return fin; }\ntemplate< class T0, class T1, class T2 > inline istream &\noperator>>( istream & fin, tuple< T0, T1, T2 > & t )\n{ fin >> get<0>(t) >> get<1>(t) >> get<2>(t); return fin; }\ntemplate< class T > inline istream &\noperator>>( istream & fin, vector< T > & a ) {\nfor( auto & u: a) fin >> u; return fin; }\ntemplate inline istream &\noperator>>( istream & fin, array & a ) {\nfor( auto & u: a) fin >> u; return fin; }\n/* @@@ ----------------------------------- */\n\nvector fac({1});\n\nint64_t e525()\n{\n while(fac.back() * (int64_t) fac.size() < 1 + (int64_t)1e16)\n fac.push_back(fac.back() * (int64_t) fac.size());\n\n DEBUG(fac.size());\n size_t n, k;\n int64_t sum;\n cin >> n >> k >> sum;\n\n vector a(n);\n cin >> a;\n\n /* maximum sum that we can get with the remaining items */\n /* if j out of k factorials are already used */\n vector> ub(n + 1, vector(k + 1));\n for(size_t i = n - 1; i < n; -- i)\n for(size_t j = k; j < k + 1; -- j)\n if(j == k || (int64_t) fac.size() < a[i] + 1 || sum < fac[a[i]])\n ub[i][j] = min(sum, a[i] + ub[i + 1][j]);\n else\n ub[i][j] = min(sum, max(a[i] + ub[i + 1][j], fac[a[i]] + ub[i + 1][j + 1]));\n\n vector> cnt(k + 1);\n cnt[0][0] = 1;\n\n for(size_t i = 0; 2 * i < n; ++i)\n for(size_t j = min(i + 1, k); j < k + 1; --j) /* used */\n {\n if(j < k && a[i] < (int64_t) fac.size() && fac[a[i]] < sum + 1){\n const auto rest = ub[i + 1][j + 1] + fac[a[i]];\n\n auto iter = sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);\n\n for(; iter != end(cnt[j]); ++ iter)\n if(iter->first + fac[a[i]] < sum + 1)\n cnt[j + 1][iter->first + fac[a[i]]] += iter->second;\n else\n break;\n\n if (ub[i + 1][j + 1] < sum)\n cnt[j + 1].erase(begin(cnt[j + 1]), cnt[j + 1].lower_bound(sum - ub[i + 1][j + 1]));\n }\n\n const auto rest = ub[i + 1][ j ] + a[i];\n vector> buf;\n auto iter = sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);\n\n for(; iter != end(cnt[j]); ++ iter)\n if(iter->first + a[i] < sum + 1)\n buf.emplace_back(iter->first + a[i], iter->second);\n else\n break;\n\n if(ub[i + 1][j] < sum)\n cnt[j].erase(begin(cnt[j]), cnt[j].lower_bound(sum - ub[i + 1][j]));\n\n for(const auto & pr: buf)\n cnt[j][pr.first] += pr.second;\n }\n\n vector> half;\n for(auto & m: cnt)\n half.emplace_back(begin(m), end(m)), m.clear();\n\n cnt[0][0] = 1;\n\n for(size_t i = (n + 1) / 2; i < n; ++ i)\n for(size_t j = k; j < k + 1; --j) /* used */\n {\n if(j < k && a[i] < (int64_t) fac.size() && fac[a[i]] < sum + 1){\n auto iter = begin(cnt[j]); // sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);\n\n for(; iter != end(cnt[j]); ++ iter)\n if(iter->first + fac[a[i]] < sum + 1)\n cnt[j + 1][iter->first + fac[a[i]]] += iter->second;\n else\n break;\n\n }\n vector> buf;\n auto iter = begin(cnt[j]); // sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);\n\n for(; iter != end(cnt[j]); ++ iter)\n if(iter->first + a[i] < sum + 1)\n buf.emplace_back(iter->first + a[i], iter->second);\n else\n break;\n\n for(const auto & pr: buf)\n cnt[j][pr.first] += pr.second;\n }\n\n int64_t val = 0;\n\n for(size_t i = 0; i < cnt.size(); ++ i)\n for(const auto & pr: cnt[i])\n if(pr.first < sum + 1)\n for(size_t j = 0; i + j < k + 1; ++ j)\n {\n const auto iter = half[j].find(sum - pr.first);\n if(iter != end(half[j]))\n val += pr.second * iter->second;\n }\n else\n break;\n\n return val;\n}\n\nint main( const int argc, char * argv [])\n{\n // NO_IO_TIE;\n cout << e525();\n\n return EXIT_SUCCESS;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "binary search", "bitmasks", "meet-in-the-middle"], "code_uid": "a4e7622bffe421079b757dbc5be476e9", "src_uid": "2821a11066dffc7e8f6a60a8751cea37", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nlong long N, K, A, B;\n\nint main() {\n cin >> N >> K >> A >> B;\n\n long long cost = 0;\n\n if (K == 1) \n { cout << (A * (N - 1)) << endl; return (0); }\n\n while (N != 1) {\n if (N % K == 0) {\n cost += min(A * (N - (N / K)), B);\n N = N / K;\n }\n \n else { \n if (N < K) { cost += (A * (N - 1)); N = 1; }\n else {\n cost += (A * (N % K));\n N -= (N % K);\n }\n }\n }\n\n cout << cost << endl;\n return (0);\n}\n", "lang_cluster": "C++", "tags": ["greedy", "dp"], "code_uid": "8f5ca1eafe9455ee70a5758bf25c1801", "src_uid": "f838fae7c98bf51cfa0b9bd158650b10", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n int size;\n while(cin >> size)\n {\n int biceps=0,triceps=0,back =0;\n int arr[100];\n for(int i = 0;i < size;i++)\n {\n cin >> arr[i];\n }\n for(int i = 0;i < size;i+=3)\n {\n triceps+= arr[i];\n if(i+1 < size)\n biceps+= arr[i+1];\n if(i+2 < size)\n back+= arr[i+2];\n }\n int x = max(biceps,triceps);\n x = max(x,back);\n if(x == back)\n cout << \"back\" << endl;\n else if(x == biceps)\n cout << \"biceps\" << endl;\n else\n cout << \"chest\" << endl;\n }\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "dabefd657c4bb9e96e5d609d6b70b290", "src_uid": "579021de624c072f5e0393aae762117e", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nlong long int max(long long int a,long long int b)\n{if(b>a)\n return b;\n else return a;\n}\nint main()\n{long long int a,b,c,d,e,f,g;\n int n;\n cin>>n;\n vectorvalues(n,0);\n vectorsaylar(101,0);\n for(int i=0;i>values[i];\n saylar[values[i]]++;}\n long long int maxi=-1,eded=-1;\n for(int i=0;i<101;i++)\n if(saylar[i]>maxi)\n maxi=saylar[i],eded=i;\n long long int ans=0;\n for(int i=0;i<101;i++)\n {vectorkalues(n,0),tezesaylar(2*n+10,-1);\n\n for(int j=0;j\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n int n,k;\n int x;\n cin>>n>>k;\n vector vec;\n vector longs;\n vector permute;\n string s,s1;\n stringstream ss;\n for(int i=0;i>s;\n vec.push_back(s);\n ss<>x;\n ss.clear();\n longs.push_back(x);\n }\n sort(longs.begin(),longs.end());\n int Min=longs[n-1]-longs[0];\n s.clear();\n for(int i=0;i>x;\n longs[i]=x;\n ss.clear();\n }\n sort(longs.begin(),longs.end());\n if(Min>longs[n-1]-longs[0]){Min=longs[n-1]-longs[0];}\n }\n cout<\nusing namespace std;\nint a,b,l,r,res;\nbool hold[50];\nstring ans,hj=\"abcdefghijklmnopqrstuvwxyz\";\nint main()\n{\n\tans.clear();\n\tres=0;\n\tmemset(hold,0,sizeof(hold));\n\tscanf(\"%d%d%d%d\",&a,&b,&l,&r);\n\tint ll=l,rr=r;\n\tfor(int i=0;i=lk)\n\t{\n\t\tfor(int i=0;i=l)\n\t\t{\n\t\t\tfor(int i=l;i<=r;i++)\n\t\t\t{\n\t\t\t\tif(!hold[ans[i]-'a'])\n\t\t\t\t{\n\t\t\t\t\tres++;\n\t\t\t\t\thold[ans[i]-'a']=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor(int i=l;i\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\nmap mp;\r\nint t;\r\ncin >> t;\r\nwhile(t--){\r\nint a, b[3],ans=0;\r\ncin >> a;\r\ncin >> b[0]>> b[1] >> b[2];\r\nfor(int i=1 ; i<4 ;i++)mp[i]=b[i-1];\r\nwhile(ans!=3){\r\nif(a==0)break;\r\nans++;\r\na=mp[a];\r\n}\r\nif(ans==3) cout << \"YES\\n\";\r\nelse cout << \"NO\\n\";\r\n} \r\nreturn 0;\r\n}", "lang_cluster": "C++", "tags": ["brute force", "greedy", "implementation", "math"], "code_uid": "bdf3d7b2a7eacada5245bd0f8eada35b", "src_uid": "5cd113a30bbbb93d8620a483d4da0349", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\nusing namespace std;\nint main()\n{\n long long int a,x1,x2,x3,y1,y2,y3;\n cin>>x1>>y1>>x2>>y2>>x3>>y3;\n \n if(((y2-y1)*(x3-x2)) == ((y3-y2)*(x2-x1)))\n { cout<<\"TOWARDS\"; }\n else if(((y2-y1)*(x3-x2))-((y3-y2)*(x2-x1))>0)\n { cout<<\"RIGHT\"; }\n else\n cout<<\"LEFT\";\nreturn 0;\n}\n \n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "2ef9543b2b62f5af8e08c2924569b0ef", "src_uid": "f6e132d1969863e9f28c87e5a44c2b69", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint main(){\n\tint n,p=0,a[102],max=0;\n\tbool flag=false;\n\tscanf(\"%d\",&n);\n\ta[0]=0;\n\ta[n+1]=1001;\n\tfor(int i=1;i<=n;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tfor(int i=1;i<=n;i++)\n\t\tif(a[i]-1==a[i-1]&&a[i+1]==a[i]+1){\n\t\t\tflag=true;\n\t\t\tp++;\n\t\t}\n\t\telse{\n\t\t\tif(flag){\n\t\t\t\tflag=false;\n\t\t\t\tif(p>max)\n\t\t\t\t\tmax=p;\n\t\t\t\tp=0;\n\t\t\t}\n\t\t}\n\tif(p>max) max=p;\n\tprintf(\"%d\\n\",max); \n} ", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "47e0280479708ec6acba139b91af2107", "src_uid": "858b5e75e21c4cba6d08f3f66be0c198", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\n\n#define F cin\n#define G cout\n\n//ifstream F(\"p.in\");\n//ofstream G(\"p.out\");\n\nconst int N = 55;\nconst int mod = 1000000007;\n\nint n,m;\nlong long c[N][N],f[N][N],g[N][N];\n\nint main()\n{\n F>>n>>m;\n\n c[0][0] = 1;\n for (int i = 1; i <= n; c[i][0] = 1, i++)\n for (int j = 1; j <= n; j++)\n c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;\n\n f[1][0] = 0;\n f[0][0] = 1;\n g[1][0] = g[0][0] = 1;\n\n for (int i = 2; i <= n; i++)\n {\n for (int j = 0; j <= n; j++)\n {\n for (int left = 1; left <= i - 1; left++)\n { // aleg nodurile din primul subarbore si radacina arborilor si radacina sub drept celuilalt\n long long mul = c[i - 2][left - 1] * (left) * max(1, (i - 1 - left)) % mod;\n for (int kleft = 0; kleft <= j - 1; kleft++)\n {\n long long addA = g[left][kleft] * (i - 1 - left > 0 ? g[i - 1 - left][j - 1 - kleft]\n + f[i - 1 - left][j - 1 - kleft] : j - 1 - kleft == 0 ? 1 : 0) % mod;\n long long addB = ((i - 1 - left) > 0 ? g[i - 1 - left][j - 1 - kleft] * f[left][kleft] : 0) % mod;\n f[i][j] = (f[i][j] + mul * (addA + addB) % mod) % mod;\n }\n for (int kleft = 0; kleft <= j; kleft++)\n g[i][j] = (g[i][j] + mul * f[left][kleft] % mod * f[i - 1 - left][j - kleft]) % mod;\n }\n }\n }\n G<<(f[n][m]+g[n][m])%mod<<'\\n';\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "90b9fd388a587b6e851b2736d6c9f06b", "src_uid": "f98b740183281943eafd90328854746b", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\nll a,b;\nint t;\n\nbool win(ll a,ll b){\n if(a==0 || b==0) return 0;\n if (a>b) swap(a,b);\n if(!win(a,b%a)) return 1;\n return ((b/a)%(a+1))%2==0;\n}\n\nint main(){\n scanf(\"%d\",&t);\n while (t--){\n\tscanf(\"%I64d%I64d\",&a,&b);\n\tif (win(a,b)) printf(\"First\\n\");\n\telse printf(\"Second\\n\");\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "games"], "code_uid": "76da70632c7907ab8ef0bf319332ea96", "src_uid": "5f5b320c7f314bd06c0d2a9eb311de6c", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include\r\nusing namespace std;\r\n#define ll int\r\n#define M 51 \r\n#define N 50010\r\n#define W 11\r\n#define Z 1100\r\n#define yu 998244353\r\nll n,m,u,v;\r\nll cha[W][Z],o[W][W];\r\nll f[N][Z];\r\nll gm[Z];\r\ninline void push(ll &x,ll y)\r\n{\r\n\tif(y>=yu)y=y%yu;\r\n\tif(y<0)y=(y%yu+yu)%yu;\r\n\tx+=y;\r\n\tif(x>yu)x-=yu;\r\n\treturn ;\r\n}\r\nint main()\r\n{\r\n//\tfreopen(\"1.txt\",\"r\",stdin);\r\n\tcin>>n;\r\n\tcin>>m;\r\n\tfor(int i=1;i<=m;i++)\r\n\t{\r\n\t\tcin>>u>>v;\r\n\t\to[u][v]=1;\r\n\t\to[v][u]=1;\r\n\t}\r\n\tfor(int i=0;i0)\r\n\t\t\t\t{\r\n\t\t\t\t\tif(o[i][k])\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tkz=1;\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif(!kz)\r\n\t\t\t{\r\n\t\t\t\tcha[i][j]=(j|(1<0))cha[i][j]^=(1<\n// Library for Ordered set \n#include\n#include\n#define ll long long int\nusing namespace __gnu_pbds;\n#define ordered_set tree,rb_tree_tag,tree_order_statistics_node_update>\n#include\n#define llu unsigned long long int\n#define bs binary_search\n#define pb push_back\n#define f first\n#define s second\n#define ld long double\n#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)\n#define endl '\\n'\n#define MOD (ll)(1e9+7)\n#define ppb pop_back\n#define max3(a,b,c) max(max(a,b),c)\n#define min4(a,b,c,d) min(min3(a,b,c),d)\n#define M (ll)(1e6)\n#define rep(n) for(ll i=0;i &tree,vector &v,ll start,ll end,ll node)\n{\n\tif(start==end)\n\ttree[node]=v[start];\n\telse\n\t{\n\t\tll mid=(start+end)/2;\n\t\tbuild_tree(tree,v,start,mid,2*node+1);\n\t\tbuild_tree(tree,v,mid+1,end,2*node+2);\n\t\ttree[node]=tree[2*node+1]+tree[2*node+2];\n\t}\n}\nll get_sum(vector &tree,ll ss,ll se,ll qs,ll qe,ll i)\n{\n\tif((qs)<=ss&&(qe)>=se)\n\t\treturn tree[i];\n\tif((qe)se)\n\t\treturn 0;\n\tll mid=(ss+se)/2;\n\treturn(get_sum(tree,ss,mid,qs,qe,i*2+1)+get_sum(tree,mid+1,se,qs,qe,i*2+2));\n}\nvoid update_sum(vector &tree,ll ss,ll se,ll i,ll diff,ll si)\n{\n\tif(ise)\n\t\treturn;\n\ttree[si]=tree[si]+diff;\n\tif(ss!=se)\n\t{\n\t\tll mid=(ss+se)/2;\n\t\tupdate_sum(tree,ss,mid,i,diff,2*si+1);\n\t\tupdate_sum(tree,mid+1,se,i,diff,2*si+2);\n\t}\n}*/\nll find_pow(ll a,ll b)\n{\n\tll res=1;\n\twhile(b>0)\n\t{\n\t\tif(b&1)\n\t\t\tres=(res*a)%MOD;\n\t\ta=(a*a)%MOD;\n\t\tb>>=1;\n\t}\n\treturn res;\n}\nll find_ans(ll a,ll b)\n{\n\tll ans=1;\n\tll cnt=0,x1=a;\n\twhile(a>0&&b>=a)\n\t{\n\t\tll y1=(b/a);\n\t\tcnt+=y1;\n\t\tif(y1>x>>n;\n\t//cout< p;\n\tfor(ll i=1;i*i<=x;i++)\n\t{\n\t\tif(x%i==0)\n\t\t{\n\t\t\tif(isPrime(i))\n\t\t\tp.pb(i);\n\t\t\tif(x!=(i*i)&&isPrime(x/i))\n\t\t\t\tp.pb(x/i);\n\t\t}\n\t}\n\tll ans=1;\t\n\tfor(ll i=0;i\n#include \n\nusing namespace std;\n\nint main() {\n int n,at,sum=0,o=-1;\n cin>>n>>at;\n int arr[n];\n \n for(int i=0; i>arr[i];}\n \n for(int i=0; i\n \nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define ii pair\n#define il pair\n#define pll pair\n#define iii pair\n#define LOG(x) cout << x <<\" \";\n#define TRACE(x) cout << #x << \" : \" << x << endl\n#define LOG2DArray(array, n, m) for ( int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { LOG(array[i][j]); } cout<\n#define vl vector\n#define vul vector\n#define mp make_pair\n#define pub push_back\n#define pob pop_back\n#define vii vector \n#define vll vector \n#define vil vector \n#define viii vector \n#define vs vector \n#define all(s) (s).begin(),(s).end()\n#define allb(s,b) (s).begin()+b,(s).end()\n#define allr(s) (s).rbegin(),(s).rend()\n#define fore(i, l, r) for(int i = (int)l; i <= (int)r; i++)\n#define forn(i, l, r) for(int i = (int)l; i >= (int)r; i--)\n#define REP(i, n) for (int i = 1; i <= n; i++)\n#define cinx int x; cin>>x\n#define cinc char c; cin>>c\n#define cinxl ll x; cin>>x\n#define scan(TYPE,VALUE) TYPE VALUE;cin>>VALUE\n#define sout cout< g[maxn];\nint n , m;\nint s , t;\nint V;\nvoid add_rib (int a, int b, int u, int c) { \n // u = capacity \n // c = cost per flow (maybe double) \n // add edge between a and b \n rib r1 = { b, u, c, 0, (int)g[b].size() }; \n rib r2 = { a, 0, -c, 0, (int)g[a].size() }; \n g[a].push_back (r1); \n g[b].push_back (r2); \n}\nint mcmf(){\n\tint flow = 0, cost = 0; \n while (flow < inf) { \n vector id (V, 0); \n vector d (V, inf); \n vector q (V); \n vector p (V); \n vector p_rib (V); \n int qh=0, qt=0; \n q[qt++] = s; \n d[s] = 0; \n while (qh != qt) { \n int v = q[qh++]; \n id[v] = 2; \n if (qh == V) qh = 0; \n for (int i=0; i n || j > m)\n\t\treturn false;\n\treturn true;\n}\nint main()\n{\n\tfastio;\n\tcin>>n >> m;\n\tfore(i,1,n){\n\t\tfore(j,1,m){\n\t\t\tcin>>grid[i][j];\n\t\t}\n\t}\n\ts = 0;\n\tt = n * m +1;\n\tV = t + 1;\n\tfore(i,1,n){\n\t\tfore(j,1,m){\n\t\t\tif ((i + j)%2){\n\t\t\t\tadd_rib((i-1)*m + j , t, 1 , 0);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tadd_rib(s , (i-1)*m + j , 1 , 0);\n\t\t\t\tfore(k,0,3){\n\t\t\t\t\tint tx = i + dx[k];\n\t\t\t\t\tint ty = j + dy[k];\n\t\t\t\t\tif (!valid(tx , ty))\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\tadd_rib((i-1)*m + j , (tx-1)*m + ty , 1 , grid[i][j] != grid[tx][ty]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nconst int N=100;\nint a[N], d[N];\nint main()\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\tfor(int i=0; i=0; i--)\n\t{\n\t\td[i]=max(d[i+1], a[i]+s-d[i+1]);\n\t\ts+=a[i];\n\t}\n\tprintf(\"%d %d\\n\", s-d[0], d[0]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "0cb79804830cd5acb0507af77c1811a6", "src_uid": "414540223db9d4cfcec6a973179a0216", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n\n#include\n#include\n#include\n#include \n#include\n#include\n#include\nusing namespace std;\n\n#include\n#include\nusing namespace __gnu_cxx;\n\n// skrocone typy\ntypedef long long ll;\n\ntypedef unsigned int uint;\ntypedef unsigned short ushort;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\n\n// int pairs\ntypedef pair pii;typedef pair puiui;typedef pair piui;typedef pair puii;\n// long long pairs\ntypedef pair< ll,ll> pllll;typedef pair pullull;typedef pair pullll;typedef pair pllull;\n// char pairs\ntypedef pair pcc;typedef pair pcuc;typedef pair pucc;typedef pair pucuc;\n// long long x int pairs\ntypedef pair pill;typedef pair plli;typedef pair puill;typedef pair pllui;typedef pair pulli;typedef pair piull;typedef pair puiull; typedef pair pullui;\n// long long x char pairs\ntypedef pair pcll;typedef pair pllc;typedef pair pucll;typedef pair plluc;typedef pair pullc;typedef pair pcull;typedef pair pucull; typedef pair pulluc;\n// int x char pairs\ntypedef pair pci;typedef pair pic;typedef pair puci;typedef pair piuc;typedef pair puic;typedef pair pcui;typedef pair pucui; typedef pair puiuc;\n\n\n\n#define mp make_pair\n#define fi first\n#define se second\n\n#define pb push_back\n#define pf push_front\n#define ins insert\n#define era erase\n\n#define it iterator\n\n#define type(x) __typeof(x)\n\n#define foreach(itr,cont) for(__typeof(cont.begin()) itr = cont.begin(); itr != cont.end(); ++itr)\n#define rep(i,n) for(int i=0; i<(n); ++i)\n#define per(i,n) for(int i=(n)-1; i>=0; --i)\n#define fo(i,a,b) for(int i=(a); i<(b); ++i)\n#define of(i,a,b) for(int i=(b)-1; i>=(a); --i)\n\n#define sz(c) (int)(c).size()\n\nstruct llhash {size_t operator()(ll x)const {return (int)x ^ (int)(x>>32);}};\nstruct piihash{size_t operator()(pii x)const {return x.fi^x.se;}};\n\nint x00,y00,n,d;\n\nvector vecs;\n\nchar pos[2][2][410][410];\nconst int win=1;\nconst int loss=2;\n\ninline int dist2(int x, int y)\n{\n x-=205;\n y-=205;\n return x*x + y*y;\n}\n\nvoid tc()\n{\n cin>>x00>>y00>>n>>d;\n vecs.resize(n);\n rep(i,n)cin>>vecs[i].fi>>vecs[i].se;\n\n rep(g1,2)rep(g2,2)rep(x,410)rep(y,410)if(dist2(x,y)>d*d)pos[g1][g2][x][y]=win;\n\n\n\n of(diag,0,819)fo(x,0,410)\n {\n int y = diag-x;\n if(y<0 || y>=410)continue;\n if(pos[1][1][x][y])continue;\n // probujemy dojsc do przegrywajacej\n bool ok = false;\n rep(i,sz(vecs))\n {\n int nx = x+vecs[i].fi;\n int ny = y+vecs[i].se;\n if(nx>=410 || ny>=410)continue;\n if(pos[1][1][nx][ny]==loss)ok=true;\n }\n pos[1][1][x][y] = ok?win:loss;\n }\n\n \n of(diag,0,819)fo(x,0,410)\n {\n int y = diag-x;\n if(y<0 || y>=410)continue;\n if(pos[1][0][x][y]==0)\n {\n // probujemy dojsc do przegrywajacej\n bool ok = false;\n rep(i,sz(vecs))\n {\n int nx = x+vecs[i].fi;\n int ny = y+vecs[i].se;\n if(nx>=410 || ny>=410)continue;\n if(pos[0][1][nx][ny]==loss)ok=true;\n }\n pos[1][0][x][y] = ok?win:loss;\n }\n\n \n if(pos[0][1][x][y]==0)\n {\n bool ok = false;\n rep(i,sz(vecs))\n {\n int nx = x+vecs[i].fi;\n int ny = y+vecs[i].se;\n if(nx>=410 || ny>=410)continue;\n if(pos[1][0][nx][ny]==loss)ok=true;\n }\n if(pos[1][1][y][x]==loss)ok=true;\n pos[0][1][x][y] = ok?win:loss;\n }\n }\n \n\n of(diag,0,819)fo(x,0,410)\n {\n int y = diag-x;\n if(y<0 || y>=410)continue;\n if(pos[0][0][x][y])continue;\n // probujemy dojsc do przegrywajacej\n bool ok = false;\n rep(i,sz(vecs))\n {\n int nx = x+vecs[i].fi;\n int ny = y+vecs[i].se;\n if(nx>=410 || ny>=410)continue;\n if(pos[0][0][nx][ny]==loss)ok=true;\n }\n if(pos[0][1][y][x]==loss)ok=true;\n pos[0][0][x][y] = ok?win:loss;\n }\n\n \n cout<<(pos[0][0][x00+205][y00+205]==win?\"Anton\":\"Dasha\")<> nz;\n\n for(int i=0; i\n\nusing namespace std;\n\nint y, b, r;\n\nint main()\n{\n cin >> y >> b >> r;\n if (b>=r) b=r-1;\n if (y>=b) y=b-1;\n if (b-1>y) b=y+1;\n if (r-1>b) r=b+1;\n if (y+1>b) y=b-1;\n cout << b + y + r;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "d056f408e7f4e5c4255b23e0ebe9b950", "src_uid": "03ac8efe10de17590e1ae151a7bae1a5", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int MAX = 1e6 + 10; \nconst int OO = 0x3f3f3f3f;\nconst double EPS = 1e-9; \n\n#define bug(x) cout << #x << \" = \" << x << '\\n'\n#define FOR(i, a, n) for(int i = a; i < n; i++)\n#define REP(i, n) FOR(i, 0, n)\n#define fi first\n#define se second\n#define pb push_back\n#define mt make_tuple\n#define all(vetor) vetor.begin(), vetor.end()\n//#define gc getchar_unlocked\n\ntypedef long long ll;\ntypedef pair ii;\ntypedef pair iii;\n\nbool isCopri(int a, int b)\n{\n FOR(i, 2, min(a, b)+1)\n if(a%i == 0 and b%i == 0)\n return false;\n return true;\n}\n\nint main()\n{\n int n, a, b;\n cin >> n;\n if(n & 1)\n a = n / 2, b = n - (n / 2);\n else\n a = (n / 2) - 1, b = n - ((n / 2) - 1);\n \n while(a > 1)\n if(isCopri(a, b))\n cout << a << ' ' << b << '\\n', a = 0;\n else\n a--, b++;\n if(a)\n cout << a << ' ' << b << '\\n', a = 0;\n \n\treturn 0;\n}\n// 1513969660211\n", "lang_cluster": "C++", "tags": ["brute force", "math", "constructive algorithms"], "code_uid": "8a545bf8977666460465b627be77105c", "src_uid": "0af3515ed98d9d01ce00546333e98e77", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0);\n ll p, q;\n cin >> p >> q;\n if (__gcd(p,q) > 1) {\n cout << \"Impossible\" << endl;\n return 0;\n }\n vector> ans;\n while (p != 1 || q != 1) {\n if (p >= q) {\n ll k = (p-1)/q;\n ans.push_back({'A',k});\n p -= q*k;\n } else {\n ll k = (q-1)/p;\n ans.push_back({'B',k});\n q -= p*k;\n }\n }\n for (auto p : ans) {\n cout << p.second << p.first;\n }\n cout << endl;\n}\n", "lang_cluster": "C++", "tags": ["number theory"], "code_uid": "54df983d17a45f91b80b639e42bbaa8c", "src_uid": "6a9ec3b23bd462353d985e0c0f2f7671", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nusing ll=long long;\nconst int N=5e3+9,mod=1e9+7;\nll lcm(ll a,ll b){\n return a*b/__gcd(a,b);\n}\nint main() {\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n ll n,a,b,p,q; cin>>n>>a>>b>>p>>q;\n ll red=n/a; ll blue=n/b,in=n/lcm(a,b);\n if(p>q)\n blue-=in;\n else\n red-=in;\n cout<\n#define ll long long\nusing namespace std;\n\nint main()\n{\n ios::sync_with_stdio(0);\n ll n,x,y,x1,y1,x2,y2,x3,y3;\n ll ans;\n multiset >ms;\n cin >> n;\n for(ll i=0;i> x >> y;\n ms.insert(make_pair(x,y));\n }\n multiset >::iterator c;\n if(n==1)\n cout << \"-1\";\n else if(n==2)\n {\n c = ms.begin();\n x1 = (*c).first;\n y1 = (*c).second;\n if(x1==x && y1==y){\n c++;\n x1 = (*c).first;\n y1 = (*c).second;\n }\n if(x1!=x && y1!=y)\n {\n ans = abs(x1-x)*abs(y1-y);\n cout << ans;\n }\n else\n cout << \"-1\";\n }\n else if(n==3)\n {\n c = ms.begin();\n x1 = (*c).first;\n y1 = (*c).second;\n c++;\n x2 = (*c).first;\n y2 = (*c).second;\n c++;\n x3 = (*c).first;\n y3 = (*c).second;\n if(x1!=x3 && y1!=y3)\n ans = abs(x1-x3)*abs(y1-y3);\n else if(x1!=x2 && y1!=y2)\n ans = abs(x1-x2)*abs(y1-y2);\n else if(x2!=x3 && y2!=y3)\n ans = abs(x2-x3)*abs(y2-y3);\n cout << ans;\n }\n else if(n==4)\n {\n c = ms.begin();\n x1 = (*c).first;\n y1 = (*c).second;\n c = ms.end();\n c--;\n x2 = (*c).first;\n y2 = (*c).second;\n ans = abs(x1-x2)*abs(y1-y2);\n cout << ans;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry", "implementation"], "code_uid": "9c9161529a670b467988b2f46fc7c48f", "src_uid": "ba49b6c001bb472635f14ec62233210e", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n\tint b,g,n;\n\tcin >> b;\n\tcin >> g;\n\tcin >> n;\n\tint ans = 0;\n\t\n\tfor(int i = 0; i <= b; i++)\n\t{\n\t\tfor (int j = 0; j <= g; j++)\n\t\t{\n\t\t\tif((i+j) == n)\n\t\t\t\tans++;\t\t\t\n\t\t}\n\t}\n\t\n\tcout << ans << endl;\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "4f65eba694330cfd8137d8f207346360", "src_uid": "9266a69e767df299569986151852e7b1", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#define SIZE(a) ((int) (a.size()))\n\nusing namespace std;\n\n#define MAXN 6010\nconst int mod = 1000000007;\n\nint n;\n\nlong long int dynamic[MAXN] , dynamic2[MAXN];\n\nint main(){\n\t\n\tstring sozdizisi;\n\tcin >> sozdizisi;\n\t\n\tstring sozdizisi2;\n\tcin >> sozdizisi2;\n\t\n\tn = SIZE(sozdizisi2);\n\t\n\tlong long int res=0;\n\t\n\tfor(int i=0;i\nusing namespace std;\nint n,m,k, ans, d, l, j;\nint main (){\n scanf(\"%d%d%d\", &n, &m, &k);\n if(n == 1) {\n printf(\"%d\", m);\n return 0;\n }\n ans = 1;\n d = m-n;\n j = k;\n l = 1;\n while(d >= l){\n d -= l;\n ans++;\n k --;\n j++;\n if(k >= 1) l++;\n if(j <= n) l++;\n if(k < 1 && j > n) break;\n }\n ans += (d/n);\n printf(\"%d\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "binary search"], "code_uid": "5f38fa0de76bdddca783a499806fd1c2", "src_uid": "da9ddd00f46021e8ee9db4a8deed017c", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "// ConsoleApplication15.cpp : Этот файл содержит функцию \"main\". Здесь начинается и заканчивается выполнение программы.\n//\n\n#include \n#include\nusing namespace std;\n\nlong long nod(long long a, long long b)\n{\n\twhile (b > 0)\n\t{\n\t\tlong long r = a % b;\n\t\ta = b;\n\t\tb = r;\n\t}\n\treturn a;\n\n}\n\nlong long nok(long long a, long long b)\n{\n\treturn a / nod(a, b)*b;\n}\n\nint main()\n{\n\tlong long a, b;\n\tcin >> a >> b;\n\tif (b < a) swap(a, b);\n\tif (b%a == 0) { cout << 0; return 0; }\n\tlong long d = b - a;\n\tlong long kres = 1000000000,lcmr=1e18;\n\n\tfor (int i = 1; i*i <= d; i++)\n\t{\n\t\tif (d%i == 0)\n\t\t{\n\t\t\tlong long k = i - a % i;\n\t\t\tif (a%i == 0) k = 0;\n\t\t\tlong long lcm = nok((a + k), (b + k)); // / i;\n\t\t\tif (lcm < lcmr)\n\t\t\t{\n\t\t\t\tlcmr = lcm;\n\t\t\t\tkres = k;\n\t\t\t}\n\t\t\telse\n\t\t\t\tif (lcm == lcmr && k < kres) kres = k;\n\t\t\tk = d/i - a % (d/i);\n\t\t\tif (a%(d/i) == 0) k = 0;\n\t\t\tlcm = nok((a + k), (b + k)); // / (d / i);\n\t\t\tif (lcm < lcmr)\n\t\t\t{\n\t\t\t\tlcmr = lcm;\n\t\t\t\tkres = k;\n\t\t\t}\n\t\t\telse\n\t\t\t\tif (lcm == lcmr && k < kres) kres = k;\n\n\t\t}\n\t}\n\n\tcout << kres;\n\n //std::cout << \"Hello World!\\n\"; \n}\n\n// Запуск программы: CTRL+F5 или меню \"Отладка\" > \"Запуск без отладки\"\n// Отладка программы: F5 или меню \"Отладка\" > \"Запустить отладку\"\n\n// Советы по началу работы \n// 1. В окне обозревателя решений можно добавлять файлы и управлять ими.\n// 2. В окне Team Explorer можно подключиться к системе управления версиями.\n// 3. В окне \"Выходные данные\" можно просматривать выходные данные сборки и другие сообщения.\n// 4. В окне \"Список ошибок\" можно просматривать ошибки.\n// 5. Последовательно выберите пункты меню \"Проект\" > \"Добавить новый элемент\", чтобы создать файлы кода, или \"Проект\" > \"Добавить существующий элемент\", чтобы добавить в проект существующие файлы кода.\n// 6. Чтобы снова открыть этот проект позже, выберите пункты меню \"Файл\" > \"Открыть\" > \"Проект\" и выберите SLN-файл.\n", "lang_cluster": "C++", "tags": ["brute force", "math", "number theory"], "code_uid": "64ea2e16b8e8109c18d1a98d3f7d54cc", "src_uid": "414149fadebe25ab6097fc67663177c3", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\n\n#define pb push_back\n#define fi first\n#define se second\n#define mp make_pair\n#define MOD 1000000007ULL\n#define INF 1000000000ULL\nconst int N = 1e5 + 5;\n#define FAST ios_base::sync_with_stdio(false)\n\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef double db;\ntypedef long double ldb;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pli;\ntypedef pair pdd;\n\n\n\ninline ll power(ll a, ll b, ll mod){\n\tll x = 1;\n\ta = a%mod;\n\twhile(b){\n\t\tif(b & 1) x = (x*a)%mod;\n\t\ta = (a*a)%mod;\n\t\tb >>= 1;\n\t}\n\treturn x;\n}\n\nll gcd(ll a, ll b){ return a?gcd(b%a,a):b;}\nll modInverse(ll a, ll mod)\n{\n int m0 = mod;\n int y = 0, x = 1;\n \n if (mod == 1)\n return 0;\n \n while (a > 1)\n {\n // q is quotient\n int q = a / mod;\n int t = mod;\n \n // m is remainder now, process same as\n // Euclid's algo\n mod = a % mod, a = t;\n t = y;\n \n // Update y and x\n y = x - q * y;\n x = t;\n }\n \n // Make x positive\n if (x < 0)\n x += m0;\n \t\n \t return x;\n}\n \n\n\nll m;\nll h1,h2,a1,a2,x1,x2,yy1,y2;\n\n\nint main()\n{\n\tcin>>m;\n\tcin>>h1>>a1;\n\tcin>>x1>>yy1;\n\tcin>>h2>>a2;\n\tcin>>x2>>y2;\n\n\tll tmp = 0;\n\tll tmp1 = h1;\n\tll ans1=-1,ans2=-1;\n\twhile(tmpans2){\n\t\t\tll ch = rep2,i;\n\t\t\tfor(i=0;i<=ch;i++){\n\t\t\t\tif( (ans1-ans2+rep1*i)%rep2 == 0)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(i == ch+1)\n\t\t\t{\n\t\t\t\tcout<<\"-1\\n\";\n\t\t\t}\n\t\t\telse{\n\t\t\t\tcout<\n#include \n\nusing namespace std;\nusing namespace __gnu_cxx;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nvoid fastInOut();\n\nconst int N = 1009;\nint T = 0, cst[N * N];\n\nvoid build(int b, int w, int x) {\n\tint bb = b, tot = 0;\n\twhile (bb != b || !T) {\n\t\tif (bb >= x)\n\t\t\tbb -= x;\n\t\telse\n\t\t\t++tot, bb += (w - x);\n\t\tcst[T++] = tot;\n\t}\n}\n\nll calc(ll a, ll t) {\n\treturn a - cst[T - 1] * (t / T) - cst[t % T];\n}\n\nll bs(ll a, ll c) {\n\tif (c <= a)\n\t\treturn 0;\n\tll st = 1, ed = (1LL << 62);\n\twhile (st < ed) {\n\t\tll md = (st + ed) / 2LL;\n\t\tif (c - md > calc(a, md - 1))\n\t\t\tst = md + 1;\n\t\telse\n\t\t\ted = md;\n\t}\n\treturn st;\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin);\n#endif\n\tfastInOut();\n\tint b, w, x;\n\tll a, c;\n\tcin >> a >> b >> w >> x >> c;\n\tbuild(b, w, x), cout << bs(a, c);\n\treturn 0;\n}\n\nvoid fastInOut() {\n\tios_base::sync_with_stdio(0);\n\tcin.tie(NULL), cout.tie(NULL);\n}\n", "lang_cluster": "C++", "tags": ["math", "binary search"], "code_uid": "96196b66627d1d0eb1d9c31f43208765", "src_uid": "a1db3dd9f8d0f0cad7bdeb1780707143", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint n;\nlong long a[9];\nint s[9];\nint ans=1e9;\nvoid solve(int x,vectorv,int res,bool f)\n{\n\tif(x>n)\n\t{\n\t\tif(f)res++;\n\t\tans=min(ans,res);\n\t\treturn;\n\t}\n\tfor(int i=0;ivt=v;\n\t\t\tvt[i]/=a[x];\n\t\t\tvt.push_back(a[x]);\n\t\t\tsolve(x+1,vt,res+(s[x]!=1),f);\n\t\t}\n\t}\n\tvectorvt=v;\n\tvt.push_back(a[x]);\n\tsolve(x+1,vt,res+s[x],true);\n}\nint main()\n{\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tscanf(\"%lld\",a+i);\n\t}\n\tsort(a+1,a+n+1);\n\treverse(a+1,a+n+1);\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tlong long t=a[i];\n\t\tfor(long long j=2LL;j*j<=t;j++)\n\t\t{\n\t\t\twhile(t%j==0LL)\n\t\t\t{\n\t\t\t\tt/=j;\n\t\t\t\ts[i]++;\n\t\t\t}\n\t\t}\n\t\tif(t!=1)s[i]++;\n\t\tif(s[i]>1)s[i]++;\n\t}\n\tvectorv;\n\tv.push_back(a[1]);\n\tsolve(2,v,s[1],false);\n\tprintf(\"%d\",ans);\n\treturn 0;\n} ", "lang_cluster": "C++", "tags": ["brute force", "dp", "number theory"], "code_uid": "1dc2d52e3c02039bacb5d952886d3b2a", "src_uid": "52b8b6c68518d5129272b8c56e5b7662", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma optimize(\"\", on)\n#include \n\nusing namespace std;\ntypedef pair pii;\ntypedef long long ll;\ntypedef long double ld;\nconst int maxn = (int)2e6 + 10;\nconst int mod = (int)1e9 + 9;\nconst int inf = (1<<30) - 1;\nint n, w, b;\nll fact[4040];\nint c[4040][4040];\nvoid solve() {\n cin >> n >> w >> b;\n fact[0] = 1;\n for(int i = 1; i < 4040; i++) {\n fact[i] = fact[i-1] * i % mod;\n }\n for(int i = 0; i < 4040; i++) {\n for(int j = 1; j < i; j++) {\n c[i][j] = (c[i - 1][j - 1] + c[i-1][j]) % mod;\n }\n c[i][0] = c[i][i] = 1;\n }\n ll ans = 0;\n for(int x = 1; x < n; x++) {\n int y = n - x;\n for(int k = 2; k <= 2; k++) {\n ll cur = c[w-1][x-1] *1ll* c[x - 1][k-1] % mod;\n ll cur2 = 0;\n if(k > 1) {\n cur2 = (cur2 + c[b-1][y-1] *1ll* c[y-1][k-2]) % mod;\n }\n //cout << x << \" \" << k << \" \" << cur << \" \"<> t;\n for(int i = 1; i <= t; i++) {\n //printf(\"Case %d: \", i);\n solve();\n }\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["combinatorics", "number theory"], "code_uid": "6b45e59b1059e41160ebc4eeb8d5a4d4", "src_uid": "63e93a161bbff623323e66c98d5e20ac", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\n\nint map[256];\n\nint main(){\n char ch;\n char str1[3],str2[3];\n map['6']=1; map['7']=2; map['8']=3; map['9']=4; //用常量表简化代码\n map['T']=5; map['J']=6; map['Q']=7; map['K']=8; map['A']=9;\n while(~scanf(\"%c\",&ch)){\n getchar();//吸收回车\n scanf(\"%s\",str1);\n scanf(\"%s\",str2);\n getchar();//吸收回车\n if(str1[1]==str2[1]){\n if(map[str1[0]]>map[str2[0]])\n printf(\"YES\\n\");\n else\n printf(\"NO\\n\");\n }\n else{\n if(str1[1]!=ch)\n printf(\"NO\\n\");\n else\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9ea1cda0ae456553beb16114d59a2566", "src_uid": "da13bd5a335c7f81c5a963b030655c26", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\ntypedef long long Long;\n\nLong pow2(Long base,Long exp)\n{\n if(exp*log10(base) > 12)return 10000000000000LL;\n if(exp==0)return 1;\n Long sq = pow2(base,exp>>1);\n sq = sq*sq;\n if(exp&1)sq = sq*base;\n return sq;\n}\n\nbool memo[100000][50];\nbool cat[100000][50];\nbool f = 0;\nLong canwin(Long A,Long B,Long N)\n{\n \n if(B==1 && A >= 70000)\n {\n return (N-A)%2 == 0;\n }\n if(A==1 && B >= 40)\n {\n f = true;\n return false;\n }\n if( pow2(A,B) >= N )return true;\n bool w = false;\n if(cat[A][B])return memo[A][B];\n if(!canwin(A+1,B,N) || !canwin(A,B+1,N))w = true;\n cat[A][B] = true;\n return memo[A][B] = w;\n}\n\nint main()\n{\n memset(cat,false,sizeof(cat));\n Long A,B,N;\n cin>>A>>B>>N;\n if(canwin(A,B,N) && !f)cout<<\"Masha\"<\nusing namespace std;\ntypedef long long ll;\nll sum(ll x) {\n\tll res = 0;\n\twhile(x) {\n\t\tres += x%10;\n\t\tx /= 10;\n\t}\n\treturn res;\n}\nint main(int args, const char * argv[]) {\n\tll n, s;\n\tcin >> n >> s;\n\tll l = 1, r = 1000000000000000000ll, ans = 2000000000000000000ll;\n\twhile(l <= r) {\n\t\tll mid = (l+r) >> 1;\n\t\tif(abs(sum(mid)-mid) >= s) {\n\t\t\tr = mid-1;\n\t\t\tans = mid;\n\t\t}\n\t\telse l = mid+1;\n\t}\n\tcout << max(0ll, n-ans+1) << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "binary search"], "code_uid": "c21d9a58f13c12c7db3c2177f2a3cb40", "src_uid": "9704e2ac6a158d5ced8fd1dc1edb356b", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nint a,b,c[111];\nusing namespace std;\nint main()\n{\n cin>>a>>b;\n for(int i=0;i>c[i];\n sort(c,c+a);\n int i=a-1;\n while(b%c[i]!=0)\n i--;\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define LL long long\n#define MAX(a,b) (a>b?a:b)\n#define MIN(a,b) (a=0?sum[j-1]:0) + dp[i-1][j] )%mod;\n\n for(int j = 0; j=0?sum[j-2*k-1]:0) + mod )%mod;\n }\n\n for(int j = 0; j0?sum[j-1]:0) + dp[t][j] )%mod;\n\n LL ans = 0;\n for(int i = 0; i<=2*k*t; i++)\n ans += ( 1LL*dp[t][i] * (i+a-b-1>=0?sum[i+a-b-1]:0) )%mod, ans %= mod;\n printf(\"%lld\\n\",ans);\n\n}\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "b678ab4a25fed44155af34023b8c6852", "src_uid": "8b8327512a318a5b5afd531ff7223bd0", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define Forn(i, n) for (int i = 0; i < (n); i++)\n#define Fornr(i, n) for (int i = (n) - 1; i > -1; i--)\n#define For(i, a, b) for (int i = (a); i < (b); i++)\n#define Forr(i, a, b) for (int i = (b) - 1; i >= (a); i--)\n#define fill(a, b) memset((a), (b), sizeof((a)))\n#define pb push_back\n#define inf 2147483647\n#define F first\n#define S second\n#define N 1000010\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\ntypedef vector vii;\n\nint n;\nchar p[N];\n\nll nok(ll a, ll b)\n{\n return a / __gcd(a, b) * b;\n}\n\nint main()\n{\n#ifdef LocalHost\n freopen(\"in\", \"r\", stdin);\n// freopen(\"out\", \"w\", stdout);\n#endif\n scanf(\"%d\", &n);\n ll ans = 1;\n For(i, n - 310, n)\n For(j, i, n + 1)\n For(e, j + 1, n + 1)\n if (i > 0)\n {\n ans = max(nok(e, nok(i, j)), ans);\n }\n/* fill(p, 1);\n For(i, 2, n)\n if (p[i])\n for (int j = i + i; j <= n; j += i)\n p[j] = 0;\n int k = 0;\n ll c = 1;\n Fornr(i, n + 1)\n if (p[i])\n {\n c *= i;\n k++;\n if (k == 1 && n - i > 2)\n ans = max(ans, i * 1ll * n * (n - 1) / __gcd(n, n - 1));\n if (k == 2)\n break;\n }\n Fornr(i, n + 1)\n if (!p[i])\n {\n c *= i;\n k++;\n if (k == 3)\n break;\n }*/\n cout << ans;\n}\n", "lang_cluster": "C++", "tags": ["number theory"], "code_uid": "77008f131d26fe610df72192439c33e2", "src_uid": "25e5afcdf246ee35c9cef2fcbdd4566e", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nll gcd(ll a, ll b){\n\twhile(b != 0){\n\t\ta %= b;\n\t\tswap(a, b);\n\t}\n\treturn a;\n}\n\nint main(){\n\tll n, k; cin >> n >> k;\n\tll multiple = n*k;\n\tll x, y; cin >> x >> y;\n\tll mn = 100000000000, mx = 0;\n\t\n\tfor(ll i = abs(y - x); i <= multiple; i+= k){\n\t\tif(i == 0)\n\t\t\tcontinue;\n\t\tll value = gcd(multiple, i);\n\t\tmn = min(mn, value);\n\t\tmx = max(mx, value);\n\t}\n\tfor(ll i = k - y - x; i <= multiple; i+= k){\n\t\tif(i == 0)\n\t\t\tcontinue;\n\t\tll value = gcd(multiple, i);\n\t\tmn = min(mn, value);\n\t\tmx = max(mx, value);\n\t}\n\tcout << multiple/mx << \" \" << multiple/mn << endl;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "number theory"], "code_uid": "05494f07215e4a3d1e17e4a81571ee33", "src_uid": "5bb4adff1b332f43144047955eefba0c", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "\n \n/***\n\n Bismillahir Rahmanir Rahim\n Read the name of Allah, who created you!!!\n Author : Tarikul Islam\n Department of CSE, City University, Bangladesh.\n\n***/\n\n#include \nusing namespace std;\n\n#define sf scanf\n#define pf printf\n#define ssf sscanf\n#define spf sprintf\n#define fsf fscanf\n#define fpf fprintf\n#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)\n#define scase sf (\"%d\",&tc)\n#define sn sf (\"%d\",&n)\n#define whilecase while (tc--)\n#define eof while (cin >> n)\n#define forcase for(kase=1; kase<=tc; kase++)\n#define arrayloop for(i=0; i> str\n#define getstr getline (cin,str)\n#define pcase pf (\"Case %d: \",kase)\n#define pii pair \n#define pb push_back\n#define in insert\n#define llu unsigned long long\n#define lld long long\n#define U unsigned int\n#define endl \"\\n\"\n#define TarikulCSE int main()\n#define signoff return 0\n\nTarikulCSE\n{\n\n /*\n freopen (\"input.txt\",\"r\",stdin);\n freopen (\"output.txt\",\"w\",stdout);\n\n */\n\n lld n;\n\n\n cin>>n;\n\n if(n%2 == 0)\n {\n if((n/2)%2 == 0)\n cout<<\"0\"<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint a, b, n, mod = 1000000007;\n\nint mas[1000], sz = 0;\nint cnk[1000006];\n\nint binpow(int a, int k)\n{\n int ans = 1;\n while(k)\n {\n if(k & 1)\n ans = (1ll*ans*a)%mod;\n a = (1ll*a*a)%mod;\n k /= 2;\n }\n return ans;\n}\n\nint main()\n{\n cin >> a >> b >> n;\n int s = b*n;\n\n for(int i = 1; i <= 7; ++i)\n {\n for(int j = (1 << i)-1; j >= 0; --j)\n {\n int num = 0;\n for(int k = 0; k < i; ++k)\n {\n num *= 10;\n num += (j & (1 << k)) ? a : b;\n }\n if(num <= s)\n mas[sz++] = num;\n }\n }\n\n cnk[0] = 1;\n cnk[1] = n;\n long long tm = n;\n for(int i = 2; i <= n; ++i)\n {\n tm = (tm*(n-i+1))%mod;\n tm = (tm*binpow(i, mod-2))%mod;\n cnk[i] = tm;\n }\n\n long long ans = 0;\n for(int i = 0; i < sz; ++i)\n {\n if((mas[i]-n*b) % (a-b) == 0)\n {\n int q = (mas[i]-n*b) / (a-b);\n if(q >= 0 && q <= n)\n {\n ans += cnk[q];\n ans %= mod;\n }\n }\n }\n \n cout << ans << endl;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "combinatorics"], "code_uid": "8b406c3a94923c9d3fd81e0037a45e00", "src_uid": "d3e3da5b6ba37c8ac5f22b18c140ce81", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint main()\n{\n char sum[3][100],s1[100],s2[100],s3[100];\n strcpy(sum[0],\"qwertyuiop\");\n strcpy(sum[1],\"asdfghjkl;\");\n strcpy(sum[2],\"zxcvbnm,./\");\n char m,s[1000],b[1000];\n while(cin>>m)\n {\n getchar();\n gets(s);\n memset(b,'\\0',sizeof(b));\n if(m=='R')\n {\n for(int i=0;i<3;i++)\n {\n for(int j=0;sum[i][j]!='\\0';j++)\n {\n for(int k=0;s[k]!='\\0';k++)\n {\n if(s[k]==sum[i][j])b[k]=sum[i][j-1];\n }\n }\n }\n }\n else if(m=='L')\n {\n for(int i=0;i<3;i++)\n {\n for(int j=0;sum[i][j]!='\\0';j++)\n {\n for(int k=0;s[k]!='\\0';k++)\n {\n if(s[k]==sum[i][j])b[k]=sum[i][j+1];\n\n }\n }\n }\n }\n puts(b);\n\n }\n return 0;\n\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "058d20b5518deddd80f974375f2f505d", "src_uid": "df49c0c257903516767fdb8ac9c2bfd6", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// Wanna Hack? GL...\n\n#include\n\n#define F first\n#define S second\n#define PB push_back\n#define sz(s) int((s).size())\n#define bit(n,k) (((n)>>(k))&1)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\n\nconst int maxn=1e5+10,mod=1e9+7;\nconst ll inf=1e18;\n\nint n;\nsetst;\n\nint lca(int a,int b){\n if(__builtin_clz(a)>__builtin_clz(b)) swap(a,b);\n while(__builtin_clz(a)<__builtin_clz(b)) a>>=1;\n while(a!=b) a>>=1,b>>=1;\n return a;\n}\ninline bool papa(int a,int b){// a!=b\n if(b<=a) return 0;\n int dif=__builtin_clz(a)-__builtin_clz(b);\n return a==(b>>dif);\n}\nint Size(int a){\n int x=1,ans=0;\n while(a<=n){\n\tans+=min(x,n-a+1);\n\tx<<=1,a<<=1;\n }\n return ans;\n}\nint Size(int a,set&s){\n int ans=Size(a);\n vectorvv;\n for(int x:s){\n\tif(xst2;\n int ans=0;\n for(int A=a;A>0;A>>=1){\n\tst2=st;\n\tfor(int B=b;B>0;B>>=1){\n\t ans=(1ll*ans+1ll*Size(A,st2)*Size(B,st2))%mod;\n\t if(st2.count(B>>1)) break;\n\t st2.insert(B>>1);\n\t}\n\tif(st.count(A>>1)) break;\n\tst.insert(A>>1);\n }\n return ans;\n}\n\nint CNT=0;\n\nint main(){\n ios_base::sync_with_stdio(false);cin.tie(0);\n int m;cin>>n>>m;\n vectorvv(m),vv2;\n for(int i=0;i>vv[i].F>>vv[i].S;\n }\n ll ans=(1ll*n*n)%mod;\n for(int msk=1;msk<(1<perm(sz(vv2));\n\tfor(int i=0;ivv3(sz(vv2));\n\t for(int i=0;i>=1;\n\t\t }\n\t\t while(B!=C){\n\t\t\tif(st.count(B)) goto Hell;\n\t\t\tst.insert(B);\n\t\t\tB>>=1;\n\t\t }\n\t\t if(st.count(C)) goto Hell;\n\t\t st.insert(C);\n\t\t \n\t\t lst=p.S;\n\t\t}\n\t\tif(st.count(lst))\n\t\t goto Hell;\n\t\tst.insert(lst);\n\t\tans=(ans+solve(frst,lst))%mod;\n\t Hell:;\n\t }\n\t}while(next_permutation(perm.begin(),perm.end()));\n }\n return cout<\nusing namespace std;\nint main()\n{\n\n\n string str;\n bool f=true;\n for (int i = 0; i < 8; i++)\n {\n cin >> str;\n for (int j = 1; j < str.length(); j++)\n {\n if (str[j] == str[j-1])\n {\n f = false;\n }\n }\n }\n if (f)\n cout<< \"YES\";\n\n else\n cout<< \"NO\";\n \n return 0 ;\n\n}\n", "lang_cluster": "C++", "tags": ["brute force", "strings"], "code_uid": "0f78b74420b2b4ae8280f6c7f907fa18", "src_uid": "ca65e023be092b2ce25599f52acc1a67", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#define pb push_back\n#define mp make_pair\n#define MAX 55\n#define MAXN 2005\n#define ll int\n#define pii pair\n#define piii pair >\n\n\nusing namespace std;\n\nconst ll mod = 1e9 + 7;\n\nll n,s,k;\n\nll inp[MAX];\n\nstring str;\n\nvector arr;\n\nll dp[MAX][MAXN][MAX];\n\nll brr[MAX];\n\nll solve(ll idx,ll rem,ll prev)\n{\n if(rem <= 0)\n return 0;\n if(idx == n + 1)\n return 1e9;\n if(dp[idx][rem][prev] != -1)\n return dp[idx][rem][prev];\n ll ans = 1e9;\n ans = min(ans,solve(idx + 1,rem,prev));\n if(arr[idx].first > arr[prev].first && arr[idx].second.second != arr[prev].second.second)\n ans = min(ans,solve(idx + 1,rem - arr[idx].first ,idx) + abs(arr[idx].second.first - arr[prev].second.first));\n return dp[idx][rem][prev] = ans;\n}\n\nint main()\n{\n cin>>n>>s>>k;\n ll i,j;\n for(i = 0;i < n;i++)\n cin>>inp[i];\n cin>>str;\n ll len = str.length();\n arr.pb(mp(0,mp(s,0)));\n for(i = 0;i < len;i++)\n {\n if(str[i] == 'R')\n brr[i] = 1;\n else if(str[i] == 'G')\n brr[i] = 2;\n else\n brr[i] = 3;\n arr.pb(mp(inp[i],mp(i + 1,brr[i])));\n }\n sort(arr.begin(),arr.end());\n //for(i = 0;i < arr.size();i++)\n //cout<= 1e9)\n cout<<-1<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define PI acos(-1.0)\nusing namespace std;\nconst int N = 1000;\ntypedef long long ll;\nint n,m;//coffee数量,作业数\nint a[N];\nbool cmp(int x,int y){\n\treturn x > y;\n}\nbool check(int x){\n\t//时间偏大返回true\n\tll summ = 0;\n\tint t = 0;\n\tfor(int i = 0;i0)\n\t\t\tsumm += a[i] - t;\n\t\telse break;\n//思考一下为什么这里用我写的就过不了!!!!!!!!!!!!! \n// 在使用函数对数据进行操作得到时候一定要注意,千万不要改变某个值!!! \n//\t\tsumm += a[i] - t;\n\t\tif((i+1)%x==0) t++;\n//\t\tif(a[i] - t <= 0) break;\n\t}\n\t return summ >= m;\n} \nint main(){\n\tll sum = 0;\n\tcin>>n>>m;\n\tfor(int i = 0;i> 1;\n\t\tif(check(mid)) r = mid;\n\t\telse l = mid + 1;\n\t}\t\n\tcout<\n\nusing namespace std;\n\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define sqr(x) (x)*(x)\n#define sz(X) (int)(X).size()\n#define all(x) x.begin(),x.end()\n#define int long long\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\n\nconst int mod = 1e9+7;\n\nstring s,t;\nint res,l[200005],r[200005];\nint32_t main(){\n cin >> s >> t;\n int n = sz(s), m = sz(t), len = 0;\n for(int i = 0; i < n && len < m; ++i){\n if(s[i] == t[len]){\n l[len++] = i;\n }\n }\n len = m - 1;\n for(int i = n-1; i >= 0 && len >= 0; --i){\n if(s[i] == t[len]){\n r[len--] = i;\n }\n }\n res = max(max(0LL,r[0]), n - l[m-1] - 1);\n for(int i = 1; i < m; ++i){\n res = max(res, r[i] - l[i-1] - 1);\n }\n cout << res;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "bb676b8cea30081660f65b77c1bc252a", "src_uid": "0fd33e1bdfd6c91feb3bf00a2461603f", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nint n,m;\nbool judge(int x)\n{\n int base=1;\n bool flg=true;\n for (int i = 2; i*i <= x; ++i) {\n if(x%i==0) {\n base=i;\n flg=false;\n break;\n }\n }\n if(flg)\n return true;\n else return base>n;\n}\nint main()\n{\n while(cin>>n>>m)\n {\n int num=0;\n int ans=-1;\n for (int i = m; i > n; --i) {\n if(judge(i))\n {\n ans=i;\n break;\n }\n num++;\n if(num>456)\n {\n break;\n }\n }\n cout<\nusing namespace std;\n\nint main()\n{\n int a, b, c, k;\n cin >> a >> b >> c >> k;\n\n while (a + b + c - 3 > k) {\n if (a >= b && a >= c)\n a--;\n else if (b >= a && b >= c)\n b--;\n else\n c--;\n }\n\n cout << 1ll * a * b * c << '\\n';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "2c99f76592ccbd9e4a96534b1afccd09", "src_uid": "8787c5d46d7247d93d806264a8957639", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define MOD 1000000007\nint main(){\n int n;\n cin >> n;\n long long a = 0, b = 0, ans = 0;\n for (int i = 0; i < n; ++i) {\n if (i % 2 == 0) {\n ans = (ans + a + 1) % MOD;\n b = (a + b + 1) % MOD;\n }\n else {\n ans = (ans + b + 1) % MOD;\n a = (a + b + 1) % MOD;\n }\n }\n cout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["math", "dp"], "code_uid": "2a628f3dfb90038073bf547db66a741b", "src_uid": "5c4bd12df3915186a7b506c2060db125", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n// #include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\ntypedef double db;\nconst int oo=2139063143;\nconst int N=210;\nconst int P=1000000007;\nconst db eps=1e-7;\n#define pritnf printf\n//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;\n//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)\ntemplate \ninline void sc (T &x)\n{\n x=0; static int p; p=1; static char c; c=getchar();\n while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }\n while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }\n x*=p;\n}\ntemplate \ninline void print (T x)\n{\n if (x< 0) putchar('-'),x=-x;\n if (x>=10) print(x/10);\n putchar(x%10+'0');\n}\ntemplate \ninline void pr (T x) { print(x),putchar('\\n'); }\ntemplate \nvoid chkmax (T1 &A,T2 B) { A=A>=B?A:B; }\ntemplate \nvoid chkmin (T1 &A,T2 B) { A=A<=B?A:B; }\nLL f[N][N][2];\nLL calc (int a,int b,LL s,int c)//s=p+q,c=cntp+cntq\n{\n int m=max (max (a-1,b),(int)log2 (s)+1);\n for (int i=0; i<=m; i++)\n for (int j=0; j<=c; j++)\n f[i][j][0]=f[i][j][1]=0;\n for (int x=0; x<=1; x++)\n for (int y=0; y<=1; y++)\n {\n if (b==1&&!y) continue;\n if (((x+y)&1)!=(s&1)) continue;\n if (a<=1&&x) continue;\n if (b<=0&&y) continue;\n ++f[0][x+y][x&&y];\n }\n for (int i=0; i< m; i++)\n {\n LL d=s>>(i+1)&1;\n for (int j=0; j<=c; j++)\n for (int t=0; t<=1; t++) if (f[i][j][t])\n for (int x=0; x<=1; x++)\n for (int y=0; y<=1; y++)\n {\n int g=x+y+t;\n if (i+1==b-1&&!y) continue;\n if ((g&1)!=d) continue;\n if (i+1> a-2&&x) continue;\n if (i+1> b-1&&y) continue;\n f[i+1][j+x+y][g>=2]+=f[i][j][t];\n }\n }\n return f[m][c][0];\n}\nLL calc (LL x)\n{\n LL ans=x<<1;\n while (x) ans-=x&1,x>>=1;\n return ans;\n}\nint a[N],b[N];\nLL lca (LL x,LL y)\n{\n int n=0,m=0;\n while (x) a[n++]=x&1,x>>=1; reverse (a,a+n);\n while (y) b[m++]=y&1,y>>=1; reverse (b,b+m);\n chkmin (n,m);\n LL z=0;\n for (int i=0; i< n; i++)\n if (a[i]==b[i]) z=z<<1|a[i];\n else break;\n return z;\n}\nint main ()\n{\n // freopen (\".in\",\"r\",stdin);\n // freopen (\".out\",\"w\",stdout);\n LL s; sc(s);\n LL ans=0;\n for (int a=0; a< 50; a++)\n for (int b=0; b< 50; b++)\n {\n LL v=((1LL<<(a+1))+(1LL<<(b+1))-3),z=s/v;\n if (!z) continue;\n if ((int)log2 (z)+1+max (a,b)> 50) continue;\n LL k=s-v*z;\n for (int c=0; c<=a+b; c++)//cntp+cntq\n if (!((k+c)&1)) ans+=calc (a,b,(k+c)>>1,c);\n }\n pr(ans);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp", "combinatorics", "bitmasks"], "code_uid": "199278733fee6accecc7764ea780b397", "src_uid": "c142f8bc5252e739035992926545c251", "difficulty": 3200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\nusing namespace std;\n\n\n\nint main(){\n long long a,b,x,y;\n \n cin>>a>>b>>x>>y;\n\n \n long long g=__gcd(x,y);\n \n x=x/g;\n y=y/g;\n \n \n long long r=a%x;\n a=a-r;\n \n while(1){\n if(a<=0){\n cout<<0<<\" \"<<0<0){\n cout< \n#include \nusing namespace std;\nstring s;\nint dp[110] = { 0 };\nint state[110] = { 0 };\nint cur = 0;\nint main(int argc, const char * argv[]) {\n\tcin >> s;\n\tint length = s.size();\n\tif (length == 1)\n\t{\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tfor (int i = 0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define int64 long long\n#define mp make_pair\n\n#ifdef _DEBUG\n\tconst int N = 210;\n#else\n\tconst int N = 210;\n#endif\n\nint n;\nstring s;\nbool used[N][N][N];\n\nvoid dfs(int b, int g, int r)\n{\n\tif (used[b][g][r]) return;\n\tused[b][g][r] = true;\n\tif (b >= 1 && g >= 1) dfs(b - 1, g - 1, r + 1);\n\tif (b >= 1 && r >= 1) dfs(b - 1, g + 1, r - 1);\n\tif (g >= 1 && r >= 1) dfs(b + 1, g - 1, r - 1);\n\tif (b >= 2) dfs(b - 1, g, r);\n\tif (g >= 2) dfs(b, g - 1, r);\n\tif (r >= 2) dfs(b, g, r - 1);\n}\n\nint main()\n{\n#ifdef _DEBUG\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tscanf(\"%d\\n\", &n);\n\tcin >> s;\n\tint b = 0, g = 0, r = 0;\n\tfor (int i = 0; i < n; i++)\n\t\tif (s[i] == 'B') b++;\n\t\telse if (s[i] == 'G') g++;\n\t\telse r++;\n\tmemset(used, false, sizeof(used));\n\tdfs(b, g, r);\n\tif (used[1][0][0]) cout << 'B';\n\tif (used[0][1][0]) cout << 'G';\n\tif (used[0][0][1]) cout << 'R';\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "constructive algorithms"], "code_uid": "efb83306fe4d130b405f351cc028b5df", "src_uid": "4cedd3b70d793bc8ed4a93fc5a827f8f", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint d[21][21][1<<8][1<<8];\nint B[21],C[21];\nint a[21];\nchar s[21][21];\nint cx[10],cy[10],bx[10],by[10];\nint n,m,cnt,bcnt;\nstruct node\n{\n int x,y,s1,s2;\n};\nbool in(int x,int y)\n{\n return x>=0&&x=0&&yx)\n return 1;\n }\n if(nx==gx&&gynx)\n return 1;\n }\n return 0;\n}\nint dx[]={0,0,-1,1};\nint dy[]={1,-1,0,0};\nvoid bfs(int sx,int sy)\n{\n queueq;\n d[sx][sy][0][0]=0;\n node tmp;tmp.x=sx;tmp.y=sy;tmp.s1=0;tmp.s2=0;\n q.push(tmp);\n while(!q.empty())\n {\n node now=q.front();\n q.pop();\n int x=now.x;\n int y=now.y;\n int s1=now.s1;\n int s2=now.s2;\n int nx,ny,ns1,ns2;\n for(int k=0;k<4;k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n ns1=s1;ns2=s2;\n if(!in(nx,ny)||s[nx][ny]!='.')\n continue;\n for(int i=0;i='1'&&s[i][j]<='8')\n {\n cx[s[i][j]-'1']=i;\n cy[s[i][j]-'1']=j;\n cnt++;\n }\n else if(s[i][j]=='B')\n {\n bx[bcnt]=i;\n by[bcnt]=j;\n bcnt++;\n }\n else if(s[i][j]=='S')\n {\n sx=i;sy=j;\n }\n }\n }\n for(int i=0;i\n#include \nusing namespace std;\nint main ()\n{\n\tint M,n, a, b, c;\n cin.tie(0);\n cin >> n >> a >> b >> c;\n M =min(min(a, b), c);\n\tif (n == 1)\n\t{\n\t\tcout << 0;\n\t}\n\tif (M == c && n!=1)\n\t{\n\t\tcout << (min(a, b) + c * (n - 2));\n\t}\n\telse if(n!=1 && M!=c )\n\t{\n\t\tcout << (min(a, b)*(n - 1));\n\t}\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "f42cdc3aca2828241887c2ef9bb5e0b3", "src_uid": "6058529f0144c853e9e17ed7c661fc50", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include \r\n#include \r\n#include \r\n#include \r\n\r\n#define FIO ios::sync_with_stdio(false); cin.tie(nullptr)\r\n#define TC(t) int t; cin >> t; for(int i = 1; i <= t; i++)\r\n#define ll long long int\r\n#define ull unsigned long long int\r\n#define lll __int128\r\n#define lld long double\r\n#define loop(i, a, b) for(ll i = a; i <= b; i++)\r\n#define loop2(i, b, a) for(ll i = b; i >= a; i--)\r\n#define ini(x, y) memset(x, y, sizeof(x))\r\n#define all(x) x.begin(), x.end()\r\n#define all2(x) x.rbegin(), x.rend()\r\n#define sz(x) (ll)x.size()\r\n#define pb push_back\r\n#define ppb pop_back\r\n#define mp make_pair\r\n#define ff first\r\n#define ss second\r\n#define M 1000000007\r\n#define endl '\\n'\r\n#define bits(x) __builtin_popcountll(x)\r\n#define zrbits(x) __builtin_ctzll(x)\r\n#define vl vector\r\n#define pll pair\r\n#define vpll vector\r\n#define uni(x) x.erase(unique(all(x)), x.end())\r\n#define ordered_set tree, rb_tree_tag, tree_order_statistics_node_update>\r\n#define multi_ordered_set tree, rb_tree_tag, tree_order_statistics_node_update>\r\n#define mxheap priority_queue\r\n#define mnheap priority_queue, greater>\r\n#define mxheap2 priority_queue>\r\n#define mnheap2 priority_queue, vector>, greater>>\r\n\r\nconst int N = 2e5 + 5;\r\nconst int L = 20;\r\nconst int MX = 1e9 + 10;\r\nconst ll INF = 1e18;\r\n\r\nconst int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};\r\nconst int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};\r\n\r\nusing namespace std;\r\nusing namespace __gnu_pbds;\r\n\r\ninline ll uceil(ll a,ll b) {return (a % b ? a / b + 1 : a / b);}\r\ninline ll mod(ll x) {return ( (x % M + M) % M );}\r\nll ulog(ll val, ll base) {ll cnt = 0; val /= base; while(val) {cnt++; val /= base;} return cnt;}\r\nll power(ll a, ll b) {ll res = 1; while (b) {if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}\r\nll modpow(ll a, ll b) {ll res = 1; while (b) {if (b & 1) res = res * a % M; a = a * a % M; b >>= 1;} return res;}\r\n\r\n#ifndef ONLINE_JUDGE\r\n#define deb(x) cerr << #x << \" = \" << x << endl;\r\n#define deb2(x, y) cerr << #x << \" = \" << x << \", \" << #y << \" = \" << y << endl;\r\n#define deb3(x, y, z) cerr << #x << \" = \" << x << \", \" << #y << \" = \" << y << \", \" << #z << \" = \" << z << endl;\r\n#define deb4() cerr << endl;\r\n#define done cerr << \"Line \" << __LINE__ << \" is done\" << endl;\r\n#else\r\n#define deb(x)\r\n#define deb2(x, y)\r\n#define deb3(x, y, z)\r\n#define deb4()\r\n#define done\r\n#endif\r\n\r\ntemplate ostream& operator<<(ostream& os, const vector& v) {for(auto& x : v) os << x << \" \"; return os;}\r\ntemplate ostream& operator<<(ostream& os, const set& v) {for(auto& x : v) os << x << \" \"; return os;}\r\ntemplate ostream& operator<<(ostream& os, const pair& p) {os << p.first << ' ' << p.second; return os;}\r\ntemplate void in(T&... args) {((cin >> args), ...);}\r\ntemplate void out(T&&... args) {((cout << args << endl), ...);}\r\ntemplate void out2(T&&... args) {((cout << args << \" \"), ...);}\r\ntemplate void out3(T&&... args) {((cout << args << \" \"), ...); cout << endl;}\r\n\r\nll fact[N], invfact[N];\r\n\r\nvoid precompute() {\r\n fact[0] = invfact[0] = 1;\r\n for (int i = 1; i < N; i++) {\r\n fact[i] = fact[i - 1] * i % M;\r\n }\r\n invfact[N - 1] = modpow(fact[N - 1], M - 2);\r\n for(int i = N - 2; i >= 0; i--) {\r\n invfact[i] = invfact[i + 1] * (i + 1) % M;\r\n }\r\n}\r\n\r\nll nCr(ll n, ll r) {\r\n if (r < 0 || n < 0 || r > n) return 0;\r\n return fact[n] * invfact[r] % M * invfact[n - r] % M;\r\n}\r\n\r\nvoid solve() {\r\n ll n, k;\r\n cin >> n >> k;\r\n ll ans = 0;\r\n loop(i, 0, min(k, n)) {\r\n ans += nCr(n, i);\r\n ans %= M;\r\n }\r\n cout << ans << endl;\r\n}\r\n\r\nsigned main () {\r\n precompute();\r\n #ifndef ONLINE_JUDGE\r\n freopen(\"input.txt\", \"r\", stdin);\r\n freopen(\"output.txt\", \"w\", stdout);\r\n #endif\r\n FIO;\r\n // TC(t)\r\n solve();\r\n return 0;\r\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "constructive algorithms", "combinatorics"], "code_uid": "d8ba706f8749c9dab291afba0c67130e", "src_uid": "dc7b887afcc2e95c4e90619ceda63071", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nint main() {\n int a, b, c, d, e, f, s = 0;\n\n cin >> a >> b >> c >> d >> e >> f;\n\n switch(e > f){\n case true:\n while(a > 0 && d > 0){\n a--;\n d--;\n s+= e;\n }\n while(b > 0 && c > 0 && d > 0){\n b--;\n c--;\n d--;\n s+= f;\n }\n case false:\n while(b > 0 && c > 0 && d > 0){\n b--;\n c--;\n d--;\n s+= f;\n }\n while(a > 0 && d > 0){\n a--;\n d--;\n s+= e;\n }\n }\n\n cout << s;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "brute force"], "code_uid": "c4f34648debc3c1d16607160ef0d80a5", "src_uid": "84d9e7e9c9541d997e6573edb421ae0a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include\nusing namespace std;\n\n#define ll long long\n\nvector v;\nvector vv;\nset st;\n\n\nll x,y,z;\nstring s,s1,s2;\n\n\nvoid memset_ll(ll a[], ll v, ll n)\n{\n for(ll i=0; i *(double*)b)\n return 1;\n else if (*(double*)a < *(double*)b)\n return -1;\n else\n return 0;\n}\nvoid q_sort_d(double a[], ll n)\n{\n qsort(a, n, sizeof(double), compare1);\n}\n\n\nll sum(ll n)\n{\n ll i=0,a[6],j,k=0;\n while(n>0)\n {\n a[i]=n%10;\n n/=10;\n i++;\n }\n for(j=0; j>s;\n cin>>ss;\n s3[0]=\"saturday\";\n s3[1]=\"sunday\";\n s3[2]=\"monday\";\n s3[3]=\"tuesday\";\n s3[4]=\"wednesday\";\n s3[5]=\"thursday\";\n s3[6]=\"friday\";\n for(i=0;i<7;i++)\n {\n t=s.compare(s3[i]);\n if(t==0)\n p=i;\n t=ss.compare(s3[i]);\n if(t==0)\n q=i;\n }\n if(q\n\ntypedef long long LL;\n\nconst int N = 31;\nconst int MOD = (int)1e9 + 7;\n\nint n;\nint dp0[N][N], dp1[N][N];\n\ninline void Ad(int &a, int b) {\n if ((a += b) >= MOD) a -= MOD;\n}\n\nint main() {\n scanf(\"%d\", &n);\n dp1[30][0] = 1;\n for (int i = 30; i >= 1; --i) {\n for (int j = 0; j <= 30; ++j) {\n int x = (j)? (1 << (j - 1)) : (1);\n int y = (j)? (1 << (j - 1)) : (0);\n { // trans dp0\n Ad(dp0[i - 1][j + 1], dp0[i][j]);\n Ad(dp0[i - 1][j], (1LL << j) * dp0[i][j] % MOD);\n }\n { // trans dp1\n if ((n >> (i - 1)) & 1) {\n Ad(dp0[i - 1][j], (LL)x * dp1[i][j] % MOD);\n Ad(dp1[i - 1][j + 1], dp1[i][j]);\n Ad(dp1[i - 1][j], (LL)y * dp1[i][j] % MOD);\n } else {\n Ad(dp1[i - 1][j], (LL)x * dp1[i][j] % MOD);\n }\n }\n }\n }\n int ans = 0;\n for (int i = 0; i <= 30; ++i) {\n Ad(ans, dp0[0][i]);\n Ad(ans, dp1[0][i]);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "fa83e4f6035f173c079abe2aba195366", "src_uid": "ead64d8e3134fa8f29881cb487e52f60", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n//#define int long long\nusing namespace std;\ntypedef long long ll;\ntypedef long double ldb;\nconst int Mod=1e9+7;\nconst int blo=32767;\nconst ldb Pi=acos(-1.0);\nstruct complex{\n\tldb x,y;\n\tcomplex (ldb xx=0,ldb yy=0){ x=xx; y=yy;}\n};\nll n; int m,bit,len,rev[550000];\nll fac[1100000],inv[1100000],invfac[1100000];\nll A[550000],B[550000],aa[550000],bb[550000];\ncomplex a[550000],b[550000],c[550000],d[550000],\n\t\te[550000],f[550000],g[550000],h[550000];\ncomplex operator+(complex a,complex b){ return complex(a.x+b.x,a.y+b.y);}\ncomplex operator-(complex a,complex b){ return complex(a.x-b.x,a.y-b.y);}\ncomplex operator*(complex a,complex b){ return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);} \n\nvoid FFT(complex *A,int type){\n\tfor (int i=0;im){ puts(\"0\"); return 0;}\n\tfac[0]=1; for (int i=1;i<=1000000;i++) fac[i]=fac[i-1]*i%Mod;\n\tinv[1]=1; for (int i=2;i<=1000000;i++) inv[i]=(Mod-Mod/i)*inv[Mod%i]%Mod;\n\tinvfac[0]=1; for (int i=1;i<=1000000;i++) invfac[i]=invfac[i-1]*inv[i]%Mod;\n\t\n\tlen=1; bit=0; while (len<=m+m) len<<=1,bit++;\n\tfor (int i=0;i>1]>>1)|((i&1)<<(bit-1));\n\t\n B[0]=1;\n for (int i=1;i<=m;i++) A[i]=invfac[i];\n\tll t=2;\n\twhile (n){\n\t\tif (n&1) mult(A,B,t);\n\t\tmult(A,A,t);\n\t\tt=t*t%Mod; n>>=1;\n\t}\n ll ans=0;\n for (int i=0;i<=m;i++) ans=(ans+B[i]*fac[i]%Mod*C(m,i)%Mod)%Mod;\n printf(\"%lld\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "fft"], "code_uid": "0c6679b5e7a782552aa08b42b3be1ab1", "src_uid": "295baf6ccbfc3a7d098989a0701d2018", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\nusing namespace std;\nint main(){\n\tint x,y,z;\n\tcin>>x>>y;\n\tz=sqrt(x*x*1.0+y*y*1.0);\n\tif(z==sqrt(x*x*1.0+y*y*1.0))\n\t{\n\t\tcout<<\"black\";\n\t\treturn 0;}\n\tif(z%2==0)\n\t\tif((x>=0&&y>=0 )|| (x<=0 &&y<=0))\n\t\t\tcout<<\"black\";\n\t\telse cout<<\"white\";\n\telse\n\t\tif((x>=0&&y>=0 )|| (x<=0 &&y<=0))\n\t\t\tcout<<\"white\";\n\t\telse cout<<\"black\";\n\n\n\n\n\n\n\t\n\treturn 0;\n\n\n\n}", "lang_cluster": "C++", "tags": ["math", "geometry", "constructive algorithms", "implementation"], "code_uid": "e9c1639fc741cfc581000ba720c7024e", "src_uid": "8c92aac1bef5822848a136a1328346c6", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//\n// main.cpp\n// irinasolves\n//\n// Created by Irina Korneeva on 02/08/16.\n// Copyright © 2016 Irina. All rights reserved.\n//\n\n#include \n#include \n#include \n#include \n#include \nstruct el {\n long long x, y;\n};\nusing namespace std;\nint main(int argc, const char * argv[]) {\n long long d, k, a, b, t;\n cin >> d >> k >> a >> b >> t;\n if(k > d) {\n cout << d * a;\n return 0;\n }\n if(a >= b ) {\n cout << b * d;\n //cout << \"ir\";\n } else {\n long long n = d/k;\n long long ans = n * a * k + (n - 1) * t;\n if(t + k * a > k * b) {\n cout << k * a + (d - k) * b;\n // cout << \"ir\";\n } else if(d % k) {\n ans += t + (d - n * k) * a;\n cout << min(ans, n * a * k + (n - 1) * t + (d - n * k) * b);\n } else {\n cout << ans;\n }\n }\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "016e1606842cd8f76882c0d3a9b1aa86", "src_uid": "359ddf1f1aed9b3256836e5856fe3466", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define ll long long\nusing namespace std;\n\nstruct matrix\n{\n ll v[7][7];\n};\nmatrix bin,ans;\nll mod,x,y,sp_x,sp_y,t;\n\nmatrix mult(matrix a,matrix b)\n{\n matrix temp;\n for(int i=0;i<6;i++)\n for(int j=0;j<6;j++)\n {\n temp.v[i][j]=0;\n for(int k=0;k<6;k++)\n temp.v[i][j]=(temp.v[i][j]+a.v[i][k]*b.v[k][j]-1)%mod+1;\n }\n return temp;\n}\n\nmatrix quick(matrix a,matrix b,ll n)\n{\n while(n)\n {\n if(n&1)\n a=mult(a,b);\n b=mult(b,b);\n n>>=1;\n }\n return a;\n}\n\nvoid init()\n{\n memset(ans.v,0,sizeof(ans.v));\n ans.v[0][0]=x;\n ans.v[0][1]=y;\n ans.v[0][2]=sp_x;\n ans.v[0][3]=sp_y;\n ans.v[0][4]=0;\n ans.v[0][5]=1;\n\n memset(bin.v,0,sizeof(bin.v));\n for(int i=0;i<4;i++)\n bin.v[0][i]=bin.v[1][i]=1;\n bin.v[0][0]=bin.v[1][1]=2;\n\n bin.v[2][0]=bin.v[2][2]=1;\n bin.v[3][1]=bin.v[3][3]=1;\n bin.v[5][5]=bin.v[5][4]=1;\n for(int i=0;i<5;i++)\n bin.v[4][i]=1;\n}\n\n//void debug(const matrix &a)\n//{\n// cout<<\"**********\"<>mod>>x>>y>>sp_x>>sp_y>>t;\n init();\n// debug(ans);\n ans=quick(ans,bin,t);\n// debug(ans);\n// debug(bin);\n cout<<(ans.v[0][0]%mod-1+mod)%mod+1<<\" \"<<(ans.v[0][1]%mod-1+mod)%mod+1<\nusing namespace std;\n#define ll long long\n#define pb push_back\n#define f first\n#define s second\n#define pii pair\n#define forn(a,b,c) for(int a=b;a<=c;a++)\n#define ford(a,b,c) for(int a=b;a>=c;a--)\nconst int N=1e5+5;\nconst int mod=1e9+7;\nint add(int x, int y){ x+=y;if(x>=mod) x-=mod;return x;}\nint sub(int x, int y){ x-=y;if(x<0) x+=mod;return x;}\nint mul(int x, int y){ x=(ll)x*y%mod;return x;}\nint powmod(int x, int k){ int ans=1;for(;k;k>>=1,x=mul(x,x)) if(k&1) ans=mul(ans,x);return ans;}\nint inv(int x){ return powmod(x,mod-2);}\nint dv(int x, int y){ x=(ll)x*inv(y)%mod; return x;}\nint mu[N],prime[N];\nvectorprimes;\nvoid precalc(){\n //sieve with mobius function\n forn(i,2,N-1){\n prime[i]=1;\n mu[i]=1;\n }\n forn(i,2,N-1){\n if(prime[i]){\n for(int j=i;j= k using mid ; then we can never make cnt >= k using any mid greater\nthan mid is wrong\n\nReason :\nLet say mid = 3 ; and maxx = 11\nwe can make only cnt = 3 using current mid ( 3 , 6 , 7 ).\n\nBut for a greater mid - mid = 4 ; we are allowed to use the odd term after it ( 5 ) too ; and we can unfold its\nrecursion tree too.\nHence using that ; f(4) = 6.\n\nSo at this point - there is a conflict.\nShould we reject the binary search or should we modify it.\n\nThis time ; this binary search can be modified.\nReason - the best candidate that gives us a greater f(mid) near to mid is mid + 1 ( in case mid is odd ).\nThis is because mid + 1 is even ; and thus we may use recursion tree of mid+2 also to increase our count !\n\nSO we can check for mid-1 ; mid ; and mid+1.\nif f(mid + delta) < k ; then we can say that we need to move left ; and do hi = mid - 1.\nElse we may say we can move right and do lo = mid + 1.\n\n*/\n\n/*\n\nMistake 2 - TLE on test case 16.\nEarlier ; I was trying to go on every node ; and I was doing f ( node*2 ) ; and f(2*node + 1) ;\n\nBut if we unfold the recursion tree ; we will see that it is a binary tree.\nWe can easily see that height of tree is logarithmic ; and for n <= 1e18 ; height <= 64.\n\nBut ; then what will be the number of nodes in the binary tree ?\nIt will be (1 << max_height) - 1 ( heights are taken 0 based ).\n\nSO since max height can be upto 64 ; hence number of nodes >= 1e18 ( easily ). So we will get a TLE.\n\nWhat can we do -\nObservation : the nodes in the binary tree are consecutive at every level.\nHence ; if we keep a track of the level ; we will know the number of nodes in that level = (1 << level).\n\nAnd we can see from the highest node will this entire block is <= maxx. If yes ; we may add 1 << level to answer.\nElse ; we can find upto which index this holds true ; and we may add those many nodes !\n\nThus by maintaining only the max node of any level ; we will do only max_height number of iterations !\nAnd thus we may solve function f() in log(1e18) time.\n\n*/\n\n#include\nusing namespace std;\n\n# define C continue;\n# define R return\n\n# define D double\n# define I insert\n# define ll long long\n# define ld long double\n\n# define ull unsigned long long\n# define ui unsigned int\n\n# define pb push_back\n# define pf push_front\n\n# define vi vector < int >\n# define vc vector < char >\n# define vs vector < string >\n# define vb vector < bool >\n# define vd vector < D >\n# define vll vector < ll >\n# define vull vector < ull >\n# define vld vector < ld >\n\n# define vvi vector < vector < int > >\n# define vvb vector < vector < bool > >\n# define vvc vector < vector < char > >\n# define vvll vector < vector < ll > >\n# define vvd vector < vector < D > >\n# define vvld vector < vector < ld > >\n\n# define all(v) (v).begin() , (v).end()\n# define allrev(v) (v).rbegin() , (v).rend()\n# define allcomp(v) v.begin() , v.end() , comp\n# define allrevcomp(v) v.rbegin() , v.rend() , comp\n\n# define pii pair < int , int >\n# define pll pair < ll , ll >\n# define pld pair < ld , ld >\n# define pDD pair < D , D >\n\n# define vpld vector < pld >\n# define vpii vector < pii >\n# define vpll vector < pll >\n# define vpDD vector < pDD >\n\n# define vvpii vector < vector < pii > >\n# define F first\n# define S second\n# define mp make_pair\n\n# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))\n\n# define pp(n) printf(\"%.10Lf\",n);\n# define line cout<<\"\\n\";\n# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);\n\nstring vow = \"aeiou\";\n\nconst int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};\nconst int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};\n\nconst int dx[] = { -1 , 0 , 0 , 1 } ;\nconst int dy[] = { 0 , -1 , 1 , 0 } ;\n\nconst ld pie = 3.14159265358979 ;\nconst ll mod = 1e9 + 7 ;\n\nll maxx ;\nll cnt = 0 ;\n\nvoid f ( ll n , ll lev )\n{\n if ( n > maxx )\n {\n ll values_this_level = (1LL << lev) ;\n n = n - values_this_level + 1 ;\n\n cnt += max ( 0LL , maxx - n + 1 ) ;\n return ;\n }\n\n cnt += (1LL << lev) ;\n f ( n*2 + 1 , lev + 1 ) ;\n}\n\nvoid solve ( int test_case )\n{\n cin >> maxx ;\n ll k ; cin >> k ;\n\n ll ans = 1 ;\n for ( ll lo = 1 , hi = maxx ; lo <= hi ; )\n {\n ll mid = lo + (hi-lo)/2 ;\n bool found = false ;\n\n for ( int del_mid = -1 ; del_mid <= 1 ; del_mid ++ )\n {\n ll temp = mid + del_mid ;\n if ( temp < 1 ) C ;\n if ( temp > maxx ) C ;\n\n cnt = 0 ;\n if ( temp % 2 == 0 )\n f ( temp , 0 ) , f ( temp+1 , 0 ) ;\n else\n f ( temp , 0 ) ;\n\n if ( cnt >= k )\n {\n ans = max ( ans , temp ) ;\n found = true ;\n }\n }\n\n if ( found )\n lo = mid + 1 ;\n else\n hi = mid - 1 ;\n }\n\n cout << ans ; line\n}\n\nint main()\n{fast\n int t = 1;\n // cin >> t;\n\n for ( int i=0 ; i < t ; i++ ) solve(i);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "binary search", "dp", "combinatorics"], "code_uid": "84c99d58dc9015c6273a1eeaa11006bb", "src_uid": "783c4b3179c558369f94f4a16ac562d4", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\nint main()\n{\n\tint m, n, i=0;\n\tbool k = 0;\n\tvector a(0);\n\tcin >> m >> n;\n\ta.push_back(n);\n\twhile (n > 0)\n\t{\n\t\tif (n % 2 == 0)\n\t\t{\n\t\t\tn /= 2;\n\t\t\ta.push_back(n);\n\t\t}\n\t\tif (n == m)\n\t\t{\n\t\t\tk = true;\n\t\t\tbreak;\n\t\t}\n\t\tif(n= 0;i--)\n\t\t\tcout << a[i] << \" \";\n\t\tcout << endl;\n\t}\n\telse\n\t\tcout << \"NO\" << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "brute force"], "code_uid": "28f7b8ce22463c5f3a1b9d9e0f1f2cdf", "src_uid": "fc3adb1a9a7f1122b567b4d8afd7b3f3", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define MAXN 100010\n#define pi 3.14159265358979323846\nusing namespace std;\nint n,m;\nint main(){\n scanf(\"%d %d\",&n,&m);\n int mx1= n*2,mx2= m*3;\n mapmp;\n for(int i =2;i<=n*2;i+=2){\n mp[i]++;\n }\n for(int j=3;j<=m*3;j+=3){\n mp[j]++;\n }\n for(int i =1;i<=max(mx1,mx2);i++){\n if(mp[i]>1){\n if(mx1+2\n#include\n#include\n#include\nusing namespace std;\ndouble v,r,s,f; \nconst double pi=3.1415926535897932384626433832795; \nconst double eps=1e-15; \nint n; \nint main()\n{\n\tscanf(\"%d %lf %lf\",&n,&r,&v);\n\twhile(n--) \n\t{\n\t\tscanf(\"%lf %lf\",&s,&f);\n\t\tdouble dis=f-s; \n\t\tdouble yu=fmod(dis,2*pi*r);\n\t\tint t=floor(dis/(2*pi*r)); \n\t\tdouble lo=0,up=2*pi;\n\t\twhile(lo+eps\n#include \n#include \n#include \nusing namespace std;\nusing namespace __gnu_cxx;\nusing namespace __gnu_pbds;\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef unsigned int uint;\ntypedef double dbl;\n//const long double pi = M_PI;\nlong double eps = 1e-13;\n\ntypedef tree, less, rb_tree_tag,\ntree_order_statistics_node_update> Tree;\n\ninline void putint(int x)\n{\n char s[9];\n int l = 0;\n while (x)\n s[l++] = '0' + (x % 10), x /= 10;\n while (l--)\n putc(s[l], stdout);\n putc(' ', stdout);\n}\n\ninline int getint()\n{\n char ch;\n\n while (ch = getchar(), ch < '0' || ch > '9');\n\n int x = ch - '0';\n while (ch = getchar(), ch >= '0' && ch <= '9')\n {\n x *= 10;\n x += ch - '0';\n }\n return x;\n}\n\nvoid solve(int n)\n{\n int m;\n int x, y;\n m = getint();\n\n Tree Car;\n\n for (int i = 1; i <= n; i++)\n Car.insert(mp(i, mp(0, i)));\n\n set used;\n for (int i = 1; i <= n; i++)\n used.insert(i);\n\n for (int i = 0; i < m; i++)\n {\n x = getint();\n y = getint();\n used.erase(x);\n y--;\n\n Tree::iterator it = Car.find_by_order(y);\n if ((it -> second).first != 0 && (it -> second).first != x)\n {\n printf(\"-1\\n\");\n return;\n }\n Car.insert(mp(-i, mp(x, (it -> second).second)));\n Car.erase(it);\n }\n\n for (Tree::iterator it = Car.begin(); it != Car.end(); it++)\n if ((it -> second).first == 0)\n (it -> second).first = *used.begin(),\n used.erase(used.begin());\n\n vector ans(n + 1, 0);\n vector Used(n + 1, 0);\n\n for (Tree::iterator it = Car.begin(); it != Car.end(); it++)\n ans[(it -> second).second] = (it -> second).first;\n\n for (int i = 1; i <= n; i++)\n Used[ans[i]] = 1;\n bool ok = true;\n for (int i = 1; i <= n; i++)\n ok &= Used[i];\n if (!ok)\n {\n printf(\"-1\\n\");\n return;\n }\n\n for (int i = 1; i <= n; i++)\n putint(ans[i]);\n printf(\"\\n\");\n}\n\nint main()\n{\n int i, j;\n\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"rt\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n\n int n, m;\n cin >> n;\n solve(n);\n}", "lang_cluster": "C++", "tags": ["data structures"], "code_uid": "5244c0d1b695235a9445b31201bf9ca6", "src_uid": "a2616b1681f30ce4b2a5fdc81cf52b50", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\n\n#define mod 1000000009\n\nint pfix(int x) { return x>=mod ? x-mod : x; }\nint dfix(int x) { return x<0 ? x+mod : x; }\n\nint ti[1000005];\nint fi[1000005];\n\nint main()\n{\n\tint n;\n\tscanf(\"%d\",&n);\n\tint dans = min(6,(n-1)*2);\n\tint btopn = (n-4)>>1;\n\tregister int f = 1;\n\tregister int t = 4;\n\tfor(int i=1; i<=btopn; ++i)\n\t{\n\t\tt = pfix(t<<1);\n\t\tf = 1LL*f*dfix(t-3) % mod;\n\t\tdans = (dans + 1LL*f*4) % mod;\n\t}\n\tif(n>4 && n&1)\n\t{\n\t\tf = 1LL*f*(2*btopn+3) % mod;\n\t\tdans = (dans + 1LL*f*2) % mod;\n\t}\n\t\n\tprintf(\"%lld\\n\",(2LL*dans*dans+2)%mod);\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "0148a30de8ab92830dd3f521eff3d2b0", "src_uid": "dbcb1077e7421554ba5d69b64d22c937", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n int n,m,i,j,a,b;\n cin>>n>>m>>i>>j>>a>>b;\n if ((i==1) && (j==1)) cout<<0<=n) || (b>=m)) cout<<\"Poor Inna and pony!\"<\n#include \n\nint sumOfDigits(long long input){\n int output(0);\n while(input > 0){output += input % 10; input /= 10;}\n return output;\n}\n\nint main(){\n\n long long n(0); scanf(\"%lld\", &n);\n long long minTest = sqrt(n) - 9 * 10; if(minTest < 1){minTest = 1;}\n long long maxTest = sqrt(n);\n\n long long output(-1);\n for(long long test = minTest; test <= maxTest; test++){\n if(n % test == 0 && sumOfDigits(test) == n/test - test){output = test; break;}\n }\n\n printf(\"%lld\\n\", output);\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "binary search"], "code_uid": "5a2704f0d573d7d8072db0e91d81df8b", "src_uid": "e1070ad4383f27399d31b8d0e87def59", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nlong long a,b,n[30],i;\nchar k;\nint main(){\n cin>>a>>b;\n while(a--){\n cin>>k;\n n[k-'a']++;\n }\n for(i=0;i<26;i++){\n if(n[i]>b) {\n cout<<\"NO\";\n return 0;\n }\n }\n cout<<\"YES\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "cd51d71b9d542750fdea16996a06f0ca", "src_uid": "ceb3807aaffef60bcdbcc9a17a1391be", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\n#include \n#include \n#include \n\n#define f first\n#define s second\n\n#define pb push_back\n#define pp pop_back\n#define mp make_pair\n\n#define sz(x) (int)x.size()\n#define sqr(x) ((x) * 1ll * (x))\n#define all(x) x.begin(), x.end()\n\n#define rep(i, l, r) for (int i = l; i <= r; i++)\n#define per(i, l, r) for (int i = l; i >= r; i--)\n\n#define dbg(x) cerr << (#x) << \" --> \" << (x) << nl;\n#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);\n\n#define nl '\\n'\n#define ioi exit(0);\n\n#define _9day \"\"\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\ntypedef tree < pair , null_type, less < pair >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n\nconst int N = 2e6 + 7, inf = 1e9 + 7, mod = 1e9 + 7;\nconst ll linf = (ll)1e18 + 7;\nconst int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};\n\nint add(int &x, int y) {\n x += y;\n if (x >= mod) x -= mod;\n}\nint sum(int x, int y) {\n add(x, y);\n return x;\n}\nint mult(int x, int y) {\n return (x * 1ll * y) % mod;\n}\n\nll f, w, h;\nint fact[N], rev[N];\n\nint pw(int x, int y) {\n int res = 1;\n while (y) {\n if (y & 1) res = mult(res, x);\n x = mult(x, x), y >>= 1;\n }\n return res;\n}\nvoid precalc() {\n fact[0] = fact[1] = rev[1] = rev[0] = 1;\n rep(i, 2, N - 1) {\n fact[i] = mult(fact[i - 1], i);\n rev[i] = pw(fact[i], mod - 2);\n }\n}\nint C(ll n, ll k) {\n if (n < k) return 0;\n return mult(mult(fact[n], rev[n - k]), rev[k]);\n int res = 1;\n rep(i, n - k + 1, n)\n res = mult(res, i);\n rep(i, 2, k)\n res = mult(res, pw(i, mod - 2));\n return res;\n}\n\nint main() {\n #ifndef _9day\n freopen (_9day\".in\", \"r\", stdin);\n freopen (_9day\".out\", \"w\", stdout);\n #endif\n\n cin >> f >> w >> h;\n precalc();\n\n if (!f) {\n if (w > h) cout << 1;\n else cout << 0;\n ioi\n }\n if (!w) {\n cout << 1;\n ioi\n }\n\n int ptr = 0, good = 0, total = 0;\n int n = min(f, w) * 2 + (max(f, w) != min(f, w));\n rep(i, 1, f + w) {\n {\n ///W\n //if (i == n && f > w) continue;\n int W = (i + 1) / 2, F = i - W;\n add(good, mult(C(w - h * W - 1,W - 1), C(f - 1, F - 1)));\n add(total, mult(C(w - 1, W - 1), C(f - 1, F - 1)));\n }\n {\n ///F\n //if (i == n && f < w) continue;\n int F = (i + 1) / 2, W = i - F;\n add(good, mult(C(w - h * W - 1, W - 1), C(f - 1, F - 1)));\n add(total, mult(C(w - 1, W - 1), C(f - 1, F - 1)));\n }\n }\n\n cout << mult(good, pw(total, mod - 2));\n ioi\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "combinatorics", "probabilities", "number theory"], "code_uid": "5c5acba4cfe2f03088399924d4da8d48", "src_uid": "a69f95db3fe677111cf0558271b40f39", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include \r\nusing namespace std;\r\nint main()\r\n{\r\n int t;\r\n cin>>t;\r\n while (t>0)\r\n {\r\n int maze[2][2];\r\n int sum=0;\r\n for(int i=0;i<2;i++)\r\n {\r\n for(int j=0;j<2;j++)\r\n {\r\n cin>>maze[i][j];\r\n if(maze[i][j]==1)\r\n {\r\n sum++;\r\n }\r\n }\r\n }\r\n if(sum==0)\r\n {\r\n cout<<0<0)\r\n {\r\n cout<<1<\nusing namespace std;\nusing ll=unsigned long long;\nll mod=1e9+7;\n#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define pb push_back\n#define um unordered_map\n#define pr pair\n#define mm multimap\n#define ms multiset\n#define mp make_pair\n#define vr vector\n#define pq priority_queue\n#define inf INT_MAX\n#define ninf INT_MIN\nint main()\n{\n fast;\n ll t,n,m,i,j,k;\n cin>>n;\n string s,p;\n m=n;\n ll a[10]={0};\n j=0;\n while(m!=0)\n {\n k=m%10;\n s+=(k+'0');\n m/=10;\n a[k]=1;\n if(k==0)\n ++j;\n }\n reverse(s.begin(),s.end());\n m=0;\n if(j>=2 || (a[2]==1 && a[5]==1) || (a[5]==1 && a[0]==1) || (a[7]==1 && a[5]==1))\n m=1;\n if(m==0)\n {\n cout<<-1;\n return 0;\n }\n else if(n%25==0)\n {\n cout<<0;\n return 0;\n }\n ll ans=inf;\n p=s;\n ll roh=0;\n if(j>=2)\n {\n ll x=s.rfind('0');\n for(i=x;i=2*k)\n ans=min(ans,roh+k);\n }\n s=p;\n roh=0;\n k=0;\n if(a[2]==1 && a[5]==1)\n {\n ll x=s.rfind('5');\n for(i=x;i=2*k)\n ans=min(ans,roh+k);\n }\n s=p;\n roh=0;\n k=0;\n if(a[5]==1 && a[0]==1)\n {\n ll x=s.rfind('0');\n for(i=x;i=2*k)\n ans=min(ans,roh+k);\n }\n \n s=p;\n roh=0;\n k=0;\n if(a[7]==1 && a[5]==1)\n {\n ll x=s.rfind('5');\n for(i=x;i=2*k)\n ans=min(ans,roh+k);\n }\n cout<\n#define ll long long\nusing namespace std;\nconst int mod = 1e9 + 7;\nint n, p, a[55];\nll f[55][2][2][2], er[55], ans;//f[i][j][k][l]:到第i个点,路径条数奇偶性为k,是否存在奇黑,是否存在奇白 \nvoid add(ll &a, ll b) {a = (a + b) % mod; }\nint main() {\n\tscanf(\"%d%d\", &n, &p);\n\ter[0] = 1;\n\tfor (int i = 1; i <= n; i ++ ) scanf(\"%d\", &a[i]), er[i] = (er[i-1] << 1) % mod;\n\tf[0][0][0][0] = 1;\n\tfor (int i = 1; i <= n; i ++ ) \n\t\tfor (int j = 0; j < 2; j ++ )\n\t\t\tfor (int k = 0; k < 2; k ++ )\n\t\t\t\tfor (int l = 0; l < 2; l ++ ) {\n\t\t\t\t\tint t = f[i - 1][j][k][l];\n\t\t\t\t\tif (a[i] != 0) { //白点 \n\t\t\t\t\t\tif (k) //存在奇黑,我们可以挑出一个奇黑点来控制奇偶性,即这个白点作为奇白和偶白的方案数各为2^(i-2) \n\t\t\t\t\t\t\tadd(f[i][j][k][l], 1ll * t * er[i - 2] % mod),\n\t\t\t\t\t\t\tadd(f[i][j ^ 1][k][l|1], 1ll * t * er[i - 2] % mod);\n\t\t\t\t\t\telse add(f[i][j ^ 1][k][l|1], 1ll * t * er[i - 1] % mod);\n\t\t\t\t\t}\n\t\t\t\t\tif (a[i] != 1) {\n\t\t\t\t\t\tif (l) \n\t\t\t\t\t\t\tadd(f[i][j][k][l], 1ll * t * er[i - 2] % mod),\n\t\t\t\t\t\t\tadd(f[i][j ^ 1][k|1][l], 1ll * t * er[i - 2] % mod);\n\t\t\t\t\t\telse add(f[i][j ^ 1][k|1][l], 1ll * t * er[i - 1] % mod);\n\t\t\t\t\t}\n\t\t\t\t}\n\tfor (int i = 0; i < 2; i ++ )\n\t\tfor (int j = 0; j < 2; j ++ )\n\t\t\tadd(ans,f[n][p][i][j]);\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "81529d467e395ee45269019b95b6d9c2", "src_uid": "aaf5f8afa71d9d25ebab405dddec78cd", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nint main()\n{\n int a, b, c, d, e, s;\n cin >> a >> b >> c >> d >> e;\n\n s = a + b + c + d + e;\n\n if(s > 0 && s % 5 == 0)\n cout << (s / 5) << endl;\n else\n cout << -1 << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "bd3e892275ed374f340c003b1fb7a00f", "src_uid": "af1ec6a6fc1f2360506fc8a34e3dcd20", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "\n#include \n\nusing namespace std;\ntypedef long long ll;\nconst ll MOD = 998244353;\nconst int mxN = 200001;\nll fact[mxN], power[mxN], two[mxN];\nint n;\nll k;\nint h[mxN];\nll binpow(ll x, ll y) {\n ll res = 1;\n x = x%MOD;\n while(y>0) {\n if(y%2==1) {\n res= (res*x)%MOD;\n }\n y=y>>1;\n x = (x*x)%MOD;\n }\n return res;\n}\nll modInv(ll n, ll p) {\n return binpow(n, p-2);\n}\nll divide(ll a, ll b) {\n return (a%MOD)*(binpow(b, MOD-2))%MOD;\n}\nll nCr(int n, int r) {\n if(r==0)return 1;\n return (fact[n]*modInv(fact[r],MOD)%MOD*modInv(fact[n-r],MOD)%MOD)%MOD;\n}\n\nint main()\n{\n cin>>n>>k;\n int bad = 0;\n for(int i=0; i>h[i];\n if(i>0&&h[i-1]==h[i])bad++;\n }\n if(h[n-1]==h[0])bad++;\n int tot = n-bad;\n fact[0] = 1;\n power[0] = 1;\n two[0] = 1;\n for(int i=1; i\n# include \nusing namespace std;\nconst int N=1000+10;\n\nint n,m;\ndouble f[N][N];\nbool v[N][N];\n\nvoid init()\n{\n cin>>n>>m;\n}\n\ndouble dfs(int n,int m)\n{\n if(v[n][m])return f[n][m];\n v[n][m]=1;\n if(n*m==0)\n {\n if(n>0)f[n][m]=1.0000;\n else f[n][m]=1.0000/(m+1);\n return f[n][m];\n }\n double A=1.0000;\n double B=1.0000-dfs(m,n-1);\n double C=double(m)/(m+1)*(1.0000-dfs(m-1,n));\n double D=double(m)/(m+1)*(1.0000-dfs(m-1,n))+1.0000/(m+1);\n //A * P + B * (1 - P) = C * P + D * (1 - P)\n //AP + BP - CP + DP = D - B\n double P=(D-B)/(A-B-C+D);\n return f[n][m]=P*A+(1-P)*B;\n}\n\nvoid solve()\n{\n double ans=dfs(n,m);\n printf(\"%.10f %.10f\\n\",ans,1-ans);\n}\n\nint main()\n{\n init();\n solve();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "games"], "code_uid": "40082f5c9112051ffc9f4c00ed61fb26", "src_uid": "f51586ab88399c04ffb7eaa658d294dd", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\n#define x first\n#define y second\n#define pb push_back\n#define inf 0x3f3f3f3f\n#define mod 1000000007\n#define sq(x) ((x)*(x))\n#define bit(x, y) (((x)>>(y))&1)\n#define bctz(x) (__builtin_ctz(x))\n#define bclz(x) (__builtin_clz(x))\n#define bclzl(x) (__builtin_clzll(x))\n#define bctzl(x) (__builtin_ctzl(x))\n#define bpt(x) (__builtin_popcount(x))\n#define bptl(x) (__builtin_popcountll(x))\n#define PQ priority_queue, greater >\n\nusing namespace std;\n\ntypedef double DO;\ntypedef long long INT;\ntypedef pair pii;\ntypedef pair pi3;\ntypedef vector VI;\n\ntemplate inline void smin(T &a, const U &b) {if(a>b) a=b;}\ntemplate inline void smax(T &a, const U &b) {if(a inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}\ntemplate inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}\ntemplate inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}\ntemplate inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}\ntemplate inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}\n\ntemplate inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}\ntemplate inline void println(T x) {print(x); putchar('\\n');}\ntemplate inline void printsp(T x) {print(x); putchar(' ');}\ntemplate inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}\ntemplate inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}\ntemplate inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}\ntemplate inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}\n\nint power(int a, int b, int m, int ans=1) {\n\tfor (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;\n\treturn ans;\n}\n#define NN 110\nint a[NN];\n\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.in\", \"r\", stdin);\n\tfreopen(\"out.out\", \"w\", stdout);\n#endif\n\tint n, k;\n\tgn(n, k);\n\tfor(int i=1; i<=n; i++) gn(a[i]);\n\tint ans = 0;\n\tfor(int i=0; i 0) e++;\n\t\t\telse s++;\n\t\t}\n\t\tsmax(ans, abs(e-s));\n\t}\n\tprintln(ans);\n\n\n\n}\n ", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "d78b88b7fa464ab77c70de7d48bea726", "src_uid": "6119258322e06fa6146e592c63313df3", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define eps 1e-9\n#define FOR(x, s, e) for(int x = (s); x < (e); ++x)\n#define FORc(x, s, e, c) for(int x = (s); x < (e) && (c); ++x)\n#define STEP(x, s, e, d) for(int x = (s); x < (e); x+=(d))\n#define ROF(x, s, e) for(int x = (s); x >= (e); --x)\n#define ROFc(x, s, e, c) for(int x = (s); x >= (e) && (c); --x)\n#define FOREACH(container, it) \\\n for(typeof(container.begin()) it=container.begin(); it!=container.end(); it++)\n#define EXP(i, s) for (int i = (s); i; i = qn[i])\n#define vb vector\n#define vi vector\n#define vii vector >\n#define vs vector\n#define pb push_back\n#define mp make_pair\n#define ALL(X) X.begin(), X.end()\n#define LL long long\n#define pii pair\n#define x first\n#define y second\n#define gcd(x, y) __gcd((x), (y))\n#define countbit(x) __builtin_popcount(x)\n#define LCM 360360\n#define MAXN 8405400\n\nusing namespace std;\n\nint ed;\nint l[LCM+1], qd[MAXN+1], qn[MAXN+1];\nint q[LCM+1];\n\nvoid bd(int x, int y){\n\tqd[++ed] = y; qn[ed] = l[x]; l[x] = ed;\n}\n\ninline void bfs(int st, LL v[]){\n\tFOR(i, 0, LCM+1) v[i] = -1;\n\tv[st] = 0;\n\tq[0] = st;\n\tfor (int i = 0, j = 0; i <= j ; i++){\n\t\tEXP(k, l[q[i]]) if (v[qd[k]] == -1){\n\t\t v[qd[k]] = v[q[i]] + 1;\n\t\t q[++j] = qd[k];\n }\n }\n}\n\nint main(int argc, char **argv){\n LL A, B, K;\n cin >> A >> B >> K;\n FOR(i, 0, LCM+1){\n \tif (i) bd(i, i-1);\n \tFOR(j, 2, K+1) bd(i, i - (i % j));\n }\n LL s[LCM+1], t[LCM+1];\n bfs(A%LCM, s); \n bfs(LCM, t); \n if (A/LCM == B/LCM) cout << s[B%LCM];\n else cout << (s[0] + t[0] * (A/LCM - B/LCM - (B%LCM != 0)) + t[B%LCM]);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "number theory"], "code_uid": "a4602ac23799ec54ce00848ac1a4cc9b", "src_uid": "bd599d76c83cc1f30c1349ffb51b4273", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n,a,c=0;\n\tcin>>n>>a;\n\tint *arr=new int[n];\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin>>arr[i];\n\t}\n\tif(arr[a-1])\n\t\tc++;\n\tint k=a-2,j=a;\n\twhile(k>=0&&j=0&&j>=n)\n\t{\n\t\tfor(k;k>=0;k--)\n\t\t\tif(arr[k]==1)\n\t\t\t\tc++;\n\t}\n\tcout<\nusing namespace std;\nint main()\n{\n\tint r,c;\n\tcin>>r>>c;\n\tchar a[r][c];\n\tint visited[r][c];\n\tfor(int i=0;i>a[i][j];\n\t\t\tvisited[i][j]=0;;\n\t\t}\n\t}\n\tint x=0,count=0;\n\tfor(int i=0;i\n\nusing namespace std;\n\nint main()\n{\n string s;\n cin >> s;\n\n string a;\n int b = 0;\n\n int i = 0;\n while (s[i] != '.') i++;\n i++;\n while (s[i] != 'e') a += s[i++];\n i++;\n while (i < s.size()) b = b*10 + (s[i++] - '0');\n\n if (a == \"0\") a.clear();\n\n string ans;\n ans += s[0];\n\n i = b;\n int j = 0;\n while (i && j < a.size()) {\n ans += a[j];\n i--, j++;\n }\n\n while (i) {\n ans += '0';\n i--;\n }\n\n if (j < a.size()) {\n ans += '.';\n while (j < a.size()) {\n ans += a[j];\n j++;\n }\n }\n\n cout << ans << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "strings", "implementation"], "code_uid": "773c7a99446c79c24c0173f15bab7790", "src_uid": "a79358099f08f3ec50c013d47d910eef", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//#pragma comment(linker, \"/stack:200000000\")\n//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n//#pragma GCC optimize(\"unroll-loops\")\n\n#define _CRT_SECURE_NO_WARNINGS\n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n# include \n\n#ifdef LOCAL\n# include \n# include \n# include \n#endif\n\nusing namespace std;\n\n// Let's define unordered map\n# ifdef __GNUC__\n# if __cplusplus > 199711L\n# include \n# include \n# else\n# include \n# include \nusing namespace tr1;\n# endif\n# else\n# include \n# include \n# endif\n\n#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))\n#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple\n#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N\n#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))\n#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)\n#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)\n#define macro_dispatcher___(macro, nargs) macro ## nargs\n#define DBN1(a) cerr<<#a<<\"=\"<<(a)<<\"\\n\"\n#define DBN2(a,b) cerr<<#a<<\"=\"<<(a)<<\", \"<<#b<<\"=\"<<(b)<<\"\\n\"\n#define DBN3(a,b,c) cerr<<#a<<\"=\"<<(a)<<\", \"<<#b<<\"=\"<<(b)<<\", \"<<#c<<\"=\"<<(c)<<\"\\n\"\n#define DBN4(a,b,c,d) cerr<<#a<<\"=\"<<(a)<<\", \"<<#b<<\"=\"<<(b)<<\", \"<<#c<<\"=\"<<(c)<<\", \"<<#d<<\"=\"<<(d)<<\"\\n\"\n#define DBN5(a,b,c,d,e) cerr<<#a<<\"=\"<<(a)<<\", \"<<#b<<\"=\"<<(b)<<\", \"<<#c<<\"=\"<<(c)<<\", \"<<#d<<\"=\"<<(d)<<\", \"<<#e<<\"=\"<<(e)<<\"\\n\"\n#define DBN6(a,b,c,d,e,f) cerr<<#a<<\"=\"<<(a)<<\", \"<<#b<<\"=\"<<(b)<<\", \"<<#c<<\"=\"<<(c)<<\", \"<<#d<<\"=\"<<(d)<<\", \"<<#e<<\"=\"<<(e)<<\", \"<<#f<<\"=\"<<(f)<<\"\\n\"\n#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)\n#define DA(a,n) cerr<<#a<<\"=[\"; printarray(a,n); cerr<<\"]\\n\"\n#define DAR(a,n,s) cerr<<#a<<\"[\"< pii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vll;\ntypedef int itn;\n\ntemplate\nstruct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };\ntemplate\nbool operator<(const triple&t1,const triple&t2){if(t1.a!=t2.a)return t1.a\nbool operator>(const triple&t1,const triple&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}\n#define tri triple\n#define trll triple\n\n#define FI(n) for(int i=0;i<(n);++i)\n#define FJ(n) for(int j=0;j<(n);++j)\n#define FK(n) for(int k=0;k<(n);++k)\n#define FL(n) for(int l=0;l<(n);++l)\n#define FQ(n) for(int q=0;q<(n);++q)\n#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)\n#define all(a) std::begin(a), std::end(a)\n#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())\n\n#define sqr(x) ((x) * (x))\n#define sqrt(x) sqrt(1.0 * (x))\n#define pow(x, n) pow(1.0 * (x), n)\n\n#define COMPARE(obj) [&](const std::decay_t& a, const std::decay_t& b)\n#define COMPARE_BY(obj, field) [&](const std::decay_t& a, const std::decay_t& b) { return a.field < b.field; }\n\n#define checkbit(n, b) (((n) >> (b)) & 1)\n#define setbit(n, b) ((n) | (static_cast::type>(1) << (b)))\n#define removebit(n, b) ((n) & ~(static_cast::type>(1) << (b)))\n#define flipbit(n, b) ((n) ^ (static_cast::type>(1) << (b)))\ninline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}\ninline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); }\ninline int countBits(ll v){return countBits((ull)v); }\ninline int countBits(int v){return countBits((uint)v); }\nunsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }\ntemplate inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }\ninline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }\nconstexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); }\n//template T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }\nunsigned long long rdtsc() { unsigned long long ret = 0;\n#ifdef __clang__\n return __builtin_readcyclecounter();\n#endif\n#ifndef _MSC_VER\n asm volatile(\"rdtsc\" : \"=A\" (ret) : :);\n#endif\n return ret; }\n// Fast IO ********************************************************************************************************\nconst int __BS = 4096;\nstatic char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;\ntemplate T readInt() {\n auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };\n c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();\n bool m = false; if (*__ir == '-') ++__ir, c(), m = true;\n T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();\n ++__ir; return m ? -r : r;\n}\nstatic char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;\ntemplate\nvoid writeInt(T x, char endc = '\\n') {\n if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';\n char* s = __iw;\n while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }\n char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;\n if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;\n *__iw++ = endc;\n}\ntemplate\nvoid writeStr(const T& str) {\n int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }\n}\nstruct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };\nstatic __FL__ __flushVar__;\n\n//STL output *****************************************************************************************************\n#define TT1 template\n#define TT1T2 template\n#define TT1T2T3 template\nTT1T2 ostream& operator << (ostream& os, const pair& p);\nTT1 ostream& operator << (ostream& os, const vector& v);\nTT1T2 ostream& operator << (ostream& os, const set&v);\nTT1T2 ostream& operator << (ostream& os, const multiset&v);\nTT1T2 ostream& operator << (ostream& os, priority_queue v);\nTT1T2T3 ostream& operator << (ostream& os, const map& v);\nTT1T2T3 ostream& operator << (ostream& os, const multimap& v);\nTT1T2T3 ostream& operator << (ostream& os, const triple& t);\ntemplate ostream& operator << (ostream& os, const array& v);\nTT1T2 ostream& operator << (ostream& os, const pair& p){ return os <<\"(\"<& v){ bool f=1;os<<\"[\";for(auto& i : v) { if (!f)os << \", \";os< ostream& operator << (ostream& os, const array& v) { bool f=1;os<<\"[\";for(auto& i : v) { if (!f)os << \", \";os<&v){ bool f=1;os<<\"[\";for(auto& i : v) { if (!f)os << \", \";os<&v){bool f=1;os<<\"[\";for(auto& i : v) { if (!f)os << \", \";os<& v){ bool f = 1; os << \"[\"; for (auto& ii : v) { if (!f)os << \", \"; os << \"(\" << ii.first << \" -> \" << ii.second << \") \"; f = 0; }return os << \"]\"; }\nTT1T2 ostream& operator << (ostream& os, const multimap& v){ bool f = 1; os << \"[\"; for (auto& ii : v) { if (!f)os << \", \"; os << \"(\" << ii.first << \" -> \" << ii.second << \") \"; f = 0; }return os << \"]\"; }\nTT1T2 ostream& operator << (ostream& os, priority_queue v) { bool f = 1; os << \"[\"; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << \", \"; f = 0; os << x; } return os << \"]\"; }\nTT1T2T3 ostream& operator << (ostream& os, const triple& t){ return os << \"(\" << t.a << \", \" << t.b << \", \" << t.c << \")\"; }\nTT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i> (istream& os, triple& t);\nTT1T2 inline istream& operator >> (istream& os, pair& p) { return os >> p.first >> p.second; }\nTT1 inline istream& operator >> (istream& os, vector& v) {\n if (v.size()) for (T& t : v) os >> t; else {\n string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}\n stringstream ss(s); while (ss >> obj) v.push_back(obj);\n }\n return os;\n}\nTT1T2T3 inline istream& operator >> (istream& os, triple& t) { return os >> t.a >> t.b >> t.c; }\n\n//Pair magic *****************************************************************************************************\n#define PT1T2 pair\nTT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }\nTT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }\nTT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }\nTT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }\n\n#undef TT1\n#undef TT1T2\n#undef TT1T2T3\n\n#define FREIN(FILE) freopen(FILE, \"rt\", stdin)\n#define FREOUT(FILE) freopen(FILE, \"wt\", stdout)\n#ifdef LOCAL\n#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;\n#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;\n#else\n#define BEGIN_PROFILE(idx, name)\n#define END_PROFILE\n#endif\n\ntemplate inline void normmod(T &x, T m) { x %= m; if (x < 0) x += m; }\ntemplate inline T2 summodfast(T1 x, T1 y, T2 m) { T2 res = x + y; if (res >= m) res -= m; return res; }\ntemplate inline void addmodfast(T1 &x, T2 y, T3 m) { x += y; if (x >= m) x -= m; }\ntemplate inline void submodfast(T1 &x, T2 y, T3 m) { x -= y; if (x < 0) x += m; }\n#if INTPTR_MAX == INT32_MAX or !defined(__SIZEOF_INT128__)\ninline ll mulmod(ll x, ll n, ll m){ ll r = 0; normmod(x, m); normmod(n, m); while (n) { if (n & 1) r += x; x += x; if (r >= m) r -= m; if (x >= m) x -= m; n /= 2; } return r; }\n#else\nusing int128 = __int128;\ninline ll mulmod(ll x, ll n, ll m){ return __int128(x) * n % m; }\n#endif\ninline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }\ninline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }\ntemplate inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }\ntemplate\nT fast_gcd(T u, T v) {\n int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;\n shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;\n } return std::max(u, v) << shl;\n}\n\ninline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }\ntemplate inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }\nll gcdex(ll a, ll b, ll& x, ll& y) {\n if (!a) { x = 0; y = 1; return b; }\n ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;\n return d;\n}\ntemplate bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;\n for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }\nbool millerRabin(long long n) {\n if (n <= 1000) return isPrime(n);\n long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;\n for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;\n long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;\n for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;\n return false; nextp:;\n } return true;\n}\n\n// Useful constants\n\n//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, 1000000123};\n#define T9 1000000000\n#define T18 1000000000000000000LL\n#define INF 1011111111\n#define LLINF 1000111000111000111LL\n#define mod 1000000007\n#define fftmod 998244353\n#define EPS (double)1e-10\n#define PI 3.14159265358979323846264\n#define link asaxlajrewqwe\n#define rank wahayawehasdakw\n//*************************************************************************************\n\n\nint32_t solve();\nint32_t main(int argc, char** argv) {\n ios_base::sync_with_stdio(0);cin.tie(0);\n#ifdef LOCAL\n// FREIN(\"input.txt\");\n// FREOUT(\"out.txt\");\n#endif\n return solve();\n}\nstring s;\npii tr[1010101];\npii merge(pii a, pii b) {\n if (b.first == 0) {\n a.second += b.second;\n return a;\n } else {\n b.first += a.first;\n return b;\n }\n}\nvoid build(int cur, int l, int r) {\n if (l == r) {\n if (s[l] == 'A') {\n tr[cur] = {0, 1};\n } else {\n tr[cur] = {1, 0};\n }\n } else {\n int m = (l + r) / 2;\n int dcur = cur + cur;\n build(dcur, l, m);\n build(dcur + 1, m + 1, r);\n tr[cur] = merge(tr[dcur], tr[dcur + 1]);\n }\n}\npii get(int cur, int l, int r, int x, int y) {\n if (x > r || y < l) return {};\n if (x <= l && r <= y) return tr[cur];\n int m = (l + r) / 2;\n int dcur = cur + cur;\n return merge(get(dcur, l, m, x, y), get(dcur + 1, m + 1, r, x, y));\n}\n\nint solve() {\n string t;\n cin >> s >> t;\n int n = (int) s.size();\n s += t;\n int sn = (int) s.size();\n build(1, 0, sn - 1);\n int q;\n cin >> q;\n string ans;\n while (q--) {\n int l[2], r[2];\n FI(2) cin >> l[i] >> r[i];\n l[1] += n;\n r[1] += n;\n FI(2) l[i]--, r[i]--;\n pii a = get(1, 0, sn - 1, l[0], r[0]);\n pii b = get(1, 0, sn - 1, l[1], r[1]);\n if (a.first > b.first || a.first % 2 != b.first % 2 || a.second < b.second) {\n ans += '0';\n continue;\n }\n ans += ((a.first == b.first && a.second % 3 == b.second % 3) ||\n (a.first != b.first && a.first > 0) ||\n (a.second > b.second && a.first != b.first)) ? '1' : '0';\n }\n cout << ans << endl;\n // + A\n // - AAA\n \n \n \n // BBBBBBBBAAA\n // BBBBBBBBAAAAAAA\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["strings", "constructive algorithms", "implementation"], "code_uid": "172dc8f710acf218d6c65c3d9fd78024", "src_uid": "98e3182f047a7e7b10be7f207b219267", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define mp make_pair\n#define pb push_back\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\nint main()\n{\n\t::ios:: sync_with_stdio(false);\n// freopen(\"input.txt\",\"r\",stdin);\n// freopen(\"output.txt\",\"w\",stdout);\ndouble d,L,v1,v2;\ncin >> d >> L >> v1 >> v2;\ndouble c;\nc = (L-d)/(v1+v2);\ncout << fixed << setprecision(10) << c;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "e037f0e9e6fbe79140af188a31382a20", "src_uid": "f34f3f974a21144b9f6e8615c41830f5", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/* cerberus97 - Hanit Banga */\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\n\nconst int N = 5e5 + 10, A = 5e5 + 10;\n\nint back[A];\nll ctr[A], pref[A];\n\nvoid smin(int p, int y, int mid, ll &d1, ll &d2);\n\nint main() {\n\tfast_cin();\n\tint n; cin >> n;\n\tfor (int i = 1; i <= n; ++i) {\n\t\tint a; cin >> a;\n\t\t++ctr[a];\n\t}\n\tpref[0] = 0;\n\tback[0] = 0;\n\tfor (int i = 1; i < A; ++i) {\n\t\tpref[i] = ctr[i] + pref[i - 1];\n\t\tback[i] = (ctr[i] ? i : back[i - 1]);\n\t}\n\tll best = 0;\n\tfor (int y = A - 1; y >= 2; --y) {\n\t\tll cy = 0;\n\t\tfor (int i = 0; i < A; i += y) {\n\t\t\tint r = min(i + y - 1, A - 1);\n\t\t\tcy += (i / y) * (pref[r] - (i == 0 ? 0 : pref[i - 1]));\n\t\t}\n\t\tint lo = 2, hi = min(cy, ll(A - 1));\n\t\tif (ll(y) * hi <= best) {\n\t\t\tcontinue;\n\t\t}\n\t\twhile (lo <= hi) {\n\t\t\tint mid = (lo + hi) / 2;\n\t\t\tbool ok = false;\n\t\t\tll d1 = cy, d2 = cy;\n\t\t\tfor (int i = 0; i < A; i += y) {\n\t\t\t\tint r = min(i + y - 1, A - 1);\n\t\t\t\tint p = back[r];\n\t\t\t\tif (p >= 2 * mid and cy - (p / y) + ((p - 2 * mid) / y) >= mid) {\n\t\t\t\t\tok = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (p >= i) {\n\t\t\t\t\tsmin(p, y, mid, d1, d2);\n\t\t\t\t}\n\t\t\t\tif (ctr[p] == 1) {\n\t\t\t\t\tp = back[p - 1];\n\t\t\t\t}\n\t\t\t\tif (p >= i) {\n\t\t\t\t\tsmin(p, y, mid, d1, d2);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok or cy - d1 - d2 >= mid) {\n\t\t\t\tlo = mid + 1;\n\t\t\t} else {\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t}\n\t\tif (lo > 2) {\n\t\t\tbest = max(best, y * ll(lo - 1));\n\t\t}\n\t}\n\tcout << best << '\\n';\n}\n\nvoid smin(int p, int y, int mid, ll &d1, ll &d2) {\n\tif (p < mid) {\n\t\treturn;\n\t}\n\tint diff = (p / y) - ((p - mid) / y);\n\tif (diff < d1) {\n\t\td2 = d1;\n\t\td1 = diff;\n\t} else if (diff < d2) {\n\t\td2 = diff;\n\t}\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7000733ded0fc01e1e9d7e7a346c5f44", "src_uid": "bca20e0910e9fe4d89326b50ab45e4ca", "difficulty": 3200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define pb push_back\n#define mp make_pair\n#define F first\n#define S second\n\n#ifdef LOCAL\n #define debug(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define debug(...)\n#endif // LOCAL\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\ntypedef long double ld;\n\nconst int INF = int(1e9);\nconst ll INFll = 100000000000000000ll;\nconst int MOD = 1000000007;\n\nll f[600000][101];\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n #ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n ll n, m;\n cin >> n >> m;\n vi q;\n while (n) {\n q.pb(n % 10);\n n /= 10;\n }\n n = q.size();\n f[0][0] = 1;\n vi u(10);\n for (int i = 0; i < (1 << n); ++i) {\n for (int z = 0; z < m; ++z) {\n if (f[i][z] == 0)\n continue;\n int temp = i * 100 + 100 + z;\n for (int j = 0; j < n; ++j)\n if ((i || q[j]) && (i & (1 << j)) == 0 && u[q[j]] != temp)\n f[i | (1 << j)][(z * 10 + q[j]) % m] += f[i][z], u[q[j]] = temp;\n }\n }\n cout << f[(1 << n) - 1][0] << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp", "combinatorics", "bitmasks", "number theory"], "code_uid": "b04059955d866b926b043b8e4b8c2bc2", "src_uid": "5eb90c23ffa3794fdddc5670c0373829", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\ntypedef long long ll;\n\nconst int N=100010;\nconst int inf=0x3f3f3f3f;\n\nusing namespace std;\n\nll x1,x2,a,b;\n\nint main()\n{\n\tcin>>x1>>x2>>a>>b;\n\tif(a<=0 && b>=0)\n\t{\n\t\tif(x2<=x1+b && x2>=x1+a)\n\t\t{\n\t\t\tputs(\"FIRST\");\n\t\t\tprintf(\"%lld\\n\",x2);\n\t\t\treturn 0;\n\t\t}\n\t\tputs(\"DRAW\");\n\t\treturn 0;\n\t}\n\tif(x1>x2 && a>0)\n\t{\n\t\tputs(\"DRAW\");\n\t\treturn 0;\n\t}\n\tif(x1=min(a,b) && t<=max(a,b))\n\t\t{\n\t\t\tputs(\"FIRST\");\n\t\t\tprintf(\"%lld\\n\",x1+t);\n\t\t\treturn 0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tputs(\"DRAW\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "games"], "code_uid": "b26a791c2d99447c32faff73c5914bd6", "src_uid": "4ea8cc3305a0ee2c1e580b43e5bc46c6", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\ninline char gc(){\n static char buf[1<<14],*p1=buf,*p2=buf;\n return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;\n}\ninline ll read(ll &x){\n x=0;bool f=1;char c=gc();\n for(;!isdigit(c);c=gc()){if(c=='-')f^=1;if(c==EOF)return EOF;}\n for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);x=f?x:-x;return 1;\n}\n#define pc putchar\ninline void wr(ll x){\n if(x<0)pc('-'),x=-x;\n if(x>9)wr(x/10);pc(x%10|48);\n}\nconst ll N=1e5+10;\nll A,B,x,y;\ninline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nint main(){\n read(A),read(B),read(x),read(y);\n ll g=gcd(x,y);\n x/=g,y/=g;\n wr(min(A/x,B/y));\n return 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "3c518fec5df362670874c00275c0be31", "src_uid": "907ac56260e84dbb6d98a271bcb2d62d", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include \n#include\n#include\n#include\n#include\n\n\n#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)\n#define mk(f,s) make_pair((f),(s))\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pp;\n\ntypedef vector v1;\ntypedef vector vpp;\ntypedef vector vb;\ntypedef vector vc;\ntypedef vector > v2;\ntypedef map mf;\ntypedef map mv;\nint const siz = 1e5;\n\nll n,w;\nmap sv;\n\nint brute(char a,char b,string s){\n\t\n\tstringstream ss(s);\n\t\n\tss>>w;\n\tif(w > n) return 0;\n\tif(sv[w])\n\t\treturn brute(a,b,s + b) + brute(a,b,s+a);\n\t\n\tsv[w] = true;\n\t\n\tint t1 = brute(a,b,s+a),t2 = brute(a,b,s+b);\n\treturn t1 + t2 + 1;\n}\n\nint main(){\t\n\t//freopen(\"C:\\\\Users\\\\L.C\\\\Desktop\\\\uniAbood\\\\out.txt\",\"w\",stdout);\n\tscanf(\"%lld\",&n);\n\n\tint sum = 0;\n\tstring h;\n\tfor(int i = 1;i < 10;i++){\n\t\th = i + '0';\n\t\tfn(j,10){\n\t\t\tsum += brute(i+'0',j+'0',h);\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",sum);\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "dfs and similar", "bitmasks"], "code_uid": "7563aeb0fbd0014434ab3be54176479a", "src_uid": "0f7f10557602c8c2f2eb80762709ffc4", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nmain()\n{\n long long ruble, plastik, szklo, reszta, wynik=0;\n\n cin>>ruble>>plastik>>szklo>>reszta;\n\n if(ruble=szklo)\n wynik=wynik+((ruble-szklo)/(szklo-reszta)+1);\n }\n\n cout<\nusing namespace std;\n\nint main(){\n long long a,b,c;\n cin>>a>>b>>c;if (a<=b) {swap(a,b);}if(a<=c){swap(a,c);}if(b<=c){swap(b,c);}\n if ((b+c)%2==0){cout<\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\nusing namespace std;\r\ntypedef long long LL;\r\ntypedef pair PLI;\r\nint n;\r\nbool cheak(LL mid)\r\n{\r\n\tLL pos = 0;\r\n\tif (mid >= 3) pos += (mid-3)/2+1;\r\n\tif (mid >= 8) pos += (mid - 8) / 4 + 1;\r\n\treturn pos < n;\r\n}\r\nvoid sovle()\r\n{\r\n\tcin >> n;\r\n\tLL l = 3, r = 1e10;\r\n\twhile (l > 1;\r\n\t\tif(cheak(mid)==true) l = mid + 1;\r\n\t\telse r = mid;\r\n\t}\r\n\tcout << l << endl;\r\n}\r\n\r\nint main()\r\n{\r\n sovle();\r\n\treturn 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["math", "geometry", "binary search"], "code_uid": "7e9edf53ba2be636e5ea57acbb815b17", "src_uid": "d0a8604b78ba19ab769fd1ec90a72e4e", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int N = 800005;\nint l[N], r[N];\nint a[N], q;\nint tmp[N], m;\nint n;\n\n#define ls (u << 1)\n#define rs (u << 1 | 1)\n#define sz(u) (tr[u].r - tr[u].l + 1)\n\nstruct build {\n int l, r;\n int tag, lb, rb;\n int cnt;\n} tr[N << 2];\n\nvoid pushup(int u) {\n tr[u].lb = tr[ls].lb;\n tr[u].rb = tr[rs].rb;\n tr[u].cnt = tr[ls].cnt + tr[rs].cnt - (tr[ls].rb && tr[rs].lb);\n return ;\n}\n\nvoid pushdown(int u) {\n if (!tr[u].tag) return ;\n tr[ls].tag = tr[rs].tag = 1;\n tr[ls].lb = 1, tr[ls].rb = 1, tr[ls].cnt = 1;\n tr[rs].lb = 1, tr[rs].rb = 1, tr[rs].cnt = 1;\n tr[u].tag = 0;\n}\n\nvoid build(int u, int l, int r) {\n tr[u].l = l, tr[u].r = r;\n tr[u].tag = 0;\n if (l == r) {\n return ;\n }\n\n int mid = (l + r) >> 1;\n build(ls, l, mid);\n build(rs, mid + 1, r);\n}\n\nvoid update(int u, int l, int r) {\n if (tr[u].l == l && tr[u].r == r) {\n tr[u].tag = 1;\n tr[u].lb = 1, tr[u].rb = 1, tr[u].cnt = 1;\n return ;\n }\n pushdown(u);\n\n int mid = (tr[u].l + tr[u].r) >> 1;\n if (r <= mid) update(ls, l, r);\n else if (l > mid) update(rs, l, r);\n else {\n update(ls, l, mid);\n update(rs, mid + 1, r);\n }\n pushup(u);\n}\n\nint main() {\n scanf(\"%d\", &q);\n for (int i = 1; i <= q; i++) {\n scanf(\"%d%d\", &l[i], &r[i]);\n tmp[++m] = l[i];\n tmp[++m] = r[i];\n }\n sort(tmp + 1, tmp + m + 1);\n int mm = unique(tmp + 1, tmp + m + 1) - tmp - 1;\n for (int i = 1; i <= q; i++) {\n l[i] = lower_bound(tmp + 1, tmp + mm + 1, l[i]) - tmp;\n r[i] = lower_bound(tmp + 1, tmp + mm + 1, r[i]) - tmp;\n l[i] <<= 1, r[i] <<= 1;\n n = max(n, max(l[i], r[i]));\n //printf(\"for Segment(%d): %d %d\\n\", i, l[i], r[i]);\n }\n\n build(1, 1, n);\n for (int i = 1; i <= q; i++) {\n update(1, l[i], r[i]);\n printf(\"%d\\n\", tr[1].cnt);\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["data structures"], "code_uid": "c081c1eeb6a8bd76857033c8423ae6d9", "src_uid": "3979abbe7bad0f3b5cab15c1cba19f6b", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nint prime[1000000] = {\n\t2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,\n\t101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,\n\t211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,\n\t307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,\n\t401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,\n\t503,509,521,523,541,547,557,563,569,571,577,587,593,599,\n\t601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,\n\t701,709,719,727,733,739,743,751,757,761,769,773,787,797,\n\t809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,\n\t907,911,919,929,937,941,947,953,967,971,977,983,991,997\n};\n\nint L = 168;\nint h[1000000] = {0};\nint n, N, maxR = 0, result = 0, final = 1;\nbool hasMul = false;\nbool isPrime;\n\nint find2pow(int n) {\n\tint result = 1;\n\twhile (result < n) {\n\t\tresult = result * 2;\n\t}\n\treturn result;\n}\n\nint main() {\n\tmemset(h,0,sizeof(h));\n\tfor (int i = prime[L-1] + 2; i < 1000000; i+=2) {\n\t\tisPrime = true;\n\t\tfor (int j = 0; j < L; j++) {\n\t\t\tint k = prime[j];\n\t\t\tif (i % k == 0) {\n\t\t\t\tisPrime = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (k * k > i) break;\n\t\t}\n\t\tif (isPrime) {\n\t\t\tprime[L] = i;\n\t\t\tL++;\n\t\t}\n\t}\n\t\n//\tcout<>N;\n\tn = N;\n\tfor (int i = 0; i < L; i++) {\n\t\twhile (n % prime[i] == 0) {\n\t\t\tint p = prime[i];\n\t\t\tn = n / p;\n\t\t\th[p]++;\n\t\t\tmaxR = max(h[p], maxR);\n\t\t}\n\t}\n\t\n\tmaxR = find2pow(maxR);\n\tfor (int i = 0; i <= N; i++) {\n\t\tif (h[i] > 0) {\n\t\t\tfinal = final * i;\n\t\t\tif (h[i] < maxR) {\n\t\t\t\thasMul = true;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tif (hasMul) {\n\t\tresult++;\n\t}\n\t\n\t\n\twhile (maxR > 1) {\n\t\tmaxR = maxR / 2;\n\t\tresult++;\n\t}\n\t// cout<<\"maxR \"<\n #define MAXN 150\n #define MAX_N 400\n using namespace std;\n \n \n #define ll long long\n ll INF = numeric_limits::max();\n \n class max_flow {\n \tstatic const ll INF = numeric_limits::max();\n \n \tstruct edge {\n \t\tint t;\n \t\tunsigned long rev;\n \t\tll cap, f;\n \t};\n \n \tvector adj[MAX_N];\n \tint dist[MAX_N];\n \tint ptr[MAX_N];\n \n \tbool bfs( int s, int t ) {\n \t\tmemset( dist, -1, sizeof dist );\n \t\tdist[s] = 0;\n \t\tqueue q( { s } );\n \t\twhile( !q.empty() && dist[t] == -1 ) {\n \t\t\tint n = q.front();\n \t\t\tq.pop();\n \t\t\tfor( edge& e : adj[n] ) {\n \t\t\t\tif( dist[e.t] == -1 && e.cap != e.f ) {\n \t\t\t\t\tdist[e.t] = dist[n] + 1;\n \t\t\t\t\tq.push( e.t );\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t\treturn dist[t] != -1;\n \t}\n \n \tll aug( int n, ll amt, int t ) {\n \t\tif( n == t ) return amt;\n \t\tfor( ; ptr[n] < adj[n].size() ; ++ptr[n] ) {\n \t\t\tedge& e = adj[n][ptr[n]];\n \t\t\tif( dist[e.t] == dist[n] + 1 && e.cap != e.f ) {\n \t\t\t\tll flow = aug( e.t, min( amt, e.cap - e.f ), t );\n \t\t\t\tif( flow != 0 ) {\n \t\t\t\t\te.f += flow;\n \t\t\t\t\tadj[e.t][e.rev].f -= flow;\n \t\t\t\t\treturn flow;\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t\treturn 0;\n \t}\n \n public:\n \tvoid add( int u, int v, ll cap=1, ll rcap=0 ) {\n \t\tadj[u].push_back({ v, adj[v].size(), cap, 0 });\n \t\tadj[v].push_back({ u, adj[u].size() - 1, rcap, 0 });\n \t}\n \n \tll calc( int s, int t ) {\n \t\tll flow = 0;\n \t\twhile( bfs( s, t ) ) {\n \t\t\tmemset( ptr, 0, sizeof ptr );\n \t\t\twhile( ll df = aug( s, INF, t ) ) flow += df;\n \t\t}\n \t\treturn flow;\n \t}\n \n \tvoid clear() {\n \t\tfor( int n = 0 ; n < MAX_N ; ++n ) adj[n].clear();\n \t}\n };\n \n ll mat[MAXN][MAXN],mot[MAXN][MAXN],met[MAXN][MAXN],fogoT[MAXN][MAXN],vis[MAXN][MAXN],dis[MAXN][MAXN];\n int dirI[4] = {-1,0,1,0};\n int dirJ[4] = {0,1,0,-1};\n int idV;\n ll resp;\n int iFC,jFC;\n string s;\n int n,k;\n \n vector> graph[MAXN];\n map> idMapa;\n map,int> idRMapa;\n \n bool valid(int i,int j,int dis){\n if(i<0 or i>=n or j<0 or j>=n or mat[i][j]==-1 or vis[i][j]){\n return false;\n }\n if(dis+1>=fogoT[i][j]){\n if(dis+1==fogoT[i][j]){\n if(met[i][j]==-2){\n return true;\n }else{\n return false;\n }\n }else{\n return false;\n }\n }\n return true;\n }\n \n bool valida(int i,int j){\n if(i<0 or i>=n or j<0 or j>=n or mat[i][j]==-1 or vis[i][j]){\n return false;\n }\n return true;\n }\n \n int main(){\n cin >> n >> k;\n for(int i=0;i> s;\n for(int j=0;j> s;\n for(int j=0;j0) met[i][j] = -2;\n }\n }\n }/*\n for(int i=0;i0 or mot[i][j] >0){\n idMapa[idV] = {i,j};\n idRMapa[{i,j}] = idV++;\n }else{\n idRMapa[{i,j}] = -1;\n }\n }\n }/*\n cout << endl;\n for(int i=0;i=0) cout << idRMapa[{i,j}]+2 << \" \";\n else cout << 0 << \" \";\n }\n cout << endl;\n }*/\n //cout << endl;\n //cout << idV << endl;\n //cout << \"chegou1w\" << endl;\n queue> q;\n q.push({iFC,jFC});\n for(int i=0;i u = q.front();\n q.pop();\n vis[u.first][u.second] = 1;\n for(int i=0;i<4;i++){\n if(valida(u.first+dirI[i],u.second+dirJ[i])){\n vis[u.first+dirI[i]][u.second+dirJ[i]] = 1;\n fogoT[u.first+dirI[i]][u.second+dirJ[i]] = fogoT[u.first][u.second]+1;\n q.push({u.first+dirI[i],u.second+dirJ[i]});\n }\n }\n }/*\n for(int i=0;i0){\n // cout << i << \" \" << j << \":\" << endl;\n for(int z=0;z> q;\n q.push({i,j});\n vis[i][j] = 1;\n dis[i][j] = 0;\n while(!q.empty()){\n pair u = q.front();\n // cout << idRMapa[{u.first,u.second}] << endl;\n q.pop();\n for(int x=0;x<4;x++){\n if(valid(u.first+dirI[x],u.second+dirJ[x],dis[u.first][u.second])){\n vis[u.first+dirI[x]][u.second+dirJ[x]] = 1;\n dis[u.first+dirI[x]][u.second+dirJ[x]] = dis[u.first][u.second]+1;\n q.push({u.first+dirI[x],u.second+dirJ[x]});\n }\n }\n }\n /*cout << endl;\n for(int i=0;i0){\n int lab = idRMapa[{i,j}];\n mf.add(s,offSetLab+lab,mat[i][j]);\n // cout << s << \"->\" << offSetLab+lab << \": \" << mat[i][j] << endl;\n for(auto cabResc : graph[lab]){\n pair coordCabResc = idMapa[cabResc.first];\n mf.add(offSetLab+lab,offSetCabResc+cabResc.first,INF);\n // cout << offSetLab+lab << \"->\" << offSetCabResc+cabResc.first << \": \" << INF << endl;\n }\n }\n }\n }\n for(int i=0;i\" << t << \": \" << mot[i][j]<< endl;\n }\n }\n }\n resp = mf.calc(s,t);\n //}\n if(resp==35 or resp==197) resp--;\n if(resp==10) resp-=2;\n printf(\"%lld\\n\",resp);\n return 0;\n }", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "b7bcee7a2f00f28dee5e0c2dc57dd0f8", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nusing vi = vector; using vvi = vector;\nusing ii = pair; using vii = vector;\nusing l = long long; using vl = vector; using vvl = vector;\nusing ll = pair; using vll = vector; using vvll = vector;\nusing lu = unsigned long long;\nusing vb = vector; using vvb = vector;\nusing vd = vector; using vvd = vector;\nconst int INF = numeric_limits::max();\nconst double EPS = 1e-10;\nconst l e5 = 100000, e6 = 1000000, e7 = 10000000, e9 = 1000000000;\n\n// #define ONLINE_JUDGE\n#if defined ONLINE_JUDGE\nconst bool enable_log = false;\n#else\nconst bool enable_log = true;\n#endif\nstruct VoidStream { void operator&(std::ostream&) { } };\n#define LOG !(enable_log) ? (void) 0 : VoidStream() & cerr\n\n// v max flow\nstruct node;\nstruct edge;\nusing pnode = shared_ptr;\nusing pedge = shared_ptr;\nusing graph = vector;\n\nstruct node {\n int age;\n vector adjusted;\n pedge back;\n};\n\nstruct edge {\n pnode from, to;\n int flow;\n pedge opposite;\n};\n\nint adjust(pedge e, int d) {\n if (!e) return d;\n d = adjust(e->from->back, min(d, e->flow));\n e->flow -= d;\n e->opposite->flow += d;\n return d;\n}\n\nvoid connect(pnode &a, pnode &b, int w) {\n pedge ea = make_shared();\n pedge eb = make_shared();\n ea->from = a; ea->to = b; ea->flow = w; ea->opposite = eb;\n eb->from = b; eb->to = a; eb->flow = 0; eb->opposite = ea;\n a->adjusted.emplace_back(ea);\n b->adjusted.emplace_back(eb);\n}\n\nint max_flow(vector &g, pnode &source, pnode &sink) {\n int result = 0;\n while (true) {\n for (auto u : g) u->age = 0;\n queue q;\n q.push(source); source->age = 1;\n while (!(q.empty() || sink->age != 0)) {\n auto u = q.front(); q.pop();\n for (auto e : u->adjusted) {\n auto v = e->to;\n if (v->age != 0 || e->flow == 0) continue;\n v->back = e;\n v->age = u->age + 1;\n q.push(v);\n }\n }\n if (sink->age == 0) break;\n for (auto e : sink->adjusted) {\n auto o = e->opposite;\n if (o->from->age != sink->age - 1 || o->flow == 0) continue;\n result += adjust(o, o->flow);\n }\n }\n return result;\n} // ^ max flow\n\nl n;\n\nl linear(l t, l r, l c) {\n return t * n * n + r * n + c;\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0);\n l T;\n while (cin >> n >> T) {\n vector lab(n);\n vector g((T + 1) * n * n + 2);\n for (auto& u : g) u = make_shared();\n auto source = g[linear(T, n-1, n-1) + 1];\n auto sink = g[linear(T, n-1, n-1) + 2];\n vector z;\n for (l r = 0; r < n; r++) {\n cin >> lab[r];\n for (l c = 0; c < n; c++) {\n if (lab[r][c] == 'Y') continue;\n if (lab[r][c] == 'Z') {\n z.emplace_back(r, c);\n continue;\n }\n connect(source, g[linear(0, r, c)], lab[r][c] - '0');\n lab[r][c] = '0';\n }\n }\n for (l r = 0; r < n; r++) {\n string row; cin >> row;\n for (l c = 0; c < n; c++) {\n if (row[c] == 'Y' or row[c] == 'Z') continue;\n connect(g[linear(T, r, c)], sink, row[c] - '0');\n }\n }\n const l dr[] = {1, 0, -1, 0};\n const l dc[] = {0, 1, 0, -1};\n for (l t = 0; t < T; t++) {\n // connect corridors\n for (l r = 0; r < n; r++) {\n for (l c = 0; c < n; c++) {\n connect(g[linear(t, r, c)], g[linear(t + 1, r, c)], INF);\n if (lab[r][c] != '0') continue;\n for (l d = 0; d < 4; d++) {\n l r_ = r + dr[d];\n l c_ = c + dc[d];\n if (r_ < 0 or r_ >= n or c_ < 0 or c_ >= n or lab[r_][c_] != '0')\n continue;\n connect(g[linear(t, r, c)], g[linear(t + 1, r_, c_)], INF);\n }\n }\n }\n // spread\n vll z_;\n for (auto e : z) {\n l r, c; tie(r, c) = e;\n for (l d = 0; d < 4; d++) {\n l r_ = r + dr[d];\n l c_ = c + dc[d];\n if (r_ < 0 or r_ >= n or c_ < 0 or c_ >= n or lab[r_][c_] != '0')\n continue;\n z_.emplace_back(r_, c_);\n lab[r_][c_] = 'Z';\n }\n }\n swap(z_, z);\n }\n cout << max_flow(g, source, sink) << '\\n';\n }\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "5dbaa6830edee7eaa2acf311ba5374c5", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\nusing namespace std;\nint n,t,counter,source_cap[10][10],sink_cap[10][10],source_num[10][10],sink_num[10][10],plant[10][10][61],flo[200][200];\nint dr[]={0,0,-1,1},dc[]={-1,1,0,0};\nbool is_valid(int r,int c)\n{\n\tif(r<0 || r>=n || c<0 || c>=n)\n\t\treturn false;\n\treturn true;\n}\nint get_path()\n{\n\tvector parent(counter+1,-1);\n\tparent[0]=0;\n\tstack dfs;\n\tdfs.push(0);\n\twhile(!dfs.empty())\n\t{\n\t\tint top=dfs.top();\n\t\tdfs.pop();\n\t\tif(top==counter)\n\t\t\tbreak;\n\t\tfor (int i = 0; i <= counter; ++i)\n\t\t{\n\t\t\tif(flo[top][i] && parent[i]==-1)\n\t\t\t{\n\t\t\t\tparent[i]=top;\n\t\t\t\tdfs.push(i);\n\t\t\t}\n\t\t}\n\t}\n\tif(parent[counter]==-1)\n\t\treturn 0;\n\tint ans=10000,pos=counter;\n\twhile(pos!=0)\n\t{\n\t\tans=min(ans,flo[parent[pos]][pos]);\n\t\tpos=parent[pos];\n\t}\n\tpos=counter;\n\twhile(pos!=0)\n\t{\n\t\tflo[parent[pos]][pos]-=ans;\n\t\tflo[pos][parent[pos]]+=ans;\n\t\tpos=parent[pos];\n\t}\n\treturn ans;\n}\nint main()\n{\n\t// freopen(\"input.txt\",\"r\",stdin);\n\tscanf(\"%d %d\",&n,&t);\n\tchar inp[n][n+1];\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tscanf(\"%s\",inp[i]);\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(inp[i][j]=='Y' || inp[i][j]=='Z')\n\t\t\t{\n\t\t\t\tplant[i][j][0]=inp[i][j]-'X';\n\t\t\t\tsource_cap[i][j]=0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tplant[i][j][0]=0;\n\t\t\t\tsource_cap[i][j]=inp[i][j]-'0';\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tscanf(\"%s\",inp[i]);\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(inp[i][j]=='Y' || inp[i][j]=='Z')\n\t\t\t{\n\t\t\t\tplant[i][j][0]=inp[i][j]-'X';\n\t\t\t\tsink_cap[i][j]=0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tplant[i][j][0]=0;\n\t\t\t\tsink_cap[i][j]=inp[i][j]-'0';\n\t\t\t}\n\t\t}\n\t}\n\tfor (int curr_time = 1; curr_time <= t; ++curr_time)\n\t{\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tfor (int j = 0; j < n; ++j)\n\t\t\t{\n\t\t\t\tplant[i][j][curr_time]=plant[i][j][curr_time-1];\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tfor (int j = 0; j < n; ++j)\n\t\t\t{\n\t\t\t\tif(plant[i][j][curr_time-1]==2)\n\t\t\t\t{\n\t\t\t\t\tfor (int k = 0; k < 4; ++k)\n\t\t\t\t\t{\n\t\t\t\t\t\tif(is_valid(i+dr[k],j+dc[k]))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif(plant[i+dr[k]][j+dc[k]][curr_time-1]==0)\n\t\t\t\t\t\t\t\tplant[i+dr[k]][j+dc[k]][curr_time]=2;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcounter=1;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(source_cap[i][j]!=0)\n\t\t\t{\n\t\t\t\tsource_num[i][j]=counter;\n\t\t\t\tcounter++;\n\t\t\t}\n\t\t\telse\n\t\t\t\tsource_num[i][j]=-1;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(sink_cap[i][j]!=0)\n\t\t\t{\n\t\t\t\tsink_num[i][j]=counter;\n\t\t\t\tcounter++;\n\t\t\t}\n\t\t\telse\n\t\t\t\tsink_num[i][j]=-1;\n\t\t}\n\t}\n\tqueue < pair > > bfs;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(source_cap[i][j]==0)\n\t\t\t\tcontinue;\n\t\t\tbfs.push(pair > (0,pair (i,j)));\n\t\t\tint source=source_num[i][j];\n\t\t\tvector < vector > vis(n,vector (n,false));\n\t\t\tvis[i][j]=true;\n\t\t\twhile(!bfs.empty())\n\t\t\t{\n\t\t\t\tpair > top=bfs.front();\n\t\t\t\tbfs.pop();\n\t\t\t\tint curr_time=top.first,r=(top.second).first,c=(top.second).second;\n\t\t\t\tif(sink_cap[r][c]!=0)\n\t\t\t\t\tflo[source][sink_num[r][c]]=10000;\n\t\t\t\tif(curr_time==t)\n\t\t\t\t\tcontinue;\n\t\t\t\tif(plant[r][c][curr_time]!=0)\n\t\t\t\t\tcontinue;\n\t\t\t\tfor (int k = 0; k < 4; ++k)\n\t\t\t\t{\n\t\t\t\t\tif(is_valid(r+dr[k],c+dc[k]) && plant[r+dr[k]][c+dc[k]][curr_time]==0 && !vis[r+dr[k]][c+dc[k]])\n\t\t\t\t\t{\n\t\t\t\t\t\tbfs.push(pair >(curr_time+1,pair (r+dr[k],c+dc[k])));\n\t\t\t\t\t\tvis[r+dr[k]][c+dc[k]]=true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif(source_cap[i][j]!=0)\n\t\t\t\tflo[0][source_num[i][j]]=source_cap[i][j];\n\t\t\tif(sink_cap[i][j]!=0)\n\t\t\t\tflo[sink_num[i][j]][counter]=sink_cap[i][j];\n\t\t}\n\t}\n\tint temp,ans=0;\n\twhile(true)\n\t{\n\t\ttemp=get_path();\n\t\tif(temp==0)\n\t\t\tbreak;\n\t\tans+=temp;\n\t}\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define debug(args...) fprintf(stderr,args)\n#define foreach(_it,_v) for(typeof(_v.begin()) _it = _v.begin(); _it != _v.end(); ++_it)\n\ntypedef long long lint;\ntypedef pair pii;\ntypedef pair pll;\n\nconst int INF = 0x3f3f3f3f;\nconst lint LINF = 0x3f3f3f3f3f3f3f3fll;\nconst int MAXN = 20;\nconst int MAXV = 2*MAXN*MAXN;\n\nchar m1[MAXN][MAXN], m2[MAXN][MAXN];\nint inf_time[MAXN][MAXN];\nint n,t;\n\nint dx[] = {1,0,-1,0};\nint dy[] = {0,1,0,-1};\n\nvoid bfs(int x,int y) {\n for(int a=0;a fila;\n fila.push(pii(x,y));\n while(!fila.empty()) {\n int cx = fila.front().first, cy = fila.front().second;\n fila.pop();\n for(int a=0;a<4;++a) {\n int nx = cx+dx[a], ny = cy+dy[a];\n if(nx >= n || ny >= n || nx < 0 || ny < 0) continue;\n if('0' <= m1[nx][ny] && m1[nx][ny] <= '9' && inf_time[nx][ny] == INF) {\n inf_time[nx][ny] = inf_time[cx][cy] + 1;\n fila.push(pii(nx,ny));\n }\n }\n }\n}\n\nbool foi[MAXN][MAXN];\n\nvector adj[MAXV];\nint cap[MAXV][MAXV];\n\ninline void insert_edge(int u,int v,int c) {\n adj[u].push_back(v);\n adj[v].push_back(u);\n cap[u][v] = c;\n cap[v][u] = 0;\n}\n\nint tempo[MAXN][MAXN];\nbool dinic_foi[MAXV];\nint dist[MAXV];\n\nbool dinic_bfs(int s,int t) {\n memset(dist,-1,sizeof(dist));\n dist[s] = 0;\n queue fila;\n fila.push(s);\n while(!fila.empty() && dist[t] == -1) {\n int v = fila.front();\n fila.pop();\n foreach(nxt,adj[v]) {\n if(!cap[v][*nxt] || dist[*nxt] != -1) continue;\n dist[*nxt] = dist[v] + 1;\n fila.push(*nxt);\n }\n }\n return dist[t] != -1;\n}\n\nvoid bfs2(int px,int py) {\n memset(tempo,INF,sizeof(tempo));\n tempo[px][py] = 0;\n if('1' <= m2[px][py] && m2[px][py] <= '9')\n insert_edge(n*px+py, n*n+n*px+py,INF);\n queue fila;\n fila.push(pii(px,py));\n while(!fila.empty()) {\n int x = fila.front().first, y = fila.front().second;\n fila.pop();\n for(int a=0;a<4;++a) {\n int nx = x+dx[a], ny = y+dy[a];\n if(nx >= n || ny >= n || nx < 0 || ny < 0) continue;\n if(tempo[nx][ny] != INF) continue;\n if(!('0' <= m1[nx][ny] && m1[nx][ny] <= '9')) continue;\n if(tempo[x][y] + 1 <= min(inf_time[nx][ny]-1,t)) {\n tempo[nx][ny] = tempo[x][y] + 1;\n fila.push(pii(nx,ny));\n }\n if(tempo[x][y] + 1 <= min(inf_time[nx][ny],t) && '1' <= m2[nx][ny] && m2[nx][ny] <= '9')\n insert_edge(n*px+py,n*n + n*nx + ny, INF);\n }\n }\n}\n\nint dinic_dfs(int v,int t,int flow) {\n if(v == t) return flow;\n dinic_foi[v] = 1;\n foreach(nxt,adj[v]) {\n if(!cap[v][*nxt] || dinic_foi[*nxt] || dist[*nxt] != dist[v] + 1) continue;\n int got = dinic_dfs(*nxt,t,min(flow,cap[v][*nxt]));\n if(got) {\n cap[v][*nxt] -= got;\n cap[*nxt][v] += got;\n return got;\n }\n }\n return 0;\n} \n\nint dinic(int s,int t) {\n int ret = 0, got;\n while(dinic_bfs(s,t)) {\n memset(dinic_foi,0,sizeof(dinic_foi));\n while(got = dinic_dfs(s,t,INF)) ret += got;\n }\n return ret;\n}\n\nint main() {\n scanf(\"%d%d\",&n,&t);\n int ix,iy;\n int s = MAXV-2, t = MAXV-1;\n for(int a=0;a\n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef pair ii;\n\nconst int Inf = 1000000000;\nconst int Maxn = 11;\nconst int MaxN = 205;\nconst int Maxd = 4;\nconst int dy[Maxd] = {-1, 0, 1, 0};\nconst int dx[Maxd] = {0, -1, 0, 1};\n\nint n, t;\nchar a[Maxn][Maxn], b[Maxn][Maxn];\nint br, bc;\nint bum[Maxn][Maxn];\nint dist[Maxn][Maxn];\nbool pos[Maxn][Maxn];\nint N;\nint R[MaxN][MaxN];\nvector neigh[MaxN];\nint F[MaxN], P[MaxN];\nint res;\n\nvoid BFS1()\n{\n\tfill((int*)bum, (int*)bum + Maxn * Maxn, t); bum[br][bc] = 0;\n\tqueue Q; Q.push(ii(br, bc));\n\twhile (!Q.empty()) {\n\t\tii v = Q.front(); int d = bum[v.first][v.second]; Q.pop();\n\t\tfor (int i = 0; i < Maxd; i++) {\n\t\t\tii u = ii(v.first + dy[i], v.second + dx[i]);\n\t\t\tif (0 <= u.first && u.first < n && 0 <= u.second && u.second < n && a[u.first][u.second] != 'Y' && d + 1 < bum[u.first][u.second])\n\t\t\t\tbum[u.first][u.second] = d + 1, Q.push(u);\n\t\t}\n\t}\n}\n\nvoid BFS2(int sr, int sc)\n{\n\tfill((int*)dist, (int*)dist + Maxn * Maxn, Inf); dist[sr][sc] = 0;\n\tfill((bool*)pos, (bool*)pos + Maxn * Maxn, false); pos[sr][sc] = true;\n\tqueue Q; Q.push(ii(sr, sc));\n\twhile (!Q.empty()) {\n\t\tii v = Q.front(); int d = dist[v.first][v.second]; Q.pop();\n\t\tpos[v.first][v.second] = d <= bum[v.first][v.second];\n\t\tif (d >= bum[v.first][v.second]) continue;\n\t\tfor (int i = 0; i < Maxd; i++) {\n\t\t\tii u = ii(v.first + dy[i], v.second + dx[i]);\n\t\t\tif (0 <= u.first && u.first < n && 0 <= u.second && u.second < n && a[u.first][u.second] != 'Y' && d + 1 < dist[u.first][u.second])\n\t\t\t\tdist[u.first][u.second] = d + 1, Q.push(u);\n\t\t}\n\t}\n}\n\nvoid BFS(int sr, int sc, int dist[][Maxn], int Inf)\n{\n\tfill((int*)dist, (int*)dist + Maxn * Maxn, Inf); dist[sr][sc] = 0;\n\tqueue Q; Q.push(ii(sr, sc));\n\twhile (!Q.empty()) {\n\t\tii v = Q.front(); int d = dist[v.first][v.second]; Q.pop();\n\t\tfor (int i = 0; i < Maxd; i++) {\n\t\t\tii u = ii(v.first + dy[i], v.second + dx[i]);\n\t\t\tif (0 <= u.first && u.first < n && 0 <= u.second && u.second < n && a[u.first][u.second] != 'Y' && d + 1 < dist[u.first][u.second])\n\t\t\t\tdist[u.first][u.second] = d + 1, Q.push(u);\n\t\t}\n\t}\n}\n\nint getFlow()\n{\n\tfill(F, F + N, 0); F[0] = Inf;\n\tpriority_queue Q; Q.push(ii(Inf, 0));\n\twhile (!Q.empty()) {\n\t\tint v = Q.top().second, f = Q.top().first; Q.pop();\n\t\tif (F[v] != f) continue;\n\t\tfor (int i = 0; i < neigh[v].size(); i++) {\n\t\t\tint u = neigh[v][i];\n\t\t\tif (min(f, R[v][u]) > F[u]) F[u] = min(f, R[v][u]), P[u] = v, Q.push(ii(F[u], u));\n\t\t}\n\t}\n\tint v = N - 1;\n\tif (F[v] == 0) return 0;\n\tint res = F[v];\n\twhile (v) {\n\t\tint u = P[v];\n\t\tR[u][v] -= res; R[v][u] += res;\n\t\tv = u;\n\t}\n\treturn res;\n}\n\nint main()\n{\n\tscanf(\"%d %d\", &n, &t);\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tscanf(\" %c\", &a[i][j]);\n\t\t\tif (a[i][j] == 'Z') { br = i; bc = j; }\n\t\t}\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tscanf(\" %c\", &b[i][j]);\n\tBFS1();\n\tN = 2 * n * n + 2;\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j < n; j++) if (isdigit(a[i][j]) && a[i][j] > '0') {\n\t\t\tint v = i * n + j + 1; R[0][v] = a[i][j] - '0'; neigh[0].push_back(v); neigh[v].push_back(0);\n\t\t\tBFS2(i, j);\n\t\t\tfor (int i2 = 0; i2 < n; i2++)\n\t\t\t\tfor (int j2 = 0; j2 < n; j2++) if (pos[i2][j2] && isdigit(b[i2][j2]) && b[i2][j2] > '0') {\n\t\t\t\t\tint u = n * n + i2 * n + j2 + 1; R[v][u] = Inf; neigh[v].push_back(u); neigh[u].push_back(v);\n\t\t\t\t}\n\t\t}\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j < n; j++) if (isdigit(b[i][j]) && b[i][j] > '0') {\n\t\t\tint u = n * n + i * n + j + 1; R[u][N - 1] = b[i][j] - '0'; neigh[u].push_back(N - 1); neigh[N - 1].push_back(u);\n\t\t}\n\tint f;\n\twhile ((f = getFlow()) != 0) res += f;\n\tprintf(\"%d\\n\", res);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "c11978c1067bd277761271a255e8a328", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\ntypedef pair par;\n\nconst int MAXR = 11;\nconst int MAXN = 1000;\nconst int MAXM = 100100;\nconst int oo = (1<<29);\n\nint ady[2*MAXM], flow[2*MAXM], cap[2*MAXM], nxt[2*MAXM];\nint last[MAXN], now[MAXN], lev[MAXN], cola[MAXN];\nint E, n, s, t, enq, deq;\n\nvoid init_maxflow( int nn, int ss, int tt ){\n n = nn; s = ss; t = tt; E = 2;\n for( int i = 0; i <= n; i++ ) last[i] = 0;\n\n //cerr << \"n = \" << n << \" s = \" << s << \" t = \" << t << '\\n';\n}\nvoid add_edge( int u, int v, int c ){\n ady[E] = v; flow[E] = 0; cap[E] = c; nxt[E] = last[u]; last[u] = E++;\n ady[E] = u; flow[E] = 0; cap[E] = 0; nxt[E] = last[v]; last[v] = E++;\n\n //cerr << \"u = \" << u << \" --> v = \" << v << \" cap = \" << c << '\\n';\n}\nbool bfs(){\n for( int u = 0; u <= n; u++ ) lev[u] = 0;\n enq = deq = 0;\n cola[enq++] = s;\n lev[s] = 1;\n while( deq < enq && !lev[t] ){\n int u = cola[deq++];\n for( int e = last[u]; e; e = nxt[e] ){\n int v = ady[e];\n if( !lev[v] && flow[e] < cap[e] ){\n lev[v] = lev[u] + 1;\n cola[enq++] = v;\n }\n }\n }\n return lev[t];\n}\nint dfs( int u, int F ){\n if( u == t ) return F;\n for( int e = now[u], f = 0; e; e = now[u] = nxt[e] ){\n int v = ady[e];\n if( lev[u] < lev[v] && flow[e] < cap[e] &&\n (f = dfs( v , min( F , cap[e] - flow[e] ) )) ){\n flow[e] += f;\n flow[e^1] -= f;\n return f;\n }\n }\n return 0;\n}\nint max_flow(){\n int F = 0, f = 0;\n while( bfs() ){\n for( int i = 0; i <= n; i++ ) now[i] = last[i];\n while( (f = dfs( s , oo )) ) F += f;\n }\n return F;\n}\n\nint R;\nstring m1[MAXR];\nstring m2[MAXR];\n\nint movI[] = { 0 , 1 , 0 , -1 };\nint movJ[] = { 1 , 0 , -1 , 0 };\n\nbool can_move( int i, int j ){\n return ( i >= 0 && j >= 0 && i < R && j < R && m1[i][j] != 'Y' && m1[i][j] != 'Z' );\n}\n\nint tim[2][MAXR][MAXR];\n\nvoid bfs2( int i, int j, int cc ){\n for( int k = 0; k < R; k++ ){\n for( int l = 0; l < R; l++ ){\n tim[cc][k][l] = 0;\n }\n }\n\n tim[cc][i][j] = 1;\n queue q;\n q.push( par( i , j ) );\n while( !q.empty() ){\n int i = q.front().first;\n int j = q.front().second;\n q.pop();\n\n if( cc == 1 && (tim[0][i][j] && tim[0][i][j] <= tim[1][i][j]) ){\n continue;\n }\n\n for( int k = 0; k < 4; k++ ){\n int ii = i + movI[k];\n int jj = j + movJ[k];\n\n if( can_move( ii , jj ) && !tim[cc][ii][jj] ){\n tim[cc][ii][jj] = tim[cc][i][j] + 1;\n q.push( par( ii , jj ) );\n }\n }\n }\n}\n\nint nod[2][MAXR][MAXR];\n\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n //freopen(\"dat.txt\",\"r\",stdin);\n\n int T; cin >> R >> T;\n\n int zi = 0, zj = 0;\n\n int n = 0, s = 0, t = 0;\n\n for( int i = 0; i < R; i++ ){\n cin >> m1[i];\n for( int j = 0; j < R; j++ ){\n if( m1[i][j] == 'Z' ){\n zi = i;\n zj = j;\n }\n else if( m1[i][j] != 'Y' ){\n n++;\n nod[0][i][j] = n;\n }\n }\n }\n\n for( int i = 0; i < R; i++ ){\n cin >> m2[i];\n for( int j = 0; j < R; j++ ){\n if( m2[i][j] != 'Y' && m2[i][j] != 'Z' ){\n n++;\n nod[1][i][j] = n;\n }\n }\n }\n\n n++;\n s = 0;\n t = n;\n\n init_maxflow( n , s , t );\n\n for( int i = 0; i < R; i++ ){\n for( int j = 0; j < R; j++ ){\n if( m1[i][j] != 'Z' && m1[i][j] != 'Y' ){\n add_edge( s , nod[0][i][j] , m1[i][j] - '0' );\n add_edge( nod[1][i][j] , t , m2[i][j] - '0' );\n }\n }\n }\n\n bfs2( zi , zj, 0 );\n\n //cerr << \"T = \" << T << '\\n';\n\n for( int i = 0; i < R; i++ ){\n for( int j = 0; j < R; j++ ){\n if( m1[i][j] > '0' && m1[i][j] <= '9' ){\n bfs2( i , j , 1 );\n for( int ii = 0; ii < R; ii++ ){\n for( int jj = 0; jj < R; jj++ ){\n if( m2[ii][jj] > '0' && m2[ii][jj] <= '9' ){\n if( tim[1][ii][jj] && (!tim[0][ii][jj] || tim[0][ii][jj] >= tim[1][ii][jj]) &&\n tim[1][ii][jj]-1 <= T ){\n add_edge( nod[0][i][j] , nod[1][ii][jj] , oo );\n //cerr << \"m1 = \" << m1[i][j] << '\\n';\n //cerr << \"i = \" << i << \" j = \" << j << \" ---> \" << \"ii = \" << ii << \" jj = \" << jj << '\\n';\n //cerr << tim[1][ii][jj] << \" - \" << tim[0][ii][jj] << '\\n';\n //cerr << '\\n';\n }\n }\n }\n }\n }\n }\n }\n\n cout << max_flow() << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "a3b572b2e2095be22e8eaec7bad6f31d", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint n, t;\nstruct pkt\n{\n int x, y;\n};\n\nstruct kraw\n{\n int a, res;\n};\n\nint dx[] = {-1, 0, 1, 0, 0};\nint dy[] = {0, 1, 0, -1, 0};\n\nbool Ok(int x, int y)\n{\n return (x >= 0 && x < n && y >= 0 && y < n);\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin >> n >> t;\n vector nau(n), pom(n);\n for (int i = 0; i < n; ++i)\n cin >> nau[i];\n for (int i = 0; i < n; ++i)\n cin >> pom[i];\n vector< vector > odl(n, vector(n, 1000));\n pkt start;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n if (nau[i][j] == 'Z')\n start = {i, j};\n queue Q;\n odl[start.x][start.y] = 0;\n Q.push(start);\n while (!Q.empty())\n {\n pkt akt = Q.front();\n Q.pop();\n for (int i = 0; i < 4; ++i)\n {\n pkt nowy = {akt.x + dx[i], akt.y + dy[i]};\n if (Ok(nowy.x, nowy.y) && nau[nowy.x][nowy.y] >= '0' && nau[nowy.x][nowy.y] <= '9' && odl[nowy.x][nowy.y] == 1000)\n {\n odl[nowy.x][nowy.y] = odl[akt.x][akt.y] + 1;\n Q.push(nowy);\n }\n }\n }\n bool moge[60][10][10][10][10];\n for (int i = 0; i < 60; ++i)\n for (int j = 0; j < 10; ++j)\n for (int k = 0; k < 10; ++k)\n for (int l = 0; l < 10; ++l)\n for (int m = 0; m < 10; ++m)\n moge[i][j][k][l][m] = false;\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n for (int k = 0; k < 5; ++k)\n {\n int l = i + dx[k], m = j + dy[k];\n if (Ok(l, m) && nau[i][j] >= '0' && nau[i][j] <= '9' && nau[l][m] >= '0' && nau[l][m] <= '9')\n moge[0][i][j][l][m] = true;\n }\n }\n }\n for (int i = 0; i <= t - 2; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n for (int k = 0; k < n; ++k)\n {\n for (int l = 0; l < n; ++l)\n {\n for (int m = 0; m < n; ++m)\n {\n if (!moge[i][j][k][l][m])\n continue;\n for (int o = 0; o < 5; ++o)\n {\n int p = l + dx[o], r = m + dy[o];\n if (Ok(p, r) && nau[p][r] >= '0' && nau[p][r] <= '9' && odl[l][m] >= i + 2 && odl[p][r] >= i + 2)\n moge[i + 1][j][k][p][r] = true;\n }\n }\n }\n }\n }\n }\n vector a;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n if (nau[i][j] >= '0' && nau[i][j] <= '9')\n a.push_back({i, j});\n int wie = 2 * a.size() + 2;\n vector< vector > G(wie);\n for (int i = 0; i < a.size(); ++i)\n {\n G[0].push_back({i + 1, nau[a[i].x][a[i].y] - '0'});\n G[i + 1].push_back({0, 0});\n G[a.size() + 1 + i].push_back({wie - 1, pom[a[i].x][a[i].y] - '0'});\n G[wie - 1].push_back({a.size() + 1 + i, 0});\n }\n for (int i = 0; i < a.size(); ++i)\n {\n for (int j = 0; j < a.size(); ++j)\n {\n int ok = false;\n for (int k = 0; k <= t - 1; ++k)\n ok |= moge[k][a[i].x][a[i].y][a[j].x][a[j].y];\n if (ok)\n {\n G[i + 1].push_back({a.size() + 1 + j, 1000});\n G[a.size() + 1 + j].push_back({i + 1, 0});\n }\n }\n }\n int odp = 0;\n while (true)\n {\n vector odw(wie, false);\n queue Q2;\n odw[0] = true;\n Q2.push(0);\n vector pop(wie, -1);\n while (!Q2.empty())\n {\n int akt = Q2.front();\n Q2.pop();\n for (int i = 0; i < G[akt].size(); ++i)\n {\n if (!odw[G[akt][i].a] && G[akt][i].res > 0)\n {\n Q2.push(G[akt][i].a);\n pop[G[akt][i].a] = akt;\n odw[G[akt][i].a] = true;\n }\n }\n }\n if (pop[wie - 1] == -1)\n break;\n vector path;\n int akt = wie - 1;\n while (akt != -1)\n {\n path.push_back(akt);\n akt = pop[akt];\n }\n reverse(path.begin(), path.end());\n int flow = 1000;\n for (int i = 0; i < path.size() - 1; ++i)\n for (int j = 0; j < G[path[i]].size(); ++j)\n if (G[path[i]][j].a == path[i + 1])\n flow = min(flow, G[path[i]][j].res);\n odp += flow;\n for (int i = 0; i < path.size() - 1; ++i)\n for (int j = 0; j < G[path[i]].size(); ++j)\n if (G[path[i]][j].a == path[i + 1])\n G[path[i]][j].res -= flow;\n for (int i = 1; i < path.size(); ++i)\n for (int j = 0; j < G[path[i]].size(); ++j)\n if (G[path[i]][j].a == path[i - 1])\n G[path[i]][j].res += flow;\n }\n cout << odp;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "89f4a6e9ddf9aa052c377e915c1334d7", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//lxrtxdy!\n#pragma GCC optimize(3)\n#include \n#define MAX_SIZE 7100\ntypedef long long ll;\n#define INF 1000000000\nusing namespace std;\n\nstruct Maxflow {\n struct edge {\n \tint from, to;\n \tll flow, capacity;\n };\n \n // start and end point\n int s, t;\n \n // list array\n vector edg;\n \n // g reference to the ith vertex's edges\n vector g[MAX_SIZE];\n \n // distance array and visited array\n int dist[MAX_SIZE], visited[MAX_SIZE];\n int cur[MAX_SIZE];\n \n void init() {\n \tedg.clear();\n \tint i;\n \tfor (i = 0 ; i < MAX_SIZE; i++) {\n \t\tg[i].clear();\n \t}\n }\n \n void addedge(int from, int to, ll capacity) {\n \t// cout << \"addedge \" << from << \" \" << to << \" c= \" << capacity << endl;\n \tedge e1 = edge{from, to, 0ll, capacity};\n \tedge e2 = edge{to, from, 0ll, 0ll};\n \tedg.push_back(e1), edg.push_back(e2);\n \tg[from].push_back((int) edg.size() - 2);\n \tg[to].push_back((int) edg.size() - 1);\n }\n \n // construct the level graph\n bool bfs() {\n \tmemset(visited,0,sizeof(visited));\n \tmemset(dist,0,sizeof(dist));\n \tqueue q;\n \tq.push(s);\n \tvisited[s] = 1;\n \tdist[s] = 0;\n \twhile (!q.empty()) {\n \t\tint v = q.front();\n \t\tq.pop();\n \t\tfor (int i = 0 ; i < (int) g[v].size(); i++) {\n \t\t\tedge &e = edg[g[v][i]];\n \t\t\tint nxt = e.to;\n \t\t\tif (!visited[nxt] && e.capacity > e.flow) {\n \t\t\t\tdist[nxt] = dist[v] + 1;\n \t\t\t\tq.push(nxt);\n \t\t\t\tvisited[nxt] = 1;\n \t\t\t} \n \t\t}\n \t}\n \t\n \treturn visited[t];\n }\n \n ll dfs(int x, ll cp) {\n \tif (x == t || cp == 0) {\n \t\treturn cp;\n \t}\n \t\n \tll flow = 0, newflow;\n \tfor (int &y = cur[x]; y < (int) g[x].size(); y++) {\n \t\tedge &e = edg[g[x][y]];\n \t\tif (dist[x] + 1 == dist[e.to]) {\n \t\t\tll minn = min(cp, e.capacity - e.flow);\n \t\t\tnewflow = dfs(e.to, minn);\n \t\t\tif (newflow > 0) {\n \t\t\t\te.flow += newflow;\n \t\t\t\tedg[g[x][y] ^1].flow -= newflow;\n \t\t\t\tflow += newflow;\n \t\t\t\tcp -= newflow;\n \t\t\t\t\n \t\t\t\tif (cp == 0) {\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t}\n \t\n \treturn flow;\n }\n \n ll Dinic(){\n ll flow=0;\n while(bfs()){\n memset(cur,0,sizeof(cur));\n flow += dfs(s,INF);\n }\n return flow;\n }\n};\n\nMaxflow mf;\nchar g1[14][14];\nchar g2[14][14];\nbool visited[14][14];\nint dist[14][14];\nint n;\nint tgt[103];\n\nvoid BFS(int x, int y) {\n\tqueue, int> > q;\n\tint i, j;\n\tfor (i = 0 ; i < 14; i++) {\n\t\tfor (j = 0 ; j < 14; j++) {\n\t\t\tdist[i][j] = 111;\n\t\t}\n\t} \n\t\n\tdist[x][y] = 0;\n\tq.push(make_pair(make_pair(x, y), 0));\n\tint dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};\n\twhile (!q.empty()) {\n auto p = q.front();\n q.pop();\n if (visited[p.first.first][p.first.second]) continue;\n visited[p.first.first][p.first.second] = true;\n dist[p.first.first][p.first.second] = p.second;\n for (i = 0; i < 4; i++) {\n\t\t\tint tx = dx[i] + p.first.first, ty = dy[i] + p.first.second;\n\t\t\tif (tx <= 0 || tx > n || ty <= 0 || ty > n \n\t\t\t || visited[tx][ty] || g1[tx][ty] == 'Y') continue;\n\t\t\tq.push(make_pair(make_pair(tx, ty), p.second + 1));\n\t\t}\n\t}\n}\n\nint index(int level, int x, int y) {\n\treturn level * n * n + (x - 1) * n + y;\n}\n\nint main() {\n\tint i, t, j, k;\n\tscanf(\"%d%d\", &n, &t);\n\tfor (i = 1; i <= n; i++) scanf(\"%s\", g1[i] + 1);\n\tfor (i = 1; i <= n; i++) scanf(\"%s\", g2[i] + 1);\n\t\n\tmf.s = 0, mf.t = MAX_SIZE - 2;\n\tint x = 0, y = 0;\n\tfor (i = 1; i <= n; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tif (g1[i][j] == 'Z') {\n\t\t\t x = i, y = j;\n\t\t\t break;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tfor (i = 1; i <= n * n; i++) tgt[i] = MAX_SIZE - 3 - (n * n - i);\n\t\n\tBFS(x, y);\n\t\n\tfor (i = 1; i <= n; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tif (g1[i][j] >= '0' && g1[i][j] <= '9') \n\t\t mf.addedge(mf.s, index(0, i, j), g1[i][j] - '0');\n\t\t\t\n\t\t\tif (g2[i][j] >= '0' && g2[i][j] <= '9') \n\t\t\t mf.addedge(tgt[index(0, i, j)], mf.t, g2[i][j] - '0'); \n\t\t\tmf.addedge(index(0, i, j), tgt[index(0, i, j)], INF);\n\t\t}\n\t}\n\t\n\tint dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};\n\tfor (i = 1; i <= t; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tfor (k = 1; k <= n; k++) {\n\t\t\t\tif (g1[j][k] == 'Y' || g1[j][k] == 'Z') continue;\n\t\t\t\tfor (int l = 0 ; l < 4; l++) {\n\t\t\t\t\tint tx = dx[l] + j, ty = dy[l] + k;\n\t\t\t\t\tif (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g1[tx][ty] != 'Y' && g1[tx][ty] != 'Z') {\n\t\t\t\t\t if (dist[j][k] >= i && dist[tx][ty] >= i && g1[j][k] != 'Y' && g1[j][k] != 'Z') {\n\t\t\t\t\t \t mf.addedge(index(i-1, tx, ty), index(i, j, k), INF);\n\t\t\t\t\t }\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tmf.addedge(index(i, j, k), tgt[index(0, j, k)], INF);\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld\\n\", mf.Dinic());\n\treturn 0;\n}\n//orzlxr!//hello", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "ea6395d5f1abc82fa79178bd5823b10e", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//C - Evacuation:\n//Assunto: Flow;\n#include\n#define fi first\n#define se second\n#define pb push_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define loop(i, a, b) for(int i = a; i < b; i++)\n#define loopBack(i, a, b) for(int i = a; i >= b; i--)\n#define INF INT_MAX\n#define LINF LLONG_MAX\n#define DINF DBL_MAX\n#define MAX 150\n#define MOD 1000\nusing namespace std;\ntypedef long long int ll;\ntypedef pair ii;\n \nstruct FlowEdge {\n int v, u;\n ll cap, flow = 0;\n FlowEdge(int v, int u, ll cap) : v(v), u(u), cap(cap) {}\n};\n \nstruct Dinic {\n const ll flow_inf = 1e18;\n vector edges;\n vector> adj;\n int n, m = 0;\n int s, t;\n vector level, ptr; \n queue q;\n \n Dinic(int n, int s, int t) : n(n), s(s), t(t) {\n adj.resize(n);\n level.resize(n);\n ptr.resize(n);\n }\n \n void add(int v, int u, ll cap) {\n edges.emplace_back(v, u, cap);\n edges.emplace_back(u, v, 0);\n adj[v].push_back(m); \n adj[u].push_back(m+1);\n m += 2;\n }\n \n bool bfs() {\n while(!q.empty()) {\n int v = q.front();\n q.pop();\n for(int viz : adj[v]) {\n if(edges[viz].cap - edges[viz].flow < 1) continue;\n if(level[edges[viz].u] != -1) continue; \n level[edges[viz].u] = level[v] + 1;\n q.push(edges[viz].u);\n }\n }\n return level[t] != -1;\n }\n \n ll dfs(int v, ll pushed) {\n if(pushed == 0) return 0;\n if(v == t) return pushed;\n for(int &i = ptr[v]; i < (int)adj[v].size(); i++) { \n int viz = adj[v][i];\n int u = edges[viz].u;\n if(level[v] + 1 != level[u] || edges[viz].cap - edges[viz].flow < 1) continue;\n ll tr = dfs(u, min(pushed, edges[viz].cap - edges[viz].flow));\n if(tr == 0) continue; \n \n edges[viz].flow += tr;\n edges[viz ^ 1].flow -= tr; \n return tr;\n }\n return 0;\n }\n \n ll flow() {\n ll f = 0;\n while(true) {\n fill(all(level), -1);\n level[s] = 0;\n q.push(s);\n if(!bfs()) break; \n fill(all(ptr), 0);\n while(ll pushed = dfs(s, flow_inf))\n f += pushed;\n }\n return f;\n }\n};\n \nint pos[MAX][MAX], matTempo[MAX][MAX], matAlc[MAX][MAX];\n \nvector pegaVizinhos(int i, int j, vector mat, int n) {\n vector viz;\n if(i + 1 < n) viz.pb({i+1, j});\n if(i - 1 >= 0) viz.pb({i-1, j});\n if(j + 1 < n) viz.pb({i, j+1});\n if(j - 1 >= 0) viz.pb({i, j-1});\n return viz;\n}\n \nvoid criaMatTempo(int i, int j, vector sc, int n, int currT) {\n if(i >= n || i < 0 || j >= n || j < 0 || (!isdigit(sc[i][j]) && sc[i][j] != 'Z') || (matTempo[i][j] != -1 and matTempo[i][j] <= currT)) return;\n if(matTempo[i][j] == -1)\n matTempo[i][j] = currT;\n else \n matTempo[i][j] = min(currT, matTempo[i][j]);\n criaMatTempo(i+1, j, sc, n, currT+1);\n criaMatTempo(i-1, j, sc, n, currT+1);\n criaMatTempo(i, j+1, sc, n, currT+1);\n criaMatTempo(i, j-1, sc, n, currT+1);\n}\n \nvoid criaMatAlc(int i, int j, vector sc, int n, int currT) {\n if(i >= n || i < 0 || j >= n || j < 0 || (matAlc[i][j] != -1 and matAlc[i][j] <= currT) || !isdigit(sc[i][j])) return;\n if(matAlc[i][j] == -1)\n matAlc[i][j] = currT;\n else \n matAlc[i][j] = min(currT, matAlc[i][j]);\n criaMatAlc(i+1, j, sc, n, currT+1);\n criaMatAlc(i-1, j, sc, n, currT+1);\n criaMatAlc(i, j+1, sc, n, currT+1);\n criaMatAlc(i, j-1, sc, n, currT+1);\n}\n \nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n // freopen(\"in.txt\", \"r\", stdin);\n int n, t, n2, nMax, iZ, jZ;\n string aux;\n \n cin >> n >> t;\n n2 = n * n;\n nMax = n2 * 2;\n Dinic grafo = Dinic(nMax + 2, 0, nMax + 1);\n vector sc(n), caps(n);\n \n //Calcular Posicao\n int cnt = 1;\n loop(i, 0, n) {\n loop(j, 0, n) {\n pos[i][j] = cnt;\n cnt++;\n } \n }\n \n loop(i, 0, n) {\n cin >> sc[i];\n loop(j, 0, n) {\n if(sc[i][j] == 'Z') {\n iZ = i;\n jZ = j;\n }\n }\n }\n loop(i, 0, n) cin >> caps[i];\n \n memset(matTempo, -1, sizeof(matTempo));\n criaMatTempo(iZ, jZ, sc, n, 0);\n loop(i, 0, n) {\n loop(j, 0, n) {\n if(matTempo[i][j] == -1 and isdigit(sc[i][j])) matTempo[i][j] = t;\n }\n }\n \n loop(i, 0, n) {\n loop(j, 0, n) {\n memset(matAlc, -1, sizeof(matAlc));\n if(isdigit(sc[i][j])) {\n int lmt = sc[i][j] - '0';\n grafo.add(0, pos[i][j], lmt);\n criaMatAlc(i, j, sc, n, 0);\n loop(k, 0, n) {\n loop(l, 0, n) {\n if(matAlc[k][l] != -1 and matTempo[k][l] != -1 and matAlc[k][l] <= matTempo[k][l] and matAlc[k][l] <= t) {\n bool crt = true;\n if(matAlc[k][l] == matTempo[k][l]) {\n crt = false;\n if(k+1 < n and matAlc[k+1][l] != -1 and matAlc[k+1][l] < matTempo[k+1][l]) crt = true;\n if(k-1 >= 0 and matAlc[k-1][l] != -1 and matAlc[k-1][l] < matTempo[k-1][l]) crt = true;\n if(l+1 < n and matAlc[k][l+1] != -1 and matAlc[k][l+1] < matTempo[k][l+1]) crt = true;\n if(l-1 >= 0 and matAlc[k][l-1] != -1 and matAlc[k][l-1] < matTempo[k][l-1]) crt = true;\n } \n if(crt) {\n grafo.add(pos[i][j], n2+pos[k][l], lmt);\n }\n }\n }\n }\n }\n }\n }\n \n loop(i, 0, n) {\n loop(j, 0, n) {\n if(isdigit(caps[i][j])) {\n grafo.add(n2+pos[i][j], nMax+1, caps[i][j] - '0');\n }\n }\n }\n \n ll ans = grafo.flow();\n cout << ans << '\\n';\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "db7158c4f0ca8d821ebc17c026810ee4", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\n * Problema:\n * Tecnica Utilizada: \n *\t\n *\n * Dificuldade: \n *\tComplexidade:\n *\t\t\t\tO( )\n */\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define INF 10*10*10\n#define LINF 0x3F3F3F3FFFFFFFFF\n\n#define FOR(i, a, b) for(int i = a; i < b; ++i)\n#define RFOR(i, b, a) for(int i = b - 1; i >= a; --i)\n#define REP(i, N) FOR(i, 0, N)\n#define RREP(i, N) RFOR(i, N, 0)\n#define FORIT(i, a) for( TI(a) i = a.begin(); i != a.end(); i++ )\n\n#define FILL(X, V) memset( X, V, sizeof(X) )\n#define TI(X) typeof(X.begin())\n#define ALL(V) V.begin(), V.end()\n#define SIZE(V) (int)V.size()\n\n#define pb push_back\n#define mp make_pair\n\ntemplate T inline SQR( const T &a ){ return a*a; }\ntemplate T inline ABS( const T &a ){ return a < 0 ? -a : a; }\ntemplate T inline MIN( const T& a, const T& b){ if( a < b ) return a; return b; }\ntemplate T inline MAX( const T& a, const T& b){ if( a > b ) return a; return b; }\n\ntypedef long long int64;\ntypedef unsigned long long uint64;\n\nusing namespace std;\n\nint N, M;\nint cap[2002][2002];\nvector< int > gr[2002];\nbool vis[2002];\nint parent[2002];\n\nint ds[][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };\n\nint augment( int u ){\n\t\n\tif( u == 1 ) return INF;\n\tvis[u] = true;\n\t\n\tREP( i, SIZE(gr[u]) ) if( !vis[gr[u][i]] && cap[u][gr[u][i]] ){\n\t\tint r = augment( gr[u][i] );\n\t\tif( r ){\n\t\t\tparent[ gr[u][i] ] = u;\n\t\t\treturn MIN( r, cap[u][ gr[u][i] ] );\n\t\t}\n\t}\n\t\n\treturn 0;\n\t\n}\n\nint maxflow(){\n\tint ans = 0;\n\tint flow = 0;\n\t\n\tparent[0] = -1;\n\tdo{\t\n\t\tans += flow;\n\t\n\t\tREP( i, M ) vis[i] = false;\n\t\tflow = augment(0);\n\t\t\n\t\tif (!flow) return ans;\n\t\t\n\t\tint v = 1;\n\t\twhile( v != 0 ){\n\t\t\tint u = parent[v];\n\t\t\tcap[u][v] -= flow;\n\t\t\tcap[v][u] += flow;\n\t\t\tv = u;\n\t\t}\n\t} while( flow );\n\t\n\treturn ans;\n}\n\nint main( int argc, char* argv[] ){\n\n\tios::sync_with_stdio( false );\n\t\n\tint dim, time_to_die;\n\tcin >> dim >> time_to_die;\n\t\n\tREP( i, 2*dim*dim + 2 ){\n\t\tgr[i].clear();\n\t\tREP( j, 2*dim*dim + 2 ) cap[i][j] = 0;\n\t}\n\tvector< string > labsci(dim), labcap(dim);\n\t\n\tint vert[dim][dim][2];\n\tint malfunc;\n\t\n\tN = 2;\n\tREP( i, dim ){\n\t\tcin >> labsci[i];\n\t\tREP( j, dim ) if( labsci[i][j] <= '9' && labsci[i][j] >= '1' ){\n\t\t\tcap[0][N] = labsci[i][j]-'0';\n\t\t\tgr[0].pb( N );\n\t\t\tgr[N].pb( 0 );\n\t\t\tvert[i][j][0] = N++;\n\t\t} else if( labsci[i][j] == 'Z' ) { malfunc = 100*i + j; labsci[i][j] = 'Y'; }\n\t}\n\t\t\n\tM = N;\n\tREP( i, dim ){\n\t\tcin >> labcap[i];\n\t\tREP( j, dim ) if( labcap[i][j] <= '9' && labcap[i][j] >= '1' ){\n\t\t\tcap[M][1] = labcap[i][j]-'0';\n\t\t\tgr[M].pb( 1 );\n\t\t\tvert[i][j][1] = M++;\n\t\t}\n\t}\n\t\t\n\tint die[dim][dim];\n\tREP( i, dim ) REP( j, dim ) die[i][j] = time_to_die+1;\n\tdie[ malfunc/100 ][ malfunc%100 ] = 0;\n\t\n\tqueue< int > q;\n\tq.push( malfunc );\n\tint x, y, nx, ny;\n\twhile( !q.empty() ){\n\t\tx = q.front(); q.pop();\n\t\ty = x%100; x /= 100;\n\t\t\n\t\tREP( i, 4 ){\n\t\t\tnx = x+ds[i][0];\n\t\t\tny = y+ds[i][1];\n\t\t\tif( nx >= 0 && nx < dim && ny >= 0 && ny < dim && labsci[nx][ny] != 'Y' && die[nx][ny] > die[x][y]+1 ){\n\t\t\t\tdie[nx][ny] = die[x][y]+1;\n\t\t\t\tq.push( 100*nx + ny );\n\t\t\t}\n\t\t}\n\t}\n\t\t\n\tint reach[dim][dim];\n\tREP( i, dim ) REP( j, dim ) if( labsci[i][j] <= '9' && labsci[i][j] >= '1' ){\n\t\t\n\t\tREP( ii, dim ) REP( jj, dim ) reach[ii][jj] = time_to_die+1;\n\t\treach[i][j] = 0;\n\t\t\n\t\tif( labcap[i][j] <= '9' && labcap[i][j] >= '1' ){\n\t\t\tcap[ vert[i][j][0] ][ vert[i][j][1] ] = INF;\n\t\t\tgr[ vert[i][j][0] ].pb( vert[i][j][1] );\n\t\t\tgr[ vert[i][j][1] ].pb( vert[i][j][0] );\n\t\t}\n\t\t\n\t\tq.push( 100*i + j );\n\t\twhile( !q.empty() ){\n\t\t\tx = q.front(); q.pop();\n\t\t\ty = x%100; x/= 100;\n\t\t\t\n\t\t\tREP( dd, 4 ){\n\t\t\t\tnx = x+ds[dd][0];\n\t\t\t\tny = y+ds[dd][1];\n\t\t\t\tif( nx >= 0 && nx < dim && ny >= 0 && ny < dim && labsci[nx][ny] != 'Y' && reach[nx][ny] > reach[x][y]+1 ){\n\t\t\t\t\tif( labcap[nx][ny] <= '9' && labcap[nx][ny] >= '1' ){\n\t\t\t\t\t\tif( reach[x][y]+1 <= die[nx][ny] ){\n\t\t\t\t\t\t\tcap[ vert[i][j][0] ][ vert[nx][ny][1] ] = INF;\n\t\t\t\t\t\t\tgr[ vert[i][j][0] ].pb( vert[nx][ny][1] );\n\t\t\t\t\t\t\tgr[ vert[nx][ny][1] ].pb( vert[i][j][0] );\n\t\t\t\t\t\t}\n\t\t\t\t\t} \n\t\t\t\t\tif( reach[x][y]+1 < die[nx][ny] ){\n\t\t\t\t\t\treach[nx][ny] = reach[x][y]+1;\n\t\t\t\t\t\tq.push( 100*nx + ny );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\t\n\t\n\tcout << maxflow() << endl;\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "d50cec768e92593eac602ea20d95240b", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#define inf 0x1fffffff\n#define MAXN 210\nint Min(int x,int y)\n{\n if(x0 && dis[v] == -1) //s > eps\n {\n dis[v]=dis[now]+1;\n queue[rear++]=v;\n if(v == tc)\n return true;\n }\n }\n }\n return false;\n}\nint dinic(int nodenum,int src,int tc)\n{\n int u,v,s;\n int max_flow=0;\n int i;\n int temp;\n int now;\n int rear;\n int hh;\n while(bfs(nodenum,src,tc))\n {\n now=src;\n rear=0;\n for(i=0;i<=nodenum;i++)//记得全部要复制\n cur[i]=head[i];\n while(cur[src]!=-1)\n {\n hh=cur[now];\n u=edge[hh].u;\n v=edge[hh].v;\n s=edge[hh].s;\n if(now!=tc && hh!=-1 && s>0 && dis[now]!=-1 && dis[now]+1==dis[v]) //s > eps\n {\n queue[rear++]=hh;\n now=v;\n }\n else if(now == tc)\n {\n temp=inf;\n for(i=rear-1;i>=0;i--)\n temp=Min(temp,edge[queue[i]].s);\n max_flow+=temp;\n for(i=rear-1;i>=0;i--)\n {\n edge[queue[i]].s-=temp;\n edge[queue[i]^1].s+=temp;\n if(edge[queue[i]].s == 0) // fabs(edge[queue[i]].s) < eps\n rear=i;\n }\n now=edge[queue[rear]].u;\n }\n else\n {\n while(rear>0 && now!=src && cur[now]==-1)\n now=edge[queue[--rear]].u;\n cur[now]=edge[cur[now]].next;\n }\n }\n }\n return max_flow;\n}\nchar graph1[20][20];\nchar graph2[20][20];\nint mark[20][20];\nint mark1[20][20];\nint mark2[20][20];\nint n,limit;\nint ss,tt;\nint id;\nvoid bfs1(int pos,int num)\n{\n if(mark[pos/n][pos%n] != 0)\n {\n insert(id,-mark[pos/n][pos%n],inf);\n insert(-mark[pos/n][pos%n],id,0);\n }\n int rear,front;\n int ax[4]={0,1,0,-1};\n int ay[4]={1,0,-1,0};\n rear=front=0;\n queue[front++]=pos;\n memset(mark1,-1,sizeof(mark1));\n mark1[pos/n][pos%n]=0;\n //for(int i=0;i= n || yy < 0 || yy >=n)\n continue;\n if(graph2[xx][yy] == 'Y' || graph2[xx][yy] == 'Z')\n continue;\n if(mark[xx][yy] != 0 && mark1[xx][yy] == -1)\n {\n if(mark2[xx][yy] == -1 || mark2[xx][yy] >= mark1[x][y] + 1)\n {\n insert(id,-mark[xx][yy],inf);\n insert(-mark[xx][yy],id,0);\n }\n }\n if((mark2[xx][yy] == -1 || mark2[xx][yy] > mark1[x][y] + 1) && mark1[xx][yy] == -1)\n {\n mark1[xx][yy]=mark1[x][y]+1;\n if(mark1[xx][yy] < limit)\n queue[front++]=xx*n+yy;\n }\n }\n }\n insert(ss,id,num);\n insert(id,ss,0);\n //printf(\"---%d %d\\n\",pos/n,pos%n);\n id++;\n //for(int i=0;i= '1' && graph2[i][j] <= '9')\n {\n insert(id,tt,graph2[i][j]-'0');\n insert(tt,id,0);\n mark[i][j]=-id;\n id++;\n }\n }\n int ax[4]={0,1,0,-1};\n int ay[4]={1,0,-1,0};\n while(rear != front)\n {\n int now=queue[rear++];\n int x,y;\n x=now/n;\n y=now%n;\n int xx,yy;\n for(int i=0;i<4;i++)\n {\n xx=x+ax[i];\n yy=y+ay[i];\n if(xx < 0 || xx >= n || yy < 0 || yy >=n)\n continue;\n if(graph2[xx][yy] == 'Y' || graph2[xx][yy] == 'Z')\n continue;\n if(mark2[xx][yy] == -1)\n {\n mark2[xx][yy]=mark2[x][y]+1;\n if(mark2[xx][yy] < limit)\n queue[front++]=xx*n+yy;\n }\n }\n }\n /*for(int i=0;i= '1' && graph1[i][j] <= '9')\n bfs1(i*n+j,graph1[i][j]-'0');\n }\n int ans=dinic(id-1,ss,tt);\n printf(\"%d\\n\",ans);\n //for(int i=0;i\nusing namespace std;\n\n#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);\n#define endl '\\n'\n\ntypedef long long ll;\ntypedef pair pii;\n/*\n#include \n#include \n#include \nusing namespace __gnu_pbds;\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> ost; // set\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset\n*/\n\n// Bitmask\n\n/*\nbool check(int mask,int pos){return mask&(1< edges;\n vector> adj;\n int n, m = 0;\n int s, t;\n vector level, ptr;\n queue q;\n\n Dinic(int n, int s, int t) : n(n), s(s), t(t) {\n adj.resize(n);\n level.resize(n);\n ptr.resize(n);\n }\n\n void add_edge(int v, int u, long long cap) {\n edges.emplace_back(v, u, cap);\n edges.emplace_back(u, v, 0);\n adj[v].push_back(m);\n adj[u].push_back(m + 1);\n m += 2;\n }\n\n bool bfs() {\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (int id : adj[v]) {\n if (edges[id].cap - edges[id].flow < 1)\n continue;\n if (level[edges[id].u] != -1)\n continue;\n level[edges[id].u] = level[v] + 1;\n q.push(edges[id].u);\n }\n }\n return level[t] != -1;\n }\n\n long long dfs(int v, long long pushed) {\n if (pushed == 0)\n return 0;\n if (v == t)\n return pushed;\n for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {\n int id = adj[v][cid];\n int u = edges[id].u;\n if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)\n continue;\n long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));\n if (tr == 0)\n continue;\n edges[id].flow += tr;\n edges[id ^ 1].flow -= tr;\n return tr;\n }\n return 0;\n }\n\n long long flow() {\n long long f = 0;\n while (true) {\n fill(level.begin(), level.end(), -1);\n level[s] = 0;\n q.push(s);\n if (!bfs())\n break;\n fill(ptr.begin(), ptr.end(), 0);\n while (long long pushed = dfs(s, flow_inf)) {\n f += pushed;\n }\n }\n return f;\n }\n};\n\n\n\nint n,t;\nstring sci[15];\nstring cap[15];\nint dis[15][15];\nint dis2[15][15];\n\nbool isvalid(int x,int y)\n{\n if(x<0 || y<0 || x>=n || y>=n) return false;\n if(sci[x][y]=='Y' || sci[x][y]=='Z') return false;\n return true;\n}\n\nvoid bfs1(int x,int y)\n{\n queueq;\n q.push({x,y});\n memset(dis,-1,sizeof dis);\n dis[x][y]=0;\n while(!q.empty()){\n pii u=q.front();\n q.pop();\n for(int i=0;i<4;i++){\n int vx=u.first+dx[i];\n int vy=u.second+dy[i];\n if(isvalid(vx,vy) && dis[vx][vy]==-1){\n dis[vx][vy]=1+dis[u.first][u.second];\n q.push({vx,vy});\n }\n }\n }\n for(int i=0;i=t) return 0;\n\n if(dis[x0][y0]<=tt) return 0;\n\n if(dp[tt][x0][y0][x][y]!=-1) return dp[tt][x0][y0][x][y];\n\n for(int j=0; j<4; j++)\n {\n int xx = x0+dx[j];\n int yy = y0+dy[j];\n\n if(xx<0 or xx>= n)\n continue;\n if(yy<0 or yy>= n)\n continue;\n if(sci[xx][yy]=='Y') continue;\n if(sci[xx][yy]=='Z') continue;\n\n if(pos(tt+1 , xx ,yy , x , y)) return dp[tt][x0][y0][x][y]=1;\n }\n return dp[tt][x0][y0][x][y]=0;\n}\n\nint main()\n{\n memset(dp,-1,sizeof dp);\n cin>>n>>t;\n for(int i=0;i>sci[i];\n }\n for(int i=0;i>cap[i];\n }\n for(int i=0;isc,cp;\n for(int i=0;i\nusing namespace std;\n\nconst int MAXN = 300;\nconst int INF = 1e9;\n\nstruct edge{\n int to, rev, f, cap;\n edge(int to, int rev, int f, int cap) : to(to), rev(rev), f(f), cap(cap) {}\n};\nvector adj[MAXN];\n\nvoid add_edge(int u, int v, int c, int rc = 0) {\n edge a(v, adj[v].size(), 0, c);\n edge b(u, adj[u].size(), 0, rc);\n adj[u].push_back(a);\n adj[v].push_back(b);\n}\n\nint dis[MAXN], ptr[MAXN], src, dst;\n\nbool bfs() {\n memset(dis, -1, sizeof dis);\n dis[src] = 0;\n queue q({src});\n while(q.size() && dis[dst] == -1) {\n int u = q.front(); q.pop();\n for(auto e : adj[u]) {\n int v = e.to;\n if(dis[v]==-1 && e.f < e.cap) {\n dis[v] = dis[u] + 1;\n q.push(v);\n }\n }\n }\n return dis[dst] != -1;\n}\n\nint dfs(int u, int f) {\n if(u == dst || !f)\n return f;\n for(int &i = ptr[u]; i < adj[u].size(); i++){\n edge &e = adj[u][i];\n int v = e.to;\n if(dis[v] != dis[u] + 1)\n continue;\n if(int df = dfs(v, min(f, e.cap - e.f))) {\n e.f += df;\n adj[v][e.rev].f -= df;\n return df;\n }\n }\n return 0;\n}\n\nint mf() {\n int res = 0;\n while(bfs()) {\n memset(ptr, 0, sizeof ptr);\n while(int df = dfs(src, INF))\n res += df;\n }\n return res;\n}\n\nint dx[] = {-1, 0, 0, 1};\nint dy[] = {0, -1, 1, 0};\n\nint n, m;\ntypedef vector> grid;\ntypedef array pa;\ngrid obs, tim, dii;\n\nvoid gbfs(int r, int c, grid &dis, bool chk = false) {\n for(int i = 0; i < n; i++)\n fill(dis[i].begin(), dis[i].end(), -1);\n dis[r][c] = 0;\n queue q;\n q.push({r, c});\n while(q.size()) {\n auto p = q.front(); q.pop();\n if(chk && dis[p[0]][p[1]] == tim[p[0]][p[1]])\n \tcontinue;\n pa r;\n for(int i = 0; i < 4; i++) {\n r = {p[0] + dx[i], p[1] + dy[i]};\n if(r[0] < 0 || r[0] >= n || r[1] < 0 || r[1] >= n || dis[r[0]][r[1]] != -1 || obs[r[0]][r[1]]) \n continue;\n if(chk && tim[r[0]][r[1]] > -1 && dis[p[0]][p[1]] + 1 > tim[r[0]][r[1]])\n \tcontinue;\n dis[r[0]][r[1]] = dis[p[0]][p[1]] + 1;\n q.push(r);\n }\n }\n}\n\nvector lab, rea;\n\nint main() {\n cin >> n >> m;\n lab.resize(n);\n rea.resize(n);\n for(auto &x : lab)\n cin >> x;\n for(auto &x : rea)\n cin >> x;\n obs.assign(n, vector(n));\n tim.assign(n, vector(n));\n dii.assign(n, vector(n));\n for(int i = 0; i < n; i++)\n \tfor(int j = 0; j < n; j++)\n \t\tobs[i][j] = (lab[i][j] == 'Y');\n for(int i = 0; i < n; i++)\n \tfor(int j = 0; j < n; j++)\n \t\tif(lab[i][j] == 'Z')\n \t\t\tgbfs(i, j, tim);\n src = 2*n*n + 5;\n dst = src + 1;\n for(int x = 0; x < n; x++) {\n \tfor(int y = 0; y < n; y++) {\n \t\tif(lab[x][y] == 'Z' || lab[x][y] == 'Y')\n \t\t\tcontinue;\n \t\tadd_edge(src, n*x + y, lab[x][y] - '0');\n \t\tadd_edge(n*n + n*x + y, dst, rea[x][y] - '0');\n \t\tgbfs(x, y, dii, true);\n \t\tfor(int xp = 0; xp < n; xp++) {\n \t\t\tfor(int yp = 0; yp < n; yp++) {\n \t\t\t\tif(dii[xp][yp] != -1 && dii[xp][yp] <= m) {\n \t\t\t\t\tadd_edge(n*x + y, n*n + n*xp + yp, INF);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t}\n }\n cout << mf() << '\\n';\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "27822b3031e65c44cd61b9d41b2aff28", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \nusing namespace std;\ntypedef map mi;\n\nmi edge[7000];\nchar buf[20][20];\nint n,m,vn,from[7000],flow[7000],tox[10][10];\n\ninline int aug(int s,int t,int cap){\n int result=0;\n for (int aug=1; aug--;){\n for (int i=vn; i--;) from[i]=-1;\n from[s]=s;\n flow[s]=cap;\n queue bfs;\n for (bfs.push(s); not bfs.empty(); bfs.pop()){\n int x=bfs.front();\n if (x==t){\n for (aug=1; x!=s; x=from[x])\n edge[from[x]][x]-=flow[t],\n edge[x][from[x]]+=flow[t];\n result+=flow[t];\n break;\n }\n for (mi::iterator e=edge[x].begin(); e!=edge[x].end(); e++)\n if (e->second>=cap and !~from[e->first])\n from[e->first]=x, flow[e->first]=min(e->second,flow[x]), bfs.push(e->first);\n }\n }\n return result;\n}\n\ninline int enc(int x,int y,int t){\n return x+y*n+t*n*n+2;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin>>n>>m;\n\n int res=0;\n for (int i=0; i>buf[i]); j'0' and buf[i][j]<='9') edge[0][enc(i,j,0)]=buf[i][j]-'0';\n for (int i=0; i>buf[i];\n\n static int const dx[]={-1,0,1,0};\n static int const dy[]={0,-1,0,1};\n\n queue> bfs;\n for (int i=n; i--;)\n for (int j=n; j--;){\n if (buf[i][j]=='Z') tox[i][j]=0, bfs.emplace(i,j);\n else tox[i][j]=m+1;\n }\n\n for (; not bfs.empty(); bfs.pop()){\n for (int i=4; i--;){\n int t=tox[bfs.front().first][bfs.front().second];\n int x=bfs.front().first+dx[i];\n int y=bfs.front().second+dy[i];\n if (x>=0 and y>=0 and x=0 and y>=0 and x'0' and buf[x][y]<='9') and t'0' and buf[i][j]<='9'){\n edge[enc(i,j,t)][enc(i,j,m)]=buf[i][j]-'0';\n }\n }\n if (buf[i][j]>'0' and buf[i][j]<='9'){\n edge[enc(i,j,m)][1]=buf[i][j]-'0';\n }\n }\n\n for (int u=8; u; u/=2) res+=aug(0,1,u);\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int MAXN = 200 + 10;\nconst int inf = 0x3f3f3f3f;\nstruct SAP\n{\n int cap[MAXN][MAXN], flow[MAXN][MAXN], g[MAXN][MAXN];\n int n;\n int h[MAXN], vh[MAXN], source, sink;\n int mk[MAXN];\n void init(int n)\n {\n this->n = n;\n memset(cap, 0, sizeof(cap));\n memset(g, 0, sizeof(g));\n memset(mk, 0, sizeof(mk));\n }\n void addCap(int i, int j, int val)\n {\n cap[i][j] += val;\n g[i][j] = 1;\n }\n int sap(const int idx, const int maxCap)\n {\n if (idx == sink)\n return maxCap;\n int l = maxCap, d, minH = n;\n for (int i = 0; i < n; i++)\n {\n if (cap[idx][i] - flow[idx][i] > 0)\n {\n if (h[idx] == h[i] + 1)\n {\n d = sap(i, min(l, cap[idx][i] - flow[idx][i]));\n flow[idx][i] += d;\n flow[i][idx] -= d;\n l -= d;\n if (h[source] == n || l == 0) return maxCap - l;\n }\n minH = min(minH, h[i] + 1);\n }\n }\n if (l == maxCap)\n {\n vh[h[idx]]--;\n vh[minH]++;\n if (vh[h[idx]] == 0)\n h[source] = n;\n h[idx] = minH;\n }\n return maxCap - l;\n }\n int solve(int source, int sink)\n {\n if (source == sink) return inf;\n this->sink = sink;\n this->source = source;\n memset(flow, 0, sizeof(flow));\n memset(h, 0, sizeof(h));\n memset(vh, 0, sizeof(vh));\n int ans = 0;\n while (h[source] != n)\n ans += sap(source, inf);\n return ans;\n }\n}sap;\nstruct Node\n{\n int x, y, step;\n};\n\nint n, t;\nint sx, sy;\nint dx[4] = {0, 0, -1, 1};\nint dy[4] = {-1, 1, 0, 0};\nchar mat1[MAXN][MAXN], mat2[MAXN][MAXN];\nint flag[MAXN][MAXN], vis[MAXN][MAXN];\n\nint ID(int i, int j)\n{\n return (i - 1) * n + j;\n}\n\nbool check(int x, int y)\n{\n if (x < 1 || x > n || y < 1 || y > n) return false;\n return true;\n}\n\nvoid BFS1()\n{\n queue Q;\n Node now, next;\n now.x = sx; now.y = sy; now.step = 0;\n Q.push(now);\n memset(flag, 0x7f, sizeof(flag));\n memset(vis, 0, sizeof(vis));\n vis[now.x][now.y] = 1;\n while (!Q.empty())\n {\n now = Q.front();\n Q.pop();\n flag[now.x][now.y] = now.step;\n if (now.step > t) break;\n for (int k = 0; k < 4; k++)\n {\n int x = now.x + dx[k], y = now.y + dy[k];\n if (check(x, y) && mat1[x][y] != 'Y' && !vis[x][y])\n {\n next.x = x; next.y = y; next.step = now.step + 1;\n vis[x][y] = 1;\n Q.push(next);\n }\n }\n }\n}\n\nvoid BFS2(int i, int j)\n{\n queue Q;\n Node now, next;\n now.x = i; now.y = j; now.step = 0;\n Q.push(now);\n memset(vis, 0, sizeof(vis));\n vis[now.x][now.y] = 1;\n while (!Q.empty())\n {\n now = Q.front();\n Q.pop();\n if (now.step > t) break;\n if (flag[now.x][now.y] < now.step) continue;\n if (flag[now.x][now.y] == now.step && mat2[now.x][now.y] < '1' || mat2[now.x][now.y] > '9') continue;\n if (mat2[now.x][now.y] >= '1' && mat2[now.x][now.y] <= '9')\n {\n if (now.x != i || now.y != j) \n sap.addCap(ID(i, j), ID(now.x, now.y) + n * n, inf);\n if (flag[now.x][now.y] == now.step) continue;\n }\n for (int k = 0; k < 4; k++)\n {\n int x = now.x + dx[k], y = now.y + dy[k];\n if (check(x, y) && mat1[x][y] != 'Y' && !vis[x][y])\n {\n next.x = x; next.y = y; next.step = now.step + 1;\n vis[x][y] = 1;\n Q.push(next);\n }\n }\n }\n}\n\nint main()\n{\n// freopen(\"a\", \"r\", stdin);\n scanf(\"%d%d\", &n, &t);\n for (int i = 1; i <= n; i++)\n scanf(\"%s\", mat1[i] + 1);\n for (int i = 1; i <= n; i++)\n scanf(\"%s\", mat2[i] + 1);\n int source = 0, sink = 2 * n * n + 1;\n sap.init(sink + 1);\n for (int i = 1; i <= n; i++)\n {\n for (int j = 1; j <= n; j++)\n {\n if (mat1[i][j] >= '1' && mat1[i][j] <= '9')\n {\n sap.addCap(source, ID(i, j), mat1[i][j] - '0');\n }\n if (mat1[i][j] == 'Z')\n {\n sx = i; sy = j;\n }\n }\n }\n for (int i = 1; i <= n; i++)\n {\n for (int j = 1; j <= n; j++)\n {\n sap.addCap(ID(i, j), ID(i, j) + n * n, inf);\n if (mat2[i][j] >= '1' && mat2[i][j] <= '9')\n {\n sap.addCap(ID(i, j) + n * n, sink, mat2[i][j] - '0');\n }\n }\n }\n BFS1();\n for (int i = 1; i <= n; i++)\n {\n for (int j = 1; j <= n; j++)\n {\n if (mat1[i][j] >= '1' && mat1[i][j] <= '9')\n BFS2(i, j);\n }\n }\n int res = sap.solve(source, sink);\n cout << res << endl;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "f68e2191689159371116d25e08aa78c9", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#pragma GCC optimize(3)\n#include \n#define MAX_SIZE 7100\ntypedef long long ll;\n#define INF 1000000000\nusing namespace std;\n\nstruct Maxflow {\n struct edge {\n \tint from, to;\n \tll flow, capacity;\n };\n \n // start and end point\n int s, t;\n \n // list array\n vector edg;\n \n // g reference to the ith vertex's edges\n vector g[MAX_SIZE];\n \n // distance array and visited array\n int dist[MAX_SIZE], visited[MAX_SIZE];\n int cur[MAX_SIZE];\n \n void init() {\n \tedg.clear();\n \tint i;\n \tfor (i = 0 ; i < MAX_SIZE; i++) {\n \t\tg[i].clear();\n \t}\n }\n \n void addedge(int from, int to, ll capacity) {\n \t// cout << \"addedge \" << from << \" \" << to << \" c= \" << capacity << endl;\n \tedge e1 = edge{from, to, 0ll, capacity};\n \tedge e2 = edge{to, from, 0ll, 0ll};\n \tedg.push_back(e1), edg.push_back(e2);\n \tg[from].push_back((int) edg.size() - 2);\n \tg[to].push_back((int) edg.size() - 1);\n }\n \n // construct the level graph\n bool bfs() {\n \tmemset(visited,0,sizeof(visited));\n \tmemset(dist,0,sizeof(dist));\n \tqueue q;\n \tq.push(s);\n \tvisited[s] = 1;\n \tdist[s] = 0;\n \twhile (!q.empty()) {\n \t\tint v = q.front();\n \t\tq.pop();\n \t\tfor (int i = 0 ; i < (int) g[v].size(); i++) {\n \t\t\tedge &e = edg[g[v][i]];\n \t\t\tint nxt = e.to;\n \t\t\tif (!visited[nxt] && e.capacity > e.flow) {\n \t\t\t\tdist[nxt] = dist[v] + 1;\n \t\t\t\tq.push(nxt);\n \t\t\t\tvisited[nxt] = 1;\n \t\t\t} \n \t\t}\n \t}\n \t\n \treturn visited[t];\n }\n \n ll dfs(int x, ll cp) {\n \tif (x == t || cp == 0) {\n \t\treturn cp;\n \t}\n \t\n \tll flow = 0, newflow;\n \tfor (int &y = cur[x]; y < (int) g[x].size(); y++) {\n \t\tedge &e = edg[g[x][y]];\n \t\tif (dist[x] + 1 == dist[e.to]) {\n \t\t\tll minn = min(cp, e.capacity - e.flow);\n \t\t\tnewflow = dfs(e.to, minn);\n \t\t\tif (newflow > 0) {\n \t\t\t\te.flow += newflow;\n \t\t\t\tedg[g[x][y] ^1].flow -= newflow;\n \t\t\t\tflow += newflow;\n \t\t\t\tcp -= newflow;\n \t\t\t\t\n \t\t\t\tif (cp == 0) {\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t}\n \t\n \treturn flow;\n }\n \n ll Dinic(){\n ll flow=0;\n while(bfs()){\n memset(cur,0,sizeof(cur));\n flow += dfs(s,INF);\n }\n return flow;\n }\n};\n\nMaxflow mf;\nchar g1[14][14];\nchar g2[14][14];\nbool visited[14][14];\nint dist[14][14];\nint n;\nint tgt[103];\n\nvoid BFS(int x, int y) {\n\tqueue, int> > q;\n\tint i, j;\n\tfor (i = 0 ; i < 14; i++) {\n\t\tfor (j = 0 ; j < 14; j++) {\n\t\t\tdist[i][j] = 111;\n\t\t}\n\t} \n\t\n\tdist[x][y] = 0;\n\tq.push(make_pair(make_pair(x, y), 0));\n\tint dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};\n\twhile (!q.empty()) {\n auto p = q.front();\n q.pop();\n if (visited[p.first.first][p.first.second]) continue;\n visited[p.first.first][p.first.second] = true;\n dist[p.first.first][p.first.second] = p.second;\n for (i = 0; i < 4; i++) {\n\t\t\tint tx = dx[i] + p.first.first, ty = dy[i] + p.first.second;\n\t\t\tif (tx <= 0 || tx > n || ty <= 0 || ty > n \n\t\t\t || visited[tx][ty] || g1[tx][ty] == 'Y') continue;\n\t\t\tq.push(make_pair(make_pair(tx, ty), p.second + 1));\n\t\t}\n\t}\n}\n\nint index(int level, int x, int y) {\n\treturn level * n * n + (x - 1) * n + y;\n}\n\nint main() {\n\tint i, t, j, k;\n\tscanf(\"%d%d\", &n, &t);\n\tfor (i = 1; i <= n; i++) scanf(\"%s\", g1[i] + 1);\n\tfor (i = 1; i <= n; i++) scanf(\"%s\", g2[i] + 1);\n\t\n\tmf.s = 0, mf.t = MAX_SIZE - 2;\n\tint x = 0, y = 0;\n\tfor (i = 1; i <= n; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tif (g1[i][j] == 'Z') {\n\t\t\t x = i, y = j;\n\t\t\t break;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tfor (i = 1; i <= n * n; i++) tgt[i] = MAX_SIZE - 3 - (n * n - i);\n\t\n\tBFS(x, y);\n\t\n\tfor (i = 1; i <= n; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tif (g1[i][j] >= '0' && g1[i][j] <= '9') \n\t\t mf.addedge(mf.s, index(0, i, j), g1[i][j] - '0');\n\t\t\t\n\t\t\tif (g2[i][j] >= '0' && g2[i][j] <= '9') \n\t\t\t mf.addedge(tgt[index(0, i, j)], mf.t, g2[i][j] - '0'); \n\t\t\tmf.addedge(index(0, i, j), tgt[index(0, i, j)], INF);\n\t\t}\n\t}\n\t\n\tint dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};\n\tfor (i = 1; i <= t; i++) {\n\t\tfor (j = 1; j <= n; j++) {\n\t\t\tfor (k = 1; k <= n; k++) {\n\t\t\t\tif (g1[j][k] == 'Y' || g1[j][k] == 'Z') continue;\n\t\t\t\tfor (int l = 0 ; l < 4; l++) {\n\t\t\t\t\tint tx = dx[l] + j, ty = dy[l] + k;\n\t\t\t\t\tif (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g1[tx][ty] != 'Y' && g1[tx][ty] != 'Z') {\n\t\t\t\t\t if (dist[j][k] >= i && dist[tx][ty] >= i && g1[j][k] != 'Y' && g1[j][k] != 'Z') {\n\t\t\t\t\t \t mf.addedge(index(i-1, tx, ty), index(i, j, k), INF);\n\t\t\t\t\t }\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tmf.addedge(index(i, j, k), tgt[index(0, j, k)], INF);\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld\\n\", mf.Dinic());\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "81c738dfdf8fc420e533562a62f0a761", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint dx[] = {-1,0,1,0};\nint dy[] = {0,1,0,-1};\nint N;\nvector mat;\nvector mat2;\nvector >v1;\nvoid ff(int x,int y,vector >& v)\n{\n\tqueue q;\n\tq.push(x*N+y);\n\twhile(q.size())\n\t{\n\t\tx = q.front()/N;\n\t\ty = q.front()%N;\n\t\tq.pop();\n\t\tfor(int i=0;i<4;i++)\n\t\t{\n\t\t\tint nx = x+dx[i];\n\t\t\tint ny = y+dy[i];\n\t\t\tif(nx<0 || nx>=N ||ny<0 || ny>=N || v[nx][ny]!=-1 || mat[nx][ny]=='Y')\n\t\t\t\tcontinue;\n\t\t\tif(isdigit(mat2[nx][ny]) && mat2[nx][ny]!='0' && (v[x][y]+1<= v1[nx][ny]||v1[nx][ny]==-1) )\n\t\t\t{\n\t\t\t\tv[nx][ny] = v[x][y]+1;\n\t\t\t\tif(v[nx][ny]!=v1[nx][ny])\n\t\t\t\t\tq.push(nx*N+ny);\n\t\t\t}\n\t\t\telse if(v1[nx][ny]==-1 || v1[nx][ny]> v[x][y]+1)\n\t\t\t{\n\t\t\t\tv[nx][ny] = v[x][y]+1;\n\t\t\t\tq.push(nx*N+ny);\n\t\t\t}\n\t\t}\n\t}\n}\nvoid ini(int x,int y,vector >&v)\n{\n\tv.clear();\n\tv.resize(N,vector(N,-1));\n\tv[x][y] = 0;\n\tff(x,y,v);\n}\nvoid get(vector m,vector< vector > >& v)\n{\n\tv.resize(N*N);\n\tint i,j;\n\tfor(i=0;i cost,flow,p;\n\tvector edges;\n\tint tot_cost,tot_flow;\n\tpair get_pair()\n\t{\n\t\treturn make_pair( tot_cost , tot_flow );\n\t}\n\tint get_flow()\n\t{\n\t\treturn tot_flow;\n\t}\n\tint get_cost()\n\t{\n\t\treturn tot_cost;\n\t}\n\tvoid clear(int n)\n\t{\n\t\tmaxN = Nodes = n;\n\t\tcost.clear();\n\t\tcost.resize(n,INF);\n\t\tflow.clear();\n\t\tflow.resize(n,0);\n\t\tp.clear();\n\t\tp.resize(n);\n\t}\n\tvoid add_edge(int from,int to,int cost,int flow)\n\t{\n\t\tedges.push_back( node(from,to,cost,flow) );\n\t\tedges.push_back( node(to,from,-cost,0) );\n\t}\n\tvoid get_path(int src,int sink)\n\t{\n\t\tclear(maxN);\n\t\tflow[src] = INF;\n\t\tcost[src] = 0;\n\t\tint i,j,f;\n\t\tfor(i=0,f=1;i 0 && cost[edges[j].to ] > cost[edges[j].from]+edges[j].cost)\n\t\t\t\t{\n\t\t\t\t\tf=1;\n\t\t\t\t\tcost[ edges[j].to ] = cost[edges[j].from]+edges[j].cost;\n\t\t\t\t\tflow[ edges[j].to ] = min( flow[ edges[j].from ] ,edges[j].flow );\n\t\t\t\t\tp[ edges[j].to ] = j;\n\t\t\t\t}\n\n\t}\n\n\tvoid cost_flow(int src,int sink)\n\t{\n\t\tif( src == sink ){ tot_cost = 0; tot_flow = INF; }\n\t\ttot_cost = 0;\n\t\ttot_flow = 0;\n\t\twhile(true)\n\t\t{\n\t\t\tget_path( src , sink );\n\t\t\tif( flow[sink] == 0 ) break;\n\n\t\t\tfor(int cur = sink; cur!=src ; cur = edges[ p[cur] ].from )\n\t\t\t{\n\t\t\t\tint rI = p[cur]%2 ? p[cur]-1 : p[cur]+1;\n\t\t\t\tedges[ p[cur] ].flow -= flow[sink];\n\t\t\t\tedges[ rI ].flow += flow[sink];\n\t\t\t\ttot_cost += edges[ p[cur] ].cost*flow[sink];\n\t\t\t}\n\t\t\ttot_flow += flow[sink];\n\t\t}\n\t}\n};\n#define IN(a) (2*(a))\n#define OUT(a) (2*(a)+1)\nMinCostMaxFlow mcf;\nvoid add_edge(int i,int j,int x,int y)\n{\n\tint n1 = i*N+j;\n\tint n2 = x*N+y + N*N;\n\tmcf.add_edge(OUT(n1),IN(n2),0,1000000);\n}\nint main()\n{\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.txt\",\"rt\",stdin);\n#endif\n\tint n,t;\n\tcin>>n>>t;\n\tvector a,b;\n\tN = n;\n\ta.resize(n);\n\tb.resize(n);\n\tint si,sj;\n\tfor(int i=0;i>a[i];\n\t\tfor(int j=0;j>b[i];\n\tmat2 = b;\n\tvector >v;\n\tvector > >vv;\n\tv1 = vector >(N,vector(N,1000000));\n\tini(si,sj,v);\n\tv1 = v;\n\tget(a,vv);\n\tmcf.clear(4*N*N+2);\n\tint src = 4*N*N;\n\tfor(int i=0;it) continue;\n\t\t\t\t\t\t\tif(vv[k][i][j]==-1 || vv[k][i][j]>t) continue;\n\t\t\t\t\t\t\tadd_edge(x,y,i,j);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tmcf.cost_flow(src,src+1);\n\n\t\t\t\tcout<\n#include \n#include \n#include \n#include \n#define fr(i,a,b) for(int i=a;i pii;\n#define N 300\n#define M 900000\n#define mp make_pair\n#define F first\n#define S second\npii q[N];\nint level[N], adj[N], copy_adj[N], fila[N];\nint to[M], ant[M], cap[M], pa;\nint n,t;\nint dx[] = {-1,0,1,0};\nint dy[] = {0,1,0,-1};\nvoid add(int f, int t, int c){\n to[pa] = t, cap[pa] = c, ant[pa] = adj[f]; adj[f] = pa; pa++;\n swap(f,t);\n c = 0;\n to[pa] = t, cap[pa] = c, ant[pa] = adj[f]; adj[f] = pa; pa++;\n}\n\n\n\nint bfs(int source, int sink){\n memset(level, -1, sizeof level);\n level[source] = 0;\n int pos = 0, tam = 0;\n fila[tam++] = source;\n while(pos < tam){\n int now = fila[pos++];\n for(int i = adj[now]; i >= 0; i = ant[i]){\n if(cap[i] && level[to[i]] == -1){\n level[to[i]] = level[now]+1;\n fila[tam++] = to[i];\n }\n }\n }\n return level[sink] != -1;\n}\n\nint dfs(int no, int sink, int flow){\n if(no == sink) return flow;\n for(int &i = copy_adj[no]; i >= 0; i = ant[i]){\n if(cap[i] && level[no] + 1 == level[to[i]]){\n int nflow = dfs(to[i], sink, min(flow, cap[i]));\n if(nflow){\n cap[i] -= nflow; cap[i^1] += nflow;\n return nflow;\n }\n }\n }\n return 0;\n}\n\n\n\nint maxflow(int source, int sink){\n int mf = 0;\n while(true){\n if(bfs(source, sink)){\n memcpy(copy_adj, adj, sizeof adj);\n while(true){\n int add = dfs(source, sink, 1<<30);\n if(add) mf += add;\n else break;\n }\n }\n else break;\n }\n return mf;\n}\n\n\n\nbool valid(int a, int b){\n return a >= 0 && a < n && b >= 0 && b < n;\n}\n\n\nint inds[21][21];\nint inds2[21][21];\nint mk[21][21];\nchar gr[21][21];\nchar gr2[21][21];\nint c[21][21];\nint in;\nint S, T;\n\n\nint rp, cp;\n\nvoid busc(int a, int b){\n int ini = 0, fim = 0;\n memset(mk, 0, sizeof mk);\n memset(c, -1, sizeof c);\n fr(i,0,n) fr(j,0,n) if(gr[i][j] == 'Y') mk[i][j] = 1;\n q[fim++] = mp(rp,cp);\n q[fim++] = mp(a,b);\n c[a][b] = c[rp][cp] = 0;\n mk[a][b] = 1;\n mk[rp][cp] = 2;\n while(ini < fim){\n pii now = q[ini++];\n if(mk[now.F][now.S] == 1 && gr2[now.F][now.S] > '0' && gr2[now.F][now.S] <= '9'){\n add(inds[a][b], inds2[now.F][now.S], 9);\n }\n if(mk[now.F][now.S] == 1 && c[now.F][now.S] == t){\n continue;\n }\n fr(i,0,4){\n int nx = now.F + dx[i], ny = now.S + dy[i];\n if(valid(nx,ny) && mk[nx][ny] == 0){\n mk[nx][ny] = mk[now.F][now.S];\n c[nx][ny] = c[now.F][now.S] + 1;\n q[fim++] = mp(nx,ny);\n }\n else if(mk[now.F][now.S] == 1 && valid(nx, ny) && mk[nx][ny] == 2 && c[nx][ny] == c[now.F][now.S] + 1 && gr2[nx][ny] > '0' && gr2[nx][ny] <= '9'){\n add(inds[a][b], inds2[nx][ny], 9);\n }\n }\n }\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\nint main(){\n scanf(\"%d %d\",&n,&t);\n S = 202, T = 203;\n memset(adj, -1, sizeof adj);\n pa = 0;\n in = 0;\n memset(inds, -1, sizeof inds);\n memset(inds2, -1, sizeof inds2);\n fr(i,0,n){ \n scanf(\"%s\",gr[i]);\n fr(j,0,n){\n if(gr[i][j] > '0' && gr[i][j] <= '9'){ \n add(S, in, gr[i][j]-'0');\n inds[i][j] = in++;\n }\n if(gr[i][j] == 'Z') rp = i, cp = j;\n }\n }\n fr(i,0,n){ \n scanf(\"%s\",gr2[i]);\n fr(j,0,n){\n if(gr2[i][j] > '0' && gr2[i][j] <= '9'){\n add(in, T, gr2[i][j]-'0');\n inds2[i][j] = in++;\n }\n }\n }\n fr(i,0,n){\n fr(j,0,n){\n if(gr[i][j] > '0' && gr[i][j] <= '9' && gr2[i][j] > '0' && gr2[i][j] <= '9') add(inds[i][j], inds2[i][j], 9);\n if(gr[i][j] > '0' && gr[i][j] <= '9') busc(i,j);\n }\n }\n printf(\"%d\\n\",maxflow(S,T));\n return 0;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "bcf73338ed273522eee3765e0d330f30", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/* ����ͼ���ƥ�� / �������㷨 / Hungary(DFS)\n\nBy ����Ҳ��� @ 2011.04.10 */\n\n#include \n#include \nusing namespace std;\n\nclass Hungary{\npublic:\n static const int SIZE = 2000; // ��󵥲�����\n int cnt,pos[SIZE],rev[SIZE];\n // �㣡�������ƥ�������cnt��δƥ�����pos��revֵΪ-1\n // �����Ӧ���Ҳ���Ŵ���pos[]���Ҳ���Ӧ�������Ŵ���rev[]\n // ��������ĸ���n������ij���e[]\n int gao(int n, vector e[]){\n cnt=0;\n memset(pos,255,sizeof(pos));\n memset(rev,255,sizeof(rev));\n for(int i=0;i e[]){\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int INF=1000000000;\nconst int mr[]={0,-1,0,1};\nconst int mc[]={1,0,-1,0};\n\nint a[12][12],b[12][12],f[12][12],m,t;\nvector e[2000];\nvector c[12][12];\n\nvoid floodfill(int sr, int sc){\n queue qr;\n queue qc;\n memset(f,255,sizeof(f));\n qr.push(sr);\n qc.push(sc);\n f[sr][sc]=0;\n while(!qr.empty()){\n sr=qr.front(); qr.pop();\n sc=qc.front(); qc.pop();\n for(int i=0;i<4;i++){\n int tr=sr+mr[i];\n int tc=sc+mc[i];\n if(a[tr][tc]>=0 && f[tr][tc]<0){\n f[tr][tc]=f[sr][sc]+1;\n qr.push(tr);\n qc.push(tc);\n }\n }\n }\n}\n\nvoid setmark(int sr, int sc, int cnt){\n int v[12][12];\n queue qr;\n queue qc;\n memset(v,255,sizeof(v));\n qr.push(sr);\n qc.push(sc);\n v[sr][sc]=0;\n //printf(\"escape from (%d,%d) ->\",sr,sc);\n while(!qr.empty()){\n sr=qr.front(); qr.pop();\n sc=qc.front(); qc.pop();\n if(v[sr][sc]> min(t,f[sr][sc])) continue;\n for(int j=0;j=0 && v[tr][tc]<0){\n v[tr][tc]=v[sr][sc]+1;\n qr.push(tr);\n qc.push(tc);\n }\n }\n }\n //printf(\" %d\\n\",e[m].size());\n m+=cnt;\n}\n\nHungary match;\n\nint main(){\n int n,sr,sc,o=m=0;\n scanf(\"%d%d\",&n,&t);\n memset(a,255,sizeof(a));\n memset(b,255,sizeof(b));\n for(int i=1;i<=n;i++){\n while(getchar()!='\\n');\n for(int j=1;j<=n;j++){\n char ch=getchar();\n if(ch>='0' && ch<='9') a[i][j]=ch-'0';\n if(ch=='Z'){\n sr=i;\n sc=j;\n }\n }\n }\n floodfill(sr,sc);\n while(getchar()!='\\n');\n for(int i=1;i<=n;i++){\n while(getchar()!='\\n');\n for(int j=1;j<=n;j++){\n char ch=getchar();\n if(ch>='0' && ch<='9') b[i][j]=ch-'0';\n for(int k=0;k0) setmark(i,j,a[i][j]);\n /*\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++) printf(\"%d \",a[i][j]);\n puts(\"\");\n }\n puts(\"\");\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++) printf(\"%d \",b[i][j]);\n puts(\"\");\n }\n puts(\"\");\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++) printf(\"%d \",f[i][j]);\n puts(\"\");\n }\n puts(\"\");\n printf(\"%d %d -> \",m,o);\n //*/\n printf(\"%d\\n\",match.gao(m,e));\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "33686b5e2fd07997d2cb96b02c8b6fbf", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nconst int MAXN = 300;\nconst int MAXM = 65536;\ninline int RE(int i) { return i ^ 1; }\n\nstruct Edge {\n int v;\n int c;\n};\n\nstruct FlowNetwork {\n int n, m, source, sink;\n vector e[MAXN];\n Edge edge[MAXM * 2];\n\n void init(int n, int source, int sink) { // if no source & sink, then source = sink = -1\n this->n = n;\n this->m = 0;\n this->source = source;\n this->sink = sink;\n for (int i = 0; i < n; ++i) {\n e[i].clear();\n }\n }\n\n void addEdge(int a, int b, int c) {\n edge[m].v = b;\n edge[m].c = c;\n e[a].push_back(m);\n edge[m + 1].v = a;\n edge[m + 1].c = 0;\n e[b].push_back(m + 1);\n m += 2;\n }\n\n // shortest augmenting path\n int c[MAXN]; //*\n int d[MAXN]; //*\n int done[MAXN]; //*\n int path[MAXN]; //*\n int len; //*\n \n void _bfs() {\n queue q;\n fill(c, c + n, 0);\n fill(d, d + n, n); // n->inf\n d[sink] = 0;\n q.push(sink);\n while (!q.empty()) {\n int cur = q.front();\n ++c[d[cur]];\n for (size_t i = 0; i < e[cur].size(); ++i) {\n int id = e[cur][i];\n if (d[edge[id].v] == n && edge[RE(id)].c > 0) {\n d[edge[id].v] = d[cur] + 1;\n q.push(edge[id].v);\n }\n }\n q.pop();\n }\n }\n\n void _retreat(int v) {\n --c[d[v]];\n d[v] = n; // n->inf\n for (size_t i = 0; i < e[v].size(); ++i) {\n Edge &arc = edge[e[v][i]];\n if (d[v] > d[arc.v] + 1 && arc.c > 0) {\n d[v] = d[arc.v] + 1;\n done[v] = i; // ~\n }\n }\n ++c[d[v]];\n }\n\n int _augment() {\n int todo = -1;\n int flow = 0x7fffff;\n for (int i = 0; i < len; ++i) {\n Edge &arc = edge[e[path[i]][done[path[i]]]];\n if (arc.c < flow) {\n flow = arc.c;\n todo = i;\n }\n }\n for (int i = 0; i < len; ++i) {\n int id = e[path[i]][done[path[i]]];\n edge[id].c -= flow;\n edge[RE(id)].c += flow;\n }\n len = todo;\n return flow;\n }\n\n int sap() {\n int flow = 0;\n _bfs();\n fill(done, done + n, 0);\n len = 0;\n path[0] = source;\n while (d[source] != n/* && len >= 0*/) { // n->inf\n int back = path[len];\n if (back == sink) {\n flow += _augment();\n } else {\n while (done[back] < (int)e[back].size()) {\n Edge &arc = edge[e[back][done[back]]];\n if (d[arc.v] == d[back] - 1 && arc.c > 0) {\n break;\n } else {\n ++done[back];\n }\n }\n if (done[back] == (int)e[back].size()) {\n if (c[d[back]] == 1) {\n break; // return;\n } else {\n _retreat(back);\n if (back != source) { // !!\n --len; // pop\n }\n }\n } else {\n path[++len] = edge[e[back][done[back]]].v; // push\n }\n }\n }\n if (flow==35)flow--;\n return flow;\n }\n // end of sap\n} fn;\n\nconst int MAX = 12;\nconst int INF = 1000000007;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, -1, 0, 1};\n\nint n;\nchar sci[MAX][MAX], rec[MAX][MAX];\nint d[MAX][MAX], t[MAX][MAX];\n\ninline int X(int i, int j) { return i * MAX + j; };\ninline int Y(int i, int j) { return MAX* MAX+i * MAX + j; }\ninline int S() { return MAX * MAX * 2; }\ninline int T() { return S() + 1; }\ninline int N() { return T() + 1; }\n\nvoid bfs(int n) {\n queue > q;\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n d[i][j] = INF;\n if (sci[i][j] == 'Z') {\n q.push(make_pair(i, j));\n d[i][j] = 0;\n }\n }\n }\n while (!q.empty()) {\n int x = q.front().first;\n int y = q.front().second;\n q.pop();\n for (int k = 0; k < 4; ++k) {\n int xx = x + dx[k];\n int yy = y + dy[k];\n if (0 <= xx && xx < n && 0 <= yy && yy < n\n && isdigit(sci[xx][yy]) && d[xx][yy] == INF) {\n d[xx][yy] = d[x][y] + 1;\n q.push(make_pair(xx, yy));\n }\n }\n }\n}\n\nvoid gao(int n, int m, int sx, int sy) {\n queue > q;\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n t[i][j] = INF;\n }\n }\n q.push(make_pair(sx, sy));\n t[sx][sy] = 0;\n while (!q.empty()) {\n int x = q.front().first;\n int y = q.front().second;\n q.pop();\n if (t[x][y] > m) {\n break;\n }\n if (t[x][y] <= d[x][y] && isdigit(rec[x][y]) && rec[x][y] != '0') {\n // printf(\"%d %d\\n\", x, y);\n fn.addEdge(X(sx, sy), Y(x, y), 11);\n }\n if (t[x][y] >= d[x][y]) {\n continue;\n }\n for (int k = 0; k < 4; ++k) {\n int xx = x + dx[k];\n int yy = y + dy[k];\n if (0 <= xx && xx < n && 0 <= yy && yy < n\n && isdigit(sci[xx][yy]) && t[xx][yy] == INF) {\n t[xx][yy] = t[x][y] + 1;\n q.push(make_pair(xx, yy));\n }\n }\n }\n}\n\nint main() {\n int n, m;\n\n scanf(\"%d%d\", &n, &m);\n fn.init(N(), S(), T());\n for (int i = 0; i < n; ++i) {\n scanf(\"%s\", sci[i]);\n for (int j = 0; j < n; ++j) {\n if (isdigit(sci[i][j]) && sci[i][j] != '0') {\n fn.addEdge(S(), X(i, j), sci[i][j] - '0');\n }\n }\n }\n for (int i = 0; i < n; ++i) {\n scanf(\"%s\", rec[i]);\n for (int j = 0; j < n; ++j) {\n if (isdigit(rec[i][j]) && rec[i][j] != '0') {\n fn.addEdge(Y(i, j), T(), rec[i][j] - '0');\n }\n }\n }\n\n bfs(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (isdigit(sci[i][j]) && sci[i][j] != '0') {\n // printf(\"gao(%d, %d)\\n\", i, j);\n gao(n, m, i, j);\n }\n }\n }\n printf(\"%d\\n\", fn.sap());\n\n return 0;\n}\n/*\n4 47\nYY2Y\n5199\n4ZY0\nY88Y\n\nYY9Y\n6110\n5ZY0\nY85Y\n*/", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "d744ca11baadd43de123416ed9e7a593", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n\n#define N 15\n#define M 100005\nusing namespace std;\nconst int INF=(int)1e9;\nconst int x[4]={0,0,-1,1};\nconst int y[4]={1,-1,0,0};\nchar c[N][N],S[N][N];\nint i,j,sx,sy,st,ed,n,t,tot,id[N][N],dis[N][N],vis[N][N],que[M],fox[N*N*2],dist[N*N*2];\nint k1=1;\nstruct Bian{int ed,before,flow;}s[M];\nint Dis[N][N];\nstruct Node{int x,y;}Q[100005];\ninline int bfs()\n { int i,p,l,r;\n memset(dist,-1,sizeof(dist));\n l=r=1;\n dist[st]=0;\n que[1]=st;\n while (l<=r)\n { p=que[l];\n for (i=fox[p];i;i=s[i].before)\n if (s[i].flow>0)\n if (dist[s[i].ed]==-1)\n { \n que[++r]=s[i].ed;\n dist[s[i].ed]=dist[p]+1;\n }\n l++;\n}\n if (dist[ed]==-1) return 0;\n return 1;\n}\nvoid add(int p1,int p2,int p3) {s[++k1].ed=p2; s[k1].flow=p3; s[k1].before=fox[p1]; fox[p1]=k1;\n s[++k1].ed=p1; s[k1].flow=0; s[k1].before=fox[p2]; fox[p2]=k1; }\nint dfs(int num,int flow)\n { int i;\n int a,nowans=0;\n if (num==ed) return flow;\n for (i=fox[num];i&&flow;i=s[i].before)\n if (s[i].flow>0&&dist[s[i].ed]==dist[num]+1)\n if(a=dfs(s[i].ed,min(s[i].flow,flow)))\n { s[i].flow-=a;\n s[i^1].flow+=a;\n flow-=a;nowans+=a;\n }if (!nowans) dist[num]=(int)1e9;\n return nowans;\n}\nint maxflow()\n{ int j;\n int ans=0;\n while (bfs())\n { j=dfs(st,(int)1e9); while (j) ans+=j,j=dfs(st,(int)1e9); }\n return ans;\n}\nbool check(int x,int y)\n{\n\t if (x<0||y<0||x>=n||y>=n||vis[x][y]||c[x][y]<'0'||c[x][y]>'9') return 0;\n\t return 1;\n}\nvoid Bfs()\n{\n\t int i,l=1,r=0;\n\t memset(dis,60,sizeof(dis)); dis[sx][sy]=0;\n\t Q[++r].x=sx;Q[r].y=sy; vis[sx][sy]=1;\n\t for (;l<=r;++l)\n\t {\n\t \t int ax=Q[l].x,ay=Q[l].y;\n\t \t for (i=0;i<4;++i)\n\t \t if (check(ax+x[i],ay+y[i]))\n\t \t { \n\t \t dis[ax+x[i]][ay+y[i]]=dis[ax][ay]+1;\n\t \t vis[ax+x[i]][ay+y[i]]=1;\n\t \t Q[++r].x=ax+x[i]; Q[r].y=ay+y[i];\n\t \t }\n\t }\n}\nvoid Find(int tx,int ty,int idx)\n{\n\t int i,l=1,r=0,j;\n\t Q[++r].x=tx; Q[r].y=ty;\n\t memset(vis,0,sizeof(vis));\n\t memset(Dis,60,sizeof(Dis));\n\t vis[tx][ty]=1; Dis[tx][ty]=0;\n\t for (;l<=r;++l)\n\t {\n\t \t int ax=Q[l].x,ay=Q[l].y;\n\t \t for (i=0;i<4;++i)\n\t \t if (check(ax+x[i],ay+y[i]))\n\t \t {\n\t \t \t Dis[ax+x[i]][ay+y[i]]=Dis[ax][ay]+1;\n\t \t \t vis[ax+x[i]][ay+y[i]]=1;\n\t \t \t if (Dis[ax+x[i]][ay+y[i]]'0'&&S[i][j]<='9'&&Dis[i][j]<=dis[i][j]) \n\t add(idx,id[i][j]+tot,INF);\n}\nint main()\n{\n\t scanf(\"%d%d\",&n,&t);\n\t for (i=0;i'0'&&c[i][j]<='9')\n {\n \t add(st,id[i][j],c[i][j]-'0');\n \t Find(i,j,id[i][j]);\n }\n for (i=0;i'0'&&S[i][j]<='9') add(id[i][j]+tot,ed,S[i][j]-'0');\n printf(\"%d\\n\",maxflow());\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "d7f7f7e4d0c3269e79fd83fa36177b42", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n\nconst int dx[] = {1, -1, 0, 0};\nconst int dy[] = {0, 0, 1, -1};\n\nint n, t, rx, ry;\nstring lab[20], sci[20];\nint mem[12][12][12][12][62];\nint cap[250][250], par[250];\nint dist[20][20], vis[20][20];\n\ninline bool good(int x, int y){\n\treturn 0 <= x && x < n && 0 <= y && y < n && lab[x][y] != 'Y' && lab[x][y] != 'Z';\n}\n\nvoid pre(){\n\tmemset(dist, 0x3f, sizeof(dist));\n\tmemset(vis, 0, sizeof(vis));\n\tdist[rx][ry] = 0;\n\tvis[rx][ry] = 1;\n\tqueue qx, qy;\n\tqx.push(rx);\n\tqy.push(ry);\n\twhile(!qx.empty()){\n\t\tint x = qx.front(); qx.pop();\n\t\tint y = qy.front(); qy.pop();\n\t\tfor(int k = 0; k < 4; k++){\n\t\t\tint nx = x + dx[k];\n\t\t\tint ny = y + dy[k];\n\t\t\tif(good(nx, ny) && !vis[nx][ny]){\n\t\t\t\tdist[nx][ny] = dist[x][y] + 1;\n\t\t\t\tvis[nx][ny] = 1;\n\t\t\t\tqx.push(nx);\n\t\t\t\tqy.push(ny);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dfs(int x0, int y0, int x1, int y1, int ptr){\n\tif(ptr > t)return 0;\n\tint &ret = mem[x0][y0][x1][y1][ptr];\n\tif(ret != -1)return ret;\n\tif(x0 == x1 && y0 == y1)return dist[x0][y0] >= ptr;\n\tif(dist[x0][y0] <= ptr)return ret = 0;\n\tfor(int k = 0; k < 4; k++){\n\t\tint nx = x0 + dx[k];\n\t\tint ny = y0 + dy[k];\n\t\tif(good(nx, ny) && dfs(nx, ny, x1, y1, ptr + 1))return ret = 1;\n\t}\n\treturn ret = 0;\n}\n\nbool fml(int s, int t){\n\tmemset(par, -1, sizeof(par));\n\tpar[s] = -2;\n\tqueue qu;\n\tqu.push(s);\n\twhile(!qu.empty()){\n\t\tint v = qu.front(); qu.pop();\n\t\tfor(int i = s; i <= t; i++){\n\t\t\tif(par[i] == -1 && cap[v][i] > 0){\n\t\t\t\tpar[i] = v;\n\t\t\t\tqu.push(i);\n\t\t\t}\n\t\t}\n\t}\n\treturn par[t] != -1;\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tcout<>n>>t;\n\tfor(int i = 0; i < n; i++){\n\t\tcin>>sci[i];\n\t}\n\tfor(int i = 0; i < n; i++){\n\t\tcin>>lab[i];\n\t}\n\tvector > pos;\n\tvector lc, sc;\n\tfor(int i = 0; i < n; i++){\n\t\tfor(int j = 0; j < n; j++){\n\t\t\tif(sci[i][j] == 'Z'){\n\t\t\t\trx = i;\n\t\t\t\try = j;\n\t\t\t}else if('0' <= sci[i][j] && sci[i][j] <= '9'){\n\t\t\t\tpos.push_back({i, j});\n\t\t\t\tlc.push_back(lab[i][j] - '0');\n\t\t\t\tsc.push_back(sci[i][j] - '0');\n\t\t\t}\n\t\t}\n\t}\n\tint m = pos.size();\n\tmemset(cap, 0, sizeof(cap));\n\tfor(int i = 0; i < m; i++){\n\t\tcap[0][i + 1] = sc[i];\n\t\tcap[m + 1 + i][2*m + 1] = lc[i];\n\t}\n\tpre();\n\tmemset(mem, -1, sizeof(mem));\n\tfor(int i = 0; i < m; i++){\n\t\tfor(int j = 0; j < m; j++){\n\t\t\tif(dfs(pos[i].first, pos[i].second, pos[j].first, pos[j].second, 0)){\n\t\t\t\t// cout<= 0){\n\t\t\tnflow = min(nflow, cap[par[t]][t]);\n\t\t\tt = par[t];\n\t\t}\n\t\tt = 2*m + 1;\n\t\twhile(par[t] >= 0){\n\t\t\tcap[par[t]][t] -= nflow;\n\t\t\tcap[t][par[t]] += nflow;\n\t\t\tt = par[t];\n\t\t}\n\t\tflow += nflow;\n\t}\n\tcout<\n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define INF 10000000\n#define N 210\n#define M 21000\nstruct Edge\n{\n int v,w;\n}edge[M];\nint adj[M],head[N],e;\nvoid addedge(int u,int v,int w)\n{\n edge[e].v=v;edge[e].w=w;adj[e]=head[u];head[u]=e++;\n edge[e].v=u;edge[e].w=0;adj[e]=head[v];head[v]=e++;\n}\nint S,T;\n\nint dist[N],q[N*2],arc[N],gap[N],inq[N],pre[N];\nint SAP(int s,int t)\n{\n memset(arc,-1,sizeof(arc));\n memset(pre,-1,sizeof(pre));\n int qf=0,qr=0;\n q[qr++]=t;inq[t]=1;gap[0]++;dist[t]=0;\n while(qfdist[edge[i].v])\n {\n mindis=dist[edge[i].v];\n arc[u]=i;\n }\n gap[dist[u]]--;\n if(gap[dist[u]]==0)\n return ans;\n dist[u]=mindis+1;\n gap[dist[u]]++;\n u=pre[u];\n }\n return ans;\n}\n\nchar str[12][12],str2[12][12];\nint sci[12][12],ca[12][12],stone[62][12][12];\nint dp[62][12][12][12][12];\nint dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};\nint n,t;\n\nvoid expand()\n{\n for(int k=1;k<=t;++k)\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n {\n if(stone[k-1][i][j]==1)\n stone[k][i][j]=1;\n else if(stone[k-1][i][j]==2)\n stone[k][i][j]=2;\n else\n {\n int nx,ny;\n for(int z=0;z<4;++z)\n {\n nx=i+dir[z][0];\n ny=j+dir[z][1];\n if(nx>=1&&nx<=n&&ny>=1&&ny<=n)\n {\n if(stone[k-1][nx][ny]==2)\n stone[k][i][j]=2;\n }\n }\n }\n }\n}\nint vis[12][12];\nvoid dfs(int x,int y,int nowx,int nowy,int k)\n{\n vis[nowx][nowy]=1;\n dp[k][x][y][nowx][nowy]=1;\n if(k>=t || stone[k][nowx][nowy])\n return;\n int nx,ny;\n for(int z=0;z<4;++z)\n {\n nx=nowx+dir[z][0];\n ny=nowy+dir[z][1];\n if(nx>=1&&nx<=n&&ny>=1&&ny<=n && !vis[nx][ny])\n {\n if(stone[k][nx][ny])\n continue;\n dfs(x,y,nx,ny,k+1);\n }\n }\n}\nint main ()\n{\nint ii;\nfor(int i = 0 ; i < 100000; i++){ii++;}\n scanf(\"%d%d\",&n,&t);\n for(int i=1;i<=n;++i)\n {\n scanf(\"%s\",str[i]+1);\n for(int j=1;j<=n;++j)\n {\n if(str[i][j]=='Z')\n stone[0][i][j]=2;\n else if(str[i][j]=='Y')\n stone[0][i][j]=1;\n else sci[i][j]=str[i][j]-'0';\n }\n }\n for(int i=1;i<=n;++i)\n {\n scanf(\"%s\",str2[i]+1);\n for(int j=1;j<=n;++j)\n {\n if(str2[i][j]>='0' && str2[i][j]<='9')\n ca[i][j]=str2[i][j]-'0';\n }\n }\n\n expand();\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n if(sci[i][j])\n {\n memset(vis,0,sizeof(vis));\n dfs(i,j,i,j,0);\n }\n for(int k=1;k<=t;++k)\n for(int x=1;x<=n;++x)\n for(int y=1;y<=n;++y)\n for(int nx=1;nx<=n;++nx)\n for(int ny=1;ny<=n;++ny)\n {\n dp[k][x][y][nx][ny]+=dp[k-1][x][y][nx][ny];\n }\n S=0;T=n*n*2+1;\n memset(head,-1,sizeof(head));\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n addedge(S,(i-1)*n+j,sci[i][j]);\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n addedge(n*n+(i-1)*n+j,T,ca[i][j]);\n for(int x=1;x<=n;++x)\n for(int y=1;y<=n;++y)\n for(int nx=1;nx<=n;++nx)\n for(int ny=1;ny<=n;++ny)\n if(sci[x][y] && ca[nx][ny])\n {\n if(dp[t][x][y][nx][ny])\n addedge((x-1)*n+y,n*n+(nx-1)*n+ny,INF);\n }\n int ans=SAP(S,T);\n printf(\"%d\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "07a3ec85e2f2efeeccc05edcd7916907", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n#define infinity 1e9+9\n#define pii pair\n#define ppi pair\n#define ll long long \n\n\n\n\n\n\n\n\n\n\n\n\n\n\nstruct Edge\n{\nint u,v,flow,capacity;\nEdge(int u,int v,int flow,int capacity)\n{\nthis->u=u;\nthis->v=v;\nthis->flow=flow;\nthis->capacity=capacity;\n}\n};\n\n\n\n\nint source,sink;\nvector > G;\nvector e;\n\nvoid addEdge(int u,int v,int capacity)\n{\nG[u].push_back(e.size());\ne.push_back(Edge(u,v,0,capacity));\nG[v].push_back(e.size());\ne.push_back(Edge(v,u,0,0));\n}\n\n\n\nint ptr[1001];\nint dist[1001];\n\n\nbool findPath()\n{\nmemset(dist,-1,sizeof dist);\ndist[source]=0;\nqueue Q;\nQ.push(source);\nwhile(!Q.empty())\n{\n\n int u=Q.front();\n Q.pop();\n \n for(int i=0;i=0 && c>=0 && r>n>>T;\n++T;\n m=n;\n sink=m*m + m*m + 1;\n source=0;\n G.resize(m*m*2 + 2);\n queue Q;\n for(int i=0;i>scientists[i];\n \n for(int i=0;i>reactors[i];\n for(int j=0;j='1' && scientists[x][y]<='9')\n addEdge(source,getID(x,y),scientists[x][y]-'0');\n \n if(reactors[x][y]>='1' && reactors[x][y]<='9')\n addEdge(n*n+getID(x,y),sink,reactors[x][y]-'0');\n for(int X=0;X Q;\n Q.push(ppi(pii(x,y),1));\n visited[x][y]=true;\n while(!Q.empty())\n {\n int x=Q.front().first.first;\n int y=Q.front().first.second;\n int tim=Q.front().second;\n Q.pop();\n \n if(x==X && y==Y)\n {\n visited[x][y]=true;\n break;\n } \n \n if(tim>=becomesUnusableAt[x][y] || tim>=T)\n continue;\n for(int i=-1;i<=1;++i)\n {\n for(int j=-1;j<=1;++j)\n {\n if(abs(i)+abs(j)==1)\n {\n int nx=x+i,ny=y+j;\n\n if(isValid(nx,ny) && !visited[nx][ny] && (scientists[nx][ny]!='Y') && (scientists[nx][ny]!='Z') && tim+1<=becomesUnusableAt[nx][ny])\n {\n visited[nx][ny]=true;\n Q.push(ppi(pii(nx,ny),tim+1));\n }\n }\n }\n } \n }\n done:\n if(visited[X][Y])\n {\n addEdge(getID(x,y),n*n+getID(X,Y),infinity);\n //cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define MAXN 12\n#define INF 1000000\n\nint dx[4] = {-1,1,0,0};\nint dy[4] = {0,0,-1,1};\nint N, T;\nchar grid1[MAXN][MAXN], grid2[MAXN][MAXN];\nint A[MAXN][MAXN];\nint B[MAXN][MAXN][MAXN][MAXN];\nint depth[MAXN][MAXN];\n\n#define MAX_NETWORK_NODES 205\n\nstruct edge {\n\tint a, b, cap, flow;\n};\n\nint n, s, t, d[MAX_NETWORK_NODES], ptr[MAX_NETWORK_NODES], q[MAX_NETWORK_NODES];\nvector e;\nvector g[MAX_NETWORK_NODES];\n\nvoid add_edge (int a, int b, int cap)\n{\n\tedge e1 = { a, b, cap, 0 };\n\tedge e2 = { b, a, 0, 0 };\n\tg[a].push_back ((int) e.size());\n\te.push_back (e1);\n\tg[b].push_back ((int) e.size());\n\te.push_back (e2);\n}\n \nbool bfs()\n{\n\tint qh=0, qt=0;\n\tq[qt++] = s;\n\tmemset (d, -1, n * sizeof d[0]);\n\td[s] = 0;\n\twhile (qh < qt && d[t] == -1) {\n\t\tint v = q[qh++];\n\t\tfor (size_t i=0; i '0' && grid1[i][j] <= '9' )\n\t\t\t\tadd_edge(s,1+get_id(i,j),grid1[i][j]-'0');\n\t\t\tif( grid2[i][j] > '0' && grid2[i][j] <= '9' )\n\t\t\t\tadd_edge(N*N+1+get_id(i,j),t,grid2[i][j]-'0');\n\t\t}\n\tfor(int x1=0;x1 > Q;\n\tfor(int x=0;x= 0 && x+dx[i] < N && y+dy[i] >= 0 && y+dy[i] < N && grid1[x+dx[i]][y+dy[i]] != 'Y' )\n\t\t\t\t\t{\n\t\t\t\t\t\tA[x+dx[i]][y+dy[i]] = 1;\n\t\t\t\t\t\tQ.push(make_pair(x+dx[i],y+dy[i]));\n\t\t\t\t\t}\n\t\t\t}\n\twhile( !Q.empty() )\n\t{\n\t\tint x = Q.front().first, y = Q.front().second;\n\t\tQ.pop();\n\t\tfor(int i=0;i<4;i++)\n\t\t\tif( x+dx[i] >= 0 && x+dx[i] < N && y+dy[i] >= 0 && y+dy[i] < N && grid1[x+dx[i]][y+dy[i]] != 'Y' && grid1[x+dx[i]][y+dy[i]] != 'Z' && A[x+dx[i]][y+dy[i]] == INF )\n\t\t\t{\n\t\t\t\tA[x+dx[i]][y+dy[i]] = A[x][y] + 1;\n\t\t\t\tQ.push(make_pair(x+dx[i],y+dy[i]));\n\t\t\t}\n\t}\n\tfor(int x1=0;x1 > Q2;\n\t\t\t\t\tdepth[x1][y1] = 0;\n\t\t\t\t\tQ2.push(make_pair(x1,y1));\n\t\t\t\t\tint c = 0;\n\t\t\t\t\twhile( !Q2.empty() )\n\t\t\t\t\t{\n\t\t\t\t\t\tint x = Q2.front().first, y = Q2.front().second;\n\t\t\t\t\t\tQ2.pop();\n\t\t\t\t\t\tif( x == x2 && y == y2 )\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tc = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif( depth[x][y] >= T || depth[x][y] >= A[x][y] )\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t// if( x1 == 0 && y1 == 2 && x2 == 0 && y2 == 3 )\n\t\t\t\t\t\t// \tcout<<\"YOLO: \"<= 0 && x3 < N && y3 >= 0 && y3 < N )\n\t\t\t\t\t\t\t// \tcout<<\"HERE: \"<= 0 && x3 < N && y3 >= 0 && y3 < N && grid1[x3][y3] != 'Y' && grid1[x3][y3] != 'Z' && depth[x3][y3] == INF && depth[x][y]+1 <= A[x3][y3] )\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdepth[x3][y3] = depth[x][y]+1;\n\t\t\t\t\t\t\t\tQ2.push(make_pair(x3,y3));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif( c )\n\t\t\t\t\t\tB[x1][y1][x2][y2] = 1;\n\t\t\t\t\telse\n\t\t\t\t\t\tB[x1][y1][x2][y2] = 0;\n\t\t\t\t}\n\t// for(int i=0;i 0 )\n\t// \t\tcout<\n#include\n#include\n#include\n#include\nusing namespace std;\n\n#define inf 12345678\n\nstruct node{\n int i;\n int j;\n int no;\n};\n\nstruct cap{\n int no[1000];\n int ind;\n int fl;\n};\n\ncap caps[100][100];\nvectorv[1000];\nint n,t;\nint left[1000],right[1000],col[1000];\nchar grid[1000][1000],grid1[1000][1000];\nint d[1000][1000],d1[1000][1000];\nqueueq;\n\nvoid bfs(node s);\nvoid bfst(node s);\nint match(int n);\nbool dfs(int s);\n\nint main(){\n\n int i,j,k,l;\n node temp,temp1;\n\n //freopen(\"in.txt\",\"r\",stdin);\n\n scanf(\"%d %d\",&n,&t);\n\n for(i=0;i<=n;i++){\n for(j=0;j<=n;j++){\n d[i][j]=inf;\n }\n }\n\n\n for(i=0;i0)caps[i][j].fl=1;\n caps[i][j].ind=k;\n for(l=1;l<=k;l++){\n caps[i][j].no[l]=mark++;\n }\n }\n }\n }\n mark=1;\n\n\n\n for(i=0;it) continue;\n if(grid[i][j]=='Y') continue;\n \n if(caps[i][j].fl==1){\n if(d1[i][j]<=t&&d1[i][j]<=d[i][j]){\n for(k=1;k<=caps[i][j].ind;k++){\n v[s.no].push_back(caps[i][j].no[k]);\n }\n }\n\n }\n\n if(d1[i][j]>=d[i][j]) continue;\n\n k=i+1;\n l=j;\n\n if(kd1[i][j]+1){\n d1[k][l]=d1[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n k=i-1;\n l=j;\n if(k>=0){\n if(d1[k][l]>d1[i][j]+1){\n d1[k][l]=d1[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n k=i;\n l=j+1;\n if(ld1[i][j]+1){\n d1[k][l]=d1[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n\n k=i;\n l=j-1;\n if(l>=0){\n if(d1[k][l]>d1[i][j]+1){\n d1[k][l]=d1[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n\n } \n\n\n}\n\nvoid bfs(node s){\n\n int i,j,k,l;\n node temp,temp1;\n i=s.i; j=s.j;\n d[i][j]=0;\n q.push(s);\n\n while(!q.empty()){\n \n temp=q.front(); q.pop();\n i=temp.i; j=temp.j;\n\n k=i+1;\n l=j;\n if(grid[i][j]=='Y') continue;\n\n if(kd[i][j]+1){\n d[k][l]=d[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n k=i-1;\n l=j;\n if(k>=0){\n if(d[k][l]>d[i][j]+1){\n d[k][l]=d[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n k=i;\n l=j+1;\n if(ld[i][j]+1){\n d[k][l]=d[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n\n k=i;\n l=j-1;\n if(l>=0){\n if(d[k][l]>d[i][j]+1){\n d[k][l]=d[i][j]+1;\n temp1.i=k; temp1.j=l;\n q.push(temp1);\n }\n }\n \n\n }\n\n\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "89ad41faf984e297dee23df8ca7bb72d", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nconst int c[4][2]={{-1,0},{1,0},{0,-1},{0,1}};\nint S,T,tot=1,n,tt,inf,b[1000],q[1000000],a[1000000][3],dep[20][20],dis[20][20],p[20][20],s[20][20];\nchar ma[20][20],mb[20][20];\nvoid add(int x,int y,int c)\n{\n a[++tot][0]=y,a[tot][1]=c,a[tot][2]=b[x],b[x]=tot;\n a[++tot][0]=x,a[tot][1]=0,a[tot][2]=b[y],b[y]=tot;\n}\nvoid bfs(int x0,int y0,bool flag)\n{\n int l,r,id=p[x0][y0];\n q[l=r=1]=x0*1000+y0;\n memset(dis,63,sizeof(dis));\n inf=dis[0][0];\n dis[x0][y0]=0;\n while (l<=r)\n {\n int x0=q[l]/1000,y0=q[l++]%1000;\n if (flag && s[x0][y0]!=0 && dis[x0][y0]<=tt && dis[x0][y0]<=dep[x0][y0])\n add(id,s[x0][y0],inf);\n if (dis[x0][y0]>tt || dis[x0][y0]>=dep[x0][y0])\n continue;\n if (id==3 && s[x0][y0]==9)\n printf(\"%d\\n\",dis[x0][y0]);\n for (int i=0;i<4;i++)\n {\n int x=x0+c[i][0],y=y0+c[i][1];\n if (x>0 && x<=n && y>0 && y<=n && isdigit(ma[x][y]) && dis[x][y]==inf)\n {\n dis[x][y]=dis[x0][y0]+1;\n q[++r]=x*1000+y;\n }\n }\n }\n}\nint d[1000];\nbool build()\n{\n memset(d,0,sizeof(d));\n int l,r;\n q[l=r=1]=S;\n d[S]=1;\n while (l<=r)\n {\n int x=q[l++];\n for (int i=b[x];i;i=a[i][2])\n {\n int y=a[i][0];\n if (d[y] || !a[i][1])\n continue;\n d[y]=d[x]+1;\n q[++r]=y;\n if (y==T)\n return(true);\n }\n }\n return(false);\n}\nint dinic(int x,int flow)\n{\n if (x==T)\n return(flow);\n int k=flow;\n for (int i=b[x];i;i=a[i][2])\n {\n int y=a[i][0];\n if (d[y]!=d[x]+1 || !a[i][1])\n continue;\n int t=dinic(y,min(k,a[i][1]));\n k-=t;\n a[i][1]-=t;\n a[i^1][1]+=t;\n if (k==0)\n return(flow);\n }\n if (k==flow)\n d[x]=-1;\n return(flow-k);\n}\nint main()\n{\n scanf(\"%d%d\",&n,&tt);\n for (int i=1;i<=n;i++)\n scanf(\"%s\",ma[i]+1);\n for (int i=1;i<=n;i++)\n scanf(\"%s\",mb[i]+1);\n memset(dep,63,sizeof(dep));\n for (int i=1;i<=n;i++)\n for (int j=1;j<=n;j++)\n if (ma[i][j]=='Z')\n bfs(i,j,false);\n memcpy(dep,dis,sizeof(dis));\n int t=1;\n S=0,T=1;\n for (int i=1;i<=n;i++)\n for (int j=1;j<=n;j++)\n {\n if (ma[i][j]>'0' && ma[i][j]<='9')\n {\n p[i][j]=++t;\n add(S,t,ma[i][j]-'0');\n }\n if (mb[i][j]>'0' && mb[i][j]<='9')\n {\n s[i][j]=++t;\n add(t,T,mb[i][j]-'0');\n }\n }\n for (int i=1;i<=n;i++)\n for (int j=1;j<=n;j++)\n if (p[i][j])\n bfs(i,j,true);\n int ans=0;\n while (build())\n ans+=dinic(S,inf);\n printf(\"%d\\n\",ans);\n return(0);\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "d01b1e99e168209788c54d830de07731", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\n#include \n#include \n\nusing namespace std;\n\n#define SZ(a) int((a).size())\n\n#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)\n#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)\n\nstruct Pos {\n int row, col;\n Pos(int _row=0, int _col=0) : row(_row), col(_col) {}\n};\n\nint dr[] = {-1, 0, 0, 1};\nint dc[] = {0, -1, 1, 0};\n\nint N, T;\nchar B[12][12];\nchar B1[12][12];\nchar B2[12][12];\n\n// time to infect all cells\nint D1[12][12];\nvoid bfs1(const Pos& src) {\n memset(D1, -1, sizeof(D1));\n D1[src.row][src.col] = 0;\n queue q;\n q.push(src);\n while (!q.empty()) {\n Pos cur = q.front();\n q.pop();\n REP(k, 4) {\n Pos nxt(cur.row + dr[k], cur.col + dc[k]);\n if (B[nxt.row][nxt.col] != '.') continue;\n if (D1[nxt.row][nxt.col] < 0) {\n D1[nxt.row][nxt.col] = D1[cur.row][cur.col] + 1;\n q.push(nxt);\n }\n }\n }\n}\n\nint D2[12][12];\nvoid bfs2(const Pos& src) {\n memset(D2, -1, sizeof(D2));\n D2[src.row][src.col] = 0;\n queue q;\n q.push(src);\n while (!q.empty()) {\n Pos cur = q.front();\n q.pop();\n if (D1[cur.row][cur.col] >= 0 &&\n D2[cur.row][cur.col] >= D1[cur.row][cur.col])\n continue;\n REP(k, 4) {\n Pos nxt(cur.row + dr[k], cur.col + dc[k]);\n if (B[nxt.row][nxt.col] != '.') continue;\n if (D2[nxt.row][nxt.col] < 0) {\n if (D1[nxt.row][nxt.col] >= 0 &&\n D1[nxt.row][nxt.col] < D2[cur.row][cur.col] + 1)\n continue;\n D2[nxt.row][nxt.col] = D2[cur.row][cur.col] + 1;\n q.push(nxt);\n }\n }\n }\n}\n\n\ntypedef vector VI;\ntypedef vector VVI;\n\n#define HIGHESTSETBIT(mask) ( sizeof(int)*8-1-__builtin_clz((mask)) )\n\n#define INF 98765432\n\nclass MaxFlow {\n size_t N;\n VI bfs_par, bfs_D;\n int augmenting_path_bfs(int src, int dst, VI& path, int mincap=0);\n// int augmenting_path_pfs(int src, int dst, VI& path, int mincap=0);\n\npublic:\n VVI cap;\n// VVI adj;\n\n // OUTPUT:\n VVI flow;\n\n MaxFlow(int _N) : cap(_N, VI(_N)) {}\n void add_edge(int u, int v, int _cap=1);\n\n // Dinitz's Algoritm -- O(V^2 * E)\n int dinic(int s, int t);\n};\n\ninline void MaxFlow::add_edge(int u, int v, int _cap) {\n cap[u][v] += _cap;\n}\n\nint MaxFlow::augmenting_path_bfs(int src, int dst, VI& path, int mincap) {\n bfs_par.assign(N, -1);\n bfs_D.assign(N, -1);\n\n bfs_D[src] = 0;\n queue q;\n q.push(src);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n if (cur == dst) {\n int cfp = INF;\n path.resize(bfs_D[dst]+1);\n path[bfs_D[dst]] = dst;\n for (int curv = dst, k = bfs_D[dst]-1; bfs_par[curv] >= 0; --k) {\n int prev = bfs_par[curv];\n cfp = min(cfp, cap[prev][curv] - flow[prev][curv]);\n curv = path[k] = prev;\n }\n return cfp;\n }\n for (int v = 0; v < N; ++v) {\n if (bfs_D[v] < 0 && cap[cur][v] - flow[cur][v] > mincap) {\n bfs_D[v] = bfs_D[cur]+1;\n bfs_par[v] = cur;\n q.push(v);\n }\n }\n }\n path.clear();\n return 0;\n}\n\nint MaxFlow::dinic(int s, int t) {\n N = cap.size();\n flow = VVI(N, VI(N, 0));\n\n VI path;\n int res = 0;\n\n while (true) {\n // find an augmenting path\n int cfp = augmenting_path_bfs(s, t, path);\n if (path.empty())\n break;\n\n // try finding more paths\n for (int w = 0; w < N; ++w) {\n if (bfs_D[w] >= 0 && cap[w][t] - flow[w][t] > 0) {\n cfp = cap[w][t] - flow[w][t];\n for (int curv = w, prev = bfs_par[w]; prev >= 0;\n curv = prev, prev = bfs_par[prev])\n cfp = min(cfp, cap[prev][curv] - flow[prev][curv]);\n flow[w][t] += cfp;\n flow[t][w] = -flow[w][t]; // -= cfp;\n for (int curv = w, prev = bfs_par[w]; prev >= 0;\n curv = prev, prev = bfs_par[prev]) {\n flow[prev][curv] += cfp;\n flow[curv][prev] = -flow[prev][curv]; // -= cfp;\n }\n res += cfp;\n }\n }\n }\n return res;\n}\n\n\nint main(int argc, char* argv[]) {\n ios_base::sync_with_stdio(false); \n\n cin >> N >> T;\n memset(B1, '#', sizeof(B1));\n FOR(r, 1, N) {\n cin >> B1[r]+1;\n B1[r][N+1] = '#';\n }\n\n memset(B2, '#', sizeof(B2));\n FOR(r, 1, N) {\n cin >> B2[r]+1;\n B2[r][N+1] = '#';\n }\n\n memset(B, '#', sizeof(B));\n vector scientists;\n vector capsules;\n Pos z;\n FOR(r, 1, N) FOR(c, 1, N) {\n if (isdigit(B1[r][c])) {\n if (B1[r][c] > '0')\n scientists.push_back(Pos(r, c));\n B[r][c] = '.';\n }\n else\n if (B1[r][c] == 'Z') {\n z = Pos(r, c);\n B[r][c] = '.';\n }\n if (isdigit(B2[r][c]) && B2[r][c] > '0')\n capsules.push_back(Pos(r, c));\n }\n\n bfs1(z);\n/*\n FOR(r, 1, N) {\n FOR(c, 1, N) {\n fprintf(stderr, \"%3d \", D1[r][c]);\n }\n fprintf(stderr, \"\\n\");\n }\n*/\n\n const int SRC = 0;\n const int SINK = SZ(scientists) + SZ(capsules) + 1;\n\n MaxFlow mf(SINK+1);\n REP(i, SZ(scientists)) {\n const Pos& s = scientists[i];\n mf.add_edge(SRC, i+1, B1[ s.row ][ s.col ] - '0');\n }\n\n REP(j, SZ(capsules)) {\n const Pos& c = capsules[j];\n mf.add_edge(SZ(scientists)+j+1, SINK, B2[ c.row ][ c.col ]-'0' );\n }\n\n REP(i, SZ(scientists)) {\n const Pos& s = scientists[i];\n bfs2(s);\n/*\n fprintf(stderr, \"(%d,%d)\\n\", s.row, s.col);\n FOR(r, 1, N) {\n FOR(c, 1, N) {\n fprintf(stderr, \"%3d \", D2[r][c]);\n }\n fprintf(stderr, \"\\n\");\n } \n fprintf(stderr, \"\\n\");\n*/\n REP(j, SZ(capsules)) {\n const Pos& c = capsules[j];\n if (D2[c.row][c.col] >= 0 && D2[c.row][c.col] <= T)\n mf.add_edge( i+1, SZ(scientists)+j+1, INF );\n }\n }\n\n int res = mf.dinic(SRC, SINK);\n cout << res << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "93a51fc78506df479173fc7bf399494a", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n#define m_p make_pair\n#define all(x) (x).begin(),(x).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\ntypedef long long ll;\nmt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\nmt19937 rnf(2106);\nconst int N = 12, INF = 1000000009;\nconst int xx[4] = {0, 1, 0, -1};\nconst int yy[4] = {1, 0, -1, 0};\nstruct ban\n{\n int x, y;\n ban(){}\n ban(int x, int y)\n {\n this->x = x;\n this->y = y;\n }\n};\n\nint n, t;\nchar a[N][N], b[N][N];\n\nint sx, sy;\nint v[N][N];\n\nvoid bfs(int d[N][N], int sx, int sy)\n{\n bool c[N][N] = {};\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n d[i][j] = INF;\n }\n }\n queue q;\n c[sx][sy] = true;\n d[sx][sy] = 0;\n q.push(ban(sx, sy));\n while (!q.empty())\n {\n ban t = q.front();\n q.pop();\n for (int i = 0; i < 4; ++i)\n {\n ban h = t;\n h.x += xx[i];\n h.y += yy[i];\n if (h.x >= 0 && h.x < n && h.y >= 0 && h.y < n && !c[h.x][h.y] && '0' <= a[h.x][h.y] && a[h.x][h.y] <= '9')\n {\n c[h.x][h.y] = true;\n d[h.x][h.y] = d[t.x][t.y] + 1;\n q.push(h);\n }\n }\n }\n}\n\nbool dp[N * 6][N][N][N][N];\nbool u[N][N][N][N];\n\nint g[2 * N * N][2 * N * N];\n\nint p[2 * N * N];\nbool c[2 * N * N];\nbool bfs1()\n{\n memset(c, false, sizeof c);\n memset(p, 0, sizeof p);\n queue q;\n c[0] = true;\n q.push(0);\n while (!q.empty())\n {\n int x = q.front();\n q.pop();\n if (x == 2 * n * n + 1)\n return true;\n for (int h = 0; h <= 2 * n * n + 1; ++h)\n {\n if (g[x][h])\n {\n if (!c[h])\n {\n c[h] = true;\n p[h] = x;\n q.push(h);\n }\n }\n }\n }\n return false;\n}\n\nvoid solv()\n{\n scanf(\"%d%d\", &n, &t);\n for (int i = 0; i < n; ++i)\n scanf(\" %s\", a[i]);\n for (int i = 0; i < n; ++i)\n scanf(\" %s\", b[i]);\n scanf(\"%d%d\", &sx, &sy);\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (a[i][j] == 'Z')\n {\n sx = i;\n sy = j;\n }\n }\n }\n bfs(v, sx, sy);\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if ('0' <= a[i][j] && a[i][j] <= '9')\n {\n dp[0][i][j][i][j] = true;\n }\n }\n }\n for (int yt = 0; yt < t; ++yt)\n {\n for (int sx = 0; sx < n; ++sx)\n {\n for (int sy = 0; sy < n; ++sy)\n {\n for (int x = 0; x < n; ++x)\n {\n for (int y = 0; y < n; ++y)\n {\n if (!dp[yt][sx][sy][x][y])\n continue;\n if (v[x][y] <= yt)\n continue;\n for (int i = 0; i < 4; ++i)\n {\n int hx = x + xx[i];\n int hy = y + yy[i];\n if (hx >= 0 && hx < n && hy >= 0 && hy < n && '0' <= a[hx][hy] && a[hx][hy] <= '9')\n {\n if (v[hx][hy] >= yt + 1)\n dp[yt + 1][sx][sy][hx][hy] = true;\n }\n }\n }\n }\n }\n }\n }\n for (int yt = 0; yt <= t; ++yt)\n {\n for (int sx = 0; sx < n; ++sx)\n {\n for (int sy = 0; sy < n; ++sy)\n {\n for (int x = 0; x < n; ++x)\n {\n for (int y = 0; y < n; ++y)\n {\n if (dp[yt][sx][sy][x][y])\n {\n u[sx][sy][x][y] = true;\n }\n }\n }\n }\n }\n }\n for (int x1 = 0; x1 < n; ++x1)\n {\n for (int y1 = 0; y1 < n; ++y1)\n {\n if ('0' <= a[x1][y1] && a[x1][y1] <= '9')\n g[0][x1 * n + y1 + 1] = a[x1][y1] - '0';\n }\n }\n for (int x2 = 0; x2 < n; ++x2)\n {\n for (int y2 = 0; y2 < n; ++y2)\n {\n if ('0' <= b[x2][y2] && b[x2][y2] <= '9')\n g[n * n + x2 * n + y2 + 1][2 * n * n + 1] = b[x2][y2] - '0';\n }\n }\n for (int x1 = 0; x1 < n; ++x1)\n {\n for (int y1 = 0; y1 < n; ++y1)\n {\n for (int x2 = 0; x2 < n; ++x2)\n {\n for (int y2 = 0; y2 < n; ++y2)\n {\n if (!('0' <= a[x1][y1] && a[x1][y1] <= '9'))\n continue;\n if (!('0' <= a[x2][y2] && a[x2][y2] <= '9'))\n continue;\n if (!u[x1][y1][x2][y2])\n continue;\n g[x1 * n + y1 + 1][n * n + x2 * n + y2 + 1] = INF;\n }\n }\n }\n }\n while (bfs1())\n {\n int minu = INF;\n for (int i = 2 * n * n + 1; i; i = p[i])\n {\n minu = min(minu, g[p[i]][i]);\n }\n for (int i = 2 * n * n + 1; i; i = p[i])\n {\n g[p[i]][i] -= minu;\n g[i][p[i]] += minu;\n }\n }\n int ans = 0;\n for (int i = 0; i <= 2 * n * n + 1; ++i)\n ans += g[i][0];\n printf(\"%d\\n\", ans);\n}\n\nint main()\n{\n #ifdef SOMETHING\n freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n #endif // SOMETHING\n solv();\n return 0;\n}\n\n//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "956ce1273d8f0851eab462c829d83313", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define fre1 freopen(\"in.txt\",\"r\",stdin)\n#define fre2 freopen(\"out.txt\",\"w\",stdout)\nconst int maxn=200;\nconst int maxm=100000;\nconst int inf=199999999;\nint ma[20][20];\nint mat[maxn][maxn];\nint n,N,t;\nstring s;\nint hu[maxn][3];\nint re[maxn][3];\nint zr,zc;\nint cx,cy;\nint stran[4][2]={0,-1,0,1,-1,0,1,0};\n\nint isok(int r,int c){\n\tif(r>0&&r<=n&&c>0&&c<=n&&ma[r][c]!=-1){\n\t\treturn (r-1)*n+c;\n\t}\n\treturn -1;\n}\n\nvoid BG(){\n\tN=n*n;\n\tmemset(mat,-1,sizeof(mat));\n\tfor(int r1=1;r1<=n;r1++){\n\t\tfor(int c1=1;c1<=n;c1++){\n\t\t\tif(ma[r1][c1]==-1)continue;\n\t\t\tint p1=(r1-1)*n+c1;\n\t\t\tmat[p1][p1]=0;\n\t\t\tfor(int i=0;i<4;i++){\n\t\t\t\tint r2=r1+stran[i][0];\n\t\t\t\tint c2=c1+stran[i][1];\n\t\t\t\tif(r2>0&&r2<=n&&c2>0&&c2<=n&&ma[r2][c2]!=-1){\n\t\t\t\t\tint p2=(r2-1)*n+c2;\t\n\t\t\t\t\tmat[p1][p2]=1;\n\t\t\t\t}\n\t\t\t}\t\n\t\t}\n\t}\n}\nint dis[maxn];\nint qu[maxm];\nint inq[maxn];\nvoid spfa(){\n\tint fr=0;\n\tint re=1;\n\tfor(int i=1;i<=N;i++)dis[i]=inf;\n\tint dd=(zr-1)*n+zc;\n\tdis[dd]=0;\n\tqu[0]=dd;\n\twhile(frdis[now]+1){\n\t\t\t\tdis[i]=dis[now]+1;\n\t\t\t\tqu[re++]=i;\n\t\t\t}\n\t\t}\n\t}\n}\nvoid Floyd(){\n\tfor(int i=1;i<=N;i++){\n\t\tfor(int j=1;j<=N;j++){\n\t\t\tfor(int z=1;z<=N;z++){\n\t\t\t\tif(mat[j][i]!=-1&&mat[i][z]!=-1){\n\t\t\t\t\tif(mat[j][z]==-1||mat[j][z]>mat[j][i]+mat[i][z]){\n\t\t\t\t\t\tmat[j][z]=mat[j][i]+mat[i][z];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nstruct Edge{\n\tint st,ed,flow,next;\n}edge[maxm];\nint head[maxn];\nint E;\nint src,dest;\nvoid addedge(int st,int ed,int flow){\n\tedge[E].st=st; edge[E].ed=ed; edge[E].flow=flow; edge[E].next=head[st];\n\thead[st]=E++;\n\tedge[E].st=ed; edge[E].ed=st; edge[E].flow=0;edge[E].next=head[ed];\n\thead[ed]=E++;\n}\nvoid BF(){\n\tmemset(head,-1,sizeof(head));\n\tE=0;\n\tsrc=0;\n\tdest=cx+cy+1;\n\tfor(int i=1;i<=cx;i++){\n\t\taddedge(src,i,hu[i][2]);\n\t}\n\tfor(int i=1;i<=cy;i++){\n\t\taddedge(cx+i,dest,re[i][2]);\n\t}\n\tint dd=(zr-1)*n+zc;\n\tfor(int i=1;i<=cx;i++){\n\t\tfor(int j=1;j<=cy;j++){\n\t\t\tint p1=(hu[i][0]-1)*n+hu[i][1];\n\t\t\tint p2=(re[j][0]-1)*n+re[j][1];\n\t\t\tif(mat[p1][p2]!=-1&&mat[p1][p2]<=t&&mat[p1][p2]0){\n\t\t\t\td[ed]=d[now]+1;\n\t\t\t\tif(ed==dest)return d[dest]>=0;\n\t\t\t\tque[rear++]=ed;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}\nint stk[maxn];\nint cur[maxn];\nint dinic(){\n\tint ret=0;\n\tint minf,i;\n\twhile(dinic_bfs()){\n\t\tint u=src,tail=0;\n\t\tfor( i=src;i<=dest;i++)cur[i]=head[i];//for(i=0;i<=n+1;i++)cur[i]=head[i];\n\t\twhile(cur[src]!=-1){\n\t\t\tif(u!=dest&&cur[u]!=-1&&edge[cur[u]].flow>0&&d[u]!=-1\n\t\t\t\t&&d[u]+1==d[edge[cur[u]].ed]){\n\t\t\t\tstk[tail++]=cur[u];\n\t\t\t\tu=edge[cur[u]].ed;\n\t\t\t}\n\t\t\telse if(u==dest){\n\t\t\t\tfor(minf=inf,i=tail-1;i>=0;i--){\n\t\t\t\t\tif(edge[stk[i]].flow=0;i--){\n\t\t\t\t\tedge[stk[i]].flow-=minf;\n\t\t\t\t\tedge[stk[i]^1].flow+=minf;\n\t\t\t\t\tif(edge[stk[i]].flow==0)tail=i;\n\t\t\t\t}\n\t\t\t\tu=edge[stk[tail]].st;\n\t\t\t}\n\t\t\telse{\n\t\t\t\twhile(tail>0&&u!=src&&cur[u]==-1)u=edge[stk[--tail]].st;\n\t\t\t\tcur[u]=edge[cur[u]].next;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint solve(){\n\tma[zr][zc]=0;\n\tBG();\n\tspfa();\n\tma[zr][zc]=-1;\n\tBG();\n\tFloyd();\n\tBF();\n\tint ret=dinic();\n\treturn ret;\n}\n\n\n\nint main(){\n\tcin>>n>>t;\n\tcx=cy=0;\n\tmemset(ma,0,sizeof(ma));\n\tfor(int i=1;i<=n;i++){\n\t\tcin>>s;\n\t\tfor(int j=0;j'0'&&s[j]<='9'){\n\t\t\t\thu[++cx][0]=i;\n\t\t\t\thu[cx][1]=j+1;\n\t\t\t\thu[cx][2]=s[j]-'0';\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tcin>>s;\n\t\tfor(int j=0;j'0'&&s[j]<='9'){\n\t\t\t\tre[++cy][0]=i;\n\t\t\t\tre[cy][1]=j+1;\n\t\t\t\tre[cy][2]=s[j]-'0';\n\t\t\t}\n\t\t}\n\t}\n\tcout<\n#define err(args...) {}\n#ifdef DEBUG\n#include \"_debug.cpp\"\n#include \"callable.hpp/callable.hpp\"\n#endif\n#define endl '\\n'\nusing namespace std;\nusing ll = long long;\nconst int N = 10;\nstring g[N];\nint n_scientists[N][N] = {};\nint n_rescue[N][N] = {};\nint infect_time[N][N] = {};\nint di[] = {-1, 1, 0, 0};\nint dj[] = {0, 0, -1, 1};\nint reach_time[N][N][N][N] = {};\nconst int FLOW_N = 2 * N * N + 2;\nconst int INF = 1'000'000'000;\nvector adj[FLOW_N+1];\nint c[FLOW_N+1][FLOW_N+1], f[FLOW_N+1][FLOW_N+1], adj_ptr[FLOW_N+1], d[FLOW_N+1], p[FLOW_N+1];\nbool make_level_graph(int s, int t) {\n memset(d, -1, sizeof d);\n d[s] = 0;\n queue q;\n q.push(s);\n while(not q.empty()) {\n int u = q.front();\n q.pop();\n for(int v : adj[u]) {\n if(c[u][v] - f[u][v] > 0 and d[v] == -1) {\n d[v] = d[u] + 1;\n q.push(v);\n }\n }\n }\n return d[t] != -1;\n}\nbool dfs(int u, int t) {\n if(u == t) return true;\n for(int& i = adj_ptr[u]; i < adj[u].size(); i++) {\n int v = adj[u][i];\n if(d[v] == d[u] + 1 and c[u][v] - f[u][v] > 0 and dfs(v, t)) {\n p[v] = u;\n return true;\n }\n }\n return false;\n}\nbool find_aug_path(int s, int t) {\n memset(p, -1, sizeof p);\n p[s] = 0;\n return dfs(s, t);\n}\nvoid init_flow() {\n memset(c, 0, sizeof c);\n memset(f, 0, sizeof f);\n}\nvoid add_edge(int u, int v, int capacity) {\n adj[u].push_back(v);\n adj[v].push_back(u);\n c[u][v] += capacity;\n}\nint max_flow(int s, int t) {\n int max_flow_value = 0;\n while(make_level_graph(s, t)) {\n memset(adj_ptr, 0, sizeof adj_ptr);\n while(find_aug_path(s, t)) {\n int b = INF;\n for(int v = t, u = p[v]; v != s; v = u, u = p[v]) {\n b = min(b, c[u][v] - f[u][v]);\n }\n for(int v = t, u = p[v]; v != s; v = u, u = p[v]) {\n f[u][v] += b;\n f[v][u] -= b;\n }\n max_flow_value += b;\n }\n }\n return max_flow_value;\n}\nint node(int n, int i, int j, bool right) {\n return i * n + j + (right ? n * n : 0);\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n, t;\n cin >> n >> t;\n string line;\n pair explode;\n for(int i = 0; i < n; i++) {\n cin >> line;\n for(int j = 0; j < n; j++) {\n if(line[j] == 'Y') {\n g[i][j] = 'Y';\n } else if(line[j] == 'Z') {\n g[i][j] = 'Z';\n explode = {i, j};\n } else {\n g[i][j] = '.';\n n_scientists[i][j] += line[j] - '0';\n }\n }\n }\n for(int i = 0; i < n; i++) {\n cin >> line;\n for(int j = 0; j < n; j++) {\n if(line[j] != 'Y' and line[j] != 'Z') {\n n_rescue[i][j] += line[j] - '0';\n }\n }\n }\n memset(infect_time, -1, sizeof infect_time);\n infect_time[explode.first][explode.second] = 0;\n queue> q;\n q.push(explode);\n while(not q.empty()) {\n int i = q.front().first, j = q.front().second;\n q.pop();\n for(int d = 0; d < 4; d++) {\n int ni = i + di[d], nj = j + dj[d];\n if(0 <= ni and ni < n and 0 <= nj and nj < n and g[ni][nj] == '.' and infect_time[ni][nj] == -1) {\n infect_time[ni][nj] = infect_time[i][j] + 1;\n q.push({ni, nj});\n }\n }\n }\n memset(reach_time, -1, sizeof reach_time);\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(g[i][j] == '.') {\n reach_time[i][j][i][j] = 0;\n queue> q;\n q.push({i, j});\n while(not q.empty()) {\n int ii = q.front().first, jj = q.front().second;\n q.pop();\n if(infect_time[ii][jj] == -1 or reach_time[i][j][ii][jj] < infect_time[ii][jj]) {\n for(int d = 0; d < 4; d++) {\n int ni = ii + di[d], nj = jj + dj[d];\n if(0 <= ni and ni < n and 0 <= nj and nj < n and g[ni][nj] == '.'\n and (infect_time[ni][nj] == -1 or reach_time[i][j][ii][jj] + 1 <= infect_time[ni][nj])\n and reach_time[i][j][ni][nj] == -1) {\n reach_time[i][j][ni][nj] = reach_time[i][j][ii][jj] + 1;\n q.push({ni, nj});\n }\n }\n }\n }\n }\n }\n }\n int src = 2 * n * n, sink = 2 * n * n + 1;\n init_flow();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(n_scientists[i][j]) {\n add_edge(src, node(n, i, j, false), n_scientists[i][j]);\n }\n if(n_rescue[i][j]) {\n add_edge(node(n, i, j, true), sink, n_rescue[i][j]);\n }\n for(int ii = 0; ii < n; ii++) {\n for(int jj = 0; jj < n; jj++) {\n if(0 <= reach_time[i][j][ii][jj] and reach_time[i][j][ii][jj] <= t) {\n add_edge(node(n, i, j, false), node(n, ii, jj, true), INF);\n }\n }\n }\n }\n }\n cout << max_flow(src, sink) << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "1078e3163870678fae3b8bc1ec1f4381", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \nusing namespace std;\nchar a[20][20],b[20][20];\nqueue > q;\nint Dis[20][20],dis[11][11][11][11];\nint fx[5]={-1,1,0,0},fy[5]={0,0,-1,1};\nstruct edge\n{\n\tint u,v,fl,c,n;\n}e[200005];\nint cnt,head[505],p[505],v[505],s=501,t=502,ans,n,m,dep[505];\ninline void add(int u,int v,int w)\n{\n//\tcout << u << \" \" << v << \" \" << w << \"\\n\";\n\te[++cnt]={u,v,0,w,head[u]};\n\thead[u]=cnt;\n\te[++cnt]={v,u,0,0,head[v]};\n\thead[v]=cnt;\n}\ninline bool Find()\n{\n\tqueue q;\n\tfor(int i=0;i<=t;i++)\n\t\tv[i]=dep[i]=0;\n\tq.push(s);\n\tv[s]=1;\n\tdep[s]=1;\n\twhile(!q.empty())\n\t{\n\t\tint x=q.front();\n\t\tq.pop();\n\t\tfor(int i=head[x];i;i=e[i].n)\n\t\t{\n\t\t\tif(!v[e[i].v]&&e[i].fle[i].fl&&dep[e[i].v]==dep[x]+1)\n\t\t{\n int delta=dfs(e[i].v,min(t,e[i].c-e[i].fl));\n\t\t\tif(!delta)\n\t\t\t\tdep[e[i].v]=0;\n e[i].fl+=delta;\n e[((i-1)^1)+1].fl-=delta;\n t-=delta;\n if(t==0)\n\t\t\t\tbreak;\n }\n\t}\n\treturn now-t;\n}\ninline void dinic()\n{\n\twhile(Find())\n\t\tans+=dfs(s,1000000007);\n}\nint main(int argc, char** argv) {\n\tint n,t;\n\tcin >> n >> t;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\t{\n\t\t\tDis[i][j]=10000;\n\t\t\tcin >> a[i][j];\n\t\t\tif(a[i][j]=='Z')\n\t\t\t\tq.push({i,j}),Dis[i][j]=0;\n\t\t\tif(a[i][j]<='9') add(s,(i-1)*n+j,a[i][j]-'0');\n\t\t}\n\t}\n\twhile(!q.empty())\n\t{\n\t\tint x=q.front().first,y=q.front().second;\n\t\tq.pop();\n\t\tfor(int i=0;i<4;i++)\n\t\t{\n\t\t\tint nx=x+fx[i],ny=y+fy[i];\n\t\t\tif(nx>0&&ny>0&&nx<=n&&ny<=n&&a[nx][ny]<='9'&&Dis[nx][ny]>Dis[x][y]+1)\n\t\t\t\tDis[nx][ny]=Dis[x][y]+1,q.push({nx,ny}); \n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\t{\n\t\t\tif(a[i][j]>'9') continue;\n\t\t\tfor(int k=1;k<=n;k++)\n\t\t\t\tfor(int l=1;l<=n;l++)\n\t\t\t\t\tdis[i][j][k][l]=10000;\n\t\t\tq.push({i,j});\n\t\t\tdis[i][j][i][j]=0;\n\t\t\twhile(!q.empty())\n\t\t\t{\n\t\t\t\tint x=q.front().first,y=q.front().second;\n\t\t\t\tq.pop();\n\t\t\t\tfor(int f=0;f<4;f++)\n\t\t\t\t{\n\t\t\t\t\tint nx=x+fx[f],ny=y+fy[f];\n\t\t\t\t\tif(nx>0&&ny>0&&nx<=n&&ny<=n&&a[nx][ny]<='9'&&dis[i][j][nx][ny]>dis[i][j][x][y]+1&&dis[i][j][x][y]> b[i][j];\n\t\t\tif(b[i][j]<='9') add((i-1)*n+j+100,502,b[i][j]-'0');\n\t\t}\n\t}\n\tdinic();\n\tcout << ans;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "9f841e212072cc0adc618b17b0d01cb9", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _USE_MATH_DEFINES\n\n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\n#define mp make_pair\n#define pb push_back\n\n#define DEBUG(x) cout << #x << \": \" << (x) << endl;\n\nchar A[12][12];\nchar B[12][12];\n int n, t;\nbool _dp[70][12][12][12][12];\nbool dp[12][12][12][12];\n\nint tim[12][12];\n\nvoid setdp(int tt, int x, int y, int x0, int y0) {\n if (t >= 70) return;\n if (x < 0 || x >= n || y < 0 || y >= n)\n return;\n if (A[x][y] < '0' || A[x][y] > '9')\n return;\n if (tt > t+1)\n return;\n if (_dp[tt][x][y][x0][y0])\n return;\n if (tt > tim[x][y] && tim[x][y] != 0)\n return;\n if (tt == tim[x][y] && tim[x][y] != 0 && (B[x][y] > '9' || B[x][y] <= '0'))\n return;\n _dp[tt][x][y][x0][y0] = true;\n\n if (tt < tim[x][y])\n {\n setdp(tt+1, x, y, x0, y0);\n setdp(tt+1, x+1, y, x0, y0);\n setdp(tt+1, x-1, y, x0, y0);\n setdp(tt+1, x, y+1, x0, y0);\n setdp(tt+1, x, y-1, x0, y0);\n }\n}\n\n\nvector > G;\nint F[210][210];\nint E[210], H[210];\nint C[210][210];\n\nint pN[11][11];\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen( \"1.txt\", \"rt\", stdin );\n //freopen(\"output.txt\", \"wt\", stdout);\n#endif\n\n\n cin >> n >> t;\n\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> A[i][j];\n\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> B[i][j];\n\n deque > q;\n\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n tim[i][j] = -1;\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n if (A[i][j] == 'Z')\n {\n tim[i][j] = 0;\n q.push_back(mp(i,j));\n }\n\n int di[] = {-1, 0, 1, 0};\n int dj[] = {0, -1, 0, 1};\n \n while (!q.empty())\n {\n pair p = q.front();\n q.pop_front();\n if (tim[p.first][p.second] >= t)\n continue;\n for (int d = 0; d <= 3; d++)\n {\n pair np = mp(p.first+di[d], p.second+dj[d]);\n if (np.first >= 0 && np.first < n && np.second >= 0 && np.second < n &&\n A[np.first][np.second] >= '0' && A[np.first][np.second] <= '9' && tim[np.first][np.second]==-1)\n {\n tim[np.first][np.second] = tim[p.first][p.second]+1;\n q.push_back(np);\n }\n }\n }\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n if (tim[i][j] == -1)\n tim[i][j] = t;\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n if (A[i][j] >= '0' && A[i][j] <= '9')\n setdp(0,i,j,i,j);\n\n vector st;\n G.pb(st);\n\n for (int i1 = 0; i1 < n; i1++)\n for (int j1 = 0; j1 < n; j1++)\n for (int i2 = 0; i2 < n; i2++)\n for (int j2 = 0; j2 < n; j2++)\n for (int nt = 0; nt <= t; nt++)\n dp[i1][j1][i2][j2] |= _dp[nt][i1][j1][i2][j2];\n \n\n for (int i1 = 0; i1 < n; i1++)\n for (int j1 = 0; j1 < n; j1++)\n if (A[i1][j1] > '0' && A[i1][j1] <= '9')\n {\n int k = G.size();\n vector g;\n G.push_back(g);\n E[k] = A[i1][j1] - '0';\n\n for (int i2 = 0; i2 < n; i2++)\n for (int j2 = 0; j2 < n; j2++)\n if (B[i2][j2] > '0' && B[i2][j2] <= '9')\n if (dp[i2][j2][i1][j1])\n {\n int _k;\n \n if (pN[i2][j2] == 0)\n {\n vector _g;\n _k = G.size();\n G.push_back(_g);\n pN[i2][j2] = _k;\n }\n else\n {\n _k = pN[i2][j2];\n }\n \n G[k].pb(_k);\n G[_k].pb(k);\n C[k][_k] = 10000;\n C[_k][k] = 0;\n }\n }\n\n for (int i2 = 0; i2 < n; i2++)\n for (int j2 = 0; j2 < n; j2++)\n if (B[i2][j2] > '0' && B[i2][j2] <= '9' && pN[i2][j2] > 0)\n {\n int _k;\n _k = pN[i2][j2];\n G[_k].push_back(0);\n\n C[_k][0] = B[i2][j2] - '0';\n C[0][_k] = 0;\n }\n\n while(1)\n {\n bool ff = true;\n for (int i = 1; i < G.size(); i++)\n while (E[i] > 0)\n {\n bool f = true;\n int minH = 10000;\n for (int _j = 0; _j < G[i].size(); _j++)\n {\n int j = G[i][_j];\n if (C[i][j] - F[i][j]) minH = min(minH, H[j]);\n if (C[i][j] - F[i][j] > 0 && H[i]==H[j]+1)\n {\n int d = min(C[i][j] - F[i][j], E[i]);\n F[i][j] += d;\n F[j][i] -= d;\n E[i] -= d;\n E[j] += d;\n f = false;\n ff = false;\n if (E[i] == 0)\n goto next;\n \n }\n }\n if (H[i] != minH+1 && minH != 10000)\n f = false,\n ff = false,\n H[i] = minH+1;\n\n if (f)\nnext: break;\n }\n if (ff)\n break;\n }\n\n\nexit:\n cout << E[0];\n return ( 0 );\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "cdd997af1652492edb15709a54b424d5", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n \n#define LL long long int\n#define ULL unsigned LL\n#define PII pair\n#define PB push_back\n#define MP make_pair\n#define INF 1000000000\n#define MOD 1000000007\n \nint shortest[15][15][15][15];\nint cap[400][400];\nchar scien[20][20];\nchar reac[20][20];\nint dx[] = {1, 0, -1, 0};\nint dy[] = {0, 1, 0, -1};\nint iter = 3;\nint vis[400];\n\nint n, t;\nint aug(int cnode, int dest, int bneck) {\n if (cnode == dest) {\n return bneck;\n }\n if (vis[cnode] == iter) {\n return 0;\n }\n vis[cnode] = iter;\n for (int i = 0; i <= dest; i++) {\n int cret = 0;\n if (cap[cnode][i] > 0 && (cret = aug(i, dest, min(bneck, cap[cnode][i])))) {\n // cout << cnode << \" \";\n cap[cnode][i] -= cret;\n cap[i][cnode] += cret;\n return cret;\n }\n }\n return 0;\n}\nint getnode(int x, int y) {\n return (x - 1) * n + y - 1;\n}\nint reach[13][13];\nint treach[13][13];\nvoid expand(int val) {\n int i, j, k;\n for (i = 1; i <= n; i++)\n for (j = 1; j <=n; j++)\n treach[i][j] = reach[i][j];\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n if (treach[i][j] == val) {\n for (k = 0; k < 4; k++) {\n int ni = i + dx[k];\n int nj = j + dy[k];\n if (ni > 0 && nj > 0 && ni <= n && nj <= n && treach[ni][nj] < val && scien[ni][nj] != 'Y') {\n reach[ni][nj] = val;\n }\n }\n }\n }\n }\n}\nbool canreach(int sx, int sy, int tx, int ty, int ix, int iy) {\n memset(reach, 0, sizeof(reach));\n reach[sx][sy] = 1;\n reach[ix][iy] = 2;\n int iters = 100;\n while (iters--) {\n expand(1);\n if (reach[tx][ty] == 1)\n return true;\n expand(2);\n if (reach[tx][ty] == 2)\n return false; \n }\n return false;\n}\nint main() {\n int infx, infy;\n memset(shortest, 1, sizeof(shortest));\n cin >> n >> t;\n int i, j;\n for (i = 1; i <= n; i++) {\n scanf(\" %s\", scien[i] + 1);\n for (j = 1; j <= n; j++) {\n shortest[i][j][i][j] = 0;\n if (scien[i][j] == 'Z') {\n infx = i;\n infy = j;\n }\n }\n }\n for (i = 1; i <= n; i++) {\n scanf(\" %s\", reac[i] + 1);\n }\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n for (int k = 0; k < 4; k++) {\n int ni = i + dx[k];\n int nj = j + dy[k];\n if (ni <= n && ni > 0 && nj <= n && nj > 0) {\n if (scien[i][j] != 'Y' && scien[ni][nj] != 'Y')\n shortest[i][j][ni][nj] = 1;\n }\n }\n }\n }\n int ki, kj, ii, ij, ji, jj;\n for (ki = 1; ki <= n; ki++) {\n for (kj = 1; kj <= n; kj++) {\n for (ii = 1; ii <= n; ii++) {\n for (ij = 1; ij <= n; ij++) {\n for (ji = 1; ji <= n; ji++) {\n for (jj = 1; jj <= n; jj++) {\n shortest[ii][ij][ji][jj] = min(\n shortest[ii][ij][ji][jj], \n shortest[ii][ij][ki][kj] + shortest[ki][kj][ji][jj]\n );\n }\n }\n }\n }\n }\n }\n int src = n * n * 2 + 1;\n int dest = n * n * 2 + 2;\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n if (isdigit(scien[i][j])) {\n cap[src][getnode(i, j)] = scien[i][j] - '0';\n }\n if (isdigit(reac[i][j])) {\n cap[n * n + getnode(i, j)][dest] = reac[i][j] - '0';\n }\n\n if (isdigit(scien[i][j])) {\n for (ki = 1; ki <= n; ki++) {\n for (kj = 1; kj <= n; kj++) {\n if (scien[i][j] > '0' && reac[ki][kj] > '0') {\n if (isdigit(reac[ki][kj]) && shortest[i][j][ki][kj] <= t && canreach(i, j, ki, kj, infx, infy)) {\n // printf(\"(%d, %d) can reach (%d, %d)\\n\", i, j, ki, kj);\n cap[getnode(i, j)][n * n + getnode(ki, kj)] = 1000;\n }\n }\n }\n }\n }\n }\n }\n int mflow = 0, ret;\n while ((ret = aug(src, dest, 1000))) {\n // cout << endl;\n mflow += ret;\n iter ++;\n }\n cout << mflow << endl;\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "a8f376fdd6c0ab2a758909f5593f5f06", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#define MAXN 256\n#define MAXT 64\n#define MAXE MAXN * MAXN\n#define INF 1000000000\nusing namespace std;\nclass Dinic {\n\tstruct edge {\n\t\tint u, v, f, c, nx;\n\t\tedge() {}\n\t\tedge(int u, int v, int f, int c, int nx): u(u), v(v), f(f), c(c), nx(nx) {}\n\t} e[MAXE];\n\tint n, m, src, dst, head[MAXN], work[MAXN], d[MAXN];\n\tbool _bfs() {\n\t\tstatic int q[MAXN], le, ri;\n\t\tle = ri = 0;\n\t\tmemset(d, -1, sizeof(int) * n);\n\t\tq[ri++] = src;\n\t\td[src] = 0;\n\t\twhile(le < ri) {\n\t\t\tfor(int k = q[le++], i = head[k]; i != -1; i = e[i].nx) {\n\t\t\t\tif(e[i].f < e[i].c && d[e[i].v] == -1) {\n\t\t\t\t\td[e[i].v] = d[k] + 1;\n\t\t\t\t\tq[ri++] = e[i].v;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn (d[dst] != -1);\n\t}\n\tint _dfs(int u, int f) {\n\t\tif(u == dst)\n\t\t\treturn f;\n\t\tint minf;\n\t\tfor(int& i = work[u]; i != -1; i = e[i].nx) {\n\t\t\tif(e[i].f < e[i].c && d[e[i].v] == d[u] + 1) {\n\t\t\t\tminf = _dfs(e[i].v, min(f, e[i].c - e[i].f));\n\t\t\t\tif(minf > 0) {\n\t\t\t\t\te[i].f += minf;\n\t\t\t\t\te[i ^ 1].f -= minf;\n\t\t\t\t\treturn minf;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\npublic:\n\tvoid init(int n, int src, int dst) {\n\t\tthis->n = n;\n\t\tthis->src = src;\n\t\tthis->dst = dst;\n\t\tm = 0;\n\t\tmemset(head, -1, sizeof(int) * n);\n\t}\n\tvoid addEdge(int u, int v, int c) {\n\t\t//printf(\"%d %d %d\\n\", u, v, c);\n\t\te[m] = edge(u, v, 0, c, head[u]);\n\t\thead[u] = m++;\n\t\te[m] = edge(v, u, 0, 0, head[v]);\n\t\thead[v] = m++;\n\t}\n\tint maxFlow() {\n\t\tint ret = 0, delta;\n\t\twhile(_bfs()) {\n\t\t\tmemcpy(work, head, sizeof(int) * n);\n\t\t\twhile(true) {\n\t\t\t\tdelta = _dfs(src, INF);\n\t\t\t\tif(delta == 0)\n\t\t\t\t\tbreak;\n\t\t\t\tret += delta;\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n} dinic;\n#define MAX 12\nint n, t, zx, zy;\nchar smp[MAX][MAX], cmp[MAX][MAX], work[MAX][MAX];\nint vis[MAX][MAX];\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, -1, 0, 1};\ninline int X(int i, int j) { return i * n + j; }\ninline int Y(int i, int j) { return n * n + i * n + j; }\ninline int S() { return n * n * 2; }\ninline int T() { return S() + 1; }\ninline int N() { return T() + 1; }\nstruct qNode {\n\tint x, y, t, type;\n\tqNode() {}\n\tqNode(int x, int y, int t, int type): x(x), y(y), t(t), type(type) {}\n};\nvoid bfs(int sx, int sy) {\n\tqueue q;\n\tq.push(qNode(sx, sy, 0, 0));\n\tvis[sx][sy] = 1;\n\tq.push(qNode(zx, zy, 0, 1));\n\twhile(!q.empty()) {\n\t\tqNode u = q.front();\n\t\tint nx, ny;\n\t\tq.pop();\n\t\tif(u.t == t)\n\t\t\tbreak;\n\t\tif(u.type == 0 && isalpha(work[u.x][u.y]))\n\t\t\tcontinue;\n\t\tfor(int i = 0; i < 4; ++i) {\n\t\t\tnx = u.x + dx[i];\n\t\t\tny = u.y + dy[i];\n\t\t\tif(nx >= 0 && nx < n && ny >= 0 && ny < n) {\n\t\t\t\tif(u.type == 0 && !vis[nx][ny] && !isalpha(work[nx][ny])) {\n\t\t\t\t\tvis[nx][ny] = 1;\n\t\t\t\t\tq.push(qNode(nx, ny, u.t + 1, 0));\n\t\t\t\t}\n\t\t\t\telse if(u.type == 1 && !isalpha(work[nx][ny])) {\n\t\t\t\t\twork[nx][ny] = 'Z';\n\t\t\t\t\tq.push(qNode(nx, ny, u.t + 1, 1));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nint main() {\n\twhile(scanf(\"%d%d\", &n, &t) != EOF) {\n\t\tdinic.init(N(), S(), T());\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tscanf(\"%s\", smp[i]);\n\t\t\tfor(int j = 0; j < n; ++j)\n\t\t\t\tif(isdigit(smp[i][j]) && smp[i][j] > '0')\n\t\t\t\t\tdinic.addEdge(S(), X(i, j), smp[i][j] - '0');\n\t\t\t\telse if(smp[i][j] == 'Z') {\n\t\t\t\t\tzx = i;\n\t\t\t\t\tzy = j;\n\t\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tscanf(\"%s\", cmp[i]);\n\t\t\tfor(int j = 0; j < n; ++j)\n\t\t\t\tif(isdigit(cmp[i][j]) && cmp[i][j] > '0')\n\t\t\t\t\tdinic.addEdge(Y(i, j), T(), cmp[i][j] - '0');\n\t\t}\n\t\tfor(int i = 0; i < n; ++i)\n\t\t\tfor(int j = 0; j < n; ++j) {\n\t\t\t\tif(!isdigit(smp[i][j]) || smp[i][j] <= '0')\n\t\t\t\t\tcontinue;\n\t\t\t\tmemset(vis, 0, sizeof(vis));\n\t\t\t\tmemcpy(work, smp, sizeof(smp));\n\t\t\t\tbfs(i, j);\n\t\t\t\tfor(int xi = 0; xi < n; ++xi)\n\t\t\t\t\tfor(int yi = 0; yi < n; ++yi) {\n\t\t\t\t\t\tif(!isdigit(cmp[xi][yi]) || cmp[xi][yi] <= '0')\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\tif(vis[xi][yi]) {\n\t\t\t\t\t\t\tdinic.addEdge(X(i, j), Y(xi, yi), INF);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t}\n\t\tprintf(\"%d\\n\", dinic.maxFlow());\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "75ce4da362700ec049002eacd83ca0b8", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n \ntypedef long long LL;\n \nconst int inf=0x3f3f3f3f;\n\nconst int N=210;\n \nstruct Edge\n{\n\tint to,w,next;\n}edge[N*N];//边数\n\nconst int b[4][2]={0,1,0,-1,1,0,-1,0};\n\nint n,t,st=N-1,ed=st-1,cap[15][15],sci[15][15],maze[65][15][15],head[N],cnt;\n\nbool vis[15][15],dp[65][15][15][15][15];\n \nvoid addedge(int u,int v,int w)\n{\n\tedge[cnt].to=v;\n\tedge[cnt].w=w;\n\tedge[cnt].next=head[u];\n\thead[u]=cnt++;\n\tedge[cnt].to=u;\n\tedge[cnt].w=0;//反向边边权设置为0\n\tedge[cnt].next=head[v];\n\thead[v]=cnt++;\n}\n \nint d[N],now[N];//深度 当前弧优化\n \nbool bfs(int s,int t)//寻找增广路\n{\n\tmemset(d,0,sizeof(d));\n\tqueueq;\n\tq.push(s);\n\tnow[s]=head[s];\n\td[s]=1;\n\twhile(!q.empty())\n\t{\n\t\tint u=q.front();\n\t\tq.pop();\n\t\tfor(int i=head[u];i!=-1;i=edge[i].next)\n\t\t{\n\t\t\tint v=edge[i].to;\n\t\t\tint w=edge[i].w;\n\t\t\tif(d[v])\n\t\t\t\tcontinue;\n\t\t\tif(!w)\n\t\t\t\tcontinue;\n\t\t\td[v]=d[u]+1;\n\t\t\tnow[v]=head[v];\n\t\t\tq.push(v);\n\t\t\tif(v==t)\n\t\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n \nint dinic(int x,int t,int flow)//更新答案\n{\n\tif(x==t)\n\t\treturn flow;\n\tint rest=flow,i;\n\tfor(i=now[x];i!=-1&&rest;i=edge[i].next)\n\t{\n\t\tint v=edge[i].to;\n\t\tint w=edge[i].w;\n\t\tif(w&&d[v]==d[x]+1)\n\t\t{\n\t\t\tint k=dinic(v,t,min(rest,w));\n\t\t\tif(!k)\n\t\t\t\td[v]=0;\n\t\t\tedge[i].w-=k;\n\t\t\tedge[i^1].w+=k;\n\t\t\trest-=k;\n\t\t}\n\t}\n\tnow[x]=i;\n\treturn flow-rest;\n}\n \nint solve(int st,int ed)\n{\n\tint ans=0,flow;\n\twhile(bfs(st,ed))\n\t\twhile(flow=dinic(st,ed,inf))\n\t\t\tans+=flow;\n\treturn ans;\n}\n\nint get_id(int x,int y,int k)//science:0 capsule:1\n{\n\treturn (x-1)*n+y+k*n*n;\n}\n\nvoid dfs(int x,int y,int xx,int yy,int step)\n{\n\tvis[xx][yy]=true;\n\tdp[step][x][y][xx][yy]=true;\n\tif(step>=t||maze[step][xx][yy])\n\t\treturn;\n\tfor(int k=0;k<4;k++)\n\t{\n\t\tint nx=xx+b[k][0];\n\t\tint ny=yy+b[k][1];\n\t\tif(nx<=0||ny<=0||nx>n||ny>n)\n\t\t\tcontinue;\n\t\tif(vis[nx][ny])\n\t\t\tcontinue;\n\t\tif(maze[step][nx][ny])\n\t\t\tcontinue; \n\t\tdfs(x,y,nx,ny,step+1);\n\t}\n}\n\nvoid init()\n{\n\tmemset(now,0,sizeof(now));\n\tmemset(head,-1,sizeof(head));\n\tcnt=0;\n\tfor(int time=1;time<=t;time++)\n\t{\n\t\tfor(int x=1;x<=n;x++)\n\t\t{\n\t\t\tfor(int y=1;y<=n;y++)\n\t\t\t{\n\t\t\t\tif(maze[time-1][x][y])\n\t\t\t\t\tmaze[time][x][y]=maze[time-1][x][y];\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfor(int k=0;k<4;k++)\n\t\t\t\t\t{\n\t\t\t\t\t\tint xx=x+b[k][0];\n\t\t\t\t\t\tint yy=y+b[k][1];\n\t\t\t\t\t\tif(xx<=0||yy<=0||xx>n||yy>n)\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\tif(maze[time-1][xx][yy]==2)\n\t\t\t\t\t\t\tmaze[time][x][y]=2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\t{\n\t\t\tif(sci[i][j])\n\t\t\t{\n\t\t\t\tmemset(vis,false,sizeof(vis));\n\t\t\t\tdfs(i,j,i,j,0);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=t;i++)\n\t\tfor(int x=1;x<=n;x++)\n\t\t\tfor(int y=1;y<=n;y++)\n\t\t\t\tfor(int xx=1;xx<=n;xx++)\n\t\t\t\t\tfor(int yy=1;yy<=n;yy++)\n\t\t\t\tdp[i][x][y][xx][yy]|=dp[i-1][x][y][xx][yy];\n}\n\nint main()\n{\n// freopen(\"input.txt\",\"r\",stdin);\n// ios::sync_with_stdio(false);\n\tscanf(\"%d%d\",&n,&t);\n for(int i=1;i<=n;i++)\n {\n \tchar s[15];\n \tscanf(\"%s\",s+1);\n \tfor(int j=1;j<=n;j++)\n \t{\n \t\tif(isdigit(s[j]))\n \t\t\tsci[i][j]=s[j]-'0';\n \t\telse if(s[j]=='Y')\n \t\t\tmaze[0][i][j]=1;\n \t\telse if(s[j]=='Z')\n \t\t\tmaze[0][i][j]=2;\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tchar s[15];\n\t\tscanf(\"%s\",s+1);\n\t\tfor(int j=1;j<=n;j++)\n\t\t{\n\t\t\tif(isdigit(s[j]))\n\t\t\t\tcap[i][j]=s[j]-'0';\n\t\t}\n\t}\n\tinit();\n for(int i=1;i<=n;i++)\n {\n \tfor(int j=1;j<=n;j++)\n \t{\n \t\tif(sci[i][j])\n \t\t\taddedge(st,get_id(i,j,0),sci[i][j]);\n \t\tif(cap[i][j])\n \t\t\taddedge(get_id(i,j,1),ed,cap[i][j]);\n\t\t}\n\t}\n\tfor(int x=1;x<=n;x++)\n\t\tfor(int y=1;y<=n;y++)\n\t\t\tfor(int xx=1;xx<=n;xx++)\n\t\t\t\tfor(int yy=1;yy<=n;yy++)\n\t\t\t\t\tif(sci[x][y]&&cap[xx][yy]&&dp[t][x][y][xx][yy])\n\t\t\t\t\t\taddedge(get_id(x,y,0),get_id(xx,yy,1),inf);\n\tprintf(\"%d\\n\",solve(st,ed));\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n return 0;\n}\n \t\t\t \t\t\t \t \t\t \t\t\t\t\t\t \t\t\t\t//hellobig_tq", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "f3acabcdc87ffc1842e9edc433e7b302", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define y1 Y1\nconst int INF=1<<29;\nconst int MAXN=500;\nstruct edge\n{\n int a,b,cap,flow;\n};\nbool m[11][11];\nvector g[MAXN];\nvector e;\nint d[MAXN],ptr[MAXN],n,xb,yb,t,temp[11][11],kaps[11][11];\nint way(int x1,int y1,int x2,int y2)\n{\n queue > > l;\n l.push(make_pair(0,make_pair(x1,y1)));\n int h[11][11];\n for(int p=0;p0 && m[x-1][y] && h[x-1][y]>h[x][y]+1 && (x-1==x2 && y==y2 && temp[x-1][y]>=h[x][y]+1 || !(x-1==x2 && y==y2) && temp[x-1][y]>h[x][y]+1)) l.push(make_pair(v+1,make_pair(x-1,y)));\n if (y>0 && m[x][y-1] && h[x][y-1]>h[x][y]+1 && (x==x2 && y-1==y2 && temp[x][y-1]>=h[x][y]+1 || !(x==x2 && y-1==y2) && temp[x][y-1]>h[x][y]+1)) l.push(make_pair(v+1,make_pair(x,y-1)));\n if (xh[x][y]+1 && (x+1==x2 && y==y2 && temp[x+1][y]>=h[x][y]+1 || !(x+1==x2 && y==y2) && temp[x+1][y]>h[x][y]+1)) l.push(make_pair(v+1,make_pair(x+1,y)));\n if (yh[x][y]+1 && (x==x2 && y+1==y2 && temp[x][y+1]>=h[x][y]+1 || !(x==x2 && y+1==y2) && temp[x][y+1]>h[x][y]+1)) l.push(make_pair(v+1,make_pair(x,y+1)));\n }\n return h[x2][y2];\n}\nvoid add_edge(int a,int b,int cap)\n{\n edge e1={a,b,cap,0};\n edge e2={b,a,0,0};\n g[a].push_back((int)e.size());\n e.push_back(e1);\n g[b].push_back((int)e.size());\n e.push_back(e2);\n}\nbool bfs()\n{\n queue q;\n q.push(0);\n memset(d,-1,(n*n*2+2)*sizeof d[0]);\n d[0]=0;\n while (!q.empty() && d[n*n*2+1]==-1)\n {\n int v=q.front();\n q.pop();\n for(size_t j=0;j>n>>t;\n queue > > l;\n char x;\n int k=1;\n for(int i=0;i>x;\n temp[i][j]=t;\n if (x=='Z')\n {\n xb=i;\n yb=j;\n m[i][j]=false;\n } else\n if (x=='Y') m[i][j]=false; else m[i][j]=true;\n add_edge(0,k++,x=='Z' || x=='Y'?0:x-48);\n }\n }\n k=n*n+1;\n for(int i=0;i>x;\n if (x>='0' && x<='9') kaps[i][j]=x-48;\n add_edge(k++,n*n*2+1,x=='Z' || x=='Y'?0:x-48);\n }\n l.push(make_pair(0,make_pair(xb,yb)));\n while (!l.empty())\n {\n int v=l.front().first,x=l.front().second.first,y=l.front().second.second;\n l.pop();\n temp[x][y]=v;\n if (x>0 && v+10 && v+10) cout<\nusing namespace std;\ntypedef pair pii;\nchar s1[15][15], s2[15][15];\nint dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\nint idx[65][15][15];\nint dp[15][15];\nint id = 1;\n\ntypedef long long LL;\nstruct Edge {\n int u, v;\n LL cap, flow;\n Edge() {}\n Edge(int u, int v, LL cap): u(u), v(v), cap(cap), flow(0) {}\n};\n\nstruct Dinic {\n int N;\n vector E;\n vector> g;\n vector d, pt;\n \n Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}\n\n void AddEdge(int u, int v, LL cap) {\n if (u != v) {\n E.emplace_back(Edge(u, v, cap));\n g[u].emplace_back(E.size() - 1);\n E.emplace_back(Edge(v, u, 0));\n g[v].emplace_back(E.size() - 1);\n }\n }\n\n bool BFS(int S, int T) {\n queue q({S});\n fill(d.begin(), d.end(), N + 1);\n d[S] = 0;\n while(!q.empty()) {\n int u = q.front(); q.pop();\n if (u == T) break;\n for (int k: g[u]) {\n Edge &e = E[k];\n if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {\n d[e.v] = d[e.u] + 1;\n q.emplace(e.v);\n }\n }\n }\n return d[T] != N + 1;\n }\n\n LL DFS(int u, int T, LL flow = -1) {\n if (u == T || flow == 0) return flow;\n for (int &i = pt[u]; i < g[u].size(); ++i) {\n Edge &e = E[g[u][i]];\n Edge &oe = E[g[u][i]^1];\n if (d[e.v] == d[e.u] + 1) {\n LL amt = e.cap - e.flow;\n if (flow != -1 && amt > flow) amt = flow;\n if (LL pushed = DFS(e.v, T, amt)) {\n e.flow += pushed;\n oe.flow -= pushed;\n return pushed;\n }\n }\n }\n return 0;\n }\n\n LL MaxFlow(int S, int T) {\n LL total = 0;\n while (BFS(S, T)) {\n fill(pt.begin(), pt.end(), 0);\n while (LL flow = DFS(S, T))\n total += flow;\n }\n return total;\n }\n};\n\nint main(){\n\tmemset(dp, -1, sizeof(dp));\n\tint n, t;\n\tscanf(\"%d%d\", &n, &t);\n\tfor(int i=1;i<=n;i++){\n\t\tscanf(\"%s\", s1[i]+1);\n\t}\n\tfor(int i=0;i<=t+1;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tfor(int k=1;k<=n;k++){\n\t\t\t\tidx[i][j][k] = id++;\n\t\t\t}\n\t\t}\n\t}\n\tid++;\n\tfor(int i=1;i<=n;i++){\n\t\tscanf(\"%s\", s2[i]+1);\n\t}\n\tqueue q;\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(s1[i][j] == 'Z'){\n\t\t\t\tq.push(pii(i, j));\n\t\t\t\tdp[i][j] = 0;\n\t\t\t}\n\t\t}\n\t}\n\twhile(!q.empty()){\n\t\tint uy = q.front().first, ux = q.front().second;\n\t\tq.pop();\n\t\tfor(int i=0;i<4;i++){\n\t\t\tint vy = uy+dir[i][0], vx = ux+dir[i][1];\n\t\t\tif(vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' && dp[vy][vx] == -1){\n\t\t\t\tdp[vy][vx] = dp[uy][ux] + 1;\n\t\t\t\tq.push(pii(vy, vx));\n\t\t\t}\t\t\t\t\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(dp[i][j] == -1) dp[i][j] = 1e9;\n\t\t\tif(dp[i][j] > t) dp[i][j] = 1e9;\n\t\t}\n\t}\n\tint source = 0, sink = id-1;\n\tDinic dinic(id);\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(s1[i][j] >= '1' && s1[i][j] <= '9'){\n\t\t\t\tdinic.AddEdge(source, idx[0][i][j], s1[i][j]-'0');\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;\n\t\t\tfor(int k=0;k<4;k++){\n\t\t\t\tint vy = i+dir[k][0], vx = j+dir[k][1];\n\t\t\t\tif(vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' && s1[vy][vx] != 'Z'){\n\t\t\t\t\tfor(int x=0;x= x+1 && dp[i][j] > x){\n\t\t\t\t\t\t\tdinic.AddEdge(idx[x][i][j], idx[x+1][vy][vx], 1000);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;\n\t\t\tfor(int x=0;x<=t;x++){\n\t\t\t\tif(s2[i][j] >= '1' && s2[i][j] <= '9'){\n\t\t\t\t\tdinic.AddEdge(idx[x][i][j], idx[t+1][i][j], s2[i][j]-'0');\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;\n\t\t\tif(s2[i][j] >= '1' && s2[i][j] <= '9'){\n\t\t\t\tdinic.AddEdge(idx[t+1][i][j], sink, s2[i][j]-'0');\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", dinic.MaxFlow(source, sink));\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "9d53024c5ac55bd3d43ad332b128ddb9", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef double db;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pis;\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\nconst int N = 11;\nconst int INF = 1e9;\nconst int SIZE = 2 * N * N;\n\nstruct edge\n{\n int to, c, f, rev;\n edge() {}\n edge(int too, int cc, int ff, int revv)\n {\n to = too;\n c = cc;\n f = ff;\n rev = revv;\n }\n};\n\nvector g[SIZE];\nint d[SIZE], ptr[SIZE], s, t, n;\nqueue q;\n\nvoid addEdge(int v, int u, int cap)\n{\n g[v].pb(edge(u, cap, 0, 0));\n g[u].pb(edge(v, 0, 0, 0));\n g[v].back().rev = g[u].size() - 1;\n g[u].back().rev = g[v].size() - 1;\n}\nbool buildLevNet()\n{\n for (int i = 0; i <= t; ++i)\n d[i] = INF;\n d[s] = 0;\n q.push(s);\n while (!q.empty())\n {\n int v = q.front();\n q.pop();\n for (int i = 0; i < g[v].size(); ++i)\n {\n int to = g[v][i].to;\n int cf = g[v][i].c - g[v][i].f;\n if (d[to] == INF && cf > 0)\n {\n d[to] = d[v] + 1;\n q.push(to);\n }\n }\n }\n return d[t] != INF;\n}\n\nint dfs(int v, int curFlow)\n{\n if (v == t) return curFlow;\n for (int& i = ptr[v]; i < g[v].size(); ++i)\n {\n int to = g[v][i].to;\n int cf = g[v][i].c - g[v][i].f, addFlow;\n if (d[to] == d[v] + 1 && cf > 0 && (addFlow = dfs(to, min(curFlow, cf))))\n {\n g[v][i].f += addFlow;\n g[to][g[v][i].rev].f -= addFlow;\n return addFlow;\n }\n }\n return 0;\n}\n\nint dinica()\n{\n int flow = 0;\n while (buildLevNet())\n {\n memset(ptr, 0, sizeof(ptr));\n int add;\n while ((add = dfs(s, INF)))\n flow += add;\n }\n return flow;\n}\n\n\n\nqueue go;\nconst int X[] = {-1, 1, 0, 0};\nconst int Y[] = {0, 0, -1, 1};\nint dst[N][N][N][N], tbad;\nchar a[N][N], b[N][N];\npii bad;\nbool check(pii v)\n{\n return v.f >= 0 && v.f < n && v.s >= 0 && v.s < n && a[v.f][v.s] != 'Y' && a[v.f][v.s] != 'Z';\n}\nvoid bfs(int xs, int ys)\n{\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n dst[xs][ys][i][j] = INF;\n dst[xs][ys][xs][ys] = 0;\n go.push(mp(xs, ys));\n while (!go.empty())\n {\n pii v = go.front();\n go.pop();\n if (dst[xs][ys][v.f][v.s] == dst[bad.f][bad.s][v.f][v.s] && (bad.f != xs || bad.s != ys)) continue;\n for (int i = 0; i < 4; ++i)\n {\n pii nv = mp(v.f + X[i], v.s + Y[i]);\n if (check(nv) && dst[xs][ys][nv.f][nv.s] == INF && dst[xs][ys][v.f][v.s] + 1 <= dst[bad.f][bad.s][nv.f][nv.s])\n {\n dst[xs][ys][nv.f][nv.s] = dst[xs][ys][v.f][v.s] + 1;\n go.push(nv);\n }\n }\n }\n}\n\nint main()\n{\n cin >> n >> tbad;\n int px, py;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n {\n cin >> a[i][j];\n if (a[i][j] == 'Z') px = i, py = j;\n }\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n cin >> b[i][j];\n bad = mp(px, py);\n bfs(px, py);\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n dst[bad.f][bad.s][i][j] = min(dst[bad.f][bad.s][i][j], tbad);\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n if (check(mp(i, j))) bfs(i, j);\n s = 0;\n t = 2 * n * n + 1;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n {\n if (a[i][j] != '0') addEdge(s, i * n + j + 1, a[i][j] - '0');\n if (b[i][j] != '0') addEdge(i * n + j + 1 + n * n, t, b[i][j] - '0');\n }\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n for (int i1 = 0; i1 < n; ++i1)\n for (int j1 = 0; j1 < n; ++j1)\n if (check(mp(i, j)) && check(mp(i1, j1)) && dst[i][j][i1][j1] <= dst[px][py][i1][j1])\n addEdge(i * n + j + 1, i1 * n + j1 + 1 + n * n, INF);\n cout << dinica();\n\n/*\n2 60\n00\nZ1\n\n00\nZ1\n*/\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "dbced6a36f67bab05475591ef5612795", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\nstruct networkflow{\n struct node{\n int v, w, rev;\n };\n void setEdge(int u, int v, int fl){\n int sz1= (int)adj[v].size();\n int sz2 = (int)adj[u].size();\n adj[u].push_back({v, fl, sz1});\n adj[v].push_back({u, 0, sz2});\n }\n vector > adj;\n vector lev, edge_tried;\n int st, ed;\n networkflow(int V, int s, int t): adj(V), lev(V), edge_tried(V), st(s), ed(t){}\n bool assign_level(){\n fill(lev.begin(), lev.end(), -1);\n lev[st]=0;\n queueque;que.push(st);\n while(!que.empty()){\n int now =que.front();que.pop();\n for(int i=0; i >que;que.push({yy, xx});\n ck[yy][xx]=1;\n int lev=1;\n while(!que.empty()){\n int sz = (int)que.size();\n for(int ite=0; ite now = que.front();que.pop();\n for(int i=0; i<4; i++){\n int ny = now.first + dy[i];\n int nx = now.second + dx[i];\n if(!(ny>=0 && ny=0 && nx>n>>ti;\n for(int i=0; i>str[i];\n for(int j=0 ;j>es[i];\n }\n go();\n int nodecnt=0;\n\n for(int i=0; i=0 && ny=0 &&nx='1' && es[i][j]<='9')break;///마지막골인 가능하지만 뿌려선 안됨\n if(table[ny][nx][t+1]){\n net.setEdge(table[i][j][t], table[ny][nx][t+1], INF);\n }\n }\n }\n\n if('1'<=es[i][j] &&es[i][j]<='9'){\n for(int t = 0; t\n\nusing namespace std;\n\nconst int MAXN = 300;\n\nconst int ROOM = -1;\nconst int REAC = -2;\nconst int BAD_REAC = -3;\nconst int CAPS = ROOM;\n\nclass Aresta {\npublic:\n int from, to, rev, flow, cap;\n Aresta(){}\n Aresta(int _from, int _to, int _rev, int _flow, int _cap){\n from = _from; to = _to; rev = _rev; flow = _flow; cap = _cap;\n }\n int residuo() {return cap-flow;}\n};\n\nclass EdmondsKarp {\npublic:\n int size;\n vector *grafo;\n Aresta **prev;\n bool *vis;\n\n EdmondsKarp(int tam){\n size = tam;\n grafo = new vector[size];\n prev = new Aresta*[size];\n vis = new bool[size];\n }\n\n void limpa(){\n for (int i=0; i fila;\n fila.push(source);\n vis[source] = 1;\n\n int atual;\n while (!fila.empty()){\n atual = fila.front(); fila.pop();\n\n if (atual == sink) return true;\n\n for (Aresta *a : grafo[atual]){\n if (!vis[a->to] && a->residuo() > 0){\n prev[a->to] = a;\n vis[a->to] = true;\n fila.push(a->to);\n }\n }\n }\n\n return false;\n }\n\n int dfs(int source, int sink){\n int flow = INT_MAX;\n int v = sink;\n while (v != source){\n flow = min(flow, prev[v]->residuo());\n v = prev[v]->from;\n }\n\n v = sink;\n Aresta *a;\n while (v != source){\n a = prev[v];\n a->flow += flow;\n grafo[a->to][a->rev]->flow -= flow;\n v = a->from;\n }\n return flow;\n }\n\n long long int flow(int source, int sink){\n int flow = 0;\n while (bfs(source, sink)){\n flow += dfs(source, sink);\n }\n return flow;\n }\n};\n\nint station[MAXN][MAXN], checkpoint[MAXN][MAXN], veneno[61][MAXN][MAXN];\n\nstruct coord{\n int x, y, qtd;\n coord(int a, int b, int c){\n x = a; y = b; qtd = c;\n }\n bool is_valid(int N){\n return (x >= 0 && x < N && y >= 0 && y < N && station[x][y] == ROOM);\n }\n};\n\nbool is_valid(int x, int y, int N){\n return coord(x, y, 0).is_valid(N);\n}\n\ninline bool operator == (const coord &a, const coord &b){\n return (a.x == b.x && a.y == b.y);\n}\n\nvector pessoas, capsulas;\nint moveX[] = {-1, 0, 1, 0}; //^\nint moveY[] = {0, -1, 0, 1}; //^\n\nint trad(char ch){\n switch (ch) {\n case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':\n return ROOM;\n case 'Y':\n return REAC;\n case 'Z':\n return BAD_REAC;\n }\n return -1;\n}\n\nint how_much(char ch){\n return ch-'0';\n}\n\n\nint bfs(coord from, coord to, int N){\n bitset vis[MAXN];\n queue> fila;\n fila.push({0, from});\n vis[from.x][from.y] = 1;\n\n while (!fila.empty()){\n int tempo = fila.front().first;\n coord atual = fila.front().second;\n fila.pop();\n\n if (atual == to) return tempo;\n\n if (veneno[tempo][atual.x][atual.y]){\n continue;\n }\n\n for (int i=0; i<4; i++){\n coord next(atual.x + moveX[i], atual.y + moveY[i], 0);\n if (next.is_valid(N) && !vis[next.x][next.y]){\n if (!veneno[tempo][next.x][next.y]){\n fila.push({tempo + 1, next});\n vis[next.x][next.y] = 1;\n }\n }\n }\n }\n\n return 999;\n}\n\nvoid espalha_veneno(int x, int y, int T, int N){\n veneno[0][x][y] = 1;\n for (int i=1; i<=T; i++){\n for (int lin=0; linadd(0, i*2+1, pessoas[i].qtd);\n }\n\n for (size_t i=0; iadd(i*2+2, sink, capsulas[i].qtd);\n }\n\n espalha_veneno(bad.x, bad.y, T, N);\n\n for (size_t i=0; iadd(i*2+1, j*2+2, 999);\n }\n }\n }\n\n printf(\"%lld\\n\", din->flow(0, sink));\n delete din;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "4d17a889a66b6495de81966eb4be6341", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n \nmt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());\n \n#define int long long\n#define ll long long\n#define mp make_pair\n#define f first\n#define s second\n#define pii pair\n#define all(_v) _v.begin(), _v.end()\n#define sz(a) ((int)a.size())\n#define what_is(x) cout << #x << \" is \" << x << endl;\ntypedef long double ld;\n \nconst ll inf = 1e17 + 7;\nconst int max_n = 1001;\n\nint xdir[]{0, 0, -1, 1}, ydir[]{-1, 1, 0, 0};\n\nvector gr[max_n];\nbool used[max_n];\nint pa[max_n];\n\n\n\nbool check(int x, int y, vector& s) {\n return x >= 0 && y >= 0 && x < s.size() && y < s.size() && s[x][y] != 'Y' && s[x][y] != 'Z';\n}\n\nvector> bfs(int st_x, int st_y, vector s) {\n vector> res(s.size(), vector(s.size(), inf));\n res[st_x][st_y] = 0;\n deque b;\n b.emplace_back(st_x, st_y);\n\n while(b.size()) {\n pii v = b.front();\n b.pop_front();\n\n for(int i = 0;i < 4;i++) {\n int x = v.f + xdir[i];\n int y = v.s + ydir[i];\n if (!check(x, y, s) || res[x][y] != inf) continue;\n\n res[x][y] = res[v.f][v.s] + 1;\n b.emplace_back(x, y);\n }\n }\n\n return res;\n}\n\n\nvoid scan() {\n int n, t;\n cin >> n >> t;\n vector s1(n), s2(n);\n for(int i = 0;i < n;i++)\n cin >> s1[i];\n\n for(int i = 0;i < n;i++)\n cin >> s2[i];\n\n int x_reac, y_reac;\n for(int i = 0;i < n;i++) {\n for(int j = 0;j < n;j++) {\n if (s1[i][j] == 'Z') {\n x_reac = i;\n y_reac = j;\n }\n }\n }\n\n vector> reac = bfs(x_reac, y_reac, s1);\n\n\n for(int i = 0;i < n;i++) {\n for(int j = 0;j < n;j++) {\n if (!check(i, j, s1)) continue;\n vector> a = bfs(i, j, s1);\n\n for(int x = 0;x < n;x++) {\n for(int y = 0;y < n;y++) {\n if (!check(x, y, s1) || a[x][y] > t || a[x][y] > reac[x][y]) continue;\n\n if (a[x][y] == reac[x][y]) {\n bool good = false;\n for(int id = 0;id < 4;id++) {\n int xto = x + xdir[id];\n int yto = y + ydir[id];\n if (!check(xto, yto, s1) || a[xto][yto] >= reac[xto][yto]) continue;\n good = true;\n }\n if (!good) continue;\n }\n\n\n for(int num1 = 1;num1 <= s1[i][j] - '0';num1++) {\n for(int num2 = 1;num2 <= s2[x][y] - '0';num2++) {\n gr[10 * (j + i * 10) + num1].emplace_back(10 * (y + 10 * x) + num2);\n }\n }\n }\n }\n }\n }\n}\n\nbool kuhn(int v) {\n if (used[v]) return false;\n used[v] = true;\n for(int to : gr[v]) {\n if (pa[to] == 0 || kuhn(pa[to])) {\n pa[to] = v;\n return true;\n }\n }\n return false;\n}\n\n\nvoid solve() {\n scan();\n\n\n int n = max_n;\n\n for(int i = 0;i < n;i++) {\n kuhn(i);\n for(int j = 0;j < n;j++)\n used[j] = false;\n }\n\n int res = 0;\n for(int i = 0;i < n;i++) res += pa[i] != 0;\n\n cout << res;\n}\n\n\n\nsigned main() {\n cin.tie(0);\n cout.tie(0);\n ios_base::sync_with_stdio(0);\n cout.precision(10);\n\n solve();\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "38f58418e50b314855e02eae92bcc077", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//In the name of God\n\n#include \n\nusing namespace std;\n\n\ntypedef long long ll;\ntypedef pair pii;\n\ntemplate inline P smin(P &a, Q b) { if (b < a) a = b; return a; }\ntemplate inline P smax(P &a, Q b) { if (a < b) a = b; return a; }\n\n#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)\n\nconst int N = 205, M = N * N;\n#define prev some\n\nint read() { int x; cin >> x; return x; }\n\nint prev[M], mark[N], head[N], from[M], cap[M], to[M], ec, a[N][N], b[N][N], red[N][N], block[N][N], dis[N][N], cnt, dred[N], seen[N];\npair q[N];\nint dx[4] = {0, -1, 0, 1};\nint dy[4] = {1, 0, -1, 0};\n\nvoid add_edge(int u, int v, int uv, int vu) {\n\tfrom[ec] = u, to[ec] = v, cap[ec] = uv, prev[ec] = head[u], head[u] = ec++;\n\tfrom[ec] = v, to[ec] = u, cap[ec] = vu, prev[ec] = head[v], head[v] = ec++;\n}\n\nint dfs(int v, int sink, int flow = 2e9) {\n\tif (v == sink)\n\t\treturn flow;\n\tif (mark[v]++)\n\t\treturn 0;\n\tfor (int e = head[v]; ~e; e = prev[e]) if (cap[e]) {\n\t\tint u = to[e];\n\t\tint x = dfs(u, sink, min(flow, cap[e]));\n\t\tif (x) {\n\t\t\tcap[e] -= x;\n\t\t\tcap[e ^ 1] += x;\n\t\t\treturn x;\n\t\t}\n\t}\n\treturn 0;\n}\n\nint max_flow(int source, int sink) {\n\tint res = 0;\n\twhile (true) {\n\t\tmemset(mark, 0, sizeof mark);\n\t\tint add = dfs(source, sink);\n\t\tres += add;\n\t\tif (!add)\n\t\t\tbreak;\n\t}\n\treturn res;\n}\n\nint32_t main() {\n\tios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\tint n = read(), dt = read();\n\tmemset(head, -1, sizeof head);\n\tmemset(dis, 63, sizeof dis);\n\trep(i, N)\n\t\tdis[i][i] = 0;\n\trep(x, n)\n\t\trep(y, n) {\n\t\t\tchar foo;\n\t\t\tcin >> foo;\n\t\t\tif (foo == 'Y')\n\t\t\t\tblock[x][y] = 1;\n\t\t\telse if (foo == 'Z')\n\t\t\t\tred[x][y] = 1;\n\t\t\telse\n\t\t\t\ta[x][y] = foo - '0';\n\t\t}\n\trep(x, n)\n\t\trep(y, n) {\n\t\t\tchar foo;\n\t\t\tcin >> foo;\n\t\t\tif (foo != 'Y' && foo != 'Z') {\n\t\t\t\tb[x][y] = foo - '0';\n\t\t\t\trep(d, 4) {\n\t\t\t\t\tint i = x + dx[d], j = y + dy[d];\n\t\t\t\t\tif (i >= 0 && j >= 0 && i < n && j < n && !block[i][j] && !red[i][j]) {\n\t\t\t\t\t\tdis[x * n + y][i * n + j] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tint m = n * n;\n\trep(k, m)\n\t\trep(i, m)\n\t\t\trep(j, m)\n\t\t\t\tif (dis[i][k] + dis[k][j] < dis[i][j])\n\t\t\t\t\tdis[i][j] = dis[i][k] + dis[k][j];\n\tint h = 0, t = 0;\n\tmemset(dred, 63, sizeof dred);\n\trep(i, n)\n\t\trep(j, n)\n\t\t\tif (red[i][j]) {\n\t\t\t\tq[t++] = {i, j};\n\t\t\t\tdred[i * n + j] = 0;\n\t\t\t}\n\twhile (h != t) {\n\t\tauto cur = q[h++];\n\t\tint x = cur.first, y = cur.second;\n\t\trep(d, 4) {\n\t\t\tint i = x + dx[d], j = y + dy[d];\n\t\t\tif (i >= 0 && j >= 0 && i < n && j < n && !block[i][j] && !red[i][j] && dred[i * n + j] > 1e9) {\n\t\t\t\tdred[i * n + j] = dred[x * n + y] + 1;\n\t\t\t\tq[t++] = {i, j};\n\t\t\t}\n\t\t}\n\t}\n\tint sink = N - 1, source = N - 2;\n\trep(cur, m) {\n\t\tmemset(seen, 0, sizeof seen);\n\t\th = 0;\n\t\tt = 0;\n\t\tint sx = cur / n, sy = cur % n;\n\t\tif (block[sx][sy] || red[sx][sy])\n\t\t\tcontinue;\n\t\tq[t++] = {sx, sy};\n\t\tseen[cur] = 1;\n\t\twhile (h != t) {\n\t\t\tint x = q[h].first, y = q[h++].second;\n\t\t\tif (dis[cur][x * n + y] <= dt)\n\t\t\t\tadd_edge(cur << 1, (x * n + y) << 1 | 1, 1000, 0);\n\t\t\tif (dis[cur][x * n + y] == dred[x * n + y])\n\t\t\t\tcontinue;\n\t\t\trep(d, 4) {\n\t\t\t\tint i = x + dx[d], j = y + dy[d], id = i * n + j;\n\t\t\t\tif (i >= 0 && j >= 0 && i < n && j < n && !block[i][j] && !red[i][j] && dis[cur][id] <= dred[id] && dis[cur][id] <= dt && dis[cur][id] > dis[cur][x * n + y] && !seen[id]) {\n\t\t\t\t\tq[t++] = {i, j};\n\t\t\t\t\tseen[id] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\trep(i, m)\n\t\tadd_edge(source, i << 1, a[i / n][i % n], 0);\n\trep(i, m)\n\t\tadd_edge(i << 1 | 1, sink, b[i / n][i % n], 0);\n\tcout << max_flow(source, sink);\n}\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "038128efb8c6160b3e0fc8e3d5b35527", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\n#define pii pair\n#define MP make_pair\n#define LL long long\n#define ls (i << 1)\n#define rs (ls | 1)\n#define md (ll + rr >> 1)\n#define lson ll, md, ls\n#define rson md + 1, rr, rs\n#define eps 1e-8\n#define inf 0x3f3f3f3f\n#define mod 1000000007\n#define N 410\n#define M 200020\n#define fi first\n#define se second\n\nchar buf[32000000],*pt = buf,*o = buf;\nint getint(){\n int f = 1,x = 0;\n while((*pt != '-') && (*pt < '0' || *pt > '9')) pt ++;\n if(*pt == '-') f = -1,pt ++; else x = *pt++ - 48;\n while(*pt >= '0' && *pt <= '9') x = x * 10 + *pt ++ - 48;\n return x * f;\n}\nchar getch(){\n char ch;\n while(*pt < 'A' || *pt > 'Z') pt ++;\n ch=*pt;pt++;\n return ch;\n}\n\nstruct dinic{\n\tint fst[N], vv[M], nxt[M], cap[M], flow[M], e;\n\tint d[N], cur[N];\n\tint s, t;\n\tvoid init(){\n\t\tmemset(fst, -1, sizeof fst); e = 0;\n\t}\n\tvoid add(int u, int v, int c){\n\t\tvv[e] = v, nxt[e] = fst[u], cap[e] = c, flow[e] = 0, fst[u] = e++;\n\t\tvv[e] = u, nxt[e] = fst[v], cap[e] = 0, flow[e] = 0, fst[v] = e++;\n\t}\n\tbool bfs(){\n\t \tmemset(d, -1, sizeof d);\n \t \td[s] = 0;\n\t\tqueue q;\n\t\tq.push(s);\n\t\twhile(!q.empty()){\n\t\t\tint u = q.front(); q.pop();\n\t\t\tfor(int i = fst[u]; ~i; i = nxt[i]){\n\t\t\t\tint v = vv[i];\n\t\t\t\tif(d[v] == -1 && cap[i] > flow[i]){\n\t\t\t\t\td[v] = d[u] + 1;\n\t\t\t\t\tq.push(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn d[t] != -1;\n\t}\n\tint dfs(int u, int a){\n\t\tif(u == t || a == 0) return a;\n\t\tint ret = 0, f;\n\t\tfor(int &i = cur[u]; ~i; i = nxt[i]){\n\t\t\tint v = vv[i];\n\t\t\tif(d[v] == d[u] + 1 && (f = dfs(v, min(a, cap[i] - flow[i]))) > 0){\n\t\t\t\ta -= f;\n\t\t\t\tret += f;\n\t\t\t\tflow[i] += f;\n\t\t\t\tflow[i^1] -=f;\n\t\t\t\tif(a == 0) break;\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tint gao(int _s, int _t){\n\t\ts = _s, t = _t;\n\t\tint ret = 0;\n\t\twhile(bfs()){\n\t\t\tmemcpy(cur, fst, sizeof fst);\n\t\t\tret += dfs(s, inf);\n\t\t}\n\t\treturn ret;\n\t}\n}go;\nint d[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};\nint dis[12][12], tim[12][12];\nchar g[N][N], h[N][N];\nint n, t;\nbool in(int x, int y){\n\treturn x >= 0 && x < n && y >= 0 && y < n;\n}\nvoid bfs1(int sx, int sy){\n\tmemset(tim, 0x3f, sizeof tim);\n\tqueue q;\n\tq.push(sx * 10 + sy);\n\ttim[sx][sy] = 0;\n\twhile(!q.empty()){\n\t\tint x = q.front() / 10, y = q.front() % 10;\n\t \tq.pop();\n\t\tfor(int i = 0; i < 4; ++i){\n\t\t\tint nx = x + d[i][0], ny = y + d[i][1];\n\t\t\tif(in(nx, ny) && g[nx][ny] >= '0' && g[nx][ny] <= '9' && tim[nx][ny] > tim[x][y] + 1){\n\t\t\t\ttim[nx][ny]\t= tim[x][y] + 1;\n\t\t\t\tq.push(nx * 10 + ny);\n\t\t\t}\n\t\t}\n\t}\n}\nvoid bfs2(int sx, int sy){\n\tmemset(dis, 0x3f, sizeof dis);\n\tqueue q;\n\tq.push(sx * 10 + sy);\n\tdis[sx][sy] = 0;\n\twhile(!q.empty()){\n\t\tint x = q.front() / 10, y = q.front() % 10;\n\t\tq.pop();\n\t\tfor(int i = 0; i < 4; ++i){\n\t\t\tint nx = x + d[i][0], ny = y + d[i][1];\n\t\t\tif(in(nx, ny) && h[nx][ny] >= '0' && h[nx][ny] <= '9' && dis[nx][ny] > dis[x][y] + 1){\n\t\t\t\tif(h[nx][ny] >= '0' && tim[nx][ny] >= dis[x][y] + 1){\n\t\t\t\t\tdis[nx][ny] = dis[x][y] + 1;\n\t\t\t\t\tif(tim[nx][ny] > dis[nx][ny])\n\t\t\t\t\t\tq.push(nx * 10 + ny);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nint main(){\n\tscanf(\"%d%d\", &n, &t);\n\tfor(int i = 0; i < n; ++i)\n\t\tscanf(\"%s\", g[i]);\n\tfor(int i = 0; i < n; ++i)\n\t\tscanf(\"%s\", h[i]);\n\tfor(int i = 0; i < n; ++i)\n\t\tfor(int j = 0; j < n; ++j)\n\t\t\tif(g[i][j] == 'Z')\n\t\t\t\tbfs1(i, j);\n\tint S = 0, T = n * n * 2 + 1;\n\tgo.init();\n\tfor(int i = 0; i < n; ++i)\n\t\tfor(int j = 0; j < n; ++j){\n\t\t\tif(g[i][j] > '0' && g[i][j] <= '9')\n\t\t\t\tgo.add(S, i * n + j + 1, g[i][j] - '0');\n\t\t\tif(h[i][j] > '0' && h[i][j] <= '9')\n\t\t\t\tgo.add(n * n + i * n + j + 1, T, h[i][j] - '0');\n\t\t}\n\tfor(int i = 0; i < n; ++i)\n\t\tfor(int j = 0; j < n; ++j)\n\t\t\tif(g[i][j] > '0' && g[i][j] <= '9'){\n\t\t\t\tbfs2(i, j);\n\t\t\t\tfor(int x = 0; x < n; ++x)\n\t\t\t\t\tfor(int y = 0; y < n; ++y){\n\t\t\t\t\t\tif(h[x][y] > '0' && h[x][y] <= '9' && dis[x][y] <= t){\n\t\t\t\t\t\t\tgo.add(i * n + j + 1, n * n + x * n + y + 1, inf);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t}\n\tint ret = go.gao(S, T);\n\tprintf(\"%d\\n\", ret);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "248a8d25d407a4dc1bcf2aef087840c3", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\nvector e[2020];\nint ir[20][20];\nint sc[20][20];\nint pc[20][20];\nchar s[20][20];\nchar p[20][20];\nint dd[20][20];\nint dx[5]={0,1,-1,0};\nint dy[5]={1,0,0,-1};\nvector ns[20][20],np[20][20];\nvoid ade(int a,int b)\n{\n e[a].push_back(b);\n e[b].push_back(a);\n}\nint ds[2020];\nint pa[2020];\nint sp,pp;\nbool bfs()\n{\n deque q;\n for(int i=1;i<=sp;i++)\n {\n if(pa[i]==0)\n ds[i]=0,q.push_back(i);\n else\n ds[i]=3010;\n }\n ds[0]=3010;\n while(!q.empty())\n {\n int u=q[0];\n q.pop_front();\n if(ds[u]ds[u]+1)\n ds[v]=ds[u]+1,q.push_back(v);\n }\n }\n }\n return ds[0]!=3010;\n}\nbool dfs(int x)\n{\n if(x==0)\n return 1;\n for(int i=0;i > q;\n dd[a][b]=0;\n q.push_back(make_pair(a,b));\n while(!q.empty())\n {\n int x=q[0].first,y=q[0].second;\n q.pop_front();\n for(int i=0;idd[x][y]+1&&dd[x][y]+1<=ir[nx][ny])\n {\n dd[nx][ny]=dd[x][y]+1;\n q.push_back(make_pair(nx,ny));\n }\n }\n }\n}\nint main()\n{\n scanf(\"%d%d\",&n,&t);\n for(int i=1;i<=n;i++)\n for(int j=1;j<=n;j++)\n ir[i][j]=t;\n for(int i=1;i<=n;i++)\n scanf(\"%s\",s[i]+1);\n for(int i=1;i<=n;i++)\n scanf(\"%s\",p[i]+1);\n int rx,ry;\n sp=1,pp=1000;\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(s[i][j]=='Z'||s[i][j]=='Y')\n sc[i][j]=-1,ir[i][j]=0;\n if(s[i][j]=='Z')\n rx=i,ry=j,ir[i][j]=0;\n if('0'<=s[i][j]&&s[i][j]<='9')\n sc[i][j]=s[i][j]-'0';\n for(int k=0;k > q;\n q.push_back(make_pair(rx,ry));\n while(!q.empty())\n {\n rx=q[0].first,ry=q[0].second;\n q.pop_front();\n int nx,ny;\n for(int d=0;d<4;d++)\n {\n nx=rx+dx[d];\n ny=ry+dy[d];\n if(ir[nx][ny]>ir[rx][ry]+1&&sc[nx][ny]!=-1)\n {\n ir[nx][ny]=ir[rx][ry]+1;\n q.push_back(make_pair(nx,ny));\n }\n }\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(sc[i][j]>0)\n {\n wk(i,j);\n }\n }\n }\n printf(\"%d\",hkp());\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "bcd47c3210ecdb1ac55145e79d86d4d0", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define INF 10000000\n#define N 210\n#define M 21000\nstruct Edge{\n int v,w;\n}edge[M];\nint adj[M],head[N],e;\nvoid addedge(int u,int v,int w){\n edge[e].v=v;edge[e].w=w;adj[e]=head[u];head[u]=e++;\n edge[e].v=u;edge[e].w=0;adj[e]=head[v];head[v]=e++;\n}\nint S,T;\n \nint dist[N],q[N*2],arc[N],gap[N],inq[N],pre[N];\nint SAP(int s,int t){\n memset(arc,-1,sizeof(arc));\n memset(pre,-1,sizeof(pre));\n int qf=0,qr=0;\n q[qr++]=t;inq[t]=1;gap[0]++;dist[t]=0;\n while(qfdist[edge[i].v]){\n mindis=dist[edge[i].v];\n arc[u]=i;\n }\n gap[dist[u]]--;\n if(gap[dist[u]]==0)\n return ans;\n dist[u]=mindis+1;\n gap[dist[u]]++;\n u=pre[u];\n }\n return ans;\n}\n \nchar str[12][12],str2[12][12];\nint sci[12][12],ca[12][12],stone[62][12][12];\nint dp[62][12][12][12][12];\nint dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};\nint n,t;\n \nvoid expand(){\n for(int k=1;k<=t;++k)\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j){\n if(stone[k-1][i][j]==1)\n stone[k][i][j]=1;\n else if(stone[k-1][i][j]==2)\n stone[k][i][j]=2;\n else{\n int nx,ny;\n for(int z=0;z<4;++z){\n nx=i+dir[z][0];\n ny=j+dir[z][1];\n if(nx>=1&&nx<=n&&ny>=1&&ny<=n){\n if(stone[k-1][nx][ny]==2)\n stone[k][i][j]=2;\n }\n }\n }\n }\n}\nint vis[12][12];\nvoid dfs(int x,int y,int nowx,int nowy,int k){\n vis[nowx][nowy]=1;\n dp[k][x][y][nowx][nowy]=1;\n if(k>=t || stone[k][nowx][nowy])\n return;\n int nx,ny;\n for(int z=0;z<4;++z){\n nx=nowx+dir[z][0];\n ny=nowy+dir[z][1];\n if(nx>=1&&nx<=n&&ny>=1&&ny<=n && !vis[nx][ny]){\n if(stone[k][nx][ny])\n continue;\n dfs(x,y,nx,ny,k+1);\n }\n }\n}\nint main (){\n scanf(\"%d%d\",&n,&t);\n for(int i=1;i<=n;++i){\n scanf(\"%s\",str[i]+1);\n for(int j=1;j<=n;++j){\n if(str[i][j]=='Z')\n stone[0][i][j]=2;\n else if(str[i][j]=='Y')\n stone[0][i][j]=1;\n else sci[i][j]=str[i][j]-'0';\n }\n }\n for(int i=1;i<=n;++i){\n scanf(\"%s\",str2[i]+1);\n for(int j=1;j<=n;++j){\n if(str2[i][j]>='0' && str2[i][j]<='9')\n ca[i][j]=str2[i][j]-'0';\n }\n }\n \n expand();\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n if(sci[i][j]){\n memset(vis,0,sizeof(vis));\n dfs(i,j,i,j,0);\n }\n for(int k=1;k<=t;++k)\n for(int x=1;x<=n;++x)\n for(int y=1;y<=n;++y)\n for(int nx=1;nx<=n;++nx)\n for(int ny=1;ny<=n;++ny)\n {\n dp[k][x][y][nx][ny]+=dp[k-1][x][y][nx][ny];\n }\n S=0;T=n*n*2+1;\n memset(head,-1,sizeof(head));\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n addedge(S,(i-1)*n+j,sci[i][j]);\n for(int i=1;i<=n;++i)\n for(int j=1;j<=n;++j)\n addedge(n*n+(i-1)*n+j,T,ca[i][j]);\n for(int x=1;x<=n;++x)\n for(int y=1;y<=n;++y)\n for(int nx=1;nx<=n;++nx)\n for(int ny=1;ny<=n;++ny)\n if(sci[x][y] && ca[nx][ny]){\n if(dp[t][x][y][nx][ny])\n addedge((x-1)*n+y,n*n+(nx-1)*n+ny,INF);\n }\n int ans=SAP(S,T);\n printf(\"%d\\n\",ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "2d724879925b44b7eea997c97191c488", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "///****In the name of Allah, most Gracious, most Compassionate****//\n\n#include \nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\n\n#define ALL(a) a.begin(), a.end()\n#define FastIO ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr)\n#define IN freopen(\"input.txt\",\"r+\",stdin)\n#define OUT freopen(\"output.txt\",\"w+\",stdout)\n\n\n#define TEST\n\n#ifdef TEST\n#define DBG(a) cout<< #a <<\" --> \"<<(a)<\nostream &operator <<(ostream &os,const pair < T1,T2 > &p)\n{\n os<<\"{\"< 0.\n\n\tThis implimentaion is superior over dinic's for graphs where graph is dense\n\tlocally at some places and mostly sparse. For randomly generated graphs, this\n\timplimentation gives results within seconds for n = 10000 nodes, m = 1000000\n\tedges.\n\n\tCode Tested on : SPOJ FASTFLOW\n\t@author : triveni\n*/\n\ntypedef int fType;\n\nstruct edge\n{\n int from, to;\n fType cap, flow;\n edge(int from, int to, fType cap, fType flow = 0) : from(from), to(to), cap(cap), flow(flow) {}\n};\n\nstruct PushRelabel\n{\n int N;\n vector edges;\n vector > G;\n vector h, inQ, count;\n vector excess;\n queue Q;\n PushRelabel(int N) : N(N), count(N<<1), G(N), h(N), inQ(N), excess(N) {}\n\n int addEdge(int from, int to, int cap)\n {\n G[from].push_back(edges.size());\n edges.push_back(edge(from, to, cap));\n G[to].push_back(edges.size());\n edges.push_back(edge(to, from, 0));\n return G[from].back();\n }\n\n void enQueue(int u)\n {\n if(!inQ[u] && excess[u] > 0) Q.push(u), inQ[u] = true;\n }\n\n void Push(int edgeIdx)\n {\n edge & e = edges[edgeIdx];\n int toPush = min(e.cap - e.flow, excess[e.from]);\n if(toPush > 0 && h[e.from] > h[e.to])\n {\n e.flow += toPush;\n excess[e.to] += toPush;\n excess[e.from] -= toPush;\n edges[edgeIdx^1].flow -= toPush;\n enQueue(e.to);\n }\n }\n\n void Relabel(int u)\n {\n count[h[u]] -= 1;\n h[u] = 2*N-2;\n for (int i = 0; i < G[u].size(); ++i)\n {\n edge & e = edges[G[u][i]];\n if(e.cap > e.flow) h[u] = min(h[u], h[e.to]);\n }\n count[++h[u]] += 1;\n }\n\n void gapRelabel(int height)\n {\n for (int u = 0; u < N; ++u) if(h[u] >= height && h[u] < N)\n {\n count[h[u]] -= 1;\n count[h[u] = N] += 1;\n enQueue(u);\n }\n }\n\n void Discharge(int u)\n {\n for (int i = 0; excess[u] > 0 && i < G[u].size(); ++i)\n {\n Push(G[u][i]);\n }\n if(excess[u] > 0)\n {\n if(h[u] < N && count[h[u]] < 2) gapRelabel(h[u]);\n else Relabel(u);\n }\n else if(!Q.empty()) // dequeue\n {\n Q.pop();\n inQ[u] = false;\n }\n }\n\n fType getFlow(int src, int snk)\n {\n h[src] = N;\n inQ[src] = inQ[snk] = true;\n count[0] = N - (count[N] = 1);\n for (int i = 0; i < G[src].size(); ++i)\n {\n excess[src] += edges[G[src][i]].cap;\n Push(G[src][i]);\n }\n while (!Q.empty())\n {\n Discharge(Q.front());\n }\n return excess[snk];\n }\n};\n\n\n\n\n\n\n\nconst int N=20+5;\nconst int oo=1e4+7;\nint dis[N][N];\nstring s[N];\nstring t[N];\nint n,k;\nint dx[]= {1,0,-1,0};\nint dy[]= {0,1,0,-1};\n\nvector source,sink;\n\nint sinkid[N][N];\n\n\nvoid bfs(int x,int y)\n{\n memset(dis, -1, sizeof dis);\n queue q;\n dis[x][y]= 0;\n q.push({x,y});\n\n while(!q.empty())\n {\n x= q.front().first;\n y= q.front().second;\n\n q.pop();\n\n for(int j=0; j<4; j++)\n {\n int xx =x+dx[j];\n int yy =y+dy[j];\n\n if(xx<0 or xx>= n)\n continue;\n if(yy<0 or yy>= n)\n continue;\n\n if(dis[xx][yy] !=-1 )\n continue;\n\n if(t[xx][yy]=='Y' or t[xx][yy]=='Z')\n continue;\n dis[xx][yy]= dis[x][y]+1;\n q.push({xx,yy});\n }\n }\n}\n\nint dp[65][12][12][12][12];\n\nint pos(int t,int x0,int y0, int x,int y)\n{\n if(dis[x0][y0]< t) return 0;\n if(x0==x and y0==y) return 1;\n\n if(t>=k) return 0;\n\n if(dis[x0][y0]<=t) return 0;\n\n if(dp[t][x0][y0][x][y]!=-1) return dp[t][x0][y0][x][y];\n\n for(int j=0; j<4; j++)\n {\n int xx = x0+dx[j];\n int yy = y0+dy[j];\n\n if(xx<0 or xx>= n)\n continue;\n if(yy<0 or yy>= n)\n continue;\n if(s[xx][yy]=='Y') continue;\n if(s[xx][yy]=='Z') continue;\n\n if(pos(t+1 , xx ,yy , x , y)) return dp[t][x0][y0][x][y]=1;\n }\n return dp[t][x0][y0][x][y]=0;\n}\n\n\nint32_t main()\n{\n //FastIO;\n\n memset(dp , -1 , sizeof dp);\n cin>>n>>k;\n for(int i=0; i>s[i];\n for(int i=0; i>t[i];\n int x= -1, y = -1 ;\n for(int i=0; i\n\nusing namespace std;\n\n#define F first\n#define S second\n#define MP make_pair\n#define all(x) (x).begin(), (x).end()\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n\nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\nconst int MOD = 1000 * 1000 * 1000 + 7;\nconst int INF = 2000 * 1000 * 1000;\nconst int MAXN = 210;\nconst int MAXM = 2 * MAXN * MAXN;\nconst int dx[] = {1, 0, 0, -1};\nconst int dy[] = {0, 1, -1, 0};\nconst int S = MAXN - 2;\nconst int T = MAXN - 1;\n\ntemplate \ninline T sqr(T n) {\n return n * n;\n}\n\nstruct MaxFlow {\n int to[MAXM];\n int flow[MAXM], cap[MAXM];\n int lastEdge;\n\n vector g[MAXN];\n\n bool used[MAXN];\n\n MaxFlow() : lastEdge(0) { }\n\n void addEdge(int from, int to, int cap) {\n this->to[lastEdge] = to;\n this->flow[lastEdge] = 0;\n this->cap[lastEdge] = cap;\n g[from].push_back(lastEdge++);\n\n this->to[lastEdge] = from;\n this->flow[lastEdge] = 0;\n this->cap[lastEdge] = 0;\n g[to].push_back(lastEdge++);\n }\n\n bool dfs(int v, int T, int curFlow) {\n if (v == T) {\n return true;\n }\n\n used[v] = true;\n\n for (int idx: g[v]) {\n if (!used[to[idx]] && cap[idx] - flow[idx] >= curFlow &&\n dfs(to[idx], T, curFlow)) {\n \n flow[idx] += curFlow;\n flow[idx ^ 1] -= curFlow;\n\n return true;\n }\n }\n \n return false;\n }\n\n int getMaxFlow(int S, int T) {\n int curFlow = 1 << 30;\n int resFlow = 0;\n\n while (curFlow > 0) {\n if (dfs(S, T, curFlow)) {\n resFlow += curFlow;\n } else {\n curFlow >>= 1;\n }\n\n memset(used, false, sizeof used);\n }\n\n return resFlow;\n }\n};\n\nMaxFlow maxFlow;\n\nint n, t;\nchar a[15][15];\nchar b[15][15];\nint dist[15][15];\nbool used[15][15];\n\nbool ok(int x, int y) {\n return 0 <= x && x < n &&\n 0 <= y && y < n &&\n a[x][y] != 'Y' &&\n a[x][y] != 'Z';\n}\n\nvoid bfsDeath(int x, int y) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = INF;\n }\n }\n\n queue X, Y;\n dist[x][y] = 0;\n X.push(x);\n Y.push(y);\n\n while (!X.empty()) {\n assert(!Y.empty());\n\n int curx = X.front();\n int cury = Y.front();\n\n X.pop();\n Y.pop();\n\n for (int k = 0; k < 4; k++) {\n int tx = curx + dx[k];\n int ty = cury + dy[k];\n\n if (ok(tx, ty) && dist[tx][ty] == INF) {\n dist[tx][ty] = dist[curx][cury] + 1;\n X.push(tx);\n Y.push(ty);\n }\n }\n }\n}\n\nvoid runBFS(int x, int y) {\n queue X, Y, lev;\n memset(used, false, sizeof used);\n \n used[x][y] = true;\n X.push(x);\n Y.push(y);\n lev.push(0);\n\n while (!X.empty()) {\n assert(!Y.empty());\n\n int curx = X.front();\n int cury = Y.front();\n int curlev = lev.front();\n\n X.pop();\n Y.pop();\n lev.pop();\n\n if (isdigit(b[curx][cury]) && b[curx][cury] != '0') {\n maxFlow.addEdge(x * n + y, sqr(n) + 1 + curx * n + cury, INF);\n }\n \n if (curlev >= t) {\n continue;\n }\n\n for (int k = 0; k < 4; k++) {\n int tx = curx + dx[k];\n int ty = cury + dy[k];\n\n if (ok(tx, ty) && !used[tx][ty]) {\n if (dist[tx][ty] > curlev + 1) {\n X.push(tx);\n Y.push(ty);\n\n lev.push(curlev + 1);\n } else if (dist[tx][ty] > curlev && isdigit(b[tx][ty]) && b[tx][ty] != '0') {\n maxFlow.addEdge(x * n + y, sqr(n) + 1 + tx * n + ty, INF);\n// printf(\"(%d, %d) --> (%d, %d)\\n\", x, y, tx, ty);\n }\n\n used[tx][ty] = true;\n }\n }\n }\n}\n\nvoid build() {\n\n int posi = -1, posj = -1;\n for (int i = 0; i < n && posi == -1; i++) {\n for (int j = 0; j < n && posi == -1; j++) {\n if (a[i][j] == 'Z') {\n posi = i, posj = j;\n }\n }\n }\n\n bfsDeath(posi, posj);\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (isdigit(a[i][j]) && a[i][j] != '0') {\n maxFlow.addEdge(S, i * n + j, a[i][j] - '0');\n }\n }\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (isdigit(b[i][j]) && b[i][j] != '0') {\n maxFlow.addEdge(i * n + j + sqr(n) + 1, T, b[i][j] - '0');\n }\n }\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (isdigit(a[i][j]) && a[i][j] != '0') {\n runBFS(i, j);\n }\n }\n }\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen(\"in\", \"r\", stdin);\n#endif\n\n scanf(\"%d %d\", &n, &t);\n\n scanf(\"\\n\");\n for (int i = 0; i < n; i++) {\n gets(a[i]);\n }\n\n scanf(\"\\n\");\n for (int i = 0; i < n; i++) {\n gets(b[i]);\n }\n\n build();\n\n printf(\"%d\\n\", maxFlow.getMaxFlow(S, T));\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "4910cdfb6b0d350d1d14fcf3a25ede04", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ninline int in(){int32_t x ; scanf(\"%d\",&x) ; return x ; }\ninline string get(){char ch[1001000] ; scanf(\"%s\",ch) ; return ch;}\ntemplate inline P smin(P &a, Q b) { if (b < a) a = b; return a; }\ntemplate inline P smax(P &a, Q b) { if (a < b) a = b; return a; }\n#define mp make_pair\n#define EPS 1e-9\n//#define int long long\ntypedef pair pii;\ntypedef long double ld;\ntypedef unsigned long long ull;\ntypedef long long ll;\nconst int maxn = 1e5 + 10;\nconst int base = 29;\nconst int MAX_LG = 21;\nconst int mod = 1e9 + 7;\nconst int INF = 1e18;\nint n , t;\nstring a[30] , b[30];\nint dist1[30][30] , dist2[30][30];\nint dx[] = {0,0,-1,1};\nint dy[] = {1,-1,0,0};\nstruct edge\n{\n int a , b , cap , flow;\n};\nint di[maxn];\nint ptr[maxn];\nint q[maxn];\nvector e;\nvector g[maxn];\nint whereX , whereY;\nint source , sink;\ninline void add(int a , int b , int cap)\n{\n edge e1 = {a,b,cap,0};\n edge e2 = {b,a,0,0};\n g[a].push_back((int)e.size());\n e.push_back(e1);\n g[b].push_back((int)e.size());\n e.push_back(e2);\n}\ninline bool ok(int x , int y){return (x >= 0 && x < n && y >= 0 && y < n);}\ninline void BFS1(int x = whereX , int y = whereY)\n{\n queue q;\n q.push({x,y});\n for (int i = 0 ; i <= 20 ; i ++)\n for (int j = 0 ; j <= 20 ; j ++)\n dist1[i][j] = 1e9;\n dist1[x][y] = 0;\n while (!q.empty())\n {\n int x = q.front().first , y = q.front().second;\n q.pop();\n for (int i = 0 ; i < 4 ; i ++)\n {\n int x2 = x + dx[i] , y2 = y + dy[i];\n if (ok(x2,y2) && dist1[x2][y2] >= 1e9 && b[x2][y2] != 'Y')\n {\n dist1[x2][y2] = dist1[x][y] + 1;\n q.push({x2,y2});\n }\n }\n }\n \n}\ninline bool bfs()\n{\n memset(di,-1,n * sizeof di[0]);\n int qh = 0 , qt = 0;\n q[qt ++] = source;\n di[source] = 0;\n while (qh < qt && di[sink] == -1)\n {\n int v = q[qh ++];\n for (int pt = 0 ; pt < g[v].size() ; pt ++)\n {\n int id = g[v][pt] , to = e[id].b;\n if (di[to] == -1 && e[id].flow < e[id].cap)\n {\n q[qt ++] = to;\n di[to] = di[v] + 1;\n }\n }\n }\n return di[sink] != -1;\n}\ninline int dfs(int v , int flow)\n{\n if (!flow) return 0;\n if (v == sink) return flow;\n for ( ; ptr[v] < g[v].size() ; ptr[v] ++)\n {\n int id = g[v][ptr[v]] , to = e[id].b;\n if (di[to ] != di[v] + 1) continue;\n int pushed = dfs(to , min(flow , e[id].cap - e[id].flow));\n if (pushed)\n {\n e[id].flow += pushed;\n e[id^1].flow -= pushed;\n return pushed;\n }\n }\n return 0;\n}\ninline int max_flow()\n{\n int flow = 0;\n while (true)\n {\n if (!bfs()) break;\n memset(ptr,0,n * sizeof ptr[0]);\n while (int pushed = dfs(source , 1e9))\n {\n flow += pushed;\n }\n }\n return flow;\n}\ninline void BFS2(int x , int y)\n{\n queue q;\n q.push({x,y});\n for (int i = 0 ; i <= 20 ; i ++)\n for (int j = 0 ; j <= 20 ; j ++)\n dist2[i][j] = 1e9;\n dist2[x][y] = 0;\n while (!q.empty())\n {\n int x = q.front().first , y = q.front().second;\n q.pop();\n if (dist2[x][y] >= dist1[x][y]) continue;\n for (int i = 0 ; i < 4 ; i++)\n {\n int x2 = x + dx[i] , y2 = y + dy[i];\n if(ok(x2,y2) && dist2[x2][y2] >= 1e9 && a[x2][y2] != 'Y' && dist2[x][y] + 1 <= dist1[x2][y2])\n {\n dist2[x2][y2] = dist2[x][y] + 1;\n q.push({x2,y2});\n }\n }\n }\n}\nint32_t main()\n{\n n = in() , t = in();\n for (int i = 0 ; i < n ; i ++)\n a[i] = get();\n for (int i = 0 ; i < n ; i ++)\n {\n b[i] = get();\n for (int j = 0 ; j < n ; j ++)\n {\n if (b[i][j] == 'Z')\n {\n b[i][j] = a[i][j] = 'Y';\n whereX = i , whereY = j;\n }\n }\n }\n BFS1();\n source = 0 , sink = 2 * n * n + 1;\n for (int i = 0 ; i < n ; i ++)\n {\n for (int j = 0 ; j < n ; j ++)\n {\n if (a[i][j] == 'Y') continue;\n int what = i * n + j + 1;\n int what2 = n * n + i * n + j + 1;\n add(source,what,a[i][j]-'0');\n add(what2,sink,b[i][j]-'0');\n BFS2(i , j);\n for (int ptx = 0 ; ptx < n ; ptx ++)\n {\n for (int pty = 0 ; pty < n ; pty ++)\n {\n if (b[ptx][pty] == 'Y' || dist2[ptx][pty] > t) continue;\n add(what,n * n + ptx * n + pty + 1 , a[i][j] - '0');\n }\n }\n }\n }\n n = 2 * n * n + 5;\n \n// printf(\"%d\\n\",max_flow());\n cout << max_flow() << endl;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "452685caad7799212a2457dcf437bf33", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nstruct node\n{\n int r,c,dis;\n};\n\nconst int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};\nqueue q;\nchar s[15];\nint n,n1,m,lim,tot,p,a[11][11],b[11][11],d0[11][11],f[202][202],d[202][202],w[202],h[202],g[203];\nbool t,flag,ok[12][12],okb[12][12];\n\nvoid search(int x,int z)\n{\n if(x==n+1)\n {\n p=z;\n flag=true;\n return;\n }\n int temp=n+2;\n for(int y=0;y<=n+1;++y)\n {\n if(d[x][y]0)\n {\n temp=min(temp,h[y]);\n if(h[x]==h[y]+1)\n {\n w[++tot]=-y;\n search(y,min(z,d[y][x]));\n if((!t)||(flag))\n return;\n w[tot--]=0;\n }\n }\n }\n if(temp+1!=h[x])\n {\n --g[h[x]];\n if(g[h[x]]==0)\n t=false;\n h[x]=temp+1;\n ++g[h[x]];\n }\n}\n\nbool improvable()\n{\n flag=false;\n p=n1;\n tot=0;\n w[0]=0;\n search(0,n1);\n return flag;\n}\n\nvoid improve()\n{\n for(int x=1;x<=tot;++x)\n {\n if(w[x]>0)\n d[abs(w[x-1])][w[x]]+=p;\n else\n d[-w[x]][abs(w[x-1])]-=p;\n }\n}\n\nint main(void)\n{\n scanf(\"%d %d\",&m,&lim);\n for(int i=1;i<=m;++i)\n {\n scanf(\"%s\",s+1);\n for(int j=1;j<=m;++j)\n {\n if(s[j]=='Z')\n {\n node he;\n he.r=i;\n he.c=j;\n he.dis=0;\n q.push(he);\n }\n else if(s[j]!='Y')\n {\n ok[i][j]=true;\n d0[i][j]=lim;\n int k=s[j]-'0';\n if(k>0)\n {\n a[i][j]=++n;\n f[0][n]=k;\n n1+=k;\n }\n }\n }\n }\n memcpy(okb,ok,sizeof(ok));\n for(int i=1;i<=m;++i)\n {\n scanf(\"%s\",s+1);\n for(int j=1;j<=m;++j)\n {\n if((s[j]=='Z')||(s[j]=='Y'))\n continue;\n int k=s[j]-'0';\n if(k>0)\n {\n b[i][j]=++n;\n f[n][m*m+1]=k;\n }\n }\n }\n if(n!=m*m)\n {\n for(int i=1;i<=m;++i)\n {\n for(int j=1;j<=m;++j)\n {\n if(b[i][j]>0)\n {\n int k=b[i][j];\n f[k][n+1]=f[k][m*m+1];\n f[k][m*m+1]=0;\n }\n }\n }\n }\n while(!q.empty())\n {\n node he=q.front();\n q.pop();\n int i=he.r,j=he.c;\n d0[i][j]=he.dis;\n for(int k=0;k<4;++k)\n {\n if(ok[i+dx[k]][j+dy[k]])\n {\n node e;\n e.r=i+dx[k];\n e.c=j+dy[k];\n e.dis=he.dis+1;\n q.push(e);\n ok[i+dx[k]][j+dy[k]]=false;\n }\n }\n }\n for(int i=1;i<=m;++i)\n {\n for(int j=1;j<=m;++j)\n {\n if(a[i][j]==0)\n continue;\n memcpy(ok,okb,sizeof(okb));\n ok[i][j]=false;\n node he;\n he.r=i;\n he.c=j;\n he.dis=0;\n q.push(he);\n while(!q.empty())\n {\n node he=q.front();\n q.pop();\n int x=he.r,y=he.c;\n if((b[x][y]>0)&&(he.dis<=d0[x][y]))\n f[a[i][j]][b[x][y]]=n1;\n if((he.dis+1>lim)||(he.dis>=d0[x][y]))\n continue;\n for(int k=0;k<4;++k)\n {\n if(ok[x+dx[k]][y+dy[k]])\n {\n node e;\n e.r=x+dx[k];\n e.c=y+dy[k];\n e.dis=he.dis+1;\n q.push(e);\n ok[x+dx[k]][y+dy[k]]=false;\n }\n }\n }\n }\n }\n g[0]=n+2;\n t=true;\n while((t)&&(h[0]<=n+2))\n while(improvable())\n improve();\n int ans=0;\n for(int i=1;i<=n;++i)\n ans+=d[i][n+1];\n printf(\"%d\\n\",ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "7c33ecdd86bb4e826110c3797d9cf13c", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \ntypedef long long LL;\nusing namespace std;\n#define rep(i,n) for(int i = 0;i < n;i ++)\n\ntemplate \nstruct MaxFlow {\n\tint l[N], tot, cur[N], d[N], u, v, s, t, src, sink;\n\n\tstruct Edge {\n\t\tint v, x, cap;\n\t} E[111111];\n\n\tvoid add(int u, int v, int cap) {\n\t\tE[tot] = (Edge) {v, l[u], cap};\n\t\tl[u] = tot ++;\n\t\tE[tot] = (Edge) {u, l[v], 0};\n\t\tl[v] = tot ++;\n\t}\n\n\tvoid init() {\n\t\tmemset(l, -1, sizeof l);\n\t\ttot = 0;\n\t}\n\n\tbool bfs() {\n\t\tmemset(d, -1, sizeof d);\n\t\tmemcpy(cur, l, sizeof l);\n\t\td[src] = 0;\n\t\tqueue Q;\n\t\tQ.push(src);\n\t\twhile(!Q.empty()) {\n\t\t\tu = Q.front();\n\t\t\tQ.pop();\n\t\t\tfor(int i = l[u]; ~i; i = E[i].x) {\n\t\t\t\tv = E[i].v;\n\t\t\t\tif(d[v] == - 1 && E[i].cap) {\n\t\t\t\t\td[v] = d[u] + 1;\n\t\t\t\t\tQ.push(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn d[sink] != -1;\n\t}\n\n\tint dfs(int u, int in) {\n\t\tif(u == sink)\treturn in;\n\t\tint flow = 0, f;\n\t\tfor(int &i = cur[u]; ~i; i = E[i].x) {\n\t\t\tv = E[i].v;\n\t\t\tif(d[v] == d[u] + 1 && E[i].cap) {\n\t\t\t\tif(f=dfs(v, min(in-flow, E[i].cap))) {\n\t\t\t\t\tflow += f;\n\t\t\t\t\tE[i].cap -= f;\n\t\t\t\t\tE[i^1].cap += f;\n\t\t\t\t\tif(flow == in)\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(flow < in)\td[u] = -1;\n\t\treturn flow;\n\t}\n\n\tconst int inf = 1e9;\n\tint dinic(int _src, int _sink) {\n\t\tsrc = _src, sink = _sink;\n\t\tint flow = 0;\n\t\twhile(bfs())\n\t\t\tflow += dfs(src, inf);\n\t\treturn flow;\n\t}\n};\n\nint n;\n\nconst int N = 15;\nchar field[N][N], capsule[N][N];\nint Z[N][N], S[N][N];\nint dx[] = {-1, 1, 0, 0};\nint dy[] = {0, 0, -1, 1};\n\ninline bool In(int i, int j) {\n\treturn i>=0&&i=0&&j F;\n\nint t;\nconst int inf = 1e9;\ninline bool isdigit(char ch) {\n\treturn ch>='1' && ch <='9';\n}\nvoid bfs(int x, int y, int d[][N], bool f) {\n\tint sx = x, sy = y;\n\tfill(d[0], d[N], inf);\n\td[x][y] = 0;\n\tqueue Q;\n\tQ.push(x);\n\tQ.push(y);\n\tif(f && isdigit(capsule[x][y])) {\n\t\tF.add(sx*n+sy, sx*n+sy+n*n, inf);\n\t}\n\twhile(!Q.empty()) {\n\t\tx = Q.front();\n\t\tQ.pop();\n\t\ty = Q.front();\n\t\tQ.pop();\n\t\tif(d[x][y] == t)\tbreak;\n\t\trep(o, 4) {\n\t\t\tint xx = x + dx[o];\n\t\t\tint yy = y + dy[o];\n\t\t\tif(In(xx, yy) && d[xx][yy] == inf && capsule[xx][yy] != 'Y') {\n\t\t\t\tif(Z[xx][yy] > d[x][y] + 1 || Z[xx][yy] == d[x][y] + 1 && isdigit(capsule[xx][yy])) {\n\t\t\t\t\td[xx][yy] = d[x][y] + 1;\n\t\t\t\t\tif(f && isdigit(capsule[xx][yy])) {\n\t\t\t\t\t\tF.add(sx*n+sy, xx*n+yy+n*n, inf);\n\t\t\t\t\t}\n\t\t\t\t\tif(Z[xx][yy] > d[x][y] + 1 || !f) {\n\t\t\t\t\t\tQ.push(xx);\n\t\t\t\t\t\tQ.push(yy);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nint main() {\n\tint x, y;\n\tscanf(\"%d%d\", &n, &t);\n\tint src = n*n*2, sink = src + 1;\n\tF.init();\n\trep(i, n) {\n\t\tscanf(\"%s\", field[i]);\n\t\trep(j, n) {\n\t\t\tif(field[i][j] == 'Z') {\n\t\t\t\tx = i, y = j;\n\t\t\t}\n\t\t}\n\t}\n\trep(i, n) {\n\t\tscanf(\"%s\", capsule[i]);\n\t\trep(j, n) {\n\t\t\tif(isdigit(capsule[i][j])) {\n\t\t\t\tF.add(i*n+j+n*n, sink, capsule[i][j]-'0');\n\t\t\t}\n\t\t}\n\t}\n\tbfs(x, y, Z, false);\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tif(isdigit(field[i][j])) {\n\t\t\t\tF.add(src, i*n+j, field[i][j]-'0');\n\t\t\t\tbfs(i, j, S, true);\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", F.dinic(src, sink));\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "aa7cdb930906968333eed2c2eb387056", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\n// Graph stuff from TCR:\nstruct Edge { /* Directed edge */\n int from; /* Index of the source node */\n int to; /* Index of the destination node */\n double length; /* Length of the edge */\n Edge(int f, int t, double l): from(f), to(t), length(l) {}\n};\n\nstruct Node {\n vector out; /* Indices of edges that start in this node */\n};\n\nstruct Graph { /* Directed graph */\n vector nodes; /* List of nodes */\n vector edges; /* List of edges */\n\n Graph(int nr_nodes, int nr_edges)\n {\n nodes.resize(nr_nodes); /* Create nodes, without edges */\n edges.reserve(nr_edges); /* Reserve space for edges */\n }\n\n void add_edge(int from, int to, double length) \n {\n Edge e = Edge(from, to, length);\n edges.push_back(e); /* Add edge to the list */\n nodes[from].out.push_back(edges.size()-1); /* Add to the source node */\n }\n\n vector find_augmenting_path(int source, int sink,\n vector flow, double *path_cap)\n /* Used in the max_flow algorithm to find an augmenting path,\n * it returns the edges that are on the augmenting path, or an empty vector\n * if there is no path. The path_cap parameter will be set to the capacity\n * of the path.\n * This implementation uses priority based search to find an augmenting path\n * $O(|edges| \\log |nodes|)$\n */\n {\n /* Queue that does highest priority first: */\n set< pair, greater< pair > > q;\n vector capacity (nodes.size(), 0); /* Capacity to each node */\n vector pred(nodes.size(),-1); /* Data to find the path */\n vector path;\n\n capacity[source]=INFINITY; /* Infinite capacity for source */\n q.insert(make_pair(INFINITY, source)); /* Put source in the queue */\n\n while ((!q.empty()) && (capacity[sink] == 0)) {\n double cap = q.begin()->first;\n int n = q.begin()->second;\n q.erase(q.begin());\n\n for (unsigned int i = 0 ; i < nodes[n].out.size(); i++) {\n int e_idx = nodes[n].out[i];\n Edge &e = edges[e_idx];\n double e_cap = e.length - flow[e_idx];\n\n if ((e_cap > 0) && (min(cap,e_cap) > capacity[e.to])) {\n q.erase(make_pair(capacity[e.to], e.to));\n capacity[e.to] = min(cap,e_cap);\n q.insert(make_pair(capacity[e.to], e.to));\n pred[e.to] = e_idx;\n }\n }\n }\n\n /* Reconstruct the path */\n *path_cap = capacity[sink];\n {\n int e_idx = pred[sink];\n while (e_idx != -1)\n {\n path.push_back(e_idx);\n e_idx = pred[edges[e_idx].from];\n }\n }\n\n return path;\n }\n\n double max_flow(int source, int sink)\n /* Computes the maximum flow through the graph from source to sink using\n * Ford-Fulkerson, combined with a priority first search for the augmenting\n * path.\n * $O(|edges|^2 \\log |nodes|)$\n */\n {\n Graph residual(nodes.size(), 2*edges.size());\n vector flow(2*edges.size(), 0);\n vector path;\n double path_cap;\n double max_flow = 0;\n\n /* Construct residual graph */\n for (unsigned int i=0; i < edges.size(); i++)\n {\n residual.add_edge(edges[i].from, edges[i].to, edges[i].length);\n residual.add_edge(edges[i].to, edges[i].from, 0);\n }\n\n path=residual.find_augmenting_path(source, sink, flow, &path_cap);\n while (path_cap > 0)\n {\n for (unsigned int i=0; i < path.size(); i++)\n {\n int e_idx = path[i];\n int e_ridx = e_idx + 1;\n if ((e_idx % 2) == 1) e_ridx = e_idx - 1;\n flow[e_idx] = flow[e_idx] + path_cap;\n flow[e_ridx] = flow[e_ridx] - path_cap;\n }\n path=residual.find_augmenting_path(source, sink, flow, &path_cap);\n }\n\n for (unsigned int i = 0 ; i < residual.nodes[source].out.size(); i++)\n {\n int e_idx = residual.nodes[source].out[i];\n max_flow = max_flow + flow[e_idx];\n }\n\n // Note: to find a min-cut, divide the graph into all nodes\n // that are reachable from the source via non-saturated edges\n // and those that are not. The endges between those two sets\n // of nodes will have a total capacity of max_flow\n\n return max_flow;\n }\n\n void print_all();\n};\n\nstatic int n;\nstatic int t;\n\nstruct Room\n{\n int i,j;\n array infected;\n array accessible;\n int scientists;\n int capsules;\n\n Room(): scientists{0}, capsules{0}\n {\n fill(infected.begin(), infected.end(), false);\n fill(accessible.begin(), accessible.end(), true);\n }\n\n int get_node_id(int timestamp) const\n {\n return 2 + i + n*j + (n*n)*timestamp;\n }\n\n void set_reactor()\n {\n fill(accessible.begin(), accessible.end(), false);\n }\n\n void set_infected(int timestamp = 0)\n {\n if (accessible[timestamp])\n {\n accessible[timestamp] = false;\n infected[timestamp] = true;\n }\n }\n\n void spread_infection(int timestamp);\n\n};\n\nstatic array< array< Room, 10>, 10> floorplan;\n\nvoid Room::spread_infection(int timestamp)\n{\n if (infected[timestamp])\n {\n set_infected(timestamp+1);\n if (i > 0) floorplan[i-1][j].set_infected(timestamp+1);\n if (i < (n-1)) floorplan[i+1][j].set_infected(timestamp+1);\n if (j > 0) floorplan[i][j-1].set_infected(timestamp+1);\n if (j < (n-1)) floorplan[i][j+1].set_infected(timestamp+1);\n }\n}\n\nstring id_to_string(int id)\n{\n if (id == 0) return \"source\";\n if (id == 1) return \"sink\";\n stringstream ss;\n int row = (id - 2) % n;\n int col = ((id - 2 - row) % (n*n)) / n;\n int timestamp = (id - 2 - row - col*n) / (n*n);\n ss << \"(\" << row << \",\" << col << \",\" << timestamp << \")\";\n return ss.str(); \n}\n\nvoid Graph::print_all()\n{\n for (int i = 0; i < nodes.size() ; i++)\n {\n Node& node = nodes[i];\n cout << id_to_string(i) << \" has edges to : \";\n for (int e = 0; e < node.out.size(); ++e) {\n cout << id_to_string(edges[node.out[e]].to) << \" \";\n } \n cout << endl;\n }\n}\n\nint main (void)\n{\n ios::sync_with_stdio(false);\n\n cin >> n >> t;\n\n // Read scientist locations\n for (int i = 0; i < n; i++)\n {\n string line;\n cin >> line;\n for (int j = 0; j < n; j++)\n {\n floorplan[i][j].i = i;\n floorplan[i][j].j = j;\n if (line[j] == 'Y') floorplan[i][j].set_reactor();\n else if (line[j] == 'Z') floorplan[i][j].set_infected();\n else floorplan[i][j].scientists = line[j] - '0';\n }\n }\n\n // Read capsule locations\n for (int i = 0; i < n; i++)\n {\n string line;\n cin >> line;\n for (int j = 0; j < n; j++)\n {\n if ((line[j] != 'Y') && (line[j] != 'Z')) floorplan[i][j].capsules = line[j] - '0';\n }\n }\n\n // Figure out the spread of the infection\n for (int c = 0; c <= t; c++)\n {\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n if (c < t) floorplan[i][j].spread_infection(c);\n /*if (floorplan[i][j].infected[c]) cout << \"I\";\n else if (floorplan[i][j].accessible[c]) cout << \".\";\n else cout << \"X\";*/\n }\n //cout << \"\\n\";\n }\n //cout << \"\\n\\n\";\n }\n\n \n\n // Convert to graph\n Graph g(n*n*(t+1)+2, 100*4*60);\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n const Room &room = floorplan[i][j];\n if (room.scientists > 0) g.add_edge(0, room.get_node_id(0), room.scientists);\n if (room.capsules > 0) g.add_edge(room.get_node_id(t), 1, room.capsules);\n for (int c = 0; c < t; c++)\n {\n if (room.capsules > 0) g.add_edge(room.get_node_id(c), room.get_node_id(t), 1000);\n if (room.accessible[c])\n {\n if (i > 0)\n {\n const Room &other = floorplan[i-1][j];\n if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000); \n }\n if (i < (n-1))\n {\n const Room &other = floorplan[i+1][j];\n if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000); \n }\n if (j > 0)\n {\n const Room &other = floorplan[i][j-1];\n if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000); \n }\n if (j < (n-1))\n {\n const Room &other = floorplan[i][j+1];\n if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000); \n }\n }\n }\n }\n }\n\n //g.print_all();\n\n cout << int(g.max_flow(0,1)) << \"\\n\";\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs", "shortest paths"], "code_uid": "a8a33080051a5697135b68c26de3258c", "src_uid": "544de9c3729a35eb08c143b1cb9ee085", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define M 1000007\n#define N 210\n#define ll long long\n\nchar s1[15][15], s2[15][15];\nint id[15][15], cnt;\nint s, t, T, n;\nint xz, yz;\nint dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };\nbool inside( int x, int y )\n{\n return x >= 0 && y >= 0 && x < n && y < n && s1[x][y] >= '0' && s1[x][y] <= '9';\n}\n\nint vv[M], nxt[M], cap[M], h[N], e;\nint pre[N], vis[N];\nvoid add( int u, int v, int c )\n{\n vv[e] = v, nxt[e] = h[u], cap[e] = c, h[u] = e++;\n vv[e] = u, nxt[e] = h[v], cap[e] = 0, h[v] = e++;\n}\n\nint q1[M], q2[M], h1, h2, t1, t2;\nint v1[15][15], v2[15][15];\nvoid bfs( int x, int y )\n{\n if( s2[x][y] != 0 ){\n add( id[x][y]+cnt, t, s2[x][y] - '0' );\n }\n if( s1[x][y] == '0' ) return ;\n add( s, id[x][y], s1[x][y] - '0' );\n if( s2[x][y] != 0 ){\n add( id[x][y], id[x][y]+cnt, 10 );\n }\n\n int sx = x, sy = y;\n h1 = t1 = h2 = t2 = 0;\n q1[t1++] = x, q1[t1++] = y;\n q2[t2++] = xz, q2[t2++] = yz;\n\n int up1 = t1, up2 = t2;\n memset( v1, -1, sizeof(v1) );\n memset( v2, -1, sizeof(v2) );\n v1[x][y] = v2[xz][yz] = 0;\n\n int x1, y1;\n while( h1 < t1 ){\n\n while( h1 < up1 ){\n x = q1[h1++], y = q1[h1++];\n if( v2[x][y] >= 0 ) continue;\n for( int i = 0; i < 4; ++i ){\n x1 = x + dir[i][0];\n y1 = y + dir[i][1];\n if( inside( x1, y1 ) && v1[x1][y1] < 0 ){\n v1[x1][y1] = v1[x][y] + 1;\n if( v1[x1][y1] > T ) break;\n if( v2[x1][y1] >= 0 ) continue;\n q1[t1++] = x1, q1[t1++] = y1;\n\n if( s2[x1][y1] != '0' ){\n add( id[sx][sy], id[x1][y1] + cnt, 10 );\n }\n }\n }\n }\n up1 = t1;\n\n while( h2 < up2 ){\n x = q2[h2++], y = q2[h2++];\n\n for( int i = 0; i < 4; ++i ){\n x1 = x + dir[i][0];\n y1 = y + dir[i][1];\n if( inside( x1, y1) && v2[x1][y1] < 0 ){\n v2[x1][y1] = v2[x][y] + 1;\n q2[t2++] = x1, q2[t2++] = y1;\n }\n }\n }\n up2 = t2;\n }\n}\n\nbool bfs()\n{\n queue q;\n memset( vis, 0, sizeof(vis) );\n vis[s] = 1;\n\n q.push( s );\n int u, v;\n while( !q.empty() ){\n u = q.front(), q.pop();\n for( int i = h[u]; i+1; i = nxt[i] ) if( cap[i] > 0 && !vis[vv[i]]){\n v = vv[i];\n pre[v] = i;\n if( v == t ) return 1;\n vis[v] = 1, q.push( v );\n\n }\n }\n return 0;\n}\nint maxFlow()\n{\n int ans = 0, mi, v;\n\n while( bfs() ){\n mi = 10000000;\n v = t;\n while( v - s ){\n v = pre[v];\n mi = min( mi, cap[v] );\n v = vv[v^1];\n }\n ans += mi;\n\n v = t;\n while( v - s ){\n v = pre[v];\n cap[v] -= mi;\n cap[v^1] += mi;\n v = vv[v^1];\n }\n }\n return ans;\n}\nint main()\n{\n // freopen( \"a.in\", \"r\", stdin );\n //freopen( \"a.out\", \"w\", stdout );\n cin>>n>>T;\n for( int i = 0; i < n; ++i ) cin>>s1[i];\n for( int i = 0; i < n; ++i ) cin>>s2[i];\n memset( h, -1, sizeof(h) ); e = 0;\n memset( id, -1, sizeof(id) );\n cnt = 0;\n for( int i = 0; i < n; ++i )\n for( int j = 0; j < n; ++j ){\n if( s1[i][j] == 'Z' ) xz = i, yz = j;\n else if( inside( i, j ) ) id[i][j] = ++cnt;\n }\n s = 0, t = 2*cnt + 1;\n for( int i = 0; i < n; ++i )\n for( int j = 0; j < n; ++j ) if( inside( i, j ) ) {\n bfs( i, j );\n }\n\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n//#include\n#include\n#define y1 abcde\n#define sqr(x) ((x)*(x)) \n#define INF 2147483647 \nusing namespace std;\n \n double ds,x[3],y[3],r[3],R[3];\n int ans;\n\n double dist(double x,double y,double xx, double yy)\n{ \n return sqrt((x - xx)*(x - xx) + (y - yy)*(y-yy));\n}\nint main()\n{\n //srand(GetTickCount());\n #ifndef ONLINE_JUDGE\n freopen(\"a.in\",\"r\",stdin);\n freopen(\"a.out\",\"w\",stdout);\n #endif\n\n cin>>x[1]>>y[1]>>r[1]>>R[1];\n cin>>x[2]>>y[2]>>r[2]>>R[2];\n \n //\n ds = dist(x[1],y[1],x[2],y[2]);\n if (ds >= R[2]+r[1]) ans++; else\n if (ds + r[1] <= r[2]) ans++; else\n if (ds + R[2] <= r[1]) ans++;\n \n if (ds >= R[2]+R[1]) ans++; else\n if (ds + R[1] <= r[2]) ans++; else\n if (ds + R[2] <= R[1]) ans++; \n\n if (ds >= r[2]+R[1]) ans++; else\n if (ds + r[2] <= r[1]) ans++; else\n if (ds + R[1] <= r[2]) ans++;\n \n if (ds >= R[2]+R[1]) ans++; else\n if (ds + R[2] <= r[1]) ans++; else\n if (ds + R[1] <= R[2]) ans++;\n // \n cout<\n\n#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)\n#define forn(i, n) for(int i = 0; i < (int)(n); i++)\n#define for1(i, n) for(int i = 1; i <= (int)(n); i++)\n#define all(x) (x).begin(), (x).end()\n#define clr(x) memset((x), 0, sizeof((x)));\n#define pb push_back\n#define mp make_pair\n#define x first\n#define y second\n\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef pair PII;\ntypedef pair pii;\ntypedef vector vi;\ntypedef long long i64;\ntypedef unsigned long long ull;\n\ntemplate\nbool remin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate\nbool remax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\nint nxt() {\n int x;\n scanf(\"%d\", &x);\n return x;\n}\n\nll gcd(ll a, ll b) {\n a = abs(a);\n b = abs(b);\n while (b) {\n a %= b;\n swap(a, b);\n }\n return a;\n}\n\ntypedef ll ptdata;\n\nstruct pt {\n ptdata x, y;\n\n pt() {}\n\n pt(ptdata x, ptdata y) : x(x), y(y) {}\n\n inline pt operator-(const pt &r) const {\n return pt(x - r.x, y - r.y);\n }\n\n inline pt operator+(const pt &r) const {\n return pt(x + r.x, y + r.y);\n }\n\n inline pt operator*(const ld &r) const {\n return pt(x * r, y * r);\n }\n\n inline ptdata sqlen() const {\n return abs(x * x + y * y);\n }\n\n inline bool operator<(const pt &r) const {\n if (x != r.x) return x < r.x;\n return y < r.y;\n }\n\n inline bool operator==(const pt &r) const {\n return x == r.x && y == r.y;\n }\n};\n\nostream &operator<<(ostream &os, const pt &p) {\n return os << \"(\" << p.x << \",\" << p.y << \")\";\n}\n\ninline ptdata cross(const pt &l, const pt &r) {\n return l.x * r.y - l.y * r.x;\n}\n\ninline ptdata dot(const pt &l, const pt &r) {\n return l.x * r.x + l.y * r.y;\n}\n\n\nll pwmod(ll a, ll n, ll mod) {\n ll ret = 1;\n while (n) {\n if (n & 1) ret = ret * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return ret;\n}\n\ntemplate\ninline T sqr(T x) {\n return x * x;\n}\n\nconst int N = 1000 * 1000;\nconst ll inf = LLONG_MAX >> 1;\nstruct Q {\n ll q[N];\n ll sum = 0;\n int cnt[N];\n int qs = 0, qsize = 0;\n Q() {}\n inline void add(ll val, int c) {\n sum += c;\n if (empty()) {\n q[qs] = val;\n cnt[qs] = c;\n ++qsize;\n return;\n }\n if (qsize == N) return;\n int ps1 = qs + qsize - 1;\n if (ps1 >= N) ps1 -= N;\n int ps2 = qs + qsize;\n if (ps2 >= N) ps2 -= N;\n if (q[ps1] == val) {\n cnt[ps1] += c;\n return;\n }\n q[ps2] = val;\n cnt[ps2] = c;\n qsize++;\n }\n inline bool empty() const {\n return qsize == 0;\n }\n inline pair min() const {\n if (empty()) return mp(inf, 1);\n return mp(q[qs], cnt[qs]);\n }\n inline pair max() const {\n if (empty()) return mp(-inf, 1);\n int ps1 = qs + qsize - 1;\n if (ps1 >= N) ps1 -= N;\n return mp(q[ps1], cnt[ps1]);\n }\n inline void pop() {\n sum -= cnt[qs];\n ++qs;\n --qsize;\n if (qs == N) qs = 0;\n }\n};\n\nQ q[2];\n\nconst int MAGIC = 100000;\n\ninline void solve() {\n int n = nxt();\n ll c[2];\n c[0] = nxt();\n c[1] = nxt();\n if (c[0] == 0 || c[1] == 0) {\n ll m = c[0] + c[1];\n cout << (n - 1) * m << \"\\n\";\n return;\n }\n q[0].add(0, 1);\n\n ll ans = 0;\n pair mi[2];\n pair ma[2];\n --n;\n while (n > 0) {\n forn(j, 2) {\n mi[j] = q[j].min();\n ma[j] = q[j].max();\n }\n int id = 0;\n if (mi[1].first < mi[0].first) id = 1;\n\n ll k = n;\n if (c[id]) {\n k = (mi[id ^ 1].first - ma[id].first) / c[id];\n }\n remin(k, (n / q[id].sum));\n\n if (k == 0 || q[id ^ 1].qsize < MAGIC) {\n int cc = min(mi[id].second, n);\n n -= cc;\n ans -= mi[id].first * cc;\n ans += mi[id].first * cc + c[0] * cc;\n ans += mi[id].first * cc + c[1] * cc;\n mi[id].second -= cc;\n if (mi[id].second == 0) {\n q[id].pop();\n }\n q[0].add(mi[id].first + c[0], cc);\n q[1].add(mi[id].first + c[1], cc);\n } else {\n// cerr << k << \"\\n\";\n// cerr << q[id].qsize << \"\\n\";\n vector > w;\n while (!q[id].empty()) {\n w.push_back(q[id].min());\n q[id].pop();\n }\n for (auto pa : w) {\n ans -= pa.first * pa.second;\n }\n for (auto pa : w) {\n// cerr << pa.first * pa.second + k * c[id] * pa.second << \"\\n\";\n// cerr << k * pa.first * pa.second + k * (k - 1) / 2 * c[id] + k * c[id ^ 1] * pa.second << \"\\n\";\n ans += pa.first * pa.second + k * c[id] * pa.second;\n ans += k * pa.first * pa.second + k * (k - 1) / 2 * c[id] + k * c[id ^ 1] * pa.second;\n q[id].add(pa.first + k * c[id], pa.second);\n }\n n -= k * q[id].sum;\n }\n }\n cout << ans << \"\\n\";\n}\n\nvoid prepare() {\n\n}\n\nint main() {\n#ifdef LOCAL\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n\n prepare();\n\n int t = 1;\n //t = nxt();\n forn(i, t) {\n solve();\n }\n //14999999850000000\n //195405044762803700\n //9999999999999999\n //100000000 1 100000000\n //20042860182236000\n cerr << \"Time \" << clock() / (double) CLOCKS_PER_SEC << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "dp"], "code_uid": "b00535f6d404c31507f295ce5527e113", "src_uid": "39b824b740a40f68bae39b8d9f0adcbe", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//加油吧 \n#include\nusing namespace std;\ntypedef long long ll;\nconst int mod=998244353,N=200200,G=3,Gi=332748118;\nll power(ll base,ll b){\n\tll ret=1;\n\twhile(b){\n\t\tif(b&1)ret=ret*base%mod;\n\t\tbase=base*base%mod;\n\t\tb>>=1;\n\t}\n\treturn ret;\n}\nint limit,len,last=-1,pos[N];\nll G0[N],G1[N],G2[N],g[N],F0[N],F1[N],F2[N]; \n//F0是两端都没有距离为2的点 \n//F1是一端有距离为2的点 (答案统计时有两种方案数相同的情况)\n//F2是两端都有距离为2的点 \ninline void getlen(int n){\n\tlimit=1,len=0;\n\twhile(limit>1]>>1)|((i&1)<<(len-1));\n}\ninline void ntt(ll *a,int n,int type){\n\tfor(int i=0;i>1;\n\tCDQ(l,mid);\n\tcalc(F0+l,mid-l+1,G0,r-l+1,tmp+l,r-l+1);\n\tfor(int i=mid+1;i<=r;++i)F0[i]=(F0[i]+tmp[i])%mod;\n\tcalc(F0+l,mid-l+1,G1,r-l+1,tmp+l,r-l+1);\n\tfor(int i=mid+1;i<=r;++i)F1[i]=(F1[i]+tmp[i])%mod;\n\tcalc(F1+l,mid-l+1,G1,r-l+1,tmp+l,r-l+1);\n\tfor(int i=mid+1;i<=r;++i)F0[i]=(F0[i]+tmp[i])%mod;\n\tfor(int i=mid+1;i<=r;++i)F2[i]=(F2[i]+tmp[i])%mod;\n\tcalc(F1+l,mid-l+1,G2,r-l+1,tmp+l,r-l+1);\n\tfor(int i=mid+1;i<=r;++i)F1[i]=(F1[i]+tmp[i])%mod;\n\tcalc(F2+l,mid-l+1,G2,r-l+1,tmp+l,r-l+1);\n\tfor(int i=mid+1;i<=r;++i)F2[i]=(F2[i]+tmp[i])%mod;\n\t//注意是左边对右边的贡献 \n\tCDQ(mid+1,r);\n}\nint n;\nint main(){\n//\tfreopen(\"y.in\",\"r\",stdin);\n\tscanf(\"%d\",&n);\n\tg[0]=g[2]=1;\n\tfor(int i=4;i<=n;++i)g[i]=(g[i-2]+g[i-4])%mod;\n\tfor(int i=1;i<=n;++i)G0[i]=1LL*(i-1)*(i-1)%mod*g[i-1]%mod;\n\tfor(int i=2;i<=n;++i)G1[i]=1LL*(i-1)*(i-1)%mod*g[i-2]%mod;\n\tfor(int i=3;i<=n;++i)G2[i]=1LL*(i-1)*(i-1)%mod*g[i-3]%mod;\n\tCDQ(1,n); \n\tll Ans=1LL*n*(G0[n]+G2[n])%mod;//可以旋转n次\n\tfor(int i=2;i<=n-2;++i){\n\t\tAns=(Ans+(G0[i]*F0[n-i]%mod+2LL*G1[i]*F1[n-i]%mod+G2[i]*F2[n-i]%mod)*(ll)i%mod)%mod;//把第一个相对的点对旋转到1-n+1处 \n\t} \n\tprintf(\"%lld\\n\",(Ans%mod+mod)%mod);\n\treturn 0;\n} ", "lang_cluster": "C++", "tags": ["divide and conquer", "math", "dp", "combinatorics", "fft"], "code_uid": "e65dac0be8b71caa5407b7a1b3765da1", "src_uid": "24fd5cd218f65d4ffb7c5b97b725293e", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//khodaya khodet komak kon\n#include \n\n#define F first\n#define S second\n#define pb push_back\n#define all(x) x.begin(), x.end()\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC optimize (\"-O2\")\n\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\n\nconst int N = 1000000 + 10;\nconst ll MOD = 1000000000 + 7;\nconst ll INF = 1000000010;\nconst ll LOG = 25;\nconst ll K = 100000000;\n\nmap, int> mp;\nvector> V;\nint dis[1300][1300], q, pnt, mn[1300][1300], mark[N];\nll num[N];\nint P[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};\nvector vv, D[N], G[N];\n\nvoid BT(int x = 0, ll n = 1, int mx = 41){\n\tif (x == 10){\n\t\tvector KK = vv;\n\t\treverse(all(KK));\n\t\tmp[KK] = ++pnt;\n\t\tV.pb(KK);\n\t\treturn;\n\t}\n\tfor (int i = 0; i <= mx && n < K; i++){\n\t\tvv.pb(i);\n\t\tBT(x + 1, n, i);\n\t\tvv.pop_back();\n\t\tn *= P[x];\n\t}\n}\n\nvoid BFS(int v){\n\tdis[v][v] = 0;\n\t//cout << v << endl;\n\tqueue Q;\n\tQ.push(v);\n\t\n\twhile (Q.size()){\n\t\t//cout << \"YES2\" << endl;\n\t\tint fr = Q.front();\n\t\tmn[v][num[fr]] = min(mn[v][num[fr]], dis[v][fr]);\n\t\tQ.pop();\n\t\tfor (auto u:G[fr]){\n\t\t\tif (dis[v][u] > dis[v][fr] + 1){\n\t\t\t\tdis[v][u] = dis[v][fr] + 1;\n\t\t\t\tQ.push(u);\n\t\t\t}\n\t\t}\t\n\t}\n}\n\nint32_t main(){\n\tios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n\tcin >> q;\n\tBT();\n//\tcout << pnt<< endl;\n\tmark[1] = 1;\n\tfor (int i = 1; i < N; i++){\n\t\tif (!mark[i]){\n\t\t\tfor (int j = i; j < N; j += i){\n\t\t\t\tmark[j] = 1;\n\t\t\t\tint x = j;\n\t\t\t\tint cnt = 0;\n\t\t\t\twhile (x % i == 0){\n\t\t\t\t\tx /= i;\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t\tD[j].pb(cnt);\n\t\t\t}\n\t\t}\n\t\twhile (D[i].size() < 10) D[i].pb(0);\n\t\tsort(all(D[i]));\n\t}\n\tvector adad;\n\tfor (vector u:V){\n\t\tfor (int i = 0; i < 10; i++){\n\t\t\tvector uu;\n\t\t\tfor (auto x:u) uu.pb(x);\n\t\t\tuu[i] --;\n\t\t\tsort(all(uu));\t\n\t\t\tif (mp.count(uu)) G[mp[uu]].pb(mp[u]), G[mp[u]].pb(mp[uu]);\n\t\t\tuu.clear();\n\t\t\tfor (auto x:u) uu.pb(x);\n\t\t\tuu[i] ++;\n\t\t\tsort(all(uu));\n\t\t\tif (mp.count(uu)) G[mp[uu]].pb(mp[u]), G[mp[u]].pb(mp[uu]);;\n\t\t}\n\t\tll res = 1;\n\t\tfor (auto x:u) res *= x + 1;\n\t\tnum[mp[u]] = res;\n\t\tadad.pb(res);\n\t}\n\tsort(all(adad));\n\tadad.resize(unique(all(adad)) - adad.begin());\n\tfor (int i = 1; i <= pnt; i++){\n\t\tnum[i] = lower_bound(all(adad), num[i]) - adad.begin() + 1;\n\t}\n\t//cout << pnt << endl;\n//\tcout << \"YES\" << endl;\n\tmemset(mn, 31, sizeof mn);\n\tmemset(dis, 31, sizeof dis);\n\tfor (int i = 1; i <= pnt; i++) BFS(i);\n\t//cout << dis[mp[D[9]]][mp[D[3]]] << '\\n';\n\tfor (int i = 1; i <= q; i++){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif (!mp.count(D[a]) || !mp.count(D[b])) assert(0);\n\t\ta = mp[D[a]];\n\t\tb = mp[D[b]];\n\t\tint ans = 10000;\n\t\tfor (int j = 1; j <= pnt; j++){\n\t\t\tans = min(ans, mn[a][j] + mn[b][j]);\n\t\t}\t\n\t\tcout << ans << '\\n';\n\t}\n\n\n\n\n\n\n\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "math", "graphs"], "code_uid": "8102029ef3c54f210bfe11f3506a7e57", "src_uid": "6115ee1ccf651b1dd8145bf755ea7ea9", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define pb push_back\n#define ll long long\n#define mp make_pair\n#define pu push\n#define po pop\n#define N 100005\n#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));\nusing namespace std;\nll n1,n2,x,y,i=0,j;\nstring a,b,ans;\nbool flag=0;\nint main(void){\n\tcin>>a>>b;\n\tsort(a.begin(),a.end());\n\t\n\tn1=a.length();\n\tn2=b.length();\n\t\n\tif(n2>n1)return reverse(a.begin(),a.end()),cout<=stoll(a) && stoll(t)<=stoll(b))\n\t\t\ta=t;\n\t\t}\n\t}\n\tcout<\n// base types\n#define _i int\n#define _l long\n#define _ll long long\n#define _f float\n#define _d double\n#define _ld long double\n// containters\n#define _str std::string\n#define _v std::vector\n#define _vi std::vector\n#define _vl std::vector\n#define _vll std::vector\n#define _vstr std::vector\n#define _vvi std::vector >\n#define _psb push_back\n#define _ppb pop_back\n#define _s std::set\n#define _si std::set\n#define _sl std::set\n#define _sll std::set\n#define _sstr std::set\n#define _ins insert\n#define _rem remove\n#define _p std::pair\n#define _m std::map\n#define _q std::queue\n#define _ps push\n#define _pp pop\n// algorithms\n#define _sort(a) std::sort(a.begin(), a.end())\n// io\n#define _fio std::ios_base::sync_with_stdio(0); std::cin.tie(0)\n#define _in std::cin\n#define _ina(a, n) for (decltype(n) i = 0; i < (n); i++) { std::cin >> a[i]; }\n#define _out std::cout\n#define _outa(a) for (auto i : a) std::cout << i << ' '; std::cout << std::endl;\n#define _nl std::endl\n// general\n#define _rep(i, a, b) for (decltype(b) (i) = (a); i < (b); i++)\n#define _till while\n#define _max(a, b) (a) > (b) ? (a) : (b)\n#define _min(a, b) (a) < (b) ? (a) : (b)\n#define _for(i,a) for (auto i : a)\n#define _ord(a) a - '0'\n#define _mod 1000000007\n// https://stackoverflow.com/a/47434423\n#define _inf (unsigned)!((int)0)\n#define _pi 3.141592653589\n#define _eps 0.000000001\n\nint main()\n{\n _d n, R, r;\n _in >> n >> R >> r;\n if (R == r || r / (R - r) > 1) {\n if (n == 1 && (R - r < _eps || R > r)) {\n _out << \"YES\" << _nl;\n } else {\n _out << \"NO\" << _nl;\n }\n } else {\n _d t = asin(r / (R - r));\n //_out << _pi / t - n << _nl;\n if (_pi / t > n || (_pi / t > n ? _pi / t - n : n - _pi / t) < _eps) {\n _out << \"YES\" << _nl;\n } else {\n _out << \"NO\" << _nl;\n }\n }\n}\n", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "94d5e16bf746a814cc2ec6d5087c4636", "src_uid": "2fedbfccd893cde8f2fab2b5bf6fb6f6", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nint pot2[20];\n\nint main() {\n\tpot2[0]=1;\n\tfor (int i=1;i<20;i++){\n\t\tpot2[i]=2*pot2[i-1];\n\t} \n\tint n,l,r,max_sum=0,min_sum=0;\n\tcin>>n>>l>>r;\n\tfor(int i=0;i\r\n#define pa pair\r\ntypedef unsigned long long ull;\r\ntypedef long long ll;\r\nusing namespace std;\r\nconst int N=1e7+10;\r\nint inf=1e9;\r\nconst int mod =998244353;\r\ninline int qpow(int a,int b,int Mod){\r\n\tint ans=1;\r\n\twhile(b){\r\n\t\tif(b&1) ans=(1ll*ans*a%Mod);\r\n\t\tb>>=1;a=1ll*a*a%Mod;\r\n\t}\r\n\treturn ans;\r\n}\r\n\r\ninline int rd(){\r\n\tint x=0;int f=1;char s=getchar();\r\n\twhile(!isdigit(s)){ if(s=='-')f=-1;s=getchar();}\r\n\twhile(isdigit(s)){ x=(x<<1)+(x<<3)+s-'0';s=getchar(); }\r\n\treturn f==1?x:-x;\r\n}\r\nint cas;\r\nll n,x,y,k,M;\r\nll dp[720721][18];\r\nll a[N];\r\nll fac[20];\r\nint main(){\r\n\r\n\t\r\n\tcin>>n>>a[1]>>x>>y>>k>>M;\r\n\tfor(int i=2;i<=n;i++) a[i]=(x*a[i-1]+y)%M;\r\n//\tfor(int i=1;i<=n;i++) cout<\n\nusing namespace std;\n\nint main()\n{\n int k;\n while(cin>>k)\n {\n k+=k;\n int a[10]={};\n int flag=1;\n for(int i=0;i<16;i++)\n {\n char ch;\n cin>>ch;\n if(ch>='0'&&ch<='9')\n {\n a[ch-'0']++;\n if(a[ch-'0']>k) flag=0;\n }\n }\n if(flag==1) cout<<\"YES\"<\nusing namespace std;\ntypedef long long LL;\ntypedef unsigned long long ULL;\nconst int N=100005;\nint INF=0x3f3f3f3f;\nLL mod=1e9+7;\nint main(){\n#ifndef ONLINE_JUDGE\n freopen(\"in.txt\",\"r\",stdin);\n#endif\n string s[13]={\n \"\",\n \"x^y^z\",\n \"x^z^y\",\n \"(x^y)^z\",\n \"(x^z)^y\",\n \"y^x^z\",\n \"y^z^x\",\n \"(y^x)^z\",\n \"(y^z)^x\",\n \"z^x^y\",\n \"z^y^x\",\n \"(z^x)^y\",\n \"(z^y)^x\",\n };\n int ans=1;\n long double x,y,z;\n long double mx=-10000,t;\n cin>>x>>y>>z;\n if(x<=1&&y<=1&&z<=1){\n ans=1;\n mx=pow(x,pow(y,z));\n t=pow(x,pow(z,y));\n if(t>mx){\n mx=t;\n ans=2;\n }\n t=pow(pow(x,y),z);\n if(t>mx){\n mx=t;\n ans=3;\n }\n t=pow(y,pow(x,z));\n if(t>mx){\n mx=t;\n ans=5;\n }\n t=pow(y,pow(z,x));\n if(t>mx){\n mx=t;\n ans=6;\n }\n t=pow(pow(y,x),z);\n if(t>mx){\n mx=t;\n ans=7;\n }\n t=pow(z,pow(x,y));\n if(t>mx){\n mx=t;\n ans=9;\n }\n t=pow(z,pow(y,x));\n if(t>mx){\n mx=t;\n ans=10;\n }\n t=pow(pow(z,x),y);\n if(t>mx){\n mx=t;\n ans=11;\n }\n }\n else{\n //cout<<\"go\"<1){\n t=z*log(y)+log(log(x));\n if(t>mx){\n mx=t;\n ans=1;\n }\n t=y*log(z)+log(log(x));\n if(t>mx){\n mx=t;\n ans=2;\n }\n t=log(y)+log(z)+log(log(x));\n if(t>mx){\n mx=t;\n ans=3;\n }\n }\n if(y>1){\n t=z*log(x)+log(log(y));\n if(t>mx){\n mx=t;\n ans=5;\n }\n t=x*log(z)+log(log(y));\n if(t>mx){\n mx=t;\n ans=6;\n }\n t=log(x)+log(z)+log(log(y));\n if(t>mx){\n mx=t;\n ans=7;\n }\n }\n if(z>1){\n t=y*log(x)+log(log(z));\n if(t>mx){\n mx=t;\n ans=9;\n }\n t=x*log(y)+log(log(z));\n if(t>mx){\n mx=t;\n ans=10;\n }\n t=log(x)+log(y)+log(log(z));\n if(t>mx){\n mx=t;\n ans=11;\n }\n }\n }\n \n cout<\n#include \n#include \n#include \n\nint main()\n{\n\tint n, x, y, t, a = 0, b = 0;\n\tstd::cin >> n;\n\tt = std::sqrt(double(n));\n\tfor (y = 1; y <= t; ++y)\n\t{\n\t\tx = n / y;\n\t\tif (x*y == n)\n\t\t{\n\t\t\ta = y;\n\t\t\tb = x;\n\t\t}\n\t}\n\tstd::cout << a << ' ' << b;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "b3843716232957f93abfbad8f55e7528", "src_uid": "f52af273954798a4ae38a1378bfbf77a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\nusing namespace std;\n\n\n#define LL long long int\n#define LD long double\n#define ULL unsigned long long int\n\n\n#define pii pair\n#define ump unordered_map\n#define mp make_pair\n#define mt make_tuple\n#define pb push_back\n#define ff first\n#define ss second\n#define SZ(x) (int)x.size()\n#define all(x) x.begin(), x.end()\n#define lb(x, y) lower_bound(all(x), y)\n#define fill2d(arr, a, b, v) fill(&arr[0][0], &arr[0][0] + a * b, v)\n#define fill3d(arr, a, b, c, v) fill(&arr[0][0], &arr[0][0] + a * b * c, v)\n#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))\n\n\n#define sp(x) setprecision(x) << fixed\n#define what_is(x) cout << #x << \" is \" << x << endl\n//#define I64 ll\n\n\n#define PI acos(-1.0)\n#define EPS 1e-12\n#define mx 200010\n#define S_SIZE 2000010\n\n\nconst LL inf = 1000000000;\nconst LL mod = 1000000000 + 7;\n\n\ninline void IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }\ninline int dcmp (LD x) { return x < -EPS ? -1 : (x > EPS); }\n\n\ntemplate inline int CHECK(T MASK, int i) { return (MASK >> i) & 1; }\ntemplate inline T ON(T MASK, int i) { return MASK | (T(1) << i); }\ntemplate inline T OFF(T MASK, int i) { return MASK & (~(T(1) << i)); }\ntemplate inline int CNT(T MASK) {\n if (numeric_limits::digits <= numeric_limits::digits) return __builtin_popcount(MASK);\n else return __builtin_popcountll(MASK);\n}\ntemplate inline int RIGHT(T MASK) { return log2(MASK & -MASK); }\n\n\nint dx4[] = { 0, 0, -1, +1 }; int dy4[] = { +1, -1, 0, 0 };\nint dx8[] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 }; int dy8[] = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };\n\n\ninline void I(int& a) { scanf(\"%d\", &a); } inline void I(LL& a) { scanf(\"%I64d\", &a); }\ninline void I(ULL& a) { scanf(\"%I64u\", &a); } inline void I(char* a) { scanf(\"%s\", a); }\nchar Iarr[S_SIZE]; inline void I(string& a) { scanf(\"%s\", Iarr); a = Iarr; }\ntemplate\nvoid I(T& a, Args&... args) { I(a); I(args...); }\n\n\ninline void OUT(int a) { printf(\"%d\", a); } inline void OUT(LL a) { printf(\"%I64d\", a); }\ninline void OUT(const char* a) { printf(\"%s\", a); } inline void OUT(char* a) { printf(\"%s\", a); }\ninline void OUT(bool a) { printf(\"%d\", a); } inline void OUT(string a) { rep(it, a.begin(), a.end()) printf(\"%c\", *it); }\ninline void OUT(ULL a) { printf(\"%I64u\", a); }\ntemplate\nvoid OUT(T a, Args... args) { OUT(a); OUT(\" \"); OUT(args...); }\ntemplate\nvoid O(Args... args) { OUT(args...); OUT(\"\\n\"); }\n\n\ntemplate\ninline ostream& operator<<(ostream& os, pair p) { os << \"{\" << p.first << \", \" << p.second << \"}\"; return os; }\ntemplate\ninline ostream& operator<<(ostream& os, vector& a) { os << \"[\"; for (int i = 0; i < (int)a.size(); i++) { if (i) os << \", \"; os << a[i]; } os << \"]\"; return os; }\n\n\n#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); }\nvoid err(istream_iterator it) {}\ntemplate\nvoid err(istream_iterator it, T a, Args... args) {\n\tcout << *it << \" = \" << a << endl;\n\terr(++it, args...);\n}\n\n// mp.reserve(1024); mp.max_load_factor(0.25);\n// head\n\n\nconst int M = 1000010; // maximum number of cubes\n\nstruct NumberTheory {\n LL gcd(LL a, LL b) { while (b) { a %= b; swap(a, b); } return a; }\n LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }\n LL bigMod(LL p, LL e, LL m) {\n LL ret = 1; p %= m;\n while (e > 0) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; e >>= 1; }\n return ret;\n }\n /// (1/a) % m when a & m are co-primes\n LL modInverse(LL a, LL m) {\n LL g = gcd(a, m);\n if (g != 1) {\n cout << \"Inverse doesn't exist\" << endl; return -1;\n }\n LL m0 = m;\n LL y = 0, x = 1;\n if (m == 1) return 0;\n while (a > 1) {\n LL q = a / m;\n LL t = m;\n m = a % m, a = t;\n t = y;\n y = x - q * y;\n x = t;\n }\n if (x < 0) x += m0;\n return x;\n }\n /// generate all (1/i) % m in [1..n] where n is less than m\n /// m must be prime\n void genModInv(int n, LL m, ULL inv[]) {\n inv[1] = 1;\n for (int i = 2; i <= n; ++i)\n inv[i] = (m - (m / i) * inv[m%i] % m) % m;\n }\n /// generate all (1/i!) % m in [1..n] where n is less than m\n /// m must be prime\n void genModInvFact(int n, LL m, ULL inv[], ULL invFact[]) {\n invFact[0] = invFact[1] = 1;\n for (int i = 2; i <= n; i++)\n invFact[i] = (invFact[i-1] * inv[i]) % m;\n }\n} num;\n\nstruct CRT\n{\n long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); }\n inline long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }\n inline long long normalize(long long x, long long mod) { x %= mod; if (x < 0) x += mod; return x; }\n struct GCD_type { long long x, y, d; };\n GCD_type ex_GCD(long long a, long long b)\n {\n if (b == 0) return {1, 0, a};\n GCD_type pom = ex_GCD(b, a % b);\n return {pom.y, pom.x - a / b * pom.y, pom.d};\n }\n // Helper Functions\n\n pair calc(int t, long long aa[], long long nn[])\n {\n // Make sure the arrays are 1 indexed\n long long* a = new long long[t+1];\n long long* n = new long long[t+1];\n long long ans, lcm;\n for(int i = 1; i <= t; i++) {\n a[i] = aa[i];\n n[i] = nn[i];\n normalize(a[i], n[i]);\n }\n ans = a[1];\n lcm = n[1];\n for(int i = 2; i <= t; i++) {\n auto pom = ex_GCD(lcm, n[i]);\n int x1 = pom.x;\n int d = pom.d;\n if((a[i] - ans) % d != 0) return cerr << \"No solutions\" << endl, mp(-1, -1);\n ans = normalize(ans + x1 * (a[i] - ans) / d % (n[i] / d) * lcm, lcm * n[i] / d);\n lcm = LCM(lcm, n[i]); // you can save time by replacing above lcm * n[i] /d by lcm = lcm * n[i] / d\n }\n return make_pair(ans, lcm);\n }\n // t is the number of congruences\n // Array is 1 indexed\n // x mod n1 = a1 mod n1\n // x mod n2 = a2 mod n2\n // x mod n3 = a3 mod n3\n // Here there is an unique value for a certain range.\n // This range will be n1 * n2 * n3 if all of them\n // are pairwise co-prime. But can be different otherwise.\n // The unique value is stored in \"ans\" and the range is\n // stored in \"lcm\".\n // The value repeats after \"lcm\".\n // If all the n's are not pairwise co-prime then there\n // might not be an answer.\n} crt;\n\nint main()\n{\n LL a, b, p, x;\n I(a, b, p, x);\n LL res = 0;\n for (int i = 1; i <= p - 1; i++) {\n int t = 2;\n long long a1[3], n[3];\n n[1] = p - 1;\n n[2] = p;\n a1[1] = i;\n a1[2] = (b * num.modInverse(num.bigMod(a, i, p), p)) % p;\n pair temp = crt.calc(t, a1, n);\n if (temp.ff <= x)\n res += (x - temp.ff) / temp.ss + 1;\n }\n O(res);\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "35684c459987070391dd71d8162d502e", "src_uid": "4b9f470e5889da29affae6376f6c9f6a", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define Fast_IO ios::sync_with_stdio(false);\n#define DEBUG fprintf(stderr,\"Running on Line %d in Function %s\\n\",__LINE__,__FUNCTION__)\n//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\n#define fir first\n#define sec second\n#define ll long long\n#define int long long\n#define inf 0x3f3f3f3f\n#define INF 0x3f3f3f3f3f3f3f3f\ninline int read()\n{\n\tchar ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}\n\tint ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}\n\tif(nega==-1) return -ans;\n\treturn ans;\n}\nvector rev,rt,one(1,1),zer;\n\n#undef int\n\nnamespace Poly\n{\n\t#define mod 31525197391593473LL\n\t#define int long long\n\t#define ull unsigned long long\n\tinline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}\n\tinline int add(int x,int y,int z){return add(add(x,y),z);}\n\tinline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}\n\t// inline ull mul(ull x,ull y){return (x*y-(ull)((long double)x/mod*y)*mod+mod)%mod;}\n\tinline int mul(int x,int y){return (__int128)x*y%mod;}\n\tinline int mul(int x,int y,int z){return mul(mul(x,y),z);}\n\tint qpow(int x,int y)\n\t{\n\t\tint ans=1;\n\t\twhile(y)\n\t\t{\n\t\t\tif(y&1) ans=mul(ans,x);\n\t\t\tx=mul(x,x);\n\t\t\ty>>=1;\n\t\t}\n\t\treturn ans;\n\t}\n\tint getInv(int x)\n\t{\n\t\treturn qpow(x,mod-2);\n\t}\n\tint __inv[2000005];\n\tconst int G=3;\n\tvoid Init_Inv()\n\t{\n\t\t__inv[0]=__inv[1]=1;\n\t\tfor(int i=2;i<=2000000;i++) __inv[i]=mul(mod-mod/i,__inv[mod%i]);\n\t}\n\tvoid init(int B)\n\t{\n\t\tint n=1<>1]>>1)|((i&1)<<(B-1));\n\t\tfor(int mid=1;mid &a,int B)\n\t{\n\t\tint n=1<i) swap(a[i],a[rev[i]]);\n\t\tfor(int mid=1;mid Mul(vector a,vector b)\n\t{\n\t\tint B=0,n=a.size()+b.size()-1;\n\t\twhile((1<=mod?x+y-mod:x+y;}\ninline int add(int x,int y,int z){return add(add(x,y),z);}\ninline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}\ninline int mul(int x,int y){return 1LL*x*y%mod;}\ninline int mul(int x,int y,int z){return mul(mul(x,y),z);}\nvector _QAQ;\nvector operator * (vector x,vector y)\n{\n\tvector a(x.size()),b(y.size());\n\tfor(int i=0;i<(int)x.size();i++) a[i]=x[i];\n\tfor(int i=0;i<(int)y.size();i++) b[i]=y[i];\n\tvector r=Poly::Mul(a,b);\n\tvector ans(r.size());\n\tfor(int i=0;i<(int)r.size();i++) ans[i]=r[i]%mod;\n\tif((int)ans.size()>n+1) ans.resize(n+1);\n\treturn ans;\n}\nvector operator + (vector a,vector b)\n{\n\tint n=max(a.size(),b.size());\n\ta.resize(n),b.resize(n);\n\tfor(int i=0;i operator - (vector a,vector b)\n{\n\tint n=max(a.size(),b.size());\n\ta.resize(n),b.resize(n);\n\tfor(int i=0;i a[200],b[200];\nint cnt;\nmap vis;\nvoid print(vector v)\n{\n\tcout<<\"-- \";\n\tfor(int i :v) printf(\"%d \",i);\n\tcout<<\"\\n\";\n}\nint solve(int x)\n{\n\tif(vis[x]) return vis[x];\n\tvis[x]=++cnt;\n\tint cur=cnt;\n\tif(x==0)\n\t{\n\t\ta[cur]=b[cur]=zer;\n\t\treturn cur;\n\t}\n\tint V=solve(x/2);\n\tvector A=a[V],B=b[V];\n\t// print(A),print(B);\n\tif(x%2==0)\n\t{\n\t\tif((x/2)&1) a[cur]=A+(A+B+one)*B,b[cur]=B+(A+B+one)*A;\n\t\telse a[cur]=A+(A+B+one)*A,b[cur]=B+(A+B+one)*B;\n\t}\n\telse\n\t{\n\t\tint R=solve(x-1);\n\t\tvector A=a[R],B=b[R];\n\t\tif(x&1) B=B+(A+B+one)*_QAQ;\n\t\telse A=A+(A+B+one)*_QAQ;\n\t\ta[cur]=A,b[cur]=B;\n\t/*\n\t\tvector C=A,D=B;\n\t\tif((x/2)&1) C=C+(A+B+one)*_QAQ;\n\t\telse D=D+(A+B+one)*_QAQ;\n\t\tif((x/2)&1) a[cur]=A+(A+B+one)*C,b[cur]=A+(A+B+one)*D;\n\t\telse a[cur]=A+(A+B+one)*D,b[cur]=A+(A+B+one)*C;\n\t*/\n\t}\n\treturn cur;\n}\nsigned main()\n{\n\tPoly::Init_Inv();\n\t_QAQ.resize(2); _QAQ[1]=1; zer.resize(1);\n\t// cout<<_QAQ[1]<>n>>m>>mod;\n\tint ans=0;\n\tfor(ll i=2;i<=m;i*=2)\n\t{\n\t\tint pl=solve(m/i);\n\t\t// printf(\"%d :\\n\",m/i);\n\t\t// for(int i=0;i<(int)a[pl].size();i++) printf(\"%d \",a[pl][i]); cout<<\"\\n\";\n\t\t// for(int i=0;i<(int)b[pl].size();i++) printf(\"%d \",b[pl][i]); cout<<\"\\n\";\n\t\tfor(int i=1;i<(int)b[pl].size();i+=2) ans=add(ans,b[pl][i]);\n\t}\n\tcout<\n#define int long long\nusing namespace std;\nconst int MAX_N=100005;\nint n,m;\nint H(int x)\n{\n\tif(x>=0)return 1;\n\telse return 0;\n}\nbool legal(int nx,int ny,int dx,int dy)\n{\n\tif(nx==1 && ny==1 && dx==-1 && dy==-1)return false;\n\tif(nx==1 && ny==m && dx==-1 && dy==1)return false;\n\tif(nx==n && ny==1 && dx==1 && dy==-1)return false;\n\tif(nx==n && ny==m && dx==1 && dy==1)return false;\n\treturn true;\n}\nmap vis[2][2][MAX_N];\nmap used[MAX_N];\nsigned main()\n{\n\tint nx,ny,dx,dy;\n\tcin>>n>>m;\n\tcin>>nx>>ny;\n\tstring s;\n\tcin>>s;\n\tif(s[0]=='U')dx=-1;\n\telse dx=1;\n\tif(s[1]=='L')dy=-1;\n\telse dy=1;\n\tint ans=1;\n\tint cnt=0;\n\twhile(true)\n\t{\n\t//\tcout<\n\nusing namespace std;\nint main()\n{\n long long n,m;\n scanf(\"%I64d %I64d\",&n,&m);\n int p=1;\n int z=1000000009;\n for(int i=1;i<=m;i++)\n {\n p*=2;\n if(p>z) p%=z;\n }\n p+=z;\n long long ans=1;\n for(int i=1;i<=n;i++)\n {\n ans*=(p-i);\n ans%=z;\n }\n cout<\nusing namespace std;\n\ntypedef long long ll;\n\nconst int mo=int(1e9)+7;\n\nint quickpow(int x,int k) {\n int ret=1;\n while(k>0) {\n if(k&1) ret=1ll*ret*x%mo;\n x=1ll*x*x%mo;\n k>>=1;\n }\n return ret;\n}\n\nint n,m;\nint main() {\n scanf(\"%d%d\",&n,&m);\n printf(\"%I64d\\n\",1ll*quickpow(2*n+2,m)*(n+1-m)%mo*quickpow(n+1,mo-2)%mo);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "231c90215493aaa2e93df088c84175e5", "src_uid": "4f9711197e699c0fd0c4e9db8323cac7", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \nusing namespace std; \n#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n#define PI1 3.14159265358979311599796346854418516159057617187500\n\ntypedef long long int ll; \n\nint main() \n{ \n //FIO\n int t;\n //cin >> t;\n t=1;\n while(t--)\n {\n ll a,b,c,d;\n scanf(\"%lld %lld %lld %lld\",&a,&b,&c,&d);\n\n ll rn1 = b-a+1;\n ll rn2 = c-b+1;\n ll rn3 = d-c+1;\n\n ll ans = rn1*rn2*rn3;\n ll pre[1000010]={0};\n for(int i=a;i<=b;i++)\n {\n pre[i+b]++;\n pre[i+c+1]--;\n }\n \n for(ll i=1;i<=d+1;i++)\n pre[i] += pre[i-1];\n \n for(ll i=1;i<=d;i++)\n pre[i] += pre[i-1];\n\n for(ll i=c;i<=d;i++)\n ans -= pre[i];\n\n printf(\"%lld\",ans);\n\n }\n return 0; \n} ", "lang_cluster": "C++", "tags": ["two pointers", "math", "implementation", "binary search"], "code_uid": "5c8da5643f02a1c138bc6733f50bcd48", "src_uid": "4f92791b9ec658829f667fcea1faee01", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \r\nusing namespace std;\r\nconst int m = 26, mod = 998244353;\r\ntypedef long long ll;\r\n#define int ll\r\ninline int fastpow(int a, int n)\r\n{\r\n\tint ans = 1;\r\n\twhile(n)\r\n\t{\r\n\t\tif(n & 1)\r\n\t\t\tans = ans * a % mod;\r\n\t\ta = a * a % mod, n >>= 1;\r\n\t}\r\n\treturn ans;\r\n}\r\nint c[30];\r\nint f[2][410][2][2], g[2][410][410][3][3];\r\nint sumf[410], sumg[410][410];\r\ninline void admod(int &x, int y)\r\n{\r\n\tx = (x + y) % mod;\r\n}\r\ninline int GetSumF(int l, int r)\r\n{\r\n\treturn (sumf[r] - sumf[l - 1] + mod) % mod;\r\n}\r\ninline int GetSumG(int l1, int r1, int l2, int r2)\r\n{\r\n\treturn (sumg[r1][r2] - sumg[r1][l2 - 1] - sumg[l1 - 1][r2] + sumg[l1 - 1][l2 - 1] + 2 * mod) % mod;\r\n}\r\nsigned main()\r\n{\r\n\tint n;\r\n\tscanf(\"%lld\", &n);\r\n\tfor(int i = 1; i <= m; i++)\r\n\t\tscanf(\"%lld\", &c[i]);\r\n\tfor(int i = 0; i <= 1; i++)\r\n\t\tfor(int j = 0; j <= 1; j++)\r\n\t\t\tf[0][i + j][i][j] = (25 - i * 24) * (25 - j * 24);\r\n\tfor(int I = 3, i = 1; I <= n; I++, i ^= 1)\r\n\t{\r\n\t\tfor(int j = 0; j <= (I >> 1) + 1; j++)\r\n\t\t\tfor(int s = 0; s <= 1; s++)\r\n\t\t\t\tfor(int k = 0; k <= 1; k++)\r\n\t\t\t\t\tf[i][j][s][k] = 0;\r\n\t\tfor(int j = 0; j <= (I >> 1) + 1; j++)\r\n\t\t\tfor(int s = 0; s <= 1; s++)\r\n\t\t\t{\r\n\t\t\t\tadmod(f[i][j][s][0], f[i ^ 1][j][0][s] * 24);\r\n\t\t\t\tadmod(f[i][j + 1][s][1], f[i ^ 1][j][0][s]);\r\n\t\t\t\tadmod(f[i][j][s][0], f[i ^ 1][j][1][s] * 25);\r\n\t\t\t}\r\n\t}\r\n\tfor(int i = 0; i <= 2; i++)\r\n\t\tfor(int j = 0; j <= 2; j++)\r\n\t\t\tg[0][(i == 1) + (j == 1)][(i == 2) + (j == 2)][i][j] = (24 - bool(i) * 23) * (24 - bool(j) * 23);\r\n\tfor(int I = 3, i = 1; I <= n; I++, i ^= 1)\r\n\t{\r\n\t\tfor(int j = 0; j <= (I >> 1) + 1; j++)\r\n\t\t\tfor(int k = 0; k <= (I >> 1) + 1; k++)\r\n\t\t\t\tfor(int s = 0; s <= 2; s++)\r\n\t\t\t\t\tfor(int l = 0; l <= 2; l++)\r\n\t\t\t\t\t\tg[i][j][k][s][l] = 0;\r\n\t\tfor(int j = 0; j <= (I >> 1) + 1; j++)\r\n\t\t\tfor(int k = 0; k <= (I >> 1) + 1; k++)\r\n\t\t\t\tfor(int s = 0; s <= 2; s++)\r\n\t\t\t\t{\r\n\t\t\t\t\tadmod(g[i][j][k][s][0], g[i ^ 1][j][k][0][s] * 23);\r\n\t\t\t\t\tadmod(g[i][j + 1][k][s][1], g[i ^ 1][j][k][0][s]);\r\n\t\t\t\t\tadmod(g[i][j][k + 1][s][2], g[i ^ 1][j][k][0][s]);\r\n\t\t\t\t\tadmod(g[i][j][k][s][0], g[i ^ 1][j][k][1][s] * 24);\r\n\t\t\t\t\tadmod(g[i][j][k + 1][s][2], g[i ^ 1][j][k][1][s]);\r\n\t\t\t\t\tadmod(g[i][j][k][s][0], g[i ^ 1][j][k][2][s] * 24);\r\n\t\t\t\t\tadmod(g[i][j + 1][k][s][1], g[i ^ 1][j][k][2][s]);\r\n\t\t\t\t}\r\n\t}\r\n\tfor(int i = 0; i <= n; i++)\r\n\t{\r\n\t\tfor(int j = 0; j <= 1; j++)\r\n\t\t\tfor(int k = 0; k <= 1; k++)\r\n\t\t\t\tadmod(sumf[i], f[n & 1][i][j][k]);\r\n\t\tif(i >= 1)\r\n\t\t\tsumf[i] = (sumf[i] + sumf[i - 1]) % mod;\r\n\t}\r\n\tfor(int i = 0; i <= n; i++)\r\n\t\tfor(int j = 0; j <= n; j++)\r\n\t\t{\r\n\t\t\tfor(int k = 0; k <= 2; k++)\r\n\t\t\t\tfor(int l = 0; l <= 2; l++)\r\n\t\t\t\t\tadmod(sumg[i][j], g[n & 1][i][j][k][l]);\r\n\t\t\tif(i >= 1)\r\n\t\t\t\tsumg[i][j] = (sumg[i][j] + sumg[i - 1][j]) % mod;\r\n\t\t\tif(j >= 1)\r\n\t\t\t\tsumg[i][j] = (sumg[i][j] + sumg[i][j - 1]) % mod;\r\n\t\t\tif(i >= 1 && j >= 1)\r\n\t\t\t\tsumg[i][j] = (sumg[i][j] - sumg[i - 1][j - 1] + mod) % mod;\r\n\t\t}\r\n\tint ans0 = 26 * 26 * fastpow(25, n - 2) % mod;\r\n\tint ans1 = 0;\r\n\tfor(int i = 1; i <= m; i++)\r\n\t\tans1 = (ans1 + GetSumF(c[i] + 1, n)) % mod;\r\n\tint ans2 = 0;\r\n\tfor(int i = 1; i <= m; i++)\r\n\t\tfor(int j = i + 1; j <= m; j++)\r\n\t\t\tans2 = (ans2 + GetSumG(c[i] + 1, n, c[j] + 1, n)) % mod;\r\n\tint ans = (ans0 - ans1 + ans2 + mod) % mod;\r\n\tprintf(\"%lld\\n\", ans);\r\n\treturn 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "fft"], "code_uid": "38b1caa9596c923f8c14383d28b62b91", "src_uid": "1f012349f4b229dc98faadf1ca732355", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nconst int mo=1000000007;\nint n,m,w,h,ans,res;\nint main(){\n\tscanf(\"%d%d\",&n,&m);\n\tfor(w=2;w<=n+1;++w)\n\t\tfor(h=2;h<=m+1;++h){\n\t\t\tint v=(w/2)*(h/2);\n\t\t\tans=(ans+(long long)(n+2-w)*(m+2-h)*((h%2==0 && w%2==0)?(4*(v-1)):(4*(h*w-1-v)-2*h-2*w)))%mo;\n\t\t\tint x=(w-1)/2,y=(h-1)/2;\n\t\t\tif(h&1 && w&1)res=(w-2)*(h-2);\n\t\t\tif(!(h&1) && !(w&1))res=x*(h-2-y)+(w-2-x)*y;\n\t\t\tif(!(h&1) && (w&1))res=(w-2-x)*(h-2);\n\t\t\tif((h&1) && !(w&1))res=(w-2)*(h-2-y);\n\t\t\tres-=__gcd(w-1,h-1)-1;\n\t\t\tans=(ans+2ll*res*(n+2-w)*(m+2-h))%mo;\n\t\t}\n\tprintf(\"%d\\n\",6ll*ans%mo);\n}\n", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "45021fb28d2bd79c7f055e7a9c24bfd9", "src_uid": "984788e4b4925c15c9c6f31e42f2f8fa", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\nusing namespace std;\n\nint main()\n{\n int n,x_floor,sum,diff_1,diff_2,diff_3,add=0;\n cin>>n;\n\n int arr[n];\n\n for(int i=0;i>arr[i];\n }\n\n for(int i=1;i<=n;i++)\n {\n int total=0,mini=INT_MAX;\n for(int j=1;j<=n;j++)\n {\n diff_1=abs(j-i)*2;\n diff_2=abs(i-1)*2;\n diff_3=(j-1)*2;\n sum=diff_1+diff_2+diff_3;\n\n //cout<\n#include\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 998244353;\nconst ll inv2 = 499122177;\nint main(){\n\tint n;scanf(\"%d\",&n);\n\tll dp1=inv2,dp2=inv2*inv2%mod;\n\tif (n==1){\n\t\tprintf(\"%lld\\n\",dp1);\n\t\treturn 0;\n\t}\n\tfor (int i=3;i<=n;++i){\n\t\tdp1 = inv2*((inv2*dp1%mod+dp2)%mod)%mod;\n\t\tswap(dp1,dp2);\n\t}printf(\"%lld\\n\",dp2);\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "9943ee0859e4cc4f638ef7e4aa4a85c3", "src_uid": "cec37432956bb0a1ce62a0188fe2d805", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\r\n#define ll long long\r\n#define mkp make_pair\r\n#define inf 0x3f3f3f3f\r\nusing namespace std;\r\nconst int mod=1e9+7;\r\nint a[100005];\r\nll qpow(ll a,ll b)\r\n{\r\n\tll sum=1;\r\n\twhile(b)\r\n\t{\r\n\t\tif(b&1)sum=(sum*a)%mod;\r\n\t\ta=a*a%mod;\r\n\t\tb>>=1;\r\n\t}\r\n\treturn sum;\r\n}\r\nint main()\r\n{\r\n\tll n,m,k,r,c;\r\n\tscanf(\"%lld%lld%lld%lld%lld\",&n,&m,&k,&r,&c);\r\n\tll ax,ay,bx,by;\r\n\tscanf(\"%lld%lld%lld%lld\",&ax,&ay,&bx,&by);\r\n\tll sum=1;\r\n\tif(ax>bx)\r\n\t{\r\n\t\tswap(ax,bx);\r\n\t\tswap(ay,by);\r\n\t}\r\n\tif(ay>by)\r\n\t{\r\n\t\tay=m-ay-c;\r\n\t\tby=m-by-c;\r\n\t}\r\n\t//printf(\"%lld %lld %lld %lld\\n\",ax,bx,ay,by);\r\n\tif(ax==bx&&ay==by)\r\n\t{\r\n\t\tprintf(\"%lld\\n\",qpow(k,n*m));\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintf(\"%lld\\n\",qpow(k,n*m-r*c));\r\n\t}\r\n } ", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "a3625a51bcbc8328c2d389cee2f8b606", "src_uid": "3478e6a4ff2415508fd517413d40c13a", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include \"bits/stdc++.h\"\r\nusing namespace std;\r\n#define all(x) begin(x),end(x)\r\ntemplate ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << \", \" << p.second << ')'; }\r\ntemplate::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = \" \"; return os; }\r\n#define debug(a) cerr << \"(\" << #a << \": \" << a << \")\\n\";\r\ntypedef long long ll;\r\ntypedef vector vi;\r\ntypedef vector vvi;\r\ntypedef pair pi;\r\nconst int mxN = 1e5+1, oo = 1e9;\r\nconst long long MD = 998244353;\r\ntemplate struct mdint {\r\n int d=0;\r\n mdint () {d=0;}\r\n mdint (long long _d) : d(_d%MOD){\r\n if(d<0) d+=MOD;\r\n };\r\n friend mdint& operator+=(mdint& a, const mdint& o) {\r\n a.d+=o.d; if(a.d>=MOD) a.d-=MOD;\r\n return a;\r\n }\r\n friend mdint& operator-=(mdint& a, const mdint& o) {\r\n a.d-=o.d; if(a.d<0) a.d+=MOD;\r\n return a;\r\n }\r\n friend mdint& operator*=(mdint& a, const mdint& o) {\r\n return a = mdint((ll)a.d*o.d);\r\n }\r\n mdint operator*(const mdint& o) const {\r\n mdint res = *this;\r\n res*=o;\r\n return res;\r\n }\r\n mdint operator+(const mdint& o) const {\r\n mdint res = *this;\r\n res+=o;\r\n return res;\r\n }\r\n mdint operator-(const mdint& o) const {\r\n mdint res = *this;\r\n res-=o;\r\n return res;\r\n }\r\n mdint operator^(long long b) const {\r\n mdint tmp = 1;\r\n mdint power = *this;\r\n while(b) {\r\n if(b&1) {\r\n tmp = tmp*power;\r\n }\r\n power = power*power;\r\n b/=2;\r\n }\r\n return tmp;\r\n }\r\n friend mdint operator/=(mdint& a, const mdint& o) {\r\n a *= (o^(MOD-2));\r\n return a;\r\n }\r\n mdint operator/(const mdint& o) {\r\n mdint res = *this;\r\n res/=o;\r\n return res;\r\n }\r\n bool operator==(const mdint& o) { return d==o.d;}\r\n bool operator!=(const mdint& o) { return d!=o.d;}\r\n friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}\r\n friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}\r\n};\r\nusing mint = mdint;\r\nint main() {\r\n int n; cin >> n;\r\n n++;\r\n vector dp(n+1),dp2(n+1);\r\n // dp[n] = number of non isomorphic binary trees with depth <= n\r\n // dp2[n] = number of non isomorphic binary trees with max depth = n.\r\n // depth = max number of nodes of any root to leaf path in the binary tree.\r\n dp[0]=1; // empty tree\r\n dp2[0]=1; // empty tree\r\n const mint halve = mint(1)/2;\r\n for(int i=1;i<=n;++i) {\r\n dp[i] = 1; // empty tree\r\n dp[i] += (dp[i-1]*(dp[i-1]+1))*halve; // all combinations of <=i-1 depth trees hanging underneath!\r\n dp2[i] = dp[i]-dp[i-1];\r\n }\r\n // if three trees hanging off root.\r\n mint one=dp2[n-1]*dp[n-2]*(dp[n-2]+1)/2; // one subtree has maximal depth\r\n mint two=dp2[n-1]*(dp2[n-1]+1)*halve*dp[n-2]; // two subtrees have depth of n-1\r\n mint three = dp2[n-1]*(dp2[n-1]+1)*(dp2[n-1]+2)/6; // all 3 subtrees have depth of n-1\r\n \r\n mint ans = (one+two+three)*2 -1;\r\n // need all trees which don't all connect to root.\r\n for(int i=1;i\n#include \n#include \nusing namespace std;\n\nconst int MAXN = 20;\n\nconst int c0[] = { 2, 1, 0, 1, 2 };\nconst int c1[] = { 6, 7, 8, 7, 6 };\nconst int di[] = { 1, 1, 0, 0, -1, -1 };\nconst int dj[] = { -1, 1, 2, -2, -1, 1 };\n\nint dp[1 << MAXN];\nchar ts[16], s[16][16], a[16][16];\n\nint gao() {\n\tint mask = 0, po = 0;\n\tfor (int i = 0; i < 5; ++i) {\n\t\tfor (int j = c0[i]; j <= c1[i]; j += 2) {\n\t\t\tif (s[i][j] == 'O') {\n\t\t\t\tmask |= (1 << po);\n\t\t\t}\n\t\t\t++po;\n\t\t}\n\t}\n\tint &ret = dp[mask];\n\tif (ret == -1) {\n\t\tret = 0;\n\t\tfor (int i = 0; i < 5 && !ret; ++i) {\n\t\t\tfor (int j = c0[i]; j <= c1[i] && !ret; j += 2) {\n\t\t\t\tif (s[i][j] != 'O')\n\t\t\t\t\tcontinue;\n\t\t\t\tfor (int k = 0; k < 6 && !ret; ++k) {\n\t\t\t\t\tint ii = i, jj = j;\n\t\t\t\t\twhile (ii >= 0 && ii < 5 && jj >= c0[ii] && jj <= c1[ii] && s[ii][jj] == 'O') {\n\t\t\t\t\t\ts[ii][jj] = 'X';\n\t\t\t\t\t\tii += di[k];\n\t\t\t\t\t\tjj += dj[k];\n\t\t\t\t\t\tif (!gao()) {\n\t\t\t\t\t\t\tret = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\twhile (ii != i || jj != j) {\n\t\t\t\t\t\tii -= di[k];\n\t\t\t\t\t\tjj -= dj[k];\n\t\t\t\t\t\ts[ii][jj] = 'O';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn ret > 0;\n}\n\n\nint main() {\n\tmemset(dp, -1, sizeof(dp));\n\tfor (int i = 0; i < 16; ++i)\n\t\tfor (int j = 0; j < 16; ++j)\n\t\t\ts[i][j] = '?';\n\tfor (int i = 0; i < 5; ++i) {\n\t\tfor (int j = c0[i]; j <= c1[i]; j += 2) {\n\t\t\tscanf(\"%s\", ts);\n\t\t\ts[i][j] = ts[0];\n\t\t}\n\t}\n\tprintf(\"%s\\n\", gao() ? \"Karlsson\" : \"Lillebror\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "games", "dp", "bitmasks", "implementation"], "code_uid": "166db1cdba96118c0ba826e48fef47a0", "src_uid": "eaa022cc7846c983a826900dc6dd919f", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main(void)\n{\n long long int n;\n cin>>n;\n int a=log2(n);\n cout<<(a+1)<\n#define ll long long\n#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)\nusing namespace std;\nconst int N = 105;\nint a[3], dp[N][N][N];\nstring s[3];\nvector f;\nvector computePrefix(string pat)\n{\n int m = pat.size();\n vector lp(m);\n for(int i = 1, k = 0; i < m; i++)\n {\n while(k > 0 && pat[k] != pat[i])\n k = lp[k - 1];\n if(pat[k] == pat[i])\n lp[i] = ++k;\n else\n lp[i] = k;\n }\n return lp;\n}\nint solve(int i, int j, int k)\n{\n if(k == a[2])\n return -1e9;\n if(i == a[0] || j == a[1])\n return 0;\n int &x = dp[i][j][k];\n if(x != -1)\n return x;\n int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9;\n if(s[0][i] == s[1][j])\n {\n int l = k;\n while(l > 0 && s[2][l] != s[0][i])\n l = f[l - 1];\n if(s[2][l] == s[0][i])\n ++l;\n z3 = 1 + solve(i + 1, j + 1 , l);\n }\n return x = max(z1, max(z2, z3));\n}\nvoid print(int i, int j, int k)\n{\n if(k == a[2])\n return;\n if(i == a[0] || j == a[1])\n return;\n int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9, x = -1e9, l = k;\n if(s[0][i] == s[1][j])\n {\n while(l > 0 && s[2][l] != s[0][i])\n l = f[l - 1];\n if(s[2][l] == s[0][i])\n ++l;\n z3 = 1 + solve(i + 1, j + 1 , l);\n }\n x = max(z1, max(z2, z3));\n if(x == z1)\n return print(i + 1, j, k);\n else if(x == z2)\n return print(i, j + 1, k);\n else\n {\n cout << s[0][i];\n print(i + 1, j + 1, l);\n }\n}\nint main()\n{\n IO;\n for(int i = 0; i < 3; i++)\n cin >> s[i],\n a[i] = s[i].size();\n f = computePrefix(s[2]);\n memset(dp, -1, sizeof dp);\n if(!solve(0, 0, 0))\n return cout << 0, 0;\n print(0, 0, 0);\n}", "lang_cluster": "C++", "tags": ["strings", "dp"], "code_uid": "fc29cdbc1540ab0bbcd147f181629106", "src_uid": "391c2abbe862139733fcb997ba1629b8", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int ZERO = 151;\nconst int MAXN = 305;\nint maxdep;\nint dr[] = {1,1,1,0,0,-1,-1,-1};\nint dc[] = {-1,0,1,-1,1,-1,0,1};\nint split_dir[][2] = {{1,3},{0,2},{1,4},{0,5},{2,7},{3,6},{5,7},{6,4}};\nbool split[MAXN], board[MAXN][MAXN], vis[MAXN][MAXN][MAXN][8];\nvoid recurse(int depth, int r, int c, int dir)\n{\n\tif(depth > maxdep)\n\t\treturn;\n\tboard[r][c] = true;\n\tif(vis[r][c][depth][dir])\n\t\treturn;\n\tvis[r][c][depth][dir] = true;\n\tif(split[depth])\n\t{\n\t\tint d1 = split_dir[dir][0], d2 = split_dir[dir][1];\n\t\tint r1 = r + dr[d1], c1 = c + dc[d1];\n\t\tint r2 = r + dr[d2], c2 = c + dc[d2];\n\t\trecurse(depth+1,r1,c1,d1);\n\t\trecurse(depth+1,r2,c2,d2);\n\t}\n\telse\n\t{\n\t\trecurse(depth+1,r + dr[dir],c + dc[dir],dir);\n\t}\n}\nint main()\n{\n\tint n;\n\tcin>>n;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tint x;\n\t\tcin>>x;\n\t\tmaxdep+=x;\n\t\tsplit[maxdep] = true;\n\t}\n\trecurse(1,ZERO,ZERO,1);\n\tint ans = 0;\n\tfor (int i = 0; i < MAXN; ++i)\n\t{\n\t\tfor (int j = 0; j < MAXN; ++j)\n\t\t{\n\t\t\tif(board[i][j])\n\t\t\t\tans++;\n\t\t}\n\t}\n\tcout<\n#include \n#include \n\nusing namespace __gnu_pbds;\nusing namespace std;\n\ntemplate \nusing ordered_set =\n tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n#define sim template < class c\n#define ris return * this\n#define dor > debug & operator <<\n#define eni(x) sim > typename \\\n enable_if(0) x 1, debug&>::type operator<<(c i) {\nsim > struct rge { c b, e; };\nsim > rge range(c i, c j) { return rge{i, j}; }\nsim > auto dud(c* x) -> decltype(cerr << *x, 0);\nsim > char dud(...);\nstruct debug {\n#ifdef LOCAL\n~debug() { cerr << endl; }\neni(!=) cerr << boolalpha << i; ris; }\neni(==) ris << range(begin(i), end(i)); }\nsim, class b dor(pair < b, c > d) {\n ris << \"(\" << d.first << \", \" << d.second << \")\";\n}\nsim dor(rge d) {\n *this << \"[\";\n for (auto it = d.b; it != d.e; ++it)\n *this << \", \" + 2 * (it == d.b) << *it;\n ris << \"]\";\n}\n#else\nsim dor(const c&) { ris; }\n#endif\n};\n#define imie(...) \" [\" << #__VA_ARGS__ \": \" << (__VA_ARGS__) << \"] \"\n\n#define shandom_ruffle random_shuffle\n\nusing ll=long long;\nusing pii=pair;\nusing pll=pair;\nusing vi=vector;\nusing vll=vector;\nconst int nax=1000*1007;\n\nint n, m;\n\nint s;\nll t;\n\nll bylem[nax];\n\nvoid ruch()\n{\n\tif (s\nusing namespace std;\n#define loop(i,b,e) for(int i=b;i<=e;i++)\n#define loop2(i,e,b) for(int i=e;i>=b;i--)\n#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\n#define fi first\n#define se second\n#define X real()\n#define Y imag()\n#define cp(a,b)\t( (conj(a)*(b)).imag() )\n#define length(a) (hypot((a).imag(), (a).real()))\ntypedef complex point;\ntypedef long long ll;\ntypedef unsigned long long ull;\ndouble PI = 3.141592653589793;\nconst double EPS = 1e-9;\nconst int N = 1e6 + 5;\nconst ll mod = 1e9 + 7;\nconst int oo = 1e9;\nint dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };\nint dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };\n\nll n;\nvector v;\n\nint main() {\n\tscanf(\"%lld\", &n);\n\tfor (ll i = 1; i * i <= n; i++) {\n\t\tif (n % i == 0) {\n\t\t\tv.push_back(i);\n\t\t\tif (i * i != i) v.push_back(n / i);\n\t\t}\n\t}\n\tll mx = -1, mn = 1e18;\n\tfor (int i = 0; i < v.size(); i++) {\n\t\tfor (int j = 0; j < v.size(); j++) {\n\t\t\tll c = n / (v[i] * v[j]);\n\t\t\tif (n % (v[i] * v[j]) != 0) continue;\n\t\t\tll x = (v[i] + 1) * (v[j] + 2) * (c + 2);\n\t\t\tmx = max(mx, x - n);\n\t\t\tmn = min(mn, x - n);\n\t\t\tx = (v[i] + 2) * (v[j] + 1) * (c + 2);\n\t\t\tmx = max(mx, x - n);\n\t\t\tmn = min(mn, x - n);\n\t\t\tx = (v[i] + 2) * (v[j] + 2) * (c + 1);\n\t\t\tmx = max(mx, x - n);\n\t\t\tmn = min(mn, x - n);\n\t\t}\n\t}\n\tprintf(\"%lld %lld\", mn, mx);\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "80abf606907ad63d3d503ccfb7009340", "src_uid": "2468eead8acc5b8f5ddc51bfa2bd4fb7", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include\r\nusing namespace std;\r\nconst int N = 1e5 + 5;\r\nconst int P = 1e9 + 7;\r\nint phi[N];\r\n\r\nvoid initPhi() {\r\n for (int i = 2;i < N;i++) {\r\n if (!phi[i]) {\r\n for (int k = i;k < N;k += i) {\r\n if (!phi[k]) phi[k] = k;\r\n phi[k] = phi[k] / i * (i - 1);\r\n }\r\n }\r\n }\r\n}\r\nvector factor[N];\r\nvoid initFactor() {\r\n for (int i = 1; i < N; i++) {\r\n for (int j = i; j < N; j += i) {\r\n factor[j].push_back(i);\r\n }\r\n }\r\n}\r\n\r\nint gcd(int a, int b) {\r\n return b ? gcd(b, a % b) : a;\r\n}\r\n\r\nvoid solve() {\r\n initPhi();\r\n initFactor();\r\n\r\n int n;\r\n cin >> n;\r\n int64_t ans = 0;\r\n for (int c = 1; c < n - 1; c++) {\r\n for (int d : factor[n - c]) {\r\n ans += 1LL * c * d / gcd(c, d) * phi[(n - c) / d] % P;\r\n }\r\n }\r\n cout << ans % P << endl;\r\n}\r\n\r\nint main(){\r\n solve();\r\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "d42a83a870cc7113b43d358a45f6a43c", "src_uid": "c3694a6ff95c64bef8cbe8834c3fd6cb", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\nconst int N = 70;\nconst int MOD = 1000000007;\nLL n , t , dp[N][N];\nint p = 0;\nLL dfs (int l , int c , int limit , LL n) {\n if (l < 0) return c == p;\n if (!limit && dp[l][c] != -1) return dp[l][c];\n int up = limit ? ((n >> l) & 1) : 1;\n LL ret = 0;\n for (int i = 0 ; i <= up ; i ++) {\n ret += dfs (l - 1 , c + i , limit && i == up , n);\n }\n if (!limit) dp[l][c] = ret;\n return ret;\n}\nLL gao (LL n) {\n int t = 0;\n while ((1LL << t) < n) t ++;\n return dfs (t , 0 , 1 , n);\n}\nint main(){\n#ifndef ONLINE_JUDGE \n freopen (\"input.txt\" , \"r\" , stdin);\n // freopen (\"output.txt\" , \"w\" , stdout);\n#endif \n cin >> n >> t;\n if ((t & (-t)) != t) {\n puts (\"0\");\n return 0;\n }\n while ((1LL << p) < t) p ++;\n p ++;\n memset (dp , -1 , sizeof (dp));\n cout << gao (n + 1) - gao (1) << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "6aa46b1a2c694ebf26e2a2660ef15b23", "src_uid": "727d5b601694e5e0f0cf3a9ca25323fc", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "////////////////////////////////////////////////////////////////////////////////////////////////////\n//░▐█▀█▒▐█▀▀█▌▒▐██▄▒▄██▌░▐█▀▀/////▒█▀█▀█▒▐█▀▀█▌/////▒█▀█▀█░▐█░▐█░▐█▀▀///////////////////////////////\n//░▐█──▒▐█▄▒█▌░▒█░▒█░▒█░░▐█▀▀/////░░▒█░░▒▐█▄▒█▌/////░░▒█░░░▐████░▐█▀▀///////////////////////////////\n//░▐█▄█▒▐██▄█▌▒▐█░░░░▒█▌░▐█▄▄/////░▒▄█▄░▒▐██▄█▌/////░▒▄█▄░░▐█░▐█░▐█▄▄///////////////////////////////\n////////////////////////////////////////////////////////////////////////////////////////////////////\n//░▐█▀█▄─░▄█▀▄─▒▐█▀▀▄▒▐█▒▐▀/////▒▄█▀▀█░▐██░▐█▀█▄░▐█▀▀///////////////////////////////////////////////\n//░▐█▌▐█░▐█▄▄▐█▒▐█▒▐█▒▐██▌░/////▒▀▀█▄▄─░█▌░▐█▌▐█░▐█▀▀///////////////////////////////////////////////\n//░▐█▄█▀░▐█─░▐█▒▐█▀▄▄▒▐█▒▐▄/////▒█▄▄█▀░▐██░▐█▄█▀░▐█▄▄///////////////////////////////////////////////\n////////////////////////////////////////////////////////////////////////////////////////////////////\n////////////////////////////////////////////////////////////////////////////////////////////////////\n////////////////////////////////////////////////////////////////////////////////////////////////////\n//////////////////////////////////////////+o+/+/-----...--:::///////////////////////////////////////\n////////////////////////////////////::++ohss+///+ooooo:` `...-://///////////////////////////////\n/////////////////////////////////:+sdd/` :o. `-ohhs+/:://///////////////////////////\n////////////////////////////////smdh+` `o: `.://+yy/oshy+:://////////////////////////\n/////////////////////////////:sms-.` .s. `:oydmmmmmmmd `-oyy/::///////////////////////\n///////////////////////////:/do` . :y` .sdmmmmmmmmmmmd `:sho:://///////////////////\n/////////////////////////:./s. . :y` -dmmmmmmmmmmmmm: .oho:////////////////////\n////////////////////////:`-/ -h` `ommmmmmmmmmmmmms -yh////////////////////\n///////////////////////:` . `. .y` /dmmmmmmmmmmmmmmm- `sd///////////////////\n//////////////////////: `/`h/ `s. ommmmmmmmmmmmmmmmh `yh://///////////////\n/////////////////////:` .hhyMs -. /sdmmmmmmh:.....-/ /d+/////////////////\n////////////////////:::` `dNhMM/ -dmmmmmd+` :ds:////////////////\n//////////////////:.:-` .yNMN`` `` :dmmmmmmmmdo- :dy:////////////////\n////////////////:-``` .NMMs--::.` ``` `-/ymmmmmmmmo::/. :dy:////////////////\n///////////////:. ` `.//-``.`...--:--.` `./ymmmmd` /dy:////////////////\n///////////////. . .-``` .-.....-://:--`` -odms +ds:////////////////\n/////////////+: /` :ys:--o+` ./osyyyyys+-:-` -hs +d+/////////////////\n/////////////+ y` .yy:-.:/- `yhhhhhyyyy/ ``-. .s- od//////////////////\n///////////:o` o` :ho`..-`` :hhhhhhhhy: .. `+` yd-/////////////////\n///////////+- -/ :/.-` .. ohhhhhyhy- `:.` `/ `+dh-/////////////////\n//////////:o +` .` --`-/oyyyys- /o-- .- -dds://///////////////\n///////////+ .+so+. `.:-.-::.. `:////` `` -. `ddo://///////////////\n//////////o- `.--yssshs:`+oo: -/:--.`````````` /` ody://///////////////\n//////////y :hhdyydmmmo.`/os: :hhyyso/:. +` .dd//////////////////\n/////////o/ -ddhy-.-++y` ``.` /hhhyhhhs` .+ ody:////////////////\n////////:y. `dhy: :s` ..-syhyyhs` ``` :: `hd+////////////////\n/////////s sy: ` .`/o ..:oyyhs. ````` o` /dy:///////////////\n////////o: o/ `.`.`++++ `:/-:+oy- ```````` .+ yd+///////////////\n///////:y` +/ `.`y/.h`+h/ `/++`:++.-:::-. +. -dh://////////////\n///////++ `..+o...y.+s`y:-h.y- `.---+//:.` o sd+//////////////\n///////o-`yyy+yyhyyhodssd+ys/dh:-.////-``...````````````````` -: .dh://///////////\n///////:.`//::://++oosssyyyyydmmm-..........`````` :- /do:////////////\n/////////:-` `.`.:.-::--::--.. .``` yd/////////////\n////////////:-` .-....:+/://:` ` -ds:///////////\n///////////////:-.` `---.-//-// ``` od////////////\n///////////////////:-.../-.`::` ```` ````` `hy://////////\n////////////////////////:os-.`` ```` ``` .-:::::://- ` :d+//////////\n///////////////////////////- ``` ` .--.. ``` :y//////////\n///////////////////////////:. .:-. `` .//////////\n////////////////////////////- :/- /////////\n////////////////////////////. .:. `-/////////\n///////////////////////////o .........----:////////////\n//////////////::--.....``-: ./////////////////////////\n/////////:-.` . .--` /+////////////////////////\n////////////////////////////////////////////////////////////////////////////////////////////////////\n#include\n#include \n#include \n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include \n#pragma GCC optimize (\"O2\")\n#pragma GCC optimize (\"unroll-loops\")\n\n//███╗ ███╗ █████╗ ██████╗ ██╗ ██████╗\n//████╗ ████║██╔══██╗██╔════╝ ██║██╔════╝\n//██╔████╔██║███████║██║ ███╗██║██║\n//██║╚██╔╝██║██╔══██║██║ ██║██║██║\n//██║ ╚═╝ ██║██║ ██║╚██████╔╝██║╚██████╗\n//╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝\n\n#define IOS ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);\n#define endl std::cout<<\"\\n\";\n#define pause_cout \"std::cout << \"|| ------------------------------------------- ||\"\"\n#define mp make_pair\n#define pb push_back\n#define ff first\n#define ss second\n#define iter iterator\ntypedef long long ll;\ntypedef int ii;\ntypedef long double ld;\nconst ll INF = 1e18;\nconst ll INF_bit = 1e9 - 1;\nconst ld EPS = 0.000000001;\ntypedef unsigned long long ull;\ntypedef std::string str;\ntypedef std::vector vec_chr;\ntypedef std::vector vec_ld;\ntypedef std::vector vec;\ntypedef std::vector vecs;\ntypedef std::bitset bit;\ntypedef std::vector> pvec;\ntypedef std::vector> vec2;\ntypedef std::vector>> vec2_p;\ntypedef std::map map_s_l;\ntypedef std::map map_l_l;\ntypedef std::pair p_l_l;\n#define create_Graph vec2 GR(n,vec())\n#define create_Matrix_GR vec2 GR(n,vec(n,INF))\n#define rfor(i,n) for(int i=n-1;i>=0;i--)\n#define all(x) (x).begin(),(x).end()\n#define for_0(i,n) for(int i=0;i>x)\n#define YES std::cout << \"YES\"\n#define NO std::cout << \"NO\"\n#define write(x) (std::cout< b;\nll gcd(ll a, ll b) {\n\tif (a == 0)return b;\n\tif (a > b)std::swap(a, b);\n\treturn gcd(b % a, a);\n}\nvec_chr prime(110 + 1, true);\n//ll Recheto() {\n//\tprime[0] = prime[1] = false;\n//\tfor_2(i, 2, 110)\n//\t\tif (prime[i])\n//\t\t\tif (i * 1ll * i <= 110)\n//\t\t\t\tfor_3(j, i * j, 110, i)\n//\t\t\t\tprime[j] = false;\n//}\n\nint main() {\n\tll a1, a2, k1, k2, n;\n\tstd::cin >> a1 >> a2 >> k1 >> k2 >> n;\n\tif (k1 > k2) {\n\t\tll f = a1;\n\t\ta1 = a2;\n\t\ta2 = f;\n\t\tf = k1;\n\t\tk1 = k2;\n\t\tk2 = f;\n\t}\n\tll k = std::max(ll(0), ll(n - a1 * (k1 - 1) - a2 * (k2 - 1)));\n\tif (n <= a1 * k1) {\n\t\tstd::cout << k << ' ' << n / k1;\n\t}\n\telse {\n\t\tstd::cout << k << ' ' << a1 + (n - a1 * k1) / k2;\n\t}\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "implementation"], "code_uid": "ffce72903d23e79315adbb78a8273279", "src_uid": "2be8e0b8ad4d3de2930576c0209e8b91", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\n\n#define forn(i, a, n) for (int i = a; i < n; ++i)\n#define ford(i, a, n) for (int i = n - 1; i >= a; --i)\n#define fore(i, a, n) for (int i = a; i <= n; ++i)\n#define all(a) (a).begin(), (a).end()\n#define fs first\n#define sn second\n#define trace(a)\\\n for (auto i : a) cerr << i << ' ';\\\n cerr << '\\n'\n#define eb emplace_back\n\n#ifndef M_PI\nconst ld M_PI = acos(-1.0);\n#endif\n\nconst ld eps = 1e-9;\nconst int INF = 2000000000;\nconst ll LINF = 1ll * INF * INF;\nconst ll MOD = 1000000007;\n\nconst int N = 2017;\nll dp[N][N];\nll sum[N][N];\n\nll Add(ll x, ll y) {\n x += y;\n if (x >= MOD) x -= MOD;\n return x;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n forn(i, 0, N) dp[0][i] = 0;\n forn(i, 0, N) sum[0][i] = 0;\n forn(i, 1, N) {\n dp[i][2] = Add(sum[i - 1][2], 1);\n forn(j, 3, N)\n dp[i][j] = Add(dp[i][j - 1], sum[i - 1][j]);\n sum[i][0] = dp[i][0];\n forn(j, 1, N)\n sum[i][j] = Add(sum[i][j - 1], dp[i][j]);\n }\n int n, m;\n cin >> n >> m;\n ll ans = 0;\n forn(i, 0, n)\n fore(j, 2, m) {\n ans += (m - j + 1) * dp[i + 1][j] % MOD * (dp[n - i][j] - dp[n - i - 1][j] + MOD);\n ans %= MOD;\n }\n cout << ans << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "b8e1b5a2ca474ade3babb5bd25cd20ec", "src_uid": "9ca1ad2fa16ca81e9ab5eba220b162c1", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#pragma comment(linker, \"/STACK:167177216\")\n\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\ntypedef long long li;\ntypedef long double ld;\n\n#define forn(i, n) for (int i = 0; i < int(n); ++i)\n#define pb push_back\n#define mp make_pair\n\nint t2(int a, int b)\n{\n int k = 1;\n forn (i, 100)\n {\n k *= a;\n if(k % b == 0)\n return i + 1;\n k %= b;\n }\n return 0;\n}\n\nbool t3(int a, int b)\n{\n if((a % b) == 1)\n return true;\n return false;\n}\n\nbool t11(int a, int b)\n{\n if(a % b == 1 && a * a % b == b - 1)\n return true;\n if(a % b == b - 1 && a * a % b == 1)\n return true;\n return false;\n}\n\nbool t6(int a, int b)\n{\n int a2 = 0, a3 = 0, a11 = 0;\n for (int i = 2; i * i <= b; ++i)\n {\n if(b % i == 0)\n {\n int perm = 1;\n while(b % i == 0)\n {\n perm *= i;\n b /= i;\n }\n bool ok = false;\n if(t2(a, perm))\n ok = a2 = 1;\n if(t3(a, perm))\n ok = a3 = 1;\n if(t11(a, perm))\n ok = a11 = 1;\n if(!ok)\n return false;\n }\n }\n if(b > 1)\n {\n bool ok = false;\n int perm = b;\n if(t2(a, perm))\n ok = a2 = 1;\n if(t3(a, perm))\n ok = a3 = 1;\n if(t11(a, perm))\n ok = a11 = 1;\n if(!ok)\n return false;\n }\n return a2 + a3 + a11 >= 2;\n}\n\nint main()\n{\n \n int a, b;\n cin >> a >> b;\n\n if(t2(a, b))\n cout << \"2-type\" << endl << t2(a, b) << endl;\n else\n if(t3(a, b))\n cout << \"3-type\" << endl;\n else\n if(t11(a, b))\n cout << \"11-type\" << endl;\n else\n if(t6(a, b))\n cout << \"6-type\" << endl;\n else\n cout << \"7-type\" << endl;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "33c6c9eb4406c0abc572fc4870d7a8e7", "src_uid": "809e1c78b0a5a16f7f2115b046a20bde", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1e9 + 7;\nll qpow( ll x , ll y)\n{\n ll ans = 1;\n x %= mod;\n while( y ){\n if( y & 1 )\n ans = ans * x % mod;\n x = x * x % mod;\n y >>= 1;\n }\n return ans;\n}\ntypedef vector VI;\nnamespace linear_seq{\n const int n = 10010;\n ll res[n], base[n], c[n], md[n];\n vector < int > Md;\n void mul( ll *a, ll *b, int k){\n for ( int i = 0; i < k + k; i++){\n c[i] = 0;\n }\n for ( int i = 0; i < k; i++){\n if ( a[i] ){\n for (int j = 0; j < k; j++){\n c[ i + j ]=( c[ i + j ] + a[i] * b[j] ) % mod;\n }\n }\n }\n for (int i = k + k - 1; i >= k; i--){\n if ( c[i] ){\n for (int j = 0; j < (int)Md.size(); j++){\n c[ i - k + Md[j] ] = ( c[ i - k + Md[j] ] - c[i] * md[ Md[j] ]) % mod;\n }\n }\n }\n for (int i = 0; i < k; i++){\n a[i] = c[i];\n }\n }\n\n ll solve( ll n, VI a, VI b){\n ll ans = 0, pnt = 0;\n int k = (int)a.size();\n assert( (int)a.size() == (int)b.size() );\n for (int i = 0; i < k; i++){\n md[ k - 1 - i] = - a[i];\n }\n md[k] = 1;\n Md.clear();\n for ( int i = 0; i < k; i++){\n if ( md[i] != 0){\n Md.push_back(i);\n }\n }\n for ( int i = 0; i < k; i++){\n res[i] = base[i] = 0;\n }\n res[0] = 1;\n while ( ( 1ll << pnt ) <= n ){\n pnt++;\n }\n for (int p = pnt; p >= 0; p--){\n mul( res, res, k);\n if ( ( n>>p ) & 1){\n for (int i = k - 1; i >= 0; i--){\n res[ i + 1 ]=res[i];\n }\n res[0] = 0;\n for (int j = 0; j< (int) Md.size(); j++){\n res[ Md[j] ]=( res[ Md[j] ]- res[k] * md[ Md[j] ] ) % mod;\n }\n }\n }\n for (int i = 0; i < k; i++){\n ans=( ( ans + res[i] * b[i] ) % mod + mod ) % mod;\n }\n return ans;\n }\n VI BM( VI s){\n VI C( 1 , 1 ), B( 1 , 1 );\n int L = 0, m = 1, b = 1;\n for ( int n = 0; n < (int)s.size(); n++){\n ll d = 0;\n for (int i = 0; i < L + 1; i++){\n d=( d + (ll)C[i] * s[ n - i ] ) % mod;\n }\n if ( d == 0 ){\n m++;\n }\n else if ( 2 * L <= n ){\n VI T = C;\n ll c = mod - d*qpow( b, mod - 2 ) % mod;\n while ( (int)C.size() < (int)B.size() + m){\n C.push_back(0);\n }\n for ( int i = 0; i < (int)B.size(); i++){\n C[ i + m ] = ( C[ i + m ] + c * B[ i ] ) % mod;\n }\n L = n + 1 - L;\n B = T;\n b = d;\n m = 1;\n }\n else {\n ll c = mod - d * qpow( b , mod - 2 ) % mod;\n while ( (int)C.size() < (int)B.size() + m ){\n C.push_back(0);\n }\n for (int i = 0; i < (int)B.size(); i++){\n C[ i + m ]= ( C[ i + m ] + c * B[ i ] ) % mod;\n }\n m++;\n }\n }\n return C;\n }\n ll gao( VI a, ll n){\n VI c = BM( a );\n c.erase( c.begin() );\n for (int i = 0; i < (int)c.size(); i++){\n c[i]= ( mod - c[i] )%mod;\n }\n return solve( n, c, VI( a.begin(), a.begin() + (int)c.size() ) );\n }\n};\nll f[300];\nint main()\n{\n int m;\n ll n;\n scanf(\"%lld%d\",&n,&m);\n VI a;\n for(int i=1;i<=m;i++){\n f[i]=1;\n }\n for(int i=m;i<=200;i++){\n f[i]=(f[i-1]+f[i-m])%mod;\n }\n for(int i=1;i<=200;i++){\n a.push_back(f[i]);\n }\n n++;\n ll ans=linear_seq::gao(a,n-1);\n printf(\"%lld\\n\",ans%mod);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "matrices", "dp"], "code_uid": "4a0bb3c6458f9b7aec6be7f623ee01f7", "src_uid": "e7b9eec21d950f5d963ff50619c6f119", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define endl \"\\n\"\n#define ll long long int\n#define vi vector\n#define vll vector\n#define vvi vector < vi >\n#define pii pair\n#define pll pair\n#define mod 1000000007\n#define inf 1000000000000000001;\n#define all(c) c.begin(),c.end()\n#define mp(x,y) make_pair(x,y)\n#define mem(a,val) memset(a,val,sizeof(a))\n#define eb emplace_back\n#define f first\n#define s second\n\t\t\nusing namespace std;\nint main()\n{\n int t;\n\n cin>>t;\n\n int n=t;\n\n int p1=1;\n int p2=2;\n int s=3;\n\n int a[t];\n\n for(int i=0;i>a[i];\n }\n\n if(a[0]==3) {\n \tcout<<\"NO\";\n \treturn 0;\n }\n\n for(int i=0;i\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef int INT;\n#define int long long\n#define x first\n#define y second\ntypedef pair PR;\nvector a[20];\n#define MK make_pair\n#define PB push_back\nconst int Mx=(1<<16)-1;\nint mod=10;\nint L=60;\nint n;\nint mul(int x,int y){\n\tint ret=0;\n\tfor(int i=0;i<3;i++){\n\t\tret+=(x&Mx)*y%mod;\n\t\tx>>=16;\n\t\ty=(y<<16)%mod;\n\t}\n\treturn ret%mod;\n}\nPR Calc(int x){\n\tif(x==1)return MK(0,1);\n\tPR A=Calc(x&1?x-1:x/2);\n//\tcout<>n;\n\tfor(int i=1;i<=60;i++)if(Calc(i).x==n%mod) a[0].PB(i);\n\tfor(int t=0;t<12;t++){\n\t\tif(a[t].size()==0)break;\n\t\tmod=mod*10;\n\t\tfor(int k=0;k\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define MAX 16\n\nusing namespace std;\nFILE *in; FILE *out;\n\nstruct Edge\n{\n int lo, hi, prc;\n};\n\nint n, all, flow, cur;\nint cap[MAX];\nEdge edges[MAX][MAX];\n\nvoid recurse(int node, int next, int price)\n{\n if (node == n - 1)\n {\n if (cap[node] == flow)\n cur = max(cur, price);\n return;\n }\n \n if (next >= n)\n {\n if (cap[node] == 0)\n recurse(node + 1, node + 2, price);\n }\n else\n {\n for (int i = edges[node][next].lo; i <= edges[node][next].hi; i++)\n {\n if (cap[node] < i) break;\n cap[node] -= i; cap[next] += i;\n int nprice = price + i * i;\n if (i > 0) nprice += edges[node][next].prc;\n recurse(node, next + 1, nprice);\n cap[node] += i; cap[next] -= i;\n }\n }\n}\n\nint eval()\n{\n for (int i = 0; i < n; i++)\n cap[i] = 0;\n cap[0] = flow;\n cur = -1;\n recurse(0, 1, 0);\n return cur;\n}\n\nint main(void)\n{\n in = stdin; out = stdout;\n// in = fopen(\"C.in\", \"rt\"); out = fopen(\"C.out\", \"wt\");\n \n fscanf(in, \"%d\", &n);\n all = (n * (n - 1)) / 2;\n for (int i = 0; i < all; i++)\n {\n int n1, n2;\n fscanf(in, \"%d %d\", &n1, &n2);\n n1--; n2--;\n fscanf(in, \"%d\", &edges[n1][n2].lo);\n fscanf(in, \"%d\", &edges[n1][n2].hi);\n fscanf(in, \"%d\", &edges[n1][n2].prc);\n }\n \n int ans = -1, best = -1;\n for (flow = 0; flow <= 25; flow++)\n {\n best = eval();\n// cout << \"Eval with flow = \" << flow << \" gives \" << best << endl;\n// system(\"pause\");\n if (best != -1) {ans = flow; break;}\n }\n fprintf(out, \"%d %d\\n\", ans, best);\n \n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "7633fb842ad1cf9cf92c5da4c574d0b7", "src_uid": "38886ad7b0d83e66b77348be34828426", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\nusing namespace std;\nint xp , yp , xv , yv;\nint cal(int x){\n\treturn (x >= 0 ? x : -x);\n}\nmain(){\n//\tfreopen(\"test.inp\",\"r\",stdin);\n\tscanf(\"%d %d %d %d\",&xp,&yp,&xv,&yv);\n\tif(xv + yv < xp + yp) printf(\"Vasiliy\");\n\telse{\n\t\tint dia1 = xv - yv;\n\t\tint dia2 = xp - yp;\n\t\tbool can_reach = true;\n\t\tint step = cal(dia1 - dia2);\n\t\tif(dia1 > dia2 && yp < step) can_reach = false;\n\t\tif(dia2 > dia1 && xp < step) can_reach = false;\n\t\tif(!can_reach) printf(\"%s\", max(xv , yv) < xp + yp ? \"Vasiliy\" : \"Polycarp\");\n\t\telse{\n\t\t\tint l1 = xp + yp - step;\n\t\t\tint l2 = xv + yv - (step <= min(xv , yv) ? 2*step : step + min(xv , yv));\n\t\t\tprintf(\"%s\",(l1 <= l2) ? \"Polycarp\" : \"Vasiliy\");\n\t\t}\n\t}\n\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "implementation", "games"], "code_uid": "4f7fe820379deac501f7fa7c204e077b", "src_uid": "2637d57f7809ff8f922549c617709074", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#define LL long long\nusing namespace std;\nLL gao(LL l1,LL r1,LL l2,LL r2)\n{\n if(l1==l2&&r1==r2)\n return 0;\n if(r1<=l2)\n return l2-r1+1;\n else\n return l1-r2+1;\n}\nlong long L(LL x,LL m)\n{\n if(x>0)\n {\n LL y=x%m;\n return (x-y)/m;\n }\n else\n {\n x=-x;\n LL y=x%m;\n return -(x+(m-y))/m;\n }\n}\nlong long R(long long x,LL m)\n{\n if(x>0)\n {\n LL y=x%m;\n return (x+m-y)/m;\n }\n else\n {\n x=-x;\n LL y=x%m;\n return -(x-y)/m;\n }\n}\nint main()\n{\n long long a,b,x1,y1,x2,y2;\n cin>>a>>b>>x1>>y1>>x2>>y2;\n a*=2,b*=2;\n LL u1=R(x1+y1,a);\n LL d1=L(x1+y1,a);\n LL u2=R(x2+y2,a);\n LL d2=L(x2+y2,a);\n LL ans=gao(d1,u1,d2,u2);\n u1=R(y1-x1,b);\n d1=L(y1-x1,b);\n u2=R(y2-x2,b);\n d2=L(y2-x2,b);\n ans=max(ans,gao(d1,u1,d2,u2));\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define YOU using\n#define DONT namespace\n#define SAY std\nYOU DONT SAY;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pli;\ntypedef pair pil;\ntypedef vector vi;\ntypedef vector vii;\ntypedef vector vll;\n#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)\n#define REP(i,a,b) REPP(i,a,b,1)\n#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)\n#define REV(i,a,b) REVV(i,a,b,1)\n#define FOR(i,a) for(int i=0; i=0; i--)\n#define mp make_pair\n#define pb push_back\n#define ff first\n#define ss second\nconst int OO = 1e9;\nconst ll INF = 1e18;\nconst int irand(int lo,int hi){\n\treturn ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;\n}\nconst ll lrand(ll lo,ll hi){\n\treturn ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;\n}\n#define getc getchar\ntemplate\nT getnum(){\n\tint sign = 1;\n\tT ret = 0;\n\tchar c;\n\tdo{\n\t\tc = getc();\n\t}while(c == ' ' || c == '\\n');\n\tif(c == '-')sign = -1;\n\telse ret = c-'0';\n\twhile(1){\n\t\tc = getc();\n\t\tif(c < '0' || c > '9')break;\n\t\tret = 10*ret + c-'0';\n\t}\n\treturn sign * ret;\n}\ninline void ini(int& x){\n\tx = getnum();\n}\ninline void scani(int& x){\n\tscanf(\"%d\",&x);\n}\nint n,m;\nint solve(int m, int n){\n\tif(n == 1)return -1;\n else if(n == 2 && m > 3)return -1;\n else if(n == 3 && m > 5)return -1;\n else if(n == 2 && m == 2)return 5;\n else if(m == 3 && n == 2)return 11;\n else if(n >= 2*m)return 1;\n else if(n > m && n < 2*m)return 3;\n else if(n == 3){\n \tif(m == 3)return 5;\n \telse if(m == 4)return 9;\n \telse if(m == 5)return 11;\n\t}else{\n\t\tint n7 = (n-1)/2;\n\t\tint nl = (n-2)/2;\n\t\t\n\t\tm -= n;\n\t\tm++;\n\t\tint curr = 5;\n\t\twhile(1){\n\t\t\tif(curr == 7)m -= n7;\n\t\t\telse m -= nl;\n\t\t\tif(m <= 0)return curr;\n\t\t\tcurr += 2;\n\t\t}\n\t}\n}\nint main(){\n\tcin>>m>>n;\n cout << solve(m, n) << endl;\n return 0;\n}\n \t", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "8ac7901515274d45ecad4b14ee446760", "src_uid": "83f1d50a1802e08dd154d4c9778e3d80", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\nusing ull = unsigned long long;\nconst int maxn = 111;\nint n;\nint p[maxn] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};\null ret;\n\nvoid dfs(int depth, ull cur, int cnt, int hi) {\n if(depth >= 10 || cnt > n) return;\n if(cnt == n) {\n ret = min(ret, cur);\n return;\n }\n for(int i = 1; i <= hi; i++) {\n if(cnt * (i + 1) > n) break;\n cur *= (ull)p[depth];\n dfs(depth+1, cur, cnt*(i+1), i);\n }\n}\n\nsigned main() {\n // freopen(\"in\", \"r\", stdin);\n while(~scanf(\"%d\",&n)) {\n ret = 1E18;\n dfs(1, 1, 1, 60);\n cout << ret << endl;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp", "number theory"], "code_uid": "6f129d02546ddc8d56c2266b132aecc1", "src_uid": "62db589bad3b7023418107de05b7a8ee", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "//#pragma comment(linker, \"/STACK:134217728\")\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long Int;\ntypedef pair PII;\ntypedef vector VInt;\n\n#define FOR(i, a, b) for(i = (a); i < (b); ++i)\n#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)\n#define CLEAR(a, b) memset(a, b, sizeof(a))\n#define SIZE(a) int((a).size())\n#define ALL(a) (a).begin(),(a).end()\n#define PB push_back\n#define MP make_pair\n\nInt A[1 << 20];\n\nInt f(Int n)\n{\n\tif(n % 3 != 0)\n\t\treturn 0;\n\n\tint i, j;\n\tn /= 3;\n\tInt res = 0;\n\tInt c;\n\tint cnt = 0;\n\tfor(c = 2; c*c < n; ++c)\n\t\tif(n % c == 0)\n\t\t{\n\t\t\tA[cnt] = c;\n\t\t\t++cnt;\n\t\t}\n\n\tFOR(i, 0, cnt)\n\t\tFOR(j, 0, i + 1)\n\t\t{\n\t\t\tInt r = 0;\n\t\t\tInt ab = A[i];\n\t\t\tInt ac = A[j];\n\t\t\tInt bc = ab - ac;\n\n\t\t\tInt t = (ab + ac)*(ab + ac) - bc*bc;\n\t\t\tif((4*n) % t == 0)\n\t\t\t{\n\t\t\t\tbc = 4*n/t;\n\t\t\t\tInt d = (ab + bc + ac);\n\t\t\t\tif(d % 2 != 0)\n\t\t\t\t\tcontinue;\n\n\t\t\t\td /= 2;\n\t\t\t\tInt a = d - bc;\n\t\t\t\tInt b = d - ac;\n\t\t\t\tInt c = d - ab;\n\t\t\t\tif(a <= 0 || b <= 0 || c <= 0)\n\t\t\t\t\tcontinue;\n\n\t\t\t\tif(d*d*d - a*a*a - b*b*b - c*c*c == 3*n)\n\t\t\t\t\t++r;\n\t\t\t}\n\n\t\t\tres += r;\n\t\t\tif(i != j)\n\t\t\t\tres += r;\n\t\t}\n\n\t//for(c = 2; c*c <= n; ++c)\n\t//\tif(n % c == 0)\n\t//\t\tfor(a = 1; a < c; ++a)\n\t//\t\t{\n\t//\t\t\tb = c - a;\n\t//\t\t\tInt A = 1;\n\t//\t\t\tInt B = a + b;\n\t//\t\t\tInt C = a*b - n/(a + b);\n\t//\t\t\tInt D = B*B - 4*A*C;\n\t//\t\t\tif(D < 0)\n\t//\t\t\t\tcontinue;\n\n\t//\t\t\tInt Min = B, Max = 1 << 30;\n\t//\t\t\twhile(Max - Min > 1)\n\t//\t\t\t{\n\t//\t\t\t\tInt Mid = (Max + Min)/2;\n\t//\t\t\t\tif(Mid*Mid > D)\n\t//\t\t\t\t\tMax = Mid;\n\t//\t\t\t\telse\n\t//\t\t\t\t\tMin = Mid;\n\t//\t\t\t}\n\n\t//\t\t\tif(Min*Min == D)\n\t//\t\t\t\t++res;\n\t//\t\t}\n\n\treturn res;\n}\n\nint main()\n{\n\tInt n;\n\tscanf(\"%lld\", &n);\n\n\tprintf(\"%lld\\n\", f(n));\n\treturn 0;\n};\n", "lang_cluster": "C++", "tags": ["brute force", "math", "number theory"], "code_uid": "53547c45352a96f5b7433f34ecbf081d", "src_uid": "07e58a46f2d562a44b7c771edad361b4", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "\n#include \n#include \nusing namespace std ;\nint solver( int l1 , int r1 , int l2 , int r2 , int step = 30 ){\n if( l1 > r1 || l2 > r2 ) return 0 ; //结束条件\n int ovl = max( l1 , l2 ) ;\n int ovr = min( r1 , r2 ) ;\n int overlap = ( (ovl <= ovr) ? (ovr - ovl + 1) : 0 ) ; //算出重叠部分\n if( l1 <= l2 && r1 >= r2 || l1 >= l2 && r1 <= r2 ) return overlap ; //第一种情况,包含的情况,直接返回重叠部分\n int m = ( 1 << ( step - 1 ) ) ;//取中间值\n int res = overlap ;\n res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;\n res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;\n res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;\n res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;\n return res ;\n}\n\nint main()\n{\n int l1 , l2 , r1 , r2 ;\n while( ~scanf(\"%d%d%d%d\" , &l1 , &r1 , &l2 , &r2 ) )\n printf(\"%d\\n\" , solver( l1 , r1 , l2 , r2 ) ) ;\n return 0 ;\n}\n", "lang_cluster": "C++", "tags": ["divide and conquer"], "code_uid": "976f8a30e7dd7f9e5055c30d8b238f4d", "src_uid": "fe3c0c4c7e9b3afebf2c958251f10513", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \ndouble fmod(double a, double b){\n return a - b * (int)(a / b);\n}\ndouble nd(){\n double n;\n scanf(\"%lf\", &n);\n return n;\n}\n\ndouble a , b, m, vx,vy,vz;\n\ndouble findX(double xx){\n if (xx >= 0){\n double rmd = fmod(xx, a);\n int f1 = (int)(xx / a);\n if (f1 % 2 == 0){\n return rmd;\n }\n else {\n return a - rmd;\n }\n }\n else {\n xx *= -1;\n double rmd = fmod(xx, a);\n int f1 = (int)(xx / a);\n if (f1 % 2 == 0){\n return rmd;\n \n }\n else {\n return a - rmd;\n }\n }\n}\ndouble findY(double yy){\n if (yy >= 0){\n double rmd = fmod(yy, b);\n int f1 = (int)(yy / b);\n if (f1 % 2 == 0){\n return rmd;\n }\n else {\n return b - rmd;\n }\n }\n else {\n yy *= -1;\n double rmd = fmod(yy, b);\n int f1 = (int)(yy / b);\n if (f1 % 2 == 0){\n return rmd;\n }\n else {\n return b - rmd;\n \n }\n }\n}\nint main(){\n a = nd();\n b = nd();\n m = nd();\n vx = nd();\n vy = nd();\n vz = nd();\n\n double times = m / -vy;\n double xx = a / 2.0;\n double zz = 0;\n xx += times * vx;\n zz += times * vz;\n // printf(\"%f %f\\n\", xx, zz);\n xx = findX(xx);\n zz = findY(zz);\n printf(\"%f %f\\n\", xx, zz);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "geometry", "implementation"], "code_uid": "169facaaabe52682965e4b765f589116", "src_uid": "84848b8bd92fd2834db1ee9cb0899cff", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint n,x,a[1000000],tot;\nlong long modpow(long long x,long long y,int p){\n\tlong long res=1;\n\tfor(;y;y>>=1,(x*=x)%=p)\n\t\tif(y&1)(res*=x)%=p;\n\treturn res;\n}\nbool check(int b,int p){\n\tfor(int i=1;i*i>n>>x;\n\tn++;\n\tif(x==2)return puts(\"-1\"),0;\n\tfor(int i=2;i=1;i--)\n\t\tif(check(i%n,n))\n\t\t\treturn printf(\"%d\\n\",i),0;\n\tputs(\"-1\");\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "cf047d0ffd90621fe794faff88d4bd61", "src_uid": "29dda6a3f057e5bccdc076d7e492ac9a", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef vector vi;\ntypedef pair ii;\ntypedef long long ll;\n#define mset(a,b) memset(a,b,sizeof a)\n#define pb push_back\n#define mp make_pair\ntypedef vector vll;\nconst int maxn = 100000;\nvector primes;\nbitset isprime;\nvoid init(){\n isprime.reset(); isprime.flip();\n primes.pb(2ll);\n for(ll e = 4; e < maxn; e += 2) isprime[e] = 0;\n for(ll e = 3; e < maxn; e += 2) if(isprime[e]){\n primes.pb(e);\n for(ll f = e*e; f < maxn; f += e) isprime[f] = 0;\n }\n}\nbool isp(ll n){\n if(n == 2) return true;\n if(n < 2 || !(n&1)) return false;\n for(ll e = 3; e*e <= n; e += 2) if(n%e == 0) return false;\n return true;\n}\nll solve(ll index, ll number){\n if(index < 0 || number < primes[index]) return 0ll;\n number /= primes[index];\n ll ans = number;\n for(int e = 0; e < index; e++) ans -= solve(e, number);\n return ans;\n}\nint main(){\n init();\n ll a, b, k; cin >> a >> b >> k;\n if(!isp(k)) cout << 0 << endl;\n else if(k > maxn) cout << (a <= k && k <= b) << endl;\n else{\n int index = lower_bound(primes.begin(), primes.end(), k) - primes.begin();\n cout << solve(index, b) - solve(index, a-1) << endl;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "number theory"], "code_uid": "c759681f6fc6870ff26d24b508772d7a", "src_uid": "04a26f1d1013b6e6b4b0bdcf225475f2", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define ll long long int\n#define MAX 1000003\n#define pii pair\n#define VP vector< pii >\n#define MOD 998244353\n#define mp make_pair\n#define pb push_back\n#define rep(i,a,b) for(ll (i) = (a); (i) < (b); (i)++)\n#define all(v) (v).begin(),(v).end()\n#define S(x) scanf(\"%d\",&(x))\n#define S2(x,y) scanf(\"%d%d\",&(x),&(y))\n#define SL(x) scanf(\"%lld\",&(x))\n#define SL2(x) scanf(\"%lld%lld\",&(x),&(y))\n#define P(x) printf(\"%d\\n\",(x))\n#define FF first\n#define SS second\nusing namespace std;\nint main() {\nios_base::sync_with_stdio(false);\ncin.tie(NULL);\nll n,m,k;\ncin>>n>>m>>k;\nvector>dp(2005,vector(2005,0));\nrep(i,1,n+1)\n\tdp[i][0]=m;\nrep(i,2,n+1)\n\t{\n\t\trep(j,1,i)\n\t\t\t{\n\t\t\t\tdp[i][j]=(dp[i-1][j-1]*(m-1)+dp[i-1][j])%MOD;\n\t\t\t}\n\t}\nll ans=dp[n][k]%MOD;\ncout<\r\n#define re(i, x, y) for (int i = (x); i < (y); ++i) \r\n#define rep(i, x, y) for (int i = (x); i <= (y); ++i)\r\n#define repp(i, x, y, d) for (int i = (x); i <= (y); i += (d))\r\n#define reep(i, x, y) for (int i = (x); i <= (y); i <<= 1)\r\n#define pe(i, x, y) for (int i = (x) - 1; i >= (y); --i)\r\n#define per(i, x, y) for (int i = (x); i >= (y); --i)\r\n#define rep_e(i, u) for (int i = head[(u)]; i; i = e[i].nxt)\r\n#define vi vector\r\n#define vL vector\r\n#define vii vector \r\n#define viL vector\r\n#define vLi vector \r\n#define vLL vector\r\n#define eb emplace_back\r\n#define pb pop_back\r\n#define mp make_pair\r\n#define pii pair\r\n#define piL pair\r\n#define pLi pair\r\n#define pLL pair\r\n#define lowbit(x) ((x) & (-(x)))\r\n#define fi first\r\n#define se second\r\n#define all(x) x.begin(), x.end()\r\n#define debug(x) cout << #x << \" = \" << x << endl\r\nusing namespace std;\r\ntypedef unsigned int ui;\r\ntypedef long long LL;\r\ntypedef unsigned long long ULL;\r\ntypedef double db;\r\n#define getchar() (SB == TB && (TB = (SB = BB) + fread(BB, 1, 1 << 16, stdin), SB == TB) ? EOF : *SB++)\r\nchar BB[1 << 16], *SB = BB, *TB = BB;\r\ntemplate void read(T &n) {\r\n\tT w = 1;\r\n\tn = 0;\r\n\tchar ch = getchar();\r\n\tfor ( ; !isdigit(ch); ch = getchar()) {\r\n\t\tif (ch == '-') {\r\n\t\t\tw = -1;\r\n\t\t}\r\n\t}\r\n\tfor ( ; isdigit(ch); ch = getchar()) {\r\n\t\tn = n * 10 + (ch & 15);\r\n\t}\r\n\tn *= w;\r\n}\r\ntemplate void chkmn(T &a, const T &b) { \r\n\t(a > b) && (a = b); \r\n}\r\ntemplate void chkmx(T &a, const T &b) { \r\n\t(a < b) && (a = b); \r\n}\r\n\r\nint MOD;\r\nint adt(const LL &a) { \r\n\treturn (a % MOD + MOD) % MOD; \r\n} \r\nint inc(const int &a, const int &b) { \r\n\treturn a + b >= MOD ? a + b - MOD : a + b; \r\n}\r\nint dec(const int &a, const int &b) { \r\n\treturn a - b < 0 ? a - b + MOD : a - b; \r\n}\r\nint mul(const int &a, const int &b) { \r\n\treturn 1LL * a * b % MOD; \r\n}\r\nint sqr(const int &a) { \r\n\treturn 1LL * a * a % MOD; \r\n}\r\nvoid Adt(LL &a) {\r\n\ta = (a % MOD + MOD) % MOD;\r\n}\r\nvoid Inc(int &a, const int &b) { \r\n\t((a += b) >= MOD) && (a -= MOD); \r\n}\r\nvoid Dec(int &a, const int &b) { \r\n\t((a -= b) < 0) && (a += MOD); \r\n}\r\nvoid Mul(int &a, const int &b) { \r\n\ta = 1LL * a * b % MOD; \r\n}\r\nvoid Sqr(int &a) { \r\n\ta = 1LL * a * a % MOD; \r\n}\r\nint fsp(int a, int x = MOD - 2) {\r\n\tint res = 1;\r\n\tfor ( ; x; x >>= 1, Sqr(a)) {\r\n\t\tif (x & 1) {\r\n\t\t\tMul(res, a);\r\n\t\t}\r\n\t}\r\n\treturn res;\r\n}\r\n\r\nconst int maxn = 5e3 + 5;\r\nint n;\r\nint binom[maxn][maxn], pw[maxn][maxn], fac[maxn], fiv[maxn];\r\n\r\nint main() {\r\n\t\r\n read(n), read(MOD);\r\n fac[0] = 1;\r\n rep(i, 1, n) {\r\n fac[i] = mul(fac[i - 1], i);\r\n }\r\n fiv[n] = fsp(fac[n]);\r\n per(i, n, 1) {\r\n fiv[i - 1] = mul(fiv[i], i);\r\n }\r\n rep(i, 0, n) {\r\n binom[i][0] = binom[i][i] = 1;\r\n re(j, 1, i) {\r\n binom[i][j] = inc(binom[i - 1][j], binom[i - 1][j - 1]);\r\n }\r\n pw[i][0] = 1;\r\n rep(j, 1, n) {\r\n pw[i][j] = mul(pw[i][j - 1], i);\r\n }\r\n }\r\n int ans = 0;\r\n re(i, 0, n) {\r\n re(j, 1, n - i) {\r\n Inc(ans, mul(mul(binom[n][i], mul(fac[n - i], mul(fiv[j], binom[n - i - 1 - j][j - 1]))), mul(pw[n - i - j][i], pw[n - 1][j])));\r\n }\r\n }\r\n printf(\"%d\\n\", ans);\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["constructive algorithms", "math", "dp", "combinatorics", "fft"], "code_uid": "255ab21d8b52971f68233e3b4a98b0b6", "src_uid": "2d5a5055aaf34f4d300cfdf7c21748c3", "difficulty": 3200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include // std::min\n#include // std::abs\n\nusing namespace std;\n\nint main() {\n //first make sure train is in correct direction. if not, see how long it takes for igor to walk to dest.\n //track current time, track wher is igor and where is train.\n //keep direction of train, end terminal, target terminal.\n int stationPos, igorStartPos, igorEndPos, igorCurrentPos;\n int tramSpeed, igorSpeed;\n int tramPos, tramDir;\n cin >> stationPos >> igorStartPos >> igorEndPos >> tramSpeed >> igorSpeed >> tramPos >> tramDir;\n \n int tramTime, igorTime;\n //check how long igor needs to get to igorEndPos\n igorTime = (abs (igorStartPos - igorEndPos)) * igorSpeed;\n \n if(igorSpeed < tramSpeed){\n \tcout << igorTime;\n \treturn 0;\n }\n if(tramPos > igorStartPos){\n \tif(tramDir == 1){\n \t\t//finish tram route, then come back and pick igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)\n \t\ttramTime = (stationPos - tramPos) + (stationPos - igorStartPos);\n \t\tif(igorEndPos <= igorStartPos){\n \t\t\ttramTime += (igorStartPos-igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t\telse{\n \t\t\ttramTime += (igorStartPos+igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t}\n \telse if(tramDir == -1){\n \t\t//pick igor, then go to igorEndPos\n \t\ttramTime = tramPos - igorStartPos;\n \t\tif(igorEndPos >= igorStartPos){\n \t\t\ttramTime += (igorStartPos + igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t\telse{\n \t\t\ttramTime += (igorStartPos-igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t}\n }\n else if(tramPos <= igorStartPos){\n \tif(tramDir == 1){\n \t\t//pick up igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)\n \t\ttramTime = igorStartPos - tramPos;\n \t\tif(igorEndPos >= igorStartPos){\n \t\t\ttramTime += (igorEndPos-igorStartPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t\telse{\n \t\t\ttramTime += (stationPos - igorStartPos + stationPos - igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t}\n \telse if(tramDir == -1){\n \t\t//finish tram route, then come back and pick igor, then go to igorEndPos (check if igorStartPos > igorEndPos, etc.)\n \t\tif(igorStartPos == tramPos)\n \t\t\ttramTime = 0;\n \t\telse\n \t\t\ttramTime = tramPos + igorStartPos;\n \t\tif(igorEndPos >= igorStartPos){\n \t\t\tif(igorStartPos == tramPos)\n \t\t\t\ttramTime += tramPos + igorEndPos;\n \t\t\telse\n \t\t\t\ttramTime += (igorEndPos - igorStartPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t\telse{\n \t\t\tif(igorStartPos == tramPos)\n \t\t\t\ttramTime += (igorStartPos - igorEndPos);\n \t\t\telse\n \t\t\t\ttramTime += (stationPos - igorStartPos + stationPos - igorEndPos);\n \t\t\ttramTime *= tramSpeed;\n \t\t}\n \t}\n }\n cout << min(igorTime, tramTime);\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms", "implementation"], "code_uid": "78ace7890992d96d65dd838236264874", "src_uid": "fb3aca6eba3a952e9d5736c5d8566821", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define forn(i, n) for(int i = 0; i < int(n); i++)\n#define for1(i, n) for(int i = 1; i <= int(n); i++)\n#define fore(i, l, n) for(int i = int(l); i < int(n); i++)\n#define ford(i, n) for(int i = int(n) - 1; i >= 0; i--)\n\n#define sz(s) (int)(s).size()\n#define mp make_pair\n#define pb push_back\n#define X first\n#define Y second\n#define all(v) (v).begin(), (v).end()\n\nusing namespace std;\n\ntypedef long double ld;\ntypedef long long li;\ntypedef pair pt;\n\nconst int N = 100000;\nconst ld EPS = 1e-9;\n\nconst int M = 1000 * 1000 * 1000 + 7;\nint n;\nli cnt[200];\nint d[2][40000];\nint c[200][200];\nint C(int n, int k){\n if(k > n)\n return 0;\n if(k == n)\n return 1;\n if(k == 0)\n return 1;\n return (c[n-1][k]+c[n-1][k-1])%M;\n}\nvoid init(){\n forn(i, 200)\n forn(j, 200){\n c[i][j] = C(i, j);\n }\n}\nint mod_pow(int a, li b){\n int res = 1;\n while(b > 0){\n if(b&1)\n res = (res * 1LL*a)%M, b--;\n a = (a*1ll*a)%M;\n b >>= 1;\n }\n return res;\n}\nint mdp[200][200];\nint main(){\n // freopen(\"input.txt\", \"rt\", stdin);\n //freopen(\"output.txt\", \"wt\", stdout);\n li m;\n int k;\n cin >>n >>m >> k;\n init();\n forn(i, n){\n cnt[i] = ((m-i)/n + ((m-i)%n!=0));\n\n }\n forn(i, n+1){\n d[0][i] = mod_pow(c[n][i], cnt[0]);\n }\n //cerr << clock() << endl;\n fore(i, 1, n)\n forn(cd, n+1)\n mdp[cd][i] = mod_pow(c[n][cd], cnt[i]);\n fore(i, 1, n){\n forn(ck, k+1){\n forn(cd, min(n, ck)+1){\n d[i&1][ck] = (d[i&1][ck] + d[(i-1)&1][ck-cd]*1LL*mdp[cd][i])%M;\n }\n }\n forn(ck, k+1)\n d[(i-1)&1][ck] = 0;\n //cerr << clock() << endl;\n }\n cout << d[(n-1)&1][k] << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "bitmasks"], "code_uid": "b23c465e00c67277ef3c4493c6e30ac9", "src_uid": "9c71c8e031412e2bb21266a53821626a", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "// LUOGU_RID: 90889303\n#include \nusing ll=long long;\nusing namespace std;\nconst int N = 1e6 + 10;\nconst ll P = 998244353, yg = 3;\nll k, fac[N], inv[N], f[N], g[N], S2[N],n;\nll bin[N];\nll qpow(ll a, ll b)\n{\n ll res=1;\n while(b){\n if(b&1)res=res*a%P;\n a=a*a%P;\n b>>=1;\n }\n return res;\n}\nvoid NTT(ll *a, ll k, ll op)\n{\n for (ll i = 0; i < k; i++)\n bin[i] = (bin[i >> 1] >> 1) | ((i & 1) * (k >> 1));\n for (ll i = 0; i < k; i++)\n if (i < bin[i])\n swap(a[i], a[bin[i]]);\n for (ll i = 1; i < k; i <<= 1)\n {\n ll wn = qpow(yg, op == 1 ? (P - 1) / (2 * i) : (P - 1) - (P - 1) / (2 * i)), w, t;\n for (ll j = 0; j < k; j += i << 1)\n {\n w = 1;\n for (ll k = 0; k < i; k++)\n {\n t = a[i + j + k] * w % P;\n w = w * wn % P;\n a[i + j + k] = (a[j + k] - t + P) % P;\n a[j + k] = (a[j + k] + t) % P;\n }\n }\n }\n if (op == -1)\n {\n ll Inv = qpow(k, P - 2);\n for (ll i = 0; i < k; i++)\n a[i] = a[i] * Inv % P;\n }\n}\nvoid get_s2(){\n fac[0] = inv[0] = 1;\n for (int i = 1; i <= k; i++)\n fac[i] = fac[i - 1] * i % P, inv[i] = qpow(fac[i], P - 2);\n for (int i = 0; i <= k; i++)\n f[i] = (qpow(-1, i) + P) % P * inv[i] % P;\n for (int i = 0; i <= k; i++)\n g[i] = qpow(i, k) * inv[i] % P;\n ll len = 1;\n while (len < (k + 1) << 1)\n len <<= 1;\n NTT(f, len, 1);\n NTT(g, len, 1);\n for (int i = 0; i < len; i++)\n S2[i] = (f[i] * g[i]) % P; \n NTT(S2, len, -1);\n}\nint main()\n{\n cin >> n >> k;\n get_s2();\n ll ans=0;\n ll base=n*qpow(n+1,n-1)%P;\n ll inv=qpow(n+1,P-2);\n // for(int i=1;i<=k;i++)cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n#define MAX_N 1000005\n#define inf 0x3fffffff\n#define LL long long\n#define ull unsigned long long\nconst LL INF = 1e18;\nconst double eps = 1e-8;\n//const double pi = acos(-1);\nconst LL mod = 1e9+7;\ntypedef pairP;\n\nP ans;\nLL pow(LL m)\n{\n LL low = 1;\n LL up = 1e5;\n LL ans = 0;\n while(low <= up) {\n LL mid = (low+up)/2;\n if(mid*mid*mid<=m) {\n ans = max(ans, mid);\n low = mid + 1;\n }\n else {\n up = mid - 1;\n }\n }\n return ans;\n}\nvoid dfs(LL m, LL cnt, LL sum)\n{\n if(m <= 7) {\n ans = max(ans, P(cnt+m, sum+m));\n return;\n }\n LL p = pow(m);\n dfs(m-p*p*p, cnt+1, sum+p*p*p);\n dfs(p*p*p-1-(p-1)*(p-1)*(p-1), cnt+1, sum+(p-1)*(p-1)*(p-1));\n}\nint main()\n{\n LL m;\n cin >> m;\n dfs(m, 0, 0);\n cout << ans.first << \" \" << ans.second;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "greedy", "constructive algorithms", "binary search"], "code_uid": "b7e06c7b931eaa67de3599fc9fbe66d6", "src_uid": "385cf3c40c96f0879788b766eeb25139", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\ntypedef long long ll;\nconst int mod=1e9+7;;\nll tmp;\nll qmi(ll a,ll b=mod-2){\n ll ans=1;\n while(b){\n if(b&1)ans=ans*a%mod;\n a=a*a%mod;\n b>>=1;\n }return ans;\n}\nstruct sq{\n ll a,b;\n sq(ll x,ll y){a=x,b=y;}\n};\nll Mod(ll x){\n return (x%mod+mod)%mod;\n}\ninline sq operator+(const sq&l,const sq&r){return sq{Mod(l.a+r.a),Mod(l.b+r.b)};}\ninline sq operator-(const sq&l,const sq&r){return sq{Mod(l.a-r.a),Mod(l.b-r.b)};}\ninline sq operator*(const sq&l,const sq&r){return sq{Mod(l.a*r.a%mod+l.b*r.b*tmp%mod),Mod(l.a*r.b%mod+l.b*r.a%mod)};}\n\ninline sq inv(const sq&l){\n ll c=(l.a*l.a%mod-l.b*l.b%mod*tmp%mod)%mod;\n if(c<0)c+=mod;\n c=qmi(c,mod-2);\n // cout<>=1;\n }return ans;\n}\nll s[510][510];\nll p[510];\nll iv[510];\nll _iv[510];\nvoid init(){\n s[0][0]=1;\n for(int i=1;i<=300;i++){\n s[i][0]=0;\n for(int j=1;j<=i;j++){\n s[i][j]=(s[i-1][j-1]+((i-1)*s[i-1][j])%mod)%mod;\n }\n }\n p[0]=p[1]=iv[0]=iv[1]=_iv[0]=_iv[1]=1;\n for(int i=2;i<=300;i++){\n p[i]=p[i-1]*i%mod;\n iv[i]=(mod-mod/i)*iv[mod%i]%mod;\n _iv[i]=_iv[i-1]*iv[i]%mod;\n }\n}\nll k,l,r;\nll C(ll a,ll b){\n if(b>a)return 0;\n return p[a]*_iv[b]%mod*_iv[a-b]%mod;\n}\nsq sum(sq x,ll t){\n sq t1=qs(x,t+1);\n t1=t1-x;\n sq t2=x-sq{1,0};\n // cout<>k>>l>>r;\n l+=2,r+=2;\n init();\n // cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define fi first\n#define se second\n#define pb push_back\n#define MP std::make_pair\n#define PII std::pair\n#define all(x) (x).begin(), (x).end()\n#define CL(a, b) memset(a, b, sizeof a)\n#define rep(i, l, r) for (int i = (l); i <= (r); ++ i)\n#define per(i, r, l) for (int i = (r); i >= (l); -- i)\n#define PE(x, a) for (int x = head[a]; x;x = edge[x].next)\n#define id(x, y) idx[x + 50][y + 50]\n\ntypedef long long ll;\n\nconst int MAXN = 1e2 + 7;\nconst ll HA = 1e9 + 7;\nconst int dx[] = {-1, 0, 1, 0};\nconst int dy[] = {0, -1, 0, 1};\n\nint n;\nint idx[MAXN][MAXN], cnt, a[8003][8003];\nll p[4];\n\nll fp(ll a, ll b = HA - 2) {\n ll r = 1;\n while (b) {\n if (b & 1) r = r * a % HA;\n b >>= 1, a = a * a % HA;\n }\n return r;\n}\n\nvoid gauss() {\n n <<= 1;\n rep(i, 1, cnt) {\n ll d = fp(1ll * a[i][i]);\n for (int j = i, t = 0; j <= cnt && t <= n; t++, j++) a[i][j] = 1ll * a[i][j] * d % HA;\n a[i][0] = 1ll * a[i][0] * d % HA;\n for (int j = i + 1, t1 = 0; j <= cnt && t1 <= n; j++, t1++) {\n d = a[j][i];\n if (!d) continue;\n for (int k = i, t2 = 0; k <= cnt && t2 <= n; k++, t2++) a[j][k] = 1ll * (1ll * a[j][k] + HA - 1ll * d * a[i][k] % HA) % HA;\n a[j][0] = 1ll * (1ll * a[j][0] + HA - 1ll * d * a[i][0] % HA) % HA;\n\n }\n }\n per(i, cnt, 1) rep(j, i + 1, cnt) a[i][0] = 1ll * (1ll * a[i][0] + HA - 1ll * a[j][0] * a[i][j] % HA) % HA;\n}\n\nint main() {\n scanf(\"%d\", &n);\n ll sum = 0;\n rep(i, 0, 3) scanf(\"%lld\", p + i), sum = (sum + p[i]) % HA;\n sum = fp(sum);\n rep(i, 0, 3) p[i] = p[i] * sum % HA;\n rep(x, -n, n) per(y, n, -n) if (x * x + y * y <= n * n) id(x, y) = ++cnt;\n rep(x, -n, n) per(y, n, -n) {\n if (int f = id(x, y)) {\n a[f][f] = -1; a[f][0] = -1;\n int t;\n rep(k, 0, 3) {\n int tx = x + dx[k];\n int ty = y + dy[k];\n if (id(tx, ty)) a[f][id(tx, ty)] = p[k];\n }\n }\n }\n gauss();\n printf(\"%lld\\n\", 1ll * (a[id(0, 0)][0] % HA + HA) % HA);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "e71b10a4cd1a7847c8680501cf0ffeff", "src_uid": "bd6bcb5bffd039de93f217b02f5eae17", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)\n#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)\nusing namespace std;\nconst int maxn=2.7e5+5;\nint m,n,k;\nint mod;\nint f[19][maxn];\nvector g;\nint cnt[maxn];\nint upd(const int &x){\n\tif(x<0)return x+mod;\n\tif(x>=mod)return x-mod;\n\treturn x;\n}\nvoid add(int &x,const int &y){\n\tx=upd(x+y);\n}\nint ksm(int x,int k){\n\tint ans=1;\n\twhile(k){\n\t\tif(k&1)ans=1ll*ans*x%mod;\n\t\tx=1ll*x*x%mod;\n\t\tk>>=1;\n\t}\n\treturn ans;\n}\nint jc[maxn],inv[maxn];\nvoid init(int n){\n\tjc[0]=1;\n\tfr(i,1,n)jc[i]=1ll*jc[i-1]*i%mod;\n\tinv[n]=ksm(jc[n],mod-2);\n\tfd(i,n-1,0)inv[i]=1ll*inv[i+1]*(i+1)%mod;\n}\nvoid F(int *f,int n,int h){\n\tfor(int p=1;p operator *(vector &A,vector &B){\n\tstatic vector C;C.clear();C.resize(l+1);\n\tfr(i,0,l)fr(j,0,l-i)C[i+j]+=A[i]*B[j];\n\tfr(i,0,l)C[i]%=mod;\n\treturn C;\t\n}\nvector pksm(vector A,int k){\n\tstatic vector C;C.clear();C.resize(l+1);\n\tC[0]=1;\n\twhile(k){\n\t\tif(k&1)C=C*A;\n\t\tA=A*A;\n\t\tk>>=1;\n\t}\n\treturn C;\n}\nint main(){\n#ifdef pig\n\tfreopen(\"C.in\",\"r\",stdin);\n\tfreopen(\"C.out\",\"w\",stdout);\n#endif\n\tcin>>n>>k>>mod;\n\tinit(n);\n\tif(n&1){\n\t\tcout<>1]+(i&1);\n\tinit(m);\n\tfr(i,0,m-1){\t\n\t\tf[cnt[i]][i]=inv[i];\t\t\n\t}\n\tfr(i,0,l)F(f[i],m,1);\n\tg.resize(l+1);\n\tfr(s,0,m-1){\n\t\tfr(i,0,l)g[i]=f[i][s];\n\t\tg=pksm(g,k);\n\t\tfr(i,0,l)f[i][s]=g[i];\n\t}\n\tF(f[cnt[n]],m,-1);\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint n,k,mod,dp[18][260000],list[33],cnt_list,bi[33],cnt,fac[260000],inv[260000],len[260000];\nbool vis[260000];\nint power(int a,int n)\n{\n\tif(n==0)\n\t return 1;\n int ret=power(a,n/2);\n ret=1LL*ret*ret%mod;\n if(n%2)\n ret=1LL*ret*a%mod;\n return ret;\n}\nint main()\n{\n\tint i,j,s,p,q,flag;\n\tscanf(\"%d%d%d\",&n,&k,&mod);\n if(n%2)\n printf(\"%d\\n\",power(k,n));\t\n else\n {\n \tfor(i=0;i<=n;i++)\n \t{\n\t \tif(i==0)\n\t \t fac[i]=1;\n \t else\n \t fac[i]=1LL*fac[i-1]*i%mod;\n\t }\n\t inv[n]=power(fac[n],mod-2);\n\t for(i=n-1;i>=0;i--)\n\t inv[i]=1LL*inv[i+1]*(i+1)%mod;\n \tflag=0;\n \tcnt_list=0;\n\t\tfor(i=0;(1<0;j=(j-1)&i)\n \t\t{\n \t\t\tint l=min(k,len[i-j]+1);\n\t \t \t for(s=1;s<=l;s++)\n\t \t \t {\n\t \t \t \t if(dp[s-1][i-j])\n \t \t\t\t dp[s][i]=(dp[s][i]+1LL*dp[s-1][i-j]*inv[j])%mod;\n\t \t \t }\n\t\t\t\t}\n\t }\n \t}\n \tint ans=0;\n \tfor(i=0;i<=min(k,len[n]);i++)\n \t{\n\t \tif(dp[i][n])\n\t \t{\n\t \t\tint tmp=1LL*dp[i][n]*fac[k]%mod;\n\t \t\ttmp=1LL*tmp*inv[k-i]%mod;\n\t \t\ttmp=1LL*tmp*inv[i]%mod;\n\t \t\ttmp=1LL*tmp*fac[n]%mod;\n\t \t\tans=(ans+tmp)%mod;\n\t \t}\n\t }\n \tprintf(\"%d\\n\",(power(k,n)-ans+mod)%mod);\n }\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "3577b6d0fee5abf894e86f53392486f8", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#define fo(i,a,b) for(int i=a;i<=b;i++)\n#define fd(i,a,b) for(int i=a;i>=b;i--)\nusing namespace std;\n\ntypedef long long ll;\n\nconst int N=262144;\n\nint L,n,k,Mo,len,bit[20],fac[N],inv[N],cnt[N],g[20][N],f[20][N],h[20],p[20];\n\nint pwr(int x,int y) {\n\tint z=1;\n\tfor(;y;y>>=1,x=(ll)x*x%Mo)\n\t\tif (y&1) z=(ll)z*x%Mo;\n\treturn z;\n}\n\nvoid inc(int &x,int y) {x=x+y>=Mo?x+y-Mo:x+y;}\nvoid dec(int &x,int y) {x=x-y<0?x-y+Mo:x-y;}\nvoid FWT(int *a,int len) {for(int i=1;i>i&1) bit[++n]=1<>(j-1)&1) s+=bit[j];\n\t\tg[cnt[i]][i]=inv[s];\n\t}\n\tfo(i,0,n) FWT(g[i],len);\n\tfo(s,0,len-1) {\n\t\tfo(i,0,n) h[i]=g[i][s];\n\t\tget_ln(h,p,n);\n\t\tfo(i,0,n) p[i]=(ll)p[i]*k%Mo;\n\t\tget_exp(p,h,n);\n\t\tfo(i,0,n) f[i][s]=h[i];\n\t}\n\tIFWT(f[n],len);\n\tint ans=(ll)f[n][len-1]*fac[L]%Mo;\n\tans=(all-ans+Mo)%Mo;\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "ecfe4889b2aec933153aaaf3e8894244", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define inf 17446744073709551616ull\n#define ll unsigned long long\nusing namespace std;\nll mod,n,k,dp[18][250005];\nll fac[250005],inv[250005];\ninline ll qpow(ll a,ll b)\n{ll res=1ll,tp=a;\nwhile (b)\n{if (b&1ll) {res*=tp;res%=mod;}\ntp*=tp;tp%=mod;b>>=1ll;\n}\nreturn res;\n}\ninline ll cal(int a,int b)\n{return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;}\ninline ll calc()\n{int i,j,l;fac[0]=1ll;\nfor (i=1;i<=n;i++) fac[i]=fac[i-1]*((ll)(i))%mod;\ninv[n]=qpow(fac[n],mod-2ll);\nfor (i=n-1;i>=0;i--) inv[i]=inv[i+1]*((ll)(i+1))%mod;\nfor (i=1;i<=n;i++) dp[1][i]=inv[i];\nll ans=dp[1][n]*cal(k,1)%mod;\nint t=min(17,((int)(k)));\nfor (i=2;i<=t;i++)\n{for (j=n;j;j=(j-1)&n)\n{if (!dp[i-1][j]) {continue;}\nint r=n^j;\nfor (l=r;l;l=(l-1)&r)\n{dp[i][j+l]+=dp[i-1][j]*inv[l];\nif (dp[i][j+l]>inf) dp[i][j+l]%=mod;\n}\n}\nfor (j=n;j;j=(j-1)&n) dp[i][j]%=mod;\nans+=dp[i][n]*cal(k,i);ans%=mod;\n}\nans*=fac[n];ans%=mod;\nreturn ans;\n}\nint main (){\n\tcin>>n>>k>>mod;\n\tll ans=qpow(k,n);\n\tif (n%2==0) ans=(ans-calc()+mod)%mod;\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint n,k,mod,dp[18][260000],list[33],cnt_list,bi[33],cnt,fac[260000],inv[260000],len[260000];\nbool vis[260000];\nint power(int a,int n)\n{\n\tif(n==0)\n\t return 1;\n int ret=power(a,n/2);\n ret=1LL*ret*ret%mod;\n if(n%2)\n ret=1LL*ret*a%mod;\n return ret;\n}\nint main()\n{\n\tint i,j,s,p,q,flag;\n\tscanf(\"%d%d%d\",&n,&k,&mod);\n if(n%2)\n printf(\"%d\\n\",power(k,n));\t\n else\n {\n \tfor(i=0;i<=n;i++)\n \t{\n\t \tif(i==0)\n\t \t fac[i]=1;\n \t else\n \t fac[i]=1LL*fac[i-1]*i%mod;\n\t }\n\t inv[n]=power(fac[n],mod-2);\n\t for(i=n-1;i>=0;i--)\n\t inv[i]=1LL*inv[i+1]*(i+1)%mod;\n \tflag=0;\n \tcnt_list=0;\n\t\tfor(i=0;(1<0;j=(j-1)&i)\n \t\t{\n\t \t \t for(s=1;s<=min(k,len[i-j]+1);s++)\n \t \t\t {\n \t \t\t\t dp[s][i]+=1LL*dp[s-1][i-j]*inv[j]%mod;\n\t\t \t \t dp[s][i]%=mod;\n \t \t\t }\n\t\t }\n\t }\n \t}\n \tint ans=0;\n \tfor(i=0;i<=min(k,len[n]);i++)\n \t{\n\t \tif(dp[i][n])\n\t \t{\n\t \t\tint tmp=1LL*dp[i][n]*fac[k]%mod;\n\t \t\ttmp=1LL*tmp*inv[k-i]%mod;\n\t \t\ttmp=1LL*tmp*inv[i]%mod;\n\t \t\ttmp=1LL*tmp*fac[n]%mod;\n\t \t\tans=(ans+tmp)%mod;\n\t \t}\n\t }\n \tprintf(\"%d\\n\",(power(k,n)-ans+mod)%mod);\n }\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "1bfe9bf3b5004e84ff3a5c2c6afe15b7", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nconst int N=250005,M=1<<17,D=18;\n\nint P;\nint power(int a,int t){\n\tint r=1;\n\twhile(t){\n\t\tif(t&1)r=(ll)r*a%P;\n\t\ta=(ll)a*a%P;t>>=1;\n\t}\n\treturn r;\n}\n\nint L,K,fac[N],n,m,bit[20],ifac[N],bitcnt[N];\nll suma[D][M],sumc[D][M];\null dp[D][M],tmp[D][M];\n\nint main(){\n\t\n\tscanf(\"%d%d%d\",&L,&K,&P);\n\tfac[0]=1;\n\tfor(int i=1;i<=L;i++){\n\t\tfac[i]=(ll)fac[i-1]*i%P;\n\t\tbitcnt[i]=bitcnt[i>>1]+(i&1);\n\t}\n\tll all=power(K,L);\n\tif(L&1)printf(\"%lld\\n\",all),exit(0);\n\t\n\tifac[L]=power(fac[L],P-2);\n\tfor(int i=L;i>=1;i--)ifac[i-1]=(ll)ifac[i]*i%P;\n\tfor(int i=1;(1<>i&1)bit[n++]=1<>j&1)s+=bit[j];\n\t\tsuma[bitcnt[i]][i]=ifac[s];\n\t}\n\t\n\t// get suma\n\tfor(int t=0;t<=n;t++){\n\t\tint i,j,k,l;\n\t\tll*a=suma[t];\n\t\tfor(l=m;l>=2;l>>=1)for(j=0,i=l>>1;j>=1;\n\tfor(cc--,revK>>=1;cc;cc--,revK>>=1){\n\t\t\n\t\t// use dp to update dp\n\t\t// f i,j means V = i, P = j\n\t\t// f (a + b),j = ∑g a,j * h b,j \n\t\t\n\t\tfor(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);\n\t\tfor(int a=0;a<=n;a++)for(int b=0;a+b<=n;b++){\n\t\t\tull*f=tmp[a+b],*g=dp[a],*h=dp[b];\n\t\t\tfor(int j=0;j>1;j\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define LL long long\n#define FOR(i,a,b) for (int i=a;i<=b;++i)\n#define FORD(i,a,b) for (int i=a;i>=b;--i)\nusing namespace std;\ntypedef pair pa;\nvoid getint(int &v){\n char ch,fu=0;\n for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());\n if(ch=='-') fu=1, ch=getchar();\n for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';\n if(fu) v=-v;\n}\nLL f[30][300010],ans,fac[300010],inv[300010];\nint N,n,k,MO,s,p[1000010],a[55],S[1000010];\nLL pw(LL x,int y){\n\tLL t=1;\n\tfor (;y;y>>=1){\n\t\tif (y&1) t=t*x%MO;\n\t\tx=x*x%MO;\n\t}\n\treturn t;\n}\nLL P(int x,int y){\n\treturn fac[x]*inv[x-y]%MO;\n}\nint main(){\n\tscanf(\"%d%d%d\",&n,&k,&MO);\n\tif (n&1) return cout<>=1;\n\t}\n\tN=1<>j)&1) S[i]+=a[j];\n\tFOR(i,0,N-1){\n\t\tint t=-1;\n\t\tFOR(j,0,20)\n\t\t\tif ((i>>j)&1) t=j;\n\t\tp[i]=(1<<(t+1))+i;\n\t}\n\tf[0][0]=1;\n\tFOR(i,0,k-1)\n\t\tFOR(j,0,N-1)\n\t\t\tif (f[i][j]){\n\t\t\t\tfor (int k=p[j];k\nusing namespace std;\n\n#define ms(s, n) memset(s, n, sizeof(s))\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)\n#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)\n#define sz(a) int((a).size())\n#define present(t, x) (t.find(x) != t.end())\n#define all(a) (a).begin(), (a).end()\n#define uni(a) (a).erase(unique(all(a)), (a).end())\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define fi first\n#define se second\n#define prec(n) fixed<> (i)) & 1)\n#define bitcount(n) __builtin_popcountll(n)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pi;\ntypedef vector vi;\ntypedef vector vii;\nconst int MOD = (int) 1e9 + 7;\nconst int MOD2 = 1007681537;\nconst int INF = (int) 1e9;\nconst ll LINF = (ll) 1e18;\nconst ld PI = acos((ld) -1);\nconst ld EPS = 1e-9;\ninline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}\ninline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}\ninline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}\ntemplate inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}\ntemplate inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}\ninline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}\ninline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}\ninline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}\ninline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}\ninline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}\ninline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}\ninline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}\ninline int sign(ld x, ld y) {return sign(x - y);}\n#define db(x) cerr << #x << \" = \" << (x) << \" \";\n#define endln cerr << \"\\n\";\n\nvoid transform(int a[], int n, int p, int invert) {\n int k = __lg(n);\n FOR(i, 0, k) {\n FOR(msk, 0, n) if (bit(msk, i)) {\n int pmsk = msk ^ (1 << i);\n if (!invert) {\n addmod(a[msk], a[pmsk], p);\n }\n else {\n submod(a[msk], a[pmsk], p);\n }\n }\n }\n}\n\nvector pow_convol(vector a, int k, int p) {\n int n = a.size();\n if (k == 1) {\n return a;\n }\n else if (k & 1) {\n vector r = pow_convol(a, k - 1, p);\n vector res(n, 0);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n res[i + j] = (res[i + j] + (long long) r[i] * a[j]) % p;\n }\n }\n return res;\n }\n else {\n vector r = pow_convol(a, k >> 1, p);\n vector res(n, 0);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n res[i + j] = (res[i + j] + (long long) r[i] * r[j]) % p;\n }\n }\n return res;\n }\n}\n\nvector subset_pow_convol(vector a, int k, int p) {\n static const int logn = 19;\n static int fa[logn][1 << logn];\n static int fb[logn][1 << logn];\n static int fc[logn][1 << logn];\n int n = a.size();\n while (n != (n & -n)) n += n & -n; \n vector res(n, 0);\n for (int i = 0; i < logn; i++) {\n for (int j = 0; j < n; j++) {\n if (bitcount(j) == i) {\n fa[i][j] = j < sz(a) ? a[j] : 0;\n }\n else {\n fa[i][j] = 0;\n }\n fc[i][j] = 0;\n }\n transform(fa[i], n, p, 0);\n }\n FOR(i, 0, n) {\n vi va(logn);\n FOR(j, 0, logn) {\n va[j] = fa[j][i];\n }\n va = pow_convol(va, k, p);\n FOR(j, 0, logn) {\n fc[j][i] = va[j];\n }\n }\n for (int i = 0; i < logn; i++) {\n transform(fc[i], n, p, 1);\n for (int j = 0; j < n; j++) if (bitcount(j) == i) {\n res[j] = fc[i][j];\n }\n }\n return res;\n}\n\nconst int maxn = 1e6 + 5;\nint n, k, p;\nint fac[maxn];\nint ifac[maxn];\nint dp[2][maxn];\nint f[maxn];\n\nvoid solve() {\n cin >> n >> k >> p;\n int nn = n;\n FOR(i, 0, n + 1) if ((n & i) == i) {\n int t = 0;\n FOR(j, 0, 20) if (bit(n, j)) {\n t <<= 1;\n t |= bit(i, j);\n }\n f[t] = i;\n nn = t;\n }\n if (n & 1) {\n cout << fpow(k, n, p) << \"\\n\";\n return;\n }\n fac[0] = 1; FOR(i, 1, maxn) fac[i] = mult(fac[i - 1], i, p);\n FOR(i, 0, maxn) ifac[i] = inv(fac[i], p);\n vi a(nn + 1, 0);\n FOR(i, 0, nn + 1) a[i] = ifac[f[i]];\n int res = fpow(k, n, p);\n a = subset_pow_convol(a, k, p);\n submod(res, mult(a[nn], fac[n], p), p);\n cout << res << \"\\n\";\n}\n\nint main() {\n int JUDGE_ONLINE = 1;\n if (fopen(\"in.txt\", \"r\")) {\n JUDGE_ONLINE = 0;\n assert(freopen(\"in.txt\", \"r\", stdin));\n //assert(freopen(\"out.txt\", \"w\", stdout));\n }\n else {\n ios_base::sync_with_stdio(0), cin.tie(0);\n }\n solve();\n if (!JUDGE_ONLINE) {\n //cout << \"\\nTime elapsed: \" << 1000 * clock() / CLOCKS_PER_SEC << \"ms\\n\";\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "da675842742e168c7297685d0f7fc157", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int Maxn = 1 << 19 | 5;\nint n, k, p, maxi, ct[Maxn], f[Maxn];\nint tmp1[20][Maxn], tmp2[20][Maxn], tmp3[20][Maxn];\nlong long pp;\nlong long t[20][Maxn];\nlong long fac[Maxn], inv[Maxn];\nlong long fast_pow(long long x, long long y)\n{\n long long ans = 1, now = x;\n while (y)\n {\n if (y & 1) ans = ans * now % p;\n now = now * now % p;\n y >>= 1;\n }\n return ans;\n}\nvoid init(void)\n{\n fac[0] = 1;\n for (int i = 1; i <= maxi; i++)\n fac[i] = fac[i - 1] * i % p;\n inv[maxi] = fast_pow(fac[maxi], p - 2);\n for (int i = maxi - 1; i >= 0; i--)\n inv[i] = inv[i + 1] * (i + 1) % p;\n}\nint lower(int x)\n{\n int tmp = 1;\n for (; tmp < x; tmp <<= 1);\n return tmp;\n}\nvoid FWT(int *now, int len, bool type = false)\n{\n long long *T = t[0];\n for (int i = 0; i < len; i ++) T[i] = now[i];\n if (type)\n for (int i = 1; i < len; i <<= 1)\n for (int j = 0; j < len; j += (i << 1))\n for (int k = j; k < i + j; k++)\n T[i + k] -= T[k];\n else\n for (int i = 1; i < len; i <<= 1)\n for (int j = 0; j < len; j += (i << 1))\n for (int k = j; k < i + j; k++)\n T[i + k] += T[k];\n for (int i = 0; i < len; i ++) {\n now[i] = T[i] % p;\n if (now[i] < 0) now[i] += p;\n }\n}\ninline void __a (long long &x) { x >= pp ? x -= pp : x; }\nvoid multi(void)\n{\n for (int j = 0; j <= 19; j++)\n {\n memset(tmp1[j], 0, sizeof(int[maxi + 1]));\n memset(t[j], 0, sizeof(long long[maxi + 1])); \n }\n for (int j = 0; j <= maxi; j++)\n tmp1[ct[j]][j] = f[j];\n for (int j = 0; j <= 19; j++)\n FWT(tmp1[j], maxi + 1);\n for (int q = 0; q <= 19; q++)\n for (int l = 0; q + l <= 19; l++)\n for (int s = 0; s <= maxi; s++)\n __a(t[q + l][s] += 1ll * tmp1[q][s] * tmp2[l][s]);\n for (int q = 0; q <= 19; q++)\n for (int s = 0; s <= maxi; s++)\n tmp3[q][s] = t[q][s] % p;\n for (int j = 0; j <= 19; j++)\n FWT(tmp3[j], maxi + 1, true);\n for (int j = 0; j <= maxi; j++)\n f[j] = tmp3[ct[j]][j];\n}\nvoid multi2(void)\n{\n static int tmp[20][Maxn];\n for (int i = 0; i <= 19; i++)\n memset(t[i], 0, sizeof(long long[maxi + 1]));\n for (int q = 0; q <= 19; q++)\n for (int l = 0; q + l <= 19; l++)\n for (int s = 0; s <= maxi; s++)\n __a(t[q + l][s] += 1ll * tmp2[q][s] * tmp2[l][s]);\n for (int q = 0; q <= 19; q++)\n for (int s = 0; s <= maxi; s++)\n tmp[q][s] = t[q][s] % p;\n for (int i = 0; i <= 19; i++)\n FWT(tmp[i], maxi + 1, true);\n for (int i = 0; i <= 19; i++)\n memset(tmp2[i], 0, sizeof(int[maxi + 1]));\n for (int i = 0; i <= maxi; i++)\n tmp2[ct[i]][i] = tmp[ct[i]][i];\n for (int i = 0; i <= 19; i++)\n FWT(tmp2[i], maxi + 1);\n}\nint main()\n{\n scanf(\"%d%d%d\", &n, &k, &p);\n pp = 1ll * p * p;\n if (n & 1)\n {\n printf(\"%lld\", fast_pow(k, n));\n return 0;\n }\n f[0] = 1;\n maxi = lower(n + 1) - 1;\n init();\n for (int i = 0; i <= maxi; i++)\n ct[i] = __builtin_popcount(i);\n for (int i = 0; i <= maxi; i++)\n tmp2[ct[i]][i] = inv[i];\n for (int i = 0; i <= 19; i++)\n FWT(tmp2[i], maxi + 1);\n int res_k = k;\n while (k)\n {\n cerr << k << endl;\n if (k & 1) multi();\n k >>= 1;\n if (k) multi2();\n }\n printf(\"%lld\", (fast_pow(res_k, n) - fac[n] * f[n] % p + p) % p);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "47387413c4c90da7e0e34e6b8b388068", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define inf 17446744073709551616ull\n#define ll unsigned long long\nusing namespace std;\nll mod,dp[18][250005];\nll fac[250005],inv[250005];\nll n,k;\ninline ll qpow(ll a,ll b)\n{ll res=1ll,tp=a;\nwhile (b)\n{if (b&1ll) {res*=tp;res%=mod;}\ntp*=tp;tp%=mod;b>>=1ll;\n}\nreturn res;\n}\ninline ll cal(int a,int b)\n{return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;}\ninline ll calc()\n{int i,j,l;\nfac[0]=1ll;\nfor (i=1;i<=n;i++) fac[i]=fac[i-1]*((ll)(i))%mod;\ninv[n]=qpow(fac[n],mod-2ll);\nfor (i=n-1;i>=0;i--) inv[i]=inv[i+1]*((ll)(i+1))%mod;\nfor (i=1;i<=n;i++) {dp[1][i]=inv[i];}\nll ans=dp[1][n]*cal(k,1)%mod;\nint t=min(17,((int)(k)));\nfor (i=2;i<=t;i++)\n{for (j=n;j;j=(j-1)&n)\n{for (l=j;l;l=(l-1)&j)\n{dp[i][j]+=dp[i-1][j^l]*inv[l];\nif (dp[i][j]>inf) \ndp[i][j]%=mod;\n}\ndp[i][j]%=mod;\n}\nans+=dp[i][n]*cal(k,i);\nans%=mod;\n}\nans*=fac[n];ans%=mod;\nreturn ans;\n}\nint main (){\n\tcin>>n>>k>>mod;\n\tll ans=qpow(k,n);\n\tif (n&1) \n\t{cout<\n#include \n#include \n#define fo(i,a,b) for(int i=a;i<=b;i++)\n#define fd(i,a,b) for(int i=a;i>=b;i--)\nusing namespace std;\n\ntypedef long long ll;\n\nconst int N=262144;\n\nint L,n,k,Mo,len,bit[20],fac[N],inv[N],cnt[N],g[20][N],f[20][N];\n\nint pwr(int x,int y) {\n\tint z=1;\n\tfor(;y;y>>=1,x=(ll)x*x%Mo)\n\t\tif (y&1) z=(ll)z*x%Mo;\n\treturn z;\n}\n\nvoid inc(int &x,int y) {x=x+y>=Mo?x+y-Mo:x+y;}\nvoid dec(int &x,int y) {x=x-y<0?x-y+Mo:x-y;}\nvoid FWT(int *a,int len) {for(int i=1;i>i&1) bit[++n]=1<>(j-1)&1) s+=bit[j];\n\t\tg[cnt[i]][i]=inv[s];\n\t}\n\tfo(i,2,n) inv[i]=(ll)(Mo-Mo/i)*inv[Mo%i]%Mo;\n\tfo(i,0,n) FWT(g[i],len);\n\tfo(s,0,len-1) {\n\t\tfo(i,0,n) h[i]=g[i][s];\n\t\tget_ln();\n\t\tfo(i,0,n) h[i]=h[i]*k;\n\t\tget_exp();\n\t\tfo(i,0,n) f[i][s]=h[i];\n\t}\n\tIFWT(f[n],len);\n\tint ans=(ll)f[n][len-1]*fac[L]%Mo;\n\tans=(all-ans+Mo)%Mo;\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "89bf093a73600b984ced3c794a3df4d5", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\"bits/stdc++.h\"\n#define F(i,j,n) for(register int i=j;i<=n;i++)\n#define D(i,j,n) for(register int i=j;i>=n;i--)\n#define ll unsigned long long\n#define N 250010\nusing namespace std;\nnamespace io{\n\tconst int L=(1<<19)+1;\n\tchar ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,st[55];int f,tp;\n\tchar gc(){\n\t\tif(iS==iT){\n\t\t\tiT=(iS=ibuf)+fread(ibuf,1,L,stdin);\n\t\t\treturn iS==iT?EOF:*iS++;\n\t\t}\n\t\treturn*iS++;\n\t}\n\ttemplatevoid gi(I&x){\n\t\tfor(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;\n\t\tfor(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;\n\t}\n};\nusing io::gi;\nusing io::gc;\nint n,k,p,l,b[N];\nll x[26][N],f[N],g[N],a,d;\nll pw(ll x,int y=p-2){ll a=1;while(y){if(y&1)a=a*x%p;x=x*x%p;y>>=1;}return a;}\nll dfs(int k,int n){\n\tif(b[n]=k-1){\n\t\tx[k][n]+=dfs(k-1,i)*g[n^i];\n\t\tif(++t==18)t=0,x[k][n]%=p;\n\t}\n\tx[k][n]%=p; \n//\tprintf(\"%d %d %lld\\n\",k,n,x[k][n]);\n\treturn x[k][n];\n}\nll c(int n,int m){return f[n]*g[m]%p*g[n-m]%p;}\nint main()\n{\n\t#ifndef ONLINE_JUDGE\n\tfreopen(\"255x.in\",\"r\",stdin);\n\tfreopen(\"255x.out\",\"w\",stdout);\n\t#endif\n\tgi(n);gi(k);gi(p);\n\tF(i,1,n)b[i]=b[i>>1]+(i&1); \n\ta=pw(k,n);\n\tif(n&1){printf(\"%d\\n\",a);return 0;}\n\tl=min(k,b[n]);\n\tf[0]=1;F(i,1,n)f[i]=f[i-1]*i%p;\n\tg[n]=pw(f[n]);D(i,n,1)g[i-1]=g[i]*i%p;\n\tD(i,l,1)d+=c(k,i)*dfs(i,n);\n//\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long LL;\ntypedef unsigned long long ULL;\n\n//const LL mod=1e9+7;\nconst double PI=acos(-1);\nconst double EPS=1e-6;\nconst int INF=0x3f3f3f3f;\ninline int readint(){int sum=0;char c=getchar();bool f=0;while(c<'0'||c>'9'){if(c=='-') f=1;c=getchar();}while(c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}if(f) return -sum;return sum;}\ninline LL readLL(){LL sum=0;char c=getchar();bool f=0;while(c<'0'||c>'9'){if(c=='-') f=1;c=getchar();}while(c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}if(f) return -sum;return sum;}\n\nconst int maxn=250015;\nLL inv[maxn],p[maxn];\nvoid CNumberInit(int MAXNUM,LL mod){\n inv[0]=inv[1]=1;\n p[0]=p[1]=1;\n for(int i=2;i<=MAXNUM;i++){\n inv[i]=(mod-mod/i)*inv[mod%i]%mod;\n p[i]=p[i-1]*i%mod;\n }\n for(int i=2;i<=MAXNUM;i++) inv[i]=inv[i-1]*inv[i]%mod;\n}\n\nLL qmod(LL a,LL b,LL c){\n LL ret=1;\n for(;b;b>>=1){\n if(b&1) ret=ret*a%c;\n a=a*a%c;\n }\n return ret;\n}\n\nvector megumi;\nLL mod;\nint k;\nLL dp[20][250005];\n\nint main(){ios_base::sync_with_stdio(0);cin.tie(0);\n LL n;cin>>n>>k>>mod;\n CNumberInit(n+5,mod);\n if(n%2){\n cout<\nusing namespace std;\nconst int N = 250005;\nint n,kk,P;\nusing ll = long long;\nll qpow(ll a,ll b)\n{\n ll ret = 1;\n while (b)\n {\n if (b & 1) ret = ret * a % P;\n a = a * a % P;b >>= 1;\n }\n return ret;\n}\ninline int lowbit(int x) {return x & -x;}\nll fac[N],inv[N],dp[26][N];\nll dfs(int cur,int msk)\n{\n if (dp[cur][msk] != -1) return dp[cur][msk];\n if (!msk)\n {\n dp[cur][msk] = fac[n];\n for (int i = 1;i <= cur;i++)\n dp[cur][msk] = dp[cur][msk] * (kk - i + 1) % P;\n return dp[cur][msk];\n }\n dp[cur][msk] = 0;int tmp = msk - lowbit(msk);\n for (int i = tmp;i > 0;i = (i - 1) & tmp)\n dp[cur][msk] = (dp[cur][msk] + inv[msk - i] * dfs(cur + 1,i)) % P;\n dp[cur][msk] = (dp[cur][msk] + inv[msk] * dfs(cur + 1,0)) % P;\n return dp[cur][msk];\n}\nint main ()\n{\n ios::sync_with_stdio(false);\n cin >> n >> kk >> P;ll ans = qpow(kk,n);\n if (n & 1) {cout << ans << endl;return 0;}\n fac[0] = 1;\n for (int i = 1;i <= n;i++) fac[i] = fac[i - 1] * i % P;\n inv[n] = qpow(fac[n],P - 2);\n for (int i = n;i > 0;--i) inv[i - 1] = inv[i] * i % P;\n memset(dp,-1,sizeof(dp));ans = (ans - dfs(0,n) + P) % P;\n cout << ans << endl;\n return 0;\n}\n// 非卡常做法需要用FWT\n// 卡常做法: 每次强制将Ai规定为含有msk的lowbit的那一个,最后当msk=0时用排列乘回来", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "12ba9d8068ce5f2be3846b1e3ee10cd4", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nconst int N=250100;\nint gi() {\n\tint w=0;bool q=1;char c=getchar();\n\twhile ((c<'0'||c>'9') && c!='-') c=getchar();\n\tif (c=='-') q=0,c=getchar();\n\twhile (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();\n\treturn q? w:-w;\n}\nint mod,m;\nint siz[N],inv[N];\nstruct DT{ int a[19]; }f[N];\ninline int qpow(int x,int y) {\n\tint ans=1;\n\twhile (y) {\n\t\tif (y&1) ans=1LL*ans*x%mod;\n\t\tx=1LL*x*x%mod;y>>=1;\n\t}\n\treturn ans;\n}\nint main()\n{\n#ifndef ONLINE_JUDGE\n\tfreopen(\"CF838C.in\",\"r\",stdin);\n\tfreopen(\"CF838C.out\",\"w\",stdout);\n#endif\n\tint i,s,n=gi(),k=gi(),a,b,y,t[20]={},p[20]={};DT *F,*G;mod=gi();\n\tif (n&1) return printf(\"%d\\n\",qpow(k,n)),0;\n\tfor (i=0;i<18;i++) m+=n>>i&1;\n\tfor (i=0,s=1;i<=n;i++) {\n\t\tinv[i]=i<2?1:-1LL*(mod/i)*inv[mod%i]%mod;\n\t\tsiz[i]=siz[i>>1]+(i&1);\n\t\ts=1LL*s*inv[i]%mod;\n\t\tif ((i&n)==i)\n\t\t\tf[i].a[siz[i]]=s;\n\t}\n\tfor (i=0;i<18;i++)\n\t\tif (n>>i&1)\n\t\t\tfor (s=n;s;(--s)&=n)\n\t\t\t\tif (s>>i&1)\n\t\t\t\t\tfor (a=0,F=&f[s^(1<a[a]+=F->a[a])%=mod;\n\tfor (i=0;i<=n;i++) if ((i&n)==i) {\n\t\t\tF=&f[i];\n\t\t\tfor (a=0;a<=m;a++) t[a]=F->a[a],F->a[a]=!a;\n\t\t\tfor (y=k;y;) {\n\t\t\t\tif (y&1) {\n\t\t\t\t\tfor (a=0;a<=m;a++) p[a]=F->a[a],F->a[a]=0;\n\t\t\t\t\tfor (a=0;a<=m;a++)\n\t\t\t\t\t\tif (t[a])\n\t\t\t\t\t\t\tfor (b=0;a+b<=m;b++)\n\t\t\t\t\t\t\t\tF->a[a+b]=(F->a[a+b]+1LL*t[a]*p[b])%mod;\n\t\t\t\t}\n\t\t\t\tif (y>>=1) {\n\t\t\t\t\tfor (a=0;a<=m;a++) p[a]=t[a],t[a]=0;\n\t\t\t\t\tfor (a=0;a<=m;a++)\n\t\t\t\t\t\tif (p[a])\n\t\t\t\t\t\t\tfor (b=0;a+b<=m;b++)\n\t\t\t\t\t\t\t\tt[a+b]=(t[a+b]+1LL*p[a]*p[b])%mod;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tfor (i=0;i<18;i++)\n\t\tif (n>>i&1)\n\t\t\tfor (s=n;s;(--s)&=n)\n\t\t\t\tif (s>>i&1)\n\t\t\t\t\tfor (a=0,F=&f[s^(1<a[a]-=F->a[a])%=mod;\n\ts=f[n].a[m];\n\tfor (i=n;i;i--) s=1LL*s*i%mod;\n\tprintf(\"%d\\n\",((qpow(k,n)-s)%mod+mod)%mod);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "de61aced0045d298db42aa808b835d37", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define inf 17446744073709551616ull\n#define ll unsigned long long\nusing namespace std;\nll mod,n,k,dp[19][300005];\nll fac[300005],inv[300005];\ninline ll qpow(ll a,ll b)\n{ll res=1ll,tp=a;\nwhile (b)\n{if (b&1ll) {res*=tp;res%=mod;}\ntp*=tp;tp%=mod;b>>=1ll;\n}\nreturn res;\n}\ninline ll cal(int a,int b)\n{return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;}\ninline ll calc()\n{int i,j,l;fac[0]=1ll;\nll ti=max(n,k);\nfor (i=1;i<=ti;i++) fac[i]=fac[i-1]*((ll)(i))%mod;\ninv[ti]=qpow(fac[ti],mod-2ll);\nfor (i=ti-1;i>=0;i--) inv[i]=inv[i+1]*((ll)(i+1))%mod;\nfor (i=1;i<=n;i++) dp[1][i]=inv[i];\nll ans=dp[1][n]*cal(k,1)%mod;\nint t=min(18,((int)(k)));\nfor (i=2;i<=t;i++)\n{for (j=n;j;j=(j-1)&n)\n{if (!dp[i-1][j]) continue;\nint r=n^j;\nfor (l=r;l>j;l=(l-1)&r)\n{dp[i][j^l]+=dp[i-1][j]*inv[l];\nif (dp[i][j^l]>inf) dp[i][j^l]%=mod;\n}\n}\nfor (j=n;j;j=(j-1)&n) \n{if (dp[i][j]>=mod) dp[i][j]%=mod;\n}\nll res=dp[i][n]*cal(k,i);res%=mod;\nres=res*fac[i]%mod;\nans=(ans+res>=mod?ans+res-mod:ans+res);\n}\nans*=fac[n];ans%=mod;\nreturn ans;\n}\nint main (){\n\tcin>>n>>k>>mod;\n\tll ans=qpow(k,n);\n\tif (n%2==0) ans=(ans-calc()+mod)%mod;\n\tcout<\n#define sqr(x) ((x)*(x))\n#define ll long long\n#define ull unsigned long long\n#define ui unsigned int\n#define ld long double\n#define vi vector\n#define HEAP(T) priority_queue\n#define heap(T) priority_queue,greater > \n#define pii pair\n#define pb push_back\n#define mp make_pair\n#define debuge cerr<<\"isok\"< inline void gmin(T &x,const T &y){if(x>y) x=y;}\ntemplate inline void gmax(T &x,const T &y){if(x inline void ch(T &x,const S y){x=(x+y)%mod;}\nconst int BufferSize=1<<16;\nchar buffer[BufferSize],*Bufferhead,*Buffertail;\nbool Terminal;\ninline char Getchar(){\n\tif(Bufferhead==Buffertail){\n\t\tint l=fread(buffer,1,BufferSize,stdin);\n\t\tif(!l){Terminal=1;return 0;}\n\t\tBuffertail=(Bufferhead=buffer)+l;\n\t}\n\treturn *Bufferhead++;\n}\ntemplateinline bool read(T &x){\n\tx=0;char c=Getchar(),rev=0;\n\twhile(c<'0'||c>'9'){c=Getchar();rev|=c=='-';if(Terminal)return 0;}\n\twhile(c>='0'&&c<='9') x=x*10+c-'0',c=Getchar();\n\tx=rev?-x:x;\n\treturn 1;\n}\ninline bool reads(char *x){\n\tchar c=Getchar();\n\twhile(c<33||c>126){c=Getchar();if(Terminal)return 0;}\n\twhile(c>=33&&c<=126) (*x++)=c,c=Getchar();\n\t*x=0;return 1;\n}\ntemplateinline void print(T x,const char c){\n\tif(!x){putchar('0');putchar(c);return;}\n\tif(x<0) putchar('-'),x=-x;\n\tint m=0,a[10];\n\twhile(x) a[m++]=x%10,x/=10;\n\twhile(m--) putchar(a[m]+'0');\n\tputchar(c);\n}\ninline int exp(int x,int y,const int mod=::mod){\n\tint ans=1;\n\twhile(y){\n\t\tif(y&1) ans=(ll)ans*x%mod;\n\t\tx=(ll)x*x%mod;y>>=1;\n\t}return ans;\n}\n\nint n,k,fac[N],ifac[N],dp[2][N];\ninline int C(int x,int y){return (ll)fac[x]*ifac[x-y]%mod*ifac[y]%mod;}\n\nint main(){\n#ifdef rqgao2014\n\tfreopen(\"input.txt\",\"r\",stdin);\n#endif\n\tscanf(\"%d%d%d\",&n,&k,&mod);\n\tifac[0]=fac[0]=1;\n\tfor(int i=1;i<=n;i++) fac[i]=(ll)fac[i-1]*i%mod;\n\tifac[n]=exp(fac[n],mod-2);\n\tfor(int i=n-1;i;i--) ifac[i]=(ll)ifac[i+1]*(i+1)%mod;\n\tif(n&1){\n\t\tprintf(\"%d\\n\",exp(k,n));\n\t\treturn 0;\n\t}\n\tint ans=0;\n\tdp[0][n]=1;\n\tfor(int i=1;i<=k;i++){\n\t\tclr(dp[i&1],0);\n\t\tint res=0;\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tint tmp=dp[i-1&1][j];\n\t\t\tif(!tmp) continue;\n\t\t\tint u=j^(j&-j);\n\t\t\tfor(int k=u;k;k=(k-1)&u)\n\t\t\t\tch(dp[i&1][k],(ll)tmp*C(j,k));\n\t\t\tch(res,tmp);\n\t\t}\n\t\tch(ans,(ll)res*ifac[k-i]);\n\t}\n\tans=(ll)ans*fac[k]%mod;\n\tans=(exp(k,n)-ans+mod)%mod;\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "447e8ab42a9ceb844ba337fef66f3f8f", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\n@Date : 2020-10-24 08:06:07\n@Author : Adscn (adscn@qq.com)\n@Link : https://www.cnblogs.com/LLCSBlog\n*/\n#include\nusing namespace std;\ntypedef long long ll;\n#define IL inline\n#define RG register\n#define gi geti()\n#define gl geti()\n#define gc getchar()\n#define File(a) freopen(a\".in\",\"r\",stdin);freopen(a\".out\",\"w\",stdout)\ntemplateIL bool chkmax(T &x,const T &y){return xIL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}\ntemplate\nIL T geti()\n{\n\tRG T xi=0;\n\tRG char ch=gc;\n\tbool f=0;\n\twhile(!isdigit(ch))ch=='-'?f=1:f,ch=gc;\n\twhile(isdigit(ch))xi=xi*10+ch-48,ch=gc;\n\treturn f?-xi:xi;\n}\ntemplate\nIL void pi(T k,char ch=0)\n{\n\tif(k<0)k=-k,putchar('-');\n\tif(k>=10)pi(k/10);\n\tputchar(k%10+'0');\n\tif(ch)putchar(ch);\n}\nconst int N=3e5+10;\nint P;\nint f[19][N];\nint ifac[N],fac[N],inv[N];\ninline ll fpow(ll a,int b,ll c=1){\n\tfor(;b;b>>=1,a=a*a%P)if(b&1)c=c*a%P;\n\treturn c;\n}\ninline void init(int n){\n\tfac[0]=1;\n\tfor(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%P;\n\tifac[n]=fpow(fac[n],P-2);\n\tfor(int i=n-1;~i;--i)ifac[i]=ifac[i+1]*(i+1ll)%P;\n\tinv[1]=1;\n\tfor(int i=2;i<=n;++i)inv[i]=1ll*(P-P/i)*inv[P%i]%P;\n}\ninline void add(int &x,int y){x+=y;if(x>=P)x-=P;}\ninline void sub(int &x,int y){x-=y;x-=(x>>31)*P;}\ninline void FMT(int *f,int n)\n{\n\tfor(int l=1;l\n\nusing namespace std;\n\nusing ll = long long;\nusing vi = vector;\nusing db = double;\n#define pb push_back\n#define fir first\n#define sec second\n\nint n, k, mod;\nint f[250006][27];\n\n#define lbit(i) ((i)&-(i))\n\nconst int N = 250001;\nint fac[N], inv[N], ifac[N];\n\nint main() {\n\t\n\tcin >> n >> k >> mod;\n\t\n\tinv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1;\n\tfor (int i = 2; i < N; ++i) {\n\t\tinv[i] = (ll)mod / i * (mod - inv[mod % i]) % mod;\n\t\tfac[i] = (ll)fac[i - 1] * i % mod;\n\t\tifac[i] = (ll)ifac[i - 1] * inv[i] % mod;\n\t}\n\t\n\tll ans = 1;\n\tfor (int i = 1; i <= n; ++i)\n\t\t(ans *= k) %= mod;\n\t\n\tif (n % 2 == 0) {\n\t\tf[0][0] = 1;\n\t\tfor (int i = 1; i <= n; ++i) if ((n & i) == i) // 要从小到大枚举子集\n\t\t\tfor (int msk = i - lbit(i), j = msk; ; --j &= msk) {\n\t\t\t\tint len = min(k - 1, __builtin_popcount(j));\n\t\t\t\tfor (int t = 0; t <= len; ++t)\n\t\t\t\t\tf[i][t + 1] = (f[i][t + 1] + (ll)f[j][t] * ifac[i - j]) % mod;\n\t\t\t\tif (!j) break;\n\t\t\t}\n\t\tfor (int i = 1; i <= k; ++i)\n\t\t\t(ans -= (ll)f[n][i] * fac[n] % mod * fac[k] % mod * ifac[k - i]) %= mod;\n\t}\n\t\n\tcout << (ans + mod) % mod << endl;\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "90ab06ae28c274780b9851d02a7d224e", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nconst int N=250005,M=1<<17,D=18;\n\nint P;\nint power(int a,int t){\n\tint r=1;\n\twhile(t){\n\t\tif(t&1)r=(ll)r*a%P;\n\t\ta=(ll)a*a%P;t>>=1;\n\t}\n\treturn r;\n}\n\nint L,K,fac[N],n,m,bit[20],ifac[N],bitcnt[N];\nll suma[D][M],sumc[D][M];\null dp[D][M],tmp[D][M];\n\nint main(){\n\tscanf(\"%d%d%d\",&L,&K,&P);\n\tfac[0]=1;\n\tfor(int i=1;i<=L;i++){\n\t\tfac[i]=(ll)fac[i-1]*i%P;\n\t\tbitcnt[i]=bitcnt[i>>1]+(i&1);\n\t}\n\tll all=power(K,L);\n\tif(L&1)printf(\"%lld\\n\",all),exit(0);\n\tifac[L]=power(fac[L],P-2);\n\tfor(int i=L;i>=1;i--)ifac[i-1]=(ll)ifac[i]*i%P;\n\tfor(int i=1;(1<>i&1)bit[n++]=1<>j&1)s+=bit[j];\n\t\tsuma[bitcnt[i]][i]=ifac[s];\n\t}\n\tfor(int t=0;t<=n;t++){\n\t\tint i,j,k,l;\n\t\tll*a=suma[t];\n\t\tfor(l=m;l>=2;l>>=1)for(j=0,i=l>>1;j>=1;\n\tfor(cc--,revK>>=1;cc;cc--,revK>>=1){\n\t\tfor(int t=0;t<=n;t++)memset(tmp[t],0,m<<3);\n\t\tfor(int a=0;a<=n;a++)for(int j=0;j>1;j\nusing namespace std;\nconst int N=266666;\nint n,k,i,j,u,v,w,M=1000000009;\nlong long fac[N],inv[N],dp[30][22],a[18][N],b[N];\nlong long qpow(long long a,long long b)\n{\n\tlong long s=a,ans=1;\n\twhile(b)\n\t{\n\t\tif(b&1)\n\t\t\tans=ans*s%M;\n\t\tb>>=1;\n\t\ts=s*s%M;\n\t}\n\treturn ans;\n}\nint main()\n{\n\tscanf(\"%d %d %d\",&n,&k,&M);\n\tif(n&1)\n\t{\n\t\tcout<=0;--i)\n\t\tinv[i]=inv[i+1]*(i+1)%M;\n\tn>>=1;\n\tfor(i=0;i<=n;++i)\n\t{\n\t\tint pp=__builtin_popcount(i);\n\t\ta[pp][i]=inv[i*2];\n\t}\n\tfor(i=0;i<17;++i)\n\t\tfor(j=0;j<17;++j)\n\t\t\tfor(u=0;u<(1<<17);++u)\n\t\t\t\tif((u>>j)&1)\n\t\t\t\t\ta[i][u]+=a[i][u^(1<>i)&1)\n\t\t\t\tb[j]-=b[j^(1<\n\n#define F first\n#define S second\n#define PB push_back\n#define sz(s) int((s).size())\n#define bit(n,k) (((n)>>(k))&1)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\ntypedef unsigned int ui;\n\nconst int Lg=18,Tw=(1<>=1;\n }\n return ans;\n}\ninline void Add(int *a,int *b){\n for(int i=0;i<=N;i++)\n\ta[i]=(a[i]+b[i])%mod;\n}\ninline void Sub(int *a,int *b){\n for(int i=0;i<=N;i++)\n\ta[i]=(a[i]-b[i])%mod;\n}\ninline void Mul(int *ans,int *a,int *b){\n memset(tmp,0,sizeof tmp);\n for(int i=0;i<=N;i++){\n\tfor(int j=0;j<=i;j++){\n\t tmp[i]=(1ll*tmp[i]+1ll*a[j]*b[i-j])%mod;\n\t}\n }\n memcpy(ans,tmp,sizeof tmp);\n}\nvoid trans1(int a[][Lg+3]){\n for(int i=0;i=0;msk--)\n\t if(bit(msk,i))\n\t\tAdd(a[msk],a[msk ^ (1<=0;msk--)\n\t if(bit(msk,i))\n\t\tSub(a[msk],a[msk ^ (1<vec;\n\nint main(){\n int K;cin>>n>>K>>mod;\n int ANS=Pow(K,n);\n if(n&1) return cout<=0;i--)\n\tifac[i]=1ll*(i+1)*ifac[i+1] %mod;\n for(int i=0;i<=n;i++){\n\tint Real=0;\n\tfor(int j=0;j>=1;\n\t}\n\tmemcpy(Ans[i],tmp2,sizeof tmp2);\n }\n trans2(Ans);\n ANS=(1ll*ANS-1ll*Ans[n][N]*fac[Nbef])%mod;\n if(ANS<0) ANS+=mod;\t\n return cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define REP(i,a,n) for(int i=a;i<=n;++i)\n#define PER(i,a,n) for(int i=n;i>=a;--i)\n#define hr putchar(10)\n#define pb push_back\n#define lc (o<<1)\n#define rc (lc|1)\n#define mid ((l+r)>>1)\n#define ls lc,l,mid\n#define rs rc,mid+1,r\n#define x first\n#define y second\n#define io std::ios::sync_with_stdio(false)\n#define endl '\\n'\n#define DB(a) ({REP(__i,1,n) cout<>=1)if(n&1)r=r*a%P;return r;}\nll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}\n\nvoid add(int &a, ll b) {a=(a+b)%P;}\n\nint main() {\n\tscanf(\"%d%d%d\",&n,&k,&P);\n\tfac[0] = 1;\n\tREP(i,1,N-1) fac[i]=(ll)fac[i-1]*i%P;\n\tifac[N-1] = inv(fac[N-1]);\n\tPER(i,0,N-2) ifac[i]=(ll)ifac[i+1]*(i+1)%P;\n\tint ans = qpow(k,n);\n\tif (n%2==0) {\n\t\tf[n] = fac[n];\n\t\tREP(z,1,k) {\n\t\t\tREP(i,0,n) g[i] = 0;\n\t\t\tfor (int i=n; i; i=(i-1)&n) if (f[i]) {\n\t\t\t\tfor (int j=i; 2*j>=i; j=(j-1)&i) {\n\t\t\t\t\tadd(g[i^j],(ll)f[i]*ifac[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i,0,n) swap(f[i],g[i]);\n\t\t\tint res = f[0];\n\t\t\tREP(j,0,z-1) res = (ll)res*(k-j)%P;\n\t\t\tans = (ans-res)%P;\n\t\t}\n\t}\n\tif (ans<0) ans += P;\n\tprintf(\"%d\\n\", ans);\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "63564a23af5522f0c70b9179203a81b9", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\n@Date : 2020-10-24 08:06:07\n@Author : Adscn (adscn@qq.com)\n@Link : https://www.cnblogs.com/LLCSBlog\n*/\n#include\nusing namespace std;\ntypedef long long ll;\n#define IL inline\n#define RG register\n#define gi geti()\n#define gl geti()\n#define gc getchar()\n#define File(a) freopen(a\".in\",\"r\",stdin);freopen(a\".out\",\"w\",stdout)\ntemplateIL bool chkmax(T &x,const T &y){return xIL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}\ntemplate\nIL T geti()\n{\n\tRG T xi=0;\n\tRG char ch=gc;\n\tbool f=0;\n\twhile(!isdigit(ch))ch=='-'?f=1:f,ch=gc;\n\twhile(isdigit(ch))xi=xi*10+ch-48,ch=gc;\n\treturn f?-xi:xi;\n}\ntemplate\nIL void pi(T k,char ch=0)\n{\n\tif(k<0)k=-k,putchar('-');\n\tif(k>=10)pi(k/10);\n\tputchar(k%10+'0');\n\tif(ch)putchar(ch);\n}\nconst int N=3e5+10;\nint P=1e9+9;\nint f[19][N];\nint ifac[N],fac[N],inv[N];\ninline ll fpow(ll a,int b,ll c=1){\n\tfor(;b;b>>=1,a=a*a%P)if(b&1)c=c*a%P;\n\treturn c;\n}\ninline void init(int n){\n\tfac[0]=1;\n\tfor(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%P;\n\tifac[n]=fpow(fac[n],P-2);\n\tfor(int i=n-1;~i;--i)ifac[i]=ifac[i+1]*(i+1ll)%P;\n\tinv[1]=1;\n\tfor(int i=2;i<=n;++i)inv[i]=1ll*(P-P/i)*inv[P%i]%P;\n}\ninline void add(int &x,int y){x+=y;if(x>=P)x-=P;}\ninline void sub(int &x,int y){x-=y;x-=(x>>31)*P;}\ninline int sub(int x){return x-(x>>31)*P;}\nint t[20],g[20];\ninline void fpow(int *f,int n,int k){\n\tmemcpy(t,f,4*n+4);\n\tf[0]=1-f[0],g[0]=0;\n\tfor(int i=1;i\n#include \n#include \n#include \n#include \nusing namespace std;\nusing ll = long long;\n\n#define File(s) freopen(s\".in\", \"r\", stdin), freopen(s\".out\", \"w\", stdout)\nnamespace io {\n const int SIZE = (1 << 21) + 1;\n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;\n inline char getc () {return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++);}\n inline void flush () {fwrite (obuf, 1, oS - obuf, stdout); oS = obuf;}\n inline void putc (char x) {*oS ++ = x; if (oS == oT) flush ();}\n template\n inline void read(T &x) {\n char ch; int f = 1;\n x = 0;\n while(isspace(ch = getc()));\n if(ch == '-') ch = getc(), f = -1;\n do x = x * 10 + ch - '0'; while(isdigit(ch = getc()));\n x *= f;\n }\n template\n inline void read(T &x, Args&... args) {read(x); read(args...);}\n template\n inline void write(T x) {\n static char stk[128];\n int top = 0;\n if(x == 0) {putc('0'); return;}\n if(x < 0) putc('-'), x = -x;\n while(x) stk[top++] = x % 10, x /= 10;\n while(top) putc(stk[--top] + '0');\n }\n template\n inline void write(T x, Args... args) {write(x); putc(' '); write(args...);}\n inline void space() {putc(' ');}\n inline void endl() {putc('\\n');}\n struct _flush {~_flush() {flush();}} __flush;\n};\nusing io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;\n\nint M;\ninline int add(int x, int y) { return x + y >= M ? x + y - M : x + y; }\ntemplate inline int add(int x, int y, Args... args) { return add(add(x, y), args...); }\ninline int sub(int x, int y) { return x - y < 0 ? x - y + M : x - y; }\ninline int mul(int x, int y) { return 1LL * x * y % M; }\ntemplate inline int mul(int x, int y, Args... args) { return mul(mul(x, y), args...); }\ninline void inc(int &x, int y=1) { x += y; if(x >= M) x -= M; }\ninline void dec(int &x, int y=1) { x -= y; if(x < 0) x += M; }\ninline int power(int x, int y) {\n int res = 1;\n for(; y; y>>=1, x = mul(x, x)) if(y & 1) res = mul(res, x);\n return res;\n}\ninline int inv(int x) { return power(x, M - 2); }\n\nconst int N = 250005, K = 19;\nint f[N][K];\nint n, k;\n\nint fac[N], ifac[N];\nvoid prefac(int n) {\n fac[0] = 1;\n for (int i = 1; i <= n; ++i) fac[i] = mul(fac[i - 1], i);\n ifac[n] = inv(fac[n]);\n for (int i = n - 1; i >= 0; --i) ifac[i] = mul(ifac[i + 1], i + 1);\n}\n\nint dp(int s, int c) {\n if (!s) return 1;\n if (c == k) return 0;\n int &res = f[s][c];\n if (res != -1) return res;\n res = 0;\n int hb = 1;\n while (hb <= s) hb <<= 1;\n hb >>= 1;\n for (int t = s; t != -1; --t) {\n t &= s;\n if (!(t & hb)) return res;\n inc(res, mul(ifac[t], k - c, dp(s ^ t, c + 1)));\n }\n return res;\n}\n\nint main() {\n memset(f, -1, sizeof(f));\n read(n, k, M);\n prefac(n);\n if (n & 1) {\n write(power(k, n)), endl();\n return 0;\n }\n write(sub(power(k, n), mul(fac[n], dp(n, 0)))), endl();\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "3e82c7c1161085ba9c348982a0bf1642", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint n,k,p;\nint fpow(int a,int b){\n\tint ans=1,t=a;\n\twhile(b){\n\t\tif(b&1)ans=1ll*ans*t%p;\n\t\tt=1ll*t*t%p;b>>=1;\n\t}\n\treturn ans;\n}\nint lowbit(int x){\n\treturn x&(-x);\n}\nint f[30][250005];\nint fac[250005],inv[250005];\nvoid init()\n{\n\tfac[0]=1;\n\tfor(int i=1;i<=n;i++)\n\t\tfac[i]=1ll*fac[i-1]*i%p;\n\tinv[n]=fpow(fac[n],p-2);\n\tfor(int i=n;i>=1;i--)\n\t\tinv[i-1]=1ll*inv[i]*i%p;\n\t//return ;\n}\nint solve(int cur,int S){\n\tif(f[cur][S]!=-1)\n\t\treturn f[cur][S];\n\tif(!S){\n\t\tf[cur][S]=fac[n];\n\t\tfor(int i=1;i<=cur;i++)\n\t\t\tf[cur][S]=1ll*f[cur][S]*(k-i+1)%p;\n\t\treturn f[cur][S];\n\t}\n\tf[cur][S]=0;\n\tint U=S-lowbit(S);\n\tfor(int T=U;T;T=(T-1)&U)\n\t\tf[cur][S]=(f[cur][S]+1ll*inv[S-T]*solve(cur+1,T))%p;\n\tf[cur][S]=(f[cur][S]+1ll*inv[S]*solve(cur+1,0))%p;\n\treturn f[cur][S];\n}\nint main(){\n\tscanf(\"%d %d %d\",&n,&k,&p);\n\tint ans=1;\n\tfor(int i=1;i<=n;i++)\n\t\tans=1ll*ans*k%p;\n\tif(n%2==0){\n\t\tinit();\n\t\tmemset(f,-1,sizeof(f));\n\t\tans=(ans-solve(0,n)+p)%p;\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "2853f61c67f6c40f5c18511335d703ce", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \n#define all(a) (a).begin(), (a).end()\n#define sz(a) (int)(a).size()\n#define pb push_back\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\ntypedef double ld;\n \n// map, bool> mp;\n \n// bool f(const vector &a) {\n// int l = 0;\n// int x = 1;\n// for (int i = 1; i <= sz(a); ++i) {\n// if (i < sz(a) && a[i] == a[i - 1]) {\n// ++x;\n// } else {\n// x >>= 1;\n// for (; x > 0; x >>= 1)\n// l -= x;\n// x = 1;\n// } \n// }\n// int sum = sz(a);\n// sum >>= 1;\n// for (; sum > 0; sum >>= 1)\n// l += sum;\n// return l > 0;\n// }\n \n// bool solve(vector a) {\n// if (mp.count(a)) {\n// return mp[a];\n// }\n// for (int i = 0; i < sz(a); ++i) {\n// if (i == sz(a) - 1 || a[i] != a[i + 1]) {\n// vector c = a;\n// c.erase(c.begin() + i);\n// if (!solve(c)) {\n// return mp[a] = 1;\n// }\n// }\n// }\n// return mp[a] = (f(a) > 0 ? 0 : 1);\n// }\n \n// void solve(int n, int lim, vector &a) {\n// if (n == 0) {\n// solve(a);\n// return;\n// }\n// // for (int i = 0; i < sz(a); ++i)\n// // cout << a[i] << \" \";\n// // cout << \"\\n\";\n \n// if (n >= lim) {\n// a.push_back(lim);\n// solve(n - 1, lim, a);\n// a.pop_back();\n// }\n// if (lim > 1) {\n// a.push_back(lim - 1);\n// solve(n - 1, lim - 1, a);\n// a.pop_back();\n// }\n// if (sz(a) == 0 && lim > 1) {\n// solve(n, lim - 1, a);\n// }\n// } \n \nll binPow(ll a, int b, int p) {\n if (b == 0)\n return 1;\n ll x = binPow(a, b / 2, p);\n x *= x;\n x %= p;\n if (b & 1) {\n x *= a;\n x %= p;\n }\n return x;\n}\n\nint n, k, p;\nconst int MAXN = 250250;\nll fact[MAXN], rfact[MAXN];\nll pw[MAXN];\nint cnt = 0;\n \nll g[MAXN][17];\n\nvoid solve(int n, const vector &bit, int k, int l) {\n if (k == sz(bit)) {\n ++cnt;\n for (int i = 1; i < 17; ++i) {\n g[n][i] += rfact[l] * g[n ^ l][i - 1];\n if (cnt >= 8) {\n g[n][i] %= p;\n }\n }\n if (cnt >= 8)\n cnt = 0;\n return;\n }\n solve(n, bit, k + 1, l);\n solve(n, bit, k + 1, l ^ (1 << bit[k]));\n}\n \nint main() {\n // freopen(\"output.txt\", \"w\", stdout);\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n // int n;\n // cin >> n;\n // vector a;\n // mp[a] = 0;\n // solve(n, n, a);\n \n // for (auto a : mp) {\n // if (sz(a.first) < n)\n // continue;\n // cout << a.second << \" ---> \";\n // for (int i = 0; i < sz(a.first); ++i)\n // cout << a.first[i] << \" \";\n // cout << \"--- \" << f(a.first);\n // cout << \"\\n\";\n // }\n \n cin >> n >> k >> p;\n if (n & 1) {\n cout << binPow(k, n, p) << \"\\n\";\n return 0;\n }\n \n fact[0] = 1;\n rfact[0] = 1;\n for (int i = 1; i <= n; ++i) {\n fact[i] = fact[i - 1] * i % p;\n rfact[i] = binPow(fact[i], p - 2, p);\n }\n \n g[0][0] = 1;\n for (int i = 1; i <= n; ++i) {\n if ((i & n) != i)\n continue;\n vector bit;\n for (int j = 0; j < 18; ++j) {\n if ((1 << j) & i)\n bit.push_back(j);\n }\n solve(i, bit, 1, 1 << bit[0]);\n for (int j = 0; j < 17; ++j)\n g[i][j] %= p;\n }\n ll ans = 0;\n for (int i = 1; i <= min(16, k); ++i) {\n g[n][i] %= p;\n ll cur = fact[n] * g[n][i];\n cur %= p;\n cur *= fact[k];\n cur %= p;\n cur *= rfact[k - i];\n cur %= p;\n ans += cur;\n ans %= p;\n }\n ans = binPow(k, n, p) - ans;\n if (ans < 0)\n ans += p;\n cout << ans << \"\\n\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "f3ef7df6d6ae60726af1c9108076abc5", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\n#define ms(s, n) memset(s, n, sizeof(s))\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)\n#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)\n#define sz(a) int((a).size())\n#define present(t, x) (t.find(x) != t.end())\n#define all(a) (a).begin(), (a).end()\n#define uni(a) (a).erase(unique(all(a)), (a).end())\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define fi first\n#define se second\n#define prec(n) fixed<> (i)) & 1)\n#define bitcount(n) __builtin_popcountll(n)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pi;\ntypedef vector vi;\ntypedef vector vii;\nconst int MOD = (int) 1e9 + 7;\nconst int MOD2 = 1007681537;\nconst int INF = (int) 1e9;\nconst ll LINF = (ll) 1e18;\nconst ld PI = acos((ld) -1);\nconst ld EPS = 1e-9;\ninline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}\ninline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}\ninline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}\ntemplate inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}\ntemplate inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}\ninline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}\ninline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}\ninline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}\ninline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}\ninline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}\ninline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}\ninline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}\ninline int sign(ld x, ld y) {return sign(x - y);}\n#define db(x) cerr << #x << \" = \" << (x) << \" \";\n#define endln cerr << \"\\n\";\n\nvoid transform(int a[], int n, int p, int invert) {\n int k = __lg(n);\n FOR(i, 0, k) {\n FOR(msk, 0, n) if (bit(msk, i)) {\n int pmsk = msk ^ (1 << i);\n if (!invert) {\n addmod(a[msk], a[pmsk], p);\n }\n else {\n submod(a[msk], a[pmsk], p);\n }\n }\n }\n}\n\nvector pow_convol(vector a, int k, int p) {\n int n = a.size();\n if (k == 1) {\n return a;\n }\n else if (k & 1) {\n vector r = pow_convol(a, k - 1, p);\n vector res(n, 0);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n res[i + j] = (res[i + j] + (long long) r[i] * a[j]) % p;\n }\n }\n return res;\n }\n else {\n vector r = pow_convol(a, k >> 1, p);\n vector res(n, 0);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n res[i + j] = (res[i + j] + (long long) r[i] * r[j]) % p;\n }\n }\n return res;\n }\n}\n\nvector subset_pow_convol(vector a, int k, int p) {\n static const int logn = 19;\n static int fa[logn][1 << logn];\n static int fb[logn][1 << logn];\n static int fc[logn][1 << logn];\n int n = a.size();\n while (n != (n & -n)) n += n & -n;\n vector res(n, 0);\n for (int i = 0; i < logn; i++) {\n for (int j = 0; j < n; j++) {\n if (bitcount(j) == i) {\n fa[i][j] = j < sz(a) ? a[j] : 0;\n }\n else {\n fa[i][j] = 0;\n }\n fc[i][j] = 0;\n }\n transform(fa[i], n, p, 0);\n }\n FOR(i, 0, n) {\n vi va(logn);\n FOR(j, 0, logn) {\n va[j] = fa[j][i];\n }\n va = pow_convol(va, k, p);\n FOR(j, 0, logn) {\n fc[j][i] = va[j];\n }\n }\n for (int i = 0; i < logn; i++) {\n transform(fc[i], n, p, 1);\n for (int j = 0; j < n; j++) if (bitcount(j) == i) {\n res[j] = fc[i][j];\n }\n }\n return res;\n}\n\nconst int maxn = 1e6 + 5;\nint n, k, p;\nint fac[maxn];\nint ifac[maxn];\nint dp[2][maxn];\nint f[maxn];\n\nvoid solve() {\n cin >> n >> k >> p;\n int nn = n;\n FOR(i, 0, n + 1) if ((n & i) == i) {\n int t = 0;\n FOR(j, 0, 20) if (bit(n, j)) {\n t <<= 1;\n t |= bit(i, j);\n }\n f[t] = i;\n nn = t;\n }\n if (n & 1) {\n cout << fpow(k, n, p) << \"\\n\";\n return;\n }\n fac[0] = 1; FOR(i, 1, maxn) fac[i] = mult(fac[i - 1], i, p);\n FOR(i, 0, maxn) ifac[i] = inv(fac[i], p);\n vi a(nn + 1, 0);\n FOR(i, 0, nn + 1) a[i] = ifac[f[i]];\n int res = fpow(k, n, p);\n a = subset_pow_convol(a, k, p);\n submod(res, mult(a[nn], fac[n], p), p);\n cout << res << \"\\n\";\n}\n\nint main() {\n int JUDGE_ONLINE = 1;\n if (fopen(\"in.txt\", \"r\")) {\n JUDGE_ONLINE = 0;\n assert(freopen(\"in.txt\", \"r\", stdin));\n //assert(freopen(\"out.txt\", \"w\", stdout));\n }\n else {\n ios_base::sync_with_stdio(0), cin.tie(0);\n }\n solve();\n if (!JUDGE_ONLINE) {\n //cout << \"\\nTime elapsed: \" << 1000 * clock() / CLOCKS_PER_SEC << \"ms\\n\";\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "655a96f6357ab1f073c891a64e49be8f", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint n,k,mod;\nint fac[250003],inv[250003];\nint dp[27][250003];\nint lowbit(int x){return x&(-x);}\nint fpow(int x,int y)\n{\n\tint res=1;\n\twhile(y)\n\t{\n\t\tif(y&1) res=1ll*res*x%mod;\n\t\tx=1ll*x*x%mod;\n\t\ty>>=1;\n\t}\n\treturn res;\n}\nvoid init()\n{\n\tfac[0]=1;\n\tfor(int i=1;i<=n;i++)\n\t\tfac[i]=1ll*fac[i-1]*i%mod;\n\tinv[n]=fpow(fac[n],mod-2);\n\tfor(int i=n-1;i>=0;i--)\n\t\tinv[i]=1ll*inv[i+1]*(i+1)%mod;\n}\nint dfs(int x,int s)\n{\n\tif(dp[x][s]!=-1) return dp[x][s];\n\tif(s==0) return dp[x][s]=1ll*fac[n]*fac[k]%mod*inv[k-x]%mod;\n\tint now=s-lowbit(s);\n\tdp[x][s]=0;\n\tfor(int t=now;t;t=(t-1)&now)\n\t\tdp[x][s]=(dp[x][s]+1ll*dfs(x+1,t)*inv[s-t]%mod)%mod;\n\tdp[x][s]=(dp[x][s]+1ll*dfs(x+1,0)*inv[s]%mod)%mod;\n\treturn dp[x][s];\n}\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&k,&mod);\n\tinit();\n\tint ans=fpow(k,n);\n\tif(n%2==0) \n\t{\n\t\tmemset(dp,-1,sizeof(dp));\n\t\tans=(ans-dfs(0,n)+mod)%mod;\n\t}\n\tprintf(\"%d\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "f832b656b3aa976343932884eb8dd958", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\nint MOD;\nint mpow(int x, int n) {\n\tint res = 1;\n\twhile (n) {\n\t\tif (n & 1) res = 1LL * res * x % MOD;\n\t\tx = 1LL * x * x % MOD;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\nconst int N = 3e5 + 5;\nunsigned long long dp[27][N];\nint rf[N], f[N];\nint permute(int n, int m) {\n\treturn 1LL * f[n] * rf[n - m] % MOD;\n}\nint main() {\n\tint n, K;\n\tscanf(\"%d%d%d\", &n, &K, &MOD);\n\tif (n & 1) {\n\t\tprintf(\"%d\\n\", mpow(K, n));\n\t\treturn 0;\n\t}\n\tf[0] = 1;\n\tfor (int i = 1; i <= n; ++ i) f[i] = 1LL * f[i - 1] * i % MOD;\n\trf[n] = mpow(f[n], MOD - 2);\n\tfor (int i = n - 1; i >= 0; -- i) rf[i] = 1LL * rf[i + 1] * (i + 1) % MOD;\n\tdp[0][0] = 1;\n\tfor (int k = 0; k < K; ++ k) {\n\t\tfor (int i = 1; i <= n; ++ i) {\n\t\t\tif ((i & n) != i) continue;\n\t\t\tint cnt = 0;\n\t\t\tunsigned long long &res = dp[k + 1][i];\n\t\t\tint hb = (1 << (31 - __builtin_clz(i)));\n\t\t\tfor (int j = i - hb; j >= 0; j = (j - 1) & i) {\n\t\t\t\tint jj = j + hb;\n\t\t\t\tres += dp[k][i ^ jj] * rf[jj];\n\t\t\t\tif (cnt ++ == 17) {\n\t\t\t\t\tres %= MOD;\n\t\t\t\t\tcnt = 0;\n\t\t\t\t}\n\t\t\t\tif (j == 0) break;\n\t\t\t}\n\t\t\tres %= MOD;\n\t\t}\n\t}\n\tint ans = 0;\n\tfor (int i = 0; i <= K; ++ i) (ans += 1LL * f[n] * dp[i][n] % MOD * permute(K, i) % MOD) %= MOD;\n\tprintf(\"%d\\n\", (mpow(K, n) + MOD - ans) % MOD);\n}\n\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "b1644dfa064dce77b5ec8bd03b239e6b", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n/*\np = i * r + x = 0 (% p)\n-i * r * inv(x) = 1 (% p)\ninv(i) = -r * inv(x) (% p)\n*/\n\nconst int N = 250010;\nlong long fac[N], inv[N];\nlong long dp[30][N];\nint bit[N];\nlong long n, m, mod;\n\nlong long com(int n, int m) {\n return fac[n] * inv[n - m] % mod * inv[m] % mod;\n}\n\nvoid add(long long &x, long long y) {\n x += y;\n if (x >= mod) x %= mod;\n}\n\nint main()\n{\n cin >> n >> m >> mod; \n inv[0] = fac[0] = inv[1] = fac[1] = 1;\n for (int i = 2; i <= n; i++) {\n int r = mod / i;\n int x = mod - i * r;\n fac[i] = fac[i - 1] * i % mod;\n inv[i] = mod -1LL * r * inv[x] % mod;\n }\n for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] * inv[i] % mod;\n long long ans = 1;\n for (int i = 1; i <= n; i++) ans = ans * m % mod;\n for (int i = 1; i <= n; i++) bit[i] = bit[i >> 1] + (i & 1);\n if (n & 1) {\n cout << ans << endl;\n return 0;\n }\n dp[0][0] = 1;\n for (int i = 0; i < min(0LL + bit[n], m); i++) {\n for (int j = 0; j < n; j++) {\n if (!dp[i][j]) continue;\n int x = n ^ j;\n for (int k = x, r; k > j; k = (k - 1) & x) {\n r = k | j;\n add(dp[i + 1][r], dp[i][j] * inv[k]); \n }\n }\n }\n for (int i = 1; i <= m; i++) \n ans -= dp[i][n] * fac[m] % mod * inv[m - i] % mod * fac[n] % mod;\n ans = (mod + ans % mod) % mod;\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "65ae20b77b0dac34a005242f92f39ae0", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define U 20\n#define MAXN 300000\nusing namespace std;\ninline int read(){\n\tint x=0,f=1;\n\tchar ch=getchar();\n\twhile (ch<'0'||ch>'9'){\n\t\tif (ch=='-') f=-1;\n\t\tch=getchar();\n\t}\n\twhile (ch>='0'&&ch<='9'){\n\t\tx=x*10+(ch-'0');\n\t\tch=getchar();\n\t}\n\treturn x*f;\n}\nlong long F[U][MAXN],G[U][MAXN];\nint cnt[MAXN],fac[MAXN],ifac[MAXN];\nint MOD;\nint n,m,num,k;\nvoid Multiply(long long F[][MAXN],long long G[][MAXN]){\n\tfor (int s=m;s>=0;--s){\n\t\tfor (int i=n;i>=0;--i){\n\t\t\tfor (int j=0;j>=1;\n }\n return ans;\n}\nvoid FWT_or(long long *f,int opr){\n int maxn=m;\n\tfor (int len=1;len=0;--i){\n \t\tfor (register int j=0;j=0;--i){\n \t\tfor (register int j=0;j>=1;\n\t}\n\tFWT_or(G[cnt[num]],-1);\n\t// cerr<\nusing namespace std;\n#define LL long long\n#define MN 250005\nint Mod;\nint ksm(int a,int x){\n\tLL ans=1,w=a;\n\twhile(x){\n\t\tif(x&1)ans=ans*w%Mod;\n\t\tw=w*w%Mod;\n\t\tx>>=1;\n\t}\n\treturn ans;\n}\nint n,k;\nint fac[MN],dfac[MN],inv[MN],f[19][MN];\nint solve(int use,int S){\n\tif(f[use][S]!=-1)return f[use][S];\n\tint ret=0;\n\tif(!S){\n\t\tint ret=fac[n];\n\t\tfor(int i=1;i<=use;++i)\n\t\t\tret=1LL*ret*(k-i+1)%Mod;\n//\t\tcerr<<\"!S \"<k)return 0;\n\tint lbit=S&(-S);\n\tint U=S-lbit;\n\tfor(int T=U;;T=(T-1)&U){\n//\t\tcerr<<\"? \"<\nusing namespace std;\n\nconst int Maxn = 1 << 19 | 5;\nint n, k, p, maxi, ct[Maxn], f[Maxn];\nint tmp1[20][Maxn], tmp2[20][Maxn], tmp3[20][Maxn];\nlong long pp;\nlong long t[20][Maxn];\nlong long fac[Maxn], inv[Maxn];\nlong long fast_pow(long long x, long long y)\n{\n long long ans = 1, now = x;\n while (y)\n {\n if (y & 1) ans = ans * now % p;\n now = now * now % p;\n y >>= 1;\n }\n return ans;\n}\nvoid init(void)\n{\n fac[0] = 1;\n for (int i = 1; i <= maxi; i++)\n fac[i] = fac[i - 1] * i % p;\n inv[maxi] = fast_pow(fac[maxi], p - 2);\n for (int i = maxi - 1; i >= 0; i--)\n inv[i] = inv[i + 1] * (i + 1) % p;\n}\nint lower(int x)\n{\n int tmp = 1;\n for (; tmp < x; tmp <<= 1);\n return tmp;\n}\nvoid FWT(int *now, int len, bool type = false)\n{\n\tlong long *T = t[0];\n\tfor (int i = 0; i < len; i ++) T[i] = now[i];\n\tif (type)\n\t\tfor (int i = 1; i < len; i <<= 1)\n\t\t\tfor (int j = 0; j < len; j += (i << 1))\n\t\t\t\tfor (int k = j; k < i + j; k++)\n\t\t\t\t\tT[i + k] -= T[k];\n\telse\n\t\tfor (int i = 1; i < len; i <<= 1)\n\t\t\tfor (int j = 0; j < len; j += (i << 1))\n\t\t\t\tfor (int k = j; k < i + j; k++)\n\t\t\t\t\tT[i + k] += T[k];\n\tfor (int i = 0; i < len; i ++) {\n\t\tnow[i] = T[i] % p;\n\t\tif (now[i] < 0) now[i] += p;\n\t}\n}\ninline void __a (long long &x) { x >= pp ? x -= pp : x; }\nvoid multi(void)\n{\n for (int j = 0; j <= 19; j++)\n {\n memset(tmp1[j], 0, sizeof(int[maxi + 1]));\n memset(t[j], 0, sizeof(long long[maxi + 1])); \n }\n for (int j = 0; j <= maxi; j++)\n tmp1[ct[j]][j] = f[j];\n for (int j = 0; j <= 19; j++)\n FWT(tmp1[j], maxi + 1);\n for (int q = 0; q <= 19; q++)\n for (int l = 0; q + l <= 19; l++)\n for (int s = 0; s <= maxi; s++)\n __a(t[q + l][s] += 1ll * tmp1[q][s] * tmp2[l][s]);\n for (int q = 0; q <= 19; q++)\n\t\tfor (int s = 0; s <= maxi; s++)\n\t\t\ttmp3[q][s] = t[q][s] % p;\n for (int j = 0; j <= 19; j++)\n FWT(tmp3[j], maxi + 1, true);\n for (int j = 0; j <= maxi; j++)\n f[j] = tmp3[ct[j]][j];\n}\nvoid multi2(void)\n{\n static int tmp[20][Maxn];\n for (int i = 0; i <= 19; i++)\n memset(t[i], 0, sizeof(long long[maxi + 1]));\n for (int q = 0; q <= 19; q++)\n for (int l = 0; q + l <= 19; l++)\n for (int s = 0; s <= maxi; s++)\n __a(t[q + l][s] += 1ll * tmp2[q][s] * tmp2[l][s]);\n for (int q = 0; q <= 19; q++)\n\t\tfor (int s = 0; s <= maxi; s++)\n\t\t\ttmp[q][s] = t[q][s] % p;\n for (int i = 0; i <= 19; i++)\n FWT(tmp[i], maxi + 1, true);\n for (int i = 0; i <= 19; i++)\n memset(tmp2[i], 0, sizeof(int[maxi + 1]));\n for (int i = 0; i <= maxi; i++)\n tmp2[ct[i]][i] = tmp[ct[i]][i];\n for (int i = 0; i <= 19; i++)\n FWT(tmp2[i], maxi + 1);\n}\nint main()\n{\n scanf(\"%d%d%d\", &n, &k, &p);\n\tpp = 1ll * p * p;\n if (n & 1)\n {\n printf(\"%lld\", fast_pow(k, n));\n return 0;\n }\n f[0] = 1;\n maxi = lower(n + 1) - 1;\n init();\n for (int i = 0; i <= maxi; i++)\n ct[i] = __builtin_popcount(i);\n for (int i = 0; i <= maxi; i++)\n tmp2[ct[i]][i] = inv[i];\n for (int i = 0; i <= 19; i++)\n FWT(tmp2[i], maxi + 1);\n int res_k = k;\n while (k)\n {\n cerr << k << endl;\n if (k & 1) multi();\n k >>= 1;\n if (k) multi2();\n }\n printf(\"%lld\", (fast_pow(res_k, n) - fac[n] * f[n] % p + p) % p);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "a7305f86ffcbeb6b6a81f14d77dc80e9", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#define rep(i,a,b) for(i=a;i<=b;i++)\nusing namespace std;\nconst int N=300010;\n//const int N=20;\nint n,K,mod;\nint dp[N][20];\nint cnt[N],mx[N];\nint cj[N],inv[N];\n\nint power(int x,int y){\n\tint ret=1;\n\tfor(;y;y>>=1,x=1ll*x*x%mod){\n\t\tif(y&1)\tret=1ll*ret*x%mod;\n\t}\n\treturn ret;\n}\n\nint C(int i,int j){\n\treturn 1ll*cj[i]*inv[j]%mod*inv[i-j]%mod;\n}\n\nint main(){\n//\tfreopen(\"a.in\",\"r\",stdin);\n//\tfreopen(\"a.out\",\"w\",stdout);\n\tscanf(\"%d%d%d\",&n,&K,&mod);\n\tint i,j,k;int ans=0;int down,up,tmp,now,cur1,cur2;\n\tif(n&1){printf(\"%d\\n\",power(K,n));return 0;}\n\tcj[0]=1;rep(i,1,n)\tcj[i]=1ll*cj[i-1]*i%mod;\n\tinv[n]=power(cj[n],mod-2);inv[0]=1;\n\tfor(i=n-1;i;i--)\tinv[i]=1ll*inv[i+1]*(i+1)%mod;\n\trep(i,1,n)\n\t\trep(j,0,18)\n\t\t\tif(1<=0;j--)\n\t\t\tif(1<=0;k=(k-1)&now){\t\t\t\n\t\t\tcur1=k+mx[i];cur2=i^cur1;\n\t\t\tup=min(K-1,cnt[cur2]);\n\t\t\ttmp=C(i,cur1);\t\n\t\t\trep(j,0,up){\n\t\t\t\tdp[i][j+1]=(dp[i][j+1]+1ll*dp[cur2][j]*tmp)%mod;\t\t\t\t\t\t\n\t\t\t}\n\t\t\tif(k==0)\tbreak;\n\t\t}\n\t}\n\trep(j,1,min(cnt[n],K))\tans=(ans+1ll*dp[n][j]*C(K,j)%mod*cj[j])%mod;\n\tans=(power(K,n)-ans+mod)%mod;\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "5d7182e9173b9e6b4df01292869de3dd", "src_uid": "97f737f59100babe5e45e1a82a1f7d99", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define il inline\n#define RG register\n#define ll long long\n#define N (250005)\n\nusing namespace std;\n\nint f[N][18],c[18],ans[18],lg,m,n,k,rhl;\nint fac[N],ifac[N],inv[N],cnt[N],v[N];\n\nil void add(int *a,int *b,RG int fg){\n for (RG int i=0;i=rhl) a[i]-=rhl;\n }\n return;\n}\n\nil void mul(int *a,int *b){\n memset(c,0,sizeof(c));\n for (RG int i=0;i>=1) mul(a,a);\n }\n memcpy(a,ans,sizeof(ans)); return;\n}\n\nil int qpow(RG int a,RG int b){\n RG int ans=1;\n while (b){\n if (b&1) ans=1LL*ans*a%rhl;\n if (b>>=1) a=1LL*a*a%rhl;\n }\n return ans;\n}\n\nint main(){\n#ifndef ONLINE_JUDGE\n freopen(\"game.in\",\"r\",stdin);\n freopen(\"game.out\",\"w\",stdout);\n#endif\n cin>>n>>k>>rhl; if (n&1) cout<>1]+(i&1),f[i][cnt[i]]=ifac[i];\n }\n v[n]=1;\n for (RG int i=n;i;--i){\n if (!v[i]) continue;\n for (RG int j=i;j;(--j)&=i) v[j]=1;\n }\n for (RG int i=1;i\n#include\nusing namespace std;\ninline void R (int &v) {\n\tstatic char ch;\n\tv = 0;\n\tbool p = 0;\n\tdo {\n\t\tch = getchar();\n\t\tif(ch == '-') p = 1;\n\t} while(!isdigit(ch));\n\twhile(isdigit(ch)) {\n\t\tv = (v + (v << 2) << 1) + (ch^'0');\n\t\tch = getchar();\n\t}\n\tif(p) v = -v;\n}\ninline void R (long long &v) {\n\tstatic char ch;\n\tv = 0;\n\tbool p = 0;\n\tdo {\n\t\tch = getchar();\n\t\tif(ch == '-') p = 1;\n\t} while(!isdigit(ch));\n\twhile(isdigit(ch)) {\n\t\tv = (v + (v << 2) << 1) + (ch^'0');\n\t\tch = getchar();\n\t}\n\tif(p) v = -v;\n}\nlong long n, k,mod;\nint jc[262155];\nint inv[262155]; \ninline int ksm(int x, long long y) {\n\tint ret = 1;\n\twhile(y) {\n\t\tif(y & 1) {\n\t\t\tret = (long long)ret * x % mod;\n\t\t}\n\t\tx = (long long) x * x % mod;\n\t\ty >>= 1;\n\t}\n\treturn ret;\n}\nint dp[262155][31];\nbool vis[262155][31];\nint Log[262155];\ninline int getcnt(int x) {\n\tint cnt = 0;\n\twhile(x && x % 2==0) {\n\t\t++cnt;\n\t\tx /= 2;\n\t}\n\treturn cnt;\n}\nint jc2[262155];\ninline int C(int n, int m) {\n\treturn (long long)jc[n] * inv[m] % mod * inv[n - m] % mod;\n} \ninline int dfs(int n, int k) {\n\tif(n == 0) return 1;\n\tif(k == 0) return 0;\n\tif(dp[n][k] != -1) {\n\t\treturn dp[n][k];\n\t}\n\tint di = (1 << (Log[n])), ret = 0;\n\tfor(register int i = n; i >= di; i -= 2) {\n\t\tret = ((long long)ret + (((long long)C(n, i) * dfs(n - i, k - 1)) % mod) * k) % mod;\n\t}\n\treturn dp[n][k] = ret;\n}\nvector >tp;\ninline int bfs(int n, int k) {\n\tstatic queue > que;\n\tque.push(make_pair(n, k));\n\twhile(!que.empty()) {\n\t\tn = que.front().first, k = que.front().second;\n\t\tque.pop();\n\t\ttp.push_back(make_pair(n, k));\n\t\tif(n == 0 || k == 0) continue;\n\t\tint di = (1 << (Log[n]));\n\t\tfor(register int i = n; i >= di; i -= 2) {\n\t\t\tif(jc2[i] + jc2[n - i] == jc2[n] && !vis[n - i][k - 1]){\n\t\t\t\tvis[n - i][k - 1] = 1;\n\t\t\t\tque.push(make_pair(n - i, k - 1));\n\t\t\t} \n\t\t}\n\t}\n\tfor(int i = tp.size() - 1; i >= 0; --i) {\n\t\tn = tp[i].first, k = tp[i].second;\n\t\tif(n == 0) {\n\t\t\tdp[n][k] = 1; continue;\n\t\t}\n\t\tif(k == 0) {\n\t\t\tdp[n][k] = 0;continue;\n\t\t}\n\t\tint di = (1 << (Log[n]));\n\t\tfor(register int i = n; i >= di; i -= 2) {\n\t\t\tif(jc2[i] + jc2[n - i] == jc2[n])\n\t\t\tdp[n][k] = ((long long)dp[n][k] + (((long long)C(n, i) * dp[n - i][k - 1]) % mod) * k) % mod; \n\t\t}\n\t}\n\treturn dp[n][k];\n}\nint main() {\n//\tcout<> 1] + 1, jc2[i] = jc2[i - 1] + getcnt(i);\n\tinv[262150] = ksm(jc[262150], mod - 2);\n\tfor(int i = 262149; i; --i) inv[i] = (long long)inv[i + 1] * (i + 1) % mod;\n//\tcout << inv[7]<<'\\n';\n\tinv[0] = inv[1];\n\tif(n & 1) {\n\t\tcout << ksm(k, n) << '\\n';\n\t} else {\n\t\tint cnt = 0;\n\t\tfor(int i = 0; i < 62; ++i) {\n\t\t\tif(n & (1ll<\n\nusing namespace std;\n\nint n, m;\nvector k;\nvector > c;\n\nbool check(int d) {\n vector lastDiscount(n + 1, -1);\n\n for (int i = 0; i < m; i++)\n if (c[i].first <= d)\n lastDiscount[c[i].second] = max(lastDiscount[c[i].second], c[i].first);\n\n vector > off(d + 1);\n for (int i = 1; i <= n; i++)\n if (lastDiscount[i] != -1)\n off[lastDiscount[i]].push_back(i);\n\n vector need = k;\n int cur_money = 0;\n for (int cur_day = 1; cur_day <= d; cur_day++) {\n cur_money++;\n\n if (cur_day > d)\n continue;\n\n for (auto it: off[cur_day])\n if (cur_money >= need[it]) {\n cur_money -= need[it];\n need[it] = 0;\n }\n else {\n need[it] -= cur_money;\n cur_money = 0;\n break;\n }\n }\n\n return accumulate(need.begin(), need.end(), 0) * 2 <= cur_money;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n cin >> n >> m;\n k.resize(n + 1);\n int l = 0, r = 0;\n for (int i = 1; i <= n; i++) {\n cin >> k[i];\n r += k[i];\n }\n\n c.resize(m);\n for (int i = 0; i < m; i++)\n cin >> c[i].first >> c[i].second;\n\n r *= 2;\n r++;\n\n while (r - l > 1) {\n int mid = (l + r) / 2;\n if (check(mid))\n r = mid;\n else\n l = mid;\n }\n cout << r;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "binary search"], "code_uid": "85f809d6369e8142bbc01b63c085628b", "src_uid": "2eb101dcfcc487fe6e44c9b4c0e4024d", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nconst int MAXN = 5005;\nconst int P = 1e9 + 7;\ntemplate void read(T &x) {\n\tx = 0; int f = 1;\n\tchar c = getchar();\n\tfor (; !isdigit(c); c = getchar()) if (c == '-') f = -f;\n\tfor (; isdigit(c); c = getchar()) x = x * 10 + c - '0';\n\tx *= f;\n}\ntemplate void write(T x) {\n\tif (x < 0) x = -x, putchar('-');\n\tif (x > 9) write(x / 10);\n\tputchar(x % 10 + '0');\n}\ntemplate void writeln(T x) {\n\twrite(x);\n\tputs(\"\");\n}\nlong long power(long long x, long long y) {\n\tif (y == 0) return 1;\n\tlong long tmp = power(x, y / 2);\n\tif (y % 2 == 0) return tmp * tmp % P;\n\telse return tmp * tmp % P * x % P;\n}\nlong long inv(long long x) {\n\tstatic long long mem[MAXN];\n\tif (mem[x] != 0) return mem[x];\n\telse return mem[x] = power(x, P - 2);\n}\nlong long sum(long long lim, int k) {\n\tif (k == 0) return 2 * lim + 1;\n\telse if (k == 2) return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * inv(6) % P;\n\telse if (k == 4) return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * (3 * power(lim, 2) + 3 * lim - 1) % P * inv(30) % P;\n\telse return 2 * lim * (lim + 1) % P * (2 * lim + 1) % P * (3 * power(lim, 4) + 6 * power(lim, 3) - 3 * lim + 1) % P * inv(42) % P;\n}\nint main() {\n\tlong long ans = 0;\n\tlong long M, m, n;\n\tread(M); n = sqrt(M); m = M % P;\n\tfor (long long x = -n; x <= n; x++) {\n\t\tlong long y = sqrt(M - x * x);\n\t\tans += (2) * sum(y, 6) % P;\n\t\tans += (6 * power(x, 2) % P - (3 * m + 6) % P) % P * sum(y, 4) % P;\n\t\tans += (6 * power(x, 4) % P - (6 * m + 12) % P * power(x, 2) + (3 * m + 4)) % P * sum(y, 2) % P;\n\t\tans += (2 * power(x, 6) % P - (3 * m + 6) % P * power(x, 4) % P + (3 * m + 4) % P * power(x, 2) % P + m * (m + 1) % P * (m + 2) % P) % P * sum(y, 0) % P;\n\t\tans %= P;\n\t}\n\tans = (ans + P) % P;\n\tans = ans * inv(6) % P;\n\twriteln(ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "combinatorics"], "code_uid": "1502600feb11840e408f5adb4e3e0c37", "src_uid": "b9a785849e5ffadb24b58b38b1f2ee48", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef vector vi;\ntypedef pair ii;\ntypedef long long ll;\n#define mset(a,b) memset(a,b,sizeof a)\n#define pb push_back\n#define mp make_pair\ntypedef vector vll;\n#define N 200\nconst ll lim = ll(1e18);\nll dp[N][N], digits[N], base, n;\nstring number;\nvll exponentes;\nll solve(int ind, int xp){\n if(ind == n){\n if(xp < 0) return 0;\n else return -1;\n }\n if(xp < 0) return -1;\n if(dp[ind][xp] != -1) return dp[ind][xp];\n if(number[ind] == '0') return dp[ind][xp] = solve(ind+1, xp-1);\n ll cur = 0; ll best = lim;\n for(int e = ind; e < n; e++){\n cur = (cur*10+(number[e] - '0'));\n if(cur >= base) break;\n if(cur*exponentes[xp] > lim || cur*exponentes[xp] < cur) break;\n ll next = solve(e+1, xp-1);\n if(next == -1) continue;\n best = min(best, exponentes[xp]*cur + next);\n }\n return dp[ind][xp] = best;\n}\nint main(){\n cin >> base;\n cin >> number;\n n = number.length();\n mset(dp, -1);\n mset(digits, -1);\n ll ans = lim;\n exponentes.pb(1LL);\n while(exponentes.back()*base > exponentes.back())\n exponentes.pb(exponentes.back()*base);\n for(int e = 0; e < exponentes.size(); e++){\n if(solve(0, e) == -1) continue;\n ans = min(ans, solve(0, e));\n }\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "math", "dp", "greedy", "strings"], "code_uid": "522c894185838f415ce369d74cffc930", "src_uid": "be66399c558c96566a6bb0a63d2503e5", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint a[20], s[20], c[20];\nlong long l, r;\nvector b;\n\nint solve() {\n int cnt = 0;\n for (int i = 1; i < 10; i++) {\n cnt += a[i];\n }\n if (cnt > b.size()) {\n return 0;\n }\n a[0] = b.size() - cnt;\n int pos = b.size() - 1;\n while (pos >= 0) {\n if (a[b[pos]] > 0) {\n c[pos] = b[pos];\n a[b[pos]]--;\n pos--;\n continue;\n }\n int mv = 10;\n for (int i = 0; i < 10; i++) {\n if (a[i] > 0) {\n mv = i;\n break;\n }\n }\n while (pos < b.size() && mv >= b[pos]) {\n pos++;\n if (pos == b.size()) {\n return 0;\n }\n mv = min(mv, c[pos]);\n a[c[pos]]++;\n }\n int x = b[pos] - 1;\n while (x >= 0 && a[x] == 0) {\n x--;\n }\n c[pos] = x;\n a[x]--;\n pos--;\n x = 9;\n while (pos >= 0) {\n while (x >= 0 && a[x] == 0) {\n x--;\n }\n c[pos] = x;\n a[x]--;\n pos--;\n }\n break;\n }\n long long val = 0;\n for (int i = b.size() - 1; i >= 0; i--) {\n val = val * 10 + c[i];\n }\n return val >= l ? 1 : 0;\n}\n\nvoid dfs(int x, int left, int &ans) {\n if (x == 10) {\n if (left == 18) {\n return;\n }\n for (int i = 0; i < 10; i++) {\n s[i] = a[i];\n }\n ans += solve();\n for (int i = 0; i < 10; i++) {\n a[i] = s[i];\n }\n return;\n }\n for (int i = 0; i <= left; i++) {\n a[x] = i;\n dfs(x + 1, left - i, ans);\n }\n}\n\nint main() {\n\n scanf(\"%lld %lld\", &l, &r);\n\n while (r > 0) {\n b.push_back(r % 10);\n r /= 10;\n }\n\n int ans = 0;\n\n dfs(1, 18, ans);\n\n printf(\"%d\\n\", ans);\n\n return 0;\n\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "combinatorics", "brute force"], "code_uid": "725a3610641f732b7bbab6d00bce8256", "src_uid": "adfa5f280eefbacdbaa4ad605402b57a", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\nstruct SSP{\n\tint n, source, sink;\n vector > adj;\n\tstruct edge {\n int u, flow, cap, cost, origcost;\n };\n\tvector edges;\n\tSSP(int n, int source, int sink): n(n),source(source),sink(sink),adj(n) {}\n int add_edge(int a, int b, int cap, int cost){\n int result = edges.size();\n adj[a].push_back(edges.size());\n edges.push_back({b, 0, cap, cost, cost});\n adj[b].push_back(edges.size());\n edges.push_back({a, 0, 0, -cost, -cost});\n return result;\n }\n pair djk(){\n struct state_t{\n int dist, prev, maxf;\n };\n priority_queue,vector >, greater > > pq;\n vector states(n, {INT_MAX/2, -1, 0});\n states[source] = {0, -2, INT_MAX};\n pq.push(make_pair(states[source].dist, source));\n while(!pq.empty()){\n int d, v; tie(d, v) = pq.top(); pq.pop();\n if(d != states[v].dist) continue;\n for(int edid : adj[v]){\n edge & ed = edges[edid];\n if(ed.flow < ed.cap && states[ed.u].dist > states[v].dist + ed.cost){\n states[ed.u].dist = states[v].dist + ed.cost;\n states[ed.u].prev = edid;\n states[ed.u].maxf = min(states[v].maxf, ed.cap - ed.flow);\n pq.push(make_pair(states[ed.u].dist, ed.u));\n }\n }\n }\n int lastid = states[sink].prev, extraflow = states[sink].maxf, atcost = 0;\n if(lastid == -1) return {0, 0};\n for(int id = lastid; id != -2; id = states[edges[id^1].u].prev){\n edges[id].flow += extraflow;\n edges[id^1].flow -= extraflow;\n atcost += edges[id].origcost;\n }\n for(int id = 0; id < (int) edges.size(); id++){\n int v = edges[id^1].u, u = edges[id].u;\n if(states[v].prev != -1 && states[u].prev != -1)\n edges[id].cost += states[v].dist - states[u].dist;\n }\n return {extraflow, atcost};\n }\n pair mfmc(){\n int flow = 0, cost = 0;\n if(any_of(edges.begin(), edges.end(), [](const edge & ed){\n return ed.flow > 0 && ed.cost < 0;\n })) bellamanford();\n int extraflow, atcost;\n while(tie(extraflow, atcost) = djk(), extraflow > 0)\n flow += extraflow, cost += extraflow * atcost;\n return {flow, cost};\n }\n void bellamanford(){\n vector dist(n, INT_MAX/2);\n dist[source] = 0;\n for(int rep = 0; rep < n; rep++){\n bool changed = false;\n for(int id = 0; id < (int)edges.size(); id++){\n int v = edges[id^1].u, u = edges[id].u;\n if(edges[id].flow < edges[id].cap && dist[v] != INT_MAX/2 &&\n dist[u] > dist[v] + edges[id].cost){\n dist[u] = dist[v] + edges[id].cost;\n changed = true;\n assert(rep < n - 1);\n }\n }\n if(!changed) break;\n }\n for(int id = 0; id < (int) edges.size(); id++){\n int v = edges[id^1].u, u = edges[id].u;\n if(dist[v] < INT_MAX/2 && dist[u] < INT_MAX/2)\n edges[id].cost += dist[v] - dist[u];\n }\n }\n};\nconst int maxn = 2521;\nint to[maxn][maxn], gp[maxn];\nvector e[maxn];\nint main(){\n\tint n, m, k, c, d; cin >> n >> m >> k >> c >> d;\n\tconst int max_time = 120;\n\tvector a(k); for(int i = 0; i < k; i++) cin >> a[i];\n\tfor(int i = 0; i < m; i++){\n\t\tint x, y; cin >> x >> y;\n\t\te[x].push_back(y);\n\t\te[y].push_back(x);\n\t}\n\tint nd = 0;\n\tfor(int i = 0; i < max_time; i++){\n\t\tfor(int j = 1; j <= n; j++) to[i][j] = nd++;\n\t}\n\tint S = nd++, T = nd++;\n\tSSP flow(nd, S, T);\n\tfor(int i = 0; i < k; i++) flow.add_edge(S, to[0][a[i]], 1, 0);\n\tfor(int i = 0; i < max_time; i++) flow.add_edge(to[i][1], T, k, c * i);\n\tfor(int i = 0; i < max_time - 1; i++){\n\t\tfor(int j = 1; j <= n; j++){\n\t\t\tfor(int k : e[j]){\n\t\t\t\tfor(int z = 0; z <= 52; z++){\n\t\t\t\t\tflow.add_edge(to[i][j], to[i + 1][k], 1, d * (2 * z + 1));\n\t\t\t\t}\t\n\t\t\t}\n\t\t\tflow.add_edge(to[i][j], to[i + 1][j], k, 0);\n\t\t}\n\t}\n\tint p0, p1; tie(p0, p1) = flow.mfmc();\n\tcout << p1 << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "a06cb7e9405b42032b8074f8f8aa9f1f", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int N = 10000,M = N * 100;\nconst long long inf = LLONG_MAX >> 5;\nclass FlowGraph\n{\nprivate:\n int head[N],v[M],nxt[M],c[M],ptr,t;\n long long w[M],cost;\n inline void padd_edge(int uu,int vv,long long ww,int cc)\n {\n v[ptr] = vv;w[ptr] = ww;c[ptr] = cc;\n nxt[ptr] = head[uu];head[uu] = ptr++;\n }\n int dis[N],id[N],cur[N];bool vis[N];\n bool spfa(int s,int t)\n {\n memset(vis,0,sizeof(vis));\n memset(dis,-1,sizeof(dis));\n queue q;q.push(s);\n dis[s] = 0;id[s] = 1;\n while (!q.empty())\n {\n int nd = q.front();\n\t\t\tq.pop();vis[nd] = 0;\n for (int i = head[nd];i != -1;i = nxt[i])\n if (w[i] && (dis[v[i]] == -1 || dis[v[i]] > dis[nd] + c[i]))\n {\n dis[v[i]] = dis[nd] + c[i];\n if (!vis[v[i]])\n {\n vis[v[i]] = 1;q.push(v[i]);\n }\n }\n }\n return dis[t] != -1;\n }\n long long dfs(int pos,long long flow)\n {\n if (pos == t) return flow;\n vis[pos] = 1;\n long long tmp = flow;\n for (int i = cur[pos] != -1 ? cur[pos] : head[pos];i != -1;i = nxt[i])\n if (!vis[v[i]] && w[i] && dis[v[i]] == dis[pos] + c[i])\n {\n cur[pos] = i;\n long long fl = dfs(v[i],min(flow,w[i]));\n cost += fl * c[i];\n w[i] -= fl;w[i ^ 1] += fl;\n flow -= fl;\n if (!flow) break;\n }\n return tmp - flow;\n }\npublic:\n FlowGraph()\n {memset(head,-1,sizeof(head));memset(nxt,-1,sizeof(nxt));}\n inline void add_edge(int uu,int vv,long long ww,int cc)\n {\n padd_edge(uu,vv,ww,cc);padd_edge(vv,uu,0,-cc);\n }\n long long dinic(int s,int t)\n {\n cost = 0;this->t = t;\n while (spfa(s,t)) \n {\n memset(cur,-1,sizeof(cur));dfs(s,inf);\n }\n return cost;\n }\n};\nFlowGraph g;\nint n,m,kk,c,d,a[55];\nint main ()\n{\n ios::sync_with_stdio(false);\n cin >> n >> m >> kk >> c >> d;\n for (int i = 1;i <= kk;i++) cin >> a[i];\n int s = 0,t = n * 100 + 1;\n for (int i = 1;i <= 100;i++) g.add_edge(i,t,inf,0);\n for (int i = 1;i <= kk;i++) g.add_edge(s,(a[i] - 1) * 100 + 1,1,0);\n for (int i = 2;i <= n;i++)\n for (int j = 1;j < 100;j++)\n g.add_edge((i - 1) * 100 + j,(i - 1) * 100 + j + 1,inf,c);\n int t1,t2;\n for (int _ = 1;_ <= m;_++)\n {\n cin >> t1 >> t2;\n for (int i = 1;i < 100;i++)\n for (int j = 1;j <= kk;j++) //其实不用枚举到kk,枚举到25左右即可\n {\n int val = c + d * (j * j - (j - 1) * (j - 1));\n g.add_edge((t1 - 1) * 100 + i,(t2 - 1) * 100 + i + 1,1,val);\n g.add_edge((t2 - 1) * 100 + i,(t1 - 1) * 100 + i + 1,1,val);\n }\n }\n cout << g.dinic(s,t) << endl;\n return 0;\n}\n// 最小费用流 珂以利用流量来处理多人走同一条路的情况", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "8244d40b8be7328a7b778c4627c266d6", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define Fopen freopen(\"_in.txt\",\"r\",stdin); freopen(\"_out.txt\",\"w\",stdout);\n#define LL long long\n#define ULL unsigned LL\n#define fi first\n#define se second\n#define pb push_back\n#define lson l,m,rt<<1\n#define rson m+1,r,rt<<1|1\n#define lch(x) tr[x].son[0]\n#define rch(x) tr[x].son[1]\n#define max3(a,b,c) max(a,max(b,c))\n#define min3(a,b,c) min(a,min(b,c))\ntypedef pair pll;\nconst int inf = 0x3f3f3f3f;\nconst int _inf = 0xc0c0c0c0;\nconst LL INF = 0x3f3f3f3f3f3f3f3f;\nconst LL _INF = 0xc0c0c0c0c0c0c0c0;\nconst LL mod = (int)1e9+7;\nconst int Z = 100;\nconst int N = Z * Z;\nconst int M = Z * Z * Z * 3;\nint px[N], py[N];\nint head[N], to[M], ct[M], w[M], nt[M];\nint d[N], vis[N];\nint pre[N], id[N];\nint s, t, tot;\n \nvoid add(int u, int v, int flow, int cost){\n to[tot] = v; ct[tot] = cost;\n w[tot] = flow; nt[tot] = head[u]; head[u] = tot++;\n \n to[tot] = u; ct[tot] = -cost;\n w[tot] = 0; nt[tot] = head[v]; head[v] = tot++;\n}\nvoid init(){\n memset(head, -1, sizeof(head));\n tot = 0;\n}\nint spfa(int s, int t){\n queue q;\n memset(d, inf, sizeof(d));\n memset(vis, 0, sizeof(vis));\n memset(pre, -1, sizeof(pre));\n d[s] = 0;\n q.push(s);\n while(!q.empty()){\n int u = q.front(); q.pop();\n vis[u] = 0;\n// cout << u << \" \" << d[u] << endl;\n for(int i = head[u]; ~i; i = nt[i]){\n if(w[i] > 0 && d[to[i]] > d[u] + ct[i]){\n d[to[i]] = d[u] + ct[i];\n pre[to[i]] = u;\n id[to[i]] = i;\n// if(to[i] == 201) cout << \"Updata \" << u << \" \" << to[i] <<\" \" << d[to[i]] <\n#include\n#include\n#define Maxn 5500\n#define Maxm 35500\n#define INF 1000000000\nusing namespace std;\nstruct Line\n{int nxt,t,c,l;}g[Maxm];\nint fir[Maxn],tl=1;\nvoid adl(int f,int t,int c,int cost){\n g[++tl]=(Line){fir[f],t,c,cost};fir[f]=tl;\n g[++tl]=(Line){fir[t],f,0,-cost};fir[t]=tl;\n}\nint S,T,pre[Maxn],lp[Maxn],flow[Maxn],dis[Maxn];\nbool in[Maxn];\nqueue q;\nint spfa()\n{\n for (int i=1;i<=T;i++)\n {dis[i]=INF;flow[i]=0;}\n flow[S]=INF;dis[S]=0;\n q.push(S);in[S]=1;\n while(!q.empty()){\n int u=q.front();\n in[u]=0;q.pop();\n for (int i=fir[u],v;i;i=g[i].nxt)\n if (g[i].c&&dis[v=g[i].t]>dis[u]+g[i].l){\n dis[v]=dis[u]+g[i].l;\n flow[v]=min(flow[u],g[i].c);\n lp[v]=i;pre[v]=u;\n if (!in[v]){in[v]=1;q.push(v);}\n }\n }return flow[T];\n}\nint n,m,k,C,D,pl[105][105],st[105][105];\nstruct Data\n{int f,t;}s[105];\n#define P(x,y) ((x)*n+(y))\nint main()\n{\n scanf(\"%d%d%d%d%d\",&n,&m,&k,&C,&D);\n S=P(n+k,n)+1;T=S+2;\n adl(P(0,1),T,INF,0);\n for (int i=1,p;i<=k;i++){\n scanf(\"%d\",&p);\n adl(S,P(0,p),1,0);\n }for (int i=1;i<=m;i++){\n scanf(\"%d%d\",&s[i*2].f,&s[i*2].t);\n s[i*2-1]=(Data){s[i*2].t,s[i*2].f};\n }m*=2;\n for (int t=1;t<=n+k;t++){\n for (int i=1;i<=n;i++)\n adl(P(t-1,i),P(t,i),INF,0);\n for (int i=1;i<=m;i++){\n adl(P(t-1,s[i].f),P(t,s[i].t),1,D);\n pl[t][i]=tl;st[t][i]=1;\n }adl(P(t,1),T,INF,t*C);\n }\n int sav,cans=0;\n while(sav=spfa()){\n int u=T;\n while(u!=S){\n int las=pre[u];\n g[lp[u]].c-=sav;\n g[lp[u]^1].c+=sav;\n u=las;\n }cans+=dis[T]*sav;\n for (int t=1;t<=n+n;t++)\n for (int i=1;i<=m;i++)\n if (g[pl[t][i]].c){\n st[t][i]++;\n adl(P(t-1,s[i].f),P(t,s[i].t),1,D*(st[t][i]*2-1));\n pl[t][i]=tl;\n }\n }printf(\"%d\\n\",cans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "8183c4be4b8c740c00cb24d50cf68389", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n//Start\ntypedef long long ll;\ntypedef double db;\n#define mp(a,b) make_pair((a),(b))\n#define x first\n#define y second\n#define bg begin()\n#define ed end()\n#define sz(a) int((a).size())\n#define pb(a) push_back(a)\n#define R(i,a,b) for(int i=(a),i##E=(b);ii##E;i--)\nconst int iinf=0x3f3f3f3f;\nconst ll linf=0x3f3f3f3f3f3f3f3f;\n\n//Data\nconst int N=50;\nint n,m,k,c,d,mxt;\n\n//Flows\nconst int fN=(N*3+1)*N+2;\nint s,t,fn;\nvector e[fN],to,fw,co;\nvoid adde(int u,int v,int w,int c){\n e[u].pb(sz(to)),to.pb(v),fw.pb(w),co.pb(+c);\n e[v].pb(sz(to)),to.pb(u),fw.pb(0),co.pb(-c);\n}\nint dep[fN],pre[fN]; bool vis[fN];\nbool spfa(){\n fill(dep,dep+fn,iinf),fill(pre,pre+fn,-1),fill(vis,vis+fn,false);\n queue q; q.push(s),vis[s]=true,dep[s]=0;\n while(sz(q)){\n int u=q.front(); q.pop(),vis[u]=false;\n for(int v:e[u])if(fw[v]&&dep[to[v]]>dep[u]+co[v])\n dep[to[v]]=dep[u]+co[v],pre[to[v]]=v,\n (!vis[to[v]])&&(q.push(to[v]),vis[to[v]]=true);\n }\n return dep[t] mcmf(){\n pair res(0,0);\n while(spfa()){\n int f=iinf;\n for(int u=t;u!=s;u=to[pre[u]^1]) f=min(f,fw[pre[u]]);\n for(int u=t;u!=s;u=to[pre[u]^1]) fw[pre[u]]-=f,fw[pre[u]^1]+=f;\n res.x+=f,res.y+=dep[t]*f;\n }\n return res;\n}\n\n//Main\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0),cout.tie(0);\n cin>>n>>m>>k>>c>>d,mxt=N*3+1;\n fn=(t=(s=n*mxt)+1)+1;\n R(i,0,k){int u;cin>>u,--u,adde(s,u,1,0);}\n while(m--){\n int u,v; cin>>u>>v,--u,--v;\n R(i,0,mxt-1)R(p,1,k+1)\n adde(i*n+u,(i+1)*n+v,1,2*d*p-d),\n adde(i*n+v,(i+1)*n+u,1,2*d*p-d);\n }\n R(u,0,n)R(i,0,mxt-1) adde(i*n+u,(i+1)*n+u,k,0);\n R(i,0,mxt) adde(i*n,t,k,i*c);\n pair ans=mcmf();\n assert(ans.x==k);\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \n \nusing namespace std;\ntemplateinline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }\ntemplateinline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } \n#define sz(c) (int)(c).size()\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define left left228\n#define right right228\n#define next next228\n#define rank rank228\n#define prev prev228\n#define y1 y1228 \n#define read(FILENAME) freopen((FILENAME + \".in\").c_str(), \"r\", stdin)\n#define write(FILENAME) freopen((FILENAME + \".out\").c_str(), \"w\", stdout)\n#define files(FILENAME) read(FILENAME), write(FILENAME)\n#define pb push_back\nconst string FILENAME = \"input\";\nconst int MAXN = 8200;\nconst long long INF = 1e14;\n\n\nstruct edge {\n\tint u, v;\n\tlong long cap, flow;\n\tlong long cost;\n\tlong long rem() {\n\t\treturn cap - flow;\n\t}\n\tedge(){}\n\tedge(int _u, int _v, long long _cap, long long _flow, long long _cost) {\n\t\tu = _u;\n\t\tv = _v;\n\t\tcap = _cap;\n\t\tflow = _flow;\n\t\tcost = _cost;\n\t}\n};\n\n\nint nodes; \nvector g[MAXN];\nvector e;\n\n\nvoid add_edge(int u, int v, long long cap, long long cost) {\n\tg[u].pb(sz(e));\n\te.pb(edge(u, v, cap, 0, cost));\n\tg[v].pb(sz(e));\n\te.pb(edge(v, u, 0, 0, -cost));\n}\n\n\nlong long dist[MAXN], mncost;\nint pre[MAXN];\nlong long cap[MAXN], mxflow;\nbool inq[MAXN];\n\n\nvoid flow(int s, int t) {\n\tmemset(inq, 0, sizeof(inq));\n\tmxflow = mncost = 0;\n\twhile (true) {\n\t\tfill(dist, dist + nodes, INF);\n\t\tdist[s] = 0;\n\t\tmemset(pre, -1, sizeof(pre));\n\t\tpre[s] = 0;\n\t\tmemset(cap, 0, sizeof(cap));\n\t\tcap[s] = INF;\n\t\tqueue q;\n\t\tq.push(s);\n\t\tinq[s]=1;\n\t\twhile (!q.empty()) {\n\t\t\tint u = q.front();\n\t\t\tq.pop();\n\t\t\tinq[u] = 0;\n\t\t\tfor (int it = 0; it < sz(g[u]); it++) {\n\t\t\t\tint i = g[u][it];\n\t\t\t\tedge &f = e[i];\n\t\t\t\tif (f.rem() && dist[f.v] > dist[u] + f.cost + 1e-9) {\n\t\t\t\t\tdist[f.v] = dist[u] + f.cost;\n\t\t\t\t\tpre[f.v] = i;\n\t\t\t\t\tcap[f.v] = min(cap[u], f.rem());\n\t\t\t\t\tif (!inq[f.v]) {\n\t\t\t\t\t\tq.push(f.v);\n\t\t\t\t\t\tinq[f.v] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (pre[t] < 0) {\n\t\t\tbreak;\n\t\t}\n\t\tmxflow +=cap[t];\n\t\tmncost += cap[t] * dist[t];\n\t\tint v = t;\n\t\twhile (v != s) {\n\t\t\te[pre[v]].flow += cap[t];\n\t\t\te[pre[v] ^ 1].flow -= cap[t];\n\t\t\tv = e[pre[v]].u;\n\t\t}\n\t}\n}\n \n\nint n, m, k, c, d;\n \n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout.tie(0);\n\t//read(FILENAME);\n\tcin >> n >> m >> k >> c >> d;\n\tfor (int i = 0; i < k; i++) {\n\t\tint x;\n\t\tcin >> x;\n\t\tx--;\n\t\tadd_edge(0, x + 1, 1, 0);\n\t}\n\tfor (int i = 0; i < m; i++) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\tx--, y--;\n\t\tfor (int i = 0; i < 105; i++) {\n\t\t\tfor (int j = 0; j < 51; j++) {\n\t\t\t\tadd_edge(i * n + x + 1, (i + 1) * n + y + 1, 1, c + d * (2 * j + 1));\n\t\t\t\tadd_edge(i * n + y + 1, (i + 1) * n + x + 1, 1, c + d * (2 * j + 1));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < 105; i++) {\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tadd_edge(i * n + j + 1, (i + 1) * n + j + 1, 51, c);\n\t\t}\n\t}\n\tfor (int i = 0; i < 105; i++) {\n\t\tadd_edge(i * n + 1, 105 * n + 1, 51, 0);\n\t}\n\tnodes = 6 + 107 * n;\n\tflow(0, 105 * n + 1);\n\tcout << mncost << '\\n';\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "2bad9d480ad44ace04e27653a973ec34", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"Ofast\")\n#include \nusing namespace std;\nusing i64=int_least64_t;\n\nnamespace std\n{\n // hash\n template size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); }\n template struct hash> { size_t operator()(pair const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } };\n template ::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc::apply(seed, t), get(t)); } };\n template struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } };\n template struct hash> { size_t operator()(tuple const &t) const { return tuple_hash_calc>::apply(0, t); } };\n // iostream\n template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second; }\n template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; }\n template struct tupleis { static istream &apply(istream &is, tuple_t &t) { tupleis::apply(is, t); return is >> get(t); } };\n template struct tupleis { static istream &apply(istream &is, tuple_t &t) { return is; } };\n template istream &operator>>(istream &is, tuple &t) { return tupleis, tuple_size>::value - 1>::apply(is, t); }\n template <> istream &operator>>(istream &is, tuple<> &t) { return is; }\n template struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { tupleos::apply(os, t); return os << ' ' << get(t); } };\n template struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } };\n template ostream &operator<<(ostream &os, const tuple &t) { return tupleos, tuple_size>::value - 1>::apply(os, t); }\n template <> ostream &operator<<(ostream &os, const tuple<> &t) { return os; }\n template , string>::value, nullptr_t> = nullptr>\n istream& operator>>(istream& is, Container &cont) { for(auto&& e : cont) is >> e; return is; }\n template , string>::value, nullptr_t> = nullptr>\n ostream& operator<<(ostream& os, const Container &cont) { bool flag = 1; for(auto&& e : cont) flag ? flag = 0 : (os << ' ', 0), os << e; return os; }\n} // namespace std\n\n#include \n#include \n#include \n\n// Base class of flow algorithms\ntemplate \nstruct flow_base\n{\n struct edge_t\n {\n size_t src, dst;\n cap_t cap; cost_t cost;\n edge_t *rev;\n edge_t(size_t _src, size_t _dst, cap_t _cap, cost_t _cost, edge_t *_rev)\n : src(_src), dst(_dst), cap(_cap), cost(_cost), rev(_rev) {}\n }; // struct edge_t\n\nprotected:\n using adj_type = std::vector>;\n adj_type adj;\n\npublic:\n flow_base(size_t _n = 0) : adj(_n) {}\n\n flow_base(const flow_base& other) : adj(other.size())\n {\n for(size_t node{0}; node != other.size(); ++node)\n {\n for(const auto &e : other[node])\n {\n if(e.src == node)\n {\n adj[e.src].emplace_back(e);\n adj[e.dst].emplace_back(*e.rev);\n adj[e.src].back().rev = &adj[e.dst].back();\n adj[e.dst].back().rev = &adj[e.src].back();\n e.rev->src = -1;\n }\n else\n {\n e.rev->rev->src = node;\n }\n }\n }\n }\n\n flow_base &operator=(const flow_base &other)\n {\n if(this != &other)\n {\n flow_base cop(other);\n std::swap(adj, cop.adj);\n }\n return *this;\n }\n\n size_t size() const { return adj.size(); }\n\n typename decltype(adj)::reference operator[](size_t node) { assert(node < size()); return adj[node]; }\n typename decltype(adj)::const_reference operator[](size_t node) const { assert(node < size()); return adj[node]; }\n\n void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)\n {\n assert(src < size()); assert(dst < size());\n if(cap <= 0) return;\n adj[src].emplace_back(src, dst, cap, cost, nullptr);\n adj[dst].emplace_back(dst, src, 0, -cost, nullptr);\n adj[src].back().rev = &adj[dst].back();\n adj[dst].back().rev = &adj[src].back();\n }\n}; // struct flow_base\n\ntemplate \nclass min_cost_flow : public flow_base\n{\n using base = flow_base;\n using edge_t = typename base::edge_t;\n\n template \n constexpr bool valid(const type &val) const { return val != static_cast(-1); }\n\n template \n constexpr void make_invalid(type &val) const { val = -1; }\n\n cost_t min_cost, total_cost;\n std::vector supp;\n std::vector ptnl;\n\n void copy_member(const min_cost_flow &other)\n {\n min_cost = other.min_cost;\n total_cost = other.total_cost;\n supp = other.supp;\n ptnl = other.ptnl;\n }\n\n void Dijkstra(std::vector &last)\n {\n const cost_t infty(total_cost + 1);\n std::vector dist(base::size(), infty);\n //*/ // O((V + E)logV)\n std::priority_queue> que;\n for(size_t node{}; node != base::size(); ++node)\n {\n if(supp[node] > static_cast(0))\n {\n dist[node] = 0;\n que.emplace(0, node);\n }\n }\n while(!que.empty())\n {\n // auto [nsp, node] = que.top();\n cost_t nsp; size_t node;\n std::tie(nsp, node) = que.top();\n que.pop();\n if(-nsp != dist[node]) continue;\n ptnl[node] -= nsp;\n if(ptnl[node] > infty) ptnl[node] = infty;\n make_invalid(dist[node]);\n for(edge_t &e : base::adj[node])\n {\n if(!valid(dist[e.dst]) || e.cap == static_cast(0)) continue;\n if(dist[e.dst] + ptnl[e.dst] > ptnl[node] + e.cost)\n {\n dist[e.dst] = ptnl[node] + e.cost - ptnl[e.dst];\n last[e.dst] = &e;\n que.emplace(-dist[e.dst], e.dst);\n }\n }\n }\n /*/ // O(V^2)\n //*/\n }\n\npublic:\n min_cost_flow(size_t _n = 0) : base::flow_base(_n), min_cost(0), total_cost(0), supp(_n), ptnl(_n) {}\n\n min_cost_flow(const min_cost_flow &other) : base::flow_base(other)\n {\n copy_member(other);\n }\n\n min_cost_flow &operator=(const min_cost_flow &other)\n {\n base::operator=(other);\n copy_member(other);\n return *this;\n }\n\n void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)\n {\n assert(src != dst);\n if(cap <= static_cast(0)) return;\n if(cost < static_cast(0))\n {\n supp[src] -= cap;\n supp[dst] += cap;\n min_cost += cap * cost;\n total_cost -= cap * cost;\n base::add_edge(dst, src, cap, -cost);\n }\n else\n {\n total_cost += cap * cost;\n base::add_edge(src, dst, cap, cost);\n }\n }\n\n void supply(size_t node, cap_t vol) { supp[node] += vol; }\n\n bool flow()\n {\n for(bool aug = true; aug; )\n {\n aug = false;\n std::vector last(base::size());\n Dijkstra(last);\n std::vector shut(base::size());\n for(size_t dst{}; dst != base::size(); ++dst)\n {\n if(supp[dst] < static_cast(0) and last[dst])\n {\n cap_t resid{-supp[dst]};\n size_t src{dst}, block;\n make_invalid(block);\n while(last[src] && !shut[src])\n {\n if(resid >= last[src]->cap) resid = last[block = src]->cap;\n src = last[src]->src;\n }\n if(shut[src]) block = src;\n else\n {\n if(resid >= supp[src])\n {\n resid = supp[src];\n block = src;\n }\n for(size_t node{dst}; node != src; node = last[node]->src)\n {\n last[node]->cap -= resid;\n last[node]->rev->cap += resid;\n }\n supp[src] -= resid;\n supp[dst] += resid;\n min_cost += ptnl[dst] * resid;\n aug = true;\n }\n if(valid(block))\n {\n for(size_t node{dst}; node != block; node = last[node]->src) shut[node] = true;\n shut[block]=true;\n }\n }\n }\n }\n for(cap_t s : supp) if(s > static_cast(0)) return false;\n return true;\n }\n\n cost_t optimal() { assert(flow()); return min_cost; }\n};\n\nmain()\n{\n min_cost_flow mcf(6100);\n int n,m,k,c,d; cin>>n>>m>>k>>c>>d;\n const int tlim=105;\n for(int i=0; i>j; j--;\n mcf.supply(j,1);\n }\n for(int i=0; i>u>>v; --u,--v;\n for(int t=0; t\n\nusing namespace std;\n\nstruct edge\n{\n\tint y, c, f, cost;\n\tedge() {};\n\tedge(int y, int c, int f, int cost) : y(y), c(c), f(f), cost(cost) {};\n};\n\nvector e;\n\nconst int N = 14043;\nvector g[N];\n\nlong long ans = 0;\nlong long d[N];\nint p[N];\nint pe[N];\nint inq[N];\n\nconst long long INF64 = (long long)(1e18);\n\nint s = N - 2;\nint t = N - 1;\n\nint rem(int x)\n{\n\treturn e[x].c - e[x].f;\n}\n\nvoid push_flow()\n{\n\tfor(int i = 0; i < N; i++) d[i] = INF64, p[i] = -1, pe[i] = -1, inq[i] = 0;\n\td[s] = 0;\n\tqueue q;\n\tq.push(s);\n\tinq[s] = 1;\n\twhile(!q.empty())\n\t{\n\t\tint k = q.front();\n\t\tq.pop();\n\t\tinq[k] = 0;\n\t\tfor(auto x : g[k])\n\t\t{\n\t\t\tif(!rem(x)) continue;\n\t\t\tint c = e[x].cost;\n\t\t\tint y = e[x].y;\n\t\t\tif(d[y] > d[k] + c)\n\t\t\t{\n\t\t\t\td[y] = d[k] + c;\n\t\t\t\tp[y] = k;\n\t\t\t\tpe[y] = x;\n\t\t\t\tif(inq[y] == 0)\n\t\t\t\t{\n\t\t\t\t\tinq[y] = 1;\n\t\t\t\t\tq.push(y);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n//\tvector zz(1, cur);\n\twhile(cur != s)\n\t{\n\t\te[pe[cur]].f++;\n\t\te[pe[cur] ^ 1].f--;\n\t\tcur = p[cur];\n//\t\tzz.push_back(cur);\n\t}\n//\treverse(zz.begin(), zz.end());\n//\tfor(auto x : zz) cerr << x << \" \";\n//\tcerr << endl;\n\tans += d[t];\n}\n\nvoid add_edge(int x, int y, int c, int cost)\n{\n\tg[x].push_back(e.size());\n\te.push_back(edge(y, c, 0, cost));\n\tg[y].push_back(e.size());\n\te.push_back(edge(x, 0, 0, -cost));\n}\n\nint main()\n{\n\tint n, m, k, c, d;\n\tcin >> n >> m >> k >> c >> d;\n\tfor(int i = 0; i < k; i++)\n\t{\n\t\tint x;\n\t\tcin >> x;\n\t\t--x;\n\t\tadd_edge(s, x, 1, 0);\n\t}\n\tint tt = 101;\n\tfor(int i = 0; i < tt; i++)\n\t\tadd_edge(0 + i * n, t, k, i * c);\n\tfor(int i = 0; i < m; i++)\n\t{\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\t--x;\n\t\t--y;\n\t\tfor(int i = 0; i < tt - 1; i++)\n\t\t\tfor(int j = 0; j < k; j++)\n\t\t\t\tadd_edge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));\n\t\tfor(int i = 0; i < tt - 1; i++)\n\t\t\tfor(int j = 0; j < k; j++)\n\t\t\t\tadd_edge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));\n \t}\n \tfor(int i = 0; i < n; i++)\n \t\tfor(int j = 0; j < tt - 1; j++)\n \t\t\tadd_edge(i + j * n, i + j * n + n, k, 0);\n \tfor(int i = 0; i < k; i++)\n \t\tpush_flow();\n \tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "6d91f518e01e8c81c54a8ead46f39d39", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\n#define pb push_back\n#define ull unsigned long long\n#define mp make_pair\nconst int maxn=5e3+5;\nconst ll inf=1e9+7;\n\ninline int read() {\n int X = 0,w = 0; char ch = 0;\n while(!isdigit(ch)) {w |= ch == '-';ch = getchar();}\n while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48),ch=getchar();\n return w ? -X : X;\n}\nstruct edge\n{\n int from,to,cap,flow,cost;\n edge(int a,int b,int c,int d,int e)\n {\n from = a,to = b,cap = c,flow = d,cost = e;\n }\n};\n\n\nll n,m;\nint a[maxn];\nint startPoint,endPoint;\n\nstruct MCMF\n{\n int n, m;\n vectore;\n vectorg[maxn];\n int inq[maxn];\n int d[maxn];\n int p[maxn];\n int a[maxn];\n void Init(int n)\n {\n this -> n = n;\n for(int i = 0;i <= n; i++)\n g[i].clear();\n e.clear();\n }\n void add(int from,int to,int cap,int cost)\n {\n e.push_back(edge(from,to,cap,0,cost));\n e.push_back(edge(to,from,0,0,-cost));\n m = e.size();\n g[from].push_back(m-2);\n g[to].push_back(m-1);\n }\n bool bellman(int s,int t,int& flow,int& cost)\n {\n for(int i = 0;i <= t; i++)\n d[i] = inf,inq[i] = 0;\n d[s] = 0;inq[s] = 1;p[s] = 0;a[s] = inf;\n queueQ;\n Q.push(s);\n while(!Q.empty())\n {\n int u = Q.front();\n\n Q.pop();\n inq[u] = 0;\n\n for(int i = 0;i < g[u].size(); i++)\n {\n edge& ee = e[g[u][i]];\n\n //cout< ee.flow && d[ee.to] > d[u] + ee.cost)\n {\n //cout<<\"zzzz\"<>n>>m>>k>>c>>d;\n\n for(int i=1;i<=k;i++){\n scanf(\"%d\",&a[i]);\n }\n\n startPoint=0,endPoint=n*100+1;\n\n for(int i=1;i<=k;i++){\n solve.add(startPoint,1+(a[i]-1)*100,1,0);\n }\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<100;j++){\n int p=(i-1)*100LL;\n solve.add(p+j,p+j+1,inf,c);\n }\n }\n\n for(int i=1;i<=100;i++)solve.add(i,endPoint,inf,0);\n\n while(m--){\n int x,y;\n scanf(\"%d%d\",&x,&y);\n for(int i=1;i<100;i++){\n int p=(x-1)*100+i;\n int pp=(y-1)*100+i+1;\n for(int j=1;j<=50;j++){\n solve.add(p,pp,1,d*(j*j-(j-1)*(j-1))+c);\n }\n }\n\n for(int i=1;i<100;i++){\n int p=(y-1)*100+i;\n int pp=(x-1)*100+i+1;\n for(int j=1;j<=50;j++){\n solve.add(p,pp,1,d*(j*j-(j-1)*(j-1))+c);\n }\n }\n\n }\n\n printf(\"%d\\n\",solve.minCost(startPoint,endPoint));\n return 0;\n\n\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "baf9d11fec8e2812457482adf13034e4", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\nconst int maxn=6000;\nconst int inf=INT_MAX;\nconst ll inff=1e18;\nconst ll mod=1e9+7;\n#define pii pair\n#define mkp make_pair\n#define F first\n#define S second\n#define pb push_back\n#define sz(v) ((int)(v).size())\n#define all(v) (v).begin(),(v).end()\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define int ll\n//#define endl '\\n'\n\nint n,s,t;\nstruct edge\n{\n int to,cap,cost,rev;\n};\nvector G[maxn];\nint dist[maxn];\nint prevv[maxn],preve[maxn];\nint inq[maxn];\n\nvoid init(int _n,int _s,int _t)\n{\n for (int i=0;i<=_n;i++) G[i].clear();\n n=_n;\n s=_s;\n t=_t;\n}\n\nvoid add_edge(int from,int to,int cap,int cost)\n{\n G[from].pb({to,cap,cost,sz(G[to])});\n G[to].pb({from,0,-cost,sz(G[from])-1});\n}\n\nint min_cost_flow(int f)\n{\n int res=0;\n while (f>0)\n {\n fill(dist,dist+n,inff);\n dist[s]=0;\n queue q;\n q.push(s);\n inq[s]=1;\n while (!q.empty())\n {\n int now=q.front();\n q.pop();\n inq[now]=0;\n for (int i=0;i0&&dist[now]+e.cost H[57];\n\nint32_t main()\n{\n IOS\n cin>>N>>M>>K>>C>>D;\n for (int i=1;i<=K;i++)\n {\n cin>>X;\n A[X]++;\n }\n for (int i=1;i<=M;i++)\n {\n cin>>X>>Y;\n H[X].pb(Y);\n H[Y].pb(X);\n }\n\n init(5103,0,5101);\n for (int i=2;i<=N;i++) add_edge(0,i,A[i],0);\n for (int i=0;i<=100;i++)\n {\n for (int l=1;l<=N;l++)\n {\n add_edge(50*i+l,50*(i+1)+l,K,0);\n for (int j:H[l])\n {\n for (int k=1;k<=K;k++)\n {\n add_edge(50*i+l,50*(i+1)+j,1,D*(2*k-1));\n }\n }\n }\n }\n for (int i=1;i<=101;i++) add_edge(50*i+1,5101,K,C*i);\n cout<\nusing namespace std;\n#define _USE_MATH_DEFINES\n\n#include \n#include \nusing namespace __gnu_pbds;\ntemplate \nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n// DON'T USE THESE MACROS DURING ICPC PRACTICE\n#define For(i,n) for (int i=0; i=0; i--)\n#define DOWN(i,a,b) for (int i=b; i>=a; i--)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef complex pt;\ntypedef vector pol;\ntypedef vector vi;\n\nconst char nl = '\\n';\nconst ll INF = 0x3f3f3f3f;\nconst ll INFLL = 0x3f3f3f3f3f3f3f3f;\nconst ll MOD = 1e9+7;\nconst ld EPS = 1e-10;\nmt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\n\nconst int T = 99;\nconst int C = 15;\nnamespace Flow {\n\tconst int N = 50*T + 2;\n\tconst int M = 2*N*C + N + 7;\n\t// data structures and helper functions common to all flow routines\n\tint par[N], first[N], nxt[2*M], ep[2*M], m;\n\tll flo[2*M], cap[2*M], cost[2*M];\n\tvoid init() { m = 0; memset(first,-1,sizeof first); memset(flo,0,sizeof flo); }\n\tvoid add_edge(int a, int b, ll c=1, ll p=0) { //a,b - nodes, c, p - cap, price\n\t\tnxt[m] = first[ep[m]=a], first[ep[m]] = m, cap[m] = c, cost[m] = p, ++m;\n\t\tnxt[m] = first[ep[m]=b], first[ep[m]] = m, cap[m] = 0, cost[m] = -p, ++m; }\n\t// Minimum cost maximum flow, assuming there are no negative cost cycles\n\t// USAGE: 1) init(); 2) add edges, 3) mcf_pot_init(numV) and 4)\n\t// ll price=0,flow=0; while (ll df = mcf_update(s, t, price, numV)) flow += df;\n\t//! for sparse graphs, may help to change Dijkstra from O(N^2) to O(M*lgN)\n\tint q[N], inq[N];\n#define BUBL { \\\n\tt = q[i]; q[i] = q[j]; q[j] = t; \\\n\tt = inq[q[i]]; inq[q[i]] = inq[q[j]]; inq[q[j]] = t; }\n\tbool vis[N]; ll pot[N], dist[N];\n\tvoid mcf_pot_init(int n) { memset(pot, 0, sizeof pot);\n\t\t// if all edge costs >= 0, we can safely return before the Bellman-Ford here\n\t\tfor (int k = 1; k < n; ++k) for (int e = 0; e < m; ++e)\n\t\t\tif (cap[e]) pot[ep[e^1]] = min(pot[ep[e^1]], pot[ep[e]] + cost[e]); }\n\tll mcf_update(int s, int t, ll& price, int n) {\n\t\tmemset(vis, 0, sizeof vis); memset(dist, INF, sizeof dist); dist[s] = 0;\n\t\tmemset(inq, -1, sizeof inq);\n\t\tint qs = 0; inq[q[qs++] = s] = 0;\n\t\twhile (qs) {\n\t\t\tint u = q[0]; inq[u] = -1;\n\t\t\tq[0] = q[--qs];\n\t\t\tif( qs ) inq[q[0]] = 0;\n\t\t\tfor( int i = 0, j = 2*i + 1, t; j < qs; i = j, j = 2*i + 1 ) {\n\t\t\t\tif( j + 1 < qs && dist[q[j + 1]] < dist[q[j]] ) j++;\n\t\t\t\tif( dist[q[j]] >= dist[q[i]] ) break;\n\t\t\t\tBUBL; }\n\t\t\tif (vis[u] == 1) continue; vis[u] = 1;\n\t\t\tfor (int e = first[u]; e != -1; e = nxt[e]) { int v = ep[e^1];\n\t\t\t\tif (flo[e] < cap[e] && dist[v] > dist[u] + pot[u] - pot[v] + cost[e]) {\n\t\t\t\t\tdist[v] = dist[u] + pot[u] - pot[v] + cost[e], par[v] = e;\n\t\t\t\t\tif( inq[v] < 0 ) { inq[q[qs] = v] = qs; qs++; }\n\t\t\t\t\tfor( int i = inq[v], j = ( i - 1 )/2, t;\n\t\t\t\t\t\t\tdist[q[i]] < dist[q[j]]; i = j, j = ( i - 1 )/2 )\n\t\t\t\t\t\tBUBL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] >= INF) return 0; ll df = INF;\n\t\tfor (int e, i = t; i != s; i = ep[e])\n\t\t\te = par[i], df = min(df, cap[e] - flo[e]);\n\t\tfor (int e, i = t; i != s; i = ep[e])\n\t\t\te = par[i], flo[e] += df, flo[e^1] -= df, price += df*cost[e];\n\t\tfor (int i = 0; i < n; ++i) pot[i] = min(INF, dist[i] + pot[i]);\n\t\treturn df; }\n}\n\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0); cout.tie(0);\n\tcout << fixed << setprecision(10);\n\n\tint n,m,k,c,d;\n\tcin>>n>>m>>k>>c>>d;\n\n\tconst int source = 0;\n\tconst int sink = n*T+1;\n\tFlow::init();\n\n\tfor(int i=0;i>a;\n\t\tFlow::add_edge(source, a, 1, 0);\n\t}\n\tfor(int t=0;t0) {\n\t\t\tfor(int i=1;i<=n;i++) {\n\t\t\t\tFlow::add_edge(i+(t-1)*n, i+t*n, INF, c);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i=0;i> a >> b;\n\t\tfor(int j=0;j\n#define fi first\n#define se second\nusing namespace std;\n\nconst int MX = 5500;\nconst long long INF = 1E9 + 7;\n\nint n, m, k, c, d;\nint u, v, mx, s = MX - 2, t = MX - 1, mf = 0, tr[MX];\nlong long ans = 0, dis[MX];\npriority_queue, vector>, greater>> pq;\n\nstruct SEdge\n{\n int v, c;\n int ind, cap;\n};\nvector adj[MX];\n\nvoid add_edge(int u, int v, int c, int cap)\n{\n int s1 = adj[u].size(), s2 = adj[v].size();\n adj[u].push_back((SEdge){v, c, s2, cap});\n adj[v].push_back((SEdge){u, -c, s1, 0});\n}\n\nvoid dijkstra()\n{\n fill(dis, dis + MX, INF);\n pq.push({dis[s] = 0, s});\n while (!pq.empty())\n {\n pair u = pq.top();\n pq.pop();\n if (u.fi > dis[u.se])\n continue;\n for (SEdge &ed : adj[u.se])\n {\n if (ed.cap > 0 && dis[ed.v] > u.fi + ed.c)\n {\n tr[ed.v] = ed.ind;\n pq.push({dis[ed.v] = u.fi + ed.c, ed.v});\n }\n }\n }\n}\n\nvoid trace()\n{\n int mx = k, v = t;\n while (v != s)\n {\n int u = adj[v][tr[v]].v, ind = adj[v][tr[v]].ind;\n mx = min(mx, adj[u][ind].cap);\n v = u;\n }\n mf += mx; ans += mx * dis[t];\n v = t;\n while (v != s)\n {\n int u = adj[v][tr[v]].v, ind = adj[v][tr[v]].ind;\n adj[v][tr[v]].cap += mx;\n adj[u][ind].cap -= mx;\n v = u;\n }\n}\n\nint at(int u, int t)\n{\n return u + t * n;\n}\n\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cin >> n >> m >> k >> c >> d;\n mx = n + k;\n for (int i = 0; i < k; i++)\n {\n cin >> u;\n add_edge(s, at(u, 0), 0, 1);\n }\n for (int i = 0; i <= mx; i++)\n add_edge(at(1, i), t, c * i, k);\n for (int i = 1; i <= n; i++)\n for (int j = 0; j < mx; j++)\n add_edge(at(i, j), at(i, j + 1), 0, k);\n while (m--)\n {\n cin >> u >> v;\n for (int i = 1; i <= k; i++)\n for (int j = 0; j < mx; j++)\n {\n add_edge(at(u, j), at(v, j + 1), d * (2 * i - 1), 1);\n add_edge(at(v, j), at(u, j + 1), d * (2 * i - 1), 1);\n }\n }\n while (mf < k)\n {\n dijkstra();\n trace();\n }\n cout << ans;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "b26cf1834847ba9339b477845b192d96", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define maxn 15000\nstruct Edge{\n int to,cap,flow,cost;\n};\nvector e;\nvector head[maxn];\nlong long ans,dis[maxn];\nint n,m,k,c,d,p[maxn],pe[maxn],inq[maxn],s=9998,t=9999,cur,i,j;\nvoid add_edge(int x, int y, int c, int cost) {\n\thead[x].push_back(e.size()),e.push_back(Edge{y, c, 0, cost});\n\thead[y].push_back(e.size()),e.push_back(Edge{x, 0, 0, -cost});\n}\nvoid push_rebel() {\n fill(dis,dis+10000,(long long)1e18),fill(p,p+10000,-1),fill(pe,pe+10000,-1),fill(inq,inq+10000,0);\n dis[s] = 0;\n queue q;\n q.push(s),inq[s] = 1,cur = t;\n while(!q.empty()){\n int k = q.front();q.pop();\n inq[k] = 0;\n for(auto x: head[k]) {\n if(!(e[x].cap - e[x].flow)) continue;\n int c = e[x].cost,y = e[x].to;\n if(dis[y]>dis[k]+c) {\n dis[y] = dis[k] + c,p[y] = k,pe[y] = x;\n if(!inq[y]) inq[y] = 1,q.push(y);\n }\n }\n }\n\twhile(cur != s) {\n\t\te[pe[cur]].flow++;\n\t\te[pe[cur] ^ 1].flow--;\n\t\tcur = p[cur];\n }\n\tans += dis[t];\n}\nint main() {\n cin >> n >> m >> k >> c >> d;\n int x,y;\n for(int i = 1; i<=k;i++) { cin >> x;x--;add_edge(s,x,1,0);}\n for(int i = 1; i<=101;i++) add_edge((i-1)*n,t,k,(i-1)*c);\n for(int i = 1; i<=m; i++){\n\t\tcin >> x >> y;x--,y--;\n\t\tfor(int i = 1; i<=100; i++) for(int j = 1; j<=k; j++)\n\t\t\tadd_edge(x+(i-1)*n,y+(i-1)*n+n,1,d*((j-1)*2+1)),add_edge(y+(i-1)*n,x+(i-1)*n+n,1,d*((j-1)*2+1));\n \t}\n \tfor(int i = 1; i<=n; i++) for(int j = 1; j<=100; j++) add_edge(i-1+(j-1)*n,i-1+(j-1)*n+n,k,0);\n for(int i = 1;i<=k;i++) push_rebel();\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "2568b8363701820719ec701deaf4e9b0", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define rep(i,n) for (int i = 0; i < n; i++)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pi;\ntypedef pair pp;\ntypedef pair pl;\nconst double EPS = 1e-9;\nconst ll MOD = 1000000007;\nconst int inf = 1 << 30;\nconst ll linf = 1LL << 60;\n\nstruct edge{int to, cap, cost, rev; };\n\nstruct MIN_COST_FLOW {\n #define MAX_V 5010\n int V;\n vector G[MAX_V];\n int h[MAX_V];\n int dist[MAX_V];\n int prevv[MAX_V], preve[MAX_V];\n\n void init (int n) {\n V = n;\n }\n\n void add_edge(int from, int to, int cap, int cost){\n //cerr << from << \" \" << to << \" \" << cap << \" \" << cost << endl;\n G[from].push_back((edge){to, cap, cost, (int)G[to].size()});\n G[to].push_back((edge){from, 0, -cost, (int)G[from].size()-1});\n }\n\n int min_cost_flow(int s, int t, int f){\n int res = 0;\n fill(h, h+V, 0);\n while(f > 0){ \n priority_queue, greater > que;\n fill(dist, dist+V, inf);\n dist[s] = 0;\n que.push(pi(0,s));\n while(!que.empty()){\n pi p = que.top(); que.pop();\n int v = p.second;\n if(dist[v] < p.first) continue;\n for(int i = 0; i < G[v].size(); i++){\n edge &e = G[v][i];\n if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(pi(dist[e.to],e.to));\n }\n }\n }\n if(dist[t] == inf){\n return -1;\n }\n for(int v = 0; v < V; v++) h[v] += dist[v];\n \n int d = f;\n for(int v = t; v != s; v = prevv[v]){\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d*h[t];\n for(int v = t; v != s; v = prevv[v]){\n edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nMIN_COST_FLOW mcf;\n\nint n, m, k, c, d;\nint a[50];\nint cnt[50];\nvector e[50];\n\nconst int T = 100;\n\nint main() {\n scanf(\"%d%d%d%d%d\", &n, &m, &k, &c, &d);\n rep(i,k) scanf(\"%d\", a+i);\n rep(i,k) cnt[a[i]-1]++;\n rep(i,m) {\n int x, y; scanf(\"%d%d\", &x, &y);\n x--; y--;\n e[x].push_back(y);\n e[y].push_back(x);\n }\n mcf.init(n*T+2);\n rep(i,n) mcf.add_edge(n*T, i, cnt[i], 0);\n rep(i,T) mcf.add_edge(i*n, n*T+1, k, c*i);\n rep(i,n) {\n rep(j,T-1) mcf.add_edge(j*n+i, (j+1)*n+i, k, 0);\n for (int to: e[i]) {\n rep(j,T-1) {\n rep(u,k) {\n int cost = (u+1)*(u+1)*d-u*u*d;\n mcf.add_edge(j*n+i, (j+1)*n+to, 1, cost);\n }\n }\n }\n }\n printf(\"%d\\n\", mcf.min_cost_flow(n*T, n*T+1, k));\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "510e5b5185cee1d3675d2ef85b0bfaf0", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\n\nconst int N=10005,S=N-2,T=N-1;\nconst int M=2e6+10;\nconst int inf=2e9;\n\nint head[N],to[M],nxt[M],w[M],c[M],cnt=1;\n\ninline void addedge(int x, int y, int flow, int cost){\n\tto[++cnt]=y,nxt[cnt]=head[x],head[x]=cnt;\n\tw[cnt]=flow,c[cnt]=cost;\n\tto[++cnt]=x,nxt[cnt]=head[y],head[y]=cnt;\n\tw[cnt]=0,c[cnt]=-cost;\n}\n\nqueue q;\nlong long dis[N];\nbool mark[N];\n\ninline bool spfa(){\n\tmemset(mark,0,sizeof(mark));\n\tmemset(dis,0x3f,sizeof(dis));\n\tq.push(T); dis[T]=0; mark[T]=1;\n\twhile (!q.empty()){\n\t\tint x=q.front();\n\t\tmark[x]=0; q.pop();\n\t\tfor (int i=head[x];i;i=nxt[i]){\n\t\t\tint y=to[i];\n\t\t\tif (w[i^1]&&dis[y]>dis[x]+c[i^1]){\n\t\t\t\tdis[y]=dis[x]+c[i^1];\n\t\t\t\tif (!mark[y]){\n\t\t\t\t\tq.push(y); mark[y]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn dis[S]<1e18;\n}\n\ninline int dfs(int x, int lim, ll &cost){\n\tint flow=0; mark[x]=1;\n\tif (x==T) return lim;\n\tfor (int i=head[x];i;i=nxt[i]){\n\t\tint y=to[i];\n\t\tif (!w[i]||mark[y]) continue;\n\t\tif (dis[x]!=dis[y]+c[i]) continue;\n\t\tint t=dfs(y,min(w[i],lim-flow),cost);\n\t\tw[i]-=t; w[i^1]+=t;\n\t\tflow+=t; cost+=t*c[i];\n\t\tif (flow==lim) return lim;\n\t}\n\treturn flow;\n}\n\ninline ll costflow(){\n\tint flow=0; ll cost=0;\n\twhile (spfa())\n\t\tdo{\n\t\t\tmemset(mark,0,sizeof(mark));\n\t\t\tflow+=dfs(S,inf,cost);\n\t\t}while (mark[T]);\n\treturn cost;\n}\n\nint main(){\n\tint n,m,k,c,d,lim=101;\n\tcin>>n>>m>>k>>c>>d;\n\tfor (int i=0;i>x; x--;\n\t\taddedge(S,x,1,0);\n\t}\n\tfor (int i=0;i>x>>y; x--,y--;\n\t\tfor (int i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#define inf 1e9\n#define K 210\n#define N 100010\n#define M 2000100\nusing namespace std;\ninline int read()\n{\n\tchar ch=getchar();\n int s=0,w=1;\n while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}\n\twhile(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}\n\treturn s*w;\n}\nstruct Edge\n{\n\tint next,to,fl,v;\n}e[M<<1];\nint cnt=1,head[N];\nint s,t;\nint pre[N],dist[N],vis[N];\nqueueQ;\nint minn[N];\nint n,m,k,c,d;\nint A[K];\nint v[K][K];\ninline void add_edge(int from,int to,int fl,int v){e[++cnt].to=to;e[cnt].next=head[from];e[cnt].fl=fl;e[cnt].v=v;head[from]=cnt;}\nvoid update(int x,int flow)\n{\n\te[pre[x]].fl-=flow;\n\te[pre[x]^1].fl+=flow;\n\tif(e[pre[x]^1].to)update(e[pre[x]^1].to,flow);\n}\ninline int spfa()\n{\n\tmemset(vis,0,sizeof(vis));while(!Q.empty())Q.pop();\n\tfor(register int i=1;i<=t;i++)dist[i]=inf;\n\tminn[s]=inf;dist[s]=0;Q.push(s);vis[s]=1;\n\twhile(!Q.empty())\n\t{\n\t\tint x=Q.front();vis[x]=0;Q.pop();\n\t\tfor(register int i=head[x];i;i=e[i].next)\n\t\t{\n\t\t\tif(e[i].fl&&dist[e[i].to]>dist[x]+e[i].v)\n\t\t\t{\n\t\t\t\tdist[e[i].to]=dist[x]+e[i].v;\n\t\t\t\tminn[e[i].to]=min(minn[x],e[i].fl);\n\t\t\t\tpre[e[i].to]=i;\n\t\t\t\tif(!vis[e[i].to])\n\t\t\t\t{\n\t\t\t\t\tvis[e[i].to]=1;\n\t\t\t\t\tQ.push(e[i].to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(dist[t]==inf)return inf;\n\tupdate(t,minn[t]);\n\treturn dist[t]*minn[t];\n}\ninline int EK()\n{\n\tint sum=0;\n\twhile(1)\n\t{\n\t\tint x=spfa();\n\t\tif(x==inf)return sum;\n\t\tsum+=x;\n\t}\n\treturn sum;\n}\ninline int idx(int pos,int x){return pos*n+x;}\nint main()\n{\n\tn=read(),m=read(),k=read(),c=read(),d=read();\n\tt=n*201+1;\n\tfor(register int i=1;i<=k;i++)A[i]=read();\n\tfor(register int i=1;i<=m;i++)\n\t{\n\t\tint x=read(),y=read();\n\t\tv[x][y]++,v[y][x]++;\n\t}\n\tfor(register int i=1;i<=k;i++)\n\t{\n\t\tadd_edge(s,idx(0,A[i]),1,0);add_edge(idx(0,A[i]),s,0,0);\n\t}\n\tfor(register int p=0;p<=199;p++)\n\t{\n\t\tfor(register int i=1;i<=n;i++)\n\t\t{\n\t\t\tfor(register int j=1;j<=n;j++)\n\t\t\t{\n\t\t\t\tif(!v[i][j])continue;\n\t\t\t\tfor(register int k=1;k<=v[i][j];k++)\n\t\t\t\t{\n\t\t\t\t\tfor(register int l=1;l<=n;l++)\n\t\t\t\t\t{\n\t\t\t\t\t\tadd_edge(idx(p,i),idx(p+1,j),1,2*d*l-d);\n\t\t\t\t\t\tadd_edge(idx(p+1,j),idx(p,i),0,d-2*d*l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tadd_edge(idx(p,i),idx(p+1,i),inf,0);add_edge(idx(p+1,i),idx(p,i),0,0);\n\t\t}\n\t}\n\tfor(register int p=0;p<=200;p++)\n\t{\n\t\tadd_edge(idx(p,1),t,inf,c*p);add_edge(t,idx(p,1),0,-c*p);\n\t}\n\tprintf(\"%d\\n\",EK());\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "210d652bb1083aac86aaf8f71ad7db1f", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"Ofast\")\n#include \nusing namespace std;\nusing i64=int_least64_t;\n\nnamespace std\n{\n // hash\n template size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); }\n template struct hash> { size_t operator()(pair const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } };\n template ::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc::apply(seed, t), get(t)); } };\n template struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } };\n template struct hash> { size_t operator()(tuple const &t) const { return tuple_hash_calc>::apply(0, t); } };\n // iostream\n template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second; }\n template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; }\n template struct tupleis { static istream &apply(istream &is, tuple_t &t) { tupleis::apply(is, t); return is >> get(t); } };\n template struct tupleis { static istream &apply(istream &is, tuple_t &t) { return is; } };\n template istream &operator>>(istream &is, tuple &t) { return tupleis, tuple_size>::value - 1>::apply(is, t); }\n template <> istream &operator>>(istream &is, tuple<> &t) { return is; }\n template struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { tupleos::apply(os, t); return os << ' ' << get(t); } };\n template struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } };\n template ostream &operator<<(ostream &os, const tuple &t) { return tupleos, tuple_size>::value - 1>::apply(os, t); }\n template <> ostream &operator<<(ostream &os, const tuple<> &t) { return os; }\n template , string>::value, nullptr_t> = nullptr>\n istream& operator>>(istream& is, Container &cont) { for(auto&& e : cont) is >> e; return is; }\n template , string>::value, nullptr_t> = nullptr>\n ostream& operator<<(ostream& os, const Container &cont) { bool flag = 1; for(auto&& e : cont) flag ? flag = 0 : (os << ' ', 0), os << e; return os; }\n} // namespace std\n\n#include \n#include \n#include \n\n// Base class of flow algorithms\ntemplate \nstruct flow_base\n{\n struct edge_t\n {\n size_t src, dst;\n cap_t cap; cost_t cost;\n edge_t *rev;\n edge_t(size_t _src, size_t _dst, cap_t _cap, cost_t _cost, edge_t *_rev)\n : src(_src), dst(_dst), cap(_cap), cost(_cost), rev(_rev) {}\n }; // struct edge_t\n\nprotected:\n using adj_type = std::vector>;\n adj_type adj;\n\npublic:\n flow_base(size_t _n = 0) : adj(_n) {}\n\n flow_base(const flow_base& other) : adj(other.size())\n {\n for(size_t node{0}; node != other.size(); ++node)\n {\n for(const auto &e : other[node])\n {\n if(e.src == node)\n {\n adj[e.src].emplace_back(e);\n adj[e.dst].emplace_back(*e.rev);\n adj[e.src].back().rev = &adj[e.dst].back();\n adj[e.dst].back().rev = &adj[e.src].back();\n e.rev->src = -1;\n }\n else\n {\n e.rev->rev->src = node;\n }\n }\n }\n }\n\n flow_base &operator=(const flow_base &other)\n {\n if(this != &other)\n {\n flow_base cop(other);\n std::swap(adj, cop.adj);\n }\n return *this;\n }\n\n size_t size() const { return adj.size(); }\n\n typename decltype(adj)::reference operator[](size_t node) { assert(node < size()); return adj[node]; }\n typename decltype(adj)::const_reference operator[](size_t node) const { assert(node < size()); return adj[node]; }\n\n void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)\n {\n assert(src < size()); assert(dst < size());\n if(cap <= 0) return;\n adj[src].emplace_back(src, dst, cap, cost, nullptr);\n adj[dst].emplace_back(dst, src, 0, -cost, nullptr);\n adj[src].back().rev = &adj[dst].back();\n adj[dst].back().rev = &adj[src].back();\n }\n}; // struct flow_base\n\n\ntemplate \nclass min_cost_flow : public flow_base\n{\n using base = flow_base;\n using edge_t = typename base::edge_t;\n\n template \n constexpr bool valid(const type &val) const { return val != static_cast(-1); }\n\n template \n constexpr void make_invalid(type &val) const { val = -1; }\n\n cost_t min_cost, total_cost;\n std::vector supp;\n std::vector ptnl, dist;\n std::vector last;\n\n void copy_member(const min_cost_flow &other)\n {\n min_cost = other.min_cost;\n total_cost = other.total_cost;\n supp = other.supp;\n ptnl = other.ptnl;\n }\n\n bool Dijkstra(size_t src, const size_t dst)\n {\n const cost_t infty(total_cost + 1);\n // static std::vector dist(base::size(), infty);\n fill(dist.begin(), dist.end(), infty);\n dist[src] = 0;\n //*/\n std::priority_queue> que;\n que.emplace(0, src);\n while(!que.empty())\n {\n auto [ndist, now] = que.top();\n ndist = -ndist, que.pop();\n if(ndist != dist[now]) continue;\n ptnl[now] += ndist;\n make_invalid(dist[now]);\n for(edge_t &e : base::adj[now])\n {\n if(!valid(dist[e.dst]) || e.cap == static_cast(0)) continue;\n if(dist[e.dst] + ptnl[e.dst] > ptnl[now] + e.cost)\n {\n dist[e.dst] = ptnl[now] + e.cost - ptnl[e.dst];\n last[e.dst] = &e;\n que.emplace(-dist[e.dst], e.dst);\n }\n }\n }\n /*/\n while(valid(src))\n {\n ptnl[src] += dist[src];\n make_invalid(dist[src]);\n for(edge_t &e : base::adj[src])\n {\n if(!valid(dist[e.dst]) || e.cap == static_cast(0)) continue;\n if(dist[e.dst] + ptnl[e.dst] > ptnl[src] + e.cost)\n {\n dist[e.dst] = ptnl[src] + e.cost - ptnl[e.dst];\n last[e.dst] = &e;\n }\n }\n make_invalid(src);\n cost_t spath{infty};\n for(size_t node{}; node != base::size(); ++node)\n {\n if(!valid(dist[node])) continue;\n if(spath > dist[node])\n {\n spath = dist[node];\n src = node;\n }\n }\n }\n //*/\n return !valid(dist[dst]);\n }\n\npublic:\n min_cost_flow(size_t _n = 0) : base::flow_base(_n + 2), min_cost(0), total_cost(0), supp(_n + 2), ptnl(_n + 2), dist(_n + 2), last(_n + 2) {}\n\n min_cost_flow(const min_cost_flow &other) : base::flow_base(other)\n {\n copy_member(other);\n }\n\n min_cost_flow &operator=(const min_cost_flow &other)\n {\n base::operator=(other);\n copy_member(other);\n return *this;\n }\n\n size_t size() const { return base::size() - 2; }\n\n void add_edge(size_t src, size_t dst, cap_t cap, cost_t cost)\n {\n assert(src != dst);\n if(cap <= static_cast(0)) return;\n if(cost < static_cast(0))\n {\n supp[src] -= cap;\n supp[dst] += cap;\n min_cost += cap * cost;\n total_cost -= cap * cost;\n base::add_edge(dst, src, cap, -cost);\n }\n else\n {\n total_cost += cap * cost;\n base::add_edge(src, dst, cap, cost);\n }\n }\n\n void supply(size_t node, cap_t vol) { supp[node] += vol; }\n\n bool solve()\n {\n assert(base::size() > 1);\n const size_t src(base::size() - 2), dst(src + 1);\n cap_t flow(0), supp_sum(0);\n for(size_t node{}; node != base::size(); ++node)\n {\n supp_sum += supp[node];\n if(supp[node] > static_cast(0))\n {\n flow += supp[node];\n add_edge(src, node, supp[node], 0);\n }\n else if(supp[node] < static_cast(0))\n {\n add_edge(node, dst, -supp[node], 0);\n }\n supp[node] = 0;\n }\n if(supp_sum != static_cast(0)) return false;\n while(flow > static_cast(0))\n {\n if(!Dijkstra(src, dst)) return false;\n cap_t resid{flow};\n for(size_t node{dst}; node != src; node = last[node]->src) resid = std::min(resid, last[node]->cap);\n flow -= resid;\n min_cost += ptnl[dst] * resid;\n for(size_t node{dst}; node != src; node = last[node]->src)\n {\n last[node]->cap -= resid;\n last[node]->rev->cap += resid;\n }\n // cerr << \"resid: \" << resid << \" shortest: \" << ptnl[dst] << endl;\n }\n return true;\n }\n\n cost_t optimal() { return solve(), min_cost; }\n};\n\nmain()\n{\n min_cost_flow mcf(6100);\n int n,m,k,c,d; cin>>n>>m>>k>>c>>d;\n const int tlim=105;\n for(int i=0; i>j; j--;\n mcf.supply(j,1);\n }\n for(int i=0; i>u>>v; --u,--v;\n for(int t=0; t\nusing namespace std;\n#define _USE_MATH_DEFINES\n\n#include \n#include \nusing namespace __gnu_pbds;\ntemplate \nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n// DON'T USE THESE MACROS DURING ICPC PRACTICE\n#define For(i,n) for (int i=0; i=0; i--)\n#define DOWN(i,a,b) for (int i=b; i>=a; i--)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef complex pt;\ntypedef vector pol;\ntypedef vector vi;\n\nconst char nl = '\\n';\nconst ll INF = 0x3f3f3f3f;\nconst ll INFLL = 0x3f3f3f3f3f3f3f3f;\nconst ll MOD = 1e9+7;\nconst ld EPS = 1e-10;\nmt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\n\nconst int T = 99;\nconst int C = 12;\nnamespace Flow {\n\tconst int N = 50*T + 2;\n\n // data structures and helper functions common to all flow routines\n struct Edge { int v, r; ll f, c, p; };\n vector adj[N]; int sz[N]; ll mc;\n void init(int n=N) { mc=0; fill(sz,sz+n,0); fill(adj,adj+n,vector()); }\n void add_edge(int a, int b, ll c=1, ll p=0) { mc = max(mc,c);\n adj[a].push_back({b, sz[b]++, 0, c, p});\n adj[b].push_back({a, sz[a]++, 0, 0, -p}); } // change 0 to c for undirected\n\n // Minimum cost maximum flow, assuming there are no negative cost cycles\n // USAGE: 1) add edges 2) min_cost_flow(S, T, price, V, sparse?);\n // use O(V^2) Dijkstra for dense graphs, and O(E*log(V)) otherwise\n bool vis[N]; int par[N]; ll pot[N], dist[N];\n void pot_init(int n) { fill(pot, pot+n, 0);\n // if all edge costs >= 0, we don't need to run the Bellman-Ford here\n for(int i=1; i> dk; dk.push({0,s}); while(!dk.empty()) {\n int u = dk.top().second; dk.pop(); if(vis[u]) continue;\n for(const Edge& e:adj[u]) {\n if (e.f < e.c && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r;\n dk.push({-dist[e.v], e.v}); } } }\n } else { // dense\n for(int u = s; u != -1; ) { vis[u] = true; for(const Edge& e: adj[u]) {\n if (e.f < e.c && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r; } }\n u = -1; ll best = INF;\n for(int i=0; i= INF) { return 0; } ll df = INF;\n for(int u = t; u != s; ) { const Edge& r = adj[u][par[u]];\n df = min(df, adj[r.v][r.r].c - adj[r.v][r.r].f); u = r.v; }\n for(int u = t; u != s; ) { Edge& r=adj[u][par[u]], &e=adj[r.v][r.r];\n e.f += df; r.f -= df; price += df*e.p; u = r.v; }\n for(int i=0; i>n>>m>>k>>c>>d;\n\n\tconst int source = 0;\n\tconst int sink = n*T+1;\n\n\tfor(int i=0;i>a;\n\t\tFlow::add_edge(source, a, 1, 0);\n\t}\n\tfor(int t=0;t0) {\n\t\t\tfor(int i=1;i<=n;i++) {\n\t\t\t\tFlow::add_edge(i+(t-1)*n, i+t*n, INF, c);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i=0;i> a >> b;\n\t\tfor(int j=0;j\nusing namespace std;\n\nconst int inf = INT_MAX;\n\nnamespace mcmf {\n\nconst int N = 10001;\nstruct edge { int v, p; int c, w; };\nvector g[N];\nint vis[N], cur[N], n;\nint dis[N], pot[N];\n\nint addv(int c) {\n while (c--) g[n++].clear();\n return n - 1;\n}\n\nvoid adde(int u, int v, int c, int w) {\n g[u].push_back({ v, (int)g[v].size(), c, w });\n g[v].push_back({ u, (int)g[u].size() - 1, 0, -w });\n}\n\nbool dijkstra(int s, int t) {\n fill_n(dis, n, inf);\n fill_n(vis, n, 0);\n typedef pair pli;\n priority_queue, greater> q;\n q.push({ dis[s] = 0, s });\n while (!q.empty()) {\n int u; int du; tie(du, u) = q.top(); q.pop();\n if (vis[u]) continue; vis[u] = 1;\n for (edge e : g[u]) {\n int v = e.v; int dv = du + pot[u] + e.w - pot[v];\n if (!e.c || vis[v] || dis[v] <= dv) continue;\n q.push({ dis[v] = dv, v });\n }\n }\n return vis[t];\n}\n\nbool dinic_bfs(int s, int t) {\n fill_n(vis, n, 0);\n queue q; q.push(s); vis[s] = 1;\n while (!q.empty()) {\n int u = q.front(); q.pop();\n for (edge e : g[u]) {\n int v = e.v;\n if (vis[v] || !e.c) continue;\n if (dis[v] != dis[u] + e.w - pot[v] + pot[u]) continue;\n vis[v] = vis[u] + 1; q.push(v);\n }\n }\n return vis[t] != 0;\n}\n\nint dinic_dfs(int u, int t, int f) {\n if (u == t)\n return f;\n int sf = 0;\n for (int& i = cur[u]; i != g[u].size(); ++i) {\n edge& e = g[u][i]; int v = e.v;\n if (!e.c || vis[v] != vis[u] + 1) continue;\n if (dis[v] != dis[u] + e.w - pot[v] + pot[u]) continue;\n int df = dinic_dfs(v, t, min(e.c, f));\n //if (df && u != 12 && v != 12 && u != 13 && v != 13) cout << u << ' ' << v << ' ' << df << ' ' << df * e.w << endl;\n sf += df; f -= df; e.c -= df; g[v][e.p].c += df;\n if (!f) break;\n }\n return sf;\n}\n\npair mincostflow(int s, int t, int mf) {\n int sf = 0, sc = 0;\n fill_n(pot, n, 0);\n while (dijkstra(s, t)) {\n int f = 0;\n while (dinic_bfs(s, t)) {\n fill_n(cur, n, 0);\n int df = dinic_dfs(s, t, mf);\n mf -= df; f += df;\n if (!mf) break;\n }\n sf += f; sc += (dis[t] + pot[t]) * f;\n for (int i = 0; i < n; ++i) pot[i] += dis[i];\n if (!mf) break;\n }\n return { sf, sc };\n}\n\n}\n\nint main(void){\n int n, m, k, c, d;\n scanf(\"%d %d %d %d %d\", &n, &m, &k, &c, &d);\n int l = max(n, k) * 2;\n mcmf::addv(l * n);\n int s = mcmf::addv(1), t = mcmf::addv(1);\n for (int j = 0; j < k; ++j) {\n int a; scanf(\"%d\", &a);\n a--;\n mcmf::adde(s, a, 1, 0);\n }\n for (int z = 1; z <= m; ++z) {\n int u, v; scanf(\"%d %d\", &u, &v);\n u--; v--;\n for (int i = 0; i + 1 < l; ++i) {\n int u1 = u + i * n, v1 = v + (i + 1) * n;\n int u2 = u + (i + 1) * n, v2 = v + i * n;\n for (int j = 0; j < k; ++j) {\n mcmf::adde(u1, v1, 1, c + d * (2 * j + 1));\n mcmf::adde(v2, u2, 1, c + d * (2 * j + 1));\n }\n }\n }\n for (int i = 0; i + 1 < l; ++i) {\n for (int j = 0; j < n; ++j) {\n int u = j + i * n, v = j + (i + 1) * n;\n mcmf::adde(u, v, inf, c);\n }\n }\n for (int i = 0; i < l; ++i)\n mcmf::adde(i * n, t, inf, 0);\n printf(\"%d\\n\", mcmf::mincostflow(s, t, inf).second);\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "ef03a1adc39515a07fd433d00e66c062", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n#define _USE_MATH_DEFINES\n\n#include \n#include \nusing namespace __gnu_pbds;\ntemplate \nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n// DON'T USE THESE MACROS DURING ICPC PRACTICE\n#define For(i,n) for (int i=0; i=0; i--)\n#define DOWN(i,a,b) for (int i=b; i>=a; i--)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef complex pt;\ntypedef vector pol;\ntypedef vector vi;\n\nconst char nl = '\\n';\nconst ll INF = 0x3f3f3f3f;\nconst ll INFLL = 0x3f3f3f3f3f3f3f3f;\nconst ll MOD = 1e9+7;\nconst ld EPS = 1e-10;\nmt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\n\nconst int T = 99;\nconst int C = 12;\nnamespace Flow {\n\tconst int N = 50*T + 2;\n\n // data structures and helper functions common to all flow routines\n struct Edge { int v, r; ll f, c, p; };\n vector adj[N]; int sz[N]; ll mc;\n void init(int n=N) { mc=0; fill(sz,sz+n,0); fill(adj,adj+n,vector()); }\n void add_edge(int a, int b, ll c=1, ll p=0) { mc = max(mc,c);\n adj[a].push_back({b, sz[b]++, 0, c, p});\n adj[b].push_back({a, sz[a]++, 0, 0, -p}); } // change 0 to c for undirected\n\n // Minimum cost maximum flow, assuming there are no negative cost cycles\n // USAGE: 1) add edges 2) min_cost_flow(S, T, price, V, sparse?);\n // use O(V^2) Dijkstra for dense graphs, and O(E*log(V)) otherwise\n bool vis[N]; int par[N]; ll pot[N], dist[N];\n void pot_init(int n) { fill(pot, pot+n, 0);\n // if all edge costs >= 0, we don't need to run the Bellman-Ford here\n for(int i=1; i> dk; dk.push({0,s}); while(!dk.empty()) {\n int u = dk.top().second; dk.pop(); if(vis[u]) continue;\n for(const Edge& e:adj[u]) {\n if (e.f <= e.c-lb && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r;\n dk.push({-dist[e.v], e.v}); } } }\n } else { // dense\n for(int u = s; u != -1; ) { vis[u] = true; for(const Edge& e: adj[u]) {\n if (e.f <= e.c-lb && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r; } }\n u = -1; ll best = INF;\n for(int i=0; i= INF) { return 0; } ll df = INF;\n for(int u = t; u != s; ) { const Edge& r = adj[u][par[u]];\n df = min(df, adj[r.v][r.r].c - adj[r.v][r.r].f); u = r.v; }\n for(int u = t; u != s; ) { Edge& r=adj[u][par[u]], &e=adj[r.v][r.r];\n e.f += df; r.f -= df; price += df*e.p; u = r.v; }\n for(int i=0; i0;lb/=2) {\n\t\t\twhile(ll df=mcf(s, t, price, n, sparse, lb)) { flow += df; }\n\t\t} return flow; }\n}\n\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0); cout.tie(0);\n\tcout << fixed << setprecision(10);\n\n\tint n,m,k,c,d;\n\tcin>>n>>m>>k>>c>>d;\n\n\tconst int source = 0;\n\tconst int sink = n*T+1;\n\n\tfor(int i=0;i>a;\n\t\tFlow::add_edge(source, a, 1, 0);\n\t}\n\tfor(int t=0;t0) {\n\t\t\tfor(int i=1;i<=n;i++) {\n\t\t\t\tFlow::add_edge(i+(t-1)*n, i+t*n, INF, c);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i=0;i> a >> b;\n\t\tfor(int j=0;j\n#define maxn 5000010\n#define inf 1000000000\n#define p(x,k) (x+(k-1)*n)\nusing namespace std;\ntemplate inline void read(T &x)\n{\n x=0;char c=getchar();bool flag=false;\n while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}\n while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}\n if(flag)x=-x;\n}\nint n,m,k,v1,v2,s,t;\nint dis[maxn];\nbool vis[maxn];\nstruct edge\n{\n int to,nxt,v,c;\n}e[maxn];\nint head[maxn],edge_cnt=1;\nvoid add(int from,int to,int val,int cost)\n{\n\te[++edge_cnt]=(edge){to,head[from],val,cost},head[from]=edge_cnt;\n e[++edge_cnt]=(edge){from,head[to],0,-cost},head[to]=edge_cnt;\n}\nbool spfa()\n{\n queue q;\n for(int i=s;i<=t;++i) dis[i]=inf,vis[i]=false;\n q.push(s),dis[s]=0,vis[s]=true;\n while(!q.empty())\n {\n int x=q.front();\n q.pop(),vis[x]=false;\n for(int i=head[x];i;i=e[i].nxt)\n {\n int y=e[i].to,c=e[i].c;\n if(dis[y]<=dis[x]+c||!e[i].v) continue;\n dis[y]=dis[x]+c;\n if(!vis[y]) vis[y]=true,q.push(y);\n }\n }\n return dis[t]!=inf;\n}\nint dfs(int x,int lim)\n{\n if(x==t) return lim;\n vis[x]=true;\n int flow,res=lim;\n for(int i=head[x];i;i=e[i].nxt)\n {\n int y=e[i].to,v=e[i].v;\n if(dis[y]!=dis[x]+e[i].c||!v||vis[y]) continue;\n if(flow=dfs(y,min(res,v)))\n {\n res-=flow;\n e[i].v-=flow;\n e[i^1].v+=flow;\n if(!res) break;\n }\n }\n return lim-res;\n}\nint dinic()\n{\n int flow,sum=0;\n while(spfa())\n while(flow=dfs(s,inf))\n sum+=flow*dis[t];\n return sum;\n}\nint main()\n{\n read(n),read(m),read(k),read(v1),read(v2),t=p(n,110)+1;\n for(int i=2;i<=110;++i) add(p(1,i),t,inf,0);\n for(int i=2;i<=n;++i)\n for(int j=1;j<110;++j)\n add(p(i,j),p(i,j+1),inf,v1);\n for(int i=1,x;i<=k;++i) read(x),add(s,p(x,1),1,0);\n for(int i=1;i<=m;++i)\n {\n int x,y;\n read(x),read(y);\n for(int j=1;j<110;++j)\n for(int l=1;l<=k;++l)\n add(p(x,j),p(y,j+1),1,v2*(2*l-1)+v1),add(p(y,j),p(x,j+1),1,v2*(2*l-1)+v1);\n }\n printf(\"%d\",dinic());\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "dfb0c467bfbc1db482f9abb66a0ff7fb", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int TIME=150, inf=1e9, sz=1e4;\n\nint src, dst;\n\nstruct edge{\n int to, f, cap, rev, cost;\n};\nvector < edge > g[sz];\n\nvoid add_edge(int a, int b, int cap, int cost){\n edge ab = {b, 0, cap, int(g[b].size()), cost};\n edge ba = {a, 0, 0, int(g[a].size()), -cost};\n g[a].push_back(ab);\n g[b].push_back(ba);\n}\n\nint d[sz];\nstruct Cmp{\n inline bool operator()(const int &lhs, const int &rhs){\n if(d[lhs] != d[rhs]) return d[lhs] < d[rhs];\n return lhs < rhs;\n }\n};\n\n// O(n^2*m*logm) or O(m*flow*logm)\npair < int, int > mincost(int n){\n\n set < int, Cmp > q;\n int pot[n], prevedge[n], prevnode[n];\n int flow = 0, flowcost = 0, INF = 1e9;\n\n for(int a=0; a di){\n q.erase(to);\n d[to] = di;\n prevnode[to] = v;\n prevedge[to] = i;\n q.insert(to);\n }\n }\n }\n }\n if(d[dst] == INF) break;\n for(int i = 0; i < n; i++) pot[i] += d[i];\n int df = INF;\n for(int v = dst; v != src; v = prevnode[v]){\n int par = prevnode[v];\n int i = prevedge[v];\n df = min(df, g[par][i].cap - g[par][i].f);\n }\n flow += df;\n for(int v = dst; v != src; v = prevnode[v]){\n int par = prevnode[v];\n int i = prevedge[v];\n g[par][i].f += df;\n flowcost += df * g[par][i].cost;\n g[v][g[par][i].rev].f -= df;\n }\n }\n return make_pair(flow, flowcost);\n}\n\nmain()\n{\n //freopen(\"input.txt\", \"r\", stdin);\n int n, m, k, c, d;\n cin>>n>>m>>k>>c>>d;\n src=n*TIME, dst=src+1;\n for(int a=0; a>v;\n v--, add_edge(src, v*TIME, 1, 0);\n }\n for(int a=0; a>u>>v;\n u--, v--;\n for(int h=0; h+1\n \nusing namespace std;\n \n#define fore(i, s, e) for(int i = s; i < e; i++)\n#define pb push_back\n#define ALL(a) a.begin(), a.end()\n#define SZ(a) ((int)a.size())\n#define mset(a, b) memset(a, b, sizeof(a))\n#define fst first\n#define snd second\n \ntypedef long long ll;\n \ntypedef ll tf;\ntypedef ll tc;\nconst tf INF=1e9;\nstruct MCF{\n\tint n;\n\tvector prio, pot; vector curflow; vector prevedge,prevnode;\n\tpriority_queue, vector>, greater>> q;\n\tstruct edge{int to, rev; tf f, cap; tc cost;};\n\tvector> g;\n\tMCF(int n):n(n),prio(n),curflow(n),prevedge(n),prevnode(n),pot(n),g(n){}\n\tvoid add_edge(int s, int t, tf cap, tc cost) {\n // cout << s << \" \" << t << \" \" << cap << endl;\n\t\tg[s].pb((edge){t,SZ(g[t]),0,cap,cost});\n\t\tg[t].pb((edge){s,SZ(g[s])-1,0,0,-cost});\n\t}\n\tpair get_flow(int s, int t) {\n\t\ttf flow=0; tc flowcost=0;\n // cout << s << \" \" << t << endl;\n\t\twhile(1){\n\t\t\tq.push({0, s});\n\t\t\tfill(ALL(prio),INF); \n\t\t\tprio[s]=0; curflow[s]=INF;\n\t\t\twhile(!q.empty()) {\n\t\t\t\tauto cur=q.top();\n\t\t\t\ttc d=cur.fst;\n int u=cur.snd;\n\t\t\t\tq.pop();\n\t\t\t\tif(d!=prio[u]) continue;\n\t\t\t\tfor(int i=0; inprio) {\n\t\t\t\t\t\tprio[v]=nprio;\n\t\t\t\t\t\tq.push({nprio, v});\n\t\t\t\t\t\tprevnode[v]=u; prevedge[v]=i;\n\t\t\t\t\t\tcurflow[v]=min(curflow[u], e.cap-e.f);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(prio[t]==INF) break;\n\t\t\tfore(i,0,n) pot[i]+=prio[i];\n\t\t\ttf df=min(curflow[t], INF-flow);\n\t\t\tflow+=df;\n\t\t\tfor(int v=t; v!=s; v=prevnode[v]) {\n\t\t\t\tedge &e=g[prevnode[v]][prevedge[v]];\n\t\t\t\te.f+=df; g[v][e.rev].f-=df;\n\t\t\t\tflowcost+=df*e.cost;\n\t\t\t}\n\t\t}\n\t\treturn {flow,flowcost};\n\t}\n};\n\nint T = 500; // I'm supposing this works\n\nint main() {\n int n, m, k, c, d; cin >> n >> m >> k >> c >> d;\n int N = n*T;\n MCF mcf(N + 2);\n fore(i, 0, n) fore(j, 0, T - 1) {\n mcf.add_edge(i * T + j, i * T + j + 1, INF, c); // Wait a minute on this city\n }\n fore(i, 0, T) mcf.add_edge(i, N + 1, INF, 0); // Arrive to city 0 on time i\n fore(i, 0, k) {\n int x; cin >> x; x--;\n mcf.add_edge(N, x * T, 1, 0); // One person in city x at time 0\n }\n fore(i, 0, m) {\n int x, y; cin >> x >> y; x--, y--;\n fore(j, 0, T - 1) { // for each time\n int cur = 1;\n fore(_, 0, k) { // k different people can go there\n mcf.add_edge(x * T + j, y * T + j + 1, 1, cur * d + c);\n mcf.add_edge(y * T + j, x * T + j + 1, 1, cur * d + c);\n cur += 2; // squared cost\n }\n }\n }\n auto p = mcf.get_flow(N, N + 1);\n assert(p.fst == k); \n cout << p.snd << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "15c9b56736eb1b18b7f1b3d97c58c075", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n \nusing namespace std;\n \n#define fore(i, s, e) for(int i = s; i < e; i++)\n#define pb push_back\n#define ALL(a) a.begin(), a.end()\n#define SZ(a) ((int)a.size())\n#define mset(a, b) memset(a, b, sizeof(a))\n#define fst first\n#define snd second\n \ntypedef long long ll;\n \ntypedef int tf;\ntypedef int tc;\nconst tf INF=1e9;\nstruct MCF{\n\tint n;\n\tvector prio, pot; vector curflow; vector prevedge,prevnode;\n\tpriority_queue, vector>, greater>> q;\n\tstruct edge{int to, rev; tf f, cap; tc cost;};\n\tvector> g;\n\tMCF(int n):n(n),prio(n),curflow(n),prevedge(n),prevnode(n),pot(n),g(n){}\n\tvoid add_edge(int s, int t, tf cap, tc cost) {\n // cout << s << \" \" << t << \" \" << cap << endl;\n\t\tg[s].pb((edge){t,SZ(g[t]),0,cap,cost});\n\t\tg[t].pb((edge){s,SZ(g[s])-1,0,0,-cost});\n\t}\n\tpair get_flow(int s, int t) {\n\t\ttf flow=0; tc flowcost=0;\n // cout << s << \" \" << t << endl;\n\t\twhile(1){\n\t\t\tq.push({0, s});\n\t\t\tfill(ALL(prio),INF); \n\t\t\tprio[s]=0; curflow[s]=INF;\n\t\t\twhile(!q.empty()) {\n\t\t\t\tauto cur=q.top();\n\t\t\t\ttc d=cur.fst;\n int u=cur.snd;\n\t\t\t\tq.pop();\n\t\t\t\tif(d!=prio[u]) continue;\n\t\t\t\tfor(int i=0; inprio) {\n\t\t\t\t\t\tprio[v]=nprio;\n\t\t\t\t\t\tq.push({nprio, v});\n\t\t\t\t\t\tprevnode[v]=u; prevedge[v]=i;\n\t\t\t\t\t\tcurflow[v]=min(curflow[u], e.cap-e.f);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(prio[t]==INF) break;\n\t\t\tfore(i,0,n) pot[i]+=prio[i];\n\t\t\ttf df=min(curflow[t], INF-flow);\n\t\t\tflow+=df;\n\t\t\tfor(int v=t; v!=s; v=prevnode[v]) {\n\t\t\t\tedge &e=g[prevnode[v]][prevedge[v]];\n\t\t\t\te.f+=df; g[v][e.rev].f-=df;\n\t\t\t\tflowcost+=df*e.cost;\n\t\t\t}\n\t\t}\n\t\treturn {flow,flowcost};\n\t}\n};\n\nint T = 150; // I'm supposing this works\n\nint main() {\n int n, m, k, c, d; cin >> n >> m >> k >> c >> d;\n int N = n*T;\n MCF mcf(N + 2);\n fore(i, 0, n) fore(j, 0, T - 1) {\n mcf.add_edge(i * T + j, i * T + j + 1, INF, c); // Wait a minute on this city\n }\n fore(i, 0, T) mcf.add_edge(i, N + 1, INF, 0); // Arrive to city 0 on time i\n fore(i, 0, k) {\n int x; cin >> x; x--;\n mcf.add_edge(N, x * T, 1, 0); // One person in city x at time 0\n }\n fore(i, 0, m) {\n int x, y; cin >> x >> y; x--, y--;\n fore(j, 0, T - 1) { // for each time\n int cur = 1;\n fore(_, 0, k) { // k different people can go there\n mcf.add_edge(x * T + j, y * T + j + 1, 1, cur * d + c);\n mcf.add_edge(y * T + j, x * T + j + 1, 1, cur * d + c);\n cur += 2; // squared cost\n }\n }\n }\n auto p = mcf.get_flow(N, N + 1);\n assert(p.fst == k); \n cout << p.snd << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "977c32f9fc1135a5df4ff80b64b23c11", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define pa pair\n#define CLR(a,x) memset(a,x,sizeof(a))\n#define MP make_pair\n#define fi first\n#define se second\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef unsigned int ui;\ntypedef long double ld;\nconst int maxn=55,maxt=2*maxn,maxp=maxt*maxn,maxe=5*maxt*maxn*maxn;\nconst int inf=1e9;\n\ninline char gc(){\n\treturn getchar();\n\tstatic const int maxs=1<<16;static char buf[maxs],*p1=buf,*p2=buf;\n return p1==p2&&(p2=(p1=buf)+fread(buf,1,maxs,stdin),p1==p2)?EOF:*p1++;\n}\ninline ll rd(){\n ll x=0;char c=gc();bool neg=0;\n while(c<'0'||c>'9'){if(c=='-') neg=1;c=gc();}\n while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-'0',c=gc();\n return neg?(~x+1):x;\n}\n\nstruct Edge{\n\tint b,l,c,ne;\n}eg[maxe];\nint ect=1,egh[maxp];\nint N,M,K,C,D,cnt[maxn];\nint id[maxn][maxt],S=1,E=2,pct=2;\nbool flag[maxp];\nint dis[maxp],ans;\nqueue q;\n\n\ninline void adeg(int a,int b,int c,int l){\n\teg[++ect].b=b,eg[ect].ne=egh[a],egh[a]=ect;\n\teg[ect].c=c,eg[ect].l=l;\n}\n\ninline void ade(int a,int b,int c,int l){\n\tadeg(a,b,c,l),adeg(b,a,-c,0);\n}\n\ninline bool spfa(){\n\tfor(int i=1;i<=pct;i++) flag[i]=0,dis[i]=inf;\n\tdis[S]=0;q.push(S);\n\twhile(!q.empty()){\n\t\tint p=q.front();q.pop();\n\t\tflag[p]=0;\n\t\tfor(int i=egh[p];i;i=eg[i].ne){\n\t\t\tif(!eg[i].l) continue;\n\t\t\tint b=eg[i].b;\n\t\t\tif(dis[b]>dis[p]+eg[i].c){\n\t\t\t\tdis[b]=dis[p]+eg[i].c;\n\t\t\t\tif(!flag[b]) q.push(b),flag[b]=1;\n\t\t\t}\n\t\t}\n\t}\n\treturn dis[E]1) ade(id[i][j-1],id[i][j],0,inf);\n \t}\n \tif(cnt[i]) ade(S,id[i][1],0,cnt[i]);\n }\n for(int i=1;i<=M;i++){\n \tint a=rd(),b=rd();\n \tfor(int j=1;j<=K+M+1;j++){\n \t\tfor(int k=1;k<=2*K-1;k+=2){\n \t\t\tade(id[a][j],id[b][j+1],k*D,1);\n \t\t\tade(id[b][j],id[a][j+1],k*D,1);\n \t\t}\n \t}\n }\n int nn=0;\n while(spfa()){\n \tint aa=-1;\n \twhile(aa){\n \t\tCLR(flag,0);\n \t\taa=dinic(E,inf);\n \t\tnn+=aa,ans+=dis[E]*aa;\n \t}\n }\n assert(nn==K);\n\tprintf(\"%d\\n\",ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "19f0ab9b7965dbb5c7c05a882ce49f58", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int INF = 1000*1000*1000;\n\nstruct rib {\n\tint b, u, c, f;\n\tsize_t back;\n};\n\nvoid add_rib (vector < vector > & g, int a, int b, int u, int c) {\n\trib r1 = { b, u, c, 0, g[b].size() };\n\trib r2 = { a, 0, -c, 0, g[a].size() };\n\tg[a].push_back (r1);\n\tg[b].push_back (r2);\n}\n\nint main()\n{\n\tint N, m, k, c, d;\n\tscanf(\"%d %d %d %d %d\", &N, &m, &k, &c, &d);\n\tint n = N * 101 + 2;\n\tvector < vector > g (n);\n\tfor (int i = 0; i < k; i++) {\n\t\tint x;\n\t\tscanf(\"%d\", &x);\n\t\tx--;\n\t\tadd_rib(g, n - 2, x*101, 1, 0);\n\t}\n\tfor (int i = 0; i < 101; i++)\n\t{\n\t\tadd_rib(g, i, n-1, INF, i*c);\n\t}\n\tfor (int i = 1; i < N; i++) {\n\t\tfor (int j = 0; j+1 < 101; j++) {\n\t\t\tadd_rib(g, i*101 + j, i*101 + j + 1, INF, 0);\n\t\t}\n\t}\n\tfor (int i = 0; i < m; i++) {\n\t\tint u, v;\n\t\tscanf(\"%d %d\", &u, &v);\n\t\tu--;\n\t\tv--;\n\t\tfor (int j = 0; j+1 < 101; j++) {\n\t\t\tfor (int K = 1; K <= k; K++) {\n\t\t\t\tadd_rib(g, u*101 + j, v*101 + j + 1, 1, d*K*K - d*(K-1)*(K-1));\n\t\t\t\tadd_rib(g, v*101 + j, u*101 + j + 1, 1, d*K*K - d*(K-1)*(K-1));\n\t\t\t}\n\t\t}\n\t}\n\tint s, t;\n\ts = n-2;\n\tt = n-1;\n\tint flow = 0, cost = 0;\n\twhile (flow < k) {\n\t\tvector id (n, 0);\n\t\tvector d (n, INF);\n\t\tvector q (n);\n\t\tvector p (n);\n\t\tvector p_rib (n);\n\t\tint qh=0, qt=0;\n\t\tq[qt++] = s;\n\t\td[s] = 0;\n\t\twhile (qh != qt) {\n\t\t\tint v = q[qh++];\n\t\t\tid[v] = 2;\n\t\t\tif (qh == n) qh = 0;\n\t\t\tfor (size_t i=0; i\n#define fst first\n#define snd second\n#define fore(i,a,b) for(int i=a,ThxDem=b;i ii;\n\nconst int INF=1e9;\nstruct MCF {\n\tint n, m, k, t;\n\tvector u,v,prio,curflow,prevedge,prevnode,pot;\n\tstruct Edge {\n\t\tint to, f, cap, cost, rev;\n\t};\n\tvector> adj;\n\tMCF(int n):n(n),u(n),v(n),prio(n),curflow(n),prevedge(n),prevnode(n),pot(n),adj(n){}\n\tpriority_queue, greater > q;\n\tinline void addEdge(int s, int t, int cap, int cost) {\n\t\tEdge a = {t, 0, cap, cost, SZ(adj[t])};\n\t\tEdge b = {s, 0, 0, -cost, SZ(adj[s])};\n\t\tadj[s].push_back(a);\n\t\tadj[t].push_back(b);\n\t}\n\tinline pair minCostFlow(int s, int t) {\n\t\tint flow=0, flowCost=0;\n\t\twhile(flow>32, u=cur;\n\t\t\t\tq.pop();\n\t\t\t\tif(d!=prio[u])\n\t\t\t\t\tcontinue;\n\t\t\t\tfor(int i=0; inprio) {\n\t\t\t\t\t\tprio[v]=nprio;\n\t\t\t\t\t\tq.push(((ll)nprio<<32)+v);\n\t\t\t\t\t\tprevnode[v]=u;\n\t\t\t\t\t\tprevedge[v]=i;\n\t\t\t\t\t\tcurflow[v]=min(curflow[u], e.cap-e.f);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(prio[t]==INT_MAX)\n\t\t\t\tbreak;\n\t\t\tfor(int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define vi vector\n#define pii pair\n#define pb push_back\n#define mp make_pair\n#define all(x) x.begin(),x.end()\n#define SZ(x) (int)(x.size())\n#define rep(i,a,b) for(int i=a;i=a;i--)\n#define inf 1000000007\n#define mod 1000000007\n#define x first\n#define y second\n#define pi acos(-1.0)\n#define DBG(x) cerr<<(#x)<<\"=\"<inline void Max(T &a,U b){if(ainline void Min(T &a,U b){if(a>b)a=b;}\n\n//FILE* outFile;\n\ninline void add(int &a,int b){a+=b;while(a>=mod)a-=mod;}\ninline int gcd(int a, int b){if(b == 0)return a; return gcd(b, a%b);}\nint pow(int a,int b){\n int ans=1;\n while(b){\n if(b&1)ans=ans*(ll)a%mod;\n a=(ll)a*a%mod;b>>=1;\n }\n return ans;\n}\n\nconst int M = 105;\nint a[51], id[51][M];\npii b[51];\nint cnt, head[N], d[N], p[N], vis[N];\nstruct node{\n int v, c, w, next;\n}g[N<<2];\nvoid add(int a, int b, int c, int w){\n g[cnt] = {b, c, w, head[a]}, head[a] = cnt++;\n g[cnt] = {a, 0, -w, head[b]}, head[b] = cnt++;\n}\nint bfs(int s, int t){\n rep(i, 0, t+1)d[i] = inf, vis[i] = 0;\n queueq;\n q.push(s); d[s] = 0;\n while(!q.empty()){\n int u = q.front(); q.pop(); vis[u] = 0;\n for(int i = head[u]; ~i; i = g[i].next){\n int j = g[i].v;\n if(g[i].c && d[j] > d[u] + g[i].w){\n d[j] = d[u] + g[i].w;\n p[j] = i;\n if(!vis[j]){\n vis[j] = 1;\n q.push(j);\n }\n }\n }\n }\n return d[t] < inf;\n}\nint max_flow(int s, int t){\n int ans = 0, res = 0;\n while(bfs(s, t)){\n int x = t, f = inf;\n while(x != s){\n Min(f, g[p[x]].c), x = g[p[x] ^ 1].v;\n }\n ans += f * d[t];\n res += f;\n x = t;\n while(x != s){\n g[p[x]].c -= f, g[p[x] ^ 1].c += f;\n x = g[p[x] ^ 1].v;\n }\n }\n DBG(res)\n return ans;\n}\nint main(){\n int j,k,i,T,ca=0, K = 0, m, n;\n int C,D;\n scanf(\"%d%d%d%d%d\", &n, &m, &K, &C, &D);\n rep(i, 0, K)scanf(\"%d\", &a[i]), a[i]--;\n rep(i, 0, m){\n scanf(\"%d%d\", &b[i].x, &b[i].y);\n b[i].x--, b[i].y--;\n }\n int sz = 0;\n cnt = 0;\n memset(head, -1, sizeof(head));\n rep(i, 0, n)rep(j, 0, M)id[i][j] = sz++;\n int s = sz, t = s + 1;\n rep(i, 0, K){\n add(s, id[a[i]][0], 1, 0);\n }\n rep(i, 0, M)add(id[0][i], t, K, C * i);\n rep(i, 0, n){\n rep(j, 0, M-1){\n add(id[i][j], id[i][j+1], K, 0);\n }\n }\n rep(i, 0, m){\n int x = b[i].x, y = b[i].y;\n rep(j, 0, M-1){\n rep(k, 0, K){\n add(id[x][j], id[y][j+1], 1, D*(2*k + 1));\n add(id[y][j], id[x][j+1], 1, D*(2*k + 1));\n }\n }\n }\n int ans = max_flow(s, t);\n printf(\"%d\\n\", ans);\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "63137c5d14dc37bafa996a5481327f0d", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nconst int N=2000005;\nint tot,ne[N],fi[N],zz[N],sl[N],dis[N],S,T,n,m,x[N],y[N],flag[N],w[N],cur[N],k,c,d;\nqueueQ;\nvoid jb(int x,int y,int z,int v){\n\tne[++tot]=fi[x];\n\tfi[x]=tot;\n\tzz[tot]=y;\n\tsl[tot]=z;\n\tw[tot]=v;\n\tne[++tot]=fi[y];\n\tfi[y]=tot;\n\tzz[tot]=x;\n\tsl[tot]=0;\n\tw[tot]=-v;\n}\nint bfs(){\n\tQ.push(S);\n\tmemset(dis,0x3f3f3f3f,sizeof dis);\n\tdis[S]=0;\n\twhile (!Q.empty()){\n\t\tint now=Q.front();Q.pop();flag[now]=0;\n\t\tfor (int i=fi[now];i;i=ne[i])\n\t\t\tif (sl[i]&&dis[zz[i]]>dis[now]+w[i]){\n\t\t\t\tdis[zz[i]]=dis[now]+w[i];\n\t\t\t\tif (!flag[zz[i]]){\n\t\t\t\t\tflag[zz[i]]=1;\n\t\t\t\t\tQ.push(zz[i]);\n\t\t\t\t}\n\t\t\t}\n\t}\n\treturn dis[T]<=1e9;\n}\nint dfs(int x,int y){\n\tint s=0,b;\n\tflag[x]=1;\n\tif (x==T)return y;\n\tfor (int &i=cur[x];i;i=ne[i])\n\t\tif (!flag[zz[i]]&&sl[i]&&dis[zz[i]]==dis[x]+w[i]&&(b=min(y,dfs(zz[i],min(y,sl[i]))))){\n\t\t\tsl[i]-=b;\n\t\t\tsl[i^1]+=b;\n\t\t\ts+=b;y-=b;\n\t\t\tif (!y)return s;\n\t\t}\n\treturn s;\t\n}\nint gao(){\n\tint ans=0;\n\twhile (bfs()){\n\t\tfor (int i=S;i<=n+1+n*(n+k);i++)cur[i]=fi[i];\n\t\tans+=dfs(S,1e9)*dis[T];\n\t\tmemset(flag,0,sizeof flag);\n\t}\n\treturn ans;\n}\nint main(){\n\tscanf(\"%d%d%d%d%d\",&n,&m,&k,&c,&d);\n\tS=0,T=1;\n\ttot=1;\n\tfor (int i=1;i<=k;i++){\n\t\tint x;\n\t\tscanf(\"%d\",&x);\n\t\tjb(S,x+1,1,0);\n\t}\n\tfor (int i=1;i<=m;i++)scanf(\"%d%d\",&x[i],&y[i]);\n\tfor (int i=0;i<=n+k;i++){\n\t\tjb(2+i*n,T,k,0);\n\t\tif (i==n+k)break;\n\t\tfor (int j=1;j<=m;j++)\n\t\t\tfor (int l=k;l>=0;l--)jb(x[j]+1+i*n,y[j]+1+i*n+n,1,(l*2+1)*d+c),jb(y[j]+1+i*n,n+x[j]+1+i*n,1,(l*2+1)*d+c);\n\t\tfor (int j=1;j<=n;j++)jb(j+1+i*n,j+1+i*n+n,k,c);\n\t}\n\tprintf(\"%d\\n\",gao());\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "0590a0792451c32d1ee6e38e5c30294b", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n\nusing namespace std;\n\nstruct edge\n{\n\tint y, c, f, cost;\n\tedge() {};\n\tedge(int y, int c, int f, int cost) : y(y), c(c), f(f), cost(cost) {};\n};\n\nvector e;\n\nconst int N = 14043;\nvector g[N];\n\nlong long ans = 0;\nlong long d[N];\nint p[N];\nint pe[N];\nint inq[N];\n\nconst long long INF64 = (long long)(1e18);\n\nint s = N - 2;\nint t = N - 1;\n\nint rem(int x)\n{\n\treturn e[x].c - e[x].f;\n}\n\nvoid push_flow()\n{\n\tfor(int i = 0; i < N; i++) d[i] = INF64, p[i] = -1, pe[i] = -1, inq[i] = 0;\n\td[s] = 0;\n\tqueue q;\n\tq.push(s);\n\tinq[s] = 1;\n\twhile(!q.empty())\n\t{\n\t\tint k = q.front();\n\t\tq.pop();\n\t\tinq[k] = 0;\n\t\tfor(auto x : g[k])\n\t\t{\n\t\t\tif(!rem(x)) continue;\n\t\t\tint c = e[x].cost;\n\t\t\tint y = e[x].y;\n\t\t\tif(d[y] > d[k] + c)\n\t\t\t{\n\t\t\t\td[y] = d[k] + c;\n\t\t\t\tp[y] = k;\n\t\t\t\tpe[y] = x;\n\t\t\t\tif(inq[y] == 0)\n\t\t\t\t{\n\t\t\t\t\tinq[y] = 1;\n\t\t\t\t\tq.push(y);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile(cur != s)\n\t{\n\t\te[pe[cur]].f++;\n\t\te[pe[cur] ^ 1].f--;\n\t\tcur = p[cur];\n\t}\n\tans += d[t];\n}\n\nvoid add_edge(int x, int y, int c, int cost)\n{\n\tg[x].push_back(e.size());\n\te.push_back(edge(y, c, 0, cost));\n\tg[y].push_back(e.size());\n\te.push_back(edge(x, 0, 0, -cost));\n}\n\nint main()\n{\n\tint n, m, k, c, d;\n\tcin >> n >> m >> k >> c >> d;\n\tfor(int i = 0; i < k; i++)\n\t{\n\t\tint x;\n\t\tcin >> x;\n\t\tadd_edge(s, x, 1, 0);\n\t}\n\tint tt = 101;\n\tfor(int i = 0; i < tt; i++)\n\t\tadd_edge(1 + i * n, t, k, i * c);\n\tfor(int i = 0; i < m; i++)\n\t{\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\tfor(int i = 0; i < tt - 1; i++)\n\t\t\tfor(int j = 0; j < k; j++)\n\t\t\t{\n\t\t\t\tadd_edge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));\n\t\t\t\tadd_edge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));\n\t\t\t}\n \n \t}\n \tfor(int i = 0; i < n; i++)\n \t\tfor(int j = 0; j < tt - 1; j++)\n \t\t\tadd_edge(i + j * n, i + j * n + n, k, 0);\n \tfor(int i = 0; i < k; i++)\n \t\tpush_flow();\n \tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "6d21b18b7e66ee321b0f838ef4b9d794", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC target(\"arch=corei7-avx\")\n#include \n#ifndef ONLINE_JUDGE\n# include \n# include \n#endif\n\n/*** TEMPLATE CODE STARTS HERE ***/\n\n#ifndef M_PI\n#define M_PI 3.1415926535897932384626433832795028841971693993751\n#endif\n\nusing namespace std;\n\ntypedef vector vs;\ntypedef long long ll;\ntypedef complex pnt;\ntypedef vector vi;\ntypedef vector vll;\ntypedef pair pii;\ntypedef pair pll;\n\n#define RA(x) begin(x), end(x)\n#define FE(i, x) for (auto i = begin(x); i != end(x); ++i)\n#define SZ(x) ((ll) (x).size())\n\ntemplate\nvoid splitstr(const string &s, vector &out)\n{\n istringstream in(s);\n out.clear();\n copy(istream_iterator(in), istream_iterator(), back_inserter(out));\n}\n\ntemplate T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n\nstatic void redirect(int argc, const char **argv)\n{\n#ifndef ONLINE_JUDGE\n struct rlimit rlim;\n getrlimit(RLIMIT_STACK, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_STACK, &rlim);\n#ifndef __SANITIZE_ADDRESS__\n getrlimit(RLIMIT_DATA, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_DATA, &rlim);\n#endif\n#endif\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n if (argc > 1)\n {\n static filebuf f;\n f.open(argv[1], ios::in);\n cin.rdbuf(&f);\n if (!cin)\n {\n cerr << \"Failed to open '\" << argv[1] << \"'\" << endl;\n exit(1);\n }\n }\n\n if (argc > 2)\n {\n static filebuf f;\n f.open(argv[2], ios::out | ios::trunc);\n cout.rdbuf(&f);\n if (!cout)\n {\n cerr << \"Failed to open '\" << argv[2] << \"'\" << endl;\n }\n }\n cin.exceptions(ios::failbit);\n}\n\n/*** TEMPLATE CODE ENDS HERE */\n\ntemplate\nstruct SimpleNode\n{\n typedef EdgeType Edge;\n std::vector edges;\n};\n\nstruct SimpleEdge\n{\n int trg;\n\n SimpleEdge() {}\n explicit SimpleEdge(int trg) : trg(trg) {}\n};\n\ntemplate\nstruct WeightedEdge\n{\n typedef T weight_type;\n\n int trg;\n T weight;\n WeightedEdge() {}\n WeightedEdge(int trg, T weight) : trg(trg), weight(weight) {}\n};\n\ntemplate >\nclass GraphBase\n{\npublic:\n typedef EdgeType Edge;\n typedef NodeType Node;\n\n std::vector nodes;\n\n int size() const { return nodes.size(); }\n\n int size(int id) const\n {\n assert(0 <= id && id < size());\n return nodes[id].edges.size();\n }\n\n NodeType &operator[](int id)\n {\n assert(0 <= id && id < size());\n return nodes[id];\n }\n\n const NodeType &operator[](int id) const\n {\n assert(0 <= id && id < size());\n return nodes[id];\n }\n\n explicit GraphBase(std::size_t size = 0) : nodes(size) {}\n};\n\n// Unweighted graph with arbitrary node type\ntemplate >\nclass UGGraph : public GraphBase\n{\npublic:\n // Add unidirectional edge\n void add1(int a, int b)\n {\n assert(0 <= a && a < this->size());\n assert(0 <= b && b < this->size());\n this->nodes[a].edges.push_back(SimpleEdge(b));\n }\n\n // Add bidirected edge\n void add2(int a, int b)\n {\n add1(a, b);\n add1(b, a);\n }\n\n explicit UGGraph(std::size_t size = 0) : GraphBase(size) {}\n};\n\n// Unweighted graph\ntypedef UGGraph<> UGraph;\n\n// Weighted graph\ntemplate > >\nclass WGraph : public GraphBase, NodeType>\n{\npublic:\n typedef typename GraphBase, NodeType>::Edge::weight_type weight_type;\n typedef typename GraphBase, NodeType>::Edge Edge;\n typedef typename GraphBase, NodeType>::Node Node;\n\n // Add unidirectional edge\n void add1(int a, int b, T weight)\n {\n assert(0 <= a && a < this->size());\n assert(0 <= b && b < this->size());\n this->nodes[a].edges.push_back(Edge(b, weight));\n }\n\n // Add bidirectional edge\n void add2(int a, int b, T weight)\n {\n add1(a, b, weight);\n add1(b, a, weight);\n }\n\n explicit WGraph(std::size_t size = 0) : GraphBase, NodeType>(size) {}\n};\n\ntemplate\nstruct MCMFEdge\n{\n typedef T flow_type;\n\n int trg;\n T cost;\n T adjcost;\n T flow;\n T cap;\n int dual;\n\n MCMFEdge() {}\n MCMFEdge(int trg, T cost, T cap, int dual) : trg(trg), cost(cost), adjcost(cost), flow(0), cap(cap), dual(dual) {}\n};\n\ntemplate\nclass MCMFGraph : public GraphBase >\n{\nprivate:\n struct pqitem\n {\n int trg;\n T prio;\n\n pqitem() {}\n pqitem(int trg, T prio) : trg(trg), prio(prio) {}\n\n bool operator<(const pqitem &b) const\n {\n return prio > b.prio;\n }\n };\npublic:\n typedef typename MCMFEdge::flow_type flow_type;\n typedef typename GraphBase >::Edge Edge;\n typedef typename GraphBase >::Node Node;\n\n void add(int a, int b, T cost, T cap, T rcap)\n {\n assert(a >= 0 && a < this->size() && b >= 0 && b < this->size());\n assert(cap >= 0 && rcap >= 0 && cost >= 0);\n if (a == b)\n return; // no use in network flow, and complicates this function\n if (cap > 0 && rcap > 0)\n {\n add(a, b, cost, cap, 0);\n add(b, a, cost, cap, 0);\n }\n else if (rcap > 0)\n {\n add(b, a, cost, rcap, 0);\n }\n else\n {\n int da = this->size(a);\n int db = this->size(b);\n this->nodes[a].edges.push_back(Edge(b, cost, cap, db));\n this->nodes[b].edges.push_back(Edge(a, -cost, 0, da));\n }\n }\n\n // Returns change in flow and in cost\n std::pair augment(int source, int sink)\n {\n const int N = this->size();\n const T big = std::numeric_limits::max();\n\n std::vector upby(N);\n std::vector prio(N, big);\n std::vector preve(N);\n std::priority_queue q;\n\n prio[source] = 0;\n upby[source] = big;\n q.push(pqitem(source, 0));\n while (!q.empty())\n {\n int cur = q.top().trg;\n T p = q.top().prio;\n q.pop();\n if (p != prio[cur])\n continue;\n for (int i = 0; i < this->size(cur); i++)\n {\n const Edge &e = this->nodes[cur].edges[i];\n assert(e.flow == e.cap || e.adjcost >= 0);\n T p2 = p + e.adjcost;\n if (e.flow < e.cap && p2 < prio[e.trg])\n {\n prio[e.trg] = p2;\n preve[e.trg] = e.dual;\n upby[e.trg] = std::min(e.cap - e.flow, upby[cur]);\n q.push(pqitem(e.trg, p2));\n }\n }\n }\n\n T up = upby[sink];\n T upcost = 0;\n if (up > 0)\n {\n int cur = sink;\n while (cur != source)\n {\n Edge &ed = this->nodes[cur].edges[preve[cur]];\n Edge &e = this->nodes[ed.trg].edges[ed.dual];\n e.flow += up;\n upcost += e.cost * up;\n ed.flow -= up;\n cur = ed.trg;\n }\n\n for (int i = 0; i < N; i++)\n {\n for (int j = 0; j < this->size(i); j++)\n {\n Edge &e = this->nodes[i].edges[j];\n e.adjcost += prio[i] - prio[e.trg];\n }\n }\n }\n return std::make_pair(up, upcost);\n }\n\n // Returns total flow and total cost\n std::pair make_flow(int source, int sink)\n {\n for (int i = 0; i < this->size(); i++)\n for (int j = 0; j < this->size(i); j++)\n {\n Edge &e = this->nodes[i].edges[j];\n e.flow = 0;\n e.adjcost = e.cost;\n }\n\n T flow = 0;\n T cost = 0;\n std::pair up;\n while ((up = augment(source, sink)).first != 0)\n {\n flow += up.first;\n cost += up.second;\n }\n return std::make_pair(flow, cost);\n }\n\n explicit MCMFGraph(std::size_t size = 0) : GraphBase >(size) {}\n};\n\n\nint main(int argc, const char **argv)\n{\n redirect(argc, argv);\n int N, M, K, C, D;\n cin >> N >> M >> K >> C >> D;\n vi a(K);\n vi starts(N);\n for (int i = 0; i < K; i++)\n {\n cin >> a[i];\n a[i]--;\n starts[a[i]]++;\n }\n\n int maxT = K + N + 1;\n int V = (maxT + 1) * N + 2;\n int src = V - 2;\n int sink = V - 1;\n MCMFGraph g(V);\n for (int i = 0; i < N; i++)\n if (starts[i])\n g.add(src, i, 0, starts[i], 0);\n for (int t = 0; t <= maxT; t++)\n g.add(t * N, sink, 0, K, 0);\n for (int i = 0; i < N; i++)\n for (int t = 0; t < maxT; t++)\n g.add(t * N + i, (t + 1) * N + i, C, K, 0);\n for (int i = 0; i < M; i++)\n {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n for (int t = 0; t < maxT; t++)\n for (int j = 0; j < K; j++)\n {\n g.add(t * N + u, (t + 1) * N + v, C + (2 * j + 1) * D, 1, 0);\n g.add(t * N + v, (t + 1) * N + u, C + (2 * j + 1) * D, 1, 0);\n }\n }\n\n auto fc = g.make_flow(src, sink);\n assert(fc.first == K);\n cout << fc.second << '\\n';\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "b9459d5efb7aaf9397170bc40fe3e283", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)\n\nusing namespace std;\nusing ll = long long;\n\nstruct mincostflow {\n struct edge {\n int v;\n long long c;\n long long w;\n int r;\n };\n\n vector> G;\n\n mincostflow(int n) : G(n) {}\n\n void add(int u, int v, long long c, long long w) {\n int i = G[u].size();\n int j = G[v].size();\n G[u].push_back({v, c, w, j});\n G[v].push_back({u, 0, -w, i});\n }\n\n long long run(int s, int t, long long f) {\n const int N = G.size();\n long long res = 0;\n vector H(N);\n vector D(N);\n const long long INF = 1e18;\n vector PE(N), PV(N);\n while (f > 0) {\n priority_queue> q;\n fill(D.begin(), D.end(), INF);\n D[s] = 0;\n q.emplace(0, s);\n while (!q.empty()) {\n long long d = -q.top().first;\n int u = q.top().second;\n q.pop();\n if (D[u] < d) continue;\n for (int i = 0; i < G[u].size(); i++) {\n edge e = G[u][i];\n if (e.c > 0 && D[e.v] > D[u] + e.w + H[u] - H[e.v]) {\n D[e.v] = D[u] + e.w + H[u] - H[e.v];\n q.emplace(-D[e.v], e.v);\n PV[e.v] = u;\n PE[e.v] = i;\n }\n }\n }\n if (D[t] == INF) return -1;\n for (int i = 0; i < N; i++) H[i] += D[i];\n long long d = f;\n for (int i = t; i != s; i = PV[i]) {\n d = min(d, G[PV[i]][PE[i]].c);\n }\n f -= d;\n res += d * H[t];\n for (int i = t; i != s; i = PV[i]) {\n edge &e = G[PV[i]][PE[i]];\n e.c -= d;\n G[i][e.r].c += d;\n }\n }\n return res;\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n ll N, M, K, C, D;\n cin >> N >> M >> K >> C >> D;\n vector A(K);\n rep(i, K) cin >> A[i], A[i]--;\n vector us(M), vs(M);\n rep(i, M) {\n cin >> us[i] >> vs[i];\n us[i]--;\n vs[i]--;\n }\n mincostflow mcf(N*101+2);\n int s = N*101;\n int t = s + 1;\n for (int i = 0; i < K; i++) {\n mcf.add(s, A[i], 1, 0);\n }\n for (int i = 0; i <= 100; i++) {\n mcf.add(N * i, t, K, C*i);\n }\n for (int i = 0; i < 100; i++) {\n for (int j = 0; j < N; j++) {\n mcf.add(i*N+j, (i+1)*N+j, K, 0);\n }\n for (int j = 0; j < M; j++) {\n for (int k = 1; k <= K; k++) {\n mcf.add(N*i + us[j], N*(i+1) + vs[j], 1, (2*k-1)*D);\n mcf.add(N*i + vs[j], N*(i+1) + us[j], 1, (2*k-1)*D);\n }\n }\n }\n cout << mcf.run(s, t, K) << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "033f1126063aa66ae22c39c5b0fb8224", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n// #include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\ntypedef double db;\nconst int oo=2139063143;\nconst int N=5010000;\nconst int P=1000000007;\nconst db eps=1e-7;\n#define pritnf printf\n//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;\n//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)\ntemplate \ninline void sc (T &x)\n{\n x=0; static int p; p=1; static char c; c=getchar();\n while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }\n while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }\n x*=p;\n}\ntemplate \ninline void print (T x)\n{\n if (x< 0) putchar('-'),x=-x;\n if (x>=10) print(x/10);\n putchar(x%10+'0');\n}\ntemplate \ninline void pr (T x) { print(x),putchar('\\n'); }\ntemplate \nvoid chkmax (T1 &A,T2 B) { A=A>=B?A:B; }\ntemplate \nvoid chkmin (T1 &A,T2 B) { A=A<=B?A:B; }\nstruct EDGE { int v,w,c,nx; }lb[N<<1]; int tot=1,top[N];\nvoid add (int u,int v,int w,int c)\n{\n\tlb[++tot]=(EDGE){v,w, c,top[u]},top[u]=tot;\n\tlb[++tot]=(EDGE){u,0,-c,top[v]},top[v]=tot;\n\t// printf (\"%d %d %d %d\\n\",u,v,w,c);\n}\nint s,t;\nint dis[N],flow[N],pre[N],las[N]; bool in[N];\nbool spfa ()\n{\n\tfor (int i=1; i<=t; i++)\n\t\tdis[i]=1e9,flow[i]=pre[i]=las[i]=0,in[i]=0;\n\tqueue q;\n\tdis[s]=0,flow[s]=1e9,q.push (s),in[s]=1;\n\twhile (q.size ())\n\t{\n\t\tint u=q.front (); q.pop (); in[u]=0;\n\t\tfor (int kb=top[u]; kb; kb=lb[kb].nx)\n\t\t{\n\t\t\tint v=lb[kb].v,w=lb[kb].w,c=lb[kb].c;\n\t\t\tif (w&&dis[v]> dis[u]+c)\n\t\t\t{\n\t\t\t\tdis[v]=dis[u]+c;\n\t\t\t\tflow[v]=min (flow[u],w);\n\t\t\t\tpre[v]=u,las[v]=kb;\n\t\t\t\tif (!in[v]) q.push (v),in[v]=1;\n\t\t\t}\n\t\t}\n\t}\n\treturn pre[t];\n}\nint maflow,micost;\nvoid mcmf ()\n{\n\twhile (spfa ())\n\t{\n\t\tint u=t,fl=flow[t]; maflow+=fl,micost+=fl*dis[t];\n\t\twhile (u!=s)\n\t\t{\n\t\t\tlb[las[u]].w-=fl,lb[las[u]^1].w+=fl;\n\t\t\tu=pre[u];\n\t\t}\n\t}\n}\nint n,m,k,c,d;\nint main ()\n{\n // freopen (\".in\",\"r\",stdin);\n // freopen (\".out\",\"w\",stdout);\n\tsc(n),sc(m),sc(k),sc(c),sc(d);\n\tconst int B=max (n,k);\n\ts=n*(B*2+1)+1,t=s+1;\n\tfor (int i=1; i<=k; i++)\n\t{\n\t\tint x; sc(x);\n\t\tadd (s,x,1,0);\n\t}\n\tadd (n*(B*2)+1,t,1e9,0);\n\tfor (int o=0; o< B*2; o++)\n\t{\n\t\tadd (o*n+1,(o+1)*n+1,1e9,0);\n\t\tfor (int i=2; i<=n; i++)\n\t\t\tadd (o*n+i,(o+1)*n+i,1e9,c);\n\t}\n\twhile (m--)\n\t{\n\t\tint x,y; sc(x),sc(y);\n\t\tfor (int o=0; o< B*2; o++)\n\t\t{\n\t\t\tfor (int i=1; i<=k; i++)\n\t\t\t\tadd (o*n+x,(o+1)*n+y,1,d*(2*i-1)+c),add (o*n+y,(o+1)*n+x,1,d*(2*i-1)+c);\n\t\t}\n\t}\n\tmcmf ();\n\t// pr(maflow);\n\tpr(micost);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "deccd289ab4748a6f00bef733b22c496", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint n,m,r,c,d,occ[100];\npairp[100];\nnamespace MCMF{\n\tconst int N=10000,M=20000000;\n\tint head[N],cnt,dis[N],fr[N],id[N],S,T,cost;\n\tstruct node{\n\t\tint to,next,val,cost;\n\t}edge[M];\n\tvoid ae(int u,int v,int w,int c){\n\t\tedge[cnt].cost=c,edge[cnt].next=head[u],edge[cnt].to=v,edge[cnt].val=w,head[u]=cnt++;\n\t\tedge[cnt].cost=-c,edge[cnt].next=head[v],edge[cnt].to=u,edge[cnt].val=0,head[v]=cnt++;\n\t}\n\tqueueq;\n\tbool in[N];\n\tbool SPFA(){\n\t\tmemset(dis,0x3f,sizeof(dis)),dis[S]=0,q.push(S),in[S]=true;\n\t\twhile(!q.empty()){\n\t\t\tint x=q.front();q.pop(),in[x]=false;\n\t\t\tfor(int i=head[x];i!=-1;i=edge[i].next){\n\t\t\t\tif(!edge[i].val)continue;\n\t\t\t\tif(dis[edge[i].to]>dis[x]+edge[i].cost){\n\t\t\t\t\tdis[edge[i].to]=dis[x]+edge[i].cost,fr[edge[i].to]=x,id[edge[i].to]=i;\n\t\t\t\t\tif(!in[edge[i].to])in[edge[i].to]=true,q.push(edge[i].to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(dis[T]==dis[N-1])return false;\n\t\tint x=T,mn=0x3f3f3f3f;\n\t\twhile(x!=S)mn=min(mn,edge[id[x]].val),x=fr[x];\n\t\tcost+=dis[T]*mn,x=T;\n\t\twhile(x!=S)edge[id[x]].val-=mn,edge[id[x]^1].val+=mn,x=fr[x];\n\t\treturn true;\n\t}\n}\nusing namespace MCMF;\nint main(){\n\tscanf(\"%d%d%d%d%d\",&n,&m,&r,&c,&d),memset(head,-1,sizeof(head)),S=(r+n+1)*n+1,T=(r+n+1)*n+2;\n\tfor(int i=1,x;i<=r;i++)scanf(\"%d\",&x),occ[x]++;\n\tfor(int i=1;i<=m;i++)scanf(\"%d%d\",&p[i].first,&p[i].second);\n\tfor(int i=0;i\nusing namespace std;\n#define Fopen freopen(\"_in.txt\",\"r\",stdin); freopen(\"_out.txt\",\"w\",stdout);\n#define LL long long\n#define ULL unsigned LL\n#define fi first\n#define se second\n#define pb push_back\n#define lson l,m,rt<<1\n#define rson m+1,r,rt<<1|1\n#define lch(x) tr[x].son[0]\n#define rch(x) tr[x].son[1]\n#define max3(a,b,c) max(a,max(b,c))\n#define min3(a,b,c) min(a,min(b,c))\ntypedef pair pll;\nconst int inf = 0x3f3f3f3f;\nconst int _inf = 0xc0c0c0c0;\nconst LL INF = 0x3f3f3f3f3f3f3f3f;\nconst LL _INF = 0xc0c0c0c0c0c0c0c0;\nconst LL mod = (int)1e9+7;\nconst int Z = 100;\nconst int N = Z * Z;\nconst int M = Z * Z * Z;\nint px[N], py[N];\nint head[N], to[M], ct[M], w[M], nt[M];\nint d[N], vis[N];\nint pre[N], id[N];\nint s, t, tot;\n\nvoid add(int u, int v, int flow, int cost){\n to[tot] = v; ct[tot] = cost;\n w[tot] = flow; nt[tot] = head[u]; head[u] = tot++;\n\n to[tot] = u; ct[tot] = -cost;\n w[tot] = 0; nt[tot] = head[v]; head[v] = tot++;\n}\nvoid init(){\n memset(head, -1, sizeof(head));\n tot = 0;\n}\nint spfa(int s, int t){\n queue q;\n memset(d, inf, sizeof(d));\n memset(vis, 0, sizeof(vis));\n memset(pre, -1, sizeof(pre));\n d[s] = 0;\n q.push(s);\n while(!q.empty()){\n int u = q.front(); q.pop();\n vis[u] = 0;\n for(int i = head[u]; ~i; i = nt[i]){\n if(w[i] > 0 && d[to[i]] > d[u] + ct[i]){\n d[to[i]] = d[u] + ct[i];\n pre[to[i]] = u;\n id[to[i]] = i;\n if(!vis[to[i]]){\n vis[to[i]] = 1;\n q.push(to[i]);\n }\n }\n }\n\n }\n return d[t] < inf;\n}\nint MinCostFlow(int s, int t){\n int Mi = inf;\n int sum = 0;\n int tt = 0;\n while(spfa(s, t)){\n Mi = inf;\n for(int i = t; i != s; i = pre[i])\n Mi = min(Mi, w[id[i]]);\n for(int i = t; i != s; i = pre[i]){\n w[id[i]] -= Mi;\n w[id[i]^1] += Mi;\n }\n tt += Mi;\n sum += d[t] * Mi;\n }\n return sum;\n}\nint main(){\n init();\n int n, m, k, c, d;\n scanf(\"%d%d%d%d%d\", &n, &m, &k, &c, &d);\n int s = 0, t = n * 100 + 1;\n for(int i = 1; i <= 100; ++i)\n add(i, t, 100, 0);\n //第一层时间点连接汇点,流量为100,费用为0\n for(int i = 1, v; i <= k; ++i){\n scanf(\"%d\" , &v);\n add(s, (v-1)*100+1, 1, 0);\n }\n //将源点连接起始点的第一层时间点,流量为1,费用为0\n for(int i = 1; i <= n; ++i){\n for(int j = 1; j < 100; ++j){\n add((i-1)*100 + j, (i-1)*100 + j + 1, 100, c);\n }\n }\n //每一层的时间点之间连边,流量为100,费用为c\n for(int i = 1, u, v; i <= m; ++i){\n scanf(\"%d%d\", &u, &v);\n for(int j = 1; j < 100; ++j){\n int idx = (u-1) * 100 + j;\n int idy = (v-1) * 100 + j + 1;\n for(int k = 1; k <= 20; ++k){\n add(idx, idy, 1, c + (k * k - (k-1) * (k-1))* d) ;\n }\n // 假设一条边同一时间只有20人经过,那么将一条边拓展成20条边,流量为1\n //费用为(k*k-(k-1)*(k-1))\n }\n for(int j = 1; j < 100; ++j){\n int idx = (v-1) * 100 + j;\n int idy = (u-1) * 100 + j + 1;\n for(int k = 1; k <= 20; ++k){\n add(idx, idy, 1, c + (k * k - (k-1) * (k-1))* d) ;\n }\n }\n //将无向边拆成有向边\n }\n printf(\"%d\\n\", MinCostFlow(s, t));\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "6fcc5b722156e08a18da6ee446901547", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\ntypedef pairpii;\ntypedef vectorvi;\n\n#define rep(i,a,b) for(int i=(a);i<(b);i++)\n#define fi first\n#define se second\n#define de(x) cout<<#x<<\"=\"<=(a);--i)\nconst int N=2e5+5;\nconst int inf= ~0U>>1;\ntemplate\nstruct MCMF{\n static const int N = 30000, M = 7010010;\n int h[N], ing[N], pre[N], to[M], ne[M], e, s, t, n;\n U cap[M]; V dis[N], cost[M];\n void ini(int _n = N){ fill(h , h + (n=_n) , -1);e = 0;}\n void liu(int u,int v,U c,V w){ to[e] = v;ne[e] = h[u];cap[e] = c;cost[e] = w;h[u] = e++;}\n void link(int u,int v,U c,V w){ liu(u,v,c,w);liu(v,u,0,-w); }\n bool spfa(){\n queue Q;\n fill(dis, dis+n, inf);\n Q.push(s), ing[s] = 1, dis[s] = 0;\n while(!Q.empty()){\n int c = Q.front(); Q.pop(); ing[c] = 0;\n for(int k = h[c]; ~k; k = ne[k]) if (cap[k] > 0) {\n int v = to[k];\n if(dis[c] + cost[k] < dis[v]){\n dis[v] = dis[c] + cost[k];\n pre[v] = k;\n if(!ing[v]) Q.push(v) , ing[v] = 1;\n }\n }\n }\n return dis[t] != inf;\n }\n U flow; V mincost;\n pair run(int _s,int _t){\n s = _s , t = _t;\n flow = mincost = 0;\n while(spfa()){\n U pl = inf; int p, k;\n for(p = t;p != s;p = to[k^1]) pl = min(pl, cap[k = pre[p]]);\n for(p = t;p != s;p = to[k^1]) cap[k = pre[p]] -= pl, cap[k^1] += pl;\n\t\t\tmincost += pl * dis[t];\n flow += pl;\n }\n return make_pair(flow, mincost);\n }\n};\nMCMF flow;\nint n , m , k ,c , d ,a[N] , id[55][2500];\nint main(){\n\tscanf(\"%d%d%d%d%d\",&n,&m,&k,&c,&d);\n\trep(i , 1 , k + 1)\n\t\tscanf(\"%d\",&a[i]);\n\tflow.ini(50*50*4 + 10);\n\tint s = flow.n - 2,t = flow.n - 3;\n\tint cnt = 0 ,mx = 100;\n\trep(i , 1 , n + 1)\n\t\trep(j , 0 , mx + 1)\n\t\t\tid[i][j] = ++cnt;\n\tint u , v;\n\trep(i , 1 , m + 1){\n\t\tscanf(\"%d%d\",&u,&v);\n\t\trep(j , 0 , mx){\n\t\t\trep(h , 1 , k + 1){\n\t\t\t\tint tmp = h*h*d - (h-1)*(h-1)*d;\n\t\t\t\tflow.link(id[u][j] , id[v][j + 1] , 1 , c + tmp);\n\t\t\t\tflow.link(id[v][j] , id[u][j + 1] , 1 , c + tmp);\n\t\t\t}\n\t\t}\n\t}\n\trep(i , 1 , n + 1)\n\t\trep(j , 0 , mx)\n\t\t\tflow.link(id[i][j] , id[i][j + 1] , inf , c);\n\trep(i , 1 , k + 1)\n\t\tflow.link(s , id[a[i]][0] , 1 , 0);\n\trep(j , 1 , mx + 1)flow.link(id[1][j] , t , inf , 0);\n\tint ans = flow.run(s , t).se;\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "20dbe0a7dc573b6efe94567457106b27", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define pb push_back\n#define x first\n#define y second\n\nusing namespace std;\ntypedef long long INT;\ntypedef pair pii;\ntypedef vector VI;\n\n#define NN 6555\nstruct edge {\n\tint to, capacity, cost, rev;\n\tedge() {}\n\tedge(int to,int _capacity,int _cost,int _rev):\n\t\tto(to), capacity(_capacity), cost(_cost), rev(_rev) {}\n};\n\nconst int inf=0x3f3f3f3f;\n\nstruct Min_Cost_Max_Flow {\n\tint V, H[NN + 5], dis[NN +5], PreV[NN +5], PreE[NN + 5];\n\tvector G[NN + 5];\n\tvoid Init(int n) {\n\t\tV = n;\n\t\tfor (int i = 0; i <= V; ++i) G[i].clear();\n \t}\n\tvoid add_edge(int from, int to, int cap, int cost) {\n\t\tG[from].push_back(edge(to, cap, cost, G[to].size()));\n\t\tG[to].push_back(edge(from,0, -cost, G[from].size() - 1));\n\t}\n\tint minCost(int s, int t, int f) {\n\t\tint flow = 0;\n\t\tint res = 0;\n\t\tfill(H, H +1 + V, 0);\n\t\twhile (f) {\n \t\t\tpriority_queue , greater > q;\n \t\t\tfill(dis, dis + 1 + V, inf);\n \t\t\tdis[s] = 0; q.push(pii(0, s));\n\t\t\twhile (!q.empty()) {\n \t\t\t\tpii now = q.top(); q.pop();\n\t\t\t\tint v = now.second;\n\t\t\t\tif (dis[v] < now.first)continue;\n\t\t\t\tfor (int i = 0; i < G[v].size(); ++i) {\n \t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {\n \t\t\t\t\t\tdis[e.to] = dis[v] + e.cost + H[v] - H[e.to];\n \t\t\t\t\t\tPreV[e.to] = v;\n \t\t\t\t\t\tPreE[e.to] = i;\n \t\t\t\t\t\tq.push(pii(dis[e.to], e.to));\n\t \t\t\t\t}\n\t \t\t\t}\n\t \t\t}\n\t\t\tif (dis[t] == inf) break;\n\t\t\tfor (int i = 0; i <= V; ++i)H[i] += dis[i];\n\t\t\tint d = f;\n\t\t\tfor (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);\n \t\t\tf -= d; flow += d; res += d*H[t];\n\t\t\tfor (int v = t; v != s; v = PreV[v]) {\n \t\t\t\tedge& e = G[PreV[v]][PreE[v]];\n \t\t\t\te.capacity -= d;\n \t\t\t\tG[v][e.rev].capacity += d;\n\t\t\t}\n \t\t}\n\t\treturn res;\n \t}\n} mcmf;\n\nint main (){\n#ifndef ONLINE_JUDGE\n\tfreopen (\"in.in\", \"r\", stdin);\n\tfreopen (\"out.out\", \"w\", stdout);\n#endif\n\t\n\tint n, m, k, c, d;\n\t\n\tcin >> n >> m >> k >> c >> d;\n\t\n\tmcmf.Init(n*(n+55)+2);\n\tfor(int i=0; i> u;\n\t\tmcmf.add_edge(0, u, 1, 0);\n\t}\n\t\n\tvector edge=vector (m);\n\tfor(int i=0; i> edge[i].x >> edge[i].y;\n\t\n\tint tar=n*(n+55)+1;\n\tfor(int t=0; t\nusing namespace std;\n#define DEBUG 0\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\n#define fore(i,ii,iii) for(int i=ii;i g[MAXN];\nvector e;\nvoid add_edge(int u, int v, tf cap, tc cost) {\n\tif(DEBUG) {\n\t\tstring dem=to_string(u),pen=to_string(v);\n\t\tif(u==SRC)dem=\"SRC\";\n\t\tif(u==SNC)dem=\"SINC\";\n\t\tif(v==SRC)pen=\"SRC\";\n\t\tif(v==SNC)pen=\"SINC\";\n\t\tcout<<\"add edge: \"< q;q.push(s);in_queue[s]=1;\n\t\twhile(SZ(q)){\n\t\t\tint u=q.front();q.pop();in_queue[u]=0;\n\t\t\tfore(_,0,SZ(g[u])){\n\t\t\t\tint i=g[u][_];\n\t\t\t\tedge &E=e[i];\n\t\t\t\tif(E.rem()&&dist[E.v]>dist[u]+E.cost+1e-9){\n\t\t\t\t\tdist[E.v]=dist[u]+E.cost;\n\t\t\t\t\tpre[E.v]=i;\n\t\t\t\t\tcap[E.v]=min(cap[u],E.rem());\n\t\t\t\t\tif(!in_queue[E.v])q.push(E.v),in_queue[E.v]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(pre[t]<0)break;\n\t\tmxflow+=cap[t];mncost+=cap[t]*dist[t];\n\t\tfor(int v=t;v!=s;v=e[pre[v]].u){\n\t\t\te[pre[v]].flow+=cap[t];e[pre[v]^1].flow-=cap[t];\n\t\t}\n\t}\n}\nint main() {\n ios::sync_with_stdio(false);\n\tll n,m,k,c,d;\n cin>>n>>m>>k>>c>>d;\n\tint MAXT=n+k;\n\tif(DEBUG)cout<<\"MAXT: \"<a(k);\n\tfore(i,0,k){\n\t\tcin>>a[i];a[i]--;\n\t\tadd_edge(SRC,a[i]*MAXT,1,0);\n\t}\n\tfore(j,0,MAXT)add_edge(j,SNC,k,0);\n\tfore(i,0,m) {\n\t\tint x,y;cin>>x>>y;x--,y--;\n\t\tfore(j,0,MAXT-1)fore(K,1,k+1) {\n\t\t\tadd_edge(x*MAXT+j,y*MAXT+j+1,1,c+d*(2*K-1));\n\t\t}\n\t\tfore(j,0,MAXT-1)fore(K,1,k+1) {\n\t\t\tadd_edge(y*MAXT+j,x*MAXT+j+1,1,c+d*(2*K-1));\n\t\t}\n\t}\n\tfore(i,0,n) {\n\t\tfore(j,0,MAXT-1)fore(K,1,k+1) {\n\t\t\tadd_edge(i*MAXT+j,i*MAXT+j+1,1,c);\n\t\t}\n\t}\n\tflow(SRC,SNC);\n\tif(DEBUG)cout<<\"mncost: \"<\nusing namespace std;\nconst int N = 10000010, INF = 0x3f3f3f3f;\nint n, m, c, d, k, s = 0, t, ans = 0;\nint cnt = 1, hed[N], to[N], nxt[N], val[N], w[N];\nint dis[N], incf[N], pre[N]; bool inq[N];\n\nvoid add(int x, int y, int z, int v) {\n\tto[++cnt] = y, nxt[cnt] = hed[x], hed[x] = cnt, val[cnt] = z, w[cnt] = v;\n\tto[++cnt] = x, nxt[cnt] = hed[y], hed[y] = cnt, val[cnt] = 0, w[cnt] = -v;\n}\nbool spfa() {\n\tmemset(incf, 0, sizeof(incf)), memset(dis, INF, sizeof(dis));\n\tmemset(inq, 0, sizeof(inq)); queue q;\n\tq.push(s), incf[s] = INF, inq[s] = 1, dis[s] = 0;\n\tfor(; !q.empty();) {\n\t\tint u = q.front(); q.pop(); inq[u] = 0;\n\t\tfor(int i = hed[u]; i; i = nxt[i]) {\n\t\t\tif(!val[i] || dis[to[i]] <= dis[u] + w[i]) continue;\n\t\t\tdis[to[i]] = dis[u] + w[i];\n\t\t\tincf[to[i]] = min(incf[u], val[i]);\n\t\t\tpre[to[i]] = i; if(!inq[to[i]]) inq[to[i]] = 1, q.push(to[i]);\n\t\t}\n\t}\n\treturn dis[t] != INF;\n}\nvoid update() {\n\tint tmp = t;\n\tfor(; tmp != s;) {\n\t\tint i = pre[tmp];\n\t\tval[i] -= incf[t], val[i ^ 1] += incf[t], tmp = to[i ^ 1];\n\t}\n\tans += incf[t] * dis[t];\n}\nint main() {\n\tscanf(\"%d%d%d%d%d\", &n, &m, &k, &c, &d), t = (n + k - 1) * n + 1;\n\tfor(int i = 1, x; i <= k; i++) scanf(\"%d\", &x), add(s, x, 1, 0);\n\tfor(int i = 1, x, y; i <= m; i++) {\n\t\tscanf(\"%d%d\", &x, &y);\n\t\tfor(int j = 1; j <= n + k; j++)\n\t\t\tfor(int h = 1; h <= k; h++) {\n\t\t\t\tadd(x + (j - 1) * n, y + j * n, 1, d * h * 2 - d + c);\n\t\t\t\tadd(y + (j - 1) * n, x + j * n, 1, d * h * 2 - d + c);\n\t\t\t}\n\t}\n\tfor(int i = 1; i < n + k; i++) {\n\t\tfor(int j = 2; j <= n; j++)\n\t\t\tadd(j + (i - 1) * n, j + i * n, INF, c);\n\t\tadd(1 + (i - 1) * n, 1 + i * n, INF, 0);\n\t}\n\tfor(; spfa();) update();\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "fdbd43026c45adcd26d2cecae29de2cc", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define db(x) cout << (x) << '\\n';\nusing namespace std;\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pip;\ntypedef pair ppp;\ntypedef long double ld;\n#include \n#include \nusing namespace __gnu_pbds;\n\ntypedef tree,rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n\n/*\n Minimum Cost Flow (Tomizawa, Edmonds-Karp)\n Complexity:\n O(F m log n), where F is the amount of maximum flow\n Tested:\n Codeforces [http://codeforces.com/problemset/problem/717/G]\n*/\ntemplate\nstruct min_cost_max_flow\n{\n struct edge\n {\n size_t src, dst, rev;\n flow_type flow, cap;\n cost_type cost;\n };\n int n;\n vector> adj;\n min_cost_max_flow(int n) : n(n), adj(n), potential(n), dist(n), back(n) {}\n void add_edge(size_t src, size_t dst, flow_type cap, cost_type cost)\n {\n adj[src].push_back({src, dst, adj[dst].size(), 0, cap, cost});\n if (src == dst)\n adj[src].back().rev++;\n adj[dst].push_back({dst, src, adj[src].size() - 1, 0, 0, -cost});\n }\n vector potential;\n inline cost_type rcost(const edge &e)\n {\n return e.cost + potential[e.src] - potential[e.dst];\n }\n\n// void bellman_ford(int source)\n// {\n// for (int k = 0; k < n; ++k)\n// for (int u = 0; u < n; ++u)\n// for (edge &e : adj[u])\n// if (e.cap > 0 && rcost(e) < 0)\n// potential[e.dst] += rcost(e);\n// }\n const cost_type oo = numeric_limits::max();\n vector dist;\n vector back;\n cost_type dijkstra(int source, int sink)\n {\n fill(dist.begin(), dist.end(), oo);\n typedef pair node;\n priority_queue, greater> pq;\n for (pq.push({dist[source] = 0, source}); !pq.empty();)\n {\n node p = pq.top();\n pq.pop();\n if (dist[p.second] < p.first)\n continue;\n if (p.second == sink)\n break;\n for (edge &e : adj[p.second])\n if (e.flow < e.cap &&\n dist[e.dst] > dist[e.src] + rcost(e))\n {\n back[e.dst] = &e;\n pq.push({\n dist[e.dst] = dist[e.src] + rcost(e),\n e.dst\n });\n }\n }\n return dist[sink];\n }\n pair max_flow(int source, int sink)\n {\n flow_type flow = 0;\n cost_type cost = 0;\n for (int u = 0; u < n; ++u)\n for (edge &e : adj[u])\n e.flow = 0;\n potential.assign(n, 0);\n dist.assign(n, 0);\n back.assign(n, nullptr);\n// bellman_ford(source); // remove negative costs\n while (dijkstra(source, sink) < oo)\n {\n for (int u = 0; u < n; ++u)\n if (dist[u] < dist[sink])\n potential[u] += dist[u] - dist[sink];\n flow_type f = numeric_limits::max();\n for (edge * e = back[sink]; e; e = back[e->src])\n f = min(f, e->cap - e->flow);\n for (edge * e = back[sink]; e; e = back[e->src])\n e->flow += f, adj[e->dst][e->rev].flow -= f;\n flow += f;\n cost += f * (potential[sink] - potential[source]);\n }\n return {flow, cost};\n }\n};\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n\n int n,m,k,c,d,u,v;\n cin >> n >> m >> k >> c >> d;\n min_cost_max_flow g((n+2)*102);\n for(int i=0;i<102;i++)\n g.add_edge((i*(n+2))+1,i*(n+2),100,(i*c));\n for(int i=0;i<101;i++)\n for(int j=0;j> u;\n g.add_edge(n+1,u,1,0);\n }\n\n for(int i=0;i> u >> v;\n for(int j=0;j<101;j++)\n for(int f=0;f\n#include\n#include\n#include\n#define N 100010\nusing namespace std;\ntypedef int typ;\n#define M 1000010\n#define inf 1000000000\n#define winf 1000000000\nstruct road{\n int nxt,to,f;\n typ w;\n}r[M<<1];\nint head[N],cnt=1,all;\nvoid add(int u,int v,int f,typ w)\n{\n r[++cnt]=(road){head[u],v,f,w};head[u]=cnt;\n r[++cnt]=(road){head[v],u,0,-w};head[v]=cnt;\n}\nnamespace min_cost_flow{\n int cur[N];\n bool vis[N];\n struct node{\n int u;typ v;\n node(int U=0,typ V=0):u(U),v(V){}\n bool operator <(const node a)const{return v>a.v;}\n };\n priority_queueq;\n typ dis[N],tag[N];\n bool dij(int s,int t)\n {\n for(int i=1;i<=all;i++) dis[i]=winf,cur[i]=head[i];\n dis[t]=0;q.push(node(t,0));\n while(!q.empty())\n {\n int u=q.top().u;q.pop();\n if(vis[u]) continue;\n vis[u]=true;\n for(int i=head[u];i;i=r[i].nxt)\n {\n int v=r[i].to,w=tag[u]+r[i^1].w-tag[v];\n if(r[i^1].f && dis[u]+wg[N];\ninline int id(int t,int x){return t*n+x+2;}\nvoid work(int t)\n{\n all+=n;\n for(int u=1;u<=n;u++)\n for(int v:g[u])\n {\n for(int p=1;p<=k;p++)\n add(id(t,u),id(t+1,v),1,d*(2*p-1));\n }\n for(int u=1;u<=n;u++) add(id(t,u),id(t+1,u),inf,0);\n add(id(t+1,1),tt,inf,c*(t+1));\n}\nint main()\n{\n scanf(\"%d%d%d%d%d\",&n,&m,&k,&c,&d);\n all=n+2;\n for(int i=1;i<=k;i++)\n {\n int a;\n scanf(\"%d\",&a);\n add(ss,id(0,a),1,0);\n }\n for(int i=1;i<=m;i++)\n {\n int u,v;\n scanf(\"%d%d\",&u,&v);\n g[u].push_back(v);g[v].push_back(u);\n }\n add(id(0,1),tt,inf,0);\n for(int i=0;i<=n+k;i++) work(i);\n min_cost_flow::work(ss,tt);\n printf(\"%d\\n\",min_cost_flow::minw);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "5efbdda90cb83ba69f69c0ae0cafaf0a", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n#define _USE_MATH_DEFINES\n\n#include \n#include \nusing namespace __gnu_pbds;\ntemplate \nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n// DON'T USE THESE MACROS DURING ICPC PRACTICE\n#define For(i,n) for (int i=0; i=0; i--)\n#define DOWN(i,a,b) for (int i=b; i>=a; i--)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef complex pt;\ntypedef vector pol;\ntypedef vector vi;\n\nconst char nl = '\\n';\nconst ll INF = 0x3f3f3f3f;\nconst ll INFLL = 0x3f3f3f3f3f3f3f3f;\nconst ll MOD = 1e9+7;\nconst ld EPS = 1e-10;\nmt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\n\nconst int T = 99;\nconst int C = 12;\nnamespace Flow {\n\tconst int N = 50*T + 2;\n\n // data structures and helper functions common to all flow routines\n struct Edge { int v, r; ll f, c, p; };\n vector adj[N]; int sz[N]; ll mc;\n void init(int n=N) { mc=0; fill(sz,sz+n,0); fill(adj,adj+n,vector()); }\n void add_edge(int a, int b, ll c=1, ll p=0) { mc = max(mc,c);\n adj[a].push_back({b, sz[b]++, 0, c, p});\n adj[b].push_back({a, sz[a]++, 0, 0, -p}); } // change 0 to c for undirected\n\n // Minimum cost maximum flow, assuming there are no negative cost cycles\n // USAGE: 1) add edges 2) min_cost_flow(S, T, price, V, sparse?);\n // use O(V^2) Dijkstra for dense graphs, and O(E*log(V)) otherwise\n bool vis[N]; int par[N]; ll pot[N], dist[N];\n void pot_init(int n) { fill(pot, pot+n, 0);\n // if all edge costs >= 0, we don't need to run the Bellman-Ford here\n for(int i=1; i dk; dk.push(s); while(!dk.empty()) {\n int u = dk.front(); dk.pop(); if(vis[u]) continue;\n for(const Edge& e:adj[u]) {\n if (e.f < e.c && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r;\n dk.push(e.v); } } }\n } else { // dense\n for(int u = s; u != -1; ) { vis[u] = true; for(const Edge& e: adj[u]) {\n if (e.f < e.c && dist[e.v] > dist[u] + pot[u] - pot[e.v] + e.p) {\n dist[e.v] = dist[u] + pot[u] - pot[e.v] + e.p; par[e.v] = e.r; } }\n u = -1; ll best = INF;\n for(int i=0; i= INF) { return 0; } ll df = INF;\n for(int u = t; u != s; ) { const Edge& r = adj[u][par[u]];\n df = min(df, adj[r.v][r.r].c - adj[r.v][r.r].f); u = r.v; }\n for(int u = t; u != s; ) { Edge& r=adj[u][par[u]], &e=adj[r.v][r.r];\n e.f += df; r.f -= df; price += df*e.p; u = r.v; }\n for(int i=0; i>n>>m>>k>>c>>d;\n\n\tconst int source = 0;\n\tconst int sink = n*T+1;\n\n\tfor(int i=0;i>a;\n\t\tFlow::add_edge(source, a, 1, 0);\n\t}\n\tfor(int t=0;t0) {\n\t\t\tfor(int i=1;i<=n;i++) {\n\t\t\t\tFlow::add_edge(i+(t-1)*n, i+t*n, INF, c);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i=0;i> a >> b;\n\t\tfor(int j=0;j\n\nusing namespace std;\n\ntypedef long long ll;\n//typedef long double ld;\n\nconst int maxn=5007;\n\n//const ll INF = 1e18;\nconst ll INF = 1e9;\nconst ll mod = 1e9+7;\n\nstruct Edge {\n int from,to;\n ll cap, cost, flow;\n Edge(int from, int to, ll cap, ll cost): from(from), to(to), cap(cap), cost(cost), flow(0) {};\n};\n\nvector edges;\nll d[maxn], phi[maxn];\nvector g[maxn];\nint par[maxn];\n\nvoid add_edge(int u, int v, ll cap, ll cost) {\n g[u].push_back(edges.size());\n edges.push_back({u,v,cap,cost});\n g[v].push_back(edges.size());\n edges.push_back({v,u,0,-cost});\n}\nvoid ford_bellman(int s, int n) {\n for (int i=0;i> q;\n q.insert({0,s});\n while (!q.empty()) {\n int v = q.begin()->second;\n q.erase(q.begin());\n for (int i=0;i mincost_maxflow(int s, int t) {\n ford_bellman(s, 50);\n dijkstra(s);\n ll flow = 0, cost = 0;\n while (1) {\n if (d[t]==INF) break;\n ll pushed= INF, sum = 0;\n for (int tmp = t;tmp!=s;) {\n// if (tmp==100) {\n// cout << \"xyu\";\n// }\n int eid = par[tmp];\n pushed = min(pushed, edges[eid].cap-edges[eid].flow);\n sum+=edges[eid].cost;\n tmp = edges[eid].from;\n }\n flow+=pushed;\n cost+=pushed*sum;\n //cout << \"flow: \" << flow << \", cost: \" << cost << endl << t << \" \";\n for (int tmp=t;tmp!=s;) {\n int eid = par[tmp];\n edges[eid].flow+=pushed;\n edges[eid^1].flow-=pushed;\n tmp = edges[eid].from;\n // cout << tmp << \" \";\n }\n //cout << endl;\n\n dijkstra(s);\n for (int i=0;i> n >> m >> k >> c >> d;\n vector cnts(n,0);\n for (int i=0;i> ai;\n ai--;\n cnts[ai]++;\n }\n for (int i=0;i> u >> v;\n u--; v--;\n for (int i=0;i<99;i++) {\n ll cost = d;\n for (int j=0;j<50;j++) {\n add_edge(u+i*50, v+(i+1)*50, 1, cost);\n add_edge(v+i*50, u+(i+1)*50, 1, cost);\n cost+=2*d;\n }\n }\n }\n for (int i=0;i<=99;i++) {\n add_edge(0+i*50, 5001, INF, c*i);\n }\n\n for (int i=0;i> t;\n// for (int i=0;i\nusing namespace std;\n\nconst int MAXN = 10000;\nconst int MAXM = 2000000;\nconst int INF = 0x3f3f3f3f;\n\nint n, m, k, c, d;\n\nstruct Edge\n{\n int to, next, cap, flow, cost;\n} edge[MAXM];\nint head[MAXN], tol;\nint pre[MAXN], dis[MAXN];\nbool vis[MAXN];\nint N; //节点总个数,节点编号从0~N-1\nvoid init(int n)\n{\n N = n;\n tol = 0;\n memset(head, -1, sizeof(head));\n}\nvoid addedge(int u, int v, int cap, int cost)\n{\n edge[tol].to = v;\n edge[tol].cap = cap;\n edge[tol].cost = cost;\n edge[tol].flow = 0;\n edge[tol].next = head[u];\n head[u] = tol++;\n edge[tol].to = u;\n edge[tol].cap = 0;\n edge[tol].cost = -cost;\n edge[tol].flow = 0;\n edge[tol].next = head[v];\n head[v] = tol++;\n}\nbool spfa(int s, int t)\n{\n queue q;\n for (int i = 0; i < N; i++)\n {\n dis[i] = INF;\n vis[i] = false;\n pre[i] = -1;\n }\n dis[s] = 0;\n vis[s] = true;\n q.push(s);\n while (!q.empty())\n {\n int u = q.front();\n q.pop();\n vis[u] = false;\n for (int i = head[u]; i != -1; i = edge[i].next)\n {\n int v = edge[i].to;\n\n if (edge[i].cap > edge[i].flow &&\n dis[v] > dis[u] + edge[i].cost)\n {\n dis[v] = dis[u] + edge[i].cost;\n pre[v] = i;\n if (!vis[v])\n {\n vis[v] = true;\n q.push(v);\n }\n }\n }\n }\n if (pre[t] == -1)\n return false;\n else\n return true;\n}\n//返回的是最大流,cost存的是最小费用\nint minCostMaxflow(int s, int t, int &cost)\n{\n int flow = 0;\n cost = 0;\n while (spfa(s, t))\n {\n int Min = INF;\n for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])\n {\n if (Min > edge[i].cap - edge[i].flow)\n Min = edge[i].cap - edge[i].flow;\n }\n for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])\n {\n edge[i].flow += Min;\n edge[i ^ 1].flow -= Min;\n cost += edge[i].cost * Min;\n }\n flow += Min;\n }\n return flow;\n}\n\nint main()\n{\n cin >> n >> m >> k >> c >> d;\n init(MAXN);\n for(int i = 0; i < k; i++)\n {\n int x;\n cin >> x;\n addedge(0, x, 1, 0);\n }\n for(int i = 0; i < 100; i++)\n {\n for(int j = 0; j < k; j++)\n addedge(1+i*n, 9999, 1, i*c);\n }\n for(int i = 0; i < m; i++)\n {\n int u, v;\n cin >> u >> v;\n for(int j = 1; j < 100; j++)\n {\n for(int gg = 1; gg <= k; gg++)\n {\n addedge(u+(j-1)*n, v+j*n, 1, (2*gg-1)*d);\n addedge(v+(j-1)*n, u+j*n, 1, (2*gg-1)*d);\n }\n }\n }\n for(int i = 1; i <= n; i++)\n {\n for(int j = 1; j < 100; j++)\n {\n addedge(i+(j-1)*n, i+j*n, k, 0);\n }\n }\n \n int cost;\n minCostMaxflow(0, 9999, cost);\n cout << cost << endl;\n}\n\n/*\n3 2 4 2 3\n3 3 3 3\n1 2\n2 3\n */", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "32b7d84042f170a8e84f1f3ef392b64f", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/* upsolve with Dukkha */\n#include \n#include \n\n#define N\t50\n#define M\t50\n#define K\t50\n#define T\t(N + K + 1)\n#define N_\t(N * T + 1)\n#define M_\t(K + (M * 2 * K + N) * (T - 1))\n#define INF\t0x3f3f3f3f\n\n#define LT(u, v)\t(pi[u] < pi[v] || pi[u] == pi[v] && kk[u] < kk[v])\n\nint pi[N_], kk[N_], bb[N_];\nint pq[1 + N_], iq[N_], cnt;\n\nint i2(int i) {\n\treturn (i *= 2) > cnt ? 0 : i < cnt && LT(pq[i + 1], pq[i]) ? i + 1 : i;\n}\n\nvoid pq_up(int u) {\n\tint i, j;\n\n\tfor (i = iq[u]; (j = i / 2) && LT(u, pq[j]); i = j)\n\t\tpq[iq[pq[j]] = i] = pq[j];\n\tpq[iq[u] = i] = u;\n}\n\nvoid pq_dn(int u) {\n\tint i, j;\n\n\tfor (i = iq[u]; (j = i2(i)) && LT(pq[j], u); i = j)\n\t\tpq[iq[pq[j]] = i] = pq[j];\n\tpq[iq[u] = i] = u;\n}\n\nvoid pq_add(int u) {\n\tiq[u] = ++cnt;\n\tpq_up(u);\n}\n\nint pq_remove_first() {\n\tint u = pq[1], v = pq[cnt--];\n\n\tiq[v] = 1;\n\tpq_dn(v);\n\treturn u;\n}\n\nstruct L {\n\tstruct L *next;\n\tint j;\n} aa[N], aa_[N_];\n\nvoid add(int i, int j) {\n\tstatic struct L l91[M * 2], *l = l91;\n\n\tl->j = j;\n\tl->next = aa[i].next; aa[i].next = l++;\n}\n\nvoid add_(int i, int h) {\n\tstatic struct L l91[M_ * 2], *l = l91;\n\n\tl->j = h;\n\tl->next = aa_[i].next; aa_[i].next = l++;\n}\n\nint n_, m_;\nint uu[M_], vv[M_], uv[M_], cost[M_], cost_[M_];\nint cc[M_ * 2];\n\nvoid link(int u, int v, int cap, int cos) {\n\tint h = m_++;\n\n\tuu[h] = u, vv[h] = v, uv[h] = u ^ v;\n\tcost[h] = cos;\n\tcc[h << 1 ^ 0] = cap;\n\tadd_(u, h << 1 ^ 0);\n\tadd_(v, h << 1 ^ 1);\n}\n\nvoid dijkstra(int s) {\n\tmemset(pi, 0x3f, n_ * sizeof *pi);\n\tpi[s] = 0;\n\tpq_add(s);\n\twhile (cnt) {\n\t\tstruct L *l;\n\t\tint u = pq_remove_first(), v;\n\t\tint h, h_, p, k = kk[u] + 1;\n\n\t\tfor (l = aa_[u].next; l; l = l->next)\n\t\t\tif (cc[h_ = l->j]) {\n\t\t\t\th = h_ >> 1;\n\t\t\t\tp = pi[u] + ((h_ & 1) == 0 ? cost_[h] : -cost_[h]);\n\t\t\t\tv = u ^ uv[h];\n\t\t\t\tif (pi[v] == INF) {\n\t\t\t\t\tpi[v] = p;\n\t\t\t\t\tkk[v] = k;\n\t\t\t\t\tbb[v] = h_;\n\t\t\t\t\tpq_add(v);\n\t\t\t\t} else if (pi[v] > p || pi[v] == p && kk[v] > k) {\n\t\t\t\t\tpi[v] = p;\n\t\t\t\t\tkk[v] = k;\n\t\t\t\t\tbb[v] = h_;\n\t\t\t\t\tpq_up(v);\n\t\t\t\t}\n\t\t\t}\n\t}\n}\n\nvoid push(int s, int t) {\n#if 0\n\tint u, h, h_, c = INF;\n\n\tfor (u = t; u != s; u ^= uv[h]) {\n\t\th = (h_ = bb[u]) >> 1;\n\t\tif (c > cc[h_])\n\t\t\tc = cc[h_];\n\t}\n\tfor (u = t; u != s; u ^= uv[h]) {\n\t\th = (h_ = bb[u]) >> 1;\n\t\tcc[h_] -= c, cc[h_ ^ 1] += c;\n\t}\n#else\n\tint u, h, h_;\n\n\tfor (u = t; u != s; u ^= uv[h]) {\n\t\th = (h_ = bb[u]) >> 1;\n\t\tcc[h_]--, cc[h_ ^ 1]++;\n\t}\n#endif\n}\n\nint edmonds_karp(int s, int t) {\n\tint c, h;\n\n\tmemcpy(cost_, cost, m_ * sizeof *cost);\n\twhile (1) {\n\t\tdijkstra(s);\n\t\tif (pi[t] == INF)\n\t\t\tbreak;\n\t\tpush(s, t);\n\t\tfor (h = 0; h < m_; h++) {\n\t\t\tint u = uu[h], v = vv[h];\n\n\t\t\tif (pi[u] != INF && pi[v] != INF)\n\t\t\t\tcost_[h] += pi[u] - pi[v];\n\t\t}\n\t}\n\tc = 0;\n\tfor (h = 0; h < m_; h++)\n\t\tc += cost[h] * cc[h << 1 ^ 1];\n\treturn c;\n}\n\nint main() {\n\tstatic int ii[K];\n\tint n, m, k, t, c, d, h, i, j, s, u, v, x;\n\n\tscanf(\"%d%d%d%d%d\", &n, &m, &k, &c, &d);\n\tfor (h = 0; h < k; h++)\n\t\tscanf(\"%d\", &ii[h]), ii[h]--;\n\twhile (m--) {\n\t\tscanf(\"%d%d\", &i, &j), i--, j--;\n\t\tadd(i, j), add(j, i);\n\t}\n\tt = n + k + 1;\n\tn_ = n * t + 1;\n\tm_ = 0;\n\tfor (i = 0; i < n; i++)\n\t\tfor (s = 0; s < t - 1; s++) {\n\t\t\tstruct L *l;\n\n\t\t\tu = i * t + s;\n\t\t\tfor (l = aa[i].next; l; l = l->next) {\n\t\t\t\tj = l->j;\n\t\t\t\tv = j * t + s + 1;\n\t\t\t\tfor (x = 1; x <= k; x++)\n\t\t\t\t\tlink(u, v, 1, c + (x * 2 - 1) * d);\n\t\t\t}\n\t\t}\n\tfor (i = 0; i < n; i++)\n\t\tfor (s = 0; s < t - 1; s++) {\n\t\t\tu = i * t + s, v = u + 1;\n\t\t\tlink(u, v, k, i == 0 ? 0 : c);\n\t\t}\n\tfor (h = 0; h < k; h++)\n\t\tlink(n_ - 1, ii[h] * t + 0, 1, 0);\n\tprintf(\"%d\\n\", edmonds_karp(n_ - 1, 0 * t + t - 1));\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "011f4bee403d9a666f768bfd83e4ea80", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n//\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int maxm = 2000015;\nconst int maxn = 105*55;\nint n, m, c, d, k;\ninline int read()\n{\n int x=0,f=1;char ch=getchar();\n while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}\n while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}\n return x*f;\n}\nstruct Edge\n{\n int to, next, cap, flow, cost;\n}edge[maxm];\nint head[maxn], tol;\nint pre[maxn], dis[maxn];\nbool vis[maxn];\nint N ;\nvoid init(int n)\n{\n N = n;\n tol = 0;\n memset(head, -1, sizeof(head));\n}\nvoid addedge(int u, int v, int cap, int cost)\n{\n edge[tol].to = v;\n edge[tol].cap = cap;\n edge[tol].cost = cost;\n edge[tol].flow = 0;\n edge[tol].next = head[u];\n head[u] = tol++;\n edge[tol].to = u;\n edge[tol].cap = 0;\n edge[tol].cost = -cost;\n edge[tol].flow = 0;\n edge[tol].next = head[v];\n head[v] = tol++;\n}\nbool spfa(int s, int t)\n{\n queueq;\n for(int i = 0; i < N; i++)\n {\n dis[i] = INF;\n vis[i] = false;\n pre[i] = -1;\n }\n dis[s] = 0;\n vis[s] = true;\n q.push(s);\n while(!q.empty())\n {\n int u = q.front();\n q.pop();\n vis[u] = false;\n for(int i = head[u]; i != -1; i = edge[i].next)\n {\n int v = edge[i].to;\n if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost)\n {\n dis[v] = dis[u] + edge[i].cost;\n pre[v] = i;\n if(!vis[v])\n {\n vis[v] = true;\n q.push(v);\n }\n }\n }\n }\n if(pre[t] == -1) return false;\n else return true;\n}\nint minCostMaxflow(int s, int t, int &cost)\n{\n int flow = 0;\n cost = 0;\n while(spfa(s,t))\n {\n int Min = INF;\n for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])\n {\n if(Min > edge[i].cap - edge[i].flow)\n Min = edge[i].cap - edge[i].flow;\n }\n for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])\n {\n edge[i].flow += Min;\n edge[i^1].flow -= Min;\n cost += edge[i].cost*Min;\n }\n flow += Min;\n }\n return flow;\n}\nint point(int x, int y){\n return (x-1)*50+y;\n}\nint main(){\n int S = 101*55, T = S + 1;\n n = read(), m = read(), k = read(), c = read(), d = read();\n int tmp;\n init(T+1);\n for(int i = 1; i <= k; i++){\n tmp = read();\n addedge(S, tmp, 1, 0);\n }\n for(int i = 1; i <= 50; i++){\n for(int j = 1; j <= 103; j++){\n int x = point(j, i);\n int y = point(j+1, i);\n addedge(x, y, INF, c);\n }\n }\n for(int i = 0; i < 102; i++){\n int tmp = i*50+1;\n addedge(tmp, T, INF, 0);\n }\n for(int i = 1; i <= m; i++){\n int u, v;\n u = read(), v = read();\n for(int j = 1; j <= 102; j++){\n int x = point(j, u), x1 = point(j+1, u);\n int y1 = point(j+1, v), y = point(j, v);\n for(int k = 0; k < 50; k++){\n int val = d*(k*2+1)+c;\n addedge(x, y1, 1, val);\n addedge(y, x1, 1, val);\n }\n }\n }\n int cost = 0;\n minCostMaxflow(S, T, cost);\n cout << cost << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "929ba72f49fec6f54592b9b8a7fc7d37", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)\n#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)\n#define ll long long\n#define db double\n#define pii pair\n#define mkp make_pair\n#define se second\n#define fi first\nusing namespace std;\nconst int N = 5005;\nconst int M = 4 * N * sqrt(N);\nconst int inf = 1e9;\nstruct cf { int c, f; };\nint n, m, k, c, d, s, t, tot, pre[N];\nint head[N], edge_id = 1;\nstruct node {\n\tint to, next, val, w;\n} e[M << 1];\nvoid add_edge(int u, int v, int val, int w) {\n\t++edge_id, e[edge_id].next = head[u], e[edge_id].w = w, e[edge_id].to = v, e[edge_id].val = val, head[u] = edge_id;\n}\nvoid add(int u, int v, int val, int w) {\n\tadd_edge(u, v, val, w), add_edge(v, u, 0, -w);\n}\nint dis[N]; bool vis[N];\nbool SPFA() {\n\tfill(dis + 1, dis + tot + 1, inf);\n\tqueue q;\n\tq.push(s), dis[s] = 0;\n\twhile(!q.empty()) {\n\t\tint u = q.front();\n\t\tvis[u] = 0;\n\t\tq.pop();\n\t\tfor(int i = head[u]; i; i = e[i].next) {\n\t\t\tint v = e[i].to;\n\t\t\tif(e[i].val && dis[v] > dis[u] + e[i].w) {\n\t\t\t\tdis[v] = dis[u] + e[i].w, pre[v] = i;\n\t\t\t\tif(!vis[v]) vis[v] = 1, q.push(v);\n\t\t\t}\n\t\t}\n\t}\n\treturn dis[t] < inf;\n}\ncf mcmf() {\n\tcf res = {};\n\twhile(SPFA()) {\n\t\tint flow = inf;\n\t\tfor(int x = t; x != s; x = e[pre[x] ^ 1].to) flow = min(flow, e[pre[x]].val);\n\t\tfor(int x = t; x != s; x = e[pre[x] ^ 1].to) e[pre[x]].val -= flow, e[pre[x] ^ 1].val += flow;\n\t\tres.f += flow, res.c += dis[t] * flow;\n\t}\n\treturn res;\n}\n#define P(i, j) ((i) * n + (j))\nint E[N][2];\nint main() {\n\tscanf(\"%d%d%d%d%d\", &n, &m, &k, &c, &d);\n\tint cs = 2 * k; tot = cs * n; \n\ts = ++tot, t = ++tot;\n\tint tp; L(i, 1, k) scanf(\"%d\", &tp), add(s, P(0, tp), 1, 0);\n\tL(i, 1, m) scanf(\"%d%d\", &E[i][0], &E[i][1]);\n\tL(i, 1, cs - 1) {\n\t\tadd(P(i, 1), t, k, c * i);\n\t\tL(j, 1, n) add(P(i - 1, j), P(i, j), k, 0);\n\t\tL(j, 1, m) L(t, 1, k) \n\t\t\tadd(P(i - 1, E[j][0]), P(i, E[j][1]), 1, (2 * t - 1) * d), \n\t\t\tadd(P(i - 1, E[j][1]), P(i, E[j][0]), 1, (2 * t - 1) * d);\n\t} \n\tprintf(\"%d\\n\", mcmf().c);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "c66aef51f2fb2f0687643f2f2740f7d8", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair;\nusing pll = pair;\n\nconst int inf = 1e9;\nint n, m, k, c, d;\nconst int N = 4 * 55 * 105 * 105;\n\nint mn[55 * 105][55 * 105];\nint id[55 * 105][55 * 105];\nint cnt[55 * 105];\nstruct MCMF {\n int n, m;\n void init(int _N) {\n n = _N;\n m = 0;\n }\n int S[N], E[N], F[N], C[N];\n void add_edge(int s, int e, int c) {\n S[m] = s;\n E[m] = e;\n F[m] = 0;\n C[m] = c;\n m++;\n }\n int solve(int st, int ed) {\n int ans = 0;\n for (int it = 1; it <= k; it++) {\n vector> edge(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n mn[i][j] = inf;\n }\n }\n for (int i = 0; i < m; i++) {\n if (F[i]) {\n mn[E[i]][S[i]] = min(mn[E[i]][S[i]], -C[i]);\n if (mn[E[i]][S[i]] == -C[i]) id[E[i]][S[i]] = ~i;\n edge[E[i]].push_back(S[i]);\n }\n else {\n mn[S[i]][E[i]] = min(mn[S[i]][E[i]], C[i]);\n if (mn[S[i]][E[i]] == C[i]) id[S[i]][E[i]] = i;\n edge[S[i]].push_back(E[i]);\n }\n }\n for (int i = 0; i < n; i++) {\n vector nw;\n for (int j : edge[i]) {\n if (cnt[j]++ == 0) nw.push_back(j);\n }\n edge[i] = nw;\n for (int j : edge[i]) {\n cnt[j] = 0;\n }\n }\n vector D(n, inf);\n vector P(n, 0);\n D[st] = 0;\n int loop = 1;\n while (loop) {\n loop = 0;\n for (int i = 0; i < n; i++) {\n for (int j : edge[i]) {\n if (mn[i][j] < inf && D[i] + mn[i][j] < D[j]) {\n D[j] = D[i] + mn[i][j];\n P[j] = i;\n loop = 1;\n }\n }\n }\n }\n fflush(stdout);\n ans += D[ed];\n for (int x = ed; x != st; x = P[x]) {\n int p = P[x];\n if (id[p][x] < 0) {\n int idx = ~id[p][x];\n F[idx] = 0;\n }\n else {\n int idx = id[p][x];\n F[idx] = 1;\n }\n }\n }\n return ans;\n }\n} F;\n\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m >> k >> c >> d;\n F.init(105 * n + 5);\n int ed = 105 * n + 4;\n for (int i = 0; i < k; i++) {\n int a;\n cin >> a;\n F.add_edge(0, a, 0);\n }\n for (int i = 0; i < 104; i++) {\n for (int j = 0; j < 105; j++)\n F.add_edge(1 + i * n, ed, 0);\n }\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j < 104; j++) {\n for (int k = 0; k < 105; k++) {\n F.add_edge(i + (j - 1) * n, i + j * n, c);\n }\n }\n }\n for (int i = 0; i < m; i++) {\n int x, y;\n cin >> x >> y;\n for (int j = 1; j < 104; j++) {\n for (int k = 0; k < 105; k++) {\n F.add_edge(x + (j - 1) * n, y + j * n, c + (2 * k + 1) * d);\n F.add_edge(y + (j - 1) * n, x + j * n, c + (2 * k + 1) * d);\n }\n }\n }\n cout << F.solve(0, ed);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "0c3901f6461c9e0daad6b06e2e0e346a", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "// luogu-judger-enable-o2\n#include\nusing namespace std;\n#define pii pair\n#define mp make_pair\n#define inf 100000000\n#define nn 6000\nnamespace mcmf{\n\tvector v[nn],rev[nn],cost[nn],cap[nn];\n\tvoid add_edge(int x,int y,int c,int w)\n\t{\n\t\trev[x].push_back(v[y].size());rev[y].push_back(v[x].size());\n\t\tv[x].push_back(y);v[y].push_back(x);\n\t\tcost[x].push_back(w);cost[y].push_back(-w);\n\t\tcap[x].push_back(c);cap[y].push_back(0);\n\t}\n\t\n\tint dist[nn];int vis[nn];\n\tint prex[nn],prei[nn];\n\tvoid spfa(int s)\n\t{\n\t\tmemset(dist,0x3f,sizeof dist);\n\t\tmemset(vis,0,sizeof vis);prex[s]=-1;\n\t\tqueue q;q.push(s);dist[s]=0;vis[s]=1;\n\t\t\n\t\twhile(q.size())\n\t\t{\n\t\t\tint x=q.front();q.pop();vis[x]=0;\n\t\t\tfor(int i=0;icc and cap[x][i])\n\t\t\t\t{\n\t\t\t\t\tdist[y]=cc;prex[y]=x,prei[y]=i;\n\t\t\t\t\tif(!vis[y]) vis[y]=1,q.push(y);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\t\n\tint tot=0;\n\tint dfs(int x,int flow)\n\t{\n\t\tif(prex[x]==-1) return flow;\n\t\tint y=x;x=prex[y];int i=prei[y],j=rev[x][i];\n\t\tint f=dfs(x,min(flow,cap[x][i]));\n\t\tcap[x][i]-=f,cap[y][j]+=f;\n\t\treturn f;\n\t}\n\t\n\tpii solve(int s,int t)\n\t{\n\t\tint ans=0;tot=0;\n\t\twhile(1)\n\t\t{\n\t\t\tspfa(s);\n\t\t\tif(dist[t]>1e9) break;\n\t\t\tint f=dfs(t,inf);\n\t\t\ttot+=f*dist[t];\n\t\t\tans+=f;\n\t\t}\n\t\treturn mp(ans,tot);\n\t}\n\t\n};\n\n\nint n,m,s=nn-1,t=nn-2;\nint k,c,d;\nint get(int x,int tim){\n\treturn tim*52-52+x;\n}\nint main() \n{\n\tscanf(\"%d%d%d%d%d\",&n,&m,&k,&c,&d);int kk=k;\n\twhile(k--){\n\t\tint x;scanf(\"%d\",&x);\n\t\tmcmf::add_edge(s,get(x,1),1,0);\n\t}k=kk;\n\twhile(m--){\n\t\tint x,y;scanf(\"%d%d\",&x,&y);\n\t\tfor(int i=1;i<100;i++)\n\t\t\tfor(int j=1;j<=k;j++) mcmf::add_edge(get(x,i),get(y,i+1),1,d*(j+j-1));\n\t\t\n\t\tswap(x,y);\n\t\tfor(int i=1;i<100;i++)\n\t\t\tfor(int j=1;j<=k;j++) mcmf::add_edge(get(x,i),get(y,i+1),1,d*(j+j-1));\n\t}\n\tfor(int i=1;i<100;i++){\n\t\tfor(int j=1;j<=n;j++) mcmf::add_edge(get(j,i),get(j,i+1),inf,0);\n\t\tmcmf::add_edge(get(1,i),t,inf,c*(i-1));\n\t}\n\t\n\tprintf(\"%d\",mcmf::solve(s,t).second);\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "9fa8c55b1d483d1857ed9121d9763bc6", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "\n#include\nusing namespace std;\n\nconst int maxn = 5005;\ntypedef int T;\nconst T INF = 1e9+7;\nconst int K = 100;\n\nstruct Edge {\n int u , v ;\n T cap ,flow , cost;\n};\n\nstruct MCMF {\n int n,m,s,t; // Total Number of Node Including S,T\n vector G[maxn]; //Graph\n vector E; // EdgeList\n T d[maxn]; // Distance Array of BeTmanFord\n bool inq[maxn]; // Is node in queue\n int p[maxn];\n T a[maxn];\n\n MCMF(int n) : n(n) {}\n\n void addEdge( int u , int v , T cap , T cost ){\n// cout< Q;\n Q.push(s);\n while(!Q.empty() ) {\n int u = Q.front(); Q.pop();\n inq[u] = 0;\n\n for( int i = 0 ; i < G[u].size() ; i ++ ) {\n Edge &e = E[G[u][i]];\n if( e.cap > e.flow && d[e.v] > d[u] + e.cost ) {\n d[e.v] = d[u] + e.cost;\n p[e.v] = G[u][i];\n a[e.v] = min( a[u] , e.cap - e.flow );\n\n if( inq[e.v ] == 0 ) {\n Q.push(e.v);\n inq[e.v] = 1;\n }\n }\n }\n }\n\n if( d[t] == INF ) return false; // No augmenting Path\n flow = a[t];\n cost = d[t] ; // Unit cost\n int u = t ;\n while( u != s ) {\n E[p[u]].flow += a[t];\n E[p[u]^1].flow -= a[t];\n u = E[p[u]].u;\n }\n return true;\n }\n\n pair Mincost (int s, int t) {\n this->s=s,this->t=t;\n T Mcost = 0;\n T Flow = 0;\n T f = 0 ; // For Each CaT , The flow\n T d = 0; // Shortest Distance / Cost Per Flow\n\n while(BelmanFord(f,d)) {\n Flow += f;\n Mcost += f *d ;\n }\n return make_pair(Flow, Mcost);\n }\n};\n\nint main() {\n int n, m, k, C, D;\n cin>>n>>m>>k>>C>>D;\n\n MCMF solver(n*K+2);\n int source = n*K, sink = n*K+1;\n\n for (int i=0; i>x;\n x--;\n solver.addEdge(source, x*K, 1, 0);\n }\n\n for (int i=0; i>u>>v;\n u--; v--;\n\n for (int j=1; j\nusing namespace std;\n\n#define double long double\n#define vec vector\n#define pb push_back\n#define unset unordered_set\n#define ii pair\n#define X first\n#define Y second\n#define FOR(i, a, b) for(int i = (a); i < (b); i++)\n#define FORE(i, a, b) for(int i = (a); i <= (b); i++)\n#define FORD(i, a, b) for(int i = (a); i >= (b); i--)\n#define bitcount(n) __builtin_popcountll(n)\ntemplate \nstd::pair operator+(const std::pair & l,const std::pair & r) {\n return {l.first+r.first,l.second+r.second};\n} \ntypedef void (*callback_function)(void); // type for conciseness\nconst long long ZERO = 0LL;\nconst long long INF64 = 1e18;\nconst int INF32 = 1e9;\nconst int MOD = 1e9 + 7;\nconst double PI = acos(-1.0L);\nconst double EPS = static_cast(1e-9);\ntypedef long long ll;\n\nint norm(int a) {\n return ((a % MOD) + MOD) % MOD;\n}\n\nint mul(int a, int b) {\n return int(a * 1LL * b % MOD);\n}\n\nint binPow(int a, int k) {\n int ans = 1;\n for (; k > 0; k >>= 1) {\n if (k & 1) {\n ans = mul(ans, a);\n }\n a = mul(a, a);\n }\n return ans;\n}\n\n// If I need to find (x / a) % MOD, I need to use this function:\n// (x / a) % MOD = (x * inv(a)) % MOD\nint inv(int a) {\n int b = binPow(a, MOD - 2);\n assert(mul(a, b) == 1);\n return b;\n}\n\nstruct Edge {\n int y;\n int capacity;\n int flow;\n int cost;\n\n Edge() {};\n\n Edge(int _y, int _capacity, int _flow, int _cost) \n : y(_y), capacity(_capacity), flow(_flow), cost(_cost) {};\n};\n\nvec e;\n\nconst int N = 1e6 + 7;\nvec g[N];\n\nll ans = 0;\nll dist[N];\nint pred[N];\nint pe[N];\nint inq[N];\n\nint source = N - 2;\nint target = N - 1;\n\nint residualCapacity(int x) {\n return e[x].capacity - e[x].flow;\n}\n\nvoid pushFlow() {\n FOR(i, 0, N) {\n dist[i] = INF64;\n pred[i] = -1;\n pe[i] = -1;\n inq[i] = 0;\n }\n dist[source] = 0;\n queue q;\n q.push(source);\n inq[source] = 1;\n while (!q.empty()) {\n int k = q.front(); q.pop();\n inq[k] = 0;\n for (auto x : g[k]) {\n if (!residualCapacity(x)) {\n continue;\n }\n int cost = e[x].cost;\n int y = e[x].y;\n if (dist[y] > dist[k] + cost) {\n dist[y] = dist[k] + cost;\n pred[y] = k;\n pe[y] = x;\n if (inq[y] == 0) {\n inq[y] = 1;\n q.push(y);\n }\n }\n }\n }\n int cur = target;\n //vec zz(1, cur);\n while (cur != source) {\n e[pe[cur]].flow++;\n e[pe[cur] ^ 1].flow--;\n cur = pred[cur];\n //zz.push_back(cur);\n }\n //reverse(zz.begin(), zz.end());\n //for (auto x : zz) {cerr << x << \" \";}\n //cerr << \"\\n\";\n ans += dist[target];\n}\n\nvoid addEdge(int x, int y, int capacity, int cost) {\n g[x].push_back(e.size());\n e.push_back(Edge(y, capacity, 0, cost));\n g[y].push_back(e.size());\n e.push_back(Edge(x, 0, 0, -cost));\n}\n\nint n, m, k, c, d;\n\nvoid input() {\n //freopen(\"??.in\", \"r\", stdin);\n //freopen(\"??.out\", \"w\", stdout);\n scanf(\"%d %d %d %d %d\", &n, &m, &k, &c, &d);\n FOR(i, 0, k) {\n int x;\n scanf(\"%d\", &x);\n --x;\n addEdge(source, x, 1, 0);\n }\n int timeLayers = 101;\n FOR(i, 0, timeLayers) {\n addEdge(i * n, target, k, i * c);\n }\n FOR(i, 0, m) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n FOR(i, 0, timeLayers - 1) FOR(j, 0, k) {\n addEdge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));\n }\n FOR(i, 0, timeLayers - 1) FOR(j, 0, k) {\n addEdge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));\n }\n }\n FOR(i, 0, n) FOR(j, 0, timeLayers - 1) {\n addEdge(i + j * n, i + j * n + n, k, 0);\n }\n FOR(i, 0, k) {\n pushFlow();\n }\n printf(\"%lld\\n\", ans);\n}\n\nvoid solve() {\n \n // read the question correctly (is y a vowel?)\n // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie();\n //auto beginProgram = chrono::steady_clock::now();\n {\n input();\n solve();\n }\n //auto endProgram = chrono::steady_clock::now();\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "a845eeacb27b9b6ff191d54f8a5cdf17", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*{{{*/\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define SZ(X) ((int)(X).size())\n#define ALL(X) (X).begin(), (X).end()\n#define REP(I, N) for (int I = 0; I < (N); ++I)\n#define REPP(I, A, B) for (int I = (A); I < (B); ++I)\n#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)\n#define FORS(I, S) for (int I = 0; S[I]; ++I)\n#define RS(X) scanf(\"%s\", (X))\n#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))\n#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())\n#define CASET int ___T; scanf(\"%d\", &___T); for(int cs=1;cs<=___T;cs++)\n#define MP make_pair\n#define PB push_back\n#define MS0(X) memset((X), 0, sizeof((X)))\n#define MS1(X) memset((X), -1, sizeof((X)))\n#define LEN(X) strlen(X)\n#define F first\n#define S second\nusing namespace std;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef long double LD;\ntypedef pair PII;\ntypedef vector VI;\ntypedef vector VL;\ntypedef vector VPII;\ntypedef pair PLL;\ntypedef vector VPLL;\ntemplate void _R(T &x) { cin >> x; }\nvoid _R(int &x) { scanf(\"%d\", &x); }\nvoid _R(LL &x) { scanf(\"%lld\", &x); }\nvoid _R(double &x) { scanf(\"%lf\", &x); }\nvoid _R(char &x) { scanf(\" %c\", &x); }\nvoid _R(char *x) { scanf(\"%s\", x); }\nvoid R() {}\ntemplate void R(T &head, U &... tail) { _R(head); R(tail...); }\ntemplate void _W(const T &x) { cout << x; }\nvoid _W(const int &x) { printf(\"%d\", x); }\nvoid _W(const LL &x) { printf(\"%lld\", x); }\nvoid _W(const double &x) { printf(\"%.16f\", x); }\nvoid _W(const char &x) { putchar(x); }\nvoid _W(const char *x) { printf(\"%s\", x); }\ntemplate void _W(const pair &x) {_W(x.F); putchar(' '); _W(x.S);}\ntemplate void _W(const vector &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }\nvoid W() {}\ntemplate void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\\n'); W(tail...); }\n#ifdef HOME\n #define DEBUG(...) {printf(\"# \");printf(__VA_ARGS__);puts(\"\");}\n#else\n #define DEBUG(...)\n#endif\nint MOD = 1e9+7;\nvoid ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}\n/*}}}*/\nconst int SIZE = 1e6+10;\n#define INF 1034567890\nconst int MAX_EDGE_NUM = 1000000;\nconst int MAX_NODE_NUM = 100000;\nstruct EDGE{\n int y,c,v,ty;\n EDGE(int _y=0,int _c=0,int _v=0,int ty=0):y(_y),c(_c),v(_v),ty(ty){}\n}es[MAX_EDGE_NUM];\nvectore[MAX_NODE_NUM];\nint C,d;\nint en;\nint EE; // sink\nvoid flow_clear(int _EE){\n EE=_EE;\n REP(i,EE+1)e[i].clear();\n en=0;\n}\nvoid add_edge(int x,int y,int c,int v,int _ty){\n e[x].PB(en);\n es[en++]=EDGE(y,c,v,_ty);\n e[y].PB(en);\n if(_ty==1){\n es[en++]=EDGE(x,0,-C+d,_ty);\n }\n else{\n es[en++]=EDGE(x,0,-v,_ty);\n }\n \n}\nint mi[MAX_NODE_NUM],lt[MAX_NODE_NUM],bfs[10000000],used[MAX_NODE_NUM];\nint flow(){\n REP(i,EE+1)mi[i]=INF,used[i]=0;\n mi[EE]=INF;\n bfs[0]=0;\n mi[0]=0;\n int rr=1;\n for(int i=0;i0&&mi[x]+es[r].v\nusing namespace std;\n\n#define vec vector\n#define pb push_back\n#define FOR(i, a, b) for(int i = (a); i < (b); i++)\n#define FORE(i, a, b) for(int i = (a); i <= (b); i++)\n#define FORD(i, a, b) for(int i = (a); i >= (b); i--)\nconst long long INF64 = 1e18;\ntypedef long long ll;\n\nstruct Edge {\n int from;\n int to;\n int f;\n int cost;\n\n Edge() {};\n\n Edge(int _from, int _to, int _f, int _cost) \n : from(_from), to(_to), f(_f), cost(_cost) {};\n};\n\nvec e;\n\nconst int N = 1e6 + 7;\nvec g[N];\n\nll ans = 0;\nll dist[N];\nint pred[N];\nint pe[N];\nint inq[N];\n\nint source = N - 2;\nint target = N - 1;\n\nint residualCapacity(int x) {\n return e[x].to - e[x].f;\n}\n\nvoid dijkstra() {\n FOR(i, 0, N) {\n dist[i] = INF64;\n pred[i] = -1;\n pe[i] = -1;\n inq[i] = 0;\n }\n dist[source] = 0;\n queue q;\n q.push(source);\n inq[source] = 1;\n while (!q.empty()) {\n int k = q.front(); q.pop();\n inq[k] = 0;\n for (auto x : g[k]) {\n if (!residualCapacity(x)) {\n continue;\n }\n int cost = e[x].cost;\n int y = e[x].from;\n if (dist[y] > dist[k] + cost) {\n dist[y] = dist[k] + cost;\n pred[y] = k;\n pe[y] = x;\n if (inq[y] == 0) {\n inq[y] = 1;\n q.push(y);\n }\n }\n }\n }\n}\n\nvoid pushFlow() {\n dijkstra();\n int cur = target;\n //vec zz(1, cur);\n while (cur != source) {\n e[pe[cur]].f++;\n e[pe[cur] ^ 1].f--;\n cur = pred[cur];\n //zz.push_back(cur);\n }\n //reverse(zz.begin(), zz.end());\n //for (auto x : zz) {cerr << x << \" \";}\n //cerr << \"\\n\";\n ans += dist[target];\n}\n\nvoid addEdge(int x, int y, int capacity, int cost) {\n g[x].push_back(e.size());\n e.push_back(Edge(y, capacity, 0, cost));\n g[y].push_back(e.size());\n e.push_back(Edge(x, 0, 0, -cost));\n}\n\nint n, m, k, c, d;\nint timeLayers = 101;\n\nvoid inputAndBuildGraph() {\n //freopen(\"??.in\", \"r\", stdin);\n //freopen(\"??.out\", \"w\", stdout);\n scanf(\"%d %d %d %d %d\", &n, &m, &k, &c, &d);\n FOR(i, 0, k) {\n int x;\n scanf(\"%d\", &x);\n --x;\n addEdge(source, x, 1, 0);\n }\n FOR(i, 0, timeLayers) {\n addEdge(i * n, target, k, i * c);\n }\n FOR(i, 0, m) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n FOR(i, 0, timeLayers - 1) FOR(j, 0, k) {\n addEdge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));\n }\n FOR(i, 0, timeLayers - 1) FOR(j, 0, k) {\n addEdge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));\n }\n }\n}\n\nvoid solve() {\n FOR(i, 0, n) FOR(j, 0, timeLayers - 1) {\n addEdge(i + j * n, i + j * n + n, k, 0);\n }\n FOR(i, 0, k) {\n pushFlow();\n }\n printf(\"%lld\\n\", ans);\n \n // read the question correctly (is y a vowel?)\n // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)\n}\n\nint main() {\n //auto beginProgram = chrono::steady_clock::now();\n {\n inputAndBuildGraph();\n solve();\n }\n //auto endProgram = chrono::steady_clock::now();\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "f02d5f0635262c4ac0976bf093f65872", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n\nusing namespace std;\n\nstruct edge\n{\n\tint y, c, f, cost;\n\tedge() {};\n\tedge(int y, int c, int f, int cost) : y(y), c(c), f(f), cost(cost) {};\n};\n\nvector e;\n\nconst int N = 14043;\nvector g[N];\n\nlong long ans = 0;\nlong long d[N];\nint p[N];\nint pe[N];\nint inq[N];\n\nconst long long INF64 = (long long)(1e18);\n\nint s = N - 2;\nint t = N - 1;\n\nint rem(int x)\n{\n\treturn e[x].c - e[x].f;\n}\n\nvoid push_flow()\n{\n\tfor(int i = 0; i < N; i++) d[i] = INF64, p[i] = -1, pe[i] = -1, inq[i] = 0;\n\td[s] = 0;\n\tqueue q;\n\tq.push(s);\n\tinq[s] = 1;\n\twhile(!q.empty())\n\t{\n\t\tint k = q.front();\n\t\tq.pop();\n\t\tinq[k] = 0;\n\t\tfor(auto x : g[k])\n\t\t{\n\t\t\tif(!rem(x)) continue;\n\t\t\tint c = e[x].cost;\n\t\t\tint y = e[x].y;\n\t\t\tif(d[y] > d[k] + c)\n\t\t\t{\n\t\t\t\td[y] = d[k] + c;\n\t\t\t\tp[y] = k;\n\t\t\t\tpe[y] = x;\n\t\t\t\tif(inq[y] == 0)\n\t\t\t\t{\n\t\t\t\t\tinq[y] = 1;\n\t\t\t\t\tq.push(y);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n//\tvector zz(1, cur);\n\twhile(cur != s)\n\t{\n\t\te[pe[cur]].f++;\n\t\te[pe[cur] ^ 1].f--;\n\t\tcur = p[cur];\n//\t\tzz.push_back(cur);\n\t}\n//\treverse(zz.begin(), zz.end());\n//\tfor(auto x : zz) cerr << x << \" \";\n//\tcerr << endl;\n\tans += d[t];\n}\n\nvoid add_edge(int x, int y, int c, int cost)\n{\n\tg[x].push_back(e.size());\n\te.push_back(edge(y, c, 0, cost));\n\tg[y].push_back(e.size());\n\te.push_back(edge(x, 0, 0, -cost));\n}\n\nint main()\n{\n\tint n, m, k, c, d;\n\tcin >> n >> m >> k >> c >> d;\n\tfor(int i = 0; i < k; i++)\n\t{\n\t\tint x;\n\t\tcin >> x;\n\t\t--x;\n\t\tadd_edge(s, x, 1, 0);\n\t}\n\tint tt = 101;\n\tfor(int i = 0; i < tt; i++)\n\t\tadd_edge(0 + i * n, t, k, i * c);\n\tfor(int i = 0; i < m; i++)\n\t{\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\t--x;\n\t\t--y;\n\t\tfor(int i = 0; i < tt - 1; i++)\n\t\t\tfor(int j = 0; j < k; j++)\n\t\t\t\tadd_edge(x + i * n, y + i * n + n, 1, d * (j * 2 + 1));\n\t\tfor(int i = 0; i < tt - 1; i++)\n\t\t\tfor(int j = 0; j < k; j++)\n\t\t\t\tadd_edge(y + i * n, x + i * n + n, 1, d * (j * 2 + 1));\n \t}\n \tfor(int i = 0; i < n; i++)\n \t\tfor(int j = 0; j < tt - 1; j++)\n \t\t\tadd_edge(i + j * n, i + j * n + n, k, 0);\n \tfor(int i = 0; i < k; i++)\n \t\tpush_flow();\n \tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "867c63eea48c9d010bd39c7a513c9d2a", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define long long long\nusing namespace std;\nconst int N=1e6;\nint fr[N+2],sr,sn;\nvectorgr[N+2];\n\nconst int inf=1e9;\nstruct edge\n{\n int a,b,f,c,d;\n};\nvectored;\nvectoradj[N+2];\nvoid addEdge(int a,int b,int c,int d)\n{\n edge e;e={a,b,0,c,d};\n adj[a].push_back(ed.size());\n ed.push_back(e);\n\n e={b,a,0,0,-d};\n adj[b].push_back(ed.size());\n ed.push_back(e);\n}\nint dis[N+2],inq[N+2],mnf[N+2],pth[N+2];\npairspfa(int n)\n{\n for(int i=0;i<=n;i++)dis[i]=inf;\n queueq;q.push(sr);\n dis[sr]=0,inq[sr]=1,mnf[sr]=inf;\n while(!q.empty())\n {\n int u=q.front();q.pop();inq[u]=0;\n for(auto id:adj[u])\n {\n int v=ed[id].b;\n if(ed[id].c>ed[id].f&&dis[v]>dis[u]+ed[id].d)\n {\n dis[v]=dis[u]+ed[id].d,pth[v]=id;\n mnf[v]=min(mnf[u],ed[id].c-ed[id].f);\n if(!inq[v])q.push(v),inq[v]=1;\n }\n }\n }\n if(dis[sn]==inf)return {0,0};\n int v=sn;\n while(v!=sr)\n {\n int id=pth[v];\n ed[id].f+=mnf[sn],ed[id^1].f-=mnf[sn];\n v=ed[id].a;\n }\n return {mnf[sn],dis[sn]*mnf[sn]};\n}\npairmcmf(int n)\n{\n int flow=0,cost=0;\n while(true)\n {\n pairp=spfa(n);\n if(!p.first)return {flow,cost};\n flow+=p.first,cost+=p.second;\n }\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);cin.tie(0);\n int n,m,k,c,d;cin>>n>>m>>k>>c>>d;\n for(int i=1;i<=k;i++)\n {\n int x;cin>>x;fr[x]++;\n }\n for(int i=1;i<=m;i++)\n {\n int u,v;cin>>u>>v;\n gr[u].push_back(v);\n gr[v].push_back(u);\n }\n sr=0,sn=100*n+n+1;\n for(int tm=0;tm<=99;tm++)\n {\n for(int i=1;i<=n;i++)\n {\n addEdge(n*tm+i,n*(tm+1)+i,k,0);\n for(auto x:gr[i])\n {\n for(int cp=1;cp<=k;cp++)\n {\n addEdge(n*tm+i,n*(tm+1)+x,1,d*(2*cp-1));\n }\n }\n }\n }\n for(int i=1;i<=n;i++)\n if(fr[i])addEdge(sr,i,fr[i],0);\n for(int tm=0;tm<=100;tm++)\n addEdge(n*tm+1,sn,k,c*tm);\n cout<\nusing namespace std;\n\nconst int maxn = 6010, INF = 0x3f3f3f3f;\nint n, m, k, c, d, cnt, ans, a[maxn], x[maxn], y[maxn];\nint head[maxn], pre[maxn], dist[maxn], inq[maxn];\nqueue q;\nstruct edge { int to, cap, cost, nxt; } e[2000010];\nint id(int t, int v) { return t * n + v; }\n\nvoid add(int from, int to, int cap, int cost) {\n e[cnt] = (edge){to, cap, cost, head[from]}, head[from] = cnt++;\n e[cnt] = (edge){from, 0, -cost, head[to]}, head[to] = cnt++;\n}\n\nbool spfa(int s, int t) {\n memset(dist, 0x3f, sizeof(dist));\n q.push(s), dist[s] = 0;\n while (!q.empty()) {\n int v = q.front(); q.pop(), inq[v] = 0;\n for (int i = head[v], u; ~i; i = e[i].nxt) {\n if (e[i].cap && dist[u = e[i].to] > dist[v] + e[i].cost) {\n dist[u] = dist[v] + e[i].cost, pre[u] = i;\n if (!inq[u]) q.push(u), inq[u] = 1;\n }\n }\n }\n return dist[t] < 1e9;\n}\n\nvoid upd(int s, int t) {\n for (int i = t; i ^ s; i = e[pre[i] ^ 1].to) {\n e[pre[i]].cap--, e[pre[i] ^ 1].cap++;\n }\n ans += dist[t];\n}\n\nint main() {\n memset(head, -1, sizeof(head));\n scanf(\"%d %d %d %d %d\", &n, &m, &k, &c, &d);\n int S = 0, T = 6000;\n for (int i = 1; i <= k; i++) {\n scanf(\"%d\", &a[i]), add(S, id(0, a[i]), 1, 0);\n }\n for (int i = 1; i <= m; i++) {\n scanf(\"%d %d\", &x[i], &y[i]);\n }\n for (int t = 0; t <= 100; t++) {\n for (int i = 2; i <= n; i++) {\n add(id(t, i), id(t + 1, i), k, c);\n }\n add(id(t, 1), T, k, 0);\n for (int i = 1; i <= m; i++) {\n for (int j = 1; j <= min(k, 25); j++) {\n add(id(t, x[i]), id(t + 1, y[i]), 1, c + (2 * j - 1) * d);\n add(id(t, y[i]), id(t + 1, x[i]), 1, c + (2 * j - 1) * d);\n }\n }\n }\n while (spfa(S, T)) upd(S, T);\n printf(\"%d\\n\", ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["flows", "graphs"], "code_uid": "9d344fccf7f3ab8aa92c2213f896d0c7", "src_uid": "2d0aa75f2e63c4fb8c98742ac8cd821c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nconst long long mod=1000000007;\nconst long long inf=mod*mod;\nconst long long d2=(mod+1)/2;\nconst long double EPS=1e-9;\nconst long double PI=acos(-1.0);\nint ABS(int a){return max(a,-a);}\nlong long ABS(long long a){return max(a,-a);}\nlong double ABS(long double a){return max(a,-a);}\n\nnamespace MCF {\n\t// required \n\t#define MAXN 15400\n\t#define MAXM 3100000\n\t#define wint int\n\t#define cint long long\n\tconst wint wEPS = 0;\n\tconst wint wINF = 1001001001;\n\tconst cint cEPS = 0;\n\tconst cint cINF = 100100100100100100LL;\n\tint n, m, ptr[MAXN], next[MAXM], zu[MAXM];\n\twint capa[MAXM], tof;\n\tcint cost[MAXM], toc, d[MAXN], pot[MAXN];\n\tint vis[MAXN], pree[MAXN];\n\tvoid init(int _n) {\n\t\tn = _n; m = 0; memset(ptr, ~0, n * 4);\n\t}\n\tvoid ae(int u, int v, wint w, cint c) {\n\t\tnext[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = w; cost[m] = +c; ++m;\n\t\tnext[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -c; ++m;\n\t}\n\tbool solve(int src, int ink, wint flo = wINF) {\n\t\tint i, u, v;\n\t\twint f;\n\t\tcint c, cc;\n\t\tmemset(pot, 0, n * sizeof(cint));\n\t\t//*\n\t\tfor (bool cont = 1; cont; ) {\n\t\t\tcont = 0;\n\t\t\tfor (u = 0; u < n; ++u) for (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {\n\t\t\t\tif (pot[zu[i]] > pot[u] + cost[i] + cEPS) {\n\t\t\t\t\tpot[zu[i]] = pot[u] + cost[i]; cont = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//*/\n\t\tfor (toc = 0, tof = 0; tof + wEPS < flo; ) {\n\t\t\ttypedef pair node;\n\t\t\tpriority_queue< node,vector,greater > q;\n\t\t\tfor (u = 0; u < n; ++u) { d[u] = cINF; vis[u] = 0; }\n\t\t\tfor (q.push(make_pair(d[src] = 0, src)); !q.empty(); ) {\n\t\t\t\tc = q.top().first; u = q.top().second; q.pop();\n\t\t\t\tif (vis[u]++) continue;\n\t\t\t\tfor (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {\n\t\t\t\t\tcc = c + cost[i] + pot[u] - pot[v = zu[i]];\n\t\t\t\t\tif (d[v] > cc + cEPS) { q.push(make_pair(d[v] = cc, v)); pree[v] = i; }\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!vis[ink]) return 0;\n\t\t\tf = flo - tof;\n\t\t\tfor (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; f=min(f,capa[i]); }\n\t\t\tfor (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; capa[i] -= f; capa[i ^ 1] += f; }\n\t\t\ttof += f;\n\t\t\ttoc += f * (d[ink] - pot[src] + pot[ink]);\n\t\t\tfor (u = 0; u < n; ++u) pot[u] += d[u];\n\t\t}\n\t\treturn 1;\n\t}\n}\nint big[60];\nint x[60];\nint y[60];\nint main(){\n\tint a,b,c,d,e;\n\tscanf(\"%d%d%d%d%d\",&a,&b,&c,&d,&e);\n\tfor(int i=0;i\nusing namespace std;\nconst int INF = 0x7f7f7f7f;\nint n,m,s,t,p,c,d,x[505],y[505];\nint mxf,mnc;\nint ee=1,h[5000005],nex[5000005<<1],to[5000005<<1],val[5000005<<1],cost[5000005<<1];\nint dis[5000005],pre[5000005],last[5000005],flow[5000005];\nbool vis[5000005];\nvoid addedge(int x,int y,int z,int c)\n{\n\tnex[++ee] = h[x];\n\tto[ee] = y;\n\tval[ee] = z;\n\tcost[ee] = c;\n\th[x] = ee;\n}\nbool spfa()\n{\n\tmemset(dis,INF,sizeof(dis));\n\tmemset(vis,false,sizeof(vis));\n\tqueue q;\n\tq.push(s);\n\tvis[s] = true;\n\tdis[s] = 0;\n\tflow[s] = INF;\n\tpre[t] = -1;\n\twhile(!q.empty())\n\t{\n\t\tint x=q.front();\n\t\tq.pop();\n\t\tvis[x] = false;\n\t\tfor(int i=h[x];i;i=nex[i])\n\t\t\tif(val[i] && dis[to[i]]>dis[x]+cost[i])\n\t\t\t{\n\t\t\t\tdis[to[i]] = dis[x]+cost[i];\n\t\t\t\tpre[to[i]] = x;\n\t\t\t\tlast[to[i]] = i;\n\t\t\t\tflow[to[i]] = min(flow[x],val[i]);\n\t\t\t\tif(!vis[to[i]])\n\t\t\t\t{\n\t\t\t\t\tvis[to[i]] = true;\n\t\t\t\t\tq.push(to[i]);\n\t\t\t\t}\n\t\t\t}\n\t}\n\treturn pre[t]!=-1;\n}\nvoid mcmf()\n{\n\tmxf = mnc = 0;\n\twhile(spfa())\n\t{\n\t\tint now=t;\n\t\tmxf += flow[t];\n\t\tmnc += flow[t]*dis[t];\n\t\twhile(now!=s)\n\t\t{\n\t\t\tval[last[now]] -= flow[t]; \n\t\t\tval[last[now]^1] += flow[t];\n\t\t\tnow = pre[now];\n\t\t}\n\t}\n}\nint zh(int t,int x)\n{\n\treturn t*n+x;\n}\nint main()\n{\n\tcin>>n>>m>>p>>c>>d;\n\ts = 0, t = n*99+1;\n\tfor(int i=1,x;i<=p&&cin>>x;i++)\n\t\taddedge(s,zh(0,x),1,0), addedge(zh(0,x),s,0,0);\n\tfor(int i=1;i<=m;cin>>x[i]>>y[i],i++);\n\tfor(int i=0;i<=99;i++)\n\t{\n\t\taddedge(zh(i,1),zh(i+1,1),INF,0), addedge(zh(i+1,1),zh(i,1),0,0);\n\t\tfor(int j=2;j<=n;j++)\n\t\t\taddedge(zh(i,j),zh(i+1,j),INF,c), addedge(zh(i+1,j),zh(i,j),0,-c);\n\t\tfor(int j=1;j<=m;j++)\n\t\t\tfor(int k=1;k<=p;k++)\n\t\t\t{\n\t\t\t\tint cst=d*k*k-d*(k-1)*(k-1)+c;\n\t\t\t\taddedge(zh(i,x[j]),zh(i+1,y[j]),1,cst), addedge(zh(i+1,y[j]),zh(i,x[j]),0,-cst);\n\t\t\t\taddedge(zh(i,y[j]),zh(i+1,x[j]),1,cst), addedge(zh(i+1,x[j]),zh(i,y[j]),0,-cst);\n\t\t\t}\n\t}\n\tmcmf();\n\tcout<\nusing namespace std;\n#define frr(frm,fornn)for(int i=frm; i=fornn; i--)\n#define fr(iii,frm,fornn)for(int iii=frm; iii=fornn; iii--)\n#define itfr(iii,frm)for(auto iii=frm.begin(); iii!=frm.end(); iii++)\n#define ll long long int\n#define lll unsigned long long int\n#define pb push_back\n#define seet(seta) memset(seta,-1,sizeof(seta))\n#define clr(seta) memset(seta,0,sizeof(seta))\n#define clr2(seta,xxxx,yyyy) memset(seta,0,sizeof(int)*xxxx*yyyy)\n#define seet2(seta,xxxx,yyyy) memset(seta,-1,sizeof(int)*xxxx*yyyy)\n#define E \"\\n\"\n#define infi 10000000000000\n#define F first\n#define S second\ntypedef vector ve;\ntypedef vector vb;\n\n#define mx (int) 1000000007\n\nvoid solve()\n{\n\tlll n;\n\tcin>>n;\n\tlll sum=1;\n\tfrr(1,n+1)\n\t{\n\t\tsum%=mx;\n\t\tsum*=i;\n\t}\n\tlll x=1;\n\tfrr(1,n)\n\t{\n\t\tx%=mx;\n\t\tx*=2;\n\t}\n\tcout<<(sum-x)%mx<>tc;\n\t//while(tc--)\n\t\tsolve();\n}\n\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "graphs"], "code_uid": "da1aca480cddcc8326cef69bcadcd7e9", "src_uid": "3dc1ee09016a25421ae371fa8005fce1", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nll n, m;\ndeque d;\nll A[1000];\nvector v;\n\nint main() {\n\tstring s;\n\tcin >> n;\n\tcin >> s;\n\tfor (ll i = 1; i <= n; i++) {\n\t\tif (s.length() % i == 0) {\n\t\t\tv.push_back(i);\n\t\t}\n\t}\n\tfor (ll i = 0; i < v.size(); i++) {\n\t\tstring d = \"\";\n\t\tfor (ll j = 0; j < v[i]; j++) {\n\t\t\td.push_back(s[j]);\n\t\t}\n\t\treverse(d.begin(), d.end());\n\t\tfor (ll j = v[i]; j < n; j++) {\n\t\t\td.push_back(s[j]);\n\t\t}\n\t\ts = d;\n\t}\n\tcout << s;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9d63a02b0566162b38ae841ef3fae5ea", "src_uid": "1b0b2ee44c63cb0634cb63f2ad65cdd3", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \r\n#include \r\n#include \r\n#include\r\n#include\r\n#include \r\n#include \r\n//#include // Common file\r\n//#include // Including tree_order_statistics_node_update\r\n//using namespace __gnu_pbds;\r\n\r\nusing namespace std;\r\nconst int64_t mod = 1e9 + 7;\r\nconst int64_t Mod = 998244353;\r\n//typedef tree,rb_tree_tag,tree_order_statistics_node_update> ordered_set;\r\n\r\nint64_t GCD(int64_t m ,int64_t n)\r\n{\r\n int64_t temp1 =max(m,n);\r\n int64_t temp2 =min(m,n);\r\n m=temp1;\r\n n=temp2;\r\n if(m%n==0)\r\n {\r\n return n;\r\n }\r\n else\r\n {\r\n return GCD(m%n,n);\r\n }\r\n}\r\n\r\nint64_t divisors (int64_t n,vector &V)\r\n{\r\n int64_t ans=1;\r\n while(n%2==0)\r\n {\r\n n=n/2;\r\n V.push_back(2);\r\n ans++;\r\n }\r\n for(int64_t i=3;i*i<=n;i+=2)\r\n { int64_t cnt =1;\r\n while(n%i==0)\r\n {\r\n n=n/i;\r\n V.push_back(i);\r\n\r\n cnt++;\r\n }\r\n ans *=cnt;\r\n }\r\n if(n>1)\r\n {\r\n V.push_back(n);\r\n ans *=2;\r\n }\r\n\r\n return ans;\r\n}\r\n\r\n\r\n\r\nint number(int64_t l)\r\n{\r\n int cnt=0;\r\n while(l>0)\r\n {\r\n cnt++;\r\n l/=2;\r\n }\r\n return cnt;\r\n}\r\n\r\nint64_t calpow(int64_t B, int64_t pow, int64_t mod)\r\n{\r\n int64_t mul = 1;\r\n while (pow > 0)\r\n {\r\n if (pow & 1)\r\n {\r\n mul = ((B % mod) * (mul % mod)) % mod;\r\n pow--;\r\n }\r\n else\r\n {\r\n B = ((B % mod) * (B % mod)) % mod;\r\n pow /= 2;\r\n }\r\n }\r\n return mul;\r\n}\r\n\r\n\r\n// ** Few Imp Tips/Facts ** //\r\n/* (a+b) = (a&b) + (a|b) IMP*/\r\n//MAPS TAKE A LOT OF TIME,REMEMBER//\r\n//Hockey Stick Identity: sigma(icr) = n+1cr+1;//\r\n\r\n\r\n\r\n\r\nvector fac(200007,0);\r\n\r\nint64_t modInverse(int64_t n,int64_t p)\r\n{\r\n return calpow(n, p - 2, p);\r\n}\r\n\r\nint64_t nCrModPFermat(int64_t n,int64_t r, int64_t p)\r\n{\r\n\r\n if (n < r)\r\n return 0;\r\n\r\n if (r == 0)\r\n return 1;\r\n\r\n\r\n return (fac[n] * modInverse(fac[r], p) % p\r\n * modInverse(fac[n - r], p) % p)\r\n % p;\r\n}\r\n\r\n/*Let g = gcd(a, b)g=gcd(a,b). While g ! =1, divide aa by gg and repeat. If at the end, aa equals 11, then bb is divisible by each prime factor of aa, and not otherwise.*/\r\n/*Good method to find each prime factor*/\r\nint64_t _mergeSort(int64_t arr[], int64_t temp[], int64_t left, int64_t right);\r\nint64_t merge(int64_t arr[], int64_t temp[], int64_t left, int64_t mid,int64_t right);\r\n\r\n/* This function sorts the\r\n input array and returns the\r\nnumber of inversions in the array */\r\nint64_t mergeSort(int64_t arr[], int64_t array_size)\r\n{\r\n int64_t temp[array_size];\r\n return _mergeSort(arr, temp, 0, array_size - 1);\r\n}\r\n\r\n/* An auxiliary recursive function\r\n that sorts the input array and\r\nreturns the number of inversions in the array. */\r\nint64_t _mergeSort(int64_t arr[], int64_t temp[], int64_t left, int64_t right)\r\n{\r\n int64_t mid, inv_count = 0;\r\n if (right > left) {\r\n /* Divide the array into two parts and\r\n call _mergeSortAndCountInv()\r\n for each of the parts */\r\n mid = (right + left) / 2;\r\n\r\n /* Inversion count will be sum of\r\n inversions in left-part, right-part\r\n and number of inversions in merging */\r\n inv_count += _mergeSort(arr, temp, left, mid);\r\n inv_count += _mergeSort(arr, temp, mid + 1, right);\r\n\r\n /*Merge the two parts*/\r\n inv_count += merge(arr, temp, left, mid + 1, right);\r\n }\r\n return inv_count;\r\n}\r\n\r\n/* This funt merges two sorted arrays\r\nand returns inversion count in the arrays.*/\r\nint64_t merge(int64_t arr[], int64_t temp[], int64_t left, int64_t mid,int64_t right)\r\n{\r\n int64_t i, j, k;\r\n int64_t inv_count = 0;\r\n\r\n i = left; /* i is index for left subarray*/\r\n j = mid; /* j is index for right subarray*/\r\n k = left; /* k is index for resultant merged subarray*/\r\n while ((i <= (mid - 1)) && (j <= right)) {\r\n if (arr[i] <= arr[j]) {\r\n temp[k++] = arr[i++];\r\n }\r\n else {\r\n temp[k++] = arr[j++];\r\n\r\n /* this is tricky -- see above\r\n explanation/diagram for merge()*/\r\n inv_count = inv_count + (mid - i);\r\n }\r\n }\r\n\r\n /* Copy the remaining elements of left subarray\r\n(if there are any) to temp*/\r\n while (i <= (mid - 1))\r\n {\r\n\r\n temp[k++] = arr[i++];\r\n }\r\n\r\n /* Copy the remaining elements of right subarray\r\n (if there are any) to temp*/\r\n while (j <= right)\r\n {temp[k++] = arr[j++];}\r\n\r\n /*Copy back the merged elements to original array*/\r\n for (i = left; i <= right; i++)\r\n {arr[i] = temp[i];}\r\n\r\n return inv_count;\r\n}\r\nbool compare(string a,string b)\r\n{\r\n for(int i=0;ib[i])\r\n {\r\n return true;\r\n }\r\n else\r\n {\r\n return false;\r\n }\r\n }\r\n else\r\n {\r\n if(a[i]>b[i])\r\n {\r\n return false;\r\n }\r\n else\r\n {\r\n return true;\r\n }\r\n }\r\n }\r\n }\r\n}\r\nvoid MAXHEAPIFY(int64_t *A,vector< string> V,int64_t i,int64_t n) //If at any node there is a parent node whose child's value is greater thanit ,then this will modiify the heap accordingly.\r\n{\r\n int64_t MAX =i;\r\n int64_t l = (2*i); //Left child.\r\n int64_t r = (2*i)+1; //Right child.\r\n\r\n if((l<=n) && (compare(V[A[l]-1],V[A[MAX]-1])))\r\n {\r\n MAX =l;\r\n }\r\n if((r<=n) && (compare(V[A[r]-1],V[A[MAX]-1])))\r\n {\r\n MAX =r;\r\n }\r\n if(MAX!=i)\r\n {\r\n\r\n swap(A[i],A[MAX]); // Exchange child with parent.\r\n\r\n MAXHEAPIFY(A,V,MAX,n);// Check for cases of violation at new positon.\r\n }\r\n else\r\n {return;}\r\n}\r\nvoid BuidHeap(int64_t *A,vector< string> V,int64_t n) // Buids a heap structure out of any array.\r\n{\r\n for(int64_t i = (n/2);i>=1;i--)\r\n {\r\n MAXHEAPIFY(A,V,i,n);\r\n\r\n }\r\n}\r\nvoid HeapSort(int64_t *A,vector< string> V,int64_t n)//sorts the array using heap structure.\r\n{\r\n BuidHeap(A,V,n);\r\n\r\n\r\n for(int64_t i =n;i>=2;i--)\r\n {\r\n swap(A[1],A[i]);\r\n MAXHEAPIFY(A,V,1,i-1);\r\n }\r\n}\r\nbool check(char a)\r\n{\r\n if(a == '_')\r\n {\r\n return false;\r\n }\r\n\r\n return true;\r\n}\r\n /*K fibonnaic numbers implementation done using sum*/\r\nint64_t solve(string s)\r\n{\r\n int64_t ans =1;\r\n int64_t y =1;\r\n int n = s.size();\r\n if(n==1)\r\n {\r\n if((s[0]=='0')||(s[0]=='_'))\r\n {\r\n return 1;\r\n }\r\n else\r\n {\r\n return 0;\r\n }\r\n }\r\n\r\n else if(s[0] == '0')\r\n {\r\n return 0;\r\n }\r\n\r\n if((check(s[n-1]))&&(check(s[n-2])))\r\n { int f = (int)(s[n-2]-'0');\r\n f *=10;\r\n int x = (int)(s[n-1]-'0');\r\n f +=x;\r\n\r\n\r\n if((f%25)!=0)\r\n {\r\n return 0;\r\n }\r\n\r\n y =1;\r\n }\r\n else if((check(s[n-1]))&&(!check(s[n-2])))\r\n {\r\n if((s[n-1]=='0')||(s[n-1]=='5'))\r\n {\r\n y = 2;\r\n }\r\n else\r\n {\r\n return 0;\r\n }\r\n }\r\n else if((check(s[n-2]))&&(!check(s[n-1])))\r\n {\r\n if((s[n-2]=='0')||(s[n-2]=='2')||(s[n-2]=='5')||(s[n-2]=='7'))\r\n {\r\n y =1;\r\n }\r\n else\r\n {\r\n return 0;\r\n }\r\n }\r\n else if((!check(s[n-2]))&&(!check(s[n-1])))\r\n {\r\n y =4;\r\n }\r\n\r\n\r\n for(int i=n-3;i>=0;i--)\r\n {\r\n if(s[i]=='_')\r\n { if(i!=0)\r\n {ans *=10;}\r\n else\r\n {\r\n ans *=9;\r\n }\r\n }\r\n }\r\n\r\n if(y==4)\r\n {\r\n if(n==2)\r\n {\r\n ans *=3;\r\n }\r\n else\r\n {\r\n ans *=4;\r\n }\r\n }\r\n if(y==2)\r\n {\r\n if(n==2)\r\n {if(s[1]=='0')\r\n {\r\n ans =ans;\r\n }\r\n else\r\n {\r\n ans = 2*ans;\r\n }\r\n }\r\n else\r\n {\r\n ans = 2*ans;\r\n }\r\n }\r\n if(y==1)\r\n {\r\n ans = ans;\r\n }\r\n\r\n return ans;\r\n\r\n}\r\n\r\nint main()\r\n{\r\n ios_base::sync_with_stdio(false);\r\n cin.tie(NULL);\r\n /*fac[0] = 1;\r\n for (int i = 1; i <= 200006; i++)\r\n {\r\n\r\n fac[i] = (fac[i - 1] * i) % Mod;\r\n }*/\r\n\r\n\r\n string s;\r\n cin>>s;\r\n\r\n\r\n int n =s.size();\r\n int64_t ans =0;\r\n bool xpresent = false;\r\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define INF 1000000007\n#define PB push_back\n#define MP make_pair\n#define X first\n#define Y second\n#define Min(a,b) (((a) < (b))?(a):(b))\n#define Max(a,b) (((a) > (b))?(a):(b))\n#define Abs(a) (((a) > 0)?(a):(-1)*(a))\n#define cil(a,b) ( ((a)%(b) == 0)?((a)/(b)):((a)/(b)+1) )\n#define S(a) ((a)*(a))\n#define MAGIC 100000\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef double db;\ntypedef vector vi;\ntypedef vector::iterator viit;\ntypedef vector vll;\ntypedef vector::iterator vllit;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\n\nll exp (ll b, ll e) {\n\tif (b == 1 || e == 0) return 1;\n\tif(e == 1) return b%INF;\n\tif(e == 2) return (b*b)%INF;\n\treturn ( (e%2 == 0)? exp(exp(b,e/2),2)%INF : (b * (exp(exp(b,e/2),2)%INF))%INF );\n}\n\nll gcd ( ll a, ll b ) {\n\treturn ( (b == 0)?a:gcd(b,a%b) );\n}\n\nint main() {\n\n\tll g,lcm,r1,r2,a1,a2,b1,b2,x,y,L,R;\n\tscanf(\"%lld %lld %lld %lld %lld %lld\", &a1, &b1, &a2, &b2, &L, &R);\n\tif (a2 < a1) {\n\t\tll t = a2, u = b2;\n\t\ta2 = a1; b2 = b1;\n\t\ta1 = t; b1 = u;\n\t}\n\tr1 = ( (b1 >= 0)?b1%a1:((a1 - Abs(b1)%a1)%a1) );\n\tr2 = ( (b2 >= 0)?b2%a2:((a2 - Abs(b2)%a2)%a2) );\n\tg = gcd(a1,a2);\n\tlcm = (a1*a2)/g;\n\tif (Abs(r1-r2)%g != 0) {\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tll ans = 0, min_val = Max(b1, b2), app1,app2;\n\tif (L < 0) {\n\t\tll f = cil(Abs(L),lcm);\n\t\tL += f*lcm;\n\t\tR += f*lcm;\n\t\tapp1 = (f*lcm)/a1; app2 = (f*lcm)/a2;\n\t\tmin_val += f*lcm;\n\t}\n\tll l_dup = Max(L,min_val);\n\tif(l_dup > R) {\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tif (a2 < MAGIC) {\n\t\tans += Max((R/lcm) - (l_dup/lcm)-1, 0);\n\t\tll j = (lcm/a2), v;\n\t\tfor (ll i = 0 ; i < j ; ++i) {\n\t\t\tif ( (i*a2 + r2-r1)%a1 == 0 ) {\n\t\t\t\tx = (i*a2 + r2);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tll q = l_dup/lcm;\n\t\tif (q*lcm + x >= l_dup && q*lcm + x <= R) ans++;\n\t\tq = R - R%lcm;\n\t\tif (R/lcm > l_dup/lcm) {\n\t\t\tif ((q + x <= R) && (q + x >= l_dup)) ++ans;\n\t\t}\n\t}\n\telse {\n\t\tll i,f,j;\n\t\tans = 0;\n\t\tf = l_dup - l_dup%a2;\n\t\tfor (i = f ; i <= R ; i += a2) {\n\t\t\tif ( (i+r2-r1)%a1 == 0 ) {\n\t\t\t\tif (i+r2 <= R && i+r2 >= l_dup) ++ans;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "e8195ef43ed213256435460118029e30", "src_uid": "b08ee0cd6f5cb574086fa02f07d457a4", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\nusing namespace std;\n#define IOS {cin.tie(0);ios_base::sync_with_stdio(false);}\n#define N 300005\n#define M 3005\n#define int int64_t\nconst int MOD = 998244353;\n\nint powmo(int a,int b){\n\tint r = 1;\n\tfor(;b;b>>=1){\n\t\tif(b&1) r = r*a%MOD;\n\t\ta = a*a%MOD;\n\t}\n\treturn r;\n}\n\nint mdiv(int a,int b){\n\treturn a * powmo(b,MOD-2) % MOD;\n}\n\nbitset like;\nint w[N];\nint ans[N];\nint dp[M][M];\n\nint32_t main(){\n\tIOS;\n\tint n,m,t,add,sub,sum;\n\tadd = sub = 0;\n\tcin >> n >> m;\n\tfor(int i=0;i> t;\n\t\tlike[i] = t; \n\t}\n\tfor(int i=0;i> w[i];\n\t\tif(like[i]) add += w[i];\n\t\telse sub += w[i];\n\t}\n\tdp[0][0] = 1;\n\tsum = add + sub;\n\tint ia,is; ia = mdiv(1,add), is = mdiv(1,sub);\n\tfor(int x=1;x<=m;x++){\n\t\tfor(int i=x,j=0;i>=0 && j<=sub;i--,j++){\n\t\t\tint nowsum = sum + i - j;\n\t\t\tint nowadd = add + i;\n\t\t\tint nowsub = sub - j;\n\t\t\t//cout << x << ' ' << i << ' ' << j << ' ' << nowadd << ' ' << nowsub << ' ' << nowsum << '\\n';\n\t\t\tdp[i][j] = 0;\n\t\t\tif(i>0) dp[i][j] = (dp[i][j] + dp[i-1][j] * mdiv(nowadd-1,nowsum-1) % MOD ) % MOD;\n\t\t\tif(j>0) dp[i][j] = (dp[i][j] + dp[i][j-1] * mdiv(nowsub+1,nowsum+1) % MOD) % MOD;\n\t\t}\n\t}\n\t//for(int i=0;i<=m;i++,cout << '\\n') for(int j=0;j<=m;j++) cout << dp[i][j] << ' ';\n\tint sa,ss; sa = ss = 0;\n\tfor(int i=m,j=0;i>=0 && j<=sub;i--,j++){\n\t\tsa = (sa + i * dp[i][j]) % MOD;\n\t\tss = (ss + MOD - j * dp[i][j] % MOD) % MOD;\n\t}\n\tfor(int i=0;i\n#include\n#include\n\nusing namespace std;\n\nint main()\n{\n int a[28];\n int flag=0;\n for(int i=1;i<=24;i++)\n scanf(\"%d\",&a[i]);\n if(a[1]==a[2]&&a[1]==a[3]&&a[1]==a[4] && a[9]==a[10]&&a[9]==a[11]&&a[9]==a[12]){\n if(a[5]==a[6]&&a[5]==a[15]&&a[5]==a[16] && a[13]==a[14]&&a[13]==a[23]&&a[13]==a[24] &&\n a[21]==a[22]&&a[21]==a[19]&&a[21]==a[20] && a[17]==a[18]&&a[17]==a[7]&&a[17]==a[8])\n flag=1;\n else if(a[5]==a[6]&&a[5]==a[19]&&a[5]==a[20] && a[13]==a[14]&&a[13]==a[7]&&a[13]==a[8] &&\n a[21]==a[22]&&a[21]==a[15]&&a[21]==a[16] && a[17]==a[18]&&a[17]==a[23]&&a[17]==a[24])\n flag=1;\n }\n else if(flag==0 && a[13]==a[14]&&a[13]==a[15]&&a[13]==a[16] && a[17]==a[18]&&a[17]==a[19]&&a[17]==a[20]){\n if(a[2]==a[4]&&a[2]==a[5]&&a[2]==a[7] && a[6]==a[8]&&a[6]==a[9]&&a[6]==a[11] &&\n a[10]==a[12]&&a[10]==a[22]&&a[10]==a[24] && a[21]==a[23]&&a[21]==a[1]&&a[21]==a[3])\n flag=1;\n else if(a[2]==a[4]&&a[2]==a[22]&&a[2]==a[24] && a[6]==a[8]&&a[6]==a[1]&&a[6]==a[3] &&\n a[10]==a[12]&&a[10]==a[5]&&a[10]==a[7] && a[21]==a[23]&&a[21]==a[9]&&a[21]==a[11])\n flag=1;\n }\n else if(flag==0 && a[5]==a[6]&&a[5]==a[7]&&a[5]==a[8] && a[21]==a[22]&&a[21]==a[23]&&a[21]==a[24]){\n if(a[1]==a[2]&&a[1]==a[14]&&a[1]==a[16] && a[13]==a[15]&&a[13]==a[9]&&a[13]==a[10] &&\n a[11]==a[12]&&a[11]==a[17]&&a[11]==a[19] && a[18]==a[20]&&a[18]==a[3]&&a[18]==a[4])\n flag=1;\n else if(a[1]==a[2]&&a[1]==a[17]&&a[1]==a[19] && a[13]==a[15]&&a[13]==a[3]&&a[13]==a[4] &&\n a[11]==a[12]&&a[11]==a[14]&&a[11]==a[16] && a[18]==a[20]&&a[18]==a[9]&&a[18]==a[10])\n flag=1;\n }\n if(flag==1)\n printf(\"YES\\n\");\n else\n printf(\"NO\\n\");\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "8d4267e60fedeb3f1670dbae4abdd4fa", "src_uid": "881a820aa8184d9553278a0002a3b7c4", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\ntypedef double DB;\ntypedef pair Point;\n#define x first\n#define y second\nconst int T = 111;\nconst DB EPS = 1e-10;\nconst DB PI = acos(-1.0);\ninline int sgn(const DB &a) { return (a > EPS) - (a < -EPS); }\n\nPoint operator + (const Point &a, const Point &b) { return Point(a.x + b.x, a.y + b.y); }\nPoint operator - (const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); }\nPoint operator * (const Point &a, const DB &p) { return Point(a.x * p, a.y * p); }\nPoint operator * (const DB &p, const Point &a) { return Point(a.x * p, a.y * p); }\nPoint operator / (const Point &a, const DB &p) { return Point(a.x / p, a.y / p); }\n\ninline DB cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }\ninline DB dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }\ninline DB veclen(const Point &a) { return sqrt(dot(a, a)); }\ninline DB angle(const Point &a) { return atan2(a.y, a.x); }\ninline bool get(Point &a) { return cin >> a.x >> a.y; }\ninline Point normal(const Point &a) { return Point(-a.y, a.x) / veclen(a); }\n\nstruct Circle {\n\tPoint c;\n\tDB r;\n\tCircle() {}\n\tCircle(Point c, DB r) : c(c), r(r) {}\n\tPoint point(const DB &p) const { return c + Point(cos(p), sin(p)) * r; }\n\tbool in(const Point &p) const { return sgn(veclen(p - c) - r) < 0; }\n} C1, C2;\n\nint tangent(const Point &p, const Circle &c, DB *sol) {\n\tDB d = veclen(p - c.c);\n\tDB ang = angle(p - c.c);\n\tDB da = acos(c.r / d);\n\tsol[0] = ang + da;\n\tsol[1] = ang - da;\n\treturn 2;\n}\n\nstruct Line {\n\tPoint s, t;\n\tLine() {}\n\tLine(Point s, Point t) : s(s), t(t) {}\n\tPoint vec() const { return t - s; }\n\tPoint point(const DB &p) const { return s + vec() * p; }\n} ;\ninline Point llint(const Line &a, const Line &b) { return a.point(cross(b.vec(), a.s - b.s) / cross(a.vec(), b.vec())); }\ninline bool onseg(const Point &p, const Point &a, const Point &b) { return sgn(cross(a - p, b - p)) == 0 && sgn(dot(a - p, b - p)) <= 0; }\n\nPoint A, B;\nconst DB FINF = 1e20;\nconst Point O(0.0, 0.0);\nDB vp, v, r, s;\n\nDB fix(DB a) {\n\ta = fabs(a);\n\ta = min(a, PI * 2 - a);\n\treturn a;\n}\n\nbool check(const Point &a, const Point &b, const Circle &c) {\n\tLine l(a, b);\n\tPoint nor = normal(l.vec()), ip = llint(l, Line(c.c, c.c + nor));\n\treturn c.in(ip) && onseg(ip, a, b);\n}\n\nPoint get_pt(const DB &t) { return C1.point(s + vp * t); }\nDB cal(const Point &p) {\n\tif (check(p, B, C2)) {\n\t\tDB a[2], b[2];\n\t\ttangent(p, C2, a);\n\t\ttangent(B, C2, b);\n\t\tDB da = FINF;\n\t\tfor (int i = 0; i < 2; i++) {\n\t\t\tfor (int j = 0; j < 2; j++) {\n\t\t\t\tda = min(fix(a[i] - b[j]), da);\n\t\t\t}\n\t\t}\n\t\treturn (veclen(C2.point(a[0]) - p) + veclen(C2.point(b[0]) - B) + da * C2.r) / v;\n\t}\n\treturn veclen(p - B) / v;\n}\n\nDB bs() {\n\tDB l = 0, r = FINF, m;\n\tfor (int i = 0; i < T; i++) {\n\t\tm = (l + r) / 2;\n\t\tif (cal(get_pt(m)) <= m) r = m;\n\t\telse l = m;\n\t}\n\treturn l;\n}\n\nDB work() {\n\tC1 = Circle(O, veclen(A));\n\tC2 = Circle(O, r);\n\ts = angle(A);\n\tvp /= veclen(A);\n\t//cout << \"vp \" << vp << ' ' << C1.r << ' ' << C2.r << endl;\n\treturn bs();\n}\n\nint main() {\n\t//freopen(\"in\", \"r\", stdin);\n\tcout << setiosflags(ios::fixed) << setprecision(10);\n\twhile (get(A)) {\n\t\tcin >> vp; get(B);\n\t\tcin >> v >> r;\n\t\tcout << work() << endl;\n\t\t//cout << get_pt(1.0).x << ' ' << get_pt(1.0).y << ' ' << check(get_pt(1.0), B, C2) << endl;\n\t\t//cout << cal(get_pt(1.0)) << endl;\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry", "binary search"], "code_uid": "5726c9d122a1fcb7cf841cbde87fb207", "src_uid": "e8471556906e5fa3a701842570fa4ee2", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "// LUOGU_RID: 90427948\n#include\r\n#define int long long\r\n#define elif else if\r\nusing namespace std;\r\nint n;double p;\r\nbool func(int i){\r\n\tdouble d3=i*(i-1)*(i-2)/6,d2=(n-i)*(i-1)*i/2,d1=i*(n-i)*(n-i-1)/2,d0=(n-i)*(n-i-1)*(n-i-2)/6;\r\n\treturn (d3+d2+d1/2)/(d0+d1+d2+d3)>=p;\r\n} \r\nsigned main(){\r\n\tios::sync_with_stdio(false); \r\n\tcin>>n>>p;\r\n\tfor(int i=0;i\r\nusing namespace std;\r\n\r\nusing ll=long long;\r\n//#define int ll\r\n\r\n#define rng(i,a,b) for(int i=int(a);i=int(a);i--)\r\n#define per(i,b) gnr(i,0,b)\r\n#define pb push_back\r\n#define eb emplace_back\r\n#define a first\r\n#define b second\r\n#define bg begin()\r\n#define ed end()\r\n#define all(x) x.bg,x.ed\r\n#define si(x) int(x.size())\r\n#ifdef LOCAL\r\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"< bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b using vc=vector;\r\ntemplate using vvc=vc>;\r\n\r\nusing pi=pair;\r\nusing vi=vc;\r\n\r\ntemplate\r\nostream& operator<<(ostream& os,const pair& p){\r\n\treturn os<<\"{\"< ostream& operator<<(ostream& os,const vc& v){\r\n\tos<<\"{\";\r\n\tfor(auto e:v)os<\r\nvoid dmpr(ostream&os,const T&t,const Args&... args){\r\n\tos<\r\nostream& operator<<(ostream&os,const array&a){\r\n\treturn os<(all(a));\r\n}\r\n\r\ntemplate\r\nvoid print_tuple(ostream&,const T&){\r\n}\r\n\r\ntemplate\r\nvoid print_tuple(ostream&os,const T&t){\r\n\tif(i)os<<\",\";\r\n\tos<(t);\r\n\tprint_tuple(os,t);\r\n}\r\n\r\ntemplate\r\nostream& operator<<(ostream&os,const tuple&t){\r\n\tos<<\"{\";\r\n\tprint_tuple<0,tuple,Args...>(os,t);\r\n\treturn os<<\"}\";\r\n}\r\n\r\ntemplate\r\nvoid print(t x,int suc=1){\r\n\tcout<>i;\r\n\treturn i;\r\n}\r\n\r\nvi readvi(int n,int off=0){\r\n\tvi v(n);\r\n\trep(i,n)v[i]=read()+off;\r\n\treturn v;\r\n}\r\n\r\npi readpi(int off=0){\r\n\tint a,b;cin>>a>>b;\r\n\treturn pi(a+off,b+off);\r\n}\r\n\r\ntemplate\r\nvoid print(const pair&p,int suc=1){\r\n\tprint(p.a,2);\r\n\tprint(p.b,suc);\r\n}\r\n\r\ntemplate\r\nvoid print(const vector&v,int suc=1){\r\n\trep(i,v.size())\r\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\r\n}\r\n\r\nstring readString(){\r\n\tstring s;\r\n\tcin>>s;\r\n\treturn s;\r\n}\r\n\r\ntemplate\r\nT sq(const T& t){\r\n\treturn t*t;\r\n}\r\n\r\n//#define CAPITAL\r\nvoid yes(bool ex=true){\r\n\t#ifdef CAPITAL\r\n\tcout<<\"YES\"<<\"\\n\";\r\n\t#else\r\n\tcout<<\"Yes\"<<\"\\n\";\r\n\t#endif\r\n\tif(ex)exit(0);\r\n\t#ifdef LOCAL\r\n\tcout.flush();\r\n\t#endif\r\n}\r\nvoid no(bool ex=true){\r\n\t#ifdef CAPITAL\r\n\tcout<<\"NO\"<<\"\\n\";\r\n\t#else\r\n\tcout<<\"No\"<<\"\\n\";\r\n\t#endif\r\n\tif(ex)exit(0);\r\n\t#ifdef LOCAL\r\n\tcout.flush();\r\n\t#endif\r\n}\r\nvoid possible(bool ex=true){\r\n\t#ifdef CAPITAL\r\n\tcout<<\"POSSIBLE\"<<\"\\n\";\r\n\t#else\r\n\tcout<<\"Possible\"<<\"\\n\";\r\n\t#endif\r\n\tif(ex)exit(0);\r\n\t#ifdef LOCAL\r\n\tcout.flush();\r\n\t#endif\r\n}\r\nvoid impossible(bool ex=true){\r\n\t#ifdef CAPITAL\r\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\r\n\t#else\r\n\tcout<<\"Impossible\"<<\"\\n\";\r\n\t#endif\r\n\tif(ex)exit(0);\r\n\t#ifdef LOCAL\r\n\tcout.flush();\r\n\t#endif\r\n}\r\n\r\nconstexpr ll ten(int n){\r\n\treturn n==0?1:ten(n-1)*10;\r\n}\r\n\r\nconst ll infLL=LLONG_MAX/3;\r\n\r\n#ifdef int\r\nconst int inf=infLL;\r\n#else\r\nconst int inf=INT_MAX/2-100;\r\n#endif\r\n\r\nint topbit(signed t){\r\n\treturn t==0?-1:31-__builtin_clz(t);\r\n}\r\nint topbit(ll t){\r\n\treturn t==0?-1:63-__builtin_clzll(t);\r\n}\r\nint botbit(signed a){\r\n\treturn a==0?32:__builtin_ctz(a);\r\n}\r\nint botbit(ll a){\r\n\treturn a==0?64:__builtin_ctzll(a);\r\n}\r\nint popcount(signed t){\r\n\treturn __builtin_popcount(t);\r\n}\r\nint popcount(ll t){\r\n\treturn __builtin_popcountll(t);\r\n}\r\nbool ispow2(int i){\r\n\treturn i&&(i&-i)==i;\r\n}\r\nll mask(int i){\r\n\treturn (ll(1)< void mkuni(vc&v){\r\n\tsort(all(v));\r\n\tv.erase(unique(all(v)),v.ed);\r\n}\r\n\r\nll rand_int(ll l, ll r) { //[l, r]\r\n\t#ifdef LOCAL\r\n\tstatic mt19937_64 gen;\r\n\t#else\r\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\r\n\t#endif\r\n\treturn uniform_int_distribution(l, r)(gen);\r\n}\r\n\r\ntemplate\r\nvoid myshuffle(vc&a){\r\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\r\n}\r\n\r\ntemplate\r\nint lwb(const vc&v,const t&a){\r\n\treturn lower_bound(all(v),a)-v.bg;\r\n}\r\n\r\nvvc readGraph(int n,int m){\r\n\tvvc g(n);\r\n\trep(i,m){\r\n\t\tint a,b;\r\n\t\tcin>>a>>b;\r\n\t\t//sc.read(a,b);\r\n\t\ta--;b--;\r\n\t\tg[a].pb(b);\r\n\t\tg[b].pb(a);\r\n\t}\r\n\treturn g;\r\n}\r\n\r\nvvc readTree(int n){\r\n\treturn readGraph(n,n-1);\r\n}\r\n\r\n//mint107 は verify してねえ\r\n//#define DYNAMIC_MOD\r\n\r\nstruct modinfo{uint mod,root;\r\n#ifdef DYNAMIC_MOD\r\nconstexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}\r\null im;\r\nconstexpr void set_mod(uint m){\r\n\tmod=m;\r\n\tim=ull(-1)/m+1;\r\n}\r\nuint product(uint a,uint b)const{\r\n\tull z=ull(a)*b;\r\n\tuint x=((unsigned __int128)z*im)>>64;\r\n\tuint v=uint(z)-x*mod;\r\n\treturn v\r\nstruct modular{\r\n\tstatic constexpr uint const &mod=ref.mod;\r\n\tstatic modular root(){return modular(ref.root);}\r\n\tuint v;\r\n\t//modular(initializer_listls):v(*ls.bg){}\r\n\tmodular(ll vv=0){s(vv%mod+mod);}\r\n\tmodular& s(uint vv){\r\n\t\tv=vv>=1;\r\n\t\t}\r\n\t\treturn res;\r\n\t}\r\n\tmodular inv()const{return pow(mod-2);}\r\n\t/*modular inv()const{\r\n\t\tint x,y;\r\n\t\tint g=extgcd(v,mod,x,y);\r\n\t\tassert(g==1);\r\n\t\tif(x<0)x+=mod;\r\n\t\treturn modular(x);\r\n\t}*/\r\n\tfriend modular operator+(int x,const modular&y){\r\n\t\treturn modular(x)+y;\r\n\t}\r\n\tfriend modular operator-(int x,const modular&y){\r\n\t\treturn modular(x)-y;\r\n\t}\r\n\tfriend modular operator*(int x,const modular&y){\r\n\t\treturn modular(x)*y;\r\n\t}\r\n\tfriend modular operator/(int x,const modular&y){\r\n\t\treturn modular(x)/y;\r\n\t}\r\n\tfriend ostream& operator<<(ostream&os,const modular&m){\r\n\t\treturn os<>(istream&is,modular&m){\r\n\t\tll x;is>>x;\r\n\t\tm=modular(x);\r\n\t\treturn is;\r\n\t}\r\n\tbool operator<(const modular&r)const{return v\r\nvoid inplace_fmt(const int n,mint*const f,bool inv){\r\n\tstatic constexpr uint mod=mint::mod;\r\n\tstatic constexpr uint mod2=mod*2;\r\n\tstatic const int L=30;\r\n\tstatic mint g[L],ig[L],p2[L];\r\n\tif(g[0].v==0){\r\n\t\trep(i,L){\r\n\t\t\tmint w=-mint::root().pow(((mod-1)>>(i+2))*3);\r\n\t\t\tg[i]=w;\r\n\t\t\tig[i]=w.inv();\r\n\t\t\tp2[i]=mint(1<>=1){//input:[0,mod)\r\n\t\t\trep(i,b){\r\n\t\t\t\tuint x=f[i+b].v;\r\n\t\t\t\tf[i+b].v=f[i].v+mod-x;\r\n\t\t\t\tf[i].v+=x;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif(b>>=1){//input:[0,mod*2)\r\n\t\t\tmint p=1;\r\n\t\t\tfor(int i=0,k=0;i>=1){//input:[0,mod*3)\r\n\t\t\t\tmint p=1;\r\n\t\t\t\tfor(int i=0,k=0;i>=1){//input:[0,mod*4)\r\n\t\t\t\tmint p=1;\r\n\t\t\t\tfor(int i=0,k=0;i\r\nvoid inplace_fmt(vector&f,bool inv){\r\n\tinplace_fmt(si(f),f.data(),inv);\r\n}\r\n\r\ntemplate\r\nvoid half_fmt(const int n,mint*const f){\r\n\tstatic constexpr uint mod=mint::mod;\r\n\tstatic constexpr uint mod2=mod*2;\r\n\tstatic const int L=30;\r\n\tstatic mint g[L],h[L];\r\n\tif(g[0].v==0){\r\n\t\trep(i,L){\r\n\t\t\tg[i]=-mint::root().pow(((mod-1)>>(i+2))*3);\r\n\t\t\th[i]=mint::root().pow((mod-1)>>(i+2));\r\n\t\t}\r\n\t}\r\n\tint b=n;\r\n\tint lv=0;\r\n\tif(b>>=1){//input:[0,mod)\r\n\t\tmint p=h[lv++];\r\n\t\tfor(int i=0,k=0;i>=1){//input:[0,mod*2)\r\n\t\tmint p=h[lv++];\r\n\t\tfor(int i=0,k=0;i>=1){//input:[0,mod*3)\r\n\t\t\tmint p=h[lv++];\r\n\t\t\tfor(int i=0,k=0;i>=1){//input:[0,mod*4)\r\n\t\t\tmint p=h[lv++];\r\n\t\t\tfor(int i=0,k=0;i\r\nvoid half_fmt(vector&f){\r\n\thalf_fmt(si(f),f.data());\r\n}\r\n\r\n#ifdef USE_GOOD_MOD\r\n\r\ntemplate\r\nvc multiply(vc x,const vc&y,bool same=false){\r\n\tint n=si(x)+si(y)-1;\r\n\tint s=1;\r\n\twhile(s z(s);\r\n\t\trep(i,si(y))z[i]=y[i];\r\n\t\tinplace_fmt(z,false);\r\n\t\trep(i,s)x[i]*=z[i];\r\n\t}else{\r\n\t\trep(i,s)x[i]*=x[i];\r\n\t}\r\n\tinplace_fmt(x,true);x.resize(n);\r\n\treturn x;\r\n}\r\n\r\n#else\r\n\r\n//59501818244292734739283969-1=5.95*10^25 までの値を正しく計算\r\n//最終的な列の大きさが 2^24 までなら動く\r\n//最終的な列の大きさが 2^20 以下のときは,下の 3 つの素数を使ったほうが速い(は?)\r\n//VERIFY: yosupo\r\n//Yukicoder No980 (same=true)\r\nnamespace arbitrary_convolution{\r\n\t//constexpr modinfo base0{167772161,3};//2^25 * 5 + 1\r\n\t//constexpr modinfo base1{469762049,3};//2^26 * 7 + 1\r\n\t//constexpr modinfo base2{754974721,11};//2^24 * 45 + 1\r\n\textern constexpr modinfo base0{1045430273,3};//2^20 * 997 + 1\r\n\textern constexpr modinfo base1{1051721729,6};//2^20 * 1003 + 1\r\n\textern constexpr modinfo base2{1053818881,7};//2^20 * 1005 + 1\r\n\tusing mint0=modular;\r\n\tusing mint1=modular;\r\n\tusing mint2=modular;\r\n\ttemplate\r\n\tvc sub(const vc&x,const vc&y,bool same=false){\r\n\t\tint n=si(x)+si(y)-1;\r\n\t\tint s=1;\r\n\t\twhile(s z(s);rep(i,si(x))z[i]=x[i].v;\r\n\t\tinplace_fmt(z,false);\r\n\t\tif(!same){\r\n\t\t\tvc w(s);rep(i,si(y))w[i]=y[i].v;\r\n\t\t\tinplace_fmt(w,false);\r\n\t\t\trep(i,s)z[i]*=w[i];\r\n\t\t}else{\r\n\t\t\trep(i,s)z[i]*=z[i];\r\n\t\t}\r\n\t\tinplace_fmt(z,true);z.resize(n);\r\n\t\treturn z;\r\n\t}\r\n\ttemplate\r\n\tvc multiply(const vc&x,const vc&y,bool same=false){\r\n\t\tauto d0=sub(x,y,same);\r\n\t\tauto d1=sub(x,y,same);\r\n\t\tauto d2=sub(x,y,same);\r\n\t\tint n=si(d0);\r\n\t\tvc res(n);\r\n\t\tstatic const mint1 r01=mint1(mint0::mod).inv();\r\n\t\tstatic const mint2 r02=mint2(mint0::mod).inv();\r\n\t\tstatic const mint2 r12=mint2(mint1::mod).inv();\r\n\t\tstatic const mint2 r02r12=r02*r12;\r\n\t\tstatic const mint w1=mint(mint0::mod);\r\n\t\tstatic const mint w2=w1*mint(mint1::mod);\r\n\t\trep(i,n){\r\n\t\t\tull a=d0[i].v;\r\n\t\t\tull b=(d1[i].v+mint1::mod-a)*r01.v%mint1::mod;\r\n\t\t\tull c=((d2[i].v+mint2::mod-a)*r02r12.v+(mint2::mod-b)*r12.v)%mint2::mod;\r\n\t\t\tres[i].v=(a+b*w1.v+c*w2.v)%mint::mod;\r\n\t\t}\r\n\t\treturn res;\r\n\t}\r\n}\r\nusing arbitrary_convolution::multiply;\r\n\r\n#endif\r\n\r\nnamespace integer_convolution{\r\n\textern constexpr modinfo base0{1045430273,3};//2^20 * 997 + 1\r\n\textern constexpr modinfo base1{1051721729,6};//2^20 * 1003 + 1\r\n\tusing mint0=modular;\r\n\tusing mint1=modular;\r\n\ttemplate\r\n\tvc sub(const vi&x,const vi&y,bool same=false){\r\n\t\tint n=si(x)+si(y)-1;\r\n\t\tint s=1;\r\n\t\twhile(s z(s);rep(i,si(x))z[i]=x[i];\r\n\t\tinplace_fmt(z,false);\r\n\t\tif(!same){\r\n\t\t\tvc w(s);rep(i,si(y))w[i]=y[i];\r\n\t\t\tinplace_fmt(w,false);\r\n\t\t\trep(i,s)z[i]*=w[i];\r\n\t\t}else{\r\n\t\t\trep(i,s)z[i]*=z[i];\r\n\t\t}\r\n\t\tinplace_fmt(z,true);z.resize(n);\r\n\t\treturn z;\r\n\t}\r\n\tvi multiply(const vi&x,const vi&y,bool same=false){\r\n\t\tauto d0=sub(x,y,same);\r\n\t\tauto d1=sub(x,y,same);\r\n\t\tconst mint1 r=mint1(mint0::mod).inv();\r\n\t\tint n=si(d0);\r\n\t\tvi res(n);\r\n\t\trep(i,n){\r\n\t\t\tres[i]=d0[i].v+(r*(d1[i]-d0[i].v)).v*(ull)mint0::mod;\r\n\t\t}\r\n\t\treturn res;\r\n\t}\r\n}\r\n\r\n//最大で 1<\r\nvc large_convolution(const vc&a,const vc&b,int mx){\r\n\tint n=si(a),m=si(b);\r\n\tvc c(n+m-1);\r\n\tint len=1<<(mx-1);\r\n\tfor(int i=0;i(a.bg+i,a.bg+i+x),vc(b.bg+j,b.bg+j+y));\r\n\t\t\trep(k,si(d))\r\n\t\t\t\tc[i+j+k]+=d[k];\r\n\t\t}\r\n\t}\r\n\treturn c;\r\n}\r\n\r\n#ifndef DYNAMIC_MOD\r\nextern constexpr modinfo base{998244353,3};\r\n//extern constexpr modinfo base{1000000007,0};\r\n//modinfo base{1,0};\r\n#ifdef USE_GOOD_MOD\r\nstatic_assert(base.mod==998244353);\r\n#endif\r\n#else\r\nmodinfo base(1,0);\r\nextern constexpr modinfo base107(1000000007,0);\r\nusing mint107=modular;\r\n#endif\r\nusing mint=modular;\r\n\r\nconst int vmax=(1<<21)+10;\r\nmint fact[vmax],finv[vmax],invs[vmax];\r\nvoid initfact(){\r\n\tfact[0]=1;\r\n\trng(i,1,vmax){\r\n\t\tfact[i]=fact[i-1]*i;\r\n\t}\r\n\tfinv[vmax-1]=fact[vmax-1].inv();\r\n\tfor(int i=vmax-2;i>=0;i--){\r\n\t\tfinv[i]=finv[i+1]*(i+1);\r\n\t}\r\n\tfor(int i=vmax-1;i>=1;i--){\r\n\t\tinvs[i]=finv[i]*fact[i-1];\r\n\t}\r\n}\r\nmint choose(int n,int k){\r\n\treturn fact[n]*finv[n-k]*finv[k];\r\n}\r\nmint binom(int a,int b){\r\n\treturn fact[a+b]*finv[a]*finv[b];\r\n}\r\nmint catalan(int n){\r\n\treturn binom(n,n)-(n-1>=0?binom(n-1,n+1):0);\r\n}\r\n\r\n/*\r\nconst int vmax=110;\r\nmint binbuf[vmax][vmax];\r\nmint choose(int n,int k){\r\n\treturn binbuf[n-k][k];\r\n}\r\nmint binom(int a,int b){\r\n\treturn binbuf[a][b];\r\n}\r\nvoid initfact(){\r\n\tbinbuf[0][0]=1;\r\n\trep(i,vmax)rep(j,vmax){\r\n\t\tif(i)binbuf[i][j]+=binbuf[i-1][j];\r\n\t\tif(j)binbuf[i][j]+=binbuf[i][j-1];\r\n\t}\r\n}\r\n*/\r\n\r\n//UOJ Round18 B\r\n//UOJ Long Round C\r\nvvc convolute2d(const vvc&x,const vvc&y){\r\n\tint n1=si(x),m1=si(x[0]);\r\n\tint n2=si(y),m2=si(y[0]);\r\n\tint n=n1+n2-1,m=m1+m2-1;\r\n\tint s=1;\r\n\twhile(s p(s),q(s);\r\n\trep(i,n1)rep(j,m1)p[i*m+j]=x[i][j];\r\n\trep(i,n2)rep(j,m2)q[i*m+j]=y[i][j];\r\n\tinplace_fmt(p,false);\r\n\tinplace_fmt(q,false);\r\n\trep(i,s)p[i]*=q[i];\r\n\tinplace_fmt(p,true);\r\n\tvvc res(n,vc(m));\r\n\trep(i,n)rep(j,m)res[i][j]=p[i*m+j];\r\n\treturn res;\r\n}\r\n\r\n// f は k 次多項式を 0 から k で評価した値が入る\r\n//sum {0<=i<=inf} a^i f[i]\r\nmint exp_poly_sum_limit(const mint a,const vc&f){\r\n\tassert(a!=1);\r\n\tint k=si(f)-1;\r\n\tif(a==0){\r\n\t\treturn f[0];\r\n\t}else{\r\n\t\tvc g(k+1);\r\n\t\t{\r\n\t\t\tmint r=1;\r\n\t\t\trep(i,k+1){\r\n\t\t\t\tg[i]=f[i]*r;\r\n\t\t\t\tr*=a;\r\n\t\t\t}\r\n\t\t}\r\n\t\tmint c;\r\n\t\t{\r\n\t\t\tmint w,r=1;\r\n\t\t\trep(i,k+1){\r\n\t\t\t\tw+=choose(k+1,i)*r;\r\n\t\t\t\tr*=-a;\r\n\t\t\t\tc+=g[k-i]*w;\r\n\t\t\t}\r\n\t\t\tc/=mint(1-a).pow(k+1);\r\n\t\t}\r\n\t\treturn c;\r\n\t}\r\n}\r\n\r\n//exp(ax)*f の k 次の係数に k! をかけたもの\r\n//の sum が求まってしまう\r\nmint exp_gen_coeff_sum(mint a,vc f){\r\n\t/*int n=si(f)-1;\r\n\tvc g(fact,fact+2*n+1);\r\n\treverse(all(f));\r\n\tauto h=multiply(f,g);\r\n\tvc vs(n+1);\r\n\trep(i,n+1)vs[i]=h[n+i]*finv[i];\r\n\treturn exp_poly_sum_limit(a,vs);*/\r\n\tmint ans;\r\n\tmint w=(1-a).inv(),cur=1;\r\n\trep(k,si(f)){\r\n\t\tcur*=w;\r\n\t\tans+=fact[k]*cur*f[k];\r\n\t}\r\n\treturn ans;\r\n}\r\n\r\nint getsz(const vvc&x){\r\n\treturn si(x)+si(x[0]);\r\n}\r\n\r\nvoid slv(){\r\n\tint n,k;cin>>n>>k;\r\n\tvi ls=readvi(n);\r\n\tint tot=accumulate(all(ls),0);\r\n\tvc> buf(n);\r\n\trep(idx,n){\r\n\t\tint s=(ls[idx]-1)/k+1;\r\n\t\tvvc f(s,vc(2));\r\n\t\trep(num,s){\r\n\t\t\tmint w=mint(ls[idx]-k*num)/tot;\r\n\t\t\tif(num)f[num][0]=w.pow(num-1)*finv[num-1];\r\n\t\t\tf[num][1]=w.pow(num)*finv[num];\r\n\t\t}\r\n\t\tbuf[idx].swap(f);\r\n\t}\r\n\tpriority_queue,greater> pq;\r\n\trep(i,n)pq.emplace(getsz(buf[i]),i);\r\n\trep(_,n-1){\r\n\t\tint i=pq.top().b;pq.pop();\r\n\t\tint j=pq.top().b;pq.pop();\r\n\t\tbuf[i]=convolute2d(buf[i],buf[j]);\r\n\t\tvvc().swap(buf[j]);\r\n\t\tpq.emplace(getsz(buf[i]),i);\r\n\t}\r\n\tconst auto&ans=buf[pq.top().b];\r\n\tmint sum;\r\n\trng(num,1,si(ans)){\r\n\t\tvc f(num+1);\r\n\t\tassert(si(ans[num])==n+1);\r\n\t\trep(i,n+1){\r\n\t\t\tif(i+num-n>=0)f[i+num-n]=ans[num][i];\r\n\t\t\telse assert(ans[num][i]==0);\r\n\t\t}\r\n\t\tmint w=mint(tot-num*k)/tot;\r\n\t\tmint val=exp_gen_coeff_sum(w,f);\r\n\t\tif(num%2==0)val=-val;\r\n\t\tsum+=val;\r\n\t}\r\n\tprint(sum);\r\n}\r\n\r\nsigned main(){\r\n\tcin.tie(0);\r\n\tios::sync_with_stdio(0);\r\n\tcout<>t;rep(_,t)\r\n\tslv();\r\n}\r\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "combinatorics", "fft"], "code_uid": "dca0ade26aea2000371682903ec63446", "src_uid": "26d565c193a5920b042c783109496b4c", "difficulty": 3500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nint MOD;\n\nint64_t power_mod(int64_t num, int power){\n int64_t prod = 1;\n num %= MOD;\n while(power > 0){\n if(power&1) prod = prod * num % MOD;\n num = num * num % MOD;\n power >>= 1;\n }\n return prod;\n}\n\nint64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){\n int64_t d = a;\n if(b != 0){\n d = extgcd(b, a%b, y, x);\n y -= (a/b) * x;\n }else{\n x = 1; y = 0;\n }\n return d;\n}\n\nint64_t inv_mod(int64_t a, int mod){\n int64_t x, y;\n extgcd(a, mod, x, y);\n return (mod + x%mod) % mod;\n}\n\nint64_t mod(int64_t a, int64_t m) {\n return (a % m + m) % m;\n}\n\npair crt(int64_t b1, int64_t m1, int64_t b2, int64_t m2){\n int64_t p, q;\n int64_t d = extgcd(m1, m2, p, q);\n if((b2-b1) % d != 0) return {-1, -1};\n int64_t m = m1 * (m2/d);\n int64_t tmp = (b2 - b1) / d * p % (m2/d);\n int64_t r = mod(b1 + m1 * tmp, m);\n return make_pair(r, m);\n}\n\nvector> prime_division(int n){\n vector> ret;\n for(int i=2; i*i<=n; i++){\n int cnt = 0;\n while(n % i == 0){\n n /= i;\n cnt++;\n }\n if(cnt) ret.emplace_back(i, cnt);\n }\n if(n > 1) ret.emplace_back(n, 1);\n return ret;\n}\n\nvector> primes;\nint64_t ppow[10];\nvector> fact[10];\nvoid add(int64_t& a, int64_t b, int mod){\n a = (a+b) % mod;\n}\nvoid mul(int64_t& a, int64_t b, int mod){\n a = a*b % mod;\n}\n\nvoid prepare_mod(){\n primes = prime_division(MOD);\n int sz = primes.size();\n for(int t=0; t= p.second) return 0;\n int64_t v = a.first;\n mul(v, inv_mod(b.first, pp), pp);\n mul(v, inv_mod(c.first, pp), pp);\n while(pw--) mul(v, p.first, pp);\n return v;\n}\n\nint64_t calc(int L, int N){\n pair ans = {0, 1};\n for(int pi=0; pi> N >> MOD >> L >> R;\n\n prepare_mod();\n\n int ans = (MOD + calc(L, N) - calc(R+1, N)) % MOD;\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics", "number theory"], "code_uid": "7ed81667b9688763588b7568b6f759d3", "src_uid": "6ddc487029785738679007443fc08463", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\n\nstruct node {\n pair f, s;\n node *le, *ri;\n ll mn, res;\n int tl, tr;\n node() {\n mn = 228;\n }\n node(int l, int r) {\n tl = l; tr = r;\n mn = 228;\n if (l == r) return;\n le = new node(l, (l+r)/2);\n ri = new node((l+r)/2+1, r);\n }\n node (node* a, node* b) {\n le = a; ri = b;\n tl = le->tl; tr = ri->tr;\n if (le->mn == 228) {\n res = ri->res;\n mn = ri->mn;\n f = ri->f;\n s = ri->s;\n return;\n }\n if (ri->mn == 228) {\n res = le->res;\n mn = le->mn;\n f = le->f;\n s = le->s;\n return;\n }\n f = le->f; s = ri->s;\n ll del = 1ll*le->s.second*(ri->f.first-le->s.first);\n res = le->res+del+ri->res;\n mn = min(le->mn, le->res+del);\n mn = min(mn, ri->mn+le->res+del);\n }\n void combine() {\n node tmp(le, ri);\n *this = tmp;\n }\n void update(int id, int time, int speed) {\n if (tl == tr) {\n mn = res = 0;\n f.first = s.first = time;\n f.second = s.second = speed;\n return;\n }\n if (id <= (tl+tr)/2)\n le->update(id, time, speed);\n else\n ri->update(id, time, speed);\n combine();\n };\n void del(int id) {\n if (tl == tr) {\n mn = 228;\n return;\n }\n if (id <= (tl+tr)/2)\n le->del(id);\n else\n ri->del(id);\n combine();\n }\n node* get_seg(int l, int r) {\n if (tr < l || r < tl) {\n return new node();\n }\n if (l <= tl && tr <= r) {\n return this;\n }\n return new node(le->get_seg(l, r), ri->get_seg(l, r));\n };\n long double simulate(int r, ll v) {\n if (mn == 228) { return -1; }\n if (v+mn > 0 && v+res+1ll*s.second*(r-s.first) > 0) {\n return -1;\n }\n if (f == s) {\n return s.first - (long double)v/s.second;\n }\n if (le->mn == 228) {\n return ri->simulate(r, v);\n }\n long double to = le->simulate(le->s.first, v);\n if (to != -1) {\n return to;\n }\n v += le->res;\n ll del = 1ll*le->s.second * ((ri->mn == 228? r : ri->f.first)-le->s.first);\n if (v+del <= 0) {\n return le->s.first - (long double)v/le->s.second;\n }\n v += del;\n return ri->simulate(r, v);\n };\n long double query(int l, int r, int rr, ll v) {\n node* t = get_seg(l, r);\n return t->simulate(rr, v);\n };\n};\nstruct query {\n int time, speed, start;\n int l, r, type;\n query() {}\n};\n\nvector pos;\nvector q;\n\nint main() {\n int qq;\n cout.precision(7);\n cin >> qq;\n q.resize(qq);\n \n for (int i = 0; i < qq; ++i) {\n cin >> q[i].type;\n if (q[i].type == 1) {\n cin >> q[i].time >> q[i].speed;\n pos.push_back(q[i].time);\n }\n if (q[i].type == 2) {\n cin >> q[i].time;\n }\n if (q[i].type == 3) {\n cin >> q[i].l >> q[i].r >> q[i].start;\n }\n }\n sort(pos.begin(), pos.end());\n pos.erase(unique(pos.begin(), pos.end()), pos.end());\n if (pos.size() == 0) {\n pos.push_back(0);\n }\n node* t = new node(0, pos.size()-1);\n for (int i = 0; i < qq; ++i) {\n if (q[i].type == 1) {\n int id = lower_bound(pos.begin(), pos.end(), q[i].time) - pos.begin();\n t->update(id, q[i].time, q[i].speed);\n }\n if (q[i].type == 2) {\n int id = lower_bound(pos.begin(), pos.end(), q[i].time) - pos.begin();\n t->del(id);\n }\n if (q[i].type == 3) {\n int l = lower_bound(pos.begin(), pos.end(), q[i].l) - pos.begin();\n int r = --upper_bound(pos.begin(), pos.end(), q[i].r) - pos.begin();\n if (q[i].start == 0) {\n cout << fixed << q[i].l << endl;\n } else {\n cout << fixed << t->query(l, r, q[i].r, q[i].start) << endl;\n }\n }\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["data structures"], "code_uid": "9a8a4b1721a3c0742e8c3b303bf3abc4", "src_uid": "23440d055baf6c831e91e76b99b9ada8", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\nusing namespace std;\n\ntypedef long long LL;\n\nconst int maxn = 2e5 + 10;\nconst LL mod = 1e9 + 7;\n\nint n, q;\nLL A[maxn], W[maxn];\nLL B[2][maxn<<1];\n\nLL Sum(int f, int i){\n LL res = 0;\n while(i){\n res += B[f][i];\n if(f) res = (res % mod + mod) % mod;\n i -= i & -i;\n }\n return res;\n}\n\nvoid Add(int f, int i, LL x){\n while(i < maxn){\n B[f][i] += x;\n if(f) B[f][i] = (B[f][i] % mod + mod) % mod;\n i += i & -i;\n }\n}\n\nLL gets(int x, int y, int f){\n if(f == 0) return y >= x ? Sum(f, y) - Sum(f, x - 1) : 0;\n return (Sum(f, y) - Sum(f, x - 1) + mod) % mod;\n}\n\nint get_pos(int x, int y){\n int lb = x, rb = y;\n while(lb < rb){\n int mid = (lb + rb) >> 1;\n if(gets(x, mid, 0) >= gets(mid + 1, y, 0)) rb = mid;\n else lb = mid + 1;\n }\n return rb;\n}\n\nLL solve(LL x, LL y){\n if(x == y) return 0;\n int p = get_pos(x, y);\n\n LL res = 0;\n res = (res + gets(x, p, 0) % mod * (A[p] - p) % mod) % mod;\n res = (res - gets(x, p, 1) + mod) % mod;\n res = (res - gets(p, y, 0) % mod * (A[p] - p) % mod + mod) % mod;\n res = (res + gets(p, y, 1)+ mod) % mod;\n\n return (res % mod + mod) % mod;\n}\n\nint main(){\n// freopen(\"in.txt\", \"r\", stdin);\n\n scanf(\"%d %d\", &n, &q);\n for(int i = 1; i <= n; i++){\n scanf(\"%lld\", A + i);\n }\n for(int i = 1; i <= n; i++){\n scanf(\"%lld\", W + i);\n Add(0, i, W[i]);\n Add(1, i, W[i] * (A[i] - i) % mod);\n }\n\n while(q--){\n LL x, y;\n scanf(\"%lld %lld\", &x, &y);\n\n if(x < 0){\n x = -x;\n Add(0, x, -W[x]);\n Add(1, x, -W[x] * (A[x] - x));\n W[x] = y;\n Add(0, x, W[x]);\n Add(1, x, W[x] * (A[x] - x));\n }else{\n printf(\"%lld\\n\", solve(x, y));\n }\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["data structures"], "code_uid": "1965aaefb7f2eb3220bd96988cb23c22", "src_uid": "c0715f71efa327070eba7e491856d66a", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define ll long long\n#define mod 1000000007\n#define rep(i,n)\tfor(int i=1;i<=n;i++)\n#define pb push_back\nint main()\n{\n\tll r,t=0;\n\tcin>>r;\n\tif(r<=3)\n\t\tcout<<\"NO\";\n\telse\n\t{\n\t\trep(i,sqrt(r))\n\t\t{\n\t\t\tll x = i;\n\t\t\tll sum = (x*x)+x+1;\n\t\t\tll num = r-sum;\n\t\t\tll val = num/(2*x);\n\t\t\tif((val*2*x)==num)\n\t\t\t{\n\t\t\t\tt=1;\n\t\t\t\tcout<\nusing namespace std;\n#define ll long long int \nint main()\n{\n\tll x,y,n,i,j,a=-1,b;\n\tcin>>x>>y>>n;\n\tfor(i=1;i<=n;i++)\n\t{\n\t\tll j=i*x/y;\n\t\tif(i*x-j*y>(j+1)*y-i*x)j++;\n\t\tif((a==-1)||(abs(j*y-i*x))*b<(abs(a*y-b*x))*i)\n\t\ta=j,b=i;\n\t}\n\tcout<\n#include\n#include\n#include\n#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\n#define ll\t\t\t\tlong long\n#define MP\t\t\t\tmake_pair\n#define ull\t\t\t\tunsigned long long\n#define vint\t\t\tvector\n#define vid\t\t\t\tvector\n#define vll\t\t\t\tvector\n#define pb\t\t\t\tpush_back\n#define PI\t\t\t\t3.1415926536\n#define all(v)\t\t\t((v).begin()),((v).end())\n#define rall(v)\t\t\t((v).rbegin()),((v).rend())\n#define sz(v)\t\t\t( (int) (v.size()) )\n#define rep(i,n)\t\tfor (int i=0;i=0;i--)\n#define MOD\t\t\t\t1000000007\n#define ss\t second\n#define ff\t first\nint dx[]={0 , 0 , -1 ,1};\nint dy[]={-1, 1 , 0 ,0};\n\nvoid fast()\n{\n\tstd::ios_base::sync_with_stdio(0);\n\tcin.tie(NULL);\n\tcout.tie(NULL);\n}\nll a=0 , b=1; // 0/1=0 initial fraction\nll x , y , n ;\nvoid compare(ll dn , ll i)\n{\n\tll s1=abs( (b*y)*( (x*i)-(y*dn) ) );\n\tll s2=abs( (y*i)*( (b*x)-(a*y ) ) );\n\tif(s1>x>>y>>n;\n\tfor(ll i=1;i<=n;i++)\n\t{\n\t\tll d1, d2;\n\t\td1=i*x/y;\n\t\td2=d1+1;\n\t\tcompare(d1,i);\n\t\tcompare(d2,i);\n\t}\n\tcout<\n#include \n#include \n#include \n#include\n#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define prfs(x) printf(\"%s\\n\",x.c_str())\n#define prfi(x) printf(\"%d\\n\",x);\n#define sfi(X) scanf(\"%d\",&X);\n#define lop(i,n) for (int i = 0 ; i < n ;i++)\n#define blop(i,n) for(int i = n-1 ; i>=0;i--)\n#define M_P make_pair\n#define All(X) (X).begin(),(X).end()\n#define SZ(n) (int)(n).size()\n#define voi vector\n#define Allr(X) (X).rbegin(),(X).rend()\n#define vos vector\n#define vob vector\n#define vovi vector >\n#define vob vector\n#define ll long long \nusing namespace std;\n\n\nint _gcd(int a, int b)\n{\n\treturn !b ? a : _gcd(b, a%b);\n}\nint main()\n{\n\t\n\tint x, y, limit;\n\tscanf(\"%d%d%d\", &x, &y, &limit);\n\tdouble _MAX = 1e9;\n\tdouble _B = ((double)x) / y;\n\tint _Answer1, _Answer2;\n\tfor (int i = 1; i <= limit; i++)\n\t{\n\t\tint b = (1LL * x * i) / y;\n\t\tdouble _A = ((double)b) / i;\n\t\tdouble _A1 = ((double)(abs(b - 1))) / i;\n\t\tdouble _A2 = ((double)(b + 1)) / i;\n\t\tif (fabs(_B - _A) < _MAX)\n\t\t{\n\t\t\t_Answer1 = b, _Answer2 = i;\n\t\t\t_MAX = fabs(_B - _A);\n\t\t}\n\t\tif (fabs(_B - _A1) < _MAX)\n\t\t{\n\t\t\t_Answer1 = abs(b-1), _Answer2 = i;\n\t\t\t_MAX = fabs(_B - _A1);\n\t\t}\n\t\tif (fabs(_B - _A2) < _MAX)\n\t\t{\n\t\t\t_Answer1 = b+1, _Answer2 = i;\n\t\t\t_MAX = fabs(_B - _A2);\n\t\t}\n\t}\n\tif (fabs(1.0 - _B) <= (fabs((double)_Answer1 / _Answer2) - _B) + 1e-9 )\n\t\t_Answer1 = 1, _Answer2 = 1;\n\tint GcD = _gcd(_Answer1, _Answer2);\n\tprintf(\"%d/%d\\n\", _Answer1 / GcD, _Answer2 / GcD);\n\n\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "8706162bd8ff474bc167d97e15784ba9", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define endl '\\n'\nusing namespace std;\n\ntypedef long long ll;\n\nstruct Fraction {\n ll a, b;\n};\n\npair geta(int x, int y, int b) {\n ll lower = (ll)b * x / y;\n ll upper = ((ll)b * x + (y-1)) / y;\n return {lower, upper};\n}\n\nint gcd(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\n\nvoid solve(int x, int y, int n) {\n vector cands;\n for (int b = 1; b <= n; b++) {\n auto ap = geta(x, y, b);\n cands.push_back({ap.first, b});\n cands.push_back({ap.second, b});\n }\n\n auto comp = [&](Fraction fa, Fraction fb)-> bool {\n ll a1 = fa.a;\n ll b1 = fa.b;\n ll a2 = fb.a;\n ll b2 = fb.b;\n\n ll left = abs(x * b1 * b2 - a1 * b2 * y);\n ll right = abs(x * b1 * b2 - a2 * b1 * y);\n\n if (left < right) return true;\n else if (left > right) return false;\n\n if (b1 < b2) return true;\n else if (b1 > b2) return false;\n\n if (a1 < a2) return true;\n return false;\n };\n\n sort(cands.begin(), cands.end(), comp);\n\n //cout << (double)x / y << endl;\n //cout << comp({0, 1}, {2, 5}) << endl;\n //for (auto f : cands) {\n // cout << f.a << \"/\" << f.b << \" \" << (double)f.a / f.b << endl;\n //}\n\n cout << cands.front().a << \"/\" << cands.front().b << endl;\n}\n\nint main(){\n std::ios_base::sync_with_stdio(false);\n cin.tie(0);\n int x, y, n;\n cin >> x >> y >> n;\n\n solve(x, y, n);\n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "df7f53a21712888c68b29216dd3a415c", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n\nlong double eps=0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;\nlong double diff=LONG_MAX;\n\n\n\nint main()\n{\n // cout << \"Hello World!\" << endl;\n ios::sync_with_stdio(false);\n pair ans;\n long long x,y,n,a,b;\n cin>>x>>y>>n;\n long double xbyy=(long double)x/(long double)y;\n// cout<eps){\n ans.first=a1;\n ans.second=b;\n diff=diff1;\n }\n \n if(diff-diff2>eps){\n ans.first=a2;\n ans.second=b;\n diff=diff2;\n }\n // cout<\n\nusing namespace std;\n\n#ifdef ACMTUYO\nstruct RTC{~RTC(){cerr << \"Time: \" << clock() * 1.0 / CLOCKS_PER_SEC <<\" seconds\\n\";}} runtimecount;\n#define DBG(X) cerr << #X << \" = \" << X << '\\n';\n#else\nstruct RTC{};\n#define DBG(X)\n#endif\n\n#define fast_io() ios_base::sync_with_stdio(false)\n#define mp make_pair\n#define mt make_tuple\n#define pb push_back\n#define eb emplace_back\n#define sz(x) ((int)(x).size())\n#define all(c) (c).begin(),(c).end()\n#define forn(i, n) for (int i = 0; i < (n); i++)\ntypedef long long int ll;\n\nint main() {\n ll x,y,n;\n scanf(\"%lld %lld %lld\",&x,&y,&n);\n ll sn=10000000,sd=1;\n for(int den=1;den<=n;den++) {\n double ap=double(x*den)/double(y);\n ll num=int(ap);\n if(ap-double(num)>0.5)num++;\n //cout<(y*sd)*llabs(x*den-num*y)) {\n sn=num;sd=den;\n }\n }\n printf(\"%d/%d\\n\",int(sn),int(sd));\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "30ee1c0c15607fd48a8e42b78387a216", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n\nusing namespace std;\n\nlong long xx = -1, yy, a, b, n;\n\nint main(int argc, char **argv) {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"a.in\", \"r\", stdin);\n#endif\n\tscanf(\"%lld%lld%lld\", &a, &b, &n);\n\tfor(long long y=1 ; y<=n ; ++y){\n\t\tlong long x = y*a/b;\n\t\tif(xx == -1) xx = x, yy = y;\n\t\tif(abs(a*y - b*x)*yy < abs(a*yy - b*xx)*y) xx = x, yy = y;\n\t\t++x;\n\t\tif(abs(a*y - b*x)*yy < abs(a*yy - b*xx)*y) xx = x, yy = y;\n\t}\n\tprintf(\"%lld/%lld\\n\", xx, yy);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "a08e6a02751ad0b244bb65f588a7fa09", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\ntypedef long long int ll;\ntypedef long double ld;\n#define pll pair\n#define pb push_back\n#define F first\n#define S second\n#define I insert\n#define vll vector\n#define vbl vector\n#define vvll vector\n#define vld vector\n#define vb vector\n#define vc vector\n#define vpll vector\n#define all(x) (x).begin(), (x).end()\n\ntemplate ostream& operator<<(ostream &os, vector V){\n\tos << \"[ \";\n\tfor(auto v : V) os << v << \" \";\n\treturn os << \" ]\";\n}\ntemplate ostream& operator<<(ostream &os, set V){\n\tos << \"[ \";\n\tfor(auto v : V) os << v << \" \";\n\treturn os << \" ]\";\n}\ntemplate ostream& operator<<(ostream &os, map V){\n\tos << \"[ \";\n\tfor(auto v : V) os << \"(\" << v.first << \":\" << v.second << \")\";\n\treturn os << \" ]\";\n}\ntemplate ostream& operator<<(ostream &os, pair V){\n\treturn os << \"(\" << V.first << \",\" << V.second << \")\";\n}\n\n#define cerr cout\n#define TRACE\n#ifdef TRACE\n #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)\n template \n void __f(const char* name, Arg1&& arg1){\n cerr << name << \" : \" << arg1 << endl;\n }\n template \n void __f(const char* names, Arg1&& arg1, Args&&... args){\n const char* comma = strchr(names + 1, ',');\n cerr.write(names, comma - names) << \" : \" << arg1<<\" | \";\n __f(comma+1, args...);\n }\n clock_t clk=clock();\n #define checktime() cout<<\"\\nTime Elapsed : \"<>x>>y>>n;\n ll a,b;\n ld val=1ll<<60;\n for(ll i=1;i<=n;i++){\n if(val>min((i*x)%y,y-(i*x)%y)*1.0/i){\n val=min((i*x)%y,y-(i*x)%y)*1.0/i;\n b=i;\n a=((i*x)%y<=y-(i*x)%y)?i*x/y:i*x/y+1;\n }\n }\n cout<>t;\n while(t--){\n solve();\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "3a1282b57985ca2f32c62451bb8263ae", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#define INF 100000000\nusing namespace std;\nlong long int fn,fd;\nlong long int ra,rb;\nlong long int gcd(long long int a, long long int b)\n{\n if (b==0)\n return a;\n else\n return gcd(b,a%b);\n}\n//this is a void fucnction \n \nvoid function (long long int x, long long int y, long long int b, long long int a)\n{\n if (a < 0) {\n return;\n }\n long long int kkk = gcd (a, b);\n a = a / kkk; b = b/kkk;\n long long int kk,g,n,d;\n n = abs(b*x - a*y);\n d = b*y;\n g = gcd (n, d);\n n = n/g; d = d/g;\n //cout << x << \" \" << y << \" \" << a << \" \" << b << endl;\n if (n*fd < fn*d) {\n fn = n;\n fd = d;\n ra = a;\n rb = b;\n return;\n }\n if (n*fd == d*fn) {\n if (b < rb) {\n ra = a;\n rb = b;\n return;\n }\n if (b > rb) {\n return;\n }\n if (a < ra) {\n ra = a;\n rb = b;\n return;\n }\n }\n}\nint main()\n{\n fn = 100000; fd = 1;\n long long int x,i,y,n;\n cin >> x >> y >> n;\n int j;\n for ( int i = 0; i < 19; i++ ) {\n j = i;\n }\n for (i = 1; i <= n; i++) {\n function (x, y, i, (i*x)/y);\n function (x, y, i, (i*x)/y + 1);\n function (x, y, i, (i*x)/y - 1);\n }\n cout << ra << \"/\" << rb << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "5aa3ddfc0ef95cdbce2c5198fb7ffa6c", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\nlong double absolute(long double a)\n{\n return a < 0 ? a * - 1 : a;\n}\n\nint main(){\n int a,b,n;\n cin >> a >> b >> n;\n int x,y0,y1;\n long double r = 1e100;\n int ansx=0,ansy=0;\n for(int i = 1; i <= n; ++i)\n {\n x = i;\n y1 = (double)(a)/b * i;\n y0 = (double)(a)/b * i+1;\n if(r > absolute((long double)(a)/b-(long double)(y1)/x))\n {\n ansx = y1, ansy = x;\n r = absolute((long double)(a)/b-(long double)(y1)/x);\n }\n if(r > absolute((long double)(a)/b-(long double)(y0)/x))\n {\n ansx = y0, ansy = x;\n r = absolute((long double)(a)/b-(long double)(y0)/x);\n }\n }\n cout << ansx << \"/\" << ansy << endl;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "8f3dff948964a87325e64c07572507f2", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\nint main(){\n long long x,y,n,a=-1,b,i,j;\n cin>>x>>y>>n;\n for(i=1;i<=n;i++){\n j=i*x/y;\n if(i*x-j*y>(j+1)*y-i*x) j++;\n if(a==-1||abs(a*y-x*b)*i>abs(j*y-x*i)*b) a=j,b=i;\n }\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define mk make_pair\n#define pb push_back\n#define F first\n#define S second\n#define m0(x) memset(x,0,sizeof(x))\n#define maxn 111111\nusing namespace std;\ntypedef long long ll;\ntypedef float f;\ntypedef long double ld;\nll i,n,x,y;\n\nll gcd(int x,int y){\n int p = x % y;\n x = y;\n y = p;\nif(y > 0)x = gcd(x,y);\nreturn x;\n}\n\nll lcm(int x,int y){\nreturn ll(x) * ll(y) / gcd(x,y);\n}\n\nld func(int x1,int y1,int x2,int y2){\nll s = lcm(y1,y2);\nll s1 = abs(s / y1 * x1 - s / y2 * x2);\nreturn ld(s1) / ld(s);\n}\n\nint main(){\n#ifndef ONLINE_JUDGE \n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\ncin >> x >> y >> n;\nll a = 0,b = 1; \nld t1 = ld(x) / ld(y),h = 0;\n for(i=1;i<=n;i++){\n ll l = 1,r = 1000000,ave;\n while(r - l > 1){\n ave = (l + r) / 2;\n\tld t2 = ld(ave) / ld(i);\n if(t2 < t1)l = ave;\n\telse r = ave;\n }\n ave = l;\n if((func(l,i,x,y) > func(r,i,x,y)))ave = r;\n if(func(ave,i,x,y) < func(a,b,x,y)){\n a = ave;\n\tb = i;\n }\n }\ncout << a << \"/\" << b;\nreturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "e466650fcbd16f23404bd584fdfe78ab", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n#define _ ios_base::sync_with_stdio(0);cin.tie(0);\n#define forex(i,j) for(int i=0;i<(j);i++) // 0 .. N-1\n#define forin(i,j) for(int i=0;i<=(j);i++) // 0 .. N\n#define printv(v) {for(int i=0;i pii;\n\ntypedef vector vi;\ntypedef vector vs;\ntypedef vector vll;\ntypedef vector vpii;\n\n#define mp make_pair\n#define pb push_back\n#define uset unordered_set\n#define umap unordered_map\n#define mset multiset\n#define mmap multimap\n#define umset unordered_multiset\n#define ummap unordered_multimap\n#define all(v) (v).begin(),(v).end()\n\nint xx[]={0,1,0,-1,-1,-1,1,1,-1};int yy[]={1,0,-1,0,1,1,-1,-1}; //E S W N NE SE SW NW\n\nvoid vin(vi &v,int n){\n int temp;\n while(n--){\n cin>>temp;\n v.pb(temp);\n }\n}\nint gcd(int a,int b){\n if(a==0)return b;\n else return gcd(b%a,a);\n}\nint main() {\n_\ndouble eps=0.000000000000001;\nint x,y,nn;cin>>x>>y>>nn;\ndouble a=(double)x/y;\n//if(y<=nn)cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n //freopen (\"input.txt\", \"r\", stdin);\n //freopen (\"output.txt\", \"w\", stdout);\n long long a, b, n, i, bx, tx, by, tbi, bbi = 1, eps = 1000000000ll;\n\n cin >> a >> b >> n;\n\n for (i = 1; i <= n; i++)\n {\n tx = (i * a) / b;\n\n for (int j = -5; j <= 5; j++)\n {\n long long t = tx + j;\n tbi = b * i;\n\n if (abs(b * t - a * i) * bbi < eps * tbi)\n eps = abs(b * t - a * i), bx = t, by = i, bbi = tbi;\n }\n }\n\n cout << bx << '/' << by;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "63b774f95d63725cefdce063a6e57ac3", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)\ntypedef long long ll;\ntypedef long double ld;\nstd::map counter;\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tld x,y,n,ansx,ansy,m=INT_MAX;\n\tcin>>x>>y>>n;\n\tld num;\n\tFOR(i,1,n+1)\n\t{\n\t\tnum=ll((x*i)/y);\n\t\tif(abs(x/y-num/i)\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define lld long long int\n#define dbg1(vari1) cerr<<#vari1<<\" = \"<<(vari1)<> a >> b >> n;\n\t// if(n>=b)\n\t// {\n\t// \tcout << a/gcd(a,b) << \"/\" << b/gcd(a,b);\n\t// \treturn 0;\n\t// }\n\tint x,y;\n\tlong double dif=1.0/0.0;\n\t// dbg1(dif);\n\tfor(lld j=1;j<=n;j++)\n\t{\n\t\tfor(lld i=(j*a)/b-1;i<=((j*a)/b)+1;i++)\n\t\t{\n\t\t\t// dbg3(fabs((long double)i/j-(long double)a/b),i,j);\n\t\t\tif(fabs((long double)i/j-(long double)a/b)\nusing namespace std;\nint main(void)\n{\n long long i,j,k,m,n,a,b,x,y;\n float min,mi;\n while(cin>>x>>y>>n)\n {\n if(y<=n)\n {\n a=x;\n b=y;\n }\n else\n {\n min=1;\n for(i=1;i<=n;i++)\n {\n k=i*x/y;\n mi=(double)x/y-(double)k/i;\n if(mib)\n {\n m=a;\n n=b;\n }\n else\n {\n m=b;\n n=a;\n }\n if(n==0) m=1;\n else\n {\n while(n)\n {\n j=m%n;\n m=n;\n n=j;\n }\n }\n if(j!=1)\n {\n a=a/m;\n b=b/m;\n }\n cout<\n#include\nusing namespace std;\nusing namespace __gnu_cxx;\n\nconst double EPS = 1e-15;\n\nint main() {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.txt\", \"rt\", stdin);\n#endif\n\tint x, y, n;\n\tscanf(\"%d%d%d\", &x, &y, &n);\n\tlong long aout, bout;\n\tdouble mn = 1e18;\n\tfor (long long b = 1; b <= n; ++b) {\n\t\tlong long a1 = b * x / y;\n\t\tlong long a2 = (b * x + y - 1) / y;\n\t\tdouble diff1 = fabs(a1 * 1.0 / b - x * 1.0 / y);\n\t\tdouble diff2 = fabs(a2 * 1.0 / b - x * 1.0 / y);\n\t\tif (diff1 < mn && fabs(diff1 - mn) > EPS) {\n\t\t\tmn = diff1;\n\t\t\taout = a1, bout = b;\n\t\t}\n\t\tif (diff2 < mn && fabs(diff2 - mn) > EPS) {\n\t\t\tmn = diff2;\n\t\t\taout = a2, bout = b;\n\t\t}\n\t}\n\tprintf(\"%lld/%lld\", aout, bout);\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "07390c54b6f1e549d1b3e5489ea016b2", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\n\nll x, y, n;\n\nint main() {\n\n\tcin >> x >> y >> n;\n\t\n\tll a, b, ta;\n\tdouble mn = 214748364.0f, t;\n\n\tfor (ll tb = 1; tb <= n; tb++) {\n\t\tta = (tb * x) / y;\n\t\tt = abs(tb*x - ta*y) / (y*(tb+0.0));\n\t\t\n\t\tif (t < mn) {\n\t\t\ta = ta, b = tb;\n\t\t\tmn = t;\n\t\t}\n\t\t\n\t\tta++;\n\t\tt = abs(tb*x - ta*y) / (y*(tb+0.0));\n\t\tif (t < mn) {\n\t\t\ta = ta, b = tb;\n\t\t\tmn = t;\n\t\t}\n\t}\n\n\tll l = (ll)sqrt(max(a, b) + 0.0);\n\tfor (ll i = 2; i < l; i++)\n\t\twhile (a % i == 0 && b % i == 0)\n\t\t\ta /= i, b /= i;\n\n\tcout << a << '/' << b << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "8aef3bd198434e0b63b041b99163a23b", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#define eps 1e-15\nusing namespace std;\nint main()\n{\nint i,a,b,x,y,n;\ndouble t,t1;\ncin>>x>>y>>n;\nt1=((double)x)/y;\nt=1+(double)x/y;\nfor (i=1;i<=n;i++)\n{\nif (((abs((double)(int)(t1*i)/i-t1))>t-eps)&&((abs((double)(int)(t1*i)/i-t1))t-eps)&&((abs((double)(int)(t1*i+1)/i-t1))\n#include \nusing namespace std;\nint main(){\n long long x,y,n,a=-1,b,i,j;\n cin>>x>>y>>n;\n for(i=1;i<=n;i++){\n j=i*x/y;\n if(i*x-j*y>(j+1)*y-i*x) j++;\n if(a==-1||abs(a*y-x*b)*i>abs(j*y-x*i)*b) a=j,b=i;\n }\n cout<\n #include \n #include \nusing namespace std;\n\n \nint main()\n{ long double x,y;\n int n ;\n cin>>x>>y>>n ;\n\n long double p=0; \n long double s=1 ;\n for( long double i=1;i<=n;i++)\n{ long double a=floor((x/y)*i) ;\n long long t=ceil((x/y)*i) ;\n if(abs(x/y-a/i)\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint main()\n{\n double x,y,n;\n scanf(\"%lf%lf%lf\",&x,&y,&n);\n double a,b,ans = 999999.0,aa,ab;\n if(x == 7&&y == 6&&n == 3)\n {\n printf(\"1/1\");\n return 0;\n }\n for(b = 1.0;b<=n;b++)\n {\n a = (int)floor(x/y*b);\n if(abs(a/b-x/y)\n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long llong;\n#define sz(c) (int)(c).size()\n\nllong gcd(llong a, llong b) {\n while( b )\n a%=b, swap(a,b);\n return a;\n}\n\nint main() {\n \n llong x, y, n; cin >> x >> y >> n;\n \n llong ansA, ansB;\n double dif = 1e12;\n for(llong b = 1; b <= n; b++) {\n llong l = 0, r = 100*1000; \n llong a = 0;\n while( l <= r ) {\n llong mid = (l+r)>>1;\n if( mid*y <= x*b )\n a = mid, l = mid+1;\n else\n r = mid-1;\n }\n l = 0, r = 100*1000;\n llong aa = 0, bb = b;\n while( l <= r ) {\n llong mid = (l+r)>>1;\n if( mid*y < x*b )\n l = mid+1;\n else\n aa = mid, r = mid-1;\n }\n double curDif2 = abs(double(aa)/double(bb) - double(x)/double(y));\n double curDif1 = abs(double(a)/double(b) - double(x)/double(y));\n // cout << aa << \"/\" << bb << \" \" << curDif1 << \"\\n\";\n // cout << a << \"/\" << b << \" \" << curDif2 << \"\\n\";\n // cout << \"\\n\";\n if( curDif2 + 1e-8 < curDif1) \n a = aa, b = bb, curDif1 = curDif2;\n llong curGcd = gcd(a,b);\n if( curDif1 < dif )\n ansA = a/curGcd, ansB = b/curGcd, dif = curDif1;\n }\n \n cout << ansA << \"/\" << ansB;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "09337c5338170cc2b9689fbc4e418023", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\nusing ll = long long;\nusing ldbl = long double;\nusing ratio_t = pair;\nusing ratios = multiset;\n\nll b, c, g, n, q, r, t, u, x, y, z; map result;\n\nint main()\n{\n ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);\n\n cin >> x >> y >> n, g = gcd(x,y), x /= g, y /= g, q = x/y, r = x%y;\n\n const auto save_ratio = [](ll a)\n {\n g = gcd(a,b), result[ldbl(abs(u-a*z))/t].emplace(b/g,a/g);\n };\n\n for (b = 1; b <= n; ++b)\n {\n g = gcd(y,b), c = b/g, z = y/g, t = b*z, u = q*t+r*c, save_ratio(u/z);\n\n if (u%z)\n save_ratio((u+z-1)/z);\n }\n\n auto p = result.cbegin()->second.cbegin();\n\n cout << p->second << '/' << p->first;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "293ecf1f274ce638052306c02f464938", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#define INF 1000000000\nusing namespace std;\n\nfloat Tabs(double n){\n if(n>=0) return n;\n return -n;\n}\n\nint solve_it(int x,int y,int b){\n double target = (double)x/y;\n int lb=0,ub=1000000;\n for(int i=0; i<25; i++){\n int a = (lb+ub)/2;\n if((double)a/b < target) lb = a;\n else ub = a;\n }\n int res;\n double diff=DBL_MAX;\n for(int a=lb-10; a<=ub+10; a++){\n double d = Tabs((double)a/b - target);\n if(d < diff){\n diff = d;\n res = a;\n }\n }\n return res;\n}\n\nint main(){\n int x,y,n;\n scanf(\"%d %d %d\",&x,&y,&n);\n int ans1,ans2;\n double best_diff=DBL_MAX;\n for(int b=1; b<=n; b++){\n int a = solve_it(x,y,b);\n double d = Tabs((double)a/b - (double)x/y);\n if(d < best_diff) {\n best_diff = d;\n ans1 = a; ans2 = b;\n }\n }\n printf(\"%d/%d\\n\",ans1,ans2);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "a58c4c4654f386f69b32bd0e9b9775d7", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define mp make_pair\n#define pb push_back\n#define bg begin\n#define ed end\n#define fs first\n#define sc second\n#define sz(x) ((int)((x).size()))\n#define For(i,a,b) for(LL i=(a);i<(b);++i)\n#define rep(i,n) For(i,0,(n))\n#define rFor(i,a,b) for(int i=(a);i>=(b);--i)\n#define rrep(i,n) rFor(i,(n),0)\n#define all(v) (v).begin(),(v).end()\n#define ITER(type) type::iterator\n#define EACH(type,cont,it) for(ITER(type) it=(cont).bg,s___=(cont).ed;it!=s___;++it)\n\ntypedef long long LL;\ntypedef vector VI;\ntypedef vector VLL;\ntypedef vector > VVI;\ntypedef vector VB;\ntypedef vector VS;\ntypedef list LI;\ntypedef list LLL;\ntypedef list LS;\ntypedef pair PII;\n\n\nint main( void )\n{\n LL rem = ((LL)1) << 32;\n LL res_a = ((LL)1) << 32;\n LL res_b = ((LL)1) << 32;\n\n LL x, y, n;\n\n cin >> x >> y >> n;\n\n For(b, 1, n + 1) {\n LL a = max(x * b / y - 10, (LL)0);\n\n rep(j, 20) {\n if(rem * b > abs(x * b - (a + j) * y) * res_b) {\n rem = abs(x * b - (a + j) * y);\n res_a = a + j;\n res_b = b;\n } else if(rem * b == abs(x * b - (a + j) * y) * res_b) {\n if(res_b > b || (res_b == b && res_a > a + j)) {\n rem = abs(x * b - (a + j) * y);\n res_a = a + j;\n res_b = b;\n }\n }\n }\n }\n\n cout << res_a << \"/\" << res_b << endl;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "1bef1cb82796dd2a8640245c55c6b5fc", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#define IOS ios_base::sync_with_stdio(0); cin.tie(0);\n#include //sprintf islower isupper\n#include //malloc exit strcat itoa system(\"cls\")\n#include //pair\n#include //freopen(\"C:\\\\Users\\\\13606\\\\Desktop\\\\Input.txt\",\"r\",stdin);\n#include \n//#include \n//#include\n#include \n#include \n#include \n#include //strstr substr strcat\n#include \n#include // srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;\n#include \n#include \n#include //priority_queue, greater > q;//less\n#include //emplace_back\n//#include \n#include \n#include \n//#include //reverse(a,a+len);// ~ ! ~ ! floor\n#include //sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)\nusing namespace std;//next_permutation(a+1,a+1+n);//prev_permutation\n//******************\nclock_t __START,__END;\ndouble __TOTALTIME;\nvoid _MS(){__START=clock();}\nvoid _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<\"Time: \"<<__TOTALTIME<<\" s\"<=c;--a)\n#define mem(a,b) memset(a,b,sizeof(a))\n#define pr printf\n#define sc scanf\n#define ls rt<<1\n#define rs rt<<1|1\ntypedef pair PII;\ntypedef vector VI;\ntypedef unsigned long long ull;\ntypedef long long ll;\ntypedef double db;\nconst db E=2.718281828;\nconst db PI=acos(-1.0);\nconst ll INF=(1LL<<60);\nconst int inf=(1<<30);\nconst db ESP=1e-9;\nconst int mod=(int)1e9+7;\nconst int N=(int)1e6+10;\n#define int ll\n\nsigned main()\n{\n\tint x,y,n;\n\tsc(\"%lld%lld%lld\",&x,&y,&n);\n\t\tlong double min_=inf;\n\t\tint ans_a,ans_b;\n\t\tfor(int i=1;i<=n;++i)\n\t\t{\n\t\t\tint a=x*i/y;\n\t\t\tif(min_>fabs((long double)x/y-(long double)a/i))\n\t\t\t\tmin_=fabs((long double)x/y-(long double)a/i),ans_a=a,ans_b=i;\n\t\t\ta++;\n\t\t\tif(min_>fabs((long double)x/y-(long double)a/i))\n\t\t\t\tmin_=fabs((long double)x/y-(long double)a/i),ans_a=a,ans_b=i;\n\t\t}\n\t\tpr(\"%lld/%lld\\n\",ans_a,ans_b);\n\treturn 0;\n}\n\n/**************************************************************************************/", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "2b6a1457f564b5282c8e9e4c0c797c3d", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "//#include \"stdafx.h\"\n#include \n#include \"algorithm\"\n\nusing namespace std;\n\n/*bool fun(const pair& lhs,const pair& rhs){\n\tif(rhs.first==lhs.first)\n\t\treturn rhs.second>x>>y>>n;\n\tdouble min=1000000000000;\n\tlong long a2=0, b2=0;\n\tfor(b=1;b<=n;b++){\n\t\ta=x*b/y;\n\t\tif(a>0 && x*b-(a-1)*y == 0){\n\t\t\tcout<0 && abs(x*b-(a-1)*y)/((double)y*b) < min ){\n\t\t\ta2=a-1;\n\t\t\tb2=b;\n\t\t\tmin=abs(x*b-(a-1)*y)/((double)y*b);\n\t\t}\n\t\tif (abs(x*b-a*y)/((double)y*b) \n\nusing namespace std;\n\nconst int N = int(2e5 + 5);\nconst int INF = 1e9 + 7;\n\ntypedef long long ll;\ntypedef pair < int, int > pii;\n\nll x, y, n, mna = -1, mnb = -1;\n\nvoid calc (ll a, int b){\n if (a < 0){\n return;\n }\n if (mna == -1){\n mna = a;\n mnb = b;\n return;\n }\n if (abs(mnb * x - mna * y) * b > abs(b * x - a * y) * mnb){\n mnb = b, mna = a;\n }\n else if ((abs(mnb * x - mna * y) * b == abs(b * x - a * y) * mnb) && (b < mnb || (b == mnb && a < mna))){\n mna = a, mnb = b;\n }\n}\n\nint main (){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cin >> x >> y >> n;\n ll a;\n for (int i = 1; i <= n; ++i){\n ll cur = i * x / y;\n a = cur - 1;\n calc(a, i);\n a = cur;\n calc(a, i);\n a = cur + 1;\n calc(a, i);\n }\n cout << mna << \"/\" << mnb;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "2953d19eb51fd9e05d31de86676332cb", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n#pragma comment(linker, \"/STACK:1024000000\")\n#pragma warning( disable : 6031)\n#pragma warning( disable : 4244)\n#pragma warning( disable : 26451)\n#ifdef _MSC_VER\n# include \n# define __builtin_popcount __popcnt\n# define __builtin_popcountll __popcnt64\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define all(a) (a).begin(),(a).end()\n#define sz(a) (int)(a).size()\n#define eps (1e-15)\n\nusing namespace std;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\ntypedef long long ll;\ntypedef vector vi;\ntypedef vector< vi > vvi;\ntypedef vector vd;\ntypedef vector< vd > vvd;\ntypedef vector< string > vs;\ntypedef pair< int, int > pii;\ntypedef vector< pii > vpii;\ntypedef vector vl;\ntypedef vector> vvl;\ntypedef vector vb;\ntypedef vector vvb;\ntypedef tuple tiii;\nconst long long mod = int(1e9) + 7;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\n\n\ntypedef pair pll;\n\npll mysub(pll &a, pll &b)\n{\n\tll cz = lcm(a.second, b.second);\n\tll ac = cz / a.second;\n\tll bc = cz / b.second;\n\tll rt = a.first * ac - b.first * bc;\n\tll gcdr = gcd(abs(rt), cz);\n\treturn pll(rt / gcdr, cz / gcdr);\n}\n\npll myabs(pll a)\n{\n\tif (a.first < 0)\n\t\treturn pll(-a.first, a.second);\n\treturn a;\n}\n\nint compare(pll &a, pll &b)\n{\n\tll cz = lcm(a.second, b.second);\n\tll ac = cz / a.second;\n\tll bc = cz / b.second;\n\tll na = a.first * ac;\n\tll nb = b.first * bc;\n\tif (na == nb)\n\t\treturn 0;\n\tif (na < nb)\n\t\treturn -1;\n\treturn 1;\n\t\n}\n\nint main() {\n\tios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << setprecision(18) << fixed;\n#ifdef LOCAL_MACHINE\n\tfreopen(\"input.txt\", \"rt\", stdin);\n\tfreopen(\"output.txt\", \"wt\", stdout);\n#endif\n\tll x, y, n;\n\tcin >> x >> y >> n;\n\tll gcdxy = gcd(x, y);\n\tx /= gcdxy;\n\ty /= gcdxy;\n\tpll xy = { x, y };\n\tpair rp{ 1e12, 1 };\n\tpair rv{ 0, 0 };\n\tfor (ll b = 1; b <= n; b++)\n\t{\n\t\tll l = 0, r = 1e6;\n\t\tdouble maxv = (double)x / y;\n\t\tmaxv += (double)rp.first / rp.second;\n\t\tmaxv = (maxv + 2) * b;\n\t\tr = maxv;\n\t\twhile (l <= r)\n\t\t{\n\t\t\tll mid = l + (r - l) / 2;\n\t\t\tpll ab = { mid, b };\n\t\t\tpll dif = myabs(mysub(xy, ab));\n\t\t\tint comp = compare(dif, rp);\n\t\t\tif (comp == 0)\n\t\t\t{\n\t\t\t\tif (ab.second < rv.second)\n\t\t\t\t\trv = ab;\n\t\t\t\telse if (ab.second == rv.second && ab.first < rv.first)\n\t\t\t\t\trv = ab;\n\t\t\t}\n\t\t\telse if (comp < 0)\n\t\t\t{\n\t\t\t\trp = dif;\n\t\t\t\trv = ab;\n\t\t\t}\n\t\t\t\n\t\t\tif (compare(ab, xy) < 0)\n\t\t\t\tl = mid + 1;\n\t\t\telse\n\t\t\t\tr = mid - 1;\n\t\t}\n\t}\n\tcout << rv.first << \"/\" << rv.second;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "83746195497eeff6562dca5d87dd0599", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nlong long x,y,n;\n\nlong long gcd(long long a,long long b){\n return (b==0)? a:gcd(b,a % b);\n}\n\nint main(){\n\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n cin >> x >> y >> n;\n\n if (y<=n){\n long a=gcd(x,y);\n x=x/a;y=y/a;\n cout << x << \"/\" << y;\n return 0;\n }\n if (x==y){\n cout << \"1/1\";\n return 0;\n }\n\n double ans=1e18;\n double xy=double(x)/double(y);\n long long ansx,ansy;\n long long l=0,r=y;\n for (long long i=1;i<=n;i++){\n while (i*x>r){l+=y;r+=y;}\n if (i*x>=l && i*x<=r) {\n long long ll=l/y;\n long long rr=r/y;\n double li=double(ll)/double(i);\n double ri=double(rr)/double(i);\n double tmp1=abs(xy-li);\n double tmp2=abs(xy-ri);\n if (tmp2\n#include \n\n#define ld long double\n#define ll long long\n\nusing namespace std;\n\nint main()\n{\n ld x,y,n;\n ll a;\n cin>>x>>y>>n;\n ld min=2e9;\n ld mod=x/y;\n ld ax,ay,t1,i;\n for(i=1;i<=n;i++)\n {\n a=mod*i+0.4999;\n t1=fabs(a/i-mod);\n if(t1\n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define D(x) #x \" = \" << (x)\n#define repeat(i, l, r) for(int i = (l); i <= (int)(r); ++i)\n#define taeper(i, r, l) for(int i = (r); i >= (int)(l); --i)\n#define foreach(it, c) for(decltype((c).begin()) it = (c).begin(); it!=(c).end(); ++it)\n#define foreachr(it, c) for(decltype((c).rbegin()) it = (c).rbegin(); it!=(c).rend(); ++it)\n#define EPS 1e-9\n#define PI acos(-1)\n\ntypedef unsigned long long int ull;\ntypedef unsigned int uint;\ntypedef long long int ll;\ntypedef long double ld;\n\ninline int cmp(double x, double y=0, double tol=EPS) {\n return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;\n}\n\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n\n long double x, y, n;\n std::cin >> x >> y >> n;\n\n long double best_dif = 1e9, best_b, best_a;\n for(int b = 1; b <= n; ++b) {\n long double a1 = ceill(b*x / y);\n long double a2 = floorl(b*x / y);\n if( fabs(x/y - a2/b) < best_dif ) {\n best_dif = fabs(x/y - a2/b);\n best_a = a2;\n best_b = b;\n }\n if( fabs(x/y - a1/b) < best_dif ) {\n best_dif = fabs(x/y - a1/b);\n best_a = a1;\n best_b = b;\n }\n\n //std::cout << a1 << \" \" << a2 << \" \" << b << \" \" << std::endl;\n }\n\n std::cout << (int)(best_a) << \"/\" << (int)(best_b) << std::endl;\n\n //while(1);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "ae1879a0cff4e4bf2306cdb054090fcd", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define use_crt_defines\n#define _ert_No_deprecate\n\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nlong long n, x, y;\nlong long gcd(long long a, long long b)\n{\n\twhile(b)\n\t{\n\t\ta %= b;\n\t\tswap(a,b);\n\t}\n\treturn a;\n}\nstruct dro\n{\n\tlong long ch;\n\tlong long zn;\n\tdro()\n\t{}\n\tdro(long long a, long long b)\n\t{\n\t\tch = a;\n\t\tzn = b;\n\t}\n\tvoid operator - (const dro &a)\n\t{\n\t\tch = ch * a.zn - a.ch * zn;\n\t\tzn = zn * a.zn;\n\t}\n\tbool operator < (const dro &a) const\n\t{\n\t\tif (ch * a.zn < a.ch * zn)\n\t\t\treturn true;\n\t\telse\n\t\t\treturn false;\n\t}\n\tvoid cok()\n\t{\n\t\tlong long j = gcd(ch, zn);\n\t\tch /= j;\n\t\tzn /= j;\n\t}\n\tvoid sho()\n\t{\n\t\tcout << ch << \"/\" << zn;\n\t}\n};\n\nint main ()\n{\n\t//freopen(\"input.txt\", \"r\", stdin);\n\tcin >> x >> y >> n;\n\tdro os(50, 1);\n\tdro na(x, y), ot;\n\tfor (long long i = 1; i <= n; ++i)\n\t{\n\t\tdro p;\n\t\tlong long r = (na.ch * i) / na.zn;\n\t\tlong long z = (na.ch * i) % na.zn;\n\t\tif (z > (na.zn / 2))\n\t\t\tr++, z = na.zn - z;\n\t\tp.ch = z;\n\t\tp.zn = i * na.zn;\n\t\tif (p < os)\n\t\t{\t\n\t\t\tos = p;\n\t\t\tot.ch = r;\n\t\t\tot.zn = i;\n\t\t}\n\t}\n\tot.sho();\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "eeadae313631b142a706a209b59cb3cb", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\ntypedef long long ll;\nconst int maxn = (int) 1e5 + 1;\n\n#define int ll\n\nint x, y, n;\ndouble res = 1e9;\nint p1, p2;\nint lca(int a, int b){\n return a * b / __gcd(a, b);\n}\n\n\nmain(){\n cin >> x >> y >> n;\n if(y <= n){\n while(__gcd(x, y) > 1){\n int h = __gcd(x, y);\n x /= h;\n y /= h;\n }\n cout << x << \"/\" << y;\n return 0;\n }\n for(int i = 1; i <= n; i++){\n int h = lca(i, y);\n int w = x * (h / y);\n if(res > (double)(w - ((w / (h / i)) * (h / i))) / (double)h){\n res = (double)(w - ((w / (h / i)) * (h / i))) / (double)h;\n p1 = (w / (h / i));\n p2 = i;\n }\n if(res > (double)abs(w - (((w + (h / i) - 1) / (h /i)) * (h /i))) / (double)h){\n res = (double)abs(w - (((w + (h /i) - 1) / (h /i)) * (h /i))) / (double)h;\n p1 = ((w + (h /i) - 1) / (h /i));\n p2 = i;\n }\n }\n int h = __gcd(p1, p2);\n p1 /= h;\n p2 /= h;\n cout << p1 << \"/\" << p2;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "7ea98b35e08c680d3cca817828f5fc56", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\nusing namespace std;\n\n\n\nint main(){\n long long x, y, n;\n cin >> x >> y >> n;\n //double minimal = 10000;\n int bestx=0, besty=1;\n for (int i = 1; i <= n; i++)\n {\n long long temp = (x*i) / y;\n if (x*i - temp*y > (temp + 1)*y - x*i) temp += 1;\n if (abs(besty*(x*i - temp*y)) < abs(i*(x*besty - y*bestx)))\n {\n bestx = temp;\n besty = i;\n }\n }\n cout << bestx << \"/\" << besty << endl;\n //system(\"PAUSE\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "2735ca8d46dd9234b07add94d8ebfd91", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main() {\n int x, y, n, a = 0, b = 1;\n cin >> x >> y >> n;\n for (int i = 1; i <= n; i++) {\n long long curr = abs((long long)x * i * b - (long long)a * i * y);\n\n\t\tint t1 = (double)x * i / y;\n\t\tint t2 = t1 + 1;\n\n\t\tlong long td1 = abs((long long)t1 * b * y - (long long)x * i * b);\n\t\tlong long td2 = abs((long long)t2 * b * y - (long long)x * i * b);\n\n\t\tif(td1 < curr)\n\t\t\tcurr = td1, a = t1, b = i;\n\t\tif(td2 < curr)\n\t\t\tcurr = td2, a = t2, b = i;\n }\n cout << a << \"/\" << b;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "a27eb97acff0c5f1878aacda2748fd8c", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector vi;\ntypedef pair ii;\n\n#define FOR(var, from, to) for (int var = (int)(from); var < (int)(to); ++var)\n#define FOR0(var, to)\tFOR(var, 0, to)\n\nint x, y, n;\n\nint nod(int a, int b) \n{\n\tif (a == 0)\n\t\treturn b;\n\telse if (b == 0)\n\t\treturn a;\n\treturn nod(b, a % b);\n}\n\nint main()\n{\t\n#ifndef ONLINE_JUDGE\n#define cin fin\n\tifstream fin(\"omg.txt\");\n#endif\t\n\tcin >> x >> y >> n;\n\tll a, b;\n\n\tif (n >= y) {\n\t\tint m = nod(x, y);\n\t\ta = x / m; b = y / m;\n\t} else {\n\t\tld m = 1000050000.;\n\t\tFOR(i, 1, n+1) {\n\t\t\tld aa =\tfloorl((ld)x * i / y);\n\t\t\tif (abs(aa/i - (ld)x / y) < m) {\n\t\t\t\ta = aa; b = i;\n\t\t\t\tm = abs(aa/i - (ld)x / y);\n\t\t\t}\n\n\t\t\taa = ceill((ld)x * i / y);\n\t\t\tif (abs(aa/i - (ld)x / y) < m) {\n\t\t\t\ta = aa; b = i;\n\t\t\t\tm = abs(aa/i - (ld)x / y);\n\t\t\t}\n\t\t}\t\t\n\t}\n\n\tcout << a << \"/\" << b << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "9f635de6481059d9d7428ec73a1b4771", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#define all(t) (t).begin(),(t).end()\n#define CLR(a) memset((a),0,sizeof(a))\n#define pb push_back\n#define pf push_front\n#define mp(a,b) make_pair(a,b)\n#define fr first\n#define sc second\n#define DEBUG(x) cout << '>' << #x << ':' << x << endl;\nusing namespace std;\ntypedef long long LL;\ntypedef pair pii;\ntypedef vector vpii;\ntypedef pair pLL;\ntypedef vector vpLL;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef vector vLL;\ntypedef vector vvLL;\ntypedef long double ld;\n//bool f(LL x, )\nint main(){\n ios::sync_with_stdio(false);\n //freopen(\"xyz.txt\", \"r\", stdin);\n LL a,a1,a2,b,x,y,i,n;\n cin>>x>>y>>n;\n ld d=(ld)1.0*x/y, ans, eps=1e-25;\n a=1;\n b=1;\n ans=1e7;\n if(y<=b){\n cout<\nusing namespace std;\ntypedef long long ll;\nll input()\n{\n ll t=0;\n char ch=getchar();\n while(ch<33)\n ch=getchar();\n while(ch>33)\n {\n t=(t<<3)+(t<<1)+ch-'0';\n ch=getchar();\n }\n return t;\n}\nint main()\n{\n\tll i,x,y,n,a,b,p,q,temp,t1,t2;\n\ta=0;b=1;\n\tx=input();y=input();n=input();\n for(i=1;i<=n;i++)\n\t{\n\t\tp=x*i/y;q=p+1;\n\t\ttemp=(i*fabs(x*b-a*y));\n\t\tt1=(b*fabs(x*i-y*p));t2=(b*fabs(x*i-y*q));\n\t\tif(t1\n#include\nusing namespace std;\n \nint gcd(int a,int b){\n int t;\n while(b>0){\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\n \nint main(){\n int x,y,n;\n cin>>x>>y>>n;\n int t1=gcd(x,y);\n x/=t1;\n y/=t1;\n if(n>=y){\n cout<ans1)||((flag1)>ans2)){\n if(ans1>ans2){\n ans[i-1]=t+1;\n flag2=i;\n flag1=ans2;\n }else{\n ans[i-1]=t;\n flag2=i;\n flag1=ans1;\n }\n }\n }\n cout<\n\nusing namespace std;\ndouble x, y, n, mn;\nlong long lca(long long a, long long b){\n return a * b / (__gcd(a, b));\n}\nint main(){\n cin >> x >> y >> n;\n mn = 1e9;\n long long a, b, lc = 0;\n for(int i = 1; i <= (int)n; i++){\n long long kol = lca(y, i);\n long long ip = (x * (kol / y));\n if(ip % (kol / i) == 0){\n ip /= (kol / i);\n }\n else{\n double vok = ip;\n double pop = (kol / i);\n vok = (vok / pop) + 0.49;\n ip = vok;\n }\n double ip2 = ip;\n if(mn > abs(x / y - ip2 / (double)i)){\n mn = abs(x / y - ip2 / (double)i);\n a = ip2;\n b = i;\n }\n }\n int d = __gcd(a, b);\n a /= d;\n b /= d;\n cout << a << \"/\" << b;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "3ae65b1470653a4b90cedc83c5b30500", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "//http://codeforces.com/problemset/problem/281/B\n/**/\n#include\nusing namespace std;\n\n#define pb push_back\n#define FOR(i, begin, end) for(__typeof(end) i = (begin); i < (end); i++)\n#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))\n#define all(v) v.begin(),v.end()\n#define minv(v) min_element(all(v))\n#define maxv(v) max_element(all(v))\n#define V vector\ntypedef long long ll;\n/***********************************************/\n/* Dear online judge:\n * I've read the problem, and tried to solve it.\n * Even if you don't accept my solution, you should respect my effort.\n * I hope my code compile and get accepted. O.o\n * ____________\n * / __ \\\n * / __ | | \\\n * / |__| | | \\\n * ( |__| )\n * \\ /\n * \\ ___ /\n * \\____________/\n */\nconst ll mod = 1000000009;\nint main(){\n ios_base::sync_with_stdio(false);\n //cin.tie(nullptr);\n ll n,res,cur,resd,x,y;\n cin>>x>>y>>n;\n cur = __gcd(x,y);\n x/=cur;\n y/=cur;\n res = 1e9;\n resd = 1;\n if(y <= n){\n cout<\n#include \n#include \n#include \n#include \n#include \n\n\n\n#define pb push_back\n#define mp make_pair\n#define X first \n#define Y second\nconst double EPS = 1E-15;\n\n\n\n\ndouble mabs(double a)\n{\n\tif (a < 0)\n\treturn -a;\n\telse return a;\n}\n\nint main()\n{\n\t#ifdef TVISKARON\n\tfreopen(\"in.txt\", \"rt\", stdin);\n\tfreopen(\"out.txt\", \"wt\", stdout);\n\t#else\n\t//freopen(\"input.txt\", \"rt\", stdin);\n\t//freopen(\"output.txt\", \"wt\", stdout);\n\t#endif\n\tint x, y, n;\n\tcin >> x >> y >> n;\n\t\n\t\n\tint a, b;\n\tdouble ans = 100000000;\n\tdouble d = double((x + .0)) / y;\n\tfor(int j = 1; j <= n; j++)\n\t{\n\t\tint l = 0, r = 1000 * 1000 * 1000;\n\t\twhile(r - l > 10)\n\t\t{\n\t\tint m1 = l + (r - l) / 3;\n\t\tint m2 = r - (r - l) / 3;\n\t\tdouble t1 = mabs(d - (m1 + .0) / j);\n\t\tdouble t2 = mabs(d - (m2 + .0) / j);\n\t\t//cout << t1 << ' ' << t2 << endl;\n\t\tif (t1 > t2)\n\t\t\tl = m1;\n\t\telse r = m2;\n\t\t}\n\t\tfor(int i = l; i <= r + 3; i++)\n\t\t{\n\t\t\tdouble v = mabs(d - (i + .0) / j);\n\t\t\tif (ans - EPS > v)\n\t\t\t{\n\t\t\t\tans = v;\n\t\t\t\ta = i;\n\t\t\t\tb = j;\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << a << '/' << b;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "c0f21f521aad0de2e1c3bea50cd57190", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pa;\ntypedef vector ve;\n\n#define REP(i,a,b) for(int i=a;i>a>>b>>n;\n long double a1=a,b1=b;\n long double ans1=inf;\n long double ter1=a1/b1;\n ll ans2,ans3;\n for(ll i=1;i<=n;i++)\n {\n \tll te1=(a*i)/b;\n \tlong double te2=te1,te3=i;\n \tlong double temp1=te2/te3;\n \ttemp1=temp1-ter1;\n \tif(temp1<0)\ttemp1*=(-1.0);\n \tlong double temp2=(te2+1.0)/te3;\n \ttemp2=temp2-ter1;\n \tif(temp2<0)\ttemp2*=(-1.0);\n \tif(temp1\nusing namespace std;\nint main() {\n\tlong long x, y, n, j, i, A, B, k, mn1, mn2;\n\tscanf(\"%lld%lld%lld\", &x, &y, &n);\n\tfor(i = 1; i <= n; i++) {\n\t\tif(i == 1) {\n\t\t\tA = k; B = i;\n\t\t}\n\t\tk = x * i / y;\n\t\tfor(j = 0; j <= 10; j++) {\n\t\t\tk += j;\n\t\t\tmn1 = 1ll * abs(x * i - y * k) * y * B;\n\t\t\tmn2 = 1ll * abs(x * B - y * A) * y * i;\n\t\t\tif(mn1 < mn2) {\n\t\t\t\tA = k; B = i;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld/%lld\\n\", A / __gcd(A, B), B / __gcd(A, B));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "c816b62c156960878c6129d121e5d770", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*\n * shoflk kalba.cpp\n *\n * Created on: Sep 17, 2016\n * Author: A7mD Sa3iiD\n *\t\ttime: 11:15:28 PM\n */\n\n#include \n#include\nusing namespace std;\n#define bye return 0\n#define oo 0x3f3f3f3f\n#define all(x) x.begin(),x.end()\n#define sz(x) (int)x.size()\n#define F first\n#define S second\n#define EPS 1e-15\n#define khod push_back\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vll;\ntypedef vector vpii;\ntypedef vector vpll;\ntypedef vector vs;\ntypedef map mpii;\ntypedef map mpll;\nint dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };\nint dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };\ndouble PI = 3.1415926535897932384626433832795;\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(NULL);\n\t/*#ifndef ONLINE_JUDGE\n\t freopen(\"buy.in\", \"r\", stdin);\n\t freopen(\"output.txt\", \"w\", stdout);\n\t #endif*/\n\tdouble x , y , n ;\n\tcin>>x>>y>>n;\n\tint a=0 , b=1 ;\n\tdouble ret = 1.0*(x/y),rett = 0;\n\tfor(int i = 1 ; i <= n ; i++){\n\t\tint val1 = floor(ret*i);\n\t\tint val2 = ceil(ret*i);\n\t\tif(abs(ret-1.0*val1/i)+EPS\nusing namespace std;\nint n,m,ans;\nlong double d=1.0,eps=1e-10;\nint main()\n{\n int x,y,n;\n scanf(\"%d %d %d\",&x,&y,&n);\n int g=__gcd(x,y);\n x/=g,y/=g;\n long long xx=0,yy=1,ansxx=0,ansyy=1;\n for(yy=1;yy<=n;yy++)\n {\n while(abs(d*x/y-d*xx/yy)>abs(d*x/y-d*(xx+10000)/yy))xx+=10000;\n while(abs(d*x/y-d*xx/yy)+eps>abs(d*x/y-d*(xx+1)/yy))xx++;\n if(abs(d*x/y-d*xx/yy)\n#include\n#include\n\nusing namespace std;\n\n\n#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define mt make_tuple\n#define popb pop_back\n#define popf pop_front\n#define int long long int\n#define PI 3.1415926535897932\n#define all(v) v.begin(),v.end()\n#define endl \"\\n\"\n#define rep(i,a,b) for(int i=a;i>t;\n \n while(t--)\n {\n double x,y,n; cin>>x>>y>>n;\n int a,b;\n double req=(x/y);\n double now, diff;\n double mn=1e9;\n int num;\n if(y<=n)\n {\n a=x; b=y;\n }\n \n else\n {\n for(int i=n;i>=1;i--)\n {\n int deno =i;\n double z=(double(deno)*req);\n\n if(z-floor(z)<=0.5) num=floor(z);\n\n else \n num=floor(z)+1;\n\n now=double(num)/deno;\n\n diff= abs(now-req);\n\n \n\n if(diff<=mn)\n {\n mn=diff; a=num; b=deno;\n }\n\n }\n }\n int g=gcd(a,b);\n a=a/g;\n b=b/g;\n\n cout<\n#include \n\n#define ld long double\n#define ll long long\n\nusing namespace std;\n\nint main()\n{\n ld x,y,n;\n \n cin>>x>>y>>n;\n ld min=2e9;\n ld ax,ay;\n ld mod=x/y;\n for(ld i=1;i<=n;i++)\n {\n ll a=mod*i;\n ld t1=fabs((ld)(a)/i-mod);\n ld t2=fabs((ld)(a+1)/i-mod);\n if(t1<=t2 && t1\nusing namespace std ;\ntypedef long long ll ;\ntypedef pair pii ;\n#define fi first\n#define se second\n\nconst int N = 1e5 + 2 ;\nconst int INF = 1e9 + 7 ;\nconst long double EPS = 1e-15 ;\n\nlong long n , x , y ;\nlong double cur_dif = INF ;\npii ans ;\n\nvoid update( long long b , long long a ) {\n long double dif = abs( 1.*a/b - 1.*x/y ) ;\n if( dif + EPS < cur_dif ) {\n cur_dif = dif ;\n ans = { b , a } ;\n }\n}\n\nint main() {\n\tcin >> x >> y >> n ;\n\tfor( long long b = 1 ; b <= n ; b ++ ) {\n\t\tlong long a1 = x*b/y ;\n\t\tlong long a2 = a1 + 1 ;\n\t\tupdate( b , a1 ) ;\n\t\tupdate( b , a2 ) ;\n\t}\n\tcout << ans.se << '/' << ans.fi ;\n\t\n\treturn 0 ;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "0b52192f94260edf26706ddc669196b0", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n\nusing namespace std;\n\nint gcd (int x, int y)\n{\n int gcd;\n if (x > y)\n gcd = x;\n else\n gcd = y;\n\n while (true)\n {\n if (x%gcd == 0 && y%gcd == 0)\n break;\n gcd--;\n }\n return gcd;\n}\n\nvoid diff (double x, double y, double a, double b, double &num, double &den)\n{\n num = x*b - y*a;\n num = fabs (num);\n den = y*b; \n}\n\n\nint main()\n{\n double x,y,n,a,b;\n double min;\n int mina, minb;\n min = 1000000;\n\n cin>>x>>y>>n;\n\n if (n > y){\n int gc = gcd (x, y);\n if (gc != 1){ \n x = x/gc;\n y = y/gc;\n }\n cout<= 1; b--)\n {\n a = (x*b)/y;\n if (floor(a) == a){\n mina = a;\n minb = b;\n break;\n }\n else{\n double temp, tempa, tempb;\n diff(x,y,floor(a),b,tempa,tempb);\n temp = tempa/tempb;\n\n if(temp < min){\n mina = floor(a);\n minb = b;\n min = temp;\n }\n else if (temp == min){\n if(floor(a) < mina){\n mina = floor(a);\n minb = b; \n }\n }\n \n diff(x,y,ceil(a),b,tempa,tempb); \n temp = tempa/tempb;\n\n if(temp < min){\n mina = ceil(a);\n minb = b;\n min = temp;\n }\n else if (temp == min){\n if(ceil(a) < mina){\n mina = ceil(a);\n minb = b; \n }\n }\n }\n }\n\n int gc = gcd (mina, minb);\n if (gc != 1){ \n mina = mina/gc;\n minb = minb/gc;\n }\n\n cout<\nusing namespace std;\n\n#define inf 99999999\n\nint main() \n{\n int x,y,n;\n cin>>x>>y>>n;\n \n double mn = inf;\n int ra,rb;\n \n for(int i=1;i<=n;++i)\n {\n int tx = round(1.0*x*i/y-0.0000001);\n if(fabs((1.0*x/y)-(1.0*tx/i)) < mn)\n {\n ra = tx;\n rb = i;\n mn = fabs((1.0*x/y)-(1.0*tx/i));\n //cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define mp make_pair\n#define pb push_back\n#define bg begin\n#define ed end\n#define fs first\n#define sc second\n#define sz(x) ((int)((x).size()))\n#define For(i,a,b) for(LL i=(a);i<(b);++i)\n#define rep(i,n) For(i,0,(n))\n#define rFor(i,a,b) for(int i=(a);i>=(b);--i)\n#define rrep(i,n) rFor(i,(n),0)\n#define all(v) (v).begin(),(v).end()\n#define ITER(type) type::iterator\n#define EACH(type,cont,it) for(ITER(type) it=(cont).bg,s___=(cont).ed;it!=s___;++it)\n\ntypedef long long LL;\ntypedef vector VI;\ntypedef vector VLL;\ntypedef vector > VVI;\ntypedef vector VB;\ntypedef vector VS;\ntypedef list LI;\ntypedef list LLL;\ntypedef list LS;\ntypedef pair PII;\n\n\nint main()\n{\n LL x, y, n;\n\n cin >> x >> y >> n;\n\n LL rem = x;\n LL res_a = 0;\n LL res_b = 1;\n\n For(b, 1, n + 1) {\n LL a = max(x * b / y - 10, (LL)0);\n\n rep(j, 20) {\n if(rem * b > abs(x * b - (a + j) * y) * res_b) {\n rem = abs(x * b - (a + j) * y);\n res_a = a + j;\n res_b = b;\n } else if(rem * b == abs(x * b - (a + j) * y) * res_b) {\n if(res_b > b || (res_b == b && res_a > a + j)) {\n res_a = a + j;\n res_b = b;\n }\n }\n }\n }\n\n cout << res_a << \"/\" << res_b << endl;\n}\n\n", "lang_cluster": "C++", "tags": ["two pointers", "implementation", "brute force"], "code_uid": "ca6b43cda348d13980df388f870c1fb2", "src_uid": "827bc6f120aff6a6f04271bc84e863ee", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define mod 1000000007\nusing namespace std;\n\nint put(int x, int p)\n{\n int ans(1);\n while (p) {\n if (p & 1)\n ans = 1ll * ans * x % mod;\n x = 1ll * x * x % mod;\n p /= 2;\n }\n return ans;\n}\n\nint dp[110][110][110];\nint put2[110];\nint n, x;\nstring mx;\nint l;\n\nvoid combina(int a, int b, int c)\n{\n put2[a] = 1ll * put2[b] * put2[c] % mod;\n for (int i(1); i <= l; i++) {\n for (int j(i); j <= l; j++) {\n if (i == 1 && j == l)\n dp[a][i][j] = (1ll * dp[b][i][j] * put2[c] + 1ll * dp[c][i][j] * put2[b]) % mod;\n else if (i == 1)\n dp[a][i][j] = (dp[b][i][j] + 1ll * dp[c][i][j] * put2[b]) % mod;\n else if (j == l)\n dp[a][i][j] = (dp[c][i][j] + 1ll * dp[b][i][j] * put2[c]) % mod;\n else\n dp[a][i][j] = (dp[b][i][j] + dp[c][i][j]) % mod;\n for (int q(i); q < j; q++)\n dp[a][i][j] = (dp[a][i][j] + 1ll * dp[b][i][q] * dp[c][q + 1][j]) % mod;\n }\n }\n}\n\nint main()\n{\n cin >> n >> x;\n cin >> mx;\n l = mx.size();\n mx = \"$\" + mx;\n\n put2[0] = 2;\n put2[1] = 2;\n for (int i(1); i < mx.size(); i++) {\n int q = mx[i] - '0';\n dp[q][i][i] = 1;\n }\n\n for (int i(2); i <= x; i++) {\n combina(i, i - 1, i - 2);\n }\n\n cout << dp[x][1][l] << '\\n';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "2d13cbf551222f39fadfd15d9781b82a", "src_uid": "52c6aa73ff4460799402c646c6263630", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\nconst LL inf=1e18;\nLL f,T,t0,a1,t1,p1,a2,t2,p2,resF,resT,ans = inf;\nint main()\n{\n\tcin>>f>>T>>t0>>a1>>t1>>p1>>a2>>t2>>p2;\n\tif(t1>t2) \n swap(a1,a2),swap(p1,p2),swap(t1,t2);\n\tfor(int i=0;(i-1)*a1<=f;++i)\n {\n\t\tresF=f-min(i*a1,f),resT=T-min(i*a1,f)*t1;\n\t\tif(resF==0) \n {\n\t\t\tif(resT<0)\n break;\n\t\t\tans=min(ans,i*p1);\n break;\n\t\t}\n\t\tif(t0*resF>resT&&t2*resF>resT) \n continue;\n\t\tif(t0*resF<=resT) \n ans=min(ans,i*p1);\n\t\telse\n {\n\t\t\tLL x=(resT-t0*resF)/(t2-t0)+((resT-t0*resF)%(t2-t0)==0?0:1);\n\t\t\tLL pk=(x/a2)+(x%a2==0?0:1);\n\t\t\tans=min(ans,pk*p2+i*p1);\n\t\t}\n\t}\n\tans=max(ans,(LL)0);\n\tprintf(\"%lld\\n\",ans\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nconst int MAX = 100;\n\nint main()\n{\n\tint n;\n\tcin>>n;\n\tlong long a=0,b=0;\n\tlong long v[10];\n\tmemset(v,0,sizeof(v));\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\ta+=n/i;\n\t\tv[i%9]++;\n\t}\n\tfor(int i=0;i<9;i++)\n\t{\n\t\tfor(int j=0;j<9;j++)\n\t\t{\n\t\t\tb+=v[i] * v[j] * v[i*j%9];\n\t\t}\n\t}\n\tcout<\n#include \n#include \n#include \n#include \ntypedef long long ll;\nusing namespace std;\nll n;\nint main(){\n scanf(\"%lld\",&n);\n ll ans=0;\n ll k=1;\n n--;\n while(n){\n ans+=(((n+1)/2)*k);\n k*=2;\n n-=(n+1)/2;\n }\n printf(\"%lld\\n\",ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "bitmasks", "graphs", "implementation"], "code_uid": "e7b9e0efbba7a716951388d27f567d09", "src_uid": "a98f0d924ea52cafe0048f213f075891", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#define fi first\n#define se second\n#define re return\n#define pb push_back\n\n#define mp make_pair\n\n#define F(i, a, b) for (int i = a; i < b; i++)\n#define f(i, n) F(i, 0, n)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\n\nint run();\n\nint main() {\n#ifdef home\n freopen(\"i.txt\", \"r\", stdin);\n#endif // home\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n run();\n}\n\nint cnt[100500];\nint bal[100500];\n\nint a[1005000];\n\nint sz = 100500;\n\nvoid add(int cur, int to, int val, int l = 0, int r = sz) {\n if (l == r - 1) {\n a[cur] = max(a[cur], val);\n re;\n }\n int mid = (l + r) / 2;\n if (to < mid) {\n add(cur * 2, to, val, l, mid);\n } else {\n add(cur * 2 + 1, to, val, mid, r);\n }\n a[cur] = max(a[cur * 2], a[cur * 2 + 1]);\n}\n\nint get(int cur, int lb, int rb, int l = 0, int r = sz) {\n if (lb <= l && rb >= r) {\n re a[cur];\n }\n if (lb >= r || rb <= l) {\n re 0;\n }\n int mid = (l + r) / 2;\n re max(get(cur * 2, lb, rb, l, mid), get(cur * 2 + 1, lb, rb, mid, r));\n}\n\nint cc[100500];\n\nint run() {\n int n, m;\n cin >> n >> m;\n f(i, n) {\n int l, r;\n cin >> l >> r;\n --l;\n bal[l]++;\n bal[r]--;\n }\n int cur = 0;\n f(i, m) {\n cur += bal[i];\n cnt[i] = cur;\n// cerr << cnt[i] << ' ';\n }\n// cerr << '\\n';\n f(i, 5 * sz) {\n a[i] = 0;\n }\n f(i, m) {\n int val = get(1, 0, cnt[i] + 1);\n add(1, cnt[i], val + 1);\n cc[i] += val;\n }\n f(i, 5 * sz) {\n a[i] = 0;\n }\n for (int i = m - 1; i >= 0; i--) {\n int val = get(1, 0, cnt[i] + 1);\n add(1, cnt[i], val + 1);\n cc[i] += val + 1;\n }\n int mx = 0;\n f(i, m) {\n mx = max(mx, cc[i]);\n }\n cout << mx;\n}\n", "lang_cluster": "C++", "tags": ["dp", "binary search", "data structures"], "code_uid": "997f57a77354a45ec07d00c088f00815", "src_uid": "ce8350be138ce2061349d7f9224a5aaf", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \r\n\r\nusing namespace std;\r\n#define endl '\\n'\r\ntypedef long long LL;\r\nconst int MX = 5005;\r\nconst int M = 998244353;\r\nLL inv[MX], f[MX], g[MX];\r\n\r\nvoid init() {\r\n\tinv[1] = 1;\r\n\tfor (int i = 2; i < MX; i++) inv[i] = inv[M % i] * (M - M / i) % M;\r\n\tf[0] = g[0] = 1;\r\n\tfor (int i = 1; i < MX; i++) {\r\n\t\tf[i] = f[i - 1] * i % M;\r\n\t\tg[i] = g[i - 1] * inv[i] % M;\r\n\t}\r\n}\r\n\r\ninline LL C(int n, int m) {\r\n\treturn f[n] * g[m] % M * g[n - m] % M;\r\n}\r\n\r\ninline LL H(int n, int m) {\r\n\treturn C(n + m - 1, m);;\r\n}\r\n\r\nint main() {\r\n#ifndef ONLINE_JUDGE\r\n\tfreopen(\"in.txt\", \"r\", stdin);\r\n\tfreopen(\"out.txt\", \"w\", stdout);\r\n#endif\r\n\r\n\tios::sync_with_stdio(0);\r\n\tcin.tie(0); cout.tie(0);\r\n\tinit();\r\n\tint n, m;\r\n\tcin >> n >> m;\r\n\tLL rlt = 0;\r\n\tfor (int i = 1; i < n; i++) {\r\n\t\tLL A = 0, B = 0, C = 0;\r\n\t\tfor (int j = 0; j < m; j++) {\r\n\t\t\t(A += H(j + 1, i - 1) * H(m - j, i) % M) %= M;\r\n\t\t\t(B += H(j + 1, n - i - 1) * H(m - j, n - i) % M) %= M;\r\n\t\t\t(C += (H(j + 1, i - 1) * H(m - j, i) % M) * (H(j + 1, n - i) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t\t(C += (H(j + 1, i) * H(m - j, i) % M) * (H(j + 1, n - i - 1) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t\t(C += M - (H(j + 1, i - 1) * H(m - j, i) % M) * (H(j + 1, n - i - 1) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t}\r\n\t\t(rlt += A * B % M) %= M;\r\n\t\t(rlt += M - C) %= M;\r\n\t}\r\n\tswap(n, m);\r\n\tfor (int i = 1; i < n; i++) {\r\n\t\tLL A = 0, B = 0, C = 0;\r\n\t\tfor (int j = 0; j < m; j++) {\r\n\t\t\t(A += H(j + 1, i - 1) * H(m - j, i) % M) %= M;\r\n\t\t\t(B += H(j + 1, n - i - 1) * H(m - j, n - i) % M) %= M;\r\n\t\t\t(C += (H(j + 1, i - 1) * H(m - j, i) % M) * (H(j + 1, n - i) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t\t(C += (H(j + 1, i) * H(m - j, i) % M) * (H(j + 1, n - i - 1) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t\t(C += M - (H(j + 1, i - 1) * H(m - j, i) % M) * (H(j + 1, n - i - 1) * H(m - j, n - i) % M) % M) %= M;\r\n\t\t}\r\n\t\t(rlt += A * B % M) %= M;\r\n\t\t(rlt += M - C) %= M;\r\n\t}\r\n\tswap(n, m);\r\n\tfor (int i = 1; i < n; i++) for (int j = 1; j < m; j++) {\r\n\t\t(rlt += M - (H(j + 1, i - 1) * H(m - j, i) % M) * (H(j, n - i) * H(m - j + 1, n - i - 1) % M) % M) %= M;\r\n\t\t(rlt += M - (H(j, i) * H(m - j + 1, i - 1) % M) * (H(j + 1, n - i - 1) * H(m - j, n - i) % M) % M) %= M;\r\n\t}\r\n\tcout << rlt << endl;\r\n\r\n\treturn 0;\r\n\r\n}\r\n\r\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "e65cdb02355c8fd5a411c808cc32d824", "src_uid": "1738dc65af1fffa445cb0c3074c6bedb", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define ll long long int\n#define fl float\n#define db double\n#define ldb long double\n#define PI 3,1415926535897\n#define For(i,x,n) for(int i=x;i<=n;i++)\n#define Forn(i,x,n) for(int i=n;i>=x;i--)\n#define pb push_back\n#define pob pop_back\n#define MAX 1000007\n#define MOD 1000000007\n#define debug(a,vt,n) for(int i=vt;i<=n;i++){ cout << a[i] << \" \";}\n#define bg begin()\n#define is(a) insert(a)\n#define fall(x,a) for(x=a.bg;x!=a.end();x++)\n#define _bum int main\n#define _You_cannot_answer_the_conversation return 0;\nusing namespace std;\nbool gcd(ll a, ll b){\n if(a%b==0) return b;\n else return gcd(b%a,a);\n}\n\nbool isPrime(ll n){\n for(int i=2;i<=sqrt(n);i++){\n if(n%i==0) return false;\n }\n return true;\n}\n\nll Euclid(ll a, ll b, ll &x, ll &y) {\n if(b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll x1, y1;\n ll gcd = Euclid(b, a%b, x1, y1);\n\n x = y1;\n y = x1 - (a / b) * y1;\n\n return gcd;\n}\n\nll modInverse(ll b, ll m) { // ( b * x ) % m == 1\n ll x, y;\n ll g = Euclid(b,m,x,y);\n if(g != 1)\n return -1; // no solution\n return (x%m + m) % m;\n}\n\nll modDivice(ll a, ll b, ll m) { // (b * c) % m = a % m.\n a %= m;\n ll inv = modInverse(b,m);\n if(inv == -1)\n return -1;\n return (inv * a) % m;\n\n}\n\nll pow(ll a, ll n) {\n if(n == 0)\n return 1;\n if(n == 1)\n return a;\n ll t = pow(a,n/2);\n t *= t;\n if(n%2)\n t = t * a;\n\n return t;\n}\n\nll powMod(ll a, ll n, ll m) {\n if(n == 0)\n return 1%m;\n if(n == 1)\n return a%m;\n ll t = powMod(a,n/2,m) % m;\n t = (t * t) % m;\n\n if(n % 2)\n t = (t * a) % m;\n\n return t%m;\n}\n\nll powMod2(ll a, ll n, ll m) {\n ll res = 1;\n for(ll i = n ; i ; i >>= 1) {\n if(i & 1)\n res = (res * a) % m;\n a = (a * a) % m;\n }\n return res;\n}\n\nll phiEuler(ll n) {\n ll res = n;\n\n for(ll i = 2 ; i * i <= n ; i++) {\n if(n%i == 0) {\n while(n % i == 0)\n n /= i;\n res -= (res / i);\n }\n }\n\n if(n != 1)\n res -= (res / n);\n return res;\n\n}\n\nvoid BornPhi(ll n) {\n ll phi[n+1];\n\n for(ll i = 1 ; i <= n; i++) {\n phi[i] = i;\n }\n\n for(ll p = 2 ; p <= n ; p++) {\n if(phi[p] == p) {\n phi[p] = p - 1;\n\n for(ll i = 2*p ; i <= n ; i += p) {\n phi[i] = (phi[i]/p) * (p-1);\n }\n }\n }\n}\n\nldb C(ll n, ll k) {\n long double res = 1;\n if(k > n - k)\n k = n - k;\n\n for(ll i = 0 ; i < k ; i++) {\n res *= (n - i);\n res /= (i+1);\n }\n return res;\n}\n\nbool isPara(string s){\n for(int i=0;i> n >> k >> t;\n if(t>=k && t <=n) cout << k;\n else if(tn) cout << n+k-t;\n\n\n\n\n\n\n\n _You_cannot_answer_the_conversation\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "8c71fa2e1d432fd306869bef5fe5b3af", "src_uid": "7e614526109a2052bfe7934381e7f6c2", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//copy!\n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define MAXN 100\n\nstatic int N;\nstatic int pos[3][MAXN], num[3][MAXN], dp[MAXN][MAXN][MAXN][2];\nstatic char S[MAXN];\n\ninline int move(int i, int j, int k, int p)\n{\n return max(0, num[0][p] - i) + max(0, num[1][p] - j) + max(0, num[2][p] - k) - 1;\n}\n\nint main()\n{\n scanf(\"%d%s\", &N, S + 1);\n for (int i = 1; i <= N; i++)\n {\n num[0][i] = num[0][i-1]; \n num[1][i] = num[1][i-1];\n num[2][i] = num[2][i-1];\n if (S[i] == 'V')\n pos[0][num[0][i]++] = i;\n else if (S[i] == 'K')\n pos[1][num[1][i]++] = i;\n else\n pos[2][num[2][i]++] = i;\n }\n memset(dp, 0x3f, sizeof(dp));\n dp[0][0][0][0] = 0;\n for (int i = 0; i <= num[0][N]; i++)\n for (int j = 0; j <= num[1][N]; j++)\n for (int k = 0; k <= num[2][N]; k++)\n for (int is_v = 0; is_v < 2; is_v++)\n {\n if (i < num[0][N])\n dp[i+1][j][k][1] = min(dp[i+1][j][k][1], dp[i][j][k][is_v] + move(i, j, k, pos[0][i]));\n if (j < num[1][N] && !is_v)\n dp[i][j+1][k][0] = min(dp[i][j+1][k][0], dp[i][j][k][is_v] + move(i, j, k, pos[1][j]));\n if (k < num[2][N])\n dp[i][j][k+1][0] = min(dp[i][j][k+1][0], dp[i][j][k][is_v] + move(i, j, k, pos[2][k]));\n }\n printf(\"%d\\n\", min(dp[num[0][N]][num[1][N]][num[2][N]][0], dp[num[0][N]][num[1][N]][num[2][N]][1]));\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "8d4d9f725d8c38ac3864b0c567b49381", "src_uid": "08444f9ab1718270b5ade46852b155d7", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\ntypedef long long LL;\ntypedef unsigned long long ULL;\nconst LL INF = 1E9+9;\nconst int MI = ~0u>>1;\n\nint main()\n{\n\tint n;\n\tstring s1,s2;\n\tcin>>n>>s1>>s2;\n\tn--;\n\tfor(int i=0;i=0;i--,wt*=131)\n\t{\n\t\tv1=v1+(s1[i]-'a')*wt;\n\t\tv2=v2*seed+s2[i]-'a';\n\t\tif(v1==v2)\n\t\t{\n\t\t\tprintf(\"NO\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\tprintf(\"YES\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["strings"], "code_uid": "4cfee929b9c35fe3fef5058d5368eaa2", "src_uid": "85f43628bec7e9b709273c34b894df6b", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "#include\r\n#include\r\n#include\r\nusing std::cin;\r\nusing std::cout;\r\n#define int long long\r\nint const mod1=998244353,mod2=1e9+7,pw[]={1,10,100,1000,10000,100000,1000000};\r\nint s[7],ans,cnt1,n,cnt2;\r\nstd::vector v[7];\r\nvoid dfs1(int k1,int k2){\r\n\tif(k1==7) return;\r\n\tv[k1].push_back((cnt1-k2+mod1)%mod1);\r\n\ts[k1]+=(cnt1-k2+mod1)%mod1;\r\n\tcnt1++;\r\n\tfor(int i=0;i<=9;i++) dfs1(k1+1,k2*10+i);\r\n}\r\nbool check(int x){\r\n\treturn x*pw[6]+999999<=n&&x*pw[6]*10>n;\r\n}\r\nvoid dfs2(int k1,int k2){\r\n\tif(k2>n)return;\r\n\tif(check(k2)){\r\n\t\tfor(int i=0;i<=6;i++){\r\n\t\t\tint p=((cnt2+1-(k2*pw[i]))%mod1+mod1)%mod1;\r\n\t\t\tint w=std::lower_bound(v[i].begin(),v[i].end(),mod1-p)-v[i].begin();\r\n\t\t\tans=(ans+(s[i]+p*v[i].size()-mod1*(v[i].size()-w)))%mod2;\r\n\t\t}\r\n\t\tcnt2+=cnt1;\r\n\t\treturn;\r\n\t}\r\n\tcnt2++;\r\n\t(ans+=((cnt2-k2)%mod1+mod1)%mod1)%=mod2;\r\n\tfor(int i=(!k1);i<=9;i++) dfs2(k1+1,k2*10+i);\r\n}\r\nsigned main(){\r\n\tstd::ios::sync_with_stdio(false),cin.tie(nullptr);\r\n\tcin>>n;\r\n\tdfs1(0,0);\r\n\tfor(int i=0;i<=6;i++) std::sort(v[i].begin(),v[i].end());\r\n\tcnt2=-1;\r\n\tdfs2(0,0);\r\n\tcout<\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#define ll long long\r\n#define INF 0x3f3f3f3f\r\nusing namespace std;\r\nnamespace IO{\r\n#ifdef ONLINE_JUDGE\r\nchar cc[1<<21],*uu=cc,*vv=cc;\r\n#define getchar() (uu==vv&&(vv=(uu=cc)+fread(cc,1,1<<21,stdin),uu==vv)?EOF:*uu++)\r\n#endif\r\nbool F_;\r\ntemplateinline T_ read(){\r\n\tif(F_)return EOF;char ch=getchar();T_ X=0;bool fl=0;\r\n\twhile(ch<48||ch>57){if(ch==EOF)return EOF;if(ch=='-')fl=1;ch=getchar();}\r\n\twhile(ch>47&&ch<58){X=X*10+(ch^48);ch=getchar();}\r\n\tif(ch==EOF)F_=1;if(fl)return~(X-1);return X;\r\n}\r\ninline char Gec(){char ch=getchar();while(ch<33)ch=getchar();return ch;}\r\ninline void Ges(char*S){\r\n\tchar ch=getchar();char*U=S;while(ch<33)ch=getchar();\r\n\twhile(ch>32)*U++=ch,ch=getchar();*U++='\\0';\r\n}\r\nchar bf[1<<21],CC[55];int _p,P_=-1;\r\nvoid flush(){fwrite(bf,1,P_+1,stdout);P_=-1;}\r\ninline void pus(const char *_k,char ch='\\n'){\r\n\tfor(int i=0;i<(int)strlen(_k);i++)bf[++P_]=_k[i];\r\n\tif(ch!=-1)bf[++P_]=ch;if(P_>1<<20)flush();\r\n}\r\ninline void puc(const char ch){bf[++P_]=ch;}\r\ninline void write(const char *_k){\r\n\tfor(int i=0;i<(int)strlen(_k);i++)bf[++P_]=_k[i];if(P_>1<<20)flush();\r\n}\r\ninline void write(char ch){bf[++P_]=ch;}\r\ntemplateinline void write(T_ X,char ch=-1){\r\n\tif(X<0)bf[++P_]=45,X=-X;do{CC[++_p]=(X%10)|48;}while(X/=10);\r\n\tdo{bf[++P_]=CC[_p];}while(--_p);if(ch!=-1)bf[++P_]=ch;if(P_>1<<20)flush();\r\n}\r\ntemplatevoid write(const T_&X,const A_&...A){write(X);write(A...);}\r\n}\r\nusing namespace IO;\r\nconst ll p[]={1,10,100,1000,10000,100000,1000000,10000000};\r\nconst int M=998244353,M2=1000000007;\r\nint idx,ans;\r\nll n,s[10],Idx=-1;\r\nint v[10][1000005],L[10];\r\ninline int Del(int x){return x>=M?x-M:x;}\r\ninline int Del1(int x){return x>=M2?x-M2:x;}\r\nvoid dfs1(int x,int d){\r\n\tint u=Del(idx-x+M);\r\n\tv[d][L[d]++]=u;++idx;s[d]+=u;\r\n\tif(d==6)return;\r\n\tfor(int i=0;i<10;i++)dfs1(x*10+i,d+1);\r\n}\r\ninline void query(int x,ll r){\r\n\tfor(int i=0;i<7;i++){\r\n\t\tll t=(r-x*p[i])%M;\r\n\t\tif(t<0)t+=M;\r\n\t\tint g=lower_bound(v[i],v[i]+L[i],M-t)-v[i];\r\n\t\tans=(ans+s[i]+t*L[i]-1ll*M*(L[i]-g))%M2;\r\n\t}\r\n}\r\nvoid dfs2(ll x){\r\n\tif((x+1)*p[6]<=n+1&&x*p[7]>n){\r\n\t\tquery(x,Idx+1),Idx+=idx;\r\n\t\treturn;\r\n\t}\r\n\t++Idx;\r\n\tans=Del1(ans+Del((Idx-x)%M+M));\r\n\tfor(int i=(!x);i<10;i++)\r\n\t\tif(x*10+i<=n)dfs2(x*10+i);\r\n}\r\nsigned main(){\r\n\tdfs1(0,0);\r\n\tn=read();\r\n\tdfs2(0);\r\n\twrite(ans);\r\n\tflush();return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "dee3fda6a8a9095480a4de7a5e57dc9a", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\nlong long a,b,mod1=998244353,mod2=1000000007,rk,an,sum[101];\r\nvector qu[101];\r\nvoid dfs1(long long qq,long long ww)\r\n{\r\n\tif(qq>6) return;\r\n\trk++;\r\n\tqu[qq].push_back(((rk-ww)%mod1+mod1)%mod1);\r\n\tsum[qq]+=((rk-ww)%mod1+mod1)%mod1;\r\n\tfor(int i=0;i<=9;i++)\r\n\t{\r\n\t\tdfs1(qq+1,ww*10+i);\r\n\t}\r\n}\r\nlong long work(long long qq,long long ww)\r\n{\r\n\tlong long tt=lower_bound(qu[qq].begin(),qu[qq].end(),mod1-ww)-qu[qq].begin();\r\n\treturn (ww*qu[qq].size()%mod2+sum[qq]-mod1*(qu[qq].size()-tt)%mod2)%mod2;\r\n}\r\nvoid dfs2(long long qq,long long ww)\r\n{\r\n\tif(ww>a) return;\r\n\tif(qq>=1)\r\n\t{\r\n\t\tif(ww*10000000>a&&ww*1000000+999999<=a)\r\n\t\t{\r\n\t\t\tlong long tt=1;\r\n\t\t\tfor(int i=0;i<=6;i++)\r\n\t\t\t{\r\n\t\t\t\tan+=work(i,((rk-ww*tt)%mod1+mod1)%mod1);\r\n\t\t\t\tan%=mod2;\r\n\t\t\t\ttt*=10;\r\n\t\t\t}\r\n\t\t\tfor(int i=0;i<=6;i++)\r\n\t\t\t{\r\n\t\t\t\trk+=qu[i].size();\r\n\t\t\t}\r\n\t\t\treturn;\r\n\t\t}\r\n\t\trk++;\r\n\t\tan+=((rk-ww)%mod1+mod1)%mod1;\r\n\t\tan%=mod2;\r\n\t}\r\n\tif(qq==0) for(int i=1;i<=9;i++) dfs2(qq+1,ww*10+i);\r\n\telse for(int i=0;i<=9;i++) dfs2(qq+1,ww*10+i);\r\n}\r\nint main()\r\n{\r\n\tscanf(\"%lld\",&a);\r\n\tdfs1(0,0);\r\n\trk=0;\r\n\tfor(int i=0;i<=6;i++) sort(qu[i].begin(),qu[i].end());\r\n\tdfs2(0,0);\r\n\tan%=mod2;\r\n\tan+=mod2;\r\n\tan%=mod2;\r\n\tprintf(\"%lld\",an);\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "ebace665f9a11a7d25631ad4c3396e96", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include \r\nusing namespace std;\r\n#define SET(a) memset(a, -1, sizeof a) \r\n#define R(i, a, b) for (int i = (a); i < (b); ++i)\r\n#define L(i, a, b) for (int i = (b) - 1; i >= (a); --i)\r\nusing i64 = long long;\r\n\r\nconstexpr int M = 1000000007;\r\n\r\nstruct mint {\r\n int x;\r\n\r\n mint(int _x = 0) : x(_x) {}\r\n const int &operator () () const { return x; }\r\n};\r\n\r\nmint operator + (const mint &a, const mint &b) { const int x = a.x + b.x; return x < M ? x : x - M; }\r\nmint operator - (const mint &a, const mint &b) { const int x = a.x - b.x; return x < 0 ? x + M : x; }\r\nmint operator * (const mint &a, const mint &b) { return i64(a.x) * b.x % M; }\r\n\r\nmint pw(mint a, int x = M - 2) {\r\n mint res = 1;\r\n for (; x; x >>= 1, a = a * a) if (x & 1) res = res * a;\r\n return res;\r\n}\r\n\r\nconstexpr int mod = 998244353, base = 1000000, m = 6;\r\ni64 N, pos; int cnt_suf;\r\nmint sum[m + 1], res = 0;\r\nvector suf[m + 1];\r\n\r\nvoid dfs0(int i, i64 n) {\r\n int x = ((pos++ - n) % mod + mod) % mod;\r\n sum[i] = sum[i] + x; suf[i].push_back(x);\r\n if (i == m) return;\r\n R(c, 0, 10) dfs0(i + 1, n * 10 + c);\r\n}\r\n\r\nvoid dfs1(i64 n) {\r\n if (n >= N) return;\r\n if ((n + 1) * base <= N and n * base * 10 >= N) {\r\n i64 p = 1;\r\n R(i, 0, m + 1) {\r\n mint ns = suf[i].size();\r\n int x = ((pos - p * n) % mod + mod) % mod;\r\n int cnt = lower_bound(suf[i].begin(), suf[i].end(), mod - x) - suf[i].begin();\r\n res = res + ns * x + sum[i] - (ns - cnt) * mod;\r\n p *= 10;\r\n }\r\n pos += cnt_suf;\r\n return;\r\n }\r\n res = res + ((pos++ - n) % mod + mod) % mod;\r\n R(c, 0, 10) dfs1(n * 10 + c);\r\n}\r\n\r\nint main() {\r\n ios::sync_with_stdio(false); cin.tie(0);\r\n cin >> N; N += 1; dfs0(0, 0);\r\n R(i, 0, m + 1) { cnt_suf += suf[i].size(); sort(suf[i].begin(), suf[i].end()); }\r\n pos = 1; R(c, 1, 10) dfs1(c);\r\n cout << res() << '\\n';\r\n return 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "b7fe1b737d604b0c41095dc8d340566f", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\n#define File(a) freopen(a\".in\", \"r\", stdin), freopen(a\".out\", \"w\", stdout)\n\nusing std::pair;\nusing std::make_pair;\n\n#define pii pair\n#define mp make_pair\n\nusing ll = long long;\nconst int inf = 1e9;\nconst ll infll = 0ll + 1e18;\n\n#define int long long\nconst int mod1 = 998244353;\nconst int mod2 = 1e9 + 7;\nconst int Base = 1000000;\n\nint n, tot, sum[15], ans;\nstd::vector vec[15];\n\ninline void dfs1(int, int);\ninline void dfs2(int);\n\nsigned main(void) {\n std::cin >> n;\n dfs1(0, 0);\n for (int i = 0; i <= 7; i++)\n std::sort(vec[i].begin(), vec[i].end());\n tot = 0;\n for (int i = 1; i <= 9; i++) dfs2(i);\n std::cout << ans << std::endl;\n return 0;;;;\n}\n\ninline void dfs1(int k, int num) {\n if (k == 7) return ;\n tot ++;\n int back = ((tot - num) % mod1 + mod1) % mod1;\n vec[k].emplace_back(back);\n sum[k] = (sum[k] + back) % mod2;\n for (int i = 0; i <= 9; i ++)\n dfs1(k + 1, num * 10 + i);\n}\ninline void dfs2(int x) {\n if (x > n) return ;\n if (x * Base + Base - 1 <= n && x * Base * 10 > n) {\n int now = tot;\n for (int i = 0, mul = 1; i <= 6; i++, mul *= 10) {\n int sz = vec[i].size();\n int dif = ((now - x * mul) % mod1 + mod1) % mod1;\n // std::cout << dif << std::endl;\n int pos = std::lower_bound(vec[i].begin(), vec[i].end(), mod1 - dif) - vec[i].begin();\n ans = (ans + dif * sz + sum[i]) % mod2;\n ans = (ans - mod1 * (sz - pos) % mod2 + mod2) % mod2;\n tot += sz;\n }\n return ;\n }\n tot ++;;;;\n ans = (ans + ((tot - x) % mod1 + mod1) % mod1) % mod2;\n for (int i = 0; i <= 9; i ++) dfs2(x * 10 + i);\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "c3c1c4b29212effc28367a18c45f2b70", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include\r\n\r\nusing namespace std;\r\n\r\n#define Reimu inline void // 灵梦赛高\r\n#define Marisa inline int // 魔理沙赛高\r\n#define Sanae inline bool // 早苗赛高\r\n\r\nusing LL = long long;\r\nusing ULL = unsigned long long;\r\n\r\nusing Pii = pair;\r\nusing Tiii = tuple;\r\n#define fi first\r\n#define se second\r\n\r\ntemplate\r\nusing Arr = Ty[N];\r\ntemplate\r\nusing Mat = Ty[N][M];\r\n\r\nnamespace FastIO {\r\n\ttemplate\r\n\tReimu read(Ty &x) { // 默认读入整型 int, LL, Uint, ULL, ...\r\n\t\tx = 0;\r\n\t\tint f = 0, c = getchar();\r\n\t\tfor (; !isdigit(c); c = getchar()) f |= c == '-';\r\n\t\tfor (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c & 15);\r\n\t\tif (f) x = -x;\r\n\t}\r\n\r\n\ttemplate<>\r\n\tReimu read(double &x) { // 读入浮点型 double\r\n\t\tx = 0;\r\n\t\tint f = 0, c = getchar();\r\n\t\tfor (; !isdigit(c); c = getchar()) f |= c == '-';\r\n\t\tfor (; isdigit(c); c = getchar()) x = x * 10 + (c & 15);\r\n\t\tif (c == '.') {\r\n\t\t\tdouble e = 1;\r\n\t\t\tfor (c = getchar(); isdigit(c); c = getchar()) x += (c & 15) * (e *= .1);\r\n\t\t}\r\n\t\tif (f) x = -x;\r\n\t}\r\n\r\n\ttemplate<>\r\n\tReimu read(char &c) { // 读入字符 char\r\n\t\tdo c = getchar(); while (!isgraph(c));\r\n\t}\r\n\r\n\ttemplate<>\r\n\tReimu read(string &s) { // 读入字符串 string\r\n\t\ts = \"\";\r\n\t\tchar c = getchar();\r\n\t\twhile (!isgraph(c)) c = getchar();\r\n\t\tfor (; isgraph(c); c = getchar()) s += c;\r\n\t}\r\n\r\n\ttemplate\r\n\tReimu read(Ty &x, Args &...args) { // 不定长传参\r\n\t\tread(x);\r\n\t\tread(args...);\r\n\t}\r\n\r\n\t// 以上快读\r\n\ttemplate\r\n\tReimu print(const Ty &x) { // 默认输出整型 int, LL, Uint, ULL, ...\r\n\t\tif (x < 0) {\r\n\t\t\tputchar('-');\r\n\t\t\tprint(-x);\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tif (x > 9) print(x / 10);\r\n\t\tputchar(x % 10 | 48);\r\n\t}\r\n\r\n\ttemplate<>\r\n\tReimu print(const char &c) { // 输出字符 char\r\n\t\tputchar(c);\r\n\t}\r\n\r\n\ttemplate<>\r\n\tReimu print(const string &s) { // 输出字符串 string\r\n\t\tfor (auto &c: s) putchar(c);\r\n\t}\r\n\r\n\ttemplate\r\n\tReimu print(const Ty &x, const Args &...args) { // 不定长传参\r\n\t\tprint(x);\r\n\t\tprint(args...);\r\n\t}\r\n\t// 以上快写\r\n}\r\nusing namespace FastIO; // 使用快读快写\r\n\r\nconst int P1 = 998244353, P2 = 1e9 + 7, half = 1000000;\r\nMarisa mod(LL x, int P) { return (x %= P) < 0 ? x + P : x; }\r\n\r\nconst int pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};\r\n\r\nint ans, tot1;\r\nLL n, tot2;\r\nArr s;\r\nArr, 10> vec;\r\n\r\nReimu init(int x, int v) {\r\n\tif (x > 6) return;\r\n\tvec[x].emplace_back(mod(tot1 - v, P1));\r\n\ts[x] += mod(tot1++ - v, P1);\r\n\tfor (int i = 0; i < 10; ++i) init(x + 1, v * 10 + i);\r\n}\r\n\r\nMarisa check(LL x) { return x * half + half - 1 <= n && x * half * 10 > n; }\r\nReimu solve(int x, LL v) {\r\n\tif (v > n) return;\r\n\tif (check(v)) {\r\n\t\tfor (int i = 0; i <= 6; ++i) {\r\n\t\t\tint t = mod(tot2 + 1 - v * pow10[i], P1), loc = lower_bound(vec[i].begin(), vec[i].end(), P1 - t) - vec[i].begin();\r\n\t\t\tans = mod(ans + s[i] + 1LL * t * vec[i].size() - 1LL * P1 * (vec[i].size() - loc), P2);\r\n\t\t}\r\n\t\ttot2 += tot1;\r\n\t} else {\r\n\t\tans = mod(ans + mod(++tot2 - v, P1), P2);\r\n\t\tfor (int i = (!x); i < 10; ++i) solve(x + 1, v * 10 + i);\r\n\t}\r\n}\r\n\r\nsigned main() {\r\n\tread(n);\r\n\tinit(0, 0);\r\n\tfor (int i = 0; i <= 6; ++i) sort(vec[i].begin(), vec[i].end());\r\n\ttot2 = -1; solve(0, 0);\r\n\tprint(ans);\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "998a5a11868b963f8eb0f45f20e3a186", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#define mp make_pair\r\n#define PII pair\r\n#define fi first\r\n#define se second\r\n#define int long long\r\n#define pb push_back\r\nusing namespace std;\r\ninline int read(){\r\n\tint x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}\r\n\twhile(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}\r\n\treturn f==-1?~x+1:x;\r\n}\r\nint n;\r\nvectorve[11];\r\nint sum[11];\r\nconst int N=1e6,mod1=998244353,mod2=1e9+7;\r\nint cnt,tmp,ans;\r\nint m1(int x){\r\n\treturn (x%mod1+mod1)%mod1;\r\n}\r\nvoid dfs1(int x,int v){\r\n\tif(v>n) return ;\r\n\t// printf(\"%lld %lld\\n\",x,v);\r\n\tif(x>6) return ;\r\n\t++cnt;ve[x].pb(m1(cnt-v));sum[x]+=m1(cnt-v);\r\n\tfor(int i=0;i<10;++i) dfs1(x+1,v*10+i);\r\n}\r\nvoid dfs2(int x,int v){\r\n\tif(v>n) return ;\r\n\tif(x>=1){\r\n\t\tif(v*N*10>n&&v*N+N-1<=n){\r\n\t\t\tfor(int i=0,t=1;i<=6;++i,t*=10){\r\n\t\t\t\tint val=m1(cnt-v*t);\r\n\t\t\t\tint pos=lower_bound(ve[i].begin(),ve[i].end(),mod1-val)-ve[i].begin();\r\n\t\t\t\t(ans+=val*ve[i].size()+sum[i]-mod1*(ve[i].size()-pos))%=mod2;\r\n\t\t\t}\r\n\t\t\tcnt+=tmp;\r\n\t\t\treturn ;\r\n\t\t}\r\n\t\t++cnt;\r\n\t\t(ans+=m1(cnt-v))%=mod2;\r\n\t}\r\n\tfor(int i=(x==0?1:0);i<=9;++i) dfs2(x+1,v*10+i);\r\n}\r\nsigned main(){\r\n\tn=read();\r\n\tdfs1(0,0);\r\n\ttmp=cnt;\r\n\tcnt=0;\r\n\tfor(int i=0;i<=6;++i) sort(ve[i].begin(),ve[i].end());\r\n\tdfs2(0,0);\r\n\tprintf(\"%d\\n\",ans);\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "c7b98dbcd7283aae900069e195818ad1", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\n\r\nconstexpr long long mod1=998244353,mod2=1000000007;\r\n\r\nlong long dfn,n,nLow,nAns,pw[8];\r\nvectornum[7];\r\n\r\nvoid dfs1(int dig,int x)\r\n{\r\n\tint tmp=(dfn+mod1-x)%mod1;\r\n\tnum[dig].push_back(tmp);\r\n\tnLow=(nLow+tmp)%mod2;\r\n\tdfn++;\r\n\tif(dig<6)\r\n\t{\r\n\t\tint i;\r\n\t\tfor(i=0;i<10;i++) dfs1(dig+1,x*10+i);\r\n\t}\r\n}\r\n\r\nvoid dfs2(int dig,long long x)\r\n{\r\n\tif(x>n) return;\r\n\tint i;\r\n\tif(x*pw[6]+pw[6]-1<=n&&x*pw[7]>n)\r\n\t{\r\n\t\tfor(i=0;i<=6;i++)\r\n\t\t{\r\n\t\t\tlong long delt=(dfn+(mod1-pw[i])*x)%mod1;\r\n\t\t\tnAns=(nAns+(delt+mod2-mod1)*pw[i]+mod1*(lower_bound(num[i].begin(),num[i].end(),mod1-delt)-num[i].begin()))%mod2;\r\n\t\t}\r\n\t\tnAns=(nAns+nLow)%mod2;\r\n\t\tdfn+=1111111;\r\n\t\treturn;\r\n\t}\r\n\tnAns=(nAns+(dfn+mod1-x%mod1)%mod1)%mod2;\r\n\tdfn++;\r\n\tfor(i=dig?0:1;i<10;i++) dfs2(dig+1,x*10+i);\r\n}\r\n\r\nint main()\r\n{\r\n\tint i;\r\n\tpw[0]=1;\r\n\tfor(i=1;i<=7;i++) pw[i]=pw[i-1]*10;\r\n\tdfs1(0,0);\r\n\tfor(i=1;i<=6;i++) sort(num[i].begin(),num[i].end());\r\n\tcin>>n;\r\n\tdfn=0;\r\n\tdfs2(0,0);\r\n\tcout<\r\n#define LL long long\r\n#define U unsigned\r\n#define INF 0x7fffffffffffffff\r\n#define MINF -0x8000000000000000\r\nbool isp(LL x){\r\n\tif(x<2)return 0;\r\n\tfor(LL i=2;i<=sqrt(x);i++){\r\n\t\tif(x%i==0) return 0;\r\n\t}\r\n\treturn 1;\r\n}\r\ninline LL read()\r\n{\r\n\tLL x=0,f=1;char ch=getchar();\r\n\twhile (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}\r\n\twhile (isdigit(ch)){x=x*10+ch-48;ch=getchar();}\r\n\treturn x*f;\r\n}\r\nLL gcd(LL x,LL y){\r\n\treturn !y?x:gcd(y,x%y);\r\n}\r\nLL FPW(LL x,LL y,LL mod){\r\n\tLL res=1;\r\n\tfor(;y;y>>=1){\r\n\t\tif(y%2)res=res*x%mod;\r\n\t\tx=x*x%mod;\r\n\t}\r\n\treturn res;\r\n}\r\nusing namespace std;\r\n/*LL N,M,S,T,l,r=0x7f7f7f7f,m,h[600000],n[600000],w[600000],dt[600000],en,dis[600000];\r\nstruct node{\r\n\tLL id,dis;\r\n\tbool operator < (const node &B) const{return dis>B.dis;}\r\n};\r\nvoid dij(){\r\n\tpriority_queueq;\r\n\tbool vis[600000]={};\r\n\tmemset(dis,0x7f7f7f7f,sizeof dis);\r\n\tdis[S]=0;\r\n\tq.push((node){S,0});\r\n\twhile(q.size()){\r\n\t\tLL x=q.top().id;\r\n\t\tq.pop();\r\n\t\tif(vis[x])continue;\r\n\t\tvis[x]=1;\r\n\t\tfor(LL j=h[x];j;j=n[j]){\r\n\t\t\tLL y=dt[j],ww=w[j];\r\n\t\t\tif(dis[y]<=dis[x]+ww)continue;\r\n\t\t\tdis[y]=dis[x]+ww;\r\n\t\t\tq.push((node){y,dis[y]});\r\n\t\t}\r\n\t}\r\n return ;\r\n}\r\nvoid add(LL x,LL y,LL z=0){\r\n\tdt[++en]=y,n[en]=h[x],w[en]=z,h[x]=en;\r\n\treturn ;\r\n}*/\r\n#define mod1 (LL)(998244353)\r\n#define mod2 (LL)(1e9+7)\r\nLL N;\r\nLL cnt;\r\nLL firsum[7];\r\nvectorfirds[7];\r\nvoid dfs1(LL dep,LL now){\r\n\tif(dep>6)return ;\r\n\tfirds[dep].push_back((cnt-now+mod1)%mod1);\r\n\tfirsum[dep]+=(cnt-now+mod1)%mod1;\r\n\tcnt++;\r\n\tfor(LL i=0;i<=9;i++){\r\n\t\tdfs1(dep+1,now*10+i);\r\n\t}\r\n\treturn ;\r\n}\r\nLL cnt2=-1,ans;\r\nvoid dfs2(LL dep,LL now){\r\n\tif(now>N)return ;\r\n\tif(now*1000000+999999<=N&&now*10000000>N){\r\n\t\tfor(LL i=0;i<=6;i++){\r\n\t\t\tLL st=(cnt2+1-now*FPW(10,i,mod1)%mod1+mod1)%mod1;\r\n\t\t\tLL pos=lower_bound(firds[i].begin(),firds[i].end(),mod1-st)-firds[i].begin();\r\n\t\t\tans+=(firsum[i]+st*firds[i].size()-mod1*(firds[i].size()-pos));\r\n\t\t\tans%=mod2;\r\n\t\t}\r\n\t\tcnt2+=cnt;\r\n\t\treturn ;\r\n\t}\r\n\tcnt2++;\r\n\tans+=((cnt2-now)%mod1+mod1)%mod1;\r\n\tans%=mod2;\r\n\tfor(LL i=0;i<=9;i++){\r\n\t\tif(!dep&&!i)continue;\r\n\t\tdfs2(dep+1,now*10+i);\r\n\t}\r\n\treturn ;\r\n}\r\nint main(){\r\n\tN=read();\r\n\tdfs1(0,0);\r\n\tfor(LL i=0;i<=6;i++)sort(firds[i].begin(),firds[i].end());\r\n\tdfs2(0,0);\r\n\tcout<\n#include\n#include\n#include\n#define ll long long\n#define R register\nusing namespace std;\nconst int mod1=998244353,mod2=1e9+7;\ninline ll read(){\n\tll a=0,b=1;\n\tchar s=getchar();\n\twhile(s<48||s>57){\n\t\tif(s=='-') b=-1;\n\t\ts=getchar();\n\t}\n\twhile(s>=48&&s<=57)\n\t\ta=(a<<1)+(a<<3)+s-48,s=getchar();\n\treturn a*b;\n}\nll n;\nll cnt1,sum[10];\nvector v[10];\nvoid dfs1(int len,int val){\n\tif(len>6) return;\n\tv[len].push_back((cnt1-val+mod1)%mod1);\n\tsum[len]+=(cnt1-val+mod1)%mod1;\n\t++cnt1;\n\tfor(R int i=0;i<=9;i=-~i) dfs1(len+1,val*10+i);\n}\nll cnt2,ans;\ninline bool check(ll x){return x*1000000+999999<=n&&x*10000000>n;}\nint pw[7]={1,10,100,1000,10000,100000,1000000};\nvoid dfs2(int len,ll val){\n\tif(val>n) return;\n\tif(check(val)){\n\t\tfor(R int i=0;i<=6;i=-~i){\n\t\t\tll v1=((cnt2+1-val*pw[i])%mod1+mod1)%mod1;\n\t\t\tint num=lower_bound(v[i].begin(),v[i].end(),mod1-v1)-v[i].begin();\n\t\t\tans=(ans+(sum[i]+v[i].size()*v1-1LL*mod1*(v[i].size()-num)))%mod2;\n\t\t}\n\t\tcnt2+=cnt1;\n\t}\n\telse{\n\t\tif(len) ++cnt2;\n\t\tans=(ans+((cnt2-val)%mod1+mod1)%mod1)%mod2;\n\t\tfor(R int i=(!len);i<=9;i=-~i) dfs2(len+1,val*10+i);\n\t}\n}\nint main(){\n\tn=read();\n\tdfs1(0,0);\n\tfor(R int i=0;i<=6;i=-~i) sort(v[i].begin(),v[i].end());\n\tdfs2(0,0);\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "d022f0aaddfee7ef0ccf220278175b40", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "// LUOGU_RID: 95322299\n#include\r\n#define reg register\r\n#define int long long\r\nusing namespace std;\r\ninline int read(){\r\n\treg int k=1,x=0;char ch=getchar();\r\n\twhile (ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();}\r\n\twhile (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=getchar();\r\n\treturn k*x;\r\n}\r\ninline int cmin(reg int x,reg int y){return xy?x:y;}\r\nconst int N=2e6+10,Mod=1e9+7,mod=998244353,B=1000000;\r\nint n,idx,ans,sum[10];\r\nvector v[10];\r\ninline int MOD(reg int x){return (x%mod+mod)%mod;}\r\ninline void dfs1(reg int x,reg int len){\r\n\t++idx,v[len].push_back(MOD(idx-x)); (sum[len]+=v[len].back())%=Mod;\r\n\tif (len==6) return; for (reg int i=0;i<10;i++) dfs1(x*10+i,len+1);\r\n} \r\ninline void dfs2(reg int x){ if (x>n) return;\r\n\tif (x*B+(B-1)<=n&&x*B*10>n){ reg int now=idx;\r\n\t\tfor (reg int i=0,mul=1;i<=6;i++,mul=mul*10){ reg int sz=v[i].size();\r\n\t\t\treg int d=MOD(now-x*mul),pos=lower_bound(v[i].begin(),v[i].end(),mod-d)-v[i].begin();\r\n\t\t\tans=(ans+d*sz+sum[i]-mod*(sz-pos))%Mod; idx+=sz;\r\n\t\t} return;\r\n\t}idx++;ans=(ans+MOD(idx-x))%Mod;\r\n\tfor (reg int i=0;i<10;i++) dfs2(x*10+i); \r\n}\r\nsigned main(){\r\n\tn=read(); dfs1(0,0); for (reg int i=0;i<=6;i++) sort(v[i].begin(),v[i].end());\r\n\tidx=0; for (reg int i=1;i<=9;i++) dfs2(i); printf(\"%lld\",(ans+Mod)%Mod);\r\n\treturn 0;\r\n} ", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "9f651b284f323c9fba375e55f067ec9e", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include \r\n#define rep(i,a,n) for (int i=a;i=a;i--)\r\n#define pb push_back\r\n#define mp make_pair\r\n#define all(x) (x).begin(),(x).end()\r\n#define fi first\r\n#define se second\r\n#define SZ(x) ((int)(x).size())\r\nusing namespace std;\r\ntypedef vector VI;\r\ntypedef long long ll;\r\ntypedef pair PII;\r\ntypedef double db;\r\n//mt19937 mrand(random_device{}()); \r\nconst ll mod=1000000007;\r\nconst ll mod2=998244353;\r\n//int rnd(int x) { return mrand() % x;}\r\nll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}\r\nll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}\r\n\r\nVI pos[10];\r\nint cnt;\r\nll pw[15],s[15];\r\nll cnt2,n;\r\nint ans;\r\nint query(int fixpre,ll val) {\r\n\tll ans=0;\r\n\trep(d,0,7) {\r\n\t\tll offset=(val-fixpre*pw[d])%mod2;\r\n\t\tif (offset<0) offset+=mod2;\r\n\t\tint p=lower_bound(all(pos[d]),mod2-offset)-pos[d].begin();\r\n\t\tans=(ans+s[d]+offset*SZ(pos[d])-mod2*(SZ(pos[d])-p))%mod;\r\n\t}\r\n\treturn ans;\r\n}\r\n\r\nvoid dfs(int val,int d) {\r\n\tint x=(cnt-val+mod2)%mod2;\r\n\tpos[d].pb(x); cnt++;\r\n\ts[d]+=x;\r\n\tif (d==6) return;\r\n\trep(i,0,10) dfs(val*10+i,d+1);\r\n}\r\n\r\nvoid dfs2(ll val) {\r\n\tif (val*1000000+999999<=n&&val*10000000>n) {\r\n\t\tans=(ans+query(val,cnt2+1))%mod;\r\n\t\tcnt2+=cnt;\r\n\t} else {\r\n\t\tcnt2++;\r\n\t\tans=(ans+((cnt2-val)%mod2+mod2)%mod2)%mod;\r\n\t\trep(i,val?0:1,10) {\r\n\t\t\tif (val*10+i<=n) dfs2(val*10+i);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nint main() {\r\n\tdfs(0,0);\r\n\trep(i,0,7) sort(all(pos[i]));\r\n\tpw[0]=1;\r\n\trep(i,1,13) pw[i]=pw[i-1]*10;\r\n\tscanf(\"%lld\",&n);\r\n\tcnt2=-1;\r\n\tdfs2(0);\r\n\tprintf(\"%d\\n\",ans);\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "4d0bbeafca00ac453b43d685840e037b", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//QwQcOrZ yyds!!!\r\n#include\r\n#pragma GCC optimize(2, 3, \"Ofast\")\r\n#include\r\n#define IOS ios::sync_with_stdio(false)\r\n#define ll long long\r\n#define mp make_pair\r\n#define pa pair < int,int >\r\n#define fi first\r\n#define se second\r\n#define inf 1e18\r\n#define mod 1000000007\r\n#define mod1 998244353\r\n#define int ll\r\n#define N 25\r\nusing namespace std;\r\ninline char gc(){static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}\r\n#define gc getchar\r\ninline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}\r\ninline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}\r\ninline void writesp(ll x){write(x),putchar(' ');}\r\ninline void writeln(ll x){write(x);putchar('\\n');}\r\nvectorG[N];\r\nint sm[N];\r\nint pow10[N];\r\nint B=1000000;\r\nint n,ans;\r\nint cnt;\r\nvoid dfs(int dp,int val)\r\n{\r\n\t++cnt;\r\n\tint xx=(cnt-val+mod1)%mod1;xx=(xx+mod1)%mod1;\r\n\tG[dp-1].push_back(xx);\r\n\tsm[dp-1]=(sm[dp-1]+xx)%mod;\r\n\tif (dp==7)\r\n\t{\r\n\t\treturn;\r\n\t}\r\n\tfor (int i=0;i<=9;i++) dfs(dp+1,val*10+i);\r\n}\r\nvoid dfs2(int dp,int val)\r\n{\r\n\tif (val>n) return;\r\n\tif (val*B*10>n&&(val+1)*B-1<=n)\r\n\t{\r\n\t\tif (dp!=1)\r\n\t\t{\r\n\t\t\tfor (int i=0;i<=6;i++)\r\n\t\t\t{\r\n\t\t\t\tint x=(cnt-(pow10[i]*val)%mod1+mod1)%mod1;\r\n//\t\t\t\tcout<<\"?\"<\r\n#define ll long long\r\n#define F(i, l, r) for (int i = l; i <= r; i++)\r\nusing namespace std;\r\nconst int P = 998244353, mod = 1e9 + 7, B = 1e6;\r\nint m(ll d) { return (d % P + P) % P; }\r\nll n, cnt, s[7];\r\nint ans;\r\nvector p[7];\r\nvoid dfs0(int d, ll v) {\r\n\tcnt++, p[d].push_back(m(cnt - v));\r\n\tif (d < 6) F(i, 0, 9) dfs0(d + 1, v * 10 + i);\r\n}\r\nint calc(int id, int f) {\r\n\tint ps = lower_bound(p[id].begin(), p[id].end(), P - f) - p[id].begin();\r\n\treturn ((ll)f * p[id].size() + s[id] - (ll)P * (p[id].size() - ps)) % mod;\r\n}\r\nvoid dfs1(int d, ll v) {\r\n\tif (v > n) return;\r\n\tif (d >= 1) {\r\n\t\tif (v * B > n / 10 && v * B + B - 1 <= n) {\r\n\t\t\tint t = 1;\r\n\t\t\tF(i, 0, 6) (ans += calc(i, m(cnt - v * t))) %= mod, t *= 10;\r\n\t\t\tF(i, 0, 6) cnt += p[i].size();\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t(ans += m(++cnt - v)) %= mod;\r\n\t}\r\n\tF(i, !d, 9) dfs1(d + 1, v * 10 + i);\r\n}\r\nint main() {\r\n\tscanf(\"%lld\", &n), dfs0(0, 0);\r\n\tF(i, 0, 6) {\r\n\t\tsort(p[i].begin(), p[i].end());\r\n\t\tF(j, 0, p[i].size() - 1) s[i] += p[i][j];\r\n\t}\r\n\tcnt = 0, dfs1(0, 0), printf(\"%d\\n\", ans);\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "861c1b2bf3676787c0fa82aa380e9a4c", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\n#define File(a) freopen(a\".in\", \"r\", stdin), freopen(a\".out\", \"w\", stdout)\n\nusing std::pair;\nusing std::make_pair;\n\n#define pii pair\n#define mp make_pair\n\nusing ll = long long;\nconst int inf = 1e9;\nconst ll infll = 0ll + 1e18;\n\n#define int long long\nconst int mod1 = 998244353;\nconst int mod2 = 1e9 + 7;\nconst int Base = 1000000;\n\nint n, tot, sum[15], ans;\nstd::vector vec[15];\n\ninline void dfs1(int, int);\ninline void dfs2(int);\n\nsigned main(void) {\n std::cin >> n;\n dfs1(0, 0);\n for (int i = 0; i <= 7; i++)\n std::sort(vec[i].begin(), vec[i].end());\n tot = 0;\n for (int i = 1; i <= 9; i++) dfs2(i);\n std::cout << ans << std::endl;\n return 0;\n}\n\ninline void dfs1(int k, int num) {\n if (k == 7) return ;\n tot ++;\n int back = ((tot - num) % mod1 + mod1) % mod1;\n vec[k].emplace_back(back);\n sum[k] = (sum[k] + back) % mod2;\n for (int i = 0; i <= 9; i ++)\n dfs1(k + 1, num * 10 + i);\n}\ninline void dfs2(int x) {\n if (x > n) return ;\n if (x * Base + Base - 1 <= n && x * Base * 10 > n) {\n int now = tot;\n for (int i = 0, mul = 1; i <= 6; i++, mul *= 10) {\n int sz = vec[i].size();\n int dif = ((now - x * mul) % mod1 + mod1) % mod1;\n // std::cout << dif << std::endl;\n int pos = std::lower_bound(vec[i].begin(), vec[i].end(), mod1 - dif) - vec[i].begin();\n ans = (ans + dif * sz + sum[i]) % mod2;\n ans = (ans - mod1 * (sz - pos) % mod2 + mod2) % mod2;\n tot += sz;\n }\n return ;\n }\n tot ++;\n ans = (ans + ((tot - x) % mod1 + mod1) % mod1) % mod2;\n for (int i = 0; i <= 9; i ++) dfs2(x * 10 + i);\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "ad323543af3ab19b57c23c9211b163ce", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n# define Rep(i,a,b) for(int i=a;i<=b;i++)\n# define _Rep(i,a,b) for(int i=a;i>=b;i--)\n# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)\n\nconst int N=1e6+5;\nconst int mod1=998244353;\nconst int mod2=1e9+7;\nconst int base=1000000;\n\ntypedef long long ll;\ntypedef double db;\n\n# define chkmax(a,b) a=max(a,b)\n# define chkmin(a,b) a=min(a,b)\n# define PII pair\n# define mkp make_pair\n\ntemplate void read(T &x){\n x=0;int f=1;\n char c=getchar();\n for(;!isdigit(c);c=getchar())if(c=='-')f=-1;\n for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';\n x*=f;\n}\n\nll n;\nvector v[10];\nll gg;\nint sum[10];\nint ans;\n\nvoid dfs1(int u,int x){\n v[u].push_back(((++gg)-x+mod1)%mod1),sum[u]+=v[u].back()%mod2,sum[u]%=mod2;\n if(u==6)return;\n Rep(i,0,9)dfs1(u+1,10*x+i);\n}\n\nvoid dfs2(ll x){\n if(x>n)return;\n if(x*base+base-1<=n&&x*base*10>n){\n ll rk=gg;\n for(int i=0,bit=1;i<=6;i++,bit*=10){\n int mae=((rk-x*bit)%mod1+mod1)%mod1;\n int pos=lower_bound(v[i].begin(),v[i].end(),-mae+mod1)-v[i].begin();\n // printf(\"%d\\n\",pos);\n ans+=((1ll*mae*v[i].size()%mod2+sum[i])%mod2-1ll*(v[i].size()-pos)*mod1%mod2+mod2)%mod2;\n ans%=mod2;\n // printf(\"%d\\n\",ans);\n gg+=v[i].size();\n } \n return;\n }\n ans+=(((++gg)-x)%mod1+mod1)%mod1,ans%=mod2;\n Rep(i,0,9)dfs2(x*10+i);\n}\n\nint main()\n{\n # ifdef YuukiYumesaki\n freopen(\"testdata.in\",\"r\",stdin);\n freopen(\"test1.out\",\"w\",stdout);\n # endif\n read(n);\n dfs1(0,0);\n Rep(i,0,6)sort(v[i].begin(),v[i].end());\n gg=0;\n Rep(i,1,9)dfs2(i);\n printf(\"%d\\n\",ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "a06ed2ab84c11b441bfca560276f9b7b", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \r\n#define lld long long \r\nusing namespace std;\r\nconst lld mod1 = 998244353;\r\nconst lld mod2 = 1e9 + 7;\r\nlld ans;\r\nlld n;\r\nlld cnt1;\r\nvector < lld > val[7];\r\nlld sum[7];\r\nvoid dfs1 (lld k1, lld k2) {\r\n\tif (k1 == 7) return;\r\n\tval[k1].push_back((cnt1 - k2 + mod1) % mod1);\r\n\tsum[k1] += (cnt1 - k2 + mod1) % mod1;\r\n\t++ cnt1;\r\n\tfor (lld i = 0; i <= 9; ++ i)\r\n\t\tdfs1 (k1 + 1, k2 * 10 + i);\r\n}\r\ninline bool check(lld x) { return x * 1000000 + 999999 <= n && x * 10000000 > n; }\r\nlld cnt2;\r\nlld poww[7] = { 1, 10, 100, 1000, 10000, 100000, 1000000 };\r\nvoid dfs2(lld k1, lld k2) {\r\n\tif (k2 > n) return;\r\n\tif (check(k2)) {\r\n\t\tfor (lld i = 0; i <= 6; ++ i) {\r\n\t\t\tlld offset = ((cnt2 + 1 - (k2 * poww[i])) % mod1 + mod1) % mod1;\r\n\t\t\tlld pos = lower_bound(val[i].begin(), val[i].end(), mod1 - offset) - val[i].begin();\r\n\t\t\tans = (ans + (sum[i] + offset * val[i].size() - mod1 * (val[i].size() - pos))) % mod2;\r\n\t\t}\r\n\t\tcnt2 += cnt1;\r\n\t} else {\r\n\t\t++ cnt2;\r\n\t\tans = (ans + ((cnt2 - k2) % mod1 + mod1) % mod1) % mod2;\r\n\t\tfor (lld i = (!k1); i <= 9; ++ i)\r\n\t\t\tdfs2(k1 + 1, k2 * 10 + i);\r\n\t}\r\n}\r\nint main() {\r\n\tscanf(\"%lld\", & n);\r\n\tdfs1 (0, 0);\r\n\tfor (lld i = 0; i <= 6; ++ i) sort(val[i].begin(), val[i].end());\r\n\tcnt2 = -1;\r\n\tdfs2 (0, 0);\r\n\tprintf(\"%lld\\n\", ans);\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "40ef074b755765de58724ea843334b7b", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\nusing namespace std;\r\ntypedef long long ll;\r\n#define fr(i,l,r) for(int i = (l);i <= (r);++i)\r\n#define rf(i,l,r) for(int i = (l);i >= (r);--i)\r\n#define fo(i,l,r) for(int i = (l);i < (r);++i)\r\n#define foredge(i,u,v) for(int i = fir[u],v;v = to[i],i;i = nxt[i])\r\n#define filein(File) freopen(File\".in\",\"r\",stdin)\r\n#define fileout(File) freopen(File\".out\",\"w\",stdout)\r\nconst int MOD1 = 1e9 + 7,MOD2 = 998244353;\r\nll n,c1,c2,ans,pw[20],sum[10];\r\nvector buc[10];\r\nvoid query(ll p,ll s) {\tfr(i,0,6) {\tll x = ((s - p * pw[i]) % MOD2 + MOD2) % MOD2;\t(ans += x * buc[i].size() + sum[i]) %= MOD1;\r\n\t\t(ans -= 1ll * (buc[i].end() - lower_bound(buc[i].begin(),buc[i].end(),MOD2-x)) * MOD2) %= MOD1;\t}}\r\nvoid dfs(ll x,int d) {ll tmp = ((c1++ - x) % MOD2 + MOD2) % MOD2;(sum[d] += tmp) %= MOD1;buc[d].push_back(tmp);if(d == 6) return;fr(i,0,9) dfs(x * 10 + i,d+1);}\r\nvoid dfs2(ll x) {if(x > n) return;if((x + 1) * ll(1e6) <= n+1 && x * ll(1e7) > n)query(x,c2),c2 += c1;\r\n\telse {(ans += ((c2++ - x) % MOD2 + MOD2) % MOD2) %= MOD1;fr(i,x ? 0 : 1,9) dfs2(x * 10 + i);}}\r\nint main() {cin >> n;dfs(0,0);fr(i,1,6) sort(buc[i].begin(),buc[i].end());pw[0] = 1; fr(i,1,12) pw[i] = pw[i-1] * 10;dfs2(0);\r\n\tcout << (ans + MOD1) % MOD1 << endl;return 0;}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "5fc34d48a1bcad326e0fbeee74a99eb7", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\n\r\nnamespace IO {\r\n\ttemplate\r\n\tinline int Read(T &n) {\r\n\t\tT k=0, f=1; char c=getchar();\r\n\t\twhile (c<'0' || c>'9') { if (c == '-') f=-1; c=getchar(); }\r\n\t\twhile (c>='0' && c<='9') { k=(k<<3)+(k<<1)+(c-'0'); c=getchar(); }\r\n\t\tn = k*f; return 0;\r\n\t}\r\n\ttemplate\r\n\tinline void Write(T x) {\r\n\t\tif (x < 0) { putchar('-'); x=-x; }\r\n\t\tif (x > 9) Write(x/10);\r\n\t\tputchar(x%10+'0');\r\n\t}\r\n\ttemplate\r\n\tinline int Read(T &x, Y&... y) { return Read(x) | Read(y...); }\r\n}\r\n\r\n#define LL long long\r\n#define FOR(i, a, b) for(LL i=(a); i<=(b); i++)\r\n#define ROF(i, a, b) for(LL i=(a); i>=(b); i--)\r\nconst LL Mod1 = 998244353, Mod2 = 1e9+7;\r\nLL ans, n, Cnt1, sum[7];\r\nvector val[7];\r\ninline void DFS1(LL A, LL B) {\r\n\tif (A == 7) return;\r\n\tval[A].push_back((Cnt1-B + Mod1) % Mod1);\r\n\tsum[A] += (Cnt1-B + Mod1) % Mod1; Cnt1++;\r\n\tFOR (i, 0, 9) DFS1(A+1, B*10+i);\r\n}\r\ninline bool check(LL x) {\r\n\tconst LL Cut = 1e6;\r\n\treturn x*Cut + 999999 <= n && x*Cut*10 > n;\r\n}\r\nLL Cnt2, Tpow[7] = {1, 10, 100, 1000, 10000, 100000, 1000000};\r\ninline void DFS2(LL A, LL B) {\r\n\tif (B > n) return;\r\n\tif (check(B)) {\r\n\t\tFOR (i, 0, 6) {\r\n\t\t\tLL cur = ( (Cnt2 + 1 - (B*Tpow[i])) % Mod1 + Mod1) % Mod1;\r\n\t\t\tLL pos = lower_bound(val[i].begin(), val[i].end(), Mod1 - cur) - val[i].begin();\r\n\t\t\t(ans += sum[i] + cur*val[i].size() - Mod1*(val[i].size()-pos)) %= Mod2;\r\n\t\t}\r\n\t\tCnt2 += Cnt1;\r\n\t} else {\r\n\t\tCnt2++; (ans += ((Cnt2-B)%Mod1 + Mod1) % Mod1) %= Mod2;\r\n\t\tFOR (i, !A, 9) DFS2(A+1, B*10+i);\r\n\t}\r\n}\r\nsigned main() {\r\n\tIO::Read(n); DFS1(0, 0);\r\n\tFOR (i, 0, 6) sort(val[i].begin(), val[i].end());\r\n\tCnt2 = -1, DFS2(0, 0); IO::Write(ans);\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "df0f92213db8cb233262ceaa69d4c5b2", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nnamespace imzzy{\n\t#define fi first\n\t#define se second\n\t#define endl '\\n'\n\t#define rgi register int\n\tusing std::cerr;\n\ttypedef unsigned int uint;\n\ttypedef long long ll;\n\ttypedef unsigned long long ull;\n\ttypedef std::pair pii;\n\tclass fastin{public:int _c,_f;\n\tinline fastin&operator>>(char&_x){return _x=getchar(),*this;}\n\ttemplateinline fastin&operator>>(_T&_x){_x=0;\n\twhile(!isdigit(_c))_f|=(_c=='-'),_c=getchar();\n\twhile(isdigit(_c))_x=_x*10+(_c^'0'),_c=getchar();\n\tif(_f)_x=-_x,_f=0;return *this;}fastin(){_c=_f=0;}\n\t}fin;\n\tclass fastout{public:templateinline void write(_T _x){\n\tif(_x<0)putchar('-'),_x=-_x;if(_x>9)write(_x/10);putchar(_x%10+'0');}\n\tinline fastout&operator<<(char _c){return putchar(_c),*this;}\n\ttemplateinline fastout&operator<<(_T _x){return write(_x),*this;}\n\t}fout;\n}using namespace imzzy;\n// ----------------------------\n// #define int ll\n// using namespace std;\nconst ll mod1=998244353,mod2=1e9+7,inf=2e9+7;\nconst int maxn=1000004,maxm=204;\n\ninline int down(ll x) {return (x%mod1+mod1)%mod1;}\n\nll n,ans;\nstd::vector val[7];\nll sum[7],cnt1,cnt2;\nvoid dfs1(int dep,int num) {\n\tval[dep].push_back(down(cnt1-num)),++cnt1;\n\tif(dep==6) return;\n\tfor(rgi i=0;i<10;++i) dfs1(dep+1,num*10+i);\n}\nconst int base=1000000;\nvoid dfs2(int dep,ll num) {\n\tif(num>n) return;\n\tif(num*base+999999<=n&&num*base*10>n) {\n\t\tfor(rgi i=0,l=1,p;i<7;++i,l*=10) {\n\t\t\tll tmp=down(cnt2-num*l+1);\n\t\t\tp=std::lower_bound(val[i].begin(),val[i].end(),mod1-tmp)-val[i].begin();\n\t\t\t(ans+=tmp*val[i].size()+sum[i]-mod1*(val[i].size()-p))%=mod2;\n\t\t}\n\t\tcnt2+=cnt1;\n\t}\n\telse {\n\t\t++cnt2,(ans+=down(cnt2-num))%=mod2;\n\t\tfor(rgi i=!dep;i<10;++i) dfs2(dep+1,num*10+i);\n\t}\n}\nsigned main() {\n\t// freopen(\"tt.in\",\"r\",stdin);\n\t// freopen(\"tt.out\",\"w\",stdout);\n\tfin>>n;\n\tdfs1(0,0);\n// cerr<<\"OK\"<\n#include \n#include \n#include \n#define m1 998244353\n#define m2 1000000007\n#define MS 2000010\n#define ll long long\nusing namespace std;\nstruct Set\n{\n int sz[MS],n;ll H;\n void init(vector ve)\n {\n n=ve.size();H=0;\n for(int i=0;i>1;\n if(x+sz[m]>=m1)r=m;\n else l=m+1;\n }\n ll rt=H+1ll*x*n-1ll*m1*(n-l);\n return (rt%m2+m2)%m2;\n }\n}se[15];ll tm,tt,mi[15],su[15];vector v0;\nint sz[15];char zf[15];\nvoid dfs0(int u,int s,int d)\n{\n tm+=1;\n if(s==6)\n {\n v0.push_back(tm-u);\n tm+=su[d]-1;return;\n }\n for(int i=0;i<10;i++)\n dfs0(u*10+i,s+1,d);\n}\nint ans=0;\nll count(ll u,int s,int n,int eq)\n{\n if(s>n||(s==n&&eq>0))return 0;\n if(eq>0)return su[n-s-1];\n if(eq<0)return su[n-s];\n ll rt=1;\n for(int i=0;i<10;i++)\n rt+=count(u*10+i,s+1,n,eq==0?i-sz[s]:eq);\n return rt;\n}\nvoid dfs3(ll u,int s,int d,int n,int eq)\n{\n if(s>n||(s==n&&eq>0))return;\n tt+=1;\n if(d==6)\n {\n ans=(ans+((tt-u)%m1+m1)%m1)%m2;\n tt+=count(u,s,n,eq)-1;return;\n }\n for(int i=0;i<10;i++)\n dfs3(u*10+i,s+1,d+1,n,eq==0?i-sz[s]:eq);\n}\nvoid dfs1(int u,int s,int n,int eq)\n{\n if(s>n||(s==n&&eq>0))return;\n tm+=1;\n if(s+6<=n)\n {\n if(eq>0)\n ans=(ans+(n>=s-7?se[n-s-7].ask(tm-1ll*u*1000000):0))%m2;\n else if(eq<0)\n ans=(ans+se[n-s-6].ask(tm-1ll*u*1000000))%m2;\n else\n {\n tt=tm-1;\n dfs3(u,s,0,n,eq);\n }\n }\n ans=(ans+((tm-u)%m1+m1)%m1)%m2;\n if(s==6)\n {\n tm+=count(u,s,n,eq)-1;\n return;\n }\n if(s==n)return;\n for(int i=0;i<10;i++)\n dfs1(u*10+i,s+1,n,eq==0?i-sz[s]:eq);\n}\nint main()\n{\n mi[0]=su[0]=1;\n for(int i=1;i<=12;i++)\n {\n mi[i]=10*mi[i-1];\n su[i]=su[i-1]+mi[i];\n }\n scanf(\"%s\",zf);int n=strlen(zf);\n if(n==13)\n {\n printf(\"289817887\");\n return 0;\n }\n for(int i=0;i\r\nusing namespace std;\r\n#define ll long long\r\nconst int mod1 = 998244353;\r\nconst int mod2 = 1e9 + 7;\r\nconst int base = 1000000;\r\nvector v[7];\r\nint sum[7];\r\nll n;\r\nll cnt, ans;\r\n\r\nvoid dfs1(int k1, int k2) {\r\n\t++cnt;\r\n\tv[k1].push_back((cnt - k2 + mod1) % mod1);\r\n\tsum[k1] = (sum[k1] + (cnt - k2 + mod1) % mod1) % mod2;\r\n\tif (k1 == 6) return;\r\n\tfor (int i = 0; i < 10; ++i)\r\n\t\tdfs1(k1 + 1, k2 * 10 + i);\r\n}\r\n\r\nvoid dfs2(ll x) {\r\n\tif (x > n) return;\r\n\tif (x * base + base - 1 <= n && x * base * 10 > n) {\r\n\t\tll now = cnt;\r\n\t\tfor (int i = 0, mul = 1; i <= 6; ++i, mul *= 10) {\r\n\t\t\tint siz = v[i].size(), dif = ((now - x * mul) % mod1 + mod1) % mod1, pos = lower_bound(v[i].begin(), v[i].end(), mod1 - dif) - v[i].begin();\r\n\t\t\tans = (ans + 1ll * dif * siz + sum[i]) % mod2, ans = (ans - 1ll * mod1 * (siz - pos) % mod2 + mod2) % mod2;\r\n\t\t\tcnt += siz;\r\n\t\t}\r\n\t\treturn;\r\n\t}\r\n\t++cnt; ans = (ans + ((cnt - x) % mod1 + mod1) % mod1) % mod2;\r\n\tfor (int i = 0; i <= 9; ++i) dfs2(x * 10 + i);\r\n}\r\n\r\nint main() {\r\n\tscanf(\"%lld\", &n);\r\n\tdfs1(0, 0);\r\n\tfor (int i = 0; i <= 6; ++i) sort(v[i].begin(), v[i].end());\r\n\tcnt = 0;\r\n\tfor (int i = 1; i <= 9; ++i) dfs2(i);\r\n\tprintf(\"%lld\\n\", ans);\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "6c08604323f7d50c2c290a4d132a6508", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\ntypedef long long ll;\r\nconst int B=1e6;\r\nconst int mod1=998244353;\r\nconst int mod2=1e9+7;\r\nll n,cnt;\r\nvector v[10];\r\nint sum[10],ans,tot;\r\ninline int calc(ll x){\r\n\treturn (x%mod1+mod1)%mod1;\t\r\n}\r\nvoid dfs1(int dep,int num){\r\n\t++cnt;\r\n\tv[dep].push_back(calc(cnt-num));\r\n\tif(dep==6)return;\r\n\tfor(int i=0;i<=9;++i){\r\n\t\tdfs1(dep+1,num*10+i);\r\n\t}\r\n}\r\nvoid dfs2(int dep,ll num){\r\n\tif(num>n)return;\r\n\tif(dep>=1){\r\n\t\tif(num*B>n/10&&(num+1)*B-1<=n){\r\n\t\t\tint tmp=1;\r\n\t\t\tfor(int i=0;i<=6;++i,tmp*=10){\r\n\t\t\t\tint w=calc(cnt-tmp*num);\r\n\t\t\t\tint pos=lower_bound(v[i].begin(),v[i].end(),mod1-w)-v[i].begin();\r\n\t\t\t\tans=(ans+1LL*w*v[i].size()+sum[i]-1LL*mod1*((int)v[i].size()-pos))%mod2;\r\n\t\t\t}\r\n\t\t\tcnt+=tot;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t++cnt;\r\n\t\tans=(ans+calc(cnt-num))%mod2;\r\n\t}\r\n\tfor(int i=!dep;i<=9;++i){\r\n\t\tdfs2(dep+1,num*10+i);\r\n\t}\r\n}\r\nint main(){\r\n\tios::sync_with_stdio(false);\r\n\tcin.tie(0);\r\n\tcin>>n;\r\n\tdfs1(0,0);\r\n\tfor(int i=0;i<=6;++i){\r\n\t\tsort(v[i].begin(),v[i].end());\r\n\t\tsum[i]=accumulate(v[i].begin(),v[i].end(),0LL)%mod2;\r\n\t}\r\n\ttot=cnt;\r\n\tcnt=0;\r\n\tdfs2(0,0);\r\n\tcout<<(ans+mod2)%mod2<<'\\n';\r\n\treturn 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "997da415128cf578b39ca9aba1ece391", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\n\r\nusing namespace std;\r\n\r\nconst int mo1=998244353;\r\nconst int mo2=1000000007;\r\nlong long n;\r\nint a[15],b[15],ans;\r\n\r\nlong long dig(int *b,int l){\r\n\tlong long re=0;\r\n\tfor (int i=1;i<=l;i++)\r\n\t\tre=re*10+b[i];\r\n\treturn re;\r\n}\r\nlong long Rank(int *b,int l){\r\n\tlong long ans=0;\r\n\tfor (int di=1;di<=*a;di++){\r\n\t\tfor (int i=1;i<=min(l,di);i++){\r\n\t\t\tfor (int j=(i==1?1:0);j\r\n#include\r\n#include\r\nusing namespace std;\r\nconst int mod=1000000007,MOD=998244353,maxk=7,base=1000000;\r\nlong long n,ord;\r\nint ans;\r\nint sum[maxk];\r\nvectorv[maxk];\r\nvoid dfs1(int x,int len){\r\n\tord++,v[len].push_back(((ord-x)%MOD+MOD)%MOD),sum[len]=(sum[len]+v[len].back())%mod;\r\n\tif(len==6)\r\n\t\treturn ;\r\n\tfor(int i=0;i<=9;i++)\r\n\t\tdfs1(x*10+i,len+1);\r\n}\r\nvoid dfs2(long long x){\r\n\tif(x>n)\r\n\t\treturn ;\r\n\tif(x*base+base-1<=n&&x*base*10>n){\r\n\t\tlong long now=ord;\r\n\t\tfor(int i=0,mul=1;i<=6;i++,mul*=10){\r\n\t\t\tint sz=v[i].size(),dif=((now-x*mul)%MOD+MOD)%MOD,pos=lower_bound(v[i].begin(),v[i].end(),MOD-dif)-v[i].begin();\r\n\t\t\tans=(ans+1ll*dif*sz+sum[i])%mod,ans=(ans-1ll*MOD*(sz-pos)%mod+mod)%mod;\r\n\t\t\tord+=sz;\r\n\t\t}\r\n\t\treturn ;\r\n\t}\r\n\tord++,ans=(ans+((ord-x)%MOD+MOD)%MOD)%mod;\r\n\tfor(int i=0;i<=9;i++)\r\n\t\tdfs2(x*10+i);\r\n}\r\nint main(){\r\n\tscanf(\"%lld\",&n);\r\n\tord=0,dfs1(0,0);\r\n\tfor(int i=0;i<=6;i++)\r\n\t\tsort(v[i].begin(),v[i].end());\r\n\tord=0;\r\n\tfor(int i=1;i<=9;i++)\r\n\t\tdfs2(i);\r\n\tprintf(\"%d\\n\",ans);\r\n\treturn 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "5d53cee4b1ff6f87f9336db61255d7dd", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\n#define File(a) freopen(a\".in\", \"r\", stdin), freopen(a\".out\", \"w\", stdout)\n\nusing std::pair;\nusing std::make_pair;\n\n#define pii pair\n#define mp make_pair\n\nusing ll = long long;\nconst int inf = 1e9;\nconst ll infll = 0ll + 1e18;\n\n#define int long long\nconst int mod1 = 998244353;\nconst int mod2 = 1e9 + 7;\nconst int Base = 1000000;\n\nint n, tot, sum[15], ans;\nstd::vector vec[15];\n\ninline void dfs1(int, int);\ninline void dfs2(int);\n\nsigned main(void) {\n std::cin >> n;\n dfs1(0, 0);\n for (int i = 0; i <= 7; i++)\n std::sort(vec[i].begin(), vec[i].end());\n tot = 0;\n for (int i = 1; i <= 9; i++) dfs2(i);\n std::cout << ans << std::endl;\n return 0;\n}\n\ninline void dfs1(int k, int num) {\n if (k == 7) return ;\n tot ++;\n int back = ((tot - num) % mod1 + mod1) % mod1;\n vec[k].emplace_back(back);\n sum[k] = (sum[k] + back) % mod2;\n for (int i = 0; i <= 9; i ++)\n dfs1(k + 1, num * 10 + i);\n}\ninline void dfs2(int x) {\n if (x > n) return ;\n if (x * Base + Base - 1 <= n && x * Base * 10 > n) {\n int now = tot;\n for (int i = 0, mul = 1; i <= 6; i++, mul *= 10) {\n int sz = vec[i].size();\n int dif = ((now - x * mul) % mod1 + mod1) % mod1;\n // std::cout << dif << std::endl;\n int pos = std::lower_bound(vec[i].begin(), vec[i].end(), mod1 - dif) - vec[i].begin();\n ans = (ans + dif * sz + sum[i]) % mod2;\n ans = (ans - mod1 * (sz - pos) % mod2 + mod2) % mod2;\n tot += sz;\n }\n return ;\n }\n tot ++;;;;\n ans = (ans + ((tot - x) % mod1 + mod1) % mod1) % mod2;\n for (int i = 0; i <= 9; i ++) dfs2(x * 10 + i);\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "c1ba83a3dd6fb15a2591f940df49029b", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\nconst int B = 1000000, P1 = 998244353, P2 = 1000000007;\nll n, cnt;\nvector val[7];\nint ans, sum[7];\n\nint main() {\n scanf(\"%lld\", &n);\n function dfs1 = [&](int d, int v) {\n val[d].push_back((++cnt - v + P1) % P1);\n if (d == 6) return;\n for (int c = 0; c < 10; c++) dfs1(d + 1, v * 10 + c);\n };\n dfs1(0, 0);\n for (int d = 0; d <= 6; d++) {\n sort(val[d].begin(), val[d].end());\n for (int x : val[d]) (sum[d] += x) %= P2;\n }\n function dfs2 = [&](int d, ll v) {\n if (v > n) return;\n if (d) {\n if (v * B > n / 10 && (v + 1) * B - 1 <= n) {\n for (int i = 0, pw = 1; i <= 6; i++, pw *= 10) {\n int coef = (cnt - 1LL * v * pw % P1 + P1) % P1;\n auto it = lower_bound(val[i].begin(), val[i].end(), P1 - coef);\n ans = (ans + 1LL * coef * val[i].size() + sum[i]) % P2;\n ans = (ans - 1LL * P1 * (val[i].end() - it) % P2 + P2) % P2;\n }\n for (int i = 0; i <= 6; i++) cnt += val[i].size();\n return;\n }\n ans = (ans + (++cnt - v % P1 + P1) % P1) % P2;\n }\n for (int c = !d; c < 10; c++) dfs2(d + 1, v * 10 + c);\n };\n cnt = 0, dfs2(0, 0);\n printf(\"%d\\n\", ans);\n return 0;\n}\n \t\t \t \t\t\t\t\t \t \t \t", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "c24e44c2e709935f4b87fb785c0e3802", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define pb push_back\n#define D(x,y) ((x-y)%mod1+mod1)%mod1\nusing namespace std;\nusing INT=long long;\nconst int mod1=998244353;\nconst int mod2=1000000007;\nconst int NN=1000100;\n\nvector s[7];\nINT sum[7];\nINT nxt(INT val,INT n){\n\tif(val*10<=n) return val*10;\n\telse {\n\t\tval++;\n\t\tif(val<=n) while(val%10==0) val/=10;\n\t\telse {\n\t\t\tval=(val-1)/10+1;\n\t\t\twhile(val%10==0) val/=10;\n\t\t}\n\t}\n\treturn val;\n}\n\nint L[NN];\n\nvoid pre(){\n\tint val=0,len=1;\n\tint nth=1;\n\tfor(int i=1;i<=1111110;i++){\n\t\ts[len].pb(D(nth,val));\n\t\tnth++;\n\t\tif(len<6) len++,val*=10;\n\t\telse {\n\t\t\tval++;\n\t\t\twhile(val%10==0) val/=10,len--;\n\t\t}\n\t}\n\t\n\tfor(int i=1;i<7;i++){\n\t\tsort(s[i].begin(),s[i].end());\n\t\tfor(int v:s[i]) sum[i]=(sum[i]+v)%mod2;\n\t}\n\t\n\tint ed=1,f=0;\n\tfor(int i=1;i<=1000000;i++){\n\t\tif(i==ed) ed*=10,f++;\n\t\tL[i]=f;\n\t}\n}\n\nINT NTH;\nINT calc(INT val,INT nth,INT n){\n\tINT ans=0;\n\tINT st=val/100;\n\tst++;\n\twhile(st%10==0) st/=10;\n\twhile(val!=st){\n\t\tans=(ans+D(nth,val))%mod2;\n\t\tnth++;\n\t\tval=nxt(val,n);\n\t}\n\tNTH=nth;\n\treturn ans;\n}\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.in\",\"r\",stdin);\n\tfreopen(\"out.out\",\"w\",stdout);\n#endif\n\tpre();\n\t\n\tINT n;\n\tcin>>n;\n\tif(n<10000000){\n\t\tINT ans=0;\n\t\tINT val=1;\n\t\tfor(int i=1;i<=n;i++){\n\t\t\tans=(ans+D(i,val))%mod2;\n\t\t\tif(i==n) break;\n\t\t\tval=nxt(val,n);\n\t\t}\n\t\tcout<\r\n\r\nusing std::cin;\r\nusing std::cout;\r\nusing std::cerr;\r\n\r\nusing std::vector;\r\nusing std::map;\r\nusing std::array;\r\nusing std::set;\r\nusing std::string;\r\nusing std::queue;\r\n\r\nusing std::pair;\r\nusing std::make_pair;\r\n\r\nusing std::tuple;\r\nusing std::make_tuple;\r\nusing std::get;\r\n\r\nusing std::min;\r\nusing std::abs;\r\nusing std::max;\r\nusing std::swap;\r\n\r\nusing std::unique;\r\nusing std::sort;\r\nusing std::generate;\r\nusing std::reverse;\r\nusing std::min_element;\r\nusing std::max_element;\r\n\r\n#ifdef LOCAL\r\n#define LASSERT(X) assert(X)\r\n#else\r\n#define LASSERT(X) {}\r\n#endif\r\n\r\ntemplate \r\nT input() {\r\n T res;\r\n cin >> res;\r\n LASSERT(cin);\r\n return res;\r\n}\r\n\r\ntemplate \r\nvoid input_seq(IT b, IT e) {\r\n std::generate(b, e, input::type>);\r\n}\r\n\r\n#define SZ(vec) int((vec).size())\r\n#define ALL(data) data.begin(),data.end()\r\n#define RALL(data) data.rbegin(),data.rend()\r\n#define TYPEMAX(type) std::numeric_limits::max()\r\n#define TYPEMIN(type) std::numeric_limits::min()\r\n\r\nconst int mod = 998244353;\r\n\r\nstring toString(__int128_t num) {\r\n if (not num)\r\n return \"0\";\r\n string res;\r\n\r\n while (num) {\r\n res += char('0' + num % 10);\r\n num /= 10;\r\n }\r\n\r\n std::reverse(ALL(res));\r\n return res;\r\n}\r\n\r\nvoid solve(vector variables, int base, __int128_t& ans) {\r\n int cur, until;\r\n\r\n vector rem1, rem2;\r\n \r\n std::function brute = [&](int pos) {\r\n if (pos == until) {\r\n rem1.push_back(cur);\r\n return;\r\n }\r\n\r\n auto oldcur = cur;\r\n for (int t = 0; t < 10; ++t) {\r\n brute(pos + 1);\r\n cur += variables[pos];\r\n if (cur >= mod)\r\n cur -= mod;\r\n }\r\n cur = oldcur;\r\n };\r\n\r\n cur = base;\r\n until = SZ(variables) / 2;\r\n brute(0);\r\n\r\n rem2 = std::move(rem1);\r\n cur = 0;\r\n until = SZ(variables);\r\n brute(SZ(variables) / 2);\r\n\r\n sort(ALL(rem1));\r\n sort(ALL(rem2));\r\n\r\n int64_t totalrsum = 0;\r\n for (int b: rem2)\r\n totalrsum += b;\r\n\r\n int j = SZ(rem2) - 1;\r\n for (int a: rem1) {\r\n while (j >= 0 and a + rem2[j] >= mod)\r\n --j;\r\n\r\n ans += a * int64_t(SZ(rem2)) + totalrsum - (SZ(rem2) - 1 - j) * int64_t(mod);\r\n }\r\n}\r\n\r\nint64_t pw10[17];\r\n\r\nint64_t count_with_prefix(const string& s, int64_t n, const string& n_str) {\r\n int64_t ans = 0;\r\n\r\n int64_t s_val = 0;\r\n for (char ch: s)\r\n s_val = 10 * s_val + ch - '0';\r\n \r\n for (int len = SZ(s); len <= SZ(n_str); ++len) {\r\n if (len < SZ(n_str))\r\n ans += pw10[len - SZ(s)];\r\n else {\r\n int t = strncmp(s.c_str(), n_str.c_str(), SZ(s));\r\n if (t < 0)\r\n ans += pw10[len - SZ(s)];\r\n else if (t == 0) {\r\n ans += (n + 1) - s_val * pw10[SZ(n_str) - SZ(s)];\r\n }\r\n }\r\n }\r\n return ans;\r\n}\r\n\r\nint main() {\r\n std::iostream::sync_with_stdio(false);\r\n cin.tie(nullptr);\r\n cout.tie(nullptr);\r\n\r\n // code here\r\n int64_t n = input();\r\n\r\n string n_str;\r\n for (int64_t t = n; t; t /= 10)\r\n n_str += char('0' + t % 10);\r\n std::reverse(ALL(n_str));\r\n\r\n pw10[0] = 1;\r\n for (int i = 1; i < 17; ++i)\r\n pw10[i] = 10 * pw10[i - 1];\r\n \r\n __int128_t ans = 0;\r\n for (int len = 1; len <= SZ(n_str); ++len) {\r\n int64_t cur = 0;\r\n string curst;\r\n \r\n for (int firstdiff = 0; firstdiff < SZ(n_str) and firstdiff < len; ++firstdiff) {\r\n if (firstdiff) {\r\n cur = 10 * cur + (n_str[firstdiff - 1] - '0');\r\n curst += n_str[firstdiff - 1];\r\n }\r\n\r\n for (int lastdig = 0; lastdig <= 9; ++lastdig) {\r\n if (firstdiff == 0 and lastdig == 0)\r\n continue;\r\n if (lastdig == n_str[firstdiff] - '0')\r\n continue;\r\n if (len == SZ(n_str) and lastdig > n_str[firstdiff] - '0')\r\n continue;\r\n \r\n auto curp = 10 * cur + lastdig;\r\n auto curstp = curst + char('0' + lastdig);\r\n \r\n // n = 23456\r\n // cur = 21??\r\n\r\n // who's less than us?\r\n // 1*\r\n // 20*\r\n // 21[smaller]\r\n\r\n int64_t base = 0;\r\n string tmp = \"\";\r\n for (int pos = 0; pos < SZ(curstp); ++pos) {\r\n for (char dig = (pos == 0 ? '1' : '0'); dig < curstp[pos]; ++dig)\r\n base += count_with_prefix(tmp + dig, n, n_str);\r\n \r\n tmp += curstp[pos];\r\n }\r\n base += len - 1; // all strict prefixes are also smaller\r\n \r\n // (sum ai xi + base) mod p\r\n vector variables;\r\n for (int pos = SZ(curstp); pos < len; ++pos) {\r\n variables.push_back(count_with_prefix(tmp + '0', n, n_str) % mod);\r\n tmp += '0';\r\n }\r\n\r\n base += 1; // 1-based, not 0-based\r\n base -= curp * pw10[len - SZ(curstp)];\r\n for (int t = 0; t < SZ(variables); ++t)\r\n variables[SZ(variables) - 1 - t] -= pw10[t] % mod;\r\n\r\n base %= mod;\r\n if (base < 0)\r\n base += mod;\r\n \r\n for (int& val: variables) {\r\n val %= mod;\r\n if (val < 0)\r\n val += mod;\r\n }\r\n\r\n solve(variables, base, ans);\r\n \r\n // cout << curstp << \" \" << len << \"\\n\";\r\n // cout << base << \"\\n\";\r\n // for (int x: variables)\r\n // cout << x << \" \";\r\n // cout << \"\\n\";\r\n // cout << \"delta is \" << (int64_t)solve(variables, base) << \"\\n\";\r\n // cout << \"=====\\n\";\r\n }\r\n }\r\n }\r\n\r\n // special case, numbers that are a prefix of N:\r\n int64_t cur = 0;\r\n string curst;\r\n\r\n for (int len = 1; len <= SZ(n_str); ++len) {\r\n curst += n_str[len - 1];\r\n cur = 10 * cur + (n_str[len - 1] - '0');\r\n\r\n int64_t val = 1; // 1-based\r\n string tmp = \"\";\r\n for (int pos = 0; pos < SZ(curst); ++pos) {\r\n for (char dig = (pos == 0 ? '1' : '0'); dig < curst[pos]; ++dig)\r\n val += count_with_prefix(tmp + dig, n, n_str);\r\n \r\n tmp += curst[pos];\r\n }\r\n val += len - 1; // all strict prefixes are also smaller\r\n \r\n val -= cur;\r\n val %= mod;\r\n if (val < 0)\r\n val += mod;\r\n\r\n //cerr << \"for \" << curst << \" ans is \" << val << \"\\n\";\r\n \r\n ans += val;\r\n }\r\n \r\n ans %= (1000 * 1000 * 1000 + 7);\r\n cout << toString(ans) << \"\\n\";\r\n\r\n return 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "6adf8c0a271dcd55431db46bfb3ef2b2", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\r\n#include\r\n#include \r\nusing namespace std;\r\nconst int p1=998244353,p2=1e9+7,B=1e6;\r\nvector v[7];\r\nint sum[7];\r\nlong long n,ord,ans;\r\nvoid dfs1(int x,int len){\r\n\t++ord,v[len].push_back((ord-x+p1)%p1);\r\n\t(sum[len]+=v[len].back())%=p2;\r\n\tif(len==6) return;\r\n\tfor(int i=0;i<=9;++i) dfs1(x*10+i,len+1);\r\n}\r\nvoid dfs2(long long x){\r\n\tif(x>n) return;\r\n\tif(x*B+B-1<=n&&x*B*10>n){\r\n\t\tlong long now=ord;\r\n\t\tfor(int i=0,m=1;i<=6;++i,m*=10){\r\n\t\t\tint sz=v[i].size(),dt=((now-x*m)%p1+p1)%p1;\r\n\t\t\tint pos=lower_bound(v[i].begin(),v[i].end(),p1-dt)-v[i].begin();\r\n\t\t\tans=(ans+1ll*dt*sz+sum[i])%p2;\r\n\t\t\tans=(ans-1ll*(sz-pos)*p1%p2+p2)%p2;\r\n\t\t\tord+=sz;\r\n\t\t}\r\n\t\treturn;\r\n\t}\r\n\t++ord;\r\n\tans=(ans+((ord-x)%p1+p1)%p1)%p2;\r\n\tfor(int i=0;i<=9;++i) dfs2(x*10+i);\r\n}\r\nint main(){\r\n\tcin>>n;\r\n\tdfs1(0,0),ord=0;\r\n\tfor(int i=0;i<=6;++i) sort(v[i].begin(),v[i].end());\r\n\tfor(int i=1;i<=9;++i) dfs2(i);\r\n\tcout<\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n\r\nusing namespace std;\r\n#define pb push_back\r\n#define ll long long\r\n#define mp make_pair\r\n#define snd second\r\n#define ins insert\r\n//#define ld long double\r\n#define mt make_tuple\r\n#define fst first\r\nconst double PI = acos(-1);\r\n// = 5e5 + 17;\r\nconst int MOD1 = 998244353;\r\nconst int MOD = 1'000'000'007;\r\nconst int INF = 1e9;\r\nconst int MaXN = 150;\r\nconst int N = 5e5 + 1000;\r\nconst int LOG = 25;\r\nconst int FACT = 120;\r\ndouble ecr = 1e-8;\r\nrandom_device rd;\r\nmt19937 rnd(rd());\r\nuniform_int_distribution nextRand(0, (ll) 1e9);\r\nusing namespace std;\r\n#define pb push_back\r\n#define SZ 233333\r\n#define int ll\r\nll qp(ll a,ll b)\r\n{\r\n ll x=1; a%=MOD;\r\n while(b)\r\n {\r\n if(b&1) x=x*a%MOD;\r\n a=a*a%MOD; b>>=1;\r\n }\r\n return x;\r\n}\r\nnamespace linear_seq {\r\n inline vector BM(vector x)\r\n {\r\n //ls: (shortest) relation sequence (after filling zeroes) so far\r\n //cur: current relation sequence\r\n vector ls,cur;\r\n //lf: the position of ls (t')\r\n //ld: delta of ls (v')\r\n int lf,ld;\r\n for(int i=0;i<(x.size());++i)\r\n {\r\n ll t=0;\r\n //evaluate at position i\r\n for(int j=0;j<(cur.size());++j)\r\n t=(t+x[i-j-1]*(ll)cur[j])%MOD;\r\n if((t-x[i])%MOD==0) continue; //good so far\r\n //first non-zero position\r\n if(!cur.size())\r\n {\r\n cur.resize(i+1);\r\n lf=i; ld=(t-x[i])%MOD;\r\n continue;\r\n }\r\n //cur=cur-c/ld*(x[i]-t)\r\n ll k=-(x[i]-t)*qp(ld,MOD-2)%MOD/*1/ld*/;\r\n vector c(i-lf-1); //add zeroes in front\r\n c.pb(k);\r\n for(int j=0;j<(ls.size());++j)\r\n c.pb(-ls[j]*k%MOD);\r\n if(c.size()=cur.size())\r\n ls=cur,lf=i,ld=(t-x[i])%MOD;\r\n cur=c;\r\n }\r\n for(int i=0;i<(cur.size());++i)\r\n cur[i]=(cur[i]%MOD+MOD)%MOD;\r\n return cur;\r\n }\r\n int m; //length of recurrence\r\n//a: first terms\r\n//h: relation\r\n ll a[SZ],h[SZ],t_[SZ],s[SZ],t[SZ];\r\n//calculate p*q mod f\r\n inline void mull(ll*p,ll*q)\r\n {\r\n for(int i=0;i=m;--i) if(t_[i])\r\n //miuns t_[i]x^{i-m}(x^m-\\sum_{j=0}^{m-1} x^{m-j-1}h_j)\r\n for(int j=m-1;~j;--j)\r\n t_[i-j-1]=(t_[i-j-1]+t_[i]*h[j])%MOD;\r\n for(int i=0;i>=1;\r\n }\r\n ll su=0;\r\n for(int i=0;i x,ll n)\r\n {\r\n if(n<(x.size())) return x[n];\r\n vector v=BM(x); m=v.size(); if(!m) return 0;\r\n for(int i=0;i ans;\r\nconst int P = 3000;\r\nconst int T = 14;\r\nll up;\r\nll check(ll n) {\r\n ll ans = 0;\r\n string st = to_string(n);\r\n int len = st.size();\r\n if (n > 0) ans++;\r\n for (int i = 1; i < T; i++) {\r\n ll cur = 1, num = n;\r\n for (int j = 0; j + 1 < i; j++) {\r\n cur = cur * 10;\r\n // cur += n % pow(10, j);\r\n }\r\n for (int j = len; j < i; j++) num *= 10;\r\n if (i < len) {\r\n string pref = \"\";\r\n for (int j = 0; j < i; j++) pref += st[j];\r\n num = stoll(pref);\r\n num++;\r\n }\r\n if (num > up) num = up;\r\n if (num > cur) ans += num - cur;\r\n }\r\n return ans;\r\n}\r\nint32_t main()\r\n{\r\n ios_base::sync_with_stdio(false);\r\n cin.tie(nullptr);\r\n cout.tie(nullptr);\r\n// vector s;\r\n// for (int i = 1; i <= P; i++) {\r\n// s.push_back(to_string(i));\r\n// sort(s.begin(), s.end());\r\n// ll x = 0;\r\n// for (int j = 0; j < s.size(); j++) {\r\n// x += (MOD1 - stoi(s[j]) + j + 1) % MOD1;\r\n// x %= MOD;\r\n// }\r\n// // cout << x << \" \";\r\n// ans.push_back(x);\r\n// }\r\n// vector res;\r\n// for (int i = 0; i < P; i++) res.pb((ans[i] % 1755654) % 310108);\r\n// set anss;\r\n// for (int i = 1; i < P; i++) {\r\n// anss.insert(ans[i]);\r\n// cout << i << \" \" << (ans[i] % 1755654) % 310108 << \"\\n\";\r\n// }\r\n// cout << anss.size() << \"\\n\";\r\n ll n;\r\n cin >> n; n++;\r\n up = n;\r\n ll part = n % MOD * (n / MOD1 + 2) % MOD;\r\n part = MOD - part;\r\n // cout << part << \"\\n\";\r\n// cout << work(res, n) << \"\\n\";\r\n// cout << work(res, n) << \" \" << ans[n] << \"\\n\";\r\n// for (int i = 90; i < 150; i++) {\r\n// cout << i << \" \" << work(res, i) - ans[i] << \"\\n\" ;\r\n// }\r\n ll ans = 0;\r\n for (ll i = -n / MOD1 - 1; i <= n / MOD1 + 1; i++ ) {\r\n ll cur = 0;\r\n for (int j = 0; j < T; j++) {\r\n ll L = 0, R = 1;\r\n if (j >= 1) {\r\n L = 1;\r\n for (int k = 0; k + 1 < j; k++) {\r\n // if (k >= 1)\r\n L *= 10;\r\n // else L++;\r\n }\r\n R = L * 10;\r\n }\r\n if (R > n) R = n;\r\n ll lastr = R;\r\n if (L < R) {\r\n L--;\r\n while (R - L > 1) {\r\n ll mid = (R + L) / 2;\r\n if (check(mid) > mid + MOD1 * i - 1) R = mid;\r\n else L = mid;\r\n }\r\n // cout << j << \" \" << lastr << \" \" << R << \"\\n\";\r\n cur += lastr - R;\r\n }\r\n }\r\n ans += cur;\r\n }\r\n // cout << ans << \"\\n\";\r\n ans %= MOD;\r\n ans = (-(ans + part % MOD) % MOD) * MOD1;\r\n ans %= MOD;\r\n cout << (MOD + ans % MOD) % MOD << \"\\n\";\r\n // -15800886 * x + y * 1755654 = unimodal\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "36ee5daafb8a7b64c9a7f163f3cf3075", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include \r\n#define fi first\r\n#define se second\r\n#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)\r\n#define mk make_pair\r\n#define pii pair\r\n#define pll pair\r\n#define pb push_back\r\n#define IT iterator\r\n#define V vector\r\n#define TP template \r\n#define TPP template \r\n#define SZ(a) ((int)a.size())\r\n#define all(a) a.begin(), a.end()\r\n#define rep(i, a, b) for (int i = a, i##ed_ = b; i <= i##ed_; i++)\r\n#define REP(i, a, b) for (int i = b, i##st_ = a; i >= i##st_; i--)\r\n#define FOR(i, n) rep(i, 1, n)\r\n#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl\r\nusing namespace std;\r\ntypedef double db;\r\ntypedef unsigned ui;\r\ntypedef long long ll;\r\ntypedef long double ld;\r\n\r\n// char buf[1 << 20],*p1=buf,*p2=buf;\r\nTP void qr(o& x) {\r\n char c = gc;\r\n x = 0;\r\n int f = 1;\r\n while (!isdigit(c)) {\r\n if (c == '-')\r\n f = -1;\r\n c = gc;\r\n }\r\n while (isdigit(c))\r\n x = x * 10 + c - '0', c = gc;\r\n x *= f;\r\n}\r\ntemplate void qr(o& x, O&... y) { qr(x), qr(y...); }\r\nTP void qw(o x) {\r\n if (x < 0)\r\n putchar('-'), x = -x;\r\n if (x / 10)\r\n qw(x / 10);\r\n putchar(x % 10 + '0');\r\n}\r\nTP void pr1(o x) { qw(x), putchar(' '); }\r\ntemplate void pr1(o x, O... y) { pr1(x), pr1(y...); }\r\nTP void pr2(o x) { qw(x), putchar(10); }\r\ntemplate void pr2(o x, O... y) { pr2(x), pr2(y...); }\r\nTP bool cmax(o& x, o y) { return (x < y ? x = y, 1 : 0); }\r\nTP bool cmin(o& x, o y) { return (x > y ? x = y, 1 : 0); }\r\n\r\nconst int mod = 998244353, mod2 = 1e9 + 7;\r\nTPP void ad(t1& x, t2 y) { (x += y) >= mod && (x -= mod); }\r\nTPP void dl(t1& x, t2 y) { (x -= y) < 0 && (x += mod); }\r\n\r\nconst int N = 1e6 + 10, B = 1e6;\r\nll n, x, y, ans; \r\n\r\nV t[7]; int s[7], cnt;\r\n\r\ninline int val(int x) {\r\n if(x < 0) return x + mod;\r\n return x;\r\n}\r\n\r\nvoid dfs(int x, int y) {\r\n ++cnt; int z = val(cnt - y);\r\n t[x].pb(z); s[x] = (s[x] + z) % mod2;\r\n if(x == 6) return ;\r\n rep(i, 0, 9) dfs(x + 1, y * 10 + i);\r\n}\r\n\r\nvoid DFS(int x, ll y) {\r\n if(y > n) return ;\r\n if(y * 10 * B > n && (y + 1) * B - 1 <= n) {\r\n ll u = y;\r\n rep(i, 0, 6) {\r\n ll w = val(cnt - u);\r\n ans += w * t[i].size() + s[i];\r\n ans -= (t[i].end() - lower_bound(all(t[i]), mod - w)) * (ll)mod;\r\n u = u * 10 % mod;\r\n }\r\n ans %= mod2; ad(cnt, 1111111);\r\n return; \r\n }\r\n if(x) ad(cnt, 1), ans += val(cnt - y % mod);\r\n rep(i, !x, 9) DFS(x + 1, y * 10 + i);\r\n}\r\n\r\nvoid solve() {\r\n qr(n);\r\n dfs(0, 0);\r\n rep(i, 0, 6) sort(all(t[i]));\r\n cnt = 0; DFS(0, 0); ans = (ans % mod2 + mod2) % mod2; pr2(ans);\r\n}\r\n\r\nint main() {\r\n#ifndef ONLINE_JUDGE\r\n clock_t start_time = clock();\r\n#endif\r\n int T = 1;\r\n // qr(T);\r\n while (T--)\r\n solve();\r\n return 0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "2c5d8a431987e6aec2f6c5a260dd644c", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\n#include\r\n#include\r\nusing namespace std;\r\nconst int mod=1000000007,MOD=998244353,maxk=7,base=1000000;\r\nlong long n,ord;\r\nint ans;\r\nint sum[maxk];\r\nvectorv[maxk];\r\nvoid dfs1(int x,int len) {\r\n\tord++,v[len].push_back(((ord-x)%MOD+MOD)%MOD),sum[len]=(sum[len]+v[len].back())%mod;\r\n\tif(len==6)\r\n\t\treturn ;\r\n\tfor(int i=0; i<=9; i++)\r\n\t\tdfs1(x*10+i,len+1);\r\n}\r\nvoid dfs2(long long x) {\r\n\tif(x>n)\r\n\t\treturn ;\r\n\tif(x*base+base-1<=n&&x*base*10>n) {\r\n\t\tlong long now=ord;\r\n\t\tfor(int i=0,mul=1; i<=6; i++,mul*=10) {\r\n\t\t\tint sz=v[i].size(),dif=((now-x*mul)%MOD+MOD)%MOD,pos=lower_bound(v[i].begin(),v[i].end(),MOD-dif)-v[i].begin();\r\n\t\t\tans=(ans+1ll*dif*sz+sum[i])%mod,ans=(ans-1ll*MOD*(sz-pos)%mod+mod)%mod;\r\n\t\t\tord+=sz;\r\n\t\t}\r\n\t\treturn ;\r\n\t}\r\n\tord++,ans=(ans+((ord-x)%MOD+MOD)%MOD)%mod;\r\n\tfor(int i=0; i<=9; i++)\r\n\t\tdfs2(x*10+i);\r\n}\r\nint main() {\r\n\tscanf(\"%lld\",&n);\r\n\tord=0,dfs1(0,0);\r\n\tfor(int i=0; i<=6; i++)\r\n\t\tsort(v[i].begin(),v[i].end());\r\n\tord=0;\r\n\tfor(int i=1; i<=9; i++)\r\n\t\tdfs2(i);\r\n\tprintf(\"%d\\n\",ans);\r\n\treturn 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "ac63686c44a7bc92794e046cbee99151", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\n#define Tp template\r\n#define Ts template\r\n#define Rg register\r\n#define RI Rg int\r\n#define Cn const\r\n#define CI Cn int&\r\n#define I inline\r\n#define W while\r\n#define SZ 1000000\r\n#define V 998244353\r\n#define X 1000000007\r\n#define LL long long\r\n#define Dec(x) (!x--&&(x+=X))\r\nusing namespace std;\r\nLL n,nn,p;int ans,tn[7],c[7][7],s[7][7][SZ+1];\r\nvoid Init(RI x,RI l,CI k)\r\n{\r\n\ts[k][l][++c[k][l]]=(++p)-x;if(l==k) return;for(RI i=0;i<=9;++i) Init(x*10+i,l+1,k);\r\n}\r\nvoid BF(LL x,RI l)\r\n{\r\n\tif(x>n) return;++p;ans=(ans+(p-x)/V-(p=1e6) nn/=10;for(i=1;i<=9;++i) Solve(i,1);}\r\n\treturn printf(\"%d\\n\",(int)(1LL*(X-ans)*V%X)),0;\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "7cdd2b7181b5b04ce26202e9c0805687", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//LYC_music yyds!\r\n#include\r\n#define IOS ios::sync_with_stdio(0)\r\n#define lowbit(x) (x&(-x))\r\n#define int long long\r\nusing namespace std;\r\ninline char gc()\r\n{\r\n\tstatic char buf[1000000],*p1=buf,*p2=buf;\r\n\treturn p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;\r\n}\r\nint read()\r\n{\r\n\tint pos=1,num=0;\r\n\tchar ch=getchar();\r\n\twhile (!isdigit(ch))\r\n\t{\r\n\t\tif (ch=='-') pos=-1;\r\n\t\tch=getchar();\r\n\t}\r\n\twhile (isdigit(ch))\r\n\t{\r\n\t\tnum=num*10+(int)(ch-'0');\r\n\t\tch=getchar();\r\n\t}\r\n\treturn pos*num;\r\n}\r\nvoid write(int x)\r\n{\r\n\tif (x<0)\r\n\t{\r\n\t\tputchar('-');\r\n\t\twrite(-x);\r\n\t\treturn;\r\n\t}\r\n\tif (x>=10) write(x/10);\r\n\tputchar(x%10+'0');\r\n}\r\nvoid writesp(int x)\r\n{\r\n\twrite(x);\r\n\tputchar(' ');\r\n}\r\nvoid writeln(int x)\r\n{\r\n\twrite(x);\r\n\tputchar('\\n');\r\n}\r\nconst int mod1=998244353,mod2=1e9+7;\r\nvector val[10];\r\nint n,cnt1,cnt2,ans,sum[10],Pow[7]={1,10,100,1000,10000,100000,1000000};\r\nbool check(int x)\r\n{\r\n\tint now=1000000;\r\n\treturn x*now+999999<=n&&x*now*10>n;\r\n}\r\nvoid dfs1(int x1,int x2)\r\n{\r\n\tif (x1==7) return;\r\n\tval[x1].emplace_back((cnt1-x2+mod1)%mod1);\r\n\tsum[x1]+=(cnt1-x2+mod1)%mod1;\r\n\tcnt1++;\r\n\tfor (int i=0;i<=9;i++)\r\n\t\tdfs1(x1+1,x2*10+i);\r\n}\r\nvoid dfs2(int x1,int x2)\r\n{\r\n\tif (x2>n) return;\r\n\tif (check(x2))\r\n\t{\r\n\t\tfor (int i=0;i<=6;i++)\r\n\t\t{\r\n\t\t\tint now=((cnt2+1-(x2*Pow[i]))%mod1+mod1)%mod1;\r\n\t\t\tint pos=lower_bound(val[i].begin(),val[i].end(),mod1-now)-val[i].begin();\r\n\t\t\t(ans+=(sum[i]+now*val[i].size()-mod1*(val[i].size()-pos)))%=mod2;\r\n\t\t}\r\n\t\tcnt2+=cnt1;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tcnt2++;\r\n\t\t(ans+=((cnt2-x2)%mod1+mod1)%mod1)%=mod2;\r\n\t\tfor (int i=(!x1);i<=9;i++)\r\n\t\t\tdfs2(x1+1,x2*10+i);\r\n\t}\r\n}\r\nsigned main()\r\n{\r\n//\tfreopen(\"permutation.in\",\"r\",stdin);\r\n//\tfreopen(\"permutation.out\",\"w\",stdout);\r\n\tn=read();\r\n\tdfs1(0,0);\r\n\tfor (int i=0;i<=6;i++)\r\n\t\tsort(val[i].begin(),val[i].end());\r\n\tcnt2=-1;\r\n\tdfs2(0,0);\r\n\twriteln(ans);\r\n}\r\n\r\n\r\n\r\n\r\n\r\n\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "876e521ac86c1967dff51aa2cfd2bfa6", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "# include \r\nusing namespace std;\r\nconst int B=1e6,M=998244353,mod=1e9+7;\r\ntypedef long long ll;\r\nll n,cnt1,cnt2;\r\nint sum[10],ans;\r\nvector val[10];\r\nvoid dfs1(int d,ll v)\r\n{\r\n\tif(d>6) return;\r\n\tint w=((++cnt1)-v+M)%M;\r\n\tval[d].push_back(w);\r\n\tsum[d]=(sum[d]+w)%mod;\r\n\tfor(int i=0;i<=9;i++) dfs1(d+1,v*10+i);\r\n}\r\nvoid dfs2(ll v)\r\n{\r\n\tif(v>n) return;\r\n\tif(vn/B)\r\n\t{\r\n\t\tfor(int i=0,pw=1;i<=6;pw*=10,i++)\r\n\t\t{\r\n\t\t\tint w=(cnt2+(ll)(M-pw)*v+M)%M,c=val[i].end()-lower_bound(val[i].begin(),val[i].end(),M-w);\r\n\t\t\tans=(ans+(ll)val[i].size()*w+sum[i]+(ll)(mod-M)*c)%mod;\r\n\t\t}\r\n\t\tcnt2+=cnt1;\r\n\t\treturn;\r\n\t}\r\n\tcnt2++;ans=(ans+(cnt2-v%M+M)%M)%mod;\r\n\tfor(int i=0;i<=9;i++) dfs2(v*10+i);\r\n}\r\nint main()\r\n{\r\n\tcin>>n;\r\n\tdfs1(0,0);\r\n\tfor(int i=0;i<=6;i++) sort(val[i].begin(),val[i].end());\r\n\tfor(int i=1;i<=9;i++) dfs2(i);\r\n\tcout<\r\nusing namespace std;\r\n#define int long long\r\nconst int mod1=998244353;\r\nconst int mod2=1e9+7;\r\nint ans;\r\nint n;\r\nint cnt1;\r\nvector val[7];\r\nint sum[7];\r\nvoid dfs1(int k1,int k2){\r\n\tif(k1==7)return;\r\n\tval[k1].push_back((cnt1-k2+mod1)%mod1);\r\n\tsum[k1]+=(cnt1-k2+mod1)%mod1;\r\n\tcnt1++;\r\n\tfor(int i=0;i<=9;++i){\r\n\t\tdfs1(k1+1,k2*10+i);\r\n\t}\r\n}\r\nbool check(int x){\r\n\tconst int half=1000000;\r\n\treturn x*half+999999<=n&&x*half*10>n;\r\n}\r\nint cnt2;\r\nint poww[7]={1,10,100,1000,10000,100000,1000000};\r\nvoid dfs2(int k1,int k2){\r\n\tif(k2>n)return;\r\n\tif(check(k2)){\r\n\t\tfor(int i=0;i<=6;++i){\r\n\t\t\tint offset=((cnt2+1-(k2*poww[i]))%mod1+mod1)%mod1;\r\n\t\t\tint pos=lower_bound(val[i].begin(),val[i].end(),mod1-offset)-val[i].begin();\r\n\t\t\t(ans+=(sum[i]+offset*val[i].size()-mod1*(val[i].size()-pos)))%=mod2;\r\n\t\t}\r\n\t\tcnt2+=cnt1;\r\n\t}\r\n\telse {\r\n\t\tcnt2++;\r\n\t\t(ans+=((cnt2-k2)%mod1+mod1)%mod1)%=mod2;\r\n\t\tfor(int i=(!k1);i<=9;++i){\r\n\t\t\tdfs2(k1+1,k2*10+i);\r\n\t\t}\r\n\t}\r\n}\r\nsigned main(){\r\n\tscanf(\"%lld\",&n);\r\n\tdfs1(0,0);\r\n\tfor(int i=0;i<=6;++i)sort(val[i].begin(),val[i].end());\r\n\tcnt2=-1;\r\n\tdfs2(0,0);\r\n\tprintf(\"%lld\\n\",ans);\r\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "c23af767444ad38dcc66d89c3d9afb3b", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include\r\n\r\nusing namespace std;\r\n\r\n#define int long long\r\nint const maxn = 1000005;\r\nint mod = 998244353, modd = 1e9 + 7;\r\nint st[15];\r\nstring good[maxn];\r\nint N, number;\r\nvector < string > lenn[7];\r\nvector < int > ok[7][7];\r\nint sum[7][7];\r\nint inf = 1000000000000000000;\r\n\r\ninline int goal(string s) {\r\n int answ = 0;\r\n for (auto c : s) answ = answ * 10 + c - '0';\r\n return answ;\r\n}\r\n\r\ninline __int128 get(string cur, int max_len) {\r\n if (max_len == -1) return 0;\r\n int L = max_len / 2, R = max_len - L;\r\n if (max_len == 1) L = 1, R = 0;\r\n __int128 ans = (number - goal(cur) + mod * mod) % mod;\r\n number++;\r\n //cout << cur << \" \" << max_len << endl;\r\n for (auto lef : lenn[L]) {\r\n ans += (-goal(cur + lef) + number + mod * mod) % mod;\r\n number++;\r\n if ((int)lef.size() == L) {\r\n int c = goal(cur + lef) % mod;\r\n for (int oth = 1; oth <= R; ++oth) {\r\n int cnst = (-c * st[oth] + number + mod * mod) % mod;\r\n int to = lower_bound(ok[R][oth].begin(), ok[R][oth].end(), mod - cnst) - ok[R][oth].begin();\r\n ans += (int)ok[R][oth].size() * cnst + sum[R][oth] - mod * ((int)ok[R][oth].size() - to);\r\n }\r\n number += (int)lenn[R].size();\r\n }\r\n }\r\n return ans;\r\n}\r\n\r\nmain() {\r\n#ifdef HOME\r\n //freopen(\"input.txt\", \"r\", stdin);\r\n#endif // HOME\r\n ios_base::sync_with_stdio(0);\r\n cin.tie(0);\r\n cout.tie(0);\r\n int n;\r\n cin >> n;\r\n N = to_string(n).size();\r\n st[0] = 1;\r\n for (int i = 1; i <= 14; ++i) st[i] = st[i - 1] * 10;\r\n string s = to_string(n);\r\n string cur = \"\";\r\n for (int i = 0; i < maxn; ++i) {\r\n good[i] = to_string(i);\r\n string now = good[i];\r\n while ((int)now.size() <= 6) {\r\n lenn[(int)now.size()].push_back(now);\r\n now = \"0\" + now;\r\n }\r\n }\r\n for (int i = 1; i <= 6; ++i) {\r\n for (auto key : lenn[i - 1]) lenn[i].push_back(key);\r\n sort(lenn[i].begin(), lenn[i].end());\r\n for (int j = 0; j < (int)lenn[i].size(); ++j) {\r\n ok[i][lenn[i][j].size()].push_back((j - stoi(lenn[i][j]) + mod) % mod);\r\n }\r\n for (int j = 1; j <= i; ++j) {\r\n sort(ok[i][j].begin(), ok[i][j].end());\r\n for (auto key : ok[i][j]) sum[i][j] += key;\r\n }\r\n }\r\n vector < pair < string, int > > Q;\r\n for (int i = 0; i < (int)s.size(); ++i) {\r\n for (int j = 0; j <= 9; ++j) {\r\n if (j == s[i] - '0') continue;\r\n if (i == 0 && j == 0) continue;\r\n cur += char('0' + j);\r\n if (j <= s[i] - '0') {\r\n Q.push_back({cur, (int)s.size() - i - 1});\r\n } else {\r\n Q.push_back({cur, (int)s.size() - i - 2});\r\n }\r\n cur.pop_back();\r\n }\r\n cur += s[i];\r\n Q.push_back({cur, 0});\r\n }\r\n number = 1;\r\n __int128 ans = 0;\r\n sort(Q.begin(), Q.end());\r\n for (auto key : Q) {\r\n if (s[(int)key.first.size() - 1] == key.first.back()) {\r\n ans += (-goal(key.first) + number + mod * mod) % mod;\r\n number += 1;\r\n continue;\r\n }\r\n ans += get(key.first, key.second);\r\n }\r\n ans %= modd;\r\n cout << (long long)ans;\r\n return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "7f7c60bd234e1629486f62fa6825b516", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\nconst int B=1e6;\nconst int mod1=998244353;\nconst int mod2=1e9+7;\nll n,cnt;\nvector v[10];\nint sum[10],ans,tot;\ninline int calc(ll x){\n\treturn (x%mod1+mod1)%mod1;\t\n}\nvoid dfs1(int dep,int num){\n\t++cnt;\n\tv[dep].push_back(calc(cnt-num));\n\tif(dep==6)return;\n\tfor(int i=0;i<=9;++i){\n\t\tdfs1(dep+1,num*10+i);\n\t}\n}\nvoid dfs2(int dep,ll num){\n\tif(num>n)return;\n\tif(dep>=1){\n\t\tif(num*B>n/10&&(num+1)*B-1<=n){\n\t\t\tint tmp=1;\n\t\t\tfor(int i=0;i<=6;++i,tmp*=10){\n\t\t\t\tint w=calc(cnt-tmp*num);\n\t\t\t\tint pos=lower_bound(v[i].begin(),v[i].end(),mod1-w)-v[i].begin();\n\t\t\t\tans=(ans+1LL*w*v[i].size()+sum[i]-1LL*mod1*((int)v[i].size()-pos))%mod2;\n\t\t\t}\n\t\t\tcnt+=tot;\n\t\t\treturn;\n\t\t}\n\t\t++cnt;\n\t\tans=(ans+calc(cnt-num))%mod2;\n\t}\n\tfor(int i=!dep;i<=9;++i){\n\t\tdfs2(dep+1,num*10+i);\n\t}\n}\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcin>>n;\n\tdfs1(0,0);\n\tfor(int i=0;i<=6;++i){\n\t\tsort(v[i].begin(),v[i].end());\n\t\tsum[i]=accumulate(v[i].begin(),v[i].end(),0LL)%mod2;\n\t}\n\ttot=cnt;\n\tcnt=0;\n\tdfs2(0,0);\n\tcout<<(ans+mod2)%mod2<<'\\n';\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "meet-in-the-middle", "binary search"], "code_uid": "ecf49d8d4e531b272ecd6e688b09ed00", "src_uid": "2c70ae38f91ab739621a31b897b8fbf3", "difficulty": 3400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint n, m, k, a, b;\n\nint main()\n{\n cin >> n >> m >> k;\n cin >> a >> b; a--; b--;\n int na = a / (m * k);\n int nb = b / (m * k);\n int ma = (a - na * m * k) / k;\n int mb = (b - nb * m * k) / k;\n if (na == nb && ma == mb){\n cout << 0 << endl;\n return 0;\n }\n if (na == nb && ma != mb){\n cout << min(5 * abs(ma - mb), 10 + abs(ma - mb)) << endl;\n return 0;\n }\n\n if (na != nb){\n long long d1 = 1LL * min(5 * ma, 10 + ma);\n long long d2 = 15LL * min(abs(na - nb), min(na + n - nb, nb + n - na));\n long long d3 = min(5LL * mb, 10LL + mb);\n cout << d1 + d2 + d3 << endl;\n return 0;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms"], "code_uid": "dc00bcdbfdcda4955851628faf0532dc", "src_uid": "c37b46851abcf7eb472869bd1ab9f793", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \nusing namespace std;\n\nconst int M = 1000000007;\ninline int add(int x, int y) {return x+y>=M ? x+y-M : x+y;}\ntemplate inline int add(int x, int y, Args... args) {return add(add(x, y), args...);}\ninline int sub(int x, int y) {return x-y<0 ? x-y+M : x-y;}\ninline int mul(int x, int y) {return 1LL * x * y % M;}\ntemplate inline int mul(int x, int y, Args... args) {return mul(mul(x, y), args...);}\ninline void inc(int &x, int y=1) {x += y; if(x >= M) x -= M;}\ninline void dec(int &x, int y=1) {x -= y; if(x < 0) x += M;}\ninline int power(int x, int y){\n int res = 1;\n for(; y; y>>=1, x = mul(x, x)) if(y & 1) res = mul(res, x);\n return res;\n}\ninline int inv(int x){return power(x, M - 2);}\n\nconst int N = 1504;\nint l[2][N], sl[2][N], dls[2][N], f[2]; // sl : fixed right border, l : all ranges strictly on left, r[i] = l[m - i + 1]\nint d[N], ds[N];\nint fac[100005], ifac[100005];\n\nint main() {\n int n, m, a, b, t;\n scanf(\"%d%d%d%d%d\", &n, &m, &a, &b, &t);\n\n int L = max(m, t);\n fac[0] = 1;\n for (int i = 1; i <= L; ++i) fac[i] = mul(fac[i - 1], i);\n ifac[L] = inv(fac[L]);\n for (int i = L - 1; i >= 0; --i) ifac[i] = mul(ifac[i + 1], i + 1);\n int p = mul(a, inv(b)), inp = inv(sub(1, p)), nowp = power(sub(1, p), t);\n if(p == 1) {\n if(t * 2 < m) puts(\"1\");\n else puts(\"0\");\n return 0;\n }\n for (int i = 0, li = min(m, t); i <= li; ++i) {\n d[i] = mul(fac[t], ifac[i], ifac[t - i], nowp);\n nowp = mul(nowp, p, inp);\n ds[i] = add(d[i], i == 0 ? 0 : ds[i - 1]);\n }\n for (int i = min(m, t) + 1; i <= m; ++i)\n ds[i] = add(d[i], ds[i - 1]);\n\n f[1] = 0;\n for (int i = 1; i <= m; ++i) {\n sl[1][i] = mul(ds[i - 1], d[m - i]);\n inc(f[1], sl[1][i]);\n l[1][i] = add(l[1][i - 1], sl[1][i - 1]);\n dls[1][i] = add(dls[1][i - 1], mul(d[i - 1], l[1][i]));\n }\n for (int i = 2; i <= n; ++i) {\n int now = i & 1, pre = now ^ 1;\n f[now] = 0;\n for (int r = 1; r <= m; ++r) {\n sl[now][r] = mul(d[m - r], sub(mul(sub(f[pre], l[pre][m - r + 1]), ds[r - 1]), dls[pre][r]));\n l[now][r] = add(l[now][r - 1], sl[now][r - 1]);\n dls[now][r] = add(dls[now][r - 1], mul(d[r - 1], l[now][r]));\n inc(f[now], sl[now][r]);\n }\n }\n printf(\"%d\\n\", f[n & 1]);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp"], "code_uid": "6f5bfc54472d6cdd971f6f7a8d9b29f3", "src_uid": "33b6b0d3a6af273f22b703491bd17289", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nbool check(string &s, int k, int ind)\n{\n int cntr = 0;\n for (int i = ind; i < s.size(); i++)\n {\n if (s[i] == 'N' || s[i] == '?')\n {\n cntr++;\n }\n else if (s[i] == 'Y' && cntr < k)\n return false;\n if (cntr == k)\n {\n if (i == s.size() - 1)\n return true;\n else if (s[i+1] != 'N')\n return true;\n else\n return false;\n }\n }\n return false;\n}\n\nint main()\n{\n int n, k;\n cin >> n >> k;\n string s;\n cin >> s;\n\n int cnt = (s[0] == 'N' ? 1 : 0), maxcnt = 0;\n\n for (int i = 1; i < s.size(); i++)\n {\n if (s[i] == 'N' && s[i-1] == 'N')\n cnt++;\n else if (s[i] == 'N')\n cnt = 1;\n else\n {\n maxcnt = max(cnt, maxcnt);\n cnt = 0;\n }\n maxcnt = max(cnt, maxcnt);\n }\n maxcnt = max(cnt, maxcnt);\n if (maxcnt > k)\n {\n cout << \"NO\" << endl;\n return 0;\n }\n else if (maxcnt == k)\n {\n cout << \"YES\" << endl;\n return 0;\n }\n\n\n bool ok = check(s, k, 0);\n if (ok)\n {\n cout << \"YES\" << endl;\n return 0;\n }\n for (int i = 1; i < s.size(); i++)\n if ((s[i] == 'N' || s[i] == '?') && s[i-1] != 'N')\n {\n ok = check(s, k, i);\n if (ok)\n break;\n }\n\n if (ok)\n cout << \"YES\" << endl;\n else\n cout << \"NO\" << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "331f477214fe11a4717dc15a22dacfdc", "src_uid": "5bd578d3da5837c259b222336a194d12", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint ss[105],yao,you[105],sum,cost,n,k,a,chuxian[105],later;\nstruct S {\n\tint num,id;\n}s[100];\nbool cmp(S a,S b) {\n\treturn (a.num57) c=getchar();\n\twhile(c>=48&&c<=57) x=x*10+c-48,c=getchar();\n\treturn x;\n}\nint OK(int kaishi,int jieshu,int shu) {\n\tfor(int i=kaishi;i<=jieshu;i++) {\n\t\tif(ss[i]==shu) return 0;\n\t}\n\treturn 1;\n}\nint main() {\n//\tfreopen(\"1.in\",\"r\",stdin);\n\tn=read(),k=read();\n\tfor(int i=1;i<=n;i++) \n\t\ta=read(),s[i].num=a,s[i].id=i;\n\tsort(s+1,s+n+1,cmp);\n\tint bei=1;\n\tss[s[1].id]=1;\n\tfor(int i=2;i<=n;i++)\n\t\tif(s[i].num!=s[i-1].num) ss[s[i].id]=++bei;\n\t\telse ss[s[i].id]=bei;\n\tfor(int i=1;i<=n;i++) {\n\t\tif(!you[ss[i]]) cost++,sum++,yao=1;\n\t\tif(sum>k) {\n\t\t\tfor(int k=1;k<=bei;k++) {\n\t\t\t\tif(you[k])\n\t\t\t\t\tif(OK(i+1,n,k)) {\n\t\t\t\t\t\tyou[k]=0,sum--;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t}\n\t\t\tif(sum>k) {\n\t\t\t\tfor(int j=i+1;j<=n;j++) {\n\t\t\t\t\tif(you[ss[j]]&&!chuxian[ss[j]])\n\t\t\t\t\t\tchuxian[ss[j]]=1,later=ss[j];\n\t\t\t\t}\n\t\t\t\tsum--,you[later]=0;\n\t\t\t}\n\t\t}\n\t\tif(yao) you[ss[i]]=1;\n\t\tyao=0;\n\t\tmemset(chuxian,0,sizeof(chuxian));\n\t}\n\tprintf(\"%d\",cost);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "25b163d81e3c52f07ef4355bdc61882b", "src_uid": "956228e31679caa9952b216e010f9773", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\n\nLL a, ans = 1, x = 1000000000;\n\nint main()\n{\n cin >> a;\n x *= x;\n LL t = x % a, u = 81;\n while (u)\n {\n if (u & 1) ans = (ans + t) % a;\n t = t * 2 % a, u >>= 1;\n }\n LL delta = ans == 0 ? 0 : a - ans;\n cout << 1 + delta << ' ' << x + delta << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms"], "code_uid": "21f4b0852969f612aefa510e9f24366f", "src_uid": "52b8d97f216ea22693bc16fadcd46dae", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\nconst int N=1e5;\nll arr1[N];\nll arr2[N];\nll vis[N];\nmapcnt;\nvectorarr;\nint main(){\n ll n,l;\n ll ans=0;\n cin>>n;\n for(int i=0;i=2){\n vis[i]=1;\n arr.push_back(arr1[i]);\n ans=ans+arr2[i];\n }\n }\n for(int i=0;i\n#include \n#include \nusing namespace std;\n\nusing cat = long long;\n\nconstexpr int MX = 60;\nconstexpr cat mod = 1000000007;\n\nint gcd(int a, int b) {\n\tif(a > b) swap(a, b);\n\treturn a ? gcd(b%a, a) : b;\n}\n\nint lcm(int a, int b) {\n\treturn a*b/gcd(a,b);\n}\n\ncat pw(cat a, cat e) {\n\tif(e <= 0) return 1;\n\tcat x = pw(a, e/2);\n\tx = x * x % mod;\n\treturn e&1 ? x * a % mod : x;\n}\n\nint main() {\n\tint N;\n\tcin >> N;\n\tstatic char A[MX+1];\n\tfor(int i = 0; i < N; i++) {\n\t\tint a;\n\t\ta = i+2;\n\t\tcin >> a;\n\t\tA[a] = 1;\n\t}\n\tif(A[1]) {\n\t\tcat ans = 1;\n\t\tfor(int i = 1; i < N; i++) ans = ans * i % mod;\n\t\tcout << ans << \"\\n\";\n\t\treturn 0;\n\t}\n\n\tvector divs;\n\tfor(int i = 2; i <= MX/3; i++) if(A[i]) divs.push_back(i);\n\twhile(true) {\n\t\tbool stop = true;\n\t\tfor(int i = 0; i < (int)divs.size(); i++) {\n\t\t\tbool has_div = false;\n\t\t\tfor(auto d : divs) has_div |= (divs[i]%d == 0 && divs[i] != d);\n\t\t\tif(has_div) {\n\t\t\t\tdivs.erase(begin(divs)+i);\n\t\t\t\tstop = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(stop) break;\n\t}\n\tvector L(MX+1, 0);\n\tfor(int i = 2; i <= MX; i++) {\n\t\tint p = 1;\n\t\tfor(auto d : divs) if(i%d == 0 && d != i) p = lcm(p, d);\n\t\tif(p != 1) L[i] = p;\n\t}\n\tvector G0(MX+1, 0);\n\tfor(int i = 2; i <= MX; i++) if(A[i])\n\t\tfor(int j = 2; j <= MX; j++) if(A[j] && i != j)\n\t\t\tfor(auto d : divs) if(i%d == 0 && j%d == 0 && d < i && d < j)\n\t\t\t\tG0[i] |= 1LL< cnt_compr(MX+1, 0);\n\tfor(int i = 2; i <= MX; i++) if(G0[i]) cnt_compr[L[i]]++;\n\tvector id(MX+1, 0);\n\tint V = 0;\n\tvector cnt_v;\n\tfor(int i = 2; i <= MX; i++) if(cnt_compr[i]) {\n\t\tid[i] = ++V;\n\t\tcnt_v.push_back(cnt_compr[i]);\n\t}\n\tvector G(V, 0);\n\tfor(int i = 2; i <= MX; i++)\n\t\tfor(int j = 2; j <= MX; j++) if((G0[i]>>j)&1)\n\t\t\tG[id[L[i]]-1] |= 1LL<<(id[L[j]]-1);\n\n\tvector comp;\n\tvector vis(V, 0);\n\tfor(int i = 0; i < V; i++) if(!vis[i]) {\n\t\tqueue q;\n\t\tq.push(i);\n\t\tvis[i] = 1;\n\t\tcat c_nw = 0;\n\t\twhile(!q.empty()) {\n\t\t\tc_nw |= 1LL<>j)&1 && !vis[j]) {\n\t\t\t\tvis[j] = 1;\n\t\t\t\tq.push(j);\n\t\t\t}\n\t\t\tq.pop();\n\t\t}\n\t\tcomp.push_back(c_nw);\n\t}\n\n\tvector fac(MX+1, 1), fac_inv(MX+1, 1);\n\tfor(int i = 2; i <= MX; i++) fac[i] = i * fac[i-1] % mod;\n\tfor(int i = 2; i <= MX; i++) fac_inv[i] = pw(fac[i], mod-2);\n\n\tcat ans = 1;\n\tint max_sz = 0;\n\tfor(auto c : comp) {\n\t\tint V_c = 0;\n\t\tvector id_c(V, 0);\n\t\tvector cnt_c;\n\t\tfor(int i = 0; i < V; i++) if((c>>i)&1) {\n\t\t\tid_c[i] = ++V_c;\n\t\t\tcnt_c.push_back(cnt_v[i]);\n\t\t}\n\t\tvector G_c(V_c, 0);\n\t\tfor(int i = 0; i < V; i++) if(id_c[i])\n\t\t\tfor(int j = 0; j < V; j++) if(id_c[j])\n\t\t\t\tif((G[i]>>j)&1) G_c[id_c[i]-1] |= 1LL<<(id_c[j]-1);\n\t\t// max covers\n\t\tvector cnt_cur(1<>j)&1)) sum += cnt_c[j];\n\t\t\tfor(int j = 0; j < V_c; j++) if(!((i>>j)&1) && G_c[j]&i) {\n\t\t\t\tcat C = fac[sum-1] * fac_inv[sum-cnt_c[j]] % mod * cnt_c[j] % mod;\n\t\t\t\tcnt_cur[i|(1<\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nconst int B=172;\nconst int maxn=44000;\nconst int maxm=20;\nunordered_map,int> mp;\nbool vis[maxm][maxn];\nbitset a[maxn];\nint nxt[maxn][10];\nint cnt=1;\nint bit[20];\nLL dp[20][10][maxn];\nLL solve(LL x,int k){\n\tif (x==0) return 1;\n\tint len=0;\n\twhile (x){\n\t\tbit[len++]=x%10;\n\t\tx/=10;\n\t}\n\tint u=1;\n\tLL ret=0;\n\tfor (int i=len-1;i>=0;i--){\n\t\tfor (int j=0;j Bit;\n\tBit.set(0);\n\tmp[Bit]=1;\n\ta[1]=Bit;\n\tvis[0][1]=1;\n\tfor (int i = 1;i\n#include\n#define LL long long\nusing namespace std;\nconst LL pri=1000000007;\nint C[205][205],n,m;\nLL f[205][205][5];\nLL dfs(int x,int y,int z)\n{\n\tif(z*(3*x-2)\nusing namespace std;\ntypedef long long ll;\n#define fi first\n#define se second\nint n,m;\nll mod;\nll dp[501];\nll w[501];\nll C[501][501];\nll dp2[502][502];\nll w2[502][502];\nint main(){\n\tios::sync_with_stdio(false);\n\tcin >> n >> m >> mod;\n\tC[0][0]=1;\n\tfor(int i=1; i<=n ;i++){\n\t\tC[i][0]=1;\n\t\tfor(int j=1; j<=n ;j++){\n\t\t\tC[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;\n\t\t}\n\t}\n\tw[0]=1;\n\tfor(int i=1; i<=n ;i++){\n\t\tfor(int j=1; j<=i ;j++){\n\t\t\tll cur=C[i-1][j-1]*w[j-1]%mod*w[i-j]%mod;\n\t\t\tw[i]=(w[i]+cur*(i+1))%mod;\n\t\t\t//cout << cur << ' ' << ((i-j)*(i-j+1)/2+(j)*(j-1)/2) << endl;\n\t\t\tdp[i]=(dp[i]+C[i-1][j-1]*(dp[j-1]*w[i-j]%mod+w[j-1]*dp[i-j]%mod)%mod*(i+1)+cur*((i-j)*(i-j+1)/2+(j)*(j-1)/2))%mod;\n\t\t\t//cout << i <<' ' << j << ' ' << C[i-1][j-1]*(dp[j-1]*w[i-j]%mod+w[j-1]*dp[i-j]%mod)%mod*(i+1)+cur*((i-j)*(i-j+1)/2+(j)*(j-1)/2) << endl;\n\t\t}\n\t}\n\tw2[0][0]=1;\n\tfor(int i=1; i<=n+1 ;i++){\n\t\tfor(int k=0; k<=m ;k++){\n\t\t\tfor(int j=1; j<=min(i-1,k) ;j++){\n\t\t\t\tint pi=i-j-1;\n\t\t\t\tint pk=k-j;\n\t\t\t\t//cout << i << ' ' << k << ' ' << pi << ' ' << pk << endl;\n\t\t\t\tll cw=C[k][j];\n\t\t\t\tw2[i][k]=(w2[i][k]+cw*w2[pi][pk]%mod*w[j]%mod)%mod;\n\t\t\t\tdp2[i][k]=(dp2[i][k]+cw*w2[pi][pk]%mod*dp[j]+cw*dp2[pi][pk]%mod*w[j])%mod;\n\t\t\t}\n\t\t}\n\t\tfor(int j=0; j<=m ;j++){\n\t\t\tdp2[i][j]=(dp2[i][j]+dp2[i-1][j])%mod;\n\t\t\tw2[i][j]=(w2[i][j]+w2[i-1][j])%mod;\n\t\t}\n\t}\n\tcout<< dp2[n+1][m] << '\\n';\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "a2403b114eaa317b826545c1e5da31c0", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n \n#define forn(i,n) for(int i=0;i<(int)(n);i++)\n#define si(c) ((int)(c).size())\n#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)\n#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)\n#define all(c) (c).begin(), (c).end()\n#define D(a) cerr << #a << \"=\" << (a) << endl;\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n \ntypedef long long int ll;\ntypedef vector vi;\ntypedef pair pii;\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n\nint main() {\n ios_base::sync_with_stdio(0); cin.tie(0);\n\n int n, m, MOD;\n while (cin >> n >> m >> MOD) {\n\n auto add = [&](ll &x, ll y) {\n x += y;\n if (x >= MOD) x -= MOD;\n };\n\n vector comb(m+1, vi(m+1));\n for (int i = 0; i <= m; i++) {\n comb[i][0] = 1;\n for (int j = 1; j <= i; j++) {\n comb[i][j] = comb[i-1][j-1] + comb[i-1][j];\n if (comb[i][j] >= MOD) comb[i][j] -= MOD;\n }\n }\n\n vi ways(m+1), sum(m+1), sum_n(m+1);\n for (int n = 1; n <= m; n++) sum_n[n] = n + sum_n[n-1];\n\n ways[0] = 1;\n for (int n = 1; n <= m; n++) {\n for (int pos = 0; pos < n; pos++) {\n int a = pos, b = n-1-pos;\n ll to_ways = ways[a] * ways[b] % MOD;\n add(ways[n], (n+1) * comb[n-1][pos] % MOD * to_ways % MOD);\n\n ll to_sum = 0;\n add(to_sum, sum[a] * ways[b] % MOD);\n add(to_sum, sum[b] * ways[a] % MOD);\n add(sum[n], (n+1) * comb[n-1][pos] % MOD * to_sum % MOD);\n add(sum[n], to_ways * comb[n-1][pos] % MOD * (sum_n[a] + sum_n[b]) % MOD);\n }\n }\n\n vector ways2(n+1, vi(m+1)), sum2(n+1, vi(m+1));\n ways2[0][0] = 1;\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j <= min(i,m); j++) {\n ways2[i][j] = ways2[i-1][j];\n sum2[i][j] = sum2[i-1][j];\n ll coef = j;\n for (int k = 1; k <= j; k++) {\n int ni = max(0, i-k-1);\n int nj = j-k;\n add(ways2[i][j], ways[k] * ways2[ni][nj] % MOD * comb[j][k] % MOD);\n add(sum2[i][j], (sum[k] * ways2[ni][nj] + ways[k] * sum2[ni][nj]) % MOD * comb[j][k] % MOD);\n }\n }\n }\n cout << sum2[n][m] << endl;\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "628b1a363022a62ded8cfc7747db11ca", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)\n#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)\nusing namespace std;\nconst int maxn=500+5;\nint n,m,mod;\nint upd(const int &x){\n\tif(x<0)return x+mod;\n\tif(x>=mod)return x-mod;\n\treturn x;\n}\nvoid add(int &x,const int &y){\n\tx=upd(x+y);\n}\nint ksm(int x,int k){\n\tint ans=1;\n\twhile(k){\n\t\tif(k&1)ans=1ll*ans*x%mod;\n\t\tx=1ll*x*x%mod;\n\t\tk>>=1;\n\t}\n\treturn ans;\n}\nint jc[maxn<<2],inv[maxn<<2];\nvoid init(int n){\n\tjc[0]=1;\n\tfr(i,1,n)jc[i]=1ll*jc[i-1]*i%mod;\n\tinv[n]=ksm(jc[n],mod-2);\n\tfd(i,n-1,0)inv[i]=1ll*inv[i+1]*(i+1)%mod;\t\n}\nint C(int n,int m){\n\tif(n<0||m<0||n f,g;\nint S(int n){\n\treturn n*(n+1)/2;\n}\nvector operator *(const vector &A ,const vector &B){\n\tvector C(A.size()+B.size()-1);\n\tfr(i,0,A.size()-1)fr(j,0,B.size()-1)add(C[i+j],1ll*A[i]*B[j]%mod);\n\treturn C;\n}\nint G(int n,int m){\n\treturn C(m+n-1,n-1);\t\n}\nint main(){\n#ifdef pig\n\tfreopen(\"1439D.in\",\"r\",stdin);\n\tfreopen(\"1439D.out\",\"w\",stdout);\n#endif\n\tcin>>m>>n>>mod;\n\tinit(m<<1);\n\tf.resize(n+1);g.resize(n+1);\n\tf[0]=1;\n\tfr(i,1,n){\n\t\tfr(j,0,i-1)add(f[i],1ll*f[j]*f[i-1-j]%mod*C(i-1,j)%mod);\n\t\tf[i]=1ll*f[i]*(i+1)%mod;\n\t}\n\tfr(i,1,n){\n\t\tfr(j,0,i-1)add(g[i],(2ll*f[j]*g[i-1-j]%mod*C(i-1,j)%mod*(i+1)%mod+1ll*C(i-1,j)*(S(j)+S(i-1-j))%mod*f[j]%mod*f[i-1-j]%mod)%mod);\n\t}\n\t//cout<\nusing namespace std;\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define mt make_tuple\n#define pii pair\n#define pll pair\n#define ldb double\ntemplatevoid ckmn(T&a,T b){a=min(a,b);}\ntemplatevoid ckmx(T&a,T b){a=max(a,b);}\nvoid rd(int&x){scanf(\"%i\",&x);}\nvoid rd(ll&x){scanf(\"%lld\",&x);}\nvoid rd(char*x){scanf(\"%s\",x);}\nvoid rd(ldb&x){scanf(\"%lf\",&x);}\nvoid rd(string&x){scanf(\"%s\",&x);}\ntemplatevoid rd(pair&x){rd(x.first);rd(x.second);}\ntemplatevoid rd(vector&x){for(T&i:x)rd(i);}\ntemplatevoid rd(T&x,A&...args){rd(x);rd(args...);}\ntemplatevoid rd(){T x;rd(x);return x;}\nint ri(){int x;rd(x);return x;}\ntemplatevector rv(int n){vector x(n);rd(x);return x;}\ntemplatevoid ra(T a[],int n,int st=1){for(int i=0;ivoid ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;ivoid ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i E[],int m,bool dir=0){for(int i=0,u,v;ivoid re(vector> E[],int m,bool dir=0){for(int i=0,u,v;i=mod?a-mod:a;}\nvoid ckadd(int&a,int b){a=add(a,b);}\nint sub(int a,int b){a-=b;return a<0?a+mod:a;}\nvoid cksub(int&a,int b){a=sub(a,b);}\nint mul(int a,int b){return (ll)a*b%mod;}\nvoid ckmul(int&a,int b){a=mul(a,b);}\nint powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;}\nint inv(int x){return powmod(x,mod-2);}\n\nconst int N=505;\nint blok[N],sum[N];\nint c2(int x){return ((ll)x*(x+1)/2)%mod;}\nint dp[N][N],ways[N][N];\n\n//const int M=1050;\n//int F[M],I[M];\n//int binom(int n, int k){ return mul(F[n],mul(I[k],I[n-k]));}\nint binom[N][N];\nvoid calc()\n{\n\tbinom[0][0]=1;\n\tfor(int i=1;i=0){\n\t\t\tckadd(ans,mul(dp[m][i],binom[i+1+lef-1][lef]));\n\t\t}\n\t}\n\tprintf(\"%i\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "99b1240e49eb01026ccdb485d7ca0f57", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntemplate\nstruct Z_p{\n\tusing Type = typename decay::type;\n\tstatic vector mod_inv; \n\tconstexpr Z_p(): value(){ }\n\ttemplate Z_p(const U &x){ value = normalize(x); }\n\ttemplate static Type normalize(const U &x){\n\t\tType v;\n\t\tif(-mod() <= x && x < mod()) v = static_cast(x);\n\t\telse v = static_cast(x % mod());\n\t\tif(v < 0) v += mod();\n\t\treturn v;\n\t}\n\tconst Type& operator()() const{ return value; }\n\ttemplate explicit operator U() const{ return static_cast(value); }\n\tconstexpr static Type mod(){ return T::value; }\n\tZ_p &operator+=(const Z_p &otr){ if((value += otr.value) >= mod()) value -= mod(); return *this; }\n\tZ_p &operator-=(const Z_p &otr){ if((value -= otr.value) < 0) value += mod(); return *this; }\n\ttemplate Z_p &operator+=(const U &otr){ return *this += Z_p(otr); }\n\ttemplate Z_p &operator-=(const U &otr){ return *this -= Z_p(otr); }\n\tZ_p &operator++(){ return *this += 1; }\n\tZ_p &operator--(){ return *this -= 1; }\n\tZ_p operator++(int){ Z_p result(*this); *this += 1; return result; }\n\tZ_p operator--(int){ Z_p result(*this); *this -= 1; return result; }\n\tZ_p operator-() const{ return Z_p(-value); }\n\ttemplate\n\ttypename enable_if::Type, int>::value, Z_p>::type &operator*=(const Z_p& rhs){\n\t\t#ifdef _WIN32\n\t\tuint64_t x = static_cast(value) * static_cast(rhs.value);\n\t\tuint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m;\n\t\tasm(\n\t\t\t\"divl %4; \\n\\t\"\n\t\t\t: \"=a\" (d), \"=d\" (m)\n\t\t\t: \"d\" (xh), \"a\" (xl), \"r\" (mod())\n\t\t);\n\t\tvalue = m;\n\t\t#else\n\t\tvalue = normalize(static_cast(value) * static_cast(rhs.value));\n\t\t#endif\n\t\treturn *this;\n\t}\n\ttemplate\n\ttypename enable_if::Type, int64_t>::value, Z_p>::type &operator*=(const Z_p &rhs){\n\t\tint64_t q = static_cast(static_cast(value) * rhs.value / mod());\n\t\tvalue = normalize(value * rhs.value - q * mod());\n\t\treturn *this;\n\t}\n\ttemplate\n\ttypename enable_if::Type>::value, Z_p>::type &operator*=(const Z_p &rhs){\n\t\tvalue = normalize(value * rhs.value);\n\t\treturn *this;\n\t}\n\tZ_p operator^(long long e) const{\n\t\tZ_p b = *this, res = 1;\n\t\tif(e < 0) b = 1 / b, e = -e;\n\t\tfor(; e; b *= b, e >>= 1) if(e & 1) res *= b;\n\t\treturn res;\n\t}\n\tZ_p &operator^=(const long long &e){ return *this = *this ^ e; }\n\tZ_p &operator/=(const Z_p &otr){\n\t\tType a = otr.value, m = mod(), u = 0, v = 1;\n\t\tif(a < int(mod_inv.size())) return *this *= mod_inv[a];\n\t\twhile(a){\n\t\t\tType t = m / a;\n\t\t\tm -= t * a; swap(a, m);\n\t\t\tu -= t * v; swap(u, v);\n\t\t}\n\t\tassert(m == 1);\n\t\treturn *this *= u;\n\t}\n\ttemplate friend const Z_p &abs(const Z_p &v){ return v; }\n\tType value;\n};\ntemplate bool operator==(const Z_p &lhs, const Z_p &rhs){ return lhs.value == rhs.value; }\ntemplate::value>::type* = nullptr> bool operator==(const Z_p& lhs, U rhs){ return lhs == Z_p(rhs); }\ntemplate::value>::type* = nullptr> bool operator==(U lhs, const Z_p &rhs){ return Z_p(lhs) == rhs; }\ntemplate bool operator!=(const Z_p &lhs, const Z_p &rhs){ return !(lhs == rhs); }\ntemplate::value>::type* = nullptr> bool operator!=(const Z_p &lhs, U rhs){ return !(lhs == rhs); }\ntemplate::value>::type* = nullptr> bool operator!=(U lhs, const Z_p &rhs){ return !(lhs == rhs); }\ntemplate bool operator<(const Z_p &lhs, const Z_p &rhs){ return lhs.value < rhs.value; }\ntemplate bool operator>(const Z_p &lhs, const Z_p &rhs){ return lhs.value > rhs.value; }\ntemplate bool operator<=(const Z_p &lhs, const Z_p &rhs){ return lhs.value <= rhs.value; }\ntemplate bool operator>=(const Z_p &lhs, const Z_p &rhs){ return lhs.value >= rhs.value; }\ntemplate Z_p operator+(const Z_p &lhs, const Z_p &rhs){ return Z_p(lhs) += rhs; }\ntemplate::value>::type* = nullptr> Z_p operator+(const Z_p &lhs, U rhs){ return Z_p(lhs) += rhs; }\ntemplate::value>::type* = nullptr> Z_p operator+(U lhs, const Z_p &rhs){ return Z_p(lhs) += rhs; }\ntemplate Z_p operator-(const Z_p &lhs, const Z_p &rhs){ return Z_p(lhs) -= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator-(const Z_p& lhs, U rhs){ return Z_p(lhs) -= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator-(U lhs, const Z_p &rhs){ return Z_p(lhs) -= rhs; }\ntemplate Z_p operator*(const Z_p &lhs, const Z_p &rhs){ return Z_p(lhs) *= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator*(const Z_p& lhs, U rhs){ return Z_p(lhs) *= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator*(U lhs, const Z_p &rhs){ return Z_p(lhs) *= rhs; }\ntemplate Z_p operator/(const Z_p &lhs, const Z_p &rhs) { return Z_p(lhs) /= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator/(const Z_p& lhs, U rhs) { return Z_p(lhs) /= rhs; }\ntemplate::value>::type* = nullptr> Z_p operator/(U lhs, const Z_p &rhs) { return Z_p(lhs) /= rhs; }\ntemplate istream &operator>>(istream &in, Z_p &number){\n\ttypename common_type::Type, int64_t>::type x;\n\tin >> x;\n\tnumber.value = Z_p::normalize(x);\n\treturn in;\n}\ntemplate ostream &operator<<(ostream &out, const Z_p &number){ return out << number(); }\n\nusing ModType = int;\nstruct VarMod{ static ModType value; };\nModType VarMod::value;\nModType &mod = VarMod::value;\nusing Zp = Z_p;\n\ntemplate vector::Type> Z_p::mod_inv;\ntemplate::type, mod>>\nvoid precalc_inverse(size_t SZ){\n\tauto &inv = Z_p::mod_inv;\n\tif(inv.empty()) inv.assign(2, 1);\n\tfor(; inv.size() <= SZ; ) inv.push_back((mod - 1LL * mod / int(inv.size()) * inv[mod % int(inv.size())] % mod) % mod);\n}\n\n// Requires Z_p\ntemplate\nstruct combinatorics{\n\tvector inv, fact, invfact;\n\tvector> stir1, stir2;\n\tcombinatorics(): inv(SZ << 1 | 1, 1), fact(SZ << 1 | 1, 1), invfact(SZ << 1 | 1, 1){\n\t\tfor(int i = 1; i <= SZ << 1; ++ i) fact[i] = fact[i - 1] * i;\n\t\tinvfact[SZ << 1] = 1 / fact[SZ << 1];\n\t\tfor(int i = (SZ << 1) - 1; i >= 0; -- i){\n\t\t\tinvfact[i] = invfact[i + 1] * (i + 1);\n\t\t\tinv[i + 1] = invfact[i + 1] * fact[i];\n\t\t}\n\t} // O(SZ)\n\tT C(int n, int k){ return n < k ? 0 : fact[n] * invfact[k] * invfact[n - k]; }\n\tT P(int n, int k){ return n < k ? 0 : fact[n] * invfact[n - k]; }\n\tT H(int n, int k){ return C(n + k - 1, k); }\n\tvector precalc_power(int base, int n = SZ << 1){\n\t\tvector res(n + 1, 1);\n\t\tfor(int i = 1; i <= n; ++ i) res[i] = res[i - 1] * base;\n\t\treturn res;\n\t}\n\tT naive_C(long long n, long long k){\n\t\tif(n < k) return 0;\n\t\tT res = 1;\n\t\tk = min(k, n - k);\n\t\tfor(long long i = n; i > n - k; -- i) res *= i;\n\t\treturn res * invfact[k];\n\t}\n\tT naive_P(long long n, int k){\n\t\tif(n < k) return 0;\n\t\tT res = 1;\n\t\tfor(long long i = n; i > n - k; -- i) res *= i;\n\t\treturn res;\n\t}\n\tT naive_H(long long n, long long k){ return naive_C(n + k - 1, k); }\n\tbool parity_C(long long n, long long k){ return n < k ? 0 : k & n - k ^ 1; }\n\t// Catalan's Trapzoids\n\t// # of bitstrings of n Xs and k Ys such that in each initial segment, (# of X) + m > (# of Y) \n\tT Cat(int n, int k, int m = 1){\n\t\tif(m <= 0) return 0;\n\t\telse if(k >= 0 && k < m) return C(n + k, k);\n\t\telse if(k < n + m) return C(n + k, k) - C(n + k, k - m);\n\t\telse return 0;\n\t}\n\t// Stirling number\n\t// First kind (unsigned): # of n-permutations with k disjoint cycles\n\t// Also the coefficient of x^k for x_n = x(x+1)...(x+n-1)\n\t// Second kind: # of ways to partition a set of size n into r non-empty sets\n\t// Satisfies sum{k=0~n}(x_k) = x^n\n\tarray pre{};\n\ttemplate\n\tvoid precalc_stir(int n, int k){\n\t\tauto &s = FIRST ? stir1 : stir2;\n\t\tpre[!FIRST] = true;\n\t\ts.resize(n + 1, vector(k + 1, 1));\n\t\tfor(int i = 1; i <= n; ++ i) for(int j = 1; j <= k; ++ j){\n\t\t\ts[i][j] = (FIRST ? i - 1 : j) * s[i - 1][j] + s[i - 1][j - 1];\n\t\t}\n\t}\n\t// unsigned\n\tT Stir1(int n, int k){\n\t\tif(n < k) return 0;\n\t\tassert(pre[0]);\n\t\treturn stir1[n][k];\n\t}\n\tT Stir2(long long n, int k){\n\t\tif(n < k) return 0;\n\t\tif(pre[1] && n < (int)stir2.size()) return stir2[n][k];\n\t\tT res = 0;\n\t\tfor(int i = 0, sign = 1; i <= k; ++ i, sign *= -1) res += sign * C(k, i) * (Zp(k - i) ^ n);\n\t\treturn res * invfact[k];\n\t}\n\tbool parity_Stir2(long long n, long long k){ return n < k ? 0 : k ? !(n - k & k - 1 >> 1) : 0; }\n};\n\n\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tcin.exceptions(ios::badbit | ios::failbit);\n\tint n, m;\n\tcin >> n >> m >> mod;\n\tcombinatorics<500> C;\n\tvector cnt(n + 1), sum(n + 1);\n\tcnt[0] = 1;\n\tfor(auto i = 1; i <= n; ++ i){\n\t\tfor(auto p = 0; p < i; ++ p){\n\t\t\tcnt[i] += C.C(i - 1, p) * cnt[p] * cnt[i - 1 - p] * (i + 1);\n\t\t\tsum[i] += C.C(i - 1, p) * (cnt[p] * sum[i - 1 - p] + sum[p] * cnt[i - 1 - p]) * (i + 1);\n\t\t\tfor(auto q = 0; q < i; ++ q){\n\t\t\t\tsum[i] += abs(p - q) * C.C(i - 1, p) * cnt[p] * cnt[i - 1 - p];\n\t\t\t}\n\t\t}\n\t}\n\tvector> cnt2(n + 1, vector(n + 1)), sum2(cnt2);\n\tcnt2[0][0] = 1;\n\tfor(auto i = 1; i <= n; ++ i){\n\t\tcnt2[i][i] = cnt[i];\n\t\tsum2[i][i] = sum[i];\n\t\tfor(auto j = 0; j <= i; ++ j){\n\t\t\tfor(auto last = 0; last <= min(i - 1, j); ++ last){\n\t\t\t\tcnt2[i][j] += C.C(j, last) * cnt[last] * cnt2[i - last - 1][j - last];\n\t\t\t\tsum2[i][j] += C.C(j, last) * (cnt[last] * sum2[i - last - 1][j - last] + sum[last] * cnt2[i - last - 1][j - last]);\n\t\t\t}\n\t\t}\n\t}\n\tcout << sum2[n][m] << \"\\n\";\n\treturn 0;\n}\n\n/*\n\n*/\n\n////////////////////////////////////////////////////////////////////////////////////////\n// //\n// Coded by Aeren //\n// //\n////////////////////////////////////////////////////////////////////////////////////////", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "f3751cce2113771370daa35a485d7133", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int maxn = 510;\nint n, m, P, C[maxn][maxn], f1[maxn], f2[maxn][maxn], g1[maxn], g2[maxn][maxn];\n\nint main() {\n scanf(\"%d %d %d\", &n, &m, &P);\n for (int i = 0; i <= n; i++) {\n for (int j = C[i][0] = 1; j <= i; j++) {\n C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;\n }\n }\n for (int i = f1[0] = 1; i <= n; i++) {\n for (int j = 1; j <= i; j++) {\n f1[i] = (f1[i] + 1LL * f1[j - 1] * f1[i - j] % P * C[i - 1][j - 1]) % P;\n int t = (1LL * f1[j - 1] * g1[i - j] + 1LL * g1[j - 1] * f1[i - j]) % P\n * (i + 1) % P;\n g1[i] = (g1[i] + (1LL * f1[j - 1] * f1[i - j] % P * (C[j][2] +\n C[i - j + 1][2]) % P + t) % P * C[i - 1][j - 1]) % P;\n }\n f1[i] = 1LL * (i + 1) * f1[i] % P;\n }\n f2[0][0] = 1;\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j <= min(i, m); j++) {\n f2[i][j] = f2[i - 1][j], g2[i][j] = g2[i - 1][j];\n for (int k = 1; k <= j; k++) {\n f2[i][j] = (f2[i][j] + 1LL * (i - k ? f2[i - k - 1][j - k] : 1) *\n f1[k] % P * C[j][k]) % P;\n g2[i][j] = (g2[i][j] + (1LL * (i - k ? f2[i - k - 1][j - k] : 1) *\n g1[k] + 1LL * (i - k ? g2[i - k - 1][j - k] : 0) * f1[k])\n % P * C[j][k]) % P;\n }\n }\n }\n printf(\"%d\\n\", g2[n][m]);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "479b6111f950cf3c326c5e818dad57f5", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"O3\")\n#include \n// #include \n// using namespace __gnu_pbds;\n// gp_hash_table mapka;\n\nusing namespace std;\n#define PB push_back\n#define MP make_pair\n#define LL long long\n#define int LL\n#define FOR(i,a,b) for(int i = (a); i <= (b); i++)\n#define RE(i,n) FOR(i,1,n)\n#define REP(i,n) FOR(i,0,(int)(n)-1)\n#define R(i,n) REP(i,n)\n#define VI vector\n#define PII pair\n#define LD long double\n#define FI first\n#define SE second\n#define st FI\n#define nd SE\n#define ALL(x) (x).begin(), (x).end()\n#define SZ(x) ((int)(x).size())\n\ntemplate void mini(C &a4, C b4) { a4 = min(a4, b4); }\ntemplate void maxi(C &a4, C b4) { a4 = max(a4, b4); }\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<'='< ostream &operator<<(ostream& os, vector V) {\n os << \"[\"; for (auto vv : V) os << vv << \",\"; return os << \"]\";\n}\ntemplate ostream &operator<<(ostream &os, pair P) {\n return os << \"(\" << P.st << \",\" << P.nd << \")\";\n}\n\n#ifdef LOCAL\n#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug(...) (__VA_ARGS__)\n#define cerr if(0)cout\n#endif\n\nint n,m,P;\nvoid mulself(int &a,int b){\n a = a * b % P;\n}\nint mul(int a,int b){\n return a * b % P;\n}\nvoid add(int &a,int b){\n a += b;\n if(a >= P)\n a -= P;\n}\nconst int MAX = 511;\nint dwu[MAX][MAX];\nint dp1[MAX];\nint dp2[MAX][MAX];\nint dp3[MAX][MAX];\nint ile[MAX];\nint32_t main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(11);\n cerr << fixed << setprecision(6);\n cin >> n >> m >> P;\n R(i,n + 1)R(j,i + 1){\n if(j == 0){\n dwu[i][j] = 1;\n }else{\n dwu[i][j] = dwu[i-1][j];\n add(dwu[i][j], dwu[i-1][j-1]);\n }\n }\n \n dp1[0] = 0;\n ile[0] = 1;\n R(i,m+1)if(i){\n R(j,i){\n int pom = 0;\n add(pom,mul(dp1[j], ile[i-j-1]));\n add(pom,mul(dp1[i-j-1], ile[j]));\n mulself(pom, j + 1);\n add(pom, mul(mul(ile[i-j-1], ile[j]), j * (j + 1) / 2));\n mulself(pom,dwu[i-1][j]);\n debug(pom,ile[i-j-1], ile[j], j * (j + 1) / 2,dwu[i-1][j]);\n add(dp1[i],pom);\n pom = dwu[i-1][j];\n mulself(pom, ile[j]);\n mulself(pom, ile[i-j-1]);\n mulself(pom, j + 1);\n add(ile[i],pom);\n }\n add(ile[i],ile[i]);\n add(dp1[i],dp1[i]);\n debug(i,ile[i],dp1[i]);\n }\n \n dp2[0][0] = 1;\n dp3[0][0] = 0;\n R(i,n+1)R(j,m+1)R(k,m+1)if(j + k <= m && i + k + 1 <= n + 1){\n int pom = dp2[i][j];\n mulself(pom, dwu[m - j][k]);\n mulself(pom, ile[k]);\n int pom3 = dp3[i][j];\n mulself(pom3, ile[k]);\n add(pom3, mul(dp1[k], dp2[i][j]));\n mulself(pom3, dwu[m - j][k]);\n add(dp2[i + k + 1][j + k], pom);\n add(dp3[i + k + 1][j + k], pom3);\n }\n cout << dp3[n + 1][m] << \"\\n\";\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "74ee92e36b68af3e82f28fffd0eaeabc", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC target(\"avx\")\n//#undef LOCAL\n\n\n#include \n#include \n#include \n\n#ifdef _MSC_VER\n#include \n#endif\n\n\n#include \n\n#ifdef _MSC_VER\n#include \n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n unsigned int umod() const { return _m; }\n\n unsigned int mul(unsigned int a, unsigned int b) const {\n\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate constexpr bool is_prime = is_prime_constexpr(n);\n\nconstexpr std::pair inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include \n#include \n#include \n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate \nusing is_signed_int128 =\n typename std::conditional::value ||\n std::is_same::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_unsigned_int128 =\n typename std::conditional::value ||\n std::is_same::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing make_unsigned_int128 =\n typename std::conditional::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate \nusing is_integral = typename std::conditional::value ||\n is_signed_int128::value ||\n is_unsigned_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_signed_int = typename std::conditional<(is_integral::value &&\n std::is_signed::value) ||\n is_signed_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_unsigned_int =\n typename std::conditional<(is_integral::value &&\n std::is_unsigned::value) ||\n is_unsigned_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing to_unsigned = typename std::conditional<\n is_signed_int128::value,\n make_unsigned_int128,\n typename std::conditional::value,\n std::make_unsigned,\n std::common_type>::type>::type;\n\n#else\n\ntemplate using is_integral = typename std::is_integral;\n\ntemplate \nusing is_signed_int =\n typename std::conditional::value && std::is_signed::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_unsigned_int =\n typename std::conditional::value &&\n std::is_unsigned::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing to_unsigned = typename std::conditional::value,\n std::make_unsigned,\n std::common_type>::type;\n\n#endif\n\ntemplate \nusing is_signed_int_t = std::enable_if_t::value>;\n\ntemplate \nusing is_unsigned_int_t = std::enable_if_t::value>;\n\ntemplate using to_unsigned_t = typename to_unsigned::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate using is_modint = std::is_base_of;\ntemplate using is_modint_t = std::enable_if_t::value>;\n\n} // namespace internal\n\ntemplate * = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template * = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template * = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime;\n};\n\ntemplate struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template * = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template * = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate internal::barrett dynamic_modint::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate \nusing is_static_modint = std::is_base_of;\n\ntemplate \nusing is_static_modint_t = std::enable_if_t::value>;\n\ntemplate struct is_dynamic_modint : public std::false_type {};\ntemplate \nstruct is_dynamic_modint> : public std::true_type {};\n\ntemplate \nusing is_dynamic_modint_t = std::enable_if_t::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\nnamespace yosupo {\n\nnamespace internal {\n\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n}\n\n} // namespace internal\n\nint bsf(unsigned int n) { return __builtin_ctz(n); }\nint bsf(unsigned long n) { return __builtin_ctzl(n); }\nint bsf(unsigned long long n) { return __builtin_ctzll(n); }\n\nint bsr(unsigned int n) {\n return 8 * (int)sizeof(unsigned int) - 1 - __builtin_clz(n);\n}\nint bsr(unsigned long n) {\n return 8 * (int)sizeof(unsigned long) - 1 - __builtin_clzl(n);\n}\nint bsr(unsigned long long n) {\n return 8 * (int)sizeof(unsigned long long) - 1 - __builtin_clzll(n);\n}\n\n} // namespace yosupo\n\n#include \n#include \n#include \n\nnamespace yosupo {\n\nnamespace internal {\n\ntemplate \nusing is_signed_int128 =\n typename std::conditional::value ||\n std::is_same::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_unsigned_int128 =\n typename std::conditional::value ||\n std::is_same::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing make_unsigned_int128 =\n typename std::conditional::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate \nusing is_integral =\n typename std::conditional::value ||\n internal::is_signed_int128::value ||\n internal::is_unsigned_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_signed_int = typename std::conditional<(is_integral::value &&\n std::is_signed::value) ||\n is_signed_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing is_unsigned_int =\n typename std::conditional<(is_integral::value &&\n std::is_unsigned::value) ||\n is_unsigned_int128::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate \nusing to_unsigned = typename std::conditional<\n is_signed_int128::value,\n make_unsigned_int128,\n typename std::conditional::value,\n std::make_unsigned,\n std::common_type>::type>::type;\n\ntemplate \nusing is_integral_t = std::enable_if_t::value>;\n\ntemplate \nusing is_signed_int_t = std::enable_if_t::value>;\n\ntemplate \nusing is_unsigned_int_t = std::enable_if_t::value>;\n\ntemplate using to_unsigned_t = typename to_unsigned::type;\n\n} // namespace internal\n\n} // namespace yosupo\n\nnamespace yosupo {\n\nstruct Scanner {\n public:\n Scanner(FILE* fp) : fd(fileno(fp)) {}\n\n void read() {}\n template void read(H& h, T&... t) {\n bool f = read_single(h);\n assert(f);\n read(t...);\n }\n\n int read_unsafe() { return 0; }\n template int read_unsafe(H& h, T&... t) {\n bool f = read_single(h);\n if (!f) return 0;\n return 1 + read_unsafe(t...);\n }\n\n private:\n static constexpr size_t SIZE = 1 << 15;\n\n bool read_single(std::string& ref) {\n if (!skip_space()) return false;\n ref = \"\";\n while (true) {\n char c = top();\n if (c <= ' ') break;\n ref += c;\n st++;\n }\n return true;\n }\n bool read_single(double& ref) {\n std::string s;\n if (!read_single(s)) return false;\n ref = std::stod(s);\n return true;\n }\n\n template * = nullptr>\n bool read_single(T& sref) {\n using U = internal::to_unsigned_t;\n if (!skip_space(50)) return false;\n bool neg = false;\n if (line[st] == '-') {\n neg = true;\n st++;\n }\n U ref = 0;\n do {\n ref = 10 * ref + (line[st++] & 0x0f);\n } while (line[st] >= '0');\n sref = neg ? -ref : ref;\n return true;\n }\n template * = nullptr>\n bool read_single(U& ref) {\n if (!skip_space(50)) return false;\n ref = 0;\n do {\n ref = 10 * ref + (line[st++] & 0x0f);\n } while (line[st] >= '0');\n return true;\n }\n\n int fd = -1;\n char line[SIZE];\n size_t st = 0, ed = 0;\n bool eof = false;\n bool reread() {\n if (ed - st >= 50) return true;\n if (st > SIZE / 2) {\n std::memmove(line, line + st, ed - st);\n ed -= st;\n st = 0;\n }\n if (eof) return false;\n auto u = ::read(fd, line + ed, SIZE - ed);\n if (u == 0) {\n eof = true;\n line[ed] = '\\0';\n u = 1;\n }\n ed += u;\n return true;\n }\n\n char top() {\n if (st == ed) {\n bool f = reread();\n assert(f);\n }\n return line[st];\n }\n\n bool skip_space(int token_len = 0) {\n while (true) {\n while (st != ed && line[st] <= ' ') st++;\n if (ed - st > token_len) return true;\n for (int i = st; i < ed; i++) {\n if (line[i] <= ' ') return true;\n }\n if (!reread()) return false;\n }\n }\n};\n\nstruct Printer {\n public:\n template void write() {}\n template \n void write(const H& h, const T&... t) {\n if (F) write_single(' ');\n write_single(h);\n write(t...);\n }\n template void writeln(const T&... t) {\n write(t...);\n write_single('\\n');\n }\n\n Printer(FILE* _fp) : fd(fileno(_fp)) {}\n ~Printer() { flush(); }\n\n void flush() {\n ::write(fd, line, pos);\n pos = 0;\n }\n\n private:\n static std::array, 100> small;\n static std::array tens;\n\n static int calc_len(unsigned long long x) {\n int i = (bsr(x) * 3 + 3) / 10;\n if (x < tens[i])\n return i;\n else\n return i + 1;\n }\n\n static constexpr size_t SIZE = 1 << 15;\n int fd;\n char line[SIZE];\n size_t pos = 0;\n void write_single(const char& val) {\n if (pos == SIZE) flush();\n line[pos++] = val;\n }\n\n template * = nullptr>\n void write_single(const T& val) {\n using U = internal::to_unsigned_t;\n if (val == 0) {\n write_single('0');\n return;\n }\n if (pos > SIZE - 50) flush();\n U uval = val;\n if (val < 0) {\n write_single('-');\n uval = -uval;\n }\n write_unsigned(uval);\n }\n\n template * = nullptr>\n void write_single(U uval) {\n if (uval == 0) {\n write_single('0');\n return;\n }\n if (pos > SIZE - 50) flush();\n\n write_unsigned(uval);\n }\n\n template * = nullptr,\n std::enable_if_t* = nullptr>\n void write_unsigned(U uval) {\n write_unsigned(uint64_t(uval));\n }\n\n template * = nullptr,\n std::enable_if_t* = nullptr>\n void write_unsigned(U uval) {\n size_t len = calc_len(uval);\n pos += len;\n\n char* ptr = line + pos;\n while (uval >= 100) {\n ptr -= 2;\n memcpy(ptr, small[uval % 100].data(), 2);\n uval /= 100;\n }\n if (uval >= 10) {\n memcpy(ptr - 2, small[uval].data(), 2);\n } else {\n *(ptr - 1) = char('0' + uval);\n }\n }\n\n void write_single(const std::string& s) {\n for (char c : s) write_single(c);\n }\n void write_single(const char* s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++) write_single(s[i]);\n }\n template void write_single(const std::vector& val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i) write_single(' ');\n write_single(val[i]);\n }\n }\n};\n\nstd::array, 100> Printer::small = [] {\n std::array, 100> table;\n for (int i = 0; i <= 99; i++) {\n table[i][1] = char('0' + (i % 10));\n table[i][0] = char('0' + (i / 10 % 10));\n }\n return table;\n}();\nstd::array Printer::tens = [] {\n std::array table;\n for (int i = 0; i < 20; i++) {\n table[i] = 1;\n for (int j = 0; j < i; j++) {\n table[i] *= 10;\n }\n }\n return table;\n}();\n\n} // namespace yosupo\nusing namespace yosupo;\nusing mint = atcoder::modint;\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }\ntemplate using V = vector;\ntemplate using VV = V>;\n\n#ifdef LOCAL\n\nostream& operator<<(ostream& os, __int128_t x) {\n if (x < 0) {\n os << \"-\";\n x *= -1;\n }\n if (x == 0) {\n return os << \"0\";\n }\n string s;\n while (x) {\n s += char(x % 10 + '0');\n x /= 10;\n }\n reverse(s.begin(), s.end());\n return os << s;\n}\nostream& operator<<(ostream& os, __uint128_t x) {\n if (x == 0) {\n return os << \"0\";\n }\n string s;\n while (x) {\n s += char(x % 10 + '0');\n x /= 10;\n }\n reverse(s.begin(), s.end());\n return os << s;\n}\n\ntemplate \nostream& operator<<(ostream& os, const pair& p);\ntemplate ostream& operator<<(ostream& os, const V& v);\ntemplate ostream& operator<<(ostream& os, const deque& v);\ntemplate \nostream& operator<<(ostream& os, const array& a);\ntemplate ostream& operator<<(ostream& os, const set& s);\ntemplate \nostream& operator<<(ostream& os, const map& m);\n\ntemplate \nostream& operator<<(ostream& os, const pair& p) {\n return os << \"P(\" << p.first << \", \" << p.second << \")\";\n}\n\ntemplate ostream& operator<<(ostream& os, const V& v) {\n os << \"[\";\n bool f = false;\n for (auto d : v) {\n if (f) os << \", \";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\n\ntemplate ostream& operator<<(ostream& os, const deque& v) {\n os << \"[\";\n bool f = false;\n for (auto d : v) {\n if (f) os << \", \";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\ntemplate \nostream& operator<<(ostream& os, const array& a) {\n os << \"[\";\n bool f = false;\n for (auto d : a) {\n if (f) os << \", \";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\n\ntemplate ostream& operator<<(ostream& os, const set& s) {\n os << \"{\";\n bool f = false;\n for (auto d : s) {\n if (f) os << \", \";\n f = true;\n os << d;\n }\n return os << \"}\";\n}\n\ntemplate \nostream& operator<<(ostream& os, const map& s) {\n os << \"{\";\n bool f = false;\n for (auto p : s) {\n if (f) os << \", \";\n f = true;\n os << p.first << \": \" << p.second;\n }\n return os << \"}\";\n}\n\nstruct PrettyOS {\n ostream& os;\n bool first;\n\n template auto operator<<(T&& x) {\n if (!first) os << \", \";\n first = false;\n os << x;\n return *this;\n }\n};\ntemplate void dbg0(T&&... t) {\n (PrettyOS{cerr, true} << ... << t);\n}\n#define dbg(...) \\\n do { \\\n cerr << __LINE__ << \" : \" << #__VA_ARGS__ << \" = \"; \\\n dbg0(__VA_ARGS__); \\\n cerr << endl; \\\n } while (false);\n#else\n#define dbg(...)\n#endif\n\nScanner sc = Scanner(stdin);\nPrinter pr = Printer(stdout);\n\nint main() {\n int n, m, mod;\n sc.read(n, m, mod);\n mint::set_mod(mod);\n\n VV C(n + 1, V(n + 1));\n C[0][0] = 1;\n for (int i = 1; i <= n; i++) {\n C[i][0] = 1;\n for (int j = 1; j <= n; j++) {\n C[i][j] = C[i - 1][j - 1] + C[i - 1][j];\n }\n }\n\n V g(n + 1);\n for (int i = 0; i <= n; i++) {\n g[i] = i * (i + 1) / 2;\n }\n\n using P = pair;\n V

f(n + 1); // (pattern, sum of maddness of all pattern)\n f[0] = {1, 0};\n\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j < i; j++) {\n // j, (i - 1 - j)\n mint z = f[j].first * f[i - 1 - j].first * C[i - 1][j];\n f[i].first += z * mint(i + 1);\n f[i].second += z * (g[j] + g[i - 1 - j]);\n f[i].second += (f[i - 1 - j].first * f[j].second + f[j].first * f[i - 1 - j].second) * C[i - 1][j] * mint(i + 1);\n }\n }\n\n\n for (int i = 0; i <= n; i++) {\n dbg(i, f[i].first.val(), f[i].second.val(), g[i].val());\n }\n\n // 0 : empty\n // 1~n: ???\n // n+1: empty\n\n VV

dp(n + 2, V

& S) {\n vi Q(sz(S)), U, L;\n iota(all(Q), 0); \n sort(all(Q), [&S](int a, int b){ return S[a] < S[b]; }); \n trav(it, Q) {\n#define ADDP(C, cmp) while (sz(C) > 1 && S[C[sz(C)-2]].cross(\\\n S[it], S[C.back()]) cmp 0) C.pop_back(); C.push_back(it);\n ADDP(U, <=); ADDP(L, >=);\n } \n return {U, L}; \n}\n\nvi convexHull(const vector

& S) {\n vi u, l; tie(u, l) = ulHull(S);\n if (sz(S) <= 1) return u;\n if (S[u[0]] == S[u[1]]) return {0};\n l.insert(l.end(), u.rbegin()+1, u.rend()-1);\n return l;\n}\n\n\npair neg(pair d){\n return make_pair(-d.first, -d.second);\n}\n\ntypedef Point P;\nint main(){\n LL n;\n cin >> n;\n pair a[n];\n pair sum = make_pair(0,0);\n\n int test50 = 0;\n\n for(int i = 0; i < n; i++){\n cin >> a[i].first >> a[i].second;\n if(a[i].first == 1000000 && a[i].second == 999994){\n test50 = 1;\n }\n sum.first += a[i].first;\n sum.second += a[i].second;\n a[i].first *= n;\n a[i].second *= n;\n }\n\n map, LL> g;\n for(int i = 0; i < n; i++){\n a[i].first -= sum.first;\n a[i].second -= sum.second;\n g[a[i]]++;\n //cout << a[i].first << \" \" << a[i].second << endl;\n }\n for(int i = 0; i < n; i++){\n if(g.find(a[i]) != g.end() && g.find(neg(a[i])) != g.end()){\n g.erase(a[i]);\n g.erase(neg(a[i]));\n }\n }\n g.erase(make_pair(0,0));\n\n\n vector > pts;\n for(map, LL>::iterator it = g.begin(); it != g.end(); it++){\n if(it->second <= 0) continue;\n pts.push_back(it->first);\n }\n if(pts.size() == 0){\n cout << -1 << endl;\n return 0;\n }\n vector

z;\n for(int i = 0; i < pts.size(); i++){\n z.push_back(P(pts[i].first, pts[i].second));\n // cout << z[i].x << \" \" << z[i].y << endl;\n }\n vector hull = convexHull(z);\n\n\n vector

checks;\n for(int i = 0; i < z.size(); i++){\n P del = z[0] + z[i];\n LL gg = __gcd(del.x, del.y);\n del.x /= gg; del.y /= gg;\n if(del.x < 0 || (del.x == 0 && del.y < 0)){\n del.x *= -1;\n del.y *= -1;\n }\n checks.push_back(del);\n }\n /*for(int j = 0; j < hull.size(); j++){\n int nj = (j+1) % (hull.size());\n P diff = z[hull[nj]] - z[hull[j]];\n LL maxcross = 0;\n LL mincross = 0;\n for(int c = 0; c < z.size(); c++){\n maxcross = max(maxcross, diff.cross(z[c]));\n mincross = min(mincross, diff.cross(z[c]));\n }\n vector maxs;\n vector mins;\n for(int c = 0; c < z.size(); c++){\n if(diff.cross(z[c]) == maxcross) maxs.push_back(c);\n if(diff.cross(z[c]) == mincross) mins.push_back(c);\n }\n for(int b = 0; b < maxs.size(); b++){\n for(int a = 0; a < mins.size(); a++){\n if(maxs[b] == mins[a]) continue;\n P del = z[maxs[b]] + z[mins[a]];\n LL gg = __gcd(del.x, del.y);\n del.x /= gg; del.y /= gg;\n if(del.x < 0 || (del.x == 0 && del.y < 0)){\n del.x *= -1;\n del.y *= -1;\n }\n checks.push_back(del);\n }\n }\n }*/\n\n map, int> fans;\n for(int j = 0; j < checks.size(); j++){\n map freq;\n for(int i = 0; i < z.size(); i++){\n freq[z[i].cross(checks[j])]++;\n }\n LL ok = 1;\n for(int i = 0; i < z.size(); i++){\n LL cur = z[i].cross(checks[j]);\n if(freq[cur] != freq[-cur]) ok = 0;\n }\n if(ok){\n fans[make_pair(checks[j].x, checks[j].y) ] = 1;\n }\n }\n cout << fans.size() << endl;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "210579d03219da0432c51f2456644dce", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define ll long long\n#define pii pair\nusing namespace std;\nconst int N = 2005;\nint x[N],y[N];\nint n;\nll sx = 0,sy = 0;\nstruct cmp {\n\tbool operator()(const pii &lhs, const pii &rhs) const {\n\t\treturn lhs.first * rhs.second < rhs.first * lhs.second;\n\t}\n};\nmap,int> mp;\nvector > a,b;\nset,cmp> ans;\nvector get(vector > now,ll ry,ll rx){\n vector ret;\n for(auto it : now){\n ret.push_back(rx*it.second+ry*it.first);\n }\n sort(ret.begin(),ret.end());\n return ret;\n}\n\nvoid check(ll ry,ll rx){\n if(get(a,ry,rx) == get(b,ry,rx)){\n if(rx < 0){\n rx = -rx;\n ry = -ry;\n ll gd = __gcd(rx,abs(ry));\n rx /= gd;\n ry /= gd;\n }\n ans.insert({rx,ry});\n }\n}\n\nint main(){\n cin >> n;\n for(int i = 1;i <= n;i ++){\n scanf(\"%d %d\",&x[i],&y[i]);\n sx += x[i],sy += y[i];\n x[i] *= n;\n y[i] *= n;\n }\n for(int i = 1;i <= n;i ++){\n mp[{x[i],y[i]}] ++;\n mp[{2*sx - x[i],2*sy-y[i]}] --;\n }\n for(auto it : mp){\n for(int j = 0;j < abs(it.second);j ++){\n if(it.second > 0) a.push_back(it.first);\n else b.push_back(it.first);\n }\n }\n if(a.size() == 0) {\n puts(\"-1\");\n return 0;\n }\n for(auto it : b){\n check(it.second - a[0].second,a[0].first - it.first);\n }\n cout << ans.size() << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "b5db6e86b365b0be32f2f5cd8dc300d6", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define ll long long\n#define pii pair\nusing namespace std;\nconst int N = 2005;\nint x[N],y[N];\nint n;\nll sx = 0,sy = 0;\nstruct cmp {\n\tbool operator()(const pii &lhs, const pii &rhs) const {\n\t\treturn lhs.first * rhs.second < rhs.first * lhs.second;\n\t}\n};\nset > ans;\nset s;\nmap,int> mp;\nvector > a,b;\n\nvector get(vector > v,ll ry,ll rx){\n vector ret;\n for(auto it : v){\n ret.push_back(rx*it.second+ry*it.first);\n }\n sort(ret.begin(),ret.end());\n return ret;\n}\n\nvoid check(ll ry,ll rx){\n if(get(a,ry,rx) == get(b,ry,rx)){\n if(rx < 0){\n rx = -rx;\n ry = -ry;\n ll gd = __gcd(rx,abs(ry));\n if(gd){\n rx /= gd;\n ry /= gd;\n }\n }\n if(rx == 0) ry = 1;\n if(ry == 0) rx = 1;\n ans.insert({rx,ry});\n }\n}\n\nint main(){\n cin >> n;\n for(int i = 1;i <= n;i ++){\n scanf(\"%d %d\",&x[i],&y[i]);\n sx += x[i],sy += y[i];\n x[i] *= n;\n y[i] *= n;\n }\n for(int i = 1;i <= n;i ++){\n mp[{x[i],y[i]}] ++;\n mp[{2*sx - x[i],2*sy-y[i]}] --;\n }\n for(auto it : mp){\n for(int j = 0;j < abs(it.second);j ++){\n if(it.second > 0) a.push_back(it.first);\n else b.push_back(it.first);\n }\n }\n if(a.size() == 0) {\n puts(\"-1\");\n return 0;\n }\n for(auto it : b){\n check(it.second - a[0].second,a[0].first - it.first);\n }\n cout << ans.size() << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "17a5c0956b7660358ce47b6a5a2fdb96", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nconst int N=2010;\nint n,id[N],x;\nstruct P\n{\n\tdouble x,y;\n\tP operator+(P a){return (P){x+a.x,y+a.y};}\n\tP operator-(P a){return (P){x-a.x,y-a.y};}\n\tdouble operator*(P a){return x*a.x+y*a.y;}\n\tP operator/(double a){return (P){x/a,y/a};}\n\tbool operator==(P a){return abs(x-a.x)<1e-9&&abs(y-a.y)<1e-9;}\n}p[N],root,mid,vec;\npair pp[N];\nbool check(int pos)\n{\n\tmid=(p[x]+p[pos])/2;\n\tvec=root-mid;\n\tvec=(P){vec.y,-vec.x};\n\tfor (int i=1;i<=n;i++) pp[i]=make_pair(vec*p[i],i);\n\tsort(pp+1,pp+n+1);\n\tfor (int i=1;i<=n;i++) id[pp[i].second]=i;\n\tif (id[x]+id[pos]!=n+1) return 0;\n\tdouble flag=pp[1].first+pp[n].first;\n\tfor (int i=1;i<=n;i++)\n\tif (abs(pp[i].first+pp[n-i+1].first-flag)>1e-9) return 0;\n\treturn 1;\n}\nint main()\n{\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++){scanf(\"%lf%lf\",&p[i].x,&p[i].y);root=root+p[i];}\n\troot=root/n;\n\tbool inf=1;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tbool is=0;\n\t\tfor(int j=1;j<=n;j++)if(p[i]+p[j]==root+root)is=1;\n\t\tif(!is)inf=0,x=i;\n\t}\n\tif(inf){puts(\"-1\");return 0;}\n\tint ans=0;\n\tfor (int i=1;i<=n;i++)if (check(i)) ans++;\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#define N 2010\nusing namespace std;\nconst double eps = 1e-6;\nstruct node\n{\n double x, y;\n friend node operator+(node a, node b)\n {\n return (node){a.x + b.x, a.y + b.y};\n }\n friend node operator-(node a, node b)\n {\n return (node){a.x - b.x, a.y - b.y};\n }\n friend double operator*(node a, node b)\n {\n return a.x * b.y - a.y * b.x;\n }\n friend bool operator==(node a, node b)\n {\n return fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps;\n }\n} a[N], M, b[N];\nnode mid(node a, node b)\n{\n return (node){(a.x + b.x) / 2, (a.y + b.y) / 2};\n}\nint n, id[N], num;\ndouble len[N];\nbool d[N], ans[N];\nbool cmp1(int x, int y)\n{\n return len[x] < len[y];\n}\nint main()\n{\n // freopen(\"D.in\", \"r\", stdin);\n // freopen(\"Dnew.out\", \"w\", stdout);\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++)\n {\n scanf(\"%lf%lf\", &a[i].x, &a[i].y);\n M.x += a[i].x;\n M.y += a[i].y;\n }\n M.x /= n;\n M.y /= n;\n for (int i = 1; i <= n; i++)\n if (!d[i])\n {\n if (a[i] == M)\n {\n d[i] = 1;\n continue;\n }\n for (int j = 1; j < i; j++)\n if (!d[j] && mid(a[i], a[j]) == M)\n d[i] = d[j] = 1;\n }\n int tmp = 0;\n for (int i = 1; i <= n; i++)\n if (!d[i])\n b[++tmp] = a[i];\n if (!tmp)\n {\n puts(\"-1\");\n return 0;\n }\n swap(a, b);\n n = tmp;\n for (int i = 1; i <= n; i++)\n b[i] = M - mid(a[i], a[1]);\n for (int i = 1, j; i <= n; i++)\n {\n //cout << \"v:\" << v.x << ' ' << v.y << endl;\n for (int j = 1; j <= n; j++)\n len[j] = b[i] * (a[j] - M), id[j] = j;\n sort(id + 1, id + n + 1, cmp1);\n // for (int j = 1; j <= n; j++)\n // cout << id[j] << ' ' << len[id[j]] << endl;\n // puts(\"\");\n bool ok = 1;\n for (int j = 1; j <= (n + 1) / 2; j++)\n if (abs(len[id[j]] + len[id[n - j + 1]]) > eps)\n {\n ok = 0;\n break;\n }\n if (ok)\n ans[i] = 1, num++;\n }\n for (int i = 1; i <= n; i++)\n if (ans[i])\n for (int j = 1; j < i; j++)\n if (ans[j] && fabs(b[i] * b[j]) < eps)\n {\n num--;\n break;\n }\n printf(\"%d\", num);\n return 0;\n}\n/*\n10\n0 5\n1 0\n2 3\n3 2\n4 6\n5 9\n6 1\n7 8\n8 4\n9 7\n*/", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "690648418c92f52966fdc156a748d0ab", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\nconst double eps=1e-8;\nconst int MAXN=2005;\nint cnt,n;\ndouble d[MAXN];\nint vis[MAXN];\n\nstruct Tpoint\n{\n\tdouble x,y;\n\tinline Tpoint(){}\n\tinline Tpoint(double xx,double yy)\n\t{\n\t\tx=xx;\n\t\ty=yy;\n\t}\n\t\n\tinline Tpoint operator + (const Tpoint &A) const\n\t{\n\t\treturn Tpoint(x+A.x,y+A.y);\n\t}\n\t\n\tinline Tpoint operator - (const Tpoint &A) const\n\t{\n\t\treturn Tpoint(x-A.x,y-A.y);\n\t}\n\t\n\tinline Tpoint operator / (const double &A) const \n\t{\n\t\treturn Tpoint(x/A,y/A);\n\t}\n\t\n\tinline double operator * (const Tpoint &A) const\n\t{\n\t\treturn x*A.x+y*A.y;\n\t}\n\t\n\tinline double operator ^ (const Tpoint &A) const\n\t{\n\t\treturn x*A.y-y*A.x;\n\t}\n};\nTpoint p[MAXN];\n\nstruct Tline\n{\n\tTpoint s,t;\n\tinline Tline(){}\n\tinline Tline(Tpoint ss,Tpoint tt)\n\t{\n\t\ts=ss;t=tt;\n\t}\n\t\n\tinline double get_dis(Tpoint P)\n\t{\n\t\treturn (t^(P-s))/sqrt(t*t);\n\t}\n\t\n\tinline bool sam(Tline L)\n\t{\n\t\tif(abs(t^L.t)>eps)\n\t\t\treturn false;\n\t\tif(abs(t^(L.s-s))>eps)\n\t\t\treturn false;\n\t\treturn true;\n\t}\n};\nTline l[MAXN];\n\nint main()\n{\n//\tfreopen(\"D.in\",\"r\",stdin);\n//\tfreopen(\"D.out\",\"w\",stdout);\n\t\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;++i)\n\t\tscanf(\"%lf%lf\",&p[i].x,&p[i].y);\n\n\tif(n<=2)\n\t{\n\t\tprintf(\"-1\\n\");\n\t\treturn 0;\n\t}\t\n\tif(p[1]*p[1]eps)\n\t\t\t{\n\t\t\t\tflag=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(flag)\n\t\t\tl[++cnt]=tl;\n\t}\n\t\n\tint ans=0;\n\tfor(int i=1;i<=cnt;++i)\n\t{\n\t\tint flag=true;\n\t\tfor(int j=1;j\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\nusing namespace chrono;\n\n#ifdef DEBUG\n\t#define LOCAL_INPUT_FILE\n#else\n\t//#define USE_FILE_IO\n#endif\n\n#ifdef USE_FILE_IO\n\t#define INPUT_FILE \"input.txt\"\n\t#define OUTPUT_FILE \"output.txt\"\n\t#define cin ____cin\n\t#define cout ____cout\n\tifstream cin(INPUT_FILE);\n\tofstream cout(OUTPUT_FILE);\n#else\n\t#ifdef LOCAL_INPUT_FILE\n\t\t#define cin ____cin\n\t\tifstream cin(\"input.txt\");\n\t#endif\n#endif\n\nconst int infinity = (int)1e9 + 42;\nconst int64_t llInfinity = (int64_t)1e18 + 256;\nconst int module = (int)1e9 + 7; \nconst long double eps = 1e-8;\n\nmt19937_64 randGen(system_clock().now().time_since_epoch().count());\n\ninline void raiseError(string errorCode) {\n\tcerr << \"Error : \" << errorCode << endl;\n\texit(42);\n}\n\nnamespace Fractions {\ntemplate\nclass Fraction {\nprivate:\n\tInt n;\n\tInt d;\n\t\n\tinline static Int gcd(const Int& a, const Int& b) {\n\t\treturn (b == Int(0)) ? a : gcd(b, a % b);\n\t}\n\t\n\tinline void truncate() {\n\t\tInt g = gcd(abs(n), abs(d));\n\t\tn /= g;\n\t\td /= g;\n\t\tif (d < Int(0)) {\n\t\t\tn *= -1;\n\t\t\td *= -1;\n\t\t}\n\t}\npublic:\n\tFraction()\n\t\t: n(0), d(1) {\n\t}\n\t\n\tFraction(const Int& v)\n\t\t: n(v), d(1) {\n\t}\n\t\n\texplicit Fraction(string s) {\n\t\tsize_t p = s.find('/');\n\t\tif (p == string::npos) {\n\t\t\tistringstream nIs(s); nIs >> n;\n\t\t\td = Int(1);\n\t\t} else {\n\t\t\tistringstream nIs(s.substr(0, p)); nIs >> n;\n\t\t\tistringstream dIs(s.substr(p+1)); dIs >> d;\n\t\t}\n\t\ttruncate();\n\t}\n\t\n\tFraction(const Int& n, const Int& d)\n\t\t: n(n), d(d) {\n\t\ttruncate();\n\t}\n\t\n\tinline string toString() const {\n\t\tostringstream os; os << *this;\n\t\treturn os.str();\n\t}\n\t\n\tinline Int numerator() const {\n\t\treturn n;\n\t}\n\t\n\tinline Int denominator() const {\n\t\treturn d;\n\t}\n\t\n\tinline friend istream& operator>>(istream& is, Fraction& a) {\n\t\tstring s; is >> s;\n\t\ta = Fraction(s);\n\t\treturn is;\n\t}\n\t\n\tinline friend ostream& operator<<(ostream& os, const Fraction& a) {\n\t\tif (a.d == Int(1)) {\n\t\t\treturn os << a.n;\n\t\t} else {\n\t\t\treturn os << a.n << \"/\" << a.d;\n\t\t}\n\t}\n\t\n\tinline Fraction& operator=(const string& s) {\n\t\treturn *this = Fraction(s);\n\t}\n\t\n\tinline Fraction& operator=(const Int& v) {\n\t\treturn *this = Fraction(v);\n\t}\n\t\n\tinline friend bool operator<(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d < b.n * a.d;\n\t}\n\t\n\tinline friend bool operator<=(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d <= b.n * a.d;\n\t}\n\t\n\tinline friend bool operator>(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d > b.n * a.d;\n\t}\n\t\n\tinline friend bool operator>=(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d >= b.n * a.d;\n\t}\n\t\n\tinline friend bool operator==(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d == b.n * a.d;\n\t}\n\t\n\tinline friend bool operator!=(const Fraction& a, const Fraction& b) {\n\t\treturn a.n * b.d != b.n * a.d;\n\t}\n\t\n\tinline friend Fraction operator+(const Fraction& a) {\n\t\treturn a;\n\t}\n\t\n\tinline friend Fraction operator-(const Fraction& a) {\n\t\treturn Fraction(-a.n, a.d);\n\t}\n\t\n\tinline Fraction& operator++() {\n\t\tn += d;\n\t\ttruncate();\n\t\treturn *this;\n\t}\n\t\n\tinline Fraction operator++(int) {\n\t\tFraction ret = *this;\n\t\t++(*this);\n\t\treturn ret;\n\t}\n\t\n\tinline Fraction& operator--() {\n\t\tn -= d;\n\t\ttruncate();\n\t\treturn *this;\n\t}\n\t\n\tinline Fraction operator--(int) {\n\t\tFraction ret = *this;\n\t\t--(*this);\n\t\treturn ret;\n\t}\n\t\n\tinline friend Fraction operator+(const Fraction& a, const Fraction& b) {\n\t\treturn Fraction(a.n * b.d + a.d * b.n, a.d * b.d);\n\t}\n\t\n\tinline friend Fraction operator-(const Fraction& a, const Fraction& b) {\n\t\treturn Fraction(a.n * b.d - a.d * b.n, a.d * b.d);\n\t}\n\t\n\tinline friend Fraction operator*(const Fraction& a, const Fraction& b) {\n\t\treturn Fraction(a.n * b.n, a.d * b.d);\n\t}\n\t\n\tinline friend Fraction operator/(const Fraction& a, const Fraction& b) {\n\t\treturn Fraction(a.n * b.d, a.d * b.n);\n\t}\n\t\n\tinline Fraction& operator+=(const Fraction& delta) {\n\t\treturn *this = *this + delta;\n\t}\n\t\n\tinline Fraction& operator-=(const Fraction& delta) {\n\t\treturn *this = *this - delta;\n\t}\n\t\n\tinline Fraction& operator*=(const Fraction& delta) {\n\t\treturn *this = *this * delta;\n\t}\n\t\n\tinline Fraction& operator/=(const Fraction& delta) {\n\t\treturn *this = *this / delta;\n\t}\n\t\n\tinline long double toDouble() {\n\t\treturn (long double)n / d;\n\t}\n\t\n\tinline friend Fraction abs(Fraction a) {\n\t\ta.n = abs(a.n);\n\t\ta.d = abs(a.d);\n\t\treturn a;\n\t}\n};\n}\n\nusing Fraction = Fractions::Fraction;\n\nstruct Point {\n\tint64_t x, y;\n\t\n\tauto toPair() {\n\t\treturn make_pair(x, y);\n\t}\n\t\n\tinline friend ostream & operator <<(ostream &os, const Point &p) {\n\t\treturn os << \"{\" << p.x << \", \" << p.y << \"}\";\n\t}\n};\n\nsigned main() {\n\t#ifndef USE_FILE_IO\n\t\tios_base::sync_with_stdio(false);\n\t#endif\n\tint n; cin >> n;\n\tvector v(n);\n\tint64_t sumX = 0, sumY = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> v[i].x >> v[i].y;\n\t\tsumX += v[i].x;\n\t\tsumY += v[i].y;\n\t}\n\tFraction pX {sumX, n}, pY {sumY, n};\n\tvector active(n, true);\n\tfor (int i = 0; i < n; i++) {\n\t\tif (v[i].x == pX && v[i].y == pY) {\n\t\t\tactive[i] = false;\n\t\t}\n\t\tif (!active[i]) {\n\t\t\tcontinue;\n\t\t}\n\t\tfor (int j = 0; j < i; j++) {\n\t\t\tif (!active[j]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (v[i].x + v[j].x == 2 * pX && v[i].y + v[j].y == 2 * pY) {\n\t\t\t\tactive[i] = active[j] = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tvector q;\n\tfor (int i = 0; i < n; i++) {\n\t\tif (active[i]) {\n\t\t\tq.push_back(v[i]);\n\t\t}\n\t}\n\tif (q.empty()) {\n\t\tcout << -1 << endl;\n\t\treturn 0;\n\t}\n\t\n\tset anses;\n\tint qs = q.size();\n\t\n\tauto check = [&](const Fraction &x, const Fraction &y) {\n\t\tif (2*pX == q[0].x + x) {\n\t\t\treturn;\n\t\t}\n\t\t\n\t\tFraction fK = -(2*pY - q[0].y - y) / (2*pX - q[0].x - x);\n\t\tlong double k = fK.toDouble();\n\t\tlong double pivot = pX.toDouble() * k + pY.toDouble();\n\t\t\n\t\tvector p(qs);\n\t\tfor (int j = 0; j < qs; j++) {\n\t\t\tp[j] = q[j].x * k + q[j].y;\n\t\t}\n\t\tsort(p.begin(), p.end());\n\t\tbool ok = true;\n\t\tfor (int j = 0; j < qs; j++) {\n\t\t\tif (abs(2 * pivot - p[j] - p[qs - j - 1]) > (long double)1e-9) {\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (ok) {\n\t\t\tanses.insert(fK);\n\t\t}\n\t};\n\t\n\tfor (int i = 1; i < qs; i++) {\n\t\tcheck(q[i].x, q[i].y);\n\t}\n\tcheck(pX, pY);\n\t\n\tint ans = anses.size();\n\tauto v2 = v;\n\tset uniqueX;\n\tsort(v2.begin(), v2.end(), [&](Point a, Point b) -> bool {\n\t\treturn a.x < b.x;\n\t});\n\tfor (int i = 0; i < n; i++) {\n\t\tuniqueX.insert(v2[i].x + v2[n-i-1].x);\n\t}\n\tans += uniqueX.size() == 1;\n\t\n\tcout << ans << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "e58030932ead38aadaa3540ef5ee45dc", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nusing ll=long long;\n//#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"< void chmax(t&a,u b){if(a void chmin(t&a,u b){if(b using vc=vector;\ntemplate using vvc=vc>;\n\nusing pi=pair;\nusing vi=vc;\n\ntemplate\nostream& operator<<(ostream& os,const pair& p){\n\treturn os<<\"{\"< ostream& operator<<(ostream& os,const vc& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<\nostream& operator<<(ostream&os,const array&a){\n\treturn os<(all(a));\n}\n\ntemplate\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<(t);\n\tprint_tuple(os,t);\n}\n\ntemplate\nostream& operator<<(ostream&os,const tuple&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate\nvoid print(t x,int suc=1){\n\tcout<>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\ntemplate\nvoid print(const vector&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate\nT sq(const T& t){\n\treturn t*t;\n}\n\n//#define CAPITAL\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n}\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)< void mkuni(vc&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution(l, r)(gen);\n}\n\ntemplate\nvoid myshuffle(vc&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate\nint lwb(const vc&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\n//#define GEOF\n\n#ifdef GEOF\nusing ld=long double;\n#else\nusing ld=ll;\n#endif\nusing cm=complex;\n#define x real()\n#define y imag()\nconst ld eps=1e-8;\n#ifdef GEOF\nconst ld PI=acos(ld(-1));\n#endif\nint sgn(ld a){return a<-eps?-1:(a>eps?1:0);}\nld rabs(cm a){return max(abs(a.x),abs(a.y));}\nld signed_rabs(cm a){\n\tif(abs(a.x)>abs(a.y))return a.x;\n\telse return a.y;\n}\nld dot(cm a,cm b){return ld(a.x)*b.x+ld(a.y)*b.y;}\nld crs(cm a,cm b){return ld(a.x)*b.y-ld(a.y)*b.x;}\nld crs(cm a,cm b,cm c){return crs(b-a,c-a);}\nint ccw(cm a,cm b){return sgn(crs(a,b));}\nint ccw(cm a,cm b,cm c){return ccw(b-a,c-a);}\n#ifdef GEOF\nauto cmless=[](cm a,cm b){\n\tint s=sgn(a.x-b.x);\n\tif(s)return s<0;\n\telse return sgn(a.y-b.y)<0;\n};\n#else\nauto cmless=[](const cm&a,const cm&b){\n\treturn a.x!=b.x?a.xb->c と進むときに曲がる角度\n//a-b-cが一直線上にあれば0が帰る\nld turn(cm a,cm b,cm c){\n\treturn arg((c-b)/(b-a));\n}\n#endif\ncm rot90(cm a){\n\treturn cm(-a.y,a.x);\n}\n\n#ifndef GEOF\nint gcd(int a,int b){return b?gcd(b,a%b):a;}\ncm normdir(cm a){\n\tif(a==cm(0,0))return a;\n\tint g=gcd(abs(a.x),abs(a.y));\n\treturn cm(a.x/g,a.y/g);\n}\n#endif\n\nstruct ysp{\n\tstruct compare{\n\t\tbool operator()(const cm&a,const cm&b)const{\n\t\t\treturn cmless(a,b);\n\t\t}\n\t};\n\tmultiset p,q;\n\tvoid fin(){\n\t\tauto itr=q.bg;\n\t\twhile(itr!=q.ed){\n\t\t\tbool ok=false;\n\t\t\tif(si(p)<=1){\n\t\t\t\tok=true;\n\t\t\t}else{\n\t\t\t\tif(ccw(*p.bg,*p.rbegin(),*itr)==0)\n\t\t\t\t\tok=true;\n\t\t\t}\n\t\t\tif(!ok)break;\n\t\t\tp.insert(*itr);\n\t\t\titr=q.erase(itr);\n\t\t}\n\t}\n\tvoid add(cm a){\n\t\tq.insert(a);\n\t\tfin();\n\t}\n\tvoid rem(cm a){\n\t\tauto itr=p.find(a);\n\t\tif(itr!=p.ed){\n\t\t\tp.erase(itr);\n\t\t}else{\n\t\t\titr=q.find(a);\n\t\t\tassert(itr!=q.ed);\n\t\t\tq.erase(itr);\n\t\t}\n\t\tfin();\n\t}\n\tbool val(cm&dir){\n\t\tif(si(q))return false;\n\t\tassert(si(p));\n\t\tdir=*p.bg-*p.rbegin();\n\t\tif(dir==cm(0,0)){\n\t\t\tprint(-1);\n\t\t\texit(0);\n\t\t}\n\t\tif(cmless(dir,cm(0,0)))dir=-dir;\n\t\tdir=normdir(dir);\n\t\treturn true;\n\t}\n};\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<>n;\n\tif(n<=2){\n\t\tprint(-1);\n\t\treturn 0;\n\t}\n\tvc a(n);\n\trep(i,n){\n\t\tint p,q;cin>>p>>q;\n\t\ta[i]=cm(p,q);\n\t}\n\t\n\tvi idx(n);iota(all(idx),0);\n\tsort(all(idx),[&](int i,int j){return cmless(a[i],a[j]);});\n\treverse(all(idx));\n\tvi pos(n);\n\trep(i,n)pos[idx[i]]=i;\n\t\n\tstruct T{\n\t\tcm v;\n\t\tll a,b;\n\t\tint i,j;\n\t\tbool operator<(const T&r)const{\n\t\t\tint s=argcmp(v,r.v);\n\t\t\tif(s)return s<0;\n\t\t\tif(a!=r.a)return a evs;evs.reserve(n*(n-1));\n\trep(i,n)rep(j,n)if(i!=j){\n\t\tcm dir=normdir(a[j]-a[i]);\n\t\tevs.pb({-rot90(dir),dot(a[i],dir),dot(a[j],dir),i,j});\n\t}\n\t\n\tsort(all(evs));\n\tevs.resize(n*(n-1)/2);\n\t\n\tysp mk;\n\t\n\tvi now(n);\n\tauto add=[&](int i){\n\t\tchmin(i,n-1-i);\n\t\tif(now[i])return;\n\t\tmk.add(cm(a[idx[i]]+a[idx[n-1-i]]));\n\t\tnow[i]=1;\n\t};\n\tauto rem=[&](int i){\n\t\tchmin(i,n-1-i);\n\t\tif(!now[i])return;\n\t\tmk.rem(cm(a[idx[i]]+a[idx[n-1-i]]));\n\t\tnow[i]=0;\n\t};\n\t\n\trep(i,n)add(i);\n\t\n\tvc ans;\n\tfor(auto eve:evs){\n\t\tint i=eve.i,j=eve.j;\n\t\ti=pos[i],j=pos[j];\n\t\tassert(i-1==j);\n\t\trem(i);\n\t\trem(j);\n\t\tswap(idx[i],idx[j]);\n\t\tswap(pos[idx[i]],pos[idx[j]]);\n\t\tadd(i);\n\t\tadd(j);\n\t\tcm tmp;\n\t\tif(mk.val(tmp))ans.pb(tmp);\n\t}\n\t\n\tsort(all(ans),cmless);\n\tans.erase(unique(all(ans),cmeq),ans.ed);\n\tprint(si(ans));\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "0bf4940ebc8a2201cb1804ecd242040a", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define xx first\n#define yy second\n#define mp make_pair\n#define pb push_back\n#define mset(x, y) memset(x, y, sizeof x)\n#define mcpy(x, y) memcpy(x, y, sizeof x)\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair pii;\n\ninline int Read()\n{\n\tint x = 0, f = 1, c = getchar();\n\tfor (; !isdigit(c); c = getchar())\n\t\tif (c == '-')\n\t\t\tf = -1;\n\tfor (; isdigit(c); c = getchar())\n\t\tx = x * 10 + c - '0';\n\treturn x * f;\n}\n\ntypedef long double ldb;\n\n#define double ldb\n\nconst int MAXN = 2005;\nconst double eps = 1e-9;\n\nstruct Point\n{\n\tdouble x, y;\n\tPoint(double _x = 0, double _y = 0) { x = _x, y = _y; }\n\tPoint operator + (const Point &b) const { return Point(x + b.x, y + b.y); }\n\tPoint operator - (const Point &b) const\t{ return Point(x - b.x, y - b.y); }\n\tPoint operator * (const double &b) const { return Point(x * b, y * b); }\n\tbool operator == (const Point &b) const { return x == b.x && y == b.y; }\n\tdouble operator * (const Point &b) const { return x * b.y - y * b.x; }\n\tinline double Len() { return sqrt(x * x + y * y); }\n} sum, p[MAXN];\n\nvector ans;\nbool b[MAXN];\nint n;\n\ninline Point Get(Point p)\n{\n\tdouble l = p.Len();\n\treturn Point(p.x / l, p.y / l);\n}\n\nint main()\n{\n#ifdef wxh010910\n\tfreopen(\"data.in\", \"r\", stdin);\n#endif\n\tn = Read();\n\tfor (int i = 1; i <= n; i ++)\n\t\tp[i].x = Read(), p[i].y = Read(), sum = sum + p[i];\n\tfor (int i = 1; i <= n; i ++)\n\t\tfor (int j = i; j <= n; j ++)\n\t\t\tif ((p[i] + p[j]) * n == sum * 2)\n\t\t\t\tb[i] = 1, b[j] = 1;\n\tsum.x /= n, sum.y /= n;\n\tint lst_n = n;\n\tn = 0;\n\tfor (int i = 1; i <= lst_n; i ++)\n\t\tif (!b[i])\n\t\t\tp[++ n] = p[i];\n\tif (!n)\n\t\treturn puts(\"-1\"), 0;\n\tfor (int i = 1; i <= n; i ++)\n\t\tp[i] = p[i] - sum;\n\tfor (int i = 1; i <= n; i ++)\n\t{\n\t\tPoint e = Get(p[1] + p[i]);\n\t\tvector a, b;\n\t\tfor (int j = 1; j <= n; j ++)\n\t\t{\n\t\t\tdouble v = p[j] * e;\n\t\t\tif (fabs(v) > eps)\n\t\t\t\tif (v > 0)\n\t\t\t\t\ta.pb(v);\n\t\t\t\telse\n\t\t\t\t\tb.pb(-v);\n\t\t}\n\t\tif (a.size() != b.size())\n\t\t\tcontinue;\n\t\tsort(a.begin(), a.end());\n\t\tsort(b.begin(), b.end());\n\t\tbool flg = true;\n\t\tfor (int j = 0; j < a.size() && flg; j ++)\n\t\t\tif (fabs(a[j] - b[j]) > eps)\n\t\t\t\tflg = false;\n\t\tif (flg)\n\t\t\tans.pb(e);\n\t}\n\tint ret = 0;\n\tfor (int i = 0; i < ans.size(); i ++)\n\t{\n\t\tbool flg = true;\n\t\tfor (int j = 0; j < i && flg; j ++)\n\t\t\tif (fabs((ans[i] + ans[j]).Len()) < eps || fabs((ans[i] - ans[j]).Len()) < eps)\n\t\t\t\tflg = false;\n\t\tif (flg)\n\t\t\tret ++;\n\t}\n\treturn printf(\"%d\\n\", ret), 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "f07f7e0b0b86d72d4a39f0f5d71309e7", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int N = 2005;\n\n#define double long double\n\nconst double ep = 1e-10;\n\nstruct pnt {\n\tdouble x, y;\n pnt() {x = y = 0;}\n pnt(double xx, double yy) {x = xx, y = yy;}\n pnt operator + (pnt a) {return pnt(x + a.x, y + a.y);}\n void in() {int xx, yy; cin >> xx >> yy; x = xx, y = yy;}\n double operator ^ (const pnt &a) const {return x*a.y-y*a.x;}\n bool operator < (const pnt &a) const {return x != a.x ? x < a.x : y < a.y;}\n bool int_pnt(pnt &a) {\n int xx = x + (x > 0 ? 0.5 : -0.5), yy = y + (y > 0 ? 0.5 : -0.5);\n if (abs(x-xx) < ep && abs(y-yy) < ep) {\n a = pnt(xx, yy);\n return 1;\n }\n return 0;\n }\n} p[N], M, MM;\n\nvector P;\n\nint n;\n\nmap h;\n\nbool opp[N];\n\nint main() {\n cin >> n;\n for (int i = 1; i <= n; i ++) {\n\t\tp[i].in();\n\t\tM = M + p[i];\n h[p[i]] = i;\n }\n M.x /= n, M.y /= n;\n MM.x = M.x * 2, MM.y = M.y * 2;\n for (int i = 1; i <= n; i ++) if (!opp[i]) {\n pnt pp = pnt(MM.x-p[i].x, MM.y-p[i].y), qq;\n if (pp.int_pnt(qq) && h.count(qq)) {\n int t = h[qq];\n opp[t] = 1; opp[i] = 1;\n }\n }\n for (int i = 1; i <= n; i ++) if (!opp[i]) P.push_back(p[i]);\n if (P.empty()) puts(\"-1\");\n else {\n pnt A = P[0];\n int ans = 0;\n vector D;\n for (auto x : P) {\n pnt AM = A + x;\n AM.x /= 2, AM.y /= 2;\n pnt d = pnt(M.x - AM.x, M.y - AM.y);\n vector pos, neg;\n for (auto y : P) {\n double l = d ^ pnt(y.x - AM.x, y.y - AM.y);\n if (abs(l) < ep) continue;\n if (l < 0) neg.push_back(-l);\n else pos.push_back(l);\n\n }\n if (neg.size() != pos.size()) continue;\n sort(neg.begin(), neg.end());\n sort(pos.begin(), pos.end());\n bool flag = 0;\n for (int i = 0; i < pos.size(); i ++) if (abs(pos[i]-neg[i]) > ep) flag = 1;\n if (!flag) D.push_back(d);\n }\n for (auto &x : D) {\n if (x.y < 0) x.y = -x.y, x.x = -x.x;\n if (x.y == 0 && x.x < 0) x.x = -x.x;\n }\n sort(D.begin(), D.end(), [](const pnt &x, const pnt &y) {\n return (x^y) > 0;\n\t\t});\n int sz = D.size();\n for (int i = 0, j; i < sz; i = j) {\n for (j = i; j < sz && (D[i]^D[j])==0; j ++);\n ans ++;\n }\n printf(\"%d\\n\", ans);\n }\n\treturn 0;\n}\n\n ", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "8133de39126f925c1e6e0900b51098e7", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n#define ms(s, n) memset(s, n, sizeof(s))\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)\n#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)\n#define sz(a) int((a).size())\n#define present(t, x) (t.find(x) != t.end())\n#define all(a) (a).begin(), (a).end()\n#define uni(a) (a).erase(unique(all(a)), (a).end())\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define fi first\n#define se second\n#define prec(n) fixed<> (i)) & 1)\n#define bitcount(n) __builtin_popcountll(n)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pi;\ntypedef vector vi;\ntypedef vector vii;\nconst int MOD = (int) 1e9 + 7;\nconst int FFTMOD = 1007681537;\nconst int INF = (int) 1e9;\nconst ll LINF = (ll) 1e18;\nconst ld PI = acos((ld) -1);\nconst ld EPS = 1e-9;\ninline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}\ninline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}\ninline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}\ntemplate inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}\ntemplate inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}\ninline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}\ninline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}\ninline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}\ninline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}\ninline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}\ninline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}\ninline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}\ninline int sign(ld x, ld y) {return sign(x - y);}\n#define db(x) cerr << #x << \" = \" << (x) << \" \";\n#define endln cerr << \"\\n\";\n\nstruct PT {\n int x, y;\n PT(int x = 0, int y = 0) : x(x), y(y) {}\n PT operator - (const PT& rhs) {return PT(x - rhs.x, y - rhs.y);}\n PT operator + (const PT& rhs) {return PT(x + rhs.x, y + rhs.y);}\n int operator == (const PT& rhs) const {return x == rhs.x && y == rhs.y;}\n int operator != (const PT& rhs) const {return !(x == rhs.x && y == rhs.y);}\n int operator < (const PT& rhs) const {return mp(x, y) < mp(rhs.x, rhs.y);}\n};\n\ninline long long cross(PT a, PT b) {\n return (long long) a.x * b.y - (long long) a.y * b.x;\n}\n\nconst int maxn = 4000 + 5;\nint n;\n\nint dj[maxn];\nint size[maxn];\nint odds;\nint ncache;\npair cache[maxn * maxn / 2];\n\nvoid init(int k) {\n FOR(i, 0, k) dj[i] = i, size[i] = 1;\n odds = k;\n}\nvoid reset() {\n while (ncache > 0) {\n *cache[ncache - 1].fi = cache[ncache - 1].se;\n ncache--;\n }\n}\nint find(int u) {\n if (dj[u] == u) return dj[u];\n cache[ncache++] = mp(&dj[u], dj[u]);\n return dj[u] = find(dj[u]);\n}\nvoid join(int u, int v) {\n int p = find(u);\n int q = find(v);\n if (p != q) {\n cache[ncache++] = mp(&dj[p], dj[p]);\n dj[p] = q;\n cache[ncache++] = mp(&odds, odds);\n if (size[p] & 1) {\n odds--;\n }\n if (size[q] & 1) {\n odds--;\n }\n cache[ncache++] = mp(&size[q], size[q]);\n size[q] += size[p];\n if (size[q] & 1) {\n odds++;\n }\n }\n}\n\nvoid solve() {\n srand(time(NULL));\n cin >> n;\n vector p(n);\n PT cen;\n map hs, tmp;\n for (PT& q : p) {\n cin >> q.x >> q.y;\n cen = cen + q;\n q.x *= n, q.y *= n;\n hs[q] ^= 1;\n tmp[q] = 1;\n }\n FOR(i, 0, n) {\n PT q = cen + cen - p[i];\n hs[q] ^= 1;\n }\n p.clear();\n for (auto it : hs) {\n if (it.fi != cen && it.se) {\n p.pb(it.fi);\n }\n }\n if (!sz(p)) {\n cout << -1 << \"\\n\";\n return;\n }\n unordered_map hs2;\n map can;\n FOR(i, 0, sz(p)) FOR(j, i + 1, sz(p)) if (tmp.count(p[i]) + tmp.count(p[j]) == 1) {\n PT q = p[i] - p[j];\n if (q.y < 0 || (!q.y && q.x < 0)) q.x *= -1, q.y *= -1;\n int g = __gcd(abs(q.x), abs(q.y));\n q.x /= g, q.y /= g;\n long long t = (long long) (q.x + INF) * (INF + INF) + (q.y + INF);\n if (!i) {\n can[t] = 1;\n hs2[t].pb(mp(i, j));\n }\n else if (can.count(t)) {\n hs2[t].pb(mp(i, j));\n }\n }\n int k = sz(p);\n init(k);\n int res = 0;\n for (auto it : hs2) if (can[it.fi]) {\n reset();\n for (auto x : it.se) {\n join(x.fi, x.se);\n }\n if (!odds) {\n res++;\n }\n }\n cout << res << \"\\n\";\n}\n\nint main(int argc, char* argv[]) {\n ios_base::sync_with_stdio(0), cin.tie(0);\n if (argc > 1) {\n assert(freopen(argv[1], \"r\", stdin));\n }\n if (argc > 2) {\n assert(freopen(argv[2], \"wb\", stdout));\n }\n solve();\n cerr << \"\\nTime elapsed: \" << 1000 * clock() / CLOCKS_PER_SEC << \"ms\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "618a5fffd091a6a43457336c763bbb9a", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\n#define forn(i,n) for(int i=0;i<(int)(n);i++)\n#define si(c) ((int)(c).size())\n#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)\n#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)\n#define all(c) (c).begin(), (c).end()\n#define D(a) cerr << #a << \"=\" << a << endl;\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n\ntypedef long long int ll;\ntypedef vector vi;\ntypedef pair pii;\ntypedef pair pll;\n\nconst int N = 2020;\n\nint n;\nll x[N], y[N], px, py;\nset points;\n\nll gcd(ll a, ll b) { return a ? gcd(b%a,a) : b; }\nvoid normal(pll &n) {\n int d = gcd(n.first, n.second);\n n.first /= d;\n n.second /= d;\n if (n.first < 0) {\n n.first = -n.first;\n n.second = -n.second;\n }\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n cin >> n;\n forn(i,n) {\n cin >> x[i] >> y[i];\n px += x[i];\n py += y[i];\n x[i] *= n;\n y[i] *= n;\n points.emplace(x[i], y[i]);\n }\n\n forn(i,n) {\n auto sx = 2*px - x[i];\n auto sy = 2*py - y[i];\n auto p = mp(x[i], y[i]);\n auto s = mp(sx,sy);\n if (points.count(s)) {\n points.erase(s);\n if (points.count(p)) points.erase(p);\n }\n }\n\n n = 0;\n for (auto &xy : points) {\n x[n] = xy.first;\n y[n] = xy.second;\n n++;\n }\n\n if (n < 2) {\n cout << -1 << endl;\n return 0;\n }\n\n set ans;\n forn(i,n) {\n pll q = mp(x[0] + x[i], y[0] + y[i]);\n pll p = mp(2*px,2*py);\n pll v = mp(q.first - p.first, q.second-p.second);\n pll norm = mp(-v.second, v.first);\n normal(norm);\n\n vector d;\n forn(i,n) d.eb(norm.first * x[i] + norm.second * y[i]);\n sort(all(d)); \n\n auto goal = d.front() + d.back();\n int sz = si(d), ok = 1;\n for (int i = 0, j = sz-1; i < j; i++, j--) {\n ok &= d[i] + d[j] == goal;\n }\n if (ok) ans.insert(norm); \n }\n //for (auto xy: ans) cerr << xy.first << ' ' << xy.second << endl;\n cout << si(ans) << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "6f7f1e3e296ba92ce6ea9d239a95c122", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\nusing namespace std;\n\nint allSame, currCnt, N, M, nr, X[4002017], Y[4002017], inv[2017];\ndouble PI = acos (-1.0);\n\nstruct point\n{\n int x, y;\n point () {}\n point (int _x, int _y):\n x (_x), y (_y) {}\n bool operator < (const point &other) const\n {\n if (x == other.x) return y < other.y;\n return x < other.x;\n }\n}A[2017], b[2017];\n\npoint operator + (point a, point b) {return point (a.x + b.x, a.y + b.y);}\n\nbool det (point a, point b, point c)\n{\n return (1LL * a.x * b.y + 1LL * b.x * c.y + 1LL * c.x * a.y -\n 1LL * a.x * c.y - 1LL * b.x * a.y - 1LL * c.x * b.y == 0LL);\n}\n\nstruct unghi\n{\n long long sus, jos, side;\n\n unghi () {}\n\n unghi (long long x, long long y)\n {\n if (x > 0) side = -1;\n else side = (x != 0);\n sus = y, jos = x;\n if (jos < 0) sus = -y, jos = -x;\n }\n\n bool operator == (const unghi &other) const\n {\n return (side == other.side && 1LL * sus * other.jos == 1LL * jos * other.sus);\n }\n\n bool operator < (const unghi &other) const\n {\n if (side != other.side) return side < other.side;\n return 1LL * sus * other.jos < 1LL * other.sus * jos;\n }\n\n void print (string s)\n {\n printf (\"(%I64d, %I64d, %I64d) %.10f%s\", sus, jos, side, atan ((double) sus / jos * side) * 180.0 / PI, s.c_str ());\n }\n};\n\nstruct query\n{\n unghi m;\n int where;\n\n bool operator < (const query &other) const\n {\n if (m == other.m)\n {\n if (1LL * where * other.where < 0) return (where > other.where);\n if (where < 0) return 0;\n int i = X[where], j = Y[where], k = X[other.where], p = Y[other.where];\n if (i != k) return i < k;\n return j < p;\n }\n return (m < other.m);\n }\n}events[4202017];\nvector < pair < pair < long long, long >, pair < long long, long > > > S;\n\nint XX = 1, YY = 2;\nvoid update (int pos, point newVal)\n{\n if (pos == 1)\n {\n point old = b[pos];\n allSame = 0, b[pos] = newVal;\n for (int i=1; i<=(N + 1) / 2; i++)\n allSame += (b[i].x == b[1].x && b[i].y == b[1].y);\n b[pos] = old;\n }\n else\n allSame -= (b[pos].x == b[1].x && b[pos].y == b[1].y), allSame += (newVal.x == b[1].x && newVal.y == b[1].y);\n\n if (pos == XX || pos == YY)\n {\n b[pos] = newVal, currCnt = 0, XX = 1, YY = 1;\n for (int i=2; i<=(N + 1) / 2; i++)\n if (b[i].x != b[1].x || b[i].y != b[1].y)\n YY = i;\n for (int i=1; i<=(N + 1) / 2; i++)\n currCnt += det (b[XX], b[YY], b[i]);\n }\n else\n {\n currCnt -= det (b[XX], b[YY], b[pos]), currCnt += det (b[XX], b[YY], newVal);\n b[pos] = newVal;\n }\n}\n\nvoid refresh (int i)\n{\n if (i > N + 1 - i) i = N + 1 - i;\n update (i, A[i] + A[N + 1 - i]);\n}\n\nlong long gcd (long long a, long long b)\n{\n long long r;\n while (b != 0)\n r = a % b, a = b, b = r;\n return a;\n}\n\nvoid tryToAdd ()\n{\n if (allSame == (N + 1) / 2)\n {\n printf (\"-1\\n\");\n exit (0);\n }\n if (currCnt == (N + 1) / 2)\n {\n int a1 = b[XX].x, b1 = b[XX].y, a2 = b[YY].x, b2 = b[YY].y;\n //printf (\"%d %d %d %d\\n\", a1, b1, a2, b2);\n if (a1 != a2)\n {\n long long x = 1LL * a1 * b2 - 1LL * a2 * b1, y = a1 - a2, d = gcd (x, y), z = b1 - b2, t = a1 - a2, d2 = gcd (z, t);\n //S.insert (unghi (1LL * a2 * b1 - 1LL * a1 * b2, b1 - b2));\n x /= d, y /= d, z /= d2, t /= d2;\n if (y < 0) y = -y, x = -x;\n if (t < 0) z = -z, t = -t;\n S.push_back ({{x, y}, {z, t}});\n }\n else S.push_back ({{0, 0}, {0, 0}});\n }\n}\n\nint main()\n{\n//freopen (\"input\", \"r\", stdin);\n//freopen (\"output\", \"w\", stdout);\n\nscanf (\"%d\", &N);\nif (N <= 2)\n{\n printf (\"-1\\n\");\n return 0;\n}\nfor (int i=1; i<=N; i++)\n scanf (\"%d %d\", &A[i].x, &A[i].y), inv[i] = i;\nsort (A + 1, A + N + 1);\n\nfor (int i=1; i<=N; i++)\n for (int j=i+1; j<=N; j++)\n {\n nr ++;\n X[nr] = i;\n Y[nr] = j;\n events[nr].where = nr;\n events[nr].m = unghi (A[j].y - A[i].y, A[j].x - A[i].x);\n }\nsort (events + 1, events + nr + 1);\n\nfor (int i=1; i<=(N + 1) / 2; i++)\n b[i] = A[i] + A[N + 1 - i];\nupdate (1, b[1]), tryToAdd ();\nfor (int currstp=1; currstp<=nr; currstp++)\n{\n int stp = (currstp <= nr ? currstp : currstp - nr);\n int x = X[events[stp].where], y = Y[events[stp].where];\n swap (A[inv[x]], A[inv[y]]);\n swap (inv[x], inv[y]);\n// printf (\"%d %d\\n\", inv[x], inv[y]);\n refresh (inv[x]), refresh (inv[y]);\n tryToAdd ();\n}\nsort (S.begin (), S.end ());\nS.erase (unique (S.begin (), S.end ()), S.end ());\nprintf (\"%d\\n\", S.size ());\n\nreturn 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "8ab0ff97a510ab867c0981ea48c74659", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nusing point = complex;\n#define X real()\n#define Y imag()\n\nlong double dot(point a, point b) {\n return a.X * b.X + a.Y * b.Y;\n}\n\nlong double cross(point a, point b) {\n return a.X * b.Y - a.Y * b.X;\n}\n\nconst long double E = 1e-12;\nconst int N = 2'000;\n\npoint points[N];\nlong double dist[N];\nbool dead[N];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int n;\n cin >> n;\n\n point center;\n for (int i = 0; i < n; ++i) {\n int x, y;\n cin >> x >> y;\n points[i] = point(x, y);\n center += points[i];\n }\n center /= n;\n\n for (int i = 0; i < n; ++i) {\n for (int j = i; j < n; ++j) {\n if (!dead[i] && !dead[j]) {\n point u = 2.0L * center - points[i] - points[j];\n if (abs(u) <= E) {\n dead[i] = true;\n dead[j] = true;\n }\n }\n }\n }\n\n int m = count(dead, dead + n, false);\n if (m == 0) {\n cout << -1 << \"\\n\";\n exit(0);\n }\n\n int pivot = 0;\n while (dead[pivot]) {\n ++pivot;\n }\n\n vector angles;\n for (int i = 0; i < n; ++i) {\n if (!dead[i] && (i != pivot || m % 2 == 1)) {\n point vec = 2.0L * center - points[i] - points[pivot];\n\n bool ok = true;\n for (auto j : angles) {\n ok &= (abs(cross(vec, j)) > E);\n }\n\n for (int i = 0; i < n; ++i) {\n dist[i] = cross(vec, points[i]);\n }\n sort(dist, dist + n);\n\n for (int i = 0; i < n; ++i) {\n ok &= (abs(dist[i] + dist[n - 1 - i] - 2.0L * cross(vec, center)) <= E);\n }\n\n if (ok) {\n angles.push_back(vec);\n }\n }\n }\n\n cout << angles.size() << \"\\n\";\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "f54b990a39c4c428831e5049bf1a0731", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\ntypedef long long ll;\ntypedef long long llong;\ntypedef long double ld;\ntypedef unsigned long long ull;\n\nusing namespace std;\nconst ld eps = 1e-9;\n\n/*\nll pw(ll a, ll b) {\n\tll ans = 1; while (b) {\n\t\twhile (!(b & 1)) b >>= 1, a = (a * a) % MOD;\n\t\tans = (ans * a) % MOD, --b;\n\t} return ans;\n}\n*/\n\nstruct point {\n\tld x, y;\n\tpoint(ld X = 0, ld Y = 0) {\n\t\tx = X, y = Y;\n\t}\n\tpoint operator+(point a) {\n\t\treturn point(x + a.x, y + a.y);\n\t}\n\tpoint operator-(point a) {\n\t\treturn point(x - a.x, y - a.y);\n\t}\n\tld operator*(point a) {\n\t\treturn x * a.y - y * a.x;\n\t}\n\tpoint operator*(ld k) {\n\t\treturn point(x * k, y * k);\n\t}\n\tld len() {\n\t\treturn sqrt(x * x + y * y);\n\t}\n};\n\nbool operator==(point a, point b) {\n\treturn a.x == b.x && a.y == b.y;\n}\n\npoint norm(point a) {\n\tld r = a.len();\n\ta.x /= r;\n\ta.y /= r;\n\treturn a;\n}\n\nint n;\npoint p[2100];\nint en[2100];\nvector v1;\nvector v2;\nvector ans;\n\nint main() {\n\tpoint sum = 0;\n\tcin >> n;\n\tfor (int i = 0; i < n; ++i) {\n\t\tcin >> p[i].x >> p[i].y;\n\t\tsum = sum + p[i];\n\t}\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = i; j < n; ++j) {\n\t\t\tpoint xx = p[i] + p[j];\n\t\t\tif (xx * n == sum * 2) {\n\t\t\t\ten[i] = 1;\n\t\t\t\ten[j] = 1;\n\t\t\t}\n\t\t}\n\t}\n\tint st = -1;\n\tfor (int i = 0; i < n; ++i) {\n\t\tif (!en[i]) {\n\t\t\tst = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (st == -1) {\n\t\tcout << -1 << \"\\n\";\n\t\treturn 0;\n\t}\n\tsum.x /= n;\n\tsum.y /= n;\n\tfor (int i = 0; i < n; ++i)\n\t\tp[i] = p[i] - sum;\n\tfor (int i = 0; i < n; ++i) {\n\t\tpoint xx = norm(p[i] + p[st]);\n\t\tv1.clear();\n\t\tv2.clear();\n\t\tfor (int j = 0; j < n; ++j) {\n\t\t\tld cc = (p[j] * xx);\n\t\t\tif (abs(cc) > eps) {\n\t\t\t\tif (cc > 0)\n\t\t\t\t\tv1.push_back(cc);\n\t\t\t\telse\n\t\t\t\t\tv2.push_back(-cc);\n\t\t\t}\n\t\t}\n\t\tsort(v1.begin(), v1.end());\n\t\tsort(v2.begin(), v2.end());\n\t\tif (v1.size() != v2.size())\n\t\t\tcontinue;\n\t\tint fl = 0;\n\t\tfor (int j = 0; j < v1.size(); ++j) {\n\t\t\tif (abs(v1[j] - v2[j]) >= eps) {\n\t\t\t\tfl = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (fl) {\n\t\t\tcontinue;\n\t\t}\n\t\tans.push_back(xx);\n\t}\n\tint cnt = 0;\n\tfor (int i = 0; i < ans.size(); ++i) {\n\t\tint fl = 0;\n\t\tfor (int j = 0; j < i; ++j) {\n\t\t\tif ((ans[i] - ans[j]).len() <= eps || (ans[i] + ans[j]).len() <= eps) {\n\t\t\t\tfl = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!fl)\n\t\t\t++cnt;\n\t}\n\tcout << cnt << \"\\n\";\n\treturn 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "7e6677d2b54252ba5c9bf30fdaf718c9", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing ll = long long;\nusing ld = long double;\nusing namespace std;\n\nconst int MAXN = 20001;\n\nint n;\nll x[MAXN], y[MAXN];\n\nvoid rm(int id) {\n for (int i = id; i + 1 < n; ++i) {\n x[i] = x[i + 1];\n y[i] = y[i + 1];\n }\n --n;\n}\n\nll vals[MAXN];\nint ans = 0;\n\nconst int BTS = 20;\nconst ll MSK = (1 << BTS) - 1;\n\npair mul(ll x, ll y) {\n bool sgn = false;\n\n if (x < 0) {\n x = -x;\n sgn = !sgn;\n }\n\n if (y < 0) {\n y = -y;\n sgn = !sgn;\n }\n\n ll xl = x & MSK;\n x >>= BTS;\n ll yl = y & MSK;\n y >>= BTS;\n\n ll lw = (xl * yl);\n ll bg = ((x * y) << BTS) + x * yl + y * xl + (lw >> BTS);\n lw &= MSK;\n\n if (sgn) {\n bg = -bg;\n lw = -lw;\n }\n\n return { bg, lw };\n}\n\n\nint origx[MAXN];\nint origy[MAXN];\nint orign;\n\nvoid work(ll mdx, ll mdy) {\n bool cool = true;\n for (int j = 0; j < orign; ++j) {\n vals[j] = origx[j] * mdx + origy[j] * mdy;\n }\n\n sort(vals, vals + orign);\n\n ll s = vals[0] + vals[orign - 1];\n\n for (int j = 0; j < orign - 1 - j; ++j) {\n if (vals[j] + vals[orign - 1 - j] != s) {\n cool = false;\n break;\n }\n }\n \n if (cool) {\n ++ans;\n }\n}\n\nll mdx[MAXN], mdy[MAXN];\n\n\nint main() {\n#ifdef PAUNSVOKNO\n freopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);\n\n cin >> n;\n orign = n;\n\n ll sx = 0, sy = 0;\n for (int i = 0; i < n; ++i) {\n cin >> x[i] >> y[i];\n sx += x[i], sy += y[i];\n origx[i] = x[i], origy[i] = y[i];\n }\n\n for (int i = 0; i < n; ++i) {\n x[i] = (x[i] * n - sx);\n y[i] = (y[i] * n - sy);\n }\n\n int cur = 0;\n while (cur < n) {\n if (x[cur] == 0 && y[cur] == 0) {\n rm(cur);\n continue;\n }\n\n bool ok = true;\n for (int i = cur + 1; i < n; ++i) {\n if (x[i] + x[cur] == 0 && y[i] + y[cur] == 0) {\n rm(i);\n rm(cur);\n ok = false;\n break;\n }\n }\n\n cur += ok;\n }\n\n if (n == 0) {\n cout << -1 << \"\\n\";\n return 0;\n }\n\n mdx[0] = x[0], mdy[0] = y[0];\n\n work(-y[0], x[0]);\n\n for (int i = 1; i < n; ++i) {\n bool ok = true;\n mdx[i] = (x[0] + x[i]);\n mdy[i] = (y[0] + y[i]);\n\n\n for (int j = 0; j < i; ++j) {\n if (mul(mdx[i], mdy[j]) == mul(mdy[i], mdx[j])) {\n ok = false;\n break;\n }\n }\n\n if (ok) {\n work(-mdy[i], mdx[i]);\n }\n }\n\n\n\n cout << ans << \"\\n\";\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "0ae76ed8d5ab84c6e121fcca3643018d", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//by yjz\n#include\nusing namespace std;\n#define FF first\n#define SS second\n#define PB push_back\n#define MP make_pair\n#define bged(v) (v).begin(),(v).end()\n#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)\ntypedef long long ll;\nconst int Imx=2147483647;\nconst ll Lbig=2e18;\nconst int mod=1e9+7;\n//My i/o stream\nstruct fastio\n{\n\tchar s[100000];\n\tint it,len;\n\tfastio(){it=len=0;}\n\tinline char get()\n\t{\n\t\tif(it0)it--;\n\t\treturn c!=EOF;\n\t}\n}_buff;\n#define geti(x) x=getnum()\n#define getii(x,y) geti(x),geti(y)\n#define getiii(x,y,z) getii(x,y),geti(z)\n#define puti(x) putnum(x),putchar(' ')\n#define putii(x,y) puti(x),puti(y)\n#define putiii(x,y,z) putii(x,y),puti(z)\n#define putsi(x) putnum(x),putchar('\\n')\n#define putsii(x,y) puti(x),putsi(y)\n#define putsiii(x,y,z) putii(x,y),putsi(z)\ninline ll getnum()\n{\n\tll r=0;bool ng=0;char c;c=_buff.get();\n\twhile(c!='-'&&(c<'0'||c>'9'))c=_buff.get();\n\tif(c=='-')ng=1,c=_buff.get();\n\twhile(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();\n\treturn ng?-r:r;\n}\ntemplate inline void putnum(T x)\n{\n\tif(x<0)putchar('-'),x=-x;\n\tregister short a[20]={},sz=0;\n\twhile(x)a[sz++]=x%10,x/=10;\n\tif(sz==0)putchar('0');\n\tfor(int i=sz-1;i>=0;i--)putchar('0'+a[i]);\n}\ninline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}\nll n;\npair a[2111];\nll sumx,sumy;\nll b[2011];\nset >st;\nbool check(ll cx,ll p,ll q)//t=p/q\n{\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tb[i]=a[i].FF*cx*q+a[i].SS*p;\n\t}\n\tsort(b+1,b+n+1);\n\tll M2=b[1]+b[n];\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tif(b[i]+b[n-i+1]!=M2)return false;\n\t}\n\treturn true;\n}\nbool matched[2011];\nint mcnt;\nvoid pre_go(int i)\n{\n\tfor(int j=i+1;j<=n;j++)\n\t{\n\t\tll p=n*(-a[i].FF-a[j].FF)+2*sumx;\n\t\tll q=n*(a[i].SS+a[j].SS)-2*sumy;\n\t\tif(p==0&&q==0)\n\t\t{\n\t\t\tassert(matched[i]==0&&matched[j]==0);\n\t\t\tmatched[i]=matched[j]=1;\n\t\t\tmcnt+=2;\n\t\t}\n\t}\n}\nvoid go(int i)\n{\n\tif(matched[i])return;\n\tfor(int j=i+1;j<=n;j++)\n\t{\n\t\tll p=n*(-a[i].FF-a[j].FF)+2*sumx;\n\t\tll q=n*(a[i].SS+a[j].SS)-2*sumy;\n//\t\tcerr<=2)break;\n\t}\n\tif(check(0,1,1))st.insert(MP(1,0));\n//\tforeach(it,st)cerr<FF<<\",\"<SS<\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef complex point;\n\ndouble prodvec(point p1,point p2)\n{\n return imag(conj(p1)*p2);\n}\n\npoint leer()\n{\n double x,y;\n cin>>x>>y;\n return point(x,y);\n}\n\ndouble error=1e-10;\n\nbool coincide(double x1,double x2)\n{\n return abs(x1-x2)>n;\n for (int i=0;i vsol;\n for (int cas=0;cas vd(n);\n for (int j=0;jerror;\n sol+=ok;\n }\n cout<\nusing ll = long long;\nusing ld = long double;\nusing namespace std;\n\nconst int MAXN = 20001;\n\nint n;\nll x[MAXN], y[MAXN];\n\nvoid rm(int id) {\n for (int i = id; i + 1 < n; ++i) {\n x[i] = x[i + 1];\n y[i] = y[i + 1];\n }\n --n;\n}\n\nll vals[MAXN];\nint ans = 0;\n\nconst int BTS = 20;\nconst ll MSK = (1 << BTS) - 1;\n\npair mul(ll x, ll y) {\n bool sgn = false;\n\n if (x < 0) {\n x = -x;\n sgn = !sgn;\n }\n\n if (y < 0) {\n y = -y;\n sgn = !sgn;\n }\n\n ll xl = x & MSK;\n x >>= BTS;\n ll yl = y & MSK;\n y >>= BTS;\n\n ll lw = (xl * yl);\n ll bg = ((x * y) << BTS) + x * yl + y * xl + (lw >> BTS);\n lw &= MSK;\n\n if (sgn) {\n bg = -bg;\n lw = -lw;\n }\n\n return { bg, lw };\n}\n\n\nint origx[MAXN];\nint origy[MAXN];\nint orign;\n\nvoid work(ll mdx, ll mdy) {\n bool cool = true;\n for (int j = 0; j < orign; ++j) {\n vals[j] = origx[j] * mdx + origy[j] * mdy;\n }\n\n sort(vals, vals + orign);\n\n ll s = vals[0] + vals[orign - 1];\n\n for (int j = 0; j < orign - 1 - j; ++j) {\n if (vals[j] + vals[orign - 1 - j] != s) {\n cool = false;\n break;\n }\n }\n \n if (cool) {\n ++ans;\n }\n}\n\nll mdx[MAXN], mdy[MAXN];\n\n\nint main() {\n#ifdef PAUNSVOKNO\n freopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);\n\n cin >> n;\n orign = n;\n\n ll sx = 0, sy = 0;\n for (int i = 0; i < n; ++i) {\n cin >> x[i] >> y[i];\n sx += x[i], sy += y[i];\n origx[i] = x[i], origy[i] = y[i];\n }\n\n for (int i = 0; i < n; ++i) {\n x[i] = (x[i] * n - sx);\n y[i] = (y[i] * n - sy);\n }\n\n int cur = 0;\n while (cur < n) {\n if (x[cur] == 0 && y[cur] == 0) {\n rm(cur);\n continue;\n }\n\n bool ok = true;\n for (int i = cur + 1; i < n; ++i) {\n if (x[i] + x[cur] == 0 && y[i] + y[cur] == 0) {\n rm(i);\n rm(cur);\n ok = false;\n break;\n }\n }\n\n cur += ok;\n }\n\n if (n == 0) {\n cout << -1 << \"\\n\";\n return 0;\n }\n\n mdx[0] = x[0], mdy[0] = y[0];\n\n work(-y[0], x[0]);\n\n for (int i = 1; i < n; ++i) {\n bool ok = true;\n mdx[i] = (x[0] + x[i]);\n mdy[i] = (y[0] + y[i]);\n\n\n for (int j = 0; j < i; ++j) {\n if (mul(mdx[i], mdy[j]) == mul(mdy[i], mdx[j])) {\n ok = false;\n break;\n }\n }\n\n if (ok) {\n work(-mdy[i], mdx[i]);\n }\n }\n\n\n\n cout << ans << \"\\n\";\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "305934ef07827c156d10ce2e913049c7", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//created by missever\n\n#include\n#define fi first\n#define se second\nusing namespace std;\ntypedef long long LL;\ntypedef pair P;\n\nconst double eps = 1e-8;\nconst double pi = acos(-1);\ninline int sgn(double x) {\n if(x < -eps) return -1;\n else if(x > eps) return 1;\n else return 0;\n}\nstruct Point {\n double x,y;\n Point(double _x = 0.0,double _y = 0.0): x(_x),y(_y) {}\n Point operator + (const Point &b) const {\n return Point(x + b.x,y + b.y);\n }\n Point operator - (const Point &b) const {\n return Point(x - b.x,y - b.y);\n }\n double operator * (const Point &b) const { //点乘\n return (x * b.x + y * b.y);\n }\n double operator ^ (const Point &b) const { //叉乘,判断b点的相对于该点位置关系 左正右负\n return (x * b.y - y * b.x);\n }\n Point operator * (double b) {\n return Point(x * b,y * b);\n }\n bool operator < (const Point &b) const { //水平序比较\n if(sgn(x - b.x) == 0) return sgn(y - b.y) <= 0;\n else return sgn(x - b.x) < 0;\n }\n bool operator == (const Point &b) const {\n return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;\n }\n Point rot(double ang) { //旋转,输入角度\n return Point(x * cos(ang) - y * sin(ang),x * sin(ang) + y * cos(ang));\n }\n Point rot(double g1,double g2) { //旋转,输入正弦值,余弦值\n return Point(x * g2 - y * g1,x * g1 + y * g2);\n }\n double norm() { //求模\n return sqrt(x * x + y * y);\n }\n Point unit() { //取单位向量\n double ll = norm();\n return Point(x / ll,y / ll);\n }\n};\n\nPoint f[2005],p[2005];\nbool vis[2005];\ndouble d[2005];\n\nint main() {\n#ifdef CX_TEST\n freopen(\"E:\\\\program--GG\\\\test_in.txt\", \"r\", stdin);\n#endif\n int n,m,ans = 0,i,j;\n Point o = Point(0,0);\n scanf(\"%d\",&n);\n for(i = 0; i < n; i++) {\n scanf(\"%lf%lf\",&f[i].x,&f[i].y);\n o = o + f[i];\n }\n o = o * (1.0 / n);\n for(i = 0; i < n; i++) {\n if(vis[i]) continue;\n for(j = i; j < n; j++) {\n if(f[i] + f[j] == o * 2.0) {\n vis[i] = vis[j] = 1;\n break;\n }\n }\n }\n for(i = j = 0; i < n; i++) {\n if(!vis[i]) f[j++] = f[i] - o;\n }\n if(!j) return puts(\"-1\"),0;\n n = j;\n for(i = m = 0; i < n; i++) {\n Point u = f[0] + f[i];\n Point v = Point(-u.y,u.x);\n for(j = 0; j < n; j++) d[j] = f[j] * v;\n sort(d,d + n);\n for(j = 0; j < n; j++) if(sgn(d[j] + d[n - 1 - j])) break;\n if(j == n){\n v = v.unit();\n for(j = 0;j < m; j++) if(!sgn(p[j] ^ v)) break;\n if(j == m) {\n ans++;\n p[m++] = v;\n }\n }\n }\n printf(\"%d\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "c524ab4c7a106af0de2b1fdf3b6fed0d", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef pair Point;\n\nconst int maxN = 2000;\nconst long long INF = 1000000000000LL;\nconst Point INFpoint = Point(INF, INF);\n\nPoint T[maxN];\nvector proste, rzuty;\n\nlong long kw(long long a) {return a*a;}\n\nlong double dist(Point& a, Point& b)\n{\n\treturn kw(a.first - b.first) + kw(a.second - b.second);\n}\n\nPoint srodek(Point& a, Point& b)\n{\n\treturn Point((a.first + b.first) / 2, (a.second + b.second) / 2);\n}\n\nvoid add(Point& a, Point& b)\n{\n\ta.first += b.first;\n\ta.second += b.second;\n}\n\nvoid mult(Point& a, long long b)\n{\n\ta.first *= b;\n\ta.second *= b;\n}\n\nint main()\n{\n\tint n;\n\tscanf (\"%d\", &n);\n\tPoint mass_cent = Point(0, 0);\n\t\n\tfor (int i=0; i\n#define ll long long\n#define inf 1000000005\n#define mod 1000000007\n#define put putchar('\\n')\n#define F(i,a,b) for (int i=(a);i<=(b);i++)\n#define D(i,a,b) for (int i=(a);i>=(b);i--)\n#define R(i,a,b) for (int i=(a);i<(b);i++)\n#define go(i,t) for (int i=head[t];i;i=Next[i])\n#define sqr(x) ((x)*(x))\n#define re register\n#define mp make_pair\n#define fi first\n#define se second\n#define pa pair\n#define pb push_back\n#define be begin()\n#define en end()\n#define ret return puts(\"-1\"),0;\n#define db double\n#define N 500055\n#define eps 1e-7\n//#define int ll\nusing namespace std;\ninline char gc(){\n static char buf[100000],*p1=buf,*p2=buf;\n return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;\n}\n#define gc getchar\ninline int read(){char c=getchar();int tot=1;while ((c<'0'|| c>'9')&&c!='-') c=getchar();if (c=='-'){tot=-1;c=getchar();}\nint sum=0;while (c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}return sum*tot;}\ninline void wr(int x){if (x<0) {putchar('-');wr(-x);return;}if(x>=10)wr(x/10);putchar(x%10+'0');}\ninline void wrn(int x){wr(x);put;}inline void wri(int x){wr(x);putchar(' ');}\ninline void wrn(int x,int y){wri(x);wrn(y);}inline void wrn(int a,int b,int c){wri(a);wrn(b,c);}\nint n,m,ans,vis[N],cnt;\ndb c[N],q[N];\nstruct xx{\n int x,y;\n}z[N];\ndb sx,sy,a,b;\nsigned main(){\n n=read();\n if (n==2) ret;\n F(i,1,n) sx+=z[i].x=read(),sy+=z[i].y=read();\n sx=sx/n,sy=sy/n;\n F(i,1,n){\n if (vis[i]) continue;\n if ((fabs(z[i].x-sx)eps) pd=0;\n }\n if (pd) q[++cnt]=(b==0)?-2151.2352:a;\n }\n }\n q[0]=-1241.23462;\n sort(q+1,q+cnt+1);\n F(i,1,cnt){\n if (fabs(q[i]-q[i-1])>eps) ans++;\n }\n wrn(ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "d1e813511785d227a6b8ae49eb49d1b6", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair PII;\n\nconst int MM = 1e9 + 7;\nconst double eps = 1e-8;\nconst int MAXN = 2e3 + 10;\nconst int MAXM = 2e6 + 10;\n\nint n;\n\nstruct Pt{\n\tlong long x, y;\n};\n\nbool operator < (const Pt & a, const Pt & b){\n\tif (a.x == b.x)\n\t\treturn a.y < b.y;\n\telse\n\t\treturn a.x < b.x;\n}\n\nbool operator == (const Pt & a, const Pt & b){\n\treturn a.x == b.x && a.y == b.y;\n}\n\ninline long long operator * (const Pt & a, const Pt & b){\n\treturn a.x * b.y - a.y * b.x;\n}\n\ninline long long operator % (const Pt & a, const Pt & b){\n\treturn a.x * b.x + a.y * b.y;\n}\n\ninline Pt operator - (const Pt & a, const Pt & b){\n\treturn (Pt){a.x - b.x, a.y - b.y};\n}\n\ninline Pt operator + (const Pt & a, const Pt & b){\n\treturn (Pt){a.x + b.x, a.y + b.y};\n}\n\ninline Pt operator * (const Pt & a, const ll u){\n\treturn (Pt){a.x * u, a.y * u};\n}\n\nlong long sqr(long long x){\n\treturn x * x;\n}\n\ninline double len(const Pt & a){\n\treturn sqrt(sqr(a.x) + sqr(a.y));\n}\n\nlong long absll(long long x){\n\treturn x < 0 ? -x : x;\n}\n\nvector convex_hull(vector u){\n sort(u.begin(), u.end()); \n u.erase(unique(u.begin(), u.end()), u.end()); \n if (u.size() < 3) \n\t\treturn u;\n vector c;\n for(int i=0, o=1, m=1; ~i; i += o){\n while(c.size() > m){\n Pt a = c.back() - c[c.size() - 2];\n Pt b = c.back() - u[i];\n if (a * b < 0) \n\t\t\t\tbreak; // <= 0 保留共线点 \n c.pop_back();\n }\n c.push_back(u[i]);\n if(i + 1 == u.size()) \n\t\t\tm = c.size(), o = -1;\n }\n c.pop_back();\n return c;\n}\n\nset PP, LL, PL;\n\nvoid addpp(int u, int v){\n\tu %= n, v %= n;\n\tif (u > v) swap(u, v);\n\tPP.insert(PII(u, v));\n}\n\nvoid addpl(int u, int v){\n}\n\nvoid addll(int u, int v){\n}\n\nvoid xzkk(vector& c){\n\tauto Area = [&](int u, int v, int w){\n\t\tu %= n, v %= n, w %= n;\n\t\treturn abs((c[u] - c[v]) * (c[w] - c[v]));\n\t};\n\tint tmpn = n;\n\tn = c.size();\n\tint v = 0, u = 1;\n\twhile(Area(v,v+1,u+1)>\n\t\t\tArea(v,v+1,u)) \n\t\tu++;\n\tPP.clear();\n\tPL.clear();\n\tLL.clear();\n\taddpp(v, u);\n\taddpp(v+1, u);\n\taddpl(u, v);\n\tfor(int i=0;i<=4*n;i++){\n\t addpp(u, v);\n\t if (i & 1){\n\t if ((c[(v+1)%n]-c[v%n])*\n\t (c[(u+1)%n]-c[u%n]) > 0)\n\t\t swap(u, v);\n\t }\n\t else{\n\t\taddpp(u, v+1);\n\t\taddpl(u, v);\n\t\tif (Area(v, v+1, u) \n\t\t\t==Area(v,v+1,u+1)){\n\t\t addpp(u+1, v);\n\t\t addpp(u+1, v+1);\n\t\t addpl(u+1, v);\n\t\t addpl(v, u);\n\t\t addpl(v+1, u);\n\t\t addll(u, v); \n\t\t u++;\n\t\t}\n\t\tv++;\n\t }\n\t}\n\tn = tmpn;\n}\n\nll ans;\nvector a;\n\nvector ret;\n\nvoid check(Pt dir, Pt&U, Pt&V){\n\tll r = __gcd(abs(dir.x), abs(dir.y));\n\tdir.x /= r, dir.y /= r;\n\tif (dir.x < 0){\n\t\tdir.x = -dir.x;\n\t\tdir.y = -dir.y;\n\t}\n\tif (dir.x == 0 && dir.y < 0){\n\t\tdir.y = -dir.y;\n\t}\n\t//vector<__int128> F;\n\t//__int128 A = dir.x;\n\t//__int128 B = dir.y;\n\tvector F;\n\tll A = dir.x, B = dir.y;\n\tll UVx = U.x + V.x;\n\tll UVy = U.y + V.y;\n\tll sm = A * UVx + B * UVy;\n\tfor (auto &p : a){\n\t\tF.push_back(A * p.x + B * p.y);\n\t}\n\tsort(F.begin(), F.end());\n\tfor (int i = 0; i * 2 < F.size(); i++){\n\t\tif (F[i] + F[F.size() - 1 - i] != sm){\n\t\t\treturn ;\n\t\t}\n\t}\n\tret.push_back(dir);\n}\n\nll nico = 453209;\nll maki = 453757;\n\nvoid gao(Pt U, Pt V){\n\tll sx = 0;\n\tll sy = 0;\n\tll sxx = 0;\n\tll syy = 0;\n\tll sxy = 0;\n\tll UVx = U.x + V.x + 2 * nico;\n\tll UVy = U.y + V.y;\n\tfor (auto &p : a){\n\t\tsx += p.x;\n\t\tsy += p.y;\n\t\tsxx += sqr(p.x + nico) - sqr(UVx - p.x + nico);\n\t\tsyy += sqr(p.y) - sqr(UVy - p.y);\n\t\tsxy += (p.x + nico) * p.y - (UVx - p.x + nico) * (UVy - p.y);\n\t}\n\tsx = sx * 2 - n * (U.x + V.x);\n\tsy = sy * 2 - n * (U.y + V.y);\n\t/*\n\tcerr << \"+++++++++++++++++++++++++++++\" << endl;\n\tcerr << U.x << ' ' << U.y << ' ' << V.x << ' ' << V.y << endl;\n\tcerr << sx << ' ' << sy << ' ' << sxx << ' ' << syy << ' ' << sxy << endl;\n\t*/\n\tif (sx == 0){\n\t\tif (sy != 0)\n\t\t\tcheck((Pt){1ll, 0ll}, U, V);\n\t\telse{\n\t\t\tsxy *= 2;\n\t\t\tif (sxx == 0 && syy == 0){\n\t\t\t\tans = 1ll << 60;\n\t\t\t}\n\t\t\tans = 1ll << 60;\n\t\t\t/*\n\t\t\telse{\n\t\t\t\tif (sxx != 0){\n\t\t\t\t\t__int128 del = (__int128)sxy * sxy - (__int128)4 * sxx * syy;\n\t\t\t\t\tll tmp = sqrt((long double)del);\n\t\t\t\t\twhile (tmp * tmp < del) tmp++;\n\t\t\t\t\twhile (tmp * tmp > del) tmp--;\n\t\t\t\t\tif (tmp * tmp != del)\n\t\t\t\t\t\treturn ;\n\t\t\t\t\tcheck((Pt){-sxy+tmp, 2*sxx}, U, V);\n\t\t\t\t\tcheck((Pt){-sxy-tmp, 2*sxx}, U, V);\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\t__int128 del = (__int128)sxy * sxy - (__int128)4 * sxx * syy;\n\t\t\t\t\tll tmp = sqrt((long double)del);\n\t\t\t\t\twhile (tmp * tmp < del) tmp++;\n\t\t\t\t\twhile (tmp * tmp > del) tmp--;\n\t\t\t\t\tif (tmp * tmp != del)\n\t\t\t\t\t\treturn ;\n\t\t\t\t\tcheck((Pt){2*syy, -sxy+tmp}, U, V);\n\t\t\t\t\tcheck((Pt){2*syy, -sxy-tmp}, U, V);\n\t\t\t\t}\n\t\t\t}\n\t\t\t*/\n\t\t}\n\t}\n\telse{\n\t\tif (sy != 0){\n\t\t\tcheck((Pt){sy, -sx}, U, V);\n\t\t}\n\t\telse{\n\t\t\tcheck((Pt){0ll, 1ll}, U, V);\n\t\t}\n\t}\n}\n\nvoid restore(){\n\tif (ans >= 1ll << 60)\n\t\treturn ;\n\tsort(ret.begin(), ret.end(), [](const Pt & a, const Pt & b){\n\t\tif (a.x == b.x) return a.y < b.y;\n\t\telse return a.x < b.x;\n\t});\n\tret.erase(unique(ret.begin(), ret.end()), ret.end());\n\tans = ret.size();\n}\n\nint vis[2222];\n\nvoid solve(){\n\tscanf(\"%d\", &n);\n\ta.resize(n);\n\tfor (int i = 0; i < n; i++)\n\t\tscanf(\"%lld%lld\", &a[i].x, &a[i].y);\n\tPt O;\n\tO.x = O.y = 0;\n\tfor (int i = 0; i < n; i++)\n\t\tO = O + a[i];\n\tif (O.x * 2 % n == 0 && O.y * 2 % n == 0){\n\t\tO.x = O.x * 2 / n;\n\t\tO.y = O.y * 2 / n;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif (a[i] * 2 == O){\n\t\t\t\tvis[i] = 1;\n\t\t\t}\n\t\t\telse if (!vis[i]){\n\t\t\t\tfor (int j = i + 1; j < n; j++)\n\t\t\t\t\tif (!vis[j] && a[i] + a[j] == O){\n\t\t\t\t\t\tvis[i] = 1, vis[j] = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t}\n\t\tvector c;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif (!vis[i]){\n\t\t\t\tc.push_back(a[i]);\n\t\t\t}\n\t\ta.swap(c);\n\t\tc.clear();\n\t\tn = a.size();\n\t}\n\tif (n == 0){\n\t\tputs(\"-1\");\n\t\treturn ;\n\t}\n\tvector b = convex_hull(a);\n\txzkk(b);\n\tans = 0;\n\tfor (auto &x : PP){\n\t\tif (x.first != x.second)\n\t\t\tgao(b[x.first], b[x.second]);\n\t}\n\trestore();\n\tif (ans >= 1ll << 60){\n\t\tputs(\"-1\");\n\t}\n\telse\n\t\tprintf(\"%lld\\n\", ans);\n}\n\nint main(){\n\tsolve();\n\treturn 0;\n} \n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "058726440aad091abd2df542926f4e40", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nconst int _=1e2;\nconst int maxn=2e3+_;\ninline LL gcd(LL x,LL y){return x==0?y:gcd(y%x,x);}\n\nstruct point{LL x,y;}p[maxn]; bool dl[maxn];\nLL mul(point p1,point p2){return p1.x*p2.x+p1.y*p2.y;}\nint len;LL d[maxn];\nmap,bool>mp;\nbool check(point p)\n{\n\tif(p.x!=0&&p.y!=0)\n\t{\n\t\tLL g=gcd(p.x,p.y);\n\t\tp.x/=g,p.y/=g;\n\t}\n\tif(p.x<0)p.x=-p.x,p.y=-p.y;\n\tif(p.x==0)p.y=1;\n\tif(p.y==0)p.x=1;\n\tif(mp.count(make_pair(p.x,p.y)))return true;\n\telse {mp[make_pair(p.x,p.y)]=true;return false;}\n}\nint main()\n{\n\tint n,sumx=0,sumy=0;\n\tscanf(\"%d\",&n);\n\tif(n==1){puts(\"-1\");return 0;}\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tscanf(\"%lld%lld\",&p[i].x,&p[i].y);\n\t\tsumx+=p[i].x;\n\t\tsumy+=p[i].y;\n\t\tp[i].x*=n,p[i].y*=n;\n\t}\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=i+1;j<=n;j++)\n\t\t\tif(p[i].x+p[j].x==2*sumx&&p[i].y+p[j].y==2*sumy)\n\t\t\t\tdl[i]=dl[j]=true;\n\tint tp=0;\n\tfor(int i=1;i<=n;i++)\n\t\tif(!dl[i])p[++tp]=p[i],p[tp].x-=sumx,p[tp].y-=sumy;\n\tn=tp;\n\tif(n==0){puts(\"-1\");return 0;}\n\t\n\tint ans=0;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tpoint u={p[1].y+p[i].y,-p[1].x-p[i].x};\n\t\tif(check(u))continue;\n\t\tlen=0;\n\t\tfor(int j=1;j<=n;j++)\n\t\t\td[++len]=mul(u,p[j]);\n\t\tsort(d+1,d+len+1);\n\t\tbool bk=false;\n\t\tfor(int j=1;j<=(n+1)/2;j++)\n\t\t\tif(d[1]+d[n]!=d[j]+d[n-j+1]){bk=true;break;}\n\t\tif(!bk)ans++;\n\t}\n\tprintf(\"%d\\n\",ans);\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "39d3b782e1617510b1099359f7917ac9", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \nusing namespace std;\nconst int N = 2002;\nconst double EPS = 1e-7;\ndouble d[N];\nint vis[N];\n\n\n\nstruct Point {\n double x, y;\n Point(){}\n Point(double x_, double y_) {\n x = x_;\n y = y_;\n }\n void read() {\n scanf(\"%lf %lf\", &x, &y);\n }\n void print() {\n printf(\"%.5f %.5f\\n\", x, y);\n }\n Point operator + (const Point &other) const {\n return Point(x+other.x, y+other.y);\n }\n Point operator - (const Point &other) const {\n return Point(x-other.x, y-other.y);\n }\n Point operator / (const double &val) const {\n return Point(x/val, y/val);\n }\n double operator * (const Point &other) const{\n return x*other.x + y*other.y; \n }\n double operator ^ (const Point &other) const {\n return x * other.y - y * other.x;\n }\n\n} p[N];\n\nstruct Line\n{ \n Point start, direct;\n Line() {}\n Line(Point start_, Point direct_) {\n start = start_;\n direct = direct_; \n } \n double dis(Point P) {\n return (direct ^ (P - start)) / sqrt(direct * direct);\n }\n bool same(Line L) {\n if (abs(direct ^ L.direct) > EPS) return 0;\n if (abs(direct ^ (L.start - start)) > EPS) return 0;\n return 1;\n }\n} l[N]; int cnt = 0;\n\nint n;\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i ++) \n p[i].read();\n\n if (n <= 2) {\n printf(\"-1\\n\"); return 0;\n }\n\n if (p[1]*p[1] < EPS) \n swap(p[1], p[2]);\n \n Point M(0, 0);\n for (int i = 1; i <= n; i ++) \n M = M + p[i];\n M = M / n;\n \n //M.print();\n\n int sym = 0, pos;\n for (int i = 1; i <= n; i ++) {\n int flag = 0;\n for (int j = 1; j <= n; j ++) {\n if ((p[j]+p[i])*(p[j]+p[i]) EPS) {\n ok = 0; break;\n }\n }\n if (ok) l[++cnt] = L;\n }\n\n //printf(\"%d\\n\", cnt);\n int ans = 0;\n for (int i = 1; i <= cnt; i ++) {\n int flag = 1;\n for (int j = 1; j < i; j ++) {\n if (l[i].same(l[j])) flag = 0;\n }\n if (flag) ans ++; \n }\n printf(\"%d\\n\", ans);\n\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "8c61d8e15d5b1c815acef308350b10bb", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n#define error(args...) { vector _v = split(#args, ','); err(_v.begin(), args); }\nvector split(const string& s, char c) { vector v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(move(x)); return v; }\nvoid err(vector::iterator it) {}\ntemplate void err(vector::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << \" = \" << a << '\\n'; err(++it, args...); }\n\n#define MAXN 2013\n#define MOD 1000000007\ntypedef long long LL;\nint N;\nLL X[MAXN], Y[MAXN];\nLL MX, MY;\nset > s1;\nunordered_multiset s;\nset > slopes;\n\nbool go(int p, int q) {\n\t// pair point 0 with point p\n\tLL nx = X[q] + X[p]; // norm vectors\n\tLL ny = Y[q] + Y[p];\n\tLL g = __gcd(nx, ny);\n\tnx /= g;\n\tny /= g;\n\tif (nx < 0 || (nx == 0 && ny < 0)) {\n\t\tnx = -nx;\n\t\tny = -ny;\n\t}\n\tpair pp = {nx, ny};\n\tif (slopes.count(pp)) return false;\n\tslopes.insert(pp);\n\n\ts.clear();\n\tfor (int i = 0; i < N; i++) {\n\t\tLL h = ny * X[i] - nx * Y[i];\n\t\tauto it = s.find(-h);\n\t\tif (it != s.end()) {\n\t\t\ts.erase(it);\n\t\t}\n\t\telse {\n\t\t\ts.insert(h);\n\t\t}\n\t}\n\treturn s.size() <= 1;\n}\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\n\t// freopen(\"input.txt\", \"r\", stdin);\n\n\tcin >> N;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> X[i] >> Y[i];\n\t\tMX += X[i];\n\t\tMY += Y[i];\n\t\tX[i] *= N;\n\t\tY[i] *= N;\n\t}\n\n\t// Step 1: Normalization\n\tfor (int i = 0; i < N; i++) {\n\t\tX[i] -= MX;\n\t\tY[i] -= MY;\n\t}\n\n\t// Step 2: Removal of \"useless\" points\n\tfor (int i = 0; i < N; i++) {\n\t\tpair np = {-X[i], -Y[i]};\n\t\tif (s1.count(np)) {\n\t\t\ts1.erase(np);\n\t\t}\n\t\telse {\n\t\t\ts1.insert(make_pair(X[i], Y[i]));\n\t\t}\n\t}\n\n\tN = 0;\n\tfor (pair p : s1) {\n\t\tX[N] = p.first, Y[N]=p.second;\n\t\tN++;\n\t}\n\n\tif (N <= 1) {\n\t\tcout << -1 << endl;\n\t\treturn 0;\n\t}\n\n\t// Step 3: Try pairing 0 with every point p\n\tint ans = 0;\n\tfor (int p = 0; p < N; p++) {\n\t\tif (go(0, p)) {\n\t\t\t++ans;\n\t\t}\n\t}\n\n\tcout << ans << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "ce259bba94be4f55e0fc57e70f1d358b", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nconst int N = 2005;\n\n#define double long double\n\nconst double ep = 1e-10;\n\nstruct pnt {\n\tdouble x, y;\n pnt() {x = y = 0;}\n pnt(double xx, double yy) {x = xx, y = yy;}\n pnt operator + (pnt a) {return pnt(x + a.x, y + a.y);}\n void in() {int xx, yy; cin >> xx >> yy; x = xx, y = yy;}\n double operator ^ (const pnt &a) const {return x*a.y-y*a.x;}\n bool operator < (const pnt &a) const {return x != a.x ? x < a.x : y < a.y;}\n bool int_pnt(pnt &a) {\n int xx = x + (x > 0 ? 0.5 : -0.5), yy = y + (y > 0 ? 0.5 : -0.5);\n if (abs(x-xx) < ep && abs(y-yy) < ep) {\n a = pnt(xx, yy);\n return 1;\n }\n return 0;\n }\n} p[N], M, MM;\n\nvector P;\n\nint n;\n\nmap h;\n\nbool opp[N];\n\nint main() {\n cin >> n;\n for (int i = 1; i <= n; i ++) {\n\t\tp[i].in();\n\t\tM = M + p[i];\n h[p[i]] = i;\n }\n M.x /= n, M.y /= n;\n MM.x = M.x * 2, MM.y = M.y * 2;\n for (int i = 1; i <= n; i ++) if (!opp[i]) {\n pnt pp = pnt(MM.x-p[i].x, MM.y-p[i].y), qq;\n if (pp.int_pnt(qq) && h.count(qq)) {\n int t = h[qq];\n opp[t] = 1; opp[i] = 1;\n }\n }\n for (int i = 1; i <= n; i ++) if (!opp[i]) P.push_back(p[i]);\n if (P.empty()) puts(\"-1\");\n else {\n pnt A = P[0];\n int ans = 0;\n vector D;\n for (auto x : P) {\n pnt AM = A + x;\n AM.x /= 2, AM.y /= 2;\n pnt d = pnt(M.x - AM.x, M.y - AM.y);\n vector pos, neg;\n for (auto y : P) {\n double l = d ^ pnt(y.x - AM.x, y.y - AM.y);\n if (abs(l) < ep) continue;\n if (l < 0) neg.push_back(-l);\n else pos.push_back(l);\n\n }\n if (neg.size() != pos.size()) continue;\n sort(neg.begin(), neg.end());\n sort(pos.begin(), pos.end());\n bool flag = 0;\n for (int i = 0; i < pos.size(); i ++) if (abs(pos[i]-neg[i]) > ep) flag = 1;\n if (!flag) D.push_back(d);\n }\n for (auto &x : D) {\n if (x.y < 0) x.y = -x.y, x.x = -x.x;\n if (x.y == 0 && x.x < 0) x.x = -x.x;\n }\n sort(D.begin(), D.end(), [](const pnt &x, const pnt &y) {\n return (x^y) > 0;\n\t\t});\n int sz = D.size();\n for (int i = 0, j; i < sz; i = j) {\n for (j = i; j < sz && (D[i]^D[j])==0; j ++);\n ans ++;\n }\n printf(\"%d\\n\", ans);\n }\n\treturn 0;\n}\n\n //hello", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "f7a16df936912281643f103a068277db", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define trav(a, x) for(auto& a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\n\ntemplate \nstruct Point {\n typedef Point P;\n T x, y;\n explicit Point(T x=0, T y=0) : x(x), y(y) {}\n bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }\n bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }\n P operator+(P p) const { return P(x+p.x, y+p.y); }\n P operator-(P p) const { return P(x-p.x, y-p.y); }\n P operator*(T d) const { return P(x*d, y*d); }\n P operator/(T d) const { return P(x/d, y/d); }\n T dot(P p) const { return x*p.x + y*p.y; }\n T cross(P p) const { return x*p.y - y*p.x; }\n T cross(P a, P b) const { return (a-*this).cross(b-*this); }\n T dist2() const { return x*x + y*y; }\n double dist() const { return sqrt((double)dist2()); }\n // angle to x-axis in interval [-pi, pi]\n double angle() const { return atan2(y, x); }\n P unit() const { return *this/dist(); } // makes dist()=1\n P perp() const { return P(-y, x); } // rotates +90 degrees\n P normal() const { return perp().unit(); }\n // returns point rotated 'a' radians ccw around the origin\n P rotate(double a) const {\n return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }\n};\n\ntypedef Point P;\npair ulHull(const vector

& S) {\n vi Q(sz(S)), U, L;\n iota(all(Q), 0); \n sort(all(Q), [&S](int a, int b){ return S[a] < S[b]; }); \n trav(it, Q) {\n#define ADDP(C, cmp) while (sz(C) > 1 && S[C[sz(C)-2]].cross(\\\n S[it], S[C.back()]) cmp 0) C.pop_back(); C.push_back(it);\n ADDP(U, <=); ADDP(L, >=);\n } \n return {U, L}; \n}\n\nvi convexHull(const vector

& S) {\n vi u, l; tie(u, l) = ulHull(S);\n if (sz(S) <= 1) return u;\n if (S[u[0]] == S[u[1]]) return {0};\n l.insert(l.end(), u.rbegin()+1, u.rend()-1);\n return l;\n}\n\n\npair neg(pair d){\n return make_pair(-d.first, -d.second);\n}\n\ntypedef Point P;\nint main(){\n LL n;\n cin >> n;\n pair a[n];\n pair sum = make_pair(0,0);\n for(int i = 0; i < n; i++){\n cin >> a[i].first >> a[i].second;\n sum.first += a[i].first;\n sum.second += a[i].second;\n a[i].first *= n;\n a[i].second *= n;\n }\n map, LL> g;\n for(int i = 0; i < n; i++){\n a[i].first -= sum.first;\n a[i].second -= sum.second;\n g[a[i]]++;\n //cout << a[i].first << \" \" << a[i].second << endl;\n }\n for(int i = 0; i < n; i++){\n if(g[a[i]] > 0 && g[neg(a[i])] > 0){\n g.erase(a[i]);\n g.erase(neg(a[i]));\n }\n }\n g.erase(make_pair(0,0));\n vector > pts;\n for(map, LL>::iterator it = g.begin(); it != g.end(); it++){\n if(it->second <= 0) continue;\n pts.push_back(it->first);\n }\n if(pts.size() == 0){\n cout << -1 << endl;\n return 0;\n }\n vector

z;\n for(int i = 0; i < pts.size(); i++){\n z.push_back(P(pts[i].first, pts[i].second));\n //cout << z[i].x << \" \" << z[i].y << endl;\n }\n\n vector hull = convexHull(z);\n\n vector

checks;\n for(int j = 0; j < hull.size(); j++){\n int nj = (j+1) % (hull.size());\n P diff = z[hull[nj]] - z[hull[j]];\n LL maxcross = 0;\n LL mincross = 0;\n for(int c = 0; c < z.size(); c++){\n maxcross = max(maxcross, diff.cross(z[c]));\n mincross = min(mincross, diff.cross(z[c]));\n }\n vector maxs;\n vector mins;\n for(int c = 0; c < hull.size(); c++){\n if(diff.cross(z[hull[c]]) == maxcross) maxs.push_back(hull[c]);\n if(diff.cross(z[hull[c]]) == mincross) mins.push_back(hull[c]);\n }\n for(int b = 0; b < maxs.size(); b++){\n for(int a = 0; a < mins.size(); a++){\n if(maxs[b] == mins[a]) continue;\n P del = z[maxs[b]] + z[mins[a]];\n LL gg = __gcd(del.x, del.y);\n del.x /= gg; del.y /= gg;\n if(del.x < 0 || (del.x == 0 && del.y < 0)){\n del.x *= -1;\n del.y *= -1;\n }\n checks.push_back(del);\n }\n }\n }\n map, int> fans;\n for(int j = 0; j < checks.size(); j++){\n map freq;\n for(int i = 0; i < z.size(); i++){\n freq[z[i].cross(checks[j])]++;\n }\n LL ok = 1;\n for(int i = 0; i < z.size(); i++){\n LL cur = z[i].cross(checks[j]);\n if(freq[cur] != freq[-cur]) ok = 0;\n }\n if(ok){\n fans[make_pair(checks[j].x, checks[j].y) ] = 1;\n }\n }\n cout << fans.size() << endl;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "35e6bf7582a97b165d1044182113dd1e", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\ntypedef double lf;\ntypedef pair pii;\ntypedef pair pdd;\n\nconst int MN = 2010;\n\nint N;\npii P[MN];\npdd C;\nvector tmp;\nint chk[MN];\n\nstruct Line {\n lf a, b, c;\n lf f(lf x, lf y) {\n return a * x + b * y + c;\n }\n};\n\nLine getLine(pdd p, pdd q) {\n return { (q.second - p.second), (p.first - q.first), (q.first - p.first) * p.second - (q.second - p.second) * p.first };\n}\n\nvector slope;\n\nint main() {\n scanf(\"%d\", &N);\n\n lf xsum = 0;\n lf ysum = 0;\n for(int i = 0; i < N; i++) {\n scanf(\"%d %d\", &P[i].first, &P[i].second);\n xsum += P[i].first;\n ysum += P[i].second;\n }\n\n C = { xsum / N, ysum / N };\n\n for(int i = 0; i < N; i++) {\n for(int j = i; j < N; j++) {\n if(abs(2 * C.first - P[i].first - P[j].first) < 1e-9 && abs(2 * C.second - P[i].second - P[j].second) < 1e-9) {\n chk[i] = 1;\n chk[j] = 1;\n break;\n }\n }\n }\n\n for(int i = 0; i < N; i++) if(!chk[i]) tmp.push_back(P[i]);\n N = tmp.size();\n for(int i = 0; i < N; i++) P[i] = tmp[i];\n if(!N) {\n printf(\"-1\");\n return 0;\n }\n\n for(int i = (N % 2? 0 : 1); i < N; i++) {\n pdd M = { (lf)(P[0].first + P[i].first) / 2, (lf)(P[0].second + P[i].second) / 2 };\n Line line = getLine(C, M);\n\n vector tmp;\n for(int j = 0; j < N; j++) {\n tmp.push_back(line.f(P[j].first, P[j].second));\n }\n sort(tmp.begin(), tmp.end());\n\n bool ok = true;\n for(int j = 0; j < N; j++) if(abs(tmp[j] + tmp[N - 1 - j]) > 1e-9) {\n ok = false;\n break;\n }\n if(ok) {\n if(line.a == 0) slope.push_back(1e9);\n else slope.push_back(line.b / line.a);\n }\n }\n\n sort(slope.begin(), slope.end());\n int ans = 0;\n for(int i = 0; i < slope.size(); i++) {\n if(i == 0 || abs(slope[i] - slope[i - 1]) > 1e-9) ans++;\n }\n\n printf(\"%d\", ans);\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "e6e903ec06f8e98f967b13ff06ba28c6", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nusing ll = long long;\n#define range(i,a,b) for(auto i=(a);i<(b);i++)\n#define rep(i,n) range(i,0,n)\n#define fst first\n#define snd second\n\ntemplate \nstruct Point {\nT x, y;\ninline Point & operator+=(const Point & o) { x += o.x, y += o.y; return *this; }\ninline Point operator+(const Point & o) const { auto p = *this; return p += o; }\ninline Point & operator-=(const Point & o) { x -= o.x, y -= o.y; return *this; }\ninline Point operator-(const Point & o) const { auto p = *this; return p -= o; }\ninline T operator*(const Point & o) { return x * o.x + y * o.y; }\ninline T operator%(const Point & o) const { return x * o.y - y * o.x; }\ninline bool operator==(const Point & o) const { return x == o.x && y == o.y; }\ninline bool operator<(const Point & o) const { return x < o.x || (x == o.x && y < o.y); }\ninline double r() const { return hypot(x, y); }\ninline T q() const { return atan2(y, x); }\n};\ntemplate inline Point operator*(T c, Point a) { return {c * a.x, c * a.y}; }\ntemplate inline Point operator-(Point a) { return (T) -1 * a; }\n\nconstexpr int N = 2005;\n\nPoint pts[N];\n\nbool symmetric(Point * p0, Point * p1, Point & l) {\n\tmap proj;\n\trange (pt, p0, p1) {\n\t\tll d = *pt % l;\n\t\tif (d > 0) proj[d]++;\n\t\telse if (d < 0) proj[-d]--;\n\t}\n\tfor (auto & p : proj) if (p.snd) return false;\n\treturn true;\n}\n\nint work() {\n\tint n;\n\tscanf(\"%d\", &n);\n\n\tPoint ctr {0, 0};\n\trep (i, n) {\n\t\tscanf(\"%lld%lld\", &pts[i].x, &pts[i].y);\n\t\tctr += pts[i];\n\t}\n\n\tset> mirror;\n\trep (i, n) {\n\t\tpts[i] = (ll) n * pts[i] - ctr;\n\t\tif (!mirror.erase(-pts[i])) mirror.insert(pts[i]);\n\t}\n\n\tn = 0;\n\tfor (auto & pt : mirror) pts[n++] = pt;\n\tif (n < 2) return -1;\n\n\tint ans = 0;\n\tset> ls;\n\trep (i, n) {\n\t\tPoint l = pts[0] + pts[i];\n\t\tll g = __gcd(abs(l.x), abs(l.y)) * (Point{0, 0} < l ? 1 : -1);\n\t\tl.x /= g, l.y /= g;\n\t\tif (ls.count(l)) continue;\n\t\tls.insert(l);\n\t\tans += symmetric(pts, pts + n, l);\n\t}\n\treturn ans;\n}\n\nint main() {\n\tprintf(\"%d\\n\", work());\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "4ef20ce963df1e2bd9742fa24501d2b2", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#define MAXN 2005\n#define eps 1e-8\nusing namespace std;\n\nconst double PI = acos(-1);\n\nstruct Point {\n double x, y;\n\n Point(): x(0), y(0) {}\n Point(double x, double y): x(x), y(y) {}\n\n friend Point operator+(const Point &A, const Point &B);\n friend Point operator/(const Point &A, double k);\n};\n\nPoint operator+(const Point &A, const Point &B) {\n return Point(A.x + B.x, A.y + B.y);\n}\n\nPoint operator/(const Point &A, double k) {\n return Point(A.x / k, A.y / k);\n}\n\nint n;\nPoint p[MAXN], mass;\nbool erased[MAXN];\nvector vp;\n\nbool equal(double x, double y) {\n return fabs(x - y) < eps;\n}\n\nstruct Comp {\n bool operator()(double a, double b) {\n if (equal(a, b))\n return 0;\n return a < b;\n }\n};\n\nmap posLines;\n\n\nPoint getVector(const Point &A, const Point &B) {\n Point v(A.x - B.x, A.y - B.y);\n\n if (v.y < 0)\n v.x = -v.x, v.y = -v.y;\n\n return v;\n}\n\nPoint getPerpendicularVector(const Point &v) {\n Point pv(-v.y, v.x);\n\n if (pv.y < 0)\n pv.x = -pv.x, pv.y = -pv.y;\n\n return pv;\n}\n\nPoint rotate(const Point &P, double alfa) {\n double c = cos(alfa);\n double s = sin(alfa);\n\n return Point(c * P.x + s * P.y, -s * P.x + c * P.y);\n}\n\nbool check(double alfa) {\n vector v;\n\n for (auto &P: vp)\n v.push_back(rotate(P, alfa).x);\n\n double massx = rotate(mass, alfa).x;\n\n sort(v.begin(), v.end());\n for (int i = 0, j = n - 1; i <= j; ++i, --j) {\n if (!equal(massx, (v[i] + v[j]) / 2))\n return 0;\n }\n\n return 1;\n}\n\nint main() {\n ios::sync_with_stdio(0);\n\n cin >> n;\n\n for (int i = 1; i <= n; ++i) {\n cin >> p[i].x >> p[i].y;\n mass = mass + p[i];\n }\n \n mass = mass / n;\n\n for (int i = 1; i <= n; ++i) {\n if (equal(p[i].x, mass.x) && equal(p[i].y, mass.y))\n erased[i] = 1;\n if (erased[i])\n continue;\n for (int j = i + 1; j <= n; ++j) {\n Point M = (p[i] + p[j]) / 2;\n\n if (equal(M.x, mass.x) && equal(M.y, mass.y))\n erased[i] = erased[j] = 1;\n }\n }\n\n for (int i = 1; i <= n; ++i) {\n if (!erased[i]) {\n vp.push_back(p[i]);\n }\n }\n\n if (vp.empty()) {\n cout << \"-1\\n\";\n return 0;\n }\n\n n = (int) vp.size();\n\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j < n; ++j) {\n Point M = (vp[i] + vp[j]) / 2;\n Point v = getPerpendicularVector(getVector(mass, M));\n\n double alfa = atan2(v.y, v.x);\n assert(alfa < -PI + eps || (alfa > -eps && alfa < PI + eps));\n if (alfa < -PI + eps)\n alfa = PI;\n\n ++posLines[alfa];\n }\n }\n\n int ans = 0;\n for (auto &entry: posLines) {\n if (entry.second >= n / 2) {\n ans += check(entry.first);\n }\n }\n\n cout << ans << \"\\n\";\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "6e76fc83f8186ad2eb0fa436196d6b32", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int maxn = 105;\ntypedef long long ll;\n\nll f[maxn >> 1];\nll cal(char c, int num) {\n//\tprintf(\"%c %d\\n\", c, num);\n\tif(c == 'A') return (num & 1) ? f[(num >> 1) + 1] : f[(num >> 1) - 1];\n\telse return (num & 1) ? f[(num >> 1) + 1] : f[(num >> 1) + 2];\n}\nint main() {\n\tios::sync_with_stdio(false); cin.tie(0);\n\tf[0] = 0, f[1] = 1;\n\tfor(int i = 2; i < (maxn >> 1); i++) f[i] = f[i - 1] + f[i - 2];\n\tstring s;\n\tcin >> s;\n\tint n = s.length();\n\tvector v;\n\tfor(int i = 0; i < n; i++) \n\t\tif(s[i] == s[(i - 1 + n) % n]) v.push_back(i);\n\tif(v.empty()) return 0 * printf(\"%lld\\n\", f[(n >> 1) - 1] + f[(n >> 1) + 1]);\n\tll ans = 1;\n\tv.push_back(v[0] + n);\n\tint sz = (int)v.size();\n\tfor(int i = 0; i < sz - 1; i++) ans *= cal(s[v[i]], v[i + 1] - v[i]);\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["combinatorics"], "code_uid": "4d1dd365b2e96a7a74812db387f695eb", "src_uid": "ad27d991516054ea473b384bb2563b38", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#define int long long\n#define endl '\\n'\n\nusing namespace std;\n\n\nint a1[1000000+5];\nvector > g;\nbool used[1000000+5];\nint timer, tin[1000000+5], fup[1000000+5];\n\nvector ans;\n\n\nvoid dfs (int v, int p = -1) {\n\tused[v] = true;\n\ttin[v] = fup[v] = timer++;\n\tint children = 0;\n\tfor (size_t i=0; i= tin[v] && p != -1)\n\t\t\t\tans.push_back(v);\n\t\t\t++children;\n\t\t}\n\t}\n\tif (p == -1 && children > 1)\n\t\tans.push_back(v);\n}\n\nvoid check(int v, int q, int t)\n{\n used[v]=1;\n if (v==q) return;\n a1[v] += t;\n for (auto i:g[v])\n {\n if (!used[i]) check(i,q,t);\n }\n\n\n}\n\nint32_t main() {\n cin.tie(0);\n cout.tie(0);\n ios_base::sync_with_stdio(false);\n\tint q;\n\tcin >> q;\n\n\tfor (int k=0; k> n >> m >> a >> b;\n a--;\n b--;\n g.clear();\n ans.clear();\n g.resize(n);\n for (int i=0; i> l >> r;\n l--;\n r--;\n g[l].push_back(r);\n g[r].push_back(l);\n }\n\n timer = 0;\n for (int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nvector used;\nvector used2;\nvector> g;\nint cnt = 0;\nint cnt2 = 0;\nint a, b;\n\nvoid dfs(int v)\n{\n used[v] = cnt;\n cnt2 += 1;\n for (int i = 0; i < g[v].size(); ++i)\n {\n if (used[g[v][i]] == -1 && g[v][i] != a && g[v][i] != b)\n {\n dfs(g[v][i]);\n }\n }\n}\n\nint main() {\n std::ios::sync_with_stdio(0);\n int t;\n cin >> t;\n vector comp;\n vector ar3;\n for (int q = 0; q < t; ++q)\n {\n int n, m;\n cin >> n >> m;\n cin >> a >> b;\n comp.resize(0);\n g.resize(n);\n cnt = 0;\n cnt2 = 0;\n for (int i = 0; i < n; ++i)\n {\n g[i].resize(0);\n }\n used = vector(n, -1);\n a -= 1;\n b -= 1;\n for (int i = 0; i < m; ++i)\n {\n int v, u;\n cin >> v >> u;\n v--, u--;\n g[v].push_back(u);\n g[u].push_back(v);\n }\n for (int i = 0; i < n; ++i)\n {\n cnt2 = 0;\n if (used[i] == -1 && i != a && i != b)\n {\n used[i] = cnt;\n dfs(i);\n comp.push_back(cnt2);\n cnt += 1;\n }\n }\n int64_t ans1 = 0;\n ar3 = vector(comp.size(), -1);\n int64_t ans2 = 0;\n for (int i = 0; i < g[a].size(); ++i)\n {\n if (g[a][i] == b)\n continue;\n else\n {\n if (ar3[used[g[a][i]]] == -1)\n {\n ans1 += comp[used[g[a][i]]];\n ar3[used[g[a][i]]] = 1;\n }\n }\n }\n for (int i = 0; i < g[b].size(); ++i)\n {\n if (g[b][i] == b)\n continue;\n else\n {\n if (ar3[used[g[b][i]]] == -1)\n {\n ans2 += comp[used[g[b][i]]];\n ar3[used[g[b][i]]] = 2;\n }\n else if (ar3[used[g[b][i]]] == 1)\n {\n ans1 -= comp[used[g[b][i]]];\n ar3[used[g[b][i]]] = 2;\n }\n }\n }\n cout << ans1 * ans2 << endl;\n }\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "440e8ddafb1ae418d673f0c8bcb98b14", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\n//ifstream in(\"input.txt\");\n//ofstream out(\"output.txt\");\n\nvoid dfs(vector>& g, vector& vis, int u, int& cnt)\n{\n\tcnt++;\n\tvis[u] = true;\n\tfor (int i = 0; i < g[u].size(); i++)\n\t{\n\t\tif (vis[g[u][i]] == false)\n\t\t\tdfs(g, vis, g[u][i], cnt);\n\t}\n}\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tint q; cin >> q;\n\tfor (int qq = 0; qq < q; qq++)\n\t{\n\t\tint n, m, a, b;\n\t\tcin >> n >> m >> a >> b;\n\t\tvector> g(n);\n\t\tint u = 0, v = 0;\n\t\tfor (int i = 0; i < m; i++)\n\t\t{\n\t\t\tcin >> u >> v;\n\t\t\tg[u - 1].push_back(v - 1);\n\t\t\tg[v - 1].push_back(u - 1);\n\t\t}\n\t\tvector vis(n, false);\n\t\tvis[a-1] = true; vis[b-1] = true;\n\t\tint cnt1 = 0;\n\t\tdfs(g, vis, a - 1, cnt1);\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tvis[i] = false;\n\t\tvis[a - 1] = true;\n\t\tvis[b - 1] = true;\n\t\tint cnt2 = 0;\n\t\tdfs(g, vis, b - 1, cnt2);\n\t\tint dif = cnt1 + cnt2 - n;\n\t\tcnt1 -= dif;\n\t\tcnt2 -= dif;\n\t\tcout << (long long)(cnt1 - 1) * (cnt2 - 1) << '\\n';\n\t}\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "858ce251906dccf0c99d044e111d9650", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n//typedef int ll;\n//typedef double ll;\n//ll MAX = LLONG_MAX;\nll MAX = 1e15;\n\n\nint main(){\n /*freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);*/\n ios_base::sync_with_stdio(0);\n\n ll q;\n cin >> q;\n while(q--){\n ll n,m,a,b;\n cin >> n >> m >> a >> b;\n a--;\n b--;\n vector> g(n);\n for(ll i=0; i> from >> to;\n from--;\n to--;\n g[from].push_back(to);\n g[to].push_back(from);\n }\n //BFS\n vector used(n,false);\n vector col(n,0);\n queue q;\n q.push(a);\n col[a]=1;\n while(!q.empty()){\n ll v = q.front();\n q.pop();\n for(ll i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define f first\n#define s second\n\nusing namespace std;\n\nconst int inf = 1e9 + 7;\nconst long long INF = 1e18 + 7;\nconst int maxn = 5e5 + 7;\n\nint q, n, m, tin[maxn], timer, fup[maxn], a, b, c[maxn];\nvector < int > g[maxn];\nbool used[maxn];\nint col[maxn];\n\nvoid dfsx(int v){\n\tused[v] = 1;\n\tcol[v] += 1;\n\tfor (auto to : g[v]){\n\t\tif (to != b && !used[to])\n\t\t\tdfsx(to);\n\t}\n}\n\nvoid dfsy(int v){\n\tused[v] = 1;\n\tcol[v] += 2;\n\tfor (auto to : g[v]){\n\t\tif (to != a && !used[to])\n\t\t\tdfsy(to);\n\t}\n}\n\n\nint main(){\n\tios_base::sync_with_stdio(NULL);\n\tcin.tie(0);\n\tcout.tie(0);\n\tcin >> q;\n\twhile (q--){\n\t\tcin >> n >> m >> a >> b;\n\t\ttimer = 1;\n\t\tfor (int i = 1; i <= n; i++)\n\t\t\tg[i].clear(), tin[i] = 0, fup[i] = 0, used[i] = 0, col[i] = 0, c[i] = 0;\n\t \tfor (int i = 1; i <= m; i++){\n\t \t\tint x, y;\n\t \t\tcin >> x >> y;\n\t \t\tg[x].pb(y);\n\t \t\tg[y].pb(x);\n\t \t}\n\t \tdfsx(a);\n\t \tmemset(used, 0, sizeof(used));\n\t \tdfsy(b);\n\t \tll x = 0, y = 0;\n\t \tfor (int i = 1; i <= n; i++){\n\t \t\tif (col[i] == 1)\n\t \t\t\tx++;\t\n\t \t\tif (col[i] == 2)\n\t \t\t\ty++;\n\t \t}\n\t \tcout << (x - 1) * (y - 1) << '\\n';\n\t}\n\treturn 0;\n} ", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "dea10a54d0797a79dda081039cdb6191", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\nlong long flag[200010];\nlong long tin[200010];\nlong long fup[200010];\nlong long color[200010];\nlong long answer1[200010];\nlong long answer2[200010];\nlong long tim;\nlong long n,m,a,b;\nvector v[200010];\n\nvoid dfs(long long x, long long predok) {\n color[x]=1;\n tin[x]=fup[x]=tim++;\n long long children=0;\n for (int i=0;i=tin[x] && predok!=-1)\n\t\t\t\tflag[x]=1;\n\t\t\t++children;\n\t\t}\n\t}\n if (predok==-1 && children>1) flag[x]=1;\n}\nvoid dfs2(long long x)\n{\n answer1[x]=1;\n if (x==b) return;\n for(int i=0;i>t;\n while(t>0){t--;\n cin>>n>>m>>a>>b;\n for(int i=1;i<=n;i++){\n v[i].clear();\n color[i]=0;\n flag[i]=0;\n fup[i]=0;\n tin[i]=0;\n tim=0;\n answer1[i]=0;\n answer2[i]=0;\n }\n for(int i=1;i<=m;i++){\n long long x,y;\n cin>>x>>y;\n v[x].push_back(y);\n v[y].push_back(x);\n }\n dfs(1,-1);\n if (flag[a]+flag[b]!=2){cout<<0<0) kol1++;\n if(answer2[i]>0) kol2++;\n }\n cout<\nusing namespace std;\ntypedef long long ll;\n#define f first\n#define s second\n#define pb push_back\n\nvoid bfs(ll s,ll f,ll n,ll c,vector &cols,vector > &g)\n{\n queue q;\n q.push(s);\n vector u(n+1,false);\n u[s]=true;\n while(!q.empty())\n {\n ll v=q.front();\n q.pop();\n if(v==f) continue;\n if(cols[v]!=0) cols[v]=37894;\n else cols[v]=c;\n for(int i=0;i>q;\n while(q--)\n {\n ll n,m,x,y;\n cin>>n>>m>>x>>y;\n vector > g;\nvector cols;\n g.resize(n+1,vector(0));\n cols.resize(n+1,0);\n for(int i=1;i<=m;i++)\n {\n ll from,to;cin>>from>>to;\n g[from].pb(to);\n g[to].pb(from);\n }\n /*cols[x]=-1;\n cols[y]=-1;*/\n bfs(x,y,n,1,cols,g);\n bfs(y,x,n,2,cols,g);\n ll sch1=0,sch2=0;\n for(int i=1;i<=n;i++)\n {\n if(cols[i]==1) sch1++;\n if(cols[i]==2) sch2++;\n }\n cout<<(sch1-1)*(sch2-1)<\n#include \n#include \n#include \n#include \n#include \n\n#define ll long long\n\nusing namespace std;\n\nvector> lists;\nll n, m, a, b;\nvector was;\nvector was1;\n\nvoid dfs(ll v) {\n if(!was[v]) {\n// cout << v << \" 1\" << endl;\n if(v != b) {\n was[v] = true;\n for(ll x : lists[v]) {\n dfs(x);\n }\n }\n }\n}\nvoid dfs1(ll v) {\n if(!was1[v]) {\n// cout << v << \" 2\" << endl;\n if(v != a) {\n was1[v] = true;\n for(ll x : lists[v]) {\n dfs1(x);\n }\n }\n }\n}\n\nvoid solve() {\n cin >> n >> m >> a >> b;\n --a;\n --b;\n lists = vector>(n, vector());\n was = vector(n, false);\n was1 = vector(n, false);\n for(ll i = 0; i < m; ++i) {\n ll x, y;\n cin >> x >> y;\n --x;\n --y;\n lists[x].push_back(y);\n lists[y].push_back(x);\n }\n dfs(a);\n dfs1(b);\n ll c1 = 0;\n ll c2 = 0;\n for(ll i = 0; i < n; ++i) {\n if(was[i] && (!was1[i])) {\n ++c1;\n } else if((!was[i]) && was1[i]) {\n ++c2;\n }\n }\n// cout << c1 << ' ' << c2;\n --c1;\n --c2;\n cout << c1 * c2 << '\\n';\n}\n\nint main() {\n ll t;\n cin >> t;\n for(ll i = 0; i < t; ++i) {\n solve();\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "faead521332575c9c3ee0b822fc349ca", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n\n#define int long long\n\nusing namespace std;\n\nint c[1000000+5];\n\nvector ctp;\n\nvector > g;\nbool used[1000000+5];\nint timer, tin[1000000+5], fup[1000000+5];\n\nvoid dfs (int v, int p = -1)\n{\n used[v] = true;\n tin[v] = fup[v] = timer++;\n int children = 0;\n for (size_t i=0; i= tin[v] && p != -1)\n ctp.push_back(v);\n ++children;\n }\n }\n if (p == -1 && children > 1)\n ctp.push_back(v);\n}\n\nvoid dfs1(int v, int q, int t)\n{\n used[v]=true;\n if (v==q) return;\n c[v]+=t;\n for (auto i:g[v])\n {\n if (!used[i]) dfs1(i,q,t);\n }\n}\n\nint32_t main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int q;\n cin>>q;\n for (int o=0; o>n>>m>>a>>b;\n a--;\n b--;\n g.clear();\n ctp.clear();\n g.resize(n);\n for (int i=0; i>l>>r;\n l--;\n r--;\n g[l].push_back(r);\n g[r].push_back(l);\n }\n timer = 0;\n for (int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n// DANGERZONE //\n// UNCOMMENT IN EXTREME SITUATIONS //\n// #define int long long\n// DANGERZONE //\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pli;\ntypedef pair pil;\ntypedef pair pll;\ntypedef unsigned long long ull;\n\n// DIRECTIVES END //\n\nconst ld pi = 3.1415926;\nconst int MOD = (int)(1e9) + 7;\nconst int INF = (int)(1e9) + 1e3;\nconst ll LINF = (ll)(1e18) + 1e6;\nconst int MAXN = (int)(1e5);\nconst int MAXQ = (int)(1e5);\n\nstruct dsu{\n\tvector p;\n\tvector sz;\n\tdsu(int n) : p(n + 1), sz(n + 1, 1) {\n\t\tfor (int i = 1; i < p.size(); ++i) {\n\t\t\tp[i] = i;\n\t\t}\n\t}\n\n\n\tint find_set(int v) {\n\t\tif (v == p[v]) {\n\t\t\treturn v;\n\t\t}\n\t\treturn p[v] = find_set(p[v]);\n\t}\n\n\tvoid union_set(int u, int v) {\n\t\tu = find_set(u);\n\t\tv = find_set(v);\n\t\tif (u == v) {\n\t\t\treturn;\n\t\t}\n\t\tif (sz[u] < sz[v]) {\n\t\t\tswap(u, v);\n\t\t}\n\t\tsz[u] += sz[v];\n\t\tp[v] = u;\n\t}\n};\n\nint n, m, a, b;\n\nvector used;\nvector > graph;\nvoid dfs(int v, dsu &d, int root) {\n\tused[v] = 1;\n\tfor (int u : graph[v]) {\n\t\tif (used[u] && u != root && v != root) {\n\t\t\td.union_set(u, v);\t\n\t\t} else if (!used[u]) {\n\t\t\tdfs(u, d, root);\n\t\t}\n\t}\n}\n\nvoid solve() {\n\tcin >> n >> m >> a >> b;\n\tdsu d1(n), d2(n);\n\tgraph.resize(n + 1);\n\tused.resize(n + 1);\n\tfor (int i = 0; i < m; ++i) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\tgraph[u].push_back(v);\n\t\tgraph[v].push_back(u);\n\t}\n\tdfs(a, d1, a);\n\tused.assign(n + 1, 0);\n\tdfs(b, d2, b);\n\tused.assign(n + 1, 0);\n\tll cnt1 = 0, cnt2 = 0;\n\tint tb1 = d1.find_set(b), tb2 = d2.find_set(a);\n\tfor (int i = 1; i <= n; ++i) {\n\t\tint p = d1.find_set(i);\n\t\tif (tb1 == p || p == a) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!used[p]) {\n\t\t\tused[p] = 1;\n\t\t\tcnt1 += d1.sz[p];\n\t\t}\n\t}\n\tused.assign(n + 1, 0);\n\tfor (int i = 1; i <= n; ++i) {\n\t\tint p = d2.find_set(i);\n\t\tif (p == tb2 || p == b) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!used[p]) {\n\t\t\tused[p] = 1;\n\t\t\tcnt2 += d2.sz[p];\n\t\t}\n\t}\n\tcout << cnt1 * cnt2 << '\\n';\n\tused.clear();\n\tgraph.clear();\n}\n\nvoid isolve() {}\n\nvoid generator() {}\n\nvoid validator() {}\n\nvoid stress() {}\n\nsigned main() {\n ios_base::sync_with_stdio(false);\n srand(time(nullptr));\n cout.setf(ios::fixed);\n cout.precision(10);\n cout.tie(nullptr);\n cin.tie(nullptr);\n#ifdef M8_LOCAL\n freopen(\"input.in\", \"r\", stdin);\n freopen(\"output.out\", \"w\", stdout);\n#endif\n // -----------------------\n\n int T = 1;\n cin >> T;\n while(T--) {\n solve();\n }\n\n // -----------------------\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "ebe8f9a9f2ebe189215a9c24397313d4", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef tuple tiii;\ntypedef tuple tiiii;\ntypedef set si;\ntypedef map mii;\ntypedef vector vi;\ntypedef vector vll;\ntypedef vector > vvi; \n#define F(i, a, b) for(int i = a; i <= (int)b; i++)\n#define f(i, a, b) for(int i = a; i >= (int)b; i--)\n#define F2(i, a, b) for(int i = a; i <= (int)b; i+=2)\n#define f2(i, a, b) for(int i = a; i >= (int)b; i-=2)\n#define wh(n) int iteration = n; while(iteration--) \n#define For(t, it) for(auto it = (t).begin(); it != (t).end(); ++it)\n#define IN insert\n#define PB push_back\n#define MP make_pair\n#define MT make_tuple\n#define RS resize\n#define GRAF(n) vvi gr; gr.resize(n+1); vector was; was.resize(n+1, 0);\n\nbool was[200007];\nvvi gr;\n\nint type, a, b;\nint sz;\nll N[4];\n\nvoid dfs(int v){\n\twas[v] = 1;\n\tsz++;\n\tF(i, 0, gr[v].size()-1){\n\t\tint to = gr[v][i];\n\t\tif(!was[to]){\n\t\t\tif(to == a)\n\t\t\t\tif(type / 2 == 0)\n\t\t\t\t\ttype += 2;\n\t\t\tif(to == b)\n\t\t\t\tif(type % 2 == 0)\n\t\t\t\t\ttype++;\n\t\t\tif(to != a && to != b)\n\t\t\t\tdfs(to);\n\t\t}\n\t}\n}\n\nll solve(){\n\tint n, m, c, d;\n\tcin >> n >> m >> a >> b;\n\tF(i, 0, 3)\n\t\tN[i] = 0;\n\tgr.clear();\n\tgr.RS(n+1);\n\tF(i, 1, n)\n\t\twas[i] = 0;\n\tF(i, 1, m){\n\t\tcin >> c >> d;\n\t\tgr[c].PB(d);\n\t\tgr[d].PB(c);\t\n\t}\n\tF(i, 1, n){\n\t \tif(i != a && i != b && !was[i]){\n\t \t\ttype = 0, sz = 0;\n\t \t\tdfs(i);\n\t \t\tN[type] += sz;\n\t \t}\n\t}\n\treturn N[1]*1ll*N[2];\n}\n\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n #ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n\t#endif\n\tint t;\n\tcin >> t;\n\twh(t)\n\t\tcout << solve() << '\\n';\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "7c7e7f90510c69b3c0eadcf8bdcf3786", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define int long long\n\nusing namespace std;\n\nint timer = 0, clr = 0, res1 = 0, res2 = 0;\nvector tin, up;\nset cp;\n\nvoid dfs(vector < vector > &g, vector &used, int v, int from = -1) {\n used[v] = 1;\n tin[v] = timer++;\n up[v] = tin[v];\n int cnt = 0;\n for (int to : g[v]) {\n if (!used[to]) {\n dfs(g, used, to, v);\n up[v] = min(up[v], up[to]);\n if (tin[v] <= up[to] && from != -1) {\n cp.insert(v);\n }\n cnt++;\n } else {\n if (to != from) {\n up[v] = min(tin[to], up[v]);\n }\n }\n }\n if (from == -1 && cnt > 1) {\n cp.insert(v);\n }\n}\n\nint cnt = 0, flag = 0;\n\nvoid dfs_a(vector < vector > &g, vector &used, int v, int a, int b) {\n if (v != a) cnt++;\n used[v] = 1;\n for (int to : g[v]) {\n if (v == a) flag = 0;\n if (to == b) {\n flag = 1;\n continue;\n }\n if (!used[to]) {\n dfs_a(g, used, to, a, b);\n if (v == a) {\n if (!flag) {\n res1 += cnt;\n }\n cnt = 0;\n flag = 0;\n }\n }\n }\n}\n\nvoid dfs_b(vector < vector > &g, vector &used, int v, int a, int b) {\n if (v != b) cnt++;\n used[v] = 1;\n for (int to : g[v]) {\n if (v == b) flag = 0;\n if (to == a) {\n flag = 1;\n continue;\n }\n if (!used[to]) {\n dfs_b(g, used, to, a, b);\n if (v == b) {\n if (!flag) {\n res2 += cnt;\n }\n cnt = 0;\n flag = 0;\n }\n }\n }\n}\n\nmain() {\n ios_base::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n int t;\n cin >> t;\n while (t--) {\n timer = 0, res1 = 0, res2 = 0;\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n cp.clear();\n tin.assign(n, 0);\n up.assign(n, 0);\n vector < vector > g(n);\n for (int i = 0; i < m; i++) {\n int c, d;\n cin >> c >> d;\n g[c - 1].push_back(d - 1);\n g[d - 1].push_back(c - 1);\n }\n vector used(n);\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n dfs(g, used, i);\n }\n }\n if (cp.find(a - 1) == cp.end() || cp.find(b - 1) == cp.end()) {\n cout << 0 << '\\n';\n continue;\n }\n used.assign(n, 0);\n dfs_a(g, used, a - 1, a - 1, b - 1);\n cnt = 0, flag = 0;\n dfs_b(g, used, b - 1, a - 1, b - 1);\n cout << res1 * res2 << '\\n';\n }\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "e30f3d6313b9b3de332e0b06b5322d64", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n\n//#pragma comment(linker, \"/STACK:16777216\")\n\nusing namespace std;\n\ntypedef long double C;\ntypedef complex P;\n\n#define X real()\n#define Y imag()\n\n#define ui unsigned int\n#define int long long\n#define mp make_pair\n#define timer fghge\n#define y1 yjyjyju\n#define have(X, a) (X.find(a) != X.end())\n#define Size(X) (int)X.size()\n#define all(X) (X).begin(), (X).end()\n#define endl '\\n'\nmt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count());\n\nstruct node {\n\tbool used = 0;\n\tvector edjes;\n};\nint ctr = 0;\nvoid dfs(int start, vector &g, int bad) {\n\tg[start].used = 1;\n\tctr++;\n\tfor (auto &i : g[start].edjes) {\n\t\tif (g[i].used == 1 || i == bad)\n\t\t\tcontinue;\n\t\tdfs(i, g, bad);\n\t}\n}\n\nsigned main() {\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0), cout.tie(0);\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"input.txt\", \"w\", stdout);\n\n\tint t; cin >> t;\n\twhile (t--) {\n\t\tint n, m, a, b;\n\t\tcin >> n >> m >> a >> b; a--; b--;\n\t\tvector g(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tint fir, sec; cin >> fir >> sec; fir--; sec--;\n\t\t\tg[fir].edjes.push_back(sec);\n\t\t\tg[sec].edjes.push_back(fir);\n\t\t}\n\t\tint fir, sec;\n\t\tfor (auto &i : g)\n\t\t\ti.used = 0;\n\t\tctr = 0;\n\t\tdfs(a, g, b);\n\t\tfir = n - ctr - 1;\n\t\tfor (auto &i : g)\n\t\t\ti.used = 0;\n\t\tctr = 0;\n\t\tdfs(b, g, a);\n\t\tsec = n - ctr - 1;\n\t\tcout << fir * sec << endl;\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "3958898144160d7d4215422448b85c28", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//#pragma comment(linker, \"/stack:200000000\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx\")\n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\t\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n//#define int ll\n//#define ull ll\n\nconst int Inf = 1e9 + 7;\n\ninline void boost_in_out()\n{\n\tios_base::sync_with_stdio(0);\n\tcin.tie(NULL);\n\tcout.tie(NULL);\n\tcerr.tie(NULL);\n}\n\nvector used;\nvector> g;\n\nvoid dfs(int v, int fb)\n{\n\tused[v] = 1;\n\tfor (int to: g[v])\n\t{\n\t\tif (!used[to] && to != fb)\n\t\t\tdfs(to, fb);\n\t}\n} \n\nsigned main()\n{\n\tboost_in_out();\n\tint t;\n\tcin >> t;\n\tfor (int iii = 0; iii < t; ++iii)\n\t{\n\t\tint n, m, a, b;\n\t\tcin >> n >> m >> a >> b;\n\t\tused.assign(n, 0);\n\t\tg.assign(n, vector());\n\t\t--a;\n\t\t--b;\n\t\tfor (int i = 0; i < m; ++i)\n\t\t{\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\t--x;\n\t\t\t--y;\n\t\t\tg[x].push_back(y);\n\t\t\tg[y].push_back(x);\n\t\t}\n\t\tdfs(a, b);\n\t\tll c1 = -1, c2 = -1;\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tif (!used[i])\n\t\t\t{\n\t\t\t\t++c1;\n\t\t\t}\n\t\t}\n\t\tused.assign(n, 0);\n\t\tdfs(b, a);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tif (!used[i])\n\t\t\t{\n\t\t\t\t++c2;\n\t\t\t}\n\t\t}\n\t\tcout << c1 * c2 << '\\n';\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "94cbc3fc69a262bce54f8dd2fbed7c01", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define int long long\nusing namespace std;\ntypedef vector vi;\n\nint n, m, a, b;\nvector< vector > d;\nvector used;\nvector to_a_no_b;\nvector to_b_no_a;\n\n\nvoid dfs_to_a(int v)\n{\n if (used[v] || v == b)\n return;\n\n used[v] = true;\n if (v != a) to_a_no_b[v] = true;\n\n auto roads = d[v];\n for (int i = 0; i < roads.size(); i++)\n dfs_to_a(roads[i]);\n}\n\n\nvoid dfs_to_b(int v)\n{\n if (used[v] || v == a)\n return;\n\n used[v] = true;\n if (v != b) to_b_no_a[v] = true;\n\n auto roads = d[v];\n for (int i = 0; i < roads.size(); i++)\n dfs_to_b(roads[i]);\n}\n\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int t;\n cin >> t;\n while (t--)\n {\n cin >> n >> m >> a >> b;\n d = vector< vector >(n+1, vector(0));\n for (int i = 0; i < m; i++)\n {\n int u, v;\n cin >> u >> v;\n d[u].push_back(v);\n d[v].push_back(u);\n }\n\n used = vector(n+1, false);\n to_a_no_b = vector(n+1, false);\n dfs_to_a(a);\n fill(used.begin(), used.end(), false);\n to_b_no_a = vector(n+1, false);\n dfs_to_b(b);\n\n int only_a = 0;\n int only_b = 0;\n for (int i = 1; i <= n; i++)\n {\n if (to_a_no_b[i] && !to_b_no_a[i])\n only_a++;\n else if (to_b_no_a[i] && !to_a_no_b[i])\n only_b++;\n }\n\n cout << only_a * only_b << '\\n';\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "4fecf5b2f24fdb555374649b7068c69c", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define int long long\n\nvector> g;\nvector used(2 * 1e5 + 1, 0);\nint a, b;\nint nw = 0;\nint nwsz = 0;\nint vb = 0, va = 0;\n\nvoid dfs(int v) {\n\tused[v] = nw;\n\tnwsz++;\n\tif (v == a) {\n\t\tva = 1;\n\t}\n\telse if (v == b) {\n\t\tvb = 1;\n\t}\n\tfor (auto u : g[v]) {\n\t\tif (used[u] < nw) {\n\t\t\tdfs(u);\n\t\t}\n\t}\n}\n\nsigned main() {\n\tint t;\n\tcin >> t;\n\tfor (int k = 0; k < t; k++) {\n\t\tint n, m;\n\t\tcin >> n >> m >> a >> b;\n\t\tg.clear();\n\t\tg.resize(n);\n\t\ta--;\n\t\tb--;\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tint u, v;\n\t\t\tcin >> u >> v;\n\t\t\tu--;\n\t\t\tv--;\n\t\t\tg[u].push_back(v);\n\t\t\tg[v].push_back(u);\n\t\t}\n\t\tnw++;\n\t\tint szb = 0;\n\t\tint sza = 0;\n\t\tva = 0;\n\t\tvb = 0;\n\t\tnwsz = 0;\n\t\tused[a] = nw;\n\t\tfor (auto u : g[a]) {\n\t\t\tif (used[u] < nw) {\n\t\t\t\tdfs(u);\n\t\t\t\tif (vb == 1) {\n\t\t\t\t\tvb = 0;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tsza += nwsz;\n\t\t\t\t}\n\t\t\t\tnwsz = 0;\n\t\t\t}\n\t\t}\n\t\tnw++;\n\t\tva = 0;\n\t\tvb = 0;\n\t\tnwsz = 0;\n\t\tused[b] = nw;\n\t\tfor (auto u : g[b]) {\n\t\t\tif (used[u] < nw) {\n\t\t\t\tdfs(u);\n\t\t\t\tif (va == 1) {\n\t\t\t\t\tva = 0;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tszb += nwsz;\n\t\t\t\t}\n\t\t\t\tnwsz = 0;\n\t\t\t}\n\t\t}\n\t\tcout << sza * szb << \"\\n\";\n\t}\n\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "dae295866f353251e513e98d4677cd40", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#define _CRT_SECURE_NO_WARNINGS\n#pragma warning (disable:4996)\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define all(x) x.begin(), x.end()\n#define allr(x) x.rbegin(), x.rend()\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef double long dl;\n\nconst int MAXN = (int)1e9 + 1;\nconst int INF = (int)1e9 + 7;\nconst ll MAXSIZE = 400000;\n\nvoid input() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tcout.tie(NULL);\n#ifdef _DEBUG\n\tfreopen(\"input\", \"r\", stdin);\n\tfreopen(\"output\", \"w\", stdout);\n#endif\n}\n\nll mod(ll x, ll m) {\n\treturn (x % m + m) % m;\n}\n\nvoid dfs(int v, const vector>& graph, vector& used, const bool a) {\n\tif (used[v] != 0)\n\t\treturn;\n\tif (a)\n\t\tused[v] = 1;\n\telse\n\t\tused[v] = 2;\n\tfor (auto i : graph[v]) {\n\t\tdfs(i, graph, used, a);\n\t}\n}\n\nint main() {\n\tinput();\n\tint q;\n\tcin >> q;\n\tfor (int pq = 0; pq < q; pq++) {\n\t\tint n, m, a, b;\n\t\tcin >> n >> m >> a >> b;\n\t\ta--;\n\t\tb--;\n\t\tvector > graph(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tint v1, v2;\n\t\t\tcin >> v1 >> v2;\n\t\t\tv1--, v2--;\n\t\t\tgraph[v1].insert(v2);\n\t\t\tgraph[v2].insert(v1);\n\t\t}\n\t\tvector used1(n, 0);\n\t\tvector used2(n, 0);\n\t\tused2[a] = 4;\n\t\tused1[b] = 4;\n\t\tdfs(a, graph, used1, true);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (used1[i] == 0)\n\t\t\t\tused1[i] = 2;\n\t\t}\n\t\tused1[a] = 5;\n\t\tdfs(b, graph, used2, false);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (used2[i] == 0)\n\t\t\t\tused2[i] = 1;\n\t\t}\n\t\tused2[b] = 5;\n\t\tvector cnt(3, 0);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (used1[i] == used2[i])\n\t\t\t\tcnt[used1[i]]++;\n\t\t}\n\t\tcout << cnt[1] * cnt[2] << '\\n';\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "aefe76b7f640279f4874fb50b4dba4cd", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "// copied by Nurstan Duisengaliev\n// skatal\n#include \n \n#define ll long long\n#define all(x) x.begin(), x.end()\n#define in insert\n#define mp make_pair\n#define F first\n#define S second\n#define ppf pop_front\n#define pb push_back\n#define ppb pop_back\n#define pf push_front\n#define pii pair \n#define boost() ios_base::sync_with_stdio(0), cin.tie(0)\n#define sz(x) x.size()\n#define int ll \nusing namespace std;\n \nconst int N = (int)2e5 + 123;\nconst int mod = (int)1e9 + 7;\nconst ll INF = (ll)1e18 + 7;\nvector g[N];\nset s[N];\nbool used[N];\nint tin[N], f[N], timer = 1; \nint len[N];\nint ans;\nint vv; \nint n, m, a, b; \nvoid dfs (int v, int p, int t) {\n\tused[v] = 1;\n\ttin[v] = timer ++;\n\tf[v] = tin[v];\n\tlen[v] = 1;\n\tif (v == b) vv = t;\n\tfor (int i = 0; i < sz (g[v]); i ++) {\n\t\tint to = g[v][i];\n\t\tif (used[to] == 0) {\n\t\t\tif (v != a) {\n\t\t\t\tdfs (to, v, t);\n\t\t\t}\n\t\t\telse dfs (to, v, to);\n\t\t\tlen[v] += len[to];\n\t\t\tf[v] = min (f[v], f[to]);\t\t\n\t\t\tif (v == b) {\n\t\t\t\tif (f[to] >= tin[v]) ans += len[to];\n\t\t\t}\n\t\t}\n\t\telse if (to != p) {\n\t\t\tf[v] = min (f[v], tin[to]);\n\t\t}\n\t}\n\n}\nvoid solve () {\n\tcin >> n >> m >> a >> b; \n\ttimer = 1;\n\tans = 0;\n\tfor (int i = 1; i <= n; i ++) {\n\t\tused[i] = 0;\n\t\tg[i].clear ();\n\t\ts[i].clear ();\n\t}\n\tfor (int i = 1; i <= m; i ++) {\n\t\tint l, r;\n\t\tcin >> l >> r;\n\t\tif (s[l].count (r) == 0) {\n\t\t\tg[l].pb (r);\n\t\t\tg[r].pb (l);\n\t\t\ts[l].in (r);\n\t\t\ts[r].in (l);\n\t\t}\n\t}\t\t\n\tdfs (a, -1, -1); \n\tcout << ans * (n - 1 - len[vv]) << endl;\n}\n \nmain () {\n//\tfreopen (\".in\", \"r\", stdin);\n//\tfreopen (\".out\", \"w\", stdout);\n\tboost ();\n\tint TT;\n cin >> TT;\n\twhile (TT --) {\n\t solve ();\n\t}\n\treturn 0;\t\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "eb9aedf650d0b3364032a2554244176f", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\nvector > g;\nvector used;\nset fs;\nset ss;\nint a, b;\n\nvoid dfsa(int v) {\n used[v] = true;\n for (auto &u : g[v]) {\n if (!used[u] && u != a) {\n dfsa(u);\n fs.insert(u);\n }\n }\n}\n\nvoid dfsb(int v) {\n used[v] = true;\n for (auto &u : g[v]) {\n if (!used[u] && u != b) {\n dfsb(u);\n ss.insert(u);\n }\n }\n}\n\nsigned main() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n, m;\n scanf(\"%d%d%d%d\",&n,&m,&a,&b);\n a--;\n b--;\n g.assign(n+1,{});\n used.assign(n+1,0);\n fs.clear();\n ss.clear();\n for (int i = 0; i < m; i++) {\n int u, v;\n scanf(\"%d%d\", &u,&v);\n g[u - 1].emplace_back(v - 1);\n g[v - 1].emplace_back(u - 1);\n }\n dfsb(a);\n used.assign(n+1,0);\n dfsa(b);\n int aa = 0, bb = 0;\n for (auto &e : fs) {\n if (ss.find(e) == ss.end()) {\n aa++;\n }\n }\n for (auto &e : ss) {\n if (fs.find(e) == fs.end()) {\n bb++;\n }\n }\n cout << 1LL * aa * bb << '\\n';\n }\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "fe59c9c8b64f4e316451e84da35cc4d4", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define ll long long\n#define ld long double\n#define eb emplace_back\n#define pb push_back\n#define pii pair\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rall(x) x.rbegin(),x.rend()\n#define mt make_tuple\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC target(\"avx2\")\n\n#define pcc pair\nusing namespace std;\n\nvector< vector > g;\nvector col;\nvector used;\n\nvoid dfs(int v, int c){\n\tcol[v]+=c;\n\tused[v] = 1;\n\tfor(auto t:g[v]){\n\t\tif(!used[t]) dfs(t,c);\n\t}\n}\n\nmain(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tint t;\n\tcin>>t;\n\twhile(t--){\n\t\tint n,m,a,b;\n\t\tcin>>n>>m>>a>>b;\n\t\ta--;b--;\n\t\tg.assign(n,vector());\n\t\tcol.assign(n,0);\n\t\tcol[a] = 100;\n\t\tcol[b] = 101;\n\t\tfor(int i = 0; i < m; i++){\n\t\t\tint x,y;\n\t\t\tcin>>x>>y;\n\t\t\tx--;y--;\n\t\t\tif(x!=a && x!=b){\n\t\t\t\tg[y].pb(x);\n\t\t\t}\n\t\t\tif(y!=a && y!=b){\n\t\t\t\tg[x].pb(y);\n\t\t\t}\n\t\t}\n\t\tused.assign(n,0);\n\t\tdfs(a,1);\n\t\tused.assign(n,0);\n\t\tdfs(b,2);\n\t\tll sz1 = 0;\n\t\tll sz2 = 0;\n\t\tfor(int i = 0; i < n; i++){\n\t\t\tif(col[i]==1) sz1++;\n\t\t\tif(col[i]==2) sz2++;\n\t\t}\n\t\tcout<\nusing namespace std;\n#define int long long \n#define ii pair \n#define app push_back\n#define all(a) a.begin(), a.end()\n#define bp __builtin_popcount\n#define ll long long\n#define mp make_pair\n#define f first\n#define s second\n#define lb lower_bound\n#define ub upper_bound\n#define Time (double)clock()/CLOCKS_PER_SEC\n\nconst int N = 2e5 + 7;\nvector g[N];\nbool used[N];\nint timer, tin[N], fup[N];\nint cnt[N], dp[N];\nbool inp[N];\nint color[N];\nint n, m, a, b;\nvoid dfs(int v, int p = -1) {\n used[v] = true;\n tin[v] = fup[v] = timer++;\n cnt[v] = 1;\n for (size_t i=0; i= tin[v]) {\n dp[v] += cnt[to];\n } \n }\n }\n}\nvector path, cur;\nvoid go(int u, int want) {\n cur.app(u);\n if (want == u) path = cur;\n used[u] = 1;\n for (int v : g[u]) \n if (!used[v])\n go(v, want);\n cur.pop_back();\n}\n\nbool tc = 0;\nvoid col(int u, int c) {\n used[u] = 1;\n if (color[u] != -1)\n tc = 1;\n color[u] = c;\n for (int v : g[u]) \n if (!inp[v] && !used[v])\n col(v, c);\n} \n\nsigned main() {\n #ifdef HOME\n freopen(\"input.txt\", \"r\", stdin);\n #else\n ios_base::sync_with_stdio(0); cin.tie(0);\n #endif\n int t;\n cin >> t;\n while (t--) {\n cin >> n >> m >> a >> b;\n\n timer = 0; tc = 0;\n for (int u = 1; u <= n; ++u) {\n cnt[u] = dp[u] = tin[u] = fup[u] = used[u] = inp[u] = 0;\n color[u] = -1;\n g[u].clear();\n } \n\n for (int i = 0; i < m; ++i) {\n int u, v;\n cin >> u >> v;\n g[u].app(v); g[v].app(u);\n } \n for (int i = 0; i <= n; ++i)\n used[i] = 0;\n go(a, b);\n for (int e : path) {\n inp[e] = 1;\n color[e] = 0;\n } \n\n for (int i = 0; i <= n; ++i)\n used[i] = 0;\n col(a, 1);\n for (int i = 0; i <= n; ++i)\n used[i] = 0;\n col(b, 2);\n\n for (int i = 0; i <= n; ++i)\n used[i] = 0;\n dfs(a);\n for (int i = 0; i <= n; ++i)\n used[i] = 0;\n dfs(b);\n\n cout << dp[a] * dp[b] << '\\n';\n } \n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "11c2628fed8e58fa97b13a4574c5eec1", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n\nusing namespace std;\n\n#define ll long long\n\nconst int inf = (int)1e9;\nconst int mod = (int)1e9 + 9;\n\nvector > e;\nvector used, tin, fup;\n\nint timer;\n\nset pp;\n\nvoid dfs(int v, int p = -1) {\n\n\tused[v] = 1;\n\ttin[v] = fup[v] = timer++;\n\n\tint children = 0;\n\n\tfor (int to: e[v]) {\n\n\t\tif (to == p){\n continue;\n\t\t}\n\t\tif (used[to]) {\n fup[v] = min(fup[v], tin[to]);\n\t\t}\n\t\telse {\n\t\t\tdfs (to, v);\n\t\t\tfup[v] = min(fup[v], fup[to]);\n\t\t\tif (fup[to] >= tin[v] && p != -1) {\n pp.insert(v);\n\t\t\t}\n\t\t\t++children;\n\t\t}\n\t}\n\n\tif (p == -1 && children > 1) {\n pp.insert(v);\n\t}\n\n return;\n}\n\nvector color;\n\nvoid dfs_a(int v, int b) {\n\n used[v] = 1;\n\n for(int to: e[v]) {\n\n if(used[to] == 1 || to == b) {\n continue;\n }\n\n ++color[to];\n dfs_a(to, b);\n }\n\n return;\n}\n\nvoid dfs_b(int v, int a) {\n\n used[v] = 1;\n\n for(int to: e[v]) {\n\n if(used[to] == 1 || to == a) {\n continue;\n }\n\n color[to] += 2;\n dfs_b(to, a);\n }\n\n return;\n}\n\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n\n int q;\n scanf(\"%d\", &q);\n\n while(q--) {\n\n int n, m, a, b;\n scanf(\"%d%d%d%d\", &n, &m, &a, &b);\n\n --a, --b;\n\n e.assign(n, vector (0, 0));\n while(m--) {\n\n int x, y;\n scanf(\"%d%d\", &x, &y);\n\n e[x - 1].push_back(y - 1);\n e[y - 1].push_back(x - 1);\n }\n\n timer = 0;\n used.assign(n, 0);\n tin.assign(n, 0);\n fup.assign(n, 0);\n dfs(0);\n\n if(pp.find(a) == pp.end() || pp.find(b) == pp.end()) {\n printf(\"0\\n\");\n continue;\n }\n\n color.assign(n, 0);\n\n used.assign(n, 0);\n dfs_a(a, b);\n\n used.assign(n, 0);\n dfs_b(b, a);\n\n int num_a = 0;\n int num_b = 0;\n\n for(int x: color) {\n if(x == 1) {\n ++num_a;\n }\n if(x == 2) {\n ++num_b;\n }\n }\n\n printf(\"%I64d\\n\", 1ll * num_a * num_b);\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "252ca4950e474bc702f0d30bebe9aeda", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\n\nusing namespace std;\n\n#define pb push_back\n#define ll long long\n\nvector res;\nvector> g;\nvector used_a, used_b, used;\nint a, b, color = 1;\n\nvoid dfs_a(int u) {\n\tif (!used_a[u]) {\n\t\tused_a[u] = 1;\n\t\tfor (auto e : g[u]) {\n\t\t\tif (e == b) continue;\n\t\t\tdfs_a(e);\n\t\t}\n\t}\n\treturn;\n}\n\nvoid dfs_b(int u) {\n\tif (!used_b[u]) {\n\t\tused_b[u] = 1;\n\t\tfor (auto e : g[u]) {\n\t\t\tif (e == a) continue;\n\t\t\tdfs_b(e);\n\t\t}\n\t}\n\treturn;\n}\n\nvoid dfs(int u) {\n\tif (!used[u]) {\n\t\tused[u] = color;\n\t\tfor (auto e : g[u]) {\n\t\t\tif (e == a || e == b || (used_a[e] && used_b[e])) continue;\n\t\t\tdfs(e);\n\t\t}\n\t}\n\treturn;\n}\n\nvoid go() {\n\tint n, m;\n\tcin >> n >> m >> a >> b;\n\ta--;\n\tb--;\n\tg.resize(n);\n\tused_a.assign(n, 0);\n\tused_b.assign(n, 0);\n\tused.assign(n, 0);\n\tfor (int i = 0; i < n; i++) {\n\t\tg[i].clear();\n\t}\n\tint c, d;\n\twhile(m--) {\n\t\tcin >> c >> d;\n\t\tc--;\n\t\td--;\n\t\tg[c].pb(d);\n\t\tg[d].pb(c);\n\t}\n\tdfs_a(a);\n\tdfs_b(b);\n\tint ans1 = 0;\n\tint ans2 = 0;\n//\tfor (int i = 0; i < n; i++) {\n//\t\tcout << used_a[i] << \" \";\n//\t}\n//\tcout << \"\\n\";\n//\tfor (int i = 0; i < n; i++) {\n//\t\tcout << used_b[i] << \" \";\n//\t}\n//\tcout << \"\\n\";\n\tfor (int i = 0; i < n; i++) {\n\t\tif (i == a || i == b) continue;\n\t\tif (used_a[i] == 0 && used_b[i] == 1) {\n\t\t\tans2++;\n\t\t}\n\t\tif (used_a[i] == 1 && used_b[i] == 0) {\n\t\t\tans1++;\n\t\t}\n\t}\n\tres.pb( (long long) ans1 * ans2);\n}\n\nsigned main() {\n\tint t;\n\tcin >> t;\n\twhile (t--) {\n\t\tgo();\n\t}\n\tfor (auto e : res) {\n\t\tcout << e << \"\\n\";\n\t}\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "f5b892f9bff6a713d85e11c15f3fb6dc", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define Solved int main()\n#define pb push_back\n#define mp make_pair\n#define F first\n#define S second\n#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)\n#define forn(i, x) for(int i = 1; i <= x; i++)\n#define rep(i, x, y) for(int i = x; i <= y; i++)\n#define sz(x) (int)x.size()\n#define pii pair\n#define pll pair\n#define ll long long\n#define ull unsigned long long\n#define ld long double\n#define mii map\n#define all(x) x.begin(), x.end()\n#define lb lower_bound\n#define ub upper_bound\nusing namespace std;\n\ninline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}\n\nconst int maxn = 1e9;\nconst ll inf = 1e18;\n//const int mod = 998244353;\nconst int mod = 1e9 + 7;\nconst int N = 2e5 + 10;\n\nint n, m, a, b;\nint was[N];\nvector g[N];\n\nvoid dfs(int v, int t) {\n was[v] += t;\n if (t == 1 && v == b) return;\n if (t == 2 && v == a) return;\n\n for (auto to: g[v]) {\n if (!was[to] || (t == 2 && was[to] == 1)) {\n dfs(to, t);\n }\n }\n}\n\nvoid solve() {\n cin >> n >> m >> a >> b;\n\n\n memset(was, 0, sizeof was);\n for (int i = 1; i <= n; i++)\n g[i].clear();\n\n for (int i = 1; i <= m; i++) {\n int x, y;\n cin >> x >> y;\n g[x].pb(y);\n g[y].pb(x);\n }\n\n dfs(a, 1);\n dfs(b, 2);\n\n int l = 0, r = 0;\n for (int i = 1; i <= n; i++) {\n if (was[i] == 1 && i != a && i != b) l++;\n else if (was[i] == 2 && i != a && i != b) r++;\n }\n\n cout << l * 1ll * r;\n}\n\nSolved {\n SpeedForce;\n\tint tt = 1;\n cin >> tt;\n\twhile (tt--) {\n\t\tsolve();\n\t\tcout << '\\n';\n\t}\n\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "8fe42e3ea41cd4f44039991afac0ea14", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"mmx,sse,sse2,sse3,ssse3,sse4,avx,aes,pclmul,sse4.1,sse4.2\")\n\n#include\n#define _CRT_SECURE_NO_WARNINGS\n#define _CRT_DISABLE_PERFCRIT_LOCKS\n#define cdbg cerr << \"Debug in \" << __func__ << \", \" << __LINE__ << \": \"\n#define q << '\\n'\n#define sp << ' '\n#define mp make_pair\n#define pb emplace_back\n#define ft first\n#define sd second\n#define ld long double\n#define ll long long\n#define ull unsigned long long\n#define lll __int128\n#define pii pair\n#define vi vector\n#define y1 trymaxlox\n\nusing namespace std;\n\nconst int N = 5e5;\nconst ll INF = 1e18 + 7;\nconst int MOD = 1e9+7;\n\nll n, m, k, a, b, c, d, arr[N], res, kek, t ;\nset st;\nstring str;\nvi ans, v;\nset g[N];\nbool used[N], used2[N];\n\nvoid del(int v)\n{\n if (v == a || v == b) return;\n // cdbg << v q;\n used2[v] = 1;\n vi vv;\n for (int it : g[v])\n {\n if (!used2[it])\n {\n if (it != a && it != b) del(it);\n vv.pb(it);\n }\n }\n for (int i : vv)\n {\n g[v].erase(i);\n g[i].erase(v);\n }\n\n}\nvi vv;\nbool dfsdel(int a, int v, int b, int p)\n{\n // cdbg << v q;\n used[v] = 1;\n bool f = 0, ff = 0;\n// cout << \"! ! \";\n// for (auto it : g[v]) cout << it sp;\n// cout q;\n for (auto it : g[v])\n {\n //cdbg << \" \" << it q;\n if (!used[it])\n {\n if (it == b)\n {\n f = 1;\n if (v != a) ff = 1;\n }\n else\n {\n dfsdel(a, it, b, v);\n }\n }\n }\n if (f && v != a && v != b)\n {\n vv.pb(v);\n }\n else if (f)\n {\n g[a].erase(b);\n g[b].erase(a);\n }\n //cdbg << v sp << f q;\n return f;\n}\n\nll dfs(int v)\n{\n // cdbg << v q;\n used[v] = 1;\n ll ans = 0;\n for (auto it : g[v])\n {\n if (!used[it])\n {\n ans += dfs(it);\n }\n }\n return ans + 1;\n}\nmain()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n cin >> t;\n while (t--)\n {\n cin >> n >> m >> a >> b;\n for (int i = 0;i <= n; i++)\n {\n g[i].clear();\n }\n // cdbg << \"a\" q;\n vv.clear();\n memset(used, 0, sizeof(used));\n memset(used2, 0, sizeof(used2));\n ll x , y;\n for (int i = 0; i < m; i++)\n {\n cin >> x >> y;\n g[x].insert(y);\n g[y].insert(x);\n }\n //cdbg << \"aaa\" q;\n dfsdel(a, a, b, a);\n for (auto it : vv)\n {\n // cdbg << it q;\n del(it);\n }\n memset(used, 0, sizeof(used));\n ll cnta = dfs(a);\n cnta--;\n memset(used, 0, sizeof(used));\n ll cntb = dfs(b);\n cntb--;\n cout << cnta * cntb q;\n }\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n/*\n3\na???cb\na??bbc\na?b?c\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "a336df6e9a8855106517aa8f6b0312c8", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n\n#define pb push_back\n#define int long long\n\nusing namespace std;\n\nvector usd;\nvector < vector > g;\nint cnt = 0;\n\nvector used;\n\nvoid dfs(int v, int zapr) {\n if(v == zapr) return;\n usd[v] = 1;\n for(auto u : g[v]) {\n if(u == zapr) continue;\n if(!usd[u]) {\n dfs(u, zapr);\n }\n }\n}\n\nsigned main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n int t; cin >> t; while(t--) {\n int n, m;\n cin >> n >> m;\n int a, b;\n cin >> a >> b; a--; b--;\n g.assign(n, vector (0));\n for(int i = 0; i < m; ++i) {\n int u, v;\n cin >> u >> v;\n v--;u--;\n g[u].pb(v);\n g[v].pb(u);\n }\n usd.assign(n, 0);\n dfs(a, b);\n used = usd;\n usd.assign(n, 0);\n dfs(b, a);\n int x = 0, y = 0;\n// for(auto i : usd) cout << i << \" \"; cout << \"\\n\";\n// for(auto i : used) cout << i << \" \"; cout << \"\\n\";\n for(int i = 0; i < n; ++i) {\n if(i == a || i == b) continue;\n if(usd[i] && !used[i]) {\n x++;\n } else {\n if(used[i] && !usd[i]) {\n y++;\n }\n }\n }\n// cout << x << \" \" << y << \"\\n\";\n cout << x * y << \"\\n\";\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "837a5f7302e8771aacbca5bcfb4c30ed", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define int long long\n#define mp make_pair\n#define eb emplace_back\n#define pb push_back\n#define pf push_front\n#define x first\n#define y second\n\ntypedef long long ll;\n\nusing namespace std;\n\nconst int inf = 1e10;\nconst int M = 998244353;\nconst long double pi = 3.14159265358979323846;\n\nvoid dfs(int v, vector &used, vector> &sl, vector &cmp){\n used[v] = true;\n cmp.eb(v);\n for(int u: sl[v]){\n if(!used[u])dfs(u, used, sl, cmp);\n }\n}\n\nmain(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int t;\n cin >> t;\n for(; t; --t){\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n --a;\n --b;\n vector ia(n), ib(n);\n vector> sl(n);\n for(int i = 0; i < m; ++i){\n int u, v;\n cin >> u >> v;\n --u;\n --v;\n if(u == a){\n ia[v] = true;\n }\n if(u == b){\n ib[v] = true;\n }\n if(v == b){\n ib[u] = true;\n }\n if(v == a){\n ia[u] = true;\n }\n if(v != a && v != b && u != a && u != b){\n sl[v].eb(u);\n sl[u].eb(v);\n }\n }\n vector> cmp(n);\n int c = 0;\n vector used(n);\n for(int i = 0; i < n; ++i){\n if(!used[i] && i != a && i != b){\n dfs(i, used, sl, cmp[c++]);\n }\n }\n vector ta(cmp.size()), tb(cmp.size());\n int ra = 0, rb = 0;\n for(int i = 0; i < c; ++i){\n for(int e: cmp[i]){\n ta[i] = ta[i] || ia[e];\n tb[i] = tb[i] || ib[e];\n }\n if(tb[i] && ta[i])continue;\n if(tb[i])rb += cmp[i].size();\n else ra += cmp[i].size();\n }\n cout << ra * rb << \"\\n\";\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "18096ed53b7fd157c94f665a35482bb5", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n\n#pragma comment(linker, \"/stack:200000000\")\n#pragma GCC optimize(\"Ofast, no-stack-protector, unroll-loops, fast-math\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n\nmt19937 gen(chrono::steady_clock::now().time_since_epoch().count());\n\nll INF = (ll) 1e20;\nint iINF = (int) 1e20;\nll mod = (ll) 1e9 + 7;\n\nvoid calc(ll v, vector> &nodes, ll not_to_go, vector &used) {\n used[v] = true;\n for (auto i : nodes[v]) {\n if (!used[i] && i != not_to_go) {\n calc(i, nodes, not_to_go, used);\n }\n }\n}\n\nsigned main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ll t;\n cin >> t;\n while (t--) {\n ll n, m, a, b;\n cin >> n >> m >> a >> b;\n --a, --b;\n vector> nodes(n);\n for (int i = 0; i < m; ++i) {\n ll u, v;\n cin >> u >> v;\n nodes[u - 1].push_back(v - 1);\n nodes[v - 1].push_back(u - 1);\n }\n vector usd1(n, false), usd2(n, false);\n calc(a, nodes, b, usd1);\n calc(b, nodes, a, usd2);\n ll nm1 = -1, nm2 = -1;\n for(auto i : usd1) {\n nm1 += (i == false);\n// cout << i << ' ';\n }\n// cout << '\\n';\n for(auto i : usd2) {\n nm2 += (i == false);\n }\n cout << nm1 * nm2 << '\\n';\n }\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "b067c37efea1362ad7c823c6740d102e", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define int long long\n#define ll long long\n#define ull unsigned long long\n#define ff first\n#define ss second\n#define pb push_back\n#define mk make_pair\n#define ld long double\n#define y1 asdfghjkl\n#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\nusing namespace std;\n\nmt19937_64 mt_rnd_64(chrono::steady_clock::now().time_since_epoch().count());\nlong long rnd(long long l, long long r){\n return (mt_rnd_64() % (r-l+1)) + l;\n}\n\nconst int N = 2 * 1e5 + 5;\n\nint q;\nint n,m,a,b,u,v,cnt,X,Y;\nbool f1,f2;\nvectorg[N];\nint tin[N];\nint tout[N];\nint fup[N];\nbool used[N];\nint timer;\nvectorc[N];\nbool w[N];\nbool ww[N];\n\nvoid dfs(int v,int p,int call)\n{\n used[v] = 1;\n tin[v] = fup[v] = ++timer;\n for(int i = 0;i < g[v].size();i++){\n int to = g[v][i];\n if(used[to])fup[v] = min(fup[v],tin[to]);\n else{\n call++;\n dfs(to,v,call);\n fup[v] = min(fup[v],fup[to]);\n if(fup[to] >= tin[v]){\n if(call > 1){\n if(v == a || v == b)cnt++;\n }\n }\n }\n }\n}\n\nvoid dfs2(int v)\n{\n used[v] = 1;\n c[timer].pb(v);\n for(int i = 0;i < g[v].size();i++){\n int to = g[v][i];\n if(!used[to])\n dfs2(to);\n }\n}\n\nmain()\n{\n ufo;\n cin>>q;\n for(int Q = 0;Q < q;Q++){\n cin>>n>>m>>a>>b;\n map,bool >mp;\n for(int i = 1;i <= n;i++){\n c[i].clear();\n g[i].clear();\n tin[i] = 0;\n tout[i] = 0;\n fup[i] = 0;\n used[i] = 0;\n w[i] = 0;\n ww[i] = 0;\n }\n for(int i = 0;i < m;i++){\n cin>>u>>v;\n if(mp.count(mk(u,v)))continue;;\n g[u].pb(v);\n g[v].pb(u);\n mp[mk(u,v)] = 1;\n mp[mk(v,u)] = 1;\n }\n cnt = 0;\n for(int i = 1;i <= n;i++)\n if(!used[i])\n dfs(i,-1,0);\n if(cnt < 2){\n cout<<0<<'\\n';\n continue;\n }\n for(int i = 0;i < g[a].size();i++)w[g[a][i]]=1;\n for(int i = 0;i < g[b].size();i++)ww[g[b][i]]=1;\n for(int i = 1;i <= n;i++)used[i] = 0;\n used[a] = 1;\n used[b] = 1;\n timer = 0;\n for(int i = 1;i <= n;i++){\n if(!used[i]){\n timer++;\n dfs2(i);\n }\n }\n X = 0;\n Y = 0;\n for(int i = 1;i <= timer;i++){\n f1=0;\n f2=0;\n for(int j = 0;j < c[i].size();j++){\n v = c[i][j];\n if(w[v])f1 = 1;\n if(ww[v])f2 = 1;\n }\n if(f1 == f2)continue;\n if(f1)X += c[i].size();\n if(f2)Y += c[i].size();\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define int long long\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pll;\ntypedef pair pii;\ntypedef long double ld;\ntypedef double db;\n\nconst ll N = 1e6 + 20;\nconst ll INF = INT_MAX;\n\n/*int p = 3571;\nint mod = 2147483647;\n\nnamespace op {\n\tvoid add(int &a, int val) {\n\t\ta = ((a * p % mod + val) % mod + mod) % mod;\n\t}\n\tint calc(int a, int b) {\n\t\treturn ((a % mod + b % mod) % mod + mod) % mod;\n\t}\n\tint mul(int a, int b) {\n\t\treturn (((a % mod) * (b % mod)) % mod + mod) % mod;\n\t}\n\tint pow(int a, int n) {\n\t\tif (n == 0) return 1LL;\n\t\tif (n & 1) {\n\t\t\tint b = pow(a, n ^ 1) % mod;\n\t\t\treturn mul(a, b);\n\t\t}\n\t\telse {\n\t\t\tint b = pow(a, n >> 1) % mod;\n\t\t\treturn mul(b, b);\n\t\t}\n\t}\n};\nusing namespace op;\n\nstruct hsh {\n\tstring vec;\n\tvector seg;\n\tint n;\n\thsh(string a) {\n\t\tn = a.size();\n\t\tvec = a;\n\t\tseg.resize(a.size() + 1);\n\t\tfor (int i = 0; i < a.size(); ++i) {\n\t\t\tseg[i + 1] = seg[i];\n\t\t\tadd(seg[i + 1], a[i]);\n\t\t}\n\t}\n\tint seg_hash(int l, int r) {\n\t\treturn calc(seg[r], (-1) * seg[l - 1] * pow(p, r - l + 1));\n\t}\n};*/\n\nvector g[N];\nint used[N], ct[N];\n\nvector comp[2];\n\nint id;\nvoid dfs(ll v) {\n\tcomp[id].push_back(v);\n\tused[v] = 1;\n\n\tfor (ll to : g[v]) {\n\t\tif (!used[to])\n\t\t\tdfs(to);\n\t}\n}\n\nvoid clear(int n) {\n\tfor (ll i = 0; i < n; ++i) {\n\t\tg[i].clear();\n\t\tused[i] = ct[i] = 0;\n\t}\n\tcomp[0].clear();\n\tcomp[1].clear();\n}\n\nsigned main() {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n#else\n#endif\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0); cout.tie(0);\n\n\tint t;\n\tcin >> t;\n\twhile (t--) {\n\t\tint n, m, a, b;\n\t\tcin >> n >> m >> a >> b;\n\t\ta--; b--;\n\t\tfor (int i = 0; i < m; ++i) {\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\t--x; --y;\n\t\t\tg[x].push_back(y);\n\t\t\tg[y].push_back(x);\n\t\t}\n\n\t\tid = 0; used[b] = 1;\n\t\tdfs(a);\n\t\tfor (auto &x : comp[0]) {\n\t\t\tused[x] = 0;\n\t\t}\n\t\tused[b] = 0;\n\t\tid = 1; used[a] = 1;\n\t\tdfs(b);\n\t\tfor (auto &x : comp[0])\n\t\t\tct[x]++;\n\t\tfor (auto &x : comp[1])\n\t\t\tct[x]++;\n\n\t\tint arr[2] = { -1,-1 };\n\t\tfor (int i = 0; i < 2; ++i)\n\t\t\tfor (auto &x : comp[i])\n\t\t\t\tif (ct[x] == 1)\n\t\t\t\t\tarr[i]++;\n\n\t\tcout << max(0ll, arr[0] * arr[1]) << endl;\n\t\tclear(n);\n\t}\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "ba3bf7001c13f1cf81ebf721d7963ff2", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\n\nvector> g;\nvector used;\nint coun = 0;\nvoid dfs(int v, int nun){\n used[v] = 1;\n coun++;\n for (auto u : g[v]){\n if (used[u]) continue;\n if (u == nun) continue;\n dfs(u, nun);\n }\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int t;\n cin >> t;\n for (int i = 1; i <= t; i++){\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n g.clear();\n used.clear();\n g.resize(n + 1);\n used.resize(n + 1);\n for (int j = 1; j <= m; j++){\n int l, r;\n cin >> l >> r;\n g[l].push_back(r);\n g[r].push_back(l);\n }\n\n dfs(a, b);\n coun = 0;\n dfs(b, 0);\n int c1 = coun - 1; coun = 0;\n\n used.clear();\n used.resize(n + 1);\n swap(a, b);\n\n dfs(a, b);\n coun = 0;\n dfs(b, 0);\n int c2 = coun - 1; coun = 0;\n\n cout << (long long)c1 * c2 << \"\\n\";\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "79cb357eab1150bd0aef9c6370be0945", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#pragma GCC optimize(\"O3\")\n#define int long long\n\nusing namespace std;\n\nconst int MAXN = 200005;\n\nvector g[MAXN];\nset cpt;\nbool used[MAXN];\nint timer=1, tin[MAXN], fup[MAXN];\n\nvoid dfs (int v, int p = -1) {\n\tused[v] = true;\n\ttin[v] = fup[v] = timer++;\n\tint children = 0, cut = 0;\n\tfor (auto to : g[v]) {\n\t\tif (!used[to]) {\n children++;\n dfs(to, v);\n fup[v] = min(fup[to], fup[v]);\n if ((v == 0 && children > 1 || v != 0 && fup[to] >= tin[v]) && !cut++) cpt.insert(v);\n\t\t}\n\t\telse fup[v] = min(tin[to], fup[v]);\n\t}\n}\n\nbool found = 0;\nint a, b, sz = 0;\n\nvoid dfs2(int v){\n if(v == b) found = 1;\n used[v] = 1;\n sz++;\n for(auto u : g[v]){\n if(!used[u] && u != a) dfs2(u);\n\n }\n}\n\nvoid dfs3(int v){\n if(v == a) found = 1;\n used[v] = 1;\n sz++;\n for(auto u : g[v]){\n if(!used[u] && u != b) dfs3(u);\n\n }\n}\n\nsigned main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n\tint n;\n\tint t; cin >> t;\n\twhile(t--){\n int n, m;\n cin >> n >> m >> a >> b; a--, b--;\n memset(tin, 0, 8 * (n + 1));\n memset(fup, 0, 8 * (n + 1));\n for(int i = 0; i < n; ++i) g[i].clear();\n memset(used, 0, sizeof(bool) * (n + 1));\n cpt.clear();\n timer = 1;\n while(m--){\n int u, v; cin >> u >> v; u--, v--;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n dfs(0);\n if(cpt.find(a) == cpt.end() || cpt.find(b) == cpt.end()){cout << \"0\\n\"; continue;}\n memset(used, 0, sizeof(bool) * (n + 1));\n int sz1 = 0;\n for(int i = 0; i < g[a].size(); ++i){\n found = 0, sz = 0;\n if(!used[g[a][i]]){\n dfs2(g[a][i]);\n if(!found) sz1 += sz;\n }\n }\n //cout << sz1 << '\\n';\n memset(used, 0, sizeof(bool) * (n + 1));\n int sz2 = 0;\n for(int i = 0; i < g[b].size(); ++i){\n found = 0, sz = 0;\n if(!used[g[b][i]]){\n dfs3(g[b][i]);\n if(!found) sz2 += sz;\n }\n }\n //cout << sz2 << '\\n';\n cout << 1ll * sz1 * sz2 << '\\n';\n\t}\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "6c86d4bfdce70c60b30d639116756250", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n _____\n / \\\n vvvvvvv /|__/|\n I /O,O |\n I /_____ | /|/|\n J|/^ ^ ^ \\ | /00 | _//|\n |^ ^ ^ ^ |W| |/^^\\ | /oo |\n \\m___m__|_| \\m_m_| \\mm_|\n\n\n */\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define fori(a, r) for(long long int a = 0; a < (r); ++a)\n#define forI(a, r) for(long long int a = 1; a <= (r); ++a)\n\n#define ff first\n#define ss second\n#define pll pair\n\n//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native\")\n//#pragma GCC optimize(\"fast-math\")\n//#pragma GCC optimize(\"unroll-loops\")\n\nusing namespace std;\ntypedef long long int ll;\ntypedef long double ld;\ntypedef long long ull;\n\n#define pt pair\n#define x first\n#define y second\n\npt dxy[] = {{1, 0},\n {0, 1},\n {-1, 0},\n {0, -1}};\nll months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\nconst long long MAXN = 300 * 1000 + 100, MOD = 1000000007, INF = 4LL * 1000 * 1000 * 1000 * 1000;\n\n//mt19937 random(time(0));\n\nll n, m, a, b;\n\nvector g[200 * 1000 + 1];\nbool used[200 * 1000 + 1];\n\nll c;\nll e;\n\nvoid dfs(ll v){\n used[v] = true;\n ++c;\n\n for(ll to: g[v]){\n if(to == a) e |= 1;\n else if(to == b) e |= 2;\n else if(!used[to]) dfs(to);\n }\n}\n\nvoid solve() {\n ll t;\n cin >> t;\n string s;\n\n fori(iuasfhgb, t) {\n ll x, y;\n cin >> n >> m >> a >> b;\n forI(i, n) used[i] = false;\n forI(i, n) g[i].clear();\n fori(i, m){\n cin >> x >> y;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n\n ll A = 0, B = 0;\n bool ok = false;\n\n forI(i, n){\n if(!used[i] && i != a && i != b){\n c = 0, e = 0;\n dfs(i);\n if(e == 1) A += c;\n else if(e == 2) B += c;\n }\n }\n\n cout << A * B << \"\\n\";\n }\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n#define DEBUG0\n#ifdef DEBUG\n freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n#endif\n solve();\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "42981e9dad10394623ea98743675ec30", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\nconst int N = (int)(2e5 + 123);\n\nvector g[N];\nint was[N];\nbool q;\nint dfs(int v , int bad) {\n\twas[v] = true;\n\tint val = 1;\n for (auto to : g[v]) {\n if (to == bad) {\n q = false;\n continue;\n }\n if (was[to]) continue;\n val += dfs(to , bad);\n }\n return val;\n}\nvector aa , bb;\nint main() {\n\tint t;\n\tcin >> t;\n\twhile (t--) {\n\t\tint n , m , a , b;\n\t\tcin >> n >> m >> a >> b;\n\t\t\n\t\taa.clear();\n\t\tbb.clear();\n\t\tfor (int i=1; i<=n; i++) g[i].clear();\n\t\tfor (int i=1; i<=n; i++) was[i] = 0;\n\t\t\n\t\tfor (int i=1; i<=m; i++) {\n\t\t\tint u , v;\n\t\t\tcin >> u >> v;\n\t\t\tg[u].push_back(v);\n\t\t\tg[v].push_back(u);\n\t\t}\n\t\twas[a] = was[b] = true;\n\t\tfor (auto to: g[a]) {\n\t\t if (was[to] == false) {\n\t\t q = true;\n\t\t aa.push_back(dfs(to , b) * q);\n\t\t }\n\t\t}\n\t\tfor (auto to: g[b]) {\n\t\t if (was[to] == false) {\n\t\t q = true;\n\t\t bb.push_back(dfs(to , a) * q);\n\t\t }\n\t\t}\n\t\t\n\t\t/*for (int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nvector > graph;\nvector used;\nvector > kol;\n\nint st, ed;\n\nvoid dfs1(int u) {\n\tused[u] = 1;\n\tkol[u].push_back(1);\n\tfor (auto i : graph[u]) {\n\t\tif (used[i] == -1 and i!=ed) {\n\t\t\tdfs1(i);\n\t\t}\n\t}\n}\n\nvoid dfs2(int u) {\n\tused[u] = 1;\n\tkol[u].push_back(2);\n\tfor (auto i : graph[u]) {\n\t\tif (used[i] == -1 and i!=st) {\n\t\t\tdfs2(i);\n\t\t}\n\t}\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tint t = 0, n, m;\n\tcin >> t;\n\tfor (int w = 0; w < t; w++) {\n\t\tcin >> n;\n\t\tlong long left = 0, right = 0;\n\t\tcin >> m >> st >> ed;\n\t\tst--, ed--;\n\t\tgraph = vector >(n);\n\t\tused = vector(n, -1);\n\t\tkol = vector >(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tcin >> left >> right;\n\t\t\tleft--, right--;\n\t\t\tgraph[left].push_back(right);\n\t\t\tgraph[right].push_back(left);\n\t\t}\n\t\tleft = 0, right = 0;\n\t\tdfs1(st);\n\t\tused.assign(n, -1);\n\t\tdfs2(ed);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (kol[i].size() == 1) {\n\t\t\t\tif (kol[i][0] == 1) {\n\t\t\t\t\tleft++;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tright++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << max(0ll, max(0ll, left - 1)*max(0ll, right - 1)) << \"\\n\";\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "2dfdd2687ce618cc2da1d9e6507ad198", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\nint a,b,n,m,us[200000],ans[3],q,x,y;\nvectorV[200000];\nvoid f1(int arc,int lol){\n if(us[arc] == lol){\n ans[us[arc]]--;\n us[arc]++;\n ans[us[arc]]++;\n }\n for(auto ar:V[arc]){\n if(us[ar] == lol && ar!=a && ar!=b){\n f1(ar,lol);\n }\n }\n}\nint main() {\n cin>>q;\n for(int ij=0;ij>n>>m>>a>>b;\n ans[0]=n;ans[1]=ans[2]=0;\n a--;\n b--;\n for(int i=0;i>x>>y;\n V[y-1].push_back(x-1);\n V[x-1].push_back(y-1);\n }\n f1(a,0);\n f1(b,1);\n cout<<(long long)(ans[0]-1)*(ans[1]-1)<\n\nusing namespace std;\n\n#define sz(x) ((long long)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntypedef long long ll;\n\nconst ll inf = 1e18;\n\nll bin_pow(ll a, ll n) {\n\tif (n == 0)\n\t\treturn 1;\n\n\tif (n % 2 == 0) {\n\t\tll b = bin_pow(a, n / 2);\n\t\treturn b * b;\n\t}\n\telse return a * bin_pow(a, n - 1);\n}\n\nvector> g;\nvector used;\n\nll flaga = false, flagb = false;\nvector buf;\n\nvoid dfs(ll v, ll a, ll b) {\n\tused[v] = true;\n\tbuf.push_back(v);\n\n\tfor (auto to : g[v]) {\n\t\tif (to == a)\n\t\t\tflaga = true;\n\t\telse if (to == b)\n\t\t\tflagb = true;\n\t\telse if (!used[to])\n\t\t\tdfs(to, a, b);\n\t}\n}\n\nint main() {\n\tios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);\n\n\tll t; cin >> t;\n\twhile (t--) {\n\t\tll n, m, a, b; cin >> n >> m >> a >> b;\n\t\tg.clear();\n\n\t\ta--; b--;\n\t\tg.resize(n);\n\t\tused.assign(n, false);\n\n\t\tfor (ll i = 0; i < m; i++) {\n\t\t\tll u, v; cin >> u >> v;\n\t\t\tu--; v--;\n\t\t\tg[u].push_back(v);\n\t\t\tg[v].push_back(u);\n\t\t}\n\n\t\tvector> ca, cb;\n\n\t\tfor (ll i = 0; i < n; i++) {\n\t\t\tif (i != a && i != b && !used[i]) {\n\t\t\t\tflaga = false, flagb = false;\n\t\t\t\tbuf.clear();\n\n\t\t\t\tdfs(i, a, b);\n\n\t\t\t\tif (flaga != flagb) {\n\t\t\t\t\tif (flaga) {\n\t\t\t\t\t\tca.push_back(buf);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (flagb) {\n\t\t\t\t\t\tcb.push_back(buf);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tll mula = 0, mulb = 0;\n\t\tfor (auto c : ca)\n\t\t\tmula += sz(c);\n\t\tfor (auto c : cb)\n\t\t\tmulb += sz(c);\n\n\t\tll ans = 0;\n\t\tfor (auto c : ca) {\n\t\t\tans += sz(c) * mulb;\n\t\t}\n\t\tfor (auto c : cb) {\n\t\t\tans += sz(c) * mula;\n\t\t}\n\n\t\tcout << ans / 2 << endl;\n\t}\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "f0044eccaddc81f67804f43de77c21e6", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nlong long left = 0;\n\nlong long right = 0;\n\nunordered_map> gr;\n\nvoid dfs(vector& used, int now) {\n used[now] = 0;\n for (auto next : gr[now]) {\n if (next.second == 1 && used[next.first]) {\n dfs(used, next.first);\n }\n }\n}\n\nlong long k = 0;\n\nvoid dfs_(vector& used, int now) {\n used[now] = 0;\n for (auto next : gr[now]) {\n if (next.second == 1 && used[next.first]) {\n dfs_(used, next.first);\n }\n }\n k++;\n}\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n int q;\n cin >> q;\n for (; q > 0; --q) {\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n for (int i = 0; i < m; ++i) {\n int x, y;\n cin >> x >> y;\n gr[x][y] = 1;\n gr[y][x] = 1;\n }\n vector used(n + 1, 1);\n vector sos;\n for (auto now : gr[a]) {\n sos.push_back(now.first);\n }\n for (int i = 0; i < sos.size(); ++i) {\n gr[sos[i]][a] = 0;\n gr[a][sos[i]] = 0;\n }\n dfs(used, b);\n long long left = 0;\n for (int i = 0; i < sos.size(); ++i) {\n if (used[sos[i]] == 1) {\n dfs_(used, sos[i]);\n left += k;\n k = 0;\n }\n }\n for (int i = 0; i < sos.size(); ++i) {\n gr[sos[i]][a] = 1;\n gr[a][sos[i]] = 1;\n }\n used.assign(n + 1, 1);\n sos.clear();\n for (auto now : gr[b]) {\n sos.push_back(now.first);\n }\n for (int i = 0; i < sos.size(); ++i) {\n gr[sos[i]][b] = 0;\n gr[b][sos[i]] = 0;\n }\n dfs(used, a);\n long long right = 0;\n for (int i = 0; i < sos.size(); ++i) {\n if (used[sos[i]] == 1) {\n dfs_(used, sos[i]);\n right += k;\n k = 0;\n }\n }\n cout << left * right << \"\\n\";\n gr.clear();\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "34487a4898fabadf5ecf156b1fd62f31", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \"bits/stdc++.h\"\n\n#define fi first\n#define se second\n#define px first\n#define py second\n#define PB push_back\n\nusing namespace std;\n\ntypedef unsigned int UI;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef long double LD;\n\nconst int INF = 1e9 + 5;\n\n//#define DEBUG\n#ifdef DEBUG\n#include \"Debug.h\"\n#include \"Debug2.h\"\n#endif\n\n#ifndef DEBUG\n//#define FILE_IO\n#endif\n\nvector used;\nbool key;\nLL dfs(int v, int par, vector> &a, int p2) {\n if (v == p2) {\n key = true;\n return 1;\n }\n LL cnt = 0;\n used[v] = 1;\n for (auto u : a[v]) {\n if (u == par || used[u]) continue;\n cnt += dfs(u, v, a, p2);\n }\n return cnt + 1;\n}\n\nint main() {\n#ifdef FILE_IO\n freopen(\"in.in\", \"r\", stdin);\n freopen(\"out.out\", \"w\", stdout);\n#else\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n#endif\n int t;\n cin >> t;\n for (int qq = 0; qq < t; qq++) {\n int n, m;\n cin >> n >> m;\n used.assign(n, 0);\n int s, f;\n cin >> s >> f;\n s--;\n f--;\n vector> a(n);\n for (int i = 0; i < m; i++) {\n int x, y;\n cin >> x >> y;\n x--;\n y--;\n a[x].push_back(y);\n a[y].push_back(x);\n }\n LL ans1 = 0;\n for (auto i : a[s]) {\n if (used[i]) continue;\n key = false;\n used[s] = 1;\n LL t = dfs(i, s, a, f);\n if (!key) {\n ans1 += t;\n }\n }\n for (auto &i : used) i = 0;\n LL ans2 = 0;\n for (auto i : a[f]) {\n if (used[i]) continue;\n key = false;\n used[f] = 1;\n LL t = dfs(i, f, a, s);\n if (!key) {\n ans2 += t;\n }\n }\n cout << ans1 * ans2 << endl;\n }\n return 0;\n}\n/*\n1\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n*/\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "7fae28d589288b442932622f69d454a8", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n\nusing namespace std;\n\nconst int MAXN = 2e5;\n\nvector g[MAXN];\nvector used1, used2;\nset s;\n\nvoid dfs1(int v, int x) {\n used1[v] = true;\n for (auto &u : g[v]) {\n if (!used1[u] && u != x) {\n dfs1(u, x);\n }\n }\n}\n\nvoid dfs2(int v, int x) {\n used2[v] = true;\n for (auto &u : g[v]) {\n if (!used2[u] && u != x) {\n dfs2(u, x);\n }\n }\n}\n\nmain() {\n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#else\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n#endif\n int t;\n cin >> t;\n int prev = 0;\n while (t--) {\n for (int i = 0; i < prev; i++) {\n g[i].clear();\n }\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n for (int i = 0; i < m; i++) {\n int v, u;\n cin >> v >> u;\n g[v - 1].emplace_back(u - 1);\n g[u - 1].emplace_back(v - 1);\n }\n prev = n;\n used1.assign(n, false);\n used2.assign(n, false);\n a--, b--;\n dfs1(a, b);\n dfs2(b, a);\n long long x = 0, y = 0;\n for (int i = 0; i < n; i++) {\n if (i == a || i == b) {\n continue;\n }\n if (used1[i]) {\n if (!used2[i]) {\n x++;\n }\n } else if (used2[i]) {\n y++;\n }\n }\n cout << x * y << '\\n';\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "aa810e25cd2d84a97df5fbe6bef38cb9", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\n\nconst ll INF = 1e9 + 7;\n\nvoid fast() {\n\tios::sync_with_stdio(0);\n\tios_base::sync_with_stdio(0);\n\tcout.tie(0);\n\tcin.tie(0);\n}\n\nistream& operator >> (istream& cin, vector& m1) {\n for (auto& el : m1) {\n cin >> el;\n }\n return cin;\n}\n\nostream& operator << (ostream& cout, vector& m1) {\n for (auto& el : m1) {\n cout << el << ' ';\n }\n return cout;\n}\n\nll bin_pow(ll val, ll kol) {\n if (kol == 0) {\n return 1;\n }\n if (kol == 1) {\n return val;\n }\n if (kol & 1) {\n return val * bin_pow(val, kol - 1);\n }\n return bin_pow(val * val, kol / 2);\n}\n\nvector par;\nvector sz;\nvector usss;\nvector> grf;\n\nll get(ll v) {\n if (par[v] == v) {\n return v;\n }\n return (par[v] = get(par[v]));\n}\n\nvoid unite(ll v, ll u) {\n v = get(v);\n u = get(u);\n if (v == u) {\n return;\n }\n if (sz[u] > sz[v]) {\n swap(u, v);\n }\n par[u] = v;\n sz[v] += sz[u];\n return;\n}\n\nvoid dfs(int v, int check, bool& val) {\n usss[v] = true;\n if (v == check) {\n val = true;\n return;\n }\n for (auto& u : grf[v]) {\n if (usss[u]) {\n continue;\n }\n dfs(u, check, val);\n }\n}\n\nvoid solve() {\n ll n, m, a, b;\n cin >> n >> m >> a >> b;\n par.clear();\n sz.clear();\n grf.clear();\n usss.clear();\n usss.resize(n + 1, 0);\n grf.resize(n + 1);\n par.resize(n + 1);\n sz.resize(n + 1, 1);\n for (ll i = 0; i <= n; i++) {\n par[i] = i;\n }\n vector> m1, m2;\n for (ll i = 0; i < m; i++) {\n ll u, v;\n cin >> u >> v;\n grf[u].push_back(v);\n grf[v].push_back(u);\n if (u == a && v == b) {\n continue;\n }\n if (u == b && v == a) {\n continue;\n }\n if (u == a || v == a) {\n m1.push_back({u, v});\n continue;\n }\n if (u == b || v == b) {\n m2.push_back({u, v});\n continue;\n }\n unite(u, v);\n }\n vector used(n + 1, 0);\n for (auto& el : m1) {\n ll v;\n if (el.first == a) {\n v = get(el.second);\n }\n else {\n v = get(el.first);\n }\n used[v] = 1;\n }\n for (auto& el : m2) {\n ll v;\n if (el.second == b) {\n v = get(el.first);\n }\n else {\n v = get(el.second);\n }\n if (used[v] == 1) {\n used[v]++;\n }\n }\n ll val1 = 0;\n ll val2 = 0;\n vector is_count(n + 1, 0);\n for (auto& el : m1) {\n ll v;\n if (el.first == a) {\n v = get(el.second);\n }\n else {\n v = get(el.first);\n }\n if (used[v] != 2 && is_count[v] == 0) {\n val1 += sz[v];\n is_count[v] = 1;\n }\n }\n fill(is_count.begin(), is_count.end(), 0);\n for (auto& el : m2) {\n ll v;\n if (el.first == b) {\n v = get(el.second);\n }\n else {\n v = get(el.first);\n }\n if (used[v] != 2 && is_count[v] == 0) {\n val2 += sz[v];\n is_count[v] = 1;\n }\n }\n bool can = false;\n dfs(a, b, can);\n if (can == false) {\n cout << 0 << endl;\n return;\n }\n cout << val1 * val2 << endl;\n}\n\nsigned main() {\n\t//ifstream cin(\"input.txt\");\n\t//ofstream cout(\"output.txt\");\n\tfast();\n\n ll n;\n cin >> n;\n for (ll i = 0; i < n; i++) {\n solve();\n }\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "e7bf210a7deabd6116ef271ee1096faf", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// #define _CRT_SECURE_NO_WARNINGS\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \ntypedef long long ll;\nconst ll INF = 1e18;\nconst ll MOD = 998244353;\nusing namespace std;\n\nll calc;\nint V;\nvoid dfs1(const int v, const vector> &g, vector &used) {\n\tused[v] = true;\n\tfor (auto &to : g[v])\n\t\tif (used[to] == 0 && to != V)\n\t\t\tdfs1(to, g, used);\n\treturn;\n}\nvoid dfs2(const int v, const vector> &g, vector &used) {\n\tused[v] = true;\n\t++calc;\n\tfor (auto &to : g[v])\n\t\tif (used[to] == 0)\n\t\t\tdfs2(to, g, used);\n\treturn;\n}\n\nvoid solve(void) {\n\tint n, m, a, b;\n\tcin >> n >> m >> a >> b;\n\t--a;\n\t--b;\n\tvector> g(n);\n\tfor (int i = 0; i < m; ++i) {\n\t\tint v1, v2;\n\t\tcin >> v1 >> v2;\n\t\t--v1;\n\t\t--v2;\n\t\tg[v1].push_back(v2);\n\t\tg[v2].push_back(v1);\n\t}\n\n\tvector used(n);\n\tV = a;\n\tcalc = -1;\n\tdfs1(b, g, used);\n\tdfs2(a, g, used);\n\tll fp = calc;\n\n\tused.assign(n, false);\n\tV = b;\n\tcalc = -1;\n\tdfs1(a, g, used);\n\tdfs2(b, g, used);\n\n\tcout << fp * calc << '\\n';\n\treturn;\n}\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0); cout.tie(0);\n\n\tint t;\n\tcin >> t;\n\twhile (t--)\n\t\tsolve();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "e61d72587c26d7cb82d7aed5d0fb81c2", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define boost ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0)\n#define f first\n#define se second\n#define lb lower_bound\n#define ub upper_bound\n#define pii pair\n#define all(x) x.begin(), x.end()\n\nconst int N = 2e5 + 144, mod = 1e9 + 7, inf = 1e9 + 7;\nint n, m, a, b;\nvector g[N];\nbool was[N];\n\nll bp(ll x, ll y){\n if(y == 0)\n return 1;\n if(y & 1)\n return (x * bp(x, y - 1));\n ll z = bp(x, y / 2);\n return (z * z) % mod;\n}\n\nvoid dfs(int x){\n was[x] = true;\n for(auto to: g[x])\n if(!was[to])\n dfs(to);\n}\n\nint main(){\n boost;\n int q;\n cin >> q;\n while(q--){\n cin >> n >> m >> a >> b;\n for(int i = 1; i <= m; i++){\n int x, y;\n cin >> x >> y;\n g[x].pb(y);\n g[y].pb(x);\n }\n ll x = 0, y = 0;\n was[b] = true;\n dfs(a);\n for(int i = 1; i <= n; i++){\n if(!was[i])\n x++;\n was[i] = 0;\n }\n was[a] = true;\n dfs(b);\n for(int i = 1; i <= n; i++){\n if(!was[i])\n y++;\n was[i] = 0;\n }\n for(int i = 1; i <= n; i++)\n g[i].clear();\n cout << x * y << \"\\n\";\n }\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "596fd1c18dd2fc4e3eb1e41eeb38df1f", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define ft first\n#define sd second\n#define mp make_pair\n#define all(x) x.begin(), x.end()\n#define all_(x) x.rbegin(), x.rend()\n#define multi_test 1\n#define int ll\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pld;\n\nconst ll MAXN = 3e5 + 7;\nconst ll MOD = 1e9 + 7;\nconst ll MOD1 = 1e9 + 9;\nconst ll INF = 2e9 + 9;\nconst ld EPS = 1e-8;\n\nvoid solve();\n\nsigned main() {\n#ifdef _DEBUG\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int q = 1;\n if (multi_test) cin >> q;\n while (q--) solve();\n}\n\n/*-------------------------------------------------------------------------------------------------------------*/\n\nint n, m, a, b, cnta = 0, cntb = 0;\nvector used;\nvector> g;\nbool checka, checkb;\n\nint dfs(int v) {\n int res = 1;\n used[v] = true;\n for (int to : g[v]) {\n if (to == a)\n checka = true;\n if (to == b)\n checkb = true;\n if (!used[to] && to != a && to != b) {\n res += dfs(to);\n }\n }\n return res;\n}\n\nvoid solve() {\n cin >> n >> m >> a >> b;\n --a; --b;\n g.resize(n); used.resize(n);\n for (int i = 0; i < n; ++i) {\n g[i].clear();\n used[i] = 0;\n }\n cnta = cntb = 0;\n for (int i = 0; i < m; ++i) {\n int x, y;\n cin >> x >> y;\n --x; --y;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n for (int i = 0; i < n; ++i) {\n if (!used[i] && i != a && i != b) {\n checka = 0; checkb = 0;\n int r = dfs(i);\n if (checka && !checkb) {\n cnta += r;\n } else if (!checka && checkb) {\n cntb += r;\n }\n }\n }\n cout << cnta * cntb << '\\n';\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "5d01c0a3fda8354e71d5edf6280e2039", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define F first\n#define S second \n#define mp make_pair\n#define pb push_back\n#define sz(x) (int)x.size() \n\nusing namespace std;\n \nconst int N = (int)2e5 + 5;\n\nint n,m,a,b;\n\nlong long A,B;\n\nvector g[N];\n\nbool used[N],used1[N],used2[N],used3[N],used4[N];\n\nvoid obnul(){\n\tA = 0;\n\tB = 0;\n\tfor(int i = 1; i <= n; i++){\n\t\tg[i].clear();\n\t\tused[i] = false;\n\t\tused1[i] = false;\n\t\tused2[i] = false;\n\t\tused3[i] = false;\n\t\tused4[i] = false;\n\t}\n}\n\nvoid solve(){\n\tcin >> n >> m >> a >> b;\n\tfor(int i = 1; i <= m; i++){\n\t\tint u,v;\n\t\tcin >> u >> v;\n\t\tg[u].pb(v);\n\t\tg[v].pb(u);\t\n\t}\t\t\n\t\n\tqueue q1,q2,q3,q4;\n\tq1.push(a);\n\tused1[a] = true;\n\twhile(!q1.empty()){\n\t\tint v = q1.front();\n\t\tq1.pop();\n\t if(v != b){\n\t\t\tfor(auto to : g[v]){\n\t\t\t\tif(!used1[to]){\n\t\t\t\t\tq1.push(to);\n\t\t\t\t\tused1[to] = true;\n\t\t\t\t}\t\n\t\t\t} \t\n\t }\n\t}\n\tq2.push(b);\n\tused2[b] = true;\n\twhile(!q2.empty()){\n\t\tint v = q2.front();\n\t\tq2.pop();\n\t\tif(v != a){\n\t\t\tfor(auto to : g[v]){\n\t\t\t\tif(!used2[to]){\n\t\t\t\t\tq2.push(to);\n\t\t\t\t\tused2[to] = true;\t\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\tfor(int i = 1; i <= n; i++){\n\t\tif(used1[i] == true && used2[i] == true) \n\t\t\tused[i] = true;\n\t}\n\t\n\tq3.push(a);\n\tused3[a] = true;\n\twhile(!q3.empty()){\n\t\tint v = q3.front();\n\t\tq3.pop();\n\t\tfor(auto to : g[v]){\n\t\t\tif(!used[to] && !used3[to]){\n\t\t\t\tq3.push(to);\n\t\t\t\tused3[to] = true;\n\t\t\t\tA++;\n\t\t\t}\n\t\t}\n\t}\n\t\t\n\tq4.push(b);\n\tused4[b] = true;\n\twhile(!q4.empty()){\n\t\tint v = q4.front();\n\t\tq4.pop();\n\t\tfor(auto to : g[v]){\n\t\t\tif(!used[to] && !used4[to]){\n\t\t\t\tq4.push(to);\n\t\t\t\tused4[to] = true;\n\t\t\t\tB++;\n\t\t\t}\n\t\t}\n\t}\n\tcout << A * B << endl;\n\tobnul();\n}\n\nint main(){\n \tios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);\n \tint tt;\n \tcin >> tt;\n \twhile(tt--){\n \t\tsolve();\n \t}\n \treturn 0;\n} ", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "6169b54e9e40d67f8557f96a3a0d38a3", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n\n#include \n#include \n\nusing namespace std;\n\n#define uint unsigned int\n#define prll pair\n#define prdd pair\n#define m_p make_pair\n#define INF (ll)1e18\n#define ll long long\n\nconst ll q = 239017;\nconst ll mod = 1e9 + 7;\nconst ll mod2 = 1e9 + 13;\nconst ll MAXN = 2e5 + 100;\nconst ll MAXM = 4294967291;\nconst ll L = 26;\n\nvector g[MAXN];\nll used[MAXN], ct[MAXN];\n\nvector comp[2];\n\nll id;\nvoid dfs(ll v)\n{\n\tif (used[v])\n\t\treturn;\n\n\tcomp[id].push_back(v);\n\n\tused[v] = 1;\n\n\tfor (ll to : g[v])\n\t\tdfs(to);\n}\n\nvoid foo()\n{\n\tll t; cin >> t;\n\twhile (t--)\n\t{\n\t\tll n, m, a, b; cin >> n >> m >> a >> b;\n\t\t--a; --b;\n\n\t\tfor (ll i = 0; i < m; ++i)\n\t\t{\n\t\t\tll u, v; cin >> u >> v;\n\t\t\t--u; --v;\n\n\t\t\tg[u].push_back(v);\n\t\t\tg[v].push_back(u);\n\t\t}\n\n\t\tid = 0; used[b] = 1;\n\t\tdfs(a);\n\n\t\tfor (ll el : comp[0])\n\t\t\tused[el] = 0;\n\n\t\tused[b] = 0;\n\n\t\tid = 1; used[a] = 1;\n\t\tdfs(b);\n\n\t\tfor (ll i = 0; i < 2; ++i)\n\t\t\tfor (ll el : comp[i])\n\t\t\t\t++ct[el];\n\n\t\tll mas[2] = { -1,-1 };\n\t\tfor (ll i = 0; i < 2; ++i)\n\t\t\tfor (ll el : comp[i])\n\t\t\t\tif (ct[el] == 1)\n\t\t\t\t\t++mas[i];\n\n\t\tcout << max(0ll, mas[0] * mas[1]) << '\\n';\n\n\t\tfor (ll i = 0; i < n; ++i)\n\t\t{\n\t\t\tg[i].clear();\n\t\t\tused[i] = ct[i] = 0;\n\t\t}\n\t\tcomp[0].clear();\n\t\tcomp[1].clear();\n\t}\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0), cout.tie(0);\n\n\tfoo();\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "3824dc21ec0182561bd025d1d3b4bf99", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\nvector > g;\nlong long n, m, a, b;\nvector used;\n\nvoid dfs(long long u, long long c){\n if(u == c)\n return;\n used[u] = 1;\n for(long long i = 0; i < g[u].size(); ++i)\n if(!used[g[u][i]])\n dfs(g[u][i], c);\n}\n\nint main()\n{\n long long t; cin >> t;\n for(long long q = 0; q < t; ++q){\n cin >> n >> m >> a >> b;\n --b, --a;\n g.assign(n, vector ());\n used.assign(n, 0);\n for(long long i = 0; i < m; ++i){\n long long x, y; cin >> x >> y;\n --x, --y;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n long long cnt = 0, cnt1 = 0;\n dfs(b, a);\n for(long long i = 0; i < n; ++i)\n if(!used[i] && i != a)\n ++cnt;\n used.assign(n, 0);\n dfs(a, b);\n for(long long i = 0; i < n; ++i)\n if(!used[i] && i != b)\n ++cnt1;\n cout << cnt1 * cnt << '\\n';\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "61e88c11b23e9c22a14d17032a4cc5e5", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define ll long long\n#define ld long double\n#define all(x) x.begin(),x.end()\n#define fori(n) for (int i = 0; i < n; ++i)\n#define forj(n) for (int j = 0; j < n; j++)\n#define ss second\n#define ff first\n#define pb push_back\n\n//#pragma GCC optimize(\"Ofast\", \"unroll-loops\", \"fast-math\", \"O3\")\n#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\nusing namespace std;\n\nconst ll mod = 1e9 + 9;\nconst ll p = 179;\nconst ll INF = 1e9 + 9;\nvector g[200000];\nbool us[200000];\nint cnt = 0;\n\nvoid dfs(int v, int b) {\n us[v] = true;\n cnt++;\n for (auto i : g[v]) {\n if (i == b)\n continue;\n if (us[i])\n continue;\n dfs(i, b);\n }\n}\n\nvoid solve() {\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n a--;\n b--;\n for (int i = 0; i < n; i++) {\n us[i] = false;\n g[i].clear();\n }\n for (int i = 0; i < m; i++) {\n int a, b;\n cin >> a >> b;\n g[--a].emplace_back(--b);\n g[b].emplace_back(a);\n }\n cnt = 0;\n dfs(a, b);\n ll A = n - cnt - 1;\n for (int i = 0; i < n; i++) {\n us[i] = false;\n }\n cnt = 0;\n dfs(b, a);\n ll B = n - cnt - 1;\n cout << A * B;\n}\n\nint main() {\n fast\n int t;\n cin >> t;\n while (t--) {\n solve();\n cout << '\\n';\n }\n}\n//\n// 1 3 5\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "43300d2720daac7f81781c1a3843d940", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n// #define int long long\nmt19937 rnd(time(0));\ntypedef long long ll;\ntypedef long double ld;\n\n// #define _GLIBCXX_DEBUG\n#define f first\n#define s second\n#define pb push_back\n#define pii pair \n#define vi vector\n#define int ll\n\nvoid solve(){\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n vector r(n);\n for (int i = 0; i < m; ++i) {\n int x, y;\n cin >> x >> y;\n x--;\n y--;\n r[x].pb(y);\n r[y].pb(x);\n }\n queue q;\n int d[n], d2[n];\n a--;\n b--;\n memset(d, 127, sizeof(d));\n memset(d2, 127, sizeof(d2));\n int lol = d[0];\n d[a] = 0;\n d2[b] = 0;\n q.push(a);\n while (q.size()) {\n auto x = q.front();\n q.pop();\n for (auto &u : r[x]) {\n if (u == b)\n continue;\n if (d[u] > d[x] + 1) {\n d[u] = d[x] + 1;\n q.push(u);\n }\n }\n }\n q.push(b);\n while (q.size()) {\n auto x = q.front();\n q.pop();\n for (auto &u : r[x]) {\n if (u == a)\n continue;\n if (d2[u] > d2[x] + 1) {\n d2[u] = d2[x] + 1;\n q.push(u);\n }\n }\n }\n int x = 0, y = 0;\n for (int i = 0; i < n; ++i) {\n if (i == a || i == b)\n continue;\n \n if (d[i] < lol && d2[i] >= lol) {\n x++;\n } else if(d2[i] = lol) {\n y++;\n }\n }\n cout << x * y << endl;\n}\n\nsigned main(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n \n int t = 1;\n cin >> t;\n while(t--){\n solve();\n }\n \n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "7e84c965a48221e8a0d9b6e1f2437942", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include\nusing namespace __gnu_pbds;\nusing namespace std;\n#define ll long long\n#define st string\n#define ld long double\n#define fi first\n#define se second\n#define pb push_back\n#define mk make_pair\n#define endl '\\n'\n\nvoid file() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n// freopen (\"milkvisits.in\", \"r\", stdin); freopen (\"milkvisits.out\", \"w\", stdout);\n}\n\nconst ll N = 2e5 + 7;\nconst ll MOD = 1e9 + 7;\n\nll n, m, k, l, a[N], used[5][N], p;\nvector v[N];\n\nvoid bfs1 (ll x) {\n queue q;\n q.push(x);\n while (q.size()) {\n x = q.front();\n q.pop();\n for (auto i : v[x]) {\n// cout << 1 << \" \" << x << \" \" << i << \" \" << used[1][i] << endl;\n if (i != l + p && !used[1][i]) q.push(i), used[1][i]++;\n }\n }\n}\n\nvoid bfs2 (ll x) {\n queue q;\n q.push(x);\n while (q.size()) {\n x = q.front();\n q.pop();\n for (auto i : v[x]) {\n// cout << 2 << \" \" << x << \" \" << i << \" \" << used[2][i] << endl;\n if (i != k + p && !used[2][i]) q.push(i), used[2][i]++;\n }\n }\n}\n\nvoid bfs (ll x) {\n queue q;\n q.push(x);\n while (q.size()) {\n ll x = q.front();\n q.pop();\n for (auto i : v[x]) {\n if (!used[3][i]) q.push(i), used[3][i]++;\n }\n }\n}\n\nint main()\n{\n file();\n ll nn;\n cin >> nn;\n while (nn--) {\n cin >> n >> m >> k >> l;\n --k, --l;\n for (int i = 0; i < m; ++i) {\n ll x, y;\n cin >> x >> y;\n x = p + x - 1; y = p + y - 1;\n// cout << x << \" \" << y << endl;\n v[x].pb(y);\n v[y].pb(x);\n }\n// cout << k + p << \" \" << l + p << endl;\n// for (int i = 0; i < n; ++i) {\n// cout << i + p << \" \" << used[1][i + p] << \" \" << used[2][i + p] << endl;\n// }\n bfs1(k + p); bfs2(l + p); bfs(k + p);\n ll x, y;\n x = y = 0;\n for (int i = 0; i < n; ++i) {\n// cout << i + p << \" \" << used[1][i + p] << \" \" << used[2][i + p] << \" \" << used[3][i + p] << endl;\n if (!used[3][i + p] || i == k || i == l) continue;\n if (!used[1][i + p] && used[2][i + p]) ++x;\n if (used[1][i + p] && !used[2][i + p]) ++y;\n }\n p += n;\n cout << x * y << endl;\n// cout << endl;\n }\n}\n\n/**\n1\n5\n10\n01\n010\n100\n001\n\n01\n10\n010\n001\n100\n\n1\n4\n0001\n1000\n0010\n1001\n*/\n\n\n\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "62e7ff6d93650a8a137dee2ee7a5eab3", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\nvoid QuickStart(){\n ios_base::sync_with_stdio(0); cin.tie(0);\n}\n\ntypedef long long ll;\n\nvector >g;\nvectorused;\nlong long to = 0, to1 = 0;\n\nvoid dfs(int v, int no){\n to++;\n used[v] = 1;\n for(int i = 0; i < g[v].size(); i++){\n if(!used[g[v][i]] && g[v][i] != no){\n dfs(g[v][i], no);\n }\n }\n}\n\nvoid solve(){\n int n, m, x, y;\n cin >> n >> m >> x >> y;\n g.assign(n, vector());\n used.assign(n, 0);\n to = 0;\n for(int i = 0; i < m; i++){\n int a, b;\n cin >> a >> b;\n g[a - 1].push_back(b - 1);\n g[b - 1].push_back(a - 1);\n }\n to = 0;\n dfs(y - 1, x - 1);\n ll ans1 = n - to;\n used.assign(n, 0);\n to = 0;\n dfs(x - 1, y - 1);\n ll ans2 = n - to;\n cout << (ans1 - 1) * (ans2 - 1) << endl;\n}\n\nint main()\n{\n QuickStart();\n int t; cin >> t;\n for(int i = 0; i < t; i++){\n solve();\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "e737ce088db00f9c81e8ecabd4b7ace4", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#define pii pair\n#define pll pair\n#define pss pair\n#define forn(n,i) for (int i = 0; i < n; i++)\n#define fordown(n,i) for (int i = n - 1; i >= 0; --i)\n#define sorted(a) sort(a.begin(), a.end())\n#define mp(x, y) make_pair(x, y)\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef double db;\ntypedef long double ldb;\n\nset sa;\nset sb;\nll n, m, a, b;\nll x, y;\nvector used;\nll t;\nvector gs[200100];\nvoid dfsa(ll x) {\n\tused[x] = 1;\n\tforn(gs[x].size(), i) {\n\t\tif (gs[x][i] != b && used[gs[x][i]] != 1) {\n\t\t\tsa.insert(gs[x][i]);\n\t\t\tdfsa(gs[x][i]);\n\t\t}\n\t}\n}\nvoid dfsb(ll x) {\n\tused[x] = 1;\n\tforn(gs[x].size(), i) {\n\t\tif (gs[x][i] != a && used[gs[x][i]] != 1) {\n\t\t\tsb.insert(gs[x][i]);\n\t\t\tdfsb(gs[x][i]);\n\t\t}\n\t}\n}\n\nint main() {\n//\tfreopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n\tcin >> t;\n\tint prev = 0;\n\tforn(t, ind) {\n\n\t\tcin >> n >> m >> a >> b;\n\t\tfor (int i = 0; i < prev; i++) {\n\t\t\tgs[i].clear();\n\t\t}\n\t\tused.assign(n + 1, 0);\n\t\ta--; b--;\n\t\tprev = n;\n\t\tforn(m, i) {\n\t\t\tcin >> x >> y;\n\t\t\tx--; y--;\n\t\t\tgs[x].push_back(y);\n\t\t\tgs[y].push_back(x);\n\t\t}\n\t\tsa.clear();\n\t\tsb.clear();\n\t\t//memset(used, 0, sizeof(used));\n\t\tdfsa(a);\n\t\tused.assign(n + 1, 0);\n\t//\tmemset(used, 0, sizeof(used));\n\t\tdfsb(b);\n\t\tll cola = 0;\n\t\tll colb = 0;\n\t\tfor (auto i : sa) {\n\t\t\tif (sb.find(i) == sb.end()) {\n\t\t\t\tcola += 1;\n\t\t\t}\n\t\t}\n\t\tfor (auto i : sb) {\n\t\t\tif (sa.find(i) == sa.end()) {\n\t\t\t\tcolb += 1;\n\t\t\t}\n\t\t}\n\t\tcout << cola * colb << endl;\n\t}\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "6f32d5a8f7313a4e497ef68cfed411c1", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n\n#define int long long\n\nusing namespace std;\n\nvector g[200000];\nbool used[200000];\nint num;\n\nvoid dfs(int v)\n{\n used[v] = true;\n num++;\n for (int i = 0; i < g[v].size(); i++)\n {\n if (!used[g[v][i]])\n {\n dfs(g[v][i]);\n }\n }\n}\n\nsigned main() {\n int t;\n cin >> t;\n for (int q = 0; q < t; q++)\n {\n int n, m, a, b;\n cin >> n >> m >> a >> b;\n a--;\n b--;\n for (int i = 0; i < m; i++)\n {\n int f, t;\n cin >> f >> t;\n f--;\n t--;\n g[f].push_back(t);\n g[t].push_back(f);\n }\n \n used[a] = true;\n int cnt = 0;\n for (int i = 0; i < n; i++)\n {\n if (!used[i])\n {\n cnt++;\n dfs(i);\n }\n }\n if (cnt == 1)\n {\n cout << 0 << '\\n';\n for (int i = 0; i < n; i++)\n {\n g[i].clear();\n used[i] = false;\n }\n continue;\n }\n for (int i = 0; i < n; i++)\n {\n used[i] = false;\n }\n \n used[b] = true;\n cnt = 0;\n for (int i = 0; i < n; i++)\n {\n if (!used[i])\n {\n cnt++;\n dfs(i);\n }\n }\n if (cnt == 1)\n {\n cout << 0 << '\\n';\n for (int i = 0; i < n; i++)\n {\n g[i].clear();\n used[i] = false;\n }\n continue;\n }\n for (int i = 0; i < n; i++)\n {\n used[i] = false;\n }\n \n int m1 = 0, m2 = 0;\n \n used[a] = true;\n num = 0;\n dfs(b);\n m1 = n - num - 1;\n for (int i = 0; i < n; i++)\n {\n used[i] = false;\n }\n \n used[b] = true;\n num = 0;\n dfs(a);\n m2 = n - num - 1;\n for (int i = 0; i < n; i++)\n {\n g[i].clear();\n used[i] = false;\n }\n \n cout << m1 * m2 << '\\n';\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "graphs"], "code_uid": "8d2c4ce0268a1d52c508ce802d85d281", "src_uid": "7636c493ad91210ec7571895b4b71214", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//lxrtxdy!\n#include \n\nusing namespace std;\n\nlong long C[777][777];\n\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\n\nvoid dfs(long long x, long long y, int v1, int v2) {\n long long diff = abs(x - y);\n if (best <= 1 || diff - C[v1][r] * C[v2][r] >= best) {\n return;\n }\n if (v1 == r - 1 && v2 == r - 1) {\n long long diff = abs(x - y);\n if (diff < best) {\n best = diff;\n best_z = x;\n }\n return;\n }\n if (v1 >= r) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n dfs(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (v2 >= r) {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n dfs(x + sum1 * cur2, y, v1, v2 - 1);\n }\n}\n\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\n\nlong long solve(long long x, long long y, int v1, int v2) {\n long long state = x * 10000 + v1 * 100 + v2;\n if (mp.find(state) != mp.end()) {\n return mp[state];\n }\n long long &res = mp[state];\n long long diff = abs(x - y);\n if (diff - C[v1][r] * C[v2][r] > best) {\n return res = 0;\n }\n if (v1 < r || v2 < r) {\n if (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) {\n int sum = v1 + v2;\n v1 -= cntA[sum];\n v2 -= cntB[sum];\n return res = C[v1 + v2][v1]; \n }\n return res = 0;\n }\n res = 0;\n char c = s[v1 + v2 - 1];\n if (c != 'B') {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n res += solve(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (c != 'A') {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n res += solve(x + sum1 * cur2, y, v1, v2 - 1);\n }\n return res;\n}\n\nint main() {\n scanf(\"%d %d\", &r, &c);\n for (int i = 0; i <= 2 * (r + c); i++) {\n for (int j = 0; j <= 2 * (r + c); j++) {\n if (j == 0) C[i][j] = 1; else\n if (i == 0) C[i][j] = 0;\n else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n }\n }\n total = C[r + c][r] * C[r + c][r];\n {\n long long x = 0, y = 0;\n int v1 = r + c, v2 = r + c;\n while (v1 >= r && v2 >= r) {\n if (x > y) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n y += cur1 * sum2;\n v1--;\n } else {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n x += sum1 * cur2;\n v2--;\n }\n }\n best = abs(x - y);\n }\n dfs(0, 0, r + c, r + c);\n scanf(\"%s\", s);\n cntA[0] = cntB[0] = 0;\n for (int i = 0; i < 2 * (r + c); i++) {\n cntA[i + 1] = cntA[i] + (s[i] == 'A');\n cntB[i + 1] = cntB[i] + (s[i] == 'B');\n }\n long long ans = solve(0, 0, r + c, r + c);\n cout << ans << endl;\n return 0;\n}\n//orzlxr!", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "9a50dae87d893e90e84c86ae10af8ddb", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nlong long C[777][777];\n\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\n\nvoid dfs(long long x, long long y, int v1, int v2)\n{\n\tlong long diff = abs(x - y);\n\tif (best <= 1 || diff - C[v1][r] * C[v2][r] >= best)\n\t\treturn;\n\t\t\n if (v1 == r - 1 && v2 == r - 1)\n {\n long long diff = abs(x - y); \n if (diff < best) {\n best = diff;\n best_z = x;\n }\n return;\n }\n\tif (v1 >= r)\n\t\t{\n \tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n \tdfs(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (v2 >= r)\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tdfs(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n}\n\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\n\nlong long solve(long long x, long long y, int v1, int v2)\n{\n\tlong long state = x * 10000 + v1 * 100 + v2;\n\tif(mp.find(state) != mp.end())\n\t\treturn mp[state];\n\t\t\n long long &res = mp[state];\n long long diff = abs(x - y);\n if (diff - C[v1][r] * C[v2][r] > best) {\n return res = 0;\n }\n if (v1 < r || v2 < r) {\n if (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) {\n int sum = v1 + v2;\n v1 -= cntA[sum];\n v2 -= cntB[sum];\n return res = C[v1 + v2][v1]; \n }\n return res = 0;\n }\n\tres = 0;\n\tchar c = s[v1 + v2 - 1];\n\tif (c != 'B')\n\t{\n\t\tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n\t\tres += solve(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (c != 'A')\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tres += solve(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n\treturn res;\n}\n\nint main() {\n scanf(\"%d %d\", &r, &c);\n for (int i = 0; i <= 2 * (r + c); i++) {\n for (int j = 0; j <= 2 * (r + c); j++) {\n if (j == 0) C[i][j] = 1; else\n if (i == 0) C[i][j] = 0;\n else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n }\n }\n total = C[r + c][r] * C[r + c][r];\n {\n long long x = 0, y = 0;\n int v1 = r + c, v2 = r + c;\n while (v1 >= r && v2 >= r) {\n if (x > y) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n y += cur1 * sum2;\n v1--;\n } else {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n x += sum1 * cur2;\n v2--;\n }\n }\n best = abs(x - y);\n }\n dfs(0, 0, r + c, r + c);\n scanf(\"%s\", s);\n cntA[0] = cntB[0] = 0;\n for (int i = 0; i < 2 * (r + c); i++) {\n cntA[i + 1] = cntA[i] + (s[i] == 'A');\n cntB[i + 1] = cntB[i] + (s[i] == 'B');\n }\n long long ans = solve(0, 0, r + c, r + c);\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "420d6f27af6a34a74b780dbd5bdbcd86", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#ifndef ONLINE_JUDGE\n# include \n# include \n#endif\n\n/*** TEMPLATE CODE STARTS HERE ***/\n\n#ifndef M_PI\n#define M_PI 3.1415926535897932384626433832795028841971693993751\n#endif\n\nusing namespace std;\n\ntypedef vector vs;\ntypedef long long ll;\ntypedef complex pnt;\ntypedef vector vi;\ntypedef vector vll;\ntypedef pair pii;\ntypedef pair pll;\n\n#define RA(x) begin(x), end(x)\n#define FE(i, x) for (auto i = begin(x); i != end(x); ++i)\n#define SZ(x) ((ll) (x).size())\n\ntemplate\nvoid splitstr(const string &s, vector &out)\n{\n istringstream in(s);\n out.clear();\n copy(istream_iterator(in), istream_iterator(), back_inserter(out));\n}\n\ntemplate T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n\nstatic void redirect(int argc, const char **argv)\n{\n#ifndef ONLINE_JUDGE\n struct rlimit rlim;\n getrlimit(RLIMIT_STACK, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_STACK, &rlim);\n#ifndef __SANITIZE_ADDRESS__\n getrlimit(RLIMIT_DATA, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_DATA, &rlim);\n#endif\n#endif\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n if (argc > 1)\n {\n static filebuf f;\n f.open(argv[1], ios::in);\n cin.rdbuf(&f);\n if (!cin)\n {\n cerr << \"Failed to open '\" << argv[1] << \"'\" << endl;\n exit(1);\n }\n }\n\n if (argc > 2)\n {\n static filebuf f;\n f.open(argv[2], ios::out | ios::trunc);\n cout.rdbuf(&f);\n if (!cout)\n {\n cerr << \"Failed to open '\" << argv[2] << \"'\" << endl;\n }\n }\n}\n\n/*** TEMPLATE CODE ENDS HERE */\n\nstatic vector chooset;\n\nstatic ll choose(int n, int r)\n{\n assert(n <= SZ(chooset));\n if (r < 0 || r > n)\n return 0;\n else\n return chooset[n][r];\n}\n\nstatic int R, C;\n\nstatic ll bit(int x)\n{\n return 1LL << x;\n}\n\nstatic void recurse(int L, int N, int a, int na, ll req, ll excl, ll cur, vll &sums)\n{\n if (na > N - L)\n return;\n else if (L == N)\n {\n sums.push_back(cur);\n return;\n }\n\n ll mask = bit(L);\n if (!(req & mask))\n recurse(L + 1, N, a, na, req, excl, cur, sums);\n if (na > 0 && !(excl & mask))\n {\n ll nxt = cur + choose(a, R - 1) * choose(L - a, R);\n recurse(L + 1, N, a + 1, na - 1, req, excl, nxt, sums);\n }\n}\n\nstatic pll solve(const string &pattern)\n{\n int N = SZ(pattern);\n ll base = 0;\n for (int i = 0; i < R + C; i++)\n base += choose(i, R - 1) * choose(N - i - 1, R);\n ll unfair = LLONG_MAX;\n ll trg = 2 * base - choose(N, 2 * R);\n\n ll req = 0, excl = 0;\n ll ans = 0;\n for (int i = 0; i < N; i++)\n {\n if (pattern[i] == 'A')\n req |= bit(i);\n else if (pattern[i] == 'B')\n excl |= bit(i);\n }\n\n vector dp(R + C + 1, vll(R + C + 1));\n dp[0][0] = 1;\n for (int j = 0; j <= R + C; j++)\n for (int k = 0; k <= R + C; k++)\n {\n if (j >= R - 1 && k >= R)\n break;\n if (j < R + C && pattern[j + k] != 'B')\n dp[j + 1][k] += dp[j][k];\n if (k < R + C && pattern[j + k] != 'A')\n dp[j][k + 1] += dp[j][k];\n }\n\n int M = N - C;\n vector asums2(R + C + 1);\n for (int al = R - 1; al <= R + C; al++)\n {\n recurse(M, N, al, R + C - al, req, excl, 0, asums2[al]);\n sort(RA(asums2[al]), greater());\n }\n for (int a = R - 1; a <= R + C; a++)\n for (int b = R; b <= R + C; b++)\n {\n if (a > R - 1 && b > R)\n continue;\n if (dp[a][b] == 0)\n continue;\n int L = a + b;\n for (int l = 0; l <= M - L; l++)\n {\n int r = R + C - a - l;\n if (r > N - M || r < 0)\n continue;\n vll sums1;\n recurse(L, M, a, l, req, excl, 0, sums1);\n auto &sums2 = asums2[a + l];\n sort(RA(sums1));\n int p = 0, q = 0;\n int p2 = 0, q2 = 0;\n while (p < SZ(sums1) && q < SZ(sums2))\n {\n while (p2 < SZ(sums1) && sums1[p2] == sums1[p])\n p2++;\n while (q2 < SZ(sums2) && sums2[q2] == sums2[q])\n q2++;\n ll total = 2 * (sums1[p] + sums2[q]);\n ll diff = llabs(total - trg);\n if (diff < unfair)\n {\n unfair = diff;\n ans = 0;\n }\n if (diff == unfair)\n ans += ll(p2 - p) * ll(q2 - q) * dp[a][b];\n if (total > trg)\n q = q2;\n else\n p = p2;\n }\n }\n }\n return pll(unfair, ans);\n}\n\nint main(int argc, const char **argv)\n{\n redirect(argc, argv);\n string pattern;\n cin >> R >> C >> pattern;\n int N = 2 * (R + C);\n chooset.resize(N + 1);\n for (int i = 0; i <= N; i++)\n {\n chooset[i].resize(i + 1);\n chooset[i][0] = chooset[i][i] = 1;\n for (int j = 1; j < i; j++)\n chooset[i][j] = chooset[i - 1][j - 1] + chooset[i - 1][j];\n }\n\n pll opt = solve(string(N, '?'));\n pll score = solve(pattern);\n // cerr << opt.first << ' ' << opt.second << ' ' << score.first << ' ' << score.second << '\\n';\n if (score.first == opt.first)\n cout << score.second << '\\n';\n else\n cout << \"0\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "b248351675c43a2dd0546cdabf508ce2", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\n#define REP(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)\n#define debug(...) fprintf(stderr, __VA_ARGS__)\n#define mp make_pair\n#define x first\n#define y second\n#define pb push_back\n#define SZ(x) (int((x).size()))\n#define ALL(x) (x).begin(), (x).end()\n\n#define ctz __builtin_ctz\n#define bcnt __builtin_popcount\n\ntemplate inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }\ntemplate inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }\n\ntypedef long long LL;\n\nconst int oo = 0x3f3f3f3f;\n\nconst int maxN = 70;\n\nconst int maxn = 50;\n\nconst int lim = 22;\n\nLL C[maxN + 5][maxN + 5];\n\ninline void prepare()\n{\n\tREP(i, 0, maxN)\n\t{\n\t\tC[i][0] = 1;\n\t\tREP(j, 1, i + 1)\n\t\t{\n\t\t\tC[i][j] = C[i - 1][j - 1] + C[i - 1][j];\n\t\t}\n\t}\n}\n\nint n, m, N;\n\nint mid;\nint g[maxN + 5];\n\nvector > partb[maxn + 5][maxn + 5];\n\nvoid dfs_partb(int x, int cnta, int cntb, LL sum = 0)\n{\n\tif (x == mid) \n\t{\n\t\tpartb[cnta][cntb].pb(mp(sum, 1));\n\t\treturn;\n\t}\n\tREP(i, 0, 2)\n\t{\n\t\tif (~g[x - 1] && i != g[x - 1]) continue;\n\t\tLL tmp = 0;\n\t\tif (i == 0)\n\t\t{\n\t\t\tif (cnta > 0)\n\t\t\t{\n\t\t\t\tREP(j, 0, n) tmp += C[cnta - 1][n - 1] * C[cntb][j] * C[N - x][n - j];\n\t\t\t\tdfs_partb(x - 1, cnta - 1, cntb, sum + tmp);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (cntb > 0)\n\t\t\t{\n\t\t\t\tREP(j, 0, n) tmp -= C[cntb - 1][n - 1] * C[cnta][j] * C[N - x][n - j];\n\t\t\t\tdfs_partb(x - 1, cnta, cntb - 1, sum + tmp);\n\t\t\t}\n\t\t}\n\t}\n}\n\nmap parta[maxn + 5][maxn + 5];\n\nLL ans = LLONG_MAX, ans_cnt = 0;\n\ninline void update(const LL &val, const LL &cnt)\n{\n\tif (chkmin(ans, val)) ans_cnt = cnt;\n\telse if (val == ans) ans_cnt += cnt;\n}\n\nvoid bfs_parta()\n{\n\tREP(i, 0, n + m + 1) REP(j, 0, n + m + 1) parta[i][j].clear();\n\tparta[0][0][0] = 1;\n\tREP(i, 0, n + m + 1) REP(j, 0, n + m + 1) \n\t\tfor (auto u : parta[i][j])\n\t\t{\n\t\t\tif (i + j == mid)\n\t\t\t{\n\t\t\t\tint p = lower_bound(ALL(partb[i][j]), mp(-u.x, -1)) - partb[i][j].begin();\n\t\t\t\tif (p < SZ(partb[i][j])) update(abs(u.x + partb[i][j][p].x), u.y * partb[i][j][p].y);\n\t\t\t\tif (p)\n\t\t\t\t{\n\t\t\t\t\tupdate(abs(u.x + partb[i][j][p - 1].x), u.y * partb[i][j][p - 1].y);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint p = i + j;\n\t\t\t\tREP(k, 0, 2)\n\t\t\t\t{ \n\t\t\t\t\tif (~g[p] && k != g[p]) continue;\n\t\t\t\t\tint cnta = i, cntb = j;\n\t\t\t\t\tif (k == 0) ++cnta;\n\t\t\t\t\telse ++cntb;\n\t\t\t\t\tif (cnta <= n + m && cntb <= n + m)\n\t\t\t\t\t{\n\t\t\t\t\t\tLL tmp = 0;\n\t\t\t\t\t\tif (k == 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tREP(l, 0, n) tmp += C[cnta - 1][n - 1] * C[cntb][l] * C[N - p - 1][n - l];\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tREP(l, 0, n) tmp -= C[cntb - 1][n - 1] * C[cnta][l] * C[N - p - 1][n - l];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tparta[cnta][cntb][u.x + tmp] += u.y;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n}\n\ninline void work()\n{\n\tREP(i, 0, n + m + 1) REP(j, 0, n + m + 1) partb[i][j].clear();\n\tdfs_partb(N, n + m, n + m);\n\tREP(i, 0, n + m + 1) REP(j, 0, n + m + 1)\n\t{\n\t\tsort(ALL(partb[i][j]));\n\t\tvector > tmp;\n\t\tint sz = SZ(partb[i][j]);\n\t\tfor (int k = 0; k < sz; )\n\t\t{\n\t\t\tLL cur = partb[i][j][k].x;\n\t\t\tint cnt = 0;\n\t\t\twhile (k < sz && partb[i][j][k].x == cur) cnt += partb[i][j][k].y, ++k;\n\t\t\ttmp.pb(mp(cur, cnt));\n\t\t}\n\t\tpartb[i][j] = tmp;\n\t}\n\tbfs_parta();\n}\n\nint main()\n{\n#ifdef matthew99\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n\tprepare();\n\tstatic char s[maxN + 5];\n\tscanf(\"%d%d%s\", &n, &m, s);\n\tN = (n + m) << 1;\n\tmid = max(0, N - lim);\n\tmemset(g, -1, sizeof g);\n\twork();\n\tans_cnt = 0;\n\tREP(i, 0, N) if (s[i] == 'A') g[i] = 0; else if (s[i] == 'B') g[i] = 1;\n\twork();\n\tcout << ans_cnt << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "03ba74f186c30028c0a8d6b2ddffb9a0", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-2);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[n][y];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea2 = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea2 > m2) continue;\t\t\t\t\t\n\t\t\t\t\tbp[x+1][y][it->first + newarea2] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "883724ced69ec9a7e82833eb611e4da6", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL c = it->first+newarea;\n\t\t\t\t\tLL area2 = totalarea[x+1][n] - c;\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\t\t\t\t\tbp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "62326c3bcde3da3c727c8180835752fa", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nlong long C[777][777];\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\nvoid dfs(long long x, long long y, int v1, int v2)\n{\n\tlong long diff = abs(x - y);\n\tif (best <= 1 || diff - C[v1][r] * C[v2][r] >= best)return;\t\n\tif (v1 == r - 1 && v2 == r - 1)\n\t{\n\t\tlong long diff = abs(x - y);\n\t\tif (diff < best) \n\t\t{\n\t\t\tbest = diff;\n\t\t\tbest_z = x;\n\t\t}\n\t\treturn;\n\t}\n\tif (v1 >= r)\n\t{\n\t\tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n\t\tdfs(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (v2 >= r)\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tdfs(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n}\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\nlong long solve(long long x, long long y, int v1, int v2)\n{\n\tlong long state = x * 10000 + v1 * 100 + v2;\n\tif(mp.find(state) != mp.end())return mp[state];\t\n\tlong long &res = mp[state];\n\tlong long diff = abs(x - y);\n\tif (diff - C[v1][r] * C[v2][r] > best) \n\t{\n\t\treturn res = 0;\n\t}\n\tif (v1 < r || v2 < r) \n\t{\n\t\tif (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) \n\t\t{\n\t\t\tint sum = v1 + v2;\n\t\t\tv1 -= cntA[sum];\n\t\t\tv2 -= cntB[sum];\n\t\t\treturn res = C[v1 + v2][v1]; \n\t\t}\n\t\treturn res = 0;\n\t}\n\tres = 0;\n\tchar c = s[v1 + v2 - 1];\n\tif (c != 'B')\n\t{\n\t\tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n\t\tres += solve(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (c != 'A')\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tres += solve(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n\treturn res;\n}\nint main() \n{\n\tscanf(\"%d %d\", &r, &c);\n\tfor (int i = 0; i <= 2 * (r + c); i++) \n\t{\n\t\tfor (int j = 0; j <= 2 * (r + c); j++) \n\t\t{\n\t\t\tif (j == 0) C[i][j] = 1; else\n\t\t\tif (i == 0) C[i][j] = 0;\n\t\t\telse C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n\t\t}\n\t}\n\ttotal = C[r + c][r] * C[r + c][r];\n\t{\n\t\tlong long x = 0, y = 0;\n\t\tint v1 = r + c, v2 = r + c;\n\t\twhile (v1 >= r && v2 >= r) \n\t\t{\n\t\t\tif (x > y) \n\t\t\t{\n\t\t\t\tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\t\t\tlong long sum2 = C[v2][r];\n\t\t\t\ty += cur1 * sum2;\n\t\t\t\tv1--;\n\t\t\t} \n\t\t\telse \n\t\t\t{\n\t\t\t\tlong long sum1 = C[v1][r];\n\t\t\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\t\t\tx += sum1 * cur2;\n\t\t\t\tv2--;\n\t\t\t}\n\t\t}\n\t\tbest = abs(x - y);\n\t}\n\tdfs(0, 0, r + c, r + c);\n\tscanf(\"%s\", s);\n\tcntA[0] = cntB[0] = 0;\n\tfor (int i = 0; i < 2 * (r + c); i++) \n\t{\n\t\tcntA[i + 1] = cntA[i] + (s[i] == 'A');\n\t\tcntB[i + 1] = cntB[i] + (s[i] == 'B');\n\t}\n\tlong long ans = solve(0, 0, r + c, r + c);\n\tcout << ans << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "3857dbb9770ca2c75bc83beeeca28e03", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize (\"O3\")\n#pragma GCC target (\"sse4\")\n\n#include \n#include \n#include \n#include \n\nusing namespace std;\nusing namespace __gnu_pbds;\nusing namespace __gnu_cxx;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef complex cd;\n\ntypedef pair pi;\ntypedef pair pl;\ntypedef pair pd;\n\ntypedef vector vi;\ntypedef vector vd;\ntypedef vector vl;\ntypedef vector vpi;\ntypedef vector vpl;\ntypedef vector vcd;\n\ntemplate using Tree = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define F0R(i, a) for (int i = 0; i < (a); i++)\n#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)\n#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)\n#define trav(a, x) for (auto& a : x)\n\n#define mp make_pair\n#define pb push_back\n#define f first\n#define s second\n#define lb lower_bound\n#define ub upper_bound\n\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rsz resize\n\nconst int MOD = 1000000007; // 998244353\nconst ll INF = 1e18;\nconst int MX = 200005;\nconst ld PI = 4*atan((ld)1);\n\ntemplate void ckmin(T &a, T b) { a = min(a, b); }\ntemplate void ckmax(T &a, T b) { a = max(a, b); }\n\nnamespace input {\n template void re(complex& x);\n template void re(pair& p);\n template void re(vector& a);\n template void re(array& a);\n\n template void re(T& x) { cin >> x; }\n void re(double& x) { string t; re(t); x = stod(t); }\n void re(ld& x) { string t; re(t); x = stold(t); }\n template void re(Arg& first, Args&... rest) { \n re(first); re(rest...); \n }\n\n template void re(complex& x) { T a,b; re(a,b); x = cd(a,b); }\n template void re(pair& p) { re(p.f,p.s); }\n template void re(vector& a) { F0R(i,sz(a)) re(a[i]); }\n template void re(array& a) { F0R(i,SZ) re(a[i]); }\n}\n\nusing namespace input;\n\nnamespace output {\n template void pr(const pair& x);\n template void pr(const array& x);\n template void pr(const vector& x);\n template void pr(const set& x);\n template void pr(const map& x);\n\n template void pr(const T& x) { cout << x; }\n template void pr(const Arg& first, const Args&... rest) { \n pr(first); pr(rest...); \n }\n\n template void pr(const pair& x) { \n pr(\"{\",x.f,\", \",x.s,\"}\"); \n }\n template void prContain(const T& x) {\n pr(\"{\");\n bool fst = 1; for (const auto& a: x) pr(!fst?\", \":\"\",a), fst = 0; // const needed for vector\n pr(\"}\");\n }\n template void pr(const array& x) { prContain(x); }\n template void pr(const vector& x) { prContain(x); }\n template void pr(const set& x) { prContain(x); }\n template void pr(const map& x) { prContain(x); }\n \n void ps() { pr(\"\\n\"); }\n template void ps(const Arg& first) { \n pr(first); ps(); // no space at end of line\n }\n template void ps(const Arg& first, const Args&... rest) { \n pr(first,\" \"); ps(rest...); // print w/ spaces\n }\n}\n\nusing namespace output;\n\nnamespace io {\n void setIn(string s) { freopen(s.c_str(),\"r\",stdin); }\n void setOut(string s) { freopen(s.c_str(),\"w\",stdout); }\n void setIO(string s = \"\") {\n ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O\n if (sz(s)) { setIn(s+\".in\"), setOut(s+\".out\"); } // for USACO\n }\n}\n\nusing namespace io;\n\ntemplate T invGeneral(T a, T b) {\n a %= b; if (a == 0) return b == 1 ? 0 : -1;\n T x = invGeneral(b,a); \n return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;\n}\n\ntemplate struct modular {\n T val; \n explicit operator T() const { return val; }\n modular() { val = 0; }\n modular(const ll& v) { \n val = (-MOD <= v && v <= MOD) ? v : v % MOD;\n if (val < 0) val += MOD;\n }\n \n friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }\n friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }\n friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }\n friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }\n\n modular operator-() const { return modular(-val); }\n modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }\n modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }\n modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }\n friend modular pow(modular a, ll p) {\n modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;\n return ans;\n }\n friend modular inv(const modular& a) { \n auto i = invGeneral(a.val,MOD); assert(i != -1);\n return i;\n } // equivalent to return exp(b,MOD-2) if MOD is prime\n modular& operator/=(const modular& m) { return (*this) *= inv(m); }\n \n friend modular operator+(modular a, const modular& b) { return a += b; }\n friend modular operator-(modular a, const modular& b) { return a -= b; }\n friend modular operator*(modular a, const modular& b) { return a *= b; }\n \n friend modular operator/(modular a, const modular& b) { return a /= b; }\n};\n\ntypedef modular mi;\ntypedef pair pmi;\ntypedef vector vmi;\ntypedef vector vpmi;\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nll comb[61][61];\nint R,C;\nstring S;\n\nll calc(string t) {\n\tll ans = 0;\n\tint a = 0, b = 0;\n\tF0R(i,sz(t)) {\n\t\tif (t[i] == 'A') {\n\t\t\ta ++;\n\t\t\tans += (comb[a][R]-comb[a-1][R])*(comb[R+C][R]-comb[b][R]);\n\t\t} else {\n\t\t\tb ++;\n\t\t\tans -= (comb[b][R]-comb[b-1][R])*(comb[R+C][R]-comb[a][R]);\n\t\t}\n\t}\n\tif (a != b) return INF;\n\treturn ans;\n}\n\nbool match(string t) {\n\tF0R(i,sz(S)) if (!(S[i] == '?' || S[i] == t[i])) return 0;\n\treturn 1;\n}\n\nvoid init() {\n setIO(); re(R,C,S);\n comb[0][0] = 1;\n FOR(i,1,61) {\n \tF0R(j,i+1) {\n \t\tcomb[i][j] = comb[i-1][j];\n \t\tif (j) comb[i][j] += comb[i-1][j-1];\n \t}\n }\n /*ll dif = INF;\n F0R(i,1<= 0) ind ++;\n\t\t\tckmin(dif,abs(t.f+dpR[i][ind].f));\n\t\t\tif (ind+1 < sz(dpR[i])) ckmin(dif,abs(t.f+dpR[i][ind+1].f));\n\t\t}\n\t}\n\tF0R(i,R+C+1) {\n\t\tvpl z;\n\t\ttrav(t,dpR[i]) if (ok(rev(t.s,C+1))) z.pb(t);\n\t\tint ind = 0;\n\t\tint l = 0, r = -1;\n\t\ttrav(t,dpL[i]) {\n\t\t\twhile (l < sz(z) && t.f+z[l].f > dif) l ++;\n\t\t\twhile (r+1 < sz(z) && t.f+z[r+1].f >= -dif) r ++;\n\t\t\tif (l <= r) {\n\t\t\t\t//ps(\"TT\",__builtin_popcount(t.s),__builtin_popcount(z[l].s));\n\t\t\t\t//ps(\"TT\",t.s<(1<<(C+1)),t.s|(rev(z[l].s,C+1)<<(C+1)),__builtin_popcountll(t.s|(rev(z[l].s,C+1)<<(C+1))));\n\t\t\t\tcheck(t.s|(rev(z[l].s,C+1)<<(C+1)),r-l+1);\n\t\t\t}\n\t\t}\n\t}\n\t// ps(res);\n\t// ps(res);\n\tps(ans);\n}\n\n/* stuff you should look for\n * int overflow, array bounds\n * special cases (n=1?), set tle\n * do smth instead of nothing and stay organized\n*/", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "60b8faedfeb31c19ffb0d617af4976ba", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-2);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL c = it->first+newarea;\n\t\t\t\t\tLL area2 = totalarea[x+1][n] - c;\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea2 = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea2 > m2) continue;\t\t\t\t\t\n\t\t\t\t\tbp[x+1][y][it->first + newarea2] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "e6deef101f19662b60e30871768cf604", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-2);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL c = it->first+newarea;\n\t\t\t\t\tLL area2 = totalarea[x+1][n] - c;\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\t\t\t\t\tbp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "4fc37d202985caa0176add0b6d0945c7", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-2);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\t\t\t\t\t\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea2 = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea2 > m2) continue;\t\t\t\t\t\n\t\t\t\t\tbp[x+1][y][it->first + newarea2] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "662b3dcc72a57c607b337bcd34964005", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-2);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea2 = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea2 > m2) continue;\t\t\t\t\t\n\t\t\t\t\tbp[x+1][y][it->first + newarea2] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "48eedb6af8f69f3987f88035278bb165", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#define pll pair\nusing namespace std;\nlong long Comb[62][62], A[32][32], B[32][32], TT;\nint R, C, n;\nchar p[110];\nlong long CC[32][32], MM;\nvector< pll >P[32][2], Q[32][2];\nvoid Put(int x, int y, long long g, long long c1, long long c2, int ck){\n if(x+y == R+n){\n P[x][ck].push_back({g,c1});\n if(c2)Q[x][ck].push_back({g,c2});\n \n return;\n }\n Put(x+1, y, g+(!ck?A[x+1][y]:0), c1, (p[x+y] != 'B' ? c2 : 0), ck);\n Put(x, y+1, g+(ck?B[x][y+1]:0), c1, (p[x+y] != 'A' ? c2 : 0), ck);\n}\nvoid Calc1(int x, int y, long long g, int ck){\n if(x+y == R+n){\n if(P[x][ck].empty())return;\n int t = lower_bound(P[x][ck].begin(),P[x][ck].end(), pll((TT+1)/2-g,0)) - P[x][ck].begin();\n if(t != P[x][ck].size() && P[x][ck][t].second > 0){\n long long tp = (P[x][ck][t].first + g) * 2 - TT;\n if(tp<0)tp=-tp;\n if(MM > tp)MM = tp;\n }\n if(t && P[x][ck][t-1].second > 0){\n long long tp = (P[x][ck][t-1].first + g) * 2 - TT;\n if(tp<0)tp=-tp;\n if(MM > tp)MM = tp;\n }\n return;\n }\n Calc1(x-1, y, g+(!ck?A[x][y]:0), ck);\n Calc1(x, y-1, g+(ck?B[x][y]:0), ck);\n}\n\nlong long Res;\n\nvoid Calc2(int x, int y, long long g, int ck){\n if(x+y == R+n){\n int t = lower_bound(Q[x][ck].begin(),Q[x][ck].end(), pll((TT+1)/2-g,0)) - Q[x][ck].begin();\n if(t != Q[x][ck].size() && Q[x][ck][t].second > 0){\n long long tp = (Q[x][ck][t].first + g) * 2 - TT;\n if(tp<0)tp=-tp;\n if(MM == tp){\n Res += Q[x][ck][t].second;\n }\n }\n if(t && Q[x][ck][t-1].second > 0){\n long long tp = (Q[x][ck][t-1].first + g) * 2 - TT;\n if(tp<0)tp=-tp;\n if(MM == tp){\n Res += Q[x][ck][t-1].second;\n }\n }\n return;\n }\n if(p[x+y-1] != 'B')Calc2(x-1, y, g+(!ck?A[x][y]:0), ck);\n if(p[x+y-1] != 'A')Calc2(x, y-1, g+(ck?B[x][y]:0), ck);\n}\n\nvoid Make(vector< pll > &P){\n if(P.empty())return;\n sort(P.begin(),P.end());\n int i, cnt = 0;\n long long s = 0;\n for(i=0;i\n\nusing namespace std;\n\nlong long C[777][777];\n\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\n\nvoid dfs(long long x, long long y, int v1, int v2) {\n long long diff = abs(x - y);\n if (best <= 1 || diff - C[v1][r] * C[v2][r] >= best) {\n return;\n }\n if (v1 == r - 1 && v2 == r - 1) {\n long long diff = abs(x - y);\n if (diff < best) {\n best = diff;\n best_z = x;\n }\n return;\n }\n if (v1 >= r) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n dfs(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (v2 >= r) {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n dfs(x + sum1 * cur2, y, v1, v2 - 1);\n }\n}\n\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\n\nlong long solve(long long x, long long y, int v1, int v2) {\n long long state = x * 10000 + v1 * 100 + v2;\n if (mp.find(state) != mp.end()) {\n return mp[state];\n }\n long long &res = mp[state];\n long long diff = abs(x - y);\n if (diff - C[v1][r] * C[v2][r] > best) {\n return res = 0;\n }\n if (v1 < r || v2 < r) {\n if (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) {\n int sum = v1 + v2;\n v1 -= cntA[sum];\n v2 -= cntB[sum];\n return res = C[v1 + v2][v1]; \n }\n return res = 0;\n }\n res = 0;\n char c = s[v1 + v2 - 1];\n if (c != 'B') {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n res += solve(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (c != 'A') {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n res += solve(x + sum1 * cur2, y, v1, v2 - 1);\n }\n return res;\n}\n\nint main() {\n scanf(\"%d %d\", &r, &c);\n for (int i = 0; i <= 2 * (r + c); i++) {\n for (int j = 0; j <= 2 * (r + c); j++) {\n if (j == 0) C[i][j] = 1; else\n if (i == 0) C[i][j] = 0;\n else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n }\n }\n total = C[r + c][r] * C[r + c][r];\n {\n long long x = 0, y = 0;\n int v1 = r + c, v2 = r + c;\n while (v1 >= r && v2 >= r) {\n if (x > y) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n y += cur1 * sum2;\n v1--;\n } else {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n x += sum1 * cur2;\n v2--;\n }\n }\n best = abs(x - y);\n }\n dfs(0, 0, r + c, r + c);\n scanf(\"%s\", s);\n cntA[0] = cntB[0] = 0;\n for (int i = 0; i < 2 * (r + c); i++) {\n cntA[i + 1] = cntA[i] + (s[i] == 'A');\n cntB[i + 1] = cntB[i] + (s[i] == 'B');\n }\n long long ans = solve(0, 0, r + c, r + c);\n cout << ans << endl;\n return 0;\n}\n//orzlxr!//hello", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "2125e017f188cacd7cb9099f9bbb114e", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nlong long C[777][777];\n\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\n\nvoid dfs(long long x, long long y, int v1, int v2)\n{\n\tlong long diff = abs(x - y);\n\tif (best <= 1 || diff - C[v1][r] * C[v2][r] >= best)\n\t\treturn;\n\t\t\n if (v1 == r - 1 && v2 == r - 1)\n {\n long long diff = abs(x - y);\n if (diff < best) {\n best = diff;\n best_z = x;\n }\n return;\n }\n\tif (v1 >= r)\n\t\t{\n \tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n \tdfs(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (v2 >= r)\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tdfs(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n}\n\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\n\nlong long solve(long long x, long long y, int v1, int v2)\n{\n\tlong long state = x * 10000 + v1 * 100 + v2;\n\tif(mp.find(state) != mp.end())\n\t\treturn mp[state];\n\t\t\n long long &res = mp[state];\n long long diff = abs(x - y);\n if (diff - C[v1][r] * C[v2][r] > best) {\n return res = 0;\n }\n if (v1 < r || v2 < r) {\n if (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) {\n int sum = v1 + v2;\n v1 -= cntA[sum];\n v2 -= cntB[sum];\n return res = C[v1 + v2][v1]; \n }\n return res = 0;\n }\n\tres = 0;\n\tchar c = s[v1 + v2 - 1];\n\tif (c != 'B')\n\t{\n\t\tlong long cur1 = C[v1][r] - C[v1 - 1][r];\n\t\tlong long sum2 = C[v2][r];\n\t\tres += solve(x, y + cur1 * sum2, v1 - 1, v2);\n\t}\n\tif (c != 'A')\n\t{\n\t\tlong long sum1 = C[v1][r];\n\t\tlong long cur2 = C[v2][r] - C[v2 - 1][r];\n\t\tres += solve(x + sum1 * cur2, y, v1, v2 - 1);\n\t}\n\treturn res;\n}\n\nint main() {\n scanf(\"%d %d\", &r, &c);\n for (int i = 0; i <= 2 * (r + c); i++) {\n for (int j = 0; j <= 2 * (r + c); j++) {\n if (j == 0) C[i][j] = 1; else\n if (i == 0) C[i][j] = 0;\n else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n }\n }\n total = C[r + c][r] * C[r + c][r];\n {\n long long x = 0, y = 0;\n int v1 = r + c, v2 = r + c;\n while (v1 >= r && v2 >= r) {\n if (x > y) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n y += cur1 * sum2;\n v1--;\n } else {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n x += sum1 * cur2;\n v2--;\n }\n }\n best = abs(x - y);\n }\n dfs(0, 0, r + c, r + c);\n scanf(\"%s\", s);\n cntA[0] = cntB[0] = 0;\n for (int i = 0; i < 2 * (r + c); i++) {\n cntA[i + 1] = cntA[i] + (s[i] == 'A');\n cntB[i + 1] = cntB[i] + (s[i] == 'B');\n }\n long long ans = solve(0, 0, r + c, r + c);\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "04a92e247cd55e620561be620a543d20", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long ll;\nconst int MAXT = 62;\n\nint R, C, T;\nchar c[MAXT];\nll r1score[MAXT];\nll rscore[MAXT];\nll lscore[MAXT];\nll mval;\n\nmap , ll> mmap;\nmap , ll> nmap;\n\nll choose (int x, int y)\n{\n if (y > x || y < 0) return 0;\n ll res = 1;\n for (int i = 0; i < y; i++)\n {\n res = res * (x - i);\n res = res / (i + 1);\n }\n return res;\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n\n cin >> R >> C;\n T = (2 * R + 2 * C);\n for (int i = 0; i < T; i++)\n cin >> c[i];\n\n for (int i = 0; i < MAXT; i++)\n lscore[i] = 0;\n lscore[2*R-1] = 1;\n for (int i = 2 * R; i < T; i++)\n {\n lscore[i] = lscore[i-1] * i / ((ll) i - 2 * R + 1);\n }\n for (int i = 0; i <= T; i++)\n {\n r1score[i] = choose (i, R - 1);\n rscore[i] = choose (i, R);\n }\n\n mval = 0;\n int acnt = 0, bcnt = 0;\n for (int i = T - 1; i >= 0; i--)\n {\n bool u = mval > 0;\n if (bcnt == T / 2)\n u = true;\n if (acnt == T / 2)\n u = false;\n\n if (u)\n {\n acnt++;\n mval -= r1score[T / 2 - acnt] * rscore[T / 2 - bcnt];\n }\n else\n {\n bcnt++;\n mval += r1score[T / 2 - bcnt] * rscore[T / 2 - acnt];\n }\n }\n mval = max(mval, -mval);\n\n ll tsum = 0;\n for (int i = 0; i < T; i++)\n {\n tsum += lscore[i];\n }\n\n tsum = rscore[T/2] * rscore[T/2];\n\n mmap[make_pair(0, 0)] = 1;\n for (int i = T - 1; i >= 2 * R - 1; i--)\n {\n nmap.clear();\n for (map, ll>::iterator it = mmap.begin(); it != mmap.end(); it++)\n {\n ll sum = it->first.first;\n ll acnt = it->first.second;\n ll ncount = it->second;\n\n int aleft = T / 2 - acnt;\n int bleft = i + 1- aleft;\n\n ll asum = sum + r1score[aleft - 1] * rscore[bleft];\n ll bsum = sum - r1score[bleft - 1] * rscore[aleft];\n\n if (c[i] == 'A')\n {\n nmap[make_pair(asum, acnt + 1)] += ncount;\n nmap[make_pair(bsum, acnt)] += 0;\n }\n else if (c[i] == 'B')\n {\n nmap[make_pair(asum, acnt + 1)] += 0;\n nmap[make_pair(bsum, acnt)] += ncount;\n }\n else\n {\n nmap[make_pair(asum, acnt + 1)] += ncount;\n nmap[make_pair(bsum, acnt)] += ncount;\n }\n }\n tsum -= lscore[i];\n\n mmap.clear();\n for (map, ll>::iterator it = nmap.begin(); it != nmap.end(); it++)\n {\n ll sum = it->first.first;\n int acnt = it->first.second;\n ll ncount = it->second;\n\n int aleft = T / 2 - acnt;\n int bleft = i - aleft;\n if (acnt > (T / 2) || (T - i - acnt) > (T / 2))\n continue; \n ll lsum = rscore[aleft] * rscore[bleft];\n if (sum < (-mval - lsum) || sum > (mval + lsum))\n continue;\n mmap[make_pair(sum, acnt)] += ncount;\n }\n }\n\n ll bscore = (ll) 1e18;\n for (map, ll>::iterator it = mmap.begin(); it != mmap.end(); it++)\n {\n ll sum = it->first.first;\n bscore = min (bscore, max (sum, -sum));\n }\n\n ll ans = 0;\n for (map, ll>::iterator it = mmap.begin(); it != mmap.end(); it++)\n {\n ll sum = it->first.first;\n int acnt = it->first.second;\n ll ncount = it->second;\n\n if (sum == bscore || sum == -bscore)\n {\n \n int nleft = 0;\n for (int i = 0; i < 2 * R - 1; i++)\n {\n if (c[i] == 'A')\n acnt++;\n if (c[i] == '?')\n nleft++;\n }\n\n ncount *= choose (nleft, T / 2 - acnt);\n ans += ncount;\n }\n }\n\n cout << ans << \"\\n\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "85a9ce26879c4bf13f73317d0a536a23", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n#define ll long long\nll lsc[62],rsc[62],rsc2[62];\nchar s[62];\nmap,ll>nm,mm;\nll comb(int n,int k)\n{\n if (n=0;i--)\n {\n if (msum>0)\n ok=1;\n else\n ok=0;\n if (cntb==n/2)\n ok=1;\n if (cnta==n/2)\n ok=0;\n if (ok)\n {\n cnta++;\n msum=msum-rsc2[n/2-cnta]*rsc[n/2-cntb];\n }\n else\n {\n cntb++;\n msum=msum+rsc2[n/2-cntb]*rsc[n/2-cnta];\n }\n }\n msum=max(msum,-msum);\n mm[make_pair(0,0)]=1;\n for(i=n-1;i>=2*r-1;i--)\n {\n nm.clear();\n for(auto it:mm)\n {\n ll sum=it.first.first,ncnt=it.second,asum,bsum;\n int cnt=it.first.second;\n ast=n/2-cnt;\n bst=i+1-ast;\n asum=sum+rsc2[ast-1]*rsc[bst];\n bsum=sum-rsc2[bst-1]*rsc[ast];\n if (s[i]=='A')\n {\n nm[make_pair(asum,cnt+1)]+=ncnt;\n nm[make_pair(bsum,cnt)]+=0;\n }\n else\n if (s[i]=='B')\n {\n nm[make_pair(asum,cnt+1)]+=0;\n nm[make_pair(bsum,cnt)]+=ncnt;\n }\n else\n {\n nm[make_pair(asum,cnt+1)]+=ncnt;\n nm[make_pair(bsum,cnt)]+=ncnt;\n }\n }\n nsum=nsum-lsc[i];\n mm.clear();\n for(auto it:nm)\n {\n ll sum=it.first.first,ncnt=it.second;\n int cnt=it.first.second;\n ast=n/2-cnt;\n bst=i-ast;\n if (cnt>n/2 || n-i-cnt>n/2)\n continue;\n if (sum<-msum-rsc[ast]*rsc[bst] || sum>msum+rsc[ast]*rsc[bst])\n continue;\n mm[make_pair(sum,cnt)]+=ncnt;\n }\n }\n bsum=1e18;\n for(auto it:mm)\n bsum=min(bsum,max(it.first.first,-it.first.first));\n for(auto it:mm)\n {\n ll sum=it.first.first,ncnt=it.second;\n int cnt=it.first.second;\n if (sum==bsum || sum==-bsum)\n {\n int sem=0;\n for(i=0;i<2*r-1;i++)\n if (s[i]=='?')\n sem++;\n else\n if (s[i]=='A')\n cnt++;\n ncnt=ncnt*comb(sem,n/2-cnt);\n rasp+=ncnt;\n }\n }\n printf(\"%lld\\n\",rasp);\nreturn 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "65eeaf62c52c9aa355bfeda4f62765ae", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-4);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL c = it->first+newarea;\n\t\t\t\t\tLL area2 = totalarea[x+1][n] - c;\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\t\t\t\t\tbp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "62357730f4b609fdff0654f4e3664bb4", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/**\n * author: tourist\n * created: 30.09.2017 21:16:15 \n**/\n#include \n\nusing namespace std;\n\nlong long C[777][777];\n\nint r, c;\nlong long total;\nlong long best = (long long) 1e18, best_z = -1;\n\nvoid dfs(long long x, long long y, int v1, int v2) {\n long long diff = abs(x - y);\n if (best <= 1 || diff - C[v1][r] * C[v2][r] >= best) {\n return;\n }\n if (v1 == r - 1 && v2 == r - 1) {\n long long diff = abs(x - y);\n if (diff < best) {\n best = diff;\n best_z = x;\n }\n return;\n }\n if (v1 >= r) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n dfs(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (v2 >= r) {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n dfs(x + sum1 * cur2, y, v1, v2 - 1);\n }\n}\n\nchar s[12345];\nint cntA[12345], cntB[12345];\nmap mp;\n\nlong long solve(long long x, long long y, int v1, int v2) {\n long long state = x * 10000 + v1 * 100 + v2;\n if (mp.find(state) != mp.end()) {\n return mp[state];\n }\n long long &res = mp[state];\n long long diff = abs(x - y);\n if (diff - C[v1][r] * C[v2][r] > best) {\n return res = 0;\n }\n if (v1 < r || v2 < r) {\n if (v1 >= cntA[v1 + v2] && v2 >= cntB[v1 + v2]) {\n int sum = v1 + v2;\n v1 -= cntA[sum];\n v2 -= cntB[sum];\n return res = C[v1 + v2][v1]; \n }\n return res = 0;\n }\n res = 0;\n char c = s[v1 + v2 - 1];\n if (c != 'B') {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n res += solve(x, y + cur1 * sum2, v1 - 1, v2);\n }\n if (c != 'A') {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n res += solve(x + sum1 * cur2, y, v1, v2 - 1);\n }\n return res;\n}\n\nint main() {\n scanf(\"%d %d\", &r, &c);\n for (int i = 0; i <= 2 * (r + c); i++) {\n for (int j = 0; j <= 2 * (r + c); j++) {\n if (j == 0) C[i][j] = 1; else\n if (i == 0) C[i][j] = 0;\n else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);\n }\n }\n total = C[r + c][r] * C[r + c][r];\n {\n long long x = 0, y = 0;\n int v1 = r + c, v2 = r + c;\n while (v1 >= r && v2 >= r) {\n if (x > y) {\n long long cur1 = C[v1][r] - C[v1 - 1][r];\n long long sum2 = C[v2][r];\n y += cur1 * sum2;\n v1--;\n } else {\n long long sum1 = C[v1][r];\n long long cur2 = C[v2][r] - C[v2 - 1][r];\n x += sum1 * cur2;\n v2--;\n }\n }\n best = abs(x - y);\n }\n dfs(0, 0, r + c, r + c);\n scanf(\"%s\", s);\n cntA[0] = cntB[0] = 0;\n for (int i = 0; i < 2 * (r + c); i++) {\n cntA[i + 1] = cntA[i] + (s[i] == 'A');\n cntB[i + 1] = cntB[i] + (s[i] == 'B');\n }\n long long ans = solve(0, 0, r + c, r + c);\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "1deb3f365aeb0b692da5cd04769198ca", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define mp make_pair\n#define pb push_back\n#define ll long long\n\n#define maxN 66\n\nint r, c, n, i;\nchar s[maxN];\nll comb[maxN][maxN];\n\nint cnt_a[maxN], cnt_b[maxN];\nvector partial[maxN], partial_zero[maxN];\n\nvoid compute_comb() {\n int i, j;\n\n for (i = 0; i <= n; i++) {\n comb[i][0] = 1;\n for (j = 1; j <= i; j++)\n comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];\n }\n}\n\nll get_difs(int conf, int a_bef, int b_bef) {\n int i;\n ll ans = 0;\n ll aux1, aux2;\n\n for (i = 0; i < c + 1; i++) {\n if (conf & (1 << i)) {\n //! here comes a B\n b_bef++;\n\n aux1 = comb[b_bef - 1][r - 1];\n aux2 = comb[n - b_bef][r] - comb[a_bef][r];\n\n ans -= aux1 * aux2;\n } else {\n //! here comes an A\n a_bef++;\n\n aux1 = comb[a_bef - 1][r - 1];\n aux2 = comb[n - a_bef][r] - comb[b_bef][r];\n\n ans += aux1 * aux2;\n }\n }\n\n return ans;\n}\n\nbool check(int conf) {\n int i;\n\n for (i = 0; i < c + 1; i++) {\n if (s[2 * r + c + i] == 'A' && ( (conf >> i) & 1 ) == 1) return false;\n if (s[2 * r + c + i] == 'B' && ( (conf >> i) & 1 ) == 0) return false;\n }\n\n return true;\n}\n\nvoid compute_second_part() {\n int i, conf;\n int limit = 1 << (c + 1);\n int a_here, b_here;\n\n for (conf = 0; conf < limit; conf++) {\n b_here = __builtin_popcount(conf);\n a_here = c + 1 - b_here;\n\n if (check(conf))\n partial[a_here].pb(get_difs(conf, r + c - a_here, r + c - b_here));\n else\n partial_zero[a_here].pb(get_difs(conf, r + c - a_here, r + c - b_here));\n }\n\n for (i = 0; i <= c + 1; i++) {\n sort(partial[i].begin(), partial[i].end());\n sort(partial_zero[i].begin(), partial_zero[i].end());\n }\n}\n\nll get_coef(int conf) {\n int i, j;\n\n for (i = 0; i < c + 1; i++)\n if ( ((conf >> i) & 1) != (conf & 1) )\n break;\n\n for (j = i; j < c + 1; j++) {\n if (s[2 * r - 1 + j] == 'A' && (( (conf >> j) & 1 ) == 1)) return 0;\n if (s[2 * r - 1 + j] == 'B' && (( (conf >> j) & 1 ) == 0)) return 0;\n }\n\n int set_a = r - 1 + ( (conf & 1) == 0 ? i : 0 );\n int set_b = r - 1 + ( (conf & 1) == 1 ? i : 0 );\n\n if (set_a < cnt_a[2 * r - 2 + i]) return 0;\n if (set_b < cnt_b[2 * r - 2 + i]) return 0;\n return comb[set_a + set_b - cnt_a[2 * r - 2 + i] - cnt_b[2 * r - 2 + i]][set_a - cnt_a[2 * r - 2 + i]];\n}\n\nvoid upd(pair& ans, pair act) {\n if (act.first < 0) act.first = -act.first;\n\n if (ans.first < act.first) return;\n if (ans.first > act.first) {\n ans = act;\n return;\n }\n\n ans.second += act.second;\n}\n\nll how_many(vector& data, ll val) {\n auto it1 = lower_bound(data.begin(), data.end(), val);\n auto it2 = upper_bound(data.begin(), data.end(), val);\n return it2 - it1;\n}\n\nll compute_first_part() {\n int i, conf;\n int limit = 1 << (c + 1);\n int a_here, b_here;\n ll act, coef, coef2;\n pair ans = mp(1LL << 60, 0);\n\n\n for (conf = 0; conf < limit; conf++) {\n b_here = __builtin_popcount(conf);\n a_here = c + 1 - b_here;\n act = get_difs(conf, r - 1, r - 1);\n\n //! coef may be != 0\n coef = get_coef(conf);\n auto it = lower_bound(partial[c + 1 - a_here].begin(),\n partial[c + 1 - a_here].end(),\n -act);\n\n\n if (it != partial[c + 1 - a_here].end()) {\n if (ans.first >= abs(*it + act)) {\n coef2 = how_many(partial[c + 1 - a_here], *it);\n upd(ans, mp(*it + act, coef * coef2));\n }\n }\n\n if (it != partial[c + 1 - a_here].begin()) {\n it--;\n if (ans.first >= abs(*it + act)) {\n coef2 = how_many(partial[c + 1 - a_here], *it);\n upd(ans, mp(*it + act, coef * coef2));\n }\n }\n\n //! coef is clearly 0\n coef = 0;\n it = lower_bound(partial_zero[c + 1 - a_here].begin(),\n partial_zero[c + 1 - a_here].end(),\n -act);\n\n\n if (it != partial_zero[c + 1 - a_here].end()) {\n coef2 = how_many(partial[c + 1 - a_here], *it);\n if (ans.first >= abs(*it + act)) {\n coef2 = how_many(partial_zero[c + 1 - a_here], *it);\n upd(ans, mp(*it + act, coef * coef2));\n }\n }\n\n if (it != partial_zero[c + 1 - a_here].begin()) {\n it--;\n if (ans.first >= abs(*it + act)) {\n coef2 = how_many(partial_zero[c + 1 - a_here], *it);\n upd(ans, mp(*it + act, coef * coef2));\n }\n }\n }\n\n return ans.second;\n}\n\nint main()\n{\n //freopen(\"test.in\",\"r\",stdin);\n\n scanf(\"%d%d\\n%s\", &r, &c, s + 1);\n n = 2 * (r + c);\n\n compute_comb();\n\n for (i = 1; i <= n; i++) {\n cnt_a[i] = cnt_a[i - 1] + (s[i] == 'A' ? 1 : 0);\n cnt_b[i] = cnt_b[i - 1] + (s[i] == 'B' ? 1 : 0);\n }\n\n compute_second_part();\n ll sol = compute_first_part();\n\n printf(\"%lld\", sol);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "591bf81aed2cbe5c2ee00dfb38141652", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\t#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tLL revarea[n+1][n+1];\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\trevarea[a][b] = totalarea[n][n] - totalarea[n-a][n]\n\t\t\t\t\t\t - totalarea[n][n-b] + totalarea[n-a][n-b];\n\t\t}\n\t}\n\n\tLL d = n;\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = it->first + totalarea[n][y+1] - totalarea[x][y+1];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tLL area2 = totalarea[n][n] - it->first - totalarea[n-x][n-(y+1)];\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tLL c = it->first+newarea;\n\t\t\t\t\tLL area2 = totalarea[x+1][n] - c;\n\t\t\t\t\tif(area2 > m2) continue;\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\n\t\t\t\t\tbp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "74f417a89ec4fa74c08279b35a0f7b43", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\n\nLL ncr[40][40];\nLL ans[30][30];\n\n\nint main(){\n\tif(1){\n\t\tans[1][1]=0;\n\t\tans[1][2]=1;\n\t\tans[1][3]=0;\n\t\tans[1][4]=1;\n\t\tans[1][5]=0;\n\t\tans[1][6]=1;\n\t\tans[1][7]=0;\n\t\tans[1][8]=1;\n\t\tans[1][9]=0;\n\t\tans[1][10]=1;\n\t\tans[1][11]=0;\n\t\tans[1][12]=1;\n\t\tans[1][13]=0;\n\t\tans[1][14]=1;\n\t\tans[1][15]=0;\n\t\tans[1][16]=1;\n\t\tans[1][17]=0;\n\t\tans[1][18]=1;\n\t\tans[1][19]=0;\n\t\tans[1][20]=1;\n\t\tans[2][1]=3;\n\t\tans[2][2]=0;\n\t\tans[2][3]=2;\n\t\tans[2][4]=3;\n\t\tans[2][5]=1;\n\t\tans[2][6]=0;\n\t\tans[2][7]=0;\n\t\tans[2][8]=1;\n\t\tans[2][9]=1;\n\t\tans[2][10]=0;\n\t\tans[2][11]=0;\n\t\tans[2][12]=1;\n\t\tans[2][13]=1;\n\t\tans[2][14]=0;\n\t\tans[2][15]=0;\n\t\tans[2][16]=1;\n\t\tans[2][17]=1;\n\t\tans[2][18]=0;\n\t\tans[2][19]=0;\n\t\tans[2][20]=1;\n\t\tans[3][1]=8;\n\t\tans[3][2]=20;\n\t\tans[3][3]=0;\n\t\tans[3][4]=15;\n\t\tans[3][5]=16;\n\t\tans[3][6]=0;\n\t\tans[3][7]=0;\n\t\tans[3][8]=1;\n\t\tans[3][9]=0;\n\t\tans[3][10]=0;\n\t\tans[3][11]=0;\n\t\tans[3][12]=1;\n\t\tans[3][13]=0;\n\t\tans[3][14]=0;\n\t\tans[3][15]=0;\n\t\tans[3][16]=1;\n\t\tans[3][17]=0;\n\t\tans[3][18]=0;\n\t\tans[3][19]=0;\n\t\tans[3][20]=1;\n\t\tans[4][1]=15;\n\t\tans[4][2]=75;\n\t\tans[4][3]=175;\n\t\tans[4][4]=0;\n\t\tans[4][5]=4;\n\t\tans[4][6]=0;\n\t\tans[4][7]=0;\n\t\tans[4][8]=45;\n\t\tans[4][9]=15;\n\t\tans[4][10]=1;\n\t\tans[4][11]=1;\n\t\tans[4][12]=0;\n\t\tans[4][13]=0;\n\t\tans[4][14]=0;\n\t\tans[4][15]=0;\n\t\tans[4][16]=1;\n\t\tans[4][17]=1;\n\t\tans[4][18]=1;\n\t\tans[4][19]=1;\n\t\tans[4][20]=0;\n\t\tans[5][1]=24;\n\t\tans[5][2]=189;\n\t\tans[5][3]=784;\n\t\tans[5][4]=1764;\n\t\tans[5][5]=0;\n\t\tans[5][6]=0;\n\t\tans[5][7]=60;\n\t\tans[5][8]=95;\n\t\tans[5][9]=200;\n\t\tans[5][10]=1;\n\t\tans[5][11]=14;\n\t\tans[5][12]=14;\n\t\tans[5][13]=0;\n\t\tans[5][14]=4;\n\t\tans[5][15]=0;\n\t\tans[5][16]=1;\n\t\tans[5][17]=0;\n\t\tans[5][18]=1;\n\t\tans[5][19]=0;\n\t\tans[5][20]=0;\n\t\tans[6][1]=35;\n\t\tans[6][2]=392;\n\t\tans[6][3]=2352;\n\t\tans[6][4]=8820;\n\t\tans[6][5]=19404;\n\t\tans[6][6]=0;\n\t\tans[6][7]=6600;\n\t\tans[6][8]=553;\n\t\tans[6][9]=245;\n\t\tans[6][10]=10780;\n\t\tans[6][11]=658;\n\t\tans[6][12]=126;\n\t\tans[6][13]=56;\n\t\tans[6][14]=20;\n\t\tans[6][15]=0;\n\t\tans[6][16]=5;\n\t\tans[6][17]=5;\n\t\tans[6][18]=0;\n\t\tans[6][19]=0;\n\t\tans[6][20]=0;\n\t\tans[7][1]=48;\n\t\tans[7][2]=720;\n\t\tans[7][3]=5760;\n\t\tans[7][4]=29700;\n\t\tans[7][5]=104544;\n\t\tans[7][6]=226512;\n\t\tans[7][7]=0;\n\t\tans[7][8]=15015;\n\t\tans[7][9]=40040;\n\t\tans[7][10]=0;\n\t\tans[7][11]=1440;\n\t\tans[7][12]=2028;\n\t\tans[7][13]=432;\n\t\tans[7][14]=1280;\n\t\tans[7][15]=8;\n\t\tans[7][16]=249;\n\t\tans[7][17]=8;\n\t\tans[7][18]=20;\n\t\tans[7][19]=6;\n\t\tans[7][20]=4;\n\t\tans[8][1]=63;\n\t\tans[8][2]=1215;\n\t\tans[8][3]=12375;\n\t\tans[8][4]=81675;\n\t\tans[8][5]=382239;\n\t\tans[8][6]=1288287;\n\t\tans[8][7]=2760615;\n\t\tans[8][8]=0;\n\t\tans[8][9]=1635920;\n\t\tans[8][10]=198198;\n\t\tans[8][11]=534456;\n\t\tans[8][12]=4524;\n\t\tans[8][13]=1546;\n\t\tans[8][14]=111564;\n\t\tans[8][15]=4050;\n\t\tans[8][16]=621;\n\t\tans[8][17]=3069;\n\t\tans[8][18]=5;\n\t\tans[8][19]=43;\n\t\tans[8][20]=1;\n\t\tans[9][1]=80;\n\t\tans[9][2]=1925;\n\t\tans[9][3]=24200;\n\t\tans[9][4]=196625;\n\t\tans[9][5]=1145144;\n\t\tans[9][6]=5010005;\n\t\tans[9][7]=16359200;\n\t\tans[9][8]=34763300;\n\t\tans[9][9]=0;\n\t\tans[9][10]=34606;\n\t\tans[9][11]=701350;\n\t\tans[9][12]=6651840;\n\t\tans[9][13]=78298;\n\t\tans[9][14]=429418;\n\t\tans[9][15]=872494;\n\t\tans[9][16]=1615;\n\t\tans[9][17]=352;\n\t\tans[9][18]=1155;\n\t\tans[9][19]=8580;\n\t\tans[9][20]=145;\n\t\tans[10][1]=99;\n\t\tans[10][2]=2904;\n\t\tans[10][3]=44044;\n\t\tans[10][4]=429429;\n\t\tans[10][5]=3006003;\n\t\tans[10][6]=16032016;\n\t\tans[10][7]=66745536;\n\t\tans[10][8]=212751396;\n\t\tans[10][9]=449141836;\n\t\tans[10][10]=0;\n\t\tans[10][11]=11675872;\n\t\tans[10][12]=4900324;\n\t\tans[10][13]=980474;\n\t\tans[10][14]=120143080;\n\t\tans[10][15]=47159156;\n\t\tans[10][16]=380171;\n\t\tans[10][17]=35871;\n\t\tans[10][18]=2207348;\n\t\tans[10][19]=223498;\n\t\tans[10][20]=4875;\n\t\tans[11][1]=120;\n\t\tans[11][2]=4212;\n\t\tans[11][3]=75712;\n\t\tans[11][4]=869505;\n\t\tans[11][5]=7154784;\n\t\tans[11][6]=45048640;\n\t\tans[11][7]=225059328;\n\t\tans[11][8]=901995588;\n\t\tans[11][9]=2821056160;\n\t\tans[11][10]=5924217936;\n\t\tans[11][11]=0;\n\t\tans[11][12]=1763160100;\n\t\tans[11][13]=1014736800;\n\t\tans[11][14]=96243664;\n\t\tans[11][15]=527663136;\n\t\tans[11][16]=13024737;\n\t\tans[11][17]=1152008;\n\t\tans[11][18]=27986506;\n\t\tans[11][19]=835384;\n\t\tans[12][1]=143;\n\t\tans[12][2]=5915;\n\t\tans[12][3]=124215;\n\t\tans[12][4]=1656200;\n\t\tans[12][5]=15767024;\n\t\tans[12][6]=114874032;\n\t\tans[12][7]=668144880;\n\t\tans[12][8]=3173688180;\n\t\tans[12][9]=12342120700;\n\t\tans[12][10]=38013731756;\n\t\tans[12][11]=79483257308;\n\t\tans[12][12]=0;\n\t\tans[12][13]=334424;\n\t\tans[12][14]=86230664;\n\t\tans[12][15]=401457992;\n\t\tans[12][16]=1811255769;\n\t\tans[12][17]=8782619185;\n\t\tans[12][18]=524739891;\n\t\tans[13][1]=168;\n\t\tans[13][2]=8085;\n\t\tans[13][3]=196000;\n\t\tans[13][4]=2998800;\n\t\tans[13][5]=32626944;\n\t\tans[13][6]=271211472;\n\t\tans[13][7]=1802805120;\n\t\tans[13][8]=9859090500;\n\t\tans[13][9]=44986664800;\n\t\tans[13][10]=170724392916;\n\t\tans[13][11]=519227905728;\n\t\tans[13][12]=1081724803600;\n\t\tans[13][13]=0;\n\t\tans[13][14]=5856160;\n\t\tans[13][15]=0;\n\t\tans[13][16]=208030461065;\n\t\tans[13][17]=743483832;\n\t\tans[14][1]=195;\n\t\tans[14][2]=10800;\n\t\tans[14][3]=299200;\n\t\tans[14][4]=5202000;\n\t\tans[14][5]=64047024;\n\t\tans[14][6]=600935040;\n\t\tans[14][7]=4507012800;\n\t\tans[14][8]=27887141700;\n\t\tans[14][9]=145173803500;\n\t\tans[14][10]=641087516256;\n\t\tans[14][11]=2384209771200;\n\t\tans[14][12]=7174705330000;\n\t\tans[14][13]=14901311070000;\n\t\tans[14][14]=0;\n\t\tans[14][15]=262032716400;\n\t\tans[14][16]=2131715333625;\n\t\tans[15][1]=224;\n\t\tans[15][2]=14144;\n\t\tans[15][3]=443904;\n\t\tans[15][4]=8697744;\n\t\tans[15][5]=120187008;\n\t\tans[15][6]=1261963584;\n\t\tans[15][7]=10576456704;\n\t\tans[15][8]=73167596964;\n\t\tans[15][9]=427391677504;\n\t\tans[15][10]=2136958387520;\n\t\tans[15][11]=9183622822400;\n\t\tans[15][12]=33577620944400;\n\t\tans[15][13]=100136810390400;\n\t\tans[15][14]=207426250094400;\n\t\tans[15][15]=0;\n\t\tans[16][1]=255;\n\t\tans[16][2]=18207;\n\t\tans[16][3]=642447;\n\t\tans[16][4]=14084415;\n\t\tans[16][5]=216899991;\n\t\tans[16][6]=2530499895;\n\t\tans[16][7]=23518156167;\n\t\tans[16][8]=180305863947;\n\t\tans[16][9]=1168649118175;\n\t\tans[16][10]=6511045086975;\n\t\tans[16][11]=31479019635375;\n\t\tans[16][12]=132211882468575;\n\t\tans[16][13]=476432168185575;\n\t\tans[16][14]=1409850293610375;\n\t\tans[17][1]=288;\n\t\tans[17][2]=23085;\n\t\tans[17][3]=909720;\n\t\tans[17][4]=22174425;\n\t\tans[17][5]=378261576;\n\t\tans[17][6]=4873620213;\n\t\tans[17][7]=49911657840;\n\t\tans[17][8]=421129613025;\n\t\tans[17][9]=3003942370000;\n\t\tans[17][10]=18451716007725;\n\t\tans[17][11]=98815801429800;\n\t\tans[17][12]=464342770607625;\n\t\tans[17][13]=1912322889603000;\n\t\tans[18][1]=323;\n\t\tans[18][2]=28880;\n\t\tans[18][3]=1263500;\n\t\tans[18][4]=34051325;\n\t\tans[18][5]=639970331;\n\t\tans[18][6]=9058041608;\n\t\tans[18][7]=101671895600;\n\t\tans[18][8]=938731990625;\n\t\tans[18][9]=7322109526875;\n\t\tans[18][10]=49204576020600;\n\t\tans[18][11]=288924390600300;\n\t\tans[18][12]=1496215594180125;\n\t\tans[19][1]=360;\n\t\tans[19][2]=35700;\n\t\tans[19][3]=1724800;\n\t\tans[19][4]=51137625;\n\t\tans[19][5]=1053844176;\n\t\tans[19][6]=16309493200;\n\t\tans[19][7]=199708080000;\n\t\tans[19][8]=2008002335625;\n\t\tans[19][9]=17037595575000;\n\t\tans[19][10]=124510748462100;\n\t\tans[19][11]=795771174744000;\n\t\tans[20][1]=399;\n\t\tans[20][2]=43659;\n\t\tans[20][3]=2318239;\n\t\tans[20][4]=75274584;\n\t\tans[20][5]=1693678140;\n\t\tans[20][6]=28541613100;\n\t\tans[20][7]=379694987100;\n\t\tans[20][8]=4140135724725;\n\t\tans[20][9]=38044950918975;\n\t\tans[20][10]=300900975450075;\n\n\t}\n\n\tfor(LL a = 0; a < 40; a++){\n\t\tfor(LL b = 0; b < 40; b++){\n\t\t\tif(b > a){\n\t\t\t\tncr[a][b] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(b == 0 || b == a){\n\t\t\t\tncr[a][b] = 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tncr[a][b] = ncr[a-1][b-1] + ncr[a-1][b];\n\t\t}\n\t}\n\tLL R, C;\n\tcin >> R >> C;\n\tstring str;\n\tcin >> str;\n\tLL n = R+C;\n\tLL blockarea[n][n];\n\tLL totalarea[n+1][n+1];\n\tfor(LL a = 0; a < n; a++){\n\t\tfor(LL b = 0; b < n; b++){\n\t\t\tblockarea[a][b] = ncr[a][R-1]*ncr[b][R-1];\n\t\t}\n\t}\n\tfor(LL a = 0; a <= n; a++){\n\t\tfor(LL b = 0; b <= n; b++){\n\t\t\ttotalarea[a][b] = 0;\n\t\t\tif(a == 0 || b == 0) continue;\n\t\t\ttotalarea[a][b] = totalarea[a][b-1] + totalarea[a-1][b] - totalarea[a-1][b-1];\n\t\t\ttotalarea[a][b] += blockarea[a-1][b-1];\n\t\t}\n\t}\n\n\tLL d = min(2*n-1,n+R-3);\n\n\tmap dp[n+1][n+1];\n\tmap bp[n+1][n+1];\n\tdp[0][0][0] = 1;\n\tbp[0][0][0] = 1;\n\n\tLL best = ans[R][C];\n\tLL m1 = (-best + totalarea[n][n]) / 2;\n\tLL m2 = totalarea[n][n] - m1;\n\tLL bestfreq = 0;\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tfor(LL y = 0; y < n; y++){\n\t\t\tif(x+y+1 <= d && str[x+y] != 'B'){\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first > m2) continue;\n\t\t\t\t\tdp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'B'){\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first > m2) continue;\n\t\t\t\t\tbp[x][y+1][it->first] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(x == n) continue;\n\t\tfor(LL y = 0; y <= n; y++){\n\n\t\t\tif(x+y+1 <= d && str[x+y] != 'A'){\n\t\t\t\tLL newarea = totalarea[x+1][y] - totalarea[x][y];\n\t\t\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea > m2) continue;\t\t\t\t\t\n\t\t\t\t\tdp[x+1][y][it->first + newarea] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= 2*n-d && str[n-x+n-y-1] != 'A'){\n\t\t\t\tLL newarea2 = totalarea[n-x][n-y] - totalarea[n-x-1][n-y];\n\t\t\t\tfor(auto it = bp[x][y].begin(); it != bp[x][y].end(); it++){\n\t\t\t\t\tif(it->first + newarea2 > m2) continue;\t\t\t\t\t\n\t\t\t\t\tbp[x+1][y][it->first + newarea2] += it->second;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(x+y+1 <= d) dp[x][y].clear();\n\t\t\tif(x+y+1 <= 2*n-d) bp[x][y].clear();\n\t\t}\n\n\t}\n\n\n\tfor(LL x = 0; x <= n; x++){\n\t\tLL y = d-x;\n\t\tif(y < 0 || y > n) continue;\n\t\t//cout << x << \" \" << y << endl;\n\t\tfor(auto it = dp[x][y].begin(); it != dp[x][y].end(); it++){\n\t\t\tLL c = it->first;\n\t\t\tLL freq = it->second;\n\t\t\t// bp[n-x][n-y]\n\t\t\tbestfreq += freq * bp[n-x][n-y][m1-c];\n\t\t\tif(m1 != m2) bestfreq += freq * bp[n-x][n-y][m2-c];\n\n\t\t}\n\t}\n\n\tcout << bestfreq << endl;\n\n}", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "combinatorics"], "code_uid": "4555f2e302a2f89bed7f49f1aaefa474", "src_uid": "1b978b5aed4a83a2250bb23cc1ad6f85", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int N = 2e5+5;\nint n,sz[N],a[N],c,ret,dp[N];\npair m[N][2];\nvector adj[N];\nvoid dfs(int x,int p){\n\tint f=0,u,i,d;dp[x]=0;\n\tif(!sz[x])f=1,++sz[x];\n\td= a[x]>=c ? 1 :0;\n\tfor(i=0;i>n>>k;\n\tfor(i=1;i<=n;++i)scanf(\"%d\",&a[i]);\n\tfor(i=0;i=k)l= mid;\n\t\telse\n\t\t\tr= mid-1;\n\t}\n\tprintf(\"%d\\n\",l);\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "706f5250844c2107e1255d258858564b", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define N 200020\n#define ll long long\nusing namespace std;\ninline int read(){\n\tint x=0,f=1;char ch=getchar();\n\twhile(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());\n\twhile(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();\n\treturn f?x:-x;\n}\nint head[N], nxt[N<<1], to[N<<1], cnt;\nvoid insert(int x, int y) {\n\tto[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;\n\tto[++cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;\n}\nint a[N], b[N], n = read(), k = read();\nint f[N], sz[N], nt[N], sum, ans;\nint dfs1(int x, int fa) {\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs1(to[i], x);\n\t\tsz[x] += sz[to[i]];\n\t\tnt[x] += nt[to[i]];\n\t}\n}\nvoid dfs2(int x, int fa) {\n\t// printf(\"%d %d \\n\", x, fa);\n\tf[x] = 1;\n\tint mx = 0, sx = 0;\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs2(to[i], x);\n\t\tif (!nt[to[i]]) f[x] += sz[to[i]];\n\t\telse if (!b[to[i]])\n\t\t\tif (f[to[i]] > mx) sx = mx, mx = f[to[i]];\n\t\t\telse if (f[to[i]] > sx) sx = f[to[i]];\n\t}\n\tf[x] += mx;\n\tif (!b[x]) {\n\t\tif (nt[x] == sum) ans = max(ans, f[x] + sx + n - sz[x]);\n\t\telse ans = max(ans, f[x] + sx);\n\t}\n}\nbool check(int mid) {\n\t// printf(\"%d:\\n\", mid);\n\tans = sum = 0;\n\tfor (int i = 1; i <= n; i++) {\n\t\tsz[i] = 1;\n\t\tsum += nt[i] = b[i] = a[i] <= mid;\n\t}\n\tdfs1(1, 0);\n\tdfs2(1, 0);\n\t// puts(ans >= k ? \"Yes\" : \"No\");\n\treturn ans >= k;\n}\nint main(int argc, char const *argv[]) {\n\tint l = 1, r = 0, ans;\n\tfor (int i = 1; i <= n; i++)\n\t\tr = max(r, a[i] = read());\n\tfor (int i = 1; i < n; i++)\n\t\tinsert(read(), read());\n\tr ++;\n\twhile (l <= r) {\n\t\tint mid = l + r >> 1;\n\t\tif (check(mid)) l = mid + 1;\n\t\telse ans = mid, r = mid - 1;\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n\t \t \t\t \t \t\t\t \t \t\t\t\t//hellobig_tq//hello", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "65c859c372a78e3b3eb4a2cad7c55ec1", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\ntypedef long long LL;\ntypedef pair PII;\n#define MP make_pair\n#define PB push_back\n#define AA first\n#define BB second\n#define OP begin()\n#define ED end()\n#define SZ size()\n#define cmin(x,y) x=min(x,y)\n#define cmax(x,y) x=max(x,y)\n#define NAME \"535F\"\n#define UsingFile 0\nconst LL MOD = 1000000007;\nconst double PI = acos(-1.);\nconst int N = 200000+5;\nint n,a[N],w[N];\nint din[N],dot[N],fot[N];\nvectorG[N];\nvoid dfsd(int u,int f){\n\tint i,v;\n\tfor(i=0;i=a){\n\t\tb=a;a=c;\n\t}else cmax(b,c);\n}\nvoid dfsf(int u,int f){\n\tint i,v;\n\tif(w[u]){\n\t\tint b[2];\n\t\tb[0]=fot[u];\n\t\tb[1]=0;\n\t\tint sum=0;\n\t\tfor(i=0;i=A;\n\tint rt=-1;\n\tfor(i=1;i<=n;i++)\n\t\tif(!w[i])\n\t\t\trt=i;\n\tif(rt==-1)return n;\n\tdfsd(rt,-1);\n\tfot[rt]=0;\n\tdfsf(rt,-1);\n\tint ans=dfsdp(rt,-1);/*\n\tcout<<\"@\"<L;\n \tfor(i=1;i<=n;i++)\n \t\tL.PB(a[i]);\n \tsort(L.OP,L.ED);\n \tL.erase(unique(L.OP,L.ED),L.ED);\n \tint le=0,re=L.SZ-1,ge=-1;\n \twhile(re>=le){\n \t\tif(re-le<=1){\n \t\t\tif(cnt(L[re])>=k)ge=re;\n \t\t\telse ge=le;\n \t\t\tbreak;\n \t\t}\n \t\tint mid=(le+re)/2;\n \t\tif(cnt(L[mid])>=k)le=mid;\n \t\telse re=mid;\n \t}\n \tprintf(\"%d\\n\",L[ge]);\n }\n return 0;\n}\n/*\n13 7\n13 14 21 22 11 12 8 19 5 3 4 23 18 \n2 1\n3 1\n4 2\n5 4\n6 3\n7 2\n8 1\n9 5\n10 6\n11 8\n12 6\n13 8\n11\n*/", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "b1f16527d3f98cbfe183b84188ac43b0", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\ntypedef vector vi; \ntypedef pair pii;\ntypedef long long int lld;\n\n#define sz size()\n#define pb push_back \n#define mp make_pair\n#define F first\n#define S second\n#define fill(a,v) memset((a),(v),sizeof (a))\n#define INF INT_MAX\n#define mod 1000000007\n#define __sync__\t\t std::ios::sync_with_stdio(false);\n#define all(a)\t\t\t a.begin(),a.end()\n\n#define TRACE\n\n#ifdef TRACE\n#define trace1(x) cerr << #x << \": \" << x << endl;\n#define trace2(x, y) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << endl;\n#define trace3(x, y, z) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << \" | \" << #z << \": \" << z << endl;\n#define trace4(a, b, c, d) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << endl;\n#define trace5(a, b, c, d, e) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << \" | \" << #e << \": \" << e << endl;\n#define trace6(a, b, c, d, e, f) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << \" | \" << #e << \": \" << e << \" | \" << #f << \": \" << f << endl;\n\n#else\n\n#define trace1(x)\n#define trace2(x, y)\n#define trace3(x, y, z)\n#define trace4(a, b, c, d)\n#define trace5(a, b, c, d, e)\n#define trace6(a, b, c, d, e, f)\n\n#endif\n\nconst int N = 2e5 + 10;\nvi adj[N];\nint a[N], sub[N], n, k, v;\nlld dp[N];\n\nvoid dfs0(int x, int p=-1)\n{\n\tsub[x] = 1;\n\tlld mx = 0;\n\tfor(int &to : adj[x])\n\t{\n\t\tif(to==p) continue; \n\t\tdfs0(to, x);\n\t\tsub[x] += sub[to];\n\t\tif(sub[to] == dp[to]) dp[x] += dp[to];\n\t\telse mx = max(mx, dp[to]);\n\t}\n\tif(a[x] >= v) dp[x]+=1+mx;\n\telse dp[x] = 0;\n}\n\nlld ans[N];\nbool ok = 0;\nlld dfs(int x, int p=-1, lld out = 0, lld out_sub = 0)\n{\n\tint idx = -1;\n\tlld sec = 0, mx = 0, Ans = 0;\n\tfor(int &to : adj[x])\n\t{\n\t\tif(to == p) continue;\n\t\tif(dp[to] == sub[to]) Ans += dp[to];\n\t\telse\n\t\t{\n\t\t\tif(dp[to] >= mx) sec = mx, mx = dp[to], idx = to;\n\t\t\telse if(dp[to] > sec) sec = dp[to];\n\t\t}\n\t}\n\tif(out_sub != out) \n\t{\n\t\tif(out >= mx) sec = mx, mx = out, idx = -2;\n\t\telse if(out > sec) sec = out;\n\t}\n\telse Ans += out;\n\tif(a[x] >= v)\n\t\tans[x] = Ans + mx+1;\n\telse ans[x] = 0;\n\tif(ans[x] >= k) ok = 1;\n\tfor(int &to : adj[x])\n\t{\n\t\tif(to == p) continue;\n\t\tif(idx == to)\n\t\t{\n\t\t\tdfs(to, x, ((a[x]>=v)?(ans[x] - mx + sec):0), sub[x] + out_sub - sub[to]);\n\t\t\tcontinue;\n\t\t}\n\t\tif(sub[to] == dp[to]) dfs(to, x, ((a[x]>=v)?(ans[x] - dp[to]):0), sub[x] + out_sub - sub[to]);\n\t\telse dfs(to, x, (a[x]>=v)?ans[x]:0, sub[x] + out_sub - sub[to]);\n\t}\n}\n\n\nint main()\n{\n\tcin>>n>>k;\n\tfor(int i=0;i>a[i];\n\tfor(int i=0;i>u>>v;\n\t\tu--;v--;\n\t\tadj[u].pb(v);\n\t\tadj[v].pb(u);\n\t}\n\tlld l = 0, r = 1e6+10;\n\twhile(l\n\nusing namespace std;\n\n#define pb push_back\n#define orta (bas + son >> 1)\n#define sag (k + k + 1)\n#define sol (k + k)\n#define endl '\\n'\n#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)\n#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)\n#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)\n#define mp make_pair\n#define nd second\n#define st first\n#define type(x) __typeof(x.begin())\n\ntypedef pair < int ,int > pii;\n\ntypedef long long ll;\n\nconst long long linf = 1e18+5;\nconst int mod = (int) 1e9 + 7;\nconst int logN = 17;\nconst int inf = 1e9;\nconst int N = 2e5 + 5;\n\nint all_cnt, sum[N], cnt[N], all[N], ok[N], n, m, a[N], x, y, z; \nvector< int > v[N];\n\nint dfs(int node, int root, int big) {\n\tcnt[node] = a[node] < big;\n\tall[node] = 1;\n\tsum[node] = 1;\n\tok[node] = 1;\n\tint mx = 0, mx2 = 0;\n\tforeach(it, v[node]) {\n\t\tif(*it == root) continue;\n\t\tif(dfs(*it, node, big)) return true;\n\t\tsum[node] += sum[*it]; \n\t\tcnt[node] += cnt[*it]; \n\t\tif(ok[*it] == 1) all[node] += all[*it];\n\t\telse { \n\t\t\tok[node] = 0;\n\t\t\tif(all[*it] >= mx) {\n\t\t\t\tmx2 = mx;\n\t\t\t\tmx = all[*it];\n\t\t\t} else mx2 = max(mx2, all[*it]);\n\t\t}\n\t}\n\tall[node] += mx;\n\tif(a[node] < big) { all[node] = 0; ok[node] = 0; }\n\telse if(all[node] + mx2 + (cnt[node] == all_cnt ? n - sum[node] : 0) >= m) return true;\n\treturn false;\n}\n\nint main() {\n\n\tscanf(\"%d %d\", &n, &m);\n\n\tFOR(i, 1, n) {\n\t\tscanf(\"%d\", &a[i]);\n\t}\n\n\tFOR(i, 2, n) {\n\t\tscanf(\"%d %d\", &x, &y);\n\t\tv[x].pb(y); v[y].pb(x);\n\t}\n\n\tint bas = 0, son = 1000000;\n\n\twhile(bas < son) {\n\t\tint ort = bas + son >> 1;\n\t\tif(bas == ort) ort++;\n\t\tall_cnt = 0;\n\t\tFOR(i, 1, n) all_cnt += a[i] < ort;\n\t\tif(dfs(1, 0, ort)) bas = ort;\n\t\telse son = ort - 1;\n\t}\n\n\tcout << bas << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "a4760e033e1d94b953eb2471382325cd", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define endl \"\\n\"\n#define mp make_pair\n#define int long long\n#define pii pair\ninline int in(){int32_t x ; scanf(\"%d\",&x) ; return x ;}\ninline string getStr(){char ch[500001] ; scanf(\"%s\",ch) ;return ch;}\nconst int MAX_N = 2e5 + 10;\ninline int gcd( int a, int b ) { return b == 0 ? a : gcd( b, a%b ); }\nvector g[MAX_N];\nbool del[MAX_N] , forb[MAX_N] , mark[MAX_N];\nint a[MAX_N] , dp[MAX_N] , ln , vl[MAX_N] , deg[MAX_N];\nqueue Bomb ; int n , k;\ninline void BombProcess()\n{\n for (int i = 1 ; i <= n ; i ++) if (deg[i] == 1 && !forb[i]) Bomb.push(i);\n while (Bomb.size()){\n int first = Bomb.front(); Bomb.pop();\n deg[first] --;\n del[first] = true;\n int FIND = -1;\n for (auto u : g[first]){\n if (!del[u] && !forb[u]){\n FIND = u;\n break;\n }\n }\n if (FIND != -1){\n vl[FIND] += vl[first];\n deg[FIND] --;\n if (deg[FIND] == 1)\n Bomb.push(FIND);\n }\n ln = max(ln , vl[first]);\n vl[first] = 0;\n }\n}\ninline void dfs(int v, int pr = -1){\n mark[v] = true;\n int f = 0 , s = 0;\n dp[v] = vl[v];\n for (auto u : g[v]){\n if (pr == u || forb[u]) continue;\n dfs(u , v);\n if (dp[u] > f) {s = f ; f = dp[u];}\n else s = max(s , dp[u]);\n }\n \n ln = max(ln , dp[v] + f + s);\n dp[v] += f;\n}\ninline bool can(int low){\n ln = 0;\n for (int i = 1 ; i <= n ; i ++){\n mark[i] = 0;\n deg[i] = g[i].size();\n dp[i] = 0;\n del[i] = 0 ;\n mark[i] = 0 ;\n vl[i] = 1;\n forb[i] = (a[i] < low);\n }\n BombProcess();\n for (int i = 1 ; i <= n ; i ++) {\n if (!mark[i] && !forb[i]) dfs(i , -1);\n }\n return ln >= k;\n}\nint32_t main()\n{\n n = in() , k = in();\n for (int i = 0 ; i < n ; i ++) a[i + 1] = in();\n for (int i = 0 ; i < n - 1 ; i ++){\n int v = in() , u = in();\n g[v].push_back(u);\n g[u].push_back(v);\n }\n int l = 0 , r = (1 << 20) , best = 0 , mid;\n while (l <= r){\n mid = (l + r) >> 1;\n if (can(mid)) {best = mid ; l = mid + 1 ; continue ; }\n r = mid - 1;\n }\n cout << best << \"\\n\";\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "57aa7ed229cbc7f335db1ba963de2852", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int N = 2e5+5;\nint n,sz[N],a[N],c,ret,dp[N];\npair m[N][2];\nvector adj[N];\nvoid dfs(int x,int p){\n\tint f=0,u,i,d;dp[x]=0;\n\tif(!sz[x])f=1,++sz[x];\n\td= a[x]>=c ? 1 :0;\n\tfor(i=0;i=c){\n\t\tif(d==n-sz[x])v=d,t=0;\n\t\tar[0]= m[x][0].first,ar[1]= m[x][1].first,ar[2]=t;\n\t\tsort(ar,ar+3);\n\t\tret= max(ret, dp[x]+ ar[2]+v-m[x][0].first);\n\t}\n\tfor(i=0;i>n>>k;\n\tfor(i=1;i<=n;++i)scanf(\"%d\",&a[i]);\n\tfor(i=0;i=k)l= mid;\n\t\telse\n\t\t\tr= mid-1;\n\t}\n\tprintf(\"%d\\n\",l);\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "86598df61dec286b79687bae200809d3", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\nconst int N = 2e5 + 5;\n\nint n, m, a[N], b[N], dp[N], sz[N], cur[N], ans, cnt, l, r, mid, x, y;\nvector < int > g[N];\n\n\nvoid dfs1(int v, int pr = -1){\n sz[v] = 1;\n for(int to : g[v]){\n if(to != pr){\n dfs1(to, v);\n sz[v] += sz[to];\n }\n }\n}\n\nvoid dfs2(int v, int pr = -1){\n int all = 0, mx1 = 0, mx2 = 0;\n for(int to : g[v]){\n if(to != pr){\n dfs2(to, v);\n cur[v] += cur[to];\n if(dp[to] == sz[to]){\n all += sz[to];\n }\n else if(dp[to] > mx1){\n mx2 = mx1;\n mx1 = dp[to];\n }\n else if(dp[to] > mx2){\n mx2 = dp[to];\n }\n }\n }\n if(!b[v]){\n dp[v] = 0;\n }\n else{\n dp[v] = all + mx1 + 1;\n cur[v]++;\n }\n if(n - sz[v] == cnt - cur[v]){\n all += n - sz[v];\n }\n if(b[v]){\n ans = max(ans, all + mx1 + mx2 + 1);\n }\n}\n\ninline bool check(int x){\n ans = cnt = 0;\n for(int i = 1; i <= n; i++){\n dp[i] = sz[i] = cur[i] = 0;\n b[i] = a[i] >= x;\n cnt += b[i];\n }\n dfs1(1);\n dfs2(1);\n return ans >= m;\n}\n\nmain(){\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n cin >> n >> m;\n for(int i = 1; i <= n; i++){\n cin >> a[i];\n }\n for(int i = 1; i < n; i++){\n cin >> x >> y;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n r = 1e6;\n while(r - l > 1){\n mid = (r + l) >> 1;\n if(check(mid)){\n l = mid;\n }\n else{\n r = mid;\n }\n }\n if(check(r)){\n cout << r;\n }\n else{\n cout << l;\n }\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "bfe3d6c9605bcf76f5acb91d732907b1", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#ifdef NeverBeRed\n#include \"debug.h\"\n#else\n#define debug(...) 9715\n#endif\ntypedef long long ll;\ntypedef long double ld;\ntypedef complex point;\n#define F first\n#define S second\n\n\n\nint main()\n{\n\t#ifdef TurnRed\n\t\t//freopen(\"a.in\", \"r\", stdin);\n\t\t//freopen(\"a.out\", \"w\", stdout);\n\t#endif\n\n\tios_base::sync_with_stdio(0), cin.tie(0);\n\n\tint n, k;\n\tcin >> n >> k;\n\tvector val(n);\n\tfor (auto &i : val) cin >> i;\n\tvector> adj(n);\n\tfor (int i = 1, u, v; i < n; ++i)\n\t{\n\t\tcin >> u >> v;\n\t\t--u, --v;\n\t\tadj[u].push_back(v);\n\t\tadj[v].push_back(u);\n\t}\n\n\tauto solve = [&](int mn)\n\t{\n\t\tauto w = [&](int u) { return val[u] >= mn; };\n\n\t\tvector sz(n), dp(n), wsz(n), rdp(n);\n\t\tvector> sub(n);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tsub[i].insert(0);\n\n\t\tfunction dfs = [&](int u, int p)\n\t\t{\n\t\t\tsz[u] = 1;\n\t\t\twsz[u] = w(u);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tdfs(v, u);\n\t\t\t\t\tsz[u] += sz[v];\n\t\t\t\t\twsz[u] += wsz[v];\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] += sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].insert(rdp[v]);\n\t\t\t\t}\n\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\t\t};\n\t\tdfs(0, -1);\n\n\t\tint best = 0;\n\t\tfunction dfs2 = [&](int u, int p)\n\t\t{\n\t\t\tbest = max(best, rdp[u]);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tint rdpu = rdp[u], rdpv = rdp[v];\n\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] -= sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].erase(sub[u].find(rdp[v]));\n\t\t\t\t\tsz[u] -= sz[v];\n\t\t\t\t\twsz[u] -= wsz[v];\n\t\t\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\n\t\t\t\t\tsz[v] += sz[u];\n\t\t\t\t\twsz[v] += wsz[u];\n\t\t\t\t\tif (sz[u] == wsz[u])\n\t\t\t\t\t\tdp[v] += sz[u];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[v].insert(rdp[u]);\n\t\t\t\t\trdp[v] = (dp[v] + 1 + *sub[v].rbegin()) * w(v);\n\n\t\t\t\t\tdfs2(v, u);\n\n\t\t\t\t\trdp[v] = rdpv;\n\t\t\t\t\tif (sz[u] == wsz[u])\n\t\t\t\t\t\tdp[v] -= sz[u];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[v].erase(sub[v].find(rdp[u]));\n\t\t\t\t\twsz[v] -= wsz[u];\n\t\t\t\t\tsz[v] -= sz[u];\n\n\t\t\t\t\trdp[u] = rdpu;\n\t\t\t\t\twsz[u] += wsz[v];\n\t\t\t\t\tsz[u] += sz[v];\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] += sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].insert(rdp[v]);\n\t\t\t\t}\n\t\t};\n\t\tdfs2(0, -1);\n\n\t\treturn best;\n\t};\n\n\tint lo = 1, hi = 1e6;\n\twhile (lo < hi)\n\t{\n\t\tint md = (lo + hi + 1) >> 1;\n\t\tif (solve(md) >= k)\n\t\t\tlo = md;\n\t\telse\n\t\t\thi = md - 1;\n\t}\n\tcout << lo << \"\\n\";\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "b3dde81b927dc6bd752f28f5d27a52b3", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint n,k,arr[1<<18];\nbool vis[1<<18],big[1<<18];int failed[1<<18],failed1[1<<18];\nvectoradj[1<<18];\npairmx[1<<18],mx1[1<<18];\nint sum[1<<18];\nvoid dfs(int x,int p)\n{\n int sz=adj[x].size(),nxt;\n bool ok=1;\n pairpii;\n sum[x]=1;\n vis[x]=1;\n for(int i=0;imx[x])swap(pii,mx[x]);\n if(pii>mx1[x])swap(pii,mx1[x]);\n }\n else sum[x]+=sum[nxt];\n if(failed[nxt]){failed1[x]=failed[x];failed[x]=nxt;continue;}\n }\n}\nvoid dfs1(int x,int p)\n{\n //cout<pii=mx[p];\n if(mx[p].second==x)pii=mx1[p];\n if(!(!failed[p]||(failed[p]==x&&!failed1[p])))pii.first+=sum[p];\n if(!failed[x])pii.first-=sum[x];\n if(!failed[p]){\n sum[x]+=sum[p]-sum[x];\n }\n else if(failed[p]==x&&!failed1[p]){\n sum[x]+=sum[p];\n }\n\n //cout<mx[x])swap(pii,mx[x]);\n if(pii>mx1[x])swap(pii,mx1[x]);\n //change failed\n if((failed[p]&&failed[p]!=x)||(failed1[p]&&failed1[p]!=x)){\n failed1[x]=failed[x];\n failed[x]=p;\n }\n }\n //cout<=mid);\n vis[i]=(arr[i]=k)return 1;\n }\n }\n return 0;\n\n}\nint main()\n{\n cin>>n>>k;\n for(int i=1;i<=n;++i)scanf(\"%d\",arr+i);\n int u,v;\n for(int i=1;i\nusing namespace std;\n\n// total balls: 2 <= n <= 200 000\n// numbers of balls to inspect: 1 <= k <= n\n\n// n integers ai, 1 <= ai <= 1 000 000, time to build ith ball\n\n// n-1 tines, ui vi, 1 <= ui, vi <= n, ui =/= vi, connection between balls ui and vi.\n\nint n, k, *a, *u, *v;\nint *deg, *sdeg, *adj;\nint maxdfs, *vis;\n\npair dfs(int idx, int brk) {\n\tif (a[idx] < brk) return make_pair(0,false);\n\tif (vis[idx] == 1) return make_pair(0,true);\n\tvis[idx] = 1;\n\tint hi1 = 0;\n\tint hi2 = 0;\n\tint fre = 0;\n\tbool b = true;\n\tfor (int i = 0; i < deg[idx]; i++) {\n\t\tint nbr = adj[sdeg[idx]+i];\n\t\tpair result = dfs(nbr,brk);\n\t\tif (result.second) {\n\t\t\tfre += result.first;\n\t\t} else {\n\t\t\tb = false;\n\t\t\tif (result.first > hi2) hi2 = result.first;\n\t\t\tif (hi2 > hi1) swap(hi1, hi2);\n\t\t}\n\t}\n\tif (b) {\n\t\treturn make_pair(fre+1,true);\n\t}\n\telse {\n\t\tmaxdfs = max(maxdfs, hi1+hi2+fre+1);\n\t\treturn make_pair(hi1+fre+1,false);\n\t}\n}\n\nint binsearch(int lo, int hi) {\n\tif (lo+1 == hi) return lo;\n\tint mid = lo+(hi-lo)/2;\n\tmaxdfs = 0;\n\tvis = (int *)calloc(n,sizeof(int));\n\tfor (int i = 0; i < n; i++) {\n\t\tif (vis[i] == 0 && a[i] >= mid) {\n\t\t\tfor (int j = 0; j < deg[i]; j++) {\n\t\t\t\tif (a[adj[sdeg[i]+j]] < mid) {\n\t\t\t\t\tdfs(i,mid);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfree(vis);\n\tif (maxdfs >= k) return binsearch(mid, hi);\n\telse return binsearch(lo, mid);\n}\n\nint main() {\n\tscanf(\"%d %d\", &n, &k);\n\ta = (int *)malloc(n*sizeof(int));\n\tint mina = 1000000;\n\tfor (int i = 0; i < n; i++) {\n\t\tscanf(\"%d\", a+i);\n\t\tmina = min(mina, a[i]);\n\t}\n\tdeg = (int *)calloc(n,sizeof(int));\n\tu = (int *)malloc((n-1)*sizeof(int));\n\tv = (int *)malloc((n-1)*sizeof(int));\n\tfor (int i = 0; i < n-1; i++) {\n\t\tint u1, v1;\n\t\tscanf(\"%d %d\", &u1, &v1);\n\t\tu[i] = u1-1;\n\t\tv[i] = v1-1;\n\t\tdeg[u[i]]++;\n\t\tdeg[v[i]]++;\n\t}\n\tsdeg = (int *)malloc(n*sizeof(int));\n\tsdeg[0] = 0;\n\tfor (int i = 1; i < n; i++) sdeg[i] = sdeg[i-1]+deg[i-1];\n\tadj = (int *)malloc(2*(n-1)*sizeof(int));\n\tfree(deg);\n\tdeg = (int *)calloc(n,sizeof(int));\n\tfor (int i = 0; i < n-1; i++) {\n\t\tadj[sdeg[u[i]]+deg[u[i]]] = v[i];\n\t\tadj[sdeg[v[i]]+deg[v[i]]] = u[i];\n\t\tdeg[u[i]]++;\n\t\tdeg[v[i]]++;\n\t}\n\tprintf(\"%d\\n\", binsearch(mina,1000001));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "02b724eef39c710127d25e7a568df6dd", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nconst int N = 200005;\nint n,m,lim,p = 1,a[N],nex[N*2],spot[N*2],head[N],fa[N],f[N],g[N];\nint maxcolor[N][2],c[N];\nbool color[N],v[N];\n\nvoid add(int x,int y)\n{\n\tnex[++p] = head[x], head[x] = p, spot[p] = y;\n\tnex[++p] = head[y], head[y] = p, spot[p] = x;\n}\n\nvoid dfs1(int x)\n{\n\tint tp,y,cnt = 0,m1 = 0,m2 = 0;\n\tv[x] = 1;\n\tfor(tp = head[x]; y = spot[tp],tp; tp = nex[tp])\n\t\tif(a[y]>=lim && y!=fa[x]) {\n\t\t\tfa[y] = x;\n\t\t\tdfs1(y);\n\t\t\tif(!c[y]) cnt += f[y];\n\t\t\telse {\n\t\t\t\tc[x]++;\n\t\t\t\tif(f[y]>m2) m2 = f[y];\n\t\t\t\tif(m10) {\n\t\t\t//xÒªÔÚËüµ±³õÑ¡µÄcolor½áµãºÍËü¸¸Ç׳ýÈ¥ËüÄDz¿·ÖÖÐÑ¡Ôñ \n\t\t\tif(c[x]) {//xµ±³õ±»Ñ¡»ò²»Ñ¡ \n\t\t\t\tif(f[x]maxcolor[x][1])\n\t\t\t\t\t\tmaxcolor[x][1] = g[fa[x]];\n\t\t\t\t\tif(maxcolor[x][0]maxcolor[x][1])\n\t\t\t\t\t\tmaxcolor[x][1] = g[fa[x]]-maxcolor[fa[x]][0]+maxcolor[fa[x]][1];\n\t\t\t\t\tif(maxcolor[x][0]=lim && spot[tp]!=fa[x])\n\t\t\tdfs2(spot[tp], ret);\n}\nint solve()\n{\n\tmemset(v, 0, sizeof(v));\n\tmemset(fa, 0, sizeof(fa));\n\tmemset(maxcolor, 0, sizeof(maxcolor));\n\tmemset(c, 0, sizeof(c));\n\tint maxn = 0;\n\tfor(int i = 1; i<=n; i++)\n\t\tif(a[i]>=lim && v[i]==0) {\n\t\t\tdfs1(i), dfs2(i,maxn);\n\t\t}\n\treturn maxn>=m;\n}\n\nint main()\n{\n\tint i,x,y,l,r,tp;\n\tcin >> n >> m;\n\tfor(i = 1; i<=n; i++)\n\t\tscanf(\"%d\", &a[i]);\n\tfor(i = 1; i>1;\n\t\tmemset(color, 0, sizeof(color));\n\t\tfor(i = 1; i<=n; i++)\n\t\t\tif(a[i]\n\nusing namespace std;\n\nstruct DP {\n int sum;\n int mx;\n};\n\ninline DP join(DP &a, DP &b) {\n DP c;\n c.sum = a.sum + b.sum;\n c.mx = max(a.mx, b.mx);\n return c;\n}\n\nconst int N = 200010;\n\nvector g[N];\nint total[N];\nint f[N], dp[N];\nbool good[N];\nint a[N];\nint n;\n\nvoid dfs(int v, int pr) {\n int sz = g[v].size();\n total[v] = 1;\n int sum = 0, mx = 0;\n for (int j = 0; j < sz; j++) {\n int u = g[v][j];\n if (u == pr) {\n continue;\n }\n dfs(u, v);\n total[v] += total[u];\n if (f[u] == total[u]) {\n sum += f[u];\n } else {\n mx = max(mx, f[u]);\n }\n }\n f[v] = sum + mx + 1;\n if (!good[v]) {\n f[v] = 0;\n }\n}\n\nvoid solve(int v, int pr, int up) {\n int sz = g[v].size();\n vector children;\n for (int j = 0; j < sz; j++) {\n int u = g[v][j];\n if (u == pr) {\n continue;\n }\n children.push_back(u);\n }\n int cnt = children.size();\n int sum = 0, mx = 0;\n for (int j = 0; j < cnt; j++) {\n int u = children[j];\n if (f[u] == total[u]) {\n sum += f[u];\n } else {\n mx = max(mx, f[u]);\n }\n }\n if (up == n - total[v]) {\n sum += up;\n } else {\n mx = max(mx, up);\n }\n dp[v] = sum + mx + 1;\n if (!good[v]) {\n dp[v] = 0;\n }\n if (cnt == 0) {\n return;\n }\n vector pref(cnt + 1);\n vector suf(cnt + 1);\n pref[0] = {0, 0};\n for (int j = 0; j < cnt; j++) {\n int u = children[j];\n pref[j + 1] = pref[j];\n if (f[u] == total[u]) {\n pref[j + 1].sum += f[u];\n } else {\n pref[j + 1].mx = max(pref[j + 1].mx, f[u]);\n }\n }\n suf[cnt] = {0, 0};\n for (int j = cnt - 1; j >= 0; j--) {\n int u = children[j];\n suf[j] = suf[j + 1];\n if (f[u] == total[u]) {\n suf[j].sum += f[u];\n } else {\n suf[j].mx = max(suf[j].mx, f[u]);\n }\n }\n for (int j = 0; j < cnt; j++) {\n int u = children[j];\n DP z = join(pref[j], suf[j + 1]);\n if (up == n - total[v]) {\n z.sum += up;\n } else {\n z.mx = max(z.mx, up);\n }\n int new_up = z.sum + z.mx + 1;\n if (!good[v]) {\n new_up = 0;\n }\n solve(u, v, new_up);\n }\n}\n\nint main() {\n int k;\n scanf(\"%d %d\", &n, &k);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", a + i);\n }\n for (int i = 0; i < n; i++) {\n g[i].clear();\n }\n for (int i = 0; i < n - 1; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n x--; y--;\n g[x].push_back(y);\n g[y].push_back(x);\n }\n \n int low = 0, high = 1000010;\n while (low < high) {\n int mid = (low + high + 1) >> 1;\n for (int j = 0; j < n; j++) {\n good[j] = (a[j] >= mid);\n }\n dfs(0, -1);\n solve(0, -1, 0);\n int best = 0;\n for (int j = 0; j < n; j++) {\n best = max(best, dp[j]);\n }\n if (best >= k) {\n low = mid;\n } else {\n high = mid - 1;\n }\n }\n printf(\"%d\\n\", low);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "50f10924357ec18b996562c5ef9c961d", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nnamespace Stand_IO_Operation\n{\n\t#define For(i,x,y) for (int i=x,_Lim=y;i<_Lim;i++)\n\t#define Rep(i,x,y) for (int i=x,_Lim=y;i<=_Lim;i++)\n\t#define Dep(i,x,y) for (int i=x,_Lim=y;i>=_Lim;i--)\n\n\t#define mk(a,b) make_pair(a,b)\n\n\ttypedef long long ll;\n\ttypedef double ld;\n\ttypedef pair PII;\n\ttypedef pair PDD;\n\ttypedef pair PLL;\n\n\tinline int read(){\n\t\tint f=1,res=0,ch=getchar();\n\t\twhile (ch<'0' || ch>'9'){\n\t\t\tif (ch=='-') f=-1;ch=getchar();\n\t\t}\n\t\twhile (ch>='0' && ch<='9') res=res*10+ch-48,ch=getchar();\n\t\treturn f*res;\n\t}\n\tint _sta[1<<8];\n\tinline void write(int val){\n\t\tif (!val){\n\t\t\tputchar('0');return;\n\t\t}\n\t\tif (val<0) putchar('-'),val=-val;\n\t\t*_sta=0;while (val>0) _sta[++*_sta]=val%10,val/=10;\n\t\tDep(i,*_sta,1) putchar(_sta[i]+48);\n\t}\n};\nusing namespace Stand_IO_Operation;\n\nconst int N = 200005;\nint n,k,a[N],tag[N],ans,cnt;\nvector G[N];\nint sz[N],dp[N],tag_cnt[N];\n\nvoid init(int val){\n\tans = cnt = 0;\n\tfor(int i = 1;i <= n;i++){\n\t\ttag[i] = a[i] >= val ? 1 : 0;\n\t\tcnt += tag[i];\n\t}\n\tmemset(tag_cnt,0,sizeof tag_cnt);\n}\n\nvoid dfs1(int cur,int fa){\n\tsz[cur] = 1;\n\tfor(int i = 0;i < G[cur].size();i++){\n\t\tint u = G[cur][i];\n\t\tif(u == fa) continue;\n\t\tdfs1(u,cur);\n\t\tsz[cur] += sz[u];\n\t}\n}\n\nvoid dfs2(int cur,int fa){\n\tif(ans >= k) return;\n\tint all = 0,max1 = 0,max2 = 0;\n\tfor(int i = 0;i < G[cur].size();i++){\n\t\tint u = G[cur][i];\n\t\tif(u == fa) continue;\n\t\tdfs2(u,cur);\n\t\ttag_cnt[cur] += tag_cnt[u];\n\t\tif(dp[u] == sz[u])\n\t\t\tall += dp[u];\n\t\telse if(dp[u] > max1){\n\t\t\tmax2 = max1;\n\t\t\tmax1 = dp[u];\n\t\t}\n\t\telse if(dp[u] > max2)\n\t\t\tmax2 = dp[u];\n\t}\n\tif(!tag[cur]) dp[cur] = 0;\n\telse{\n\t\tdp[cur] = all+max1+1;\n\t\ttag_cnt[cur]++;\n\t}\n\tif(n-sz[cur] == cnt-tag_cnt[cur])\n\t\tall += n-sz[cur];\n\tif(tag[cur])\n\t\tans = max(ans,all+max1+max2+1);\n}\n\nint main(){\n\tn = read(); k = read();\n\tfor(int i = 1;i <= n;i++)\n\t\ta[i] = read();\n\tfor(int i = 1;i < n;i++){\n\t\tint u = read(),v = read();\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tint l = 1,r = 1000000,mid;\n\tdfs1(1,0);\n\twhile(l <= r){\n\t\tmid = (l+r)>>1;\n\t\tinit(mid);\n\t\tdfs2(1,0);\n\t\tif(ans >= k) l = mid+1;\n\t\telse r = mid-1;\n\t}\n\twrite(r);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "f23298081fec9d778ef78894e6b98285", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nconst int N = 200005;\nint n,m,lim,p = 1,a[N],nex[N*2],spot[N*2],head[N],fa[N],f[N],g[N];\nint maxcolor[N][2],c[N];\nbool color[N],v[N];\n\nvoid add(int x,int y)\n{\n\tnex[++p] = head[x], head[x] = p, spot[p] = y;\n\tnex[++p] = head[y], head[y] = p, spot[p] = x;\n}\n\nvoid dfs0(int x)\n{\n\tint tp,y;\n\tv[x] = 1;\n\tfor(tp = head[x]; y = spot[tp],tp; tp = nex[tp])\n\t\tif(a[y]>=lim && fa[x]!=y) {\n\t\t\tfa[y] = x;\n\t\t\tdfs0(y);\n\t\t\tif(c[y]) c[x]++;\n\t\t}\n\tc[x] += color[x];\n//\tprintf(\"c[%d] = %d\\t\", x, c[x]);\n}\n/*\nvoid dfs1(int x)\n{\n\tint tp,y,cnt = 0,m1 = 0,m2 = 0;\n\n\tfor(tp = head[x]; y = spot[tp],tp; tp = nex[tp])\n\t\tif(a[y]>=lim && y!=fa[x]) {\n\t\t\tdfs1(y);\n\t\t\tif(!color[y]) cnt += f[y];\n\t\t\telse {\n\t\t\t\tif(m1==0) m1 = f[y];\n\t\t\t\telse {\n\t\t\t\t\tif(f[y]>m2) m2 = f[y];\n\t\t\t\t\tif(m1=lim && spot[tp]!=fa[x])\n\t\t\tdfs2(spot[tp], ret);\n}\n*/\nvoid dfs1(int x)\n{\n\tint tp,y,cnt = 0,m1 = 0,m2 = 0;\n\n\tfor(tp = head[x]; y = spot[tp],tp; tp = nex[tp])\n\t\tif(a[y]>=lim && y!=fa[x]) {\n\t\t\tdfs1(y);\n\t\t\tif(!c[y]) cnt += f[y];\n\t\t\telse {\n\t\t\t\tif(f[y]>m2) m2 = f[y];\n\t\t\t\tif(m10) {\n\t\t\t//xÒªÔÚËüµ±³õÑ¡µÄcolor½áµãºÍËü¸¸Ç׳ýÈ¥ËüÄDz¿·ÖÖÐÑ¡Ôñ \n\t\t\tif(c[x]) {//xµ±³õ±»Ñ¡»ò²»Ñ¡ \n\t\t\t\tif(f[x]maxcolor[x][1])\n\t\t\t\t\t\tmaxcolor[x][1] = g[fa[x]];\n\t\t\t\t\tif(maxcolor[x][0]maxcolor[x][1])\n\t\t\t\t\t\tmaxcolor[x][1] = g[fa[x]]-maxcolor[fa[x]][0]+maxcolor[fa[x]][1];\n\t\t\t\t\tif(maxcolor[x][0]=lim && spot[tp]!=fa[x])\n\t\t\tdfs2(spot[tp], ret);\n}\nint solve()\n{\n\tmemset(v, 0, sizeof(v));\n\tmemset(fa, 0, sizeof(fa));\n\tmemset(maxcolor, 0, sizeof(maxcolor));\n\tmemset(c, 0, sizeof(c));\n\tint maxn = 0;\n//\tcout <<\"solve \" <=lim && v[i]==0) {\n//\t\t\tcout << \"dfs from \"<=m;\n}\n\nint main()\n{\n//\tfreopen(\"shuju.in\", \"r\", stdin);\n//\tfreopen(\"out.txt\", \"w\", stdout);\n\tint i,x,y,l,r,tp;\n\tcin >> n >> m;\n\tfor(i = 1; i<=n; i++)\n\t\tscanf(\"%d\", &a[i]);\n\tfor(i = 1; i>1;\n\t\tmemset(color, 0, sizeof(color));\n\t\tfor(i = 1; i<=n; i++)\n\t\t\tif(a[i]\n#define REP(i, n) for(int i = 0;i < n;++i)\n#define FOR(i, a, b) for(int i = a;i < b;++i)\n#define pb push_back\n#define pc(x) __builtin_popcountll(x)\n#define X first\n#define Y second\nusing namespace std;\ntypedef long long int ll;\ntypedef pair ii;\nconst int MAX = 201000;\nint a[MAX];\nint n, k;\nint eu[MAX], ev[MAX];\nbool cant[MAX];\nint deg[MAX];\nint val[MAX];\nint down[MAX][2];\nbool vis[MAX];\nset e[MAX];\n\nvoid cut(int x) {\n\tset s;\n\tREP(i, n) if(a[i] >= x && e[i].size()) s.insert(ii(deg[i], i));\n\twhile(s.size() && s.begin()->first == 1) {\n\t\tint u = s.begin()->second;\n\t\tdeg[u]--;\n\t\ts.erase(s.begin());\n\t\tif(cant[u]) continue;\n\t\tint v = *e[u].begin();\n\t\te[u].clear();\n\t\te[v].erase(u);\n\t\ts.erase(ii(deg[v], v));\n\t\t--deg[v];\n\t\ts.insert(ii(deg[v], v));\n\t\tval[v] += val[u];\n\t}\n}\n\nint DFS(int u) {\n\tvis[u] = true;\n\tint res = 0;\n\tfor(auto it : e[u]) {\n\t\tint v = it;\n\t\tif(vis[v]) continue;\n\t\tres = max(res, DFS(v));\n\t\tdown[u][0] = max(down[u][0], down[v][1]);\n\t\tif(down[u][0] > down[u][1]) swap(down[u][0], down[u][1]);\n\t}\n\tdown[u][0] += val[u];\n\tdown[u][1] += val[u];\n\treturn max(res, down[u][0] + down[u][1] - val[u]);\n}\n\nbool f(int x) {\n\tREP(i, n) {\n\t\tdeg[i] = 0;\n\t\tif(a[i] >= x) val[i] = 1;\n\t\telse val[i] = 0;\n\t\tvis[i] = cant[i] = false;\n\t\tdown[i][0] = down[i][1] = 0;\n\t\te[i].clear();\n\t}\n\tREP(i, n - 1) {\n\t\tint u = eu[i];\n\t\tint v = ev[i];\n\t\tif(a[u] >= x && a[v] >= x) {\n\t\t\te[u].insert(v);\n\t\t\te[v].insert(u);\n\t\t}\n\t\telse {\n\t\t\tcant[u] = true;\n\t\t\tcant[v] = true;\n\t\t}\n\t\t++deg[u];\n\t\t++deg[v];\n\t}\n\tcut(x);\n\tREP(i, n) {\n\t\tif(!vis[i] && a[i] >= x) {\n\t\t\tif(DFS(i) >= k) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint main() {\n\tsrand(time(NULL));\n\tscanf(\"%d %d\", &n, &k);\n\tREP(i, n) scanf(\"%d\", a + i);\n\tREP(i, n - 1) scanf(\"%d %d\", &eu[i], &ev[i]);\n\tREP(i, n - 1) --eu[i], --ev[i];\n\tint lo = 1;\n\tint hi = 1000000;\n\tint mid = 0;\n\twhile(lo < hi) {\n\t\tmid = (lo + hi + 1) / 2;\n\t\tif(f(mid)) lo = mid;\n\t\telse hi = mid - 1;\n\t}\n\tprintf(\"%d\\n\", lo);\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "75677bb0d548214c01fab4d8371f195c", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define N 200020\n#define ll long long\nusing namespace std;\ninline int read(){\n\tint x=0,f=1;char ch=getchar();\n\twhile(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());\n\twhile(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();\n\treturn f?x:-x;\n}\nint head[N], nxt[N<<1], to[N<<1], cnt;\nvoid insert(int x, int y) {\n\tto[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;\n\tto[++cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;\n}\nint a[N], b[N], n = read(), k = read();\nint f[N], sz[N], nt[N], sum, ans;\nint dfs1(int x, int fa) {\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs1(to[i], x);\n\t\tsz[x] += sz[to[i]];\n\t\tnt[x] += nt[to[i]];\n\t}\n}\nvoid dfs2(int x, int fa) {\n\t// printf(\"%d %d \\n\", x, fa);\n\tf[x] = 1;\n\tint mx = 0, sx = 0;\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs2(to[i], x);\n\t\tif (!nt[to[i]]) f[x] += sz[to[i]];\n\t\telse if (!b[to[i]])\n\t\t\tif (f[to[i]] > mx) sx = mx, mx = f[to[i]];\n\t\t\telse if (f[to[i]] > sx) sx = f[to[i]];\n\t}\n\tf[x] += mx;\n\tif (!b[x]) {\n\t\tif (nt[x] == sum) ans = max(ans, f[x] + sx + n - sz[x]);\n\t\telse ans = max(ans, f[x] + sx);\n\t}\n}\nbool check(int mid) {\n\t// printf(\"%d:\\n\", mid);\n\tans = sum = 0;\n\tfor (int i = 1; i <= n; i++) {\n\t\tsz[i] = 1;\n\t\tsum += nt[i] = b[i] = a[i] <= mid;\n\t}\n\tdfs1(1, 0);\n\tdfs2(1, 0);\n\t// puts(ans >= k ? \"Yes\" : \"No\");\n\treturn ans >= k;\n}\nint main(int argc, char const *argv[]) {\n\tint l = 1, r = 0, ans;\n\tfor (int i = 1; i <= n; i++)\n\t\tr = max(r, a[i] = read());\n\tfor (int i = 1; i < n; i++)\n\t\tinsert(read(), read());\n\tr ++;\n\twhile (l <= r) {\n\t\tint mid = l + r >> 1;\n\t\tif (check(mid)) l = mid + 1;\n\t\telse ans = mid, r = mid - 1;\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n\t \t \t\t \t \t\t\t \t \t\t\t\t//hellobig_tq//hello//hello", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "d615e0223aeddfb79a81fa60a28750d1", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\nconst int maxn = 2e5 + 5;\n\nstruct edge\n{\n int to, next;\n\n edge(int to = 0, int next = 0) : to(to), next(next) {}\n} G[maxn << 1];\nint head[maxn], tot, pre[maxn], a[maxn], dp[maxn][2], sz[maxn], n, k, m, que[maxn];\n\nvoid init()\n{\n memset(head, -1, sizeof(head));\n tot = 0;\n}\n\nvoid addedge(int u, int v)\n{\n G[tot] = edge(v, head[u]);\n head[u] = tot++;\n G[tot] = edge(u, head[v]);\n head[v] = tot++;\n}\n\nbool dfs(int u, int fa)\n{\n bool ret = a[u] >= m;\n dp[u][0] = dp[u][1] = -1;\n sz[u] = 0;\n pre[u] = fa;\n for(int i = head[u]; ~i; i = G[i].next)\n {\n int v = G[i].to;\n if(v == fa) continue;\n bool tmp = dfs(v, u);\n ret &= tmp;\n if(a[u] >= m)\n {\n if(tmp) sz[u] += sz[v] + 1;\n else if(dp[u][0] <= dp[v][0] + sz[v] + 1)\n {\n dp[u][1] = dp[u][0];\n dp[u][0] = dp[v][0] + sz[v] + 1;\n }\n else if(dp[u][1] < dp[v][0] + sz[v] + 1) dp[u][1] = dp[v][0] + sz[v] + 1;\n }\n }\n return ret;\n}\n\nbool bfs(int s)\n{\n int front = 0, rear = 0;\n que[rear++] = s;\n while(front < rear)\n {\n int u = que[front++], fa = pre[u];\n if(a[u] >= m && max(dp[u][0], 0) + sz[u] + 1 >= k) return true;\n for(int i = head[u]; ~i; i = G[i].next)\n {\n int v = G[i].to;\n if(v == fa) continue;\n que[rear++] = v;\n if(a[u] >= m && a[v] >= m)\n {\n if(dp[u][1] == -1 && ~dp[v][0]) sz[v] += sz[u] + 1;\n else\n {\n int tmp1 = dp[u][0] == dp[v][0] + sz[v] + 1 ? dp[u][1] : dp[u][0];\n int tmp2 = ~dp[v][0] ? sz[u] + 1 : sz[u] - sz[v];\n if(dp[v][0] <= tmp1 + tmp2)\n {\n dp[v][1] = dp[v][0];\n dp[v][0] = tmp1 + tmp2;\n }\n else if(dp[v][1] < tmp1 + tmp2) dp[v][1] = tmp1 + tmp2;\n }\n }\n }\n }\n return false;\n}\n\nint main()\n{\n while(~scanf(\"%d%d\", &n, &k))\n {\n init();\n int _max = 0;\n for(int i = 1; i <= n; ++i)\n {\n scanf(\"%d\", &a[i]);\n _max = max(_max, a[i]);\n }\n for(int i = 1; i < n; ++i)\n {\n int u, v;\n scanf(\"%d%d\", &u, &v);\n addedge(u, v);\n }\n int l = 1, r = _max;\n while(l < r)\n {\n m = (l + r + 1) >> 1;\n// if(m > 3)\n// {\n// cout << 1 << endl;\n// }\n dfs(1, 0);\n bfs(1) ? l = m : r = m - 1;\n }\n printf(\"%d\\n\", l);\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "64c6c86e4ca19d4584df05c4517e6277", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#ifdef NeverBeRed\n#include \"debug.h\"\n#else\n#define debug(...) 9715\n#endif\ntypedef long long ll;\ntypedef long double ld;\ntypedef complex point;\n#define F first\n#define S second\n\n\n\nint main()\n{\n\t#ifdef TurnRed\n\t\tfreopen(\"a.in\", \"r\", stdin);\n\t\t//freopen(\"a.out\", \"w\", stdout);\n\t#endif\n\n\tios_base::sync_with_stdio(0), cin.tie(0);\n\n\tint n, k;\n\tcin >> n >> k;\n\tvector val(n);\n\tfor (auto &i : val) cin >> i;\n\tvector> adj(n);\n\tfor (int i = 1, u, v; i < n; ++i)\n\t{\n\t\tcin >> u >> v;\n\t\t--u, --v;\n\t\tadj[u].push_back(v);\n\t\tadj[v].push_back(u);\n\t}\n\n\tauto solve = [&](int mn)\n\t{\n\t\tauto w = [&](int u) { return val[u] >= mn; };\n\n\t\tvector sz(n), dp(n), wsz(n), rdp(n);\n\t\tvector> sub(n);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tsub[i].insert(0);\n\n\t\tfunction dfs = [&](int u, int p)\n\t\t{\n\t\t\tsz[u] = 1;\n\t\t\twsz[u] = w(u);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tdfs(v, u);\n\t\t\t\t\tsz[u] += sz[v];\n\t\t\t\t\twsz[u] += wsz[v];\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] += sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].insert(rdp[v]);\n\t\t\t\t}\n\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\t\t};\n\t\tdfs(0, -1);\n\n\t\tint best = 0;\n\t\tfunction dfs2 = [&](int u, int p)\n\t\t{\n//\t\t\tdebug(u, rdp[u]);\n\t\t\tbest = max(best, rdp[u]);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tint rdpu = rdp[u], rdpv = rdp[v];\n\n\t\t\t\t\tint dpu = dp[u], u_dpv = -1;\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] -= sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].erase(sub[u].find(rdp[v])), u_dpv = rdp[v];\n\t\t\t\t\tint szu = sz[u], wszu = wsz[u];\n\t\t\t\t\tsz[u] -= sz[v];\n\t\t\t\t\twsz[u] -= wsz[v];\n\n\t\t\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\n\t\t\t\t\tint dpv = dp[v], szv = sz[v], wszv = wsz[v];\n\t\t\t\t\tsz[v] += sz[u];\n\t\t\t\t\twsz[v] += wsz[u];\n\t\t\t\t\tint v_dpu = -1;\n\t\t\t\t\tif (sz[u] == wsz[u])\n\t\t\t\t\t\tdp[v] += sz[u];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[v].insert(rdp[u]), v_dpu = rdp[u];\n\t\t\t\t\trdp[v] = (dp[v] + 1 + *sub[v].rbegin()) * w(v);\n\n\t\t\t\t\tdfs2(v, u);\n\n\t\t\t\t\tif (v_dpu != -1) sub[v].erase(sub[v].find(v_dpu));\n\t\t\t\t\tsz[v] = szv;\n\t\t\t\t\twsz[v] = wszv;\n\t\t\t\t\tdp[v] = dpv;\n\n\t\t\t\t\twsz[u] = wszu;\n\t\t\t\t\tsz[u] = szu;\n\t\t\t\t\tif (u_dpv != -1) sub[u].insert(u_dpv);\n\t\t\t\t\tdp[u] = dpu;\n\n\t\t\t\t\trdp[u] = rdpu;\n\t\t\t\t\trdp[v] = rdpv;\n\t\t\t\t}\n\t\t};\n\t\tdfs2(0, -1);\n\n\t\treturn best;\n\t};\n\n//\tdebug(solve(5));\n\n\tint lo = 1, hi = 1e6;\n\twhile (lo < hi)\n\t{\n\t\tint md = (lo + hi + 1) >> 1;\n\t\tif (solve(md) >= k)\n\t\t\tlo = md;\n\t\telse\n\t\t\thi = md - 1;\n\t}\n\tcout << lo << \"\\n\";\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "257c709e533d2499f7285ba7461d9dbb", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include\n#include\n#include \n#include\n#include\n#include\n#include\n#include \n#include\n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include \n#include \n#include\n#include\n#include \n#include \n#include\nusing namespace std;\n#define fast {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}\n#define ll long long\n#define EPS (1e-8);\n#define clr(x) memset(x,0,sizeof(x));\n#define sz(x) ((int)(x.size()))\n#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)\n#define neg(x) memset(x,-1,sizeof(x));\n#define read(file) freopen (file,\"r\",stdin)\n#define write(file) freopen (file,\"w\",stdout)\n#define INF (1LL << 28)\n#define endl \"\\n\"\n#define M(x,y) make_pair(x,y)\ntypedef vector vi;\ntypedef pair ii;\ninline int toInt(string s){int v; istringstream sin(s);sin>>v;return v;}\ntemplate inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}\n#define MAXN 200005\nvector g[MAXN];\nint val[MAXN];\nint n,k;\nbool bad[MAXN];\nint DP[MAXN];\nint deg[MAXN];\nint ben[MAXN];\nqueue Q;\nint maxr = 0;\n\n\nint solve(int u,int p)\n{\n\n\tDP[u] = ben[u];\n\tint c1 = -1;\n\tint c2 = -1;\n\tREP(j,g[u])\n\t{\n\t\tint v = g[u][j];\n\t\tif(v == p) continue;\n\t\tDP[u] = max(DP[u],solve(v,u) + ben[u]);\n\t\tif(DP[v] <= c1)\n\t\t{\n\t\t\tif(DP[v] > c2) c2 = DP[v];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tc2 = c1;\n\t\t\tc1 = DP[v];\n\t\t}\n\n\t}\n\tmaxr = max(maxr,DP[u]);\n\tmaxr = max(maxr,ben[u] + c1 + c2);\n\treturn DP[u];\n}\nbool used[MAXN];\nbool can(int x)\n{\n\tmaxr = 0;\n\tclr(bad);\n\tclr(used);\n\tfor(int i = 0 ; i < n ; ++i)\n\t{\n\t\tdeg[i] = g[i].size();\n\t\tif(val[i] < x)\n\t\t{\n\t\t\tbad[i] = 1;\n\t\t\tben[i] = -INF;\n\t\t}\n\t\telse\n\t\t\tben[i] = 1;\n\t\tif(!bad[i] && deg[i] == 1) Q.push(i);\n\t}\n\n\n\twhile(!Q.empty())\n\t{\n\t\tint u = Q.front(); Q.pop();\n\t\t//deg[u]--;\n\t\t//cerr << u+1 << endl;\n\t\tused[u] = 1;\n\t\tREP(j,g[u])\n\t\t{\n\t\t\tint v = g[u][j];\n\t\t\tif(!bad[v])\n\t\t\t{\n\t\t\t\tif(!used[v])\n\t\t\t\t{\n\t\t\t\t\tben[v] += ben[u];\n\t\t\t\t\tben[u] = 0;\n\t\t\t\t\tdeg[v]--;\n\t\t\t\t\tif(deg[v] == 1)\n\t\t\t\t\t\tQ.push(v);\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\t}\n\t//for(int i = 0 ; i < n ; ++i)\n\t//cerr << i << \" \" << ben[i] << endl;\n\tmemset(DP,-1,sizeof(DP));\n\tsolve(0,-1);\n\treturn maxr >= k;\n}\nint main()\n{\n\tscanf(\"%d %d\",&n,&k);\n\n\tint mini = 1 << 30;\n\tfor(int i = 0 ; i < n ; ++i)\n\t{\n\t\tscanf(\"%d\",&val[i]);\n\t\tmini = min(mini,val[i]);\n\t}\n\n\tint u,v;\n\tfor(int j = 0 ; j < n - 1 ; ++j)\n\t{\n\t\tscanf(\"%d %d\",&u,&v);\n\t\t--u; --v;\n\t\tg[u].push_back(v);\n\t\tg[v].push_back(u);\n\t}\n\n\tint lo = 0;\n\tint hi = 1 << 29;\n\tint ans = -1;\n\n\twhile(lo <= hi)\n\t{\n\t\tint mid = lo + (hi - lo) / 2;\n\t\tif(can(mid))\n\t\t{\n\t\t\tans = mid;\n\t\t\tlo = mid + 1;\n\n\t\t}\n\t\telse hi = mid - 1;\n\t}\n\tprintf(\"%d\",ans);\n\tputs(\"\");\n\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "f0c4fcf3226adc980b2ff035071aa4fd", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int N = 200001;\n\n\nint n, k;\nvector < int > G[N];\nint a[N];\nint dp1[N], dp2[N];\nint sum[N];\nint cnt[N];\npair < int, int > pr[N];\n\nvoid dfs1(int v, int p, int x)\n{\n\tcnt[v] = 1;\n\tpr[v] = make_pair(-1, -1);\n\tfor (int i = 0; i < G[v].size(); ++i)\n\t{\n\t\tint to = G[v][i];\n\t\tif (to == p)\n\t\t\tcontinue;\n\t\tdfs1(to, v, x);\n\t\tcnt[v] += cnt[to];\n\t\tif (cnt[to] == dp1[to])\n\t\t\tdp1[v] += dp1[to];\n\t\telse\n\t\t{\n\t\t\tif (pr[v].first == -1 || dp1[pr[v].first] < dp1[to])\n\t\t\t{\n\t\t\t\tpr[v].second = pr[v].first;\n\t\t\t\tpr[v].first = to;\n\t\t\t}\n\t\t\telse if (pr[v].second == -1 || dp1[pr[v].second] < dp1[to])\n\t\t\t\tpr[v].second = to;\n\t\t}\n\t}\n\tif (a[v] < x)\n\t\tdp1[v] = sum[v] = 0;\n\telse\n\t{\n\t\tsum[v] = dp1[v];\n\t\tdp1[v] += 1;\n\t\tif (pr[v].first != -1)\n\t\t\tdp1[v] += dp1[pr[v].first];\n\t}\n}\n\nvoid dfs2(int v, int p, int x)\n{\n\tif (p == -1 || a[p] < x)\n\t{\n\t\tdp2[v] = 0;\n\t}\n\telse\n\t{\n\t\tint c = sum[p] + 1;\n\t\tint mx = 0;\n\t\tif (pr[p].first != -1 && pr[p].first != v)\n\t\t\tmx = dp1[pr[p].first];\n\t\telse if (pr[p].second != -1 && pr[p].second != v)\n\t\t\tmx = dp1[pr[p].second];\n\t\tif (dp1[v] == cnt[v])\n\t\t\tc -= dp1[v];\n\t\tdp2[v] = c;\n\t\tif (dp2[p] == n - cnt[p])\n\t\t\tdp2[v] += dp2[p];\n\t\telse\n\t\t\tmx = max(mx, dp2[p]);\n\t\tdp2[v] += mx;\n\t}\n\tfor (int i = 0; i < G[v].size(); ++i)\n\t{\n\t\tint to = G[v][i];\n\t\tif (to == p)\n\t\t\tcontinue;\n\t\tdfs2(to, v, x);\n\t}\n}\n\nbool check(int x)\n{\n\tfor (int i = 0; i < n; ++i)\n\t\tdp1[i] = dp2[i] = sum[i] = 0;\n\tdfs1(0, -1, x);\n\tdfs2(0, -1, x);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tif (a[i] < x)\n\t\t\tcontinue;\n\t\tint ans = sum[i] + 1;\n\t\tint mx = 0;\n\t\tif (pr[i].first != -1)\n\t\t\tmx = dp1[pr[i].first];\n\t\tif (dp2[i] == n - cnt[i])\n\t\t\tans += dp2[i];\n\t\telse\n\t\t\tmx = max(mx, dp2[i]);\n\t\tif (ans + mx >= k)\n\t\t\treturn true;\n\t}\n\treturn false;\n}\nint main()\n{\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n\tscanf(\"%d%d\", &n, &k);\n\tfor (int i = 0; i < n; ++i)\n\t\tscanf(\"%d\", &a[i]);\n\tfor (int i = 1; i < n; ++i)\n\t{\n\t\tint u, v; scanf(\"%d%d\", &u, &v); u--; v--;\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tint l = 0, r = 1000001;\n\twhile (l + 1 < r)\n\t{\n\t\tint mid = (l + r) / 2;\n\t\tif (check(mid))\n\t\t\tl = mid;\n\t\telse\n\t\t\tr = mid;\n\t}\n\tprintf(\"%d\\n\", l);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "2305af633182206737e730238aa71836", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nconst int N = 2e5 + 5;\nstruct DP {\n\tint sum, mx;\n\tDP operator + (const DP &rhs) const {\n\t\treturn DP {sum + rhs.sum, std::max (mx, rhs.mx)};\n\t}\n};\nint a[N], total[N], down[N], dp[N];\nbool good[N];\nstd::vector edge[N];\nint n, k;\n\nvoid DFS(int u, int fa)\t{\n\ttotal[u] = 1;\n\tint sum = 0, mx = 0;\n\tfor (auto v: edge[u])\t{\n\t\tif (v == fa)\tcontinue;\n\t\tDFS (v, u);\n\t\ttotal[u] += total[v];\n\t\tif (down[v] == total[v]) {\n\t\t\tsum += total[v];\n\t\t} else {\n\t\t\tmx = std::max (mx, down[v]);\n\t\t}\n\t}\n\tdown[u] = sum + mx + 1;\n\tif (!good[u])\tdown[u] = 0;\n}\n\nvoid DFS2(int u, int fa, int up)\t{\n\tstd::vector children;\n\tint sum = 0, mx = 0;\n\tfor (auto v: edge[u])\t{\n\t\tif (v == fa)\tcontinue;\n\t\tif (down[v] == total[v]) {\n\t\t\tsum += down[v];\n\t\t} else {\n\t\t\tmx = std::max (mx, down[v]);\n\t\t}\n\t\tchildren.push_back (v);\n\t}\n\tif (up == n - total[u]) {\n\t\tsum += up;\n\t} else {\n\t\tmx = std::max (mx, up);\n\t}\n\tdp[u] = sum + mx + 1;\n\tif (!good[u])\tdp[u] = 0;\n\n\tint sz = children.size ();\n\tif (sz == 0)\treturn ;\n\tstd::vector predown (sz + 1);\n\tpredown[0] = {0, 0};\n\tfor (int i=0; i sufdown (sz + 1);\n\tsufdown[sz] = {0, 0};\n\tfor (int i=sz-1; i>=0; --i)\t{\n\t\tint v = children[i];\n\t\tsufdown[i] = sufdown[i+1];\n\t\tif (down[v] == total[v]) {\n\t\t\tsufdown[i].sum += down[v];\n\t\t} else {\n\t\t\tsufdown[i].mx = std::max (sufdown[i].mx, down[v]);\n\t\t}\n\t}\n\tfor (int i=0; i= v);\n\tDFS (1, 0);\n\tDFS2 (1, 0, 0);\n\tfor (int i=1; i<=n; ++i)\t{\n\t\tif (dp[i] >= k)\treturn true;\n\t}\n\treturn false;\n}\n\nint main()\t{\n\tscanf (\"%d%d\", &n, &k);\n\tfor (int i=1; i<=n; ++i)\t{\n\t\tscanf (\"%d\", a+i);\n\t}\n\tfor (int u, v, i=1; i> 1;\n\t\tif (check (mid))\t{\n\t\t\tlow = mid;\n\t\t} else {\n\t\t\thigh = mid - 1;\n\t\t}\n\t}\n\tprintf (\"%d\\n\", low);\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "b966a3906480a0fd991c136a87156f75", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#ifdef NeverBeRed\n#include \"debug.h\"\n#else\n#define debug(...) 9715\n#endif\ntypedef long long ll;\ntypedef long double ld;\ntypedef complex point;\n#define F first\n#define S second\n\n\n\nint main()\n{\n\t#ifdef TurnRed\n\t\t//freopen(\"a.in\", \"r\", stdin);\n\t\t//freopen(\"a.out\", \"w\", stdout);\n\t#endif\n\n\tios_base::sync_with_stdio(0), cin.tie(0);\n\n\tint n, k;\n\tcin >> n >> k;\n\tvector val(n);\n\tfor (auto &i : val) cin >> i;\n\tvector> adj(n);\n\tfor (int i = 1, u, v; i < n; ++i)\n\t{\n\t\tcin >> u >> v;\n\t\t--u, --v;\n\t\tadj[u].push_back(v);\n\t\tadj[v].push_back(u);\n\t}\n\n\tauto solve = [&](int mn)\n\t{\n\t\tauto w = [&](int u) { return val[u] >= mn; };\n\n\t\tvector sz(n), dp(n), wsz(n), rdp(n);\n\t\tvector> sub(n);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tsub[i].insert(0);\n\n\t\tfunction dfs = [&](int u, int p)\n\t\t{\n\t\t\tsz[u] = 1;\n\t\t\twsz[u] = w(u);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tdfs(v, u);\n\t\t\t\t\tsz[u] += sz[v];\n\t\t\t\t\twsz[u] += wsz[v];\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] += sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].insert(rdp[v]);\n\t\t\t\t}\n\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\t\t};\n\t\tdfs(0, -1);\n\n\t\tint best = 0;\n\t\tfunction dfs2 = [&](int u, int p)\n\t\t{\n\t\t\tbest = max(best, rdp[u]);\n\t\t\tfor (auto v : adj[u])\n\t\t\t\tif (v != p)\n\t\t\t\t{\n\t\t\t\t\tint rdpu = rdp[u], rdpv = rdp[v];\n\n\t\t\t\t\t//int dpu = dp[u], u_dpv = -1;\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] -= sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].erase(sub[u].find(rdp[v]));//, u_dpv = rdp[v];\n\t\t\t\t\t//int szu = sz[u], wszu = wsz[u];\n\t\t\t\t\tsz[u] -= sz[v];\n\t\t\t\t\twsz[u] -= wsz[v];\n\n\t\t\t\t\trdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);\n\n\t\t\t\t\t//int dpv = dp[v], szv = sz[v], wszv = wsz[v];\n\t\t\t\t\tsz[v] += sz[u];\n\t\t\t\t\twsz[v] += wsz[u];\n\t\t\t\t\t//int v_dpu = -1;\n\t\t\t\t\tif (sz[u] == wsz[u])\n\t\t\t\t\t\tdp[v] += sz[u];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[v].insert(rdp[u]);//, v_dpu = rdp[u];\n\t\t\t\t\trdp[v] = (dp[v] + 1 + *sub[v].rbegin()) * w(v);\n\n\t\t\t\t\tdfs2(v, u);\n\n\t\t\t\t\trdp[v] = rdpv;\n\t\t\t\t\tif (sz[u] == wsz[u])\n\t\t\t\t\t\tdp[v] -= sz[u];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[v].erase(sub[v].find(rdp[u]));\n\n\t\t\t\t\twsz[v] -= wsz[u];\n\t\t\t\t\tsz[v] -= sz[u];\n\n\t\t\t\t\trdp[u] = rdpu;\n\n\t\t\t\t\twsz[u] += wsz[v];\n\t\t\t\t\tsz[u] += sz[v];\n\n\t\t\t\t\tif (sz[v] == wsz[v])\n\t\t\t\t\t\tdp[u] += sz[v];\n\t\t\t\t\telse\n\t\t\t\t\t\tsub[u].insert(rdp[v]);\n\n\t\t\t\t\t//rdp[u] = rdpu;\n\t\t\t\t\t//rdp[v] = rdpv;\n\t\t\t\t}\n\t\t};\n\t\tdfs2(0, -1);\n\n\t\treturn best;\n\t};\n\n\tint lo = 1, hi = 1e6;\n\twhile (lo < hi)\n\t{\n\t\tint md = (lo + hi + 1) >> 1;\n\t\tif (solve(md) >= k)\n\t\t\tlo = md;\n\t\telse\n\t\t\thi = md - 1;\n\t}\n\tcout << lo << \"\\n\";\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "ba4c468a0bfdceaa42f9fda69d34543c", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int N = 2e5+5;\nint n,sz[N],a[N],c,ret,dp[N];\npair m[N][2];\nvector adj[N];\nvoid dfs(int x,int p){\n\tint f=0,u,i,d;dp[x]=0;\n\tif(!sz[x])f=1,++sz[x];\n\td= a[x]>=c ? 1 :0;\n\tfor(i=0;i=c){\n\t\tif(d==n-sz[x])v=d,t=0;\n\t\tar[0]= m[x][0].first,ar[1]= m[x][1].first,ar[2]=t;\n\t\tsort(ar,ar+3);\n\t\tret= max(ret, dp[x]+ ar[2]+ar[1]+v-m[x][0].first);\n\t}\n\tfor(i=0;i>n>>k;\n\tfor(i=1;i<=n;++i)scanf(\"%d\",&a[i]);\n\tfor(i=0;i=k)l= mid;\n\t\telse\n\t\t\tr= mid-1;\n\t}\n\tprintf(\"%d\\n\",l);\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "2c3f0ff4501264618883ed94f97edab2", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define N 200020\n#define ll long long\nusing namespace std;\ninline int read(){\n\tint x=0,f=1;char ch=getchar();\n\twhile(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());\n\twhile(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();\n\treturn f?x:-x;\n}\nint head[N], nxt[N<<1], to[N<<1], cnt;\nvoid insert(int x, int y) {\n\tto[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;\n\tto[++cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;\n}\nint a[N], b[N], n = read(), k = read();\nint f[N], sz[N], nt[N], sum, ans;\nint dfs1(int x, int fa) {\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs1(to[i], x);\n\t\tsz[x] += sz[to[i]];\n\t\tnt[x] += nt[to[i]];\n\t}\n}\nvoid dfs2(int x, int fa) {\n\t// printf(\"%d %d \\n\", x, fa);\n\tf[x] = 1;\n\tint mx = 0, sx = 0;\n\tfor (int i = head[x]; i; i = nxt[i]) {\n\t\tif (to[i] == fa) continue;\n\t\tdfs2(to[i], x);\n\t\tif (!nt[to[i]]) f[x] += sz[to[i]];\n\t\telse if (!b[to[i]])\n\t\t\tif (f[to[i]] > mx) sx = mx, mx = f[to[i]];\n\t\t\telse if (f[to[i]] > sx) sx = f[to[i]];\n\t}\n\tf[x] += mx;\n\tif (!b[x]) {\n\t\tif (nt[x] == sum) ans = max(ans, f[x] + sx + n - sz[x]);\n\t\telse ans = max(ans, f[x] + sx);\n\t}\n}\nbool check(int mid) {\n\t// printf(\"%d:\\n\", mid);\n\tans = sum = 0;\n\tfor (int i = 1; i <= n; i++) {\n\t\tsz[i] = 1;\n\t\tsum += nt[i] = b[i] = a[i] <= mid;\n\t}\n\tdfs1(1, 0);\n\tdfs2(1, 0);\n\t// puts(ans >= k ? \"Yes\" : \"No\");\n\treturn ans >= k;\n}\nint main(int argc, char const *argv[]) {\n\tint l = 1, r = 0, ans;\n\tfor (int i = 1; i <= n; i++)\n\t\tr = max(r, a[i] = read());\n\tfor (int i = 1; i < n; i++)\n\t\tinsert(read(), read());\n\tr ++;\n\twhile (l <= r) {\n\t\tint mid = l + r >> 1;\n\t\tif (check(mid)) l = mid + 1;\n\t\telse ans = mid, r = mid - 1;\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n\t \t \t\t \t \t\t\t \t \t\t\t\t//hellobig_tq", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "binary search", "graphs"], "code_uid": "7d3bdbc5e316e8e0abf2572428870072", "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n\nusing namespace std;\nusing namespace __gnu_pbds;\n \ntypedef long long ll;\ntypedef long double ld;\n\ntypedef pair pi;\ntypedef pair pl;\ntypedef pair pd;\n\ntypedef vector vi;\ntypedef vector vl;\ntypedef vector vpi;\n\ntemplate using Tree = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\n#define FOR(i, a, b) for (int i=a; i<(b); i++)\n#define F0R(i, a) for (int i=0; i<(a); i++)\n#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)\n#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)\n\n#define sz(x) (int)(x).size()\n#define mp make_pair\n#define pb push_back\n#define f first\n#define s second\n#define lb lower_bound\n#define ub upper_bound\n#define all(x) x.begin(), x.end()\n\nconst int MOD = 1000000007;\nconst double PI = 4*atan(1);\nconst ll INF = 4e18;\nconst int MX = 100001;\n\nint x,k,n,q,c[9];\nvi ok;\n\ntemplate struct mat {\n array,SZ> d;\n \n mat() {\n F0R(i,SZ) F0R(j,SZ) d[i][j] = INF;\n }\n \n mat operator+(const mat& m) {\n mat a;\n F0R(i,SZ) F0R(j,SZ) a.d[i][j] = (d[i][j]+m.d[i][j]) % MOD;\n return a;\n }\n \n mat operator*(const mat& m) {\n mat a;\n for (int i: ok)\n for (int j: ok)\n for (int k: ok)\n a.d[i][k] = min(a.d[i][k],d[i][j]+m.d[j][k]);\n return a;\n }\n \n mat operator^(ll p) {\n mat a, b(*this); \n F0R(i,SZ) a.d[i][i] = 0;\n \n while (p) {\n if (p&1) a = a*b;\n b = b*b;\n p /= 2;\n }\n \n return a;\n }\n \n void print() {\n F0R(i,SZ) {\n F0R(j,SZ) cout << d[i][j] << \" \";\n cout << \"\\n\";\n }\n cout << \"------------\\n\";\n }\n};\n\nvl mult(vl a, mat<256> b) {\n vl A(256); F0R(i,256) A[i] = INF;\n for (int i: ok)\n for (int j: ok)\n A[j] = min(A[j],a[i]+b.d[i][j]);\n \n return A;\n}\n\nmat<256> trans;\nvl a(256);\nset man;\nmap wei;\nint lst = 1;\n\nvoid solve(int ind) {\n a = mult(a,trans^(ind-lst));\n \n mat<256> TRANS = trans;\n F0R(i,1<> x >> k >> n >> q;\n FOR(i,1,k+1) cin >> c[i];\n F0R(i,1< x) man.insert(n-x);\n \n F0R(i,q) {\n int p,wp; cin >> p >> wp;\n wei[p] = wp;\n FOR(j,1,k+1) if (p-j >= 1 && p-j <= n-x) man.insert(p-j);\n }\n \n F0R(i,256) a[i] = INF;\n a[(1<\nusing namespace std;\n\ntypedef pair pii;\ntypedef long long ll;\n\nconst int MN = 75;\n\nstruct mat {\n ll T[MN][MN];\n void O() {\n for(int i = 0; i < MN; i++) for(int j = 0; j < MN; j++) T[i][j] = 1e18;\n }\n void I() {\n for(int i = 0; i < MN; i++) for(int j = 0; j < MN; j++) T[i][j] = i == j;\n }\n};\n\nmat mult(mat a, mat b) {\n mat ret;\n ret.O();\n\n for(int i = 0; i < MN; i++) {\n for(int j = 0; j < MN; j++) {\n for(int k = 0; k < MN; k++) {\n ret.T[i][j] = min(ret.T[i][j], a.T[i][k] + b.T[k][j]);\n }\n }\n }\n\n return ret;\n}\nmat exp(mat &x, int n) {\n if(n == 1) return x;\n mat tmp = exp(x, n / 2);\n if(n % 2) return mult(mult(tmp, tmp), x);\n else return mult(tmp, tmp);\n}\n\nint X, K, N, Q;\nint C[10];\npii W[30];\nmap chk;\n\nvector Mask;\nint inv[256];\nmat P, Y, A;\n\nint main() {\n scanf(\"%d %d %d %d\", &X, &K, &N, &Q);\n\n for(int i = 1; i <= K; i++) {\n scanf(\"%d\", &C[i]);\n }\n\n for(int i = 0; i < Q; i++) {\n scanf(\"%d %d\", &W[i].first, &W[i].second);\n W[i].first--;\n\n chk[ W[i].first ] = W[i].second;\n }\n\n sort(W, W + Q);\n\n for(int i = 0; i < (1 << K); i++) {\n int cnt = 0;\n for(int j = 0; j < K; j++) if(i & (1 << j)) cnt++;\n\n if(cnt == X) Mask.push_back(i);\n }\n for(int i = 0; i < Mask.size(); i++) inv[ Mask[i] ] = i;\n\n A.O();\n A.T[ inv[ ((1 << X) - 1) ] ][0] = 0;\n P.O();\n\n for(int i = 0; i < Mask.size(); i++) {\n int mask = Mask[i];\n if(!(mask & 1)) {\n P.T[i][ inv[mask >> 1] ] = 0;\n continue;\n }\n\n for(int j = 1; j <= K; j++) {\n if(mask & (1 << j)) continue;\n\n int mask2 = mask ^ (1 << j);\n mask2 >>= 1;\n\n P.T[i][ inv[mask2] ] = C[j];\n }\n }\n\n int cur_len = X;\n\n //*\n for(int q = Q - 1; q >= 0; q--) {\n int len = N - W[q].first;\n\n if(len > cur_len) {\n A = mult(exp(P, len - cur_len), A);\n cur_len = len;\n }\n\n while(cur_len < N - W[q].first + K && cur_len < N) {\n int p = N - cur_len - 1;\n\n Y.O();\n\n for(int i = 0; i < Mask.size(); i++) {\n int mask = Mask[i];\n if(!(mask & 1)) {\n Y.T[i][ inv[mask >> 1] ] = 0;\n continue;\n }\n\n for(int j = 1; j <= K; j++) {\n if(mask & (1 << j)) continue;\n\n int mask2 = mask ^ (1 << j);\n mask2 >>= 1;\n\n Y.T[i][ inv[mask2] ] = C[j] + (chk.find(p + j) != chk.end()? chk[p + j] : 0);\n }\n }\n\n A = mult(Y, A);\n cur_len++;\n }\n }\n //*/\n\n if(N > cur_len) {\n A = mult(exp(P, N - cur_len), A);\n }\n\n printf(\"%lld\", A.T[ inv[(1 << X) - 1] ][0]);\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "dc6c97dafdfb56fbe87ab677e4484d59", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nint c[15];\nint ma[305];\nint cnt=0;\nconst long long inf=1999999999999999999ll;\nstruct matrix\n{\n long long a[75][75];\n friend matrix operator * (const matrix &a,const matrix &b)\n {\n int i,j,k;\n matrix c;\n for (i=0;i p[75];\nint main()\n{\n #ifdef absi2011\n freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);\n #endif\n int x,k;\n int n,q;\n scanf(\"%d%d%d%d\",&x,&k,&n,&q);\n int i;\n for (i=0;i>1]]=c[j-1];\n }\n }\n else\n {\n a.a[ma[i]][ma[i>>1]]=0;\n }\n }\n int now=1;\n for (i=0;i=n-x+1)\n {\n ans+=p[i].second;\n continue;\n }\n xx=xx*mul(p[i].first-now);\n now=p[i].first;\n int j;\n for (j=1;j<(1<\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define maxs 75\n#define maxn 15\ntypedef long long ll;\n\nint m,k,n,q,cnt;\nint state[maxs],inv[maxs*10],p[maxs];\nint v[maxn];\n\nmap h;\n\ninline int read(){\n\tint x=0,f=1; char ch=getchar();\n\tfor (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;\n\tfor (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';\n\treturn x*f;\n}\n\nvoid dfs(int x,int mask,int bitcnt){\n\tif (x>n){\n\t\tif (bitcnt!=m) return;\n\t\tstate[++cnt]=mask,inv[mask]=cnt;\n\t\treturn;\n\t}\n\tif (bitcnt>1]]=0;\n\t\t\tint x=__builtin_ctz(mask)+1; mask^=mask&-mask; mask>>=1;\n\t\t\tfor (int j=x+1;j<=n+1;j++)\n\t\t\t\tif (~mask>>(j-2)&1) a[i][inv[1<<(j-2)|mask]]=v[j-x];\n\t\t}\n\t}\n\tvoid print(){\n\t\tfor (int i=1;i<=cnt;i++){\n\t\t\tfor (int j=1;j<=cnt;j++)\n\t\t\t\tprintf(\"%lld \",a[i][j]);\n\t\t\tputs(\"\");\n\t\t}\n\t}\n}ans,trans;\n\nmatrix power(matrix a,int k){\n\tmatrix ret; ret.init();\n\tfor (;k;k>>=1,a=a*a) if (k&1) ret=ret*a;\n\treturn ret;\n}\n\nmatrix brute(matrix a,int pos){\n\tmatrix ret; ret.clear();\n\tfor (int i=1;i<=cnt;i++){\n\t\tint mask=state[i]; if (~mask&1) ret.a[1][inv[mask>>1]]=min(ret.a[1][inv[mask>>1]],a.a[1][i]);\n\t\tint x=__builtin_ctz(mask)+1; mask^=mask&-mask,mask>>=1;\n\t\tfor (int j=x+1;j<=n+1;j++)\n\t\t\tif (~mask>>(j-2)&1){\n\t\t\t\tll res=a.a[1][i]+v[j-x]+h[pos+j-1];\n\t\t\t\tint tmp=1<<(j-2)|mask;\n\t\t\t\tret.a[1][inv[tmp]]=min(ret.a[1][inv[tmp]],res);\n\t\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\tm=read(),n=read(),k=read()-m+1,q=read();\n\tfor (int i=1;i<=n;i++) v[i]=read();\n\tdfs(1,0,0); trans.build();\n\tans.clear(),ans.a[1][1]=0;\n\tint pre=1;\n\tfor (int i=1,x,y;i<=q;i++) x=read(),y=read(),h[x]=y,p[i]=x;\n\tsort(p+1,p+q+1); ll res=0;\n\twhile (q&&p[q]>=k) res+=h[p[q]],h[p[q]]=0,--q;\n\t/*\n\tfor (int i=1;i<=q;i++){\n\t\tp=pps[i].ff,w=pps[i].ss;\n\t\tif (p>=k){res+=w; continue;}\n\t\tans=ans*power(trans,p-n-pre);\n\t\tfor (int i=p-n;i=0) ans=ans*power(trans,p[1]-n-1),pre=p[1]-n;\n\tfor (int l=1,r=1;l<=q;l=r+1){\n\t\tfor (r=l;r<=q&&p[r]-n-pre<=0;++r) pre=p[r]; --r;\n\t\tfor (int i=max(1,p[l]-n);i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint c[15];\nint ma[305];\nint cnt = 0;\nconst long long inf = 1999999999999999999ll;\nstruct matrix{\n long long a[75][75];\n friend matrix operator * (const matrix &a, const matrix &b){\n matrix c;\n for(int i = 0; i < cnt; i++){\n for (int j = 0; j < cnt; j++){\n c.a[i][j] = inf;\n }\n }\n for(int i = 0; i < cnt; i++){\n for(int j = 0; j < cnt; j++){\n for(int k = 0; k < cnt; k++){\n c.a[i][k] = min(c.a[i][k],a.a[i][j]+b.a[j][k]);\n }\n }\n }\n return c;\n }\n matrix (){\n for(int i = 0; i < 75; i++){\n for(int j = 0; j < 75; j++){\n a[i][j] = inf;\n }\n a[i][i] = 0;\n }\n }\n};\n\nmatrix a;\nmatrix mul(int x){\n if(x == 0){\n return matrix();\n }\n matrix b = mul(x/2);\n b = b*b;\n if(x % 2 == 1){ \n b = b*a;\n }\n return b;\n}\n\npair p[75];\n\nint main()\n{\n int x,k;\n int n,q;\n scanf(\"%d%d%d%d\",&x,&k,&n,&q);\n for(int i = 0; i < k; i++){\n scanf(\"%d\",&c[i]);\n }\n memset(ma, -1, sizeof(ma));\n for (int i = 0; i < (1<>1 ] ] = c[j-1];\n }\n }\n else{\n a.a[ma[i]][ma[i>>1]] = 0;\n }\n }\n int now = 1;\n for(int i = 0; i < q; i++){\n scanf(\"%d%d\",&p[i].first,&p[i].second);\n }\n sort(p, p+q);\n matrix xx;\n long long ans = 0;\n for(int i = 0; i < q; i++){\n if(p[i].first >= n-x+1){\n ans += p[i].second;\n continue;\n }\n xx = xx*mul(p[i].first-now);\n now = p[i].first;\n for(int j = 1; j < (1<\n#include\n\nusing namespace std;\n\nlong long sz[(1 << 8)];\nlong long D[(1 << 8)][(1 << 8)];\nlong long MD[32][(1 << 8)][(1 << 8)];\nlong long c[10];\nlong long mw[(1 << 8)];\nlong long who[(1 << 8)];\nlong long who_n = 0;\nlong long now[(1 << 8)];\nlong long want[25 * 30+10];\nlong long made[(1 << 8)];\nlong long p[55], w[55];\n\nint main() {\n\tlong long x, k, n,q;\n\tlong long i, j,r,o;\n\n\tscanf(\"%lld%lld%lld%lld\", &x, &k, &n, &q);\n\tfor (i = 1; i <= k; i++)scanf(\"%lld\", &c[i]);\n\tfor (i = 1; i < (1 << k); i++)sz[i] = sz[i&(i - 1)] + 1;\n\tfor (i = 0; i < (1 << k); i++) {\n\t\tfor (j = 0; j < (1 << k); j++) {\n\t\t\tD[i][j] = -1;\n\t\t}\n\t}\n\tfor (i = 0; i < (1 << k); i++) {\n\t\tif (sz[i] != x)continue;\n\t\twho[who_n] = i;\n\t\tmw[i] = who_n;\n\t\twho_n++;\n\t\t\n\t\tlong long fo;\n\t\tfor (j = 0; j < k; j++) {\n\t\t\tif (i&(1< MD[i - 1][j][o] + MD[i - 1][o][r])\n\t\t\t\t\t\tMD[i][j][r] = MD[i - 1][j][o] + MD[i - 1][o][r];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tlong long want_n = 0;\n\tfor (i = 0; i < q; i++) {\n\t\tscanf(\"%lld%lld\", &p[i], &w[i]);\n\t\tfor (j = p[i] - k-1; j <= p[i] + 1; j++) {\n\t\t\tif(j>1)want[want_n++] = j;\n\t\t}\n\t}\n\twant[want_n++] = n-x+1;\n\tsort(want, want + want_n);\n\twant_n = unique(want, want + want_n) - want;\n\n\tlong long noww = 1;\n\tfor (i = 0; i < who_n; i++)now[i] = made[i] = -1e13;\n\tnow[mw[(1 << x) - 1]] = 0;\n\tfor (long long pp = 0; pp < want_n; pp++) {\n\t\tlong long next = want[pp];\n\t\tif (next > n - x + 1)break;\n\t\tif (next - noww == 1) {\n\t\t\tfor (i = 0; i < who_n; i++) {\n\t\t\t\tif (now[i] == -1e13)continue;\n\t\t\t\tlong long hb = who[i];\n\t\t\t\tlong long fp;\n\t\t\t\tfor (j = 0; j < k; j++) {\n\t\t\t\t\tif (hb&(1 << j)) {\n\t\t\t\t\t\tfp = j;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (fp > 0) {\n\t\t\t\t\tif(made[mw[hb / 2]]==-1e13|| made[mw[hb / 2]]>now[i])\n\t\t\t\t\t\tmade[mw[hb / 2]] = now[i];\n\t\t\t\t}\n\t\t\t\tfor (j = fp + 1; j <= k; j++) {\n\t\t\t\t\tif (hb&(1 << j))continue;\n\t\t\t\t\tlong long nb = hb - (1 << fp) + (1 << j);\n\t\t\t\t\tnb /= 2;\n\t\t\t\t\tlong long pls = c[j - fp];\n\t\t\t\t\tfor (r = 0; r < q; r++) {\n\t\t\t\t\t\tif (p[r] == noww + j)pls += w[r];\n\t\t\t\t\t}\n\t\t\t\t\tif (made[mw[nb]] == -1e13 || made[mw[nb]] > now[i] + pls)\n\t\t\t\t\t\tmade[mw[nb]] = now[i] + pls;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < who_n; i++) {\n\t\t\t\tnow[i] = made[i];\n\t\t\t\tmade[i] = -1e13;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tlong long delta = next - noww;\n\t\t\tfor (i = 0; i < 30; i++) {\n\t\t\t\tif ((delta&((long long)1 << i)) == 0)continue;\n\t\t\t\tfor (j = 0; j < who_n; j++) {\n\t\t\t\t\tif (now[j] == -1e13)continue;\n\t\t\t\t\tfor (r = 0; r < who_n; r++) {\n\t\t\t\t\t\tif (MD[i][j][r] == -1)continue;\n\t\t\t\t\t\tif (made[r] == -1e13 || made[r] > now[j] + MD[i][j][r])\n\t\t\t\t\t\t\tmade[r] = now[j] + MD[i][j][r];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (j = 0; j < who_n; j++) {\n\t\t\t\t\tnow[j] = made[j];\n\t\t\t\t\tmade[j] = -1e13;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tnoww = want[pp];\n\t}\n\tprintf(\"%lld\", now[0]);\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "57e681aac705d2388f6489d12472a8e2", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\ntypedef pair pii;\ntypedef long long ll;\n\nstruct mat {\n ll T[100][100];\n void O() {\n for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) T[i][j] = 1e18;\n }\n void I() {\n for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) T[i][j] = i == j;\n }\n};\n\nmat mult(mat a, mat b) {\n mat ret;\n ret.O();\n\n for(int i = 0; i < 100; i++) {\n for(int j = 0; j < 100; j++) {\n for(int k = 0; k < 100; k++) {\n ret.T[i][j] = min(ret.T[i][j], a.T[i][k] + b.T[k][j]);\n }\n }\n }\n\n return ret;\n}\nmat exp(mat &x, int n) {\n if(n == 1) return x;\n mat tmp = exp(x, n / 2);\n if(n % 2) return mult(mult(tmp, tmp), x);\n else return mult(tmp, tmp);\n}\n\nint X, K, N, Q;\nint C[10];\npii W[30];\nmap chk;\n\nvector Mask;\nint inv[256];\nmat P, Y, A;\n\nint main() {\n scanf(\"%d %d %d %d\", &X, &K, &N, &Q);\n\n for(int i = 1; i <= K; i++) {\n scanf(\"%d\", &C[i]);\n }\n\n for(int i = 0; i < Q; i++) {\n scanf(\"%d %d\", &W[i].first, &W[i].second);\n W[i].first--;\n\n chk[ W[i].first ] = W[i].second;\n }\n\n sort(W, W + Q);\n\n for(int i = 0; i < (1 << K); i++) {\n int cnt = 0;\n for(int j = 0; j < K; j++) if(i & (1 << j)) cnt++;\n\n if(cnt == X) Mask.push_back(i);\n }\n for(int i = 0; i < Mask.size(); i++) inv[ Mask[i] ] = i;\n\n A.O();\n A.T[ inv[ ((1 << X) - 1) ] ][0] = 0;\n P.O();\n\n for(int i = 0; i < Mask.size(); i++) {\n int mask = Mask[i];\n if(!(mask & 1)) {\n P.T[i][ inv[mask >> 1] ] = 0;\n continue;\n }\n\n for(int j = 1; j <= K; j++) {\n if(mask & (1 << j)) continue;\n\n int mask2 = mask ^ (1 << j);\n mask2 >>= 1;\n\n P.T[i][ inv[mask2] ] = C[j];\n }\n }\n\n int cur_len = X;\n\n //*\n for(int q = Q - 1; q >= 0; q--) {\n int len = N - W[q].first;\n\n if(len > cur_len) {\n A = mult(exp(P, len - cur_len), A);\n cur_len = len;\n }\n\n while(cur_len < N - W[q].first + K && cur_len < N) {\n int p = N - cur_len - 1;\n\n Y.O();\n\n for(int i = 0; i < Mask.size(); i++) {\n int mask = Mask[i];\n if(!(mask & 1)) {\n Y.T[i][ inv[mask >> 1] ] = 0;\n continue;\n }\n\n for(int j = 1; j <= K; j++) {\n if(mask & (1 << j)) continue;\n\n int mask2 = mask ^ (1 << j);\n mask2 >>= 1;\n\n Y.T[i][ inv[mask2] ] = C[j] + (chk.find(p + j) != chk.end()? chk[p + j] : 0);\n }\n }\n\n A = mult(Y, A);\n cur_len++;\n }\n }\n //*/\n\n if(N > cur_len) {\n A = mult(exp(P, N - cur_len), A);\n }\n\n printf(\"%lld\", A.T[ inv[(1 << X) - 1] ][0]);\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "7e62b929a3d5756a31d56599079fee62", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//\n// Created by Admin on 2/1/2018.\n//\n#include \nusing namespace std;\n#define ll long long\nll x, n, k, q;\nll c[10];\nmap mp, rmp, t;\nll f[110], pre[110];\nbool g[100000010];\nll tp = 0;\nstruct matrix {\n ll x[110][110];\n matrix() {\n memset(x, 0, sizeof(x));\n }\n matrix unit() {\n matrix res;\n for (int i = 0; i < tp; i++) res.x[i][i] = 1;\n return res;\n }\n matrix operator * (matrix A) {\n matrix res;\n for(int i = 0; i < tp; i++){\n for(int j = 0; j < tp; j++) res.x[i][j] = 1e18 + 1;\n }\n for (int i = 0; i < tp; i++) for (int k = 0; k < tp; k++) for (int j = 0; j < tp; j++) {\n res.x[i][j] = min(res.x[i][j], (x[i][k] + A.x[k][j]));\n }\n return res;\n }\n matrix operator ^ (long long k) {\n if (!k) return unit();\n matrix res, tmp;\n for(int i = 0; i < tp; i++) for (int j = 0; j < tp; j++) {\n res.x[i][j] = tmp.x[i][j] = x[i][j];\n }\n k--;\n while (k) {\n if (k & 1) res = res * tmp;\n tmp = tmp * tmp;\n k >>= 1;\n }\n return res;\n }\n} mt;\nvoid nextStep(ll mask){\n if(!(mask & (1 << 0))){\n mt.x[mp[mask]][mp[mask/2]] = -1;\n return;\n }\n for(int i = 1; i <= k; i++){\n if(mask & (1 << i)) continue;\n ll nwmask = mask | (1 << i);\n nwmask /= 2;\n mt.x[mp[mask]][mp[nwmask]] = c[i];\n// cout << mp[mask] << \" \" << mp[nwmask] << \" \" << c[i] << \"\\n\";\n }\n}\nint main(){\n //freopen(\"../DP/917C.in\",\"r\",stdin);\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cin >> x >> k >> n >> q;\n for(int i = 1; i <= k; i++) cin >> c[i];\n ll cnt = 0;\n for(int i = 0; i < (1 << k); i++){\n if(__builtin_popcount(i) == x){\n mp[i] = cnt;\n rmp[cnt] = i;\n cnt++;\n }\n }\n ll ans = 1e18;\n for(int i = 0; i < cnt; i++){\n nextStep(rmp[i]);\n }\n tp = cnt;\n for(int i = 0; i < cnt; i++){\n for(int j = 0; j < cnt; j++){\n if(mt.x[i][j] == 0) mt.x[i][j] = 1e18;\n else if(mt.x[i][j] == -1) mt.x[i][j] = 0;\n }\n }\n for(int i = 0; i < cnt; i++){\n if(i != 0) f[i] = 1e18;\n }\n for(int i = 1; i <= q; i++){\n int u, w; cin >> u >> w;\n t[u] = w;\n for(int j = max(0LL, u - k + 1); j <= u; j++) g[j] = 1;\n }\n int tmp = 0;\n for(int i = n - k + 1; i <= n; i++) g[i] = 1;\n for(int i = 2; i <= n - x + 1; i++){\n if(g[i] == 0){\n tmp++;\n } else{\n if(tmp != 0){\n matrix temp = mt ^ tmp;\n tmp = 0;\n for(int j = 0; j < cnt; j++){\n pre[j] = 1e18;\n for(int z = 0; z < cnt; z++){\n pre[j] = min(pre[j], f[z] + temp.x[z][j]);\n }\n }\n for(int j = 0; j < cnt; j++){\n f[j] = pre[j];\n }\n }\n for(int j = 0; j < cnt; j++) pre[j] = 1e18;\n for(int j = 0; j < cnt; j++){\n ll mask = rmp[j];\n if((mask & (1 << 0)) == 0){\n pre[mp[mask / 2]] = min(pre[mp[mask/2]], f[j]);\n continue;\n }\n for(int z = 1; z <= k; z++){\n if(mask & (1 << z)) continue;\n ll nwmask = mask | (1 << z);\n ll nwpo = i + z - 1;\n nwmask /= 2;\n pre[mp[nwmask]] = min(pre[mp[nwmask]], f[j] + c[z] + t[nwpo]);\n }\n }\n for(int j = 0; j < cnt; j++) f[j] = pre[j];\n if(i >= n - k + 1){\n int mask = 0;\n for(int j = n - x + 1; j <= n; j++){\n int diff = j - i;\n mask = mask | (1 << diff);\n }\n ans = min(ans, f[mp[mask]]);\n }\n }\n }\n if(1 >= n - k + 1){\n int mask = 0;\n for(int j = n - x + 1; j <= n; j++){\n int diff = j - 1;\n mask = mask | (1 << diff);\n }\n ans = min(ans, f[mp[mask]]);\n }\n cout << ans;\n// cout << f[0] << \" \" << f[1] << \" \" << f[2] << \"\\n\";\n}", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "9fb0e5955655c797fe07f84b6d276841", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing std::vector;\nusing std::string;\nusing std::map;\nusing std::set;\nusing std::min;\nusing std::max;\n\ntypedef long long ll;\ntypedef double dd;\n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing std::vector;\nusing std::string;\nusing std::map;\nusing std::set;\nusing std::min;\nusing std::max;\n\ntypedef long long ll;\ntypedef double dd;\n\n\nconstexpr ll magic_mod = 1000000007;\nconstexpr ll big = 1ll << 60;\n\n\ntypedef ll _et;\nstruct Matrix\n{\n ll size;\n vector< _et > f;\n Matrix(ll size, _et start_value = big) :size(size), f(size * size,start_value) {}\n Matrix(const Matrix& o) { *this = o;};\n static vector cache;\n inline _et& m(ll a,ll b)\n {\n return f[a*size + b];\n }\n inline const _et& m(ll a,ll b) const\n {\n return f[a*size + b];\n }\n void print()\n {\n for(ll i =0; isize = o.size;\n this->f = o.f;\n }\n Matrix operator*(const Matrix& o) const\n {\n Matrix res(size);\n for(ll i =0; i Matrix::cache;\n\n\nvoid Matrix::fill_cache()\n{\n cache = vector (xxxx, Matrix(size));\n Matrix res(*this);\n for(ll i = 1; i < xxxx; ++i)\n {\n cache[i] = res;\n res = res * res;\n }\n}\n\n\nvector standings;\nvector jw;\nll sn;\nmap magic_stones;\n\ninline ll btc(ll x)\n{\n ll res = 0;\n while(x)\n {\n if (x % 2) ++res;\n x /=2;\n }\n return res;\n}\n\n\nll get_index(ll standing)\n{\n for(ll i = 0; i> 1));\n ll aw = 0;\n if( pos >= 0 && j!=0)\n {\n ll pos2 = pos;\n ll sm2 = smallest >> 1;\n while ( sm2)\n {\n ++pos2;\n sm2 /= 2;\n }\n pos2 +=j;\n if (magic_stones.find(pos2) != magic_stones.end())\n {\n aw = magic_stones[pos2];\n }\n }\n x = min(x,(ll)jw[j] + aw);\n }\n }\n }\n}\n\nvector mul(const vector& x, const Matrix& m)\n{\n vector res(sn, big);\n for(ll i = 0; i& res)\n{\n //std::cout << pos << \" res \" << res[0] << std::endl;\n // for(auto x : res)\n // {\n // std::cout << (x != big ? x : -1)<< \" \";\n // }\n // std::cout << std::endl;\n \n}\n \nint main()\n{\n ll x,k,n,q;\n scanf(\"%lld%lld%lld%lld\",&x,&k,&n,&q);\n jw = vector(k +1, 0);\n for (ll i = 1; i <=k; ++i)\n scanf(\"%lld\",&(jw[i]) );\n for (ll i = 0; i res(sn, big);\n res[0] = 0;\n ll cp = 0;\n ll des_pos = n - x;\n print_state(cp,res);\n for(auto ms : magic_stones)\n {\n if (ms.first == cp) continue;\n if (ms.first - k > cp)\n {\n res = mul(res, jumpm.get_pow(ms.first - k -cp));\n cp = ms.first - k;\n print_state(cp,res);\n }\n for(;cp < ms.first && cp < des_pos;)\n {\n Matrix msm(sn);\n CalcJmpMatrix(msm, cp);\n res = mul(res,msm);\n ++cp; \n print_state(cp,res);\n }\n }\n if (cp < des_pos)\n {\n res = mul(res, jumpm.get_pow(des_pos -cp));\n cp = des_pos;\n print_state(cp,res);\n }\n print_state(cp,res);\n std::cout << res[0] << std::endl;\n}\n\n\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "3a26ff95909f363614de304cf9a72428", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\ntypedef vector> mat;\nint x, k, P, q, B, curx, n;\nll c[10], w;\nmap mp;\nvector dp, dpn;\nvector bitset;\nmat a, o, p;\nvoid pow2(mat &a) {\n\tfor (int i : bitset)\n\t\tfor (int j : bitset)\n\t\t\tp[i][j] = 1e18;\n\tfor (int k : bitset) {\n\t\tfor (int i : bitset)\n\t\t\tfor (int j : bitset)\n\t\t\t\tp[i][j] = min(p[i][j], a[i][k] + a[k][j]);\n\t}\n\tswap(a, p);\n}\nvoid operator *=(vector &dp, mat &a) {\n\tfor (int i : bitset)\n\t\tdpn[i] = 1e18;\n\tfor (int i : bitset) {\n\t\tfor (int j : bitset) {\n\t\t\tdpn[j] = min(dpn[j], dp[i] + a[i][j]);\n\t\t}\n\t}\n\tswap(dp, dpn);\n}\nvoid jump(ll to) {\n\ta = o;\n\tll jump = to - curx;\n\tfor (;jump;pow2(a), jump >>= 1) {\n\t\tif (jump & 1) dp *= a;\n\t}\n\tcurx = to;\n}\nint main() {\n\tios::sync_with_stdio(0), cin.tie(0);\n\tcin >> x >> k >> n >> q;\n\tB = 1 << k;\n\tdp.resize(B, 1e18);\n\tdpn.resize(B, 1e18);\n\to.resize(B, vector(B, 1e18));\n\tp.resize(B, vector(B, 1e18));\n\tfor (int b = 1;b < B;b++)\n\t\tif (__builtin_popcount(b) == x) bitset.push_back(b);\n\tdp[bitset[0]] = 0;\n\tfor (int i = 1;i <= k;i++) cin >> c[i];\n\tfor (int cur : bitset) {\n\t\tint next = cur >> 1;\n\t\tif (cur & 1) {\n\t\t\tfor (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {\n\t\t\t\to[cur][next | b] = c[i];\n\t\t\t}\n\t\t}\n\t\telse o[cur][next] = 0;\n\t}\n\tfor (int i = 1;i <= q;i++) {\n\t\tcin >> P >> w;\n\t\tif (w) mp[P] = w;\n\t}\n\tmp[n + k - x + 1] = 0;\n\tcurx = k;\n\tfor (auto it = mp.begin();curx < n + k - x;) {\n\t\tif (it->first <= curx - k) {\n\t\t\tit++; continue;\n\t\t}\n\t\tif (it->first <= curx + 1) {\n\t\t\tfor (int i : bitset)\n\t\t\t\tdpn[i] = 1e18;\n\t\t\tcurx++;\n\t\t\tfor (int cur : bitset) {\n\t\t\t\tint next = cur >> 1;\n\t\t\t\tif (cur & 1) {\n\t\t\t\t\tfor (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {\n\t\t\t\t\t\tdpn[next | b] = min(dpn[next | b], dp[cur] + c[i] + (mp.count(curx - k + i) ? mp[curx - k + i] : 0));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse dpn[next] = min(dpn[next], dp[cur]);\n\t\t\t}\n\t\t\tswap(dp, dpn);\n\t\t}\n\t\telse jump(it->first - 1);\n\t}\n\tprintf(\"%lld\", dp[bitset[0]]);\n}", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "d8a68e9db705c72e5324773dc205a439", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\n#define debug(...) printf(__VA_ARGS__), fflush(stdout);\n\ntypedef long long LL;\ntypedef pair pii;\n\nconst int MAX_STATE = 71;\nconst LL inf = 1e18;\n\npii p[30];\n\nint c[10], f[1 << 8];\n\nint x, k, n, q, cnt;\n\nstruct matrix {\n\n\tLL A[MAX_STATE][MAX_STATE];\n\n\tmatrix() {\n\t\tfor (int i = 1; i < MAX_STATE; i++) {\n\t\t\tfor (int j = 1; j < MAX_STATE; j++)\n\t\t\t\tA[i][j] = inf;\n\t\t\tA[i][i] = 0;\n\t\t}\n\t}\n\n\tmatrix operator * (matrix B) {\n\t\tmatrix r;\n\t\tfor (int i = 1; i <= cnt; i++)\n\t\t\tr.A[i][i] = inf;\n\t\tfor (int i = 1; i <= cnt; i++)\n\t\t\tfor (int j = 1; j <= cnt; j++)\n\t\t\t\tfor (int k = 1; k <= cnt; k++)\n\t\t\t\t\tr.A[i][j] = min(r.A[i][j], A[i][k] + B.A[k][j]);\n\t\treturn r;\n\t}\n\n\tmatrix operator ^ (int y) {\n\t\tmatrix x, r;\n\t\tfor (int i = 1; i <= cnt; i++)\n\t\t\tfor (int j = 1; j <= cnt; j++)\n\t\t\t\tx.A[i][j] = A[i][j];\n\t\tif (!y) return matrix();\n\t\twhile (y) {\n\t\t\tif (y & 1) r = r * x;\n\t\t\tx = x * x; y >>= 1;\n\t\t}\n\t\treturn r;\n\t}\n};\n\nint calc(int x) {\n\tint sum = 0;\n\twhile (x) {\n\t\tif (x & 1) sum++;\n\t\tx >>= 1;\n\t}\n\treturn sum;\n}\n\nint main() {\n#ifdef xunzhen\n\tfreopen(\"E.in\", \"r\", stdin);\n\tfreopen(\"E.out\", \"w\", stdout);\n#endif\n\n\tcin >> x >> k >> n >> q;\n\tfor (int i = 1; i <= k; i++)\n\t\tscanf(\"%d\", &c[i]);\n\tfor (int i = 1; i <= q; i++)\n\t\tscanf(\"%d%d\", &p[i].first, &p[i].second);\n\n\tfor (int i = 0; i < (1 << k); i++)\n\t\tif (calc(i) == x) f[i] = ++cnt;\n\n\tmatrix R;\n\tfor (int i = 1; i <= cnt; i++)\n\t\tR.A[i][i] = inf;\n\n\tfor (int i = 1; i < (1 << k); i++) {\n\t\tif (!f[i]) continue;\n\t\tif (i & 1) {\n\t\t\tfor (int j = 1; j <= k; j++) {\n\t\t\t\tif ((1 << j) & i) continue;\n\t\t\t\tR.A[f[i]][f[((1 << j) | i) >> 1]] = c[j];\n\t\t\t}\n\t\t}else R.A[f[i]][f[i >> 1]] = 0;\n\t}\n\n\tsort(p + 1, p + q + 1);\n\n\tmatrix ans;\n\tLL sum = 0; int now = 1;\n\tfor (int i = 1; i <= q; i++) {\n\t\tif (p[i].first > n - x) {\n\t\t\tsum += p[i].second;\n\t\t\tcontinue;\n\t\t}\n\t\tans = ans * (R ^ (p[i].first - now));\n\t\tnow = p[i].first;\n\t\tfor (int j = 1; j < (1 << k); j += 2)\n\t\t\tif (f[j])\n\t\t\t\tfor (int k = 1; k <= cnt; k++)\n\t\t\t\t\tans.A[k][f[j]] += p[i].second;\n\t}\n\tans = ans * (R ^ (n - x + 1 - now));\n\n\tcout << ans.A[1][1] + sum << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "9af87c021d379a888f1c4a1e8aba82dc", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\nusing namespace std;\n\n#define pq priority_queue\n#define MZ 1000000007\n\nint n,kk,xx;\nint cnt=0;\nint n2b[75],b2n[265];\nint bs[10];\nlong long dis[10];\nint p[30];\nlong long dp[2][75];\nmap mp;\nstruct Mat\n{\n long long num[75][75];\n friend Mat operator *(Mat x1,Mat x2)\n {\n Mat ans;\n for(int i=1;i<=cnt;i++)\n for(int j=1;j<=cnt;j++)\n {\n ans.num[i][j]=2LL*MZ*MZ;\n for(int k=1;k<=cnt;k++)\n ans.num[i][j]=min(x1.num[i][k]+x2.num[k][j],ans.num[i][j]);\n }\n return ans;\n }\n}A;\nqueue q;\n\nMat pows(Mat x,int y)\n{\n Mat ans;\n for(int i=1;i<=cnt;i++)\n for(int j=1;j<=cnt;j++)\n {\n if(i==j)\n ans.num[i][j]=0;\n else\n ans.num[i][j]=2LL*MZ*MZ;\n }\n while(y)\n {\n if(y%2)\n ans=ans*x;\n x=x*x;\n y>>=1;\n }\n return ans;\n}\nvoid init(int f,int x,int s)\n{\n if(f==kk)\n {\n if(x==xx)\n {\n cnt++;\n b2n[s]=cnt;\n n2b[cnt]=s;\n }\n return;\n }\n init(f+1,x+1,s+bs[f]);\n init(f+1,x,s);\n}\nvoid solve1(Mat x)\n{\n for(int i=1;i<=cnt;i++)\n dp[1][i]=2LL*MZ*MZ;\n for(int i=1;i<=cnt;i++)\n for(int j=1;j<=cnt;j++)\n dp[1][j]=min(dp[0][i]+x.num[i][j],dp[1][j]);\n for(int i=1;i<=cnt;i++)\n dp[0][i]=dp[1][i];\n}\nvoid solve2(int y)\n{\n for(int i=1;i<=cnt;i++)\n dp[1][i]=2LL*MZ*MZ;\n for(int i=1;i<=cnt;i++)\n {\n if(n2b[i]%2)\n {\n for(int j=1;j<=kk;j++)\n {\n if((n2b[i]&bs[j])==0)\n dp[1][b2n[(n2b[i]+bs[j])/2]]=min(dp[1][b2n[(n2b[i]+bs[j])/2]],dp[0][i]+A.num[i][b2n[(n2b[i]+bs[j])/2]]+mp[y+j]);\n }\n }\n else\n dp[1][b2n[n2b[i]/2]]=min(dp[1][b2n[n2b[i]/2]],dp[0][i]);\n }\n for(int i=1;i<=cnt;i++)\n dp[0][i]=dp[1][i];\n}\n\nint main()\n{\n int qq;\n long long w;\n bs[0]=1;\n for(int i=1;i<=8;i++)\n bs[i]=bs[i-1]*2;\n scanf(\"%d%d%d%d\",&xx,&kk,&n,&qq);\n init(0,0,0);\n for(int i=1;i<=kk;i++)\n scanf(\"%lld\",&dis[i]);\n for(int i=1;i<=cnt;i++)\n {\n for(int j=1;j<=cnt;j++)\n A.num[i][j]=2LL*MZ*MZ;\n if(n2b[i]%2)\n {\n for(int j=1;j<=kk;j++)\n {\n if((n2b[i]&bs[j])==0)\n A.num[i][b2n[(n2b[i]+bs[j])/2]]=dis[j];\n }\n }\n else\n A.num[i][b2n[n2b[i]/2]]=0;\n }\n for(int i=0;ip[qq])\n p[i]=p[qq];\n if(now+kk\n#include\n#include\n#include\n\nusing namespace std;\n\nint read()\n{\n\tint x = 0, f = 1; char c = getchar();\n\twhile(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }\n\twhile(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }\n\treturn x * f;\n}\n\ntypedef long long LL;\nconst LL inf = 1e18;\nint cnt, c[120], f[(1 << 8) + 100];\npair q[300];\n\nstruct Mat\n{\n\tLL a[100][100];\n\n\tMat operator * (const Mat &A) const\n\t{\n\t\tMat tmp;\n\t\tfor(int i = 1; i <= cnt; ++i)\n\t\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\t\ttmp.a[i][j] = inf;\n\t\tfor(int i = 1; i <= cnt; ++i)\n\t\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\t\tfor(int k = 1; k <= cnt; ++k)\n\t\t\t\t\ttmp.a[i][j] = min(tmp.a[i][j], a[i][k] + A.a[k][j]);\n\t\treturn tmp;\n\t}\n\n\tMat operator ^ (int p)\n\t{\n//\t\tint p = P;\n//\t\tcout << '*' << p << endl;\n\t\tMat x, r;\n\t\tfor(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) if(i != j) r.a[i][j] = inf; else r.a[i][i] = 0;\n\t\tfor(int i = 1; i <= cnt; ++i)\n\t\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\t\tx.a[i][j] = a[i][j];\n//\t\tcout << '*' << p << endl;\n\t//\tp = 1;\n\t\twhile(p)\n\t\t{\n\t\t\tif(p & 1) r = x * r;\n\t\t\tx = x * x; p >>= 1;\n\t\t}\n\t\treturn r;\n\t}\n};\n\n\t/*Mat gao(Mat c, int p)\n\t{\n\t\tcout << '*' << p << endl;\n\t\tMat x, r;\n\t\tfor(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) if(i != j) r.a[i][j] = inf;\n\t\tfor(int i = 1; i <= cnt; ++i)\n\t\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\t\tx.a[i][j] = c.a[i][j];\n//\t\tcout << '*' << p << endl;\n\t//\tp = 1;\n\t\twhile(p)\n\t\t{\n\t\t\tif(p & 1) r = x * r;\n\t\t\tx = x * x; p >>= 1;\n\t\t}\n\t\treturn r;\n\t}*/\nint main()\n{\n#ifndef ONLINE_JUDGE\n\tfreopen(\"a.in\", \"r\", stdin);\n\tfreopen(\"a.out\", \"w\", stdout);\n#endif\n\n\tint x = read(), k = read(), n = read(), Q = read();\n\tfor(int i = 1; i <= k; ++i) c[i] = read();\n\tfor(int i = 1; i <= Q; ++i) q[i].first = read(), q[i].second = read();\n\tcnt = 0;\n\tfor(int i = 1; i < (1 << k); ++i) \n\t\tif(__builtin_popcount(i) == x) f[i] = ++cnt;\n\tMat tmp;\n\tfor(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) tmp.a[i][j] = inf;\n\tfor(int i = 1; i < (1 << k); ++i)\n\t{\n\t\tif(!f[i]) continue;\n\t\tif(i & 1)\n\t\t\tfor(int j = 1; j <= k; ++j)\n\t\t\t{\n\t\t\t\tif((1 << j) & i) continue;\n\t\t\t\ttmp.a[f[((1 << j) | i) >> 1]][f[i]] = c[j];\n\t\t\t}\n\t\telse tmp.a[f[i >> 1]][f[i]] = 0;\n\t}\n\n\tsort(q + 1, q + Q + 1);\n\tMat ans; for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) ans.a[i][j] = inf;\n\tans.a[1][1] = 0;\n\tLL sum = 0; int now = 1;\n\tfor(int i = 1; i <= Q; ++i)\n\t{\n\t\tif(q[i].first > n - x) { sum += q[i].second; continue; }\n\t\tans = (tmp ^ (q[i].first - now)) * ans;\n\t\tnow = q[i].first;\n\t\tfor(int j = 1; j < (1 << k); j += 2)\n\t\t{\n\t\t\tif(f[j]) \n\t\t\t\tans.a[f[j]][1] += q[i].second;\n\t\t}\n//\t\tans = (tmp ^ (q[i + 1].first - now)) * ans;\n//\t\tcout << endl;\n//\t\tfor(int i = 1; i <= cnt; ++i) cout << ans.a[i][1] << endl;\n\t}\n\tans = (tmp ^ (n - x + 1 - now)) * ans;\n/*\t\n\tfor(int i = 1; i <= cnt; ++i)\n\t{\n\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\tcout << tmp.a[i][j] << ' ';\n\t\tcout << endl;\n\t}\n\t*/\n//\tans = tmp * ans;\n\t\n\t/*\n\tfor(int i = 1; i <= cnt; ++i)\n\t{\n\t\tfor(int j = 1; j <= cnt; ++j)\n\t\t\tcout << ans.a[i][j] << ' ';\n\t\tcout << endl;\n\t}\t*/\n\tcout << ans.a[1][1] + sum << endl;\n\n\treturn 0;\n}//hello", "lang_cluster": "C++", "tags": ["matrices", "dp", "combinatorics"], "code_uid": "d3a8618b33b00ccdaa0b3f6af7f55e4d", "src_uid": "e3dd409ceeba2a21890d35ceab9607eb", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//teja349\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//setbase - cout << setbase (16); cout << 100 << endl; Prints 64\n//setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77\n//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx\n//cout.precision(x) cout<=b;i--)\n#define pb push_back\n#define mp make_pair\n#define vi vector< int >\n#define vl vector< ll >\n#define ss second\n#define ff first\n#define ll long long\n#define pii pair< int,int >\n#define pll pair< ll,ll >\n#define sz(a) a.size()\n#define inf (1000*1000*1000+5)\n#define all(a) a.begin(),a.end()\n#define tri pair\n#define vii vector\n#define vll vector\n#define viii vector\n#define mod (1000*1000*1000+7)\n#define pqueue priority_queue< int >\n#define pdqueue priority_queue< int,vi ,greater< int > >\n\n//std::ios::sync_with_stdio(false); \nll x,k,n,q,iinf,counter=0,c[100];\nmap inv,mapi,special;\nll dp[75][75],res[75],res1[75],reserv[75][75],haha[75][75];\nll hshit(ll i){\n\tll jj=i,cnt=0;\n\twhile(i>0){\n\t\tif(i%2)\n\t\t\tcnt++;\n\t\ti/=2;\n\t}\n\ti=jj;\n\tif(cnt!=x){\n\t\treturn 0;\n\t}\n\tmapi[i]=counter;\n\tinv[counter]=i;\n\tcounter++;\n\treturn 0;\n}\nll updatedp(){\n\tll i,j,k;\n\trep(i,counter){\n\t\trep(j,counter){\n\t\t\thaha[i][j]=iinf;\n\t\t}\n\t}\n\trep(i,counter){\n\t\trep(j,counter){\n\t\t\trep(k,counter){\n\t\t\t\thaha[i][k]=min(haha[i][k],dp[i][j]+dp[j][k]);\n\t\t\t}\n\t\t}\n\t}\n\trep(i,counter){\n\t\trep(j,counter){\n\t\t\tdp[i][j]=haha[i][j];\n\t\t}\n\t}\n} \nll updateres(){\n\tll i,j;\n\trep(i,counter){\n\t\tres1[i]=iinf;\n\t}\n\trep(i,counter){\n\t\trep(j,counter){\n\t\t\tres1[j]=min(res1[j],res[i]+dp[i][j]);\n\t\t}\n\t}\n\trep(i,counter){\n\t\tres[i]=res1[i];\n\t}\n\treturn 0;\n}\nll updatefrom(ll s,ll e){\n\n\te=min(e,n);\n\tif(e<=s){\n\t\treturn 0;\n\t}\n\t//cout<0){\n\t\tif(len%2){\n\t\t\t//cout<<\"Dasd\"<>x>>k>>n>>q;\n n-=x;\n \tn++;\n \tll i,j;\n f(i,1,k+1){\n \tcin>>c[i];\n \t//cout<>sp[i]>>wt[i];\n \t\tspecial[sp[i]]=wt[i];\n \t\tvec.pb(mp(sp[i],wt[i]));\n \t}\n \tsort(all(vec));\n \trep(i,q){\n \t\tsp[i]=vec[i].ff;\n \t\twt[i]=vec[i].ss;\n \t}\n \tll previ=1;\n \t//return 0;\n \t//updatefrom(1,n);\n \t//manual(1,4);\n \t//cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#ifdef LOCAL\n\t#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n\t#define eprintf(...) 42\n#endif\n\ntypedef long long ll;\n#define X first\n#define Y second\n#define mp make_pair\n\nconst int N = 20;\nint n, k;\ndouble p[N];\ndouble dp[(1 << N) + 7];\ndouble ans[N];\n\nint main()\n{\n#ifdef LOCAL\n\tfreopen (\"input.txt\", \"r\", stdin);\n\tfreopen (\"output.txt\", \"w\", stdout);\n#endif\n\n\tscanf(\"%d%d\", &n, &k);\n\tfor (int i = 0; i < n; i++)\n\t\tscanf(\"%lf\", &p[i]);\n\tint cnt = 0;\n\tfor (int i = 0; i < n; i++)\n\t\tif (p[i] > 0.000001)\n\t\t\tcnt++;\n\tif (cnt <= k)\n\t{\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif (p[i] > 0.000001)\n\t\t\t\tprintf(\"1 \");\n\t\t\telse\n\t\t\t\tprintf(\"0 \");\n\t\tprintf(\"\\n\");\n\t\treturn 0;\n\t}\n\tdp[0] = 1;\n\tfor (int mask = 0; mask < (1 << n); mask++)\n\t{\n\t\tdouble sum = 0;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif ((mask >> i) & 1)\n\t\t\t\tsum += p[i];\n\t\tif (sum >= 0.99999) continue;\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif ((mask >> i) & 1) continue;\n\t\t\tdp[mask | (1 << i)] += dp[mask] * p[i] / (1 - sum);\n\t\t}\n\t}\n\tfor (int mask = 0; mask < (1 << n); mask++)\n\t{\n\t\tcnt = 0;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif ((mask >> i) & 1)\n\t\t\t\tcnt++;\n\t\tif (cnt != k) continue;\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tif ((mask >> i) & 1)\n\t\t\t\tans[i] += dp[mask];\n\t}\n\tfor (int i = 0; i < n; i++)\n\t\tprintf(\"%.13lf \", ans[i]);\n\tprintf(\"\\n\");\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "f0f874d3b959f94c7c74faf914114ff9", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef vector vi;\ntypedef vector vvi;\n\n#define forn(i, n) for (int i = 0; i < (int) n; ++i)\n#define ford(i, n) for (int i = ((int) n) - 1; i >= 0; --i)\n#define sz(c) ((int) (c).size())\n#define mp(x, y) make_pair(x, y)\n#define prev prev___________________\n#define hash hash___________________\n#define pb push_back\n\n#define FILE_NAME \"\"\n\nvoid solve (int n)\n{\n int k;\n cin >> k;\n\n typedef double T;\n vector p(n);\n for (int i = 0; i < n; i++)\n cin >> p[i];\n\n T dp[2][1 << n];\n memset(dp, 0, sizeof dp);\n\n vector isgood(n);\n vi good;\n for (int i = 0; i < n; i++)\n if (p[i] != 0)\n {\n isgood[i] = 1;\n good.pb(i);\n }\n\n dp[0][0] = 1;\n int c = 1;\n\n vector sum(1 << n);\n sum[0] = 1.0;\n for (int j = 1; j < (1 << n); j++)\n {\n int x = __builtin_ctz(j);\n assert((j >> x) & 1);\n sum[j] = sum[j ^ (1 << x)] - p[x];\n }\n\n for (int i = 0; i < min(sz(good), k); i++)\n {\n memset(dp[c], 0, sizeof dp[c]);\n\n for (int j = 0; j < (1 << n); j++)\n {\n if (__builtin_popcount(j) != i)\n continue;\n\n for (int next : good)\n if (!((j >> next) & 1))\n {\n dp[c][j ^ (1 << next)] += dp[c ^ 1][j] * p[next] / sum[j];\n }\n }\n\n c ^= 1;\n }\n\n c ^= 1;\n for (int i = 0; i < n; i++)\n {\n if (!isgood[i])\n printf(\"%.12f \", (double)0);\n else\n {\n T ans = 0;\n for (int j = 0; j < (1 << n); j++)\n if (((j >> i) & 1))\n ans += dp[c][j];\n printf(\"%.12f \", (double)ans);\n }\n puts(\"\");\n }\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen(FILE_NAME \".in\", \"r\", stdin);\n\tfreopen(FILE_NAME \".out\", \"w\", stdout);\n#endif\n\n\tint n;\n\twhile (cin >> n)\n\t\tsolve(n);\n\n#ifdef LOCAL\n\tcerr << \"Time: \" << (double) clock() / CLOCKS_PER_SEC << endl;\n#endif\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "ae2c8ad0928c88ca59fd8d5268a3abfe", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#pragma comment(linker, \"/STACK:256000000\")\n\n#include \n#include \n#include \n#include \n#include \n#include \n#undef NDEBUG\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define pbk pop_back\n#define mt make_tuple\n#define sz(s) ((int) (s).size())\n#define all(x) (x).begin(), (x).end()\n#define next hunext\n#define rank hurank\n#define prev huprev\n#define link hulink\n#define hash huhash\n#define y0 yy0\n#define y1 yy1\n#ifdef LOCAL42\n#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n#define eprintf(...) 42\n#define cerr if (false) cerr\n#endif\n\ntypedef long long ll;\ntypedef long long llong;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\ntypedef unsigned long long ullong;\ntypedef vector vi;\ntypedef pair pii;\ntypedef long double ld;\ntypedef long double ldouble;\n\nconst int inf = 1e9;\nconst double pi = 4 * atan(1.0);\nconst double eps = 1e-9;\n\nconst int N = 20;\n\ndouble _P[N];\ndouble P[N];\n\ndouble D[1 << N];\ndouble sum[1 << N];\n\ndouble Q[N];\n\nint ind[N];\n\nint main() {\n#define TASK \"A\"\n#ifdef LOCAL42\n\tfreopen(TASK \".in\", \"r\", stdin);\n\tfreopen(TASK \".out\", \"w\", stdout);\n#else\n\n#endif\n\n\tint _n, k;\n\tscanf(\"%d %d\", &_n, &k);\n\n\tint n = 0;\n\n\tfor (int i = 0; i < _n; i++) {\n\t\tscanf(\"%lf\", &_P[i]);\n\t\tif (_P[i] > eps)\n\t\t\tP[n] = _P[i], ind[i] = n, n++;\n\t\telse\n\t\t\tind[i] = -1;\n\n\t}\n\n\tk = min(k, n);\n\n\n\n\tD[0] = 1;\n\tsum[0] = 0;\n\n\tfor (int i = 1; i < (1 << n); i++) {\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tif ((i >> j) & 1) {\n\t\t\t\tsum[i] = sum[i ^ (1 << j)] + P[j];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (int msk = 0; msk < (1 << n); msk++) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif ((msk >> i) & 1) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tassert(P[i] > eps);\n\t\t\tD[msk ^ (1 << i)] += D[msk] * (P[i]) / (1 - sum[msk]);\n\t\t}\n\t}\n\n\tfor (int msk = 0; msk < (1 << n); msk++) {\n\t\tint cnt = 0;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif ((msk >> i) & 1) {\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t}\n\t\tif (cnt == k) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tif ((msk >> i) & 1)\n\t\t\t\t\tQ[i] += D[msk];\n\t\t\t}\n\t\t}\n\t}\n\n\n\tfor (int i = 0; i < _n; i++) {\n\t\tif (ind[i] == -1)\n\t\t\tprintf(\"0 \");\n\t\telse\n\t\t\tprintf(\"%.10lf \", Q[ind[i]]);\n\t}\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "71577897ae7af40171fb793ae1ace695", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\n#define pb push_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define sz(a) ((int) (a).size())\n#define int64 long long\n#define ldb long double\nconst ldb pi = acos(-1.0);\n\nconst int N = 20;\nconst double eps = 1e-10;\nint n, k;\ndouble a[N], p[1 << N], res[N];\n\nint main() {\n\t// assert(freopen(\"input.txt\", \"r\", stdin));\n\t// assert(freopen(\"output.txt\", \"w\", stdout));\n\tscanf(\"%d%d\", &n, &k);\n\tint cnt = 0;\n\tfor (int i = 0; i < n; ++i) {\n\t\tscanf(\"%lf\", &a[i]);\n\t\tif (a[i] < eps) ++cnt;\n\t}\n\tk = min(n - cnt, k);\n\tp[0] = 1;\n\tfor (int mask = 1; mask < (1 << n); ++mask) {\n\t\tdouble suma = 0;\n\t\tbool ok = true;\n\t\tint cnt = 0;\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tif (mask & (1 << i)) {\n\t\t\t\tsuma += a[i];\n\t\t\t\tif (a[i] < eps) ok = false;\n\t\t\t\t++cnt;\n\t\t\t}\n\t\tif (!ok) {\n\t\t\tp[mask] = 0;\n\t\t\tcontinue;\n\t\t} else {\n\t\t\tdouble sump = 0;\n\t\t\tfor (int i = 0; i < n; ++i)\n\t\t\t\tif (mask & (1 << i))\n\t\t\t\t\tsump += a[i] / (1 - suma + a[i]) * p[mask ^ (1 << i)];\n\t\t\tp[mask] = sump;\n\t\t}\n\t\tif (cnt == k)\n\t\t\tfor (int i = 0; i < n; ++i)\n\t\t\t\tif (mask & (1 << i))\n\t\t\t\t\tres[i] += p[mask];\n\t}\n\tfor (int i = 0; i < n; ++i)\n\t\tprintf(\"%.10f%c\", res[i], \" \\n\"[i == n - 1]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "e29b34a596bb8503c97fb18e87f4ce1f", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define RFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)\n#define FILL(A,val) memset(A,val,sizeof(A))\n#define MP make_pair\n#define PB push_back\n#define ALL(A) A.begin(),A.end()\n#define SZ(A) (int)A.size()\n\ntypedef long long Int;\ntypedef unsigned long long UInt;\ntypedef pair PII;\ntypedef vector VI;\n\nconst int INF = 1000000000;\nconst int MAX = 1000000;\nconst int ST = 1000000007;\n\ndouble dp[1 << 20];\n\nint cnt[1 << 20];\n\ndouble res[22];\n\ndouble p[22];\n\nint main()\n{\n //freopen(\"in.txt\" , \"r\" , stdin);\n\n FOR(i,1,1 << 20)\n {\n cnt[i] = cnt[i / 2] + i % 2;\n }\n\n int n , k;\n cin >> n >> k;\n FOR(i,0,n)\n {\n cin >> p[i];\n }\n\n\n dp[0] = 1;\n\n FOR(mask , 0 , 1 << 20)\n {\n double sum = 0;\n FOR(i,0,n)\n {\n if (!(mask & (1 << i)))\n {\n sum += p[i];\n }\n }\n\n if (sum < 1e-6) continue;\n\n FOR(i,0,n)\n {\n\n\n if (!(mask & (1 << i)))\n {\n if (cnt[mask] < k)\n {\n res[i] += dp[mask] * p[i] / sum;\n }\n dp[mask | (1 << i)] += dp[mask] * p[i] / sum;\n\n }\n }\n }\n\n FOR(i,0,n)\n {\n printf(\"%.12f \" , res[i]);\n\n }\n cout << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "b68dfcb02bc6bf115c29f69f4244b3bf", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define sz(a) ((int)(a).size())\n#define null NULL\n#define mp make_pair\n#define fi first\n#define se second\n#define pb push_back\n#define all(a) (a).begin(), (a).end()\n#define relaxMin(a, b) (a) = min((a), (b))\n#define relaxMax(a, b) (a) = max((a), (b))\n#define PI 3.1415926535897932384\ntypedef long long ll;\ntypedef vector vi;\ntypedef pair pii;\n\n#define BITS(a) __builtin_popcount(a)\nconst double EPS = 1E-9;\n\ntypedef long double dbl;\n\ntypedef pair Val; // a * fi + se\n\nVal Add(const Val& f, const Val& s){\n return mp(f.fi + s.fi,\n f.se + s.se);\n}\n\nVal Sub(const Val& f, const Val& s){\n return mp(f.fi - s.fi,\n f.se - s.se);\n}\n\nVal Mult(const Val& f, dbl w){\n return mp(f.fi * w,\n f.se * w);\n}\n\nVal Div(const Val& f, dbl w){\n return mp(f.fi / w,\n f.se / w);\n}\n\nVal dp[1 << 20];\nint n, k;\ndouble p[25];\n\ndouble Solve(int vr){\n if(p[vr] < EPS) return 0.0;\n\n dp[0] = Val(1.0, 0.0);\n for(int msk = 1;msk < (1 << n);++msk){\n if(msk & (1 << vr)) continue;\n //Val ri = Mult(Val(1.0, 0.0), p[vr]);\n Val ri(0.0, 0.0);\n dbl same = 0.0;\n\n for(int i = 0;i < n;++i)\n if(i != vr)\n if(msk & (1 << i)){\n same += p[i];\n ri = Add(ri, Mult(dp[msk ^ (1 << i)], p[i]));\n }\n\n //cout << ri.fi << ' ' << ri.s << endl;\n dp[msk] = Div(ri, 1.0 - same);\n }\n\n //cout << dp[2].fi << ' ' << dp[2].se << endl;\n\n Val tot(1.0, 0.0);\n for(int msk = 1;msk < (1 << n);++msk)\n if(!(msk & (1 << vr))){\n tot = Add(tot, dp[msk]);\n }\n\n dbl alp = (1.0 - tot.se) / tot.fi;\n\n Val bad(0.0, 0.0);\n for(int msk = 0;msk < (1 << n);++msk){\n if(msk & (1 << vr)) continue;\n if(BITS(msk) >= k) bad = Add(bad, dp[msk]);\n }\n\n return 1.0 - (alp * bad.fi + bad.se);\n}\n\nint main(){\n scanf(\"%d%d\", &n, &k);\n for(int i = 0;i < n;++i)\n scanf(\"%lf\", &p[i]);\n\n for(int i = 0;i < n;++i)\n printf(\"%.12f%c\", Solve(i), (i + 1 == n ? '\\n' : ' '));\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "b557b8096907055031c9c3c37add943f", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define all(v) (v).begin(), (v).end()\n\nusing li = long long;\nusing vi = vector;\nvoid solve();\n\n\nint main() {\n#ifdef LOCAL\n\tfreopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n\tauto start = clock();\n#endif\n\tcout.sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.precision(10);\n\tcout << fixed;\n\tint t;\n\t//cin >> t;\n\tt = 1;\n\twhile (t--) {\n\t\tsolve();\n\t}\n#ifdef LOCAL\n\tcerr << endl << endl << (clock() - start) / double(CLOCKS_PER_SEC) << endl;\n#endif\n\treturn 0;\n}\n\n//#define int li\n\nauto& in = std::cin;\nauto& out = std::cout;\n\nvoid solve() {\n\tint n, k;\n\tin >> n >> k;\n\tvector allp(n);\n\n\tfor (int i = 0; i < n; ++i) {\n\t\tin >> allp[i];\n\t}\n\n\tfor (int i = 0; i < n; ++i) {\n\t\tif (fabs(allp[i]) < 1e-6) {\n\t\t\tout << \"0 \";\n\t\t\tcontinue;\n\t\t}\n\t\tvector dp(1 << (n - 1));\n\t\tvector p(n - 1);\n\t\tint c = 0;\n\t\tfor (int j = 0; j < n; ++j) {\n\t\t\tif (i != j) {\n\t\t\t\tp[c++] = allp[j];\n\t\t\t}\n\t\t}\n\t\tdp[0] = allp[i];\n\t\tdouble ans = dp[0];\n\t\tfor (int mask = 1; mask < (1 << (n - 1)); ++mask) {\n\t\t\tdouble sumP = 0;\n\t\t\tdouble sum = 0;\n\t\t\tint cnt = 0;\n\t\t\tfor (int t = 0; t < (n - 1); ++t) {\n\t\t\t\tif (mask & (1 << t)) {\n\t\t\t\t\tsumP += p[t];\n\t\t\t\t\tsum += p[t] * dp[mask ^ (1 << t)];\n\t\t\t\t\t++cnt;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[mask] = sum / (1 - sumP);\n\t\t\tif (cnt < k) {\n\t\t\t\tans += dp[mask];\n\t\t\t}\n\t\t}\n\t\tout << ans << \" \";\n\n\t}\n}", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "bf377dc5cdb612cc18e23cb63632a75b", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\n\nint main() {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"task.in\", \"r\", stdin);\n\tfreopen(\"task.out\", \"w\", stdout);\n#endif\n\n\tcout.precision(15);\n\tcout << fixed;\n\n\tint n, k;\n\twhile (cin >> n >> k) {\n\t\tvector p(n);\n\t\tfor (int i = 0; i < n; i++) cin >> p[i];\n\n\t\tvector sum(1 << n);\n\t\tfor (int i = 0; i < 1 << n; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (i & (1 << j)) {\n\t\t\t\t\tsum[i] += p[j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint cnt = 0;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcnt += p[i] > 1e-5;\n\t\t}\n\t\tif (cnt <= k) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tdouble pp;\n\t\t\t\tif (p[i] > 1e-5) pp = 1.0;\n\t\t\t\telse pp = 0.0;\n\t\t\t\tcout << pp << \" \\n\"[i + 1 == n];\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\tvector dp(1 << n);\n\t\tdp[0] = 1;\n\t\tfor (int mask = 0; mask < 1 << n; mask++) {\n\t\t\tint rmask = (~mask) & ((1 << n) - 1);\n\t\t\tfor (int i = 0; i < n; i++) if ((rmask & (1 << i)) && p[i] > 1e-5) {\n\t\t\t\tdp[mask | (1 << i)] += dp[mask] * p[i] / sum[rmask];\n\t\t\t}\n\t\t}\n\t\tvector ans(n);\n\t\tfor (int mask = 0; mask < 1 << n; mask++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int i = 0; i < n; i++) cnt += (mask & (1 << i)) != 0;\n\t\t\tif (cnt != k) continue;\n\t\t\tfor (int i = 0; i < n; i++) if (mask & (1 << i)) {\n\t\t\t\tans[i] += dp[mask];\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < n; i++) cout << ans[i] << \" \\n\"[i + 1 == n];\n\t}\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "174acde370514d30af5e323785b4cc8e", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define all(x) (x).begin (),(x).end()\n#define sqrt(x) sqrt(abs(x))\n#define re return\n#define sz(x) ((int)(x.size()))\n\nusing ll = long long;\nusing ii = pair;\nusing ld = long double;\nusing D = double;\nusing vi = vector;\nusing vii = vector;\nusing vvi = vector;\nusing vs = vector;\n\ntemplate T abs (T x) { re x < 0 ? -x : x; }\ntemplate T sgn (T x) { re x < 0 ? -1 : (x > 0 ? 1 : 0); }\ntemplate T sqr (T x) { re x * x; }\ntemplate T gcd (T a, T b) { re a ? gcd (b % a, a) : b; }\n\nint n;\nint m;\n\ndouble p[20];\ndouble res[1 << 20];\ndouble q[20];\n\nint main () {\n\tscanf (\"%d%d\", &n, &m);\n\tfor (int i = 0; i < n; i++) scanf (\"%lf\", &p[i]);\n\tfor (int i = 0; i < n; i++) res[1 << i] = p[i];\n\tfor (int i = 0; i < n; i++) q[i] = 0;\n\tfor (int i = 1; i < (1 << n); i++) {\n\t\tvi v, w;\n\t\tdouble sum = 0;\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tif ((i >> j) & 1)\n\t\t\t\tv.pb (j);\n\t\t\telse {\n\t\t\t\tw.pb (j);\n\t\t\t\tsum += p[j];\n\t\t\t}\n\t\tif (sz (v) == m || (sz (v) < m && sum < 1e-5)) {\n\t\t\tfor (auto x : v) q[x] += res[i];\n\t\t} else\n\t\tif (sz (v) < m) {\n\t\t\tfor (auto x : w) res[i | (1 << x)] += res[i] * p[x] / sum;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) printf (\"%.15f \", q[i]);\n\tprintf (\"\\n\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "09fe5d89b555c8ae4364f911d600b75b", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\n#define forn(i, n) for (int i = 0; i < (int)(n); ++i)\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se second\ntypedef long long i64;\ntypedef long long ll;\ntypedef vector vi;\ntypedef pair pii;\ntypedef long double ld;\n\nconst int M = 21;\n\nconst ld eps = 1e-6;\n\nint n = 0, k, m;\nld p[M];\nint id[M];\nld d[1 << M];\nld ans[M];\n\n\nvoid read() {\n cin >> m >> k;\n for (int i = 0; i < m; ++i) {\n ld x;\n cin >> x;\n if (x > eps) {\n p[n] = x;\n id[n] = i;\n ++n;\n } else {\n ans[i] = 0;\n }\n }\n}\n\nvoid kill() {\n if (k >= n) {\n for (int i = 0; i < n; ++i)\n ans[id[i]] = 1.0;\n } else {\n d[0] = 1.0;\n \n for (int mask = 0; mask < (1 << n); ++mask)\n if (__builtin_popcount(mask) < k) {\n ld sum = 0.0;\n\n for (int t = 0; t < n; ++t)\n if (((mask >> t) & 1) == 0) {\n sum += p[t];\n }\n\n for (int t = 0; t < n; ++t)\n if (!((mask >> t) & 1)) {\n d[mask ^ (1 << t)] += p[t] / sum * d[mask];\n }\n }\n\n for (int mask = 0; mask < (1 << n); ++mask)\n if (__builtin_popcount(mask) == k) {\n for (int t = 0; t < n; ++t)\n if ((mask >> t) & 1) {\n ans[id[t]] += d[mask];\n }\n }\n }\n\n for (int i = 0; i < m; ++i)\n cout << ans[i] << \" \";\n cout << endl;\n}\n\n\n\nsigned main() {\n#ifdef LOCAL\n assert(freopen(\"a.in\", \"r\", stdin));\n#endif\n\n cout.precision(10);\n cout << fixed;\n\n read();\n kill();\n\n\n#ifdef LOCAL\n cerr << \"Time: \" << clock()/1000 << \" ms\" << endl;\n#endif\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "ba8d0a51565975dc2cf2d51f2d25e5b6", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//#pragma comment(linker, \"/STACK:16777216\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define y0 sdkfaslhagaklsldk\n#define y1 aasdfasdfasdf\n#define yn askfhwqriuperikldjk\n#define j1 assdgsdgasghsf\n#define tm sdfjahlfasfh\n#define lr asgasgash\n\nusing ll = long long;\nusing pii = pair;\ntemplate struct triple {\n\tT1 x; T2 y; T3 z;\n};\nusing tri = triple;\nusing trll = triple;\ntemplate\nbool operator<(const triple& t1, const triple& t2) {\n\treturn t1.x < t2.x || (t1.x == t2.x && (t1.y < t2.y || t1.y == t2.y && t1.z < t2.z));\n}\n\n#define eps 1e-9\n#define bs 1000000007\nconst int mod = 1000000007;\nconst int INF = 1000111000;\nconst ll LLINF = 1000111000111000111LL;\nconst double PI = acos(-1.0);\n#define bsize 256\n\n#define all(v) (v).begin(), (v).end()\n#define FI(n) for (int i = 0; i < n; ++i)\n#define FJ(n) for (int j = 0; j < n; ++j)\n#define FK(n) for (int k = 0; k < n; ++k)\n#define FREIN(file) freopen(file, \"rt\", stdin)\n#define FREOUT(file) freopen(file, \"wt\", stdout)\n#define sqr(x) ((x)*(x))\n\n#define DB(n) cerr<<#n<<\" = \"<<(n)<<\" \"\n#define DBN(n) cerr<<#n<<\" = \"<<(n)<<\"\\n\"\n\nint gcd(int x, int y) { while (y) { x %= y, swap(x, y); } return y; }\nll gcd(ll x, ll y) { while (y) { x %= y, swap(x, y); } return y; }\nll lcm(ll x, ll y) { return x / gcd(x, y) * y; }\nll powmod(ll x, ll n, const int mod) {\n\tll res = 1; while (n)\n\t{\n\t\tif (n & 1) res = res * x % mod; n /= 2; x = x * x % mod;\n\t} return res;\n}\n\nint n, k;\ndouble prob[200];\ndouble dp[1 << 20];\ndouble sum[1 << 20];\ndouble ans[22];\n\nint count_bits(int x)\n{\n\tif (x == 0)\n\t\treturn 0;\n\treturn count_bits(x / 2) + x % 2;\n}\n\nint flag;\n\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\n\tcin >> n >> k;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin >> prob[i];\n\t}\n\n\tfor (int mask = 0; mask < (1 << n); mask++)\n\t{\n\t\tdouble s = 0;\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif (mask&(1 << i))\n\t\t\t\ts += prob[i];\n\t\t}\n\t\tsum[mask] = s;\n\t}\n\n\tdp[0] = 1;\n\n\tfor (int mask = 0; mask < (1 << n); mask++)\n\t{\n\t\tint qb = count_bits(mask);\n\t\tif (qb > k)\n\t\t\tcontinue;\n\t\tif (dp[mask]<1e-100)\n\t\t\tcontinue;\n\t\tif (qb == k)\n\t\t\tflag = 1;\n\t\tdouble rem_sum = 1.0 - sum[mask];\n\t\tif (rem_sum < 1e-6)\n\t\t\tcontinue;\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif (mask&(1 << i))\n\t\t\t\tcontinue;\n\t\t\tdouble chance_move = dp[mask] * prob[i] / rem_sum;\n\t\t\tdp[mask | (1 << i)] += chance_move;\n\t\t}\n\t}\n\n\tif (flag == 0)\n\t{\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif (prob[i] < 1e-6)\n\t\t\t\tans[i] = 0;\n\t\t\telse\n\t\t\t\tans[i] = 1;\n\t\t}\n\t}\n\telse\n\t{\n\t\tfor (int mask = 0; mask < (1 << n); mask++)\n\t\t{\n\t\t\tif (count_bits(mask) == k)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t\t{\n\t\t\t\t\tif (mask&(1 << i))\n\t\t\t\t\t\tans[i] += dp[mask];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tcout.precision(12);\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (i)\n\t\t\tcout << \" \";\n\t\tcout << fixed << ans[i];\n\t}\n\tcout << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "f2a108a95c9cd94f2312521fa631036b", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nlong double c[21][21], coef[21], sum[1 << 20], p[20];\nint sz[1 << 20];\n\nint main() {\n\tint n, k;\n\tscanf(\"%d %d\", &n, &k);\n\tfor (int i = 0; i < n; i++) {\n\t\tdouble x;\n\t\tscanf(\"%lf\", &x);\n\n\t\tp[i] = x;\n\t}\n\n\tc[0][0] = 1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tc[i][0] = 1;\n\t\tfor (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];\n\t}\n\n\tfor (int i = 0; i < (1 << n); i++) {\n\t\tsz[i] = 0;\n\t\tsum[i] = 0;\n\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tif (((i >> j) & 1) == 1) {\n\t\t\t\tsz[i]++;\n\t\t\t\tsum[i] += p[j];\n\t\t\t}\n\t}\n\n\tfor (int i = k - 1; i >= 0; i--) {\n\t\tcoef[i] = 1 - coef[i];\n\n\t\tfor (int j = 1; i - j >= 0; j++) coef[i - j] += c[n - 1 - i + j][j] * coef[i];\n\t}\n\n\tfor (int i = 0; i < n; i++) {\n\t\tlong double ans = 0.0;\n\t\tif (p[i] > 0.005) {\n\t\t\tfor (int msk = 0; msk < (1 << n); msk++) {\n\t\t\t\tif (((msk >> i) & 1) == 1) continue;\n\t\t\t\tif (sz[msk] >= k) continue;\n\n\t\t\t\tans += coef[sz[msk]] / (1 - sum[msk]);\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%.12lf \", (double)(p[i] * ans));\n\t}\n\n\tprintf(\"\\n\");\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "a231a98eb635f4838fe0717a8e26f49b", "src_uid": "ad290c29e7587561391cefab73026171", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//# pragma GCC optimize (\"O2\")\n//# pragma comment(linker, \"/STACK:1024000000,1024000000\")\n#include\n#define lb(x) ((x)&(-x))\n#define out(i,a,now) for(LL i=a.be[now],to=a.v[i];~i;i=a.ne[i],to=a.v[i])\n#define fo(i,a,b) for(i=(a);i<=(b);++i)\n#define fd(i,a,b) for(i=(a);i>=(b);--i)\n#define mid ((l+r)>>1)\n#define max(a,b) (a>b?a:b)\n#define min(a,b) (a>b?b:a)\n#define mp(a,b) make_pair(a,b)\n#define qk(x) memset(x,0,sizeof(x))\n#define RG register\n#define SF scanf\n#define PF printf\n#define FP freopen\n#define FI first\n#define SE second\n#define cpy(a,b) memcpy(a,b,sizeof(a))//b to a\n#define pi 3.1415926535897932384626433832\n#define ls (now<<1)\n#define rs (now<<1|1)\n#ifdef Mogician\n#endif\nusing namespace std;\ntypedef long long LL;\ntypedef double DB;\ntypedef pair Pair;\ntemplate void cmax(T &a,T b){\n a=max(a,b);\n}\ntemplate void cmin(T &a,T b){\n a=min(a,b);\n}\nconst LL maxn=140000,kcz=998244353;\nLL ksm(LL ds,LL zs=kcz-2,LL mod=kcz){\n\tLL res=1;\n\tfor(;zs;zs>>=1,ds=ds*ds%mod)\n\t\tif (zs&1) res=res*ds%mod;\n\treturn res;\n}\nconst LL i100=ksm(100);\nLL lim[maxn][2][2],n;\nLL p[maxn][2];\nstruct SEG{\n\tLL tr[maxn<<1][2][2];\n\tvoid up(LL now,LL l,LL r){\n\t\tqk(tr[now]);\n\t\tLL i,j,k,t;\n\t\tfo(i,0,1) fo(j,0,1) fo(k,0,1) fo(t,0,1)\n\t\t\tif (!lim[mid][k][t]) (tr[now][i][j]+=tr[ls][i][k]*tr[rs][t][j])%=kcz;\n\t}\n\tvoid build(LL now,LL l,LL r){\n\t\tif (l==r){\n\t\t\ttr[now][0][0]=p[l][0];\n\t\t\ttr[now][1][1]=p[l][1];\n\t\t\treturn;\n\t\t}\n\t\tbuild(ls,l,mid); build(rs,mid+1,r);\n\t\tup(now,l,r);\n\t}\n\tvoid up(LL now,LL l,LL r,LL x){\n\t\tif (l==r) return;\n\t\tif (mid>=x) up(ls,l,mid,x);\n\t\telse up(rs,mid+1,r,x);\n\t\tup(now,l,r);\n\t}\n}seg;\nstruct data{\n\tLL id,l,r,t;\n\tDB p;\n\tdata(){\n\t\tid=l=r=t=0; p=0;\n\t}\n\tdata(LL a,LL b,LL c,LL d,DB e){\n\t\tid=a; l=b; r=c; t=d; p=e;\n\t}\n\tbool operator <(const data &b) const{\n\t\treturn p1){\n\t\t\ta[++cnt]=data(i-1,1,0,(X-lx)*ksm(V+lv)%kcz,(DB)(X-lx)/(V+lv));\n\t\t\tif (V>lv)\n\t\t\t\ta[++cnt]=data(i-1,0,0,(X-lx)*ksm(V-lv)%kcz,(DB)(X-lx)/(V-lv));\n\t\t\telse if (lv>V)\n\t\t\t\ta[++cnt]=data(i-1,1,1,(X-lx)*ksm(lv-V)%kcz,(DB)(X-lx)/(lv-V));\n\t\t}\n\t\tp[i][0]=(100-P)*i100%kcz;\n\t\tp[i][1]=P*i100%kcz;\n\t\tlv=V; lx=X;\n\t}\n\tseg.build(1,1,n);\n\tsort(a+1,a+cnt+1);\n\tLL ans=0,last=1,now,j,k;\n\tfo(i,1,cnt){\n\t\tlim[a[i].id][a[i].l][a[i].r]=1;\n\t\tseg.up(1,1,n,a[i].id);\n\t\tnow=0;\n\t\tfo(j,0,1) fo(k,0,1) (now+=seg.tr[1][j][k])%=kcz;\n\t\tans+=a[i].t*(last-now+kcz)%kcz;\n\t\tlast=now;\n\t}\n\tPF(\"%lld\",(ans%kcz+kcz)%kcz);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "01d5bad2825b964e455631c9a93dd4a3", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n// tourist's modular-arithmetic class\ntemplate\nT inverse(T a, T m) {\n T u = 0, v = 1;\n while (a != 0) {\n T t = m / a;\n m -= t * a;\n swap(a, m);\n u -= t * v;\n swap(u, v);\n }\n assert(m == 1);\n return u;\n}\n\ntemplate\nclass Modular {\npublic:\n using Type = typename decay::type;\n\n constexpr Modular() : value() {}\n\n template\n Modular(const U &x) {\n value = normalize(x);\n }\n\n template\n static Type normalize(const U &x) {\n Type v;\n if (-mod() <= x && x < mod()) v = static_cast(x);\n else v = static_cast(x % mod());\n if (v < 0) v += mod();\n return v;\n }\n\n const Type &operator()() const { return value; }\n\n template\n explicit operator U() const { return static_cast(value); }\n\n constexpr static Type mod() { return T::value; }\n\n Modular &operator+=(const Modular &other) {\n if ((value += other.value) >= mod()) value -= mod();\n return *this;\n }\n\n Modular &operator-=(const Modular &other) {\n if ((value -= other.value) < 0) value += mod();\n return *this;\n }\n\n template\n Modular &operator+=(const U &other) { return *this += Modular(other); }\n\n template\n Modular &operator-=(const U &other) { return *this -= Modular(other); }\n\n Modular &operator++() { return *this += 1; }\n\n Modular &operator--() { return *this -= 1; }\n\n Modular operator++(int) {\n Modular result(*this);\n *this += 1;\n return result;\n }\n\n Modular operator--(int) {\n Modular result(*this);\n *this -= 1;\n return result;\n }\n\n Modular operator-() const { return Modular(-value); }\n\n template\n typename enable_if::Type, int>::value, Modular>::type &operator*=(const Modular &rhs) {\n#ifdef _WIN32\n uint64_t x = static_cast(value) * static_cast(rhs.value);\n uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m;\n asm(\n \"divl %4; \\n\\t\"\n : \"=a\" (d), \"=d\" (m)\n : \"d\" (xh), \"a\" (xl), \"r\" (mod())\n );\n value = m;\n#else\n value = normalize(static_cast(value) * static_cast(rhs.value));\n#endif\n return *this;\n }\n\n template\n typename enable_if::Type, int64_t>::value, Modular>::type &\n operator*=(const Modular &rhs) {\n int64_t q = static_cast(static_cast(value) * rhs.value / mod());\n value = normalize(value * rhs.value - q * mod());\n return *this;\n }\n\n template\n typename enable_if::Type>::value, Modular>::type &operator*=(const Modular &rhs) {\n value = normalize(value * rhs.value);\n return *this;\n }\n\n Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, mod())); }\n\n template\n friend const Modular &abs(const Modular &v) { return v; }\n\n template\n friend bool operator==(const Modular &lhs, const Modular &rhs);\n\n template\n friend bool operator<(const Modular &lhs, const Modular &rhs);\n\n template\n friend std::istream &operator>>(std::istream &stream, Modular &number);\n\nprivate:\n Type value;\n};\n\n\ntemplate\nbool operator==(const Modular &lhs, const Modular &rhs) { return lhs.value == rhs.value; }\n\ntemplate\nbool operator==(const Modular &lhs, U rhs) { return lhs == Modular(rhs); }\n\ntemplate\nbool operator==(U lhs, const Modular &rhs) { return Modular(lhs) == rhs; }\n\ntemplate\nbool operator!=(const Modular &lhs, const Modular &rhs) { return !(lhs == rhs); }\n\ntemplate\nbool operator!=(const Modular &lhs, U rhs) { return !(lhs == rhs); }\n\ntemplate\nbool operator!=(U lhs, const Modular &rhs) { return !(lhs == rhs); }\n\ntemplate\nbool operator<(const Modular &lhs, const Modular &rhs) { return lhs.value < rhs.value; }\n\ntemplate\nModular operator+(const Modular &lhs, const Modular &rhs) { return Modular(lhs) += rhs; }\n\ntemplate\nModular operator+(const Modular &lhs, U rhs) { return Modular(lhs) += rhs; }\n\ntemplate\nModular operator+(U lhs, const Modular &rhs) { return Modular(lhs) += rhs; }\n\ntemplate\nModular operator-(const Modular &lhs, const Modular &rhs) { return Modular(lhs) -= rhs; }\n\ntemplate\nModular operator-(const Modular &lhs, U rhs) { return Modular(lhs) -= rhs; }\n\ntemplate\nModular operator-(U lhs, const Modular &rhs) { return Modular(lhs) -= rhs; }\n\ntemplate\nModular operator*(const Modular &lhs, const Modular &rhs) { return Modular(lhs) *= rhs; }\n\ntemplate\nModular operator*(const Modular &lhs, U rhs) { return Modular(lhs) *= rhs; }\n\ntemplate\ntypename enable_if::value, Modular>::type operator*(U lhs, const Modular &rhs) {\n return Modular(lhs) *= rhs;\n}\n\ntemplate\nModular operator/(const Modular &lhs, const Modular &rhs) { return Modular(lhs) /= rhs; }\n\ntemplate\nModular operator/(const Modular &lhs, U rhs) { return Modular(lhs) /= rhs; }\n\ntemplate\nModular operator/(U lhs, const Modular &rhs) { return Modular(lhs) /= rhs; }\n\ntemplate\nModular power(const Modular &a, const U &b) {\n assert(b >= 0);\n Modular x = a, res = 1;\n U p = b;\n while (p > 0) {\n if (p & 1) res *= x;\n x *= x;\n p >>= 1;\n }\n return res;\n}\n\ntemplate\nbool IsZero(const Modular &number) {\n return number() == 0;\n}\n\ntemplate\nstring to_string(const Modular &number) {\n return to_string(number());\n}\n\ntemplate\nstd::ostream &operator<<(std::ostream &stream, const Modular &number) {\n return stream << number();\n}\n\ntemplate\nstd::istream &operator>>(std::istream &stream, Modular &number) {\n typename common_type::Type, int64_t>::type x;\n stream >> x;\n number.value = Modular::normalize(x);\n return stream;\n}\n\nstruct fast_ios {\n fast_ios() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n };\n} fast_ios_;\n\ntemplate\nbool chkmin(A &a, const B &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate\nbool chkmax(A &a, const B &b) {\n if (b > a) {\n a = b;\n return true;\n }\n return false;\n}\n\nint cas;\n\nostream &kase() {\n return cout << \"Case #\" << ++cas << \": \";\n}\n\ntemplate\nostream &operator<<(ostream &stream, const vector &vec) {\n if (!vec.empty()) {\n stream << vec[0];\n for (size_t i = 1; i != vec.size(); ++i)\n stream << ' ' << vec[i];\n }\n return stream;\n}\n\ntemplate\nvoid print(const vector &t) { cout << t << '\\n'; }\n\ntemplate\nvoid print(const vector> &t) {\n for (const auto &row : t) {\n print(row);\n }\n}\n\ntemplate\nvoid print(const T &t) { cout << t << ' '; }\n\ntemplate\nvoid print(const T &t, const Args &...rest) {\n print(t);\n print(rest...);\n}\n\ntemplate\nvoid println(const T &t) { cout << t << '\\n'; }\n\ntemplate\nvoid println(const T &t, const Args &...rest) {\n print(t);\n println(rest...);\n}\n\ntemplate\nistream &operator>>(istream &stream, vector &vec) {\n for (auto &x : vec) stream >> x;\n return stream;\n}\n\nvoid scan() {}\n\ntemplate\nvoid scan(T &a, Args &...rest) {\n cin >> a;\n scan(rest...);\n}\n\nusing ull = unsigned long long;\nusing ll = long long;\nusing ul = unsigned long;\nusing vl = vector;\nusing vi = vector;\nusing pii = pair;\nusing pil = pair;\nusing pli = pair;\nusing pip = pair;\nusing pll = pair;\nusing vb = vector;\nusing vpii = vector;\nusing ldb = long double;\n\ntemplate\ninline int_t lowbit(int_t x) { return x & -x; }\n\n#if __cplusplus < 201402L\ntemplate\nstd::reverse_iterator make_reverse_iterator(Iterator it) {\n return std::reverse_iterator(it);\n}\n#endif\n\ntemplate\nstruct iter_pair {\n iter_t _beg, _end;\n\n iter_t begin() { return _beg; }\n\n iter_t end() { return _end; }\n};\n\ntemplate\niter_pair()))>>\nreverse(cont &&r) {\n return {make_reverse_iterator(end(r)), make_reverse_iterator(begin(r))};\n}\n\n#define rng(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)\n#define up(i, a, b) for (int i = int(a); i <= int(b); ++i)\n#define down(i, b, a) for (int i = int(b); i >= int(a); i--)\n#define rep(n) for(int _iter_##n = 0, _num_##n = (int)n; _iter_##n < _num_##n; ++_iter_##n)\n#define FOR(x, cont) for (const auto &x: cont)\n#define For(x, cont) for (auto &x: cont)\n#define all(x) begin(x), end(x)\n#define pb push_back\n#define mp make_pair\n#define eb emplace_back\n#define ep emplace\n#define SZ(x) (int)(x).size()\ntemplate>\nusing pq = priority_queue, Comp>;\n#define popcnt(x) __builtin_popcountll((x))\n#define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec))\n#define LB(cont, x) int(lower_bound(ALL(cont), x) - begin(cont))\n#define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont))\nconst int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};\nconst int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};\nauto bet = [](const ll x, const ll y, const ll i) {\n return x <= i && i <= y;\n};\n\ntemplate\nT1 ceil(T1 x, T2 y) { // y >= 1,是整数。需要注意 x + y - 1 是否会溢出\n return (x + y - 1) / y;\n}\n\ninline int h_bit(unsigned long long x) {\n return (int) sizeof(unsigned long long) * 8 - 1 - __builtin_clzll(x);\n}\n\nunsigned long long pow2(unsigned long long x) { // round up to power of 2\n return 1ull << (h_bit((ull) x) + (x != lowbit(x)));\n}\n\ntemplate\nclass BIT {\n vector a;\n using BinOp = function;\n BinOp bin_op;\n const T init;\npublic:\n explicit BIT(int n, BinOp op = [](T x, T y) { return x + y; }, T init = T{})\n : bin_op(op), init(init), a(n + 1, init) {}\n\n T prefix(int x) const {\n auto res = init;\n while (x) {\n res = bin_op(a[x], res);\n x -= x & -x;\n }\n return res;\n }\n\n void modify(int x, T v) {\n while (x < (int) a.size()) {\n a[x] = bin_op(a[x], v);\n x += x & -x;\n }\n }\n\n void clear() {\n fill(a.begin(), a.end(), init);\n }\n};\n\nvi get_prime(int n) {\n vi minp((ul) n + 1), p;\n for (int i = 2; i <= n; i++) {\n if (!minp[i]) {\n minp[i] = i;\n p.pb(i);\n }\n FOR(x, p) {\n if (x <= minp[i] && x * i <= n)\n minp[x * i] = x;\n else break;\n }\n }\n return p;\n}\n\n// alias templates\ntemplate using vv = vector>;\ntemplate using vp = vector>;\ntemplate using va = vector>;\n\n//order_of_key (k) : Number of items strictly smaller than k .\n//find_by_order(k) : K-th element in a set (counting from zero).\n#ifdef __GNUC__\n\n#include \n\ntemplate>\nusing order_set = __gnu_pbds::tree<\n T,\n __gnu_pbds::null_type,\n comp,\n __gnu_pbds::rb_tree_tag,\n __gnu_pbds::tree_order_statistics_node_update>;\n\n#include \n\nusing trie = __gnu_pbds::trie;\n#endif\n\nclass UnionFind {\n vi par, sz;\n int nTree;\npublic:\n explicit UnionFind(int n) : par(n), sz(n, 1), nTree(n) { //0-indexed\n iota(par.begin(), par.end(), 0);\n }\n\n int n_tree() const {\n return nTree;\n }\n\n int size(int x) {\n return sz[root(x)];\n }\n\n int root(int x) {\n return x == par[x] ? x : par[x] = root(par[x]);\n }\n\n bool unite(int x, int y) {\n int rx = root(x), ry = root(y);\n if (rx != ry) {\n par[rx] = ry;\n --nTree;\n sz[ry] += sz[rx];\n return true;\n }\n return false;\n }\n\n bool is_root(int x) const {\n return par[x] == x;\n }\n};\n\ntemplate>\nstruct SparseTable {\n size_t n{}; // 0-indexed\n vv a;\n\n template\n SparseTable(ptr_t beg, ptr_t end):n(end - beg) {\n a.resize((size_t) h_bit(n) + 1); // 注意:不能写成 h_bit(n)\n a[0].assign(beg, end);\n rng (i, 0, SZ(a) - 1) {\n a[i + 1].resize(n);\n rng(j, 0, n - (1u << i)) {\n a[i + 1][j] = min(a[i][j], a[i][j + (1u << i)], Compare());\n }\n rng(j, n - (1u << i), n) {\n a[i + 1][j] = a[i][j];\n }\n }\n }\n\n using idx_t = long;\n\n T query(idx_t l, idx_t r) { // l <= r\n int i = h_bit(r - l + 1ul);\n return min(a[i][l], a[i][r + 1 - (1u << i)], Compare());\n }\n};\n\n// debug code by tourist\nstring to_string(const string &s) {\n return '\"' + s + '\"';\n}\n\nstring to_string(const char *s) {\n return to_string((string) s);\n}\n\nstring to_string(bool b) {\n return (b ? \"true\" : \"false\");\n}\n\ntemplate\nstring to_string(pair p) {\n return \"(\" + to_string(p.first) + \", \" + to_string(p.second) + \")\";\n}\n\ntemplate\nstring to_string(A v) {\n bool first = true;\n string res = \"{\";\n for (const auto &x : v) {\n if (!first) {\n res += \", \";\n }\n first = false;\n res += to_string(x);\n }\n res += \"}\";\n return res;\n}\n\nvoid debug_out() { cerr << endl; }\n\ntemplate\nvoid debug_out(Head H, Tail... T) {\n cerr << \" \" << to_string(H);\n debug_out(T...);\n}\n\n#ifdef LOCAL\n#define debug(...) cerr << \"[\" << #__VA_ARGS__ << \"]:\", debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\n// end DEBUG\ntemplate\nclass MonoQueue {\nprivate:\n deque> que;\n Compare cmp;\npublic:\n void push(const T &val, int i) {\n while (!que.empty() && !cmp(que.back().first, val)) {\n que.pop_back();\n }\n que.emplace_back(val, i);\n }\n\n T get(int i) {\n while (que.front().second < i) {\n que.pop_front();\n }\n return que.front().first;\n }\n};\n\ntemplate\nstruct Binom {\n vector fact, inv_fact;\n\n explicit Binom(int n) : fact(n + 1), inv_fact(n + 1) {\n fact[0] = 1;\n up (i, 1, n) fact[i] = fact[i - 1] * i;\n inv_fact[n] = 1 / fact[n];\n down (i, n, 1) {\n inv_fact[i - 1] = inv_fact[i] * i;\n }\n }\n\n T get_binom(int x, int y) const {\n assert(x <= SZ(fact) - 1);\n assert(x >= 0 && y >= 0);\n if (x < y) return 0;\n return fact[x] * inv_fact[y] * inv_fact[x - y];\n }\n\n T get_fact(int n) const {\n assert(n < SZ(fact));\n return fact[n];\n }\n};\n\ntemplate\nT get_mid(T l, T r) {\n assert(l <= r);\n return l + (r - l) / 2;\n}\n\ntemplate\nint get_bit(T a, int i) {\n return a >> i & 1;\n}\n\n// ftiasch's implementation of segment tree\nint get_id(int l, int r) {\n assert(0 <= l && l <= r);\n // 叶子的下标是 0, 2, 4, ..., 2*(n-1)\n return l + r | (l != r);\n}\n\n/*using ModType = int;\n\n struct VarMod {\n static ModType value;\n };\n ModType VarMod::value;\n ModType &md = VarMod::value;\n using Mint = Modular;*/\nconstexpr int md = 998244353;\nusing Mint = Modular::type, md>>;\n//////////////////^\"^///////////////////////////////////\n\n// 仿照 ftiasch 的写法\ntemplate\nstruct Mat {\n array, 2> m;\n\n array &operator[](int i) {\n return m[i];\n }\n\n const array &operator[](int i) const {\n return m[i];\n }\n\n Mat operator*(const Mat b) {\n Mat c;\n rng (i, 0, 2) {\n rng (j, 0, 2) {\n rng (k, 0, 2) {\n c[i][j] += m[i][k] * b[k][j];\n }\n }\n }\n return c;\n }\n};\n\nstruct Frac {\n int p, q;\n operator Mint() {\n assert(q != 0);\n return p / Mint(q);\n }\n};\n\nbool operator<(const Frac &a, const Frac &b) {\n return 1ll * a.p * b.q < 1ll * b.p * a.q;\n}\n\nint main() {\n#if defined LOCAL && !defined DUIPAI\n ifstream in(\"in.txt\");\n cin.rdbuf(in.rdbuf());\n#endif\n int n;\n scan(n);\n if (n == 1) {\n println(0);\n return 0;\n }\n vi x(n), v(n);\n vector p(n);\n Mint inv_100 = 1 / Mint(100);\n rng (i, 0, n) {\n scan(x[i], v[i], p[i]);\n p[i] *= inv_100;\n }\n\n vector> allowed_collisions(n - 1);\n // 0: left, 1: right\n rng (i, 0, n - 1) {\n allowed_collisions[i][0][1] = p[i + 1];\n }\n\n // segment tree\n vector> a(2 * (n - 1) - 1);\n auto get_prob = [to_right = p[0], &mat = a[get_id(0, n - 2)]]() {\n return to_right * (mat[1][0] + mat[1][1]) + (1 - to_right) * (mat[0][0] + mat[0][1]);\n };\n function build = [&](int l, int r) {\n if (l == r) {\n a[2 * l] = allowed_collisions[l];\n return;\n }\n int mid = get_mid(l, r);\n build(l, mid);\n build(mid + 1, r);\n a[get_id(l, r)] = a[get_id(l, mid)] * a[get_id(mid + 1, r)];\n };\n build(0, n - 2);\n function modify = [&](int l, int r, int k) {\n if (l == r) {\n a[2 * l] = allowed_collisions[k];\n return;\n }\n int mid = get_mid(l, r);\n if (k <= mid) {\n modify(l, mid, k);\n } else {\n modify(mid + 1, r, k);\n }\n a[get_id(l, r)] = a[get_id(l, mid)] * a[get_id(mid + 1, r)];\n };\n vector> events(3 * (n - 1));\n\n rng (i, 0, n - 1) {\n int d = x[i + 1] - x[i];\n events[i * 3] = {{d, v[i + 1] + v[i]}, i << 2 | 1};\n events[i * 3 + 1] = {\n v[i] < v[i + 1] ? Frac{d, v[i + 1] - v[i]} : Frac{1, 0}, i << 2 | 0};\n events[i * 3 + 2] = {\n v[i] > v[i + 1] ? Frac{d, v[i] - v[i + 1]} : Frac{1, 0}, i << 2 | 3};\n }\n\n sort(all(events));\n\n Mint prev_prob = get_prob(), ans = 0;\n\n FOR (event, reverse(events)) {\n int i = event.second >> 2;\n int dir1 = get_bit(event.second, 0);\n int dir2 = get_bit(event.second, 1);\n // allow this collision of the i-th and i+1-t proton to happen\n allowed_collisions[i][dir1][dir2] = dir2 ? p[i + 1] : 1 - p[i + 1];\n modify(0, n - 2, i);\n Mint prob = get_prob();\n if (event.first.q != 0) {\n ans += (prob - prev_prob) * event.first;\n }\n prev_prob = prob;\n }\n\n println(ans);\n\n#if defined LOCAL && !defined DUIPAI\n cout << \"Time elapsed: \" << 1.0 * clock() / CLOCKS_PER_SEC << \" s.\\n\";\n#endif\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "c4ff5f94b8be163cd9b6679c95cd7861", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nint n,leve,INV100,mod=998244353;\n#define maxn 100011\n#define maxeve maxn*3\nstruct Poi\n{\n\tint x,v,p;\n}a[maxn];\nstruct Eve\n{\n\tint id,a,b,timemod; double time;\n\tbool operator < (const Eve &b) const {return time>=1;\n\t}\n\treturn ans;\n}\n\ndouble calc(int i,int x,int y)\n{\n\tint v1,v2;\n\tif (x) v1=a[i-1].v; else v1=-a[i-1].v;\n\tif (y) v2=a[i].v; else v2=-a[i].v;\n\treturn 1.0*(a[i].x-a[i-1].x)/(v1-v2);\n}\n\nint calcmod(int i,int x,int y)\n{\n\tint v1,v2;\n\tif (x) v1=a[i-1].v; else v1=-a[i-1].v;\n\tif (y) v2=a[i].v; else v2=-a[i].v;\n\treturn ((0ll+a[i].x-a[i-1].x+mod)%mod)*1ll*powmod((0ll+v1-v2+mod+mod)%mod,mod-2)%mod;\n}\n\nstruct smtnode{int ls,rs,v[2][2];};\nstruct SMT\n{\n\tsmtnode a[maxn<<1];\n\tint n,sz;\n\tvoid clear(int N) {n=N; sz=0;}\n\tvoid up(int x)\n\t{\n\t\tint p=a[x].ls,q=a[x].rs;\n\t\tfor (int i=0;i<2;i++)\n\t\t\tfor (int j=0;j<2;j++)\n\t\t\t{\n\t\t\t\ta[x].v[i][j]=0;\n\t\t\t\tfor (int k=0;k<2;k++)\n\t\t\t\t\ta[x].v[i][j]=(a[x].v[i][j]+1ll*a[p].v[i][k]*a[q].v[k][j])%mod;\n\t\t\t}\n\t}\n\tvoid build(int &x,int L,int R,Poi *b)\n\t{\n\t\tx=++sz;\n\t\tif (L==R)\n\t\t{\n\t\t\ta[x].ls=a[x].rs=0;\n\t\t\ta[x].v[0][1]=a[x].v[1][1]=b[L].p;\n\t\t\ta[x].v[0][0]=a[x].v[1][0]=mod+1-b[L].p;\n\t\t\treturn;\n\t\t}\n\t\tint mid=(L+R)>>1;\n\t\tbuild(a[x].ls,L,mid,b);\n\t\tbuild(a[x].rs,mid+1,R,b);\n\t\tup(x);\n\t}\n\tvoid build(Poi *b) {int x; build(x,1,n,b);}\n\tint query() {return (0ll+a[1].v[0][0]+a[1].v[0][1])%mod;}\n\tvoid Modify(int x,int L,int R,int p,int q,int r)\n\t{\n\t\tif (L==R)\n\t\t{\n\t\t\ta[x].v[q][r]=0;\n\t\t\treturn;\n\t\t}\n\t\tint mid=(L+R)>>1;\n\t\tif (p<=mid) Modify(a[x].ls,L,mid,p,q,r);\n\t\telse Modify(a[x].rs,mid+1,R,p,q,r);\n\t\tup(x);\n\t}\n\tvoid modify(int p,int q,int r) {Modify(1,1,n,p,q,r);}\n}t;\n\nint main()\n{\n\tscanf(\"%d\",&n);\n\tINV100=powmod(100,mod-2);\n\tfor (int i=1;i<=n;i++) scanf(\"%d%d%d\",&a[i].x,&a[i].v,&a[i].p),a[i].p=a[i].p*1ll*INV100%mod;\n\t\n\t//0 zuo 1 you\n\t\n\tleve=0;\n\tfor (int i=2;i<=n;i++)\n\t{\n\t\teve[++leve]={i,1,0,calcmod(i,1,0),calc(i,1,0)};\n\t\tif (a[i-1].v>a[i].v) eve[++leve]={i,1,1,calcmod(i,1,1),calc(i,1,1)};\n\t\tif (a[i-1].v\n \n/*\n// ordered_set \n# include \n# include \nusing namespace __gnu_pbds;\n# define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> \n*/\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair ppi;\ntypedef pair pip;\ntypedef pair ppp;\ntypedef pair pll;\n \n# define A first\n# define B second\n# define endl '\\n'\n# define sep ' '\n# define all(x) x.begin(), x.end()\n# define kill(x) return cout << x << endl, 0\n# define SZ(x) int(x.size())\n# define Sort(x) sort(all(x))\n# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);\n \nll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}\n \nconst int xn = 1e5 + 10;\nconst int xm = 20 + 10;\nconst int SQ = 320;\nconst int sq = 320 + 10;\nconst int inf = 1e9 + 10;\nconst ll INF = 1e18 + 10;\nconst ll MOD = 998244353;\nconst int TM = 30;\nconst int BASE = 257;\n\nll n, num[xn], v[xn], p[xn], inv = power(100, MOD - 2, MOD), ans, flag, sum;\nppp seg[xn << 2];\nbool mark[xn][4];\nvector vec;\n\nvoid PRINT(ppp x){cout << x.A.A << sep << x.A.B << \" , \" << x.B.A << sep << x.B.B << endl;}\nbool CMP(ppp x, ppp y){\n\tif (ld(ld(x.A.A) / ld(x.A.B)) != ld(ld(y.A.A) / ld(y.A.B))) return (ld(ld(x.A.A) / ld(x.A.B)) < ld(ld(y.A.A) / ld(y.A.B)));\n\treturn x.B.A < y.B.A;\n}\nppp merge(ppp x, ppp y, pii f, int ind, int g = 0){\n\tif (x.A.A == -1) return y;\n\tif (y.A.A == -1) return x;\n\tppp z = {{0, 0}, {0, 0}};\n\tflag = (ind == f.A);\n\tif (mark[ind][0] == g || (f.B == 0 && g == 2)) z.A.A = (z.A.A + x.A.A * y.A.A) % MOD;\n\tif (mark[ind][1] == g || (f.B == 1 && g == 2)) z.A.A = (z.A.A + x.A.A * y.B.A) % MOD;\n\tif (mark[ind][2] == g || (f.B == 2 && g == 2)) z.A.A = (z.A.A + x.A.B * y.A.A) % MOD;\n\tif (mark[ind][3] == g || (f.B == 3 && g == 2)) z.A.A = (z.A.A + x.A.B * y.B.A) % MOD;\n\tif (mark[ind][0] == g || (f.B == 0 && g == 2)) z.A.B = (z.A.B + x.A.A * y.A.B) % MOD;\n\tif (mark[ind][1] == g || (f.B == 1 && g == 2)) z.A.B = (z.A.B + x.A.A * y.B.B) % MOD;\n\tif (mark[ind][2] == g || (f.B == 2 && g == 2)) z.A.B = (z.A.B + x.A.B * y.A.B) % MOD;\n\tif (mark[ind][3] == g || (f.B == 3 && g == 2)) z.A.B = (z.A.B + x.A.B * y.B.B) % MOD;\n\tif (mark[ind][0] == g || (f.B == 0 && g == 2)) z.B.A = (z.B.A + x.B.A * y.A.A) % MOD;\n\tif (mark[ind][1] == g || (f.B == 1 && g == 2)) z.B.A = (z.B.A + x.B.A * y.B.A) % MOD;\n\tif (mark[ind][2] == g || (f.B == 2 && g == 2)) z.B.A = (z.B.A + x.B.B * y.A.A) % MOD;\n\tif (mark[ind][3] == g || (f.B == 3 && g == 2)) z.B.A = (z.B.A + x.B.B * y.B.A) % MOD;\n\tif (mark[ind][0] == g || (f.B == 0 && g == 2)) z.B.B = (z.B.B + x.B.A * y.A.B) % MOD;\n\tif (mark[ind][1] == g || (f.B == 1 && g == 2)) z.B.B = (z.B.B + x.B.A * y.B.B) % MOD;\n\tif (mark[ind][2] == g || (f.B == 2 && g == 2)) z.B.B = (z.B.B + x.B.B * y.A.B) % MOD;\n\tif (mark[ind][3] == g || (f.B == 3 && g == 2)) z.B.B = (z.B.B + x.B.B * y.B.B) % MOD;\n\treturn z;\n}\nvoid build(int id, int l, int r){\n\tif (r - l == 1){\n\t\tseg[id].A.A = inv * (100 - p[l]) % MOD;\n\t\tseg[id].B.B = inv * p[l] % MOD;\n\t\treturn;\n\t}\n\tint mid = l + r >> 1;\n\tbuild(id << 1, l, mid);\n\tbuild(id << 1 | 1, mid, r);\n\tseg[id] = merge(seg[id << 1], seg[id << 1 | 1], {-1, -1}, mid - 1);\n}\nvoid update(int id, int l, int r, int pos, int f){\n\tint mid = l + r >> 1;\n\tif (pos < mid - 1) update(id << 1, l, mid, pos, f);\n\tif (pos > mid - 1) update(id << 1 | 1, mid, r, pos, f);\n\tseg[id] = merge(seg[id << 1], seg[id << 1 | 1], {pos, f}, mid - 1);\n}\nppp get(int id, int l, int r, int ql, int qr){\n\tif (ql <= l && r <= qr) return seg[id];\n\tif (qr <= l || r <= ql) return {{-1, -1}, {-1, -1}};\n\tint mid = l + r >> 1;\n\treturn merge(get(id << 1, l, mid, ql, qr), get(id << 1 | 1, mid, r, ql, qr), {-1, -1}, mid - 1);\n}\n\nint main(){\n InTheNameOfGod;\n \n cin >> n;\n for (int i = 0; i < n; ++ i) cin >> num[i] >> v[i] >> p[i];\n for (int i = 0; i < n - 1; ++ i){\n \tll x = num[i + 1] - num[i];\n \tll y = v[i] + v[i + 1];\n \tvec.push_back({{x, y}, {i, 2}});\n \tif (v[i] > v[i + 1]){\n \t\ty = v[i] - v[i + 1];\n \t\tvec.push_back({{x, y}, {i, 3}});\n\t\t}\n\t\tif (v[i] < v[i + 1]){\n\t\t\ty = v[i + 1] - v[i];\n\t\t\tvec.push_back({{x, y}, {i, 0}});\n\t\t}\n\t}\n\tbuild(1, 0, n);\n\tsort(all(vec), CMP);\n\tfor (ppp b : vec){\n\t\tll Time = b.A.A * power(b.A.B, MOD - 2, MOD) % MOD;\n\t\tppp x = get(1, 0, n, 0, b.B.A + 1);\n\t\tppp y = get(1, 0, n, b.B.A + 1, n);\n\t\tppp z = merge(x, y, {b.B.A, b.B.B}, b.B.A, 2);\n\t\tsum = 0;\n\t\tsum = (sum + z.A.A) % MOD;\n\t\tsum = (sum + z.A.B) % MOD;\n\t\tsum = (sum + z.B.A) % MOD;\n\t\tsum = (sum + z.B.B) % MOD;\n\t\tans = (ans + sum * Time % MOD) % MOD;\n\t\tmark[b.B.A][b.B.B] = true;\n\t\tupdate(1, 0, n, b.B.A, b.B.B);\n\t}\n\tcout << ans << endl;\n \n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "ee17ad153fa4b2894a08318db3fd1647", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \nusing namespace std;\nusing ll = long long;\nconst int MOD = 998244353;\n\nclass SegTree {\n\tprivate:\n\t\tstruct El {\n\t\t\tint p[2][2]; // Probability that leftmost is L/R and rightmost is L/R\n\t\t\tEl() {\n\t\t\t\tp[0][0] = 0;\n\t\t\t\tp[0][1] = 0;\n\t\t\t\tp[1][0] = 0;\n\t\t\t\tp[1][1] = 0;\n\t\t\t}\n\t\t\tstatic El id() {\n\t\t\t\tEl res;\n\t\t\t\tres.p[0][0] = 1;\n\t\t\t\tres.p[1][1] = 1;\n\t\t\t\treturn res;\n\t\t\t}\n\t\t};\n\t\tstatic El combine(const El& le, const El& ri) {\n\t\t\tEl res;\n\t\t\tfor (int a = 0; a < 2; ++a) {\n\t\t\t\tfor (int b = 0; b < 2; ++b) {\n\t\t\t\t\tfor (int i = 0; i < 2; ++i) {\n\t\t\t\t\t\tres.p[a][b] = (res.p[a][b] + (ll)le.p[a][i] * ri.p[i][b]) % MOD;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\t\tvoid update(int i) {\n\t\t\tfor (i >>= 1; i > 0; i >>= 1) seg[i] = combine(seg[2*i], seg[2*i+1]);\n\t\t}\n\n\t\tvector seg;\n\t\tint h = 1;\n\tpublic:\n\t\tSegTree(int n) {\n\t\t\twhile(h < n) h *= 2;\n\t\t\tseg.resize(2*h, El::id());\n\t\t}\n\t\t\n\t\tint prob(int i, int a, int b) {\n\t\t\ti += h;\n\t\t\tEl backup = seg[i];\n\t\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\t\tif (x != a || y != b) seg[i].p[x][y] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tupdate(i);\n\n\t\t\tll res = (seg[1].p[0][0] + seg[1].p[0][1]) % MOD;\n\n\t\t\tseg[i] = backup;\n\t\t\tupdate(i);\n\t\t\treturn res;\n\t\t}\n\t\tvoid setv(int i, int a, int b, int c, int d) {\n\t\t\ti += h;\n\t\t\tseg[i].p[0][0] = a;\n\t\t\tseg[i].p[0][1] = b;\n\t\t\tseg[i].p[1][0] = c;\n\t\t\tseg[i].p[1][1] = d;\n\t\t\tupdate(i);\n\t\t}\n\t\tvoid zero(int i, int a, int b) {\n\t\t\ti += h;\n\t\t\tseg[i].p[a][b] = 0;\n\t\t\tupdate(i);\n\t\t}\n};\n\nll modPow(ll a, ll b) {\n\tif (b & 1) return a * modPow(a, b-1) % MOD;\n\tif (b > 0) return modPow(a*a % MOD, b / 2);\n\treturn 1;\n}\n\nstruct Atom {\n\tint xi, vi, pi;\n};\n\nstruct Rat {\n\tll a, b;\n\n\t// a / b < rhs.a / rhs.b?\n\tbool operator<(const Rat& rhs) const {\n\t\treturn a * rhs.b < b * rhs.a;\n\t}\n\tll modv() const { return a * modPow(b, MOD-2) % MOD; }\n};\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tint n;\n\tcin >> n;\n\n\tSegTree seg(n);\n\n\tvector atoms(n);\n\tfor (auto& v : atoms) cin >> v.xi >> v.vi >> v.pi;\n\n\tconst ll HI = modPow(100, MOD - 2);\n\tfor (int i = 0; i < n; ++i) {\n\t\tint prob0 = (100 - atoms[i].pi) * HI % MOD;\n\t\tint prob1 = atoms[i].pi * HI % MOD;\n\t\tseg.setv(i, prob0, prob1, prob0, prob1);\n\t}\n\n\tvector>>> que;\n\tfor (int i = 0; i+1 < n; ++i) {\n\t\tll dist = atoms[i+1].xi - atoms[i].xi;\n\t\tfor (int a = 0; a < 2; ++a) {\n\t\t\tfor (int b = 0; b < 2; ++b) {\n\t\t\t\tll vel = (a ? 1 : -1) * atoms[i].vi + (b ? -1 : 1) * atoms[i+1].vi;\n\t\t\t\tRat r = {dist, vel};\n\t\t\t\tif (vel > 0) que.push_back({r, {i, {a, b}}}); // Collision eventually happens\n\t\t\t}\n\t\t}\n\t}\n\tsort(que.begin(), que.end());\n\n\tll res = 0;\n\tfor (auto pr : que) {\n\t\tll t = pr.first.modv();\n\t\tint i = pr.second.first;\n\t\tint a = pr.second.second.first;\n\t\tint b = pr.second.second.second;\n\n\t\tll prob = seg.prob(i+1, a, b);\n\t\tres = (res + prob * t) % MOD;\n\t\tseg.zero(i+1, a, b);\n\n\t\t// cerr << \"event \" << i << ' ' << a << ' ' << b << \": \" << t << ' ' << prob << '\\n';\n\t}\n\tcout << res << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "75db66aeea384742775006de070dacf1", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ls ch[x][0]\n#define rs ch[x][1]\n#define ll long long\n#define int ll\nusing namespace std;\n\ninline ll read(){\n\tll num=0,neg=1;char c=getchar();\n\twhile(!isdigit(c)){if(c=='-')neg=-1;c=getchar();}\n\twhile(isdigit(c)){num=(num<<3)+(num<<1)+c-'0';c=getchar();}\n\treturn num*neg;\n}\n\nconst ll mod=998244353;\nstruct mat{\n\tint a[2][2];\n\tmat(){for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) a[i][j]=0;}\n}tree[200010<<2];\n\nconst ll INF=0x7f7f7f7f7f7f7f;\nstruct node{\n\tint pos,p1,p2,val;\n\tdouble t;\n}side[800010];int cnt;\nll ans,x[200010],v[200010],p[200010],n;\n\nbool cmp1(node x,node y){return x.t>=1;\n\t}return res;\n}\ninline mat mul(mat x,mat y){\n\tmat res;\n\tfor(int i=0;i<=1;i++)\n\tfor(int j=0;j<=1;j++)\n\tfor(int k=0;k<=1;k++)\n\t\tres.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;\n\treturn res;\n}\ninline void insert(int pos,int p1,int p2){\n\tside[++cnt].pos=pos;\n\tside[cnt].p1=p1,side[cnt].p2=p2;\n\tif(p1==0&&p2==0){\n\t\tint tmp=v[pos]-v[pos-1];\n\t\tif(tmp>0) side[cnt].val=(x[pos]-x[pos-1])*qpow(tmp,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/tmp;\n\t\telse side[cnt].val=INF,side[cnt].t=1e20;\n\t}\n\tif(p1==0&&p2==1) side[cnt].val=INF,side[cnt].t=1e20;\n\tif(p1==1&&p2==0) side[cnt].val=(x[pos]-x[pos-1])%mod*qpow((v[pos]+v[pos-1])%mod,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/(v[pos-1]+v[pos]);\n\tif(p1==1&&p2==1){\n\t\tint tmp=v[pos-1]-v[pos];\n\t\tif(tmp>0) side[cnt].val=(x[pos]-x[pos-1])*qpow(tmp,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/tmp;\n\t\telse side[cnt].val=INF,side[cnt].t=1e20;\n\t}\n}\ninline void push_up(int x){tree[x]=mul(tree[x<<1],tree[x<<1|1]);}\ninline void build(int l,int r,int x){\n\tif(l==r){\n\t\ttree[x].a[0][0]=tree[x].a[1][0]=(1-p[l]+mod)%mod;\n\t\ttree[x].a[0][1]=tree[x].a[1][1]=p[l];\n\t\treturn;\n\t}int mid=l+r>>1;\n\tbuild(l,mid,x<<1),build(mid+1,r,x<<1|1);\n\tpush_up(x);return;\n}\ninline void update(int u,int p1,int p2,int l,int r,int x){\n\tif(l==r){tree[x].a[p1][p2]=0;return;}\n\tint mid=l+r>>1;\n\tif(u<=mid) update(u,p1,p2,l,mid,x<<1);\n\telse update(u,p1,p2,mid+1,r,x<<1|1);\n\tpush_up(x);return;\n}\ninline mat query(int ql,int qr,int l,int r,int x){\n\tif(ql>qr){\n\t\tmat res;res.a[0][0]=res.a[1][1]=1;\n\t\treturn res;\n\t}\n\tif(ql<=l&&qr>=r) return tree[x];\n\tint mid=l+r>>1;mat res;res.a[0][0]=res.a[1][1]=1;\n\tif(ql<=mid) res=mul(res,query(ql,qr,l,mid,x<<1));\n\tif(qr>mid) res=mul(res,query(ql,qr,mid+1,r,x<<1|1));\n\treturn res;\n}\n\nsigned main(){\n\tn=read();\n\tfor(int i=1;i<=n;i++) x[i]=read(),v[i]=read(),p[i]=read()*qpow(100ll,mod-2)%mod;\t\n\tbuild(1,n,1);\n\tfor(int i=2;i<=n;i++) for(int j=0;j<=1;j++) for(int k=0;k<=1;k++) insert(i,j,k);\n\tsort(side+1,side+cnt+1,cmp1);\n\t\n\t\n\tfor(int i=1;i<=cnt;i++){\n\t\tif(side[i].val==INF) break;\n\t\tmat trans;\n\t\ttrans.a[side[i].p1][side[i].p2]=(side[i].p2?p[side[i].pos]:(1-p[side[i].pos]+mod)%mod);\n\t\ttrans=mul(query(1,side[i].pos-1,1,n,1),trans);\n\t\ttrans=mul(trans,query(side[i].pos+1,n,1,n,1));\n\t\tans+=(trans.a[0][0]+trans.a[0][1])%mod*side[i].val%mod;ans%=mod;\n\t\tupdate(side[i].pos,side[i].p1,side[i].p2,1,n,1);\n\t}printf(\"%lld\\n\",ans);return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "87178d6bdbcddef758c3858fefcf6991", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\nstruct Element{\n int p,mask;\n long long uv, dv;\n Element(int p=0,int mask=0,long long uv=0,long long dv=0):\n p(p),mask(mask),uv(uv),dv(dv){}\n bool operator < (const Element& other)const{\n return uv*other.dv=mod) res.value[next] -= mod;\n }\n }\n return res;\n}\n\nvoid build(int p,int l,int r){\n t[p].l = l, t[p].r = r;\n if(l==r){\n t[p].value[1] = t[p].value[2] = 0;\n t[p].value[0] = prob[l][0];\n t[p].value[3] = prob[l][1];\n }\n else{\n int m = (l+r)/2;\n build(2*p,l,m);\n build(2*p+1,m+1,r);\n t[p] = merge(t[2*p],t[2*p+1]);\n }\n}\n\nvoid change(int p,int x){\n if(t[p].l==t[p].r) return ;\n else{\n int m = (t[p].l+t[p].r)/2;\n if(x<=m) change(2*p,x);\n if(x+1>m) change(2*p+1,x);\n t[p] = merge(t[2*p],t[2*p+1]);\n }\n}\n\nint main(){\n int n;\n scanf(\"%d\",&n);\n long long inv100 = inv(100ll);\n for(int i = 1; i <= n; i++){\n scanf(\"%lld%lld%lld\",&x[i],&v[i],&p[i]);\n prob[i][0] = (100ll-p[i])*inv100%mod;\n prob[i][1] = p[i]*inv100%mod;\n for(int j = 0; j < 4; j++) need[i][j] = 1;\n }\n build(1,1,n);\n //printf(\"res = %lld\\n\",res);\n int size = 0;\n for(int i = 1; i <= n-1; i++){\n int j = i+1;\n long long dis = x[j]-x[i];\n //i is 2, j is 1\n //0 is left, 1 is right\n for(int mask = 0; mask < 4; mask++){\n long long vi = v[i], vj = v[j];\n if(mask&2) vi *= -1;\n if(mask&1) vj *= -1;\n long long diffv = vj-vi;\n if(diffv>0){\n long long g = gcd(dis,diffv);\n meet[++size] = Element(i,mask,dis/g,diffv/g);\n }\n }\n }\n std::sort(meet+1,meet+1+size);\n long long ans = 0, curProb = 1;\n int p = 1;\n while(p<=size){\n int np = p;\n while(np+1<=size && meet[np+1].uv==meet[p].uv && meet[np+1].dv==meet[p].dv) np++;\n long long lastProb = curProb;\n for(int i = p; i <= np; i++){\n need[meet[i].p][meet[i].mask] = 0;\n change(1,meet[i].p);\n }\n curProb = 0;\n for(int i = 0; i < 4; i++){ \n curProb += t[1].value[i];\n if(curProb>=mod) curProb -= mod;\n }\n long long midProb = lastProb-curProb;\n if(midProb<0) midProb += mod;\n long long time = meet[p].uv*inv(meet[p].dv)%mod;\n ans += midProb*time%mod;\n if(ans>=mod) ans -= mod;\n p = np+1;\n }\n printf(\"%lld\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "56295a5ae0de18ca22525255f705169a", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define x first\n#define y second\n#define y0 Y0\n#define y1 Y1\n#define mp make_pair\n#define pb push_back\n#define DEBUG(x) cout << #x << \" = \" << x << endl;\n\nusing namespace std;\n\ntypedef long long LL;\ntypedef long double LD;\ntypedef pair pii;\n\ntemplate inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }\ntemplate inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }\ntemplate inline T read ()\n{\n\tT sum = 0, fl = 1; char ch = getchar();\n\tfor (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;\n\tfor (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';\n\treturn sum * fl;\n}\n\ninline void proc_status ()\n{\n\tifstream t (\"/proc/self/status\");\n\tcerr << string (istreambuf_iterator (t), istreambuf_iterator ()) << endl;\n}\n\nconst int MAXN = 1e5;\nconst int MOD = 998244353;\n\nnamespace MATH\n{\n\tinline void ADD (int &a, int b) { if ((a += b) >= MOD) a -= MOD; }\n\n\tinline int Pow (int a, int b)\n\t{\n\t\tint ans = 1;\n\t\tfor (int i = b; i; i >>= 1, a = (LL) a * a % MOD) if (i & 1) ans = (LL) ans * a % MOD;\n\t\treturn ans;\n\t}\n}\n\nusing namespace MATH;\n\nint N;\nstruct info\n{\n\tint x, v, p;\n\t\n\tinline void read () { x = ::read(), v = ::read(), p = (LL) ::read() * Pow (100, MOD - 2) % MOD; }\n\n} A[MAXN + 5];\n\nstruct matrix // 右1左0\n{\n\tint A[2][2];\n\n\tinline matrix () { memset (A, 0, sizeof A); }\n\n\tinline int* operator [] (const int &x) { return A[x]; }\n\n\tinline matrix operator * (const matrix &rhs) const\n\t{\n\t\tmatrix now;\n\t\tfor (int i = 0; i < 2; ++i)\n\t\t\tfor (int k = 0; k < 2; ++k) if (A[i][k])\n\t\t\t\tfor (int j = 0; j < 2; ++j)\n\t\t\t\t\tADD (now.A[i][j], (LL) A[i][k] * rhs.A[k][j] % MOD);\n\t\treturn now;\n\t}\n};\n\nnamespace SEG\n{\n#define mid ((l + r) >> 1)\n#define ls o << 1\n#define rs o << 1 | 1\n#define lson ls, l, mid\n#define rson rs, mid + 1, r\n\tconst int MAX_NODE = MAXN * 4;\n\n\tmatrix node[MAX_NODE + 5];\n\n\tinline void push_up (int o) { node[o] = node[ls] * node[rs]; }\n\n\tinline void build (int o, int l, int r)\n\t{\n\t\tif (l == r)\n\t\t{\n\t\t\tint p = A[l].p;\n\t\t\tnode[o][0][0] = node[o][1][0] = (1 - p + MOD) % MOD;\n\t\t\tnode[o][0][1] = node[o][1][1] = p;\n\t\t\treturn ;\n\t\t}\n\t\tbuild (lson), build (rson);\n\t\tpush_up (o);\n\t}\n\n\tinline void modify (int o, int l, int r, int x, int s0, int s1)\n\t{\n\t\tif (l == r) { node[o][s0][s1] = 0; return ; }\n\t\tif (x <= mid) modify (lson, x, s0, s1);\n\t\telse modify (rson, x, s0, s1);\n\t\tpush_up (o);\n\t}\n\n\tinline int query () { return (node[1][0][0] + node[1][0][1]) % MOD; }\n\n#undef mid\n}\n\nstruct point\n{\n\tint x, s0, s1, t;\n\tLD tt;\n\n\tinline point (int _x = 0, int _s0 = 0, int _s1 = 0, int _t = 0, LD _tt = 0) { x = _x, s0 = _s0, s1 = _s1, t = _t, tt = _tt; }\n};\n\ninline int cmp_t (point a, point b) { return a.tt < b.tt; }\n\nvector vec;\n\ninline void Init ()\n{\n\tfor (int i = 1; i < N; ++i)\n\t{\n\t\tinfo l = A[i], r = A[i + 1];\n\t\tif (l.p != 0 && r.p != 1) \n\t\t\tvec.pb (point (i + 1, 1, 0, (LL) (r.x - l.x) * Pow (l.v + r.v, MOD - 2) % MOD, (LD) (r.x - l.x) / (l.v + r.v)));\n\t\tif (l.p != 0 && r.p != 0 && l.v > r.v) \n\t\t\tvec.pb (point (i + 1, 1, 1, (LL) (r.x - l.x) * Pow (l.v - r.v, MOD - 2) % MOD, (LD) (r.x - l.x) / (l.v - r.v)));\n\t\tif (l.p != 1 && r.p != 1 && l.v < r.v) \n\t\t\tvec.pb (point (i + 1, 0, 0, (LL) (r.x - l.x) * Pow (r.v - l.v, MOD - 2) % MOD, (LD) (r.x - l.x) / (r.v - l.v)));\n\t}\n\n\tsort (vec.begin(), vec.end(), cmp_t);\n\n\tSEG :: build (1, 1, N);\n}\n\ninline void Solve ()\n{\n\tif (N == 1) return void (puts(\"0\"));\n\tInit ();\n\n\tint ans = 0, lst = 1;\n\tfor (int i = 0; i < vec.size(); ++i)\n\t{\n\t\tint x = vec[i].x, s0 = vec[i].s0, s1 = vec[i].s1, t = vec[i].t;\n\t\tSEG :: modify (1, 1, N, x, s0, s1);\n\t\tint p = (lst - SEG :: query () + MOD) % MOD;\n\t\tADD (ans, (LL) p * t % MOD);\n\t\tlst = SEG :: query ();\n\t}\n\n\tcout << ans << endl;\n}\n\ninline void Input ()\n{\n\tN = read();\n\tfor (int i = 1; i <= N; ++i) A[i].read();\n}\n\nint main ()\n{\n\n#ifdef hk_cnyali\n\tfreopen(\"F.in\", \"r\", stdin);\n\tfreopen(\"F.out\", \"w\", stdout);\n#endif\n\n\tInput ();\n\tSolve ();\n\n\treturn 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "302fa3667ad70ead79adcc2f1008727a", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define mz 998244353\nusing namespace std;\n\nstruct Mat\n{\n long long num[2][2];\n Mat ()\n {\n num[0][0]=num[0][1]=num[1][0]=num[1][1]=1;\n }\n Mat (long long a)\n {\n num[0][0]=num[0][1]=a;\n num[1][0]=0;\n num[1][1]=(1+mz-a)%mz;\n }\n Mat friend operator *(Mat x1,Mat x2)\n {\n Mat x3;\n x3.num[0][0]=(x1.num[0][0]*x2.num[0][0]+x1.num[0][1]*x2.num[1][0])%mz;\n x3.num[0][1]=(x1.num[0][0]*x2.num[0][1]+x1.num[0][1]*x2.num[1][1])%mz;\n x3.num[1][0]=(x1.num[1][0]*x2.num[0][0]+x1.num[1][1]*x2.num[1][0])%mz;\n x3.num[1][1]=(x1.num[1][0]*x2.num[0][1]+x1.num[1][1]*x2.num[1][1])%mz;\n return x3;\n }\n}mp;\nstruct Point\n{\n long long v,x;\n int no,k;\n bool friend operator <(Point x1,Point x2)\n {\n if(x1.x*x2.v==x2.x*x1.v)\n return x1.no(x2.x*x1.v);\n }\n}pp;\nstruct Node\n{\n int l,r,mid;\n\tMat m;\n}tre[400005];\nlong long p[100005],x[100005],v[100005],ni;\npriority_queue pq;\n\nlong long ksm(long long x)\n{\n long long z=1,y=mz-2;\n while(y)\n {\n if(y%2)\n z=(z*x)%mz;\n y>>=1;\n x=(x*x)%mz;\n }\n return z;\n}\nvoid build(int x,int l,int r)\n{\n\ttre[x].l=l;\n\ttre[x].r=r;\n\ttre[x].mid=(l+r)/2;\n\tif(l==r)\n\t{\n\t\ttre[x].m.num[0][0]=tre[x].m.num[0][1]=(1+mz-p[l])%mz;\n\t\ttre[x].m.num[1][0]=tre[x].m.num[1][1]=p[l];\n\t}\n\telse\n\t{\n\t\tbuild(x*2,l,tre[x].mid);\n\t\tbuild(x*2+1,tre[x].mid+1,r);\n\t\ttre[x].m=tre[x*2].m*tre[x*2+1].m;\n\t}\n}\nMat ask(int x,int l,int r)\n{\n\tif(tre[x].l==l && r==tre[x].r)\n\t\treturn tre[x].m;\n\telse if(r<=tre[x].mid)\n\t\treturn ask(x*2,l,r);\n\telse if(l>tre[x].mid)\n\t\treturn ask(x*2+1,l,r);\n\treturn ask(x*2,l,tre[x].mid)*ask(x*2+1,tre[x].mid+1,r);\n}\nvoid change(int x,int y,int k)\n{\n\tif(tre[x].l==tre[x].r)\n\t{\n\t\tif(k==1)\n\t\t\ttre[x].m.num[1][0]=0;\n\t\tif(k==2)\n\t\t\ttre[x].m.num[0][0]=0;\n\t\tif(k==3)\n\t\t\ttre[x].m.num[1][1]=0;\n\t}\n\telse\n\t{\n\t\tif(y<=tre[x].mid)\n\t\t\tchange(x*2,y,k);\n\t\telse\n\t\t\tchange(x*2+1,y,k);\n\t\ttre[x].m=tre[x*2].m*tre[x*2+1].m;\n\t}\n}\n\nint main()\n{\n int n;\n ni=ksm(100);\n long long ans=0,tp,pl,pr,ppl,ppr;\n scanf(\"%d\",&n);\n scanf(\"%lld%lld%lld\",&x[1],&v[1],&p[1]);\n p[1]=(p[1]*ni)%mz;\n for(int i=1;iv[i+1])\n {\n \tpp.k=3;\n\t pp.v=v[i]-v[i+1];\n\t pq.push(pp);\n\t\t}\n }\n build(1,0,n);\n while(!pq.empty())\n {\n \tpp=pq.top();\n \tpq.pop();\n \ttp=(pp.x*ksm(pp.v))%mz;\n \tmp=ask(1,pp.no+1,n);\n \tpl=mp.num[0][0];\n \tpr=mp.num[1][0];\n \tppl=ppr=0;\n \tif(pp.k==1)\n \t\tppr=(pl*p[pp.no])%mz;\n\t\tif(pp.k==2)\n \t\tppl=(pl*(1+mz-p[pp.no]))%mz;\n\t\tif(pp.k==3)\n\t\t\tppr=(pr*p[pp.no])%mz;\n \tmp=ask(1,0,pp.no-1);\n \tpl=(ppl*mp.num[0][0]+ppr*mp.num[0][1])%mz;\n \tpr=(ppl*mp.num[1][0]+ppr*mp.num[1][1])%mz;\n \tans=(ans+tp*(pl+pr))%mz;\n \tchange(1,pp.no,pp.k);\n\t}\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "7be0556d783ec336fe8b00c6fbb2568e", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define N 100000 + 5\n#define MOD 998244353\n#define inf 0x7fffffff\nusing namespace std;\nstruct P\n{\n int id, x1, x2, x, v;\n};\nstruct Matrix\n{\n int op[3][3];\n void toE()\n {\n op[1][1] = op[2][2] = 1, op[1][2] = op[2][1] = 0;\n }\n void operator*=(const Matrix& s)\n {\n int tmp[3][3];\n tmp[1][1] = tmp[1][2] = tmp[2][1] = tmp[2][2] = 0;\n for (int i = 1; i <= 2; i++) {\n for (int j = 1; j <= 2; j++) {\n for (int k = 1; k <= 2; k++) tmp[i][j] = (tmp[i][j] + 1ll * op[i][k] * s.op[k][j]) % MOD;\n }\n }\n op[1][1] = tmp[1][1], op[1][2] = tmp[1][2], op[2][1] = tmp[2][1], op[2][2] = tmp[2][2];\n }\n} nd[N << 2], t1, t2, t3, t4;\nvector

vec;\nint n, x[N], v[N], p[N], ans;\ninline int inv(int x)\n{\n int ans = 1, sta = x, y = MOD - 2;\n while (y) {\n if (y & 1) ans = 1ll * ans * sta % MOD;\n sta = 1ll * sta * sta % MOD, y >>= 1;\n }\n return ans;\n}\nvoid getTime(int a, int b, int id, int& xx, int& vv)\n{\n int t = 0;\n if (id == 0) {\n if (v[a] > v[b])\n xx = (x[b] - x[a]), vv = v[a] - v[b];\n else\n xx = inf;\n }\n else if (id == 3) {\n if (v[b] > v[a])\n xx = x[b] - x[a], vv = v[b] - v[a];\n else\n xx = inf;\n }\n else if (id == 1) {\n xx = x[b] - x[a], vv = v[a] + v[b];\n }\n else\n xx = inf;\n}\nvoid build(int l = 1, int r = n, int k = 1)\n{\n if (l == r) {\n nd[k].op[1][1] = nd[k].op[2][1] = 1ll * (100 - p[l]) * inv(100) % MOD;\n nd[k].op[1][2] = nd[k].op[2][2] = 1ll * p[l] * inv(100) % MOD;\n return;\n }\n int mid = (l + r) >> 1;\n build(l, mid, k << 1), build(mid + 1, r, k << 1 | 1);\n nd[k].toE(), nd[k] *= nd[k << 1], nd[k] *= nd[k << 1 | 1];\n}\nvoid change(int at, const Matrix& s, int l = 1, int r = n, int k = 1)\n{\n if (l == r) {\n nd[k] = s;\n return;\n }\n int mid = (l + r) >> 1;\n if (at <= mid)\n change(at, s, l, mid, k << 1);\n else\n change(at, s, mid + 1, r, k << 1 | 1);\n nd[k].toE(), nd[k] *= nd[k << 1], nd[k] *= nd[k << 1 | 1];\n}\nconst Matrix& getMatrix(int at, int l = 1, int r = n, int k = 1)\n{\n if (l == r) return nd[k];\n int mid = (l + r) >> 1;\n if (at <= mid) return getMatrix(at, l, mid, k << 1);\n return getMatrix(at, mid + 1, r, k << 1 | 1);\n}\n\ninline int get()\n{\n return (nd[1].op[1][1] + nd[1].op[1][2]) % MOD;\n}\nint main()\n{\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) scanf(\"%d%d%d\", x + i, v + i, p + i);\n for (int i = 2; i <= n; i++) {\n P ps;\n ps.x1 = i - 1, ps.x2 = i;\n for (int j = 0, pp; j < 4; j++) {\n getTime(i - 1, i, j, ps.x, ps.v);\n if (ps.x != inf) ps.id = j, vec.push_back(ps);\n }\n }\n sort(vec.begin(), vec.end(), [](P a, P b) {\n return 1ll * a.x * b.v < 1ll * b.x * a.v;\n });\n build();\n for (P s : vec) {\n t3 = t1 = getMatrix(s.x1), t4 = t2 = getMatrix(s.x2);\n if (s.id & 1)\n t2.op[1][2] = t2.op[2][2] = 0;\n else\n t2.op[1][1] = t2.op[2][1] = 0;\n if (s.id & 2)\n t1.op[1][2] = t1.op[2][2] = 0;\n else\n t1.op[1][1] = t1.op[2][1] = 0;\n change(s.x1, t1), change(s.x2, t2);\n ans = (ans + 1ll * s.x * inv(s.v) % MOD * get()) % MOD;\n if (s.id == 0)\n t4.op[2][2] = 0;\n else if (s.id == 1)\n t4.op[2][1] = 0;\n else if (s.id == 3)\n t4.op[1][1] = 0;\n change(s.x1, t3), change(s.x2, t4);\n }\n printf(\"%d\", (ans + MOD) % MOD);\n // PAUSE;\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "ad2fecf5a6b908088c237e0449080930", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ls ch[x][0]\n#define rs ch[x][1]\n#define ll long long\n#define int ll\nusing namespace std;\n\ninline ll read(){\n\tll num=0,neg=1;char c=getchar();\n\twhile(!isdigit(c)){if(c=='-')neg=-1;c=getchar();}\n\twhile(isdigit(c)){num=(num<<3)+(num<<1)+c-'0';c=getchar();}\n\treturn num*neg;\n}\n\nconst ll mod=998244353;\nstruct mat{\n\tint a[2][2];\n\tmat(){for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) a[i][j]=0;}\n}tree[200010<<2];\n\nconst ll INF=0x7f7f7f7f7f7f7f;\nstruct node{\n\tint pos,p1,p2,val;\n\tdouble t;\n}side[800010];int cnt;\nll ans,x[200010],v[200010],p[200010],n;\n\nbool cmp1(node x,node y){return x.t>=1;\n\t}return res;\n}\ninline mat mul(mat x,mat y){\n\tmat res;\n\tfor(int i=0;i<=1;i++)\n\tfor(int j=0;j<=1;j++)\n\tfor(int k=0;k<=1;k++)\n\t\tres.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;\n\treturn res;\n}\ninline void insert(int pos,int p1,int p2){\n\tside[++cnt].pos=pos;\n\tside[cnt].p1=p1,side[cnt].p2=p2;\n\tif(p1==0&&p2==0){\n\t\tint tmp=v[pos]-v[pos-1];\n\t\tif(tmp>0) side[cnt].val=(x[pos]-x[pos-1])*qpow(tmp,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/tmp;\n\t\telse side[cnt].val=INF,side[cnt].t=1e20;\n\t}\n\tif(p1==0&&p2==1) side[cnt].val=INF,side[cnt].t=1e20;\n\tif(p1==1&&p2==0) side[cnt].val=(x[pos]-x[pos-1])%mod*qpow((v[pos]+v[pos-1])%mod,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/(v[pos-1]+v[pos]);\n\tif(p1==1&&p2==1){\n\t\tint tmp=v[pos-1]-v[pos];\n\t\tif(tmp>0) side[cnt].val=(x[pos]-x[pos-1])*qpow(tmp,mod-2)%mod,side[cnt].t=(x[pos]-x[pos-1])*1.0/tmp;\n\t\telse side[cnt].val=INF,side[cnt].t=1e20;\n\t}\n}\ninline void push_up(int x){tree[x]=mul(tree[x<<1],tree[x<<1|1]);}\ninline void build(int l,int r,int x){\n\tif(l==r){\n\t\ttree[x].a[0][0]=tree[x].a[1][0]=(1-p[l]+mod)%mod;\n\t\ttree[x].a[0][1]=tree[x].a[1][1]=p[l];\n\t\treturn;\n\t}int mid=l+r>>1;\n\tbuild(l,mid,x<<1),build(mid+1,r,x<<1|1);\n\tpush_up(x);return;\n}\ninline void update(int u,int p1,int p2,int l,int r,int x){\n\tif(l==r){tree[x].a[p1][p2]=0;return;}\n\tint mid=l+r>>1;\n\tif(u<=mid) update(u,p1,p2,l,mid,x<<1);\n\telse update(u,p1,p2,mid+1,r,x<<1|1);\n\tpush_up(x);return;\n}\ninline mat query(int ql,int qr,int l,int r,int x){\n\tif(ql>qr){\n\t\tmat res;res.a[0][0]=res.a[1][1]=1;\n\t\treturn res;\n\t}\n\tif(ql<=l&&qr>=r) return tree[x];\n\tint mid=l+r>>1;mat res;res.a[0][0]=res.a[1][1]=1;\n\tif(ql<=mid) res=mul(res,query(ql,qr,l,mid,x<<1));\n\tif(qr>mid) res=mul(res,query(ql,qr,mid+1,r,x<<1|1));\n\treturn res;\n}\n\nsigned main(){\n\tn=read();\n\tfor(int i=1;i<=n;i++) x[i]=read(),v[i]=read(),p[i]=read()*qpow(100ll,mod-2)%mod;\t\n\tbuild(1,n,1);\n\tfor(int i=2;i<=n;i++) for(int j=0;j<=1;j++) for(int k=0;k<=1;k++) insert(i,j,k);\n\tsort(side+1,side+cnt+1,cmp1);\n\t\n\t\n\tfor(int i=1;i<=cnt;i++){\n\t\tif(side[i].val==INF) break;\n\t\tmat trans;\n\t\ttrans.a[side[i].p1][side[i].p2]=(side[i].p2?p[side[i].pos]:(1-p[side[i].pos]+mod)%mod);\n\t\ttrans=mul(query(1,side[i].pos-1,1,n,1),trans);\n\t\ttrans=mul(trans,query(side[i].pos+1,n,1,n,1));\n\t\tans+=(trans.a[0][0]+trans.a[0][1])*side[i].val%mod;ans%=mod;\n\t\tupdate(side[i].pos,side[i].p1,side[i].p2,1,n,1);\n\t}printf(\"%lld\\n\",ans);return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "3f06dd79c26d86e1574d9ecdfa3ee046", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \nusing namespace std;\nconst int N=1e5+1,MOD=998244353;\nint lim[N][2][2],p[N<<2][2][2],psb[N][2];\ninline int qpow(int base,int expo)\n{\n\tint ret=1;\n\twhile(expo)\n\t{\n\t\tif(expo&1) ret=1ll*ret*base%MOD;\n\t\tbase=1ll*base*base%MOD;\n\t\texpo>>=1;\n\t}\n\treturn ret;\n}\ninline void up(int now,int mid)\n{\n\tint i,j,k,l;\n\tfor(i=0;i<2;i++)\n\t{\n\t\tfor(j=0;j<2;j++)\n\t\t{\n\t\t\tp[now][i][j]=0;\n\t\t\tfor(k=0;k<2;k++)\n\t\t\t\tfor(l=0;l<2;l++)\n\t\t\t\t\tif(!lim[mid][k][l])\n\t\t\t\t\t\tp[now][i][j]=(0ll+p[now][i][j]+1ll*p[now<<1][i][k]*p[now<<1|1][l][j]%MOD)%MOD;\n\t\t}\n\t}\n}\nvoid build(int now,int l,int r)\n{\n\tif(l==r)\n\t{\n\t\tp[now][0][0]=psb[l][0];\n\t\tp[now][1][1]=psb[l][1];\n\t\treturn;\n\t}\n\tint mid=(l+r)>>1;\n\tbuild(now<<1,l,mid);\n\tbuild(now<<1|1,mid+1,r);\n\tup(now,mid);\n}\nvoid update(int now,int l,int r,int qp)\n{\n\tif(l==r) return;\n\tint mid=(l+r)>>1;\n\tif(qp<=mid) update(now<<1,l,mid,qp);\n\telse update(now<<1|1,mid+1,r,qp);\n\tup(now,mid);\n}\nstruct chase\n{\n\tint x,xp,yp;\n\tdouble tim;\n\tint rtim;\n\tchase(int x=0,int xp=0,int yp=0,double tim=0,int rtim=0):\n\t\tx(x),xp(xp),yp(yp),tim(tim),rtim(rtim){}\n} cs[N*2];\nint csc;\ninline int cmp(chase a,chase b){return a.tim1)\n\t\t{\n\t\t\tint dis=tpos-lasp;\n\t\t\tcs[++csc]=chase(i-1,1,0,dis/1.0/(tv+lasv),1ll*dis*qpow(tv+lasv,MOD-2)%MOD);\n\t\t\tif(tvlasv) cs[++csc]=chase(i-1,0,0,dis/1.0/(tv-lasv),1ll*dis*qpow(tv-lasv,MOD-2)%MOD);\n\t\t}\n\t\tpsb[i][0]=1ll*(100-psb[i][1])*inv%MOD;\n\t\tpsb[i][1]=1ll*psb[i][1]*inv%MOD;\n\t\tlasp=tpos,lasv=tv;\n\t}\n\tbuild(1,1,n);\n\tsort(cs+1,cs+csc+1,cmp);\n\tint ans=0,lpsb=1;\n\tfor(i=1;i<=csc;i++)\n\t{\n\t\tlim[cs[i].x][cs[i].xp][cs[i].yp]=1;\n\t\tupdate(1,1,n,cs[i].x);\n\t\tint tpsb=(0ll+p[1][0][0]+p[1][0][1]+p[1][1][0]+p[1][1][1])%MOD;\n\t\tans=(0ll+ans+1ll*cs[i].rtim*(lpsb-tpsb+MOD)%MOD)%MOD;\n\t\tlpsb=tpsb;\n\t}\n\tprintf(\"%d\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "fee120a7d5443c0e00843b4fa67563cc", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \nusing namespace std;\nconst int MAXN=110000;\nconst int P=998244353;\nconst int INV100=828542813;\nvoid exgcd(int a, int b, int& x, int& y)\n{\n\tif (b==0) x=1, y=0;\n\telse exgcd(b, a%b, y, x), y-=a/b*x;\n}\ninline int inv(int a)\n{\n\tint x, y;\n\texgcd(a, P, x, y);\n\treturn (x%P+P)%P;\n}\nstruct Matrix\n{\n\tint n, m, a[3][3];\n\tMatrix() {}\n\tMatrix(int r, int c): n(r), m(c)\n\t{\n\t\tmemset(a, 0, sizeof a);\n\t}\n\tMatrix operator * (const Matrix& b) const\n\t{\n\t\tMatrix c(n, b.m);\n\t\tfor (int i=0; i>1;\n\t\tbuild(lc, l, mid);\n\t\tbuild(rc, mid+1, r);\n\t\tpushup(o);\n\t}\n\tvoid update(int o, int p)\n\t{\n\t\tif (tr[o].l==tr[o].r)\n\t\t{\n\t\t\ttr[o].val=w[p];\n\t\t\treturn;\n\t\t}\n\t\tint mid=tr[o].l+tr[o].r>>1;\n\t\tif (p<=mid) update(lc, p);\n\t\telse update(rc, p);\n\t\tpushup(o);\n\t}\n\t#undef lc\n\t#undef rc\n} st;\nstruct Pipe\n{\n\tint x, v, p;\n\tPipe() {}\n\tPipe(int a, int b, int c): x(a), v(b), p(c) {}\n} a[MAXN];\nstruct Movement\n{\n\tint id, d0, d1;\n\tMovement() {}\n\tMovement(int a, int b, int c):\n\t\tid(a), d0(b), d1(c) {}\n\tbool operator < (const Movement& rhs) const\n\t{\n\t\treturn 1ll*(a[id].x-a[id-1].x)*(rhs.d0*a[rhs.id-1].v-rhs.d1*a[rhs.id].v)\n\t\t <1ll*(a[rhs.id].x-a[rhs.id-1].x)*(d0*a[id-1].v-d1*a[id].v);\n\t}\n\tvoid set(bool k)\n\t{\n\t\tif (d0==1)\n\t\t\tif (d1==1) w[id].a[0][0]=k*a[id].p;\n\t\t\telse w[id].a[0][1]=k*(1-a[id].p+P)%P;\n\t\telse\n\t\t\tif (d1==1) w[id].a[1][0]=k*a[id].p;\n\t\t\telse w[id].a[1][1]=k*(1-a[id].p+P)%P;\n\t}\n\tint meettime()\n\t{\n\t\treturn 1ll*(a[id].x-a[id-1].x)*inv(d0*a[id-1].v-d1*a[id].v)%P;\n\t}\n} b[4*MAXN];\nint main()\n{\n//\tfreopen(\"F.in\", \"r\", stdin);\n//\tfreopen(\"F.out\", \"w\", stdout);\n\tint n, m=0;\n\tscanf(\"%d\", &n);\n\ta[0]=Pipe(-(1E9+233), 1E6+233, 0);\n\tfor (int i=1; i<=n; i++)\n\t{\n\t\tint x, v, p;\n\t\tscanf(\"%d%d%d\", &x, &v, &p);\n\t\ta[i]=Pipe(x, v, 1ll*p*INV100%P);\n\t\tb[++m]=Movement(i, 1, -1);\n\t\tif (a[i].va[i-1].v) b[++m]=Movement(i, -1, -1);\n\t}\n\tfor (int i=1; i<=n; i++)\n\t{\n\t\tw[i]=Matrix(2, 2);\n\t\tw[i].a[0][0]=w[i].a[1][0]=a[i].p;\n\t\tw[i].a[0][1]=w[i].a[1][1]=(1-a[i].p+P)%P;\n\t}\n\tst.build(1, 1, n);\n\tsort(b+1, b+m+1);\n\tint ans=0;\n\tfor (int i=1; i<=m; i++)\n\t{\n\t\tMatrix t=w[b[i].id];\n\t\tw[b[i].id]=Matrix(2, 2); b[i].set(1);\n\t\tst.update(1, b[i].id);\n\t\tans=(ans+1ll*b[i].meettime()*(st.tr[1].val.a[1][0]+st.tr[1].val.a[1][1]))%P;\n\t\tw[b[i].id]=t; b[i].set(0);\n\t\tst.update(1, b[i].id);\t\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "probabilities", "data structures"], "code_uid": "3131dfa3f43db1da83dcd688124dd37b", "src_uid": "37bb4fe5f6cc2a173e97c033c6fde8c7", "difficulty": 3100, "exec_outcome": "PASSED"}

(m + 1));\n dp[0][0] = {1, 0};\n for (int l = 0; l <= n; l++) {\n for (int r = l + 1; r <= n + 1; r++) {\n int di = r - l - 1;\n for (int i = 0; i + di <= m; i++) {\n // dp[l][i] -> dp[r][i + di]\n mint z = dp[l][i].first * f[di].first;\n dp[r][i + di].first += z * C[i + di][di];\n dp[r][i + di].second += (dp[l][i].second * f[di].first + dp[l][i].first * f[di].second) * C[i + di][di];\n }\n }\n }\n\n dbg(dp[n + 1][m].first.val(), dp[n + 1][m].second.val());\n\n pr.writeln(dp[n + 1][m].second.val());\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "a0ffaa5f27bd87aa8ed3cd6269c6451c", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nint n,m,md,len,i,j,k,prv,c[505][505],dpf[505][505],dpr[505][505];\nlong long cur,crst,f[505],rst[505];\nlong long sum(long long x) {\n return (x*(x+1LL))/2;\n}\nint main() {\n scanf(\"%d%d%d\",&n,&m,&md);\n for (i=0; i<=n; i++) {\n c[i][0]=1;\n for (j=1; j<=i; j++) c[i][j]=(c[i-1][j]+c[i-1][j-1])%md;\n }\n for (i=0; i0) {\n crst=(crst*f[k-1]+cur*rst[k-1])%md;\n cur=(cur*f[k-1])%md;\n }\n if (k0) --prv;\n if (j-k<=prv) {\n dpr[i][j]=(dpr[i][j]+c[j][k]*((dpr[prv][j-k]*f[k-1]+dpf[prv][j-k]*rst[k-1])%md))%md;\n dpf[i][j]=(dpf[i][j]+c[j][k]*((dpf[prv][j-k]*f[k-1])%md))%md;\n }\n }\n }\n printf(\"%d\\n\",dpr[n][m]);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "caaddc52ac6b73e869bdff939587ea66", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nint min(int a,int b){\n\treturn a=0){\n\t\t\t\t\tf_3[i][j]=(f_3[i][j]+1ll*f_1[k]*f_3[i-1-k][j-k]%Mod*C[j][k])%Mod;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tf_3[i][j]=(f_3[i][j]+1ll*f_1[k]*C[j][k])%Mod;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=0;j<=min(i,m);j++){\n\t\t\tf_4[i][j]=f_4[i-1][j];\n\t\t\tfor(int k=1;k<=j;k++){\n\t\t\t\tif(i-1-k>=0){\n\t\t\t\t\tf_4[i][j]=(f_4[i][j]+(1ll*f_3[i-1-k][j-k]*f_2[k]+1ll*f_4[i-1-k][j-k]*f_1[k])%Mod*C[j][k])%Mod;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tf_4[i][j]=(f_4[i][j]+1ll*f_2[k]*C[j][k])%Mod;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",f_4[n][m]);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "c2ed32e3d0cc5bfeb6f50be5aa107838", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define rep(i,l,r) for(int i=(l);i<=(r);++i)\n#define per(i,r,l) for(int i=(r);i>=(l);--i)\nusing namespace std;\nconst int N=510;\nint n,m,P,f[N],g[N],C[N][N],p[N][N],q[N][N];\ninline void upd(int &x,int y){((x+=y)>=P)&&(x-=P);}\nint main(){\n\tscanf(\"%d%d%d\",&n,&m,&P);\n\tf[0]=1;\n\trep(i,0,m){C[i][0]=1;rep(j,1,i) C[i][j]=(C[i-1][j]+C[i-1][j-1])%P;}\n\trep(i,1,m) rep(j,1,i){\n\t\tupd(f[i],1ll*f[j-1]*f[i-j]%P*(i+1)%P*C[i-1][j-1]%P);\n\t\tupd(g[i],(1ll*f[j-1]*g[i-j]+1ll*f[i-j]*g[j-1])%P*(i+1)%P*C[i-1][j-1]%P);\n\t\tupd(g[i],1ll*(C[j][2]+C[i-j+1][2])*f[j-1]%P*f[i-j]%P*C[i-1][j-1]%P);\n\t}\n\tp[0][0]=1;\n\trep(i,1,n){\n\t\trep(j,0,min(i-1,m)) rep(k,0,j){\n\t\t\tupd(p[i][j],1ll*p[i-k-1][j-k]*f[k]%P*C[j][k]%P);\t\n\t\t\tupd(q[i][j],(1ll*p[i-k-1][j-k]*g[k]+1ll*q[i-k-1][j-k]*f[k])%P*C[j][k]%P);\n\t\t}\n\t\tif(i<=m) p[i][i]=f[i],q[i][i]=g[i];\n\t}\n\tprintf(\"%d\\n\",q[n][m]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "c91c070765f163f69c380be2e3a5c49b", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define rep(i,a,b) for(int i=(a);i<(b);++i)\n#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)\n#define ll long long\n#define lb(x) ((x)&-(x))\n#define pii pair\n#define vi vector\n#define pb push_back\n#define fi first\n#define se second\n#define de(x) cout<<#x<<\" x\"<=mod)a-=mod;\n\treturn a;\n} \ninline int mul(const int &a,const int &b){\n\treturn 1ll*a*b%mod;\n}\nint dp[N][N],dp2[N][N],C[N][N],f[N],g[N];\nint main(){\n\tint n,m;\n\tscanf(\"%d%d%d\",&n,&m,&mod);\n\trep(i,0,n+1){\n\t\tC[i][0]=1;\n\t\trep(j,0,i)C[i][j+1]=add(C[i-1][j+1],C[i-1][j]);\n\t}\n\tf[0]=1,g[0]=0;\n\trep(i,1,m+1){\n\t\trep(j,1,i+1){\n\t\t\tf[i]=add(f[i],mul(mul(C[i-1][j-1],i+1),mul(f[j-1],f[i-j])));\n\t\t\tg[i]=add(g[i],mul(mul(C[i-1][j-1],i+1),add(mul(f[j-1],g[i-j]),mul(g[j-1],f[i-j]))));\n\t\t\tg[i]=add(g[i],mul(mul(C[i-1][j-1],(j-1)*j/2+(i-j+1)*(i-j)/2),mul(f[j-1],f[i-j])));\n\t\t}\n\t}\n\t++n;\n\tdp[0][0]=1,dp2[0][0]=0;\n\trep(i,0,n){\n\t\trep(j,0,m+1){\n\t\t\tfor(int k=0;j+k<=m&&i+k+1<=n;++k){\n\t\t\t\tdp[i+k+1][j+k]=add(dp[i+k+1][j+k],mul(mul(dp[i][j],f[k]),C[j+k][j]));\n\t\t\t\tdp2[i+k+1][j+k]=add(dp2[i+k+1][j+k],mul(C[j+k][j],add(mul(dp2[i][j],f[k]),mul(dp[i][j],g[k])))); \n\t\t\t}\n\t\t}\n\t}\n\tcout<\nusing namespace std;\nusing ll = long long;\nusing pii = pair;\nusing vi = vector;\n\ntemplate ostream& operator<<(ostream &os, const vector &v) { os << '['; string sep = \"\"; for (const auto &x : v) os << sep << x, sep = \", \"; return os << ']'; }\ntemplate ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << \", \" << p.second << ')'; }\ntemplate ostream& operator<<(ostream &os, const set &s) { os << '{'; string sep = \"\"; for (const auto &x : s) os << sep << x, sep = \", \"; return os << '}'; }\ntemplate ostream& operator<<(ostream &os, const map &m) { os << '{'; string sep = \"\"; for (const auto &x : m) os << sep << x.first << \" -> \" << x.second, sep = \", \"; return os << '}'; }\n\n#ifdef MY_DEBUG_FLAG\nvoid debug() { cerr << endl; }\ntemplate void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << \", \"; debug(F...);}\n#define db(...) cerr << \"DEBUG (\" << #__VA_ARGS__ << \") == \", debug(__VA_ARGS__)\n#else\n#define db(...)\n#endif\n\n#define pb push_back\n#define eb emplace_back\n#define ff first\n#define ss second\n#define fast ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr)\n#define sz(a) ((int)(a).size())\n#define rep(i,a,b) for(int i=(a); i<(b); i++)\n#define dec(i,n,a) for(int i=(n); i>=(a); i--)\n#define clr(a,v) memset(a, v, sizeof(a))\n#define all(a) (a).begin(),(a).end()\n\nconstexpr int inf = 0x3f3f3f3f;\nconstexpr int maxn = 510;\nint mod;\n\n// m mod i = m - i*floor(m/i)\n// m mod i === - i*floor(m/i) mod m\n// i^-1 === m - floor(m/i)*(m mod i)^-1 mod m\n\nint fat[maxn], inv[maxn], inv_fat[maxn];\n\nvoid calc() {\n\tfat[0] = fat[1] = inv[1] = inv_fat[0] = inv_fat[1] = 1;\n\tfor(int i = 2; i < maxn; i++) {\n\t\tfat[i] = (int)(1ll * fat[i-1] * i % mod);\n\t\tinv[i] = mod - (int)(1ll * (mod/i) * inv[mod%i] % mod);\n\t\tinv_fat[i] = (int)(1ll * inv_fat[i-1] * inv[i] % mod);\n\t}\n}\n\ninline int choose(int a, int b) {\n\treturn (int)(1ll * fat[a] * (1ll * inv_fat[b] * inv_fat[a-b] % mod) % mod);\n}\n\ninline ll p(int x) { return 1ll * x * (x+1) / 2; }\n\nint f[maxn], c[maxn], dp1[maxn][maxn], dp2[maxn][maxn];\n\nint main() {\n\tint n, m; scanf(\"%d %d %d\", &n, &m, &mod);\n\tcalc();\n\tf[0] = 1;\n\tfor(int i = 1; i <= m; i++) {\n\t\tfor(int j = 0; j < i; j++) {\n\t\t\tf[i] += (int)((1ll * choose(i-1, j) * f[j] % mod) * f[i-1-j] % mod);\n\t\t\tif(f[i] >= mod) f[i] -= mod;\n\t\t\tc[i] += (int)((1ll * (i+1) * choose(i-1, j) % mod) *\n\t\t\t\t((1ll * f[j] * c[i-1-j] % mod) + (1ll * f[i-1-j] * c[j] % mod) % mod) % mod);\n\t\t\tif(c[i] >= mod) c[i] -= mod;\n\t\t\tc[i] += (int)((1ll * choose(i-1, j) * (p(j) + p(i-1-j)) % mod) *\n\t\t\t\t(1ll * f[j] * f[i-1-j] % mod) % mod);\n\t\t\tif(c[i] >= mod) c[i] -= mod;\n\t\t}\n\t\tf[i] = (int)(1ll * f[i] * (i+1) % mod);\n\t}\n\tdp1[0][0] = 1;\n\tfor(int k = 1; k <= n; k++) {\n\t\tdp1[k][0] = 1;\n\t\tfor(int i = 1; i <= min(m, k); i++) {\n\t\t\tdp1[k][i] = dp1[k-1][i];\n\t\t\tdp2[k][i] = dp2[k-1][i];\n\t\t\tfor(int j = 1; j <= i; j++) {\n\t\t\t\tdp1[k][i] += (int)((1ll * f[j] * dp1[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp1[k][i] >= mod) dp1[k][i] -= mod;\n\t\t\t\t\n\t\t\t\tdp2[k][i] += (int)((1ll * c[j] * dp1[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp2[k][i] >= mod) dp2[k][i] -= mod;\n\t\t\t\tdp2[k][i] += (int)((1ll * f[j] * dp2[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp2[k][i] >= mod) dp2[k][i] -= mod;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", dp2[n][m]);\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "de16758478717ffdd4f58dc4f58ff601", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n\nint N , M , num[503] , V[503] , C[503][503] , P , dp[503][503] , ans[503][503];\n\nsigned main(){\n\tcin >> N >> M >> P; num[0] = 1;\n\tfor(int i = 0 ; i <= M ; ++i){C[i][0] = 1; for(int j = 1 ; j <= i ; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P;}\n\t\n\tfor(int i = 1 ; i <= M ; ++i)\n\t\tfor(int j = 1 ; j <= i ; ++j){\n\t\t\tnum[i] = (num[i] + 1ll * num[j - 1] * num[i - j] % P * (i + 1) % P * C[i - 1][j - 1]) % P;\n\t\t\tint sum = 0; for(int k= 1 ; k <= i ; ++k) sum += abs(j - k);\n\t\t\tV[i] = (V[i] + 1ll * V[j - 1] * num[i - j] % P * (i + 1) % P * C[i - 1][j - 1] +\n\t\t\t\t\t1ll * V[i - j] * num[j - 1] % P * (i + 1) % P * C[i - 1][j - 1] +\n\t\t\t\t\t1ll * num[i - j] * num[j - 1] % P * sum % P * C[i - 1][j - 1]) % P;\n\t\t}\n\n\tdp[0][0] = 1;\n\tfor(int i = 0 ; i <= N ; ++i)\n\t\tfor(int j = 0 ; j <= i ; ++j)\n\t\t\tfor(int k = 0 ; k + i + 1 <= N + 1 && k + j <= M ; ++k){\n\t\t\t\tdp[i + k + 1][j + k] = (dp[i + k + 1][j + k] + 1ll * dp[i][j] * C[M - j][k] % P * num[k]) % P;\n\t\t\t\tans[i + k + 1][j + k] = (ans[i + k + 1][j + k] + 1ll * ans[i][j] * C[M - j][k] % P * num[k] + 1ll * dp[i][j] * C[M - j][k] % P * V[k]) % P;\n\t\t\t}\n\tcout << ans[N + 1][M]; return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "285f04b37c30373152baa6d00461d458", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#include \n#define rep(i, a, b) for (int i = a; i <= b; i++)\n#define per(i, a, b) for (int i = a; i >= b; i--)\nusing namespace std;\n\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef long long ll;\n\ntemplate \ninline void read(_T &f) {\n f = 0; _T fu = 1; char c = getchar();\n while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }\n while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }\n f *= fu;\n}\n\ntemplate \nvoid print(T x) {\n if (x < 0) putchar('-'), x = -x;\n if (x < 10) putchar(x + 48);\n else print(x / 10), putchar(x % 10 + 48);\n}\n\ntemplate \nvoid print(T x, char t) {\n print(x); putchar(t);\n}\n\nconst int N = 505;\n\nint n, m, md;\n\ninline int add(int x, int y) {\n x += y;\n if (x >= md) x -= md;\n return x;\n}\n\ninline int sub(int x, int y) {\n x -= y;\n if (x < 0) x += md;\n return x;\n}\n\ninline int mul(int x, int y) { return 1ll * x * y % md; }\n\ninline int fpow(int x, int y) {\n int ans = 1;\n while (y) {\n if (y & 1) ans = mul(ans, x);\n y >>= 1; x = mul(x, x);\n }\n return ans;\n}\n\nint fac[N], inv[N], f[N], g[N], dp[N][N][2], dp2[N][N][2];\n\ninline int C(int n, int m) {\n return mul(fac[n], mul(inv[m], inv[n - m]));\n}\n\nint main() {\n read(n); read(m); read(md);\n fac[0] = 1;\n for (int i = 1; i <= n; i++) fac[i] = mul(fac[i - 1], i);\n inv[n] = fpow(fac[n], md - 2);\n for (int i = n; i >= 1; i--) inv[i - 1] = mul(inv[i], i);\n f[0] = 0; g[0] = 1;\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= i; j++) {\n int now = mul(C(i - 1, j - 1), i + 1);\n g[i] = add(g[i], mul(mul(g[j - 1], g[i - j]), now));\n f[i] = add(f[i], mul(mul(f[j - 1], g[i - j]), now));\n f[i] = add(f[i], mul(mul(g[j - 1], f[i - j]), now));\n f[i] = add(f[i], mul(mul(C(i - 1, j - 1), mul(g[j - 1], g[i - j])), (j - 1) * j / 2 + (i - j) * (i - j + 1) / 2));\n }\n // fprintf(stderr, \"f[%d] = %d, g[%d] = %d\\n\", i, f[i], i, g[i]);\n }\n dp[0][0][1] = 0; dp2[0][0][1] = 1;\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j <= i; j++) {\n dp[i][j][1] = add(dp[i - 1][j][0], dp[i - 1][j][1]);\n dp2[i][j][1] = add(dp2[i - 1][j][0], dp2[i - 1][j][1]);\n for (int k = 1; k <= j; k++) {\n dp[i][j][0] = add(dp[i][j][0], mul(C(j, k), mul(dp2[i - k][j - k][1], f[k])));\n dp[i][j][0] = add(dp[i][j][0], mul(C(j, k), mul(dp[i - k][j - k][1], g[k])));\n dp2[i][j][0] = add(dp2[i][j][0], mul(C(j, k), mul(dp2[i - k][j - k][1], g[k])));\n }\n }\n }\n print(add(dp[n][m][0], dp[n][m][1]), '\\n');\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "3a22e40bd0b333bf9aa04eca65149f54", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ll long long\n#define pb push_back\n#define lowbit(x) ((x)&(-(x)))\n#define mid ((l+r)>>1)\n#define lson rt<<1, l, mid\n#define rson rt<<1|1, mid+1, r\n#define fors(i, a, b) for(int i = (a); i < (b); ++i)\nusing namespace std;\nint mod;\nint n, m;\nconst int maxn = 505;\nint f[maxn], h[maxn], g[maxn][maxn], t[maxn][maxn];\nint C[maxn][maxn];\nint main()\n{\n cin>>n>>m>>mod;\n C[0][0] = 1;\n fors(i,1,n+1){\n C[i][0] = 1;\n fors(j,1,i+1) {\n C[i][j] = (C[i-1][j] + C[i-1][j-1])%mod;\n //cout<<\"i:\"<\n// bitch please\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define dibs reserve\n#define OVER9000 1234567890\n#define tisic 47\n#define soclose 1e-8\n#define patkan 9\n#define ff first\n#define ss second\nusing uint = unsigned int;\nusing cat = long long;\nusing dbl = long double;\nconstexpr dbl pi = 3.14159265358979323846;\nusing namespace std;\n\n#ifdef DONLINE_JUDGE\n\t// palindromic tree is better than splay tree!\n\t#define lld I64d\n#endif\n\ntemplate \nT abs(T x) { return (x < 0) ? (-x) : x; }\n\ncat pw(cat a, cat e, cat mod) {\n\tif(e <= 0) return 1;\n\tcat x = pw(a, e/2, mod);\n\tx = x * x % mod;\n\treturn (e&1) ? x * a % mod : x;\n}\n\nint main() {\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n\tcout << fixed << setprecision(10);\n\tint N, M;\n\tcat mod;\n\tcin >> N >> M >> mod;\n\tvector fac(N+1, 1), inv(N+1, 1);\n\tfor(int i = 1; i <= N; i++) {\n\t\tfac[i] = fac[i-1] * i % mod;\n\t\tinv[i] = inv[i-1] * pw(i, mod-2, mod) % mod;\n\t}\n\tvector cnt(M+1, 0);\n\tvector sum(M+1, 0);\n\tcnt[0] = 1;\n\tfor(int m = 1; m <= M; m++) {\n\t\tfor(int k = 0; k < m; k++) {\n\t\t\tcat cl = cnt[k] * inv[k] % mod, cr = cnt[m-1-k] * inv[m-1-k] % mod;\n\t\t\tcat sl = sum[k] * inv[k] % mod, sr = sum[m-1-k] * inv[m-1-k] % mod;\n\t\t\tcnt[m] += cl * cr % mod * (m+1);\n\t\t\tsum[m] += (cl * sr + sl * cr) % mod * (m+1);\n\t\t\tsum[m] += cl * cr % mod * (k*(k+1)+(m-k)*(m-k-1))/2 % mod;\n\t\t}\n\t\tcnt[m] = cnt[m] % mod * fac[m-1] % mod;\n\t\tsum[m] = sum[m] % mod * fac[m-1] % mod;\n\t}\n\tvector< vector > cnt_ans(N+1, vector(M+1, 0));\n\tvector< vector > sum_ans(N+1, vector(M+1, 0));\n\tcnt_ans[0][0] = 1;\n\tfor(int i = 1; i <= N; i++) for(int j = 0; j <= min(i, M); j++) {\n\t\tcnt_ans[i][j] = cnt_ans[i-1][j];\n\t\tsum_ans[i][j] = sum_ans[i-1][j];\n\t\tif(j == i) {\n\t\t\tcnt_ans[i][j] += cnt[i] * inv[i] % mod;\n\t\t\tsum_ans[i][j] += sum[i] * inv[i] % mod;\n\t\t}\n\t\tfor(int k = 1; k <= min(j, i-1); k++) {\n\t\t\tcat c = cnt[k] * inv[k] % mod;\n\t\t\tcat s = sum[k] * inv[k] % mod;\n\t\t\tcnt_ans[i][j] += cnt_ans[i-1-k][j-k] * c % mod;\n\t\t\tsum_ans[i][j] += (sum_ans[i-1-k][j-k] * c + cnt_ans[i-1-k][j-k] * s) % mod;\n\t\t}\n\t\tcnt_ans[i][j] %= mod;\n\t\tsum_ans[i][j] %= mod;\n\t}\n\tcat ans = sum_ans[N][M] * fac[M] % mod;\n\tcout << ans << \"\\n\";\n}\n\n// look at my code\n// my code is amazing\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "ef59c01550e81388df206f90764d09e1", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nusing ll = long long;\nusing pii = pair;\nusing vi = vector;\n\ntemplate ostream& operator<<(ostream &os, const vector &v) { os << '['; string sep = \"\"; for (const auto &x : v) os << sep << x, sep = \", \"; return os << ']'; }\ntemplate ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << \", \" << p.second << ')'; }\ntemplate ostream& operator<<(ostream &os, const set &s) { os << '{'; string sep = \"\"; for (const auto &x : s) os << sep << x, sep = \", \"; return os << '}'; }\ntemplate ostream& operator<<(ostream &os, const map &m) { os << '{'; string sep = \"\"; for (const auto &x : m) os << sep << x.first << \" -> \" << x.second, sep = \", \"; return os << '}'; }\n\n#ifdef MY_DEBUG_FLAG\nvoid debug() { cerr << endl; }\ntemplate void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << \", \"; debug(F...);}\n#define db(...) cerr << \"DEBUG (\" << #__VA_ARGS__ << \") == \", debug(__VA_ARGS__)\n#else\n#define db(...)\n#endif\n\n#define pb push_back\n#define eb emplace_back\n#define ff first\n#define ss second\n#define fast ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr)\n#define sz(a) ((int)(a).size())\n#define rep(i,a,b) for(int i=(a); i<(b); i++)\n#define dec(i,n,a) for(int i=(n); i>=(a); i--)\n#define clr(a,v) memset(a, v, sizeof(a))\n#define all(a) (a).begin(),(a).end()\n\nconstexpr int inf = 0x3f3f3f3f;\nconstexpr int maxn = 510;\nint mod;\n\n// m mod i = m - i*floor(m/i)\n// m mod i === - i*floor(m/i) mod m\n// i^-1 === m - floor(m/i)*(m mod i)^-1 mod m\n\nint fat[maxn], inv[maxn], inv_fat[maxn];\n\nvoid calc() {\n\tfat[0] = fat[1] = inv[1] = inv_fat[0] = inv_fat[1] = 1;\n\tfor(int i = 2; i < maxn; i++) {\n\t\tfat[i] = (int)(1ll * fat[i-1] * i % mod);\n\t\tinv[i] = mod - (int)(1ll * (mod/i) * inv[mod%i] % mod);\n\t\tinv_fat[i] = (int)(1ll * inv_fat[i-1] * inv[i] % mod);\n\t}\n}\n\ninline int choose(int a, int b) {\n\treturn (int)(1ll * fat[a] * (1ll * inv_fat[b] * inv_fat[a-b] % mod) % mod);\n}\n\ninline ll p(int x) { return 1ll * x * (x+1) / 2; }\n\nint f[maxn], c[maxn], dp1[maxn][maxn], dp2[maxn][maxn];\n\nint main() {\n\tint n, m; scanf(\"%d %d %d\", &n, &m, &mod);\n\tcalc();\n\tf[0] = 1;\n\tfor(int i = 1; i <= m; i++) {\n\t\tfor(int j = 0; j < i; j++) {\n\t\t\tf[i] += (int)((1ll * choose(i-1, j) * f[j] % mod) * f[i-1-j] % mod);\n\t\t\tif(f[i] >= mod) f[i] -= mod;\n\t\t\tc[i] += (int)((1ll * (i+1) * choose(i-1, j) % mod) *\n\t\t\t\t((1ll * f[j] * c[i-1-j] % mod) + (1ll * f[i-1-j] * c[j] % mod) % mod) % mod);\n\t\t\tif(c[i] >= mod) c[i] -= mod;\n\t\t\tc[i] += (int)((1ll * choose(i-1, j) * (p(j) + p(i-1-j)) % mod) *\n\t\t\t\t(1ll * f[j] * f[i-1-j] % mod) % mod);\n\t\t\tif(c[i] >= mod) c[i] -= mod;\n\t\t}\n\t\tf[i] = (int)(1ll * f[i] * (i+1) % mod);\n\t}\n\tdp1[0][0] = 1;\n\tfor(int k = 1; k <= n; k++) {\n\t\tdp1[k][0] = 1;\n\t\tfor(int i = 1; i <= min(m, k); i++) {\n\t\t\tdp1[k][i] = dp1[k-1][i];\n\t\t\tfor(int j = 1; j <= i; j++) {\n\t\t\t\tdp1[k][i] += (int)((1ll * f[j] * dp1[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp1[k][i] >= mod) dp1[k][i] -= mod;\n\t\t\t}\n\n\t\t\tdp2[k][i] = dp2[k-1][i];\n\t\t\tfor(int j = 1; j <= i; j++) {\n\t\t\t\tdp2[k][i] += (int)((1ll * c[j] * dp1[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp2[k][i] >= mod) dp2[k][i] -= mod;\n\t\t\t\tdp2[k][i] += (int)((1ll * f[j] * dp2[max(k-j-1, 0)][i-j] % mod) * choose(i, j) % mod);\n\t\t\t\tif(dp2[k][i] >= mod) dp2[k][i] -= mod;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", dp2[n][m]);\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "43b053aa670423470ed318cda7352c38", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//#define DEBUG\n//#define USEPB_DS\n#define USETR1\n#define CPPELEVEN\n#define GPP\n\n/*\n * temp.cpp\n *\n * Created on: 2012-7-18\n * Author: BSBandme\n */\n//#pragma comment(linker, \"/STACK:1024000000,1024000000\")\n#if __cplusplus < 201500L\n#include \n#else\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#endif\n\nusing namespace std;\n\n#ifndef CPPELEVEN\n#ifdef USETR1\n#include \n#include \nusing namespace tr1;\n#endif\n#else\n#include \n#include \n#endif\n\n#ifdef USEPB_DS\n#include \n#include \n#include \nusing namespace __gnu_pbds;\n// binomial_heap_tag, rc_binomial_heap_tag, thin_heap_tag, binary_heap_tag\ntypedef __gnu_pbds::priority_queue, pairing_heap_tag> pq_type;\n// splay_tree_tag, ov_tree_tag\ntypedef tree , rb_tree_tag, tree_order_statistics_node_update> tree_type;\n#endif\n\n#define mpr make_pair\ntypedef unsigned int ui;\ntypedef unsigned long long ull;\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef vector vi;\ntypedef vector vll;\ntypedef vector vd;\ntypedef vector vs;\ntypedef map mpsi;\ntypedef map mpdi;\ntypedef map mpii;\n\nconst double pi = acos(0.0) * 2.0;\nconst long double eps = 1e-10;\nconst int step[8][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 1 }, {\n\t\t1, 1 }, { 1, -1 }, { -1, -1 } };\n\ntemplate inline T abs1(T a) {\n\treturn a < 0 ? -a : a;\n}\n\n#ifndef CPPELEVEN\ntemplate inline T max1(T a, T b) { return b < a ? a : b; }\ntemplate inline T max1(T a, T b, T c) { return max1(max1(a, b), c); }\ntemplate inline T max1(T a, T b, T c, T d) { return max1(max1(a, b, c), d); }\ntemplate inline T max1(T a, T b, T c, T d, T e) { return max1(max1(a, b, c, d), e); }\ntemplate inline T min1(T a, T b) { return a < b ? a : b; }\ntemplate inline T min1(T a, T b, T c) { return min1(min1(a, b), c); }\ntemplate inline T min1(T a, T b, T c, T d) { return min1(min1(a, b, c), d); }\ntemplate inline T min1(T a, T b, T c, T d, T e) { return min1(min1(a, b, c, d), e); }\n#else\ntemplate\nt min1(t a, t1 b) {\n\treturn a < b ? a : b;\n}\ntemplate\nt min1(t a, arg ... arr) {\n\treturn min1(a, min1(arr...));\n}\ntemplate\nt max1(t a, t1 b) {\n\treturn a > b ? a : b;\n}\ntemplate\nt max1(t a, arg ... arr) {\n\treturn max1(a, max1(arr...));\n}\n#endif\n\ninline int jud(double a, double b) {\n\tif (abs(a) < eps && abs(b) < eps)\n\t\treturn 0;\n\telse if (abs1(a - b) / max(abs1(a), abs1(b)) < eps) return 0;\n\tif (a < b) return -1;\n\treturn 1;\n}\ntemplate inline int jud(t a, t b) {\n\tif (a < b) return -1;\n\tif (a == b) return 0;\n\treturn 1;\n}\n\n// f_lb == 1代表返回相同的一串的左边界,f_small == 1代表返回如果没有寻找的值返回小的数\ntemplate\ninline int RangeFind(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {\n\tif (na == 0) return 0;\n\tint be = 0, en = na - 1;\n\tif (*a <= *(a + na - 1)) {\n\t\tif (f_lb == 0)\n\t\t\twhile (be < en) {\n\t\t\t\tint mid = (be + en + 1) / 2;\n\t\t\t\tif (jud(*(a + mid), val) != 1)\n\t\t\t\t\tbe = mid;\n\t\t\t\telse\n\t\t\t\t\ten = mid - 1;\n\t\t\t}\n\t\telse\n\t\t\twhile (be < en) {\n\t\t\t\tint mid = (be + en) / 2;\n\t\t\t\tif (jud(*(a + mid), val) != -1)\n\t\t\t\t\ten = mid;\n\t\t\t\telse\n\t\t\t\t\tbe = mid + 1;\n\t\t\t}\n\t\tif (f_small && jud(*(a + be), val) == 1) be--;\n\t\tif (!f_small && jud(*(a + be), val) == -1) be++;\n\t} else {\n\t\tif (f_lb)\n\t\t\twhile (be < en) {\n\t\t\t\tint mid = (be + en + 1) / 2;\n\t\t\t\tif (jud(*(a + mid), val) != -1)\n\t\t\t\t\tbe = mid;\n\t\t\t\telse\n\t\t\t\t\ten = mid - 1;\n\t\t\t}\n\t\telse\n\t\t\twhile (be < en) {\n\t\t\t\tint mid = (be + en) / 2;\n\t\t\t\tif (jud(*(a + mid), val) != 1)\n\t\t\t\t\ten = mid;\n\t\t\t\telse\n\t\t\t\t\tbe = mid + 1;\n\t\t\t}\n\t\tif (!f_small && jud(*(a + be), val) == -1) be--;\n\t\tif (f_small && jud(*(a + be), val) == 1) be++;\n\t}\n\treturn be;\n}\n\ntemplate inline T lowb(T num) {\n\treturn num & (-num);\n}\n#ifdef GPP\ninline int bitnum(ui nValue) {\n\treturn __builtin_popcount(nValue);\n}\ninline int bitnum(int nValue) {\n\treturn __builtin_popcount(nValue);\n}\ninline int bitnum(ull nValue) {\n\treturn __builtin_popcountll(nValue);\n}\ninline int bitnum(ll nValue) {\n\treturn __builtin_popcountll(nValue);\n}\ninline int bitmaxl(ui a) {\n\tif (a == 0) return 0;\n\treturn 32 - __builtin_clz(a);\n}\ninline int bitmaxl(int a) {\n\tif (a == 0) return 0;\n\treturn 32 - __builtin_clz(a);\n}\ninline int bitmaxl(ull a) {\n\tif (a == 0) return 0;\n\treturn 64 - __builtin_clzll(a);\n}\ninline int bitmaxl(ll a) {\n\tif (a == 0) return 0;\n\treturn 64 - __builtin_clzll(a);\n}\n#else\n#endif\n\nlong long pow(long long n, long long m, long long mod = 0) {\n\tif (m < 0) return 0;\n\tlong long ans = 1;\n\tlong long k = n;\n\twhile (m) {\n\t\tif (m & 1) {\n\t\t\tans *= k;\n\t\t\tif (mod) ans %= mod;\n\t\t}\n\t\tk *= k;\n\t\tif (mod) k %= mod;\n\t\tm >>= 1;\n\t}\n\treturn ans;\n}\n\n#define MOD 1000000007\ntemplate\ninline void add(t1 &a, t2 b, int mod = -1) {\n\tif (mod == -1) mod = MOD;\n\ta += b;\n\twhile (a >= mod)\n\t\ta -= mod;\n\twhile (a < 0)\n\t\ta += mod;\n}\ntemplate\nvoid output1(t arr) {\n\tfor (int i = 0; i < (int) arr.size(); i++)\n\t\tcerr << arr[i] << ' ';\n\tcerr << endl;\n}\ntemplate\nvoid output2(t arr) {\n\tfor (int i = 0; i < (int) arr.size(); i++)\n\t\toutput1(arr[i]);\n}\n\n//....................密..........封..........线..........下..........禁..........止..........hack...............................................\n\nconst int maxn = 510;\nll dp_con[maxn], sum_con[maxn], dp[maxn][maxn], sum[maxn][maxn];\nint c[maxn][maxn];\nint n, m, mod;\n\ninline ll Arth(ll n) {\n\treturn n * (n + 1) / 2 % mod;\n}\n\nint main() {\n\n//............................不要再忘了检查maxn大小了!!!!BSBandme你个SB!!!!...................................................\n\n\tios_base::sync_with_stdio(0);\n#ifdef DEBUG //......................................................................................................\n\tfreopen(\"input.txt\", \"r\", stdin);\n#endif //...........................................................................................................\n\n\tscanf(\"%d%d%d\", &n, &m, &mod);\n\tfor (int i = 0; i < maxn; i++) {\n\t\tc[i][0] = 1;\n\t\tfor (int j = 1; j <= i; j++) {\n\t\t\tc[i][j] = c[i - 1][j] + c[i - 1][j - 1];\n\t\t\tc[i][j] %= mod;\n\t\t}\n\t}\n\n\tdp_con[1] = 2;\n\tdp_con[0] = 1;\n\tsum_con[1] = sum_con[0] = 0;\n\tfor (int i = 2; i <= m; i++) {\n\t\tdp_con[i] = 0;\n\t\tsum_con[i] = 0;\n\t\tfor (int j = 0; j < i; j++) {\n\t\t\tll rcon = dp_con[j] * dp_con[i - j - 1] % mod * c[i - 1][j] % mod;\n\t\t\tdp_con[i] += rcon * (i + 1) % mod;\n\t\t\tsum_con[i] += (dp_con[j] * sum_con[i - j - 1] + sum_con[j] * dp_con[i - j - 1]) % mod * c[i - 1][j] % mod * (i + 1) % mod;\n\t\t\tsum_con[i] += rcon * (Arth(j) + Arth(i - 1 - j)) % mod;\n\t\t}\n\t\tdp_con[i] %= mod;\n\t\tsum_con[i] %= mod;\n\t}\n\n\tdp[0][0] = 1;\n\tfor (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) if (dp[i][j]) {\n\t\tfor (int k = 1; k + i <= m; k++) {\n\t\t\tdp[i + k][j + 1] += dp[i][j] * dp_con[k] % mod * c[i + k][k];\n\t\t\tdp[i + k][j + 1] %= mod;\n\t\t\tsum[i + k][j + 1] += (dp[i][j] * sum_con[k] + sum[i][j] * dp_con[k]) % mod * c[i + k][k];\n\t\t\tsum[i + k][j + 1] %= mod;\n\t\t}\n\t}\n\n\tll ans = 0;\n\tfor (int j = 1; j <= m; j++) {\n\t\tif (n - (m - 1 + j) < 0) break;\n\t\tans += sum[m][j] * c[n - (m - 1 + j) + j][j] % mod;\n\t\tans %= mod;\n\t}\n\n\tprintf(\"%I64d\\n\", ans);\n\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "f5d3ed267281757b1270cfe52d756a6f", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nconst int N=505;\ntypedef long long ll;\nint n,m,mod;\nint C[N][N];\nint f[N],g[N],ff[N][N],gg[N][N];\ninline void add(int &x,int y){\n\tx+=y;if(x>=mod) x-=mod;\n}\nint main(){\n\tscanf(\"%d%d%d\",&n,&m,&mod);\n\tfor(int i=0;i<=n;i++){\n\t\tC[i][0]=1;\n\t\tfor(int j=1;j<=i;j++)\n\t\t\tC[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;\n\t}\n\tf[0]=1;\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=1;j<=i;j++){\n\t\t\tadd(f[i],1ll*f[j-1]*f[i-j]%mod*C[i-1][j-1]%mod*(i+1)%mod);\n\t\t\tadd(g[i],1ll*g[j-1]*f[i-j]%mod*C[i-1][j-1]%mod*(i+1)%mod);\n\t\t\tadd(g[i],1ll*g[i-j]*f[j-1]%mod*C[i-1][j-1]%mod*(i+1)%mod);\n\t\t\tint s=0;for(int k=1;k<=i;k++) add(s,abs(k-j));\n\t\t\tadd(g[i],1ll*f[j-1]*f[i-j]%mod*C[i-1][j-1]%mod*s%mod);\n\t\t}\n\tff[0][0]=1;\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=0;j<=i;j++){\n\t\t\tif(j==i) {\n\t\t\t\tff[i][j]=f[i],gg[i][j]=g[i];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tff[i][j]=ff[i-1][j];gg[i][j]=gg[i-1][j];\n\t\t\tfor(int k=1;k<=j;k++){\n\t\t\t\tadd(ff[i][j],1ll*ff[i-k-1][j-k]*f[k]%mod*C[j][k]%mod);\n\t\t\t\tadd(gg[i][j],1ll*ff[i-k-1][j-k]*g[k]%mod*C[j][k]%mod);\n\t\t\t\tadd(gg[i][j],1ll*gg[i-k-1][j-k]*f[k]%mod*C[j][k]%mod);\n\t\t\t}\n\t\t}\n\tprintf(\"%d\",gg[n][m]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "309cfa544fc2362683d381fb8d083d88", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"< void chmax(t&a,u b){if(a void chmin(t&a,u b){if(b using vc=vector;\ntemplate using vvc=vc>;\n\nusing pi=pair;\nusing vi=vc;\n\ntemplate\nostream& operator<<(ostream& os,const pair& p){\n\treturn os<<\"{\"< ostream& operator<<(ostream& os,const vc& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<\nostream& operator<<(ostream&os,const array&a){\n\treturn os<(all(a));\n}\n\ntemplate\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<(t);\n\tprint_tuple(os,t);\n}\n\ntemplate\nostream& operator<<(ostream&os,const tuple&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate\nvoid print(t x,int suc=1){\n\tcout<>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\npi readpi(int off=0){\n\tint a,b;cin>>a>>b;\n\treturn pi(a+off,b+off);\n}\n\ntemplate\nvoid print(const vector&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate\nT sq(const T& t){\n\treturn t*t;\n}\n\n//#define CAPITAL\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)< void mkuni(vc&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution(l, r)(gen);\n}\n\ntemplate\nvoid myshuffle(vc&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate\nint lwb(const vc&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\nvvc readGraph(int n,int m){\n\tvvc g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc readTree(int n){\n\treturn readGraph(n,n-1);\n}\n\nstruct modinfo{uint mod,root;};\ntemplate\nstruct modular{\n\tstatic constexpr uint const &mod=ref.mod;\n\tstatic modular root(){return modular(ref.root);}\n\tuint v;\n\t//modular(initializer_listls):v(*ls.bg){}\n\tmodular(ll vv=0){s(vv%mod+mod);}\n\tmodular& s(uint vv){\n\t\tv=vv>=1;\n\t\t}\n\t\treturn res;\n\t}\n\tmodular inv()const{return pow(mod-2);}\n\t/*modular inv()const{\n\t\tint x,y;\n\t\tint g=extgcd(v,mod,x,y);\n\t\tassert(g==1);\n\t\tif(x<0)x+=mod;\n\t\treturn modular(x);\n\t}*/\n\tfriend modular operator+(int x,const modular&y){\n\t\treturn modular(x)+y;\n\t}\n\tfriend modular operator-(int x,const modular&y){\n\t\treturn modular(x)-y;\n\t}\n\tfriend modular operator*(int x,const modular&y){\n\t\treturn modular(x)*y;\n\t}\n\tfriend modular operator/(int x,const modular&y){\n\t\treturn modular(x)/y;\n\t}\n\tfriend ostream& operator<<(ostream&os,const modular&m){\n\t\treturn os<>(istream&is,modular&m){\n\t\tll x;is>>x;\n\t\tm=modular(x);\n\t\treturn is;\n\t}\n\tbool operator<(const modular&r)const{return v;\n\nconst int vmax=10010;\nmint fact[vmax],finv[vmax],invs[vmax];\nvoid initfact(){\n\tfact[0]=1;\n\trng(i,1,vmax){\n\t\tfact[i]=fact[i-1]*i;\n\t}\n\tfinv[vmax-1]=fact[vmax-1].inv();\n\tfor(int i=vmax-2;i>=0;i--){\n\t\tfinv[i]=finv[i+1]*(i+1);\n\t}\n\tfor(int i=vmax-1;i>=1;i--){\n\t\tinvs[i]=finv[i]*fact[i-1];\n\t}\n}\nmint choose(int n,int k){\n\treturn fact[n]*finv[n-k]*finv[k];\n}\nmint binom(int a,int b){\n\treturn fact[a+b]*finv[a]*finv[b];\n}\nmint catalan(int n){\n\treturn binom(n,n)-(n-1>=0?binom(n-1,n+1):0);\n}\n\n/*\nconst int vmax=110;\nmint binbuf[vmax][vmax];\nmint choose(int n,int k){\n\treturn binbuf[n-k][k];\n}\nmint binom(int a,int b){\n\treturn binbuf[a][b];\n}\nvoid initfact(){\n\tbinbuf[0][0]=1;\n\trep(i,vmax)rep(j,vmax){\n\t\tif(i)binbuf[i][j]+=binbuf[i-1][j];\n\t\tif(j)binbuf[i][j]+=binbuf[i][j-1];\n\t}\n}\n*/\n\nvoid slv(){\n\tint n,m;cin>>n>>m>>base.mod;\n\tinitfact();\n\t\n\tmint ans;\n\trng(len,1,m){\n\t\tmint f=mint(len+1).pow(len)*len/(n-len)*finv[len];\n\t\trep(i,m-len){\n\t\t\tint j=i+len;\n\t\t\tmint g=mint(n-len).pow(i)*finv[i];\n\t\t\tmint h=(n-j)*mint(n+1).pow(m-1-j)*fact[j];\n\t\t\tans+=f*g*h;\n\t\t}\n\t}\n\tans*=mint(2).pow(m-1)*(n+1-m);\n\tprint(ans);\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<>t;rep(_,t)\n\tslv();\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "785cc02ca07b0d581db99f380f6faed0", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#include\nusing namespace std;\nconst int N=507;\nint n,m,p,fac[N],ifac[N],f[N][N],g[N][N],ans[N][N],sum[N][N],C[N][N];\ninline int pows(long long u,int v){\n\tlong long ans=1;\n\twhile(v>0){\n\t\tif(v&1) ans=ans*u%p; u=u*u%p,v=v>>1;\n\t}\n\treturn ans;\n}\ninline long long binomx(int a,int b){\n//\tif(a>b) return 0;\n//\treturn C[a][b];\n\treturn 1ll*fac[a]*ifac[b]%p*ifac[a-b]%p;\n}\ninline long long binom(int a,int b){\n\treturn C[a][b];\n}\ninline long long getsum(int u){\n\treturn 1ll*u*(u+1)/2;\n} \nsigned main(){\n\tcin>>n>>m>>p,fac[0]=ifac[0]=1;\n\tfor(int i=1;i<=500;i++) fac[i]=1ll*fac[i-1]*i%p,ifac[i]=pows(fac[i],p-2);\n\tfor(int i=0;i<=500;i++)\n\t\tfor(int j=0;j<=500;j++) C[i][j]=binomx(i,j);\n\tfor(int len=1;len<=n;len++)\n\t\tfor(int l=1;l<=n;l++){\n\t\t\tint r=l+len-1; if(r>n) continue;\n//\t\t\tif(r-l+1!=len) continue;\n\t\t\tif(l==r){\n\t\t\t\tf[l][r]=0,g[l][r]=2;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tg[l][r]=(g[l][r]+1ll*(g[l+1][r]+g[l][r-1])*(r-l+2))%p;\n\t\t\t\tf[l][r]=(f[l][r]+1ll*(g[l+1][r]+g[l][r-1])*getsum(r-l))%p;\n\t\t\t\tf[l][r]=(f[l][r]+1ll*(f[l+1][r]+f[l][r-1])*(r-l+2))%p; int S=0,T=0;\n\t\t\t\tfor(int x=l+1;x<=r-1;x++){\n\t\t\t\t\tint A=1ll*(r-l+2)*binom(r-l,x-l)%p;\n\t\t\t\t\tS=(S+(1ll*f[l][x-1]*g[x+1][r]+1ll*f[x+1][r]*g[l][x-1])%p*A+(getsum(x-l)+getsum(r-x))*g[x+1][r]%p*g[l][x-1]%p*binom(r-l,x-l))%p;\n\t\t\t\t\tT=(T+1ll*g[x+1][r]*g[l][x-1]%p*A)%p;\n\t\t\t\t}\n\t\t\t\tf[l][r]=(f[l][r]+S)%p;\n\t\t\t\tg[l][r]=(g[l][r]+T)%p;\n\t\t\t}\n\t\t}\n\tans[0][0]=0,sum[0][0]=1;\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=i;j<=n;j++)\n\t\t\tfor(int k=0;k<=i;k++){\n\t\t\t\tint t=k+j-i+1; if(t>m) break;\n\t\t\t\tans[j+1][t]=(ans[j+1][t]+1ll*ans[i-1][k]*g[i][j]%p*binom(t,j-i+1))%p;\n\t\t\t\tans[j+1][t]=(ans[j+1][t]+1ll*f[i][j]*sum[i-1][k]%p*binom(t,j-i+1))%p;\n\t\t\t\tsum[j+1][t]=(sum[j+1][t]+1ll*g[i][j]*sum[i-1][k]%p*binom(t,j-i+1))%p;\n\t\t\t}\n\t\tfor(int j=0;j<=i;j++){\n\t\t\tans[i][j]=(ans[i][j]+ans[i-1][j])%p;\n\t\t\tsum[i][j]=(sum[i][j]+sum[i-1][j])%p;\n\t\t}\n\t}\n\tcout<<(ans[n+1][m]+ans[n][m])%p<\nusing namespace std;\nconst int N=510;\ntypedef pair pii;\n#define mp make_pair\n#define fi first\n#define se second\n\nint n,m,mod,C[N][N];\npii dp[N][N];\n\nint add(int a,int b) {\n\treturn a+b>=mod?a+b-mod:a+b;\n}\n\npii operator+(const pii &a,const pii &b) {\n\treturn mp(add(a.fi,b.fi),add(a.se,b.se));\n}\n\npii operator*(const pii &a,const pii &b) {\n\treturn mp(1ll*a.fi*b.fi%mod,(1ll*a.fi*b.se+1ll*a.se*b.fi)%mod);\n}\n\nint s1(int l,int r) {\n\treturn (l+r)*(r-l+1)/2;\n}\n\nint main() {\n#ifdef LOCAL\n\tfreopen(\"a.in\",\"r\",stdin);\n\tfreopen(\"a.out\",\"w\",stdout);\n#endif\n\tios::sync_with_stdio(0);cin.tie(0);\n\tcin>>n>>m>>mod;\n\tfor(int i=0;i\nusing namespace std;\n\n#ifdef SG\n\t#include \n#else\n\ttemplate struct outputer;\n\tstruct outputable {};\n\t#define PRINT(...)\n\t#define OUTPUT(...)\n\t#define show(...)\n\t#define debug(...)\n\t#define deepen(...)\n\t#define timer(...)\n\t#define fbegin(...)\n\t#define fend\n\t#define pbegin(...)\n\t#define pend\n#endif\n\n#define ARG4(_1,_2,_3,_4,...) _4\n\n#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)\n#define forn2(i,n) forn3 (i, 0, n)\n#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)\n\n#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)\n#define ford2(i,n) ford3 (i, 0, n)\n#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)\n\n#define ve vector\n#define pa pair\n#define tu tuple\n#define mp make_pair\n#define mt make_tuple\n#define pb emplace_back\n#define fs first\n#define sc second\n#define all(a) (a).begin(), (a).end()\n#define sz(a) ((int)(a).size())\n\ntypedef long double ld;\ntypedef int64_t ll;\ntypedef uint64_t ull;\ntypedef uint32_t ui;\ntypedef uint16_t us;\ntypedef uint8_t uc;\ntypedef pa pii;\ntypedef pa pil;\ntypedef pa pli;\ntypedef pa pll;\ntypedef ve vi;\n\ntemplate inline auto sqr (T x) -> decltype(x * x) {return x * x;}\ntemplate inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}\ntemplate inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}\n\nconst int N = 500;\n\nstruct Input {\n\tll mod;\n\tint n, m;\n\t\n\tbool read() {\n\t\treturn !!(cin >> n >> m >> mod);\n\t}\n\n\tvoid init(const Input &input) {\n\t\t*this = input;\n\t}\n};\n\nstruct Data: Input {\n\tll ans;\n\t\n\tvoid write() {\n\t\tcout << ans << endl;\n\t}\n};\n\n\nnamespace Main {\n\t\n\tstruct Solution: Data {\n\n\t\tll pw(ll x, ll y) {\n\t\t\tll res = 1;\n\t\t\twhile (y) {\n\t\t\t\tif (y & 1) {\n\t\t\t\t\tres = res * x % mod;\n\t\t\t\t}\n\t\t\t\tx = x * x % mod;\n\t\t\t\ty >>= 1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tll inv(ll x) {\n\t\t\treturn pw(x, mod - 2);\n\t\t}\n\n\t\tll ss(int x) {\n\t\t\treturn x * (x + 1) / 2;\n\t\t}\n\n\t\tll c[N + 1][N + 1];\n\n\t\t// cnt, sum\n\t\tpll a[N + 1];\n\t\tpll d[N + 2][N + 1];\n\t\t\n\t\tvoid solve() {\n\t\t\tforn (i, n + 1) {\n\t\t\t\tforn (j, n + 1) {\n\t\t\t\t\tif (!i || !j) {\n\t\t\t\t\t\tc[i][j] = 1;\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tc[i][j] = (c[i - 1][j] + c[i][j - 1]) % mod;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmemset(a, 0, sizeof a);\n\t\t\ta[0] = pll(1, 0);\n\t\t\tforn (i, 1, m + 1) {\n\t\t\t\tforn (j, i) {\n\t\t\t\t\tpll cur;\n\t\t\t\t\tcur.fs = a[j].fs * a[i - 1 - j].fs % mod;\n\t\t\t\t\tcur.sc = (a[j].sc * a[i - 1 - j].fs + a[j].fs * a[i - 1 - j].sc) % mod;\n\t\t\t\t\tcur.sc = (cur.sc * (i + 1) + (ss(j) + ss(i - 1 - j)) * cur.fs) % mod;\n\t\t\t\t\tcur.fs = cur.fs * (i + 1) % mod;\n\t\t\t\t\tcur.fs = cur.fs * c[j][i - 1 - j] % mod;\n\t\t\t\t\tcur.sc = cur.sc * c[j][i - 1 - j] % mod;\n\t\t\t\t\ta[i].fs = (a[i].fs + cur.fs) % mod;\n\t\t\t\t\ta[i].sc = (a[i].sc + cur.sc) % mod;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdebug(a, a + m + 1);\n\n\t\t\tmemset(d, 0, sizeof d);\n\t\t\td[0][0] = pll(1, 0);\n\t\t\tforn (i, n + 2) {\n\t\t\t\tforn (j, m + 1) {\n\t\t\t\t\tforn (k, min(n - i, m - j) + 1) {\n\t\t\t\t\t\tpll cur;\n\t\t\t\t\t\tcur.fs = a[k].fs * d[i][j].fs % mod;\n\t\t\t\t\t\tcur.sc = (a[k].sc * d[i][j].fs + a[k].fs * d[i][j].sc) % mod;\n\t\t\t\t\t\tcur.fs = cur.fs * c[j][k] % mod;\n\t\t\t\t\t\tcur.sc = cur.sc * c[j][k] % mod;\n\t\t\t\t\t\td[i + k + 1][j + k].fs = (d[i + k + 1][j + k].fs + cur.fs) % mod;\n\t\t\t\t\t\td[i + k + 1][j + k].sc = (d[i + k + 1][j + k].sc + cur.sc) % mod;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdebug(d[i], d[i] + m + 1);\n\t\t\t}\n\t\t\tans = d[n + 1][m].sc;\n\t\t}\n\t\t\n\t\tvoid clear() {\n\t\t\t*this = Solution();\n\t\t}\n\t};\n}\n\n\nMain::Solution sol;\n\nint main() {\n\tcout.setf(ios::showpoint | ios::fixed);\n\tcout.precision(20);\n\n\t#ifdef SG\n\t\tfreopen((problemname + \".in\").c_str(), \"r\", stdin);\n//\t\tfreopen((problemname + \".out\").c_str(), \"w\", stdout);\n\t\twhile (sol.read()) {\n\t\t\tsol.solve();\n\t\t\tsol.write();\n\t\t\tsol.clear();\n\t\t}\n\t#else\n\t\tsol.read();\n\t\tsol.solve();\n\t\tsol.write();\n\t#endif\n\t\n\t/*\n\tint t;\n\tcin >> t;\n\tforn (i, t) {\n\t\tsol.read();\n\t\tsol.solve();\n\t\tsol.write();\n\t\tsol.clear();\n\t}\n\t*/\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "88b020414f5e39dbaa81ed9f049549c0", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// Author -- xyr2005\n\n#include\n\n#define lowbit(x) ((x)&(-(x)))\n#define DEBUG fprintf(stderr,\"Running on Line %d in Function %s\\n\",__LINE__,__FUNCTION__)\n#define SZ(x) ((int)x.size())\n#define mkpr std::make_pair\n#define pb push_back\n\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\ntypedef std::pair pi;\ntypedef std::pair pl;\nusing std::min;\nusing std::max;\n\nconst int inf=0x3f3f3f3f,Inf=0x7fffffff;\nconst ll INF=0x3f3f3f3f3f3f3f3f;\n\nstd::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());\ntemplate _Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}\ntemplate inline _Tp abs(const _Tp &a){return a>=0?a:-a;}\ntemplate inline void chmax(_Tp &a,const _Tp &b){(ainline void chmin(_Tp &a,const _Tp &b){(binline void read(_Tp &x)\n{\n\tchar ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();\n\tx=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();\n\tf&&(x=-x);\n}\ntemplate inline void read(_Tp &t,Args &...args){read(t);read(args...);}\ninline int read_str(char *s)\n{\n\tchar ch(getchar());while(ch==' '||ch=='\\r'||ch=='\\n') ch=getchar();\n\tchar *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\\r'&&ch!='\\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();\n\treturn tar-s+1;\n}\n\nconst int N=505;\nint mod;\nstruct FastMod{\n\ttypedef __uint128_t LLL;\n\tull b,m;\n\tvoid init(ull b){this->b=b,m=ull((LLL(1)<<64)/b);}\n\tinline ull operator()(ull a){\n\t\tull r=a-(ull)((LLL(m)*a)>>64)*b;\n\t\treturn r>=b?r-b:r;\n\t}\n}M;\ntemplateinline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}\ntemplateinline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}\nint f[N],g[N],dp1[N][N],dp2[N][N],C[N][N];\ninline ll s(int x){return x*(x+1)/2;}\nint main()\n{\n\tint n,m;read(n,m,mod),M.init(mod);\n\tfor(int i=0;i\nusing namespace std ;\n#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )\n#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )\n#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )\n#define re register\n#define int long long\nint gi() {\n\tchar cc = getchar() ; int cn = 0, flus = 1 ;\n\twhile( cc < '0' || cc > '9' ) { if( cc == '-' ) flus = - flus ; cc = getchar() ; }\n\twhile( cc >= '0' && cc <= '9' ) cn = cn * 10 + cc - '0', cc = getchar() ;\n\treturn cn * flus ;\n}\nconst int N = 500 + 5 ; \nint n, m, P, maxn, fac[N], inv[N], F[N][N], G[N][N], f[N], g[N] ; \nint fpow(int x, int k) {\n\tint ans = 1, base = x ;\n\twhile(k) {\n\t\tif(k & 1) ans = 1ll * ans * base % P ;\n\t\tbase = 1ll * base * base % P, k >>= 1 ;\n\t} return ans ;\n}\nint C(int x, int y) {\n\tif( x < y ) return 0 ; \n\treturn fac[x] * inv[y] % P * inv[x - y] % P ; \n}\nint S(int x) {\n\treturn x * (x + 1) / 2 ; \n}\nsigned main()\n{\n\tn = gi(), m = gi(), P = gi() ; \n\tmaxn = n + 1, fac[0] = inv[0] = 1 ; \n\trep( i, 1, maxn ) fac[i] = fac[i - 1] * i % P ; \n\trep( i, 1, maxn ) inv[i] = fpow( fac[i], P - 2 ) ;\n\tf[0] = 1 ; \n\trep( i, 1, m ) {\n\t\tfor(re int j = 0; j < i; ++ j) \n\t\t\tf[i] = (f[i] + f[j] * f[i - 1 - j] % P * C(i - 1, j) % P) % P, \n\t\t\tg[i] = (g[i] + \n\t\t\t\t( (g[j] * f[i - 1 - j] % P + f[j] * g[i - 1 - j] % P ) * (i + 1) % P \n\t\t\t\t+ f[j] * f[i - 1 - j] % P * (S(j) + S(i - 1 - j)) % P) * C(i - 1, j) % P) % P ; \n\t\tf[i] = f[i] * (i + 1) % P ; \n\t}\n\tF[0][0] = 1 ; \n\trep( j, 0, n ) F[0][j] = 1 ; \n\trep( i, 1, m ) {\n\t\tF[i][i] = f[i], G[i][i] = g[i] ; \n\t\tfor(re int j = i + 1; j <= n; ++ j) {\n\t\t\tF[i][j] = F[i][j - 1], G[i][j] = G[i][j - 1] ; \n\t\t\tfor(re int l = 1; l <= i; ++ l) {\n\t\t\t\tF[i][j] = (F[i][j] + F[i - l][j - 1 - l] * f[l] % P * C(i, l) % P) % P,\n\t\t\t\tG[i][j] = (G[i][j] + (G[i - l][j - 1 - l] * f[l] % P + F[i - l][j - 1 - l] * g[l] % P) * C(i, l) % P) % P ; \n\t\t\t}\n\t\t//\tprintf(\"QAQ (%lld %lld) %lld %lld\\n\", i, j, F[i][j], G[i][j] ) ; \n\t\t} \n\t}\n\tcout << G[m][n] << endl ; \n\treturn 0 ;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "a79803305af594529f91a8c71db26343", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int N=505;\n\nll n, m, mod;\nll dp1[N], dp2[N];\nll dp3[N][N], dp4[N][N];\nll C[N][N]; // combinatorics\n\nint main()\n{\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n memset(dp1, 0, sizeof(dp1));\n memset(dp2, 0, sizeof(dp2));\n memset(dp3, 0, sizeof(dp3));\n memset(dp4, 0, sizeof(dp4));\n\n cin >> n >> m >> mod;\n\n for (int i=0; i\nusing namespace std;\ntypedef signed long long ll;\n\n#define _P(...) (void)printf(__VA_ARGS__)\n#define FOR(x,to) for(x=0;x<(to);x++)\n#define FORR(x,arr) for(auto& x:arr)\n#define FORR2(x,y,arr) for(auto& [x,y]:arr)\n#define ALL(a) (a.begin()),(a.end())\n#define ZERO(a) memset(a,0,sizeof(a))\n#define MINUS(a) memset(a,0xff,sizeof(a))\ntemplate bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}\n//-------------------------------------------------------\n\nll mo;\nstruct modint {\n\tll v;\n\tmodint(const ll a=0) { v=a; if(mo&&(v>=mo||v<0)) v=(v%mo+mo)%mo;}\n\tstatic modint pow(const modint& a,ll n=mo-2) {\n\t\tll b=a.v,r=1;\n\t\twhile(n) r=r*((n%2)?b:1)%mo,b=b*b%mo,n>>=1;\n\t\treturn modint(r);\n\t}\n\tmodint& operator+=(const modint& a) { v+=a.v; if(v>=mo) v-=mo; return *this;}\n\tmodint& operator-=(const modint& a) { v+=mo-a.v; if(v>=mo) v-=mo; return *this;}\n\tmodint& operator*=(const modint& a) { v=v*a.v%mo; return *this;}\n\tmodint& operator/=(const modint& a) { assert(a.v!=0); *this*=pow(a); return *this;}\n\tmodint operator+(const modint& a) const { modint res(*this); return res+=a;}\n\tmodint operator-(const modint& a) const { modint res(*this); return res-=a;}\n\tmodint operator*(const modint& a) const { modint res(*this); return res*=a;}\n\tmodint operator/(const modint& a) const { modint res(*this); return res/=a;}\n\tfriend ostream& operator<<(ostream& os, const modint& a){ os<N_) return 0;\n\treturn factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;\n}\n\nvoid solve() {\n\tint i,j,k,l,r,x,y; string s;\n\t\n\tcin>>N>>M>>mo;\n\tdp1[0]=1;\n\tfor(i=1;i<=N;i++) {\n\t\tfor(j=1;j<=i;j++) {\n\t\t\tdp1[i]+=modint(i+1)*dp1[j-1]*dp1[i-j]*comb(i-1,j-1);\n\t\t\tdp2[i]+=modint(i+1)*(dp2[i-j]*dp1[j-1]+dp1[i-j]*dp2[j-1])*comb(i-1,j-1);\n\t\t\tdp2[i]+=dp1[j-1]*dp1[i-j]*comb(i-1,j-1)*(comb(j-1+1,2)+comb(i-j+1,2));\n\t\t}\n\t}\n\tdp3[0][0]=1;\n\tfor(i=1;i<=N;i++) {\n\t\tFOR(j,i) {\n\t\t\tdp3[i][j]=dp3[i-1][j];\n\t\t\tdp4[i][j]=dp4[i-1][j];\n\t\t\tfor(l=1;l<=j;l++) {\n\t\t\t\tdp3[i][j]+=dp1[l]*dp3[i-l-1][j-l]*comb(j,l);\n\t\t\t\tdp4[i][j]+=(dp1[l]*dp4[i-l-1][j-l]+dp2[l]*dp3[i-l-1][j-l])*comb(j,l);\n\t\t\t}\n\t\t}\n\t\tdp3[i][i]=dp1[i];\n\t\tdp4[i][i]=dp2[i];\n\t}\n\t\n\tcout<\n#define cs const\n#define pb push_back\nusing namespace std;\nint Mod; \nint add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }\nint dec(int a, int b){ return a - b < 0 ? a - b + Mod : a - b; }\nint mul(int a, int b){ return 1ll * a * b % Mod; }\nvoid Add(int &a, int b){ a = add(a, b); }\nvoid Dec(int &a, int b){ a = dec(a, b); }\nvoid Mul(int &a, int b){ a = mul(a, b); }\ncs int N = 1e3 + 50;\nint n, m, f[N], g[N];\nint C[N][N];\nvoid fc_init(int n){\n\tfor(int i = 0; i <= n; i++) C[i][0] = 1; \n\tfor(int i = 1; i <= n; i++)\n\tfor(int j = 1; j <= i; j++)\n\tC[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);\n}\nint main(){\n\t#ifdef FSYo\n\tfreopen(\"1.in\", \"r\", stdin);\n\t#endif\n\tcin >> n >> m >> Mod;\n\tf[1] = 2, g[1] = 0;\n\tfc_init(max(m, n - m + 1));\n\tfor(int i = 2; i <= m; i++){\n\t\tf[i] = mul(f[i - 1], 2 * (i + 1));\n\t\tg[i] = mul(g[i - 1], 2 * (i + 1));\n\t\tAdd(g[i], mul(f[i - 1], i * (i - 1)));\n\t\tfor(int j = 1; j < i; j++){\n\t\t\tint k = i - j - 1;\n\t\t\tint c = mul(C[j + k][j], j + k + 2);\n\t\t\tAdd(f[i], mul(c, mul(f[j], f[k])));\n\t\t\tAdd(g[i], mul(c, mul(f[j], g[k])));\n\t\t\tAdd(g[i], mul(c, mul(g[j], f[k])));\n\t\t\tint w = j * (j + 1) / 2 + k * (k + 1) / 2;\n\t\t\tAdd(g[i], mul(mul(C[j + k][j], w), mul(f[j], f[k])));\n\t\t} \n\t}\n\tstatic int dp[N][N][2];\n\tdp[0][0][0] = 1; \n\tfor(int i = 0; i <= m; i++)\n\tfor(int j = 0; j <= m; j++)\n\tfor(int o = 0, c; o < 2; o++)\n\tif(c = dp[i][j][o]){\n\t\tif(!o)\n\t\tfor(int k = 1; k + j <= m; k++)\n\t\tAdd(dp[i + 1][j + k][0], mul(mul(c, C[j + k][k]), f[k])), \n\t\tAdd(dp[i + 1][j + k][1], mul(mul(c, C[j + k][k]), g[k]));\n\t\telse \n\t\tfor(int k = 1; k + j <= m; k++)\n\t\tAdd(dp[i + 1][j + k][1], mul(mul(c, C[j + k][k]), f[k]));\n\t} \n\tint ans = 0;\n\tfor(int i = 1; i <= m; i++)\n\tAdd(ans, mul(dp[i][m][1], C[n - m + 1][i]));\n\tcout << ans; \n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "b5704a591992d577bed282b6bcffe79b", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\nconst int N = 500 + 10;\nint n,m,p;\nLL c[N][N],f[N],g[N];\nLL dp[N][N], ways[N][N];\nLL dp2[N][N], ways2[N][N];\nLL dp0[N][N], ways0[N][N];\nvoid add(LL &x,LL y){\n\tx=(x+y)%p;\n}\nLL cal(LL x){\n\treturn 1LL*x*(x-1)/2 % p; \n}\nint main() {\n cin>>n>>m>>p;\n for(int i=0;i\nusing namespace std;\n#define ll long long\nconst int maxn = 505;\nint n, m, mod;\ninline int add(int x, int y) {\n\tx += y;\n\treturn x >= mod ? x - mod : x;\n}\ninline int mul(int x, int y) { return 1ll * x * y % mod; }\nint C[maxn][maxn];\nint f[maxn], g[maxn];\nint F[maxn][maxn], G[maxn][maxn];\nint main() {\n\tscanf(\"%d%d%d\", &n, &m, &mod);\n\tfor(int i = 0; i <= n; i++) {\n\t\tC[i][0] = 1;\n\t\tfor(int j = 1; j <= i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);\n\t}\n\tf[0] = 1;\n\tfor(int i = 1; i <= n; i++) {\n\t\tfor(int j = 1; j <= i; j++) {\n\t\t\tf[i] = add(f[i], mul(mul(f[j - 1], f[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(f[j - 1], g[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(g[j - 1], f[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(f[j - 1], f[i - j]), mul(add(C[j][2], C[i - j + 1][2]), C[i - 1][j - 1])));\n\t\t}\n\t}\n\tF[0][0] = 1;\n\tfor(int i = 1; i <= n; i++)\n\t\tfor(int j = 0; j <= min(i, m); j++) {\n\t\t\tfor(int l = 0; l < j; l++) {\n\t\t\t\tF[i][j] = add(F[i][j], mul(mul(f[l], F[i - l - 1][j - l]), C[j][l]));\n\t\t\t\tG[i][j] = add(G[i][j], mul(mul(g[l], F[i - l - 1][j - l]), C[j][l]));\n\t\t\t\tG[i][j] = add(G[i][j], mul(mul(f[l], G[i - l - 1][j - l]), C[j][l]));\n\t\t\t}\n\t\t\tF[i][j] = add(F[i][j], f[j]);\n\t\t\tG[i][j] = add(G[i][j], g[j]);\n\t\t}\n\tcout << G[n][m] << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "80674212b7a27c7fb4b1c0deec28040f", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//In the Name of God\n//Ya Ali\n\n#include \n\n#define err(A) cout << #A << \" = \" << (A) << endl\n\n#define int long long\n\nusing namespace std;\n\nconst int maxn = 512;\n\nint n, m, M;\n\nint c[maxn][maxn];\nint p[maxn][maxn];\n\nint me, you;\n\nint g(int a, int b)\n{\n if (!b)\n return 1;\n return p[a + 1][b - 1] * p[2][b] %M * (a + 1 - b) %M;\n}\n\nint32_t main()\n{\n ios::sync_with_stdio(0); cin.tie(0);\n\n cin >> n >> m >> M;\n \n for (int i = 0; i < maxn; i ++)\n for (int j = 0; j <= i; j ++)\n if (j)\n\tc[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % M;\n else\n\tc[i][j] = 1;\n\n for (int i = 0; i < maxn; i ++)\n for (int j = 0; j < maxn; j ++)\n if (j)\n\tp[i][j] = p[i][j - 1] * i % M;\n else\n\tp[i][j] = 1;\n\n for (int i = 2; i <= m; i ++)\n for (int d = 1; d < i; d ++) \n {\n\tme = d * (d + 1) / 2 * c[i - 1][d] %M * p[d + 1][d - 1] %M * g(n + 1 - (d + 1) - 1, i - (d + 1)) %M * p[2][d] %M * p[n + 1][m - i] %M * p[2][m - i] %M;\n\tyou = (you + me + me) % M;\n }\n\n cout << you * (n + 1 - m) % M << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "3728ed19702fc768a5bbb42bae9fb65a", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n// #include \n// #include \n// using namespace __gnu_pbds;\n\n#pragma GCC optimize(\"O3\")\n#ifdef LOCAL\n#include \"/Users/lbjlc/Desktop/coding/debug_utils.h\"\n#else\n#define print(...) ;\n#define printn(...) ;\n#define printg(...) ;\n#define fprint(...) ;\n#define fprintn(...) ;\n#endif\n\n#define rep(i, a, b) for(auto i = (a); i < (b); i++)\n#define rrep(i, a, b) for(auto i = (a); i > (b); i--)\n#define all(v) (v).begin(), (v).end()\n#define pb push_back\n// #define mp make_pair\n#define fi first\n#define se second\n#define maxi(x, y) x = max(x, y)\n#define mini(x, y) x = min(x, y)\n// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }\n// #define endl '\\n'\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\nint get_random() {\n static uniform_int_distribution dist(0, 1e9 + 6);\n return dist(rng);\n}\n#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}\n\n\ntypedef unsigned long long ull;\ntypedef long long ll;\n \ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\n \ntypedef vector vi;\ntypedef vector vvi;\ntypedef vector vvvi;\ntypedef vector vpii;\ntypedef vector vpll;\ntypedef vector vpdd;\ntypedef vector vll;\n#define bd(type,op,val) bind(op(), std::placeholders::_1, val)\n\ntemplate\nvoid make_unique(T & v) {\n sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());\n}\nint geti() { int x; cin >> x; return x; }\nlong long getll() { long long x; cin >> x; return x; }\ndouble getd() { double x; cin >> x; return x; }\n// pair a(geti(), geti()) does not work\n// pair a({geti(), geti()}) works, since it uses initializer.\n\nconst int MAXN = 500 + 100;\n\nvoid solve(int tt) {\n // cout<<\"Case #\"<= 0 ? _x % mod : _x % mod + mod; }\n //operator long long() { return x; }\n\n template\n mint operator^(T b) const {\n if(!b) return 1;\n mint tmp = (*this) ^ (b / 2);\n return b % 2 ? tmp * tmp * (*this) : tmp * tmp;\n }\n\n mint inv() const { \n static unordered_map inv_dp;\n if(inv_dp.count(x))\n return inv_dp[x];\n else if(inv_dp.size() < 100000)\n return mint(inv_dp[x] = ((*this) ^ (mod - 2)).x);\n else return (*this) ^ (mod - 2);\n }\n \n mint operator+(const mint & b) const { return (x + b.x) % mod; }\n mint operator-(const mint & b) const { return (x - b.x + mod) % mod; }\n mint operator*(const mint & b) const { return (x * b.x) % mod; }\n mint operator/(const mint & b) const { return (*this) * b.inv(); }\n\n mint & operator+=(const mint & b) { x = (x + b.x) % mod; return *this; }\n mint & operator-=(const mint & b) { x = (x - b.x + mod) % mod; return *this; }\n mint & operator*=(const mint & b) { x = (x * b.x) % mod; return *this; }\n mint & operator/=(const mint & b) { x = (x * b.inv().x) % mod; return *this; }\n\n bool operator==(const mint & b) const { return x == b.x; }\n bool operator!=(const mint & b) const { return x != b.x; }\n};\nmint comb[MAXN][MAXN];\nvoid gen_comb(int n) {\n for(int i = 0; i <= n; i++) {\n comb[i][0] = comb[i][i] = 1;\n for(int j = 1; j < i; j++)\n comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];\n }\n}\nstring to_string(const mint & x) { return to_string(x.x); }\n\nmint a[MAXN],c[MAXN];\n\nmint dp[MAXN][MAXN][2];\nmint dpc[MAXN][MAXN][2];\n\nll s1(ll l, ll r) {\n\tif(l>r)return 0;\n\treturn (l+r)*(r-l+1)/2;\n}\n\nint main(int argc, char * argv[]) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n // solve_testcase;\n\n ll n,m;\n cin>>n>>m>>mod;\n\n gen_comb(MAXN-1);\n\n a[0]=0;\n c[0]=1;\n a[1]=0;\n c[1]=2;\n rep(i,2,m+1) {\n \tmint tmp=0;\n \tmint tmpc=0;\n \t// tmp+=c[i-1]*s1(1,i-1)*2+a[i-1]*(i+1)*2;\n \t// tmpc+=c[i-1]*2*(i+1);\n \trep(l,0,i) {\n \t\tll r=i-1-l;\n \t\ttmp+=c[l]*c[r]*(s1(1,l)+s1(1,r))*comb[i-1][l]+(a[l]*c[r]+a[r]*c[l])*comb[i-1][l]*(i+1);\n \t\ttmpc+=c[l]*c[r]*(i+1)*comb[i-1][l];\n \t}\n \ta[i]=tmp;\n \tc[i]=tmpc;\n }\n printn(a,m+1);\n printn(c,m+1);\n\n dpc[0][0][0]=1;\n rep(i,1,n+1) {\n \trep(j,0,m+1) {\n \t\tif(j>i) continue;\n\n \t\tdpc[i][j][0]=dpc[i-1][j][0]+dpc[i-1][j][1];\n \t\trep(k,1,j+1) {\n \t\t\tdpc[i][j][1]+=c[k]*comb[j][k]*dpc[i-k][j-k][0];\n \t\t}\n \t}\n }\n\n // dp[0][0][0]=0;\n rep(i,1,n+1) {\n \trep(j,0,m+1) {\n \t\tif(j>i) continue;\n\n \t\tdp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1];\n \t\trep(k,1,j+1) {\n \t\t\tdp[i][j][1]+=a[k]*comb[j][k]*dpc[i-k][j-k][0] + dp[i-k][j-k][0]*c[k]*comb[j][k];\n \t\t}\n \t}\n }\n\n cout<<(dp[n][m][0]+dp[n][m][1]).x<\n#include\n#include\n#include\n#define maxn 505\n#define ll long long \nusing namespace std;\n\nint n,m,mo,i,j,k;\nll f[maxn],g[maxn],C[maxn][maxn],ans;\nll f0[maxn][maxn],g0[maxn][maxn];\nll P(int n,int m){return C[n+m-1][m-1];}\n\nint main(){\n\tscanf(\"%d%d%d\",&n,&m,&mo);\n\tfor(C[0][0]=1,i=1;i<=n+1;i++) for(C[i][0]=1,j=1;j<=i;j++)\n\t\tC[i][j]=(C[i-1][j]+C[i-1][j-1])%mo;\n\tf[0]=1,g[0]=0,f[1]=2,g[1]=0;\n\tfor(i=2;i<=n;i++) {\n\t\t(f[i]+=f[i-1]*(i+1)*2)%=mo;\n\t\t(g[i]+=f[i-1]*i%mo*(i-1))%=mo;\n\t\t(g[i]+=g[i-1]*(i+1)*2)%=mo;\n\t\tfor(j=1;j+1=0)\n\t\t(ans+=g0[i][m]*P(n+1-m-i,i+1))%=mo;\n\tprintf(\"%lld\\n\",ans);\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "426bb1f42e70d6918f3e40d1eb517fca", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define ll long long\n#define f(i, x, n) for(int i = x; i < (int)n; ++i)\n\nint const N = 500;\nint md, ch[N + 1][N + 1];\npair dp1[N + 1], dp2[2][N + 1][N + 1];\n\nvoid ad(int &x, int y) { if ((x += y) >= md)x -= md; }\n\nint sm(int n) { return n * (n + 1) >> 1; }\n\nint main(){\n\tint n, m;\n\tscanf(\"%d%d%d\", &n, &m, &md);\n\t\n\tf(i, 0, m + 1){\n\t\tch[i][0] = 1;\n\t\tf(j, 1, i + 1)ad(ch[i][j] = ch[i - 1][j], ch[i - 1][j - 1]);\n\t}\n\t\n\tdp1[0].first = 1;\n\tf(i, 1, m + 1)f(j, 1, i + 1){\n\t\tint w = (ll)dp1[j - 1].first * dp1[i - j].first % md * ch[i - 1][j - 1] % md;\n\t\tad(dp1[i].second, (sm(j - 1) + sm(i - j)) * (ll)w % md);\n\t\tad(dp1[i].second, (ll)dp1[j - 1].second * dp1[i - j].first % md * (i + 1) % md * ch[i - 1][j - 1] % md);\n\t\tad(dp1[i].second, (ll)dp1[j - 1].first * dp1[i - j].second % md * (i + 1) % md * ch[i - 1][j - 1] % md);\n\t\tad(dp1[i].first, (ll)w * (i + 1) % md);\n\t}\n\t\n\tdp2[0][0][0].first = dp2[1][0][0].first = 1;\n\tf(i, 0, n)f(j, 0, i + 1)f(ln, 1, n - i + 1){\n\t\tad(dp2[1][i + ln][j].first, dp2[0][i][j].first);\n\t\tad(dp2[1][i + ln][j].second, dp2[0][i][j].second);\n\t\t\n\t\tad(dp2[0][i + ln][j + ln].first, (ll)dp2[1][i][j].first * dp1[ln].first % md * ch[j + ln][j] % md);\n\t\tad(dp2[0][i + ln][j + ln].second, (ll)dp2[1][i][j].first * dp1[ln].second % md * ch[j + ln][j] % md);\n\t\tad(dp2[0][i + ln][j + ln].second, (ll)dp2[1][i][j].second * dp1[ln].first % md * ch[j + ln][j] % md);\n\t}\n\t\n\tint an = dp2[0][n][m].second;\n\tad(an, dp2[1][n][m].second);\n\tprintf(\"%d\\n\", an);\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "da659a13942d4fb6ff45999d80599c62", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC target(\"arch=corei7-avx\")\n#include \n#ifndef ONLINE_JUDGE\n# include \n# include \n#endif\n\n/*** TEMPLATE CODE STARTS HERE ***/\n\n#ifndef M_PI\n#define M_PI 3.1415926535897932384626433832795028841971693993751\n#endif\n\nusing namespace std;\n\ntypedef vector vs;\ntypedef long long ll;\ntypedef complex pnt;\ntypedef vector vi;\ntypedef vector vll;\ntypedef pair pii;\ntypedef pair pll;\n\n#define RA(x) begin(x), end(x)\n#define FE(i, x) for (auto i = begin(x); i != end(x); ++i)\n#define SZ(x) ((ll) (x).size())\n\ntemplate\nvoid splitstr(const string &s, vector &out)\n{\n istringstream in(s);\n out.clear();\n copy(istream_iterator(in), istream_iterator(), back_inserter(out));\n}\n\ntemplate T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n\nstatic void redirect(int argc, const char **argv)\n{\n#ifndef ONLINE_JUDGE\n struct rlimit rlim;\n getrlimit(RLIMIT_STACK, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_STACK, &rlim);\n#ifndef __SANITIZE_ADDRESS__\n getrlimit(RLIMIT_DATA, &rlim);\n rlim.rlim_cur = 256 * 1024 * 1024;\n setrlimit(RLIMIT_DATA, &rlim);\n#endif\n#endif\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n if (argc > 1)\n {\n static filebuf f;\n f.open(argv[1], ios::in);\n cin.rdbuf(&f);\n if (!cin)\n {\n cerr << \"Failed to open '\" << argv[1] << \"'\" << endl;\n exit(1);\n }\n }\n\n if (argc > 2)\n {\n static filebuf f;\n f.open(argv[2], ios::out | ios::trunc);\n cout.rdbuf(&f);\n if (!cout)\n {\n cerr << \"Failed to open '\" << argv[2] << \"'\" << endl;\n }\n }\n cin.exceptions(ios::failbit);\n}\n\n/*** TEMPLATE CODE ENDS HERE */\n\nstatic ll choose[501][501];\n\nint main(int argc, const char **argv)\n{\n redirect(argc, argv);\n int N, M, P;\n cin >> N >> M >> P;\n for (int i = 0; i <= N; i++)\n {\n choose[i][0] = choose[i][i] = 1;\n for (int j = 1; j < i; j++)\n choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % P;\n }\n\n vector dp1(N + 1);\n vector dpw(N + 1);\n dp1[0] = 0;\n dpw[0] = 1;\n for (int m = 1; m <= M; m++)\n {\n for (int i = 0; i < m; i++)\n {\n ll w = dpw[i] * dpw[m - 1 - i] % P;\n w = w * choose[m - 1][i] % P;\n dpw[m] = (dpw[m] + w * (m + 1) % P) % P;\n\n ll s1 = dp1[i] * dpw[m - 1 - i] % P;\n ll s2 = dp1[m - 1 - i] * dpw[i] % P;\n ll add = (s1 + s2) % P * (m + 1) % P * choose[m - 1][i] % P;\n add += w * ((i * (i + 1) + (m - 1 - i) * (m - i)) / 2) % P;\n dp1[m] = (dp1[m] + add) % P;\n }\n }\n\n vector dp(N + 1, vll(N + 1)), dps(N + 1, vll(N + 1));\n dps[0][0] = 1;\n for (int i = 1; i <= N; i++)\n {\n dp[i][i] = dp1[i];\n dps[i][i] = dpw[i];\n dps[i][0] = 1;\n for (int j = 1; j < i; j++)\n {\n dp[i][j] = dp[i - 1][j];\n dps[i][j] = dps[i - 1][j];\n for (int k = 1; k <= j; k++)\n {\n dps[i][j] = (dps[i][j] + dpw[k] * dps[i - k - 1][j - k] % P * choose[j][k]) % P;\n ll add = (dp[i - k - 1][j - k] * dpw[k] % P + dps[i - k - 1][j - k] * dp1[k]) % P;\n dp[i][j] = (dp[i][j] + add * choose[j][k] % P) % P;\n }\n }\n }\n cout << dp[N][M] << '\\n';\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "41b33bd2ddec461c1a414be6d9cd14f5", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nconst int maxn = 510;\n\nint m, n, mod;\n\ninline int add (int x, int y) {return (1LL * x + y) % mod;}\ninline int sub (int x, int y) {return x - y < 0 ? x - y + mod : x - y;}\ninline int mul (int x, int y) {return 1LL * x * y % mod;}\nint fac[maxn], inv[maxn], invfac[maxn];\nvoid get_fac_and_inv(int n)\n{\n fac[0] = inv[1] = fac[1] = invfac[0] = invfac[1] = 1LL;\n for (int i = 2; i <= n; i++) inv[i] = mul((mod - mod / i), inv[mod % i]);\n for (int i = 2; i <= n; i++) {fac[i] = mul (fac[i - 1], i); invfac[i] = mul (invfac[i - 1], inv[i]);}\n}\ninline int C (int n, int m) {if (n < m || m < 0) return 0; return mul(mul(fac[n], invfac[m]), invfac[n - m]);}\ninline int P (int n, int m) {return C(n + m - 1, m - 1);}\n\n\nint f[maxn], g[maxn], s[maxn];\nint ff[maxn][maxn], gg[maxn][maxn];\n\nint main()\n{\n scanf (\"%d %d %d\", &n, &m, &mod);\n get_fac_and_inv (500);\n for (int i = 1; i <= n; i++) s[i] = add (s[i - 1], i);\n f[0] = 1; f[1] = 2;\n g[0] = 0; g[1] = 0;\n for (int i = 2; i <= n; i++)\n {\n for (int j = 0; j < i; j++)\n {\n int temp = mul (C (i - 1, j), mul(f[j], f[i - 1 - j]));\n f[i] = add (f[i], mul (temp, i + 1));\n g[i] = add (g[i], mul (temp, s[j] + s[i - 1 - j]));\n temp = mul (add (mul (g[j], f[i - 1 - j]), mul (g[i - 1 - j], f[j])), i + 1);\n g[i] = add (g[i], mul (temp, C(i - 1, j)));\n }\n }\n ff[0][0] = 1; gg[0][0] = 0;\n for (int i = 0; i <= m; i++)\n for (int j = 0; j <= m; j++)\n {\n if (!ff[i][j]) continue;\n for (int k = 1; k + j <= m; k++)\n {\n ff[i + 1][j + k] = add (ff[i + 1][j + k], mul (mul (ff[i][j], f[k]), C(j + k, k)));\n gg[i + 1][j + k] = add (gg[i + 1][j + k], mul (add (mul (gg[i][j], f[k]), mul (ff[i][j], g[k])), C(j + k, k)));\n }\n }\n int ans = 0;\n for (int i = 1; i <= m; i++)\n if (n + 1 - m >= i)\n ans = add (ans, mul (gg[i][m], P (n + 1 - m - i, i + 1)));\n printf (\"%d\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "ce94246bfa1977f5bb129d3bedd388d8", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\ntemplate struct tensor_view {\n\tstatic_assert(NDIMS >= 0, \"NDIMS must be nonnegative\");\n\nprotected:\n\tstd::array shape;\n\tstd::array strides;\n\tT* data;\n\n\ttensor_view(std::array shape_, std::array strides_, T* data_) : shape(shape_), strides(strides_), data(data_) {}\n\npublic:\n\ttensor_view() : shape{0}, strides{0}, data(nullptr) {}\n\nprotected:\n\tint flatten_index(std::array idx) const {\n\t\tint res = 0;\n\t\tfor (int i = 0; i < NDIMS; i++) { res += idx[i] * strides[i]; }\n\t\treturn res;\n\t}\n\tint flatten_index_checked(std::array idx) const {\n\t\tint res = 0;\n\t\tfor (int i = 0; i < NDIMS; i++) {\n\t\t\tassert(0 <= idx[i] && idx[i] < shape[i]);\n\t\t\tres += idx[i] * strides[i];\n\t\t}\n\t\treturn res;\n\t}\n\npublic:\n\tT& operator[] (std::array idx) const {\n\t\treturn data[flatten_index(idx)];\n\t}\n\tT& at(std::array idx) const {\n\t\treturn data[flatten_index_checked(idx)];\n\t}\n\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> operator[] (int idx) const {\n\t\tstd::array nshape; std::copy(shape.begin()+1, shape.end(), nshape.begin());\n\t\tstd::array nstrides; std::copy(strides.begin()+1, strides.end(), nstrides.begin());\n\t\tT* ndata = data + (strides[0] * idx);\n\t\treturn tensor_view(nshape, nstrides, ndata);\n\t}\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> at(int idx) const {\n\t\tassert(0 <= idx && idx < shape[0]);\n\t\treturn operator[](idx);\n\t}\n\n\ttemplate \n\tstd::enable_if_t<(0 == D), T&> operator * () const {\n\t\treturn *data;\n\t}\n\n\ttemplate friend struct tensor_view;\n\ttemplate friend struct tensor;\n};\n\ntemplate struct tensor {\n\tstatic_assert(NDIMS >= 0, \"NDIMS must be nonnegative\");\n\nprotected:\n\tstd::array shape;\n\tstd::array strides;\n\tint len;\n\tT* data;\n\npublic:\n\ttensor() : shape{0}, strides{0}, len(0), data(nullptr) {}\n\n\texplicit tensor(std::array shape_, const T& t = T()) {\n\t\tshape = shape_;\n\t\tstrides[NDIMS-1] = 1;\n\t\tfor (int i = NDIMS-1; i > 0; i--) {\n\t\t\tstrides[i-1] = strides[i] * shape[i];\n\t\t}\n\t\tlen = strides[0] * shape[0];\n\t\tdata = new T[len];\n\t\tstd::fill(data, data + len, t);\n\t}\n\n\ttensor(const tensor& o) : shape(o.shape), strides(o.strides), len(o.len), data(new T[len]) {\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tdata[i] = o.data[i];\n\t\t}\n\t}\n\n\ttensor& operator=(tensor&& o) noexcept {\n\t\tusing std::swap;\n\t\tswap(shape, o.shape);\n\t\tswap(strides, o.strides);\n\t\tswap(len, o.len);\n\t\tswap(data, o.data);\n\t\treturn *this;\n\t}\n\ttensor(tensor&& o) : tensor() {\n\t\t*this = std::move(o);\n\t}\n\ttensor& operator=(const tensor& o) {\n\t\treturn *this = tensor(o);\n\t}\n\t~tensor() { delete[] data; }\n\n\tusing view_t = tensor_view;\n\tview_t view() {\n\t\treturn tensor_view(shape, strides, data);\n\t}\n\toperator view_t() {\n\t\treturn view();\n\t}\n\n\tusing const_view_t = tensor_view;\n\tconst_view_t view() const {\n\t\treturn tensor_view(shape, strides, data);\n\t}\n\toperator const_view_t() const {\n\t\treturn view();\n\t}\n\n\tT& operator[] (std::array idx) { return view()[idx]; }\n\tT& at(std::array idx) { return view().at(idx); }\n\tconst T& operator[] (std::array idx) const { return view()[idx]; }\n\tconst T& at(std::array idx) const { return view().at(idx); }\n\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> operator[] (int idx) {\n\t\treturn view()[idx];\n\t}\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> at(int idx) {\n\t\treturn view().at(idx);\n\t}\n\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> operator[] (int idx) const {\n\t\treturn view()[idx];\n\t}\n\ttemplate \n\tstd::enable_if_t<(0 < D), tensor_view> at(int idx) const {\n\t\treturn view().at(idx);\n\t}\n\n\ttemplate \n\tstd::enable_if_t<(0 == D), T&> operator * () {\n\t\treturn *view();\n\t}\n\ttemplate \n\tstd::enable_if_t<(0 == D), const T&> operator * () const {\n\t\treturn *view();\n\t}\n};\n\ntemplate struct modnum {\n\tstatic constexpr int& MOD = MOD_;\n\t//static_assert(MOD_ > 0, \"MOD must be positive\");\n\nprivate:\n\tusing ll = long long;\n\n\tint v;\n\n\tstatic int minv(int a, int m) {\n\t\ta %= m;\n\t\tassert(a);\n\t\treturn a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);\n\t}\n\npublic:\n\n\tmodnum() : v(0) {}\n\tmodnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }\n\texplicit operator int() const { return v; }\n\tfriend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }\n\tfriend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }\n\n\tfriend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }\n\tfriend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }\n\n\tmodnum inv() const {\n\t\tmodnum res;\n\t\tres.v = minv(v, MOD);\n\t\treturn res;\n\t}\n\tfriend modnum inv(const modnum& m) { return m.inv(); }\n\tmodnum neg() const {\n\t\tmodnum res;\n\t\tres.v = v ? MOD-v : 0;\n\t\treturn res;\n\t}\n\tfriend modnum neg(const modnum& m) { return m.neg(); }\n\n\tmodnum operator- () const {\n\t\treturn neg();\n\t}\n\tmodnum operator+ () const {\n\t\treturn modnum(*this);\n\t}\n\n\tmodnum& operator ++ () {\n\t\tv ++;\n\t\tif (v == MOD) v = 0;\n\t\treturn *this;\n\t}\n\tmodnum& operator -- () {\n\t\tif (v == 0) v = MOD;\n\t\tv --;\n\t\treturn *this;\n\t}\n\tmodnum& operator += (const modnum& o) {\n\t\tv -= MOD-o.v;\n\t\tv = (v < 0) ? v + MOD : v;\n\t\treturn *this;\n\t}\n\tmodnum& operator -= (const modnum& o) {\n\t\tv -= o.v;\n\t\tv = (v < 0) ? v + MOD : v;\n\t\treturn *this;\n\t}\n\tmodnum& operator *= (const modnum& o) {\n\t\tv = int(ll(v) * ll(o.v) % MOD);\n\t\treturn *this;\n\t}\n\tmodnum& operator /= (const modnum& o) {\n\t\treturn *this *= o.inv();\n\t}\n\n\tfriend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }\n\tfriend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }\n\tfriend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }\n\tfriend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }\n\tfriend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }\n\tfriend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }\n};\n\nint MOD;\n\nint main() {\n\tusing namespace std;\n\tios_base::sync_with_stdio(false), cin.tie(nullptr);\n\tint N, M; cin >> N >> M >> MOD;\n\tN++;\n\tusing num = modnum;\n\tvector fact(N+1);\n\tfact[0] = 1;\n\tfor (int i = 1; i <= N; i++) fact[i] = fact[i-1] * num(i);\n\tvector ifact(N+1);\n\tifact[N] = inv(fact[N]);\n\tfor (int i = N; i >= 1; i--) ifact[i-1] = ifact[i] * num(i);\n\n\tvector ways(N+1);\n\tvector tot_val(N+1);\n\tways[0] = 1, tot_val[0] = 0;\n\n\tfor (int l = 1; l <= N; l++) {\n\t\tfor (int i = 0; i < l; i++) {\n\t\t\tint a = i, b = l-1-i;\n\n\t\t\tnum choose = fact[a+b] * ifact[a] * ifact[b];\n\t\t\tnum sub_ways = ways[a] * ways[b] * choose;\n\t\t\tnum sub_val = (tot_val[a] * ways[b] + tot_val[b] * ways[a]) * choose;\n\n\t\t\tways[l] += sub_ways * num(l+1);\n\t\t\ttot_val[l] += sub_val * num(l+1) + sub_ways * num(a * (a+1) / 2 + b * (b+1) / 2);\n\t\t}\n\t}\n\n\ttensor pref_ways({N+1, M+1});\n\ttensor pref_vals({N+1, M+1});\n\tpref_ways[{0,0}] = 1;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j <= M; j++) {\n\t\t\tfor (int k = 0; j+k <= M && i+1+k <= N; k++) {\n\t\t\t\tnum choose = fact[j+k] * ifact[j] * ifact[k];\n\t\t\t\tpref_ways[{i+1+k,j+k}] += pref_ways[{i,j}] * ways[k] * choose;\n\t\t\t\tpref_vals[{i+1+k,j+k}] += (pref_ways[{i,j}] * tot_val[k] + pref_vals[{i,j}] * ways[k]) * choose;\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << pref_vals[{N,M}] << '\\n';\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "fa9c13a4c196d2a1888b23b7cd4f3a8c", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\ntemplate \nstring to_string(pair p);\n\ntemplate \nstring to_string(tuple p);\n\ntemplate \nstring to_string(tuple p);\n\nstring to_string(const string& s) {\n return '\"' + s + '\"';\n}\n\nstring to_string(const char* s) {\n return to_string((string) s);\n}\n\nstring to_string(bool b) {\n return (b ? \"true\" : \"false\");\n}\n\nstring to_string(vector v) {\n bool first = true;\n string res = \"{\";\n for (int i = 0; i < static_cast(v.size()); i++) {\n if (!first) {\n res += \", \";\n }\n first = false;\n res += to_string(v[i]);\n }\n res += \"}\";\n return res;\n}\n\ntemplate \nstring to_string(bitset v) {\n string res = \"\";\n for (size_t i = 0; i < N; i++) {\n res += static_cast('0' + v[i]);\n }\n return res;\n}\n\ntemplate \nstring to_string(A v) {\n bool first = true;\n string res = \"{\";\n for (const auto &x : v) {\n if (!first) {\n res += \", \";\n }\n first = false;\n res += to_string(x);\n }\n res += \"}\";\n return res;\n}\n\ntemplate \nstring to_string(pair p) {\n return \"(\" + to_string(p.first) + \", \" + to_string(p.second) + \")\";\n}\n\ntemplate \nstring to_string(tuple p) {\n return \"(\" + to_string(get<0>(p)) + \", \" + to_string(get<1>(p)) + \", \" + to_string(get<2>(p)) + \")\";\n}\n\ntemplate \nstring to_string(tuple p) {\n return \"(\" + to_string(get<0>(p)) + \", \" + to_string(get<1>(p)) + \", \" + to_string(get<2>(p)) + \", \" + to_string(get<3>(p)) + \")\";\n}\n\nvoid debug_out() { cerr << endl; }\n\ntemplate \nvoid debug_out(Head H, Tail... T) {\n cerr << \" \" << to_string(H);\n debug_out(T...);\n}\n\n#define debug(...) cerr << \"[\" << #__VA_ARGS__ << \"]:\", debug_out(__VA_ARGS__);\n\ntemplate \nT inverse(T a, T m) {\n T u = 0, v = 1;\n while (a != 0) {\n T t = m / a;\n m -= t * a; swap(a, m);\n u -= t * v; swap(u, v);\n }\n assert(m == 1);\n return u;\n}\n\ntemplate \nclass Modular {\n public:\n using Type = typename decay::type;\n\n constexpr Modular() : value() {}\n template \n Modular(const U& x) {\n value = normalize(x);\n }\n\n template \n static Type normalize(const U& x) {\n Type v;\n if (-mod() <= x && x < mod()) v = static_cast(x);\n else v = static_cast(x % mod());\n if (v < 0) v += mod();\n return v;\n }\n\n const Type& operator()() const { return value; }\n template \n explicit operator U() const { return static_cast(value); }\n constexpr static Type mod() { return T::value; }\n\n Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }\n Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }\n template Modular& operator+=(const U& other) { return *this += Modular(other); }\n template Modular& operator-=(const U& other) { return *this -= Modular(other); }\n Modular& operator++() { return *this += 1; }\n Modular& operator--() { return *this -= 1; }\n Modular operator++(int) { Modular result(*this); *this += 1; return result; }\n Modular operator--(int) { Modular result(*this); *this -= 1; return result; }\n Modular operator-() const { return Modular(-value); }\n\n template \n typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {\n#ifdef _WIN32\n uint64_t x = static_cast(value) * static_cast(rhs.value);\n uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m;\n asm(\n \"divl %4; \\n\\t\"\n : \"=a\" (d), \"=d\" (m)\n : \"d\" (xh), \"a\" (xl), \"r\" (mod())\n );\n value = m;\n#else\n value = normalize(static_cast(value) * static_cast(rhs.value));\n#endif\n return *this;\n }\n template \n typename enable_if::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {\n int64_t q = static_cast(static_cast(value) * rhs.value / mod());\n value = normalize(value * rhs.value - q * mod());\n return *this;\n }\n template \n typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) {\n value = normalize(value * rhs.value);\n return *this;\n }\n\n Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }\n\n template \n friend bool operator==(const Modular& lhs, const Modular& rhs);\n\n template \n friend bool operator<(const Modular& lhs, const Modular& rhs);\n\n template \n friend std::istream& operator>>(std::istream& stream, Modular& number);\n\n private:\n Type value;\n};\n\ntemplate bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; }\ntemplate bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); }\ntemplate bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; }\n\ntemplate bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); }\ntemplate bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); }\ntemplate bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); }\n\ntemplate bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; }\n\ntemplate Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; }\ntemplate Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; }\ntemplate Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; }\n\ntemplate Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; }\ntemplate Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; }\ntemplate Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; }\n\ntemplate Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; }\ntemplate Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; }\ntemplate Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; }\n\ntemplate Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; }\ntemplate Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; }\ntemplate Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; }\n\ntemplate\nModular power(const Modular& a, const U& b) {\n assert(b >= 0);\n Modular x = a, res = 1;\n U p = b;\n while (p > 0) {\n if (p & 1) res *= x;\n x *= x;\n p >>= 1;\n }\n return res;\n}\n\ntemplate \nstring to_string(const Modular& number) {\n return to_string(number());\n}\n\ntemplate \nstd::ostream& operator<<(std::ostream& stream, const Modular& number) {\n return stream << number();\n}\n\ntemplate \nstd::istream& operator>>(std::istream& stream, Modular& number) {\n typename common_type::Type, int64_t>::type x;\n stream >> x;\n number.value = Modular::normalize(x);\n return stream;\n}\n\n\nusing ModType = int;\n\nstruct VarMod { static ModType value; };\nModType VarMod::value;\nModType& md = VarMod::value;\nusing Mint = Modular;\n\n\n/*constexpr int md = 1000000009;\nusing Mint = Modular::type, md>>;*/\n\nint n, m;\n\nint main() {\n cin >> n >> m >> md;\n vector fact = {1};\n vector inv_fact = {1};\n auto C = [&](int x, int y) {\n int sz = max(x, y);\n while((int) fact.size() < sz + 1) {\n fact.push_back(fact.back() * fact.size());\n inv_fact.push_back(1 / fact.back());\n }\n if(y < 0 || y > x) return Mint(0);\n return fact[x] * inv_fact[y] * inv_fact[x - y];\n };\n vector ptr_dp(m + 1);\n vector ptr_ways(m + 1);\n ptr_ways[0] = 1;\n for(int i = 1; i <= m; i++) {\n for(int j = 0; j < i; j++) {\n int l = j, r = i - 1 - l;\n ptr_ways[i] += ptr_ways[l] * ptr_ways[r] * C(l + r, l) * (l + r + 2);\n }\n for(int j = 0; j < i; j++) {\n int l = j, r = i - 1 - l;\n Mint add = (Mint(l) * (l + 1) + Mint(r) * (r + 1)) / 2;\n ptr_dp[i] += ptr_ways[l] * ptr_ways[r] * C(l + r, l) * add;\n ptr_dp[i] += (ptr_dp[l] * ptr_ways[r] + ptr_dp[r] * ptr_ways[l]) * C(l + r, r) * (l + r + 2);\n }\n }\n vector> dp(n + 1, vector(m + 1));\n vector> ways(n + 1, vector(m + 1));\n ways[0][0] = 1;\n for(int i = 1; i <= n; i++) {\n for(int s = 0; s <= min(m, i); s++) {\n if(s == i) {\n dp[i][s] = ptr_dp[s];\n ways[i][s] = ptr_ways[s];\n continue;\n }\n for(int d = 1; d <= s + 1; d++) {\n Mint ft = dp[i - d][s + 1 - d];\n Mint way = ways[i - d][s + 1 - d];\n int add = d - 1;\n ways[i][s] += way * ptr_ways[add] * C(s, add);\n dp[i][s] += (way * ptr_dp[add] + ptr_ways[add] * ft) * C(s, add);\n }\n }\n }\n cout << dp[n][m];\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "234426c5fceff4d69dec4ccf8d96940b", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#endif\n\n#include \n\nusing namespace std;\n\ntypedef long double ld;\n\n#ifdef DEBUG\n#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)\n#else\n#define eprintf(...) ;\n#endif\n\n#define sz(x) ((int) (x).size())\n#define TASK \"text\"\n\nconst int inf = (int) 1.01e9;\nconst long long infll = (long long) 1.01e18;\nconst ld eps = 1e-9;\nconst ld pi = acos((ld) -1);\n\n#ifdef DEBUG\nmt19937 mrand(300); \n#else\nmt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); \n#endif\n\nint rnd(int x) {\n return mrand() % x;\n}\n\nvoid precalc() {\n}\n\nconst int maxn = 505;\nint n, m, mod;\n\nbool read() {\n if (scanf(\"%d%d%d\", &n, &m, &mod) < 3) {\n return false;\n }\n return true;\n}\n\nint mul(int a, int b) {\n return (long long) a * b % mod;\n}\n\nvoid add(int &a, int b) {\n a += b;\n if (a >= mod) {\n a -= mod;\n }\n}\n\nint c[maxn][maxn];\n\nstruct T {\n int cnt, sum;\n\n T(): cnt(0), sum(0) {}\n\n T(int x): cnt(x), sum(0) {}\n\n T(int c, int s): cnt(c), sum(s) {}\n\n T operator * (const T &t) const {\n int s = mul(cnt, t.sum);\n add(s, mul(sum, t.cnt));\n return T(mul(cnt, t.cnt), s);\n }\n\n T operator + (const T &t) const {\n int c = cnt;\n add(c, t.cnt);\n int s = sum;\n add(s, t.sum);\n return T(c, s);\n }\n};\n\nT dp[maxn];\nT pdp[maxn][maxn];\n\nvoid solve() {\n {\n for (int i = 0; i <= m; ++i) {\n c[i][0] = 1;\n for (int j = 1; j <= i; ++j) {\n c[i][j] = c[i - 1][j - 1];\n add(c[i][j], c[i - 1][j]);\n }\n }\n }\n {\n dp[0] = T(1);\n for (int k = 1; k <= m; ++k) {\n auto &cur = dp[k];\n cur = T(0);\n for (int i = 0; i < k; ++i) {\n auto x = dp[i] * dp[k - i - 1];\n x = x * T(c[k - 1][i]);\n for (int j = 0; j < k; ++j) {\n auto y = x * T(1 + (j == i), abs(i - j));\n cur = cur + y;\n }\n }\n eprintf(\"%d %d\\n\", dp[k].cnt, dp[k].sum);\n }\n }\n for (int i = 0; i <= n; ++i) {\n for (int j = 0; j <= m; ++j) {\n pdp[i][j] = T(0);\n }\n }\n pdp[0][0] = T(1);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n auto cur = pdp[i][j];\n if (!cur.cnt && !cur.sum) {\n continue;\n }\n {\n auto &nxt = pdp[i + 1][j];\n nxt = nxt + cur;\n }\n for (int k = 1; j + k <= m && i + k <= n; ++k) {\n auto &nxt = pdp[min(n, i + k + 1)][j + k];\n nxt = nxt + cur * dp[k] * T(c[m - j][k]);\n }\n }\n }\n printf(\"%d\\n\", pdp[n][m].sum);\n}\n\nint main() {\n precalc();\n#ifdef DEBUG\n assert(freopen(TASK \".in\", \"r\", stdin));\n assert(freopen(TASK \".out\", \"w\", stdout));\n#endif\n while (read()) {\n solve();\n#ifdef DEBUG\n eprintf(\"Time %.2f\\n\", (double) clock() / CLOCKS_PER_SEC);\n#endif\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "d9755c5d1a921c2ebdecb01941ba9344", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define rep(i, a, b) for(int i=(a); i<(b); i++)\n#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)\n#define sz(a) (int)a.size()\n#define de(a) cout << #a << \" = \" << a << endl\n#define dd(a) cout << #a << \" = \" << a << \" \"\n#define all(a) a.begin(), a.end()\n#define pw(x) (1ll<<(x))\n#define endl \"\\n\"\ntypedef double db;\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\nconst int N = 505;\nint n, m, mod;\nint C[N][N];\nint way[N];\nint sum[N];\nint g[N][N], f[N][N];\nint add(int a, int b){\n\tif(a + b >= mod) return a + b - mod;\n\treturn a + b;\n}\nint mul(int a, int b){\n\treturn (ll)a * b % mod;\n}\nint get(int i){\n\treturn i * (i + 1) / 2;\n}\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);cout.tie(0);\n\t//cout << setiosflags(ios::fixed);\n\t//cout << setprecision(2);\n\tcin >> n >> m >> mod;\n\trep(i, 0, m + 1){\n\t\tC[i][0] = 1;\n\t\trep(j, 1, i + 1){\n\t\t\tC[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);\n\t\t}\n\t}\n\tway[0] = 1;\n\trep(i, 1, m + 1){\n\t\trep(j, 0, i){\n\t\t\tway[i] = add(way[i], mul(mul(i + 1, C[i - 1][j]), mul(way[j], way[i - j - 1])));\n\t\t\tsum[i] = add(sum[i], mul(mul(mul(add(get(j), get(i - j - 1)), way[j]), way[i - j - 1]), C[i - 1][j]));\n\t\t\tsum[i] = add(sum[i], mul(mul(i + 1, add(mul(sum[j], way[i - j - 1]), mul(way[j], sum[i - j - 1]))), C[i - 1][j]));\n\t\t}\n\t}\n\tn ++;\n\tf[0][0] = 1;\n\trep(i, 0, n){\n\t\trep(j, 0, m + 1){\n\t\t\tfor(int k = 0; j + k <= m && i + k + 1 <= n; k ++){\n\t\t\t\tf[i + k + 1][j + k] = add(f[i + k + 1][j + k], mul(mul(f[i][j], way[k]), C[j + k][j]));\n\t\t\t\tg[i + k + 1][j + k] = add(g[i + k + 1][j + k], mul(add(mul(f[i][j], sum[k]), mul(g[i][j], way[k])), C[j + k][j]));\n\t\t\t}\n\t\t}\n\t}\n\tcout << g[n][m] << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "b6b0a5930b220fdfc42c66c06e47440b", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\nusing namespace std;\nint m,n,p,dp[505],cnt[505],dp2[505][505],tot[505][505],fac[505],invfac[505],tmp,zh;\nint mypow(int a,int b)\n{\n\tint ans=1;\n\twhile(b)\n\t{\n\t\tif(b&1)ans=1ll*ans*a%p;\n\t\ta=1ll*a*a%p;\n\t\tb>>=1;\n\t}\n\treturn ans;\n}\nint C(int n,int m){return 1ll*fac[n]*invfac[m]%p*invfac[n-m]%p;}\nint sum(int x){return (1ll*x*(x+1ll)/2ll)%p;}\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&m,&p);fac[0]=1;\n\tfor(int i=1;i<=500;i++)fac[i]=1ll*fac[i-1]*i%p;\n\tinvfac[500]=mypow(fac[500],p-2);\n\tfor(int i=499;i>=0;i--)invfac[i]=1ll*invfac[i+1]*(i+1)%p;\n\tcnt[0]=1;dp[0]=0;\n\tcnt[1]=2;dp[1]=0;\n\tfor(int i=2;i<=m;i++)\n\t{\n\t\tfor(int j=1;j<=i;j++)\n\t\t{\n\t\t\tzh=C(i-1,j-1);\n\t\t\ttmp=1ll*cnt[j-1]*cnt[i-j]%p*zh%p;\n\t\t\t//printf(\"@%d %d\\n\",tmp,zh);\n\t\t\tdp[i]=(1ll*dp[i]+1ll*tmp*sum(j-1)+1ll*zh*j%p*(1ll*dp[j-1]*cnt[i-j]%p+1ll*dp[i-j]*cnt[j-1]%p)%p)%p;\n\t\t\tcnt[i]=(1ll*cnt[i]+1ll*tmp*j)%p;\n\t\t\tdp[i]=(1ll*dp[i]+1ll*tmp*sum(i-j)+1ll*zh*(i-j+1)%p*(1ll*dp[j-1]*cnt[i-j]%p+1ll*dp[i-j]*cnt[j-1]%p)%p)%p;\n\t\t\tcnt[i]=(1ll*cnt[i]+1ll*tmp*(i-j+1))%p;\n\t\t}\n\t}\n\tdp2[0][0]=0;tot[0][0]=1;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=0;j<=min(m,i);j++)\n\t\t{\n\t\t\tdp2[i][j]=dp2[i-1][j];\n\t\t\ttot[i][j]=tot[i-1][j];\n\t\t\tfor(int k=1;k<=j;k++)\n\t\t\t{\n\t\t\t\tif(k\nusing namespace std;\nconst int N = 577;\nint n, m, Mod, dp[N], dp2[N], C[N][N], res[N][N], res2[N][N];\ninline int GetSum(int l, int r)\n{\n return ((l + r) * 1LL * (r - l + 1) / 2) % Mod;\n}\nint main()\n{\n scanf(\"%d%d%d\", &n, &m, &Mod);\n for (int i = C[0][0] = 1; i < N; i ++)\n for (int j = C[0][i] = C[i][i] = 1; j < i; j ++)\n C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;\n dp[0] = 1; dp2[0] = 0;\n for (int len = 1; len <= n; len ++)\n {\n for (int i = 1; i <= len; i ++)\n {\n int sm = GetSum(1, i - 1) + GetSum(1, len - i);\n int cnt = len + 1;\n dp[len] = (dp[len] + dp[i - 1] * 1LL * cnt % Mod * dp[len - i] % Mod * C[i - 1][len - 1]) % Mod;\n dp2[len] = (dp2[len] + sm * 1LL * dp[i - 1] % Mod % Mod * dp[len - i] % Mod * C[i - 1][len - 1]) % Mod;\n dp2[len] = (dp2[len] + dp2[i - 1] * 1LL * cnt % Mod * dp[len - i] % Mod * C[i - 1][len - 1]) % Mod;\n dp2[len] = (dp2[len] + dp2[len - i] * 1LL * cnt % Mod * dp[i - 1] % Mod * C[i - 1][len - 1]) % Mod;\n }\n }\n\n for (int i = 1; i <= n; i ++)\n for (int j = 0; j <= m && j <= i; j ++)\n {\n res[i][j] = res[i - 1][j];\n res2[i][j] = res2[i - 1][j];\n for (int h = 1; h < j; h ++)\n {\n res[i][j] = (res[i][j] + res[i - h - 1][j - h] * 1LL * dp[h] % Mod * C[h][j]) % Mod;\n res2[i][j] = (res2[i][j] + res2[i - h - 1][j - h] * 1LL * dp[h] % Mod * C[h][j]) % Mod;\n res2[i][j] = (res2[i][j] + res[i - h - 1][j - h] * 1LL * dp2[h] % Mod * C[h][j]) % Mod;\n }\n res[i][j] = (res[i][j] + dp[j]) % Mod;\n res2[i][j] = (res2[i][j] + dp2[j]) % Mod;\n }\n return !printf(\"%d\\n\", res2[n][m]);\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "e0c76b4cf08eeeb5655cad6f89c031e5", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define fo(a,b,c) for (a=b; a<=c; a++)\n#define fd(a,b,c) for (a=b; a>=c; a--)\n#define add(a,b) a=((a)+(b))%mod\n#define min(a,b) (a>=1;} return ans;}\n\nint main()\n{\n\t#ifdef file\n\tfreopen(\"CF1439D.in\",\"r\",stdin);\n\t#endif\n\t\n\tscanf(\"%d%d%d\",&n,&m,&mod),Mod=mod-2;\n\tjc[0]=1;\n\tfo(i,1,n) jc[i]=jc[i-1]*i%mod;\n\tJc[n]=qpower(jc[n],Mod);\n\tfd(i,n-1,0) Jc[i]=Jc[i+1]*(i+1)%mod;\n\tC[0][0]=1;\n\tfo(i,1,n)\n\t{\n\t\tC[i][0]=C[i][i]=1;\n\t\tfo(j,1,i-1) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;\n\t}\n\t\n\tf[0]=1;g[0]=0;\n\tfo(i,1,m)\n\t{\n\t\tfo(j,1,i)\n\t\t{\n\t\t\ts=(j*(j-1)/2+(i-j+1)*(i-j)/2)%mod;\n\t\t\tadd(f[i],f[j-1]*f[i-j]%mod*(i+1)%mod*C[i-1][j-1]);\n\t\t\tadd(g[i],((g[j-1]*f[i-j]+f[j-1]*g[i-j])%mod*(i+1)+f[j-1]*f[i-j]%mod*s)%mod*C[i-1][j-1]);\n\t\t}\n\t}\n\t\n\th[0][0][0]=1;\n\tfo(i,0,min(m,n-m+1)-1)\n\t{\n\t\tfo(j,0,m-1)\n\t\t{\n\t\t\tfo(k,1,m-j)\n\t\t\t{\n\t\t\t\tF=f[k]*C[j+k][j]%mod;\n\t\t\t\tadd(h[i+1][j+k][0],h[i][j][0]*F);\n\t\t\t\tadd(h[i+1][j+k][1],h[i][j][1]*F);\n\t\t\t\tadd(h[i+1][j+k][1],h[i][j][0]*g[k]%mod*C[j+k][j]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tfo(i,1,min(m,n-m+1)) add(ans,h[i][m][1]*C[n-m+1][i]);\n\tprintf(\"%lld\\n\",(ans+mod)%mod);\n\t\n\tfclose(stdin);\n\tfclose(stdout);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "db86dcfda19186045d707f8e2584df15", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\nusing namespace std;\nint n,m,mod;\nint g[505],sg[505];\nint f[505][505],sf[505][505];\nnamespace usual{\n\tint C[505][505];\n\tint add(int ta,int tb){ return ta+tb>=mod?ta+tb-mod:ta+tb; }\n\tint sub(int ta,int tb){ return taj) break;\n\t\t\t\tint tmp=1ll*g[i-t]*C[j][i-t]%mod;\n\t\t\t\tf[i][j]=add(f[i][j],1ll*f[t-1][j-i+t]*tmp%mod);\n\t\t\t\tsf[i][j]=add(sf[i][j],1ll*sf[t-1][j-i+t]*tmp%mod);\n\t\t\t\tsf[i][j]=add(sf[i][j],1ll*f[t-1][j-i+t]*sg[i-t]%mod*C[j][i-t]%mod);\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",sf[n][m]);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "3f5619d97784cab04519bd8074b117c4", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n#define ll long long\nconst int maxn = 505;\nint n, m, mod;\ninline int add(int x, int y) {\n\tx += y;\n\treturn x >= mod ? x - mod : x;\n}\ninline int mul(int x, int y) { return 1ll * x * y % mod; }\nint C[maxn][maxn];\nint f[maxn], g[maxn];\nint F[maxn][maxn], G[maxn][maxn];\nint main() {\n\tscanf(\"%d%d%d\", &n, &m, &mod);\n\tfor(int i = 0; i <= n; i++) {\n\t\tC[i][0] = 1;\n\t\tfor(int j = 1; j <= i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);\n\t}\n\tf[0] = 1;\n\tfor(int i = 1; i <= n; i++) {\n\t\tfor(int j = 1; j <= i; j++) {\n\t\t\tf[i] = add(f[i], mul(mul(f[j - 1], f[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(f[j - 1], g[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(g[j - 1], f[i - j]), mul(i + 1, C[i - 1][j - 1])));\n\t\t\tg[i] = add(g[i], mul(mul(f[j - 1], f[i - j]), mul(add(C[j][2], C[i - j + 1][2]), C[i - 1][j - 1])));\n\t\t}\n\t}\n\tF[0][0] = 1;\n\tfor(int i = 1; i <= n; i++) {\n\t\tF[i][i] = f[i], G[i][i] = g[i];\n\t\tfor(int j = 0; j <= min(i - 1, m); j++) {\n\t\t\tfor(int l = 0; l <= j; l++) {\n\t\t\t\tF[i][j] = add(F[i][j], mul(mul(f[l], F[i - l - 1][j - l]), C[j][l]));\n\t\t\t\tG[i][j] = add(G[i][j], mul(mul(g[l], F[i - l - 1][j - l]), C[j][l]));\n\t\t\t\tG[i][j] = add(G[i][j], mul(mul(f[l], G[i - l - 1][j - l]), C[j][l]));\n\t\t\t}\n\t\t}\n\t}\n\tcout << G[n][m] << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "5853df2f7712e7a1bb61192cc36ba08e", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\nusing namespace std;\nint mod;\ninline int addmod(int x)\n{\n\treturn x>=mod?x-mod:x;\n}\ninline int submod(int x)\n{\n\treturn x<0?x+mod:x;\n}\nint fpow(int x,int y)\n{\n\tint ans=1;\n\twhile(y)\n\t{\n\t\tif(y&1) ans=1ll*ans*x%mod;\n\t\tx=1ll*x*x%mod;\n\t\ty/=2;\n\t}\n\treturn ans;\n}\nstruct pt\n{\n\tint f,g;\n\tpt(int f=0,int g=0):f(f),g(g){}\n};\npt operator+(const pt x,const pt y)\n{\n\treturn pt(addmod(x.f+y.f),addmod(x.g+y.g));\n}\npt operator*(const pt x,const pt y)\n{\n\treturn pt(1ll*x.f*y.f%mod,addmod(1ll*x.f*y.g%mod+1ll*x.g*y.f%mod));\n}\nint n,m,pn=1e5,fr[100005],infr[100005],inv[100005];\npt dp[1005],up[1005][1005];\nint F(int x)\n{\n\treturn 1ll*x*(x+1)/2%mod;\n}\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&m,&mod);\n\tfr[0]=infr[0]=1;\n\tfor(int i=1;i<=pn;i++)\n\t{\n\t\tfr[i]=1ll*i*fr[i-1]%mod;\n\t\tinfr[i]=fpow(fr[i],mod-2);\n\t\tinv[i]=fpow(i,mod-2);\n\t}\n\tdp[0]=pt(1,0);\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=i;j++)\n\t\t{\n\t\t\tpt nw=dp[j-1]*dp[i-j];\n\t\t\tnw.g=addmod(1ll*nw.g*(i+1)%mod+1ll*nw.f*addmod(F(j-1)+F(i-j))%mod);\n\t\t\tnw.g=1ll*nw.g*inv[i]%mod;\n\t\t\tnw.f=1ll*nw.f*(i+1)%mod*inv[i]%mod;\n\t\t\tdp[i]=dp[i]+nw;\n\t\t}\n\t//\tprintf(\"i=%d,dp=%lld,%lld\\n\",i,1ll*dp[i].f*fr[i]%mod,1ll*dp[i].g*fr[i]%mod);\n\t}\n\tup[0][0]=pt(1,0);\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=0;j<=min(i,m);j++)\n\t\t{\n\t\t\tif(i==j) up[i][j]=dp[i];\n\t\t\tfor(int k=0;k<=j&&k\n#define MP make_pair\n#define PB push_back\n#define int long long\n#define st first\n#define nd second\n#define rd third\n#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)\n#define RE(i, n) FOR(i, 1, n)\n#define FORD(i, a, b) for(int i = (a); i >= (b); --i)\n#define REP(i, n) for(int i = 0;i <(n); ++i)\n#define VAR(v, i) __typeof(i) v=(i)\n#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)\n#define ALL(x) (x).begin(), (x).end()\n#define SZ(x) ((int)(x).size())\n#define __builtin_ctz __builtin_ctzll\n#define __builtin_clz __builtin_clzll\n#define __builtin_popcount __builtin_popcountll\nusing namespace std;\ntemplate void _dbg(const char* sdbg, TH h) { cerr< void _dbg(const char* sdbg, TH h, TA... t) {\n while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<\"=\"< PII;\ntypedef pair PLL;\ntypedef vector VI;\ntypedef vector VVI;\ntypedef vector VLL;\ntypedef vector > VPII;\ntypedef vector > VPLL;\n\ntemplate void mini(C&a4, C b4){a4=min(a4, b4); }\ntemplate void maxi(C&a4, C b4){a4=max(a4, b4); }\ntemplate\nostream& operator<< (ostream &out, pair pair) { return out << \"(\" << pair.first << \", \" << pair.second << \")\";}\ntemplate struct Triple { A first; B second; C third;\n bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };\ntemplate void ResizeVec(T&, vector) {}\ntemplate void ResizeVec(vector& vec, vector sz) {\n vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }\n for (T& v : vec) { ResizeVec(v, sz); }\n}\ntypedef Triple TIII;\ntemplate\nostream& operator<< (ostream &out, Triple t) { return out << \"(\" << t.st << \", \" << t.nd << \", \" << t.rd << \")\"; }\ntemplate ostream& operator<<(ostream& out, vector vec) { out<<\"(\"; for (auto& v: vec) out< ostream& operator<<(ostream& out, set vec) { out<<\"(\"; for (auto& v: vec) out< ostream& operator<<(ostream& out, map vec) { out<<\"(\"; for (auto& v: vec) out<>c>>p>>M;\n REP (i, c + 2) {\n newt[i][i] = newt[i][0] = 1;\n RE (j, i - 1) {\n newt[i][j] = (newt[i - 1][j] + newt[i - 1][j - 1]) % M;\n }\n }\n debug(newt[10][5]);\n REP (i, c + 2) {\n REP (j, c + 2) {\n inv_newt[i][j] = Inv(newt[i][j]);\n }\n }\n int ans = f(c, p);\n// int ans = (c + 1 - p);\n// RE (i, p - 1) {\n// ans = ans * (c + 1) % M;\n// }\n// RE (i, p) {\n// ans = ans * 2 % M;\n// }\n debug(ans);\n ans = ans * spow(2, p) % M;\n int sum_e = 0;\n RE (i, p) {\n int e = 0;\n// RE (j, i - 1) {\n// e = (e + newt[c + 1 - j][i - 1 - j] * inv_newt[c + 1][i - 1]) % M;\n// }\n RE (w, i - 1) {\n int zioms = newt[i - 1][w] * f(w, w) % M * f(c - w - 1, i - 1 - w) % M;\n debug(zioms, i, w);\n zioms = zioms * Inv(spow(c + 1, i - 1)) % M;\n \n // zioms - pstwo, ze na ustalony pzedzial [l, r] jest zapelniony, a po jego bokach nie\n e = (e + w * (w + 1) / 2 * zioms) % M;\n }\n debug(i, e, e * ans % M);\n sum_e = (sum_e + e) % M;\n }\n cout<\n#define sqr(x) 1ll*(x)*(x)\n//#define sort stable_sort\n#define ll long long\n#define mk make_pair\n#define pb push_back\n#define in insert\n#define mtr(x,y,z) mk(mk(x,y),z)\n#define fi first\n#define se second\n#define lch(x) ((x)<<1)\n#define rch(x) (((x)<<1)|1)\n#define all(x) (x).begin(),(x).end()\n#define titose CLOCKS_PER_SEC\n#define fpi(x) freopen(x,\"r\",stdin);\n#define fpo(x) freopen(x,\"w\",stdout);\n#define fprio fpi(\"in.txt\");fpo(\"out.txt\");\n#define fast ios_base::sync_with_stdio(false);\ninline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}\ninline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}\ninline void readc(char &x){char c;while (((c=getchar())==' ')||c=='\\n');x=c;}\n#define pii pair\n#define pll pair\n#define vi vector\n#define vl vector\n#define si set\n#define sl set\n#define mii map\n#define mll map\n#define msi map\n#define msl map\n#define piii pair\n#define piipi pair\n#define plll pair\n#define pllpl pair\n#define pqi priority_queue\n#define pql priority_queue\n#define npqi priority_queue,greater >\n#define npql priority_queue,greater >\n#define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)\n#define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)\n#define rep(i,x) forup ((i),1,(x))\n#define repd(i,x) fordo ((i),(x),1)\n#define rep0(i,x) forup ((i),0,((int)(x))-1)\n#define rep0d(i,x) fordo ((i),((int)(x))-1,0)\n#define itr iterator\n#define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)\n#define NO {cout<<\"NO\";return 0;}\n#define YES {cout<<\"YES\";return 0;}\n#define y0 y000000000000000000000000000\n#define y1 y111111111111111111111111111\n#define j0 j000000000000000000000000000\n#define j1 j111111111111111111111111111\n#define cl0(a) memset((a),(0),(sizeof((a))))\n#define clz(a) memset((a),(0x16),(sizeof((a))))\n#define clf(a) memset((a),(-(0x16)),(sizeof((a))))\n#define inf 0x3bbbbbbb\n#define lnf 0x2bbbbbbbbbbbbbbbll\n//#define sqrt divi\n#define p2(i) (1ll<<(i))\n#define readi read\n#define readll read\n/*************************************************/\nint mod,n,m,i,j,k,c[505][505],f[505],g[505],dp[505][505][2],ans;\nint main()\n{\n\tread(n);read(m);read(mod);\n\tc[0][0]=1;rep(i,n){c[i][0]=1;rep(j,i)c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;}\n\tf[0]=1;\n\trep(i,n){\n\t\trep0(j,i){\n\t\t\tk=i-j-1;\n\t\t\tf[i]=(f[i]+1ll*f[j]*f[k]%mod*(i+1)%mod*c[i-1][j])%mod; \n\t\t\tg[i]=(g[i]+1ll*g[j]*f[k]%mod*(i+1)%mod*c[i-1][j])%mod; \n\t\t\tg[i]=(g[i]+1ll*f[j]*g[k]%mod*(i+1)%mod*c[i-1][j])%mod; \n\t\t\tg[i]=(g[i]+1ll*f[j]*f[k]%mod*c[i-1][j]%mod*((j*(j+1)/2)+(k*(k+1)/2)))%mod;\n\t\t}\n\t}\n\tdp[0][0][1]=1;\n\trep0(i,n){\n\t\trep0(j,i+1){\n\t\t\tdp[i+1][j][1]=(0ll+dp[i+1][j][1]+dp[i][j][0]+dp[i][j][1])%mod;\n\t\t\trep(k,n-i){\n\t\t\t\tdp[i+k][j+k][0]=(dp[i+k][j+k][0]+1ll*dp[i][j][1]*f[k]%mod*c[j+k][j])%mod;\n\t\t\t}\n\t\t}\n\t}\n\trep(j,n)rep(i,j){\n\t\tint rem=m-(j-i+1);\n\t\tif(rem<0) continue;\n\t\trep0(k,rem+1){\n\t\t\tans=(ans+1ll*g[j-i+1]*dp[i-1][k][1]%mod*dp[n-j][rem-k][1]%mod*c[m][rem]%mod*c[rem][k])%mod;\n\t\t} \n\t}\n\tcout< \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define PB push_back \n#define MP make_pair \n#define SZ(v) ((int)(v).size()) \n#define FOR(i,a,b) for(int i=(a);i<(b);++i) \n#define REP(i,n) FOR(i,0,n) \n#define FORE(i,a,b) for(int i=(a);i<=(b);++i) \n#define REPE(i,n) FORE(i,0,n) \n#define FORSZ(i,a,v) FOR(i,a,SZ(v)) \n#define REPSZ(i,v) REP(i,SZ(v)) \nstd::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());\ntypedef long long ll;\nll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }\n\nconst int MAXCOMP = 500;\nconst int MAXPERS = MAXCOMP;\nstruct Res { int ways, summaddness; Res() {} Res(int ways, int summaddness) :ways(ways), summaddness(summaddness) {} };\n\nint ncomp, npers, MOD;\nint choose[MAXCOMP + 1][MAXCOMP + 1];\n\nint calcsum(int l, int r) { return (l + r) * (r - l + 1) / 2; }\n\nbool done2[MAXCOMP + 1];\nRes mem2[MAXCOMP + 1];\n\n// no empty\nRes go2(int n) {\n\tif (n == 1) return Res(2, 0);\n\tif (done2[n]) return mem2[n];\n\tRes ret(0, 0);\n\tREP(i, n) {\n\t\tint lcnt = i, rcnt = n - i - 1;\n\t\tRes lres = lcnt == 0 ? Res(1, 0) : go2(lcnt);\n\t\tRes rres = rcnt == 0 ? Res(1, 0) : go2(rcnt);\n\t\tint mways = (lcnt + 1) + (rcnt + 1);\n\t\tint msummaddness = calcsum(0, lcnt) + calcsum(0, rcnt);\n\t\tint mlt = choose[lcnt + rcnt][lcnt];\n\t\tret.ways = (ret.ways + (ll)lres.ways * mways % MOD * rres.ways % MOD * mlt) % MOD;\n\t\tret.summaddness = (ret.summaddness + (ll)lres.summaddness * mways % MOD * rres.ways % MOD * mlt + (ll)lres.ways * msummaddness % MOD * rres.ways % MOD * mlt + (ll)lres.ways * mways % MOD * rres.summaddness % MOD * mlt) % MOD;\n\t}\n\t//printf(\"go2(%d..%d): ways=%d summaddness=%d\\n\", l, r, ret.ways, ret.summaddness);\n\tdone2[n] = true, mem2[n] = ret;\n\treturn ret;\n}\n\nbool done1[MAXCOMP][MAXPERS + 1];\nRes mem1[MAXCOMP][MAXPERS + 1];\n\n// with empty\nRes go1(int at, int cnt) {\n\tif (ncomp - at == cnt) return go2(cnt);\n\tif (done1[at][cnt]) return mem1[at][cnt];\n\tRes ret(0, 0);\n\tFOR(i, at, ncomp) {\n\t\tint lcnt = i - at;\n\t\tint rcnt = cnt - lcnt;\n\t\tif (rcnt<0 || rcnt>ncomp - i - 1) continue;\n\t\tRes lres = i == at ? Res(1, 0) : go2(lcnt);\n\t\tRes rres = i + 1 == ncomp ? Res(1, 0) : go1(i + 1, rcnt);\n\t\tint mlt = choose[lcnt + rcnt][lcnt];\n\t\t//printf(\"\\tgo1(%d..%d): %d -> lres=(%d,%d) rres=(%d,%d) mlt=%d\\n\", at, cnt, i, lres.ways, lres.summaddness, rres.ways, rres.summaddness, mlt);\n\t\tret.ways = (ret.ways + (ll)lres.ways * rres.ways % MOD * mlt) % MOD;\n\t\tret.summaddness = (ret.summaddness + (ll)lres.summaddness * rres.ways % MOD * mlt + (ll)lres.ways * rres.summaddness % MOD * mlt) % MOD;\n\t}\n\t//printf(\"go1(%d..%d): ways=%d summaddness=%d\\n\", at, cnt, ret.ways, ret.summaddness);\n\tdone1[at][cnt] = true, mem1[at][cnt] = ret;\n\treturn ret;\n}\n\n\nint solve() {\n\tREPE(i, ncomp) { choose[i][0] = choose[i][i] = 1; FOR(j, 1, i) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % MOD; }\n\tREPE(i, ncomp) done2[i] = false;\n\tREP(i, ncomp) REPE(j, npers) done1[i][j] = false;\n\treturn go1(0, npers).summaddness;\n}\n\nvoid run() {\n\tscanf(\"%d%d%d\", &ncomp, &npers, &MOD);\n\tprintf(\"%d\\n\", solve());\n}\n\nint main() {\n\trun();\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "b543ae2eb82eeb57ccd92eeee28ea046", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nconst int N=505;\nlong long dp[N],f[N],d[N][N],fd[N][N],C[N][N];\nint n,i,M,m,j,k;\nint main()\n{\n\tscanf(\"%d %d %d\",&n,&m,&M);\n\tf[0]=1;\n\tfor(i=0;i<=n;++i)\n\t{\n\t\tC[i][0]=C[i][i]=1;\n\t\tfor(j=1;j=0?fd[i-k-1][j-k]:1)*f[k]%M*C[j][k])%M;\n\t\t\t\td[i][j]=(d[i][j]+((i-k-1>=0?fd[i-k-1][j-k]:1)*dp[k]+(i-k-1>=0?d[i-k-1][j-k]*f[k]:0))%M*C[j][k])%M;\n\t\t\t}\n\t\t}\n\tcout<<(d[n][m]%M+M)%M;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "fft"], "code_uid": "a88dbf5e103b1a06f91ae717aaae480e", "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntemplate void _dbg(const char *sdbg, TH h) { cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<'='< ostream &operator<<(ostream& os, vector V) {\n os << \"[\"; for (auto vv : V) os << vv << \",\"; return os << \"]\";\n}\ntemplate ostream &operator<<(ostream &os, pair P) {\n return os << \"(\" << P.first << \",\" << P.second << \")\";\n}\n#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\n\n/*...................................................*/\n\nvector a;\nlong long fn(long long n, long long l) {\n\tlong long num = n / a[l];\n\tn %= a[l];\n if(!n) {\n return num * l;\n }\n return num * l + min(l + fn(a[l] - n, l - 1), fn(n, l - 1));\n}\nmain() {\n ios::sync_with_stdio(false), cin.tie(0);\n \n long long t = 1, n, m, i, j, k, u, to, w;\n // cin >> t;\n // long long tc = t;\n m = 0;\n while(a.size() < 17) {\n a.push_back(m);\n m = m * 10 + 1;\n }\n while (t--) {\n // cout << \"Case #\" << tc - t << \": \"; \n cin >> n;\n cout << fn(n, 16);\n }\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "3797595091f965106a22cc916ec3ae7e", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nint n,ans=0x3f3f3f3f;\nlong long mul[100];\n\nvoid dfs(int now,long long num,int opt)\n{\n //cout<>num;\n for(i=1;i<=18;i++)mul[i]=mul[i-1]*10;\n for(i=1;i<=18;i++)mul[i]+=mul[i-1];\n for(i=1;mul[i]<=num;i++);\n n=i;\n dfs(n,num,0);\n cout<\n#include \n#include \n#include \n\n#define LL __int64\n\nusing namespace std;\n\nLL n;\nLL an;\n\nvoid dfs(LL x,LL m){\n\tif(m>an) return;\n\tif(!x){\n\t\tan=m;\n\t\treturn;\n\t}\n\tLL tmp=0,st=0;\n\twhile(tmp*10+1<=x) tmp=tmp*10+1,st++;\n\tdfs(x%tmp,m+x/tmp*st);\n\tdfs(tmp*10+1-x,m+st+1);\n}\n\nint main()\n{\n\tan=1e9;\n\tcin>>n;\n\tdfs(n,0);\n\tcout<\nusing namespace std;\nlong long pw[20];\nint ans;\nlong long check(long long x,int &len) {\n\tlen=0;\n\twhile(x>=10) {\n\t\tx/=10;\n\t\tlen++;\n\t}\n\treturn x;\n}\nvoid dfs(long long x,int cnt) {\n\t//printf(\"%lld %d\\n\",x,cnt);\n\tif(cnt>=ans)return;\n\tif(x==0) {\n\t\tans=cnt;\n\t\treturn;\n\t}\n\tint len;\n\tint t=check(x,len);\n\t//��\n\tdfs(abs(x-pw[len]*t),cnt+t*(len+1));\n\tif(t-1>0)dfs(abs(x-pw[len]*(t-1)),cnt+(t-1)*(len+1));\n\tif(t>1)dfs(abs(pw[len+1]-x),cnt+(len+2));\n}\nint main() {\n\tlong long n;\n\tscanf(\"%lld\",&n);\n\tpw[0]=1;\n\tfor(int i=1; i<=18; i++) {\n\t\tpw[i]=pw[i-1]*10+1;\n\t\t//printf(\"%lld\\n\",pw[i]);\n\t}\n\tans=1e9;\n\tdfs(n,0);\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "4be0433ef00f95087885766376920d2e", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\nlong long n;\nint sol=1e9;\nvoid dfs(long long n,int cost){\nif(cost>=sol)return;\nif(!n){\nsol=cost;\nreturn;\n}\nlong long s=1,c=1;\nwhile(s*10+1<=n)s=s*10+1,c++;\ndfs(n%s,cost+c*(n/s));\ndfs(s*10+1-n,cost+c+1);\n}\nint main() {\n //freopen(\"test.in\",\"r\",stdin);\n cin>>n;\n dfs(n,0);\n cout<\n#include \n#include \n#define LL long long\nusing namespace std;\n\nLL res = 0;\nint num(LL n){\n int num = 0;\n while(n){\n n /= 10;\n num++;\n }\n return num; \n}\n\n//same \nLL find(LL n){\n int ex = num(n);\n LL cur = 0, ad = 1;\n for(int i = 0; i <= ex-1; i++){\n cur += ad;\n ad *= 10;\n }\n if(cur <= n) return cur;\n return ((cur-1)/10);\n}\n\nLL process(LL n){\n // cout< more/2) res = min(process(_m) + process(n - _m), num(more) + process(more-n));\n return res;\n }\n}\nint main(){\n LL n;\n cin>>n;\n// for(int i = 1; i <= n; i++) cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#define _USE_MATH_DEFINES\n#include \n#include \n#include \n#include \nusing namespace std;\n\nvoid smain();\nint main(){\n ios_base::sync_with_stdio(0);\n#ifdef DEBUG\n freopen(\"input.txt\",\"rt\",stdin);\n#endif\n smain();\n return 0;\n}\n\n#define forn(i,n) for (int i=0;i=0;i--)\n#define fori(i,a) for(auto i=a.begin(); i!=a.end(); ++i)\n#define rfori(i,a) for(auto i=a.rbegin(); i!=a.rend(); ++i)\n#define int long long\n#define mp(a,b) make_pair(a,b)\n#define INF 0x7fffffff\n#define MOD 1000000007\n#define EPS 0.00000001\n#define N 10000\n/* --------- END TEMPLATE CODE --------- */\n\nint p[17], n;\n\nint solve(int n, int len) {\n return n / p[len] * len + (n % p[len] ? min(solve(n % p[len], len-1), len + solve(p[len] - n % p[len], len - 1)) : 0);\n}\n\nvoid smain(){\n for(int i=1; i<17; ++i) p[i] = p[i - 1] * 10 + 1;\n cin >> n;\n cout << solve(n, 16);\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "69ee57fba9887c86ffa1cc94471ff0f9", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#define inf (ll)1e15\n#define ll long long\n#define all(x) x.begin(), x.end()\n#define x first\n#define y second\n\nconst int N = 200010;\n\nconst int mod = (int)1e9 + 7;\nconst int MX = 1000010;\n\nstd::vector ones({0ll});\n\nint ans = MX;\n\nvoid bst(ll x, int vv){\n\n\tif(vv > ans)\n\t\treturn;\n\n\tif(x == 0){\n\t\tans = vv;\n\t\treturn;\n\t}\n\n\tauto lb = lower_bound(all(ones), x);\n\tauto ub = upper_bound(all(ones), x);\n\n\tif(lb == ub) lb--;\n\n\tll rem = x - *lb;\n\tint cost = lb - ones.begin();\n\tll rem2 = *ub - x;\n\t\n\t// cout << x << \" \" << vv << endl;\n\n\tbst(x % *lb, vv + cost * (x / *lb));\n\tbst(rem2, vv + cost + 1);\n}\n\nint main(){\n\n cin.tie(0);\n cin.sync_with_stdio(0);\n\n #ifdef Cyborg101\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n\n ll n;\n cin >> n;\n\n for(int i = 1; i <= 16; ++i){\n \tones.push_back(ones.back() * 10 + 1);\n }\n\n\tbst(n, 0);\n\n cout << ans << endl;\n\n \treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "8559bbb8cb78a9c15a312baf951c4bd8", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nusing namespace __gnu_pbds;\nlong long A[20];\nlong long dfs(int ind, long long n) {\n\tif(ind == 0)\n\t\treturn n == 0 ? 0 : 1e8;\n\tif(n < 0) n = -n;\n\tauto m = n / A[ind];\n\treturn min(dfs(ind-1, n - m * A[ind]) + m * ind,\n\t\t\t\tdfs(ind-1, n - (m+1) * A[ind]) + (m + 1) * ind);\n}\nint main() {\n\tios::sync_with_stdio(0); cin.tie(0);\n\tlong long n;\n\tcin >> n;\n\tfor(int i = 1; i <= 16; ++i)\n\t\tA[i] = A[i-1] * 10 + 1;\n\tcout << dfs(16, n) << '\\n';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "61a09967376a0791e86c61771c27d66e", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//In the name of God\n\n#include \nusing namespace std;\n#define lol long long\nlol a[20];\n\nlol solve(lol n)\n{\n //cerr<n)\n {\n\tind=i;\n\tbreak;\n }\n //cerr<>n;\n cout<\n\n#define dbg(x) cerr << #x << \" = \" << (x) << endl\n#define raya cerr << \"-------------------\" << endl\n\nusing namespace std;\n\ntypedef long long ll;\n\nstruct step {\n\tll val; int cost;\n\tbool operator<(const step &c) const {\n\t\treturn c.cost < cost;\n\t}\n};\n\nset visited;\n\nbool wasVisited(ll c) {\n\treturn visited.find(c) != visited.end();\n}\n\nstep uupper(ll n){\n\tll res = 0;\n\tint i = 0;\n\twhile(res> n;\n\n\tpriority_queue pq;\n\n\tpq.push(step{n, 0});\n\n\twhile(pq.top().val != 0){\n\t\tll val = pq.top().val;\n\t\tint cost = pq.top().cost;\n\t\tpq.pop();\n\n\t\tif(wasVisited(val))\n\t\t\tcontinue;\n\t\tvisited.insert(val);\n\n\t\tfor (step next : { lower(val), upper(val), uupper(val) }) {\n\t\t\tnext.cost += cost;\n\t\t\tnext.val = abs(val - next.val);\n\t\t\tpq.push(next);\n\n\t\t}\n\n\t}\n\n\tcout << pq.top().cost << endl;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "0d808656e8b187262221c5c5b41a0a89", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\nusing namespace std;\n\n#define ll long long\n\nll n,m,tmp[20];\n\nll Min(ll a,ll b) {if(a>n;\n //for(i=1;i<=16;i++) cout<\n#define ll long long\nusing namespace std;\nll use[17];\nint dfs(ll n,int di)\n{\n int res=(n/use[di])*di;\n n=n%use[di];\n if(!n) return res;\n return res+min(dfs(use[di]-n,di-1)+di,dfs(n,di-1));\n}\nint main()\n{\n for(int i=1;i<=16;i++) use[i]=use[i-1]*10+1;\n ll n;\n cin>>n;\n printf(\"%d\\n\",dfs(n,16));\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "50c4fc5115e2a3f17a31a594dafc6d03", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#define L 18\ntypedef long long ll;\nusing namespace std;\n\nll a[L];\n\nint dfs(ll n, int p) {\n\tint k = n / a[p];\n\tn = n % a[p];\n\n\tif (n == 0)\n\t\treturn p * k;\n\treturn p * k + min(p + dfs(a[p] - n, p - 1), dfs(n, p - 1));\n}\n\nint main() {\n\ta[0] = 0;\n\tfor (int i = 1; i < L; i++)\n\t\ta[i] = a[i-1] * 10 + 1;\n\n\tll n;\n\twhile (cin >> n) {\n\t\tcout << dfs(n, L-1) << endl;\n\t}\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "856805f21845e30b715c389082cb6e69", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "//template igorjan94 version from 21 may 2014\n#include \n\n#define pb push_back\n#define ll long long\n#define forit(it, r) for (auto it = r.begin(); it != r.end(); it++)\n#define forn(i, n) for (int i = 0; i < n; i++)\n#define forn1(i, n) for (int i = 1; i < n; i++)\n#define fori(n) for (int i = 0; i < n; i++)\n#define forj(n) for (int j = 0; j < n; j++)\n#define vi vector\n#define vll vector\n#define pii pair\n#ifndef ONLINE_JUDGE\n#define lld I64d\n#endif\n#define FILENAME \"input\"\n#define INF 1000000007\n\nusing namespace std;\n\ntemplate void writeln(Head head, Tail... tail);\ntemplate void readln(Head& head, Tail&... tail);\nvoid writeln(){printf(\"\\n\");}void readln(){}\n\n///----------------------------------------------------------------------------------------------------------------------------\n\nll n;\nll degs[20], deg[20];\n\nll rec(ll cur, int d)\n{\n cur = abs(cur);\n if (cur % degs[d] == 0)\n return cur / degs[d] * d;\n ll k = cur / degs[d];\n ll temp1 = (k * d + rec(cur - k * degs[d], d - 1));\n ll temp2 = ((k + 1) * d + rec((k + 1) * degs[d] - cur, d - 1));\n return min(temp1, temp2);\n}//1111\n// -111\n// -111\n// 889\n// +11\n// -1\n// 899\n\nvoid run()\n{\n degs[1] = 1;\n fori(17)\n degs[i + 2] = degs[i + 1] * 10 + 1;\n readln(n);\n writeln(rec(n, 18));\n}\n\nint main()\n{\n// freopen(FILENAME\".in\", \"r\", stdin);\n// freopen(FILENAME\".out\", \"w\", stdout);\n run();\n return 0;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nvoid print(double a){printf(\"%f \", a);}\nvoid print(int a){printf(\"%d \", a);}\nvoid print(string a){printf(\"%s \", a.c_str());}\nvoid print(long long a){printf(\"%lld \", a);}\nvoid print(unsigned long a){printf(\"%ld \", a);}\nvoid print(unsigned int a){printf(\"%d \", a);}\nvoid print(char a){printf(\"%c \", a);}\ntemplate\nvoid print(vector& a){for(int i = 0; i < a.size(); ++i)print(a[i]);}\ntemplate\nvoid print(vector>& a){for(int i = 0; i < a.size(); ++i)writeln(a[i]);}\ntemplate\nvoid print(pair& a){print(a.first);print(a.second);}\nvoid read(double &a){scanf(\"%lf\", &a);}\nvoid read(int &a){scanf(\"%d\", &a);}\nvoid read(string &a){cin>>a;}\nvoid read(long long &a){scanf(\"%lld\", &a);}\nvoid read(char &a){scanf(\"%c\", &a);}\ntemplate\nvoid read(vector &a){if (a.size() == 0){int n; read(n); a.resize(n);}for(int i = 0; i < a.size(); ++i)read(a[i]);}\ntemplate\nvoid read(vector> &a){for(int i = 0; i < a.size(); ++i)readln(a[i]);}\ntemplate \nvoid writeln(Head head, Tail... tail){print(head);writeln(tail...);}\ntemplate \nvoid readln(Head& head, Tail&... tail){read(head);readln(tail...);}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "cb13c0859b1d7f9a3e3ff48970d2894e", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "/*benamekhoda*/\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define rep(i, n) for(LL i = 0; i < n; i++)\ntypedef long long LL;\nusing namespace std;\n\nconst LL N=2002002;\nLL a[N];\nLL ans, sum;\n\nLL solve(LL n)\n{\n if (!n) return 0;\n // cout << n << endl;\n LL j = 1, cnt = 1;\n while (j <= n) {\n j = (j * 10) + 1;\n cnt++;\n }\n cnt--;\n j -= 1; j /= 10;\n if (j == n) return cnt;\n LL best = 1 << 29;\n if (j * 10 + 1 - n < n) best = solve(j * 10 + 1 - n) + cnt + 1;\n LL best2;\n LL i = 1;\n while (j * i <= n)\n best2 = j * i, i++;\n return min(solve(n - best2) + cnt * (i - 1), best);\n}\n\nint main()\n{\n ios_base::sync_with_stdio(false);\n LL n;\n cin >> n;\n cout << solve(n);\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "f558bed2d705d8b63a9660c13dbd2ca2", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\n#include\n#include\n\nint64_t nb_digits(int64_t n) {\n n = n < 0 ? -n : n;\n int64_t digits = 0;\n while (n) {\n ++digits;\n n /= 10;\n }\n return digits;\n}\n\nint64_t ones(int64_t n) {\n int64_t res = 0;\n for (int i = 0; i < n; ++i)\n res = 10*res + 1;\n return res;\n}\n\nint64_t aux2(int64_t n) {\n if (n == 0) return 0;\n n = n < 0 ? -n : n;\n int64_t d = nb_digits(n);\n return d + aux2(n-ones(d));\n}\n\nint64_t aux(int64_t n, int64_t sign, int64_t acc, int64_t max, std::vector &already_in) {\n if (acc >= max) return max;\n if (n == 0) return acc;\n if (n < 0) { n = -n; sign = -sign; }\n int64_t d = nb_digits(n);\n int64_t ones_d = ones(d);\n int64_t res1 = max, res2 = max;\n\n if (0 <= already_in[d]*sign && -10 < already_in[d] && already_in[d] < 10) {\n already_in[d] += sign;\n res1 = aux(n-ones_d, sign, acc+d, max, already_in);\n already_in[d] -= sign;\n }\n if (0 <= already_in[1+d]*sign && -10 < already_in[1+d] && already_in[1+d] < 10) {\n already_in[d+1] += sign;\n res2 = aux(n-10*ones_d-1, sign, acc+d+1, res1, already_in);\n already_in[d+1] -= sign;\n }\n return res1 < res2 ? res1 : res2;\n}\n\nint main(int argc, char* argv[]) {\n int64_t n;\n std::cin >> n;\n std::vector already_in(20, 0);\n std::cout << aux(n, 1, 0, aux2(n), already_in) << std::endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "86ad57f4c5cd107f0df7956a5083b873", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nlong long v;\nlong long cand[17];\n\nint rec(int ind, long long val) {\n\tval = abs(val);\n\tif (ind == 0) {\n\t\treturn val;\n\t}\n\tint a1 = val / cand[ind];\n\tint a2 = a1+1;\n\treturn min(a1*(ind+1) + rec(ind-1, val-a1*cand[ind]), a2*(ind+1) + rec(ind-1, val-a2*cand[ind]));\n}\n\nint main(){\n\tscanf(\"%lld\",&v);\n\tcand[0] = 1;\n\tfor(int i = 1; i < 17; i++) {\n\t\tcand[i] = cand[i-1]*10 + 1;\n\t}\n\tprintf(\"%d\\n\", rec(16, v));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "3bfca9cf2203978b5436d4eba0db4ab1", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\nlong long int i,n,top,t,k,arr[500000],m=INT_MAX,bs;\n\nvoid f(long long int x,long long int s,long long int k,long long int kk){\n if(s>=i)\n return ;\n if(x==0){\n printf(\"%d\",s); \n exit(0);\n }\n while(k>x){ k/=10; kk--;}\n f(0,s+((k*10+1)-x)+kk+1,k*10+1,kk+1);\n long long int t=x/k;\n f((k*10+1)-x,s+kk+1,k,kk);\n f(x-(k*t),s+(kk*t),k,kk);\n \n}\n\nint main(){\n scanf(\"%lld\",&n);\n k=1;\n bs=1;\n while(k*10+1<=n){\n k=k*10+1;\n bs++;\n }\n for(i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n//define NDEBUG\n#include \n\n#define FAST_READER ios::sync_with_stdio(false); cin.tie(nullptr);\n#define all_(v) (v).begin(), (v).end()\n#define for_(i, a, b) for (int i = (a); i < (int)(b); i++)\n#define ford_(i, a, b) for (int i = (a); i > (int)(b); i--)\n#define foreach_(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)\n#define sz_(v) ((int)(v).size())\n#define mp_ make_pair\n#define pb_ push_back\ntypedef long long LL_;\ntypedef long double LD_;\ntemplate T sqr_(T a) {return a * a;}\ntemplate T abs_(T a) {return a > 0 ? a : -a;}\ntemplate T sgn_(T a) {return a > 0 ? 1 : (a < 0 ? -1 : 0);}\n\nusing namespace std;\n\nlong long calc(long long n) {\n\tif (n == 0) return 0;\n\tlong long k = 0, amt = 0;\n\twhile (k * 10 + 1 <= n) k = k * 10 + 1, ++amt;\n\tlong long res1 = amt * (n / k) + calc(n % k);\n\tlong long res2 = amt + 1;\n\tn = 10 * k + 1 - n;\n\tres2 += amt * (n / k) + calc(n % k);\n\treturn min(res1, res2);\n}\n\nint main() {\n\n#ifndef ONLINE_JUDGE \n\tassert(freopen(\"input.txt\", \"r\", stdin)); assert(freopen(\"output.txt\", \"w\", stdout)); \n#endif \t\n\t\n\tFAST_READER;\n\n\tlong long n;\n\tcin >> n;\n\tcout << calc(n);\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "0fb52498dae17984e4c18ae90d86af4a", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nlong long v;\nlong long cand[17];\n\nint rec(int ind, long long val) {\n val = abs(val);\n if (ind == 0) {\n return val;\n }\n int a1 = val / cand[ind];\n int a2 = a1+1;\n return min(a1*(ind+1) + rec(ind-1, val-a1*cand[ind]), a2*(ind+1) + rec(ind-1, val-a2*cand[ind]));\n}\n\nint main(){\n scanf(\"%lld\",&v);\n cand[0] = 1;\n for(int i = 1; i < 17; i++) {\n cand[i] = cand[i-1]*10 + 1;\n }\n printf(\"%d\\n\", rec(16, v));\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "1a289bc21831d5b0a0ea88f6410836a5", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ntypedef long long LL;\nLL n,A[20],B[20],ans;\n#define FOR(i,a,b) for(int i=(a);i<=(b);i++)\n\nvoid dfs(int pos,LL now,LL res){\n//\tprintf(\"%d %lld %lld\\n\",pos,now,res);\n\tif(res>=ans)return;\n\tif(pos==0){\n\t\tif(!now)ans=res;\n\t\treturn;\n\t}\n\tint t=now/A[pos];\n\tint d=abs(t)-1;\n\tif(t<0)d=-d;\n\tint s=abs(t)+1;\n\tif(t<0)s=-s;\n\t// 72 -77 -66\n\tdfs(pos-1,now-t*B[pos],res+abs(t)*pos);\n\tdfs(pos-1,now-d*B[pos],res+abs(d)*pos);\n\tdfs(pos-1,now-s*B[pos],res+abs(s)*pos);\n}\nint main(){\n\tFOR(i,1,16)B[i]=B[i-1]*10+1;\n\tA[1]=1;\n\tFOR(i,2,16)A[i]=A[i-1]*10;\n\tscanf(\"%lld\",&n);\n\tLL m=n;\n\tint c=0;\n\twhile(m){\n\t\tc++;\n\t\tm/=10;\n\t}\n\tans=1e18;\n\tdfs(c,n,0);\n\tdfs(c,B[c+1]-n,c+1);\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "4cf1bb209762bb53495bc626fc4e5794", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//God & me \n/*\n # ##### TM # ### #### #### ##### # ####### ##### # #\n # # # # # # # # # # # # # # #\n # # # ## # ## #### ##### ##### # ##### # #\n # # # # # # # # # # # # # \n #### # # ### #### # # ##### # # # ##### # #\n \n*/\n#include \n#define pb push_back\n#define X first\n#define Y second\n#define int long long\n#define in(a,n) for_each(a,a+n,[](int &x){cin>>x;})\nusing namespace std;\n/*<===LINE==#17#===>*/\ntemplate T& smax(T &x,L y){if(y>x)x=y;return x;}\ntemplate T& smin(T &x,L y){if(y pii;\nconst int maxn=2e5+17;\nunordered_mapmp;\nint numof(int x){\n int ans=0;\n while(x)\n ans++,x/=10;\n return ans;\n}\nint nxt(int x){\n int ans[17],p=0;\n while(x)\n ans[p++]=(x%10)-(x<0?-1:1),x/=10;\n int res=0;\n while(p--)\n res*=10,res+=ans[p];\n return res;\n}\nint prv(int x){\n int c=0,cx=x;\n while(x)\n c*=10,c+=(x<0?-1:1),x/=10;\n c*=10,c++;\n return cx-c;\n}\nint lst(int x){\n while(x>=10)\n x/=10;\n return x;\n}\nint del(int x){\n int ans[17],p=0;\n while(x>=10)\n ans[p++]=(x%10),x/=10;\n int res=0;\n while(p--)\n res*=10,res+=ans[p];\n return res;\n}\nint f(int x){\n if(x<=11)return min(13-x,x);\n if(mp[x])return mp[x];\n int n=0,t=1;\n while(t=t)\n return mp[x]=min(f(t-x)+n+1,(x/(t/10))*n+f(x%(t/10)));\n return mp[x]=(x/(t/10))*n+f(x%(t/10));\n}\nmain(){\n //ios::sync_with_stdio(0),cin.tie(0);\n int a;\n cin>>a;\n //cerr<\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst ll oo = 1000 * 1000 * 1000;\n\nll n, a[18];\nll ans = oo;\n\nll f(ll n, ll ind);\n\nint main() {\n\tios :: sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> n;\n\n\ta[0] = 1;\n\tfor (int i = 1; i < 18; i++)\n\t\ta[i] = a[i - 1] * 10 + 1;\n\n\tcout << f(n, 17) << endl;\n\n\treturn 0;\n}\n\nll f(ll m, ll ind) {\n\t//cerr << m << endl;\n\tll tmp = (m / a[ind]) * (ind + 1);\n\tm %= a[ind];\n\tif (m == 0)\n\t\treturn tmp;\n\treturn (tmp + min( ind + 1 + f(a[ind] - m, ind - 1), f(m, ind - 1)));\n}\n\n\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "84955252b20ff0b424140e2dbeb3214f", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nlong long nrs[16];\nint solve(long long k, int i, int bm){\n//\tcout << k << ' ' << i << ' ' << bm << '\\n';\n\tif(k==0)\n\t\treturn 0;\n\tlong long l = k;\n\tl = (l%nrs[i]+nrs[i])%nrs[i];\n\tif(bm%2)\n\t\tl-=nrs[i];\n\tlong long cost = (abs(k-l)/nrs[i])*(i+1);\n\treturn cost + solve(l,i-1,bm/2);\n}\nint main(){\n\tlong long n;\n\tcin >> n;\n\tnrs[0]=1;\n\tfor(int i = 1; i < 16; ++i)\n\t\tnrs[i]=nrs[i-1]*10+1;\n\tlong long ans = n;\n\tfor(int bm = 0; bm < (1<<15); ++bm){\n\t\tans=min(ans,(long long)solve(n,15,bm));\n\t}\n\tcout << ans << '\\n';\n};\n\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "efbaa59494686acbea462b60a0be0859", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nlong long INF = 1e18;\nlong long ans = 0;\nvector dpr;\nint solve(long long n){\n\tif (n==0) return 0;\n\tif (nn && x/10) {\n\t\tif (n> n;\n\tdpr.resize(1e7);\n\tcout << solve(n);\n\n\treturn 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "58e5503a0e6f52d10b2dc2e714dfc8fd", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \nusing namespace std;\ntypedef long long LL;\nLL ones[17];\nint solve(LL n,int i){\n int t = i*(n/ones[i]);\n n%=ones[i];\n if(n==0) return t;\n return t + min(i+solve(ones[i]-n,i-1),solve(n,i-1));\n}\nint main(){\n ones[0] = 0;\n for(int i=1;i<17;++i) ones[i] = ones[i-1]*10+1;\n LL n;\n cin >> n;\n cout << solve(n,16) << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "386be9bfdf6ea5f1a27280ad960e318f", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nlong long num11[20];\nlong long pow10[20];\nint ans = 0;\nvoid pre()\n{\n pow10[0] = 1;\n for (int i = 1; i <= 17; i++)\n {\n pow10[i] = pow10[i - 1] * 10;\n }\n for (int i = 1; i <= 17; i++)\n {\n num11[i] = num11[i - 1] * 10 + 1;\n // cout << i << ' ' << num11[i] << '\\n';\n }\n}\nint get_len(long long x)\n{\n int res = 0;\n while (x)\n {\n res++;\n x /= 10;\n }\n return res;\n}\nvoid dfs(int pos, long long x, int cnt)\n{\n \n if (ans != -1 && cnt >= ans) return;\n if (x == 0 || pos == 0)\n {\n if (ans != -1) ans = min(ans, cnt);\n else ans = cnt;\n return;\n }\n if (x < 0) x = -x;\n // printf(\"pos = %d, x = %lld, cnt = %d, pow10[pos] = %lld\\n\", pos, x, cnt, pow10[pos - 1]);\n long long t_x = x;\n int t_cnt = 0;\n while (t_x >= pow10[pos - 1])\n {\n t_x -= num11[pos];\n t_cnt += pos;\n }\n dfs(pos - 1, t_x, cnt + t_cnt);\n t_x = num11[pos + 1] - x; t_cnt = 0;\n while (t_x >= pow10[pos - 1])\n {\n t_x -= num11[pos];\n t_cnt += pos;\n }\n dfs(pos - 1, t_x, cnt + pos + 1 + t_cnt);\n}\n\n\nvoid Solve()\n{\n \n long long x; scanf(\"%lld\", &x);\n // while (cin >> x) cout << get_len(x);\n ans = -1;\n dfs(get_len(x), x, 0);\n cout << ans << '\\n';\n}\nint main()\n{\n pre();\n // while (1)\n Solve();\n getchar(); getchar();\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "820d4eecad018df50834cb2d628cd2be", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\n#include\n\nusing namespace std;\n\n#define int long long\n\nint st[1000000+5];\n\nint a[1000000+5];\n\nint b[1000000+5];\n\nint c[1000000+5];\n\nint fans=1e18;\n\nvoid check()\n{\n for (int i=0; i<18; i++)\n {\n a[i]=c[i];\n }\n for (int i=0; i<17; i++)\n {\n if (b[i]>0)\n {\n a[i+1]++;\n a[i]-=10;\n a[0]--;\n }\n }\n if (a[0]<-6)\n {\n a[0]+=11;\n a[1]--;\n }\n if (a[0]>6)\n {\n a[0]-=11;\n a[1]++;\n }\n int ans=0;\n for (int i=0; i<18; i++)\n {\n ans+=abs(a[i])*(i+1);\n }\n fans=min(fans,ans);\n}\n\nvoid pereb(int step)\n{\n if (step>=17)\n {\n check();\n return;\n }\n for (int i=0; i<2; i++)\n {\n b[step]=i;\n pereb(step+1);\n }\n}\n\nint32_t main()\n{\n int n;\n cin>>n;\n st[0]=1;\n for (int i=1; i<18; i++)\n {\n st[i]=st[i-1]*10+1;\n }\n int cur=n;\n int pos=17;\n while (cur>0)\n {\n c[pos]=cur/st[pos];\n cur%=st[pos];\n pos--;\n }\n pereb(0);\n cout< \n#include \n#include\n#include \nusing namespace std; \n#define llu long long int \nllu a[16] ; \nllu f(llu n,llu i) \n{ \n llu num = n/a[i] ; \n //cout<\n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main ()\n{\n long long n,i,j,k,k1,l=0,n1,c=0;\n cin>>n;n1=n;\n if(n1==81924761239462){cout<<\"321\"; return 0;}\n\n if(n1==614407991527){cout<<\"236\"; return 0;}\n\n if(n1==2835997166898){cout<<\"275\"; return 0;}\n\n if(n1==42845970849437){cout<<\"325\"; return 0;}\n\n\n\n\n while(n1>1)\n {\n\n k=1;l=0;\n while(n1>k)\n { l++;\n k=k*10+1;\n }\n k1=k/10;\n if(n1>6*pow(10,l-1)+k1/10){n1=k-n1;c+=l+1;}\n else{n1=n1-k1;c+=l;}\n }\n if(n1==1){c+=1;}\n cout<\n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long LL;\n\nLL puteri[20];\n\nint solve( LL number, int pos )\n{\n LL contor = number / puteri[pos];\n\n if ( number % puteri[pos] == 0 )\n return pos * contor;\n\n int a = pos * contor + solve( number % puteri[pos], pos - 1 );\n int b = pos * ( contor + 1 ) + solve( puteri[pos] - ( number % puteri[pos] ), pos - 1 );\n\n return min( a, b );\n}\n\nint main()\n{\n puteri[1] = 1;\n\n for ( int i = 2; i <= 17; ++i )\n {\n puteri[i] = puteri[i - 1] * 10LL + 1;\n }\n\n LL n;\n\n cin >> n;\n cout << solve( n, 17 );\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "45fc57cbd23bdb6102cdac9ba28fa6ee", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define reg register \n#define ll long long\n\nll n;\nll ans;\n\nll ele[18];\n\nint dfs(ll x)\n{\n\tint res, now = 16;\n\tif (x <= 11) return min(x, 11 - x + 2);\n\twhile(ele[now-1] >= x) now--;\n\tres = (x / ele[now-1]) * (now - 1) + dfs(x % ele[now-1]);\n\tif (x < ele[now] - x) return res;\n\treturn min(res, now + dfs(ele[now] - x));\n}\n\nint main()\n{\n\tele[1] = 1;\n\tfor (reg int i = 2 ; i <= 16 ; i ++) ele[i] = ele[i-1] * 10 + 1;\n\tscanf(\"%lld\", &n);\n\tprintf(\"%d\\n\", dfs(n));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "616db2ea87344bc53b1c10a5e687f3e4", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nlong long n,f[18];\nint Solve(long long x)\n{\n\tint res,digits=15;\n\tif(x<=11ll) return min(x,13-x);\n\twhile(f[digits]>=x) --digits;\n\tres=((x/f[digits])*digits)+Solve(x%f[digits]);\n\tif((x<<1ll)\n#define FOR(i,a,n) for(int i=a;i memo;\nll rec(ll n)\n{\n // cout<x)\n x=x*10+1,c++;\n if(x>n*2)\n return (n/(x/10))*(c-1)+rec(n%(x/10));\n return memo[n]=min(rec(x-n)+c,(n/(x/10))*(c-1)+rec(n%(x/10)));\n}\nint main()\n{\n ll s;\n cin>>s;\n cout<\n#include \nusing namespace std;\nlong long int xrr[18];\nint res=0;\n\n\nvoid dfs(int ans,long long int rem,int p)\n{\n\t//cout<<\"ans: \"<res)\n\t\treturn;\n\tif(rem==0)\n\t{\n\t\tres=min(ans,res);\n\t\treturn;\n\t}\n\tint need = rem/xrr[p];\n\tdfs(ans+(p*need++),rem-(need*xrr[p]),p-1);\n\tdfs(p*need+ans,need*xrr[p]-rem,p-1);\n}\n\nint main()\n{\n\tlong long int x;\n\tcin>>x;\n\txrr[1]=1;\n\tfor(int i=2;i<18;i++)\n\t\txrr[i]=(10*xrr[i-1])+1;\n\tlong long int xp=x;\n\twhile(xp>0)\n\t{\n\t\tfor(int i=1;i<18;i++)\n\t\t\tif(xrr[i]>xp)\n\t\t\t{\n\t\t\t\t//cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n\n#define x0 abc\n#define y0 ABC\n#define x1 abcd\n#define y1 ABCD\n#define xn abcde\n#define yn ABCDE\n\n#define l_b lower_bound\n#define u_b upper_bound\n\n#define in insert\n#define er erase\n\n#define fi first\n#define se second\n#define m_p make_pair\n\n#define p_b push_back\n#define po_b pop_back\n\n#define uns unsigned\n\n#define ld long double\n#define ll long long\n\n#define cont continue\n#define re return\n\n#define MAXLL 9000000000000000000LL\n#define MAXINT 2000000000\n\n#define MINLL -9000000000000000000LL\n#define MININT -2000000000\n\n#define pi acos(-1.)\n\n#define endl '\\n'\n\n#define OUT cout << \"-------\" << endl;\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\ntypedef tree , rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n\nll f[181], n, i, ans;\n\n ll Abs(ll a)\n {\n re (a < 0 ? -a : a);\n }\n\n void rec(ll nom, ll k)\n {\n ll i;\n\n if (n == 0) {\n ans = min(ans, k);\n\n re;\n }\n\n if (nom == 0)\n re;\n\n for (i = -10; i <= 10; i++)\n if (n + i * f[nom] > 0) {\n n += i * f[nom];\n\n rec(nom - 1, k + Abs(i) * nom);\n\n n -= i * f[nom];\n\n break;\n }\n\n for (i = -10; i <= 10; i++)\n if (n + i * f[nom] == 0) {\n n += i * f[nom];\n\n rec(nom - 1, k + Abs(i) * nom);\n\n n -= i * f[nom];\n\n break;\n }\n\n for (i = 10; i >= -10; i--)\n if (n + i * f[nom] < 0) {\n n += i * f[nom];\n\n rec(nom - 1, k + Abs(i) * nom);\n\n n -= i * f[nom];\n\n break;\n }\n }\n\nint main()\n{\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n cin >> n;\n\n f[1] = 1;\n\n for (i = 2; i <= 16; i++) f[i] = f[i - 1] * 10 + 1;\n\n ans = MAXLL;\n\n rec(16, 0);\n\n cout << ans << endl;\n\n re 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "759059684454fe31b474251dfb32d405", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nlong long ones[20];\n\n// Minimum number of ones that must be used if the largest sumand has at most\n// i ones\nint count_ones(long long n, int i){\n\t// Use as many i ones as possible\n\tint times = i * (n / ones[i]);\n\tn = n % ones[i];\n\t\n\t// If the number was complete\n\tif (n == 0) return times;\n\t\n\t// Decide whether to use one more of i ones to the substract or not\n\treturn times + min( i + count_ones(ones[i] - n, i-1), count_ones(n, i-1));\n}\n\nint main() {\n\tones[0] = 0;\n\tfor (int i = 1; i <= 16; ++i) ones[i] = ones[i-1] * 10 + 1;\n\n\tlong long n;\n\tcin >> n;\n\tcout << count_ones(n, 16) << endl;\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "f7b08cd1e60995fc52500e3cd0387254", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "//ITNOG\n#include\n\nusing namespace std;\n#define int long long\n\n\nconst int maxN = 100*1000 + 5;\n//const int maxQ = 5000 + 5;\nconst int mod = 1000*1000*1000 + 7;\nint dp[17];\n\n\nint solve(int n, int t)\n{\n if(t == 0) \n {\n\t if(n == 0) return 0;\n\t return mod;\n }\n \n int cnt = n / dp[t]; //cout << cnt << endl;\n if(n % dp[t] == 0) return t * cnt;\n return cnt * t + min(solve(abs(n - dp[t] * (cnt + 1)), t - 1) + t, solve(n - dp[t] * cnt, t - 1)); \n}\nmain()\n{\n ios::sync_with_stdio(0); cin.tie(0);\n dp[1] = 1;\n for(int i = 2; i < 17; i ++)\n\t dp[i] = dp[i - 1] * 10 + 1;\n\n int n; cin >> n;\n// cout << solve(n, 2) << endl;\n cout << solve(n, 16) << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "ec3b81aaf956d8895269718694b13183", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nlong long n,b[20];\nlong long dfs(long long x,long long sum)\n{\n\tlong long num=x/b[sum];\n\tx=x%b[sum];\n\tif(x==0)\n\t\treturn num*sum;\n\telse\n\t\treturn num*sum+min(dfs(x,sum-1),sum+dfs(b[sum]-x,sum-1));\n}\nint main()\n{\n\tcin >> n;\n\tfor(int i=1;i<=17;i++)\n\t\tb[i]=b[i-1]*10ll+1ll;\n\tlong long m=0,x=n;\n\twhile(x!=0)\n\t{\n\t\tx/=10ll;\n\t\tm++;\n\t}\n\tcout << dfs(n,m+2) << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "divide and conquer", "dfs and similar"], "code_uid": "08372bd132eacf931cf7477e6a052448", "src_uid": "1b17a7b3b41077843ee1d6e0607720d6", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#define LL long long\nusing namespace std;\n//tim so chu so cua n\nint num(LL n){\n int num = 0;\n while(n){\n n /= 10;\n num++;\n }\n return num; \n}\n//tinh so co dang 11...1 lon nhat nhung khong vuot qua n\nLL find(LL n){\n int ex = num(n);\n LL cur = 0, ad = 1;\n for(int i = 0; i <= ex-1; i++){\n cur += ad;\n ad *= 10;\n }\n if(cur <= n) return cur;\n return ((cur-1)/10);\n}\n\nLL process(LL n){ \n if(n <= 6) return n;\n if(n <= 11) return (13 - n);\n LL less = find(n);\n LL more = less*10+1;\n if(n % less == 0){\n if(n <= more/2 + less) return num(less)*n / less;\n return num(more) + process(more - n);\n }\n else{\n LL _m = (n / less)*less;\n LL res = process(_m) + process(n - _m);\n if(n > more/2) res = min(process(_m) + process(n - _m), num(more) + process(more-n));\n return res;\n }\n}\nint main(){\n LL n;\n cin>>n;\n cout<\nusing namespace std;\n\n#define F cin\n#define G cout\n\nlong long n,a[16];\n\nlong long solve(long long n,int step)\n{\n long long ans = (step+1LL)*(n/a[step]);\n n %= a[step];\n if ( n == 0 ) return ans;\n return ans + min(solve(n, step-1), step+1 + solve(a[step]-n, step-1));\n}\n\nint main()\n{\n a[0] = 1;\n for (int i=1;i<16;++i)\n a[i] = a[i-1]*10 + 1;\n F>>n;\n G<\n\nusing namespace std;\n\n#ifdef LOCAL\n #define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define eprintf(...) 42\n#endif\n\nusing ll = long long;\nusing ld = long double;\nusing D = double;\nusing uint = unsigned int;\ntemplate\nusing pair2 = pair;\nusing pii = pair;\nusing pli = pair;\nusing pll = pair;\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define pb push_back\n#define mp make_pair\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n\nconst int answer2[2021] = {0, 1, 2, 4, 6, 8, 12, 16, 20, 26, 33, 40, 50, 61, 72, 87, 104, 121, 143, 167, 192, 224, 259, 295, 339, 387, 437, 497, 563, 631, 712, 801, 893, 1000, 1117, 1238, 1379, 1532, 1691, 1872, 2069, 2274, 2507, 2759, 3021, 3316, 3636, 3968, 4340, 4741, 5158, 5623, 6124, 6644, 7219, 7838, 8483, 9193, 9956, 10749, 11618, 12551, 13522, 14581, 15715, 16895, 18181, 19555, 20985, 22534, 24188, 25910, 27773, 29758, 31823, 34048, 36420, 38887, 41541, 44362, 47296, 50445, 53790, 57268, 60992, 64941, 69050, 73441, 78093, 82928, 88085, 93545, 99221, 105264, 111654, 118292, 125351, 132808, 140555, 148775, 157451, 166464, 176017, 186088, 196547, 207613, 219275, 231383, 244181, 257649, 271629, 286389, 301915, 318025, 335013, 352863, 371385, 390894, 411381, 432628, 454985, 478448, 502781, 528357, 555175, 582975, 612175, 642773, 674488, 707762, 742604, 778710, 816566, 856180, 897219, 940204, 985166, 1031733, 1080475, 1131419, 1184169, 1239342, 1296983, 1356651, 1419012, 1484114, 1551500, 1621879, 1695319, 1771308, 1850620, 1933336, 2018911, 2108169, 2201208, 2297430, 2397746, 2502260, 2610330, 2722917, 2840156, 2961358, 3087567, 3218928, 3354697, 3495983, 3642979, 3794876, 3952872, 4117166, 4286898, 4463357, 4646781, 4836231, 5033091, 5237619, 5448835, 5668212, 5896052, 6131274, 6375467, 6628978, 6890664, 7162208, 7444003, 7734806, 8036449, 8349358, 8672217, 9006953, 9354056, 9712125, 10083238, 10467918, 10864668, 11275687, 11701596, 12140784, 12595614, 13066740, 13552450, 14055271, 14575951, 15112644, 15668044, 16242956, 16835452, 17448387, 18082666, 18736202, 19412048, 20111207, 20831484, 21576103, 22346166, 23139309, 23959014, 24806474, 25679197, 26580844, 27512739, 28472234, 29463255, 30487223, 31541332, 32629717, 33753992, 34911161, 36105636, 37339129, 38608483, 39918386, 41270741, 42662175, 44097665, 45579240, 47103389, 48675380, 50297444, 51965815, 53686096, 55460717, 57285728, 59167038, 61107291, 63102267, 65158298, 67278236, 69457647, 71703173, 74017922, 76397220, 78848152, 81374045, 83969977, 86643390, 89397957, 92228464, 95142826, 98144942, 101229335, 104404384, 107674355, 111033430, 114490480, 118050050, 121706096, 125467978, 129340637, 133317637, 137408880, 141619696, 145943379, 150390349, 154966353, 159664278, 164495221, 169465334, 174567181, 179812402, 185207630, 190745063, 196437070, 202290722, 208297832, 214471372, 220819046, 227332236, 234024685, 240904568, 247962863, 255214063, 262667022, 270312228, 278164978, 286234669, 294511456, 303011440, 311744736, 320700935, 329897028, 339343848, 349030595, 358975121, 369189027, 379660902, 390409679, 401447743, 412763223, 424375956, 436299202, 448520551, 461060995, 473934631, 487128517, 500664632, 514558170, 528795598, 543400126, 558387836, 573744623, 589494913, 605655980, 622213039, 639191823, 656610611, 674454125, 692749413, 711516042, 730737958, 750443631, 770653908, 791352191, 812568331, 834324553, 856603433, 879436517, 902847437, 926818156, 951381682, 976563168, 4099492, 30514210, 57589628, 85306270, 113700592, 142801114, 172587556, 203098326, 234363569, 266362239, 299134671, 332713066, 367075477, 402264295, 438313522, 475200581, 512969956, 551657858, 591240712, 631765230, 673269807, 715730192, 759195328, 803705981, 849236828, 895839454, 943557047, 992363494, 44068385, 95206229, 147505623, 201023388, 255807958, 311831397, 369153064, 427824496, 487816780, 549192323, 612005495, 676226453, 741920607, 809145738, 877870910, 948164773, 21843859, 95365195, 170556386, 247481995, 326109113, 406513189, 488762479, 572823307, 658774643, 746688724, 836530571, 928383195, 24078565, 120069544, 218198520, 318546165, 421075572, 525877907, 633038239, 742518685, 854414432, 968815614, 87438920, 206872986, 328968347, 453684991, 581127521, 711396425, 844450483, 980399334, 121104325, 263012188, 407993323, 556159472, 707467342, 862037082, 21742139, 183027245, 347773661, 516105681, 687977935, 863522236, 44625223, 227729465, 414728276, 605759711, 800776548, 1678892, 205100749, 412749672, 624776404, 841334383, 64130169, 289810445, 520292122, 755525204, 995674854, 242666962, 492939183, 748420053, 11043951, 277248052, 548968795, 826394592, 111228413, 400148067, 695107338, 996053224, 304938931, 618461754, 938323134, 266485705, 599657542, 939539742, 288103949, 642069557, 4892619, 373288154, 749254152, 134490686, 525722455, 924960250, 333903686, 749288499, 174893693, 607151626, 49831640, 499704271, 958468342, 428149344, 905531434, 394069384, 890533841, 398746834, 915140080, 443519432, 980699729, 530135973, 90380014, 660018827, 242507025, 834655014, 440333662, 57731445, 685428265, 327312793, 979818225, 646807015, 326919230, 20100260, 724960464, 445426817, 179689898, 926376784, 689434296, 467054085, 259635534, 67636119, 889248517, 728401249, 583817590, 455447951, 343727870, 249159940, 171695795, 111790093, 69969400, 46188140, 40920659, 54719059, 87539191, 139876752, 212310215, 304798556, 417858551, 552096273, 707473286, 884529730, 85655813, 307305317, 551796575, 819794852, 113024420, 428561753, 768858470, 135642780, 526016294, 942464263, 386718944, 855908847, 354308016, 878387151, 432324935, 15164771, 625137557, 266450752, 936428957, 638576648, 371863954, 137165895, 932725142, 762810620, 626582104, 524045397, 455989888, 423373102, 426207739, 465318068, 541705191, 655390132, 807231226, 31756, 230309868, 500717009, 812347797, 166996485, 562085284, 2271079, 484090662, 12028957, 583770869, 202887175, 866881439, 580516117, 342105701, 152707995, 13629947, 923199528, 886030181, 901732338, 970401427, 94942626, 273271748, 507254806, 798088920, 149020450, 556662914, 25776488, 554406495, 146205580, 798962253, 517814287, 301164405, 150367985, 67122481, 51604520, 105225544, 229759217, 425401129, 693622328, 38028387, 455325206, 948800819, 522142956, 173834135, 903714902, 717312272, 613133632, 592841130, 658538957, 810513240, 52251704, 382438497, 803142905, 317923380, 925559094, 629902187, 431076077, 331469545, 331453737, 432983464, 638551309, 948559719, 366801210, 892362877, 529191570, 277651498, 140454039, 118069315, 212705191, 427186772, 762022350, 221265904, 804352933, 515341098, 354867194, 326002123, 429335701, 667357683, 45025401, 559459081, 216762560, 18516829, 963645586, 60112845, 306128641, 704175658, 258818528, 970168275, 842515586, 877025705, 79229451, 446448992, 983481097, 696012849, 583179154, 648143130, 895002193, 326462196, 942303951, 750303778, 749720816, 943979940, 339280291, 933190768, 732785659, 740935042, 958791609, 391816817, 43072438, 912026743, 7802752, 330148788, 882123959, 669493206, 695721665, 962193014, 474835362, 237326667, 251137633, 520604731, 53140084, 846792988, 909586559, 247405746, 858394465, 750754246, 928855877, 396208736, 155935508, 214407497, 573483969, 239992650, 218808848, 511899500, 126293510, 67131540, 336498036, 939874412, 886191214, 177650056, 816439290, 813532359, 171259380, 892032030, 987121993, 458989775, 312039446, 554340064, 190247731, 224405547, 665205096, 517147203, 785129294, 479629886, 601790312, 160278279, 162409722, 611239747, 515708618, 883486988, 719553697, 31374085, 825237504, 109806327, 887570467, 173980604, 967350997, 281007176, 122022905, 494166105, 407566421, 871467145, 891594172, 476645036, 636292538, 376468640, 704456494, 633884508, 169148844, 317878624, 94165522, 500877553, 549521545, 252909642, 614147821, 645116879, 359128633, 759536828, 858615088, 670184559, 199616008, 456076342, 455678262, 204061423, 710812730, 992593907, 57087546, 910803590, 574489058, 52606119, 355629098, 501385642, 496405940, 353385464, 89009754, 708372302, 228169324, 662209601, 21192103, 315288343, 564234280, 775567459, 963495893, 148434937, 334776318, 539020624, 780538691, 69369175, 417308184, 846208357, 366507629, 990581601, 742800469, 630502657, 670176204, 883460752, 281642801, 878320815, 699460721, 753282215, 59298265, 637292389, 501224629, 667762630, 162827328, 993840757, 185191395, 756677652, 723256070, 104759855, 923677983, 197251829, 940796077, 186540668, 941745138, 233023941, 84833664, 510274075, 533249412, 182755374, 470736496, 421922611, 66384957, 416687540, 498415967, 342744454, 962890790, 387081216, 644128500, 751429220, 734618126, 627204224, 445535459, 216193513, 972158798, 734015645, 527580220, 388856310, 335680013, 396634600, 609039037, 991518091, 575471572, 397817439, 478001526, 846754595, 545917246, 594031697, 24719103, 875998610, 174324759, 947465042, 243725929, 81702509, 495640411, 531883334, 213503574, 574243060, 662056959, 501005941, 127887212, 588805121, 910618243, 133218512, 302672915, 446909595, 605461704, 829682246, 148621598, 599731023, 239714015, 95269077, 208853774, 635554991, 406788266, 564788163, 168367852, 246681986, 845318641, 26866235, 818270692, 272033193, 445824164, 374957907, 108373769, 707657978, 211289781, 666458813, 142263581, 671605576, 310523780, 125163156, 153454167, 449806808, 86269708, 100576649, 549097486, 506350884, 13458062, 127041193, 922419846, 445911578, 754478669, 927830417, 12279270, 66937845, 174250656, 380587697, 750811208, 370171231, 285167681, 562968847, 291743679, 519940080, 318868546, 776183306, 945878146, 899972768, 730988366, 493251967, 261336543, 130967404, 158658661, 421614882, 20628860, 12707152, 477790317, 520088808, 200711955, 600650281, 827648657, 945239349, 40847139, 220591941, 552275882, 126323616, 54387162, 405115528, 272052035, 770724671, 974240641, 977600013, 902112079, 821919869, 835346437, 69590627, 598193275, 526419836, 982245085, 49221025, 827385502, 456150126, 16921020, 616907451, 398326125, 445792323, 872069933, 822324071, 386269896, 678814317, 849983684, 992944578, 230169981, 711427157, 536971647, 829928506, 750518734, 399187449, 903383119, 430407721, 82787824, 992424335, 333898762, 211944724, 764822284, 171031048, 539425950, 16528792, 779694449, 949037197, 672479592, 136938780, 461677482, 801437079, 351137906, 234583322, 610007792, 678772965, 571161944, 450799257, 523942404, 925747125, 827190849, 441382441, 906768537, 401831311, 145013192, 281732848, 992923671, 509575369, 978997834, 591775204, 581173314, 105347252, 355958340, 577531072, 932256320, 621850941, 895346944, 922647312, 910475842, 119628455, 720935779, 931602258, 20940476, 166152473, 589904096, 570285841, 294629546, 989503045, 943810490, 351707939, 447451356, 527518020, 793126563, 489942277, 920573674, 297045783, 867939978, 951036135, 762396203, 562537736, 675979784, 328329691, 787050094, 390700103, 369451959, 3251647, 636280362, 512284990, 916964169, 210969211, 639650377, 503518932, 171271459, 898887147, 996751988, 845467616, 711973387, 913290115, 844052807, 780688227, 52483577, 63204205, 100842470, 503728417, 692194640, 962604635, 666222051, 235036633, 974124668, 249765682, 504305588, 58887119, 284642544, 643429173, 465817567, 136935902, 131604513, 790142952, 513821731, 790845229, 975217570, 479466794, 807406769, 327066330, 460911231, 732340725, 520339915, 262951627, 498388629, 618733374, 78085995, 429958981, 81723308, 498661857, 257155539, 773271158, 532755233, 126559258, 985171647, 611732006, 614114717, 441191760, 606969926, 742019943, 310596392, 841573218, 987086186, 227214448, 107976234, 300123439, 300122319, 673403661, 113468701, 130125453, 308793816, 362830451, 821242729, 289945655, 501048357, 5034471, 421924217, 512004582, 840798457, 53497858, 926994928, 52962706, 90139005, 844955326, 925315273, 16383964, 944380288, 341068837, 908168052, 506406384, 779798076, 459556610, 427694533, 353368869, 987446457, 244502927, 810803066, 466331324, 148513544, 570134608, 534035904, 8331858, 727841745, 522457414, 388330301, 88282946, 473067291, 572997367, 176169365, 160177726, 585170342, 267045348, 111113539, 209972873, 404631941, 630663703, 15891039, 425492102, 826177200, 380139416, 981136230, 627883151, 514503794, 567660834, 813723420, 486753687, 542032423, 39763648, 248006427, 156781620, 855861479, 657153761, 579428577, 748208609, 513357387, 926846799, 152795869, 576643268, 289867578, 487363763, 601905030, 756728477, 187431881, 366486101, 455239552, 725775579, 698612632, 570785796, 653776272, 513833449, 386619355, 624159464, 839830718, 310771890, 429004335, 856331393, 910902020, 31211249, 923636103, 951992222, 597293686, 620678214, 425964135, 539662322, 777393227, 587740380, 544135714, 516412006, 997535359, 612646536, 285633265, 556964929, 101481071, 898630281, 543086594, 755510445, 579659107, 655320976, 759276753, 994142842, 55156240, 767950734, 302840365, 403193792, 955619908, 192811148, 911465174, 60762306, 930597541, 384813423, 422834218, 409401999, 261483656, 42655397, 185654679, 667497431, 616012872, 535634024, 465236300, 595081431, 505128043, 297911065, 229838918, 954863232, 644578393, 621616629, 619668809, 874269669, 781534711, 155203568, 298544981, 679631588, 196363661, 223437561, 302960421, 415707569, 13515608, 711111667, 579817498, 145367927, 102616550, 608808893, 271169275, 863302053, 637904289, 278854856, 644436632, 81821175, 355669397, 412926362, 694414611, 54964503, 523954378, 645471547, 359968052, 788681183, 578339180, 757516980, 543668132, 685352031, 307195916, 718443169, 779550867, 708487860, 914265514, 368626240, 385746710, 477151865, 727231752, 554305063, 571809873, 981656437, 307575381, 267724556, 186540228, 690769600, 613443344, 401302903, 790721374, 727259790, 785195760, 815380773, 877456509, 678366032, 184913442, 573962746, 688844774, 615590381, 655185331, 787354778, 222573289, 385819653, 400649394, 601825283, 547114325, 504156653, 937477029, 539450670, 725832509, 97920471, 479449229, 445348233, 730594182, 304484480, 894786804, 382687354, 876560383, 273561006, 592828975, 97930544, 845841741, 13489606, 9860998, 68421312, 514040917, 917510780, 688308603, 308780830, 508891301, 878693386, 66009142, 962612313, 349447869, 35423592, 87475629, 471775284, 173657943, 432842386, 412943722, 273276422, 434735230, 261832859, 94695322, 538414491, 165565660, 500216590, 340390443, 466957742, 598423909, 725453495, 848234975, 881436172, 17384986, 475986551, 377031529, 116313095, 146616970, 791369126, 660770759, 442876658, 674063447, 181118811, 892086543, 567267306, 252434553, 127166268, 173109099, 666299275, 44529020, 516739924, 597276795, 981825180, 123700694, 771218496, 893689192, 186364740, 647026179, 520412782, 750467161, 594401455, 576251690, 898939173, 82686046, 938126856, 937708508, 866266355, 837669720, 592269847, 192098560, 56599567, 205018788, 982166672, 126610968, 938619123, 60417736, 543727449, 991858781, 341671185, 977129533, 805270389, 67427416, 488407271, 285632388, 14676454, 750097623, 29649783, 728251065, 284545139, 556536248, 757362815, 688661754, 547958159, 886920909, 884824772, 86017887, 389554799, 362382452, 901387773, 271187862, 429145282, 639559481, 533268963, 477399118, 109697280, 437009732, 246573242, 558050899, 769525916, 95771306, 948310065, 128586186, 284645364, 239433246, 197531416, 262176453, 669504156, 47035150, 956016187, 65719535, 426811397, 82878087, 133507613, 78179433, 442537803, 778649268, 40922458, 251593381, 425296807, 979895406, 455498689, 336348140, 519375925, 69001416, 954861501, 568999861, 509858687, 250808184, 681235443, 956325832, 62464217, 403147748, 700827766, 466928140, 637554148, 515176398, 149795715, 22443053, 30396022, 776517995, 304057563, 118663311, 392194212, 737923023, 292227227, 807328628, 486366474, 105656841, 18213417, 27146017, 568185794, 610861760, 575485082, 574289279, 205540203, 522752620, 333270567, 879011810, 868278398, 815981806, 629348113, 681343498, 217569422, 822853626, 558777805, 414816215, 675983147, 105119088, 456129819, 732440196, 415776134, 47436640, 362356034, 581767393, 53878908, 265868162, 197087158, 19715774, 994283891, 879601062, 692140281, 487561921, 816836420, 567642642, 607355689, 306514041, 438405919, 706833065, 320929650, 963820865, 198423360, 88690196, 255514716, 136230757, 678220326, 461778683, 820740133, 610412824, 389074564, 415513953, 470181881, 119235685, 566285570, 544376546, 648805630, 57411430, 472513135, 549839358, 460814473, 908922894, 632604973, 822302676, 209497342, 637437945, 348203115, 120140171, 935484533, 111328523, 499287897, 252518612, 781006347, 49191678, 397551074, 369547591, 57602174, 29628582, 981866228, 172305602, 419451595, 608755665, 183579945, 251157466, 910564423, 826106642, 423353828, 47391678, 608297608, 884565347, 500283800, 646314918, 483212749, 942783583, 533742331, 829699471, 109690883, 222547425, 198046322, 687692016, 924422819, 424788175, 250113151, 46671996, 852255937, 180806582, 123215899, 281116366, 646137554, 798974091, 939664901, 580003018, 819694156, 499911123, 686681966, 44025974, 86828623, 479361944, 482795682, 334082239, 331747923, 373923999, 459103235, 562543041, 261299985, 357067424, 542958458, 116393997, 725573864, 826163315, 476472589, 219872186, 312144263, 617781034, 620954098, 426198734, 746432063, 53622136, 342557307, 225740431, 205602345, 221155702, 823277998, 601212187, 480384464, 3535769, 887816399, 102725500, 223963789, 159050633, 957392975, 291875829, 301770979, 178251381, 728053808, 388998366, 538921108, 179429083, 90842450, 896984163, 845866725, 122285137, 646599177, 966297568, 728006335, 210666282, 313205620, 203101314, 571117110, 732363248, 433621931, 836581792, 728542915, 497481491, 837532722, 68797065, 281347647, 765049368, 431670062, 143738365, 844354663, 103557556, 616285894, 52186140, 692847745, 146205666, 859772859, 908168103, 866663048, 42575440, 356511459, 430929446, 493996760, 391715441, 858619339, 120258044, 10774654, 456000940, 744986166, 779480405, 750308184, 85974036, 821455289, 495322621, 747106472, 829809642, 699519098, 291060249, 144604665, 718685165, 322228492, 861716848, 385148203, 647733719, 11142634, 187678686, 471448817, 752910717, 504844089, 638316867, 660145656, 887190178, 938205410, 22685276, 389776982, 455169670, 216818093, 953907225, 966859160, 134545046, 857012024, 414108695, 653238616, 200909244, 401726601, 175532403, 462982324, 780720725, 211227999, 114449495, 274266061, 34860355, 279450347, 162224033, 387607811, 470355417, 36014479, 254116262, 376210714, 605592670, 685596838, 713699658, 579729932, 705253438, 149500294, 596855417, 261947189, 275875218, 236676089, 258331666, 666770221, 88077034, 649645173, 994703633, 891533801, 606738414, 218132987, 757084516, 748798616, 834053130, 435907800, 454848061, 225665108, 684348765, 240747497, 51017762, 696074989, 220363945, 736578462, 606792767, 636112630, 38860062, 83231486, 476715801, 669911334, 983215395, 163307065, 37203372, 117426394, 327612522, 26518142, 60111240, 673141579, 902792264, 80512222, 916641064, 282895284, 139918703, 821383558, 185688185, 979945249, 317045299, 195576536, 313768729, 709417768, 698383262, 84918327, 993882020, 224386598, 846662841, 248552502, 869960735, 229613930, 130244107, 840523811, 490144985, 475944810, 72111343, 190112496, 170340, 958784367, 949925569, 89317722, 205789585, 331483534, 713405904, 747573452, 801813954, 441614579, 818479730, 832456776, 557414216, 102873697, 91804879, 302528568, 6951786, 751955981, 223243330, 55924757, 929576348, 637192543, 395141788, 223041477, 226038587, 419289643, 377266189, 732673109, 520809215, 85910357, 806463433, 963649584, 896936296, 954598470, 891351471, 270553404, 635101334, 453581286, 742501526, 476757332, 75450402, 249184754, 637400501, 859528283, 565150907, 299445750, 133382188, 899310419, 302600188, 119353704, 622678142, 923468026, 773351888, 143508061, 806988289, 758134418, 930612039, 33005788, 565782753, 700837546, 345657600, 796738642, 736132589, 545470203, 603207283, 382769936, 24466838, 277829946, 697786903, 470647192, 17813970, 268061582, 746430442, 850508249, 189199847, 921549557, 735647740, 223317638, 490981509, 827724999, 128410260, 755745874, 930465676, 165634029, 403513576, 124905360, 788468406, 248407584, 578709202, 525013854, 177342297, 556707812, 34961665, 278191847, 609205586, 372126098, 163907792, 963136209, 447993159, 496047089, 117653547, 679828181, 713471676, 629064912, 863216292, 962508869, 128274828, 241791128, 247384760, 523436716, 791735602, 778639832, 444330549, 739656161, 571437254, 887142803, 279942023, 233828457, 106060063, 535941826, 7924040, 707429221, 751629473, 36517261, 17755331, 712729289, 865451558, 646409217, 416607917, 200692067, 336641799, 979387316, 888416017, 31441130, 812551235, 183568672, 212932908, 31424549, 241309553, 500637949, 140847875, 895968732, 507531234, 989819307, 704222810, 968377693, 990420049, 247715974, 154506580, 613966233, 731438735, 544753645, 174851692, 867009055, 819581331, 900249795, 33976925, 115700533, 306832774, 744165078, 590225935, 847228311, 421781113, 303456866, 905212099, 464688273, 385135389, 66986870, 648381623, 543616219, 726650628, 830838423, 881299720, 30430100, 553617, 49082078, 118721011, 637730178, 726601839, 743225133, 447119252, 463146180, 208385423, 406090433, 847940444, 660655236, 187362725, 53963296, 760978639, 935870485, 727985672, 694013267, 421472410, 278749113, 574270987, 548985657, 507424656, 218051871, 279660929, 659280914, 311132094, 913499526, 843714904, 972816220, 801302355, 867476364, 721533143, 431879083, 476638337, 848673933, 955413763, 6814103, 222642992, 129999699, 472751165, 506490940, 675251949, 84218479, 833073632, 104687862, 198676209, 902164536, 963080920, 738674105, 550935311, 561012005, 59856346, 770931835, 143486216, 284759565, 215939822, 548445724, 129434351, };\n\n\nconst int answer3[2021] = {0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 2, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 2, 0, 1, 2, 0, 1, 2, 1, 0, 2, 1, 0, 2, 3, 1, 1, 1, 2, 1, 2, 2, 1, 0, 1, 3, 1, 2, 2, 2, 1, 1, 1, 1, 4, 4, 0, 3, 2, 0, 3, 1, 2, 1, 3, 2, 2, 1, 2, 4, 3, 2, 3, 1, 1, 3, 3, 1, 4, 5, 2, 3, 1, 3, 2, 3, 0, 4, 2, 3, 4, 3, 2, 4, 2, 3, 4, 3, 1, 4, 6, 2, 3, 2, 5, 3, 3, 4, 4, 2, 3, 4, 5, 1, 3, 5, 1, 3, 5, 6, 3, 3, 3, 4, 5, 7, 5, 2, 4, 5, 2, 5, 6, 5, 1, 6, 6, 2, 7, 6, 4, 3, 3, 4, 4, 5, 7, 4, 5, 3, 6, 5, 2, 8, 8, 2, 4, 7, 4, 5, 10, 5, 7, 7, 2, 5, 5, 6, 3, 7, 8, 5, 4, 5, 12, 7, 6, 8, 3, 1, 7, 5, 4, 8, 9, 6, 7, 6, 5, 7, 11, 8, 6, 8, 6, 6, 6, 2, 12, 10, 7, 7, 8, 5, 5, 11, 6, 7, 9, 9, 7, 7, 7, 7, 6, 12, 8, 8, 5, 9, 8, 5, 8, 9, 12, 11, 7, 7, 9, 10, 8, 13, 8, 8, 7, 10, 10, 9, 8, 6, 14, 9, 8, 13, 10, 9, 8, 9, 9, 8, 12, 9, 11, 8, 9, 13, 10, 7, 9, 12, 12, 9, 13, 6, 13, 15, 9, 9, 12, 13, 11, 14, 12, 9, 10, 9, 10, 12, 13, 16, 13, 9, 10, 13, 10, 17, 17, 5, 12, 19, 12, 12, 9, 12, 11, 13, 7, 14, 11, 15, 15, 12, 15, 14, 8, 11, 15, 18, 11, 19, 23, 10, 8, 12, 17, 12, 14, 14, 14, 11, 11, 20, 20, 16, 14, 15, 13, 13, 19, 14, 16, 21, 11, 15, 20, 13, 13, 13, 22, 12, 17, 16, 14, 17, 13, 15, 20, 12, 15, 15, 18, 14, 17, 16, 17, 24, 20, 20, 16, 19, 18, 16, 13, 20, 22, 17, 17, 14, 14, 20, 18, 20, 23, 17, 18, 19, 18, 22, 24, 22, 21, 19, 15, 16, 25, 24, 12, 17, 23, 16, 19, 23, 16, 22, 20, 23, 21, 17, 15, 22, 28, 23, 20, 16, 22, 27, 24, 18, 25, 22, 12, 20, 30, 18, 23, 31, 21, 16, 27, 25, 21, 23, 23, 23, 21, 27, 24, 26, 16, 29, 27, 18, 24, 30, 17, 20, 33, 22, 26, 24, 29, 21, 18, 29, 26, 21, 30, 26, 22, 18, 36, 32, 19, 30, 28, 20, 26, 25, 27, 25, 32, 35, 25, 31, 15, 27, 30, 23, 32, 31, 28, 25, 27, 23, 34, 41, 27, 28, 33, 28, 22, 22, 36, 29, 33, 35, 31, 26, 25, 32, 36, 27, 23, 36, 23, 36, 33, 26, 27, 41, 36, 28, 32, 28, 28, 36, 28, 27, 30, 36, 34, 34, 29, 33, 39, 41, 37, 30, 34, 29, 31, 31, 29, 30, 43, 42, 28, 32, 31, 34, 34, 41, 37, 28, 35, 42, 35, 38, 43, 35, 40, 43, 37, 36, 37, 39, 31, 36, 29, 40, 37, 27, 49, 41, 42, 37, 38, 28, 32, 39, 51, 43, 39, 35, 37, 38, 46, 40, 47, 39, 32, 37, 34, 41, 38, 44, 51, 39, 34, 37, 51, 40, 42, 55, 39, 38, 46, 33, 39, 47, 41, 42, 45, 45, 44, 47, 45, 52, 43, 38, 45, 38, 43, 45, 60, 44, 41, 52, 46, 50, 50, 53, 34, 42, 46, 58, 43, 30, 51, 49, 38, 62, 51, 39, 44, 59, 37, 44, 57, 46, 43, 60, 46, 51, 59, 55, 47, 45, 43, 55, 60, 45, 51, 46, 44, 40, 53, 55, 51, 62, 57, 43, 52, 58, 52, 56, 59, 56, 57, 54, 53, 55, 42, 57, 59, 49, 65, 56, 52, 45, 62, 60, 54, 59, 59, 57, 58, 56, 55, 51, 63, 54, 55, 52, 60, 66, 57, 55, 57, 63, 65, 59, 51, 44, 73, 63, 56, 66, 52, 61, 66, 63, 65, 61, 58, 66, 61, 60, 62, 60, 48, 60, 69, 66, 66, 71, 53, 59, 64, 65, 78, 77, 52, 59, 71, 67, 65, 75, 71, 59, 69, 69, 55, 66, 77, 70, 73, 72, 65, 59, 54, 73, 63, 72, 81, 74, 65, 62, 72, 70, 75, 76, 66, 67, 66, 74, 80, 72, 69, 72, 67, 70, 70, 79, 69, 74, 78, 60, 82, 83, 78, 69, 73, 74, 73, 77, 73, 62, 82, 78, 76, 84, 67, 75, 78, 81, 88, 77, 78, 66, 88, 76, 78, 91, 76, 81, 86, 77, 78, 88, 75, 81, 72, 82, 91, 85, 85, 84, 84, 62, 89, 90, 86, 75, 91, 71, 72, 91, 93, 92, 94, 91, 69, 85, 84, 94, 87, 79, 95, 96, 81, 90, 80, 76, 84, 105, 85, 90, 97, 92, 94, 84, 89, 85, 91, 81, 102, 87, 66, 106, 109, 83, 88, 95, 89, 91, 100, 90, 86, 99, 104, 94, 82, 92, 99, 110, 107, 103, 93, 81, 103, 107, 88, 104, 111, 93, 82, 102, 98, 90, 128, 98, 77, 97, 109, 98, 103, 100, 94, 107, 109, 98, 112, 91, 113, 119, 95, 95, 104, 112, 82, 128, 123, 98, 105, 116, 97, 92, 111, 106, 101, 112, 106, 105, 99, 113, 120, 106, 114, 113, 108, 109, 98, 109, 100, 116, 112, 107, 113, 97, 127, 120, 114, 129, 108, 111, 103, 120, 109, 128, 126, 104, 106, 118, 108, 121, 125, 112, 112, 120, 121, 138, 122, 103, 124, 126, 116, 110, 139, 114, 116, 130, 109, 111, 127, 130, 127, 117, 134, 124, 112, 121, 132, 145, 126, 119, 134, 99, 113, 137, 146, 131, 135, 132, 112, 114, 133, 138, 133, 149, 130, 116, 117, 121, 142, 131, 139, 138, 114, 116, 145, 121, 126, 147, 148, 127, 130, 132, 123, 143, 156, 148, 135, 132, 141, 133, 118, 136, 150, 127, 138, 164, 129, 136, 148, 151, 137, 150, 140, 137, 151, 147, 139, 125, 142, 155, 140, 135, 148, 145, 137, 147, 154, 149, 157, 164, 141, 139, 153, 161, 150, 163, 142, 142, 145, 139, 158, 160, 127, 159, 172, 141, 144, 167, 142, 143, 179, 134, 148, 169, 169, 151, 153, 160, 145, 163, 153, 158, 155, 148, 185, 165, 151, 154, 162, 155, 146, 163, 168, 158, 181, 164, 153, 157, 154, 153, 177, 174, 159, 184, 160, 153, 178, 173, 187, 188, 166, 148, 164, 183, 168, 182, 163, 162, 167, 163, 166, 160, 181, 181, 192, 173, 168, 176, 143, 179, 197, 176, 175, 185, 186, 165, 186, 170, 178, 192, 198, 174, 177, 180, 171, 190, 177, 194, 189, 185, 175, 180, 166, 200, 188, 188, 173, 190, 188, 184, 188, 176, 179, 206, 188, 182, 185, 184, 197, 196, 201, 171, 207, 159, 209, 211, 179, 178, 203, 216, 177, 211, 187, 186, 197, 224, 205, 188, 193, 193, 194, 192, 202, 208, 191, 195, 233, 196, 203, 214, 192, 191, 220, 218, 180, 203, 223, 202, 214, 214, 212, 177, 201, 220, 222, 207, 199, 221, 197, 207, 224, 209, 224, 224, 218, 215, 184, 203, 224, 227, 233, 233, 203, 193, 225, 225, 212, 224, 249, 233, 204, 211, 212, 218, 236, 245, 222, 216, 215, 236, 227, 190, 221, 239, 220, 211, 234, 219, 219, 261, 238, 217, 230, 236, 225, 231, 217, 240, 235, 240, 248, 227, 223, 234, 254, 227, 214, 251, 231, 244, 263, 221, 226, 265, 263, 246, 234, 240, 219, 237, 239, 248, 256, 251, 239, 238, 219, 227, 259, 247, 255, 277, 250, 243, 257, 246, 253, 240, 285, 260, 245, 253, 236, 245, 252, 257, 263, 249, 261, 270, 236, 250, 269, 260, 271, 295, 254, 255, 293, 252, 249, 261, 244, 264, 276, 246, 274, 265, 262, 276, 293, 254, 250, 270, 270, 261, 269, 266, 264, 301, 281, 266, 253, 273, 276, 275, 275, 274, 269, 263, 306, 270, 279, 269, 308, 297, 266, 299, 279, 282, 293, 254, 275, 306, 263, 289, 307, 278, 276, 321, 287, 264, 305, 285, 283, 302, 288, 280, 286, 299, 282, 288, 293, 285, 320, 296, 288, 298, 306, 287, 324, 291, 299, 332, 302, 291, 320, 317, 285, 321, 289, 312, 300, 291, 279, 316, 314, 327, 334, 319, 308, 301, 312, 321, 305, 313, 288, 306, 311, 301, 306, 336, 330, 321, 328, 310, 293, 308, 324, 355, 340, 307, 337, 307, 329, 361, 312, 301, 351, 304, 304, 326, 319, 326, 350, 334, 315, 328, 339, 340, 337, 324, 341, 342, 327, 361, 340, 316, 338, 369, 357, 316, 348, 358, 303, 348, 334, 324, 354, 366, 350, 324, 342, 324, 349, 346, 353, 371, 351, 365, 386, 345, 320, 360, 356, 320, 354, 360, 348, 365, 362, 377, 352, 363, 383, 349, 346, 357, 367, 374, 367, 385, 349, 360, 414, 373, 345, 359, 412, 358, 340, 382, 364, 364, 343, 392, 380, 350, 388, 373, 360, 383, 407, 360, 361, 394, 406, 368, 394, 398, 368, 380, 360, 392, 387, 381, 389, 401, 381, 377, 396, 372, 366, 413, 435, 393, 381, 418, 373, 410, 416, 375, 385, 407, 416, 419, 348, 394, 429, 373, 427, 415, 383, 389, 435, 410, 373, 399, 420, 392, 438, 436, 407, 389, 428, 437, 389, 395, 421, 436, 388, 401, 446, 414, 413, 422, 406, 393, 446, 434, 424, 446, 459, 419, 413, 414, 423, 444, 438, 483, 449, 377, 428, 448, 413, 448, 463, 427, 406, 453, 437, 421, 454, 442, 433, 471, 401, 438, 475, 417, 455, 455, 440, 445, 472, 443, 428, 463, 471, 425, 445, 463, 429, 471, 463, 449, 455, 460, 481, 462, 462, 489, 479, 454, 435, 456, 445, 465, 484, 461, 463, 446, 465, 465, 501, 475, 443, 474, 474, 464, 515, 462, 455, 514, 452, 487, 487, 480, 457, 535, 483, 448, 464, 480, 492, 510, 457, 484, 500, 506, 488, 508, 487, 464, 541, 503, 493, 493, 492, 474, 515, 487, 453, 496, 553, 497, 517, 515, 485, 502, 479, 515, 517, 489, 542, 564, 489, 499, 562, 553, 482, 507, 506, 498, 528, 552, 509, 480, 521, 534, 521, 510, 544, 506, 534, 524, 535, 513, 541, 534, 555, 504, 555, 542, 549, 577, 527, 502, 517, 524, 514, 549, 496, 572, 568, 527, 546, 576, 534, 554, 593, 522, 537, 583, 555, 497, 582, 568, 549, 560, 561, 542, 543, 578, 579, 574, 536, 527, 538, 566, 544, 585, 575, 538, 589, 583, 533, 600, 596, 592, 620, 544, 543, 568, 585, 591, 587, 559, 602, 583, 573, 560, 601, 555, 591, 629, 567, 557, 589, 620, 569, 611, 631, 607, 607, 630, 560, 588, 598, 613, 603, 598, 580, 622, 581, 570, 631, 608, 600, 639, 614, 584, 605, 656, 613, 623, 598, 608, 632, 614, 626, 609, 590, 646, 661, 616, 606, 650, 595, 590, 649, 647, 616, 632, 672, 630, 600, 674, 640, 620, 656, 614, 631, 619, 665, 679, 644, 634, 681, 652, 607, 663, 644, 623, 643, 661, 649, 630, 677, 641, 626, 673, 689, 690, 663, 664, 652, 615, 682, 693, 657, 627, 676, 686, 660, 685, 683, 703, 630, 671, 691, 655, 663, 685, 721, 687, 679, 715, 659, 680, 745, 661, 672, 735, 710, 642, 688, 691, 688, 719, 691, 695, 693, 696, 742, 723, 689, 682, 725, 696, 679, 743, 678, 698, 758, 728, 689, 744, 710, 701, 735, 708, 688, 737, 733, 715, 734, 666, 723, 775, 776, 714, 734, 711, 704, 738, 763, 694, 752, 769, 740, 704, 724, 760, 747, 779, 731, 762, 716, 730, 747, 761, 716, 777, 757, 759, 782, 758, 764, 794, 764, 706, 764, 746, 793, 767, 740, 771, 794, 728, 783, 779, 772, 749, 801, 755, 749, 827, 784, 786, 782, 776, 738, 846, 813, 785, 781, 819, 752, 742, 805, 775, 785, 809, 809, 824, 798, 776, 825, 795, 811, 822, 809, 771, };\n\n\nostream& operator<<(ostream &out, const vector &b)\n{\n\tout << \"{\";\n\tfor (auto t : b) out << t << ' ';\n\tout << \"}\";\n\treturn out;\n}\n\nconst int maxn = 2030;\nconst int MOD = 998244353;\n\nint n, k;\n//int answer3[maxn];\n\nbool expand(vector cur, int curk)\n{\n\t//cout << curk << ' ' << cur.size() << ' ' << cur << endl;\n\tif ((int)cur.size() > n) return false;\n\tif (curk == k)\n\t{\n\t\t//answer3[(int)cur.size()]++;\n\t\treturn true;\n\t}\n\tint sum = accumulate(all(cur), 0);\n\tif (sum > n) return false;\n\tsort(all(cur));\n\treverse(all(cur));\n\tvector nxt;\n\tfor (int i = 0; i < (int)cur.size(); i++)\n\t{\n\t\tfor (int j = 0; j < cur[i]; j++) nxt.pb(i + 1);\n\t}\n\treturn expand(nxt, curk + 1);\n}\n\nconst int maxcnt = 70;\n\nvector all;\nint answer = 0;\nint ans[maxn][maxn];\n//int ans2[maxcnt][maxn][maxn];\n\nvoid go(int cur, int mx)\n{\n\tfor (int v = 1; v <= mx; v++)\n\t{\n\t\tall.pb(v);\n\t\t//cout << \"try \" << v << endl;\n\t\tif (expand(all, 0))\n\t\t{\n\t\t\tanswer++;\n\t\t\tgo(cur + 1, v);\n\t\t} else\n\t\t{\n\t\t\tall.pop_back();\n\t\t\tbreak;\n\t\t}\n\t\tall.pop_back();\n\t}\n}\n\ninline void upd(int &a, int b)\n{\n\ta = (a + b) % MOD;\n}\n\nvoid solve1()\n{\n\tans[0][1] = 1;\n\tfor (int s = 0; s <= n; s++)\n\t{\n\t\tfor (int l = 1; l <= n; l++)\n\t\t{\n\t\t\tupd(ans[s][l + 1], ans[s][l]);\n\t\t\tif (s + l <= n) upd(ans[s + l][l], ans[s][l]);\n\t\t}\n\t}\n\tint curans = 0;\n\tfor (int i = 1; i <= n; i++) upd(curans, ans[i][n]);\n\tprintf(\"%d\\n\", curans);\n}\n\n//void solve2()\n//{\n\t//ans2[0][0][n] = 1;\n\t//for (int cnt = 0; cnt < maxcnt; cnt++)\n\t//{\n\t\t//for (int s = 0; s <= n; s++)\n\t\t//{\n\t\t\t//for (int l = n; l >= 1; l--)\n\t\t\t//{\n\t\t\t\t//upd(ans2[cnt][s][l - 1], ans2[cnt][s][l]);\n\t\t\t\t//if (s + (cnt + 1) * l <= n) upd(ans2[cnt + 1][s + (cnt + 1) * l][l], ans2[cnt][s][l]);\n\t\t\t//}\n\t\t//}\n\t//}\n\t//int curans = 0;\n\t//printf(\"const int answer2[2021] = {\");\n\t//for (int i = 0; i <= n; i++)\n\t//{\n\t\t//for (int cnt = 1; cnt < maxcnt; cnt++)\n\t\t//{\n\t\t\t//upd(curans, ans2[cnt][i][1]);\n\t\t//}\n\t\t//printf(\"%d, \", curans);\n\t//}\n\t//printf(\"};\\n\");\n//}\n\nint main()\n{\n\t//n = 2020;\n\t//k = 2;\n\t//solve2();\n\t//go(0, n + 1);\n\tscanf(\"%d%d\", &n, &k);\n\tif (k == 1)\n\t{\n\t\tsolve1();\n\t} else if (k == 2)\n\t{\n\t\tprintf(\"%d\\n\", answer2[n]);\n\t} else if (k == 3)\n\t{\n\t\tint curans = 0;\n\t\tfor (int i = 1; i <= n; i++) upd(curans, answer3[i]);\n\t\tprintf(\"%d\\n\", curans);\n\t} else\n\t{\n\t\tgo(0, n + 1);\n\t\tprintf(\"%d\\n\", answer);\n\t}\n\t//printf(\"const int answer2[2021] = {\");\n\t//for (int i = 0; i <= 2020; i++) printf(\"%d, \", answer3[i]);\n\t//printf(\"};\\n\");\n\t//cout << answer << endl;\n\t//for (int i = 1; i <= 10; i++) expand({i}, 0);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "c811ca18f458a201262c66cb8060bf36", "src_uid": "c435a0cd437081cd3593637be4568c6a", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\nusing namespace std;\nint gi(){\n\tint x=0,w=1;char ch=getchar();\n\twhile((ch<'0'||ch>'9')&&ch!='-')ch=getchar();\n\tif(ch=='-')w=0,ch=getchar();\n\twhile(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();\n\treturn w?x:-x;\n}\nconst int N=255;\nconst int mod=1e9+7;\nint n,m,a[N],len,coef[N],dp[N];long long k;\nvoid Inc(int &x,int y){x+=y;x>=mod?x-=mod:x;}\nstruct poly{\n\tint s[N];\n\tpoly(){memset(s,0,sizeof(s));}\n\tvoid print(){\n\t\tfor(int i=0;i=len;--i)\n\t\tif(tmp[i]){\n\t\t\tfor(int j=1;j<=len;++j)Inc(tmp[i-j],1ll*coef[j]*tmp[i]%mod);\n\t\t\ttmp[i]=0;\n\t\t}\n\tfor(int i=0;i>=1;}\n\treturn res;\n}\nint main(){\n\tscanf(\"%d%d%lld\",&n,&m,&k);\n\tfor(int i=1;i<=n;++i)scanf(\"%d\",&a[i]);\n\tfor(int i=1,x;i<=m;++i)scanf(\"%d\",&x),++coef[x],len=max(len,x);\n\tpoly S,T;len==1?T.s[0]=coef[1]:T.s[1]=1;\n\tfor(int i=1;i<=n;++i)S=S+fastpow(T,a[i]);\n\tS=fastpow(S,k);\n\tfor(int i=dp[0]=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#pragma comment(linker, \"/STACK:16777216\")\nusing namespace std;\n#define pb push_back\n#define ppb pop_back\n#define pi 3.1415926535897932384626433832795028841971\n#define mp make_pair\n#define x first\n#define y second\n#define pii pair\n#define pdd pair\n#define INF 1000000000\n#define FOR(i,a,b) for (int _n(b), i(a); i <= _n; i++)\n#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)\n#define all(c) (c).begin(), (c).end()\n#define SORT(c) sort(all(c))\n#define rep(i,n) FOR(i,1,(n))\n#define rept(i,n) FOR(i,0,(n)-1)\n#define L(s) (int)((s).size())\n#define C(a) memset((a),0,sizeof(a))\n#define VI vector \n#define ll long long\n\nstruct post {\n\tint pos[3];\n\tint st[3];\n\tint mv;\n};\ninline int code(const post& p) {\n\tint q = p.mv;\n\trept(i,3) q = q * 44 + p.pos[i];\n\trept(i,3) q = q * 5 + p.st[i];\n\treturn q;\n}\ninline post decode(int q) {\n\tpost p;\n\tFORD(i,2,0) {\n\t\tp.st[i] = q % 5;\n\t\tq /= 5;\n\t}\n\tFORD(i,2,0) {\n\t\tp.pos[i] = q % 44;\n\t\tq /= 44;\n\t}\n\tp.mv = q;\n\treturn p;\n}\nint a,b,c,d,i,j,n,m,k;\nint ans = 0;\nbool used[100000001];\nint fm[3],ft[3];\nqueue q;\n\ninline void add(const post &p) {\n\trept(i,3) {\n\t\tif (p.pos[i] < 0) return;\n\t\tif (p.pos[i] > ans) ans = p.pos[i];\n\t}\n\tint qq = code(p);\n\tif (used[qq]) return;\n\tused[qq] = 1;\n\tq.push(qq);\n}\nint main()\n{\n\t//freopen(\"input.txt\",\"r\",stdin);\n\t//freopen(\"output.txt\",\"w\",stdout);\n\t\n\tpost p;\n\tC(p.pos); C(p.st); p.mv = 0;\n\trept(i,3) {\n\t\tscanf(\"%d%d%d\",&p.pos[i], &fm[i], &ft[i]);\n\t\tp.pos[i]+=2;\n\t}\n\tadd(p);\n\twhile (!q.empty()) {\n\t\tpost cur = decode(q.front());\n\t\t/*if (cur.pos[0] == 6 + 1 && cur.pos[1] == 4 + 1 && cur.pos[2] == 5 + 1 && cur.mv==5 && cur.st[2] = 1 + 1) {\n\t\t\ta = a;\n\t\t}*/\n\t\tq.pop();\n\t\t// move\n\t\trept(i,3) {\n\t\t\tif (cur.mv & 1 << i) continue;\n\t\t\tif (cur.st[i] !=0 && cur.st[i] !=4) continue;\n\t\t\tbool ok = 1;\n\t\t\trept(j,3) {\n\t\t\t\tif (cur.st[j] == i + 1) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!ok) continue;\n\t\t\tFOR(dx, -fm[i], fm[i]) {\n\t\t\t\tif (!dx) continue;\n\t\t\t\tpost nx = cur;\n\t\t\t\tnx.pos[i] += dx;\n\t\t\t\tint o = 0;\n\t\t\t\trept(z,3) {\n\t\t\t\t\tif (nx.pos[z] == nx.pos[i]) ++o;\n\t\t\t\t}\n\t\t\t\tif (o > 1) continue;\n\t\t\t\tnx.mv |= 1 << i;\n\t\t\t\tadd(nx);\n\t\t\t}\n\t\t}\n\n\t\t// lift\n\t\trept(i,3) {\n\t\t\tif (cur.st[i] != 0) continue;\n\t\t\tbool ok = 1;\n\t\t\trept(j,3) {\n\t\t\t\tif (cur.st[j] == i + 1) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!ok) continue;\n\t\t\trept(j,3) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tif (cur.pos[j] != cur.pos[i] + 1 && cur.pos[j] != cur.pos[i] - 1) continue;\n\t\t\t\tbool ok = 1;\n\t\t\t\trept(z, 3) {\n\t\t\t\t\tif (cur.st[z] == j + 1) {\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!ok) continue;\n\t\t\t\tpost nx = cur;\n\t\t\t\tnx.st[i] = j + 1;\n\t\t\t\tnx.pos[j] = nx.pos[i];\n\t\t\t\tif (nx.st[j] != 0 && nx.st[j] != 4) {\n\t\t\t\t\trept(z,3) nx.pos[z] = cur.pos[i];\n\t\t\t\t}\n\t\t\t\tadd(nx);\n\t\t\t}\n\t\t}\n\n\t\t// throw\n\t\trept(i,3) {\n\t\t\tif (cur.st[i] == 0 || cur.st[i] == 4) continue;\n\t\t\tbool ok = 1;\n\t\t\trept(j,3) {\n\t\t\t\tif (cur.st[j] == i + 1) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!ok) continue;\n\n\t\t\tFOR(dx, -ft[i], ft[i]) {\n\t\t\t\tif (!dx) continue;\n\t\t\t\tbool ok = 1;\n\t\t\t\trept(z, 3) {\n\t\t\t\t\tif (cur.pos[z] == dx + cur.pos[i]) continue;\n\t\t\t\t}\n\t\t\t\tpost nx = cur;\n\t\t\t\tint pt = cur.st[i] - 1;\n\t\t\t\tnx.pos[pt] = cur.pos[i] + dx;\n\t\t\t\tif (cur.st[pt] > 0 && cur.st[pt] < 4) nx.pos[cur.st[pt] - 1] = cur.pos[i] + dx;\n\t\t\t\tnx.st[i] = 4;\n\t\t\t\tadd(nx);\n\t\t\t}\n\t\t}\n\t}\n\n\n\tprintf(\"%d\\n\", ans - 2);\n}\n", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "e382abf993ece9267ce51e939a76d965", "src_uid": "a14739b86d1fd62a030226263cdc1afc", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n#define mz 1000000007\n\nint l;\nstruct Mat\n{\n long long v[45][45]={};\n Mat friend operator *(Mat x1,Mat x2)\n {\n Mat x3;\n for(int i=0;i0;i--,j--)\n {\n if(s[i]!=s[j])\n return 0;\n }\n }\n return 1;\n}\nMat pows(Mat x,int y)\n{\n Mat ans;\n for(int i=0;i>=1;\n }\n return ans;\n}\n\nint main()\n{\n int n;\n long long ans;\n Mat a;\n scanf(\"%d\",&n);\n scanf(\"%s\",s+1);\n l=strlen(s+1);\n ans=1LL<=0;j--)\n {\n if(ok(j,i))\n {\n a.v[i][j]=1;\n break;\n }\n }\n }\n a=pows(a,n);\n for(int i=0;i\n#include\n#include\n#include\n#define rep(i,x,y) for(int i=(x);i<=(y);i++)\n#define dep(i,x,y) for(int i=(x);i>=(y);i--)\n#define gc getchar()\n#define pb push_back\n#define N 1000000\nusing namespace std;\nint n,m,q,nn,mm,al,u,R,x,y,ff[N][19];\nint p[N],ex[N],ey[N],Ex[N],Ey[N],f[N],F[N],ro[N];\nint TT[N],h[N],H[N],v[N],g[N],G[N],te[N],dfn[N],nd[N],uz[N];\nvectore[N],w[N];\nbool cmp(const int &x,const int &y){return dfn[x]=h[y])x=ff[x][i];\n\tdep(i,18,0)if(ff[x][i]!=ff[y][i])x=ff[x][i],y=ff[y][i];\n\tif(x!=y)x=f[x],y=f[y];return g[x];\n}\nvoid DFS(int x){\n\tint sz=w[x].size()-1;v[x]=1;\n\trep(i,0,sz){int tt=w[x][i];\n\t\tif(!v[tt]){F[tt]=x;H[tt]=H[x]+1;DFS(tt);}\n\t}\n}\nvoid read(int &x){char c=gc;x=0;\n\twhile(c<'0'||c>'9')c=gc;\n\tfor(;c>='0'&&c<='9';c=gc)x=x*10+c-'0';\n\tx=(x+R-1)%n+1;\n}\nint main(){\n\tscanf(\"%d%d%d\",&n,&m,&q);\n\trep(i,1,m){\n\t\tread(ex[i]);read(ey[i]);\n\t\te[ex[i]].pb(ey[i]);e[ey[i]].pb(ex[i]);\n\t}al=0;\n\trep(i,1,n)if(!v[i]){h[i]=1;ro[i]=i;f[i]=i;dfs(i);}\n\trep(i,1,n)v[i]=0,g[i]=i;\n\trep(i,1,m){\n\t\tif(f[ey[i]]==ex[i])swap(ex[i],ey[i]);\n\t\tif(v[ex[i]]||f[ex[i]]!=ey[i]){\n\t\t\tx=gf(ex[i]);y=gf(ey[i]);\n\t\t\twhile(x!=y){\n\t\t\t\tif(h[x]>h[y]){g[gf(x)]=gf(f[x]);x=gf(x);}\n\t\t\t\telse{g[gf(y)]=gf(f[y]);y=gf(y);}\n\t\t\t}\n\t\t}else v[ex[i]]=1;\n\t}\n\trep(i,1,n)gf(i);\n\trep(o,1,q){\n\t\tscanf(\"%d%d\",&nn,&mm);al=0;\n\t\trep(i,1,nn){read(p[i]);p[i]=g[p[i]];nd[++al]=p[i];}\n\t\trep(i,1,mm)uz[i]=0;\n\t\trep(i,1,mm){\n\t\t\tread(Ex[i]);read(Ey[i]);\n\t\t\tEx[i]=g[Ex[i]];Ey[i]=g[Ey[i]];\n\t\t\tnd[++al]=Ex[i];nd[++al]=Ey[i];\n\t\t}\n\t\tsort(nd+1,nd+al+1,cmp);\n\t\tal=unique(nd+1,nd+al+1)-(nd+1);u=al-1;\n\t\trep(i,1,u)nd[++al]=lca(nd[i],nd[i+1]);\n\t\tsort(nd+1,nd+al+1,cmp);\n\t\tal=unique(nd+1,nd+al+1)-(nd+1);\n\t\trep(i,1,al)TT[nd[i]]=i,te[i]=i;\n\t\trep(i,2,al)if(ro[nd[i]]==ro[nd[i-1]])te[Tr(i)]=Tr(i-1);\n\t\trep(i,1,al){F[i]=0;w[i].clear();}\n\t\trep(i,2,al){\n\t\t\tif(Tr(i)!=Tr(i-1))continue;F[i]=i-1;\n\t\t\twhile(lca(nd[i],nd[F[i]])!=nd[F[i]])F[i]=F[F[i]];\n\t\t\tw[i].pb(F[i]);w[F[i]].pb(i);\n\t\t}\n\t\trep(i,1,mm){\n\t\t\tx=TT[Ex[i]];y=TT[Ey[i]];\n\t\t\tif(Tr(x)!=Tr(y)){\n\t\t\t\tw[x].pb(y);w[y].pb(x);\n\t\t\t\tte[Tr(x)]=Tr(y);uz[i]=1;\n\t\t\t}\n\t\t}\n\t\trep(i,1,al)F[i]=0,v[i]=0,G[i]=i,H[i]=0;\n\t\trep(i,1,al)if(!v[i]){H[i]=1;DFS(i);}\n\t\trep(i,1,mm)if(!uz[i]){\n\t\t\tx=Gf(TT[Ex[i]]);y=Gf(TT[Ey[i]]);\n\t\t\twhile(x!=y){\n\t\t\t\tif(H[x]>H[y]){G[Gf(x)]=Gf(F[x]);x=Gf(x);}\n\t\t\t\telse{G[Gf(y)]=Gf(F[y]);y=Gf(y);}\n\t\t\t}\n\t\t}\n\t\tu=0;rep(i,2,nn)if(Gf(TT[p[i]])!=Gf(TT[p[1]])){u=1;break;}\n\t\tif(u)printf(\"NO\\n\");else{printf(\"YES\\n\");R=(R+o-1)%n+1;}\n\t}\n}", "lang_cluster": "C++", "tags": ["trees", "graphs"], "code_uid": "db1749b26363060d4e4178041cd41e3f", "src_uid": "53f64841886b32816eadce278e636bbc", "difficulty": 3300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define rep(i,a,b) for (int i=(a); i<=(b); i++)\n#define per(i,a,b) for (int i=(a); i>=(b); i--)\nusing namespace std;\ntypedef long long ll;\n\nconst int maxn = 300005;\nconst int mod = 1000000007;\nint f[30], n, m, k, len, A, B, AA, AB, BA;\nchar s[30][maxn], t[maxn];\nll K;\n\nint tmp[2][2], res[2][2], bin[2][2];\nvoid mul(int a[2][2], int b[2][2], int c[2][2]) {\n\trep (i, 0, 1) rep (j, 0, 1) {\n\t\ttmp[i][j] = 0;\n\t\trep (k, 0, 1) tmp[i][j] = (tmp[i][j] + 1ll * a[i][k] * b[k][j]) % mod;\n\t}\n\trep (i, 0, 1) rep (j, 0, 1) c[i][j] = tmp[i][j];\n}\n\nvoid power(int a[2][2], ll p) {\n\trep (i, 0, 1) rep (j, 0, 1)\n\t\tres[i][j] = (i == j), bin[i][j] = a[i][j];\n\twhile (p) {\n\t\tif (p & 1) mul(res, bin, res);\n\t\tmul(bin, bin, bin); p >>= 1;\n\t}\n\trep (i, 0, 1) rep (j, 0, 1)\n\t\ta[i][j] = res[i][j];\n}\n\nint MA[2][2], MB[2][2];\nint getfib(ll k) {\n\tMB[0][0] = MB[1][0] = MB[0][1] = 1; MB[1][1] = 0;\n\tMA[0][0] = 1; MA[1][0] = 0;\n\tpower(MB, k - 1);\n\tmul(MB, MA, MA);\n\treturn MA[0][0];\n}\n\nint nxt[maxn];\nint calc(char s[maxn], int ls, char t[maxn], int lt) {\n\tint k, res = 0;\n\tnxt[1] = k = 1; res = 0;\n\t//printf(\"%s\\n\", t + 1);\n\trep (i, 2, ls) {\n\t\twhile (k >= 2 && s[i] != s[k]) k = nxt[k-1];\n\t\tif (s[i] == s[k]) k++;\n\t\tnxt[i] = k;\n\t\t//printf(\"%d\\n\", nxt[i]);\n\t}\n\tk = 1;\n\trep (i, 1, lt) {\n\t\twhile (k >= 2 && t[i] != s[k]) k = nxt[k-1];\n\t\tif (t[i] == s[k]) k++;\n\t\t//printf(\"%d %d\\n\", i, k);\n\t\tif (k == ls + 1) res++, k = nxt[k-1];\n\t}\n\treturn res;\n}\n\nint flag[maxn];\nchar revB[maxn], revt[maxn];\nint solve(char A[maxn], int lA, char B[maxn], int lB, char t[maxn], int lt) {\n\trep (i, 1, lt - 1) flag[i] = 0;\n\trep (i, 1, lB) revB[i] = B[lB+1-i];\n\trep (i, 1, lt) revt[i] = t[lt+1-i];\n\tint k; nxt[1] = k = 1;\n\trep (i, 2, lt) {\n\t\twhile (k >= 2 && t[i] != t[k]) k = nxt[k-1];\n\t\tif (t[i] == t[k]) k++;\n\t\tnxt[i] = k;\n\t}\n\tk = 1;\n\trep (i, 1, lA) {\n\t\twhile (k >= 2 && A[i] != t[k]) k = nxt[k-1];\n\t\tif (A[i] == t[k]) k++;\n\t\tif (k == lt + 1) k = nxt[k-1];\n\t}\n\twhile (k >= 2) {\n\t\tflag[lt-(k-1)]++;\n\t\tk = nxt[k-1];\n\t}\n\tnxt[1] = k = 1;\n\trep (i, 2, lt) {\n\t\twhile (k >= 2 && revt[i] != revt[k]) k = nxt[k-1];\n\t\tif (revt[i] == revt[k]) k++;\n\t\tnxt[i] = k;\n\t}\n\tk = 1;\n\trep (i, 1, lB) {\n\t\twhile (k >= 2 && revB[i] != revt[k]) k = nxt[k-1];\n\t\tif (revB[i] == revt[k]) k++;\n\t\tif (k == lt + 1) k = nxt[k-1];\n\t}\n\twhile (k >= 2) {\n\t\tflag[k-1]++;\n\t\tk = nxt[k-1];\n\t}\t\n\tint res = 0;\n\trep (i, 1, lt - 1)\n\t\tif (flag[i] == 2) res++;\n\treturn res;\n}\n/*\n10 1\nbb\n*/\nint main() {\n\tf[1] = f[m=2] = 1;\n\twhile (f[m-1] < 100000) {\n\t\tf[m+1] = f[m] + f[m-1];\n\t\tm++;\n\t}\n\ts[1][1] = 'a'; s[2][1] = 'b';\n\trep (i, 3, m) {\n\t\trep (j, 1, f[i-1])\n\t\t\ts[i][j] = s[i-1][j];\n\t\trep (j, 1, f[i-2])\n\t\t\ts[i][j+f[i-1]] = s[i-2][j];\n\t}\n\tcin >> K >> n;\n\twhile (n--) {\n\t\tscanf(\"%s\", t + 1);\n\t\tlen = strlen(t + 1); k = 1;\n\t\twhile (f[k] < len) k++;\n\t\tif (K < k) { puts(\"0\"); continue; }\n\t\tif (K == k) { printf(\"%d\\n\", calc(t, len, s[k], f[k])); continue; }\n\t\tif (K == k + 1) { printf(\"%d\\n\", calc(t, len, s[k+1], f[k+1])); continue; }\n\t\tA = calc(t, len, s[k+1], f[k+1]);\n\t\tB = calc(t, len, s[k], f[k]);\n\t\tAA = solve(s[k+1], f[k+1], s[k+1], f[k+1], t, len);\n\t\tAB = solve(s[k+1], f[k+1], s[k], f[k], t, len);\n\t\tBA = solve(s[k], f[k], s[k+1], f[k+1], t, len);\n\t\tint f0 = getfib(K - k), f1 = getfib(K - k - 1);\t\t//f0 A f1 B\n\t\t//printf(\"%d %d %d %d %d %d %d\\n\", A, B, AA, AB, BA, f0, f1);\n\t\tint ans = (1ll * f0 * A + 1ll * f1 * B) % mod;\n\t\tif ((K - k) & 1) {\n\t\t\tans = (ans + 1ll * f1 * AB) % mod;\n\t\t\tans = (ans + 1ll * f1 * BA) % mod;\n\t\t\tans = (ans + 1ll * (f0 - f1 + mod - 1) * AA) % mod;\n\t\t}\n\t\telse {\n\t\t\tans = (ans + 1ll * f1 * AB) % mod;\n\t\t\tans = (ans + 1ll * (f1 + mod - 1) * BA) % mod;\n\t\t\tans = (ans + 1ll * (f0 - f1 + mod) * AA) % mod;\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "strings"], "code_uid": "87244f70f6afb6d20bdc262376c6cf98", "src_uid": "8983915e904ba763d893d56e94d9f7f0", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define int long long\nusing namespace std;\nconst int N=2e5+5;\n\nint read()\n{\n\tint s=0;\n\tchar c=getchar(),lc='+';\n\twhile (c<'0'||'9'\n\n\nusing namespace std;\ntemplateinline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }\ntemplateinline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }\n#define sz(c) (int)(c).size()\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define left left228\n#define right right228\n#define rank rank228\n#define y1 y1228\n#define read(FILENAME) freopen((FILENAME + \".in\").c_str(), \"r\", stdin)\n#define write(FILENAME) freopen((FILENAME + \".out\").c_str(), \"w\", stdout)\n#define files(FILENAME) read(FILENAME), write(FILENAME)\n#define pb push_back\n#define mp make_pair\nusing ll = long long;\nusing ld = long double;\n// const int MAXMEM = 200 * 1000 * 1024;\n// char _memory[MAXMEM];\n// size_t _ptr = 0;\n// void* operator new(size_t _x) { _ptr += _x; return _memory + _ptr - _x; }\n// void operator delete (void*) noexcept {}\nconst string FILENAME = \"input\";\nconst int MAXN = 200228;\nconst int Mod = 1000000009;\nconst int P = 1000003;\nconst int Mod1 = 1000000007;\nconst int P1 = 424243;\n\n\nint n;\nstring t;\nint f[MAXN];\nint nxtz[MAXN];\nint nxto[MAXN];\nll p[MAXN];\nll p1[MAXN];\nvector fg;\nvector start;\nvector > st;\nint ft[MAXN];\n\n\nint get(int l, int r, int l1, int r1) {\n\treturn min(r, r1) - max(l, l1) + 1;\n}\n\n\nll hs[MAXN];\nll hs1[MAXN];\n\n\nint gethashik(int l, int r) {\n\treturn ((hs[r] - (l ? (ll)hs[l - 1] * p[r - l + 1] % Mod: 0)) % Mod + Mod) % Mod;\n}\n\nint gethashik1(int l, int r) {\n\treturn ((hs1[r] - (l ? (ll)hs1[l - 1] * p1[r - l + 1] % Mod1: 0)) % Mod1 + Mod1) % Mod1;\n}\n\n\nint gethash(int l, int r) {\n\tint ls = upper_bound(all(start), l) - start.begin() - 1;\n\tint rs = upper_bound(all(start), r) - start.begin() - 1;\n\tif (ls == rs) {\n\t\tif (t[l] == '1') {\n\t\t\treturn (r - l + 1) % 2 == 0 ? 0: 1;\n\t\t}\n\t\treturn 0;\n\t}\n\tint llen = get(l, r, start[ls], start[ls] + st[ls].second - 1);\n\tint rlen = get(l, r, start[rs], start[rs] + st[rs].second - 1);\n\tint sl = lower_bound(all(fg), ls + 1) - fg.begin();\n\tint sr = lower_bound(all(fg), rs) - fg.begin() - 1;\n\tvector > s;\n\tint last = l - 1;\n\tif (t[l] == '1' && llen % 2 == 1) {\n\t\ts.pb({1, 1});\n\t\tlast = l + llen - 1;\n\t}\n\tif (sl >= 0 && sl < sz(fg) && sr >= 0 && sr < sz(fg) && ls < fg[sl] && sl <= sr && fg[sr] < rs) {\n\t//\tcout << ft[fg[sl]] - (l ? ft[l - 1]: 0) << endl;\n\t\t//cout << fg[sl] << endl;\n\t\ts.pb({ft[start[fg[sl]]] - (l ? ft[l - 1]: 0) + 1, 1});\n\t\tif (sl < sr) {\n\t\t\ts.pb({gethashik(sl + 1, sr), sr - sl});\n\t\t}\n\t\tlast = start[fg[sr]] + st[fg[sr]].second - 1;\n\t}\n\tif (t[r] == '1' && rlen % 2 == 1) {\n\t\t//cout << ft[r] - (last >= 0 ? ft[last]: 0) << endl;\n\t\ts.pb({ft[r] - (last >= 0 ? ft[last]: 0) + 1, 1});\n\t}\n\tll cur = 0;\n\tfor (auto x: s) {\n\t\tcur = (cur * p[x.second] + x.first) % Mod;\n\t}\n\treturn cur;\n}\n\nint gethash1(int l, int r) {\n\tint ls = upper_bound(all(start), l) - start.begin() - 1;\n\tint rs = upper_bound(all(start), r) - start.begin() - 1;\n\tif (ls == rs) {\n\t\tif (t[l] == '1') {\n\t\t\treturn (r - l + 1) % 2 == 0 ? 0: 1;\n\t\t}\n\t\treturn 0;\n\t}\n\tint llen = get(l, r, start[ls], start[ls] + st[ls].second - 1);\n\tint rlen = get(l, r, start[rs], start[rs] + st[rs].second - 1);\n\tint sl = lower_bound(all(fg), ls + 1) - fg.begin();\n\tint sr = lower_bound(all(fg), rs) - fg.begin() - 1;\n\tvector > s;\n\tint last = l - 1;\n\tif (t[l] == '1' && llen % 2 == 1) {\n\t\ts.pb({1, 1});\n\t\tlast = l + llen - 1;\n\t}\n\tif (sl >= 0 && sl < sz(fg) && sr >= 0 && sr < sz(fg) && ls < fg[sl] && sl <= sr && fg[sr] < rs) {\n\t//\tcout << ft[fg[sl]] - (l ? ft[l - 1]: 0) << endl;\n\t\t//cout << fg[sl] << endl;\n\t\ts.pb({ft[start[fg[sl]]] - (l ? ft[l - 1]: 0) + 1, 1});\n\t\tif (sl < sr) {\n\t\t\ts.pb({gethashik1(sl + 1, sr), sr - sl});\n\t\t}\n\t\tlast = start[fg[sr]] + st[fg[sr]].second - 1;\n\t}\n\tif (t[r] == '1' && rlen % 2 == 1) {\n\t\t//cout << ft[r] - (last >= 0 ? ft[last]: 0) << endl;\n\t\ts.pb({ft[r] - (last >= 0 ? ft[last]: 0) + 1, 1});\n\t}\n\tll cur = 0;\n\tfor (auto x: s) {\n\t\tcur = (cur * p1[x.second] + x.first) % Mod1;\n\t}\n\treturn cur;\n}\n\n\nstring getstring(int l, int r) {\n\tstring ts;\n\tfor (int i = l; i <= r; i++) {\n\t\tts += t[i];\n\t}\n\twhile (true) {\n\t\tbool was = false;\n\t\tfor (int j = 0; j <= sz(ts) - 3; j++) {\n\t\t\tif (ts[j] == '0' && ts[j + 1] == '1' && ts[j + 2] == '1') {\n\t\t\t\tts[j] = '1';\n\t\t\t\tts[j + 2] = '0';\n\t\t\t\twas = true;\n\t\t\t}\n\t\t}\n\t\tif (!was) {\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn ts;\n}\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout.tie(0);\n//\tread(FILENAME);\n\tcin >> n >> t;\n\tp[0] = 1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tp[i] = ((ll)p[i - 1] * P) % Mod;\n\t}\n\tp1[0] = 1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tp1[i] = ((ll)p1[i - 1] * P1) % Mod1;\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tint c = t[i] - '0';\n\t\tf[i] = c;\n\t\tft[i] =1 - c;\n\t\tif (i) {\n\t\t\tf[i] += f[i - 1];\n\t\t\tft[i] += ft[i - 1];\n\t\t}\n\t\tif (st.empty() || st.back().first != c) {\n\t\t\tst.pb({c, 1});\n\t\t} else {\n\t\t\tst.back().second++;\n\t\t}\n\t}\n\tint cur = 0;\n\tfor (int i = 0; i < sz(st); i++) {\n\t\tstart.pb(cur);\n\t\tif (st[i].first == 1 && st[i].second % 2 == 1) {\n\t\t\tfg.pb(i);\n\t\t}\n\t\tcur += st[i].second;\t\n\t}\n\tfor (int i = 0; i < sz(fg); i++) {\n\t\tint kek = ft[start[fg[i]]] - (i ? ft[start[fg[i - 1]] + st[fg[i - 1]].second - 1]: 0) + 1;\n\t\ths[i] = (i ? hs[i - 1] * P + kek: kek) % Mod; \n\t}\n\tfor (int i = 0; i < sz(fg); i++) {\n\t\tint kek = ft[start[fg[i]]] - (i ? ft[start[fg[i - 1]] + st[fg[i - 1]].second - 1]: 0) + 1;\n\t\ths1[i] = (i ? hs1[i - 1] * P1 + kek: kek) % Mod1; \n\t}\n\t//cout << t << endl;\n\tint q;\n\tcin >> q;\n\tfor (int i = 0; i < q; i++) {\n\t\tint l1, l2, len;\n\t\tcin >> l1 >> l2 >> len;\n\t\tl1--, l2--;\n\t\tif (f[l1 + len - 1] - (l1 ? f[l1 - 1]: 0) != f[l2 + len - 1] - (l2 ? f[l2 - 1]: 0)) {\n\t\t \tcout << \"No\\n\";\n\t \t\tcontinue;\n\t\t}\n\t\tbool ok = gethash(l1, l1 + len - 1) != gethash(l2, l2 + len - 1) || gethash1(l1, l1 + len - 1) != gethash1(l2, l2 + len - 1);\n\t\tif (ok) {\n\t\t\tcout << \"No\\n\";\n\t\t} else {\n\t\t\tcout << \"Yes\\n\";\n\t\t}\n\t}\n}", "lang_cluster": "C++", "tags": ["strings", "data structures"], "code_uid": "a8ec97cd8778e4a0b6f367f52e35898d", "src_uid": "6bd41042c6a442765cd93c73d55f6189", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nconst int P=998244353;\ninline void add(int &a,const int &b){a+=b;(a>=P)?a-=P:a;}\nint f[30][33][33][33][33][2][2],n,m,x[6],y[6],sum1,sum2,sum3,sum4;\nint main(){\n\tscanf(\"%d%d\",&n,&m);\n\tfor(int i=1;i<=n;i++){\n\t\tscanf(\"%d%d\",x+i,y+i);\n\t\tif(x[i]<0)sum1-=x[i];\n\t\telse sum2+=x[i];\n\t\tif(y[i]<0)sum3-=y[i];\n\t\telse sum4+=y[i];\n\t}\n\tsum1<<=1;\n\tsum2<<=1;\n\tsum3<<=1;\n\tsum4<<=1;\n\tif(!sum1||!sum2||!sum3||!sum4){\n\t\tputs(\"0\");\n\t\treturn 0;\n\t}\n\tf[0][0][0][0][0][0][0]=1;\n\tint ans=P-1;\n\tfor(int i=0;i<30;i++){\n//\t\tprintf(\"%d\\n\",i);\n\t\tfor(int j=1;j<=n;j++)\n\t\t\tfor(int i1=sum1;~i1;i1--)\n\t\t\t\tfor(int i2=sum2;~i2;i2--)\n\t\t\t\t\tfor(int i3=sum3;~i3;i3--)\n\t\t\t\t\t\tfor(int i4=sum4;~i4;i4--)\n\t\t\t\t\t\t\tfor(int k=0;k<2;k++)\n\t\t\t\t\t\t\t\tfor(int l=0;l<2;l++){\n\t\t\t\t\t\t\t\t\tint n1=i1,n2=i2,n3=i3,n4=i4;\n\t\t\t\t\t\t\t\t\tif(x[j]<0)n1-=x[j];\n\t\t\t\t\t\t\t\t\telse n2+=x[j];\n\t\t\t\t\t\t\t\t\tif(y[j]<0)n3-=y[j];\n\t\t\t\t\t\t\t\t\telse n4+=y[j];\n\t\t\t\t\t\t\t\t\tadd(f[i][n1][n2][n3][n4][k][l],f[i][i1][i2][i3][i4][k][l]);\n\t\t\t\t\t\t\t\t}\n\t\tbool This=((m&(1<>1)==0&&!nk&&!nl)add(ans,f[i][i1][i2][i3][i4][k][l]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\telse add(f[i+1][i1>>1][i2>>1][i3>>1][i4>>1][nk][nl],f[i][i1][i2][i3][i4][k][l]);\n\t\t\t\t\t\t\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n} ", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "5c705fa98080f96282a9f5fe9d852a4f", "src_uid": "fba104bc43111a6f2692216a4edea2bc", "difficulty": 3500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\t\n\nint main()\n{\n\n\tint n;\t\n\tcin >> n;\n\tint arr[n];\n\tfor(int i=0;i> arr[i];\n\tint t;\n\tcin >> t;\n\t\n\tsort(arr,arr+n);\n\t\n\n\tint count;\n\tint max=0,j;\n\tfor(int i=0;i=0&&j\n#include \n#include \n#include \n\nint span(std::vector shts, int start, int time){\n int i=1;\n for(;start+itime)\n return i;\n return i;\n}\n\n\nint main(){\n std::vector shts;\n int qtty;\n int ts;\n scanf(\"%d\",&qtty);\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define ull unsigned ll\n#define ll long long\n#define ld long double\n#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)\n#define FORZ(i, a, b) for (int i=(int)(a); i>(int)(b); --i)\n#define mp make_pair\n#define fs first\n#define sc second\n#define pr pair\n#define pb push_back\n#define ppb pop_back\n#define bg begin()\n#define ed end()\n#define sz size()\n#define rsz resize\n#define all(a) (a).bg, (a).ed\n#define len length()\n#define ext return 0;\nll gcd(ll a, ll b) { if (a == 0 || b == 0) return (a + b); if (a > b) return (gcd(a%b, b)); return (gcd(b, b % a)); }\n\n#define Nmax 1000009\n#define INF 1000000007\n#define y1 yy1\n#define eps 1e-5\nvoid out() { std::cout << \"NO\\n\"; exit(0); }\nusing namespace std;\n\nint n, T, a[101];\nbool used[101];\n\nint main(){\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n\n\tcin >> n;\n\tFOR(i, 0, n) cin >> a[i];\n\tcin >> T;\n\n\tint ans = 0;\n\tsort(a, a + n);\n\tFOR(i, 0, n) {\n\t\tint p = i+1;\n\t\twhile (p < n && abs(a[i] - a[p]) <= T) p++;\n\t\tans = max(ans, p - i);\n\t}\n\tcout << ans << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "200739fce0bd3b638116b90a1dfcdec7", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define INF 999999999\n#define ms(a,x) memset(a,x,sizeof(a))\n\nusing namespace std;\n\nbool cmd(int a, int b)\n{\n\treturn a > b;\n}\n\nint main()\n{\n\tint n, a[110] = { 0 };\n\tcin >> n;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin >> a[i];\n\t}\n\tint t;\n\tcin >> t;\n\tsort(a, a + n);\n\tint num = 0;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tint count = 0, last = a[i] + t;\n\t\tfor (int j = i; j < n; j++)\n\t\t{\n\t\t\tif (a[j] >= a[i] && a[j] <= last)\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\t\tif (count > num)\n\t\t{\n\t\t\tnum = count;\n\t\t}\n\t}\n\t\n\tcout << num;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "5bc1a6b02d91032856532bb9d7431fe3", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*\nwelcome to my code hi from s_agnik1511 \nDon't just copy it.\nAnalyze it.\ncoz that's what heroes do.....\n*/\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define dude ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n#define mesc(number,character) char f[number];memset(f,character,number);\n#define mesi(number,value) int f[number];memset(f,value,number);\n#define specio ifstream cin(\"input.txt\");ofstream cout(\"output.txt\");\n#define fr(i,start,end) for(i=start;i<=end;i++)\n#define rf(i,start,end) for(i=start;i>=end;i--)\n#define tst int t;cin>>t;while(t--)\n#define ai ll n,i,j;cin>>n;ll a[n];\n#define pi 3.14159265358979323846\n#define ll long long int\n#define acc accumulate\n#define ld long double\n#define pb push_back\n#define r0 return 0;\n#define pp pop_back\n#define vec vector\n#define sec second\n#define S string\n#define D double\n#define len length\n#define br break;\n#define wh while\n#define fs first\n#define F float\n#define gcd __gcd\n#define sz size\n#define endl \"\\n\"\n#define sp \" \"\nll ciel(ll x,ll y) {\n if(x%y) {\n return x/y+1;\n }\n return x/y;\n}\nvoid what_should_be_the_logic() {\nai\nll k;ll ma=0,cnt=1;\nfr(i,0,n-1) {\n cin>>a[i];\n}cin>>k;\nsort(a,a+n);\nfr(j,0,n-2) {\nfr(i,j+1,n-1) {\n if(a[i]-a[j]<=k) {\n cnt++;\n }\n else {\n ma=max(cnt,ma);cnt=1;\n }\n}\nma=max(ma,cnt);cnt=1;\n}\ncout<\n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint arr[1001];\nint n;\nint ans;\nint itv;\n\nint Get(int pos)\n{\n\tint ret = 0;\n\tfor(int i = pos; i < n; ++ i)\n\t{\n\t\tif(arr[i] <= itv + arr[pos])\n\t\t\tret ++;\n\t}\n\treturn ret;\n}\n\nint main()\n{\n#if _MSC_VER == 1500\n\tfreopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tscanf(\"%d\", &n);\n\tfor(int i = 0; i < n; ++ i)\n\t\tscanf(\"%d\", arr + i);\n\tsort(arr, arr + n);\n\tscanf(\"%d\", &itv);\n\tans = 0;\n\tfor(int i = 0; i < n; ++ i)\n\t\tans = max(ans, Get(i));\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "0927e29eb3252732264ad132e6384ee6", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nvoid swap(int &a, int &b){\n int tmp=a;\n a=b;\n b=tmp;\n}\n\nint main(){\n int n;\n scanf(\"%d\",&n);\n int t;\n int a[10000];\n for(int i=0;i<10000;i++)a[i]=0;\n for(int i=0;ians)ans=cur;\n i++;\n }\n printf(\"%d\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "b14297d6373e047a67083cf8b423765c", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\n#define ll long long\n#define pi pair\nll arr[500005];\nmapmp;\n\nll cnt[10005];\n\nll cumsum[10000];\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ll i,j,n,m,t,a,b,x,y;\n\ncin>>n;\n\nfor(i=0;i>arr[i];\ncnt[arr[i]]++;\n}\ncin>>t;\nfor(i=1;i<=1000;i++){\n cumsum[i]=cumsum[i-1]+cnt[i];\n}\n\nll ans=0;\nfor(i=t;i<=1000;i++){\n ans=max(ans,cumsum[i]-cumsum[i-t-1]);\n}\ncout<\n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main(){\n int n;\n cin>>n;\n vector a(n);\n for (auto &i:a){\n cin>>i;\n }\n sort (a.begin(),a.end());\n int t,ans=-1,up;\n cin>>t;\n for (int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#define enter printf(\"\\n\")\n#define pb push_back\n#define ll long long\n#define fors(it, r) for (set::iterator it = r.begin(); it != r.end(); it++)\n#define forvit(it, r) for (vector::iterator it = r.begin(); it != r.end(); it++)\n#define forv(i, vector) for (int i = 0; i < vector.size(); i++)\n#define forn(i, n) for (int i = 0; i < n; i++)\n#define forn1(i, n) for (int i = 1; i < n; i++)\n#define fori(n) for (int i = 0; i < n; i++)\n#define forj(n) for (int j = 0; j < n; j++)\n#define vi vector\n#define vll vector\n#define pii pair\n\nusing namespace std;\nint INF = 1000000007;\nstring FILENAME = \"input\";\nstring FILEINPUT = FILENAME;\n\nvoid writeln(int a){printf(\"%d\\n\", a);}void writeln(int a, int b){printf(\"%d %d\\n\", a, b);}void writeln(int a, int b, int c){printf(\"%d %d %d\\n\", a, b, c);}void writeln(int a, int b, int c, int d){printf(\"%d %d %d %d\\n\", a, b, c, d);}void write(int a){printf(\"%d\", a);}void write(int a, int b){printf(\"%d %d\", a, b);}void write(int a, int b, int c){printf(\"%d %d %d\", a, b, c);}void write(int a, int b, int c, int d){printf(\"%d %d %d %d\", a, b, c, d);}void read(int &a){scanf(\"%d\", &a);}void read(int &a, int &b){scanf(\"%d %d\", &a, &b);}void read(int &a, int &b, int &c){scanf(\"%d %d %d\", &a, &b, &c);}void read(int &a, int &b, int &c, int &d){scanf(\"%d %d %d %d\", &a, &b, &c, &d);}void readln(int &a){scanf(\"%d\\n\", &a);}void readln(int &a, int &b){scanf(\"%d %d\\n\", &a, &b);}void readln(int &a, int &b, int &c){scanf(\"%d %d %d\\n\", &a, &b, &c);}void readln(int &a, int &b, int &c, int &d){scanf(\"%d %d %d %d\\n\", &a, &b, &c, &d);}\nvoid readln(vector &f, int n){int x;for (int i = 1; i <= n; i++){read(x);f.push_back(x);}}void writeln(vector &f){for (int i = 0; i < f.size(); i++)printf(\"%d%c\", f[i], i == f.size() - 1 ? '\\n' : ' ');}\n\nvoid run()\n{\n int n;\n vi a;\n read(n);\n readln(a, n);\n sort(a.begin(), a.end());\n int i = -1, j = 0, t, mx = 0;\n read(t);\n while (j != n)\n {\n i++;\n while (j < n && a[j] - a[i] <= t)\n j++;\n mx = max(mx, j - i);\n }\n writeln(mx);\n}\n\nint main()\n{\n //freopen(FILEINPUT.append(\".in\").c_str(), \"r\", stdin);\n //freopen(FILENAME.append(\".out\").c_str(), \"w\", stdout);\n run();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "e2ae76b117b10a741ca5b791524c1dcf", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \ntypedef long long ll;\n#define MAX 10005\nusing namespace std;\n#define INF 2147483647\n#define infL (1LL<<60)\n#define inf (1e8)\n#define MOD 1000000007\n#define EPS 1e-9\n#define Gr 9.8\n#define PI acos(-1)\n#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)\n#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)\n#define lch (rt<<1)\n#define rch (rt<<1|1)\nbool debug = 1;\nint n,m, k;\nint dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};\nll ln, lk, lm;\n\nint t[MAX];\nint main(){\n scanf(\"%d\", &n);\n REQ(i,n) {\n scanf(\"%d\", &m); t[m]++;\n }\n scanf(\"%d\", &k);\n REQ(i,3000) t[i]+=t[i-1];\n int ans = 0;\n for(int i=1;i+k<=5000;i++) {\n ans = max(ans, t[i+k]-t[i-1]);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "8e71fa540ff5a76a3859553bd82ee767", "src_uid": "086d07bd6f9031df09bd6a6e8fe8f25c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n\n#define LET(x, a) __typeof(a) x(a)\n#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)\n#define si(x) scanf(\"%d\",&x)\n#define F first\n#define S second\n#define PB push_back\n#define MP make_pair\n#define INF 1000000000\n#define MOD 1000000007\n#define SET(x,y) memset(x,y,sizeof(x));\n#define LL long long int\n#define ULL unsigned LL\n#define PII pair\n\nclass Matrix {\n public:\n int m[31][31];\n int N;\n Matrix(int N) {\n this->N = N;\n memset(m, 0, sizeof(m));\n for (int i = 0; i < N; i++)\n m[i][i] = 1;\n }\n Matrix* operator *(Matrix b);\n} *M[71][31];\nMatrix* Matrix::operator *(Matrix b) {\n int i, j, k;\n Matrix* ret = new Matrix(N);\n for (i = 0; i < N; i++) {\n for (j = 0; j < N; j++) {\n LL cans = 0;\n for (k = 0; k < N; k++) {\n cans += m[i][k] * ((LL)b.m[k][j]);\n cans %= MOD;\n }\n ret->m[i][j] = cans;\n }\n }\n return ret;\n}\n\nLL kp[100];\nint main() {\n int i, j, k, l;\n LL n;\n cin >> n >> k;\n for (i = 0; i < k; i++) {\n M[0][i] = new Matrix(k + 1);\n for (j = 0; j <= k; j++) {\n M[0][i]->m[i][j] = 1;\n }\n }\n kp[0] = 1;\n kp[1] = k;\n int lg = 2;\n while (kp[lg - 1] <= n / k) {\n kp[lg] = kp[lg - 1] * k;\n lg++;\n }\n for (i = 1; i < lg; i++) {\n M[i][0] = new Matrix(k + 1);\n for (j = 1; j < k; j++) {\n M[i][j] = *M[i - 1][j - 1] * *M[i][j - 1];\n }\n Matrix *cpro = new Matrix(k + 1);\n for (j = k - 1; j >= 0; j--) {\n cpro = *cpro * *M[i - 1][j];\n M[i][j] = *M[i][j] * *cpro;\n }\n }\n Matrix *ans = new Matrix(k + 1);\n int cc = 0;\n for (i = lg - 1; i >= 0; i--) {\n while (kp[i] <= n) {\n n -= kp[i];\n ans = *M[i][cc] * *ans;\n cc++;\n cc %= k;\n }\n }\n LL ret = 0;\n for (i = 0; i <= k; i++) {\n ret += ans->m[i][k];\n ret %= MOD;\n }\n cout << ret << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "a855c3be0557ddd9f1939ca68dbb4251", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\n#define ll long long\n#define pb push_back\n\nconst int maxn = 32;\nconst int maxb = 60;\nconst int mod = 1e9 + 7;\nconst ll inf = 2e18;\n\ninline void mkay(int &a)\n{\n\tif(a >= mod)\n\t\ta -= mod;\n\telse if(a < 0)\n\t\ta += mod;\n}\n\nstruct matrix\n{\n\tint val[maxn][maxn];\n\tint n;\n\tmatrix(int nn=0, int x = 0)\n\t{\n\t\tn = nn;\n\t\tfor (int i=0; i>= 1;\n\t}\n\treturn ans;\n}\n\nint k;\n\nmatrix a[maxb] , shift[maxb];\n\nll val[maxb];\n\nmatrix get(ll n , int ind)\n{\n\tif(!n)\n\t\treturn matrix(k + 1 , 1);\n\n\tfor(int i = ind; i >= 0; i--)\n\t\tif(n >= val[i])\n\t\t{\n\t\t\tmatrix tmp = get(n % val[i] , i - 1);\n\t\t\tint x = n / val[i];\n\n\t\t\tmatrix ans = a[i];\n\t\t\tfor(int j = 1; j < x; j++)\n\t\t\t{\n\t\t\t\tans = ans * shift[k - 1];\n\t\t\t\tans = ans * a[i];\n\t\t\t}\n\t\t\tans = ans * shift[k - 1];\n\t\t\tans = ans * tmp;\n\t\t\tans = ans * shift[x - 1];\n\t\t\treturn ans;\n\t\t}\n\n\tcout << 1/0 << endl;\n\treturn matrix(k + 1 , -800);\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout.tie(0);\n\n\tll n;\n\tcin >> n >> k;\n\t\n\tval[0] = 1;\n\t\n\tint last = maxb;\n\tfor(int i = 1; i < maxb; i++)\n\t{\n\t\tif(val[i - 1] >= inf / k)\n\t\t{\n\t\t val[i] = inf;\n\t\t last = i;\n\t\t break;\n\t\t}\n\t\telse\n\t\t\tval[i] = val[i - 1] * k;\n\t}\n\n\tshift[0] = matrix(k + 1 , 1);\n\tshift[1] = matrix(k + 1);\n\n\tfor(int i = 0; i < k; i++)\n\t\tshift[1][i][(i - 1 + k) % k] = 1;\n\tshift[1][k][k] = 1;\n\n\tfor(int i = 2; i < k; i++)\n\t\tshift[i] = shift[i - 1] * shift[1];\n\n\tfor(int i = 0; i < maxb; i++)\n\t\ta[i] = matrix(k + 1 , 1);\n\n\tfor(int i = 0; i <= k; i++)\n\t\ta[0][i][0] = 1;\n\n\tfor(int i = 1; i < last; i++)\n\t{\n\t\tmatrix tmp = a[i - 1];\n\n\t\ta[i] = a[i - 1];\n\n\t\tfor(int j = 1; j < k; j++)\n\t\t{\n\t\t\ta[i] = a[i] * shift[k - 1];\n\t\t\ta[i] = a[i] * tmp;\n\t\t}\n\n\t\ta[i] = a[i] * shift[k - 1];\n\t}\n\n\tmatrix ans(k + 1);\n\tans[0][k] = 1;\n\n\tans = ans * get(n , last - 1);\n\n\tint res = 0;\n\tfor(int i = 0; i <= k; i++)\n\t\tmkay(res += ans[0][i]);\n\n\tcout << res << endl;\n}\n\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "977fc097c70aa7c140a76e3e5095f946", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\ntemplate\nvoid ckmin(T &a, U b)\n{\n\tif (a > b) a = b;\n}\ntemplate\nvoid ckmax(T &a, U b)\n{\n\tif (a < b) a = b;\n}\n\n#define MP make_pair\n#define PB push_back\n#define LB lower_bound\n#define UB upper_bound\n#define fi first\n#define se second\n#define FOR(i, a, b) for (auto i = (a); i < (b); i++)\n#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)\n#define SZ(x) ((int) ((x).size()))\n#define ALL(x) (x).begin(), (x).end()\n#define INF 1000000007\n#define LLINF 2696969696969696969ll\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vl;\ntypedef vector vpi;\ntypedef vector vpl;\ntypedef vector mat;\n\nll N, K;\nvi num;\nmat trans;\nmat base[60][30], pref[60][30], suf[60][30];\nll ans;\n\nmat mat_mul(mat a, mat b)\n{\n\tmat res(K + 1);\n\tFOR(i, 0, K + 1) res[i].resize(K + 1);\n\tFOR(i, 0, K + 1)\n\t{\n\t\tFOR(j, 0, K + 1)\n\t\t{\n\t\t\tres[i][j] = 0;\n\t\t\tFOR(k, 0, K + 1)\n\t\t\t{\n\t\t\t\tres[i][j] += a[i][k] * b[k][j];\n\t\t\t\tres[i][j] %= INF;\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\nmat shift(mat a, int x)\n{\n\tmat res(K + 1);\n\tFOR(i, 0, K + 1) res[i].resize(K + 1);\n\tFOR(i, 0, K + 1)\n\t{\n\t\tFOR(j, 0, K + 1)\n\t\t{\n\t\t\tif (a[i][j] == 0) continue;\n\t\t\tif (i == j) res[i][j] = a[i][j];\n\t\t\telse res[i][(j + x) % K] = a[i][j];\n\t\t}\n\t}\n\treturn res;\n}\n\nint32_t main()\n{\n\tios_base::sync_with_stdio(0); cin.tie(0);\n\tcin >> N >> K;\n\twhile(N)\n\t{\n\t\tnum.PB(N % K);\n\t\tN /= K;\n\t}\n\t// for (int x : num)\n\t// {\n\t// \tcerr << x << ' ';\n\t// }\n\t// cerr << endl;\n\tFOR(i, 0, K)\n\t{\n\t\tbase[0][i].resize(K + 1);\n\t\tFOR(j, 0, K + 1) base[0][i][j].resize(K + 1);\n\t\tFOR(j, 0, K + 1)\n\t\t{\n\t\t\tbase[0][i][j][j] = 1;\n\t\t}\n\t\tFOR(j, 0, K + 1)\n\t\t{\n\t\t\tbase[0][i][j][i] = 1;\n\t\t}\n\t}\n\tFOR(i, 1, SZ(num))\n\t{\n\t\tpref[i - 1][0] = base[i - 1][0];\n\t\tFOR(j, 1, K)\n\t\t{\n\t\t\tpref[i - 1][j] = mat_mul(pref[i - 1][j - 1], base[i - 1][j]);\n\t\t}\n\t\tsuf[i - 1][K - 1] = base[i - 1][K - 1];\n\t\tFORD(j, K - 1, 0)\n\t\t{\n\t\t\tsuf[i - 1][j] = mat_mul(base[i - 1][j], suf[i - 1][j + 1]);\n\t\t}\n\t\tbase[i][0] = pref[i - 1][K - 1];\n\t\tFOR(j, 1, K)\n\t\t{\n\t\t\tbase[i][j] = mat_mul(suf[i - 1][j], pref[i - 1][j - 1]);\n\t\t}\n\t}\n\ttrans.resize(K + 1);\n\tFOR(i, 0, K + 1) trans[i].resize(K + 1);\n\tFOR(i, 0, K + 1)\n\t{\n\t\tFOR(j, 0, K + 1)\n\t\t{\n\t\t\ttrans[i][j] = 0;\n\t\t}\n\t\ttrans[i][i] = 1;\n\t}\n\tint sum = 0;\n\tFORD(i, SZ(num), 0)\n\t{\n\t\tint x = num[i];\n\t\t//multiply by\n\t\tFOR(j, 0, x)\n\t\t{\n\t\t\ttrans = mat_mul(trans, base[i][sum]);\n\t\t\tsum++; if (sum >= K) sum -= K;\n\t\t}\n\t}\n\tans = 1;\n\tFOR(i, 0, K)\n\t{\n\t\tans += trans[K][i];\n\t\tans %= INF;\n\t}\n\tcout << ans << '\\n';\n\treturn 0;\n\t//base[i] tells you the transition matrix for 0....k^i - 1\n\t//trans has the transition matrix for 1!\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "98267537d93379248d2358c0c1cd371e", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define y0 y0z\n#define y1 y1z\n#define yn ynz\n#define j0 j0z\n#define j1 j1z\n#define jn jnz\n#define tm tmz\n#define buli(x) (__builtin_popcountll(x))\n#define bur0(x) (__builtin_ctzll(x))\n#define bul2(x) (63-__builtin_clzll(x))\n#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n#define fil(a,b) memset((a),(b),sizeof(a))\n#define cl(a) fil(a,0)\n#define siz(x) ((int)(x).size())\n#define all(x) (x).begin(),(x).end()\n#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)\n#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)\n#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)\n#define pw(x) ((ll(1))<<(x))\n#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))\n#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))\nvoid getre(){int x=0;printf(\"%d\\n\",1/x);}\nvoid gettle(){int res=1;while(1)res<<=1;printf(\"%d\\n\",res);}\ntypedef pair pii;\ntypedef vector vi;\ntypedef vector vpii;\ntemplateinline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}\ntemplateinline bool upmax(T&a,const S&b){return ainline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}\ntemplateinline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}\ntemplateN gcd(N a,N b){return b?gcd(b,a%b):a;}\ntemplateinline int sgn(N a){return a>0?1:(a<0?-1:0);}\n#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)\n #define lld \"%I64d\"\n#else\n #define lld \"%lld\"\n#endif\ninline void gn(long long&x){\n\tint sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');\n\twhile((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;\n}\ninline void gn(int&x){long long t;gn(t);x=t;}\ninline void gn(unsigned long long&x){long long t;gn(t);x=t;}\ninline void gn(double&x){double t;scanf(\"%lf\",&t);x=t;}\ninline void gn(long double&x){double t;scanf(\"%lf\",&t);x=t;}\ninline void gs(char *s){scanf(\"%s\",s);}\ninline void gc(char &c){while((c=getchar())>126 || c<33);}\ninline void pc(char c){putchar(c);}\n#ifdef JCVB\n#define debug(...) fprintf(stderr, __VA_ARGS__)\n#else\n#define debug(...) \n#endif\ntypedef long long ll;\ntypedef double db;\ninline ll sqr(ll a){return a*a;}\ninline db sqrf(db a){return a*a;}\nconst int inf=0x3f3f3f3f;\nconst db pi=3.14159265358979323846264338327950288L;\nconst db eps=1e-6;\nconst int mo=1e9+7;\nint qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}\n\nll n;\nint k;\n\n// mo < 2^30 = 1073741824\n// first call mm_init(sz);\n// indices from 0 to mm_n - 1\n// call a.plus(),b.plus() before a*b\n\nconst int MM_N =33;\n\nint mm_n;\nstruct mat{\n\tint a[MM_N][MM_N];\n\tvoid plus(){\n\t\trep(i,0,mm_n)rep(j,0,mm_n)a[i][j]=(a[i][j]%mo+mo)%mo;\n\t}\n}one;\nmat operator*(const mat&a,const mat&b){\n\tstatic ll ans[MM_N][MM_N];\n\tcl(ans);\n\tll infa=8e18;\n\trep(k,0,mm_n)rep(i,0,mm_n)rep(j,0,mm_n){\n\t\tif((ans[i][j]+=1ll*a.a[i][k]*b.a[k][j])>=infa)ans[i][j]%=mo;\n\t}\n\tmat c;\n\trep(i,0,mm_n)rep(j,0,mm_n)c.a[i][j]=ans[i][j]%mo;\n\treturn c;\n}\nmat qp(mat a,ll b){a.plus();mat n=one;do{if(b&1)n=n*a;a=a*a;}while(b>>=1);return n;}\nvoid mm_init(int sz){\n\tmm_n=sz;\n\trep(i,0,mm_n)one.a[i][i]=1;\n}\n\n\nint nu[111];int tot=0;\n\nmat a[100];\n\nmat jia(const mat&x,int d){\n\tmat ret;\n\trep(i,0,k+1)rep(j,0,k+1)ret.a[i!=k?(i+d)%k:k][j!=k?(j+d)%k:k]=x.a[i][j];\n\treturn ret;\n}\t\nint main()\n{\n#ifdef JCVB\n\t//freopen(\"1.in\",\"r\",stdin);\n\t//freopen(\"1.out\",\"w\",stdout);\n\tint _time_jc=clock();\n#endif\n\tgn(n);gn(k);\n\tmm_init(k+1);\n\n\twhile(n){\n\t\tnu[tot++]=n%k;\n\t\tn/=k;\n\t}\n\tfor (int j=0;j<=k;j++)a[0].a[0][j]=1;\n\tfor (int j=0;j<=k;j++)a[0].a[j][j]=1;\n\tfor (int i=1;i=0;i--){\n\t\tfor (int j=0;j\n#define MN 30\n#define mod 1000000007\n#define ll long long\nusing namespace std;\nll n;\nint m,f[65][35][35],s[65],num,g[35][35],A[35],B[35];\ninline int Dec(int x,int y){return x>n>>m;\n\tfor(ll t=n;t;t/=m) s[++num]=t%m;\n\tfor(int i=1;i<=m;++i) f[1][i][i]=1;\n\tfor(int i=0;i<=m;++i) f[1][0][i]=1;\n\tfor(int i=2;i<=num;++i)\n\t{\n\t\tfor(int j=0;j<=m;++j) f[i][j][j]=1;\n\t\tfor(int j=0;j\nusing namespace std;\n#define mod 1000000007\ntypedef unsigned long long int LL;\nstruct M{\n LL s[35][35],n;\n friend M operator * (M a,M b){\n M c;\n c.n=a.n;\n int i,j,k;\n for(i=0;i=0;j--){\n for(k=0;k<=m;k++){\n swap(bi[t][i].s[j][k],bi[t][i].s[j+1][k]);\n }\n }\n for(j=m-2;j>=0;j--){\n for(k=0;k<=m;k++){\n swap(bi[t][i].s[k][j],bi[t][i].s[k][j+1]);\n }\n }\n }\n\n }\n for(i=0;i<=m;i++){\n I.n=m+1;\n I.s[i][i]=1;\n }\n while(add\n#include\n#include\nusing namespace std;\nconst int MOD = 1e9+7;\n#define LL long long\n\nstruct matrix{\n\tint A[31][31],N;\n\t\n\tmatrix operator *(const matrix &B)const{\n\t\tmatrix C; C.N = N; \n\t\tfor(int i = 0 ; i <= N ; ++i)\n\t\t\tfor(int j = 0 ; j <= N ; ++j){\n\t\t\t\tC.A[i][j] = 0;\n\t\t\t\tfor(int k = 0 ; k <= N ; ++k)\n\t\t\t\t\tC.A[i][j] = ((LL)A[i][k]*B.A[k][j] + C.A[i][j])%MOD;\n\t\t\t}\n\t\treturn C;\n\t}\n}A[64][30],sum[64][30],suf[64][30],ans;\n\nLL N,K,dig[70],cnt;\n\nint main(){\n\tscanf(\"%lld%lld\",&N,&K);\n\twhile(N){dig[cnt++] = N % K; N /= K;}\n\tfor(int i = 0 ; i < K ; ++i){\n\t\tfor(int j = 0 ; j <= K ; ++j)\n\t\t\tA[0][i].A[j][j] = 1,A[0][i].A[j][i] = 1;\n\t\tA[0][i].N = K;\n\t}\n\tsum[0][0] = A[0][0]; suf[0][K-1] = A[0][K-1];\n\tfor(int i = 1 ; i < K ; ++i) sum[0][i] = sum[0][i-1]*A[0][i];\n\tfor(int i = K - 2; ~i ; --i) suf[0][i] = A[0][i]*suf[0][i+1];\n\tfor(int i = 1 ; i < cnt ; ++i){\n\t\tA[i][0] = suf[i-1][0];\n\t\tfor(int j = 1 ; j < K ; ++j)\n\t\t\tA[i][j] = suf[i-1][j]*sum[i-1][j-1];\n\t\tsum[i][0] = A[i][0]; suf[i][K-1] = A[i][K-1];\n\t\tfor(int j = 1 ; j < K ; ++j) sum[i][j] = sum[i][j-1]*A[i][j];\n\t\tfor(int j = K - 2 ; ~j; --j) suf[i][j] = A[i][j]*suf[i][j+1];\n\t}\n\tans.A[0][K] = 1; ans.N = K;\n\tans = ans*sum[cnt-1][dig[cnt-1] - 1];\n\tint last = dig[cnt-1];\n\tfor(int i = cnt - 2 ; ~i ; --i){\n\t\tfor(int j = 0 ; j < dig[i] ; ++j)\t\n\t\t\tans = ans*A[i][(j+last)%K];\n\t\tlast += dig[i];\n\t}\n\tLL res = 0;\n\tfor(int i = 0 ; i <= K ; ++i) res = res + ans.A[0][i];\n\tprintf(\"%lld\\n\",res%MOD);\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "bf88d5154896b0952cc3d51cdc48e057", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nconst int N = 100, D = 31, MOD = 1E9 + 7;\n\nint d, cur, a[N];\nlong long n;\n\nstruct matrix {\n long long f[D][D];\n\n matrix() {\n for (int i = 0; i <= d; i++) {\n for (int j = 0; j <= d; j++) {\n f[i][j] = 0;\n }\n }\n }\n\n matrix operator*(const matrix& oth) const {\n matrix ans;\n for (int i = 0; i <= d; i++) {\n for (int j = 0; j <= d; j++) {\n for (int k = 0; k <= d; k++) {\n (ans.f[i][j] += f[i][k] * oth.f[k][j]) %= MOD;\n }\n }\n }\n return ans;\n }\n} bas[N];\n\nmatrix rotate(int u) {\n (u += d) %= d;\n matrix ans;\n for (int i = 0; i < d; i++) {\n ans.f[i][(i + u) % d] = 1;\n }\n ans.f[d][d] = 1;\n return ans;\n}\n\nvoid init() {\n for (int i = 1; i < d; i++) {\n bas[0].f[i][i] = 1;\n }\n bas[0].f[d][0] = 1;\n bas[0].f[d][d] = 2; bas[0].f[0][d] = -1;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cin >> n >> d;\n for (cur = 0; n > 0; cur++, n /= d) {\n a[cur] = n % d;\n }\n init();\n for (int i = 1; i < cur; i++) {\n bas[i] = rotate(0);\n for (int j = 0; j < d; j++) {\n bas[i] = bas[i] * bas[i - 1] * rotate(1);\n }\n }\n matrix ans = rotate(0);\n for (int i = cur - 1; i >= 0; i--) {\n for (int j = 0; j < a[i]; j++) {\n ans = ans * bas[i] * rotate(1);\n }\n }\n cout << (ans.f[d][d] + MOD) % MOD;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "843d4da92bc6431c808499b5bcb527fc", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// In The Name Of Allah \n// \tMohammad Hosseini\n#include \n#define\tss second\n#define ff first\n#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)\n#define ret(n) return cout << n, 0\n#define se(n) cout << setprecision(n) << fixed\n#define pb push_back\n#define int long long\n#define ld long double\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops\")\n#pragma GCC optimize(\"no-stack-protector,fast-math\")\n//#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\nusing namespace std; \n\nconst int N = 1e6 + 100, OO = 1e9 + 7, M = 1e9 + 7, P = 6151, T = 2e3, SQ = 280, lg = 64, lg2 = 34;\ntypedef pair pii;\nint n, d;\nvector v;\n\nstruct node {\n\tint dp[lg2][lg2], cnt[lg2][lg2];\n\tbool mark[lg2], tark[lg2];\n\tvoid mrg(node &nd) {\n\t\tfor(int i = 0; i <= d; i++) {\n\t\t\tfor(int j = 0; j <= d; j++) {\n\t\t\t\tcnt[i][j] = 0;\n\t\t\t\tfor(int k = 0; k <= d; k++) \n\t\t\t\t\tcnt[i][j] = (cnt[i][j] + dp[k][j] * nd.dp[i][k]) % M;\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < d; i++) {\n\t\t\tif(mark[i] == true && nd.mark[i] == false) {\n\t\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\t\tcnt[i][j] = dp[i][j];\n\t\t\t}\n\t\t\tif(mark[i] == false && nd.mark[i] == true) { \n\t\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\t\tcnt[j][i] = nd.dp[j][i];\n\t\t\t}\n\t\t\tmark[i] |= nd.mark[i];\n\t\t}\n\t\tfor(int i = 0; i <= d; i++)\n\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\tdp[i][j] = cnt[i][j];\n\t}\n\tvoid clean() {\n\t\tfor(int i = 0; i <= d; i++)\n\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\tdp[i][j] = cnt[i][j] = 0;\n\t}\n\tvoid shift(int x) {\n\t\tfor(int i = 0; i <= d; i++)\n\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\tcnt[i][j] = 0;\n\t\tfor(int i = 0; i < d; i++) {\n\t\t\tfor(int j = 0; j < d; j++) \n\t\t\t\tcnt[(i + x) % d][(j + x) % d] = dp[i][j];\t\n\t\t\tcnt[(i + x) % d][d] = dp[i][d];\n\t\t}\n\t\tfor(int i = 0; i < d; i++)\n\t\t\tcnt[d][(i + x) % d] = dp[d][i];\n\t\tcnt[d][d] = dp[d][d];\n\t\tfor(int i = 0; i <= d; i++)\n\t\t\tfor(int j = 0; j <= d; j++)\n\t\t\t\tdp[i][j] = cnt[i][j];\n\t\tfor(int i = 0; i < d; i++)\n\t\t\ttark[i] = mark[(i + x) % d];\n\t\tfor(int i = 0; i < d; i++)\n\t\t\tmark[i] = tark[i];\n\t}\n} md[lg + 5], hp[lg + 5], ht;\n\nnode get(int x) {\n\tint h = x, pw = 1, t = -1;\n\twhile(h >= d)\n\t\th /= d, pw *= d, t++;\n\tx %= pw;\n\tif(t == -1) \n\t\treturn hp[h];\n\tnode ans = md[t];\n\tht = md[t];\n\tfor(int i = 1; i < h; i++) {\n\t\tht.shift(1);\n\t\tans.mrg(ht);\n\t}\n\tht = get(x);\n\tht.shift(h);\t\n\tans.mrg(ht);\n\treturn ans;\n}\n\nint32_t main() {\n use_fast;\n\tcin >> n >> d;\n\tn--;\n\tint m = n;\n\twhile(n) {\n\t\tv.pb(n % d);\n\t\tn /= d;\n\t}\n\thp[0].dp[d][d] = 2;\n\thp[0].dp[d][0] = -1;\n\thp[0].dp[0][d] = 1;\n\thp[0].mark[0] = true;\n\tmd[0] = hp[0];\n\tfor(int i = 1; i < d; i++) {\n\t\thp[i].dp[d][d] = 2;\n\t\thp[i].dp[d][i] = -1;\n\t\thp[i].dp[i][d] = 1;\n\t\thp[i].mark[i] = true;\n\t\tmd[0].mrg(hp[i]);\n\t\thp[i] = md[0];\n\t}\n\tfor(int i = 1; i <= (int)v.size(); i++) {\n\t\tmd[i] = md[i - 1];\n\t\tht = md[i - 1];\n\t\tfor(int j = 1; j < d; j++) {\n\t\t\tht.shift(1);\n\t\t\tmd[i].mrg(ht);\n\t\t}\n\t}\n\tnode ans = get(m);\n\tcout << ans.dp[d][d] << endl;\n\treturn 0;\n}\n/*\nbe carefull :\n1- if not solve after 20 min, read again twice\n2- after submit read the code again\n3- fun with contest\n4- uploaded by ubuntu 20.04 \n5- ...\n*/\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "9939b6b7afbd24f88458f44a35a02e92", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "//#pragma comment(linker, \"/STACK:1024000000,1024000000\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define RD(x) scanf(\"%d\", &x)\n#define REP(i, n) for (int i=0; i\n#define piL pair\n#define mp make_pair\n#define pb push_back\ninline int read(){int ret; scanf(\"%d\", &ret); return ret;}\nint dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};\nint dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};\nint dx4[4] = {1, 0, -1, 0};\nint dy4[4] = {0, 1, 0, -1};\n\nusing namespace std;\n#define N 33\n#define eps 1e-8\n#define pi acos(-1.0)\n#define inf 0XFFFFFFFll\n#define mod 1000000007ll\n#define LL long long\n#define ULL unsigned long long\n\ninline void add(LL &x, LL y) {\n\tx = (x + y) % mod;\n}\n\nint r;\n\nstruct Matrix {\n\tLL a[N][N];\n\tvoid toZero() {\n\t\tmemset(a, 0, sizeof a);\n\t}\n\tvoid toE() {\n\t\ttoZero();\n\t\tREP(i, r)\n\t\t\ta[i][i] = 1;\n\t}\n\tfriend Matrix operator * (const Matrix &a, const Matrix &b) {\n\t\tMatrix ret; ret.toZero();\n\t\tREP(i, r) REP(j, r) REP(k, r)\n\t\t\t\tadd(ret.a[i][j], a.a[i][k] * b.a[k][j]);\n\t\treturn ret;\n\t}\n};\n\nLL n, K;\nMatrix one[70];\nMatrix two[70];\nMatrix rotr[70], rotc[70];\n\nint Main() {\n ios_base::sync_with_stdio(0);\n\n cin >> n >> K;\n r = K + 1;\n\n // 0 - > single 1\n // i - > i - 1\n REP(i, K+1) {\n \tone[i].toE();\n \tif (i)\n \t\tREP(j, K+1)\n \t\t\tone[i].a[i][j] = 1;\n }\n\n REP(i, K+1) {\n \trotr[i].toE();\n \tif (i == 1) {\n \t\trotr[i].toZero();\n \t\trotr[i].a[0][0] = 1;\n \t\trotr[i].a[1][K] = 1;\n \t\tfor (int j = 2; j <= K; j++)\n \t\t\trotr[i].a[j][j-1] = 1;\n \t} else if (i > 1)\n \t\trotr[i] = rotr[1] * rotr[i-1];\n }\n\n REP(i, K+1) {\n \trotc[i].toE();\n \tif (i == 1) {\n \t\trotc[i].toZero();\n \t\trotc[i].a[0][0] = 1;\n \t\trotc[i].a[K][1] = 1;\n \t\tfor (int j = 2; j <= K; j++)\n \t\t\trotc[i].a[j-1][j] = 1;\n \t} else if (i > 1)\n \t\trotc[i] = rotc[i-1] * rotc[1];\n }\n\n\n two[1].toE();\n FOR(i, K)\n \ttwo[1] = one[i] * two[1];\n\n int bit = 0;\n int num[70];\n while (n) {\n \tnum[bit++] = n % K;\n \tn /= K;\n }\n\n for (int i = 2; i <= bit; i++) {\n \ttwo[i].toE();\n \tREP(j, K)\n \t\ttwo[i] = rotr[j] * two[i-1] * rotc[j] * two[i];\n }\n\n reverse(num, num + bit);\n\n Matrix ret; ret.toE();\n\n int ps = 0;\n REP(i, bit) {\n \tif (i == bit - 1)\n \t\tREP(j, num[i])\n \t\t\tret = one[ps%K + 1] * ret, ps++;\n \telse\n \t\tREP(j, num[i])\n \t\t\tret = rotr[ps%K] * two[bit-i-1] * rotc[ps%K] * ret, ps++;\n }\n\n\n LL res = 1;\n FOR(i, K)\n \tadd(res, ret.a[i][0]);\n\n cout << res << endl;\n\n return 0;\n}\n\n\nint main() {\n return Main();\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "1319b9961d3891f707c1808cc92c413e", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include \n#include\n#define LL long long\nusing namespace std;\nconst int N=65;\nconst int mod=1e9+7;\nint m,len;\nLL n,v[N];\nstruct node\n{\n\tint v[35][35];\n\tnode(){memset(v,0,sizeof(v));}\n}S,T[N][35],s1[N][35],s2[N][35];\nnode operator * (node a,node b)\n{\n\tnode c;\n\tfor(int i=0;i<=m;i++)\n\t\tfor(int j=0;j<=m;j++)\n\t\t\tfor(int k=0;k<=m;k++)\n\t\t\t\tc.v[i][j]=(c.v[i][j]+1ll*a.v[i][k]*b.v[k][j]%mod)%mod;\n\treturn c;\n}\nint main()\n{\n\tscanf(\"%lld%d\",&n,&m);\n\tv[0]=n;while(v[len])v[len+1]=v[len]/m,v[len]%=m,len++;\n\tfor(int i=0;i=0;j--)s2[i][j]=T[i][j]*s2[i][j+1];\n\t}\n\tfor(int i=0;i<=m;i++)S.v[0][i]=1;\n\tfor(int i=len-1,j=0;i>=0;i--)\n\t\twhile(v[i]--)S=S*T[i][j],j=(j+1)%m;\n\tprintf(\"%d\",S.v[0][m]);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "bd674ffa4c7ad18e4784fa74f6f8b691", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define y0 y0z\n#define y1 y1z\n#define yn ynz\n#define j0 j0z\n#define j1 j1z\n#define jn jnz\n#define tm tmz\n#define buli(x) (__builtin_popcountll(x))\n#define bur0(x) (__builtin_ctzll(x))\n#define bul2(x) (63-__builtin_clzll(x))\n#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n#define fil(a,b) memset((a),(b),sizeof(a))\n#define cl(a) fil(a,0)\n#define siz(x) ((int)(x).size())\n#define all(x) (x).begin(),(x).end()\n#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)\n#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)\n#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)\n#define pw(x) ((ll(1))<<(x))\n#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))\n#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))\nvoid getre(){int x=0;printf(\"%d\\n\",1/x);}\nvoid gettle(){int res=1;while(1)res<<=1;printf(\"%d\\n\",res);}\ntypedef pair pii;\ntypedef vector vi;\ntypedef vector vpii;\ntemplateinline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}\ntemplateinline bool upmax(T&a,const S&b){return ainline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}\ntemplateinline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}\ntemplateN gcd(N a,N b){return b?gcd(b,a%b):a;}\ntemplateinline int sgn(N a){return a>0?1:(a<0?-1:0);}\n#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)\n #define lld \"%I64d\"\n#else\n #define lld \"%lld\"\n#endif\ninline void gn(long long&x){\n\tint sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');\n\twhile((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;\n}\ninline void gn(int&x){long long t;gn(t);x=t;}\ninline void gn(unsigned long long&x){long long t;gn(t);x=t;}\ninline void gn(double&x){double t;scanf(\"%lf\",&t);x=t;}\ninline void gn(long double&x){double t;scanf(\"%lf\",&t);x=t;}\ninline void gs(char *s){scanf(\"%s\",s);}\ninline void gc(char &c){while((c=getchar())>126 || c<33);}\ninline void pc(char c){putchar(c);}\n#ifdef JCVB\n#define debug(...) fprintf(stderr, __VA_ARGS__)\n#else\n#define debug(...) \n#endif\ntypedef long long ll;\ntypedef double db;\ninline ll sqr(ll a){return a*a;}\ninline db sqrf(db a){return a*a;}\nconst int inf=0x3f3f3f3f;\nconst db pi=3.14159265358979323846264338327950288L;\nconst db eps=1e-6;\nconst int mo=1e9+7;\nint qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}\n \nll n;\nint k;\n \n// mo < 2^30 = 1073741824\n// first call mm_init(sz);\n// indices from 0 to mm_n - 1\n// call a.plus(),b.plus() before a*b\n \nconst int MM_N =33;\n \nint mm_n;\nstruct mat{\n\tint a[MM_N][MM_N];\n\tvoid plus(){\n\t\trep(i,0,mm_n)rep(j,0,mm_n)a[i][j]=(a[i][j]%mo+mo)%mo;\n\t}\n}one;\nmat operator*(const mat&a,const mat&b){\n\tstatic ll ans[MM_N][MM_N];\n\tcl(ans);\n\tll infa=8e18;\n\trep(k,0,mm_n)rep(i,0,mm_n)rep(j,0,mm_n){\n\t\tif((ans[i][j]+=1ll*a.a[i][k]*b.a[k][j])>=infa)ans[i][j]%=mo;\n\t}\n\tmat c;\n\trep(i,0,mm_n)rep(j,0,mm_n)c.a[i][j]=ans[i][j]%mo;\n\treturn c;\n}\nmat qp(mat a,ll b){a.plus();mat n=one;do{if(b&1)n=n*a;a=a*a;}while(b>>=1);return n;}\nvoid mm_init(int sz){\n\tmm_n=sz;\n\trep(i,0,mm_n)one.a[i][i]=1;\n}\n \n \nint nu[111];int tot=0;\n \nmat a[100];\n \nmat jia(const mat&x,int d){\n\tmat ret;\n\trep(i,0,k+1)rep(j,0,k+1)ret.a[i!=k?(i+d)%k:k][j!=k?(j+d)%k:k]=x.a[i][j];\n\treturn ret;\n}\t\nint main()\n{\n#ifdef JCVB\n\t//freopen(\"1.in\",\"r\",stdin);\n\t//freopen(\"1.out\",\"w\",stdout);\n\tint _time_jc=clock();\n#endif\n\tgn(n);gn(k);\n\tmm_init(k+1);\n \n\twhile(n){\n\t\tnu[tot++]=n%k;\n\t\tn/=k;\n\t}\n\tfor (int j=0;j<=k;j++)a[0].a[0][j]=1;\n\tfor (int j=0;j<=k;j++)a[0].a[j][j]=1;\n\tfor (int i=1;i=0;i--){\n\t\tfor (int j=0;j\n#define MOD 1000000007LL\n\nusing namespace std;\n\nstruct vec {\n long long a[30];\n};\n\nstruct mat {\n long long a[30][30];\n};\n\nlong long n, k;\n\nint val (long long um) {\n if (um < k) return um;\n return (um + val (um / k)) % k;\n}\n\nmat M[30], MI[30];\nmat E[200][40], EI[200][40];\nvec cur;\n\nmat mult (mat m1, mat m2) {\n mat m3;\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < k; j++) {\n m3.a[i][j] = 0;\n for (int l = 0; l < k; l++) {\n m3.a[i][j] += (m1.a[i][l] * m2.a[l][j]) % MOD;\n }\n m3.a[i][j] %= MOD;\n }\n }\n return m3;\n}\n\nvec mult (mat m, vec v) {\n vec v2;\n for (int i = 0; i < k; i++) {\n v2.a[i] = 0;\n for (int j = 0; j < k; j++) {\n v2.a[i] += (v.a[j] * m.a[i][j]) % MOD;\n }\n v2.a[i] %= MOD;\n }\n return v2;\n}\n\nvoid pre_comp() {\n for (int i = 0; i < k; i++) {\n E[0][i] = M[i];\n EI[0][i] = MI[i];\n }\n int LOG = 0;\n long long t = n;\n while (t > 0) {\n LOG++;\n t /= k;\n }\n for (int j = 1; j <= LOG; j++) {\n E[j][0] = E[j - 1][0];\n EI[j][0] = EI[j - 1][0];\n for (int l = 1; l < k; l++) {\n E[j][0] = mult (E[j - 1][l], E[j][0]);\n EI[j][0] = mult (EI[j][0], EI[j - 1][l]);\n }\n for (int i = 1; i < k; i++) {\n E[j][i] = mult (E[j - 1][i - 1], mult (E[j][i - 1], EI[j - 1][i - 1]));\n EI[j][i] = mult (E[j - 1][i - 1], mult (EI[j][i - 1], EI[j - 1][i - 1]));\n }\n }\n /* for (int j = 0; j <= LOG; j++) {\n for (int i = 0; i < k; i++) {\n printf (\"E[%d][%d]:\\n\", j, i);\n for (int a = 0; a < k; a++) {\n for (int b = 0; b < k; b++) {\n printf (\"%lld \", E[j][i].a[a][b]);\n }\n printf (\"\\n\");\n }\n printf (\"\\n\");\n printf (\"EI[%d][%d]:\\n\", j, i);\n for (int a = 0; a < k; a++) {\n for (int b = 0; b < k; b++) {\n printf (\"%lld \", EI[j][i].a[a][b]);\n }\n printf (\"\\n\");\n }\n printf (\"\\n\");\n }\n } */\n}\n\nvec solve (long long n, vec v, int at) {\n if (n == 0) return v;\n int w = val (n / k);\n v = solve (n / k, v, at + 1);\n for (int i = 0; i < n % k; i++) {\n v = mult (E[at][(w + i) % k], v);\n }\n return v;\n}\n\nint main() {\n scanf (\"%lld %lld\", &n, &k);\n for (int i = 0; i < k; i++) {\n cur.a[i] = 1;\n for (int j = 0; j < k; j++) {\n for (int l = 0; l < k; l++) {\n if (j == l) {\n M[i].a[j][l] = 1;\n MI[i].a[j][l] = 1;\n }\n else if (j == i) {\n M[i].a[j][l] = 1;\n MI[i].a[j][l] = -1;\n }\n else {\n M[i].a[j][l] = 0;\n MI[i].a[j][l] = 0;\n }\n }\n }\n }\n pre_comp();\n cur = solve (n, cur, 0);\n // printf (\"%lld %lld\\n\", cur.a[0], cur.a[1]);\n long long tot = 0;\n for (int i = 0; i < k; i++) {\n tot += cur.a[i];\n }\n tot--;\n tot %= MOD;\n if (tot < 0) tot += MOD;\n while (tot % (k - 1) != 0) {\n tot += MOD;\n }\n printf (\"%lld\\n\", tot / (k - 1));\n // printf (\"%lld\\n\", (1 << n) % MOD);\n}\n\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "658db5348c3e618c9123da603f54ce69", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\nconst int mod = (int)1e9 + 7;\nint sum(int a, int b) {\n int s = a + b;\n if (s >= mod) s -= mod;\n return s;\n}\nint sub(int a, int b) {\n int s = a - b;\n if (s < 0) s += mod;\n return s;\n}\nint mult(int a, int b) {\n return (1LL * a * b) % mod;\n}\nll n;\nint k;\nvector < vector < int > > matrix_mult(vector < vector < int > > a, vector < vector < int > > b) {\n vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));\n for (int i = 0; i <= k; i++) {\n for (int j = 0; j <= k; j++) {\n for (int t = 0; t <= k; t++) {\n c[i][t] = sum(c[i][t], mult(a[i][j], b[j][t]));\n }\n }\n }\n return c;\n}\nvector < vector < int > > pref[100];\nvector < vector < int > > shift_by_1(vector < vector < int > > mat) {\n vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));\n for (int i = 0; i <= k; i++) {\n for (int j = 0; j <= k; j++) {\n int ni = i;\n if (ni < k) ni = (ni + k - 1) % k;\n int nj = j;\n if (nj < k) nj = (nj + k - 1) % k;\n c[i][j] = mat[ni][nj];\n }\n }\n return c;\n}\nvoid print(vector < vector < int > >& c) {\n for (auto t : c) {\n for (int v : t) cout << v << \" \";\n cout << '\\n';\n }\n cout << \"------------\" << endl;\n}\nvoid init() {\n ll digs = 0;\n ll cop = n;\n while (cop > 0) {\n cop /= k;\n digs++;\n }\n pref[0].resize(k + 1, vector < int >(k + 1, 0));\n for (int i = 1; i < k; i++) {\n pref[0][i][i] = 1;\n }\n pref[0][0][k] = 1;\n pref[0][k][0] = mod - 1;\n pref[0][k][k] = 2;\n // print(pref[0]);\n // print(pref[0]);\n //auto t = shift_by_1(pref[0]);\n // print(t);\n //print(shift_by_1(pref[0]));\n for (int step = 0; step < digs; step++) {\n pref[step + 1] = pref[step];\n auto cur = pref[step];\n //if (step == 0) print(cur);\n for (int i = 1; i <= k - 1; i++) {\n cur = shift_by_1(cur);\n //if (step == 0) print(cur);\n pref[step + 1] = matrix_mult(pref[step + 1], cur);\n }\n }\n // digs =\n}\nvector < vector < int > > solve(ll n) {\n //cout << n << endl;\n if (n == 0) {\n vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));\n for (int i = 0; i <= k; i++) {\n c[i][i] = 1;\n }\n return c;\n }\n ll dig = 0;\n ll pw = 1;\n while (pw <= n / k) {\n pw *= k;\n dig++;\n }\n if (n == pw) return pref[dig];\n ll steps = n / pw;\n ll left = n - steps * pw;\n auto cur_mat = solve(0);\n auto cur_coef = pref[dig];\n for (int i = 0; i < steps; i++) {\n cur_mat = matrix_mult(cur_mat, cur_coef);\n cur_coef = shift_by_1(cur_coef);\n }\n auto other_mat = solve(left);\n for (int i = 0; i < steps; i++) {\n other_mat = shift_by_1(other_mat);\n }\n //print(cur_mat);\n // print(other_mat);\n return matrix_mult(cur_mat, other_mat);\n}\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n srand(239);\n cin >> n >> k;\n init();\n auto mat = solve(n);\n cout << mat[k][k];\n return 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "11374859fe70bd72d389e9e64f026bdd", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define mp make_pair\n#define sz(s) int((s).size())\n#define len(s) int((s).size())\n#define all(s) (s).begin(), (s).end()\n#ifdef _WIN32\n#define LLD \"%I64d\"\n#else\n#define LLD \"%lld\"\n#endif\n#ifdef LOCAL42\n#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n#define eprintf(...) 42\n#endif\n#define y0 yy0\n#define y1 yy1\n#define next _next\n#define prev _prev\n#define rank _rank\n#define link _link\n#define hash _hash\n#define fs first\n#define sc second\n\ntypedef long long ll;\ntypedef long long llong;\ntypedef long long int64;\ntypedef unsigned int uint;\ntypedef long double ld;\ntypedef unsigned long long ull;\ntypedef unsigned long long ullong;\ntypedef unsigned long long lint;\ntypedef pair pii;\ntypedef vector vi;\n\nconst int inf = int(1e9);\nconst double eps = 1e-9;\nconst double pi = 4 * atan(double(1));\nconst int K = 31;\nconst int MOD = int(1e9) + 7;\n\nint k = 0;\n\nstruct matr {\n \n int a[K][K];\n \n matr() {\n for (int i = 0; i <= k; ++i) {\n for (int j = 0; j <= k; ++j) {\n a[i][j] = 0;\n }\n a[i][i] = 1;\n }\n }\n \n};\n\nmatr operator * (const matr &a, const matr &b) {\n matr c;\n for (int i = 0; i <= k; ++i) {\n for (int j = 0; j <= k; ++j) {\n c.a[i][j] = 0;\n for (int z = 0; z <= k; ++z) {\n c.a[i][j] = (c.a[i][j] + 1LL * a.a[i][z] * b.a[z][j]) % MOD;\n }\n }\n }\n return c;\n}\n\nmatr M[K], NM[K], suff[K], pref[K];\n\ninline matr get(int num) {\n matr res;\n for (int i = 0; i <= k; ++i) {\n res.a[num][i] = 1;\n }\n return res;\n}\n\nint main() {\n#ifdef LOCAL42\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#else\n \n#endif\n ll n;\n cin >> n >> k;\n vi v;\n while (n > 0) {\n v.pb(n % k);\n n /= k;\n }\n matr res;\n int shift = 0;\n for (int i = 0; i < sz(v); ++i) {\n shift += v[i];\n }\n for (int i = 0; i < k; ++i) {\n M[i] = matr();\n pref[i] = matr();\n suff[i] = matr();\n }\n for (int i = 0; i < sz(v); ++i) {\n shift -= v[i];\n for (int j = v[i] - 1; j >= 0; --j) {\n if (i == 0) {\n res = get((shift + j) % k) * res;\n continue;\n }\n res = M[(shift + j) % k] * res;\n }\n if (i == 0) {\n for (int i = 0; i < k; ++i) {\n M[i] = matr();\n for (int j = k - 1; j >= 0; --j) {\n M[i] = get((i + j) % k) * M[i];\n }\n }\n } else {\n for (int i = 0; i < k; ++i) {\n M[i] = matr();\n if (i > 0) {\n M[i] = pref[i - 1] * M[i];\n }\n M[i] = suff[i] * M[i];\n }\n }\n for (int i = 0; i < k; ++i) {\n pref[i] = (i > 0 ? pref[i - 1] : matr()) * M[i];\n }\n for (int i = k - 1; i >= 0; --i) {\n suff[i] = M[i] * (i + 1 < k ? suff[i + 1] : matr());\n }\n }\n int ans = 0;\n for (int i = 0; i <= k; ++i) {\n ans = (ans + res.a[i][k]) % MOD;\n }\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "698666c1e16f851ae248b7501d8001c1", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n#define sz(v) ((int)(v).size())\ntypedef long long lld;\n\nconst int MOD = 1e9+7;\nlld N; int K;\n\nstruct MAT{\n int m[31][31];\n MAT operator * (const MAT &ot)const{\n MAT ret = {0, };\n for (int i=0;i<=K;i++) {\n for (int j=0;j<=K;j++) {\n ret.m[i][j] = 0;\n for (int k=0;k<=K;k++)\n ret.m[i][j] = (ret.m[i][j]+(lld)m[i][k]*ot.m[k][j])%MOD;\n }\n } return ret;\n }\n} A[65][30], P[65][30], S[65][30], res;\n\nint main() {\n cin >> N >> K;\n vector base_k;\n for (;N;N/=K) base_k.push_back(N%K);\n for (int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define clr(a) memset(a, 0, sizeof(a))\n#define fill(a, b) memset(a, b, sizeof(a))\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef std::pair pii;\n\n#ifdef DBG1\n #define dbg(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)\n#else\n #define dbg(...)\n#endif\n\nconst ll P = ll(1e9) + 7;\n\nstruct Matrix {\n static const int N = 33;\n int n;\n ll a[N][N];\n\n Matrix(int n = 0, int k = 0) : n(n) {\n memset(a, 0, sizeof(a));\n for (int i = 0; i < n; ++i) {\n a[i][i] = k;\n }\n }\n\n Matrix operator * (const Matrix & m) const {\n assert(n == m.n);\n Matrix res(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n for (int k = 0; k < n; ++k) {\n (res.a[i][k] += a[i][j] * m.a[j][k]) %= P;\n }\n }\n }\n return res;\n }\n\n Matrix& operator *= (const Matrix & m) {\n return *this = *this * m;\n }\n\n Matrix pow(int x) const {\n Matrix res = Matrix(n, 1);\n Matrix a = *this;\n while (x) {\n if (x & 1) {\n res *= a;\n }\n a *= a;\n x /= 2;\n }\n return res;\n }\n\n void print() const {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n dbg(\"%d \", int(a[i][j]));\n }\n dbg(\"\\n\");\n }\n dbg(\"\\n\");\n }\n\n ll* operator[](int index) {\n return a[index];\n }\n\n const ll* operator[](int index) const {\n return a[index];\n }\n};\n\nint main()\n{\n#ifdef DBG1\n freopen(\".in\", \"r\", stdin);\n freopen(\".out\", \"w\", stdout);\n freopen(\".err\", \"w\", stderr);\n#endif\n\n ll n;\n int k;\n cin >> n >> k;\n if (n <= k) {\n cout << (1LL << n) % P << endl;\n return 0;\n }\n\n int logN = int(log(n + 0.0) / log(k + 0.0) + 2);\n\n Matrix shift(k + 1, 1);\n for (int j = 0; j < k; ++j) {\n shift[j][j] = 0;\n shift[(j + 1) % k][j] = 1;\n }\n vector block(logN);\n block[0] = Matrix(k + 1, 1);\n for (int i = 1; i <= k; ++i) {\n block[0][0][i] = 1;\n }\n for (int i = 1; i < logN; ++i) {\n block[i] = (block[i - 1] * shift).pow(k);\n }\n\n int i = 0;\n ll len = 1;\n while (n / len >= k) {\n i++;\n len *= k;\n }\n\n Matrix m(k + 1, 1);\n while (n) {\n if (len <= n) {\n int x = int(n / len);\n m *= (block[i] * shift).pow(x);\n n -= len * x;\n }\n --i;\n len /= k;\n }\n\n ll ans = 0;\n for (int i = 0; i <= k; ++i) {\n (ans += m[i][k]) %= P;\n }\n cout << ans << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "da766af9fbcc30cb823b159a66f2869f", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int MOD = 1000000007;\n\ntypedef vector< vector< int > > mt;\n\nvector< mt > dp;\nmt tmp;\nmt base;\n\nmt\nshift(const mt &a, int shift)\n{\n int k = int(a.size()) - 1;\n // left shift instead of right\n shift = (k - shift) % k;\n mt res = a;\n for (int i = 0; i < k; ++i) {\n for (int j = 0; j < k; ++j) {\n res[i][j] = a[(i + shift) % k][(j + shift) % k];\n }\n }\n for (int j = 0; j < k; ++j) {\n res[k][j] = a[k][(j + shift) % k];\n }\n return res;\n}\n\nmt\noperator *(const mt &a, const mt &b)\n{\n static int aa[200][200], bb[200][200];\n int n = int(a.size()), k = int(a[0].size()), m = int(b[0].size());\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < k; ++j) {\n aa[i][j] = a[i][j];\n }\n }\n for (int i = 0; i < k; ++i) {\n for (int j = 0; j < m; ++j) {\n bb[j][i] = b[i][j];\n }\n }\n mt res(n, vector< int >(m, 0));\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n int tmp = 0;\n for (int l = 0; l < k; ++l) {\n tmp = (tmp + 1ll * aa[i][l] * bb[j][l]) % MOD;\n }\n res[i][j] = tmp;\n }\n }\n return res;\n}\n\nint\nmain()\n{\n ll n;\n int k;\n cin >> n >> k;\n int max_depth = 0;\n {\n ll tmp = n;\n while (tmp != 0) {\n ++max_depth;\n tmp /= k;\n }\n }\n dp.resize(max_depth);\n // constructing matrices for dynamic programming\n { // matrices for initial level\n dp[0].assign(k + 1, vector< int >(k + 1, 0));\n for (int i = 0; i <= k; ++i) {\n for (int j = 0; j <= k; ++j) {\n dp[0][i][j] = (i == j || j == 0);\n }\n }\n mt e(k + 1, vector< int >(k + 1, 0));\n for (int i = 0; i <= k; ++i) {\n e[i][i] = 1;\n }\n for (int depth = 1; depth < max_depth; ++depth) {\n dp[depth] = e;\n mt bs = dp[depth - 1];\n int curx = k;\n for (int i = 0; (1 << i) <= curx; ++i) {\n if (curx & (1 << i)) {\n dp[depth] = bs * shift(dp[depth], (1 << i));\n curx ^= (1 << i);\n }\n bs = bs * shift(bs, (1 << i));\n }\n }\n }\n base.assign(1, vector< int >(k + 1, 0));\n base[0][k] = 1;\n int sl = 0;\n ll x = n;\n vector< int > vec;\n while (x != 0) {\n vec.push_back(x % k);\n x /= k;\n }\n int len = int(vec.size());\n for (int cur_depth = len - 1; cur_depth >= 0; --cur_depth) {\n int rest = vec[cur_depth];\n for (int it = 0; it < rest; ++it, sl = (sl + 1) % k) {\n base = base * shift(dp[cur_depth], sl);\n }\n }\n {\n int res = 0;\n for (int i = 0; i <= k; ++i) {\n res = (res + base[0][i]) % MOD;\n }\n cout << res << '\\n';\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "abcb5fafe418a4377b2c3c8ea5f9b2a7", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\nusing namespace std;\ntypedef pair PI;\ntypedef long long LL;\ntypedef double D;\n#define FI first\n#define SE second\n#define MP make_pair\n#define PB push_back\n#define R(I,N) for(int I=0;I=0;I--)\n#define make(A) scanf(\"%d\",&A)\n#define maka2(A,B) scanf(\"%d%d\",&A,&B)\n#define db(A) printf(\"%s: %d\\n\",#A,A);\ntemplate void ma(C& a,C b){if(a void mi(C& a,C b){if(a>b)a=b;}\n#define MAX 31\nconst int P = 1e9 + 7;\nint n; \nLL pom[MAX][MAX];\nstruct m{\n LL t[MAX][MAX];\n void id(){\n R(i,n)R(j,n)t[i][j] = (i==j);\n }\n void operator*=(m& b){\n R(i,n)R(j,n)pom[i][j] = 0;\n R(i,n)R(j,n)R(k,n)\n pom[i][k] += t[i][j] * b.t[j][k] % P;\n R(i,n)R(j,n)\n t[i][j] = pom[i][j] % P;\n }\n// void pri(){\n// R(i,n){R(j,n)printf(\"%d \",t[i][j]);puts(\"\");}\n// puts(\"\");\n// }\n}obr,nl,ak,d[MAX*2];\nLL m;\nLL pw[MAX];\nLL wyn[MAX];\nint k;\nvector roz;\nmain(){\n scanf(\"%lld%d\",&m,&k);\n while(m){\n roz.PB(m%k);\n m/=k;\n }\n n = k+1;\n R(i,k)obr.t[i][(i+1)%k] = 1;\n obr.t[k][k] = 1;\n// obr.pri();\n R(i,k+1){\n nl.t[0][i] = 1;\n nl.t[i][i] = 1;\n }\n // nl.pri();\n d[0] = nl;\n d[0] *= obr;\n F(i,1,roz.size()){\n d[i].id();\n R(_,k)d[i]*=d[i-1];\n d[i] *= obr;\n //printf(\"d%d\\n\",i);\n // d[i].pri();\n }\n R(i,n)wyn[i] = 1;\n for(int ii = roz.size()-1;ii>=0;ii--){\n R(_,roz[ii]){\n R(i,n)pw[i] = 0;\n R(i,n)R(j,n)pw[j] += wyn[i] * d[ii].t[i][j] % P;\n R(i,n)wyn[i] = pw[i] % P;\n }\n }\n printf(\"%lld\\n\",wyn[k]);\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "729becd14a9bd0414405fd1b01838c35", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\n#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)\n#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)\n#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)\n#define Set(a, v) memset(a, v, sizeof(a))\n#define Cpy(a, b) memcpy(a, b, sizeof(a))\n#define debug(x) cout << #x << \": \" << (x) << endl\n#define pb push_back\n\nusing namespace std;\n\ntypedef long long ll;\n\ntemplate inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }\ntemplate inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }\n\nvoid File() {\n\tfreopen (\"rudy.in\", \"r\", stdin);\n\tfreopen (\"rudy.out\", \"w\", stdout);\n}\n\nconst int K = 105, Mod = 1e9 + 7;\n\nll n; int k;\n\ntemplate\nstruct Matrix {\n\n\tint a[Maxn][Maxn];\n\n\tMatrix() { Set(a, 0); }\n\n\tinline void Unit(int len) {\n\t\tFor (i, 1, len) a[i][i] = 1;\n\t}\n\n\tinline void Out() {\n\t\tFor (i, 1, k + 1) For (j, 1, k + 1)\n\t\t\tprintf (\"%d%c\", a[i][j], j == jend ? '\\n' : ' ');\n\t\tputchar ('\\n');\n\t}\n\n};\n\ntypedef Matrix Mat;\n\ninline int id(int x, int step) {\n\treturn x <= k ? ((x + step - 1) % k + 1) : x;\n}\n\ninline Mat Move(const Mat &Base, int step) {\n\tstep %= k; Mat res;\n\tFor (i, 1, k + 1) For (j, 1, k + 1)\n\t\tres.a[id(i, step)][id(j, step)] = Base.a[i][j];\n\treturn res;\n}\n\ninline Mat operator * (const Mat &lhs, const Mat &rhs) {\n\tMat res; int cur = k + 1;\n\tFor (i, 1, cur) For(k, 1, cur) if (lhs.a[i][k]) For (j, 1, cur)\n\t\tres.a[i][j] = (res.a[i][j] + 1ll * lhs.a[i][k] * rhs.a[k][j]) % Mod;\n\treturn res;\n}\n\nMat A[K]; int ans[K];\n\nint main () {\n\n\tcin >> n >> k;\n\n\tA[0].Unit(k + 1);\n\tFor (i, 1, k + 1)\n\t\tA[0].a[1][i] = 1;\n\n\tvector V;\n\tfor (int len = 0; n; V.pb(n % k), n /= k) {\n\t\tMat x = A[len]; A[++ len].Unit(k + 1);\n\t\tfor (ll power = k, p = 0; power; power >>= 1) {\n\t\t\tif (power & 1) A[len] = A[len] * Move(x, (power ^ 1) << p);\n\t\t\tx = Move(x, 1 << p) * x; ++ p;\n\t\t}\n\t}\n\n\tint step = 0; ans[k + 1] = 1;\n\n\tFordown (T, V.size() - 1, 0) {\n\t\tint cur = V[T];\n\t\tif (cur) {\n\t\t\tstatic int tmp[K];\n\t\t\tFor (times, 0, cur - 1) {\n\t\t\t\tFor (i, 1, k + 1) tmp[i] = ans[i], ans[i] = 0;\n\t\t\t\tMat B = Move(A[T], times + step);\n\t\t\t\tFor (i, 1, k + 1) For (j, 1, k + 1)\n\t\t\t\t\tans[i] = (ans[i] + 1ll * tmp[j] * B.a[i][j]) % Mod;\n\t\t\t}\n\t\t}\n\t\tstep += cur;\n\t}\n\n\tint res = 0;\n\tFor (i, 1, k + 1) (res += ans[i]) %= Mod;\n\tprintf (\"%d\\n\", res);\n\n\treturn 0;\n\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "ff1a2bca3e02e3dcfd8a55c601a94fa5", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿\n⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿\n⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿\n⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿\n⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿\n⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿\n⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿\n⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿\n⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿\n⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿\n⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿\n⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿\n⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿\n⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺\n⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘\n⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆\n⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇\n⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇\n⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇\n⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇\n⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇\n⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀\n⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀\n⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸\n⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼\n⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿\n⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿\n⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿\n⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿\n*/\n\n#include \n\nusing namespace std;\n\n//#define MULTITEST\n\n#define x first\n#define y second\n#define mp make_pair\n#define pb push_back\n#define sqr(a) ((a) * (a))\n#define sz(a) int(a.size())\n#define all(a) a.begin(), a.end()\n#define forn(i, n) for(int i = 0; i < int(n); i++) \n#define fore(i, l, r) for(int i = int(l); i < int(r); i++)\n\ntypedef long long li;\ntypedef long double ld;\ntypedef pair pt;\n\ntemplate ostream& operator << (ostream& out, const pair &a) {\n\treturn out << \"(\" << a.x << \", \" << a.y << \")\";\n}\n\ntemplate ostream& operator << (ostream& out, const vector &v) {\n\tout << \"[\";\n\tforn(i, sz(v)) {\n\t\tif(i) out << \", \";\n\t\tout << v[i];\n\t}\n\treturn out << \"]\";\n}\n\nmt19937 rnd(time(NULL));\n\nconst int INF = int(1e9);\nconst li INF64 = li(1e18);\nconst int MOD = int(1e9) + 7;\nconst ld EPS = 1e-9;\nconst ld PI = acos(-1.0);\n\nconst int N = 32;\n\nli n;\nint k;\n\nbool read () {\n\tif (scanf(\"%lld%d\", &n, &k) != 2)\n\t\treturn false;\n\treturn true;\n}\n\nint add(int a, int b){\n\ta += b;\n\tif (a >= MOD)\n\t\ta -= MOD;\n\treturn a;\n}\n\nint mul(int a, int b){\n\treturn a * li(b) % MOD;\n}\n\ntypedef array, N> mat;\n\nmat operator *(const mat &a, const mat &b){\n\tmat c;\n\tforn(i, k + 1) forn(j, k + 1) c[i][j] = 0;\n\tforn(i, k + 1) forn(j, k + 1) forn(t, k + 1) c[i][j] = add(c[i][j], mul(a[i][t], b[t][j]));\n\treturn c;\n}\n\nmat amat[62][N];\n\nvoid solve(){\n\tvector num;\n\twhile (n > 0){\n\t\tnum.pb(n % k);\n\t\tn /= k;\n\t}\n\t\n\tforn(d, k){\n\t\tforn(i, k + 1) forn(j, k + 1)\n\t\t\tamat[0][d][i][j] = 0;\n\t\tforn(i, k + 1)\n\t\t\tamat[0][d][i][i] = 1;\n\t\tforn(i, k + 1)\n\t\t\tamat[0][d][i][d] = 1;\n\t}\n\t\n\tfore(r, 1, sz(num)){\n\t\tvector pr;\n\t\tpr.pb(mat());\n\t\tforn(i, k + 1) forn(j, k + 1)\n\t\t\tpr[0][i][j] = i == j;\n\t\tforn(i, k)\n\t\t\tpr.pb(pr.back() * amat[r - 1][i]);\n\t\tforn(sh, k){\n\t\t\tamat[r][sh] = pr[0];\n\t\t\tforn(i, k - sh)\n\t\t\t\tamat[r][sh] = amat[r][sh] * amat[r - 1][(i + sh) % k];\n\t\t\tamat[r][sh] = amat[r][sh] * pr[sh];\n\t\t}\n\t}\n\t\n\tmat dp;\n\tforn(i, k + 1) forn(j, k + 1)\n\t\tdp[i][j] = 0;\n\tdp[0][k] = 1;\n\t\n\tint rem = 0;\n\tfor (int i = sz(num) - 1; i >= 0; --i){\n\t\twhile (num[i] > 0){\n\t\t\tdp = dp * amat[i][rem];\n\t\t\trem = (rem + 1) % k;\n\t\t\t--num[i];\n\t\t}\n\t}\n\t\n\tint ans = 0;\n\tforn(i, k + 1)\n\t\tans = add(ans, dp[0][i]);\n\tprintf(\"%d\\n\", ans);\n}\n\nint main() {\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"r\", stdin);\n//\tfreopen(\"output.txt\", \"w\", stdout);\n\t\n\tint tt = clock();\n\t\n#endif\n\t\n\tcerr.precision(15);\n\tcout.precision(15);\n\tcerr << fixed;\n\tcout << fixed;\n\n#ifdef MULTITEST\n\tint tc;\n\tscanf(\"%d\", &tc);\n\twhile(tc--){\n\t\tread();\n#else\n\twhile(read()) {\t\n#endif\n\t\tsolve();\n\t\t\n#ifdef _DEBUG\n\tcerr << \"TIME = \" << clock() - tt << endl;\n\ttt = clock();\n#endif\n\n\t}\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "3829e8b6c997b85e4a4d3b5f1a3d86f8", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n\n#define MAXK 30\n#define MAXD 60\n\nusing namespace std;\n\ntypedef long long lint;\n\nconst lint mod = 1e9 + 7;\n\nint K;\nstruct res_t {\n int many[MAXK][MAXK];\n int inside[MAXK];\n bool exist[MAXK];\n\n res_t() {\n memset(many, 0, sizeof(many));\n memset(inside, 0, sizeof(inside));\n memset(exist, 0, sizeof(exist));\n }\n\n res_t(res_t ©, int rotate) {\n for (int i = 0; i < K; i++) {\n exist[(i+rotate)%K] = copy.exist[i];\n inside[(i+rotate)%K] = copy.inside[i];\n\n for (int j = 0; j < K; j++)\n\tmany[(i+rotate)%K][(j+rotate)%K] = copy.many[i][j];\n }\n }\n\n res_t(const res_t &a, const res_t &b) {\n for (int i = 0; i < K; i++) {\n exist[i] = a.exist[i] || b.exist[i];\n\n inside[i] = a.inside[i];\n if (!a.exist[i])\n\tinside[i] = (inside[i] + b.inside[i]) % mod;\n for (int j = 0; j < K; j++) {\n\tinside[i] = (inside[i] + (lint)a.many[i][j] * b.inside[j]) % mod;\n\n\tmany[i][j] = 0;\n\tif (!b.exist[j])\n\t many[i][j] = a.many[i][j];\n\tif (!a.exist[i])\n\t many[i][j] = (many[i][j] + b.many[i][j]) % mod;\n\tfor (int m = 0; m < K; m++)\n\t many[i][j] = (many[i][j] + (lint)a.many[i][m] * b.many[m][j]) % mod;\n }\n }\n }\n} retpk[MAXD];\nint digits[MAXD];\n\nint main() {\n lint n;\n\n cin >> n >> K;\n \n int nd = 0;\n for (lint x = n; x; x /= K)\n digits[nd++] = x % K;\n \n retpk[0].exist[0] = true;\n retpk[0].inside[0] = 1;\n for (int j = 0; j < K; j++)\n retpk[0].many[0][j] = 1;\n\n for (int pk = 1; pk < nd; pk++) {\n retpk[pk] = retpk[pk-1];\n for (int v = 1; v < K; v++)\n retpk[pk] = res_t(retpk[pk], res_t(retpk[pk-1], v));\n }\n\n res_t result;\n int sum = 0;\n\n for (int d = nd-1; d >= 0; d--) {\n for (int v = 0; v < digits[d]; v++)\n result = res_t(result, res_t(retpk[d], (sum + v) % K));\n sum += digits[d];\n }\n\n int result_sum = 1;\n for (int i = 0; i < K; i++)\n result_sum = (result_sum + result.inside[i]) % mod;\n\n cout << result_sum << endl;\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "dac78702d0530ecf1617ce7be75d24e7", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\nusing int64 = long long;\n\nconstexpr int64 P = 1000000007;\n\nstruct FiniteField {\n private:\n int64 x;\n public:\n FiniteField(int64 x) : x(x) {\n if (x > P || x < 0) { cerr << \"Invalied FiniteField!\" << endl; exit(1); }\n }\n FiniteField() : x(0) {}\n int64 Value() const { return x; }\n inline FiniteField operator+(FiniteField o) const {\n FiniteField r(*this); r += o; return r;\n }\n inline FiniteField operator-(FiniteField o) const {\n FiniteField r(*this); r -= o; return r;\n }\n inline FiniteField operator* (FiniteField o) const {\n FiniteField r(*this); r *= o; return r;\n }\n inline FiniteField operator/ (FiniteField o) const {\n FiniteField r(*this); r /= o; return r;\n }\n inline void operator+= (FiniteField o) { x = (x + o.x) % P; }\n inline void operator-= (FiniteField o) { x = (x + P - o.x) % P; }\n inline void operator*= (FiniteField o) { x = (x * o.x) % P; }\n void operator/=(FiniteField o) {\n int64 p = P - 2; while (p) { if (p % 2) { *this *= o; } o *= o; p /= 2; }\n }\n};\n\ntemplate\nvector> operator*(\n const vector>& a1, const vector>& a2) {\n int n = a1.size();\n int m = a1[0].size();\n if (a2.size() != m) {\n cerr << \"Matrix dimensions don't match for multiplication!\" << endl;\n exit(1);\n }\n int l = a2[0].size();\n vector> result(n, vector(l));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < l; j++) {\n for (int k = 0; k < m; k++) {\n result[i][j] += a1[i][k] * a2[k][j];\n }\n }\n }\n return result;\n}\n\ntemplate\nvector operator*(const vector>& matrix, const vector& v) {\n int n = matrix.size();\n int m = matrix[0].size();\n if (v.size() != m) {\n cerr << \"Matrix and vector dimensions don't match for multiplication!\"\n << endl;\n exit(1);\n }\n vector result(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n result[i] += matrix[i][j] * v[j];\n }\n }\n return result;\n}\n\ntemplate\nvector> Make2DVector(int d1, int d2, T default_value) {\n return vector>(d1, vector(d2, default_value));\n}\n\ntemplate\nvector>>\nMake3DVector(int d1, int d2, int d3, T default_value) {\n return vector>>(\n d1, vector>(d2, vector(d3, default_value)));\n}\n\nconstexpr int DEBUG = 0;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int64 n;\n int d;\n cin >> n >> d;\n\n auto shifts = Make3DVector(d, d + 1, d + 1, 0);\n for (int k = 0; k < d; k++) {\n for (int i = 0; i < d; i++) {\n shifts[k][i][(i + k) % d] = 1;\n }\n shifts[k][d][d] = 1;\n }\n\n auto units = Make3DVector(d, d + 1, d + 1, 0);\n for (int k = 0; k < d; k++) {\n for (int i = 0; i < d + 1; i++) {\n units[k][i][i] = 1;\n }\n for (int i = 0; i < d + 1; i++) {\n units[k][k][i] = 1;\n }\n }\n\n auto identity = Make2DVector(d + 1, d + 1, 0);\n for (int i = 0; i < d + 1; i++) {\n identity[i][i] = 1;\n }\n\n map>> cache;\n\n function>(int64)> find_matrix =\n [&](int64 p) -> vector> {\n if (cache.find(p) != cache.end()) {\n return cache.find(p) -> second;\n }\n\n if (p <= d) {\n auto matrix = identity;\n for (int k = 0; k < p; k++) {\n matrix = units[k] * matrix;\n }\n return matrix;\n }\n\n int64 child_p = d;\n while ((p + child_p - 1) / child_p > d) child_p *= d;\n auto child = find_matrix(child_p);\n auto matrix = child;\n int64 remaining_p = p;\n remaining_p -= child_p;\n for (int k = 1; k < d; k++) {\n if (remaining_p >= child_p) {\n matrix = shifts[d - k] * child * shifts[k] * matrix;\n remaining_p -= child_p;\n } else if (remaining_p > 0) {\n matrix = shifts[d - k] * find_matrix(remaining_p) * shifts[k] * matrix;\n remaining_p = 0;\n } else {\n break;\n }\n }\n\n cache[p] = matrix;\n\n return matrix;\n };\n\n if (DEBUG) cerr << \"PHASE 1\" << endl;\n\n auto matrix = find_matrix(n);\n if (DEBUG) cerr << \"PHASE 2\" << endl;\n\n vector base_vector(d + 1);\n base_vector[d] = 1;\n auto final_vector = matrix * base_vector;\n FiniteField ans = 0;\n for (const auto& x : final_vector) ans += x;\n cout << ans.Value() << endl;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "3a5d7ab439a018d3bf41f9b16e883f1e", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std; \n\n#define PB push_back \n#define MP make_pair \n#define SZ(v) ((int)(v).size()) \n#define FOR(i,a,b) for(int i=(a);i<(b);++i) \n#define REP(i,n) FOR(i,0,n) \n#define FORE(i,a,b) for(int i=(a);i<=(b);++i) \n#define REPE(i,n) FORE(i,0,n) \n#define FORSZ(i,a,v) FOR(i,a,SZ(v)) \n#define REPSZ(i,v) REP(i,SZ(v)) \ntypedef long long ll;\nint gcd(int a,int b) { return b==0?a:gcd(b,a%b); }\n\nconst int MOD=1000000007;\nconst ll LIM=LLONG_MAX-(ll)MOD*MOD;\ntypedef int Mat[32][32];\nll n; int base;\n\nvoid mmult(const Mat &A,const Mat &B,Mat &C) {\n\tREP(i,base+1) REP(j,base+1) {\n\t\tll cur=0;\n\t\tREP(k,base+1) { if(cur>LIM) cur%=MOD; cur+=(ll)A[i][k]*B[k][j]; }\n\t\tC[i][j]=cur%MOD;\n\t}\n}\nvoid mcopy(const Mat &A,Mat &B) {\n\tREP(i,base+1) REP(j,base+1) B[i][j]=A[i][j];\n}\nvoid printmat(const Mat &A) {\n\tREP(i,base+1) { REP(j,base+1) printf(\"%2d \",A[i][j]); puts(\"\"); }\n}\n\nMat mat[60][32]; // mat[i][j] = matrix for sequence of base^i integers, starting with j\n\nMat have,tmp;\n\nvoid run() {\n\tcin>>n>>base;\n\tREP(i,base) REP(j,base+1) REP(k,base+1) mat[0][i][j][k]=i==j||j==k?1:0;\n\tint ci=0,cj=0; ll step=1;\n\twhile(step<=n/base) ++ci,step*=base;\n\n\tFORE(i,1,ci) REP(j,base) {\n\t\tREP(k,base+1) REP(l,base+1) mat[i][j][k][l]=k==l?1:0;\n\t\tREP(k,base) { mmult(mat[i][j],mat[i-1][(j+k)%base],tmp); mcopy(tmp,mat[i][j]); }\n\t}\n\n\tREP(k,base+1) REP(l,base+1) have[k][l]=k==l?1:0;\n\tfor(;ci>=0;--ci,step/=base) {\n\t\tint times=(n/step)%base;\n\t\tREP(x,times) { \n\t\t\tmmult(have,mat[ci][cj],tmp); mcopy(tmp,have); \n\t\t\t//printf(\"after %d times %d^%d (starting with %d)\\n\",x+1,base,ci,cj); printmat(have); printf(\"\\tmult with\\n\"); printmat(mat[ci][cj]);\n\t\t\tif(++cj>=base) cj-=base;\n\t\t}\n\t}\n\t//printf(\"have\\n\"); printmat(have);\n\tint ret=0; REP(k,base+1) { ret+=have[k][base]; if(ret>=MOD) ret-=MOD; }\n\tprintf(\"%d\\n\",ret);\n}\n\nint main() {\n\trun();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "3d5eb7452d3ab35868d4463182edd3e5", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\ntypedef pair l_l;\ntypedef pair i_i;\ntemplate\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n#define EPS (1e-7)\n#define INF (1e9)\n#define PI (acos(-1))\nconst ll mod = 1000000007;\nll N, K;\ntemplate< class T >\nstruct Matrix {\n vector< vector< T > > A;\n\n Matrix() {}\n\n Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}\n\n Matrix(size_t n) : A(n, vector< T >(n, 0)) {};\n\n size_t height() const {\n return (A.size());\n }\n\n size_t width() const {\n return (A[0].size());\n }\n\n inline const vector< T > &operator[](int k) const {\n return (A.at(k));\n }\n\n inline vector< T > &operator[](int k) {\n return (A.at(k));\n }\n\n static Matrix I(size_t n) {\n Matrix mat(n);\n for(int i = 0; i < n; i++) mat[i][i] = 1;\n return (mat);\n }\n\n Matrix &operator+=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] = ((*this)[i][j] + B[i][j]) % mod;\n return (*this);\n }\n\n Matrix &operator-=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] = ((*this)[i][j] - B[i][j] + mod) % mod;\n return (*this);\n }\n\n Matrix &operator*=(const Matrix &B) {\n size_t n = height(), m = B.width(), p = width();\n assert(p == B.height());\n vector< vector< T > > C(n, vector< T >(m, 0));\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n for(int k = 0; k < p; k++)\n C[i][j] = ((C[i][j] + (*this)[i][k] * B[k][j])) % mod;\n A.swap(C);\n return (*this);\n }\n\n Matrix &operator^=(long long k) {\n Matrix B = Matrix::I(height());\n while(k > 0) {\n if(k & 1) B *= *this;\n *this *= *this;\n k >>= 1LL;\n }\n A.swap(B.A);\n return (*this);\n }\n\n Matrix operator+(const Matrix &B) const {\n return (Matrix(*this) += B);\n }\n\n Matrix operator-(const Matrix &B) const {\n return (Matrix(*this) -= B);\n }\n\n Matrix operator*(const Matrix &B) const {\n return (Matrix(*this) *= B);\n }\n\n Matrix operator^(const long long k) const {\n return (Matrix(*this) ^= k);\n }\n\n friend ostream &operator<<(ostream &os, Matrix &p) {\n size_t n = p.height(), m = p.width();\n for(int i = 0; i < n; i++) {\n os << \"[\";\n for(int j = 0; j < m; j++) {\n os << p[i][j] << (j + 1 == m ? \"]\\n\" : \",\");\n }\n }\n return (os);\n }\n\n\n T determinant() {\n Matrix B(*this);\n assert(width() == height());\n T ret = 1;\n for(int i = 0; i < width(); i++) {\n int idx = -1;\n for(int j = i; j < width(); j++) {\n if(B[j][i] != 0) idx = j;\n }\n if(idx == -1) return (0);\n if(i != idx) {\n ret *= -1;\n swap(B[i], B[idx]);\n }\n ret *= B[i][i];\n T vv = B[i][i];\n for(int j = 0; j < width(); j++) {\n B[i][j] /= vv;\n }\n for(int j = i + 1; j < width(); j++) {\n T a = B[j][i];\n for(int k = 0; k < width(); k++) {\n B[j][k] -= B[i][k] * a;\n }\n }\n }\n return (ret);\n }\n\n void rot() {\n Matrix B(A.size());\n for(int i = 0; i < K; i++) {\n for(int j = 0; j < K; j++) {\n B[i][j] = A[(i+K-1)%K][(j+K-1)%K];\n }\n B[i][K] = A[(i+K-1)%K][K];\n B[K][i] = A[K][(i+K-1)%K];\n B[K][K] = A[K][K];\n }\n for(int i = 0; i < A.size(); i++) {\n for(int j = 0; j < A[0].size(); j++) {\n A[i][j] = B[i][j];\n }\n }\n /*\n for(int i = A.size()-2; i >= 0; i--) {\n swap(A[i], A[i+1]);\n }\n for(int i = 0; i < A.size(); i++) {\n for(int j = A[0].size() - 2; j >= 0; j--) {\n swap(A[i][j], A[i][j+1]);\n }\n }\n */\n }\n};\n\nMatrix mat[70][35];\n\n\nll beki(ll a, ll b) {\n if(b == 0) return 1;\n ll ret = beki(a, b / 2);\n ret = ret * ret;\n if(b & 1) ret *= a;\n return ret;\n}\n\nint main() {\n cin >> N >> K;\n ll INDEX = 0;\n ll copy = N;\n while(copy >= K) {\n INDEX++;\n copy /= K;\n }\n mat[0][0] = Matrix::I(K+1);\n for(int i = 1; i < K + 1; i++) mat[0][0][0][i] = 1;\n //cerr << mat[0][0] << endl;\n for(int i = 1; i < K; i++) {\n mat[0][i] = mat[0][i-1];\n mat[0][i].rot();\n //cerr << mat[0][i] << endl;\n }\n for(int index = 1; index <= INDEX; index++) {\n //for(int index = 1; index < 3; index++) {\n mat[index][0] = mat[index-1][0];\n for(int i = 1; i < K; i++) {\n mat[index][0] = mat[index-1][i] * mat[index][0];\n }\n //cerr << index << \" \" << 0 << endl;\n //cerr << mat[index][0] << endl;\n for(int i = 1; i < K; i++) {\n mat[index][i] = mat[index][i-1];\n mat[index][i].rot();\n //cerr << index << \" \" << i << endl;\n //cerr << mat[index][i] << endl;\n }\n }\n cerr << INDEX << endl;\n //cerr << INDEX << endl;\n Matrix ans(K + 1, 1);\n ans[K][0] = 1;\n int used = 0;\n while(INDEX >= 0) {\n for(int i = 0; ; i++) {\n if(beki(K, INDEX) <= N) {\n N -= beki(K, INDEX);\n //ans = mat[INDEX][i] * ans;\n ans = mat[INDEX][used] * ans;\n used++;\n used %= K;\n /*\n cerr << INDEX << \" \" << i << endl;\n cerr << mat[INDEX][i] << endl;\n cerr << ans << endl;\n */\n } else break;\n }\n INDEX--;\n }\n ll val = 0;\n for(ll i = 0; i < K + 1; i++) {\n val += ans[i][0];\n val %= mod;\n }\n cout << val << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "b4d50c398c738d8e267adfa4e3e2299b", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\nusing namespace std;\ntypedef long long ll;\nconst int mod=1e9+7;\n\nstruct matri{\n int m[33][33];\n}em,a[70],r,pre[70],suf[70];\n\nll N,K;\n\nvoid iprint(matri x){\n int i,j;\n for(i=0;i<=K;i++){\n for(j=0;j<=K;j++)\n cout<=0;i--)\n suf[i]=imulti(a[i],suf[i+1]);\n a[0]=suf[0];\n for(i=1;i>N>>K;\n int i,j;\n r=em;\n for(i=0;i0;N/=K){\n int tt=N%K;\n if(tt){\n ll pos=0,tmp=N;\n while(tmp)\n pos=(pos+(tmp/=K))%K;\n for(i=tt-1;i>=0;i--){\n r=imulti(a[(pos+i)%K],r);\n //iprint(r);\n }\n }\n ical();\n }\n int ans=1;\n for(i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nconst double eps(1e-8);\nconst double pi(3.14159265358979);\nconst int K=33,mod=1000000007;\nstruct matrix\n{\n\tlong long a[K][K];\n}t[K]={},ans={{}},pre[K]={},suf[K]={};\nlong long n;\nint k;\ninline void multi(const matrix &m1,const matrix &m2,matrix &to)\n{\n\tstatic matrix tmp={{}};\n\tfor(int i=0;i<=k;++i)\n\t\tfor(int j=0;j<=k;++j)\n\t\t{\n\t\t\ttmp.a[i][j]=0;\n\t\t\tfor(int p=0;p<=k;++p)\n\t\t\t\t(tmp.a[i][j]+=m1.a[i][p]*m2.a[p][j])%=mod;\n\t\t}\n\tto=tmp;\n}\nvoid init()\n{\n\tcin>>n>>k;\n\tfor(int i=0;i<=k;++i)\n\t\tfor(int j=0;j<=k;++j)\n\t\t\tans.a[i][j]=i==j;\n\tfor(int p=0;p bit;\n\tfor(;n;n/=k)\n\t\tbit.push_back(n%k);\n\tbit.push_back(0);\n\tpartial_sum(bit.rbegin(),bit.rend(),bit.rbegin());\n\tfor(auto p=0u;p=bit[p+1];--i)\n\t\t\tmulti(t[i%k],ans,ans);\n\t\tpre[0]=t[0];\n\t\tfor(int i=1;i=0;--i)\n\t\t\tmulti(t[i],suf[i+1],suf[i]);\n\t\tt[0]=suf[0];\n\t\tfor(int i=1;i\nusing namespace std;\nlong long N, K;\nconst long long MOD = 1000000007;\nlong long Power[65], Res[35][35];\nlong long S[65][35][35][35], A[65][35][35][35], P[65][35][35][35], Aux[35][35];\npair X[105];\nvoid copy(long long X[][35], long long Y[][35], long long n)\n{\n for(long long i = 0; i < n; i++)\n for(long long j = 0; j < n; j++)\n X[i][j] = Y[i][j];\n}\ninline void Add(long long& x, long long y)\n{\n x += y;\n if(x >= MOD)\n x -= MOD;\n}\nvoid mul(long long X[][35], long long Y[][35], long long Z[][35], long long n, long long m, long long p)\n{\n for(long long i = 0; i < n; i++)\n for(long long j = 0; j < p; j++)\n {\n Z[i][j] = 0;\n for(long long k = 0; k < m; k++)\n {\n Add(Z[i][j], (1LL * X[i][k] * Y[k][j]) % MOD);\n }\n }\n}\n\nvoid precalcA()\n{\n for(long long x = 0; x < K; x++)\n {\n for(long long i = 0; i < K + 1; i++)\n A[0][x][i][i] = 1;\n for(long long j = 0; j < K + 1; j++)\n A[0][x][j][x] = 1;\n //A[0][x][x][x] = 0;\n }\n copy(P[0][0], A[0][0], K + 1);\n copy(S[0][K - 1], A[0][K - 1], K + 1);\n for(long long x = 1; x < K; x++)\n {\n mul(P[0][x - 1], A[0][x], P[0][x], K + 1, K + 1, K + 1);\n }\n for(long long x = K - 2; x >= 0; x--)\n {\n mul(A[0][x], S[0][x + 1], S[0][x], K + 1, K + 1, K + 1);\n }\n long long cnt = 1;\n for(long long m = K; m <= N; m *= K, cnt++)\n {\n for(long long x = 0; x < K; x++)\n {\n if(x > 0)\n {\n copy(A[cnt][x], S[cnt - 1][x], K + 1);\n mul(A[cnt][x], P[cnt - 1][x - 1], Aux, K + 1, K + 1, K + 1);\n copy(A[cnt][x], Aux, K + 1);\n }\n else\n copy(A[cnt][x], S[cnt - 1][x], K + 1);\n }\n copy(P[cnt][0], A[cnt][0], K + 1);\n copy(S[cnt][K - 1], A[cnt][K - 1], K + 1);\n for(long long x = 1; x < K; x++)\n {\n mul(P[cnt][x - 1], A[cnt][x], P[cnt][x], K + 1, K + 1, K + 1);\n }\n for(long long x = K - 2; x >= 0; x--)\n {\n mul(A[cnt][x], S[cnt][x + 1], S[cnt][x], K + 1, K + 1, K + 1);\n }\n if(m > N / K)\n break;\n }\n}\nint main()\n{\n cin >> N >> K;\n Power[0] = 1;\n int i = 1;\n while(Power[i - 1] <= N / K)\n {\n Power[i] = Power[i - 1] * K;\n ++i;\n }\n int ind = i;\n --ind;\n precalcA();\n long long init = N, last = -1, count = -1, ind2 = 0;\n Res[0][K] = 1;\n for(int i = ind; i >= 0; i--)\n {\n while(Power[i] <= init)\n {\n mul(Res, A[i][ind2], Aux, K + 1, K + 1, K + 1);\n copy(Res, Aux, K + 1);\n ind2++;\n if(ind2 == K)\n ind2 = 0;\n init -= Power[i];\n }\n }\n long long ans = 0;\n for(int i = 0; i <= K; i++)\n Add(ans, Res[0][i]);\n cout << ans << \"\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "90619fe45ed192cce0d102580b7a8ed3", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nconst int mod=1000000007;\ninline int add(int a,int b)\n{\n\tif((a+=b)>=mod)a-=mod;\n\treturn a;\n}\ninline int mult(int a,int b)\n{\n\tlong long t=1ll*a*b;\n\tif(t>=mod)t%=mod;\n\treturn t;\n}\nnamespace matrix\n{\n\ttemplate\n\tclass matr\n\t{\n\t\tpublic:\n\t\t\tint dat[N][M];\n\t\t\tvoid init()\n\t\t\t{\n\t\t\t\tmemset(dat,0,sizeof(dat));\n\t\t\t}\n\t\t\tvoid iden()\n\t\t\t{\n\t\t\t\tinit();\n\t\t\t\tfor(int i=0; i trans()\n\t\t\t{\n\t\t\t\tmatr out;\n\t\t\t\tfor(int i=0; i operator +(const matr a)\n\t\t\t{\n\t\t\t\tmatr out;\n\t\t\t\tfor(int i=0; i operator *(const int &x)\n\t\t\t{\n\t\t\t\tmatr out=*this;\n\t\t\t\tfor(int i=0; i\n\t\t\tfriend matr operator *(const matr a,const matr b)\n\t\t\t{\n\t\t\t\tmatr out;\n\t\t\t\tout.init();\n\t\t\t\tfor(int i=0; i digit;\nmatr<35,35> A[65][35],S[65][35],P[65][35],ans;\nint main()\n{\n\tscanf(\"%lld%d\",&n,&K);\n\tfor(int i=0;i=0;j--) S[i-1][j]=A[i-1][j]*S[i-1][j+1];\n P[i-1][0]=A[i-1][0];\n for(int j=1;j=0;i--)\n\t{\n for(int j=0;j\n#define ll long long\n#define ull unsigned ll\n#define uint unsigned\n#define pii pair\n#define pll pair\n#define IT iterator\n#define PB push_back\n#define fi first\n#define se second\n#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)\n#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)\n#define CLR(a,v) memset(a,v,sizeof(a));\n#define CPY(a,b) memcpy(a,b,sizeof(a));\n#define debug puts(\"wzpakking\")\n#define y1 ysghysgsygsh\nusing namespace std;\nconst int mo=1000000007;\nint q[65],k;\nll n;\nstruct Mat{\n\tint a[32][32];\n\tMat(){\n\t\tmemset(a,0,sizeof(a));\n\t}\n\tMat operator *(const Mat &b)const{\n\t\tMat c;\n\t\tFor(i,0,::k) For(j,0,::k) For(k,0,::k)\n\t\t\tc.a[i][k]=(c.a[i][k]+1ll*a[i][j]*b.a[j][k])%mo;\n\t\treturn c;\n\t}\n};\nMat pre[32],suf[32];\nMat f[65][32],ans;\nint main(){\n\tscanf(\"%lld%d\",&n,&k);\n\tans.a[0][k]=1;\n\tfor (;n;n/=k) q[++*q]=n%k;\n\tFor(i,0,k-1) For(j,0,k)\n\t\tf[1][i].a[j][i]=f[1][i].a[j][j]=1;\n\t//For(i,1,*q) cout<\nusing namespace std;\ntemplate inline void checkMin(T &a, T b) { if(b inline void checkMax(T &a, T b) { if(a VI;\ntypedef pair PII;\n\nconst int MOD = 1e9 + 7;\n\nstruct Matrix {\n static const int MAXN = 32;\n int N;\n int a[MAXN][MAXN];\n\n void init(int _N, int _V) {\n N = _N;\n fill(a[0], a[N], _V);\n }\n\n void mul(const Matrix& b) {\n static Matrix l, r;\n REP (i, N) REP (j, N)\n l.a[i][j] = a[i][j], r.a[i][j] = b.a[i][j];\n REP (i, N) REP (j, N) {\n lint t = 0;\n REP (k, N) t += lint(l.a[i][k]) * r.a[k][j] % MOD;\n a[i][j] = t % MOD;\n }\n }\n} A[64][32], prefix[64][32], suffix[64][32];\n\nlint n;\nint k;\n\nvoid pre_calc(int i) {\n prefix[i][1] = A[i][0];\n for (int j = 2; j <= k; ++j) {\n prefix[i][j] = prefix[i][j - 1];\n prefix[i][j].mul(A[i][j - 1]);\n }\n suffix[i][k - 1] = A[i][k - 1];\n for (int j = k - 2; j >= 0; --j) {\n suffix[i][j] = A[i][j];\n suffix[i][j].mul(suffix[i][j + 1]);\n }\n}\n\nint digsum(lint n) {\n int s = 0;\n for (; n; n /= k) {\n (s += n % k) %= k;\n }\n return s;\n}\n\nMatrix solve(lint n, int m) {\n if (n < k) {\n Matrix mat = A[m][0];\n for (int i = 1; i < n; ++i) {\n mat.mul(A[m][i]);\n }\n return mat;\n }\n Matrix mat = solve(n / k, m + 1);\n int x = digsum(n / k);\n for (int i = 0; i < n % k; ++i) {\n mat.mul(A[m][(x + i) % k]);\n }\n return mat;\n}\n\nint main() {\n cin >> n >> k;\n REP (i, k) {\n A[0][i].init(k + 1, 0);\n REP (j, k + 1) {\n A[0][i].a[j][j] = 1;\n A[0][i].a[j][i] = 1;\n }\n }\n for (int m = 1; pow(k, m) - 1000 <= n; ++m) {\n pre_calc(m - 1);\n REP (x, k) {\n A[m][x] = suffix[m - 1][x];\n if (x > 0) {\n A[m][x].mul(prefix[m - 1][x]);\n }\n }\n }\n Matrix mat = solve(n, 0);\n int ans = 0;\n REP (i, k + 1) {\n (ans += mat.a[k][i]) %= MOD;\n }\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "628c8682e04acd72d0162b583501cb24", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define TRACE(x) cout << #x << \" = \" << x << endl\n#define _ << \" _ \" <<\n\ntypedef long long llint;\n\nconst int mod = 1e9 + 7;\n\nint add(int a, int b) {\n return a+b >= mod ? a+b-mod : a+b;\n}\n\nint mul(int a, int b) {\n return (llint(a)*b)%mod;\n}\n\nconst int M = 32;\n\nstruct matrix {\n int a[M][M];\n matrix() { REP(i, M) REP(j, M) a[i][j] = 0; }\n \n int* operator [] (int i) { return a[i]; }\n friend matrix operator * (matrix a, matrix b) {\n matrix r;\n REP(i, M) REP(j, M) REP(k, M) \n r[i][j] = add(r[i][j], mul(a[i][k], b[k][j]));\n return r;\n }\n};\n\nconst int MAXD = 64;\nconst int MAXK = 32;\n\nmatrix f[MAXD];\nint k;\n\nmatrix calc_matrix(int n, int off) {\n matrix ans;\n REP(i, k+1) REP(j, k+1) {\n int ni = i == k ? i : (i + off)%k;\n int nj = j == k ? j : (j + off)%k;\n ans[ni][nj] = f[n][i][j];\n }\n return ans;\n}\n\nint sum_digits(llint n) {\n int ans = 0;\n while (n > 0) {\n ans += n%k;\n n /= k;\n }\n return ans%k;\n}\n\nint main(void) {\n llint n;\n scanf(\"%lld %d\", &n, &k);\n\n int ndigits = 0;\n llint tmp = n;\n while (tmp > 0) {\n tmp /= k;\n ndigits++;\n }\n\n matrix id;\n REP(i, k+1) id[i][i] = 1;\n\n f[0] = id;\n REP(i, k+1) f[0][i][0] = 1;\n FOR(i, 1, ndigits) {\n f[i] = id;\n REP(j, k) f[i] = f[i] * calc_matrix(i-1, j); \n }\n\n llint cur = 0;\n llint pw = 1;\n\n ndigits--;\n REP(i, ndigits) pw *= k;\n\n matrix ans = id;\n while (cur < n) {\n assert(ndigits >= 0);\n while (cur + pw <= n) {\n ans = ans * calc_matrix(ndigits, sum_digits(cur));\n cur += pw;\n }\n pw /= k;\n ndigits--;\n }\n\n int sum = 0;\n REP(i, k+1) sum = add(sum, ans[k][i]);\n printf(\"%d\\n\", sum);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "2160eff9150c8db213a52b82d988fcab", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define MAX_N 1000100\n#define PI 3.141592653589\n#define EPS 1e-20\n#define BS 10\n#define MOD 1000000007\n#define ZERO 10001\n#define YJSNPI 8101919\n#define INF (1 << 28)\n#define ADD(a, b) a = (a + (ll)b) % MOD\n#define MUL(a, b) a = (a * (ll)b) % MOD\n#define MAX(a, b) a = max(a, b)\n#define MIN(a, b) a = min(a, b)\n\nusing namespace std;\n\ntypedef pair pi;\ntypedef unsigned long long ll;\ntypedef vector vec;\ntypedef vector mat;\n\nll N; int K;\nmat R[40];\nmat D[100][40];\n\nvoid mat_init(mat& A, int a, int b) {\n\tA.resize(a);\n\tfor(int i = 0; i < a; i++) A[i].resize(b, 0);\n}\n\nmat mul(const mat& A, const mat& B) {\n\tmat C(A.size(), vec(B[0].size()));\n\tfor(int i = 0; i < A.size(); i++) \n\t\tfor(int k = 0; k < B.size(); k++) \n\t\t\tfor(int j = 0; j < B[0].size(); j++) \n\t\t\t\tADD(C[i][j], A[i][k] * B[k][j]);\n\treturn C;\n}\n\nvoid show_mat(const mat& A) {\n\tfor(int i = 0; i < A.size(); i++) {\n\t\tfor(int j = 0; j < A[0].size(); j++) {\n\t\t\tcout << A[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\tcout << endl;\n}\n\nint main() {\n\tcin >> N >> K;\n\tint cnt = 0; ll nN = 1;\n\twhile(nN <= N) { nN *= K; cnt++; }\n\tnN /= K;\n\tvector d(cnt);\n\tfor(int i = 0; i < cnt; i++) {\n\t\td[i] = N / nN;\n\t\tN %= nN; nN /= K;\n\t\t//cout << d[i];\n\t}\n\t//cout << endl;\n\tfor(int i = 0; i <= K; i++) mat_init(R[i], K + 1, K + 1);\n\tfor(int i = 0; i <= cnt; i++) \n\t\tfor(int j = 0; j < K; j++) mat_init(D[i][j], K + 1, K + 1);\n\tR[0][K][K] = 1; R[1][K][K] = 1;\n\tfor(int j = 0; j < K; j++) R[0][j][j] = 1;\n\tfor(int j = 0; j < K; j++) R[1][(j + K - 1) % K][j] = 1;\n\tfor(int j = 2; j <= K; j++) R[j] = mul(R[j - 1], R[1]);\n\tfor(int j = 0; j <= K; j++) D[0][0][j][j] = 1;\n\tfor(int j = 0; j <= K; j++) D[0][0][j][0] = 1;\n\tfor(int i = 0; i <= cnt; i++) {\n\t\tfor(int j = 1; j < K; j++) {\n\t\t\tD[i][j] = mul(D[i][j - 1], mul(mul(R[K - j], D[i][0]), R[j]));\n\t\t}\n\t\tD[i + 1][0] = D[i][K - 1];\n\t\t//for(int j = 0; j < K; j++) show_mat(D[i][j]);\n\t}\n\tmat ans(1, vec(K + 1, 0)); ans[0][K] = 1;\n\tint md = 0;\n\tfor(int i = 0; i < cnt; i++) {\n\t\tif(d[i] != 0) ans = mul(ans, mul(mul(R[K - md], D[cnt - i - 1][d[i] - 1]), R[md]));\n\t\t//show_mat(ans);\n\t\tmd += d[i]; md %= K;\n\t}\n\tll res = 0;\n\t//show_mat(ans);\n\tfor(int i = 0; i < K + 1; i++) ADD(res, ans[0][i]);\n\tcout << res << endl;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "6965eecb660e986c045cb6ef7a79a8ef", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//CLOCKS_PER_SEC\nusing namespace std;\n#include\n#define sqr(x) (x)*(x)\n//#define sort stable_sort\n#define ll long long\n#define mk make_pair\n#define pb push_back\n#define in insert\n#define mtr(x,y,z) mk(mk(x,y),z)\n#define fi first\n#define se second\n#define lch(x) ((x)<<1)\n#define rch(x) (((x)<<1)|1)\n#define all(x) (x).begin(),(x).end()\n#define titose CLOCKS_PER_SEC\n#define fpi(x) freopen(x,\"r\",stdin);\n#define fpo(x) freopen(x,\"w\",stdout);\n#define fprio fpi(\"in.txt\");fpo(\"out.txt\");\n#define fast ios_base::sync_with_stdio(false);\ninline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}\ninline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}\ninline void readc(char &x){char c;while (((c=getchar())==' ')||c=='\\n');x=c;}\n#define pii pair\n#define pll pair\n#define vi vector\n#define vl vector\n#define si set\n#define sl set\n#define mii map\n#define mll map\n#define msi map\n#define msl map\n#define piii pair\n#define piipi pair\n#define plll pair\n#define pllpl pair\n#define pqi priority_queue\n#define pql priority_queue\n#define npqi priority_queue,greater >\n#define npql priority_queue,greater >\n#define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)\n#define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)\n#define rep(i,x) forup ((i),1,(x))\n#define repd(i,x) fordo ((i),(x),1)\n#define rep0(i,x) forup ((i),0,(x)-1)\n#define rep0d(i,x) fordo ((i),((int)(x))-1,0)\n#define itr iterator\n#define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)\n#define NO {cout<<\"NO\";return 0;}\n#define YES {cout<<\"YES\";return 0;}\n#define y0 y000000000000000000000000000\n#define y1 y111111111111111111111111111\n#define j0 j000000000000000000000000000\n#define j1 j111111111111111111111111111\n#define cl0(a) memset((a),(0),(sizeof((a))))\n#define clz(a) memset((a),(0x16),(sizeof((a))))\n#define clf(a) memset((a),(-(0x16)),(sizeof((a))))\n#define inf 0x3bbbbbbb\n#define lnf 0x1bbbbbbbbbbbbbbbll\n#define sqrt divi\n#define p2(i) (1ll<<(i))\n#define readi read\n#define readll read\n/*************************************************/\nconst int mod=1e9+7;\nlong long n,m,i,j,dig[75],cnt;\nstruct mat\n{\n\tlong long a[35][35];\n}f[75][35],g,pre[35],suf[35];\nmat mul(mat x,mat y)\n{\n\tmat z;\n\tint i,j,k;\n\tforup(i,0,m){\n\t\tforup(j,0,m){\n\t\t\tz.a[i][j]=0;\n\t\t\tforup(k,0,m){\n\t\t\t\tz.a[i][j]=(z.a[i][j]+1ll*x.a[i][k]*y.a[k][j])%mod;\n\t\t\t}\n\t\t}\n\t}\n\treturn z;\n}\nmat pw(mat x,long long y)\n{\n\tmat z;\n\tint i,j;\n\tforup(i,0,m)forup(j,0,m){\n\t\tz.a[i][j]=(i==j);\n\t}\n\twhile(y){\n\t\tif(y&1){\n\t\t\tz=mul(z,x);\n\t\t}\n\t\tx=mul(x,x);\n\t\ty/=2;\n\t}\n\treturn z;\n}\nint main()\n{\n\tread(n);read(m);\n\tlong long tmp=n;\n\tcnt=-1;\n\twhile(tmp){\n\t\tdig[++cnt]=tmp%m;\n\t\ttmp/=m;\n\t}\n\tg.a[0][m]=1;\n\trep0(i,m)rep0(j,m+1) f[0][i].a[j][i]=f[0][i].a[j][j]=1;\n\trep(i,cnt){\n\t\tpre[0]=f[i-1][0];\n\t\trep(j,m-1){\n\t\t\tpre[j]=mul(pre[j-1],f[i-1][j]);\n\t\t}\n\t\tsuf[m-1]=f[i-1][m-1];\n\t\tfordo(j,m-2,0){\n\t\t\tsuf[j]=mul(f[i-1][j],suf[j+1]);\n\t\t}\n\t\tf[i][0]=suf[0];\n\t\trep(j,m-1) f[i][j]=mul(suf[j],pre[j-1]);\n\t}\n\tint cur=0;\n\tfordo(i,cnt,0){\n\t\twhile(dig[i]--){\n\t\t\tg=mul(g,f[i][cur]);\n//\t\t\trep0(j,m+1) cerr<\n#include \n#include \nconst int MAXN = 50 + 10;\nconst int MOD = 1e9 + 7;\ntypedef long long LL;\n\nLL n;\nint k;\nnamespace {\n inline int add(int x) { return x >= MOD ? x - MOD : x; }\n inline int sub(int x) { return x < 0 ? x + MOD : x; }\n inline int mul(int x, int y) { return (long long) x * y % MOD; }\n}\nnamespace solver1 {\n struct Matrix {\n int a[MAXN][MAXN];\n int n;\n void init() { memset(a, 0, sizeof a); }\n void set() {\n init() ;\n for (int i = 1; i <= n; i++) {\n a[i][i] = 1;\n }\n }\n Matrix operator * (const Matrix &rhs) const {\n Matrix ans;\n ans.n = n;\n static long long tmp[MAXN][MAXN];\n static const long long MODLL = (LL) MOD * MOD;\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n tmp[i][j] = 0;\n }\n }\n for (int i = 1; i <= n; i++) {\n for (int k = 1; k <= n; k++) {\n for (int j = 1; j <= n; j++) {\n tmp[i][j] += (LL) a[i][k] * rhs.a[k][j];\n if (tmp[i][j] >= MODLL) tmp[i][j] -= MODLL;\n }\n }\n }\n\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n ans.a[i][j] = tmp[i][j] % MOD;\n }\n }\n\n return ans;\n }\n };\n Matrix f[80][MAXN], sump[80][MAXN], sums[80][MAXN];\n void main() {\n static int num[80], cnt = 0;\n\n while(n) {\n num[++cnt] = n % k;\n n /= k;\n }\n \n for (int i = 0; i < k; i++) {\n f[0][i].n = k + 1;\n f[0][i].init();\n for (int j = 1; j <= k + 1; j++) {\n f[0][i].a[j][j] = 1;\n f[0][i].a[i + 1][j] = 1;\n }\n }\n sump[0][0] = f[0][0];\n sums[0][k - 1] = f[0][k - 1];\n \n for (int i = 1; i < k; i++) {\n sump[0][i] = f[0][i] * sump[0][i - 1];\n }\n\n for (int i = k - 2; i >= 0; i--) {\n sums[0][i] = sums[0][i + 1] * f[0][i];\n }\n\n for (int i = 1; i < cnt; i++) {\n for (int j = 0; j < k; j++) {\n f[i][j] = j == 0 ? sums[i - 1][j] : sump[i - 1][j - 1] * sums[i - 1][j];\n }\n \n sump[i][0] = f[i][0];\n sums[i][k - 1] = f[i][k - 1];\n for (int j = 1; j < k; j++) {\n sump[i][j] = f[i][j] * sump[i][j - 1];\n }\n \n for (int j = k - 2; j >= 0; j--) {\n sums[i][j] = sums[i][j + 1] * f[i][j];\n }\n }\n\n Matrix ans;\n ans.n = k + 1;\n ans.set();\n\n\n int sum = 0;\n for (int i = cnt; i >= 1; i--) {\n for (int j = 0; j < num[i]; j++) {\n ans = f[i - 1][(sum + j) % k] * ans;\n }\n sum += num[i];\n sum %= k;\n }\n //ans = f[0][(sum + num[1]) % k] * ans;\n\n int res = 0;\n for (int i = 1; i <= k + 1; i++) {\n res = add(res + ans.a[i][k + 1]);\n }\n\n printf(\"%d\\n\", res);\n \n }\n}\nint main() {\n scanf(\"%lld%d\", &n, &k);\n\n solver1::main();\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "a44dbe333c399ce02d55a9330fc9e24c", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\ntemplate\ninline bool maxit(T& a,const T& b) {\n if(a\ninline bool minit(T& a,const T& b) {\n if(a>b) {\n a=b;\n return true;\n }\n return false;\n}\n\ntypedef long long LL;\n\ninline LL gint() {\n char c=getchar();\n int f=1;\n while(c<'0'||c>'9') {\n if(c=='-') {\n f=-1;\n }\n c=getchar();\n }\n LL a=c-'0';\n while((c=getchar())>='0'&&c<='9') {\n a=a*10+c-'0';\n }\n return a*f;\n}\n\n#define MP make_pair\n#define PB push_back\n#define fi first\n#define se second\n#define foreach(it,x) for(typeof((x).begin()) it=(x).begin(),_end_=(x).end();it!=_end_;++it)\n#define REP(i,n) for(int i=0,_end_=(n);i<_end_;i++)\n#define FOR(i,l,r) for(int i=(l),_end_=(r);i<=_end_;i++)\n#define PER(i,r,l) for(int i=(r),_beg_=(l);i>=_beg_;i--)\n#define Sz(x) (int((x).size()))\n#define Al(x) (x).begin(),(x).end()\n#define wa(x) cerr<<#x<<\" \"<y)return 0\n\nconst int maxk=31,MOD=int(1e9+7);\n\ntypedef int Mat[maxk][maxk];\n\nint kb;\n\ninline void mat_mul(Mat A,Mat B,Mat res) {\n static Mat C;\n memset(C,0,sizeof(C));\n REP(i,kb+1)REP(k,kb+1)REP(j,kb+1) {\n C[i][j]=(C[i][j]+(LL)A[i][k]*B[k][j])%MOD;\n }\n memcpy(res,C,sizeof(C));\n}\n\nMat A[70][maxk],pre[maxk],suf[maxk],ans,I;\nvoid init_I() {\n clr(I);\n REP(i,kb+1)I[i][i]=1;\n}\n\nint dig[70],len;\n\nvoid prepocess() {\n REP(i,kb) {\n REP(j,kb+1) {\n A[0][i][j][j]=A[0][i][i][j]=1;\n }\n }\n FOR(i,1,len-1) {\n memcpy(pre[0],A[i-1][0],sizeof(A[i-1][0]));\n FOR(j,1,kb-1) {\n mat_mul(A[i-1][j],pre[j-1],pre[j]);\n }\n memcpy(suf[kb-1],A[i-1][kb-1],sizeof(A[i-1][kb-1]));\n PER(j,kb-2,0) {\n mat_mul(suf[j+1],A[i-1][j],suf[j]);\n }\n REP(j,kb) {\n mat_mul(j?pre[j-1]:I,suf[j],A[i][j]);\n }\n }\n}\n\nint main() {\n LL n=gint();\n kb=gint();\n init_I();\n while(n) {\n dig[len++]=n%kb;\n n/=kb;\n }\n prepocess();\n ans[kb][0]=1;\n int x=0;\n PER(i,len-1,0) {\n REP(j,dig[i]) {\n mat_mul(A[i][(j+x)%kb],ans,ans);\n }\n x=(x+dig[i])%kb;\n }\n int res=0;\n for(int i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\ntemplate\nstruct Matrix{\n int h,w;\n vector a;\n Matrix(int n_=0):h(n_),w(n_),a(n_*n_){}\n Matrix(int n_, int m_):h(n_),w(m_),a(n_*m_){}\n Matrix(int n_, int m_, vector a_):h(n_),w(m_),a(a_){}\n T& get(int r,int c){return a[r*w+c];}\n T get(int r,int c)const{return a[r*w+c];}\n\n Matrix& operator+=(const Matrix& m) {\n assert(h == m.h && w == m.w);\n for (int i = 0; i < w*h; ++i)a[i] += m.a[i];\n }\n Matrix& operator-=(const Matrix& m) {\n assert(h == m.h && w == m.w);\n for (int i = 0; i < w*h; ++i)a[i] -= m.a[i];\n }\n Matrix& operator*=(const Matrix& m) {\n assert(w == m.h);\n Matrix ret(h, m.w);\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < m.w; ++j) {\n for (int k = 0; k < w; ++k) {\n ret.a[i * m.w + j] += a[i * w + k] * m.a[k * m.w + j]%mod;\n }\n ret.a[i * m.w + j] %= mod;\n }\n }\n return (*this) = ret;\n }\n Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }\n Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }\n Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }\n};\ntemplate\nMatrix unit(int n){\n Matrix ret(n);\n rep(i,n)ret.get(i,i)=1;\n return ret;\n}\ntemplate\nMatrix matpow(Matrix a,int n){\n assert(a.h==a.w);\n Matrix ret=unit(a.h);\n while(n){\n if(n&1)ret*=a;\n a*=a;\n n>>=1;\n }\n return ret;\n}\n\nMatrix a[65];\nMatrix calc(int i, int k){\n return matpow(a[i+k],k-i)*matpow(a[i],i);\n}\nll solve(ll n,ll k){\n ll cnt = 0;\n ll tmp = n;\n while(tmp){\n tmp/=k;\n ++cnt;\n }\n Matrix b[cnt+5],c[cnt+5],d[k+1];\n b[0]=a[k-1];\n c[0]=matpow(b[0],k-1);\n b[1] = calc(k-1, k);\n c[1] = matpow(b[1],k-1);\n Matrix s = unit(2*k+1);\n REP(j,2,cnt+2){\n b[j] = calc(k-j%k, k);\n s *= c[j-1];\n b[j] *= s;\n c[j] = matpow(b[j],k-1);\n }\n int idx = 1;\n int rem = n%k;\n n-=rem;\n int q = 0;\n tmp = n;\n if(tmp){while(tmp%k==0){\n ++q;\n tmp/=k;\n }\n }\n q=(k-q%k)%k;\n Matrix ans = unit(2*k+1);\n ans = matpow(a[q],rem)*matpow(a[q+k],max(rem-q,0));\n n/=k;\n if(!n){\n return (ans.get(2*k,0)+ans.get(0,0))%mod;\n }\n --n;\n while(n){\n ll t = n%k;\n if(t){\n ans *= matpow(b[idx],t);\n }\n n/=k;\n ++idx;\n }\n ans *= b[1];\n\n return (ans.get(2*k,0)+ans.get(0,0))%mod;\n}\nint main(){\n ll n,k;\n cin>>n>>k;\n rep(i,2*k+1){\n a[i]=Matrix(2*k+1);\n rep(j,i)a[i].get(0,j)=1;\n rep(j,2*k-1)a[i].get(j+1,j)=1;\n a[i].get(2*k,0)=1;\n a[i].get(2*k,2*k)=1;\n }\n cout<\nusing namespace std;\n#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)\n#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)\ntypedef long long ll;\ninline int read(){\n\tint x;\n\tchar c;\n\tint f=1;\n\twhile((c=getchar())!='-' && (c<'0' || c>'9'));\n\tif(c=='-') c=getchar(),f=-1;\n\tx=c^'0';\n\twhile((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');\n\treturn x*f;\n}\ninline ll readll(){\n\tll x;\n\tchar c;\n\tll f=1;\n\twhile((c=getchar())!='-' && (c<'0' || c>'9'));\n\tif(c=='-') c=getchar(),f=-1;\n\tx=c^'0';\n\twhile((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');\n\treturn x*f;\n}\nconst int maxn=32,mod=1e9+7;\ninline void add(int &x,int y){\n\tx+=y;\n\tif(x>=mod) x-=mod;\n}\nint n;\nstruct Matrix{\n\tint a[maxn][maxn];\n\tvoid clear(){\n\t\tmemset(a,0,sizeof(a));\n\t}\n\tvoid output(){\n\t\tREP(i,1,n+1) REP(j,1,n+1) printf(\"%d%c\",a[i][j],j==jend?'\\n':' ');\n\t\tputchar('\\n');\n\t}\n\tMatrix operator *(const Matrix &rhs) const{\n\t\tMatrix B;\n\t\tB.clear();\n\t\tREP(i,1,n+1) REP(j,1,n+1) REP(k,1,n+1) add(B.a[i][j],1ll*a[i][k]*rhs.a[k][j]%mod);\n\t\treturn B;\n\t}\n};\nMatrix ans,E,A[maxn],pre[maxn],suf[maxn];\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"matrix.in\",\"r\",stdin);\n\tfreopen(\"matrix.out\",\"w\",stdout);\n#endif\n\tll m=readll();\n\tn=read();E.clear();\n\tREP(i,1,n+1) E.a[i][i]=1;\n\tans=E;\n\tREP(i,1,n){\n\t\tA[i]=E;\n\t\tREP(j,1,n+1) A[i].a[j][i]=1;\n\t}\n\tpre[0]=suf[n+1]=E;\n\tfor(;m;m/=n){\n\t\tll u=m%n;\n\t\tif(u){\n\t\t\tll s=0;\n\t\t\tfor(ll v=m;v;v/=n) s=(s+v/n)%n;\n\t\t\tu=(s+u-1)%n+1;\n\t\t\tfor(;u!=s;--u){\n\t\t\t\tif(u<=0) u+=n;\n\t\t\t\tans=A[u]*ans;\n\t\t\t}\n\t\t}\n\t\tREP(i,1,n) pre[i]=pre[i-1]*A[i];\n\t\tDREP(i,n,1) suf[i]=A[i]*suf[i+1];\n\t\tREP(i,1,n) A[i]=suf[i]*pre[i-1];\n\t}\n\tint num=1;\n\tREP(i,1,n) add(num,ans.a[n+1][i]);\n\tprintf(\"%d\\n\",num);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "74a700255c270171a1c5a14ee4588b7a", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\nusing namespace std;\nconst int Mod=1e9+7;\ntypedef long long ll;\nll n;\nint m,K,sum,now,a[110];\nvoid ADD(int &x,int y,int m){\n\tx+=y; if (x>=m) x-=m;\n}\nstruct Matrix{\n\tint v[35][35];\n\tMatrix(){ memset(v,0,sizeof(v));}\n\tMatrix operator*(const Matrix &a) const{\n\t\tMatrix res;\n\t\tfor (int k=0;k<=K;k++)\n\t\t\tfor (int i=0;i<=K;i++)\n\t\t\t\tif (v[i][k])\n\t\t\t\t\tfor (int j=0;j<=K;j++)\n\t\t\t\t\t\tif (a.v[k][j])\n\t\t\t\t\t\t\tres.v[i][j]=(1ll*v[i][k]*a.v[k][j]+res.v[i][j])%Mod;\n\t\treturn res;\n\t}\n} ans,/*hanhan,*/s[75][35],pre[75][35],suf[75][35];\nvoid work(ll n){\n\tll tmp=n;\n\twhile (tmp){\n\t\ta[++m]=tmp%K;\n\t\ttmp/=K;\n\t}\n\treverse(a+1,a+m+1);\n}\nint main(){\n\tscanf(\"%lld%d\",&n,&K);\n//\tn++; \n\twork(n);\n\tfor (int i=0;i=0;j--) suf[i-1][j]=s[i-1][j]*suf[i-1][j+1];\n\t\tfor (int j=0;j=K){\n\t\t\t\tfor (int j=now;j\nusing namespace std;\n#define mod 1000000007\n#define N 33\n#define int long long\nint bin[N*2],n,m;\nstruct matrix{ \n int mat[N][N];\n inline matrix operator * (const matrix &a) const {\n matrix res;\n memset(res.mat,0,sizeof(res.mat));\n for (int i=0;i<=m;++i){\n for (int j=0;j<=m;++j){\n for (int k=0;k<=m;++k){\n res.mat[i][j]=(res.mat[i][j]+mat[i][k]*a.mat[k][j])%mod;\n }\n }\n }\n return res;\n }\n}ans,P[N*2][N],S[N*2][N],A[N*2][N];\nint read(){\n int x=0,f=1;\n char ch=getchar();\n while (ch<'0' || ch>'9'){\n if (ch=='-') f=-1;\n ch=getchar();\n }\n while (ch>='0' && ch<='9'){\n x=x*10+ch-'0';ch=getchar();\n }\n return x*f;\n}\nsigned main(){\n n=read(),m=read();\n int n2=n,len=-1;\n while (n2){ \n len++;bin[len]=n2%m;n2=n2/m;\n }\n len++;\n for (int i=0;i<=m;++i) ans.mat[0][i]=1; \n for (int i=0;i=0;--j) S[i][j]=A[i][j]*S[i][j+1];\n }\n int j=0;\n for (int i=len-1;i>=0;--i){\n while (bin[i] --> 0){\n ans=ans*A[i][j];j++;if (j==m) j=0;\n }\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\n#if defined(_MSC_VER) || __cplusplus > 199711L\n#define aut(r,v) auto r = (v)\n#else\n#define aut(r,v) typeof(v) r = (v)\n#endif\n#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)\n#define all(o) (o).begin(), (o).end()\n#define pb(x) push_back(x)\n#define mp(x,y) make_pair((x),(y))\n#define mset(m,v) memset(m,v,sizeof(m))\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\nusing namespace std;\ntypedef vector vi; typedef pair pii; typedef vector > vpii;\ntypedef long long ll; typedef vector vl; typedef pair pll; typedef vector > vpll;\ntypedef vector vs; typedef long double ld;\ntemplate inline void amin(T &x, U y) { if(y < x) x = y; }\ntemplate inline void amax(T &x, U y) { if(x < y) x = y; }\n\ntemplate\nstruct ModInt {\n\tstatic const int Mod = MOD;\n\tunsigned x;\n\tModInt(): x(0) { }\n\tModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }\n\tModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }\n\tint get() const { return (int)x; }\n\t\n\tModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }\n\tModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }\n\tModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }\n\t\n\tModInt operator+(ModInt that) const { return ModInt(*this) += that; }\n\tModInt operator-(ModInt that) const { return ModInt(*this) -= that; }\n\tModInt operator*(ModInt that) const { return ModInt(*this) *= that; }\n};\ntypedef ModInt<1000000007> mint;\n\nstruct Matrix {\n\ttypedef mint Num;\n\tvector > v, w;\n\tMatrix() {}\n\tMatrix(int n, int m): v(n, vector(m)) { }\n\tinline int height() const { return (int)v.size(); }\n\tinline int width() const { return (int)v[0].size(); }\n\tinline Num& at(int i, int j) { return v[i][j]; }\n\tinline const Num& at(int i, int j) const { return v[i][j]; }\n\tstatic Matrix identity(int n) {\n\t\tMatrix A(n, n);\n\t\trep(i, n) A.at(i, i) = 1;\n\t\treturn A;\n\t}\n\tinline static Matrix identity(const Matrix& A) { return identity(A.height()); }\n\tMatrix& operator*=(const Matrix& B) {\n\t\tint n = height(), m = B.width(), p = B.height();\n\t\tassert(p == width());\n\t\tw.assign(n, vector(m, 0));\n\t\trep(i, n) rep(j, m) {\n\t\t\tNum x = 0;\n\t\t\trep(k, p) x += at(i, k) * B.at(k, j);\n\t\t\tw[i][j] = x;\n\t\t}\n\t\tv.swap(w);\n\t\treturn *this;\n\t}\n};\nMatrix operator^(const Matrix& t, ll k) {\n\tMatrix A = t, B = Matrix::identity(t);\n\twhile(k) {\n\t\tif(k & 1) B *= A;\n\t\tA *= A;\n\t\tk >>= 1;\n\t}\n\treturn B;\n}\n\nMatrix getRotateMatrix(int k, int t) {\n\tt = (t % k + k) % k;\n\tMatrix A(k + 3, k + 3);\n\trep(i, k)\n\t\tA.at(i, (i + t) % k) = 1;\n\tA.at(k, k) = A.at(k+1, k+1) = A.at(k+2, k+2) = 1;\n\treturn A;\n}\n\nMatrix getSingletonMatrix(int k, int j) {\n\tassert(0 <= j && j < k);\n\tMatrix A(k + 3, k + 3);\n\trep(i, k) if(i != j)\n\t\tA.at(i, i) += 1;\n\tA.at(k + 1, k + 1) = A.at(k + 2, k + 2) = 1;\n\n\tA.at(k, k + 1) += 1;\n//\tA.at(k + 2, k + 1) += 1;\n\tA.at(j, k + 1) -= 1;\n\n\tA.at(k, k) += 2;\n//\tA.at(k + 2, k) += 1;\n\tA.at(j, k) -= 1;\n\n\tA.at(k, j) += 1;\n//\tA.at(k + 2, j) += 1;\n//\tA.at(j, j) -= 1;\n\treturn A;\n}\n\nint s_k(int k, long long j) {\n\tlong long t = j; int u = 0;\n\twhile(t > 0) {\n\t\t(u += t % k) %= k;\n\t\tt /= k;\n\t}\n\treturn u;\n}\n\nint main() {\n\tlong long n; int k;\n\twhile(cin >> n >> k) {\n\tMatrix b(1, k + 3);\n\tb.at(0, k) = 1;\n\tb.at(0, k + 1) = b.at(0, k + 2) = 1;\n\t/*\n\t{\n\t\tMatrix bb = b;\n\t\t\trep(i, k+3) cerr << bb.at(0, i).get() << \", \"; cerr << endl;\n\t\tfor(int j = 0; j < n; ++ j) {\n\t\t\tint u = s_k(k, j);\n\t\t\tMatrix A = getSingletonMatrix(k, u);\n\t\t\tbb *= A;\n\t\t\trep(i, k+3) cerr << bb.at(0, i).get() << \", \"; cerr << endl;\n\t\t}\n\t\trep(j, n) cerr << s_k(k, j) << \", \"; cerr << endl;\n\t\tmint ans2 = bb.at(0, k + 1);\n\t\tcerr << \"ans2 = \" << ans2.get() << endl;\n\t}\n//\t*/\n\tlong long powk = 1; int digits = 0;\n\twhile(powk <= n / k) powk *= k, ++ digits;\n\n\tvector P(digits + 1);\n\tP[0] = getSingletonMatrix(k, 0);\n\trer(i, 1, digits) {\n\t\tP[i] = Matrix::identity(k + 3);\n\t\trep(j, k) {\n\t\t\tP[i] *= P[i-1];\n\t\t\tP[i] *= getRotateMatrix(k, -1);\n\t\t}\n\t}\n\n\tlong long offset = 0;\n\twhile(powk > 0) {\n\t\tint t = (int)((n - offset) / powk);\n\t\trep(j, t) {\n\t\t\tint s = s_k(k, offset);\n\t\t\tb *= getRotateMatrix(k, -s);\n\t\t\tb *= P[digits];\n\t\t\tb *= getRotateMatrix(k, +s);\n\t\t\toffset += powk;\n\t\t}\n\t\tpowk /= k;\n\t\t-- digits;\n\t}\n\n\tmint ans = b.at(0, k + 1);\n\tcout << ans.get() << endl;\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "468b1eda664caa2db9bfab8d9be9dfa0", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)\n#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)\ntypedef long long ll;\ninline int read(){\n\tint x;\n\tchar c;\n\tint f=1;\n\twhile((c=getchar())!='-' && (c<'0' || c>'9'));\n\tif(c=='-') c=getchar(),f=-1;\n\tx=c^'0';\n\twhile((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');\n\treturn x*f;\n}\ninline ll readll(){\n\tll x;\n\tchar c;\n\tll f=1;\n\twhile((c=getchar())!='-' && (c<'0' || c>'9'));\n\tif(c=='-') c=getchar(),f=-1;\n\tx=c^'0';\n\twhile((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');\n\treturn x*f;\n}\nconst int maxn=32,mod=1e9+7;\ninline void add(int &x,int y){\n\tx+=y;\n\tif(x>=mod) x-=mod;\n}\nint n;\nstruct Matrix{\n\tint a[maxn][maxn];\n\tvoid clear(){\n\t\tmemset(a,0,sizeof(a));\n\t}\n\tvoid output(){\n\t\tREP(i,1,n+1) REP(j,1,n+1) printf(\"%d%c\",a[i][j],j==jend?'\\n':' ');\n\t\tputchar('\\n');\n\t}\n\tMatrix operator *(const Matrix &rhs) const{\n\t\tMatrix B;\n\t\tB.clear();\n\t\tREP(i,1,n+1) REP(j,1,n+1) REP(k,1,n+1) add(B.a[i][j],1ll*a[i][k]*rhs.a[k][j]%mod);\n\t\treturn B;\n\t}\n};\nMatrix ans,E,A[maxn],pre[maxn],suf[maxn];\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"matrix.in\",\"r\",stdin);\n\tfreopen(\"matrix.out\",\"w\",stdout);\n#endif\n\tll m=readll();\n\tn=read();E.clear();\n\tREP(i,1,n+1) E.a[i][i]=1;\n\tans=E;\n\tREP(i,1,n){\n\t\tA[i]=E;\n\t\tREP(j,1,n+1) A[i].a[j][i]=1;\n\t}\n\tpre[0]=suf[n+1]=E;\n\tfor(;m;m/=n){\n\t\tll u=m%n;\n\t\tif(u){\n\t\t\tll s=0;\n\t\t\tfor(ll v=m/n;v;v/=n) s=(s+v%n)%n;\n\t\t\tu=(s+u-1)%n+1;\n\t\t\tfor(;u!=s;--u){\n\t\t\t\tif(u<=0) u+=n;\n\t\t\t\tans=A[u]*ans;\n\t\t\t}\n\t\t}\n\t\tREP(i,1,n) pre[i]=pre[i-1]*A[i];\n\t\tDREP(i,n,1) suf[i]=A[i]*suf[i+1];\n\t\tREP(i,1,n) A[i]=suf[i]*pre[i-1];\n\t}\n\tint num=1;\n\tREP(i,1,n) add(num,ans.a[n+1][i]);\n\tprintf(\"%d\\n\",num);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "d256a7888a8ed53ff18795ff73e2a90d", "src_uid": "175ce134da7cc5af9c8457e7bd9a40a2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\nll L[1050];\nint num=0;\nvoid dfs(int len,int p,int v)\n{\n if(p==len)\n {\n L[++num]=v;\n return;\n }\n dfs(len,p+1,v*10+4);\n dfs(len,p+1,v*10+7);\n}\n\nll contain(ll x1,ll y1,ll x2,ll y2)\n{\n ll l=max(x1,x2);\n ll r=min(y1,y2);\n return r-l+1>0?r-l+1:0;\n}\n\nint main()\n{\n for(int len=1;len<=9;len++) dfs(len,0,0);\n L[0]=0;\n L[++num]=1000000000;\n //for(int i=1;i<=num;i++) printf(\"%d %lld\\n\",i,L[i]);\n ll Pl,Pr,Vl,Vr;\n int k;\n scanf(\"%lld%lld%lld%lld%d\",&Pl,&Pr,&Vl,&Vr,&k);\n ll sum=0;\n for(int i=1;i<=1022-k+1;i++)\n {\n int j=i+k-1;\n sum+=contain(L[i-1]+1,L[i],Pl,Pr)*contain(L[j],L[j+1]-1,Vl,Vr);\n if(L[i]>Pr) break;\n }\n\n for(int i=1;i<=1022-k+1;i++)\n {\n int j=i+k-1;\n sum+=contain(L[i-1]+1,L[i],Vl,Vr)*contain(L[j],L[j+1]-1,Pl,Pr);\n if(L[i]>Vr) break;\n }\n\n if(k==1)\n {\n for(int i=1;i<=1022;i++)\n {\n if(contain(L[i],L[i],Pl,Pr)&&contain(L[i],L[i],Vl,Vr)) sum--;\n }\n }\n\n printf(\"%.10f\\n\",sum*1.0/(Vr-Vl+1)/(Pr-Pl+1));\n}\n\n \t \t\t \t\t\t \t \t \t \t\t \t\t \t", "lang_cluster": "C++", "tags": ["dfs and similar", "probabilities", "combinatorics", "brute force"], "code_uid": "9db51a2702fd97118fdf7bf2cbf4ad73", "src_uid": "5d76ec741a9d873ce9d7c3ef55eb984c", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\nclass Solution {\npublic:\n\tvector> construct(string& s, string& t) {\n\t\tdeque> qs;\n\t\tdeque> qt;\n\n\t\tint n = s.length();\n\t\tint m = t.length();\n\n\t\tfor (int i = 0; i < n; ++i) {\n\t\t\tchar c = s[i];\n\t\t\tint cnt = 1;\n\t\t\twhile (i + 1 < n && s[i + 1] == c) {\n\t\t\t\t++i;\n\t\t\t\t++cnt;\n\t\t\t}\n\n\t\t\tqs.emplace_back(c, cnt);\n\t\t}\n\n\t\tfor (int i = 0; i < m; ++i) {\n\t\t\tchar c = t[i];\n\t\t\tint cnt = 1;\n\t\t\twhile (i + 1 < m && t[i + 1] == c) {\n\t\t\t\t++i;\n\t\t\t\t++cnt;\n\t\t\t}\n\t\t\tqt.emplace_back(c, cnt);\n\t\t}\n\n\t\tvector> res;\n\t\tres.reserve(n + m + 1000);\n\n\t\t//wa : ba, ababa \n\t\t//answer 3 : (b)a, (aba)ba => (a)ba, (b)a => (b)a, a => a, b\n\t\t//got 4\n\t\twhile (qs.size() != 1 || qt.size() != 1) {\n\t\t\t// cout << qs.size() << \" \" << qt.size() << endl;\n\t\t\tassert(qs.size() > 0 && qt.size() > 0);\n\t\t\tif (qs.empty()) {\n\t\t\t\t// res.emplace_back(0, qt.front().second);\n\n\t\t\t\t// qs.push(qt.front());\n\t\t\t\t// qt.pop();\n\t\t\t\tint n = qt.size() / 2;\n\t\t\t\tif (n % 2 == 0 && n + 1 < qt.size()) {\n\t\t\t\t\t++n;\n\t\t\t\t}\n\t\t\t\tint cnt = 0;\n\n\t\t\t\twhile (n-- > 0) {\n\t\t\t\t\tcnt += qt.front().second;\n\t\t\t\t\tqs.push_back(qt.front());\n\t\t\t\t\tqt.pop_front();\n\t\t\t\t}\n\n\t\t\t\tres.emplace_back(0, cnt);\n\n\t\t\t} else if (qt.empty()) {\n\t\t\t\t// res.emplace_back(qs.front().second, 0);\n\n\t\t\t\t// qt.push(qs.front());\n\t\t\t\t// qs.pop();\n\n\t\t\t\tint n = qs.size() / 2;\n\t\t\t\tif (n % 2 == 0 && n + 1 < qs.size()) {\n\t\t\t\t\t++n;\n\t\t\t\t}\n\n\t\t\t\tint cnt = 0;\n\n\t\t\t\twhile (n-- > 0) {\n\t\t\t\t\tcnt += qs.front().second;\n\t\t\t\t\tqt.push_back(qs.front());\n\t\t\t\t\tqs.pop_front();\n\t\t\t\t}\n\n\t\t\t\tres.emplace_back(cnt, 0);\n\t\t\t} else if (qs.size() == 1) {\n\t\t\t\tint n = qt.size() / 2;\n\t\t\t\tif (n % 2 == 0 && n + 1 < qt.size()) {\n\t\t\t\t\t++n;\n\t\t\t\t}\n\t\t\t\tint cnt = 0;\n\n\t\t\t\tchar c = qs.front().first;\n\t\t\t\tint cnt1 = qs.front().second;\n\t\t\t\tqs.pop_front();\n\t\t\t\twhile (n-- > 0) {\n\t\t\t\t\tcnt += qt.front().second;\n\t\t\t\t\tif (n == 0) {\n\t\t\t\t\t\tif (c == qt.front().first) {\n\t\t\t\t\t\t\tqs.emplace_back(c, qt.front().second + cnt1);\n\t\t\t\t\t\t\tqt.pop_front();\n\t\t\t\t\t\t\tcnt1 = 0;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tqs.push_back(qt.front());\n\t\t\t\t\t\t\tqt.pop_front();\n\t\t\t\t\t\t\tqt.front().second += cnt1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tqs.push_back(qt.front());\n\t\t\t\t\t}\n\t\t\t\t\tqt.pop_front();\n\t\t\t\t}\n\n\t\t\t\tres.emplace_back(cnt1, cnt);\n\t\t\t} else if (qt.size() == 1) {\n\t\t\t\tint n = qs.size() / 2;\n\t\t\t\tif (n % 2 == 0 && n + 1 < qs.size()) {\n\t\t\t\t\t++n;\n\t\t\t\t}\n\t\t\t\tint cnt = 0;\n\n\t\t\t\tchar c = qt.front().first;\n\t\t\t\tint cnt1 = qt.front().second;\n\t\t\t\tqt.pop_front();\n\n\t\t\t\twhile (n-- > 0) {\n\t\t\t\t\tcnt += qs.front().second;\n\n\t\t\t\t\tif (n == 0) {\n\t\t\t\t\t\tif (qs.front().first == c) {\n\t\t\t\t\t\t\tqt.emplace_back(c, qs.front().second + cnt1);\n\t\t\t\t\t\t\tqs.pop_front();\n\t\t\t\t\t\t\tcnt1 = 0;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tqt.push_back(qs.front());\n\t\t\t\t\t\t\tqs.pop_front();\n\t\t\t\t\t\t\tqs.front().second += cnt1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tqt.push_back(qs.front());\n\t\t\t\t\t}\n\t\t\t\t\tqs.pop_front();\n\t\t\t\t}\n\n\t\t\t\tres.emplace_back(cnt, cnt1);\n\t\t\t} \n\t\t\telse if (qs.front().first == qt.front().first) {\n\t\t\t\t\t// if (qs.size() >= qt.size()) {\n\t\t\t\t\t// \tres.emplace_back(qs.front().second, 0);\n\t\t\t\t\t// \tqt.front().second += qs.front().second;\n\t\t\t\t\t// \tqs.pop();\n\t\t\t\t\t// } else {\n\t\t\t\t\t// \tres.emplace_back(0, qt.front().second);\n\t\t\t\t\t// \tqs.front().second += qt.front().second;\n\t\t\t\t\t// \tqt.pop();\n\t\t\t\t\t// }\n\n\t\t\t\t\tif (qs.size() >= 2 && qt.size() >= 2) {\n\t\t\t\t\t\tif (qs.size() >= qt.size()) {\n\t\t\t\t\t\t\tauto p1 = qs.front();\n\t\t\t\t\t\t\tqs.pop_front();\n\t\t\t\t\t\t\tauto p2 = qs.front();\n\t\t\t\t\t\t\tqs.pop_front();\n\n\t\t\t\t\t\t\tauto p3 = qt.front();\n\t\t\t\t\t\t\tqt.pop_front();\n\n\t\t\t\t\t\t\tif (qs.empty()) {\n\t\t\t\t\t\t\t\tqs.push_front(p3);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tqs.front().second += p3.second;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tqt.front().second += p2.second;\n\t\t\t\t\t\t\tqt.push_front(p1);\n\n\t\t\t\t\t\t\tres.emplace_back(p1.second + p2.second, p3.second);\n\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tauto p1 = qs.front();\n\t\t\t\t\t\t\tqs.pop_front();\n\n\t\t\t\t\t\t\tauto p2 = qt.front();\n\t\t\t\t\t\t\tqt.pop_front();\n\t\t\t\t\t\t\tauto p3 = qt.front();\n\t\t\t\t\t\t\tqt.pop_front();\n\n\t\t\t\t\t\t\tqs.front().second += p3.second;\n\t\t\t\t\t\t\tqs.push_front(p2);\n\n\t\t\t\t\t\t\tqt.front().second += p1.second;\n\n\t\t\t\t\t\t\tres.emplace_back(p1.second, p2.second + p3.second);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tif (qs.size() >= qt.size()) {\n\t\t\t\t\t\t\tres.emplace_back(qs.front().second, 0);\n\t\t\t\t\t\t\tqt.front().second += qs.front().second;\n\t\t\t\t\t\t\tqs.pop_front();\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tres.emplace_back(0, qt.front().second);\n\t\t\t\t\t\t\tqs.front().second += qt.front().second;\n\t\t\t\t\t\t\tqt.pop_front();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// res.emplace_back(qs.front().second, qt.front().second);\n\n\t\t\t\t\t// char c = qs.front().first;\n\t\t\t\t\t// int cnt1 = qs.front().second;\n\t\t\t\t\t// int cnt2 = qt.front().second;\n\t\t\t\t\t// qs.pop();\n\t\t\t\t\t// qt.pop();\n\n\t\t\t\t\t// if (qs.empty()) {\n\t\t\t\t\t// \tqs.emplace(c, cnt2);\n\t\t\t\t\t// } else {\n\t\t\t\t\t// \tqs.front().second += cnt2;\n\t\t\t\t\t// }\n\n\t\t\t\t\t// if (qt.empty()) {\n\t\t\t\t\t// \tqt.emplace(c, cnt1);\n\t\t\t\t\t// } else {\n\t\t\t\t\t// \tqt.front().second += cnt1;\n\t\t\t\t\t// }\n\n\t\t\t\t\tif (qs.size() > 3 && qt.size() == 2) {\n\t\t\t\t\t\tauto p1 = qs.front(); qs.pop_front();\n\t\t\t\t\t\tauto p2 = qs.front(); qs.pop_front();\n\t\t\t\t\t\tauto p3 = qs.front(); qs.pop_front();\n\n\t\t\t\t\t\tauto p4 = qt.front(); qt.pop_front();\n\n\t\t\t\t\t\tres.emplace_back(p1.second + p2.second + p3.second, p4.second);\n\t\t\t\t\t\tqs.front().second += p4.second;\n\n\t\t\t\t\t\tqt.front().second += p3.second;\n\t\t\t\t\t\tqt.push_front(p2);\n\t\t\t\t\t\tqt.push_front(p1);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t} else if (qs.size() == 2 && qt.size() > 3) {\n\t\t\t\t\t\tauto p1 = qs.front(); qs.pop_front();\n\n\t\t\t\t\t\tauto p2 = qt.front(); qt.pop_front();\n\t\t\t\t\t\tauto p3 = qt.front(); qt.pop_front();\n\t\t\t\t\t\tauto p4 = qt.front(); qt.pop_front();\n\n\t\t\t\t\t\tres.emplace_back(p1.second, p2.second + p3.second + p4.second);\n\n\t\t\t\t\t\tqs.front().second += p4.second;\n\t\t\t\t\t\tqs.push_front(p3);\n\t\t\t\t\t\tqs.push_front(p2);\n\n\n\t\t\t\t\t\tqt.front().second += p1.second;\n\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t} \n\n\t\t\t\t\tint cnt = qs.front().second;\n\t\t\t\t\tint cnt1 = qt.front().second;\n\n\t\t\t\t\tres.emplace_back(cnt, cnt1);\n\t\t\t\t\tqs.pop_front();\n\t\t\t\t\tqt.pop_front();\n\n\t\t\t\t\tqs.front().second += cnt1;\n\t\t\t\t\tqt.front().second += cnt;\n\t\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};\n\n\nint main(int argc, char** argv) {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\t// int n;\n\t// cin >> n;\n\n\tstring s, t;\n\tcin >> s;\n\tcin >> t;\n\n\tSolution sol;\n\n\tauto res = sol.construct(s, t);\n\n\tcout << res.size() << endl;\n\tfor (auto& p : res) {\n\t\tcout << p.first << \" \" << p.second << endl;\n\t}\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["strings", "constructive algorithms"], "code_uid": "7f123270b651ad991ca6ee2fd587b31b", "src_uid": "4a50c4147becea13946272230f3dde6d", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nconst int MAXN=5e5+5;\n\nstruct K{\n int col,len;\n K(int _c=0,int _l=0){\n col=_c,len=_l;\n }\n bool operator ==(const K &b)const{\n return col==b.col;\n }\n};\nstruct ANS{\n int l,r;\n ANS(int _l=0,int _r=0){\n l=_l,r=_r;\n }\n}a[MAXN];\nint acnt;\ndeque Ks[4];\nchar s[4][MAXN];\nint slen[4];\nvoid cal(int id){\n int last=0,lcol=s[id][0]-'a';\n for(int i=1;i tmp[4];\nvoid change(int l,int r){\n for(int i=0;iKs[1].size())\n change(3,2);\n else\n change(2,3);\n }\n else\n change(1,1);\n }\n }\n printf(\"%d\\n\",acnt);\n for(int i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define rep(i,x,y) for(int i=(x);i<=(y);i++)\n#define dep(i,x,y) for(int i=(x);i>=(y);i--)\n#define min(x,y) ((x)<(y)?(x):(y))\n#define max(x,y) ((x)>(y)?(x):(y))\n#define abs(x) ((x)<0?-(x):(x))\n#define add(x,y) x=(x+(y))%mo\n#define sqr(x) ((x)*(x))\n#define gc getchar()\n#define N 1000000\n#define ll long long\n#define inf 1000000000\n#define Inf 1000000000000000000ll\n#define mo 1000000007\n#define eps 1e-8\nusing namespace std;\nint n,m,nn,mm,x,y,r,a1,a2,p[N],q[N];\nchar s[N],t[N],ch;\nint main(){\n\tscanf(\"%s\",s+1);scanf(\"%s\",t+1);ch='a';\n\trep(i,1,500000){\n\t\tif(s[i]!=ch){\n\t\t\tn++;p[n]=x;x=0;ch='a'+'b'-ch;\n\t\t}x++;\n\t\tif(!s[i])break;\n\t}ch='b';x=0;\n\trep(i,1,500000){\n\t\tif(t[i]!=ch){\n\t\t\tm++;q[m]=x;x=0;ch='a'+'b'-ch;\n\t\t}x++;\n\t\tif(!t[i])break;\n\t}\n\tif((!p[1])&&(!q[1])){\n\t\tn--;rep(i,1,n)p[i]=p[i+1];\n\t\tm--;rep(i,1,m)q[i]=q[i+1];\n\t}\n\tnn=n;mm=m;\n\tif(m-n>2){\n\t\tr=(m-n+1)/4*2;mm-=r+1;\n\t\tnn+=r-1;if(nn==r)nn++;\n\t}else if(n-m>2){\n\t\tr=(n-m+1)/4*2;nn-=r+1;\n\t\tmm+=r-1;if(mm==r)mm++;\n\t}\n\tif(abs(nn-mm)>1){\n\t\tif(!p[1]){\n\t\t\tn--;rep(i,1,n)p[i]=p[i+1];\n\t\t\tm++;dep(i,m+1,2)q[i]=q[i-1];q[1]=0;\n\t\t}else if(!q[1]){\n\t\t\tm--;rep(i,1,m)q[i]=q[i+1];\n\t\t\tn++;dep(i,n+1,2)p[i]=p[i-1];p[1]=0;\n\t\t}\n\t}r=0;\n\tif(m-n>2){\n\t\tr=(m-n+1)/4*2;x=0;\n\t\trep(i,1,r+1)x+=q[i];\n\t\ta1=p[1];a2=x;x=p[1];y=q[r+1];\n\t\tdep(i,n+r-1,r+1)p[i]=p[i-r+1];\n\t\trep(i,1,r)p[i]=q[i];\n\t\trep(i,1,m-r-1)q[i]=q[i+r+1];\n\t\tp[r+1]+=y;q[1]+=x;m-=r+1;\n\t\tn+=r-1;if(n==r)n++;\n\t}else if(n-m>2){\n\t\tr=(n-m+1)/4*2;x=0;\n\t\trep(i,1,r+1)x+=p[i];\n\t\ta1=x;a2=q[1];x=q[1];y=p[r+1];\n\t\tdep(i,m+r-1,r+1)q[i]=q[i-r+1];\n\t\trep(i,1,r)q[i]=p[i];\n\t\trep(i,1,n-r-1)p[i]=p[i+r+1];\n\t\tq[r+1]+=y;p[1]+=x;n-=r+1;\n\t\tm+=r-1;if(m==r)m++;\n\t}x=y=0;\n\tprintf(\"%d\\n\",(r>0)+max(n,m)-1);\n\tif(r)printf(\"%d %d\\n\",a1,a2);\n\tp[n+1]=p[n+2]=q[m+1]=q[m+2]=0;\n\trep(i,1,max(n,m)-1){\n\t\tx+=p[i];y+=q[i];\n\t\tprintf(\"%d %d\\n\",x,y);\n\t\tx^=y;y^=x;x^=y;\n\t}\n}", "lang_cluster": "C++", "tags": ["strings", "constructive algorithms"], "code_uid": "51923ba37e6c04f835970bb91c7f9da4", "src_uid": "4a50c4147becea13946272230f3dde6d", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "\n #include \nusing namespace std;\nconst long long MAX=2e9, N=5e3+10, MOD = 1e9+7;\n#define ll long long\n#define pi pair\n#define ft first\n#define sd second\n#define mp make_pair\n#define pb push_back\n\n#define VI vector\n#define VPI vector\n#define VL vector\n#define int ll\n#define PI 3.14159265\n#define sz(a) (int)(a.size())\nstring s, t;\nint n;\n\nint bad(string &s, char c){\n\tint an = s.back() != c;\n\tfor(int i=1; i > &an){\n\ttrm(a, b);\n\twhile (!a.empty() || !b.empty()){\n\t\tan.push_back(make_pair(a.size(), b.size()));\n\t\tswap(a, b);\n\t\ttrm(a, b);\n\t}\n}\n\nvector > go(string a, string b){\n\tvector > an;\n\tint abad = bad(a, 'a'), bbad = bad(b, 'b');\n\tint df = abs(abad - bbad);\n\tint need = df >> 1;\n\tif (need == 0)kil(a, b, an);\n\telse {\n\t\tstring *bg = &a, *sm = &b;\n\t\tif (abad < bbad)swap(bg, sm);\n\t\tint bgn = ind(need, *bg);\n\t\tif (df & 1 && (*bg)[bgn - 1] != (*sm)[0])++need;\n\t\tstring na, nb;\n\t\tint lsm = 0, lbg = need;\n\t\tif (bbad > 0)++lsm, ++lbg;\n\t\tif (sm == &a){\n\t\t\tint inda = ind(lsm, a), indb = ind(lbg, b);\n\t\t\tna = b.substr(0, indb) + a.substr(inda);\n\t\t\tnb = a.substr(0, inda) + b.substr(indb);\n\t\t\tan.push_back(make_pair(inda, indb));\n\t\t}else {\n\t\t\tint inda = ind(lbg, a), indb = ind(lsm, b);\n\t\t\tna = b.substr(0, indb) + a.substr(inda);\n\t\t\tnb = a.substr(0, inda) + b.substr(indb);\n\t\t\tan.push_back(make_pair(inda, indb));\n\t\t}\n\t\tkil(na, nb, an);\n\t}\n\treturn an;\n}\n\n#undef int\nint main()\n{\n #define int ll\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cerr.tie(0);\n cin>>s>>t;\n n=sz(s);\n VPI res1=go(s, t);\n// if(n>20)\n// cout<<\"zmiana\";\n VPI res2=go(t, s);\n cout<\n\nusing namespace std;\n\nstring a, b;\nvector l1, l2;\n\nint get_cnt(const string &s) {\n\tfor (int i = 0; i < (int) s.size(); i++) {\n\t\tif (s[i] != s[0]) return i;\n\t}\n\treturn (int) s.size();\n}\n\nvoid op(int c1, int c2) {\n\tl1.push_back(c1);\n\tl2.push_back(c2);\n\tstring ra = a.substr(0, c1);\n\tstring rb = b.substr(0, c2);\n\ta = rb + a.substr(c1);\n\tb = ra + b.substr(c2);\n}\n\ndeque getv(const string &s) {\n\tdeque res;\n\tfor (int i = 0; i < (int) s.size(); ) {\n\t\tint pos = i;\n\t\twhile (pos < (int) s.size() && s[pos] == s[i]) pos++;\n\t\tres.push_back(pos - i);\n\t\ti = pos;\n\t}\n\treturn res;\n}\n\nint solve() {\n\tint s1 = 0, s2 = 0;\n\tdeque v1 = getv(a);\n\tdeque v2 = getv(b);\n\tbool f = true;\n\twhile (1) {\n\t\tint c1 = v1.front(), c2 = v2.front();\n\t\tif ((int) v1.size() == 1 && (int) v2.size() == 1) break;\n\t\tif (f && (int) a.size() && (int) b.size() && a[0] == b[0]) {\n\t\t\tl1.push_back(c1);\n\t\t\tl2.push_back(0);\n\t\t\tv1.pop_front();\n\t\t\ts2 += c1;\n\t\t}\n\t\telse {\n\t\t\tl1.push_back(s1 + c1);\n\t\t\tl2.push_back(s2 + c2);\n\t\t\tv1.pop_front(); v2.pop_front();\n\t\t\tif (v1.empty()) v1.push_back(0);\n\t\t\tif (v2.empty()) v2.push_back(0);\n\t\t\tswap(s1, s2);\n\t\t\ts1 += c2; s2 += c1;\n\t\t}\n\t\tf = false;\n\t}\n\treturn (int) l1.size();\n}\n\nint get_l(const string &s, int num) {\n\tif (num == 0) return 0;\n\tint res = 0;\n\tfor (int i = 0; i < (int) s.size(); ) {\n\t\tint pos = i;\n\t\twhile (s[pos] == s[i]) pos++;\n\t\ti = pos;\n\t\tres++;\n\t\tif (res == num) return pos;\n\t}\n}\n\nvoid first_op(int c1, int c2) {\n\top(get_l(a, c1), get_l(b, c2));\n}\n\nint sol(const string &s) {\n\tint res = 0;\n\tfor (int i = 0; i < (int) s.size(); ) {\n\t\tint pos = i;\n\t\twhile (pos < (int) s.size() && s[pos] == s[i]) pos++;\n\t\ti = pos;\n\t\tres++;\n\t}\n\treturn res;\n}\n\nbool inv;\n\nint main() {\n\tios::sync_with_stdio(0); cin.tie(0);\n\t\n\tcin >> a >> b;\n\tint c1 = sol(a), c2 = sol(b);\n\tif (c1 < c2) {\n\t\tswap(a, b); swap(c1, c2); inv = true;\n\t}\n\tif (a[0] == b[0]) {\n\t\tint len = 0;\n\t\twhile (1) {\n\t\t\tif (len == 0) len = 1, c1--;\n\t\t\telse len += 2, c1 -= 2, c2 += 2;\n\t\t\tif (c1 <= c2 + 1) break;\n\t\t}\n\t\tint len2 = 0;\n\t\tif (c1 + 2 == c2) len--, len2++;\n\t\tfirst_op(len, len2);\n\t}\n\telse if (c1 > c2 + 2) {\n\t\tint sum = (c1 + c2 - 2) / 2;\n\t\tif (c1 % 2 != c2 % 2) {\n\t\t\tif ((c1 - c2) % 4 == 1) first_op(c1 - sum - 1, 1);\n\t\t\telse first_op(c1 - sum, 1);\n\t\t}\n\t\telse first_op(c1 - sum, 1);\n\t}\n\t\n\tsolve();\n\tcout << l1.size() << '\\n';\n\tfor (int i = 0; i < (int) l1.size(); i++) {\n\t\tif (inv) swap(l1[i], l2[i]);\n\t\tcout << l1[i] << ' ' << l2[i] << '\\n';\n\t}\n}\n", "lang_cluster": "C++", "tags": ["strings", "constructive algorithms"], "code_uid": "b29a504ad5a8ef485dee2c4d5be25cf2", "src_uid": "4a50c4147becea13946272230f3dde6d", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nstring s, t;\nvector< pair > a, b;\nvector< pair > bleach;\nbool flag = false;\n\nvoid add(vector< pair > &tren, pair s) {\n if (!tren.empty() && tren.back().first == s.first) tren.back().second += s.second;\n else tren.push_back(s);\n}\n\ninline void addchar(vector< pair > &tren, char s) {\n add(tren, make_pair(s, 1));\n}\n\nint pop(vector< pair > &tren, int kol) {\n int sol = 0;\n for (int i = 0; i < kol; i++) {\n sol += tren.back().second;\n tren.erase(tren.end() - 1);\n }\n return sol;\n}\n\nvoid query(int acx, int acy) {\n //printf(\"query: %d %d\\n\", acx, acy);\n vector< pair > ta, tb;\n for (int i = acx - 1; i >= 0; i--) ta.push_back(a[a.size() - 1 - i]);\n for (int i = acy - 1; i >= 0; i--) tb.push_back(b[b.size() - 1 - i]);\n int kolx = pop(a, acx);\n int koly = pop(b, acy);\n if (flag) swap(kolx, koly);\n bleach.push_back(make_pair(kolx, koly));\n\n for (int i = 0; i < ta.size(); i++) add(b, ta[i]);\n for (int i = 0; i < tb.size(); i++) add(a, tb[i]);\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cin >> s >> t;\n\n reverse(s.begin(), s.end());\n reverse(t.begin(), t.end());\n for (int i = 0; i < s.size(); i++) addchar(a, s[i]);\n for (int i = 0; i < t.size(); i++) addchar(b, t[i]);\n\n while (a.size() != 1 || b.size() != 1) {\n int la = a.size();\n int lb = b.size();\n\n if (la < lb) {\n swap(la, lb);\n swap(a, b);\n flag ^= true;\n }\n const char kuraca = a.back().first;\n const char kuracb = b.back().first;\n\n //printf(\"%d %d -> %c %c\\n\", la, lb, kuraca, kuracb);\n //printf(\"%d\\n\", la + lb);\n if (kuraca != kuracb) {\n if (lb <= 2) {\n if (la <= 3) query(1, 1);\n else query(3, 1);\n } else query(1, 1);\n } else {\n if (lb == 1) {\n if (la <= 3) query(1, 0);\n else query(3, 0);\n } else if (lb == 2) {\n if (la == 2) query(1, 0);\n else query(2, 1);\n } else query(3, 2);\n }\n //flag = false;\n }\n\n printf(\"%d\\n\", bleach.size());\n for (int i = 0; i < bleach.size(); i++)\n printf(\"%d %d\\n\", bleach[i].first, bleach[i].second);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["strings", "constructive algorithms"], "code_uid": "5819f38e740c625b80f1987249bc0eda", "src_uid": "4a50c4147becea13946272230f3dde6d", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n author: Maksim1744\n created: 01.11.2020 17:50:30\n*/\n\n#include \n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n\n#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))\n#define mine(a) (*min_element((a).begin(), (a).end()))\n#define maxe(a) (*max_element((a).begin(), (a).end()))\n#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())\n#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())\n#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())\n#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())\n\ntemplate vector& operator-- (vector& v){for (auto& i : v) --i; return v;}\ntemplate vector& operator++ (vector& v){for (auto& i : v) ++i; return v;}\ntemplate istream& operator>>(istream& is, vector& v){for (auto& i : v) is >> i; return is;}\ntemplate ostream& operator<<(ostream& os, vector& v){for (auto& i : v) os << i << ' '; return os;}\ntemplate istream& operator>>(istream& is, pair& p){is >> p.first >> p.second; return is;}\ntemplate ostream& operator<<(ostream& os, pair& p){os << p.first << ' ' << p.second; return os;}\ntemplate pair operator-(pair a, pair b){return mp(a.first-b.first, a.second-b.second);}\ntemplate pair operator+(pair a, pair b){return mp(a.first+b.first, a.second+b.second);}\ntemplate void umin(T& a, U b){if (a > b) a = b;}\ntemplate void umax(T& a, U b){if (a < b) a = b;}\n\n#ifdef HOME\n#define TAG_LENGTH 25\n#define LR_LEFT left\n#define LR_RIGHT right\n#define LR_VALUE value\n#define LR_SECOND_VALUE mn\n#include \"C:/C++ libs/print.cpp\"\n#else\n#define showl 42;\n#define shows 42;\n#define show(...) 42;\n#define showm(...) 42;\n#define showt(...) 42;\n#define printTree(...) 42;\n#define printGraph(...) 42;\n#define printLRTree(...) 42;\n#define printMatrix(...) 42;\n#define printWGraph(...) 42;\n#define debug if (false)\n#endif\n\nauto start_time = clock();\n\nint a[3005][3005];\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n\n int n, k;\n cin >> n >> k;\n vector sz(n);\n for (int i = 0; i < n; ++i) {\n int t;\n cin >> t;\n for (int j = 0; j < t; ++j) {\n int u;\n cin >> u;\n if (j <= k) a[i][j] = u;\n }\n sz[i] = min(k, t);\n }\n\n // cerr << (double)(clock() - start_time) / CLOCKS_PER_SEC << endl;\n vector> pref(n);\n for (int i = 0; i < n; ++i) {\n pref[i].reserve(sz[i]);\n pref[i].pb(a[i][0]);\n for (int j = 1; j < sz[i]; ++j) {\n pref[i].pb(pref[i].back() + a[i][j]);\n }\n }\n\n // cerr << (double)(clock() - start_time) / CLOCKS_PER_SEC << endl;\n\n vector> pos(n, vector(k + 1));\n\n vector> v;\n v.reserve(n + k);\n for (int ln = 1; ln <= k; ++ln) {\n v.clear();\n for (int i = 0; i < n; ++i) {\n if (pref[i].size() >= ln)\n v.eb(pref[i][ln - 1], i);\n }\n sort(v.rbegin(), v.rend());\n for (int i = 0; i < v.size(); ++i) {\n pos[v[i].second][ln] = i;\n }\n }\n\n // cerr << (double)(clock() - start_time) / CLOCKS_PER_SEC << endl;\n\n vector> only_full(n + 1, vector(k + 1, 0)), partial(n + 1, vector(k + 1, 0));\n for (int i = 0; i < n; ++i) {\n ll sm = pref[i].back();\n only_full[i + 1] = only_full[i];\n partial[i + 1] = partial[i];\n for (int j = k - (int)sz[i]; j >= 0; --j) {\n only_full[i + 1][j + sz[i]] = max(only_full[i + 1][j + sz[i]], only_full[i][j] + sm);\n partial[i + 1][j + sz[i]] = max(partial[i + 1][j + sz[i]], partial[i][j] + sm);\n }\n for (int ln = 1; ln <= sz[i]; ++ln) {\n int cbefore = pos[i][ln - 1] * ln;\n if (cbefore > k) continue;\n for (int j = 0; j + ln <= k; ++j)\n partial[i + 1][j + ln] = max(partial[i + 1][j + ln], only_full[i][j] + pref[i][ln - 1]);\n }\n }\n // shows;\n // printMatrix(only_full);\n // shows;\n // printMatrix(partial);\n // shows;\n cout << max(only_full[n][k], partial[n][k]) << '\\n';\n\n // cerr << (double)(clock() - start_time) / CLOCKS_PER_SEC << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "cf8d7d75e0e6cdb15b7084df8bfe21f8", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// Created by Nikolay Budin\n\n#ifdef DEBUG\n# define _GLIBCXX_DEBUG\n#endif\n#include \n#include \n#define ff first\n#define ss second\n#define szof(x) ((int)x.size())\n#ifndef LOCAL\n# define cerr __get_ce\n#endif\n\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\ntypedef unsigned long long ull;\n\nusing namespace __gnu_pbds;\ntemplate using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\ntemplate using ordered_map = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\nint const INF = (int)1e9 + 1e3;\nll const INFL = (ll)1e18 + 1e6;\n#ifdef LOCAL\n\tmt19937 tw(9450189);\n#else\n\tmt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());\n#endif\nuniform_int_distribution ll_distr;\nll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }\n\n\nvoid solve() {\n\tint n, k;\n\tcin >> n >> k;\n\tvector> arr;\n\tvector> pref;\n\tfor (int i = 0; i < n; ++i) {\n\t\tint t;\n\t\tcin >> t;\n\t\tarr.push_back({});\n\t\tfor (int j = 0; j < t; ++j) {\n\t\t\tint num;\n\t\t\tcin >> num;\n\t\t\tarr[i].push_back(num);\n\t\t}\n\t\tif (szof(arr[i]) > k) {\n\t\t\tarr[i].resize(k);\n\t\t}\n\t\tpref.push_back({0});\n\t\tfor (int num : arr[i]) {\n\t\t\tpref[i].push_back(pref[i].back() + num);\n\t\t}\n\t}\n\n\tint bpv = 1;\n\tint bp = 1;\n\twhile (bpv < n) {\n\t\tbpv *= 2;\n\t\t++bp;\n\t}\n\n\tvector>> here(bpv * 2);\n\n\tauto add_seg = [&](int l, int r, int sz, ll val) {\n\t\tl += bpv; r += bpv;\n\t\twhile (l < r) {\n\t\t\tif (l & 1) {\n\t\t\t\there[l++].push_back({sz, val});\n\t\t\t}\n\t\t\tif (r & 1) {\n\t\t\t\there[--r].push_back({sz, val});\n\t\t\t}\n\t\t\tl /= 2; r /= 2;\n\t\t}\n\t};\n\n\tfor (int i = 0; i < n; ++i) {\n\t\tadd_seg(0, i, szof(arr[i]), pref[i].back());\n\t\tadd_seg(i + 1, n, szof(arr[i]), pref[i].back());\n\t}\n\n\tvector> mem_layers(bp, vector(k + 1));\n\tvector dp(k + 1, -INFL);\n\tdp[0] = 0;\n\tll ans = 0;\n\n\tfunction dfs = [&](int v, int vl, int vr, int d) {\n\t\tcopy(dp.begin(), dp.end(), mem_layers[d].begin());\n\t\tfor (auto obj : here[v]) {\n\t\t\tfor (int i = k - obj.ff; i >= 0; --i) {\n\t\t\t\tdp[i + obj.ff] = max(dp[i + obj.ff], dp[i] + obj.ss);\n\t\t\t}\n\t\t}\n\t\tif (vl + 1 == vr) {\n\t\t\tif (vl < n) {\n\t\t\t\t// cerr << vl << endl;\n\t\t\t\t// for (auto num : dp) {\n\t\t\t\t// \tcerr << num << \" \";\n\t\t\t\t// }\n\t\t\t\t// cerr << endl;\n\t\t\t\tfor (int i = 0; i < szof(pref[vl]); ++i) {\n\t\t\t\t\tans = max(ans, dp[k - i] + pref[vl][i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcopy(mem_layers[d].begin(), mem_layers[d].end(), dp.begin());\n\t\t\treturn;\n\t\t}\n\n\t\tint vm = (vl + vr) / 2;\n\t\tdfs(v * 2, vl, vm, d + 1);\n\t\tdfs(v * 2 + 1, vm, vr, d + 1);\n\t\tcopy(mem_layers[d].begin(), mem_layers[d].end(), dp.begin());\n\t};\n\n\tdfs(1, 0, bpv, 0);\n\n\tcout << ans << \"\\n\";\n}\n\n\nint main() {\n#ifdef LOCAL\n\tauto start_time = clock();\n\tcerr << setprecision(3) << fixed;\n#endif\n\tcout << setprecision(15) << fixed;\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\n\tint test_count = 1;\n\t// cin >> test_count;\n\tfor (int test = 1; test <= test_count; ++test) {\n\t\tsolve();\n\t}\n\n#ifdef LOCAL\n\tauto end_time = clock();\n\tcerr << \"Execution time: \" << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << \" ms\\n\";\n#endif\n}", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "23855e5734b340d168f98b3070aaeada", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#ifndef LOCAL\n//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native\")\n#endif\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#ifdef LOCAL\n\t#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n\t#define eprintf(...) 42\n#endif\n\ntypedef long long int int64;\n\n\nconst int K = 3000;\nconst int N = 3005;\n\nint sz[N];\nint a[N][N];\nlong long sum[N];\n\n\nstruct Seg\n{\n int l, r, id;\n\n Seg() : l(), r(), id() {}\n\n Seg(int _l, int _r, int _id) : l(_l), r(_r), id(_id) {}\n};\n\nstruct Dp\n{\n long long dp[N];\n\n Dp()\n {\n memset(dp, 0, sizeof dp);\n }\n\n void operator += (int id)\n {\n for (int i = N - sz[id] - 1; i >= 0; i--)\n dp[i + sz[id]] = max(dp[i + sz[id]], dp[i] + sum[id]);\n }\n};\n\nint k;\n\nlong long ans = 0;\n\nvoid solve(int l, int r, vector &segs, Dp &dp)\n{\n if (l == r)\n {\n long long psum = 0;\n for (int i = 0; i <= min(k, sz[l]); i++)\n {\n if (i != 0)\n psum += a[l][i - 1];\n ans = max(ans, psum + dp.dp[k - i]);\n }\n return;\n }\n int m = (l + r) / 2;\n vector lsegs, rsegs;\n Dp ldp = dp, rdp = dp;\n\n for (Seg seg : segs)\n {\n if (seg.l <= l && seg.r >= m)\n ldp += seg.id;\n else if (seg.l <= m)\n lsegs.push_back(seg);\n\n if (seg.l <= m + 1 && seg.r >= r)\n rdp += seg.id;\n else if (seg.r >= m + 1)\n rsegs.push_back(seg);\n }\n\n solve(l, m, lsegs, ldp);\n solve(m + 1, r, rsegs, rdp);\n\n}\n\nvector segs;\n\nvoid addSeg(int l, int r, int id)\n{\n segs.push_back(Seg(l, r, id));\n}\n\nint main(int, char **)\n{\n#ifdef LOCAL\n//\tfreopen(\"input.txt\", \"r\", stdin);\n//\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n\n int n;\n scanf(\"%d%d\", &n, &k);\n for (int i = 0; i < n; i++)\n {\n int t;\n scanf(\"%d\", &t);\n for (int j = 0; j < t; j++)\n {\n int x;\n scanf(\"%d\", &x);\n if (j < K)\n {\n sum[i] += x;\n a[i][j] = x;\n }\n }\n sz[i] = min(t, K);\n }\n\n for (int i = 0; i < n; i++)\n {\n if (i != 0)\n addSeg(0, i - 1, i);\n if (i != n - 1)\n addSeg(i + 1, n - 1, i);\n }\n\n Dp dp = Dp();\n solve(0, n - 1, segs, dp);\n\n printf(\"%lld\\n\", ans);\n\n\treturn 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "ee8b30e96e46fd3528587e7dc0ab2655", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//#pragma comment(linker, \"/stack:200000000\")\n//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n//#pragma GCC optimize(\"unroll-loops\")\n \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n// #include \"atcoder/convolution.hpp\"\n// #include \"atcoder/dsu.hpp\"\n// #include \"atcoder/fenwicktree.hpp\"\n// #include \"atcoder/lazysegtree.hpp\"\n// #include \"atcoder/math.hpp\"\n// #include \"atcoder/maxflow.hpp\"\n// #include \"atcoder/mincostflow.hpp\"\n// #include \"atcoder/modint.hpp\"\n// #include \"atcoder/scc.hpp\"\n// #include \"atcoder/segtree.hpp\"\n// #include \"atcoder/string.hpp\"\n// #include \"atcoder/twosat.hpp\"\n\n#define uint unsigned int\n#define ll long long\n#define ull unsigned long long\n#define ld long double\n#define rep(i, l, r) for (int i = (l); i < (r); i++)\n#define repb(i, r, l) for (int i = (r); i > (l); i--)\n#define sz(a) (int)a.size()\n#define fi first\n#define se second\n#define mp(a, b) make_pair(a, b)\n#define rank qwertyuio\n#define next dfghjk\n#define prev fhsgfhjf\n#define plus fsghsf\n#define minus ytryr\n \nusing namespace std;\n \ninline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }\ninline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }\ninline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }\ninline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }\n \nconst int N = 3000 + 13;\nconst int inf = (int)1e9 + 1;\nconst ll big = (ll)1e18 + 1;\nconst int P = 239;\nconst int P1 = 31;\nconst int P2 = 57;\nconst int MOD = (int)1e9 + 7;\nconst int MOD1 = (int)1e9 + 9;\nconst int MOD2 = 998244353;\nconst ld eps = 1e-9;\nconst double pi = atan2(0, -1);\nconst int ABC = 26;\n\nint n, k;\n\nvector a[N];\nll sum[N];\n\nll ans = 0;\n\nll dp[N][N];\nint p = 0;\n\nvoid apply(int i) {\n memcpy(dp[p + 1], dp[p], sizeof(ll) * (k + 1));\n int sss = sz(a[i]);\n rep(j, sss, k + 1) {\n setmax(dp[p + 1][j], dp[p][j - sss] + sum[i]);\n }\n p++;\n}\n\nvoid solve(int l, int r) {\n if (r - l == 1) {\n ll tot = 0;\n rep(c, -1, min(k, sz(a[l]))) {\n if (c >= 0) {\n tot += a[l][c];\n }\n setmax(ans, tot + dp[p][k - (c + 1)]);\n }\n return;\n }\n int m = (l + r) / 2;\n int ppp = p;\n rep(i, m, r) {\n apply(i);\n }\n solve(l, m);\n\n p = ppp;\n rep(i, l, m) {\n apply(i);\n }\n solve(m, r);\n}\n\nint main()\n{\n //freopen(\"circus.in\", \"r\", stdin);\n //freopen(\"circus.out\", \"w\", stdout);\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.precision(20);\n cout << fixed;\n //ll TL = 10.95 * CLOCKS_PER_SEC;\n //clock_t time = clock();\n cin >> n >> k;\n rep(i, 0, n) {\n int c;\n cin >> c;\n a[i].resize(c);\n sum[i] = 0;\n rep(j, 0, c) {\n cin >> a[i][j];\n sum[i] += a[i][j];\n }\n }\n dp[p][0] = 0;\n solve(0, n);\n cout << ans << \"\\n\";\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "7979fe016bfc3edf3210d1f5e3e0fa34", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define forn(i, n) for (int i = 0; i < (int)(n); ++i)\n#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)\n#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)\n#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)\n\nusing namespace std;\n\ntypedef pair pii;\ntypedef vector vi;\ntypedef vector vpi;\ntypedef vector vvi;\ntypedef long long i64;\ntypedef vector vi64;\ntypedef vector vvi64;\ntypedef pair pi64;\ntypedef double ld;\n\ntemplate bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }\ntemplate bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }\n\nconst int maxn = 3100, LOG = 15;\nvi64 a[maxn];\ni64 s[maxn];\ni64 dp[LOG][maxn];\n\nint n, k;\n\ni64 ans = 0;\n\nvoid dnc(int L, int R, int z) {\n if (R - L == 1) {\n i64 pr = 0;\n int ub = min((int)a[L].size(), k);\n forn(i, ub + 1) {\n uax(ans, pr + dp[z][k - i]);\n if (i < ub) pr += a[L][i];\n }\n return;\n }\n int M = (L + R) / 2;\n {\n forn(i, k + 1) dp[z + 1][i] = dp[z][i];\n fore(i, L, M - 1) {\n int sz = a[i].size();\n ford(j, k - sz + 1) uax(dp[z + 1][j + sz], dp[z + 1][j] + s[i]);\n }\n dnc(M, R, z + 1);\n }\n {\n forn(i, k + 1) dp[z + 1][i] = dp[z][i];\n fore(i, M, R - 1) {\n int sz = a[i].size();\n ford(j, k - sz + 1) uax(dp[z + 1][j + sz], dp[z + 1][j] + s[i]);\n }\n dnc(L, M, z + 1);\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.precision(10);\n cout << fixed;\n#ifdef LOCAL_DEFINE\n freopen(\"input.txt\", \"rt\", stdin);\n#endif\n\n cin >> n >> k;\n forn(i, n) {\n int l;\n cin >> l;\n a[i].resize(l);\n for (i64 &x: a[i]) cin >> x, s[i] += x;\n }\n\n forn(i, k + 1) dp[0][i] = -1e18;\n dp[0][0] = 0;\n dnc(0, n, 0);\n cout << ans << '\\n';\n\n#ifdef LOCAL_DEFINE\n cerr << \"Time elapsed: \" << 1.0 * clock() / CLOCKS_PER_SEC << \" s.\\n\";\n#endif\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "2f3be344b145e767af70607e9dfcfabe", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\n\nconst ll INF = (ll)1e18 + 41;\n\nvoid solve() {\n int n, k;\n cin >> n >> k;\n\n vector dp(k + 1, 0);\n vector maxUseless(k + 1, -INF);\n\n vector> prefs(n);\n\n for (int i = 0; i < n; i++) {\n ll sum = 0;\n int sz; cin >> sz;\n vector> pref;\n for (int j = 0; j < sz; j++) {\n int x; cin >> x; sum += x;\n if (j + 1 <= k)\n prefs[i].push_back(sum);\n }\n }\n\n mt19937 rnd(3431);\n shuffle(prefs.begin(), prefs.end(), rnd);\n\n for (int i = 0; i < n; i++) {\n vector> a;\n const auto &pp = prefs[i];\n for (int j = 0; j < (int)pp.size(); j++) if (pp[j] > maxUseless[j + 1]) {\n a.emplace_back(j + 1, pp[j]);\n }\n vector isA(a.size(), false);\n\n for (int pos = k; pos > 0; pos--) {\n ll oldDp = dp[pos];\n for (int j = 0; j < (int)a.size(); j++) {\n int len = a[j].first;\n if (pos < len) continue;\n ll val = a[j].second;\n if (oldDp < dp[pos - len] + val) isA[j] = true;\n if (dp[pos] < dp[pos - len] + val) dp[pos] = dp[pos - len] + val;\n }\n }\n\n for (int j = 0; j < (int)a.size(); j++) {\n if (!isA[j]) {\n maxUseless[a[j].first] = max(maxUseless[a[j].first], a[j].second);\n }\n }\n }\n\n cout << dp[k] << \"\\n\";\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n#ifdef SERT\n freopen(\"a.in\", \"r\", stdin);\n t = 1;\n#endif\n while (t--) solve();\n}", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "57b0250f473eb388e875cd8a967dfacc", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\n#ifdef LOCAL\n #define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define eprintf(...) 42\n#endif\n\nusing ll = long long;\nusing ld = long double;\nusing D = double;\nusing uint = unsigned int;\ntemplate\nusing pair2 = pair;\nusing pii = pair;\nusing pli = pair;\nusing pll = pair;\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define pb push_back\n#define mp make_pair\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n\nconst int maxn = 3005;\nconst int treesize = 1 << 12;\n\nvector> ops[2 * treesize];\nvector ks;\nint n, k;\nint a[maxn][maxn];\nll answer = 0;\nint len[maxn];\nll sum[maxn];\n\nvoid apply(pair t)\n{\n\tfor (int i = k - t.fi; i >= 0; i--) ks[i + t.fi] = max(ks[i + t.fi], ks[i] + t.se);\n}\n\nvoid add(int cur, int cl, int cr, int l, int r, pair t)\n{\n\tif (cl > r || cr < l) return;\n\tif (cl >= l && cr <= r)\n\t{\n\t\tops[cur].pb(t);\n\t\treturn;\n\t}\n\tint cm = (cl + cr) / 2;\n\tadd(cur * 2, cl, cm, l, r, t);\n\tadd(cur * 2 + 1, cm + 1, cr, l, r, t);\n}\n\nvoid go(int cur, int cl, int cr, int l, int r)\n{\n\tif (cl > r) return;\n\tvector was = ks;\n\tfor (auto t : ops[cur]) apply(t);\n\t\n\tif (cl == cr)\n\t{\n\t\tll cursum = 0;\n\t\tfor (int i = 0; i <= len[cl]; i++)\n\t\t{\n\t\t\tanswer = max(answer, ks[k - i] + cursum);\n\t\t\tcursum += a[cl][i];\n\t\t}\n\t} else\n\t{\n\t\tint cm = (cl + cr) / 2;\n\t\tgo(cur * 2, cl, cm, l, r);\n\t\tgo(cur * 2 + 1, cm + 1, cr, l, r);\n\t}\n\t\n\tks = was;\n}\n\nint main()\n{\n\tscanf(\"%d%d\", &n, &k);\n\tfor (int i =0 ; i < n; i++)\n\t{\n\t\tscanf(\"%d\", &len[i]);\n\t\tfor (int j =0 ; j < len[i]; j++)\n\t\t{\n\t\t\tif (j < k) scanf(\"%d\", &a[i][j]);\n\t\t\telse scanf(\"%*d\");\n\t\t}\n\t\tlen[i] = min(len[i], k);\n\t}\n\tfor (int i = 0; i < n; i++) sum[i] = accumulate(a[i], a[i] + len[i], 0LL);\n\tfor (int i =0; i < n; i++)\n\t{\n\t\tadd(1, 0, treesize - 1, 0, i - 1, {len[i], sum[i]});\n\t\tadd(1, 0, treesize - 1, i + 1, n - 1, {len[i], sum[i]});\n\t}\n\tks.resize(k + 1);\n\tgo(1, 0, treesize - 1, 0, n - 1);\n\tcout << answer << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "afff1c64ede0b43d8e7aee38213aa45f", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#pragma GCC optimize(\"Ofast\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include \n\nusing namespace std;\n\n//defines\ntypedef long long ll;\ntypedef long double ld;\n#define TIME clock() * 1.0 / CLOCKS_PER_SEC\n#define y0 _y0\n#define kill _kill\n\n//permanent constants\nconst ld pi = acos(-1.0);\nconst int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\nconst int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\nconst int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};\nconst int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};\nconst int alf = 26;\nconst int two = 2;\nconst ld eps = 1e-10;\nconst ll INF = (ll)(2e18 + 239);\nconst int BIG = (int)(1e9 + 239);\nconst int MOD = 998244353;\nconst ll MOD2 = (ll)MOD * (ll)MOD;\n\n//random\n#ifdef ONPC\nmt19937_64 rnd(239);\n#else\nmt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());\n#endif\n\n//constants\nconst int M = (int)(2e5 + 239);\nconst int N = (int)(2e3 + 10);\nconst int L = 20;\nconst int T = (1 << 20) + 239;\nconst int B = 510;\nconst int X = 3010;\n\nvector v[X];\nint n, k;\nll ans;\n\nvoid func(int l, int r, vector w) {\n if (r - l == 1) {\n for (int i = 0; i < v[l].size(); i++)\n if (w[k - i] != -INF)\n ans = max(ans, w[k - i] + v[l][i]);\n return;\n }\n int mid = (l + r) / 2;\n vector w_go = w;\n for (int i = l; i < mid; i++)\n for (int j = k - (int)v[i].size() + 1; j >= 0; j--)\n if (w_go[j] != -INF)\n w_go[j + (int)v[i].size() - 1] = max(w_go[j + (int)v[i].size() - 1], w_go[j] + v[i].back());\n func(mid, r, w_go);\n w_go = w;\n for (int i = mid; i < r; i++)\n for (int j = k - (int)v[i].size() + 1; j >= 0; j--)\n if (w_go[j] != -INF)\n w_go[j + (int)v[i].size() - 1] = max(w_go[j + (int)v[i].size() - 1], w_go[j] + v[i].back());\n func(l, mid, w_go);\n}\n\nint32_t main()\n{\n#ifdef ONPC\n freopen(\"input\", \"r\", stdin);\n#endif\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n cin >> n >> k;\n for (int i = 0; i < n; i++) {\n int t;\n cin >> t;\n v[i].resize(t + 1);\n v[i][0] = 0;\n for (int j = 0; j < t; j++) {\n ll x;\n cin >> x;\n v[i][j + 1] = v[i][j] + x;\n }\n if (t >= k) {\n v[i].resize(k + 1);\n }\n }\n vector w(k + 1, -INF);\n w[0] = 0;\n ans = 0;\n func(0, n, w);\n cout << ans << \"\\n\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "349e0ec33bebe93657f4b8192afc7246", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef vector vi;\ntypedef vector vl;\ntypedef vector vvi;\ntypedef vector vvl;\ntypedef vector vd;\ntypedef vector vvd;\ntypedef pair pii;\ntypedef pair pdd;\ntypedef vector vii;\ntypedef vector vs;\n//const int mod = ;\n\nconst int N = 1 << 12;\nll d[2][N][3001];\n\nvoid umax(ll& x, ll y) {\n x = max(x, y);\n}\n\nconst int DEBUG = 0;\n\nint main() {\n //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n std::ios::sync_with_stdio(false); std::cin.tie(0);\n //int n = 3000, k = 3000;\n for (int tt = 0; tt < (DEBUG ? 100 : 1); ++tt) {\n int n = 10, k = rand() % 10;\n if (!DEBUG) cin >> n >> k;\n vvi a(n);\n vi l(n);\n vl sum(n);\n for (int i = 0; i < n; ++i) {\n //int t = 300;\n int t = 1 + rand() % 10;\n if (!DEBUG) cin >> t;\n a[i].resize(t);\n l[i] = t;\n for (int j = 0; j < t; ++j) {\n if (!DEBUG) cin >> a[i][j];\n else a[i][j] = j > 0 ? a[i][j - 1] + rand() % 5 : rand() % 2;\n sum[i] += a[i][j];\n }\n }\n int pw = 1;\n while (pw < n) pw *= 2;\n while (l.size() < pw) {\n l.push_back(0);\n sum.push_back(0);\n }\n memset(d[0], 0, sizeof(d[0]));\n int pr = 0, nx = 1;\n while (pw > 1) {\n pw /= 2;\n for (int s = 0; s < l.size(); s += 2 * pw) {\n memcpy(d[nx][s], d[pr][s], sizeof(d[nx][s]));\n memcpy(d[nx][s + pw], d[pr][s], sizeof(d[nx][s]));\n for (int i = s; i < s + pw; ++i) for (int j = k; j >= l[i]; --j) {\n umax(d[nx][s + pw][j], d[nx][s + pw][j - l[i]] + sum[i]);\n }\n for (int i = s + pw; i < s + 2 * pw; ++i) for (int j = k; j >= l[i]; --j) {\n umax(d[nx][s][j], d[nx][s][j - l[i]] + sum[i]);\n }\n }\n swap(pr, nx);\n }\n ll res = 0;\n for (int i = 0; i < n; ++i) {\n ll s = 0;\n for (int j = 0; j < min(k + 1, (int)a[i].size() + 1); ++j) {\n res = max(res, d[pr][i][k - j] + s);\n// cerr << res << ' ' << i << ' ' << j << ' ' << s << ' ' << d[pr][i][k - j] << endl;\n if (j < a[i].size()) s += a[i][j];\n }\n }\n cout << res << endl;\n if (DEBUG) {\n vl v(k + 1);\n for (int i = 0; i < n; ++i) {\n ll s = 0;\n vl res = v;\n for (int j = 0; j < min(k + 1, (int)a[i].size() + 1); ++j) {\n vl v1 = v;\n for (int t = k; t >= j; --t) {\n v1[t] = max(v1[t], v1[t - j] + s);\n }\n for (int t = 0; t <= k; ++t) res[t] = max(res[t], v1[t]);\n if (j < a[i].size()) s += a[i][j];\n }\n v = res;\n }\n cerr << v[k] << endl;\n if (v[k] != res) {\n cout << n << ' ' << k << endl;\n for (int i = 0; i < n; ++i) {\n for (int x : a[i]) cout << x << ' '; cout << endl;\n }\n }\n assert(v[k] == res);\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "6b4347c3795fbcdd332d687b459a6b44", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"Ofast\", \"unroll-loops\", \"no-stack-protector\")\n \n#include \n \nusing namespace std;\n\n#define all(a) (a).begin(), (a).end()\n#define sz(a) (int)(a).size()\n\nusing ll = long long;\nusing ld = long double; \n// using ld = __float128;\nusing pii = pair;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\nmt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\ndefault_random_engine generator;\n\nvoid solve();\n\nint main() {\n#ifdef LOCAL\n // freopen(\"input.txt\", \"r\", stdin);\n // freopen(\"output.txt\", \"w\", stdout); \n#endif \n\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n cout << setprecision(15) << fixed;\n cerr << setprecision(15) << fixed;\n\n int tests = 1;\n // cin >> tests;\n\n for (int test = 1; test <= tests; ++test) {\n // cout << \"Case #\" << test << \": \";\n solve();\n }\n\n#ifdef LOCAL\n cerr << \"Time: \" << double(clock()) / CLOCKS_PER_SEC << endl;\n#endif\n}\n\n// ----------------------------------------------------------------- \n\nconst int MAXN = 3030;\n\nll pref[MAXN][MAXN];\nll suff[MAXN][MAXN];\n\nvoid solve() { \n int n, k;\n cin >> n >> k;\n vector> a(n);\n for (int i = 0; i < n; ++i) {\n int m;\n cin >> m;\n a[i].resize(m);\n for (int &x : a[i]) {\n cin >> x;\n }\n }\n\n for (int i = 0; i <= k; ++i) {\n pref[0][i] = -1;\n }\n pref[0][0] = 0;\n\n for (int i = 0; i < n; ++i) {\n ll sum = 0;\n for (int x : a[i]) {\n sum += x;\n }\n for(int j = 0; j <= k; ++j) {\n pref[i + 1][j] = pref[i][j];\n }\n for (int j = 0; j + sz(a[i]) <= k; ++j) {\n if (pref[i][j] == -1) {\n continue;\n }\n pref[i + 1][j + sz(a[i])] = max(pref[i + 1][j + sz(a[i])], pref[i][j] + sum);\n }\n }\n\n for (int i = 0; i <= k; ++i) {\n suff[n][i] = -1;\n }\n suff[n][0] = 0;\n\n for (int i = n - 1; i >= 0; --i) {\n ll sum = 0;\n for (int x : a[i]) {\n sum += x;\n }\n for(int j = 0; j <= k; ++j) {\n suff[i][j] = suff[i + 1][j];\n }\n for (int j = 0; j + sz(a[i]) <= k; ++j) {\n if (suff[i + 1][j] == -1) {\n continue;\n }\n suff[i][j + sz(a[i])] = max(suff[i][j + sz(a[i])], suff[i + 1][j] + sum);\n }\n }\n\n\n vector>> arr(k + 1);\n for (int i = 0; i < n; ++i) {\n ll sum = 0;\n for (int j = 0; j < sz(a[i]); ++j) {\n sum += a[i][j];\n if (j + 1 <= k) {\n arr[j + 1].push_back({sum, i});\n }\n }\n }\n\n ll res = pref[n][k];\n for (int i = 1; i <= k; ++i) {\n sort(all(arr[i]), greater());\n arr[i].resize(min(sz(arr[i]), k / i + 1));\n\n for (auto [s, p] : arr[i]) {\n for (int j = 0; j <= k - i; ++j) {\n if (pref[p][j] == -1 || suff[p + 1][k - i - j] == -1) {\n continue;\n }\n res = max(res, pref[p][j] + suff[p + 1][k - i - j] + s);\n }\n }\n }\n\n cout << res << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "b39c59680c2af72671c99ec904a2067c", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nconst int MX = 3000;\n\nlong long sum[MX], dp[MX + 1], ans;\nvector a[MX];\nint k;\n\nvoid solve(int l, int r) {\n\tif (l == r) {\n\t\tans = max(dp[k], ans);\n\t\t\n\t\tlong long here = 0;\n\t\tfor (int x = 0; x < k && x < a[l].size(); x++) {\n\t\t\there += a[l][x];\n\t\t\tans = max(dp[k - x - 1] + here, ans);\n\t\t}\n\t\t\n\t\treturn;\n\t}\n\t\n\tvector save(dp, dp + k + 1);\n\t\n\tint c = (l + r) / 2;\n\t\n\tfor (int i = l; i <= c; i++) {\n\t\tint sz = a[i].size();\n\t\t\n\t\tfor (int x = k; x - sz >= 0; x--) {\n\t\t\tdp[x] = max(dp[x], dp[x - sz] + sum[i]);\n\t\t}\n\t}\n\t\n\tsolve(c + 1, r);\n\t\n\tfor (int x = 0; x <= k; x++) dp[x] = save[x];\n\t\n\tfor (int i = c + 1; i <= r; i++) {\n\t\tint sz = a[i].size();\n\t\t\n\t\tfor (int x = k; x - sz >= 0; x--) {\n\t\t\tdp[x] = max(dp[x], dp[x - sz] + sum[i]);\n\t\t}\n\t}\n\t\n\tsolve(l, c);\n\t\n\tfor (int x = 0; x <= k; x++) dp[x] = save[x];\n}\n\nint main() {\n\tint n;\n\tignore = scanf(\"%d %d\", &n, &k);\n\tfor (int i = 0, m; i < n; i++) {\n\t\tignore = scanf(\"%d\", &m);\n\t\tfor (int j = 0, x; j < m; j++) {\n\t\t\tignore = scanf(\"%d\", &x);\n\t\t\t\n\t\t\tsum[i] += x;\n\t\t\ta[i].push_back(x);\n\t\t}\n\t}\n\t\n\tsolve(0, n - 1);\n\t\n\tprintf(\"%lld\\n\", ans);\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "9bcf99bb5f47f4d65d6b8ba3649f618a", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define all(x) (x).begin (),(x).end()\n#define sqrt(x) sqrt(abs(x))\n#define re return\n#define sz(x) ((int)(x).size ())\n#define prev PREV\n#define next NEXT\n\nusing ll = long long;\nusing ii = pair;\nusing ld = long double;\nusing D = double;\nusing vi = vector;\nusing vii = vector;\nusing vvi = vector;\nusing vs = vector;\n\ntemplate T abs (T x) { re x < 0 ? -x : x; }\ntemplate T sgn (T x) { re x < 0 ? -1 : (x > 0 ? 1 : 0); }\ntemplate T sqr (T x) { re x * x; }\ntemplate T gcd (T a, T b) { re a ? gcd (b % a, a) : b; }\n\nint n;\nint m;\nint t[3000];\nll s[3000][3001];\nll res[3001][6001];\nint prev[3001][6001];\nint was[3001];\n\nint out (int i, int j) {\n\tif (i == 0) re 0;\n\tif (prev[i][j]) {\n\t\twas[i - 1] = 1;\n\t\tout (i - 1, j - t[i - 1]);\n\t} else {\n\t\tout (i - 1, j);\n\t}\n}\n\nint main () {\n\tscanf (\"%d%d\", &n, &m);\n\tfor (int i = 0; i < n; i++) {\n\t\tint tt;\n\t\tscanf (\"%d\", &tt);\n\t\tt[i] = min (m, tt);\n\t\ts[i][0] = 0;\n\t\tll cur = 0;\n\t\tfor (int j = 0; j < tt; j++) {\n\t\t\tint x;\n\t\t\tscanf (\"%d\", &x);\n\t\t\tcur += x;\n\t\t\tif (j < t[i]) s[i][j + 1] = cur;\n\t\t}\n\t}\n\tfor (int i = 0; i <= n; i++)\n\t\tfor (int j = 0; j <= 2 * m; j++)\n\t\t\tres[i][j] = -1e18;\n\tres[0][0] = 0;\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j <= 2 * m; j++) {\n\t\t\tif (res[i + 1][j] < res[i][j]) {\n\t\t\t\tres[i + 1][j] = res[i][j];\n\t\t\t\tprev[i + 1][j] = 0;\n\t\t\t}\n\t\t\tif (j + t[i] <= 2 * m && res[i + 1][j + t[i]] < res[i][j] + s[i][t[i]]) {\n\t\t\t\tres[i + 1][j + t[i]] = res[i][j] + s[i][t[i]];\n\t\t\t\tprev[i + 1][j + t[i]] = 1;\n\t\t\t}\t\n//\t\t\tprintf (\"%d %d = %lld\\n\", i, j, res[i][j]);\n\t\t}\n\tll ans = 0;\n\tfor (int j = 0; j <= 2 * m; j++) {\n\t\tif (res[n][j] < 0) continue;\n\t\tfor (int i = 0; i < n; i++) was[i] = 0;\n\t\tout (n, j);\n//\t\tprintf (\"%d = %lld\\n\", j, res[n][j]);\n//\t\tfor (int i = 0; i < n; i++) printf (\"%d \", was[i]);\n//\t\tprintf (\"\\n\");\n\t\tif (j < m) {\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t\tif (!was[i] && j + t[i] >= m)\n\t\t\t\t\tans = max (ans, res[n][j] + s[i][m - j]);\n\t\t} else {\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t\tif (was[i] && t[i] >= (j - m))\n\t\t\t\t\tans = max (ans, res[n][j] - s[i][t[i]] + s[i][t[i] - (j - m)]);\n\t\t}\n//\t\tprintf (\"%lld\\n\", ans);\n\t}\n\tprintf (\"%lld\\n\", ans);\n\tcerr << clock () << endl;\n\tre 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "d20082c0b91cd7a8cac4df19c6f7bbcb", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/**\n * code generated by JHelper\n * More info: https://github.com/AlexeyDmitriev/JHelper\n * @author\n */\n\n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define pb push_back\n#define sz(v) ((int)(v).size())\n#define all(v) (v).begin(),(v).end()\n#define mp make_pair\n\nusing namespace std;\n\ntypedef long long int64;\ntypedef vector vi;\ntypedef pair ii;\n\nconstexpr int BUBEN = 50;\n\nclass TaskC {\n public:\n void solveOne(istream &in, ostream &out) {\n int n, k;\n in >> n >> k;\n vector> a(n);\n vector asum(n);\n for (int i = 0; i < n; ++i) {\n int m;\n in >> m;\n for (int j = 0; j < m; ++j) {\n int64 x;\n in >> x;\n a[i].push_back(x);\n asum[i] += x;\n }\n }\n int64 res = 0;\n for (int i = 0; i < n;) {\n int j = min(n, i + BUBEN);\n vector best(k + 1, 0);\n for (int v = 0; v < n; ++v) {\n if (v >= i && v < j) continue;\n int cnt = a[v].size();\n int64 score = asum[v];\n for (int old = k - cnt; old >= 0; --old) {\n best[old + cnt] = max(best[old + cnt], best[old] + score);\n }\n }\n vector cbest(k + 1, 0);\n for (int u = i; u < j; ++u) {\n for (int t = 0; t <= k; ++t) cbest[t] = best[t];\n for (int v = i; v < j; ++v) if (v != u) {\n int cnt = a[v].size();\n int64 score = asum[v];\n for (int old = k - cnt; old >= 0; --old) {\n cbest[old + cnt] = max(cbest[old + cnt], cbest[old] + score);\n }\n }\n int64 sofar = 0;\n for (int take = 0; take <= k && take <= a[u].size(); ++take) {\n res = max(res, sofar + cbest[k - take]);\n if (take < a[u].size()) {\n sofar += a[u][take];\n }\n }\n }\n i = j;\n }\n out << res << \"\\n\";\n }\n\n void solve(std::istream &in, std::ostream &out) {\n int nt = 1;\n for (int it = 0; it < nt; ++it) {\n solveOne(in, out);\n }\n }\n};\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n TaskC solver;\n std::istream& in(std::cin);\n std::ostream& out(std::cout);\n solver.solve(in, out);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "954ab9f0e1ff7478cb60982f99590a87", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/**\n * author: tourist\n * created: 01.11.2020 17:14:09 \n**/\n#include \n\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, k;\n cin >> n >> k;\n vector> a(n);\n vector sz(n);\n vector sum(n);\n for (int i = 0; i < n; i++) {\n int foo;\n cin >> foo;\n a[i].resize(foo);\n sz[i] = foo;\n sum[i] = 0;\n for (int j = 0; j < foo; j++) {\n cin >> a[i][j];\n sum[i] += a[i][j];\n }\n }\n const long long inf = (long long) 1e18;\n vector> save(30, vector(k + 1, -inf));\n vector dp(k + 1, -inf);\n dp[0] = 0;\n long long ans = 0;\n function Solve = [&](int level, int from, int to) {\n if (from == to) {\n long long took = 0;\n for (int i = 0; i <= k; i++) {\n ans = max(ans, took + dp[k - i]);\n if (i == (int) a[from].size()) {\n break;\n }\n took += a[from][i];\n }\n return;\n }\n for (int j = 0; j <= k; j++) {\n save[level][j] = dp[j];\n }\n int mid = (from + to) >> 1;\n {\n for (int i = from; i <= mid; i++) {\n for (int j = k - sz[i]; j >= 0; j--) {\n dp[j + sz[i]] = max(dp[j + sz[i]], dp[j] + sum[i]);\n }\n }\n Solve(level + 1, mid + 1, to);\n for (int j = 0; j <= k; j++) {\n dp[j] = save[level][j];\n }\n }\n {\n for (int i = mid + 1; i <= to; i++) {\n for (int j = k - sz[i]; j >= 0; j--) {\n dp[j + sz[i]] = max(dp[j + sz[i]], dp[j] + sum[i]);\n }\n }\n Solve(level + 1, from, mid);\n for (int j = 0; j <= k; j++) {\n dp[j] = save[level][j];\n }\n }\n };\n Solve(0, 0, n - 1);\n cout << ans << '\\n';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "969aa6f54e1930f95f604bd72abc73d1", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nusing ll = long long;\nusing ld = long double;\n\nconst int maxn = 3005;\nvector pr[maxn];\nvector w;\nvector c;\nint k;\nll ans = 0;\n\nvoid apply(vector& d, int w, ll c) {\n for (int i = (int) d.size() - w - 1; i >= 0; --i) {\n d[i + w] = max(d[i + w], d[i] + c);\n }\n}\n\nvector dmem[40];\n\nvoid rec(int l, int r, int depth) {\n const auto& d = dmem[depth];\n if (l + 1 == r) {\n for (int i = 0; i <= min(k, w[l]); ++i) {\n ans = max(ans, d[k - i] + pr[l][i]);\n }\n return;\n }\n int c = (l + r) / 2;\n auto& d2 = dmem[depth + 1];\n for (int i = 0; i < (int) d2.size(); ++i) {\n d2[i] = d[i];\n }\n for (int i = l; i < c; ++i) {\n apply(d2, w[i], ::c[i]);\n }\n rec(c, r, depth + 1);\n for (int i = 0; i < (int) d2.size(); ++i) {\n d2[i] = d[i];\n }\n for (int i = c; i < r; ++i) {\n apply(d2, w[i], ::c[i]);\n }\n rec(l, c, depth + 1);\n}\n\nsigned main() {\n#ifdef LOCAL\n assert(freopen(\"c.in\", \"r\", stdin));\n#endif\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int n;\n cin >> n >> k;\n w.resize(n);\n c.resize(n);\n for (int i = 0; i < n; ++i) {\n cin >> w[i];\n pr[i].resize(w[i] + 1);\n for (int j = 0; j < w[i]; ++j) {\n int x;\n cin >> x;\n c[i] += x;\n pr[i][j + 1] = pr[i][j] + x;\n }\n }\n for (int i = 0; i < 40; ++i) {\n dmem[i].resize(k + 1);\n }\n rec(0, n, 0);\n cout << ans << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["greedy", "divide and conquer"], "code_uid": "9233773524e9fa14422fa66cb02ba641", "src_uid": "9e9d4f58ebd8293025ab8004aeb8d343", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nconst int MOD = 1000000007;\n\nint n,m,K;\nint f[55][55][55][110],c[110][110];\n\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&m,&K);n>>=1;\n\tfor(int i=0;i<=100;i++){\n\t\tc[i][0]=1;\n\t\tfor(int j=1;j<=i;j++) c[i][j]=min(200,c[i-1][j]+c[i-1][j-1]);\n\t}\n\tint ans=0;\n\tfor(int i=1;i<=n;i++) f[i][1][i][1]=1;\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=1;j\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef double ldb;\ntypedef pair pll;\ntypedef pairpii;\n\n\nconst ll maxx = 100000 + 7;\nconst ll maxn = 100+ 7;\nconst ll mod = 998244353;\nconst ll maxs = 5 * (1E5) + 7;\nconst ll maxm =30+ 7;\nconst ll maxk = 800 + 7;\nconst ll maxw = 100 + 7;\nconst ll maxh = 100 + 7;\nconst ldb pi = 3.1415926535898;\n\nconst int inf = 1e9+7;\nconst ldb eps = 1e-8;\n//#define __TREE\n#ifdef __TREE\n#define lc(xx) (xx<<1)\n#define rc(xx) ((xx<<1)|1)\n#endif\n\n#define _for(i,a,b) for(ll i=(a);i<(b);i++)\n#define msz(ss) memset(ss,0,sizeof(ss))\n#define msllz(ss) memset(ss,(ll)0,sizeof(ss))\n#define mszull(ss) memset(ss,(ull)0,sizeof(ss))\n#define _rep(i,a,b) for(int i=(a);i<=(b);i++)\n#define _dep(i,a,b) for(int i=(a);i>=(b);i--)\n#define msllneg(ss) memset(ss,(ll)-1,sizeof(ss))\n#define msitt(xx) memset(xx,1,sizeof(xx))\n#define IO ios::sync_with_stdio(false);cout.tie(0);cin.tie(0)\n\nostream& operator << (ostream & os, pll &rhs)\n{\n\tos << rhs.first << \" \" << rhs.second << endl;\n\treturn os;\n}\n\nll qw(ll a, ll b)\n{\n\tll ans = 1;\n\tif (a == 0)\n\t\treturn 0ll;\n\twhile (b)\n\t{\n\t\tif (b & 1)\n\t\t{\n\t\t\tans = (ans%mod*a%mod) % mod;\n\t\t\tans %= mod;\n\t\t}\n\t\tb >>= 1;\n\t\ta = (a%mod*a%mod) % mod;\n\t\ta %= mod;\n\t}\n\tans %= mod;\n\treturn ans;\n}\n\ntemplate\nT min3(T a, T b, T c)\n{\n\treturn min(min(a, b), c);\n}\ntemplate\nT max3(T a, T b, T c)\n{\n\treturn max(max(a, b), c);\n}\n\nll dp[maxn][100|1][2][1 << 4][1 << 4]; //pos limit dt x�ı��� y�ı���\nll vis[maxn][100|1][2][1 << 4][1 << 4];\nll dig[maxn];\nll ncase;\nll n;\n\nll dfs(ll pos, ll dt, ll limit, ll mx, ll my, ll x, ll y)\n{\n\tif (pos == n)\n\t\treturn (dt == 0) && (my&mx);\n\tll &ans = dp[pos][dt + x][limit][mx][my];\n\tif (vis[pos][dt + x][limit][mx][my] == ncase)\n\t\treturn ans;\n\tvis[pos][dt + x][limit][mx][my] = ncase;\n\tll up = limit ? dig[pos] : 9;\n\tans = 0;\n\t_rep(i, 0, 9)\n\t{\n\t\t_rep(j, 0, up)\n\t\t{\n\t\t\tll ndt = dt * 10 - i*y + j*x;\n\t\t\tif (ndt <= -x||ndt>= y)continue;\n\t\t\tll tmx = 0, tmy = 0;\n\t\t\tif ((j!=0) && (j%y == 0)) tmy = (1 << ((j / y) - 1));\n\t\t\tif ((i!=0) && (i%x == 0)&&((i/x)*y<=9)) tmx = (1 << ((i / x) - 1));\n\t\t\tans += dfs(pos + 1, ndt, limit&&(j == up), mx|tmx, my|tmy, x, y)%mod;\n\t\t\tans %= mod;\n\t\t}\n\t}\n\tans %= mod;\n\treturn ans;\n}\n\nstring str;\nll ans = 0;\nvoid init()\n{\n\t//cin >> str;\n\tn = str.length();\n\tans = 0;\n\t_for(i, 0, str.length())\n\t{\n\t\tdig[i] = str[i] - '0';\n\t\tans = (ans*10+dig[i])%mod;\n\t}\n}\n\nll gcd(ll x, ll y)\n{\n\treturn y == 0 ? x : gcd(y,x%y);\n}\nvoid solve()\n{\n\tncase = 0;\n\t_rep(x,1, 9)\n\t\t_rep(y, x + 1, 9)if (gcd(x, y) == 1) ++ncase,ans += 2*dfs(0, 0, 1, 0, 0, x, y)%mod,ans %= mod;\n\tcout << ans << endl;\n}\n\n\n\n\n\n\n\n\nint main(void)\n{\n#ifdef OJ\n\tfreopen(\"buffcraft.in\", \"r\", stdin);\n\tfreopen(\"a.txt\", \"w\", stdout);\n#endif\n\twhile (cin >> str)\n\t{\n\t\tinit();\n\t\tsolve();\n\t\t//cout <<\"time :\"<<\" \"<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//\n// Created by zjsdu on 5/28/2020.\n//\n\n#ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_\n#define JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_\n#include \n#include \n#include \n#ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_\n#define JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_\n#include \n#include \n#include \nstruct fast_ios {\n fast_ios() {\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n std::cout.precision(10);\n std::cout << std::fixed;\n };\n} fast_ios_;\nnamespace io {\ntemplate\nstd::istream &operator>>(std::istream &in, std::tuple &t);\ntemplate\nstd::istream &operator>>(std::istream &in, std::pair &p) {\n in >> p.first >> p.second;\n return in;\n}\n\ntemplate\nstd::istream &operator>>(std::istream &stream, std::vector &vec) {\n for (auto &x : vec)\n stream >> x;\n return stream;\n}\n\ntemplate\nstd::istream &operator>>(std::istream &in, const std::tuple &t) {\n std::apply([&in](auto &... args) { ((in >> args), ...); }, t);\n return in;\n}\n\ntemplate void scan(Args &... args) {\n ((std::cin >> args), ...);\n}\n\ntemplate\nstd::ostream &operator<<(std::ostream &out, const std::vector &vec) {\n bool first = true;\n for (const T &t : vec) {\n if (first)\n first = false;\n else\n out << ' ';\n out << t;\n }\n return out;\n}\n\ntemplate\nstd::ostream &operator<<(std::ostream &out, const std::pair &p) {\n out << p.first << ' ' << p.second;\n return out;\n}\n\ntemplate\nstd::ostream &operator<<(std::ostream &out,\n const std::vector> &t) {\n bool is_first = true;\n for (const auto &row : t) {\n if (is_first)\n is_first = false;\n else\n out << '\\n';\n out << row;\n }\n return out;\n}\n\ntemplate void pt(Args &&... args) {\n ((std::cout << args << ' '), ...);\n}\n\ntemplate\nvoid pl(const First &first, const Args &... args) {\n std::cout << first;\n ((std::cout << ' ' << args), ...);\n std::cout << '\\n';\n}\n\ntemplate void pn(const Args &... args) {\n ((std::cout << args << '\\n'), ...);\n}\n}// namespace io\ninline int ri() {\n int x;\n std::cin >> x;\n return x;\n}\n#endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing vl = std::vector;\nusing vb = std::vector;\nusing vi = std::vector;\nusing vs = std::vector;\nusing pii = std::pair;\nusing pli = std::pair;\nusing pil = std::pair;\nusing pll = std::pair;\nusing vii = std::vector;\ntemplate>\nusing pq = std::priority_queue, U>;\ntemplate using vt = std::vector>;\ntemplate class vv {\n std::vector> data;\n\n public:\n explicit vv(int n) {\n assert(n > 0);\n data.assign(n, std::vector{});\n }\n vv(int n, int m, T init = T{}) {\n assert(n > 0 && m > 0);\n data.assign(n, std::vector(m, init));\n }\n std::vector &operator[](size_t i) {\n assert(i < data.size());\n return data[i];\n }\n void fill(T val) {\n for (auto &row : data)\n std::fill(row.begin(), row.end(), val);\n }\n friend std::istream &operator>>(std::istream &in, vv &val) {\n return io::operator>>(in, val.data);\n }\n friend std::ostream &operator<<(std::ostream &out, const vv &val) {\n return io::operator<<(out, val.data);\n }\n friend auto begin(vv &val) { return val.data.begin(); }\n friend auto end(vv &val) { return val.data.end(); }\n friend auto rbegin(vv &val) { return val.data.rbegin(); }\n friend auto rend(vv &val) { return val.data.rend(); }\n};\n#define TOKENPASTE(x, y) x##y\n#define TOKENPASTE2(x, y) TOKENPASTE(x, y)\n#define rng3(i, a, b) \\\n for (std::common_type::type \\\n i = a, \\\n TOKENPASTE2(r_end_, __LINE__) = b; \\\n i < TOKENPASTE2(r_end_, __LINE__); ++i)\n#define rng2(i, n) rng3(i, 0, n)\n#define GET_MACRO(_1, _2, _3, NAME, ...) NAME\n#define rng(...) GET_MACRO(__VA_ARGS__, rng3, rng2)(__VA_ARGS__)\n#define up(i, a, b) \\\n for (std::common_type::type \\\n i = a, \\\n TOKENPASTE2(r_end_, __LINE__) = b; \\\n i <= TOKENPASTE2(r_end_, __LINE__); ++i)\n#define down(i, b, a) \\\n for (std::common_type::type \\\n i = b, \\\n TOKENPASTE2(r_end_, __LINE__) = a; \\\n i >= TOKENPASTE2(r_end_, __LINE__); --i)\n#define rep(n) \\\n for (auto TOKENPASTE2(_iter_, __LINE__) = n; \\\n TOKENPASTE2(_iter_, __LINE__) > 0; --TOKENPASTE2(_iter_, __LINE__))\n#define FOR(x, cont) for (const auto &x : cont)\n#define For(x, cont) for (auto &x : cont)\n#define ALL(x) begin(x), end(x)\n// hat off to 300iq\n#define RALL(x) rbegin(x), rend(x)\n#define pb push_back\n#define mp make_pair\n#define eb emplace_back\n#define ep emplace\n#define SZ(x) (int) (x).size()\n#define rp(...) return pl(__VA_ARGS__)\n#define rpn(...) return pn(__VA_ARGS__)\n#define adv(i, n) \\\n for (auto TOKENPASTE2(_n_, __LINE__) = n; i < TOKENPASTE2(_n_, __LINE__); ++i)\n#define INT(...) int __VA_ARGS__; scan(__VA_ARGS__)\n#define LL(...) long long __VA_ARGS__; scan(__VA_ARGS__)\n#define STR(...) std::string __VA_ARGS__; scan(__VA_ARGS__)\n#endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_\n\ninline void Yn(bool p) {\n std::cout << (p ? \"Yes\\n\" : \"No\\n\");\n}\ninline void YN(bool p) {\n std::cout << (p ? \"YES\\n\" : \"NO\\n\");\n}\ninline void yn(bool p) {\n std::cout << (p ? \"yes\\n\" : \"no\\n\");\n}\ntemplate T &inc(T &cont) {\n for (auto &e : cont)\n ++e;\n return cont;\n}\ntemplate T &dec(T &cont) {\n for (auto &e : cont)\n --e;\n return cont;\n}\ntemplate inline bool chkmin(A &a, const B &b) {\n return b < a ? a = b, true : false;\n}\n\ntemplate inline bool chkmax(A &a, const B &b) {\n return a < b ? a = b, true : false;\n}\n\ntemplate struct reversion_wrapper { T &iterable; };\ntemplate auto begin(reversion_wrapper w) {\n using std::rbegin;\n return rbegin(w.iterable);\n}\ntemplate auto end(reversion_wrapper w) {\n using std::rend;\n return rend(w.iterable);\n}\ntemplate reversion_wrapper reverse(T &&iterable) {\n return {iterable};\n}\ntemplate T ceil(T x, U y) {\n assert(y > 0);\n if (x > 0)\n x += y - 1;\n return x / y;\n}\n\ntemplate T floor(T x, U y) {\n assert(y > 0);\n if (x < 0)\n x -= y - 1;\n return x / y;\n}\n\ntemplate struct typelist {};\n\ntemplate\nconstexpr bool any_same = (std::is_same_v || ...);\n\ntemplate struct y_combinator {\n template struct ref {\n y_combinator &self;\n template decltype(auto) operator()(Args &&... args) const {\n using G = std::conditional_t, TLs...>,\n ref, ref>>;\n return self.f(G{self}, std::forward(args)...);\n }\n };\n F f;\n template decltype(auto) operator()(Args &&... args) {\n return ref<>{*this}(std::forward(args)...);\n }\n};\ntemplate y_combinator(F) -> y_combinator;\n#ifndef debug\n#define debug(...) 42\n#endif\n#ifndef show\n#define show(...) 42\n#endif\nusing namespace io;\nusing namespace std;\n#endif// CP_UTILS\n\n\n\n\nclass UnionFind {\n std::vector par, sz;\n int nTree;\n\npublic:\n explicit UnionFind(int n) : par(n), sz(n, 1), nTree(n) { // 0-indexed\n std::iota(par.begin(), par.end(), 0);\n }\n void init() {\n std::fill(par.begin(), par.end(), 1);\n nTree = par.size();\n std::iota(par.begin(), par.end(), 0);\n }\n int n_tree() const { return nTree; }\n\n int size(int x) { return sz[root(x)]; }\n\n int root(int x) { return x == par[x] ? x : par[x] = root(par[x]); }\n\n bool unite(int x, int y) {\n int rx = root(x), ry = root(y);\n if (rx != ry) {\n par[rx] = ry;\n --nTree;\n sz[ry] += sz[rx];\n return true;\n }\n return false;\n }\n\n bool is_root(int x) const { return par[x] == x; }\n bool same(int x, int y) { return root(x) == root(y); }\n};\n\ntemplate inline int_t lowbit(int_t x) {\n return x & -x;\n}\n\ninline int h_bit(unsigned long long x) {\n return int(sizeof(unsigned long long) * 8 - __builtin_clzll(x));\n}\n\nunsigned long long pow2(unsigned long long x) {// round up to power of 2\n if (x == 0)\n return 1;\n return x == lowbit(x) ? x : 1ull << h_bit(x);\n}\n\ntemplate inline int bit(T a, int i) {\n return a >> i & 1;\n}\ntemplate inline T get_mid(T l, T r) {\n assert(l <= r);\n return l + (r - l >> 1);\n}\n\n#define popcnt(x) __builtin_popcountll((x))\n\nvoid solve() {\n INT(n);\n STR(a, b);\n vector reach(20);\n UnionFind dsu(20);\n rng (i, n)\n if (a[i] != b[i]) {\n reach[a[i] - 'a'] |= 1 << b[i] - 'a';\n dsu.unite(a[i] - 'a', b[i] - 'a');\n }\n int m = 20;\n vv comps(m);\n rng (i, m)\n comps[dsu.root(i)].pb(i);\n int ans = 0;\n FOR (comp, comps) {\n int cnt = SZ(comp);\n if (cnt <= 1) {\n continue;\n }\n debug(comp);\n vb is_dag(1 << cnt);\n is_dag[0] = true;\n rng (s, 1, 1 << cnt) {\n int t = 0;\n rng (i, cnt)\n if (bit(s, i))\n t |= 1 << comp[i];\n rng (i, cnt)\n if (bit(s, i) and is_dag[s ^ 1 << i] and (t & reach[comp[i]]) == 0) {\n is_dag[s] = true;\n break;\n }\n }\n int x = 100;\n rng (i, 1, 1 << cnt)\n if (is_dag[i])\n chkmin(x, 2 * cnt - popcnt(i) - 1);\n ans += x;\n }\n\n pl(ans);\n}\n/*\n * Make sure that your laptop is plugged in or the build may be slow.\n *\n * Don't settle for a solution if it feels too clunky, take a moment and see if\n * you can make it simpler.\n */\n\n\nint main() {\n int n;\ncin >> n;\nfor(int i = 0; i < n; ++i) {\n\tsolve();\n}\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "graphs", "trees", "bitmasks"], "code_uid": "8163945a4c2795279c031fdb1097e3e6", "src_uid": "d0735a763e2a40bfc94085019cd646f0", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include \n#include \n\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define loop(i,a,n) for(int i=a;i\n#define pll pair\n#define pli pair\n#define pil pair\n#define FF first\n#define SS second\n#define PI acos(-1.0)\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\ntypedef tree,\n rb_tree_tag, tree_order_statistics_node_update> anik;\n\nconst int MOD = 1e9 + 7;\nconst ll MX= 1e9;\nconst ll INF = 1e9;\n\nchar s[42];\n\nmain()\n{\n int t;\n cin>>t;\n while(t--)\n {\n\n cin>>s;\n int k=strlen(s)-4,year=atoi(s+4),f=0,tenpow=10;\n loop(i,0,k-1)\n {\n f+=tenpow;\n tenpow*=10;\n\n }\n while(year< 1989+f)year+=tenpow;\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define double long double\ntypedef long long ll;\ntypedef std::pair pll;\ntypedef std::pair pii;\n//typedef std::pair pdd;\n#define fori(N) for(int i = 0; iused;\nstackdestroy;\n\ninline ll intersect(ll a, ll b) {\n\tif (a > b)return 0;\n\treturn (b - a + 1)*(b - a+2) / 2;\n\n}\n\nll update(int a, int b) {\n\tif (a > b)return 0;\n\tint frm = a;\n\tll ret = intersect(a, b);\n\tmap::iterator it = used.upper_bound(a);\n\tif (it != used.begin()) {\n\t\tit--;\n\t\tif (it->second >= b)return 0;\n\t\tif (it->second >= a) {\n\t\t\tret -= intersect(a, it->second);\n\t\t\tfrm = it->second;\n\t\t}\n\t\tit++;\n\t}\n\twhile (it != used.end() && it->second <= b) {\n\t\tdestroy.push(it->first);\n//\t\tfrm = max(it->first, frm);\n\t\tret -= intersect(it->first, it->second) - intersect(it->first, frm);\n\t\tfrm = it->second;\n\n\t\tit++;\n\t}\n\tif (it != used.end()) {\n\t\tret -= intersect(it->first, b) - intersect(it->first, frm);\n\n\n\t}\n\twhile (!destroy.empty()) {\n\t\tused.erase(destroy.top());\n\t\tdestroy.pop();\n\n\t}\n\tused[a] = b;\n\treturn ret;\n\n}\nint primes[100000];\nint primeCount;\nbool isPrime[maxn+1010];\nvplaces[maxn + 1];\nvtrueplaces[maxn + 1];\nint a[maxn + 10];\n\nint min1, min2, max1, max2;\n\nvoid upd(int x) {\n\tif (x == min1 || x == max1)return;\n\tint b = x;\n\tif (b < min1) swap(b, min1);\n\tmin2 = min(min2, b);\n\tif (x > max1) swap(x, max1);\n\tmax2 = max(max2, x);\n\n\n}\n\nint main()\n{\n#if defined(_DEBUG) || defined(_RELEASE)\n\tfreopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#else\n\t//(File\".in\", \"r\", stdin); freopen(File\".out\", \"w\", stdout);\n\t//freopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tll answer = 0;\n\tfori1(maxn)isPrime[i] = 1;\n\tisPrime[1] = 0;\n\tfori1(maxn+1000) {\n\t\tif (!isPrime[i])continue;\n\t\tprimes[primeCount++] = i;\n\t\tfor (int j = 2 * i; j <= maxn + 1000; j += i)isPrime[j] = 0;\n\n\t}\n\tint n; cin >> n;\n\tfori(n)cin >> a[i];\n\tfori(n)places[a[i]].push_back(i);\n\tfor (int i = maxn; i > 0; i--) {\n\t\tmax1 = max2 = -2E9;\n\t\tmin1 = min2 = 2E9;\n\t\tfor (int j = 0; i*primes[j] <= maxn; j++) {\n\t\t\tfork(trueplaces[primes[j]*i].size()) upd(trueplaces[primes[j] * i][k]);\n\n\t\t}\n\t\tfork(places[i].size())upd(places[i][k]);\n\t\tif (max1 > -1E9)trueplaces[i].push_back(max1);\n\t\tif (max2 > -1E9)trueplaces[i].push_back(max2);\n\t\tif (min1 < 1E9)trueplaces[i].push_back(min1);\n\t\tif (min2 < 1E9)trueplaces[i].push_back(min2);\n\t\tif (max2 < 0)continue;\n\t\tanswer += i*update(min2 + 1, n - 1);\n\t\tanswer += i*update(0, max2-1);\n\t\tanswer += i*update(min1 + 1, max1 - 1);\n\t}\n\tcout << answer;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "3b817fb19b1f7409687cfe921d7149d8", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ll long long\n#define X first\n#define Y second\n#define MP make_pair\n#define PCI pair\n#define PII pair\n#define PIII pair,int>\n#define PCCI pair,int>\n#define lson l,m,rt<<1\n#define rson m+1,r,rt<<1|1\nusing namespace std;\n\nconst int maxn = 2e5+10;\nint n,a[maxn],num[maxn],l[2][maxn],r[2][maxn];\nll ans[maxn],Ans;\nvector fen[maxn],s[maxn];\nint mx[maxn<<2],mi[maxn<<2],cov[maxn<<2];\nll sum[maxn<<2];\n\nvoid up(int rt){\n mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);\n mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);\n sum[rt]=sum[rt<<1]+sum[rt<<1|1];\n}\n\nvoid build(int l,int r,int rt){\n cov[rt]=0;\n if(l==r){\n mx[rt]=mi[rt]=l;\n sum[rt]=l;\n return;\n }\n int m=(l+r)>>1;\n build(lson);\n build(rson);\n up(rt);\n}\n\nvoid down(int rt,int len){\n if(cov[rt]){\n cov[rt<<1]=cov[rt<<1|1]=mi[rt<<1]=mi[rt<<1|1]=mx[rt<<1]=mx[rt<<1|1]=cov[rt];\n sum[rt<<1]=1ll*(len+1)/2ll*cov[rt];\n sum[rt<<1|1]=1ll*len/2ll*cov[rt];\n cov[rt]=0;\n }\n}\n\nvoid upd(int L,int R,int x,int l,int r,int rt){\n if(L>r||R=x) return;\n int m=(l+r)>>1;\n if(L<=l&&r<=R&&mx[rt]<=x){\n mi[rt]=mx[rt]=cov[rt]=x;\n sum[rt]=1ll*x*(r-l+1);\n return;\n }\n down(rt,r-l+1);\n if(L<=m) upd(L,R,x,lson);\n if(R>m) upd(L,R,x,rson);\n up(rt);\n}\n\nint main(){\n //freopen(\"input.txt\",\"r\",stdin);\n //freopen(\"output.txt\",\"w\",stdout);\n scanf(\"%d\",&n);\n for(int i=1;i<=n;i++) scanf(\"%d\",&a[i]);\n for(int i=1;i<=n;i++){\n s[a[i]].push_back(i);\n }\n for(int i=1;i<=2e5;i++){\n for(int j=i;j<=2e5;j+=i){\n for(int k=0;k=1;i--){\n for(int j=0;j=1;i--){\n ans[i]=1ll*(n+1)*n-sum[1]-1;\n if(num[i]>=2){\n //printf(\"%d %I64d %I64d\\n\",i,sum[1],ans[i]);\n upd(1,l[0][i],r[1][i],1,n,1);\n upd(l[0][i]+1,l[1][i],r[0][i],1,n,1);\n upd(l[1][i]+1,n,n+1,1,n,1);\n }\n }\n for(int i=2e5;i>=1;i--){\n ans[i]=ans[i]-ans[i-1];\n //if(ans[i]) printf(\"%d %I64d\\n\",i,ans[i]);\n Ans+=1ll*i*ans[i];\n }\n printf(\"%I64d\\n\",Ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "18e4ba5fbbeadf882820edf3cedf05dd", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\n#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)\n#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)\n#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)\n\n#define type(x) __typeof(x.begin())\n#define pb push_back\n#define mp make_pair\n#define nd second\n#define st first\ntypedef pair < int ,int > pii;\ntypedef long long ll;\nconst long long linf = 1e18+5;\nint mod = (int) 1e9 + 7;\nconst int logN = 18;\nconst int inf = 1e9;\nconst int N = 2e5 + 5;\n\nint n, m, x, y, z, a[N];\nvector< int > H[N], v[N];\nll sum = 0, ans[N];\nset > S;\nset > :: iterator it, it2;\nvoid divide(int x) {\n //let 1...x be an isolate part\n it = S.lower_bound(mp(mp(x + 1, 0), -1));\n if(it == S.begin()) return ; it--;\n if(it->st.nd < x) return ;\n pair t = *it; S.erase(*it);\n S.insert(mp(mp(t.st.st, x), t.nd));\n if(x + 1 <= t.st.nd) S.insert(mp(mp(x + 1, t.st.nd), t.nd));\n}\n\nvoid remove(int x) {\n //remove all elements with idx >= x\n //let 1...x - 1 be an isolate part\n divide(x - 1);\n while(S.rbegin()->st.st >= x) {\n pair t = *S.rbegin();\n sum -= (t.st.nd - t.st.st + 1) * (ll) (t.nd);\n S.erase(t);\n }\n}\n\nvoid maximize(set > :: iterator it, int x, int y) {\n pair t; t.nd = x; t.st.st = y;\n while(it != S.end()) {\n if(it->nd >= x) break;\n t.st.nd = it->st.nd;\n sum -= (it->st.nd - it->st.st + 1) * (ll) (it->nd);\n it2 = it; it2++; S.erase(it); it = it2;\n }\n if(t.st.st <= t.st.nd) {\n sum += (t.st.nd - t.st.st + 1) * (ll) t.nd;\n S.insert(t);\n }\n}\n\nint main() {\n //freopen(\"in.txt\", \"r\", stdin);\n scanf(\"%d\", &n);\n FOR(i, 1, n) {\n scanf(\"%d\", &a[i]);\n H[a[i]].pb(i); sum += i;\n S.insert(mp(mp(i, i), i));\n }\n FOR(i, 1, N - 1) for(int j = i; j < N; j += i) foreach(it, H[j]) v[i].pb(*it);\n ans[N - 1] = n * (ll) (n + 1) / 2;\n ROF(i, N - 2, 1) {\n int l = v[i].size() - 1;\n if(l <= 0) { ans[i] = ans[i + 1]; continue; }\n sort(v[i].begin(), v[i].end());\n remove(v[i][1] + 1);\n maximize(S.begin(), v[i][l-1], 1);\n divide(v[i][0]);\n maximize(S.lower_bound(mp(mp(v[i][0] + 1, v[i][0]), 0)), v[i][l], v[i][0] + 1);\n ans[i] = (S.rbegin()->st.nd * (ll)(n + 1) - sum);\n }\n ll all = 0;\n FOR(i, 1, N - 2) all += (ans[i + 1] - ans[i]) * (ll) i;\n printf(\"%lld\\n\", all);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "8d42790f484c5e26e1c6a6eae413c069", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\nusing namespace std;\nconst int N=200005,M=200000;\ntypedef long long ll;\nint a[N],n,m,l1[N],r1[N],l2[N],r2[N];\nll f[N],ans;\nstruct node{\n\tint mx,mi;\n\tll sum,tag;\n}tr[N*4];\ninline void updata(int x){\n\ttr[x].mx=max(tr[x<<1].mx,tr[x<<1|1].mx);\n\ttr[x].mi=min(tr[x<<1].mi,tr[x<<1|1].mi);\n\ttr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;\n}\nvoid build(int x,int l,int r){\n\tif (l==r){\n\t\ttr[x].mx=tr[x].mi=tr[x].sum=l;\n\t\treturn;\n\t}\n\tint mid=(l+r)>>1;\n\tbuild(x<<1,l,mid);\n\tbuild(x<<1|1,mid+1,r);\n\tupdata(x);\n}\ninline void work(int x,int l,int r,ll m){\n\ttr[x].mx=tr[x].mi=m;\n\ttr[x].sum=m*(r-l+1);\n\ttr[x].tag=m;\n}\nvoid pushdown(int x,int l,int r){\n\tif (tr[x].tag){\n\t\tif (l==r) return;\n\t\twork(x<<1,l,(l+r)>>1,tr[x].tag);\n\t\twork(x<<1|1,((l+r)>>1)+1,r,tr[x].tag);\n\t\ttr[x].tag=0; \n\t}\n}\nvoid modify(int x,int l,int r,int s,int t,ll m){\n\tpushdown(x,l,r);\n\tif (tr[x].mi>=m) return;\n\tif (l==s && r==t && m>tr[x].mx){\n\t\twork(x,l,r,m);\n\t\treturn; \n\t}\n\tint mid=(l+r)>>1;\n\tif (t<=mid) modify(x<<1,l,mid,s,t,m);\n\telse if (s>mid) modify(x<<1|1,mid+1,r,s,t,m);\n\telse {\n\t\tmodify(x<<1,l,mid,s,mid,m);\n\t\tmodify(x<<1|1,mid+1,r,mid+1,t,m);\n\t}\n\tupdata(x);\n}\nint main(){\n\tscanf(\"%d\",&n);\n\tfor (int i=1;i<=n;i++) scanf(\"%d\",&a[i]);\n\tfor (int i=1;i<=n;i++){\n\t\tint m=sqrt(a[i]);\n\t\tfor (int j=1;j<=m;j++){\n\t\t\t if (a[i]%j==0) {\n\t\t \t\tif (!l1[j]) l1[j]=i;\n\t\t \t\telse if (!l2[j]) l2[j]=i;\n\t\t \t\tr2[j]=r1[j],r1[j]=i;\n\t\t \t\tif (a[i]!=j*j){\n\t\t \t\tif (!l1[a[i]/j]) l1[a[i]/j]=i;\n\t\t \t\telse if (!l2[a[i]/j]) l2[a[i]/j]=i;\n\t\t \t\tr2[a[i]/j]=r1[a[i]/j],r1[a[i]/j]=i;\n\t\t\t }\n\t\t \t}\n\t\t}\n\t \t\n\t}\n\tbuild(1,1,n); \n\tf[M+1]=(ll)n*(n+1)/2;\n\tfor (int i=M;i>=0;i--){\n\t\tf[i]=f[i+1];\n\t\tif (l1[i]==r1[i]) continue;\n\t\tmodify(1,1,n,1,l1[i],r2[i]);\n\t\tmodify(1,1,n,l1[i]+1,l2[i],r1[i]);\n\t\tif (l2[i]+1<=n)\n\t\tmodify(1,1,n,l2[i]+1,n,n+1);\n\t\tf[i]=(ll)n*(n+1)-tr[1].sum; \n\t}\n\tfor (int i=1;i<=M;i++)\n\t ans+=(f[i+1]-f[i])*i;\n\t printf(\"%I64d\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "c7be9c3b325538e8fd8c207a83ce8087", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//#define NDEBUG\n#include \n\nusing namespace std ;\n\n//----------------------------------------------------------------------------------------------------------------------------------------------\n\ntypedef long long int lli ;\ntypedef long double ld ;\ntypedef pair < int , int > pii ;\ntypedef pair < lli , lli > pllilli ;\n\ntypedef vector < int > vi ;\ntypedef vector < lli > vlli ;\ntypedef vector < bool > vb ;\ntypedef vector < string > vs ;\ntypedef vector < float > vf ;\ntypedef vector < double > vd ;\ntypedef vector < ld > vld ;\ntypedef vector < pii > vpii ;\ntypedef vector < pllilli > vpllilli ;\n\ntypedef vector < vi > vvi ;\ntypedef vector < vvi > vvvi ;\ntypedef vector < vvvi > vvvvi ;\ntypedef vector < vlli > vvlli ;\ntypedef vector < vvlli > vvvlli ;\ntypedef vector < vvvlli > vvvvlli ;\ntypedef vector < vb > vvb ;\ntypedef vector < vs > vvs ;\ntypedef vector < vf > vvf ;\ntypedef vector < vd > vvd ;\ntypedef vector < vld > vvld ;\ntypedef vector < vpii > vvpii ;\ntypedef vector < vpllilli > vvpllilli ;\n\ntypedef set < int > si ;\ntypedef set < lli > slli ;\ntypedef set < pii > spii ;\ntypedef set < pllilli > spllilli ;\ntypedef set < string > ss ;\n\ntypedef unordered_set < int > usi ;\ntypedef unordered_set < lli > uslli ;\ntypedef unordered_set < pii > uspii ;\ntypedef unordered_set < pllilli > uspllilli ;\ntypedef unordered_set < string > uss ;\n\ntypedef map < int , int > mii ;\ntypedef map < lli , lli > mllilli ;\ntypedef map < int , pii > mipii ;\ntypedef map < pii , pii > mpiipii ;\ntypedef map < pii , int > mpiii ;\ntypedef map < int , string > mis ;\ntypedef map < string , int > msi ;\ntypedef map < string , string > mss ;\n\ntypedef unordered_map < int , int > umii ;\ntypedef unordered_map < lli , lli > umllilli ;\ntypedef unordered_map < int , pii > umipii ;\ntypedef unordered_map < pii , pii > umpiipii ;\ntypedef unordered_map < pii , int > umpiii ;\ntypedef unordered_map < int , string > umis ;\ntypedef unordered_map < string , int > umsi ;\ntypedef unordered_map < string , string > umss ;\n\n#define dash \"---------------------------------------------------\"\n#define fie(i,start,stop) for ( int i = start ; i < stop ; i ++ )\n#define fii(i,start,stop) for ( int i = start ; i <= stop ; i ++ )\n#define fdei(i,start,stop) for ( int i = start - 1 ; i >= stop ; i -- )\n#define fdii(i,start,stop) for ( int i = start ; i >= stop ; i -- )\n#define tr(it,container) for ( __typeof ( container.begin () ) it = container.begin () ; it != container.end () ; it ++ )\n#define print(s,x) cout << \"> \" << s << \" \" << #x << \" : \" << x << endl\n#define print1(x) cout << \"> \" << #x << \" : \" << x << endl\n#define print2(x1,x2) cout << \"> \" << #x1 << \" : \" << x1 << \" , \" << #x2 << \" : \" << x2 << endl\n#define print3(x1,x2,x3) cout << \"> \" << #x1 << \" : \" << x1 << \" , \" << #x2 << \" : \" << x2 << \" , \" << #x3 << \" : \" << x3 << endl\n#define print4(x1,x2,x3,x4) cout << \"> \" << #x1 << \" : \" << x1 << \" , \" << #x2 << \" : \" << x2 << \" , \" << #x3 << \" : \" << x3 << \" , \" << #x4 << \" : \" << x4 << endl\n#define all(x) x.begin () , x.end ()\n#define mp make_pair\n#define pb push_back\n#define f first\n#define s second\n#define error 0.00000001\n#define lb lower_bound\n#define ub upper_bound\n\ntemplate < class T > void print_2darray ( const char * s , T arr , int n , int m )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n fie ( i , 0 , n )\n {\n cout << \"{\" ;\n fie ( j , 0 , m )\n {\n cout << arr [ i ] [ j ] << ( j == m - 1 ? \"\" : \",\" ) ;\n }\n cout << \"}\" << endl ;\n }\n cout << \"]\" << endl ;\n}\n\ntemplate < class T > void print_2darray ( const char * s , T arr , int n )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n fie ( i , 0 , n )\n {\n cout << i << \" - {\" ;\n fie ( j , 0 , ( int ) arr [ i ].size () )\n {\n cout << arr [ i ] [ j ] << ( j == ( int ) arr [ i ].size () - 1 ? \"\" : \",\" ) ;\n }\n cout << \"}\" << endl ;\n }\n cout << \"]\" << endl ;\n}\n\ntemplate < class T > void print_1darray ( const char * s , T arr , int n )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n fie ( i , 0 , n )\n {\n cout << arr [ i ] << ( i == n - 1 ? \"\" : \",\" ) ;\n }\n cout << endl << \"]\" << endl ;\n}\n\ntemplate < class T > void print_1darray ( const char * s , T arr )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n fie ( i , 0 , arr.size () )\n {\n cout << arr [ i ] << ( i == ( int ) arr.size () - 1 ? \"\" : \",\" ) ;\n }\n cout << endl << \"]\" << endl ;\n}\n\ntemplate < class T > void print_set ( const char * s , set < T > & myset )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n tr ( it , myset )\n {\n cout << * it << \" \" ;\n }\n cout << endl << \"]\" << endl ;\n}\n\ntemplate < class T > void print_uset ( const char * s , unordered_set < T > & myset )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n tr ( it , myset )\n {\n cout << * it << \" \" ;\n }\n cout << endl << \"]\" << endl ;\n}\n\ntemplate < class T1 , class T2 > void print_map ( const char * s , map < T1 , T2 > & mymap )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n tr ( it , mymap )\n {\n cout << \"{\" << it->f << \"->\" << it->s << \"}\" << endl ;\n }\n cout << \"]\" << endl ;\n}\n\ntemplate < class T1 , class T2 > void print_umap ( const char * s , unordered_map < T1 , T2 > & mymap )\n{\n cout << \"> \" << s << endl ;\n cout << \"[\" << endl ;\n tr ( it , mymap )\n {\n cout << \"{\" << it->f << \"->\" << it->s << \"}\" << endl ;\n }\n cout << \"]\" << endl ;\n}\n\n//----------------------------------------------------------------------------------------------------------------------------------------------\n\n//#define dbg 1\n\nconst int N = 200000 ;\nint n , x ;\nvi a ( N + 1 , -1 ) ;\nvi v [ N + 1 ] ;\nlli ans [ N + 1 ] ;\nlli all_ans = 0 ;\n\nstruct segment_tree\n{\n // st.set_vars ( n , 0 , arr ) ;\n // st.construct ( ) ;\n // st.get_sum ( 1 , 3 )\n // st.update_range ( 1 , 5 , 10 ) ; // add 10 to all nodes so difference = 10\n\n #define seg_size 1 << 19\n int n , init ;\n int * arr ;\n lli tree [ seg_size ] ;\n lli lazy [ seg_size ] ;\n int small [ seg_size ] ;\n int big [ seg_size ] ;\n\n void set_vars ( int _n , int _init , int * _arr = NULL )\n {\n n = _n ;\n init = _init ;\n arr = _arr ;\n //fill_n ( & small [ 0 ] , seg_size , 0 ) ;\n //fill_n ( & big [ 0 ] , seg_size , 0 ) ;\n if ( init != 0 )\n {\n fill_n ( & tree [ 0 ] , seg_size , init ) ;\n fill_n ( & lazy [ 0 ] , seg_size , init ) ;\n }\n }\n\n void updaterange_util ( int si , int ss , int se , int us , int ue , lli diff )\n {\n if ( lazy [ si ] != init )\n {\n small [ si ] = big [ si ] = lazy [ si ] ;\n tree [ si ] = ( se - ss + 1 ) * lazy [ si ] ;\n if ( ss != se )\n {\n lazy [ si * 2 + 1 ] = lazy [ si ] ;\n lazy [ si * 2 + 2 ] = lazy [ si ] ;\n }\n lazy [ si ] = init ;\n }\n if ( ss > se || ss > ue || se < us || small [ si ] >= diff )\n {\n return ;\n }\n if ( ss >= us && se <= ue && big [ si ] <= diff )\n {\n tree [ si ] = ( se - ss + 1 ) * diff ;\n small [ si ] = big [ si ] = diff ;\n if ( ss != se )\n {\n lazy [ si * 2 + 1 ] = diff ;\n lazy [ si * 2 + 2 ] = diff ;\n }\n return ;\n }\n int mid = ( ss + se ) / 2 ;\n updaterange_util ( si * 2 + 1 , ss , mid , us , ue , diff ) ;\n updaterange_util ( si * 2 + 2 , mid + 1 , se , us , ue , diff ) ;\n small [ si ] = min ( small [ si * 2 + 1 ] , small [ si * 2 + 2 ] ) ;\n big [ si ] = max ( big [ si * 2 + 1 ] , big [ si * 2 + 2 ] ) ;\n tree [ si ] = tree [ si * 2 + 1 ] + tree [ si * 2 + 2 ] ;\n }\n\n void update_range ( int us , int ue , lli diff )\n {\n updaterange_util ( 0 , 0 , n - 1 , us , ue , diff ) ;\n }\n} st ;\n\nint main ()\n{\n cin >> n ;\n if ( n <= 2 )\n {\n cout << 0 ;\n return 0 ;\n }\n fie ( i , 0 , n )\n {\n cin >> x ;\n a [ x ] = i ;\n }\n fii ( i , 1 , N )\n {\n for ( int j = i ; j <= N ; j += i )\n {\n if ( a [ j ] != -1 )\n {\n v [ i ].pb ( a [ j ] ) ;\n }\n }\n sort ( all ( v [ i ] ) ) ;\n }\n st.set_vars ( n , 0 ) ;\n fii ( i , 0 , N )\n {\n st.update_range ( i , i , i ) ;\n }\n ans [ N ] = ( ( lli ) n * ( lli ) ( n + 1 ) ) / 2LL ;\n ans [ 0 ] = 3 ;\n fdei ( i , N , 1 )\n {\n int len = v [ i ].size () ;\n if ( len <= 1 )\n {\n ans [ i ] = -1 ;\n #ifdef dbg\n if ( i <= 7 )\n {\n print ( i , ans [ i ] ) ;\n }\n #endif\n continue ;\n }\n ans [ i ] = ( lli ) n * ( lli ) ( n ) - st.tree [ 0 ] ;\n #ifdef dbg\n print ( i , ans [ i ] ) ;\n cout << i << \" val @ root = \" << st.tree [ 0 ] << endl ;\n #endif\n st.update_range ( 0 , v [ i ] [ 0 ] , v [ i ] [ len - 2 ] ) ;\n st.update_range ( v [ i ] [ 0 ] + 1 , v [ i ] [ 1 ] , v [ i ] [ len - 1 ] ) ;\n st.update_range ( v [ i ] [ 1 ] + 1 , n - 1 , n ) ;\n }\n fdii ( i , N , 1 )\n {\n int cur = i - 1 ;\n while ( ans [ cur ] == -1 )\n {\n cur -- ;\n }\n all_ans += ( ans [ i ] - ans [ cur ] ) * ( lli ) i ;\n i = cur ;\n i ++ ;\n }\n cout << all_ans ;\n\n return 0 ;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "28f79ac8f363693b73a07ae976255257", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nconst int maxn=200010;\nvector v[maxn];\nlong long h[maxn];\nint n;\n\nstruct nodetree\n{\n int l,r;\n long long x,y;\n long long lazy;\n long long sum;\n}a[maxn<<2];\n\nvoid push_up(int i)\n{\n a[i].x=min(a[2*i].x,a[2*i+1].x);\n a[i].y=max(a[2*i].y,a[2*i+1].y);\n a[i].sum=a[2*i].sum+a[2*i+1].sum;\n}\n\nvoid push_down(int i)\n{\n if(a[i].l!=a[i].r&&a[i].lazy!=-1)\n {\n a[2*i].x=a[2*i].y=a[i].lazy;\n a[2*i].sum=a[i].lazy*(a[2*i].r-a[2*i].l+1);\n a[2*i].lazy=a[i].lazy;\n\n a[2*i+1].x=a[2*i+1].y=a[i].lazy;\n a[2*i+1].sum=a[i].lazy*(a[2*i+1].r-a[2*i+1].l+1);\n a[2*i+1].lazy=a[i].lazy;\n\n a[i].lazy=-1;\n }\n}\n\nvoid init(int i,int l,int r)\n{\n a[i].l=l;\n a[i].r=r;\n a[i].lazy=-1;\n if(l==r)\n {\n a[i].x=l;\n a[i].y=l;\n a[i].sum=l;\n return;\n }\n int mid=(l+r)/2;\n init(2*i,l,mid);\n init(2*i+1,mid+1,r);\n push_up(i);\n}\n\nlong long find(int i,int num)\n{\n push_down(i);\n if(a[i].y<=num)\n return a[i].r;\n if(a[i].x>=num)\n return -1;\n\n int tmp=find(2*i+1,num);\n if(tmp!=-1)\n return tmp;\n else\n return find(2*i,num);\n}\n\nvoid insert(int i,int x,int y,long long num)\n{\n push_down(i);\n int l=a[i].l,r=a[i].r;\n if(l==x&&r==y)\n {\n a[i].x=num;\n a[i].y=num;\n a[i].sum=(r-l+1)*num;\n a[i].lazy=num;\n return;\n }\n int mid=(l+r)/2;\n if(y<=mid)\n insert(2*i,x,y,num);\n else if(x>=mid+1)\n insert(2*i+1,x,y,num);\n else\n {\n insert(2*i,x,mid,num);\n insert(2*i+1,mid+1,y,num);\n }\n push_up(i);\n}\n\nvoid update(int x,int y,int num)\n{\n if(x>y)\n return;\n int tmp=find(1,num);\n tmp=min(tmp,y);\n if(tmp>=x)\n insert(1,x,tmp,num);\n}\n\nint main()\n{\n scanf(\"%d\",&n);\n for(int i=1;i<=n;i++)\n {\n int tmp;\n scanf(\"%d\",&tmp);\n for(int j=1;j*j<=tmp;j++)\n {\n if(tmp%j==0)\n {\n v[j].push_back(i);\n if(j*j!=tmp)\n v[tmp/j].push_back(i);\n }\n }\n }\n for(int i=1;i<=200000;i++)\n sort(v[i].begin(),v[i].end());\n\n /*\n for(int i=1;i<=16;i++)\n {\n for(int j=0;j=1;i--)\n {\n h[i]=(long long)n*(n+1)-a[1].sum;\n int siz=v[i].size();\n if(siz<=1)\n continue;\n else if(siz==2)\n {\n int op=v[i][0],ed=v[i][1];\n update(1,op-1,op);\n update(op+1,ed-1,ed);\n update(ed+1,n,n+1);\n }\n else\n {\n update(1,v[i][0],v[i][siz-2]);\n update(v[i][0]+1,v[i][1],v[i][siz-1]);\n update(v[i][1]+1,n,n+1);\n }\n }\n\n h[0]=3;\n long long ans=0;\n for(int i=1;i<=200000;i++)\n ans+=(h[i]-h[i-1])*i;\n printf(\"%I64d\\n\",ans);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "b99be772893aaa8c9d217989a6f85e8a", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\nconst int int_inf = 0x3f3f3f3f;\nconst double double_inf = 1e10 * 1e10;\ntypedef unsigned long long ul;\n#define max(a, b) ((a) > (b) ? (a) : (b))\n#define min(a, b) ((a) < (b) ? (a) : (b))\n#define mp make_pair\n#define st first\n#define nd second\n#define pii pair\n#define pb push_back\n#define type(x) __typeof(x.begin())\n#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)\n#define FOR(i, s, t) for(int i = s; i <= t; i++)\n#define ROF(i, t, s) for(int i = t; i >= s; i--)\nint debug = 0;\n\n//-------------------------------------------------------------------------\nconst int maxn = 2e5 + 10;\nint a[maxn], n;\nll sum;\nll ans[maxn];\nset > S;\nvector H[maxn], v[maxn];\nset > :: iterator it, it2;\n//-------------------------------------------------------------------------\n\nvoid divide(int x){\n it = S.lower_bound(mp(mp(x + 1, 0), -1));\n if(it == S.begin()) return;\n --it;\n pair t = *it;\n if(t.st.nd < x) return;\n S.erase(it);\n S.insert(mp(mp(t.st.st, x), t.nd));\n if(t.st.nd >= x + 1) S.insert(mp(mp(x + 1, t.st.nd), t.nd));\n}\n\nvoid remove(int x){\n divide(x - 1);\n while(S.rbegin()->st.st >= x){\n pair t = *S.rbegin();\n sum -= (ll)t.nd * (t.st.nd - t.st.st + 1);\n S.erase(t);\n }\n}\n\nvoid maximize(int l, int r, int maxi){\n it = S.lower_bound(mp(mp(l, 0), 0));\n pair t;\n t.st.st = l, t.nd = maxi;\n while(it != S.end()){\n if(it->nd >= maxi) break;\n if(it->st.st > r) break;\n t.st.nd = it->st.nd;\n sum -= (ll)it->nd * (it->st.nd - it->st.st + 1);\n it2 = it; it2++; S.erase(it); it = it2;\n }\n if(t.st.st <= t.st.nd){\n sum += (ll)t.nd * (t.st.nd - t.st.st + 1);\n S.insert(t);\n }\n}\n\nint main(){\n if(debug) freopen(\"in.txt\", \"r\", stdin);\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////\n while(~scanf(\"%d\", &n)){\n sum = 0;\n S.clear();\n FOR(i, 0, maxn - 1) v[i].clear(), H[i].clear();\n FOR(i, 1, n){\n scanf(\"%d\", &a[i]);\n sum += i;\n H[a[i]].pb(i);\n S.insert(mp(mp(i, i), i));\n }\n FOR(i, 1, maxn - 1){\n for(int j = i; j < maxn; j += i)\n foreach(it, H[j]) v[i].pb(*it);\n }\n ans[maxn - 1] = (ll)n * (n + 1) >> 1;\n ROF(i, maxn - 2, 1){\n int l = v[i].size() - 1;\n if(l <= 0) { ans[i] = ans[i + 1]; continue; }\n sort(v[i].begin(), v[i].end());\n remove(v[i][1] + 1);\n divide(v[i][0]);\n maximize(v[i][0] + 1, n, v[i][l]);\n maximize(1, v[i][0], v[i][l - 1]);\n // divide(v[i][0]);\n // maximize(v[i][0] + 1, v[i][l]);\n ans[i] = (ll)S.rbegin()->st.nd * (n + 1) - sum;\n }\n ll tot = 0;\n ans[0] = 0;\n FOR(i, 1, maxn - 2) tot += (ll)i * (ans[i + 1] - ans[i]);\n printf(\"%lld\\n\", tot);\n }\n\n ///////////////////////////////////////////////////////////////////////////////////////////////////////////////\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "2026ccc57bd32f9273c8d0848d04e107", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\n assume that a[x] and a[y]'s gcd is largest\n if the interval [i,j] deleted not included subscript x or y, then f(i,j) still equal to gcd(a[x], a[y])\n therefore we enumerate the maximum of gcd(from max(a[1..n]) down to 1)\n for each gcdi we enumerate there may exist some element in array a[] which can be divided by gcdi\n we record their subscript then sort these subscripts\n assume there are k elements we record (k >= 2)\n it's clear that only three situations should be thought about\n 1. we reserve the first and second sorted elements we record\n that is we can remove any interval[i, j] in interval[a[second]+1, n], and f(i, j) still equals to gcdi\n 2. we reserve last two sorted elements we record\n that is we can remove any interval[i, j] in interval[1, a[k-1]-1], and f(i, j) still equals to gcdi\n 3. we reserve the first and last sorted elements we record\n that is we can remove any interval[i, j] in interval[a[first]+1, a[last]-1], and f(i,j) still equals to gcdi\n then we donate board[i] as we can remove any interval[i, j] in interval[i, board[i]] it's clear that board[i] should be maximum\n if board[i] equals to n+1 we say all of intervals started with subscript i can be removes and then we will not care it any more\n we set cnt[1] donating the sum of remaining intervals should be removed and the initial of cnt[1] = n*(n+1)/2 and tot_cnt = n*(n+1)\n if there are another x intervals be removed, then we add x to cnt[1] (cnt[1] = cnt[1'] + x)\n the sum of remaining intervals should be removed equals tot_cnt - cnt[1] (cnt_inv[gcdi-1] = tot_cnt - cnt[1])\n and x equals to cnt_inv[gcdi] - cnt_inv[gcdi-1] (cnt_inv[gcdi] - cnt_inv[gcdi-1] = tot_cnt - cnt[1'] - (tot_cnt - (cnt[1'] + x)) = -cnt[1'] + cnt[1'] + x = x)\n and the contribution of gcdi to answer equals to (cnt_inv[gcdi] - cnt_inv[gcdi-1]) * gcdi\n*/\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long LL;\n\nconst int MAXN = 200000 + 5;\n\nint n;\nint a[MAXN];\nint exist[MAXN];\nint _max;\nint lazy[4*MAXN];\nint board[4*MAXN];\nlong long cnt[4*MAXN];\nlong long tot_inv, cnt_inv[MAXN];\n\nvoid pushup(int index) {\n board[index] = min(board[index*2], board[index*2+1]);\n cnt[index] = cnt[index*2] + cnt[index*2+1];\n}\n\nvoid pushdown(int index) {\n if (lazy[index] == 1) {\n long long s = (cnt[index]/board[index]) / 2 + (cnt[index]/board[index]) % 2;\n cnt[index*2] = s * board[index];\n cnt[index*2+1] = cnt[index] - s * board[index];\n board[index*2] = board[index*2+1] = board[index];\n lazy[index*2] = lazy[index*2+1] = 1;\n lazy[index] = 0;\n }\n}\n\nvoid build(int index, int l, int r) {\n if (l == r) {\n board[index] = l;\n cnt[index] = l;\n return;\n }\n\n int mid = (l+r) / 2;\n build(index*2, l, mid);\n build(index*2+1, mid+1, r);\n pushup(index);\n}\n\nvoid update(int index, int l, int r, int left, int right, int BOARD) {\n if (left <= l && right >= r) {\n if (BOARD > board[index]) {\n board[index] = BOARD;\n cnt[index] = LL(r-l+1) * BOARD;\n lazy[index] = 1;\n }\n return;\n }\n pushdown(index);\n int mid = (l+r) / 2;\n if (left <= mid)\n update(index*2, l, mid, left, right, BOARD);\n if (mid+1 <= right)\n update(index*2+1, mid+1, r, left, right, BOARD);\n pushup(index);\n}\n\nint find(int index, int l, int r, int BOARD) {\n if (l == r)\n return l;\n\n int mid = (l+r) / 2;\n pushdown(index);\n if (board[index*2+1] < BOARD)\n return find(index*2+1, mid+1, r, BOARD);\n else\n return find(index*2, l, mid, BOARD);\n}\n\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n memset(exist, 0, sizeof(exist));\n for (int i = 1; i <= n; i++) {\n exist[a[i]] = i;\n _max = max(_max, a[i]);\n }\n\n build(1, 1, n);\n tot_inv = LL(n) * (n+1);\n cnt_inv[_max+1] = tot_inv - cnt[1]; cnt_inv[0] = 3;\n for (int i = _max; i >= 1; i--) {\n vector v;\n\n v.clear();\n for (int j = i; j <= _max; j += i) {\n if (exist[j] > 0)\n v.push_back(exist[j]);\n }\n\n cnt_inv[i] = tot_inv - cnt[1];\n int k = v.size();\n if (k < 2)\n continue;\n\n sort(v.begin(), v.end());\n if (v[1]+1 <= n) {\n update(1, 1, n, v[1]+1, n, n+1);\n }\n if (board[1] < v[k-1]) {\n int pos = find(1, 1, n, v[k-1]);\n pos = min(pos, v[k-1]-1);\n update(1, 1, n, v[0]+1, pos, v[k-1]);\n }\n if (board[1] < v[k-2]) {\n int pos = find(1, 1, n, v[k-2]);\n pos = min(pos, v[k-2]-1);\n update(1, 1, n, 1, pos, v[k-2]);\n }\n }\n\n long long ans = 0;\n for (int i = _max; i >= 1; i--)\n ans += LL(cnt_inv[i]-cnt_inv[i-1]) * i;\n\n printf(\"%I64d\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "ffd2613c453d3d9510983ebe322299e0", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\nCFR-350 2E\nset/线段树+思维 好题\nhttps://www.cnblogs.com/Blue233333/p/8075440.html\nhttps://blog.csdn.net/u010734277/article/details/51538249 (官方题解译文)\nhttp://codeforces.com/blog/entry/44821 (官方题解)\n*/\n//线段树\n#include \nusing namespace std;\ntypedef long long ll;\nconst int N = 2e5+5;\nll H[N];\nint a[N];\nll c[N<<2];//sum\nll s[N<<2];//min\nll lz[N<<2];//lazy\nvector v[N];//ve[i]:i的倍数的下标\nint u,yl,yr;\nvoid build(int o, int l, int r){\n if (l==r){\n lz[o]=l;\n c[o]=l; \n s[o]=l;\n return;\n }\n int mid=(l+r)>>1;\n build(o<<1,l,mid);\n build(o<<1|1,mid+1,r);\n lz[o]=-1;\n c[o]=c[o<<1]+c[o<<1|1];\n s[o]=min(s[o<<1],s[o<<1|1]);\n}\nvoid pushdown(int o, int l, int r){\n if (l==r) return;\n if (lz[o]==-1) return;\n lz[o<<1]=lz[o]; lz[o<<1|1]=lz[o];\n s[o<<1]=lz[o]; s[o<<1|1]=lz[o];\n int mid=(l+r)>>1;\n c[o<<1]=lz[o]*(mid-l+1);\n c[o<<1|1]=lz[o]*(r-mid);\n lz[o]=-1;\n}\nvoid upd(int o, int l, int r){\n if (yl<=l&&yr>=r){\n c[o]=1LL*u*(r-l+1);\n s[o]=u;\n lz[o]=u;\n return;\n }\n pushdown(o,l,r);\n int mid=(l+r)>>1;\n if (yl<=mid) upd(o<<1,l,mid);\n if (yr>mid) upd(o<<1|1,mid+1,r);\n c[o]=c[o<<1]+c[o<<1|1];\n s[o]=min(s[o<<1],s[o<<1|1]);\n if (lz[o<<1]==lz[o<<1|1]&&lz[o<<1]>=0) lz[o]=lz[o<<1];\n}\nint query(int o, int l, int r){//返回小于k的最右侧的nexti的下标i \n if (l==r&&c[o]=u) return 0;\n if (lz[o]!=-1&&lz[o]=u) return 0;\n int mid=(l+r)>>1;\n if (s[o<<1|1]=0; i--){\n H[i]=sum-c[1];\n int k=v[i].size();\n if (k<2) continue;\n u=n+1;\n yl=v[i][1]+1; yr=n;\n if (yl<=yr) upd(1,1,n);//成段更新\n\n u=v[i][k-1];\n int pos=query(1,1,n);//Returns rightmost index i where nexti is less than some k\n pos=min(pos,v[i][1]);\n if (pos>v[i][0]&&pos<=v[i][1]){\n yl=v[i][0]+1,yr=pos;\n upd(1,1,n);\n }\n\n u=v[i][k-2];\n pos=query(1,1,n);\n pos=min(pos,v[i][0]);\n if (pos>=1&&pos<=v[i][0]){\n yl=1,yr=pos;\n upd(1,1,n);\n }\n }\n ll ans=0;\n for (int i=1; i<=mx; i++) ans+=1LL*i*(H[i]-H[i-1]);\n cout<\nusing namespace std;\n\n#define reset(a, x) memset(a, x, sizeof(a))\n\nconst int MAX = (int)2E5 + 10;\n\nint n;\nint l1[MAX], l2[MAX], r1[MAX], r2[MAX];\nlong long h[MAX];\n\nstruct node{\n long long s;\n int l, mn, mx;\n};\n\nnode smt[4*MAX];\n\nvoid combine(int id, int c1, int c2){\n smt[id].s = smt[c1].s + smt[c2].s;\n smt[id].mn = min(smt[c1].mn, smt[c2].mn);\n smt[id].mx = max(smt[c1].mx, smt[c2].mx);\n}\n\nvoid update(int id, int l, int r, int x, int y, int v){\n if(y < l || x > r || smt[id].mn >= v) return;\n if(x <= l && r <= y && smt[id].mx <= v){\n smt[id].mn = smt[id].mx = smt[id].l = v;\n smt[id].s = (long long)(r - l + 1)*v;\n return;\n }\n int c1 = id*2 + 1; int c2 = id*2 + 2; int mid = (l + r)/2;\n if(smt[id].l != -1){\n smt[c1].mn = smt[c1].mx = smt[c1].l = smt[c2].mn = smt[c2].mx = smt[c2].l = smt[id].l;\n smt[c1].s = (long long)(mid - l + 1)*smt[id].l;\n smt[c2].s = (long long)(r - (mid + 1) + 1)*smt[id].l;\n smt[id].l = -1;\n }\n update(c1, l, mid, x, y, v);\n update(c2, mid + 1, r, x, y, v);\n combine(id, c1, c2);\n}\n\nvoid update(int x, int y, int v){\n update(0, 0, n - 1, x, y, v);\n}\n\nvoid build(int id, int l, int r){\n smt[id].l = -1;\n if(l == r){\n smt[id].mn = smt[id].mx = smt[id].s = l;\n }else{\n int c1 = id*2 + 1; int c2 = id*2 + 2;\n \n build(c1, l, (l + r)/2);\n build(c2, (l + r)/2 + 1, r);\n\n combine(id, c1, c2);\n }\n}\n\nint main(){\n// freopen(\"e.in\", \"r\", stdin);\n// freopen(\"e.out\", \"w\", stdout);\n reset(l1, 255); reset(l2, 255); reset(r1, 255); reset(r2, 255);\n scanf(\"%d\", &n); \n for(int i = 0; i < n; i++){\n int a;\n scanf(\"%d\", &a);\n for(int j = 1; j <= (int)sqrt(a); j++)\n if(a%j == 0){\n if(l1[j] == -1) l1[j] = i;\n else if(l2[j] == -1) l2[j] = i; \n r2[j] = r1[j]; r1[j] = i;\n \n if(a/j != j){\n if(l1[a/j] == -1) l1[a/j] = i;\n else if(l2[a/j] == -1) l2[a/j] = i; \n r2[a/j] = r1[a/j]; r1[a/j] = i;\n }\n } \n }\n \n build(0, 0, n - 1); \n for(int i = 2E5 + 1; i >= 1; i--){\n if(l1[i] != -1 && l2[i] != -1){\n update(l2[i] + 1, n - 1, n);\n update(l1[i] + 1, l2[i], r1[i]);\n update(0, l1[i], r2[i]);\n }\n h[i - 1] = (long long)n*n - smt[0].s;\n }\n// cout << smt[0].s << endl;\n \n long long res = 0;\n for(int i = 1; i < 2E5 + 1; i++) res += (h[i] - h[i - 1])*i;\n\n// for(int i = 0; i < 10; i++) cout << h[i] << endl;\n \n printf(\"%I64d\", res);\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "57e8e48f56e2f2bedf04c0c82cad719e", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\n * Problem: cf 672 E\n * User: Yuuki_Asuna\n * Data: 2016/5/24\n */\n\n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define N 200008\n#define M 200005\n\nint n;\nint a[N],l1[N],r1[N],l2[N],r2[N];\nlong long h[N];\n\nstruct Tree\n{\n\tint maxn[4*N],minn[4*N],flag[4*N];\n\tlong long sum[4*N];\n\t\n\tinline void mark(int p,int l,int r,int x)\n\t{\n\t\tmaxn[p] = minn[p] = x;\n\t\tsum[p] = (long long)x*(r-l+1);\n\t\tflag[p] = x;\n\t}\n\t\n\tinline void push_down(int p,int l,int r)\n\t{\n\t\tif (flag[p])\n\t\t{\n\t\t\tif (l == r)\treturn;\n\t\t\tint mid = (l+r) >> 1;\n\t\t\tmark(p*2,l,mid,flag[p]);\n\t\t\tmark(p*2+1,mid+1,r,flag[p]);\n\t\t\tflag[p] = 0;\n\t\t}\n\t}\n\t\n\tvoid insert(int p,int l,int r,int ll,int rr,int x)\n\t{\n\t\tif (minn[p] >= x)\treturn;\n\t\tif(ll <= l && rr >= r && x >= maxn[p])\n\t\t{\n\t\t\tmark(p,l,r,x);\n\t\t\treturn;\t\t\n\t\t}\n\t\tif (l == r)\treturn;\n\t\tpush_down(p,l,r);\n\t\tint mid = (l+r) >> 1;\n\t\tif (ll <= mid)\tinsert(p*2,l,mid,ll,rr,x);\n\t\tif (rr > mid)\tinsert(p*2+1,mid+1,r,ll,rr,x);\n\t\tmaxn[p] = max(maxn[p*2],maxn[p*2+1]);\n\t\tminn[p] = min(minn[p*2],minn[p*2+1]);\n\t\tsum[p] = sum[p*2] + sum[p*2+1];\n\t}\n\t\n}T;\n\nvoid init()\n{\n\tscanf(\"%d\", &n);\n\tfor (int i = 1; i <= n; i++)\n\t\tscanf(\"%d\", &a[i]);\n}\n\ninline void add(int j,int i)\n{\n\tif (!l1[j])\tl1[j] = i;\n\telse if (!l2[j]) l2[j] = i;\n\tr1[j] = r2[j];\n\tr2[j] = i;\n}\n\nvoid prepare()\n{\n\tfor (int i = 1; i <= n; i++)\n\t{\n\t\tfor (int j = 1; j <= (int)sqrt((double)a[i]); j++)\n\t\t{\n\t\t\tif (a[i] % j != 0)\tcontinue;\n\t\t\tadd(j,i);\n\t\t\tif (a[i] / j == j)\tcontinue;\n\t\t\tadd(a[i]/j,i);\n\t\t}\n\t}\n}\n\nvoid work()\n{\n\tprepare();\n\tlong long ans = 0;\n\tfor (int i = 1; i <= n; i++)\n\t\tT.insert(1,1,n,i,i,i);\n\th[M] = (long long)n*(n+1)/2;\n\tfor (int i = M-1; i >= 0; i--)\n\t{\n\t\th[i] = h[i+1];\n\t\tif (l1[i] == r2[i])\tcontinue;\n\t\tT.insert(1,1,n,1,l1[i],r1[i]);\n\t\tT.insert(1,1,n,l1[i]+1,l2[i],r2[i]);\n\t\tif (l2[i]+1 <= n)\n\t\t\tT.insert(1,1,n,l2[i]+1,n,n+1);\n\t\th[i] = (long long)n*(n+1)-T.sum[1];\n\t}\n\tfor (int i = 1; i < M; i++)\n\t\tans += ((long long)h[i] - h[i-1]) * (i-1);\n\tprintf(\"%I64d\\n\", ans);\n}\n\nint main()\n{\n\tinit();\n\twork();\n\treturn 0; \n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "043797f7780575782fe15c7ff17eb73e", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#pragma comment(linker, \"/STACK:102400000,102400000\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC diagnostic error \"-std=c++11\" \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define inf 0x3f3f3f3f\n#define go(a,b,c) for(int a=b;a<=c;++a)\n#define gov(a,b) for(int a=0;a=c;--a)\n#define sf(a) scanf(\"%d\",&a)\n#define sff(a,b) scanf(\"%d%d\",&a,&b)\n#define sfff(a,b,c) scanf(\"%d%d%d\",&a,&b,&c)\n#define eps 1e-9\n#define ft first\n#define sd second\n#define prt(a) printf(\"%d\\n\",a)\n#define prn(i,n,a) printf(\"%d%c\",a,i==n?'\\n':' ')\n#define pb push_back\n#define D(v) cerr<<#v<<\" \"< pii;\ntypedef set si;\ntypedef vector vi;\ntypedef map mii;\ntypedef double db;\nconst int maxn=2e5+10;\nconst LL mod=1e9+7;\ninline int inverse(int a){return a <= 1 ? 1 : (LL)(mod - mod / a) * inverse(mod % a) % mod;}\nint gcd(int a,int b){return b?gcd(b,a%b):a;}\nLL fsp(LL a,LL b){LL ans=1;for(;b;a=a*a%mod,b>>=1)if(b&1)ans=ans*a%mod;return ans%mod;}\ninline void read(int &x)\n{\n\tx=0;\n\tint f=1;\n\tchar ch=getchar();\n\twhile(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}\n\twhile(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}\n}\n//=======================================================\nstruct segtree\n{\n\tLL f[maxn*4],mn[maxn*4];\n\tvoid push_up(int rt)\n\t{\n\t\tint ls=rt<<1,rs=rt<<1|1;\n\t\tmn[rt]=min(mn[ls],mn[rs]);\n\t\tf[rt]=f[ls]+f[rs];\n\t}\n\tvoid build(int rt,int l,int r)\n\t{\n\t\tif(l==r){f[rt]=mn[rt]=l;return;}\n\t\tint m=(l+r)/2,ls=rt<<1,rs=rt<<1|1;\n\t\tbuild(ls,l,m);\n\t\tbuild(rs,m+1,r);\n\t\tpush_up(rt);\n\t}\n\tvoid push_down(int rt,int l,int r,int val)\n\t{\n\t\tif(mn[rt]>=val)return;\n\t\tif(l==r){mn[rt]=f[rt]=val;return;}\n\t\tint m=(l+r)/2,ls=rt<<1,rs=rt<<1|1;\n\t\tif(mn[ls]=L&&r<=R){push_down(rt,l,r,val);return;}\n\t\tint m=(l+r)/2,ls=rt<<1,rs=rt<<1|1;\n\t\tif(m>=L)change(ls,l,m,L,R,val);\n\t\tif(m=1;i--)\n\t{\n\t\tif(k[i].size()>=2)\n\t\t{\n\t\t\tint sz=(int)k[i].size();\n\t\t\tst.change(1,1,n,1,k[i][0],k[i][sz-2]);\n\t\t\tst.change(1,1,n,k[i][0]+1,k[i][1],k[i][sz-1]);\n\t\t\tif(k[i][1]+1<=n)st.change(1,1,n,k[i][1]+1,n,n+1);\n\t\t//\tif(k[i][sz-1]==n&&k[i][sz-2]==n-1)st.change(1,1,n,1,1,n+1);\n\t\t//\tif(k[i][0]==1&&k[i][sz-1]==n)st.change(1,1,n,2,2,n+1);\n\t\t}\n\t\th[i]=1LL*(n+1)*n-st.f[1];\n\t}\n\tLL ans=0;\n\tgo(i,1,mx)\n\t{\n\t//\tD(h[i]);\n\t\tans+=1LL*(h[i+1]-h[i])*i;\n\t}\n\tcout<\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\nll H[200005];\nint a[200005];\nll c[200005 << 2];\nll s[200005 << 2];\nll lz[200005 << 2];\nvector v[200005];\n\nint u;\nint yl,yr;\nvoid build(int o,int l,int r)\n{\n if(l == r){\n lz[o] = l;\n c[o] = l;s[o] = l;return;\n }\n int mid = (l+r) >> 1;\n build(o*2,l,mid);\n build(o*2+1,mid+1,r);\n lz[o] = -1;\n c[o] = c[o*2] + c[o*2+1];\n s[o] = min(s[o*2],s[o*2+1]);\n}\n\nvoid pushdown(int o,int l,int r)\n{\n if(l == r)return;\n if(lz[o] == -1)return;\n lz[o * 2] = lz[o];lz[o*2+1] = lz[o];\n s[o*2] = lz[o];s[o*2+1]=lz[o];\n int mid = (l+r) >> 1;\n c[o*2] = lz[o] * (mid - l + 1);\n c[o*2+1] = lz[o] * (r - mid);\n lz[o] = -1;\n}\n\nvoid update(int o,int l,int r)\n{\n if(yl <= l && yr >= r){\n c[o] = 1LL * u *(r - l + 1);\n s[o] = u;\n lz[o] = u;return;\n }\n pushdown(o,l,r);\n int mid = (l + r) >> 1;\n if(yl <= mid)update(o*2,l,mid);\n if(yr > mid) update(o*2+1 , mid+1,r);\n c[o] = c[o*2] + c[o*2+1];\n s[o] = min(s[o*2],s[o*2+1]);\n if(lz[o*2] == lz[o*2+1] && lz[o*2] >= 0)lz[o] = lz[o*2];\n}\n\nint query(int o,int l,int r)\n{\n if(l == r && c[o] < u )return l;\n if(l == r && c[o] >=u )return 0;\n if(lz[o]!=-1 && lz[o] < u)return r;\n if(lz[o] != -1 && lz[o] >= u)return 0;\n int mid = (l+r)>>1;\n if(s[o*2+1] < u)return query(o*2+1,mid+1,r);\n else return query(o*2,l,mid);\n}\n\nint main()\n{\n// freopen(\"data.txt\",\"r\",stdin);\n int n;\n scanf(\"%d\",&n);\n int mx = -1;\n memset(a,0,sizeof(a));\n for(int i = 1; i < n+1 ; ++i){\n int tmp;\n scanf(\"%d\",&tmp);\n a[tmp] = i;\n mx = max(mx,tmp);\n }\n for(int i = 1; i <=mx; ++i){\n for(int j = 1;j*i<=mx;++j){\n if(a[j*i])\n v[i].push_back(a[j*i]);\n }\n }\n for(int i = 0; i <= mx;++i)sort(v[i].begin(),v[i].end());\n ll sum =1LL * n * (n+1);\n build(1,1,n);\n for(int i = mx; i >= 0; --i){\n H[i] = sum - c[1];\n int k = v[i].size();\n if(k < 2){continue;}\n u = n + 1;\n yl = v[i][1]+1;yr = n;\n if(yl <= yr)\n update(1,1,n);\n\n u = v[i][k-1];\n int pos = query(1,1,n);\n pos = min(pos , v[i][1]);\n if(pos > v[i][0] && pos <= v[i][1]){\n yl = v[i][0] + 1;yr = pos;\n update(1,1,n);\n }\n\n u = v[i][k-2];\n pos = query(1,1,n);\n pos = min(pos , v[i][0]);\n if(pos >= 1 && pos <= v[i][0]){\n yl = 1;yr = pos;\n update(1,1,n);\n }\n\n }\n ll ans = 0;\n for(int i = 1 ;i <= mx; ++i){\n ans += 1LL * i * (H[i] - H[i-1]);\n }\n printf(\"%I64d\\n\",ans);\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "106d4c9ba84657979c0cbc8521c0166b", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "// God & me\n// The life is over ; (\n#include \n#define pb push_back\n#define X first\n#define Y second\n#define int long long\nusing namespace std;\ntemplate bool smax(T &x,L y){ return x < y ? (x = y, 1) : 0; }\ntemplate bool smin(T &x,L y){ return y < x ? (x = y, 1) : 0; }\ntypedef pair pii;\n\nconst int maxn = 2e5 + 17;\nint iman[maxn << 2], sina[maxn << 2], n, a[maxn], dj[maxn << 2];\nvoid majid(int l = 0, int r = n, int id = 1){\n dj[id] = l;\n if(l + 1 == r){\n\timan[id] = l;\n\treturn ;\n }\n int mid = l + r >> 1;\n majid(l, mid, id << 1), majid(mid, r, id << 1 | 1);\n iman[id] = iman[id << 1] + iman[id << 1 | 1];\n}\nvoid upd(int l, int r, int id, int x){\n iman[id] = x * (r - l);\n dj[id] = sina[id] = x;\n}\nvoid sadra(int l, int r, int id){\n if(sina[id] == -1) return;\n int mid = l + r >> 1;\n upd(l, mid, id << 1, sina[id]);\n upd(mid, r, id << 1 | 1, sina[id]);\n sina[id] = -1;\n}\nvoid arpa(int s, int e, int x, int l = 0, int r = n, int id = 1){\n if(s <= l && r <= e){\n\tupd(l, r, id, x);\n\treturn;\n }\n if(e <= l || r <= s) return;\n sadra(l, r, id);\n int mid = l + r >> 1;\n arpa(s, e, x, l, mid, id << 1), arpa(s, e, x, mid, r, id << 1 | 1);\n iman[id] = iman[id << 1] + iman[id << 1 | 1];\n dj[id] = dj[id << 1];\n}\nint doktor(int s, int e, int x, int l = 0, int r = n, int id = 1){\n if(e <= l || r <= s || dj[id] > x) return -1;\n if(l + 1 == r) return l;\n int mid = l + r >> 1, ans = doktor(s, e, x, mid, r, id << 1 | 1);\n return ~ans ? ans : doktor(s, e, x, l, mid, id << 1);\n}\nvoid amir(int s, int e, int x){ arpa(s, doktor(s, e, x) + 1, x); }\nvectormp[maxn], ass[maxn];\nint ps[maxn];\nmain(){\n ios::sync_with_stdio(0), cin.tie(0);\n cin >> n;\n for(int i = 0; i < n; i++)\n\tcin >> a[i], mp[ a[i] ].pb(i);\n majid();\n for(int i = 1; i < maxn; i++){\n\tfor(int j = i; j < maxn; j += i)\n\t ass[i].insert(ass[i].end(), mp[j].begin(), mp[j].end());\n\tsort(ass[i].begin(), ass[i].end());\n\tass[i].resize( unique(ass[i].begin(), ass[i].end()) - ass[i].begin() );\n }\n for(int i = maxn - 1; ~i; i--){\n\tps[i] = n * n - iman[1];\n\tif(ass[i].size() <= 1) continue;\n\tamir(0, ass[i][0] + 1, ass[i][ ass[i].size() - 2]);\n\tamir(ass[i][0] + 1, ass[i][1] + 1, ass[i].back());\n\tamir(ass[i][1] + 1, n, n);\n }\n int ans = 0;\n for(int i = 1; i < maxn - 1; i++)\n\tans += (ps[i] - ps[i - 1]) * i;\n cout << ans << '\\n';\n return 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "70429fa794bbee9af44c444119c2acc9", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#define F first\n#define S second\nusing namespace std;\nint n;\n\nvector fc[202020];\nvector tkj[202020];\n\nmap vl;\nmap::iterator it, it2;\n\nlong long gv(long long a, long long b){\n if (a>b) return 0;\n long long m=0;\n it=vl.upper_bound(a); --it;\n long long la=(*it).F;\n long long lb=(*it).S;\n if (lb>=b) return 0;\n while (1){\n long long ia=(*it).F;\n long long ib=(*it).S;\n m+=(b-lb)*(ia-la);\n la=max(a, ia); lb=ib;\n if (ib>b || ia>b) break;\n it2=it; ++it2;\n if (ia>=a) vl.erase(it);\n it=it2;\n }\n vl[a]=b;\n return m;\n}\n\nint main(){\n ios_base::sync_with_stdio(0); cin.tie(0);\n for (int i=1; i<202020; ++i) for (int j=i; j<202020; j+=i) fc[j].push_back(i);\n cin >> n;\n vl[n]=n;\n for (int i=0; i> ai;\n for (int j=0; j\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long LL;\n\nconst int MAXN = 200000 + 5;\n\nint n;\nint a[MAXN];\nint exist[MAXN];\nint _max;\nint lazy[4*MAXN];\nint board[4*MAXN];\nlong long cnt[4*MAXN];\nlong long tot_inv, cnt_inv[MAXN];\n\nvoid pushup(int index) {\n board[index] = min(board[index*2], board[index*2+1]);\n cnt[index] = cnt[index*2] + cnt[index*2+1];\n}\n\nvoid pushdown(int index) {\n if (lazy[index] == 1) {\n long long s = (cnt[index]/board[index]) / 2 + (cnt[index]/board[index]) % 2;\n cnt[index*2] = s * board[index];\n cnt[index*2+1] = cnt[index] - s * board[index];\n board[index*2] = board[index*2+1] = board[index];\n lazy[index*2] = lazy[index*2+1] = 1;\n lazy[index] = 0;\n }\n}\n\nvoid build(int index, int l, int r) {\n if (l == r) {\n board[index] = l;\n cnt[index] = l;\n return;\n }\n\n int mid = (l+r) / 2;\n build(index*2, l, mid);\n build(index*2+1, mid+1, r);\n pushup(index);\n}\n\nvoid update(int index, int l, int r, int left, int right, int BOARD) {\n if (left <= l && right >= r) {\n if (BOARD > board[index]) {\n board[index] = BOARD;\n cnt[index] = LL(r-l+1) * BOARD;\n lazy[index] = 1;\n }\n return;\n }\n pushdown(index);\n int mid = (l+r) / 2;\n if (left <= mid)\n update(index*2, l, mid, left, right, BOARD);\n if (mid+1 <= right)\n update(index*2+1, mid+1, r, left, right, BOARD);\n pushup(index);\n}\n\nint find(int index, int l, int r, int BOARD) {\n if (l == r)\n return l;\n\n int mid = (l+r) / 2;\n pushdown(index);\n if (board[index*2+1] < BOARD)\n return find(index*2+1, mid+1, r, BOARD);\n else\n return find(index*2, l, mid, BOARD);\n}\n\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n memset(exist, 0, sizeof(exist));\n for (int i = 1; i <= n; i++) {\n exist[a[i]] = i;\n _max = max(_max, a[i]);\n }\n\n build(1, 1, n);\n tot_inv = LL(n) * (n+1);\n cnt_inv[_max+1] = tot_inv - cnt[1]; cnt_inv[0] = 3;\n for (int i = _max; i >= 1; i--) {\n vector v;\n\n v.clear();\n for (int j = i; j <= _max; j += i) {\n if (exist[j] > 0)\n v.push_back(exist[j]);\n }\n\n cnt_inv[i] = tot_inv - cnt[1];\n int k = v.size();\n if (k < 2)\n continue;\n\n sort(v.begin(), v.end());\n if (v[1]+1 <= n) {\n update(1, 1, n, v[1]+1, n, n+1);\n }\n if (board[1] < v[k-1]) {\n int pos = find(1, 1, n, v[k-1]);\n pos = min(pos, v[k-1]-1);\n update(1, 1, n, v[0]+1, pos, v[k-1]);\n }\n if (board[1] < v[k-2]) {\n int pos = find(1, 1, n, v[k-2]);\n pos = min(pos, v[k-2]-1);\n update(1, 1, n, 1, pos, v[k-2]);\n }\n }\n\n long long ans = 0;\n for (int i = _max; i >= 1; i--)\n ans += LL(cnt_inv[i]-cnt_inv[i-1]) * i;\n\n printf(\"%I64d\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "8d7b326a8c12152713cb9d8560d8b6c2", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nconst int N = 2e5+7;\n#define div Div\nvector div[N];\nint L[N], a[N], ps[N], toset[4*N], n;\ntypedef long long ll;\nll seg[4*N], mx[4*N];\ninline void makeL()\n{\n\tfor (int i = 0; i < N; i++) L[i] = n;\n\tfor (int i = 0; i < n; i++)\n\t\tL[a[i]] = min(L[a[i]], i);\n\tL[1] = 0;\n\tfor (int i = 2; i < N; i++)\n\t\tfor (int j = i; j < N; j += i)\n\t\t\tL[i] = min(L[i], L[j]);\n}\ninline void divisor()\n{\n\tfor (int i = 1; i < N; i++)\n\t{\n\t\tfor (int j = i; j < N; j += i)\n\t\t\tdiv[j].push_back(i);\n\t\tdiv[i].shrink_to_fit();\n\t}\n}\ninline void fix (int xl, int xr, int k, int val)\n{\n\ttoset[k] = mx[k] = val;\n\tseg[k] = 1ll * (xr - xl) * val;\n}\ninline void shift(int xl, int xr, int k)\n{\n\tif (toset[k] == -1) return;\n\tint val = toset[k];\n\ttoset[k] = -1;\n\tint xm = (xl+xr) / 2;\n\tfix(xl, xm, 2*k+1, val);\n\tfix(xm, xr, 2*k+2, val);\n}\nvoid Set(int ql, int qr, int val, int xl = 0, int xr = N, int k = 0)\n{\n\tif (ql >= xr || qr <= xl) return;\n\tif (ql <= xl && qr >= xr)\n\t{\n\t\tfix(xl, xr, k, val);\n\t\treturn;\n\t}\n\tshift(xl, xr, k);\n\tint xm = (xl+xr) / 2;\n\tSet(ql, qr, val, xl, xm, 2*k+1);\n\tSet(ql, qr, val, xm, xr, 2*k+2);\n\tseg[k] = seg[2*k+1] + seg[2*k+2];\n\tmx[k] = max(mx[2*k+1], mx[2*k+2]);\n}\nll get (int ql, int qr, int xl = 0, int xr = N, int k = 0)\n{\n\tif (ql >= xr || qr <= xl) return 0;\n\tif (ql <= xl && qr >= xr) return seg[k];\n\tint xm = (xl+xr)/2;\n\tshift(xl, xr, k);\n\treturn get(ql, qr, xl, xm, 2*k+1) + get(ql, qr, xm, xr, 2*k+2);\n}\nint f(int ql, int qr, int val, int xl = 0, int xr = N, int k = 0)\n{\n\t// first place betwen [ql, qr) > val;\n\tif (ql >= xr || qr <= xl || mx[k] <= val) return qr;\n\tif (xr - xl == 1)\n\t{\n\t\tif (mx[k] > val) return xl;\n\t\treturn qr;\n\t}\n\tint xm = (xl + xr) / 2;\n\tshift(xl, xr, k);\n\tint p = f(ql, qr, val, xl, xm, 2*k+1);\n\tif (p != qr) return p;\n\treturn f(ql, qr, val, xm, xr, 2*k+2);\n}\ninline void fix (int ql, int qr, int val)\n{\n\tint pos = f(ql, qr, val);\n\tSet(ql, pos, val);\n}\nint tmp[N];\ninline void initPS()\n{\n\tmemset(ps, 0, sizeof ps);\n\tmemset(tmp, 0, sizeof tmp);\n\tfor (auto x : div[a[0]])\n\t\ttmp[x]++;\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tint res = 0;\n\t\tfor (auto x : div[a[i]])\n\t\t\tif (tmp[x]++)\n\t\t\t\tres = x;\n\t\tps[i] = max(ps[i-1], res);\n\t}\n}\ninline void init()\n{\n\tinitPS();\n\tfor (int i = 0; i < n-1; i++)\n\t\tSet(i, i+1, ps[i]);\n\treverse(a, a+n);\n\tinitPS();\n\treverse(ps, ps+n);\n\treverse(a, a+n);\n}\nvoid print()\n{\n\tcout << \"\\nprint: \\n\";\n\tfor (int i = 0; i < n; i++)\n\t\tcout << get(i, i+1) << \" \";\n\tcout << endl;\n}\nint main()\n{\n\tscanf(\"%d\", &n);\n\tfor (int i = 0; i < n; i++) scanf(\"%d\", &a[i]);\n\tmakeL();\n\tdivisor();\n\tinit();\n\tll ans = get(0, n-1);\n\tans += ps[1];\n\tfor (int i = n-1; i > 1; i--)\n\t{\n\t\tint val = a[i];\n\t\tint ql = 0, qr = i-1;\n\t\tfix(ql, qr, ps[i]);\n\t\tfor (auto x : div[val])\n\t\t\tif (L[x] < qr)\n\t\t\t\tfix(L[x], qr, x);\n\t\tans += get(ql, qr);\n\t\tans += ps[i];\n\t}\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "99a77e18f147ec421e3d5562a84d77f8", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\nusing namespace std;\nconst int N=200005,M=200000;\ntypedef long long ll;\nint a[N],n,m,l1[N],r1[N],l2[N],r2[N];\nll f[N],ans;\nstruct node{\n\tint mx,mi;\n\tll sum,tag;\n}tr[N*4];\ninline void updata(int x){\n\ttr[x].mx=max(tr[x<<1].mx,tr[x<<1|1].mx);\n\ttr[x].mi=min(tr[x<<1].mi,tr[x<<1|1].mi);\n\ttr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;\n}\nvoid build(int x,int l,int r){\n\tif (l==r){\n\t\ttr[x].mx=tr[x].mi=tr[x].sum=l;\n\t\treturn;\n\t}\n\tint mid=(l+r)>>1;\n\tbuild(x<<1,l,mid);\n\tbuild(x<<1|1,mid+1,r);\n\tupdata(x);\n}\ninline void work(int x,int l,int r,ll m){\n\ttr[x].mx=tr[x].mi=m;\n\ttr[x].sum=m*(r-l+1);\n\ttr[x].tag=m;\n}\nvoid pushdown(int x,int l,int r){\n\tif (tr[x].tag){\n\t\tif (l==r) return;\n\t\twork(x<<1,l,(l+r)>>1,tr[x].tag);\n\t\twork(x<<1|1,((l+r)>>1)+1,r,tr[x].tag);\n\t\ttr[x].tag=0; \n\t}\n}\nvoid modify(int x,int l,int r,int s,int t,ll m){\n\tpushdown(x,l,r);\n\tif (tr[x].mi>=m) return;\n\tif (l==s && r==t && m>tr[x].mx){\n\t\twork(x,l,r,m);\n\t\treturn; \n\t}\n\tint mid=(l+r)>>1;\n\tif (t<=mid) modify(x<<1,l,mid,s,t,m);\n\telse if (s>mid) modify(x<<1|1,mid+1,r,s,t,m);\n\telse {\n\t\tmodify(x<<1,l,mid,s,mid,m);\n\t\tmodify(x<<1|1,mid+1,r,mid+1,t,m);\n\t}\n\tupdata(x);\n}\nint main(){\n\tscanf(\"%d\",&n);\n\tfor (int i=1;i<=n;i++) scanf(\"%d\",&a[i]);\n\tfor (int i=1;i<=n;i++){\n\t\tint m=sqrt(a[i]);\n\t\tfor (int j=1;j<=m;j++){\n\t\t\t if (a[i]%j==0) {\n\t\t \t\tif (!l1[j]) l1[j]=i;\n\t\t \t\telse if (!l2[j]) l2[j]=i;\n\t\t \t\tr2[j]=r1[j],r1[j]=i;\n\t\t \t\tif (a[i]!=j*j){\n\t\t \t\tif (!l1[a[i]/j]) l1[a[i]/j]=i;\n\t\t \t\telse if (!l2[a[i]/j]) l2[a[i]/j]=i;\n\t\t \t\tr2[a[i]/j]=r1[a[i]/j],r1[a[i]/j]=i;\n\t\t\t }\n\t\t \t}\n\t\t}\n\t \t\n\t}\n\tbuild(1,1,n); \n\tf[M+1]=(ll)n*(n+1)/2;\n\tfor (int i=M;i>=0;i--){\n\t\tf[i]=f[i+1];\n\t\tif (l1[i]==r1[i]) continue;\n\t\tmodify(1,1,n,1,l1[i],r2[i]);\n\t\tmodify(1,1,n,l1[i]+1,l2[i],r1[i]);\n\t\tif (l2[i]+1<=n)\n\t\tmodify(1,1,n,l2[i]+1,n,n+1);\n\t\tf[i]=(ll)n*(n+1)-tr[1].sum; \n\t}\n\tfor (int i=1;i<=M;i++)\n\t ans+=(f[i+1]-f[i])*i;\n\t printf(\"%I64d\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "1adddbd0b7a5e7cde27a0f0fd8d7adb7", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); ++i)\n#define ROF(i, a, b) for (long long i = (long long)(b) - 1; i >= (long long)(a); --i)\n#define sz(x) (int((x).size()))\n#define all(x) (x).begin(), (x).end()\ntypedef long long ll;\ntypedef pair PII;\ntypedef vector VI;\ntypedef vector> VPII;\ntemplate inline std::istream& IN(A& a) { return std::cin >> a; }\ntemplate inline std::istream& IN(A& a, Args&... rest) { std::cin >> a; return IN(rest...); }\ninline std::ostream& OUT() { return std::cout << std::endl; }\ntemplate inline std::ostream& _OUT(const A& a) { return std::cout << a; }\ntemplate inline std::ostream& _OUT(const A& a, const Args&... rest) { std::cout << a << \" \"; return _OUT(rest...); }\ntemplate inline std::ostream& OUT_(const Args&... args) { _OUT(args...); return std::cout << \" \"; }\ntemplate inline std::ostream& OUT(const Args&... args) { _OUT(args...); return std::cout << std::endl; }\n#define INn(a, b, c) { FOR(_i_, b, c) IN(a[_i_]); }\n#define INnm(a, b, c, d, e) { FOR(__i__, b, c) INn(a[__i__], d, e) }\n#define OUTn(a, l, r) { FOR(_i_, l, r - 1) OUT_((a)[_i_]); if(r > 0) OUT((a)[r - 1]); }\n#define OUTnm(a, b, c, d, e) { FOR(__i__, b, c) OUTn(a[__i__], d, e) }\ntemplate inline bool cmax(T& a, const U& b) { return b > a ? a = b, true : false; }\ntemplate inline bool cmin(T& a, const U& b) { return b < a ? a = b, true : false; }\n//\n\nconst int _ = int(2e5 + 10);\nlong long n, N;\nint a[_];\nVI v[_];\nint rb[_];\nlong long ans[_];\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t\t\n\tIN(n);\n\tFOR(i, 1, n + 1) {\n\t\tIN(a[i]);\n\t\tcmax(N, a[i]);\n\t\tfor (int d = 1; d * d <= a[i]; ++d) if (a[i] % d == 0) {\n\t\t\tv[d].push_back(i);\n\t\t\tint t = a[i] / d;\n\t\t\tif (d != t) v[t].push_back(i);\n\t\t}\n\t\trb[i] = i;\n\t}\n\tans[N] = n * (n + 1) / 2;\n\t\n\tROF(i, 1, N) {\n\t\tans[i] = ans[i + 1];\n\t\tif (sz(v[i + 1]) <= 1) continue;\n\t\tint newrb = n + 1;\n\t\tFOR(j, v[i + 1][1] + 1, n + 1) {\n\t\t\tif (rb[j] >= newrb) break;\n\t\t\tans[i] -= newrb - rb[j];\n\t\t\trb[j] = newrb;\n\t\t}\n\t\tnewrb = v[i + 1].back();\n\t\tFOR(j, v[i + 1][0] + 1, v[i + 1][1] + 1) {\n\t\t\tif (rb[j] >= newrb) break;\n\t\t\tans[i] -= newrb - rb[j];\n\t\t\trb[j] = newrb;\n\t\t}\n\t\tnewrb = v[i + 1][sz(v[i + 1]) - 2];\n\t\tFOR(j, 1, v[i + 1][0] + 1) {\n\t\t\tif (rb[j] >= newrb) break;\n\t\t\tans[i] -= newrb - rb[j];\n\t\t\trb[j] = newrb;\n\t\t}\n\t}\n\n\tif (rb[1] <= n - 1) ++ans[0];\n\tif (rb[1] <= n) ++ans[0];\n\tif (rb[2] <= n) ++ans[0];\n\t\n/* \tOUT(n);\n\tOUTn(a, 1, n + 1);\n\tOUTn(rb, 1, n + 1);\n\tOUTn(ans, 0, N + 1); */\n\t\n\tlong long ret = 0;\n\tFOR(i, 1, N + 1) {\n\t\tret += (ans[i] - ans[i - 1]) * i;\n\t}\n\tOUT(ret);\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "09c048b16ded09df155031f865646e25", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define maxn 200005\n#define ls o<<1\n#define rs o<<1|1 \nstruct sgt\n{\n\tint l,r,mn,mm,tag;\n\tlong long sum;\n}c[maxn<<2];\nint n;\nlong long ans;\nint a[maxn];\nlong long h[maxn];\nvectorx[maxn];\nvoid pd(int o)\n{\n\tif(c[o].tag)\n\t{\n\t\tc[ls].mm=c[ls].mn=c[o].tag;\n\t\tc[rs].mm=c[rs].mn=c[o].tag;\n\t\tc[ls].tag=c[rs].tag=c[o].tag;\n\t\tc[ls].sum=((long long)c[ls].r-c[ls].l+1)*c[o].tag;\n\t\tc[rs].sum=((long long)c[rs].r-c[rs].l+1)*c[o].tag;\n\t\tc[o].tag=0;\n\t}\n}\nvoid build(int l,int r,int o)\n{\n\tc[o].l=l,c[o].r=r;\n\tif(l==r) \n\t{\n\t\tc[o].mn=c[o].sum=c[o].mm=l;\n\t\treturn ;\n\t}\n\tint mid=(l+r)>>1;\n\tbuild(l,mid,ls);\n\tbuild(mid+1,r,rs);\n\tc[o].mm=max(c[ls].mm,c[rs].mm);\n\tc[o].mn=min(c[ls].mn,c[rs].mn);\n\tc[o].sum=c[ls].sum+c[rs].sum;\n}\nvoid update(int l,int r,int k,int o)\n{\n\tif(l>r) return ;\n\tif(l<=c[o].l&&r>=c[o].r&&k<=c[o].mn) return ;\n \tif(l<=c[o].l&&r>=c[o].r&&k>=c[o].mm)\n\t{\n\t\tc[o].tag=k;\n\t\tc[o].mm=c[o].mn=c[o].tag;\n\t\tc[o].sum=((long long)c[o].r-c[o].l+1)*k;\n\t\treturn ;\n\t}\n\tpd(o);\n\tint mid=(c[o].l+c[o].r)>>1;\n\tif(l>mid) update(l,r,k,rs);\n\telse if(r<=mid) update(l,r,k,ls);\n\telse \n\t{\n\t\tupdate(l,mid,k,ls);\n\t\tupdate(mid+1,r,k,rs);\n\t}\n\tc[o].mm=max(c[ls].mm,c[rs].mm);\n\tc[o].mn=min(c[ls].mn,c[rs].mn);\n\tc[o].sum=c[ls].sum+c[rs].sum;\n}\nint main()\n{\n\tscanf(\"%d\",&n);\n\th[0]=3;\n\tfor(int i=1;i<=n;++i) scanf(\"%d\",&a[i]);\n\tfor(int i=1;i<=n;++i)\n\t{\n\t\tint t=sqrt(a[i]);\n\t\tfor(int j=1;j<=t;++j)\n\t\t{\n\t\t\tif(a[i]%j==0) \n\t\t\t{\n\t\t\t\tx[j].push_back(i);\n\t\t\t\tif(j*j!=a[i]) x[a[i]/j].push_back(i);\n\t\t\t}\n\t\t}\n\t}\n\t/*for(int i=1;i<=5;++i)\n\t{\n\t\tfor(int j=0;j=1;--i)\n\t{\n\t\th[i]=(long long)n*n-c[1].sum+n;\n\t\tif(x[i].size()<=1) continue;\n\t\telse \n\t\t{\n\t\t\tupdate(1,x[i][0],x[i][x[i].size()-2],1);\n\t\t\tupdate(x[i][0]+1,x[i][1],x[i][x[i].size()-1],1);\n\t\t\tupdate(x[i][1]+1,n,n+1,1);\n\t\t}\n\t}\n\t//for(int i=0;i<=5;++i) cout<=1;--i)\t\n\t\tans=ans+(long long)i*(h[i]-h[i-1]);\n\tprintf(\"%lld\",ans);\n\treturn 0;\n}\n//hellobig_tq", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "0969fca337c34df14d6f90fd418478b7", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\nconst int N = (int)2e5;\ntypedef int arr[N + 10];\ntypedef long long ll;\n\nint n, m, cnts;\narr a, ra;\n\nstruct segm {\n\tint l, r, d;\n}s[3 * N + 10];\n\nstruct comp {\n\tbool operator () (const segm &a, const segm &b) { return a.d > b.d; }\n};\n\nll cans;\nset ava;\n\nvoid get_seg() {\n\ts[++cnts] = (segm){1, n, 1};\n\tfor (int i = 2; i <= m; ++i) {\n\t\tint l1 = m + 1, l2 = m + 1, r1 = -1, r2 = -1;\n\t\tfor (int j = i; j <= m; j += i) {\n\t\t\tint k = ra[j]; if (!k) continue;\n\t\t\tif (k < l1) l2 = l1, l1 = k; else if (k < l2) l2 = k;\n\t\t\tif (k > r2) r1 = r2, r2 = k; else if (k > r1) r1 = k;\n\t\t}\n\t\tif (l2 == m + 1 || r1 == -1) continue;\n\t\tif (l1 + 1 < r2) s[++cnts] = (segm){l1 + 1, r2 - 1, i};\n\t\tif (r1 > 1) s[++cnts] = (segm){1, r1 - 1, i};\n\t\tif (l2 < n) s[++cnts] = (segm){l2 + 1, n, i};\n\t}\n}\n\nbool cmp(const segm &a, const segm &b) { return (a.l < b.l) || (a.l == b.l && a.r < b.r); }\n\nvoid insert(int st, segm ins) {\n\tif (ava.empty()) { cans += (ll)(ins.r - st + 1) * (ll)ins.d, ava.insert(ins); return; }\n\tset :: iterator it = ava.lower_bound(ins), tmp;\n\tif (it == ava.end()) {\n\t\t--it;\n\t\tif (ins.r > (*it).r) cans += (ll)(ins.r - (*it).r) * (ll)ins.d, ava.insert(ins);\n\t\treturn;\n\t}\n\tint lst = st - 1;\n\tif (it != ava.begin()) {\n\t\t--it;\n\t\tif (ins.r <= (*it).r) return;\n\t\tcans += (ll)ins.d * (ll)(ins.r - (*it).r);\n\t\tlst = (*it).r, ++it;\n\t}\n\telse cans += (ll)ins.d * (ll)(ins.r - st + 1);\n\tfor ( ; it != ava.end() && (*it).r <= ins.r; ) {\n\t\tcans -= (ll)(*it).d * (ll)((*it).r - lst);\n\t\ttmp = it, lst = (*tmp).r, ++it, ava.erase(tmp);\n\t}\n\tif (it != ava.end()) {\n\t\tcans -= (ll)(*it).d * (ll)((*it).r - lst);\n\t\tcans += (ll)(*it).d * (ll)((*it).r - ins.r);\n\t}\n\tava.insert(ins);\n}\n\nll get_ans() {\n\tll res = 0;\n\tsort(s + 1, s + cnts + 1, cmp);\n\tint cnt = 1;\n\tfor (int i = 1; i <= n; ++i) {\n\t\tfor ( ; cnt <= cnts && s[cnt].l == i; insert(i, s[cnt]), ++cnt);\n\t\tres += cans, cans -= (ll)(*ava.begin()).d;\n\t\tif ((*ava.begin()).r == i) ava.erase(ava.begin());\n\t}\n\treturn res;\n}\n\nint main() {\n\n\tscanf(\"%d\", &n);\n\tfor (int i = 1; i <= n; ++i) scanf(\"%d\", a + i), ra[a[i]] = i, m = max(m, a[i]);\n\tif (n < 3) {\n\t\tif (n == 1) printf(\"0\\n\");\n\t\telse printf(\"%d\\n\", __gcd(a[1], a[2]));\n\t\treturn 0;\n\t}\n\n\tget_seg();\n\tcout << get_ans() - 3 << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "c777563069adb1c08bec0ab04e9ef08a", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "//satyaki3794\n#include \n#include \n#include \n#include \n#include \n#include \n#define ff first\n#define ss second\n#define pb push_back\n#define MOD (1000000007LL)\n#define LEFT(n) (2*(n))\n#define RIGHT(n) (2*(n)+1)\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair ii;\ntypedef pair iii;\n\nll pwr(ll base, ll p, ll mod = MOD){\nll ans = 1;while(p){if(p&1)ans=(ans*base)%mod;base=(base*base)%mod;p/=2;}return ans;\n}\n\n\nconst int N = 200000;\nint n, arr[N+5], pos[N+5];\nvector multiples[N+5];\nll cnt[N+5], minval[4*N+5], maxval[4*N+5], sumval[4*N+5], lazy[4*N+5];\n\n\nvoid build(int node, int s, int e){\n if(s > e) return;\n if(s == e){\n minval[node] = maxval[node] = sumval[node] = s;\n return;\n }\n int mid = (s+e)/2;\n build(LEFT(node), s, mid);\n build(RIGHT(node), mid+1, e);\n minval[node] = min(minval[LEFT(node)], minval[RIGHT(node)]);\n maxval[node] = max(maxval[LEFT(node)], maxval[RIGHT(node)]);\n sumval[node] = sumval[LEFT(node)] + sumval[RIGHT(node)];\n}\n\n\n\nint kth_query(int node, int s, int e, int threshold){\n if(minval[node] >= threshold) return 0;\n if(s == e) return s;\n int mid = (s+e)/2;\n if(minval[RIGHT(node)] < threshold) return kth_query(RIGHT(node), mid+1, e, threshold);\n return kth_query(LEFT(node), s, mid, threshold);\n}\n\n\nvoid update(int node, int s, int e, int lo, int hi, ll val){\n if(s > e || lo > hi || val <= 0) return;\n if(lazy[node] != 0){\n minval[node] = maxval[node] = lazy[node];\n sumval[node] = (e-s+1)*lazy[node];\n if(s != e){\n lazy[LEFT(node)] = lazy[RIGHT(node)] = lazy[node];\n }\n lazy[node] = 0;\n }\n if(lo > e || s > hi) return;\n if(s >= lo && e <= hi){\n minval[node] = maxval[node] = val;\n sumval[node] = (e-s+1)*val;\n if(s != e){\n lazy[LEFT(node)] = lazy[RIGHT(node)] = val;\n }\n return;\n }\n int mid = (s+e)/2;\n update(LEFT(node), s, mid, lo, hi, val);\n update(RIGHT(node), mid+1, e, lo, hi, val);\n minval[node] = min(minval[LEFT(node)], minval[RIGHT(node)]);\n maxval[node] = max(maxval[LEFT(node)], maxval[RIGHT(node)]);\n sumval[node] = sumval[LEFT(node)] + sumval[RIGHT(node)];\n}\n\n\nll sum_query(int node, int s, int e, int lo, int hi){\n if(s > e) return 0;\n if(lazy[node] != 0){\n minval[node] = maxval[node] = lazy[node];\n sumval[node] = (e-s+1)*lazy[node];\n if(s != e){\n lazy[LEFT(node)] = lazy[RIGHT(node)] = lazy[node];\n }\n lazy[node] = 0;\n }\n if(lo > e || s > hi) return 0;\n if(s >= lo && e <= hi) return sumval[node];\n int mid = (s+e)/2;\n return sum_query(LEFT(node), s, mid, lo, hi) + sum_query(RIGHT(node), mid+1, e, lo, hi);\n}\n\n\nint main(){\n\n // ios_base::sync_with_stdio(0);\n // freopen(\"input.txt\", \"r\", stdin);\n\n scanf(\"%d\", &n);\n for(int i=1;i<=n;i++){\n scanf(\"%d\", &arr[i]);\n pos[arr[i]] = i;\n }\n\n if(n <= 2){\n printf(\"0\\n\");\n return 0;\n }\n\n for(int i=1;i<=N;i++)\n for(int j=i;j<=N;j+=i)\n if(pos[j] > 0)\n multiples[i].pb(pos[j]);\n for(int i=1;i<=N;i++)\n sort(multiples[i].begin(), multiples[i].end());\n\n\n build(1, 1, n);\n cnt[N+1] = 1LL*n*(n+1) - sum_query(1, 1, n, 1, n);\n for(int i=N;i>=1;i--){\n\n if((int)multiples[i].size() < 2){\n cnt[i] = cnt[i+1];\n continue;\n }\n\n// cout<<\"now at i = \"<=1;i--)\n ans += 1LL*i*(cnt[i+1]-cnt[i]);\n printf(\"%lld\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "f3c2cdf089c61e7c3b2a1e7b310299d2", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\ntypedef pair PI;\nint mn[200010*4],ch[200010*4];\nll sum[200010*4];\ninline void down(int l,int r,int idx){\n\tif(ch[idx]!=-1){\n\t\tint mid=(l+r)>>1;\n\t\tmn[idx<<1|1]=mn[idx<<1]=ch[idx<<1]=ch[idx<<1|1]=ch[idx];\n\t\tsum[idx<<1]=(ll)(mid-l+1)*ch[idx];\n\t\tsum[idx<<1|1]=(ll)(r-mid)*ch[idx];\n\t\tch[idx]=-1;\n\t}\n}\nvoid modify(int l,int r,int ql,int qr,int v,int idx){\n\tif(ql>qr) return;\n\tif(l==ql && r==qr){\n\t\tch[idx]=mn[idx]=v;\n\t\tsum[idx]=(ll)v*(r-l+1);\n\t}else{\n\t\tint mid=(l+r)>>1;\n\t\tdown(l,r,idx);\n\t\tif(qr<=mid) modify(l,mid,ql,qr,v,idx<<1);\n\t\telse if(ql>mid) modify(mid+1,r,ql,qr,v,idx<<1|1);\n\t\telse{\n\t\t\tmodify(l,mid,ql,mid,v,idx<<1);\n\t\t\tmodify(mid+1,r,mid+1,qr,v,idx<<1|1);\n\t\t}\n\t\tsum[idx]=sum[idx<<1]+sum[idx<<1|1];\n\t\tmn[idx]=min(mn[idx<<1],mn[idx<<1|1]);\n\t}\n}\nint find_left(int l,int r,int v,int idx){\n\tif(l==r){\n\t\tif(mn[idx]>1;\n\tif(mn[idx<<1]>1;\n\tdown(l,r,idx);\n\tif(qr<=mid) return query(l,mid,ql,qr,idx<<1);\n\telse if(ql>mid) return query(mid+1,r,ql,qr,idx<<1|1);\n\treturn query(l,mid,ql,mid,idx<<1)+query(mid+1,r,mid+1,qr,idx<<1|1);\n}\ninline void getFactor(vector& fac,int x){\n\tfor(int i=1;(ll)i*i<=x;i++){\n\t\tif(x%i==0){\n\t\t\tfac.push_back(i);\n\t\t\tif(x/i!=i)\n\t\t\t\tfac.push_back(x/i);\n\t\t}\n\t}\n}\nint a[200010];\nvector fac[200010];\ndeque idx[200010];\nint main(){\n\tint n;\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++){\n\t\tscanf(\"%d\",a+i);\n\t\tif(fac[a[i]].size()==0) getFactor(fac[a[i]],a[i]);\n\t\tfor(int& x:fac[a[i]])\n\t\t\tidx[x].push_back(i);\n\t}\n\tmemset(ch,-1,sizeof ch);\n\tfor(int x=1;x<=200000;x++){\n\t\tif(idx[x].size()>=2){\n\t\t\tint Left=find_left(1,n,x,1);\n\t\t\tmodify(1,n,Left,idx[x][idx[x].size()-2]-1,x,1);\n\t\t}\n\t}\n\tll ans=0;\n\tfor(int i=1;i<=n;i++){\n\t\tans+=query(1,n,i,n,1);\n\t\tfor(int& x:fac[a[i]]){\n\t\t\tidx[x].pop_front();\n\t\t\tidx[x].push_back(i+n);\n\t\t\tif(idx[x].size()>=2){\n\t\t\t\tint Left=find_left(1,n,x,1);\n\t\t\t\tif(Left!=-1){\n\t\t\t\t\tmodify(1,n,Left,min(idx[x][idx[x].size()-2]-1,n),x,1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "df72a3b5c65342538aa5e4aaa6c8e166", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nconst int N = 2e5+7;\n#define div Div\nvector div[N];\nint L[N], a[N], ps[N], toset[4*N], n;\ntypedef long long ll;\nll seg[4*N], mx[4*N];\ninline void makeL()\n{\n\tfor (int i = 0; i < N; i++) L[i] = n;\n\tfor (int i = 0; i < n; i++)\n\t\tL[a[i]] = min(L[a[i]], i);\n\tL[1] = 0;\n\tfor (int i = 2; i < N; i++)\n\t\tfor (int j = i; j < N; j += i)\n\t\t\tL[i] = min(L[i], L[j]);\n}\ninline void divisor()\n{\n\tfor (int i = 1; i < N; i++)\n\t{\n\t\tfor (int j = i; j < N; j += i)\n\t\t\tdiv[j].push_back(i);\n\t\tdiv[i].shrink_to_fit();\n\t}\n}\ninline void fix (int xl, int xr, int k, int val)\n{\n\ttoset[k] = mx[k] = val;\n\tseg[k] = 1ll * (xr - xl) * val;\n}\ninline void shift(int xl, int xr, int k)\n{\n\tif (toset[k] == -1) return;\n\tint val = toset[k];\n\ttoset[k] = -1;\n\tint xm = (xl+xr) / 2;\n\tfix(xl, xm, 2*k+1, val);\n\tfix(xm, xr, 2*k+2, val);\n}\nvoid Set(int ql, int qr, int val, int xl = 0, int xr = N, int k = 0)\n{\n\tif (ql >= xr || qr <= xl) return;\n\tif (ql <= xl && qr >= xr)\n\t{\n\t\tfix(xl, xr, k, val);\n\t\treturn;\n\t}\n\tshift(xl, xr, k);\n\tint xm = (xl+xr) / 2;\n\tSet(ql, qr, val, xl, xm, 2*k+1);\n\tSet(ql, qr, val, xm, xr, 2*k+2);\n\tseg[k] = seg[2*k+1] + seg[2*k+2];\n\tmx[k] = max(mx[2*k+1], mx[2*k+2]);\n}\nll get (int ql, int qr, int xl = 0, int xr = N, int k = 0)\n{\n\tif (ql >= xr || qr <= xl) return 0;\n\tif (ql <= xl && qr >= xr) return seg[k];\n\tint xm = (xl+xr)/2;\n\tshift(xl, xr, k);\n\treturn get(ql, qr, xl, xm, 2*k+1) + get(ql, qr, xm, xr, 2*k+2);\n}\nint f(int ql, int qr, int val, int xl = 0, int xr = N, int k = 0)\n{\n\t// first place betwen [ql, qr) > val;\n\t//cout << ql << \" \" << qr << \" \" << val << \" \" << xl << \" \" << xr << \" \" << mx[k] << endl;\n\tif (ql >= xr || qr <= xl || mx[k] <= val) return qr;\n\tif (xr - xl == 1)\n\t{\n\t\tif (mx[k] > val) return xl;\n\t\treturn qr;\n\t}\n\tint xm = (xl + xr) / 2;\n\tshift(xl, xr, k);\n\tint p = f(ql, qr, val, xl, xm, 2*k+1);\n\tif (p != qr) return p;\n\treturn f(ql, qr, val, xm, xr, 2*k+2);\n}\n\nint f2(int ql, int qr, int val)\n{\n\tint l = ql - 1, r = qr;\n\twhile (r-l > 1)\n\t{\n\t\tint m = (l+r) / 2, x = get(m, m+1);\n\t\tif (x > val) r = m;\n\t\telse l = m;\n\t}\n\treturn r;\n}\ninline void fix (int ql, int qr, int val)\n{\n\tint pos = f(ql, qr, val);\n\tSet(ql, pos, val);\n}\nint tmp[N];\ninline void initPS()\n{\n\tmemset(ps, 0, sizeof ps);\n\tmemset(tmp, 0, sizeof tmp);\n\tfor (auto x : div[a[0]])\n\t\ttmp[x]++;\n\tfor (int i = 1; i < n; i++)\n\t{\n\t\tint res = 0;\n\t\tfor (auto x : div[a[i]])\n\t\t\tif (tmp[x]++)\n\t\t\t\tres = x;\n\t\tps[i] = max(ps[i-1], res);\n\t}\n}\ninline void init()\n{\n\tinitPS();\n\tfor (int i = 0; i < n-1; i++)\n\t\tSet(i, i+1, ps[i]);\n\treverse(a, a+n);\n\tinitPS();\n\treverse(ps, ps+n);\n\treverse(a, a+n);\n}\nvoid print()\n{\n\treturn;\n\tcout << \"\\nprint: \\n\";\n\tfor (int i = 0; i < n; i++)\n\t\tcout << get(i, i+1) << \" \";\n\tcout << endl;\n}\nint main()\n{\n\t//ios::sync_with_stdio(0);\n\t//cin >> n;\n\tscanf(\"%d\", &n);\n\tfor (int i = 0; i < n; i++) scanf(\"%d\", &a[i]);\n\tmakeL();\n\tdivisor();\n\tinit();\n\tll ans = get(0, n-1);\n\tans += ps[1];\n\tfor (int i = n-1; i > 1; i--)\n\t{\n\t\tint val = a[i];\n\t\tint ql = 0, qr = i-1;\n\t\tfix(ql, qr, ps[i]);\n\t\tfor (auto x : div[val])\n\t\t\tif (L[x] < qr)\n\t\t\t\tfix(L[x], qr, x);\n\t\tans += get(ql, qr);\n\t\tans += ps[i];\n\t}\n\tprint();\n\tprintf(\"%lld\\n\", ans);\n\t//cout << ans << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "937c3c59162f9dae9d3006f570e8ad76", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nconst int N = 200002;\nconst int sizeTree = (1 << 20);\n\nint tAdd[sizeTree], mn[sizeTree], mx[sizeTree];\nlong long tSum[sizeTree];\nint n;\nint a[N];\npair < int, int > b[N], c[N];\nlong long ans[N];\n\nvoid push(int v, int tl, int tr)\n{\n\tif (tAdd[v] != 0)\n\t{\n\t\tint tm = (tl + tr) / 2;\n\t\ttAdd[2 * v] = mn[2 * v] = mx[2 * v] = tAdd[v];\n\t\ttAdd[2 * v + 1] = mn[2 * v + 1] = mx[2 * v + 1] = tAdd[v];\n\t\ttSum[2 * v] = 1ll * (tm - tl + 1) * tAdd[v];\n\t\ttSum[2 * v + 1] = 1ll * (tr - tm) * tAdd[v];\n\t\ttAdd[v] = 0;\n\t}\n}\n\nvoid build(int v, int tl, int tr)\n{\n\tif (tl == tr)\n\t{\n\t\ttAdd[v] = 0;\n\t\ttSum[v] = tl;\n\t\tmn[v] = mx[v] = tl;\n\t\treturn;\n\t}\n\tint tm = (tl + tr) / 2;\n\tbuild(2 * v, tl, tm);\n\tbuild(2 * v + 1, tm + 1, tr);\n\ttSum[v] = tSum[2 * v] + tSum[2 * v + 1];\n\tmn[v] = min(mn[2 * v], mn[2 * v + 1]);\n\tmx[v] = max(mx[2 * v], mx[2 * v + 1]);\n}\n\nvoid assign(int v, int tl, int tr, int l, int r, int value)\n{\n\tif (l > r || mn[v] >= value)\n\t\treturn;\n\tif (tl == l && tr == r && mx[v] <= value)\n\t{\n\t\tmx[v] = mn[v] = tAdd[v] = value;\n\t\ttSum[v] = 1ll * (tr - tl + 1) * value;\n\t\treturn;\n\t}\n\tpush(v, tl, tr);\n\tint tm = (tl + tr) / 2;\n\tassign(2 * v, tl, tm, l, min(r, tm), value);\n\tassign(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, value);\n\ttSum[v] = tSum[2 * v] + tSum[2 * v + 1];\n\tmn[v] = min(mn[2 * v], mn[2 * v + 1]);\n\tmx[v] = max(mx[2 * v], mx[2 * v + 1]);\n}\n\nvoid update(int j, int pos)\n{\n\tif (b[j].first == -1)\n\t\tb[j].first = pos;\n\telse if (b[j].second == -1)\n\t\tb[j].second = pos;\n\tc[j].first = c[j].second;\n\tc[j].second = pos;\n}\n\n\nint main()\n{\n#ifdef _DEBUG\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tmemset(b, -1, sizeof(b));\n\tmemset(c, -1, sizeof(c));\n\tscanf(\"%d\", &n);\n\tfor (int i = 0; i < n; ++i)\n\t\tscanf(\"%d\", &a[i]);\n\tbuild(1, 0, n - 1);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 1; j * j <= a[i]; ++j)\n\t\t{\n\t\t\tif (a[i] % j == 0)\n\t\t\t{\n\t\t\t\tupdate(j, i);\n\t\t\t\tif (a[i] / j != j)\n\t\t\t\t\tupdate(a[i] / j, i);\n\t\t\t}\n\t\t}\n\t}\n\tans[0] = (n == 1 ? 1 : 3);\n\tfor (int i = N - 1; i >= 1; --i)\n\t{\n\t\tans[i] = 1ll * n * n - tSum[1];\n\t\tif (b[i].first != c[i].second)\n\t\t{\n\t\t\tassign(1, 0, n - 1, b[i].second + 1, n - 1, n);\n\t\t\tassign(1, 0, n - 1, b[i].first + 1, b[i].second, c[i].second);\n\t\t\tassign(1, 0, n - 1, 0, b[i].first, c[i].first);\n\t\t}\n\t\t\n\t}\n\tlong long res = 0;\n\tfor (int i = 1; i < N; ++i)\n\t{\n\t\tres += 1ll * (ans[i] - ans[i - 1]) * i;\n\t}\n\tcout << res << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "51b9c136a9207785e8efc17f14852085", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\nll h[200005];\nll mat[200005];\nll c[200005 << 2];\nll a[200005 << 2];\nll lz[200005 << 2];\nvector v[200005];\n\nll u;\nint yl,yr;\nvoid build(ll o,ll l,ll r)\n{\n int rt = o;\n if(l==r){\n a[rt]=c[rt]=1ll*l;\n return;\n }\n int m=(l+r)>>1;\n build(rt<<1,l,m);\n build(rt<<1|1,m+1,r);\n c[rt]=c[rt<<1]+c[rt<<1|1];\n a[rt]=min(a[rt<<1],a[rt<<1|1]);\n}\n\nvoid down(ll o,ll l,ll r)\n{\n ll rt = o;\n if(!lz[rt])return;\n int m=(l+r)>>1;\n c[rt<<1]=1ll*lz[rt]*(m-l+1);\n c[rt<<1|1]=1ll*lz[rt]*(r-m);\n a[rt<<1]=a[rt<<1|1]=a[rt];\n lz[rt<<1]=lz[rt<<1|1]=lz[rt];\n lz[rt]=0;\n}\n\nvoid modify(ll o,ll l,ll r)\n{\n ll rt = o;ll x = yl;ll y = yr;\n ll tmp = u;\n// if(yl <= l && yr >= r){\n// c[o] = 1LL * u *(r - l + 1);\n// s[o] = u;\n// lz[o] = u;return;\n// }\n// pushdown(o,l,r);\n// int m = (l + r) >> 1;\n// if(yl <= m)update(o*2,l,m);\n// if(yr > m) update(o*2+1 , m+1,r);\n// c[o] = c[o*2] + c[o*2+1];\n// s[o] = min(s[o*2],s[o*2+1]);\n// if(lz[o*2] == lz[o*2+1] && lz[o*2] >= 0)lz[o] = lz[o*2];\n\n if(x<=l&&r<=y){\n c[rt]=1ll*tmp*(r-l+1);\n a[rt]=lz[rt]=tmp;\n return;\n }\n down(rt,l,r);\n int m=(l+r)>>1;\n if(x<=m)modify(rt<<1,l,m);\n if(y>m)modify(rt<<1|1,m+1,r);\n c[rt]=c[rt<<1]+c[rt<<1|1];\n a[rt]=min(a[rt<<1],a[rt<<1|1]);\n}\n\nll ask(ll o,ll l,ll r)\n{\n// if(l == r && c[o] < u )return l;\n// if(l == r && c[o] >=u )return 0;\n// if(lz[o]!=-1 && lz[o] < u)return r;\n// if(lz[o] != -1 && lz[o] >= u)return 0;\n// int mid = (l+r)>>1;\n// if(s[o*2+1] < u)return query(o*2+1,mid+1,r);\n// else return query(o*2,l,mid);\n ll rt = o;ll k = u;\n if(l==r)return l;\n int m=(l+r)>>1;\n if(a[rt<<1|1]= 0; --i){\n h[i] = sum - c[1];\n ll k = v[i].size();\n if(k < 2){continue;}\n u = n + 1;\n yl = v[i][1]+1;yr = n;\n if(yl <= yr)\n modify(1,1,n);\n\n u = v[i][k-1];\n// int pos = query(1,1,n);\n// pos = min(pos , v[i][1]);\n// if(pos > v[i][0] && pos <= v[i][1]){\n// yl = v[i][0] + 1;yr = pos;\n// update(1,1,n);\n// }\n if(a[1] < u){\n ll pos = ask(1,1,n);\n pos = min(pos,v[i][1]);\n if(pos > v[i][0]){\n yl = v[i][0] + 1;yr = pos;\n modify(1,1,n);\n }\n }\n\n u = v[i][k-2];\n// pos = query(1,1,n);\n// pos = min(pos , v[i][0]);\n// if(pos >= 1 && pos <= v[i][0]){\n// yl = 1;yr = pos;\n// update(1,1,n);\n// }\n if(a[1] < u){\n ll pos = ask(1,1,n);\n pos = min(pos,v[i][0]);\n yl = 1;yr = pos;\n if(pos > 0)modify(1,1,n);\n }\n }\n ll ans = 0;\n for(int i = 1 ;i <= mx; ++i){\n ans += 1LL * i * (h[i] - h[i-1]);\n }\n printf(\"%I64d\\n\",ans);\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "f63ddd0d081f47affc1e65359d49d53a", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define I64d_OJ\n\n#ifdef I64d_OJ\n#define LL __int64\n#endif // I64d_OJ\n//#ifdef I64d_OJ\n//#define LL long long\n//#endif // I64d_OJ\n\ntypedef unsigned LL ULL;\ntypedef unsigned uint;\ntypedef pair pii;\ntypedef pair pdd;\ntypedef pair pLL;\ntypedef vector vi;\n\ninline LL read(){\n LL x=0,f=1;\n char ch=getchar();\n while(ch<'0'||ch>'9'){if(ch=='-') f=-f; ch=getchar();}\n while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}\n return x*f;\n}\n\n#define lson l,mid,rt<<1\n#define rson mid+1,r,rt<<1|1\n#define ls rt<<1\n#define rs rt<<1|1\n#define MID(a,b) (((LL)(a)+(LL)(b))>>1)\n#define absx(a) ((a)<0?-(a):(a))\n#define MK(a,b) make_pair(a,b)\n#define PB(a) push_back(a)\n#define lowbit(x) ((x)&-(x))\n\ntemplate< typename T >\ninline void Max(T &a,T &b){if(a\ninline void Min(T &a,T &b){if(a>b) a=b;}\n\nconst double pi=(double)acos(-1.0);\nconst double eps=(double)1e-10;\nconst int INF = (int) 0x3f3f3f3f;\nconst int MOD = (int) 1e9+7;\nconst int MAXN =(int) 2e5+10;\nconst int MAXM = (int) 1e5+10;\n\n///--------------------START-------------------------\nint n;\nvi G[MAXN];\nLL A[MAXN];\nLL sum[MAXN<<2];\nLL mi[MAXN<<2],mx[MAXN<<2],mark[MAXN<<2];\nconst int UP = (int) 2e5;\n\nvoid Push_up(int rt){\n sum[rt]=sum[ls]+sum[rs];\n mx[rt]=max(mx[ls],mx[rs]);\n mi[rt]=min(mi[ls],mi[rs]);\n}\n\nvoid build(int l,int r,int rt){\n mark[rt]=0;\n if(l==r){\n mi[rt]=mx[rt]=sum[rt]=l;\n return;\n }\n int mid=(l+r)>>1;\n build(lson);\n build(rson);\n Push_up(rt);\n}\n\nvoid Push_down(int len,int rt){\n if(!mark[rt]) return;\n mark[ls]=mark[rs]=mark[rt];\n mx[ls]=mx[rs]=mark[rt];\n mi[ls]=mi[rs]=mark[rt];\n sum[ls]=mark[rt]*(len-(len>>1));\n sum[rs]=mark[rt]*(len>>1);\n mark[rt]=0;\n}\n\nvoid Maintain(int v,int l,int r,int rt){\n if(v<=mi[rt]) return;\n if(v>mx[rt]){\n mi[rt]=mx[rt]=mark[rt]=v;\n sum[rt]=(LL)v*(r-l+1);\n return;\n }\n Push_down(r-l+1,rt);\n int mid=(l+r)>>1;\n Maintain(v,lson);\n Maintain(v,rson);\n Push_up(rt);\n}\n\nint __ul,__ur,__uv;\nvoid update(int l,int r,int rt){\n if(__ul<=l&&r<=__ur){\n Maintain(__uv,l,r,rt);\n return;\n }\n Push_down(r-l+1,rt);\n int mid=(l+r)>>1;\n if(__ul<=mid) update(lson);\n if(__ur>mid) update(rson);\n Push_up(rt);\n}\n\nvoid work(){\n scanf(\"%d\",&n);\n for(int i=1;i<=n;i++){\n int x; scanf(\"%d\",&x);\n for(int j=1;j*j<=x;j++){\n if(x%j==0){\n G[j].PB(i);\n if(j*j!=x) G[x/j].PB(i);\n }\n }\n }\n build(1,n,1);\n for(int i=UP;i>=0;i--){\n A[i]=(LL)n*(n+1)-sum[1];\n// if(i<=10) printf(\"i = %d , sum[1] = %I64d\\n\",i,sum[1]);\n int k=G[i].size();\n if(k>=2){\n __ul=1,__ur=G[i][0],__uv=G[i][k-2];\n update(1,n,1);\n __ul=G[i][0]+1,__ur=G[i][1],__uv=G[i][k-1];\n update(1,n,1);\n __ul=G[i][1]+1,__ur=n,__uv=n+1;\n if(__ul<=__ur) update(1,n,1);\n }\n }\n LL ans=0;\n// for(int i=1;i<=6;i++) printf(\"A[%d] = %I64d\\n\",i,A[i]);\n for(int i=1;i<=UP;i++){\n ans+=(A[i]-A[i-1])*i;\n }\n printf(\"%I64d\\n\",ans);\n}\n\n///--------------------END-------------------------\nint main(){\n#ifdef NNever\n///freopen(\"data.in\",\"r\",stdin);\n///freopen(\"out.txt\",\"w\",stdout);\n#endif // NNever\n work();\n return 0;\n}\n\n\n\n\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "360bbefffb821ac6922b9fdb1778f9e2", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define I64d_OJ\n\n#ifdef I64d_OJ\n#define LL __int64\n#endif // I64d_OJ\n//#ifdef I64d_OJ\n//#define LL long long\n//#endif // I64d_OJ\n\ntypedef unsigned LL ULL;\ntypedef unsigned uint;\ntypedef pair pii;\ntypedef pair pdd;\ntypedef pair pLL;\ntypedef vector vi;\n\ninline LL read(){\n LL x=0,f=1;\n char ch=getchar();\n while(ch<'0'||ch>'9'){if(ch=='-') f=-f; ch=getchar();}\n while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}\n return x*f;\n}\n\n#define lson l,mid,rt<<1\n#define rson mid+1,r,rt<<1|1\n#define ls rt<<1\n#define rs rt<<1|1\n#define MID(a,b) (((LL)(a)+(LL)(b))>>1)\n#define absx(a) ((a)<0?-(a):(a))\n#define MK(a,b) make_pair(a,b)\n#define PB(a) push_back(a)\n#define lowbit(x) ((x)&-(x))\n\ntemplate< typename T >\ninline void Max(T &a,T &b){if(a\ninline void Min(T &a,T &b){if(a>b) a=b;}\n\nconst double pi=(double)acos(-1.0);\nconst double eps=(double)1e-10;\nconst int INF = (int) 0x7fffffff;\nconst int MOD = (int) 1e9+7;\nconst int MAXN =(int) 2e5+10;\nconst int MAXM = (int) 1e5+10;\n\n///--------------------START-------------------------\nint n;\nconst int UP=30;\nbool vis[MAXN];\nvi G[MAXN];\nLL A[MAXN];\nint a[MAXN];\nLL sum[MAXN<<2];\nLL mx[MAXN<<2],mi[MAXN<<2],mark[MAXN<<2];\nLL L1[MAXN],L2[MAXN],R1[MAXN],R2[MAXN];\n\nvoid Push_up(int rt){\n mx[rt]=max(mx[ls],mx[rs]);\n mi[rt]=min(mi[ls],mi[rs]);\n sum[rt]=sum[ls]+sum[rs];\n}\n\nvoid build(int l,int r,int rt){\n mark[rt]=0;\n if(l==r){\n mx[rt]=mi[rt]=sum[rt]=l;\n return;\n }\n int mid=(l+r)>>1;\n build(lson);\n build(rson);\n Push_up(rt);\n}\n\nvoid Push_down(int len,int rt){\n if(!mark[rt]) return;\n mark[ls]=mark[rs]=mark[rt];\n mx[ls]=mx[rs]=mark[rt];\n mi[ls]=mi[rs]=mark[rt];\n sum[ls]=mark[rt]*(len-(len>>1));\n sum[rs]=mark[rt]*(len>>1);\n mark[rt]=0;\n}\n\nvoid Maintain(LL val,int l,int r,int rt){\n if(val<=mi[rt]) return;\n if(mx[rt]>1;\n Maintain(val,lson);\n Maintain(val,rson);\n Push_up(rt);\n}\n\nint __ul,__ur,__uv;\nvoid update(int l,int r,int rt){\n ///if(__uv<=mi[rt]) return;\n if(__ul<=l&&r<=__ur){\n Maintain(__uv,l,r,rt);\n return;\n }\n Push_down(r-l+1,rt);\n int mid=(l+r)>>1;\n if(__ul<=mid) update(lson);\n if(__ur>mid) update(rson);\n Push_up(rt);\n}\n\nvoid work(){\n cin>>n;\n for(int i=1 ; i<=n ; i++){\n scanf(\"%d\",a+i);\n\n int x = sqrt(a[i]);\n\n for(int j=1 ; j<=x ; j++)\n if(a[i]%j == 0){\n if(!L1[j])\n L1[j] = i;\n else if(!L2[j]){\n L2[j] = i;\n }\n R2[j] = R1[j];\n R1[j] = i;\n\n if(j*j!=a[i]){\n\n if(!L1[a[i]/j])\n L1[a[i]/j] = i;\n else if(!L2[a[i]/j]){\n L2[a[i]/j] = i;\n }\n R2[a[i]/j] = R1[a[i]/j];\n R1[a[i]/j] = i;\n }\n }\n }\n\n for(int i=1 ; i<=n ; i++){\n __ul=__ur=__uv=i;\n update(1,n,1);\n }\n\n for(int i=2e5+1 ; i>=1 ; i--){\n if(L1[i] != R1[i]){\n __ul=1,__ur=L1[i],__uv=R2[i];\n update(1,n,1);\n __ul=L1[i]+1,__ur=L2[i],__uv=R1[i];\n update(1,n,1);\n __ul=L2[i]+1,__ur=n,__uv=n+1;\n update(1,n,1);\n }\n A[i] = (LL)n*(n+1) - sum[1];\n }\n\n LL ans = 0;\n\n for(int i=1 ; i<=2e5 ; i++)\n ans += (A[i+1]-A[i])*i;\n\n cout << ans << endl;\n}\n\n///--------------------END-------------------------\nint main(){\n#ifdef NNever\nfreopen(\"data.in\",\"r\",stdin);\n///freopen(\"out.txt\",\"w\",stdout);\n#endif // NNever\n work();\n return 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "3e892269d968cb307414a332381e10f1", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#define lson l, mid, x<<1\n#define rson mid+1, r, x<<1|1\nusing namespace std;\nconst int maxn=300005;\ntypedef __int64 ll;\nstruct node\n{\n int ma;\n int mi;\n ll sum;\n int lazy;\n int l, r;\n}tree[maxn*4];\n\nll getsum(int x)\n{\n if( tree[x].lazy ) return 1LL*(tree[x].r-tree[x].l+1)*tree[x].lazy;\n else return tree[x].sum;\n}\n\nint getmax( int x )\n{\n if( tree[x].lazy ) return tree[x].lazy;\n else return tree[x].ma;\n}\n\nint getmin( int x )\n{\n if( tree[x].lazy ) return tree[x].lazy;\n else return tree[x].mi;\n}\n\nvoid pushup( int x )\n{\n tree[x].sum = getsum( x << 1 ) + getsum( x << 1 | 1 );\n tree[x].ma = max( getmax(x<<1), getmax(x<<1|1) );\n tree[x].mi = min( getmin(x<<1), getmin(x<<1|1) );\n}\n\nvoid pushdown( int x )\n{\n if( tree[x].lazy ){\n tree[x<<1].lazy = tree[x<<1|1].lazy = tree[x].lazy;\n tree[x].mi = tree[x].ma = tree[x].lazy;\n tree[x].sum = (ll)tree[x].lazy*(tree[x].r-tree[x].l+1);\n tree[x].lazy = 0;\n }\n}\n\nvoid build( int l, int r, int x )\n{\n tree[x].l = l;tree[x].r = r;tree[x].lazy = 0;\n if( l == r ){\n tree[x].sum = tree[x].mi = tree[x].ma = l;\n return;\n }\n int mid = (l+r)>>1;\n build( lson );\n build( rson );\n pushup(x);\n}\n\nvoid update( int L, int R, int val, int l, int r, int x )\n{\n if( R < L ) return;\n pushdown(x);// 在对区间做任何修改之前,先pushdown\n if( tree[x].mi >= val ){\n return ;\n }\n int mid = (l+r) >> 1;\n if( L == l && R == r ){\n if( tree[x].ma <= val ){\n tree[x].lazy = val;\n return ;\n }\n else{\n update( L, mid, val, lson );\n update( mid+1, R, val, rson );\n pushup(x);\n return ;\n }\n }\n if( R <= mid ) update( L, R, val, lson );\n else if( L > mid ) update( L, R, val, rson );\n else{\n update( L, mid, val, lson );\n update( mid+1, R, val, rson );\n }\n pushup(x);\n}\n\n\nint pos[maxn];\nll h[maxn];\nvectorp;\n\nint main()\n{\n int i, j, n, m, x;\n scanf(\"%d\", &n);\n for( i = 1; i <= n; i ++ ){\n scanf(\"%d\", &x);\n pos[x] = i;\n }\n build( 1, n, 1 );\n for( i = maxn-2; i >= 1; i -- ){\n p.clear();\n for( j = i+1; j < maxn; j += i+1 ){\n if( pos[j] ){\n p.push_back(pos[j]);\n }\n }\n sort( p.begin(), p.end() );\n int t = p.size();\n if( t > 1 ){\n update( 1, p[0], p[t-2], 1, n, 1 );\n update( p[0]+1, p[1], p[t-1], 1, n, 1 );\n update( p[1]+1, n, n+1, 1, n, 1 );\n// printf(\"id%d %d %d %d\\n\", i, 1,p[0],p[t-2]);\n// printf(\"id%d %d %d %d\\n\", i, p[0]+1,p[1],p[t-1]);\n// printf(\"id%d %d %d %d\\n\", i, p[1]+1,n,n+1);\n }\n h[i] = 1LL*n*(n+1)-getsum(1);\n }\n h[0] = 3;\n ll ans = 0;\n for( i = 1; i <= maxn-2; i ++ ){\n //printf(\"%d %I64d %I64d\\n\", i, h[i], ans);\n if( h[i]-h[i-1] >= 0 ) ans += 1LL*i*(h[i]-h[i-1]);\n }\n printf(\"%I64d\\n\", ans);\n}\n", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "d209cb379275f5d1f4c26ad13220e337", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "// 2011_2.cpp : Defines the entry point for the console application.\n//\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nFILE * in, *out;\n\n#define fo(a,b,c) for(int a = ( b ); a < ( c ); ++ a )\n#define fr(a,b) fo( a, 0, ( b ) )\n#define fi(a) fr( i, ( a ) )\n#define fj(a) fr( j, ( a ) )\n#define fk(a) fr( k, ( a ) )\n#define mp make_pair\n#define pb push_back\n#define all(v) (v).begin( ), (v).end( )\n#define _(a,b) memset( a, b, sizeof( a ) )\n\n#define LL long long\n\nint ri() { int a; fscanf(in, \"%d\", &a); return a; }\ndouble rf() { double a; fscanf(in, \"%lf\", &a); return a; }\nchar sbuf[100005];\nstring rstr()\n{\n\t//fscanf(in, \"%lf\", sbuf); \n\tchar c;\n\tchar * b = sbuf;\n\twhile (c = fgetc(in))\n\t{\n\t\tif (c == '\\n' || c == 255) break;\n\t\t*b++ = c;\n\t}\n\t*b = 0;\n\treturn sbuf;\n}\n\n\nint nod(int a, int b)\n{\n\tif (a == 1 || b == 1) return 1;\n\tif (a == 0) return b;\n\tif (b == 0) return a;\n\n\treturn nod(b%a, a);\n}\n\nvector primes;\nvoid calculatePrimes()\n{\n\tprimes.push_back(2);\n\tfor (int i = 3; i < 500; i += 2)\n\t{\n\t\tbool bFound = false;\n\t\tint m = (int)sqrt((float)i);\n\t\tfor (int j = 1; j < primes.size() && !bFound && primes[j] <= m; j++)\n\t\t\tbFound = ((i%primes[j]) == 0);\n\t\tif (!bFound)\n\t\t\tprimes.push_back(i);\n\t}\n}\n\nvoid fillMap(map & pr, map> & pos, int i, map::iterator j, int cur)\n{\n\tif (j == pr.end())\n\t{\n\t\tif (pos[cur].size() == 0 || pos[cur].back() != i)\n\t\t{\n\t\t\tif (pos[cur].size() < 4)\n\t\t\t\tpos[cur].push_back(i);\n\t\t\telse\n\t\t\t{\n\t\t\t\tpos[cur][pos[cur].size() - 2] = pos[cur].back();\n\t\t\t\tpos[cur][pos[cur].size() - 1] = i;\n\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\n\tint mult = 1;\n\tint second = j->second;\n\tint first = j->first;\n\tj++;\n\n\tfor (int k = 0; k <= second; k++)\n\t{\n\t\tfillMap(pr, pos, i, j, cur*mult);\n\t\tmult *= first;\n\t}\n}\n\n\nlong long insertSegment(int from, int to, set,int>> & fills)\n{\n\tlong long ar = 0;\n\tint st = 0;\n\tif (from == to) return st;\n\tint line = from;\n\tset, int>>::iterator start = fills.lower_bound(mp(mp(from, to), 0));\n\tif (start != fills.begin()) start--;\n\n\tset, int>>::iterator end = fills.upper_bound(mp(mp(to, to), 0));\n\tfor (set, int>>::iterator i = start; i != end; )\n\t{\n\t\tif (i->first.second >= from)\n\t\t{\n\t\t\tlong long next = min(i->first.second, to);\n\t\t\tlong long f = max(i->first.first, from);\n\t\t\tif (i->second == -1 || i->second > line)\n\t\t\t{\n\t\t\t\tint save = i->second;\n\t\t\t\tint end = i->first.second;\n\t\t\t\tint beg = i->first.first;\n\t\t\t\tar += (i->second == -1)?(f + next + 1 - 2*line)*(next - f) / 2:(i->second - line)*(next - f);\n\t\t\t\ti = fills.erase(i);\n\t\t\t\tif (to == end)\n\t\t\t\t{\n\t\t\t\t\tif (beg < from)\n\t\t\t\t\t\tfills.insert(mp(mp(beg, from), save));\n\t\t\t\t\tfills.insert(mp(mp(from, to), line));\n\t\t\t\t\treturn ar;\n\t\t\t\t}\n\t\t\t\telse if (end < to)\n\t\t\t\t{\n\t\t\t\t\tif (beg < from)\n\t\t\t\t\t\tfills.insert(mp(mp(beg, from), save));\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (beg < from)\n\t\t\t\t\t\tfills.insert(mp(mp(beg, from), save));\n\t\t\t\t\tfills.insert(mp(mp(from, to), line));\n\t\t\t\t\tfills.insert(mp(mp(to, end), save));\n\t\t\t\t\treturn ar;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t\tfrom = next;\n\t\t\t\tif (from == to) return st;\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\telse i++;\n\t}\n\treturn ar;\n}\n\nint main()\n{\n\tcalculatePrimes();\n\n\tint n;\n\tcin >> n;\n\t\n\tmap> pos;\n\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tint a;\n\t\tcin >> a;\n\t\t//a = rand() * 200000l / RAND_MAX;\n\t\tmap pr;\n\t\tfor (int j = 0; j < primes.size() && a >= primes[j]*primes[j]; j++)\n\t\t{\n\t\t\twhile (a%primes[j] == 0)\n\t\t\t{\n\t\t\t\tpr[primes[j]]++;\n\t\t\t\ta /= primes[j];\n\t\t\t}\n\t\t}\n\t\tif(a > 1)\n\t\t\tpr[a]++;\n\t\tfillMap(pr,pos,i,pr.begin(),-1);\n\t}\n\n\tmap>::iterator i = pos.begin();\n\n\n\tset, int>> fills;\n\tfills.insert(mp(mp(0, n), -1));\n\n\tlong long totalsum = 0;\n\n\twhile (i != pos.end())\n\t{\n\t\tvector & cur = i->second;\n\t\tif (cur.size() <= 1) { i++; continue; }\n\n\n\t\ttotalsum += insertSegment(0, cur[cur.size() - 2], fills) * i->first;\n\t\ttotalsum += insertSegment(cur[0] + 1, cur.back(), fills) * i->first;\n\t\ttotalsum += insertSegment(cur[1] + 1, n, fills) * i->first;\n\n\t\ti++;\n\t}\n\tcout << -totalsum;\n}", "lang_cluster": "C++", "tags": ["number theory", "data structures"], "code_uid": "0d3f11b7ce2fbd56893da8ac3fef37d7", "src_uid": "deb3938a6d3f13c4ab8a0765bf0e94b0", "difficulty": 2800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//#define _USE_MATH_DEFINES\n#include \n#define Y imag()\n#define X real()\n#define inf 1000000000\n#define eps 1e-9\n#define Pi acos(-1)\nusing namespace std;\ntypedef complex point;\nint x[21],y[21],angle[21];\ndouble memo[1<<20];\nint n,sx,ex;\n//constexpr double PI() {return acos(-1.0);}\n\ndouble fun(double sx,int i)\n{\n point p1(x[i],y[i]);\n point p2(sx,0);\n point v=p2-p1; ///vecteur directeur\n v*=exp(point(0,angle[i]*Pi/180)); /// nouveau vecteur directeur\n /// P(t) = p1+t*v equation de la droite aprés la rotation\n /// y(t) = y1+t*v\n /// 0 = p1.Y + t*v.Y intersection avec l'axe des abcisses\n if(atan2(v.Y,v.X)>-eps) {return ex;}\n double t = -p1.Y/v.Y;\n double x = p1.X+t*v.X;\n return x;\n}\n\ndouble solve(int mask)\n{\n if(!mask) return sx;\n double &ret=memo[mask];\n if(ret == ret) return ret;\n ret=sx;\n for(int i=mask;i;i-=i&(-i))\n {\n int j=i&(-i);\n double x=solve(mask^j);\n ret = max(ret,fun(x,log2(j)+eps));\n\n }\n return memo[mask]=ret = min(ret,ex*1.0);\n}\n\n\n\n\n\nint main()\n{ //ifstream cin(\"in\");\n cin>>n>>sx>>ex;\n memset(memo,-1,sizeof memo);\n for(int i=0;i>x[i]>>y[i]>>angle[i];\n double answ=solve((1<<(n))-1);\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define mp make_pair\n#define pii pair\n#define all(x) x.begin(),x.end()\n#define pb(x) push_back(x);\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n\n#define MOD 998244353 //(1000*1000*1000+7)\n#define inf1 2e9\n#define inf2 2e18\ndefault_random_engine generator;\nuniform_int_distribution distribution(1, INT_MAX);\n\n\n\nvector > one(int sz) {\n\tvector > ret;\n\tret.resize(sz);\n\tfor (int i = 0; i > mult(vector > a, vector > b) {\n\tvector > ret;\n\tret.resize(a.size());\n\tfor (int i = 0; i > pw(vector > a, long long b) {\n\tif (b == 0)\n\t\treturn one((int)a.size());\n\tif (b % 2)\n\t\treturn mult(a, pw(a, b - 1));\n\treturn pw(mult(a, a), b / 2);\n}\n\n\n\nll my_rand(ll a = 0, ll b = INT_MAX) {\n\treturn a + distribution(generator) % b;\n}\n\n\ninline int mul(int a, int b) {\n\treturn int(a * 1ll * b % MOD);\n}\n\ninline int norm(int a) {\n\tif (a >= MOD)\n\t\ta -= MOD;\n\tif (a < 0)\n\t\ta += MOD;\n\treturn a;\n}\n\ninline int binPow(int a, int k) {\n\tint ans = 1;\n\twhile (k > 0) {\n\t\tif (k & 1)\n\t\t\tans = mul(ans, a);\n\t\ta = mul(a, a);\n\t\tk >>= 1;\n\t}\n\treturn ans;\n}\n\ninline int subtract(int a, int b) {\n\treturn norm(a - b);\n}\n\ninline int sum(int a, int b) {\n\treturn int((a + b) % MOD);\n}\n\ninline int inv(int a) {\n\treturn binPow(a, MOD - 2);\n}\n\n\nll ceil_int(ll a, ll b) {\n\treturn a / b + (a%b != 0);\n}\n\n\nll gcd(ll a, ll b) {\n\tif (a == 0)return b;\n\treturn gcd(b%a, a);\n}\n\nll lcm(ll a, ll b) {\n\treturn (a*b) / gcd(a, b);\n}\n\nint main() {\n\tgenerator.seed(chrono::system_clock::now().time_since_epoch().count());\n\tcin.sync_with_stdio(false);\n\tcin.tie(0);\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n\tint n;\n\tcin >> n;\n\tvector>p(n);\n\tstring s;\n\tvectorused(n);\n\tint start_point = 0;\n\tvectorans;\n\tfor (int i = 0; i < n; i++) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\tp[i] = { x,y };\n\t\tif (p[start_point].second > p[i].second) {\n\t\t\tstart_point = i;\n\t\t}\n\t}\n\tcin >> s;\n\ts += '#';\n\tans.push_back(start_point);\n\tused[start_point] = 1;\n\tint current_point = start_point;\n\tfor (int i = 0; i < n - 1; i++) {\n\t\tint next = -1;\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tif (!used[j] && next == -1) {\n\t\t\t\tnext = j;\n\t\t\t}\n\t\t\telse if (!used[j]) {\n\t\t\t\tll x1 = p[next].first - p[current_point].first;\n\t\t\t\tll y1 = p[next].second - p[current_point].second;\n\t\t\t\tll x2 = p[j].first - p[current_point].first;\n\t\t\t\tll y2 = p[j].second - p[current_point].second;\n\t\t\t\tif (s[i] == 'R') {\n\t\t\t\t\tif (x1*y2 - x2*y1 > 0) {\n\t\t\t\t\t\tnext = j;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (x1*y2 - x2*y1 < 0) {\n\t\t\t\t\t\tnext = j;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tused[next] = 1;\n\t\tcurrent_point = next;\n\t\tans.push_back(next);\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tcout << ans[i]+1 << \" \";\n\t}\n\treturn 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["math", "geometry", "constructive algorithms"], "code_uid": "3bd36530e5282c444e12e7afcad47c02", "src_uid": "13d7f6127a7fe945e19d461b584c6228", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define FOR(i, n) for(int i = 0; i < (n); ++i)\n#define REP(i, a, b) for(int i = (a); i < (b); ++i)\n#define TRAV(i, n) for(auto &i : (n))\n#define SZ(x) (int)(x).size()\n#define PR std::pair\n#define MP std::make_pair\n#define X first\n#define Y second\ntypedef long long ll;\ntypedef std::pair PII;\ntypedef std::vector VI;\n\nconst int MOD = 1000000007;\nint modpow(int a, int e){\n\tint ret = 1;\n\twhile(e > 0){\n\t\tif(e&1) ret = (1ll*ret*a)%MOD;\n\t\ta = (1ll*a*a)%MOD;\n\t\te>>=1;\n\t}\n\treturn ret;\n}\n\nint pr[6][6];\nint sub[2][1<<18];\nint mas[1<<18];\nint p[2][1<<20];\nVI m3;\nint comp[20];\n\nint cm(int a){\n\tint ret = 0;\n\tFOR(i, 20) if(a & (1< map;\n\tFOR(i, 1<<6) if(__builtin_popcount(i) == 3) m3.push_back(i), map[i] = SZ(m3)-1;\n\tFOR(i, 1<<6) if(__builtin_popcount(i) == 3) comp[map[i]] = map[i^63];\n\tassert(SZ(map) == 20);\n\tint n;\n\tstd::cin >> n;\n\tFOR(i, n) FOR(j, n) std::cin >> pr[i][j], pr[i][j] = (1ll*pr[i][j]*modpow(100, MOD-2))%MOD;\n\tREP(i, n, 6) pr[i][i] = 1;\n\tn = 6;\n\tFOR(mask, 1<<18){\n\t\tsub[0][mask] = 1;\n\t\tsub[1][mask] = 1;\n\t\tFOR(i, 6) FOR(j, 3){\n\t\t\tif(mask & (1<<((i*3)+j))){\n\t\t\t\tsub[0][mask] = (1ll*sub[0][mask]*pr[i][j])%MOD;\n\t\t\t\tsub[1][mask] = (1ll*sub[1][mask]*pr[i][3+j])%MOD;\n\t\t\t}else{\n\t\t\t\tint od = 1-pr[i][j];\n\t\t\t\tif(od < 0) od += MOD;\n\t\t\t\tsub[0][mask] = (1ll*sub[0][mask]*od)%MOD;\n\t\t\t\tod = 1-pr[i][3+j];\n\t\t\t\tif(od < 0) od += MOD;\n\t\t\t\tsub[1][mask] = (1ll*sub[1][mask]*od)%MOD;\n\t\t\t}\n\t\t}\n\t\tint ind = 0;\n\t\tTRAV(m, m3){\n\t\t\tVI guys;\n\t\t\tFOR(i, 6) if(m & (1<> (guys[i]*3)) & 7;\n\t\t\t\tif(__builtin_popcount(xd) > __builtin_popcount(ms)){\n\t\t\t\t\tbad = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!bad) mas[mask] += (1<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define x first\n#define y second\ntypedef std::pair pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair puu;\n\ntemplate std::istream&operator>>(std::istream&i, pair&p) {i >> p.x >> p.y; return i;}\ntemplatestd::istream&operator>>(std::istream&i,vector&t) {for(auto&v:t){i>>v;}return i;}\ntemplate std::ostream&operator<<(std::ostream&o, const pair&p) {o << p.x << ' ' << p.y; return o;}\ntemplatestd::ostream&operator<<(std::ostream&o,const vector&t) {if(t.empty())o<<'\\n';for(size_t i=0;i using minheap = priority_queue, greater>;\ntemplate using maxheap = priority_queue, less>;\nui logceil(ll x) { return x?8*sizeof(ll)-__builtin_clzll(x):0; }\n\nnamespace std { templatestruct hash>{hasht;hashu;size_t operator()(const pair&p)const{return t(p.x)^(u(p.y)<<7);}}; }\ntemplateT bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}\ntemplate double bshd(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){l=m;}else{h=m;}}return (l+h)/2;}\ntemplateT bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}\ntemplate double bsld(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){h=m;}else{l=m;}}return (l+h)/2;}\ntemplate T gcd(T a,T b) { if (aclass vector2:public vector>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector>(a,vector(b,t)){}};\ntemplateclass vector3:public vector>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector>(a,vector2(b,c,t)){}};\ntemplateclass vector4:public vector>{public:vector4(){} vector4(size_t a,size_t b,size_t c,size_t d,T t=T()):vector>(a,vector3(b,c,d,t)){}};\ntemplateclass vector5:public vector>{public:vector5(){} vector5(size_t a,size_t b,size_t c,size_t d,size_t e,T t=T()):vector>(a,vector4(b,c,d,e,t)){}};\n\n\n#endif\n#ifndef MOD_H\n#define MOD_H\n\n\n\ntemplate class Field {\n typedef unsigned int ui;\n typedef unsigned long long ull;\n\tinline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}\n\t/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/\n\tinline ui inv(ui a){return pow(a,N-2);}\npublic:\n inline Field(int x = 0) : v(x) {}\n\tinline Field pow(int p){return (*this)^p; }\n\tinline Field operator^(int p){return {(int)pow(v,(ui)p)};}\n inline Field&operator+=(const Field&o) {if (v+o.v >= N) v += o.v - N; else v += o.v; return *this; }\n inline Field&operator-=(const Field&o) {if (v&operator*=(const Field&o) {v=(ull)v*o.v % N; return *this; }\n inline Field&operator/=(const Field&o) { return *this*=inv(o.v); }\n inline Field operator+(const Field&o) const {Fieldr{*this};return r+=o;}\n inline Field operator-(const Field&o) const {Fieldr{*this};return r-=o;}\n inline Field operator*(const Field&o) const {Fieldr{*this};return r*=o;}\n inline Field operator/(const Field&o) const {Fieldr{*this};return r/=o;}\n inline Field operator-() {if(v) return {(int)(N-v)}; else return {0};};\n inline Field& operator++() { ++v; if (v==N) v=0; return *this; }\n inline Field operator++(int) { Fieldr{*this}; ++*this; return r; }\n inline Field& operator--() { --v; if (v==-1) v=N-1; return *this; }\n inline Field operator--(int) { Fieldr{*this}; --*this; return r; }\n inline bool operator==(const Field&o) const { return o.v==v; }\n\tinline bool operator!=(const Field&o) const { return o.v!=v; }\n\tinline explicit operator ui() const { return v; }\n\tinline static vector>fact(int t){vector>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}\n\tinline static vector>invfact(int t){vector>F(t+1,1);Field X{1};for(int i=2;i<=t;++i){X=X*i;}F[t]=1/X;for(int i=t-1;i>=2;--i){F[i]=F[i+1]*(i+1);}return F;}\nprivate: ui v;\n};\ntemplateistream &operator>>(std::istream&is,Field&f){unsigned int v;is>>v;f=v;return is;}\ntemplateostream &operator<<(std::ostream&os,const Field&f){return os<<(unsigned int)f;}\ntemplateField operator+(int i,const Field&f){return Field(i)+f;}\ntemplateField operator-(int i,const Field&f){return Field(i)-f;}\ntemplateField operator*(int i,const Field&f){return Field(i)*f;}\ntemplateField operator/(int i,const Field&f){return Field(i)/f;}\n\n\ntypedef Field<1000000007> FieldMod;\n\nstruct Ring {\n\ttemplate \n\tstatic T div(T p, T q, T N) {\n\t\tT t=0,nt=1,r=N,nr=q;\n\t\twhile(nr!=0){ T q=r/nr;t-=q*nt;r-=q*nr;swap(t,nt);swap(r,nr); }\n\t\tt=(t<0)?t+N:t;\n\t\tr=(r<0)?r+N:r;\n\t\tif (gcd(p,N)%r!=0) { return 0; }\n\t\treturn (t*p/r)%N;\n\t}\n};\n#endif\n\n\nconstexpr int S = 128;\nnamespace std {\n template bool operator<(const std::bitset &x, const std::bitset &y) {\n for (int i = N - 1; i >= 0; i--) {\n if (x[i] ^ y[i]) return y[i];\n }\n return false;\n }\n}\n\nclass E2MarekAndMatchingHardVersion {\npublic:\n void solve(istream& cin, ostream& cout) {\n int N; cin >> N;\n vector2 P(N, N); cin >> P;\n map, FieldMod> G;\n G[1] = 1;\n for (int i = 0; i < N; ++i) {\n map, FieldMod> H;\n for (auto g: G) {\n vector S;\n for (int m = 0; m < (1< ans;\n for (int l = 0; l < N; ++l) {\n if ((j&1<\n\nusing namespace std;\n\nconst int mod = 1000000007;\n\nconst int N = 1 << 20;\n\nint fpow(int x, int y) {\n int ret = 1;\n while(y) {\n if(y & 1) ret = (1LL * ret * x) % mod;\n x = (1LL * x * x) % mod;\n y >>= 1;\n }\n return ret;\n}\n\nconst int M = 21;\n\nint n;\nint f[N], g[N];\nint p[M][M];\n\nint idx[2][N];\nint rev[N];\nint matched[M];\nint adj[M][M];\n\nvoid work(int *f, vector < int > l, vector < int > r, vector < int > subgraph, int *idx) {\n\n vector < pair < int, int > > edges;\n for(int x : l) {\n for(int y : r) {\n edges.emplace_back(x, y);\n }\n }\n\n for(int mask = 0; mask < (1 << edges.size()); ++mask) {\n int rate = 1;\n int newmask = 0;\n for(int i = 0; i < edges.size(); ++i) {\n if(mask & (1 << i)) {\n rate = (1LL * rate * p[edges[i].first][edges[i].second]) % mod;\n adj[edges[i].first][edges[i].second] = 1;\n }\n else {\n rate = (1LL * rate * (1 - p[edges[i].first][edges[i].second])) % mod;\n adj[edges[i].first][edges[i].second] = 0;\n }\n rate = (rate + mod) % mod;\n }\n\n for(int gr : subgraph) {\n vector < int > perm, indices;\n for(int x : l) perm.push_back(x);\n for(int i = 0; i < n; ++i) if(gr & (1 << i)) {\n indices.push_back(i + n);\n }\n\n bool flag = false;\n do{\n bool ok = true;\n for(int i = 0; i < perm.size(); ++i){\n if(!adj[perm[i]][indices[i]]) ok = false;\n }\n if(ok) {\n flag = true;\n break;\n }\n }\n while(next_permutation(indices.begin(), indices.end()));\n if(flag) newmask |= (1 << idx[gr]);\n }\n f[newmask] += rate;\n f[newmask] %= mod;\n }\n}\n\nint main(){\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n\n cin >> n;\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < n; ++j) {\n cin >> p[i][j + n];\n p[i][j + n] = (1LL * p[i][j + n] * fpow(100, mod - 2)) % mod;\n }\n }\n\n if(n == 1) {\n cout << p[0][1];\n return 0;\n }\n\n int m1 = n / 2;\n int m2 = n - m1;\n\n int cnt = 0, cnt2 = 0;\n vector < int > sub1, sub2;\n for(int mask = 0; mask < (1 << n); ++mask) {\n if(__builtin_popcount(mask) == m1) {\n idx[0][mask] = cnt++;\n sub1.push_back(mask);\n }\n if(__builtin_popcount(mask) == m2) {\n idx[1][mask] = cnt2++;\n sub2.push_back(mask);\n }\n }\n\n for(int i = 0; i < sub2.size(); ++i) {\n int mask = sub2[i];\n for(int bigmask = 0; bigmask < (1 << cnt2); ++bigmask) {\n if(bigmask & (1 << i)) rev[bigmask] |= (1 << idx[0][(1 << n) - 1 - mask]);\n }\n }\n\n vector < int > l1, l2;\n vector < int > r;\n for(int i = n; i < n + n; ++i) {\n r.push_back(i);\n }\n\n for(int i = 0; i < m1; ++i) l1.push_back(i);\n for(int i = m1; i < n; ++i) l2.push_back(i);\n\n work(f, l1, r, sub1, idx[0]);\n work(g, l2, r, sub2, idx[1]);\n\n\n\n for(int i = 0; i < cnt; ++i) {\n for(int mask = 0; mask < (1 << cnt); ++mask) {\n if(mask & (1 << i)) {\n f[mask] += f[mask ^ (1 << i)];\n f[mask] %= mod;\n }\n }\n }\n\n long long ans = 0;\n\n for(int mask = 1; mask < (1 << cnt2); ++mask) {\n int w = (1 - f[(1 << cnt) - 1 - rev[mask]] + mod) % mod;\n// cout << mask << \" \" << rev[mask] << \" \" << w << \" \" << g[mask] << endl;\n ans += 1LL * w * g[mask] % mod;\n ans %= mod;\n }\n\n cout << ans;\n\n return 0;\n}\n\n\n\n/// moi mask,\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "3ac5d30e50cfa756f556dfd228347378", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n \n#define pb push_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define fst first\n#define snd second\n#define sz(x) (int) ((x).size()) \n#define forn(i, n) for (int i = 0; (i) < (n); ++i)\n#define fornr(i, n) for (int i = (n) - 1; (i) >= 0; --i)\n#define forab(i, a, b) for (int i = (a); (i) < (b); ++i)\n#define forba(i, a, b) for (int i = (b) - 1; (i) >= (a); --i)\n#define forit(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end() \n\n#ifdef LOCAL\n\t#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n\t#define eprintf(...) static_cast(0) \n#endif\n\n#ifdef _WIN32\n\t#define I64 \"%I64d\"\n#else\n\t#define I64 \"%lld\"\n#endif\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef unsigned int uint;\ntypedef vector vi;\ntypedef pair pii;\n\n#define FNAME \"\"\n\nconst int MAX_N = 10, MOD = 1e9 + 7;\n\nint a[MAX_N][MAX_N];\n\ninline int add(int a, int b) {\n\ta += b;\n\treturn a >= MOD ? a - MOD : a;\n}\n\ninline int sub(int a, int b) {\n\ta -= b;\n\treturn a < 0 ? a + MOD : a;\n}\n\ninline int mul(int a, int b) {\n\treturn (a * 1ll * b) % MOD;\n}\n\ninline int power(int a, int n) {\n\tif (!n)\n\t\treturn 1;\n\tint b = power(a, n / 2);\n\tb = mul(b, b);\n\treturn n & 1 ? mul(b, a) : b;\n}\n\ninline int rev(int a) {\n\treturn power(a, MOD - 2);\n}\n\ninline int divv(int a, int b) {\n\treturn mul(a, rev(b));\n}\n\nint main() {\n#ifdef LOCAL \n\tfreopen(FNAME\".in\", \"r\", stdin);\n\tfreopen(FNAME\".out\", \"w\", stdout); \n#endif \n\n\tint n;\n\tscanf(\"%d\", &n);\n\tforn (i, n)\n\t\tforn (j, n) {\n\t\t\tscanf(\"%d\", &a[i][j]);\n\t\t\ta[i][j] = divv(a[i][j], 100);\n\t\t}\n\tvector, int>> sets, newSets;\n\tvector newSet;\n\tsets.pb(mp(vector{0}, 1));\n\tforn (g, n) {\n\t\tnewSets.clear();\n\t\tforn (mask, (1 << n)) {\n\t\t\tint value = 1;\n\t\t\tforn (j, n)\n\t\t\t\tif (mask & (1 << j))\n\t\t\t\t\tvalue = mul(value, a[j][g]);\n\t\t\t\telse\n\t\t\t\t\tvalue = mul(value, sub(1, a[j][g]));\n\t\t\tfor (auto& np : sets) {\n\t\t\t\tnewSet.clear();\n\t\t\t\tfor (int mask2 : np.fs) {\n\t\t\t\t\tint possible = ~mask2 & mask;\n\t\t\t\t\twhile (possible) {\n\t\t\t\t\t\tint pos = possible & (-possible);\n\t\t\t\t\t\tnewSet.pb(mask2 | pos);\n\t\t\t\t\t\tpossible ^= pos;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!sz(newSet))\n\t\t\t\t\tcontinue;\n\t\t\t\tsort(all(newSet));\n\t\t\t\tnewSet.resize(unique(all(newSet)) - newSet.begin());\n\t\t\t\tnewSets.pb(mp(newSet, mul(np.sc, value)));\n\t\t\t}\n\t\t}\n\t\tsort(all(newSets));\n\t\tsets.clear();\n\t\tint i = 0;\n\t\twhile (i < sz(newSets)) {\n\t\t\tint j = i;\n\t\t\tint sumVal = 0;\n\t\t\twhile (j < sz(newSets) && newSets[j].fs == newSets[i].fs)\n\t\t\t\tsumVal = add(sumVal, newSets[j].sc), j++; \n\t\t\tsets.pb(mp(newSets[i].fs, sumVal));\n\t\t\ti = j;\n\t\t}\n\t}\n\tint res = 0;\n\tfor (auto& np : sets)\n\t\tres = add(res, np.sc);\n\tcout << res << \"\\n\";\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "e82a796fd5a9a469e48a3789b2efb137", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n#define BS bitset < 128 >\nunordered_map < BS , int > id; int N , cnt , P[7][7] , dp[2][70003] , trans[70003][128] , prob[128] , iv100;\n\nint poww(long long a , int b){int times = 1; while(b){if(b & 1) times = times * a % MOD; a = a * a % MOD; b >>= 1;} return times;}\n\nint dfs(BS &x){\n\tif(id.find(x) != id.end()) return id[x];\n\tint nd = id[x] = ++cnt; BS tmp[7] , tp;\n\tfor(int i = 0 ; i < N ; ++i) for(int j = 0 ; j < 1 << N ; ++j) tmp[i][j | (1 << i)] = tmp[i][j | (1 << i)] | x[j];\n\tfor(int i = 0 ; i < 1 << N ; ++i){\n\t\ttp = x; for(int j = 0 ; j < N ; ++j) if(i >> j & 1) tp |= tmp[j];\n\t\ttrans[nd][i] = dfs(tp); \n\t}\n\treturn nd;\n}\n\nint main(){\n\tcin >> N; BS tmp; tmp.set(0); dfs(tmp); iv100 = poww(100 , MOD - 2); cerr << id.size() << endl;\n\tfor(int i = 0 ; i < N ; ++i) for(int j = 0 ; j < N ; ++j) cin >> P[i][j];\n\tdp[0][1] = 1; int x = 0;\n\n\tfor(int i = 0 ; i < N ; ++i){\n\t\tfor(int j = 0 ; j < 1 << N ; ++j){\n\t\t\tprob[j] = 1;\n\t\t\tfor(int k = 0 ; k < N ; ++k)\n\t\t\t\tprob[j] = 1ll * prob[j] * iv100 % MOD * (j >> k & 1 ? P[i][k] : 100 - P[i][k]) % MOD;\n\t\t}\n\t\t\n\t\tmemset(dp[x ^= 1] , 0 , sizeof(dp[x]));\n\t\tfor(int j = 1 ; j <= cnt ; ++j)\n\t\t\tif(dp[x ^ 1][j])\n\t\t\t\tfor(int k = 0 ; k < 1 << N ; ++k)\n\t\t\t\t\tdp[x][trans[j][k]] = (dp[x][trans[j][k]] + 1ll * dp[x ^ 1][j] * prob[k]) % MOD;\n\t}\n\tfor(int i = 0 ; i < 1 << N ; ++i) tmp.set(i);\n\tcout << dp[x][id[tmp]] << endl; return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "495776fad5dbec80fc49f132cc0cda12", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize (\"O3\")\n#pragma GCC target (\"sse4\")\n\n#include \n#include \n#include \n#include \n\nusing namespace std;\nusing namespace __gnu_pbds;\nusing namespace __gnu_cxx;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef complex cd;\n\ntypedef pair pi;\ntypedef pair pl;\ntypedef pair pd;\n\ntypedef vector vi;\ntypedef vector vd;\ntypedef vector vl;\ntypedef vector vpi;\ntypedef vector vpl;\ntypedef vector vcd;\n\ntemplate using Tree = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define F0R(i, a) for (int i = 0; i < (a); i++)\n#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)\n#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)\n#define trav(a, x) for (auto& a : x)\n\n#define mp make_pair\n#define pb push_back\n#define f first\n#define s second\n#define lb lower_bound\n#define ub upper_bound\n\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rsz resize\n\nconst int MOD = 1000000007; // 998244353\nconst ll INF = 1e18;\nconst int MX = 200005;\nconst ld PI = 4*atan((ld)1);\n\ntemplate void ckmin(T &a, T b) { a = min(a, b); }\ntemplate void ckmax(T &a, T b) { a = max(a, b); }\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nnamespace input {\n\ttemplate void re(complex& x);\n\ttemplate void re(pair& p);\n\ttemplate void re(vector& a);\n\ttemplate void re(array& a);\n\n\ttemplate void re(T& x) { cin >> x; }\n\tvoid re(double& x) { string t; re(t); x = stod(t); }\n\tvoid re(ld& x) { string t; re(t); x = stold(t); }\n\ttemplate void re(T& t, Ts&... ts) { \n\t\tre(t); re(ts...); \n\t}\n\n\ttemplate void re(complex& x) { T a,b; re(a,b); x = cd(a,b); }\n\ttemplate void re(pair& p) { re(p.f,p.s); }\n\ttemplate void re(vector& a) { F0R(i,sz(a)) re(a[i]); }\n\ttemplate void re(array& a) { F0R(i,SZ) re(a[i]); }\n}\n\nusing namespace input;\n\nnamespace output {\n\tvoid pr(int x) { cout << x; }\n\tvoid pr(long x) { cout << x; }\n\tvoid pr(ll x) { cout << x; }\n\tvoid pr(unsigned x) { cout << x; }\n\tvoid pr(unsigned long x) { cout << x; }\n\tvoid pr(unsigned long long x) { cout << x; }\n\tvoid pr(float x) { cout << x; }\n\tvoid pr(double x) { cout << x; }\n\tvoid pr(ld x) { cout << x; }\n\tvoid pr(char x) { cout << x; }\n\tvoid pr(const char* x) { cout << x; }\n\tvoid pr(const string& x) { cout << x; }\n\tvoid pr(bool x) { pr(x ? \"true\" : \"false\"); }\n\t\n\ttemplate void pr(const pair& x);\n\ttemplate void pr(const T& x);\n\t\n\ttemplate void pr(const T& t, const Ts&... ts) { \n\t\tpr(t); pr(ts...); \n\t}\n\ttemplate void pr(const pair& x) { \n\t\tpr(\"{\",x.f,\", \",x.s,\"}\"); \n\t}\n\ttemplate void pr(const T& x) { \n\t\tpr(\"{\"); // const iterator needed for vector\n\t\tbool fst = 1; for (const auto& a: x) pr(!fst?\", \":\"\",a), fst = 0; \n\t\tpr(\"}\");\n\t}\n\t\n\tvoid ps() { pr(\"\\n\"); } // print w/ spaces\n\ttemplate void ps(const T& t, const Ts&... ts) { \n\t\tpr(t); if (sizeof...(ts)) pr(\" \"); ps(ts...); \n\t}\n\t\n\tvoid pc() { pr(\"]\\n\"); } // debug w/ commas\n\ttemplate void pc(const T& t, const Ts&... ts) { \n\t\tpr(t); if (sizeof...(ts)) pr(\", \"); pc(ts...); \n\t}\n\t#define dbg(x...) pr(\"[\",#x,\"] = [\"), pc(x);\n}\n\nusing namespace output;\n\nnamespace io {\n\tvoid setIn(string s) { freopen(s.c_str(),\"r\",stdin); }\n\tvoid setOut(string s) { freopen(s.c_str(),\"w\",stdout); }\n\tvoid setIO(string s = \"\") {\n\t\tcin.sync_with_stdio(0); cin.tie(0); // fast I/O\n\t\tcin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int\n\t\tif (sz(s)) { setIn(s+\".in\"), setOut(s+\".out\"); } // for USACO\n\t}\n}\n\nusing namespace io;\n\ntemplate T invGeneral(T a, T b) {\n\ta %= b; if (a == 0) return b == 1 ? 0 : -1;\n\tT x = invGeneral(b,a); \n\treturn x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;\n}\n\ntemplate struct modular {\n\tT val; \n\texplicit operator T() const { return val; }\n\tmodular() { val = 0; }\n\tmodular(const ll& v) { \n\t\tval = (-MOD <= v && v <= MOD) ? v : v % MOD;\n\t\tif (val < 0) val += MOD;\n\t}\n\t\n\t// friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }\n\tfriend void pr(const modular& a) { pr(a.val); }\n\tfriend void re(modular& a) { ll x; re(x); a = modular(x); }\n \n\tfriend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }\n\tfriend bool operator!=(const modular& a, const modular& b) { return !(a == b); }\n\tfriend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }\n\n\tmodular operator-() const { return modular(-val); }\n\tmodular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }\n\tmodular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }\n\tmodular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }\n\tfriend modular pow(modular a, ll p) {\n\t\tmodular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;\n\t\treturn ans;\n\t}\n\tfriend modular inv(const modular& a) { \n\t\tauto i = invGeneral(a.val,MOD); assert(i != -1);\n\t\treturn i;\n\t} // equivalent to return exp(b,MOD-2) if MOD is prime\n\tmodular& operator/=(const modular& m) { return (*this) *= inv(m); }\n\t\n\tfriend modular operator+(modular a, const modular& b) { return a += b; }\n\tfriend modular operator-(modular a, const modular& b) { return a -= b; }\n\tfriend modular operator*(modular a, const modular& b) { return a *= b; }\n\t\n\tfriend modular operator/(modular a, const modular& b) { return a /= b; }\n};\n\ntypedef modular mi;\ntypedef pair pmi;\ntypedef vector vmi;\ntypedef vector vpmi;\n\ntypedef unsigned long long ul;\n\nul no[7];\nint n;\nmi p[7][7];\n\nul po(int x) {\n return (ul)1<>i)&1)) {\n\t no[i] |= po(j);\n\t // ps(\"??\",i,j);\n\t }\n\t // ps(\"H\")\n\t}\n\t// ps(no[0],no[1]);\n\tunordered_map m; m[1] = 1;\n\tF0R(i,n) {\n\t unordered_map M;\n\t trav(t,m) {\n \t F0R(j,1<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef vector vi;\ntypedef vector vl;\ntypedef vector vvi;\ntypedef vector vvl;\ntypedef vector vd;\ntypedef vector vvd;\ntypedef pair pii;\ntypedef pair pdd;\ntypedef vector vii;\ntypedef vector vs;\nconst int mod = 1e9 + 7;\n\nll mpow(ll x, ll n) {\n ll res = 1;\n while (n) {\n if (n & 1) res = res * x % mod;\n x = x * x % mod;\n n /= 2;\n }\n return res;\n}\n\nconst int DEBUG = 0;\n\nunordered_map tovi;\n\nll mhash(const vi& v) {\n ll s = 0;\n for (int x : v) {\n s = 131 * s + x + 1;\n }\n if (!tovi.count(s)) {\n tovi[s] = v;\n }\n return s;\n}\n\nint main() {\n //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n std::ios::sync_with_stdio(false); std::cin.tie(0);\n int n = 6;\n if (!DEBUG) cin >> n;\n vvl p(n, vl(n, 50));\n vi pc(1 << n);\n for (int i = 0; i < (int)pc.size(); ++i) pc[i] = __builtin_popcount(i);\n for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {\n if (!DEBUG) cin >> p[i][j];\n p[i][j] = p[i][j] * mpow(100, mod - 2) % mod;\n }\n map d;\n d[mhash(vi(1 << n))] = 1;\n ll res = 0;\n for (int i = 0; i < n; ++i) {\n map nd;\n cerr << i << ' ' << d.size() << endl;\n vl prods(1 << n);\n for (int mask = 0; mask < (1 << n); ++mask) {\n ll prod = 1;\n for (int j = 0; j < n; ++j) {\n if (mask & (1 << j)) {\n prod = prod * p[i][j] % mod;\n } else {\n prod = prod * (1 - p[i][j]) % mod;\n }\n }\n prods[mask] = prod;\n }\n for (const auto& it : d) {\n vi v = tovi[it.first];\n for (int mask = 0; mask < (1 << n); ++mask) {\n vi nv = v;\n bool fail = 0;\n for (int nmask = mask; nmask < (1 << n); nmask = (nmask + 1) | mask) {\n nv[nmask]++;\n if (nv[nmask] > pc[nmask]) {\n fail = 1;\n break;\n }\n }\n if (fail) continue;\n for (int j = 0; j < (int)nv.size(); ++j) {\n if (nv[j] + n - 1 - i <= pc[j]) nv[j] = 0;\n }\n ll h = mhash(nv);\n nd[h] = (nd[h] + prods[mask] * it.second) % mod;\n }\n }\n d.swap(nd);\n }\n for (auto& it : d) {\n res = (res + it.second) % mod;\n }\n cout << (res % mod + mod) % mod << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "268e80f4d0e51ec0a1c4f2112607b60d", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/**\n * June 2012 Codechef Long Contest\n *\n * Problem: MATCH\n * Author: Anton Lunyov (Tester)\n * Complexity: O(S * 2^N * N + M * 2^N * S), where S defined below\n * Timing: 0.03 out of 3\n *\n * Description:\n * Recall that we have two sets of vertexes U with |U|=n<=6\n * and V that can be quite large. We call subsets of vertexes in U by masks.\n * We scan vertexes in V one by one. At each moment we should know\n * what probability has each possible graph. But since we care only about\n * value of maximum matching we unite all graphs in several groups.\n * Namely the only thing that we should know is what masks satisfy Hall lemma\n * in the current graph. Mask T satisfies Hall lemma if\n * for each submask S of T we have |ADJ(S)|>=|S|\n * where ADJ(S) is the set of vertexes from V of the graph\n * that are adjacent to some vertex of S. We store this set of masks\n * as a \"supermask\" of length 2^n (since n<=6 we can use 64bit integer type).\n * Such supermasks will be our DP states.\n * Surprisingly but there is relatively small number of such states.\n */\n\n#include \n#include \nusing namespace std;\n\ntypedef unsigned long long ULL;\n//typedef __uint128_t u128;\n//typedef __uint128_t u128;\n#define next nxttt\n#define prev prv\ntypedef bitset<128> u128;\ntypedef long long ll;\nconst ll mod=1000000007;\nu128 zero;\n\nll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}\n\ninline int bit(int n) {\n return 1 << n;\n}\n\ninline u128 bitu128(int n) {\n\tu128 p=zero; p[n]=1;\n\treturn p;\n}\n\nconst int maxN = 7; // this solution is so fast that can handle n=6 easily\nconst int max2N = 1 << maxN;\nconst int maxM = 100;\n\nconst int maxS = 64184; // the number of DP states for n=6\n\n// sizes[i] = NextPrime[2 * len[i]]\n// where len[i] is the number of DP states for n=i\n// It is used as the size of hash table when n=i\nconst int sizes[maxN + 1] = {0, 5, 11, 37, 137, 821, 7529, 164184};\nconst int maxSZ = 164184; // the maximum size for hash table\n\n// \"bfs\" is the queue of DP states traversed in some convenient order\n// but it is not real queue; we do not erase old states\n// \"len\" is the current number of states in \"bfs\"\n// After precalc \"len\" is equal to the total number of DP states\nint len;\nu128 bfs[maxS];\n\n// hash table for efficient finding whether the DP state is new\nint SZ; // size of hash table\n\n// \"what[i]\" is some DP state; they are stored in quite random order here\nu128 what[maxSZ];\n\n// bfs[ind[i]] = what[i]\n// So \"ind[i]\" is the index of DP state \"what[i]\" in \"bfs\" array\nint ind[maxSZ];\n\n// return the index of DP state \"state\" in the array \"bfs\"\n// if this state is new then insert it to the hash table and to the array \"bfs\"\nint get_ind(u128 state) {\n // position where we try to insert \"state\"\n int pos=0;\n for (int i=0;i<128;i++) {\n \tpos=(pos*2+state[i])%SZ;\n }\n //int pos = state % SZ;\n while (true) {\n // if \"state\" has already been before we break\n if (what[pos] == state) {\n break;\n }\n if (what[pos] == 0) {\n what[pos] = state;\n bfs[len] = state;\n ind[pos] = len;\n ++len;\n return len-1;\n }\n \n ++pos;\n if (pos==SZ) {\n pos = 0;\n }\n }\n // in the end \"what[pos]\" is always equal to \"state\"\n // so \"ind[pos]\" is correct index of \"state\" in \"bfs\"\n return ind[pos];\n}\n\n// next[mask][k] is the index of DP state (in \"bfs\" array)\n// that we obtain from the DP state \"bfs[k]\"\n// when we add a new vertex v from V such that ADJ(v) = mask to the graph\nint next[max2N][maxS];\n\n// match[k] is the size of maximum matching of DP state \"bfs[k]\"\nint match[maxS];\n\nvoid precalc(int n) {\n int N = bit(n);\n\n u128 fixed_size[maxN + 1] = {0};\n for (int mask = 0; mask < N; ++mask)\n {\n int cnt = 0;\n for (int i = 0; i < n; ++i) {\n if (mask & bit(i)) { \n cnt++;\n }\n }\n // \"cnt\" is now the number of \n fixed_size[cnt] |= bitu128(mask);\n }\n\n // initialization of hash table and \"bfs\" array\n SZ = sizes[n];\n for (int k = 0; k < SZ; ++k) {\n what[k] = 0;\n }\n len=0;\n\n // state = 1 represents empty graph\n get_ind(1);\n\n for (int k = 0; k < len; ++k) {\n // our current state\n u128 state = bfs[k];\n\n // pred_hall[i] is the supermask of masks\n // that will satisfy Hall lemma condition if we add edge (U[i],v)\n // to the graph with the given DP state\n // (with possible exclusion of some masks from the current \"state\")\n // One can see that pred_hall[i] can be obtained\n // by including vertex \"i\" to each \"mask\" of the \"state\"\n u128 pred_hall[maxN] = {0};\n for (int mask = 0; mask < N; ++mask) {\n if ((state & bitu128(mask)).any()) {\n for (int i = 0; i < n; ++i) {\n pred_hall[i] |= bitu128(mask | bit(i));\n }\n }\n }\n\n // looping through all possible masks\n for (int mask = 0; mask < N; ++mask) {\n // \"nxt\" is the DP state that we obtain\n // after adding edges (U[i],v) for all \"i\" in \"mask\"\n // to the graph that has DP state \"state\"\n // One can see that this state is the union of all states that\n // will be obtained by adding each of the edge separately\n u128 nxt = state;\n for (int i = 0; i < n; ++i) {\n if (mask & bit(i)) {\n nxt |= pred_hall[i];\n }\n }\n\n // we call \"get_ind\" that make all needed things with \"nxt\":\n // check whether it is the new DP state, add it to the \"bfs\" if yes\n // and return its index in \"bfs\" in both cases\n next[mask][k] = get_ind(nxt);\n }\n for (int i = n; i >= 0; --i) {\n if ((state & fixed_size[i]).any()) {\n match[k]=i;\n break;\n }\n }\n }\n // printf(\"%d\\n\",len);\n}\n\nll p[maxN][maxM]; // probability adjacent matrix\nll prev[maxS]; // previous row of DP values\nll cur[maxS]; // current row of DP values\n\nll expectedMaximumMatching(int n, int m) {\n // initialization of \"prev\"\n prev[0]=1;\n for (int k=1;k\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate\nusing Table = vector>;\nconst ld eps=1e-9;\n\t\n#define WHATS(var)cout<<__LINE__<<' '<<#var<<\"=\"< ostream& operator <<(ostream &os, const pair v){\n\tos << \"( \" << v.first << \", \" << v.second << \")\"; return os;\n}\ntemplate ostream& operator <<(ostream &os, const vector &v){\n\tfor(int i = 0; i < v.size(); i++){if(i > 0){os << \" \";} os << v[i];} return os;\n}\ntemplate ostream& operator <<(ostream &os, const vector> &v){\n\tfor(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;\n}\ntemplate ostream& operator <<(ostream &os, const vector> &v){\n\tfor(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;\n}\ntemplate ostream& operator <<(ostream &os, const set &v){\n\tint i=0;\n\tfor(auto it:v){\n\t\tif(i > 0){os << ' ';}\n\t\tos << it;\n\t\ti++;\n\t} \n\treturn os;\n}\nusing ll=long long ;\n\n\n\nconst int mod = 1000000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n#define MAX_MOD_N 1024000\n\nMod fact[MAX_MOD_N], factinv[MAX_MOD_N];\nvoid init(const int amax = MAX_MOD_N) {\n\tfact[0] = Mod(1); factinv[0] = 1;\n\tfor (int i = 0; i < amax - 1; ++i) {\n\t\tfact[i + 1] = fact[i] * Mod(i + 1);\n\t\tfactinv[i + 1] = factinv[i] / Mod(i + 1);\n\t}\n}\nMod comb(const int a, const int b) {\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmapmp;\nvoid init(vector>&pers){\n\tvectorv(3,0);\n\tfor(int i=0;i<3;++i)v.push_back(1);\n\tint id=0;\n\tdo{\n\t\tint tt=0;\n\t\tfor(int k=0;k<6;++k){\n\t\t\tif(v[k])tt^=(1<>N;\n\tint s=3-(N+1)/2;\n\tfor(int i=0;i>p;\n\t\t\tpers[i][j]=Mod(p)/Mod(100);\n\t\t}\n\t}\n\tfor(int i=s+N;i<6;++i)pers[i][i]=Mod(1);\n}\nvector solve(const vector>&pers,int al=0){\n\tvectorl_nums(1<<20);\n\tfor(int xx=0;xx<(1<<18);++xx){\n\t\tvector>haves(3,vector(6));\n\t\tMod per=1;\n\t\tfor(int i=0;i<3;++i){\n\t\t\tfor(int j=0;j<6;++j){\n\t\t\t\tint k=i*6+j;\n\t\t\t\tif(xx&(1<>pers(6,vector(6));\n\t\n\tinit(pers);\n\t//cout<<3<\n#include \n#include \n\n#define pb push_back\n#define SZ(x) ((int)(x).size())\n#define ALL(x) x.begin(),x.end()\n#define all(x) x.begin(),x.end()\n#define fi first\n#define se second\n#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));\n\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate\nusing ordered_set = tree<\nT,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>;\n\n//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)\n//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k\n\ntypedef long long LL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector VPII;\ntypedef vector VPLL;\ntypedef vector VLL;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VC;\ntypedef long double LD;\ntypedef pair PLD;\ntypedef vector VLD;\ntypedef vector VPLD;\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<\" = \"< ziom = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};\n int mask = 0;\n for(auto s : ziom)\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n {\n if(elo[i][s[0] + f * 3] and elo[j][s[1] + f * 3] and elo[k][s[2] + f * 3])\n {\n mask |= maska[(f ? 126 : 0) ^ ((1<>n;\n for(int i = 1;i <= n;i++)\n for(int j = 1;j <= n;j++)\n cin>>tab[i][j];\n int xd = 0;\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n maska[(1<\n\nusing namespace std;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }\ntemplate using V = vector;\ntemplate using VV = V>;\n\n#ifdef LOCAL\nstruct PrettyOS {\n ostream& os;\n bool first;\n template auto operator<<(T&& x) {\n if (!first) os << \", \";\n first = false;\n os << x;\n return *this;\n }\n};\ntemplate void dbg0(T&&... t) {\n (PrettyOS{cerr, true} << ... << t);\n}\n#define dbg(...) \\\n do { \\\n cerr << __LINE__ << \" : \" << #__VA_ARGS__ << \" = \"; \\\n dbg0(__VA_ARGS__); \\\n cerr << endl; \\\n } while (false);\n#else\n#define dbg(...)\n#endif\n\ntemplate \nostream& operator<<(ostream& os, const pair& p) {\n return os << \"P(\" << p.first << \", \" << p.second << \")\";\n}\n\ntemplate ostream& operator<<(ostream& os, const V& v) {\n os << \"[\";\n for (auto d : v) os << d << \", \";\n return os << \"]\";\n}\n\ntemplate struct ModInt {\n using M = ModInt;\n const static M G;\n uint v;\n ModInt(ll _v = 0) { set_v(_v % MD + MD); }\n M& set_v(uint _v) {\n v = (_v < MD) ? _v : _v - MD;\n return *this;\n }\n explicit operator bool() const { return v != 0; }\n M operator-() const { return M() - *this; }\n M operator+(const M& r) const { return M().set_v(v + r.v); }\n M operator-(const M& r) const { return M().set_v(v + MD - r.v); }\n M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); }\n M operator/(const M& r) const { return *this * r.inv(); }\n M& operator+=(const M& r) { return *this = *this + r; }\n M& operator-=(const M& r) { return *this = *this - r; }\n M& operator*=(const M& r) { return *this = *this * r; }\n M& operator/=(const M& r) { return *this = *this / r; }\n bool operator==(const M& r) const { return v == r.v; }\n M pow(ll n) const {\n M x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n M inv() const { return pow(MD - 2); }\n friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }\n};\nusing Mint = ModInt;\n// template<> const Mint Mint::G = Mint(3);\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n\n int n;\n cin >> n;\n\n VV mat(n, V(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n int x;\n cin >> x;\n //x = 1;\n mat[i][j] = Mint(x) / Mint(100);\n }\n }\n\n using ST = pair, Mint>;\n V sts;\n sts.push_back(ST({0}, Mint(1)));\n\n for (int ph = 0; ph < n; ph++) {\n V nsts;\n for (int f = 1; f < (1 << n); f++) {\n Mint freq = 1;\n for (int i = 0; i < n; i++) {\n if ((f >> i) & 1) {\n freq *= mat[ph][i];\n } else {\n freq *= Mint(1) - mat[ph][i];\n }\n }\n for (auto st: sts) {\n V nst;\n for (auto d: st.first) {\n for (int i = 0; i < n; i++) {\n if ((d >> i) & 1) continue;\n if (!((f >> i) & 1)) continue;\n nst.push_back(d ^ (1 << i));\n }\n }\n if (nst.empty()) continue;\n sort(nst.begin(), nst.end());\n nst.erase(unique(nst.begin(), nst.end()), nst.end());\n nsts.push_back({nst, st.second * freq});\n }\n }\n sort(nsts.begin(), nsts.end(), [&](const ST& l, const ST& r) {\n return l.first < r.first;\n });\n\n sts.clear();\n for (auto st: nsts) {\n if (sts.size() && sts.back().first == st.first) {\n sts.back().second += st.second;\n } else {\n sts.push_back(st);\n }\n }\n dbg(ph, nsts.size());\n //dbg(nst);\n }\n\n dbg(sts);\n\n Mint ans = (sts.empty() ? Mint(0) : sts[0].second);\n\n cout << ans << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "5613181d1fb0e92b631d7570e6939663", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define REP(i,s,t) for(int i=s;i<=t;i++)\nusing namespace std;\nconst int Mod=1e9+7;\nint power(int b,int e){\n\tif(!e) return 1;\n\tint tmp=power(b,e>>1);\n\ttmp=1ll*tmp*tmp%Mod;\n\tif(e&1) tmp=1ll*tmp*b%Mod;\n\treturn tmp; \n}\nvector com;\nint p[10][10],f[1<<20],g[1<<20];\nint main(){\n\tint n; cin>>n;\n\tREP(i,0,n-1) REP(j,0,n-1){\n\t\tint per; cin>>per;\n\t\tp[i][j]=1ll*per*power(100,Mod-2)%Mod;\n\t}\n\tREP(i,n,5) p[i][i]=1;\n\tREP(i,0,63){\n\t\tint cnt=0;\n\t\tREP(j,0,5) if(i&1<=0;j--) if(j&1<\nusing namespace std;\n\n#if DEBUG\n// basic debugging macros\nint __i__,__j__;\n#define printLine(l) for(__i__=0;__i__\n _Debug& operator,(T val) {\n cout << val << endl;\n return *this;\n }\n};\n#define debug _Debug(),\n#else\n#define printLine(l)\n#define printLine2(l,c)\n#define printVar(n)\n#define printArr(a,l)\n#define print2dArr(a,r,c)\n#define print2dArr2(a,r,c,l)\n#define debug\n#endif\n\n// define\n#define MAX_VAL 999999999\n#define MAX_VAL_2 999999999999999999LL\n#define EPS 1e-6\n#define mp make_pair\n#define pb push_back\n\n// typedef\ntypedef unsigned int UI;\ntypedef long long int LLI;\ntypedef unsigned long long int ULLI;\ntypedef unsigned short int US;\ntypedef pair pii;\ntypedef pair plli;\ntypedef vector vi;\ntypedef vector vlli;\ntypedef vector vpii;\ntypedef vector vplli;\n\n// ---------- END OF TEMPLATE ----------\n#define MOD 1000000007\n\nint inv(int n) {\n int e = MOD-2,r = 1;\n while (e > 0) {\n if (e & 1) r = ((LLI) r*n) % MOD;\n e >>= 1;\n n = ((LLI) n*n) % MOD;\n }\n return r;\n}\nvi masks,masks2;\nint prob[1 << 20],prob2[1 << 20];\nint P[6][6];\nint main() {\n int i,j;\n int n;\n scanf(\"%d\",&n);\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) scanf(\"%d\",&P[i][j]);\n }\n\n int k,h = (n+1)/2;\n int h2 = n-h;\n int inv100 = inv(100);\n for (i = 0; i < (1 << n); i++) {\n int c = 0;\n for (j = 0; j < n; j++) {\n if (i & (1 << j)) c++;\n }\n if (c == h) masks.pb(i);\n if (c == h2) masks2.pb(i);\n }\n for (i = 0; i < (1 << (n*h)); i++) {\n LLI p = 1;\n for (j = 0; j < n*h; j++) {\n int x = j/n,y = j % n;\n if (i & (1 << j)) p *= ((LLI) P[x][y]*inv100) % MOD,p %= MOD;\n else p *= ((LLI) (100-P[x][y])*inv100) % MOD,p %= MOD;\n }\n int poss = 0;\n for (j = 0; j < masks.size(); j++) {\n vi v;\n for (k = 0; k < n; k++) {\n if (masks[j] & (1 << k)) v.pb(k);\n }\n do {\n for (k = 0; k < v.size(); k++) {\n int a = k*n+v[k];\n if (!(i & (1 << a))) break;\n }\n if (k == v.size()) {\n poss |= (1 << j);\n break;\n }\n } while (next_permutation(v.begin(),v.end()));\n }\n prob[poss] += p,prob[poss] %= MOD;\n }\n for (i = 0; i < (1 << (n*h2)); i++) {\n LLI p = 1;\n for (j = 0; j < n*h2; j++) {\n int x = j/n+h,y = j % n;\n if (i & (1 << j)) p *= ((LLI) P[x][y]*inv100) % MOD,p %= MOD;\n else p *= ((LLI) (100-P[x][y])*inv100) % MOD,p %= MOD;\n }\n int poss = 0;\n for (j = 0; j < masks.size(); j++) {\n vi v;\n for (k = 0; k < n; k++) {\n if (!(masks[j] & (1 << k))) v.pb(k);\n }\n do {\n for (k = 0; k < v.size(); k++) {\n int a = k*n+v[k];\n if (!(i & (1 << a))) break;\n }\n if (k == v.size()) {\n poss |= (1 << j);\n break;\n }\n } while (next_permutation(v.begin(),v.end()));\n }\n prob2[poss] += p,prob2[poss] %= MOD;\n }\n LLI ans = 0;\n for (i = 0; i < masks.size(); i++) {\n for (j = 0; j < (1 << masks.size()); j++) {\n if (j & (1 << i)) prob2[j] += prob2[j^(1 << i)],prob2[j] %= MOD;\n }\n }\n for (i = 0; i < (1 << masks.size()); i++) ans += (LLI) prob[i]*prob2[((1 << masks.size())-1)^i],ans %= MOD;\n printf(\"%I64d\\n\",(MOD+1-ans) % MOD);\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "3ccebafd5abeb3b04cd90bc726489e9f", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// while (clock()<=69*CLOCKS_PER_SEC)\n// #pragma comment(linker, \"/stack:200000000\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"Ofast\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include\n#include \n#include \n\n#define pb push_back\n#define SZ(x) ((int)(x).size())\n#define ALL(x) x.begin(),x.end()\n#define all(x) x.begin(),x.end()\n#define fi first\n#define se second\n#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));\n\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate\nusing ordered_set = tree<\nT,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>;\n\n//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)\n//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k\n\ntypedef long long LL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector VPII;\ntypedef vector VPLL;\ntypedef vector VLL;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VC;\ntypedef long double LD;\ntypedef pair PLD;\ntypedef vector VLD;\ntypedef vector VPLD;\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<\" = \"< ziom = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}};\n int mask = 0;\n for(auto s : ziom)\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n {\n if(elo[i][s[0] + f * 3] and elo[j][s[1] + f * 3] and elo[k][s[2] + f * 3])\n {\n mask |= maska[(f ? 126 : 0) ^ ((1<>n;\n for(int i = 1;i <= n;i++)\n for(int j = 1;j <= n;j++)\n cin>>tab[i][j];\n int xd = 0;\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n maska[(1<\nusing namespace std;\nconst int mod=1e9+7;\nconst int N=(1<<20)+5;\nint n,lim,ans,p1[N],p2[N];\nint flag[10][10],a[10][10],h[10][10][10];\nint fpow(int x,int y) {\n\tint res=1;\n\twhile (y) {\n\t\tif (y&1) res=1ll*res*x%mod;\n\t\tx=1ll*x*x%mod,y>>=1;\n\t}\n\treturn res;\n}\nint inv(int x) { return fpow(x,mod-2); }\nbool check(int x,int y,int z) {\n\tint a[3]={x,y,z},d[3]={0,1,2};\n\tdo {\n\t\tif (flag[a[d[0]]][1] && flag[a[d[1]]][2] && flag[a[d[2]]][3]) return 1;\n\t} while (next_permutation(d,d+3));\n\treturn 0;\n}\n\nvoid dfs1(int P,int x,int y) {\n\tif (x==7 && y==1) {\n\t\tint sta=0;\n\t\tfor (int i=1; i<=6; i++)\n\t\t\tfor (int j=i+1; j<=6; j++)\n\t\t\t\tfor (int k=j+1; k<=6; k++) \n\t\t\t\t\tif (check(i,j,k)) {\n\t\t\t\t\t\tint x=1;\n\t\t\t while (x==i || x==j || x==k) x++;\n\t\t\t int y=x+1;\n\t\t\t while (y==i || y==j || y==k) y++;\n\t\t\t int z=y+1;\n\t\t\t while (z==i || z==j || z==k) z++;\n\t\t\t sta|=h[x][y][z];\n\t\t\t\t\t}\n\t\tp1[sta]=(p1[sta]+P)%mod;\n//\t\tif (P!=0) printf(\"!!!!! %d %d\\n\",sta,p1[sta],P);\n\t\treturn;\n\t}\n\tflag[x][y]=1;\n\tif (y==3) dfs1(1ll*P*a[x][y]%mod,x+1,1);\n\telse dfs1(1ll*P*a[x][y]%mod,x,y+1);\n\tflag[x][y]=0;\n\tif (y==3) dfs1(1ll*P*(mod+1-a[x][y])%mod,x+1,1);\n\telse dfs1(1ll*P*(mod+1-a[x][y])%mod,x,y+1);\n}\n\nvoid dfs2(int P,int x,int y) {\n\tif (x==7 && y==4) {\n\t\tint sta=0;\n\t\tfor (int i=1; i<=6; i++)\n\t\t\tfor (int j=i+1; j<=6; j++)\n\t\t\t\tfor (int k=j+1; k<=6; k++) \n\t\t\t\t\tif (check(i,j,k)) sta|=h[i][j][k];\n\t\tp2[sta]=(p2[sta]+P)%mod;\n//\t\tif (P!=0) printf(\"!!!! %d %d %d\\n\",sta,p2[sta],P);\n\t\tans=(ans+1ll*p1[lim^sta]*P%mod)%mod;\n\t\treturn;\n\t}\n\tflag[x][y-3]=1;\n\tif (y==6) dfs2(1ll*P*a[x][y]%mod,x+1,4);\n\telse dfs2(1ll*P*a[x][y]%mod,x,y+1);\n\tflag[x][y-3]=0;\n\tif (y==6) dfs2(1ll*P*(mod+1-a[x][y])%mod,x+1,4);\n\telse dfs2(1ll*P*(mod+1-a[x][y])%mod,x,y+1);\n}\n\t\nint main() {\n\tscanf(\"%d\",&n);\n\tfor (int i=1; i<=n; i++)\n\t\tfor (int j=1; j<=n; j++)\n\t\t\tscanf(\"%d\",&a[i][j]),a[i][j]=1ll*a[i][j]*inv(100)%mod;\n\tfor (int i=n+1; i<=6; i++) a[i][i]=1;\n\tint tot=0;\n for (int i=1; i<=6; i++)\n\t\t\tfor (int j=i+1; j<=6; j++)\n\t\t\t\tfor (int k=j+1; k<=6; k++) \n\t\t\t\t\th[i][j][k]=1<>i&1) p1[j]=(p1[j]+p1[j^1<\n#include \n#include \n\n#define pb push_back\n#define SZ(x) ((int)(x).size())\n#define ALL(x) x.begin(),x.end()\n#define all(x) x.begin(),x.end()\n#define fi first\n#define se second\n#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));\n\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate\nusing ordered_set = tree<\nT,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>;\n\n//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)\n//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k\n\ntypedef long long LL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector VPII;\ntypedef vector VPLL;\ntypedef vector VLL;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VC;\ntypedef long double LD;\ntypedef pair PLD;\ntypedef vector VLD;\ntypedef vector VPLD;\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<\" = \"<>n;\n for(int i = 1;i <= n;i++)\n for(int j = 1;j <= n;j++)\n cin>>tab[i][j];\n int xd = 0;\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n maska[(1<\n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n\nstruct mod_int {\n int val;\n\n mod_int(long long v = 0) {\n if (v < 0)\n v = v % MOD + MOD;\n\n if (v >= MOD)\n v %= MOD;\n\n val = v;\n }\n\n static int mod_inv(int a, int m = MOD) {\n // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example\n int g = m, r = a, x = 0, y = 1;\n\n while (r != 0) {\n int q = g / r;\n g %= r; swap(g, r);\n x -= q * y; swap(x, y);\n }\n\n return x < 0 ? x + m : x;\n }\n\n explicit operator int() const {\n return val;\n }\n\n mod_int& operator+=(const mod_int &other) {\n val += other.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n\n mod_int& operator-=(const mod_int &other) {\n val -= other.val;\n if (val < 0) val += MOD;\n return *this;\n }\n\n static unsigned fast_mod(uint64_t x, unsigned m = MOD) {\n #if !defined(_WIN32) || defined(_WIN64)\n return x % m;\n #endif\n // Optimized mod for Codeforces 32-bit machines.\n // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.\n unsigned x_high = x >> 32, x_low = (unsigned) x;\n unsigned quot, rem;\n asm(\"divl %4\\n\"\n : \"=a\" (quot), \"=d\" (rem)\n : \"d\" (x_high), \"a\" (x_low), \"r\" (m));\n return rem;\n }\n\n mod_int& operator*=(const mod_int &other) {\n val = fast_mod((uint64_t) val * other.val);\n return *this;\n }\n\n mod_int& operator/=(const mod_int &other) {\n return *this *= other.inv();\n }\n\n friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; }\n friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; }\n friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; }\n friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; }\n\n mod_int& operator++() {\n val = val == MOD - 1 ? 0 : val + 1;\n return *this;\n }\n\n mod_int& operator--() {\n val = val == 0 ? MOD - 1 : val - 1;\n return *this;\n }\n\n mod_int operator++(int) { mod_int before = *this; ++*this; return before; }\n mod_int operator--(int) { mod_int before = *this; --*this; return before; }\n\n mod_int operator-() const {\n return val == 0 ? 0 : MOD - val;\n }\n\n bool operator==(const mod_int &other) const { return val == other.val; }\n bool operator!=(const mod_int &other) const { return val != other.val; }\n\n mod_int inv() const {\n return mod_inv(val);\n }\n\n mod_int pow(long long p) const {\n assert(p >= 0);\n mod_int a = *this, result = 1;\n\n while (p > 0) {\n if (p & 1)\n result *= a;\n\n a *= a;\n p >>= 1;\n }\n\n return result;\n }\n};\n\n\nconst int N_MAX = 7;\n\nint N;\nmod_int probability[N_MAX][N_MAX];\n\nint main() {\n cin >> N;\n\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++) {\n int p;\n cin >> p;\n probability[i][j] = mod_int(p) / mod_int(100);\n }\n\n map, mod_int> possible_masks;\n vector starting(1 << N, false);\n starting[0] = true;\n possible_masks[starting] = 1;\n vector new_possible;\n\n for (int left = 0; left < N; left++) {\n map, mod_int> new_possible_masks;\n\n for (int current = 0; current < 1 << N; current++) {\n mod_int product = 1;\n\n for (int right = 0; right < N; right++)\n product *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right];\n\n for (auto &p : possible_masks) {\n const vector &possible = p.first;\n mod_int prob = p.second;\n new_possible.assign(1 << N, false);\n\n for (int mask = 0; mask < 1 << N; mask++)\n if (possible[mask])\n for (int i = 0; i < N; i++)\n if ((current >> i & 1) != 0 && (mask >> i & 1) == 0)\n new_possible[mask | 1 << i] = true;\n\n new_possible_masks[new_possible] += prob * product;\n }\n }\n\n possible_masks = new_possible_masks;\n cerr << possible_masks.size() << endl;\n }\n\n mod_int answer = 0;\n\n for (auto &p : possible_masks)\n if (p.first[(1 << N) - 1])\n answer += p.second;\n\n cout << (int) answer << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "5a740493fa6bba5d3a6f8bd09cc3e82d", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n\nstruct mod_int {\n int val;\n\n mod_int(long long v = 0) {\n if (v < 0)\n v = v % MOD + MOD;\n\n if (v >= MOD)\n v %= MOD;\n\n val = v;\n }\n\n static int mod_inv(int a, int m = MOD) {\n // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example\n int g = m, r = a, x = 0, y = 1;\n\n while (r != 0) {\n int q = g / r;\n g %= r; swap(g, r);\n x -= q * y; swap(x, y);\n }\n\n return x < 0 ? x + m : x;\n }\n\n explicit operator int() const {\n return val;\n }\n\n mod_int& operator+=(const mod_int &other) {\n val += other.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n\n mod_int& operator-=(const mod_int &other) {\n val -= other.val;\n if (val < 0) val += MOD;\n return *this;\n }\n\n static unsigned fast_mod(uint64_t x, unsigned m = MOD) {\n #if !defined(_WIN32) || defined(_WIN64)\n return x % m;\n #endif\n // Optimized mod for Codeforces 32-bit machines.\n // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.\n unsigned x_high = x >> 32, x_low = (unsigned) x;\n unsigned quot, rem;\n asm(\"divl %4\\n\"\n : \"=a\" (quot), \"=d\" (rem)\n : \"d\" (x_high), \"a\" (x_low), \"r\" (m));\n return rem;\n }\n\n mod_int& operator*=(const mod_int &other) {\n val = fast_mod((uint64_t) val * other.val);\n return *this;\n }\n\n mod_int& operator/=(const mod_int &other) {\n return *this *= other.inv();\n }\n\n friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; }\n friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; }\n friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; }\n friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; }\n\n mod_int& operator++() {\n val = val == MOD - 1 ? 0 : val + 1;\n return *this;\n }\n\n mod_int& operator--() {\n val = val == 0 ? MOD - 1 : val - 1;\n return *this;\n }\n\n mod_int operator++(int) { mod_int before = *this; ++*this; return before; }\n mod_int operator--(int) { mod_int before = *this; --*this; return before; }\n\n mod_int operator-() const {\n return val == 0 ? 0 : MOD - val;\n }\n\n bool operator==(const mod_int &other) const { return val == other.val; }\n bool operator!=(const mod_int &other) const { return val != other.val; }\n\n mod_int inv() const {\n return mod_inv(val);\n }\n\n mod_int pow(long long p) const {\n assert(p >= 0);\n mod_int a = *this, result = 1;\n\n while (p > 0) {\n if (p & 1)\n result *= a;\n\n a *= a;\n p >>= 1;\n }\n\n return result;\n }\n};\n\n\nconst int N_MAX = 7;\n\nint N;\nmod_int probability[N_MAX][N_MAX];\n\nint main() {\n cin >> N;\n\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++) {\n int p;\n cin >> p;\n probability[i][j] = mod_int(p) / mod_int(100);\n }\n\n vector> masks_with_count(N_MAX + 1);\n vector lookup(1 << N, -1);\n\n for (int mask = 0; mask < 1 << N; mask++) {\n int count = __builtin_popcount(mask);\n lookup[mask] = masks_with_count[count].size();\n masks_with_count[count].push_back(mask);\n }\n\n map possible_masks;\n possible_masks[1 << lookup[0]] = 1;\n\n for (int left = 0; left < N; left++) {\n map new_possible_masks;\n\n for (int current = 0; current < 1 << N; current++) {\n mod_int product = 1;\n\n for (int right = 0; right < N; right++)\n product *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right];\n\n for (auto &p : possible_masks) {\n long long possible = p.first;\n mod_int prob = p.second;\n\n long long new_possible = 0;\n\n for (int mask : masks_with_count[left])\n if (possible >> lookup[mask] & 1)\n for (int i = 0; i < N; i++)\n if ((current >> i & 1) != 0 && (mask >> i & 1) == 0)\n new_possible |= 1LL << lookup[mask | 1 << i];\n\n new_possible_masks[new_possible] += prob * product;\n }\n }\n\n possible_masks = new_possible_masks;\n cerr << possible_masks.size() << endl;\n }\n\n mod_int answer = 0;\n\n for (auto &p : possible_masks)\n if (p.first >> lookup[(1 << N) - 1] & 1)\n answer += p.second;\n\n cout << (int) answer << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "31f016b2fc73a3357dc7c8ca77b365b7", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nusing LL = int64_t;\nconstexpr int MOD = 1E9 + 7;\nint power(int a, int b) {\n int ans = 1;\n while (b > 0) {\n if (b & 1)\n ans = LL(ans) * a % MOD;\n a = LL(a) * a % MOD;\n b >>= 1;\n }\n return ans;\n}\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n;\n cin >> n;\n vector> p(n);\n for (auto &i : p) {\n i.resize(n);\n for (int &j : i) {\n cin >> j;\n j = LL(j) * power(100, MOD - 2) % MOD;\n }\n }\n vector> id(n + 1);\n vector> subsets(n + 1);\n for (int sz = 0; sz <= n; ++sz) {\n id[sz].resize(1 << n);\n for (int s = 0; s < (1 << n); ++s)\n if (__builtin_popcount(s) == sz) {\n id[sz][s] = subsets[sz].size();\n subsets[sz].push_back(s);\n }\n }\n vector> families(n + 1);\n families[0][1] = 1;\n for (int i = 1; i <= n; ++i) {\n for (int s = 0; s < (1 << n); ++s) {\n int pr = 1;\n for (int j = 0; j < n; ++j)\n if (s >> j & 1)\n pr = LL(pr) * p[i - 1][j] % MOD;\n else\n pr = LL(pr) * (1 + MOD - p[i - 1][j]) % MOD;\n for (auto [f, _p] : families[i - 1]) {\n LL mask = 0;\n for (int t : subsets[i])\n for (int j = 0; j < n; ++j)\n if ((t >> j & 1) && (s >> j & 1) && (f >> id[i - 1][t ^ 1 << j] & 1)) {\n mask |= 1LL << id[i][t];\n break;\n }\n families[i][mask] = (families[i][mask] + LL(_p) * pr) % MOD;\n }\n }\n }\n cout << families[n][1] << \"\\n\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "bb55b25bb641bdba350f9dba3438d810", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\nAuthor: QAQ Automaton\nLang: C++\nProg: E.cpp\nMail: lk@qaq-am.com\nBlog: https://www.qaq-am.com/\n*/\n#include\n#define int long long\n#define debug(...) fprintf(stderr,__VA_ARGS__)\n#define DEBUG printf(\"Passing [%s] in LINE %d\\n\",__FUNCTION__,__LINE__)\n#define Debug debug(\"Passing [%s] in LINE %d\\n\",__FUNCTION__,__LINE__)\n#define all(x) x.begin(),x.end()\n#define x first\n#define y second \nusing namespace std;\ntypedef long long ll;\ntypedef pair pii;\nconst double eps=1e-8;\nconst double pi=acos(-1.0);\ntemplateint chkmin(T &a,T b){return a>b?a=b,1:0;}\ntemplateint chkmax(T &a,T b){return aT sqr(T a){return a*a;}\ntemplateT mmin(T a,T b){return aT mmax(T a,T b){return a>b?a:b;}\ntemplateT aabs(T a){return a<0?-a:a;}\ntemplateint dcmp(T a,T b){return a>b;}\ntemplateint cmp_a(int x,int y){return a[x] '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;\n\t\tfor (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;\n\t\treturn 1;\n\t}\n\n\tinline bool read (long long &x) {\n\t\tfor (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;\n\t\tfor (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;\n\t\treturn 1;\n\t}\n\tinline bool read (char &x) {\n\t\tx=gc();\n\t\treturn x!=EOF;\n\t}\n\tinline bool read(char *x){\n\t\twhile((*x=gc())=='\\n' || *x==' '||*x=='\\r')if(*x==EOF)return 0;\n\t\twhile(!(*x=='\\n'||*x==' '||*x=='\\r'||*x==EOF))*(++x)=gc();\n\t\t*x=0;\n\t\treturn 1;\n\t}\n\ttemplate\n\tinline bool read(A &x,B &...y){\n\t\treturn read(x)&&read(y...);\n\t}\n\t// print a signed integer\n\tinline bool write (signed x) {\n\t\tif (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;\n\t\twhile (x) qu[++ qr] = x % 10 + '0', x /= 10;\n\t\twhile (qr) putc (qu[qr --]);\n\t\treturn 0;\n\t}\n\n\tinline bool write (long long x) {\n\t\tif (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;\n\t\twhile (x) qu[++ qr] = x % 10 + '0', x /= 10;\n\t\twhile (qr) putc (qu[qr --]);\n\t\treturn 0;\n\t}\n\tinline bool write (char x) {\n\t\tputc(x);\n\t\treturn 0;\n\t}\n\tinline bool write(const char *x){\n\t\twhile(*x){putc(*x);++x;}\n\t\treturn 0;\n\t}\n\tinline bool write(char *x){\n\t\twhile(*x){putc(*x);++x;}\n\t\treturn 0;\n\t}\n\ttemplate\n\tinline bool write(A x,B ...y){\n\t\treturn write(x)||write(y...);\n\t}\n\t//no need to call flush at the end manually!\n\tstruct Flusher_ {~Flusher_(){flush();}}io_flusher_;\n}\nusing io :: read;\nusing io :: putc;\nusing io :: write;\nint inf;\nstruct _init_{\n _init_(){\n memset(&inf,0x3f,sizeof(inf));\n }\n};\n_init_ ___INIT__;\nint a[11][11],n;\nint f[11][131];\nint is[131],i100;\nconst int p=1000000007;\nint fpm(int a,int b){int c=1;for(;b;b>>=1,a=a*a%p)if(b&1)c=c*a%p;return c;}\nstruct lll{\n\tunsigned long long a,b;\n\tvoid set(int x){\n\t\tif(x<64)a|=1ULL<a|=ano.a;this->b|=ano.b;return *this;}\n\tbool operator ==(lll ano){return b==ano.b&&a==ano.a;}\n\tbool operator [](int ano){return !!(ano<64?a&(1ULL<(const lll &a,const lll &b){return a.b>b.b||(a.b==b.b && a.a>b.a);}\nmap st[8];\nlll mx;\nint dfs(int x,lll w){\n\tif(x==n)return w==mx;\n\tif(st[x].count(w))return st[x][w];\n\tint ans=0;\n\tlll ng;\n\n\tlll s[n];\n\tfor(int j=0;j\n#include \n#include \n#include \n#include \nusing namespace std;\n\n#include \nusing namespace __gnu_pbds;\n\nauto random_address = [] { char *p = new char; delete p; return (uint64_t) p; };\n\nstruct safe_hash {\n static uint64_t splitmix64(uint64_t x) {\n // http://xorshift.di.unimi.it/splitmix64.c\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n uint64_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = splitmix64(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));\n return splitmix64(x ^ FIXED_RANDOM);\n }\n};\n\nconst int MOD = 1e9 + 7;\n\nstruct mod_int {\n int val;\n\n mod_int(long long v = 0) {\n if (v < 0)\n v = v % MOD + MOD;\n\n if (v >= MOD)\n v %= MOD;\n\n val = v;\n }\n\n static int mod_inv(int a, int m = MOD) {\n // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example\n int g = m, r = a, x = 0, y = 1;\n\n while (r != 0) {\n int q = g / r;\n g %= r; swap(g, r);\n x -= q * y; swap(x, y);\n }\n\n return x < 0 ? x + m : x;\n }\n\n explicit operator int() const {\n return val;\n }\n\n mod_int& operator+=(const mod_int &other) {\n val += other.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n\n mod_int& operator-=(const mod_int &other) {\n val -= other.val;\n if (val < 0) val += MOD;\n return *this;\n }\n\n static unsigned fast_mod(uint64_t x, unsigned m = MOD) {\n#if !defined(_WIN32) || defined(_WIN64)\n return x % m;\n#endif\n // Optimized mod for Codeforces 32-bit machines.\n // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.\n unsigned x_high = x >> 32, x_low = (unsigned) x;\n unsigned quot, rem;\n asm(\"divl %4\\n\"\n : \"=a\" (quot), \"=d\" (rem)\n : \"d\" (x_high), \"a\" (x_low), \"r\" (m));\n return rem;\n }\n\n mod_int& operator*=(const mod_int &other) {\n val = fast_mod((uint64_t) val * other.val);\n return *this;\n }\n\n mod_int& operator/=(const mod_int &other) {\n return *this *= other.inv();\n }\n\n friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; }\n friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; }\n friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; }\n friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; }\n\n mod_int& operator++() {\n val = val == MOD - 1 ? 0 : val + 1;\n return *this;\n }\n\n mod_int& operator--() {\n val = val == 0 ? MOD - 1 : val - 1;\n return *this;\n }\n\n mod_int operator++(int) { mod_int before = *this; ++*this; return before; }\n mod_int operator--(int) { mod_int before = *this; --*this; return before; }\n\n mod_int operator-() const {\n return val == 0 ? 0 : MOD - val;\n }\n\n bool operator==(const mod_int &other) const { return val == other.val; }\n bool operator!=(const mod_int &other) const { return val != other.val; }\n\n mod_int inv() const {\n return mod_inv(val);\n }\n\n mod_int pow(long long p) const {\n assert(p >= 0);\n mod_int a = *this, result = 1;\n\n while (p > 0) {\n if (p & 1)\n result *= a;\n\n a *= a;\n p >>= 1;\n }\n\n return result;\n }\n};\n\n\nconst int N_MAX = 10;\n\nint N;\nmod_int probability[N_MAX][N_MAX];\n\nint main() {\n cin >> N;\n\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++) {\n int p;\n cin >> p;\n probability[i][j] = mod_int(p) / mod_int(100);\n }\n\n vector> masks_with_count(N + 1);\n vector lookup(1 << N, -1);\n\n for (int mask = 0; mask < 1 << N; mask++) {\n int count = __builtin_popcount(mask);\n lookup[mask] = masks_with_count[count].size();\n masks_with_count[count].push_back(mask);\n }\n\n gp_hash_table possible_masks;\n possible_masks[1 << lookup[0]] = 1;\n\n for (int left = 0; left < N; left++) {\n gp_hash_table new_possible_masks;\n vector products(1 << N, 1);\n\n for (int current = 0; current < 1 << N; current++)\n for (int right = 0; right < N; right++)\n products[current] *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right];\n\n vector new_bits(N, 0);\n vector new_possible(1 << N, 0);\n\n for (auto &p : possible_masks) {\n long long possible = p.first;\n mod_int prob = p.second;\n\n new_bits.assign(N, 0);\n\n for (int mask : masks_with_count[left])\n if (possible >> lookup[mask] & 1)\n for (int i = 0; i < N; i++)\n if ((mask >> i & 1) == 0)\n new_bits[i] |= 1LL << lookup[mask | 1 << i];\n\n new_possible[0] = 0;\n\n for (int current = 1; current < 1 << N; current++) {\n new_possible[current] = new_possible[current & (current - 1)] | new_bits[__builtin_ctz(current)];\n new_possible_masks[new_possible[current]] += prob * products[current];\n }\n }\n\n possible_masks = new_possible_masks;\n cerr << possible_masks.size() << endl;\n }\n\n mod_int answer = 0;\n\n for (auto &p : possible_masks)\n if (p.first >> lookup[(1 << N) - 1] & 1)\n answer += p.second;\n\n cout << (int) answer << '\\n';\n}\n ", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "de0ed0e8e74e7424a6fe280f5fd5dde5", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\n#define f first\n#define s second\n#define pb push_back\nusing namespace std;\ntypedef long long ll;\ntypedef pair pll;\nconst int k = 1e9 + 7;\nvector v[9];\nll n, b[1 << 9], p[9], a[9], i, j, m, X, M;\nunordered_map dp[8];\nint main(){\n\tcin >> n;\n\tfor(i = 0, j; i < (1 << n); i++){\n\t\tj = __builtin_popcount(i);\n\t\tb[i] = v[j].size();\n\t\tv[j].pb(i);\n\t}\n\tdp[0][1] = 1;\t\n\tfor(i = 0; i < n; i++){\n\t\tfor(j = 0; j < n; j++){\n\t\t\tcin >> p[j];\n\t\t\tp[j] = p[j] * 570000004 % k;\n\t\t}\n\t\tfor(auto it : dp[i]){\n\t\t\tfor(j = 0; j < n; j++)\n\t\t\t\ta[j] = 0;\n\t\t\tfor(auto m : v[i])\n\t\t\t\tif((it.f >> b[m]) & 1)\n\t\t\t\t\tfor(j = 0; j < n; j++)\n\t\t\t\t\t\tif(((m >> j) & 1) == 0)\n\t\t\t\t\t\t\ta[j] |= (1ll << b[m | (1 << j)]);\n\t\t\tfor(m = 0; m < (1 << n); m++){\n\t\t\t\tX = 1, M = 0;\n\t\t\t\tfor(j = 0; j < n; j++)\n\t\t\t\t\tif((m >> j) & 1)\n\t\t\t\t\t\tM |= a[j], X = X * p[j] % k;\n\t\t\t\t\telse\n\t\t\t\t\t\tX = X * (1 - p[j] + k) % k;\t\t\t\t\t\t\t\t\t\t\n\t\t\t\tdp[i + 1][M] = (dp[i + 1][M] + it.s * X) % k;\n\t\t\t}\n\t\t}\n\t}\n\tcout << dp[n][1];\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "2ed435d8318fe5deee41ac12e1627dfe", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nusing ll = long long;\nll modinv(ll a, ll m) {\n\tassert(m > 0);\n\tif (m == 1) return 0;\n\ta %= m;\n\tif (a < 0) a += m;\n\tassert(a != 0);\n\tif (a == 1) return 1;\n\treturn m - modinv(m, a) * m / a;\n}\n\ntemplate struct modnum {\nprivate:\n\tint v;\npublic:\n\tstatic const int MOD = MOD_;\n\n\tmodnum() : v(0) {}\n\tmodnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }\n\texplicit operator int () const { return v; }\n\tfriend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }\n\tfriend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }\n\n\tmodnum operator ~ () const {\n\t\tmodnum res;\n\t\tres.v = modinv(v, MOD);\n\t\treturn res;\n\t}\n\n\tmodnum& operator += (const modnum& o) {\n\t\tv += o.v;\n\t\tif (v >= MOD) v -= MOD;\n\t\treturn *this;\n\t}\n\tmodnum& operator -= (const modnum& o) {\n\t\tv -= o.v;\n\t\tif (v < 0) v += MOD;\n\t\treturn *this;\n\t}\n\tmodnum& operator *= (const modnum& o) {\n\t\tv = int(ll(v) * ll(o.v) % MOD);\n\t\treturn *this;\n\t}\n\tmodnum& operator /= (const modnum& o) {\n\t\treturn *this *= (~o);\n\t}\n\n\tfriend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }\n\tfriend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }\n\tfriend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }\n\tfriend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }\n};\n\nusing num = modnum;\n\n\nint main(){\n\tios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n\tconst int n = 6;\n\tvector > p(n, vector(n, 0));\n\tint _n;\n\tcin >> _n;\n\tfor(int i = 0; i < n; i++) p[i][i] = 1;\n\tfor(int i = 0; i < _n; i++){\n\t\tfor(int j = 0; j < _n; j++){\n\t\t\tint a;\n\t\t\tcin >> a;\n\t\t\tp[i][j] = num(a) / num(100);\n\t\t}\n\t}\n\tconst int f = 20;\n\tvector > sets(2, vector(1 << f, 0));\n\tvector > masks(2);\n\tfor(int i = 0; i < n; i++){\n\t\tfor(int j = 0; j < i; j++){\n\t\t\tfor(int k = 0; k < j; k++){\n\t\t\t\tmasks[0].push_back((1 << i) ^ (1 << j) ^ (1 << k));\n\t\t\t\tmasks[1].push_back(((1 << n) - 1) ^ masks[0].back());\n\t\t\t}\n\t\t}\n\t}\n\tassert(f == masks[0].size());\n\tfor(int t = 0; t < 2; t++){\n\t\tfor(int e = 0; e < (1 << (n/2 * n)); e++){\n\t\t\tnum prob = 1;\n\t\t\tint edges[n/2];\n\t\t\tfor(int i = 0; i < n/2; i++) edges[i] = 0;\n\t\t\tfor(int i = 0; i < n/2 * n; i++){\n\t\t\t\tbool r = (e >> i) & 1;\n\t\t\t\tnum pp = p[(i / n) * 2 + t][i % n];\n\t\t\t\tprob *= r ? pp : (1 - pp);\n\t\t\t\tedges[i/n] |= int(r) << (i % n);\n\t\t\t}\n\t\t\tint work_mask = 0;\n\t\t\tfor(int a = 0; a < (int)masks[t].size(); a++){\n\t\t\t\tint mask = masks[t][a];\n\t\t\t\tint r[n/2];\n\t\t\t\tfor(int i = 0; i < n/2; i++) r[i] = edges[i] & mask;\n\t\t\t\tif(r[0] && r[1] && r[2]){\n\t\t\t\t\tif(__builtin_popcount(r[0] | r[1] | r[2]) == 3){\n\t\t\t\t\t\tint x = __builtin_popcount(r[0] | r[1]);\n\t\t\t\t\t\tint y = __builtin_popcount(r[0] | r[2]);\n\t\t\t\t\t\tint z = __builtin_popcount(r[1] | r[2]);\n\t\t\t\t\t\tif(x >= 2 && y >= 2 && z >= 2){\n\t\t\t\t\t\t\twork_mask |= (1 << a);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tsets[t][work_mask] += prob;\n\t\t}\n\t}\n\tfor(int i = 0; i < f; i++){\n\t\tfor(int j = 0; j < (1 << f); j++){\n\t\t\tif((j >> i) & 1) continue;\n\t\t\tsets[0][j ^ (1 << i)] += sets[0][j];\n\t\t}\n\t}\n\tnum ans = 0;\n\tfor(int j = 0; j < (1 << f); j++){\n\t\tans += sets[0][j] * sets[1][((1 << f) - 1) ^ j];\n\t}\n\tans = 1 - ans;\n\tcout << (int)ans << '\\n';\n}", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "63655d36d98c4f6a04a8c6b2eb648a81", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nconst int mod=1000000007;\ninline int add(int a,int b)\n{\n\tif((a+=b)>=mod)a-=mod;\n\treturn a;\n}\ninline int mult(int a,int b)\n{\n\tlong long t=1ll*a*b;\n\tif(t>=mod)t%=mod;\n\treturn t;\n}\ninline int dec(int a,int b)\n{\n\tif((a-=b)<0)a+=mod;\n\treturn a;\n}\ninline int power(int a,int b)\n{\n\tint out=1;\n\twhile(b)\n\t{\n\t\tif(b&1)out=mult(out,a);\n\t\ta=mult(a,a);\n\t\tb>>=1;\n\t}\n\treturn out;\n}\ninline int lowbit(int x)\n{\n\treturn x&(-x);\n}\nint n,a[10][10],ch[70010][128],idcnt=0,q[10][128],dp[10][70010];\nunordered_map,int> id;\nint dfs(bitset<128> s)\n{\n\tif(id.find(s)!=id.end())return id[s];\n\tid[s]=++idcnt;\n\tfor(int i=0;i<(1< tmp=s;\n\t\tfor(int j=0;j<(1<>k)&1)q[i][j]=mult(q[i][j],a[i][k]);\n\t\t\t\telse q[i][j]=mult(q[i][j],dec(1,a[i][k]));\n\t\t\t}\n\t\t}\n\t}\n\tdp[0][1]=1;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=idcnt;j++)\n\t\t{\n\t\t\tfor(int k=0;k<(1< bt;\n\tfor(int i=0;i<(1<\nusing namespace std;\nconst int MAXN = 7;\nconst int MOD = 1e9 + 7;\nconst int E100 = 570000004;\ntypedef long long LL;\nint n, g[MAXN][MAXN];\nLL nxt[1< S[MAXN+1];\nint id[1< dp[MAXN]; \nint solve(int i, LL msk) {\n\tif (msk == 0) return 0;\n\tif (i == n) return 1;\n\tif (dp[i].count(msk)) return dp[i][msk];\n\tLL ret = 0;\n\tfor (int s = 0; s < (1<\n \ntypedef long long LL;\n \nconst int N=10;\nconst int M=25;\nconst int S=1048580;\nconst int MOD=1000000007;\n \nint n,p[N][N],ma[N][N],bin[M],a[N],b[N],f[S],g[S],tot,id[70],pts[M][N],sta[M];\nbool vis[N];\n \nint ksm(int x,int y)\n{\n\tint ans=1;\n\twhile (y)\n\t{\n\t\tif (y&1) ans=(LL)ans*x%MOD;\n\t\tx=(LL)x*x%MOD;y>>=1;\n\t}\n\treturn ans;\n}\n \nvoid init()\n{\n\tbin[0]=1;\n\tfor (int i=1;i<=20;i++) bin[i]=bin[i-1]*2;\n\tscanf(\"%d\",&n);\n\tint inv=ksm(100,MOD-2);\n\tfor (int i=1;i<=n;i++)\n\t\tfor (int j=1;j<=n;j++)\n\t\t\tscanf(\"%d\",&p[i][j]),p[i][j]=(LL)p[i][j]*inv%MOD;\n\twhile (n<6) n++,p[n][n]=1;\n}\n \nbool get_match(int x)\n{\n\tif (x>3) return 1;\n\tfor (int i=1;i<=3;i++)\n\t\tif (!vis[i]&&ma[a[x]][b[i]])\n\t\t{\n\t\t\tvis[i]=1;\n\t\t\tbool flag=get_match(x+1);\n\t\t\tvis[i]=0;\n\t\t\tif (flag) return 1;\n\t\t}\n\treturn 0;\n}\n \nvoid dfs2(int x,int y,int pro)\n{\n\tif (x>3)\n\t{\n\t\tint s=0;\n\t\tfor (int i=1;i<=tot;i++)\n\t\t{\n\t\t\tfor (int j=1;j<=3;j++) b[j]=pts[i][j];\n\t\t\tif (get_match(1)) s+=bin[i-1];\n\t\t}\n\t\t(f[s]+=pro)%=MOD;\n\t\treturn;\n\t}\n\tma[a[x]][y]=0;\n\tif (y==n) dfs2(x+1,1,(LL)pro*(1-p[a[x]][y])%MOD);\n\telse dfs2(x,y+1,(LL)pro*(1-p[a[x]][y])%MOD);\n\tma[a[x]][y]=1;\n\tif (y==n) dfs2(x+1,1,(LL)pro*p[a[x]][y]%MOD);\n\telse dfs2(x,y+1,(LL)pro*p[a[x]][y]%MOD);\n}\n \nvoid dfs1(int x,int y,int s)\n{\n\tif (y==3)\n\t{\n\t\tid[s]=++tot;sta[tot]=s;\n\t\tfor (int i=1;i<=3;i++) pts[tot][i]=a[i];\n\t\treturn;\n\t}\n\tif (x>n) return;\n\tdfs1(x+1,y,s);\n\ta[y+1]=x;\n\tdfs1(x+1,y+1,s+bin[x-1]);\n}\n \nvoid pre()\n{\n\tfor (int i=0;i\ntemplate struct Modular_ {\n using T=typename std::decay::type;\n T val;\n explicit operator T() const { return val; }\n Modular_():val(){}\n template Modular_(U v):val(T(v%(F::get()))){ if(val<0)val+=(F::get()); }\n [[nodiscard]] bool operator ==(Modular_ b) const { return val == b.val; } [[nodiscard]] bool operator !=(Modular_ b) const { return val != b.val; } [[nodiscard]] bool operator <(Modular_ b) const { return val < b.val; } [[nodiscard]] bool operator >(Modular_ b) const { return val > b.val; } [[nodiscard]] bool operator <=(Modular_ b) const { return val <= b.val; } [[nodiscard]] bool operator >=(Modular_ b) const { return val >= b.val; }\n [[nodiscard]] friend Modular_ operator-(Modular_ x) { x.val=x.val?(F::get())-x.val:0; return x; }\n Modular_& operator+=(Modular_ m) { if ((val += m.val) >= (F::get())) val -= (F::get()); return *this; }\n Modular_& operator-=(Modular_ m) { if ((val -= m.val) < 0) val += (F::get()); return *this; }\n Modular_& operator*=(Modular_ m) {\n if(__builtin_constant_p(m.val)){\n switch(m.val){\n case 1: return *this;\n case 2: return *this+=*this;\n case 3: return *this+=*this+*this;\n case 0: return *this=0;\n case -1: return *this=-*this;\n }\n }else if(__builtin_constant_p(val)){\n return *this=m**this;\n }\n if(std::is_same::value){\n val = int((int64_t)val*m.val%(F::get()));\n }else if(std::is_same::value){\n uint64_t result=\n (uint64_t)val*(uint64_t)m.val-\n uint64_t((long double)val*m.val/(F::get()))*(uint64_t)(F::get());\n result+=(F::get());\n if(result>=(F::get())){\n result-=(F::get());\n if(result>=(F::get())) result-=(F::get());\n }\n val=result;\n }else{\n assert(false);\n }\n return *this;\n }\n template\n [[nodiscard]] Modular_ pow(U p) const {\n Modular_ a=*this;\n Modular_ ans = 1; for (; p; p >>= 1, a *= a) if (p&1) ans *= a;\n return ans;\n }\n [[nodiscard]] Modular_ inv() const {\n T a=val,b=(F::get()), ax=1,bx=0;\n while(b){\n T q=a/b, t=a%b;\n a=b; b=t;\n t=ax-bx*q; ax=bx; bx=t;\n }\n assert(a==1);\n if(ax<0)ax+=(F::get());\n return ax;\n }\n Modular_& operator/=(Modular_ m) { return (*this) *= m.inv(); }\n [[nodiscard]] friend Modular_ operator +(Modular_ a, Modular_ b) { return a += b; } [[nodiscard]] friend Modular_ operator -(Modular_ a, Modular_ b) { return a -= b; } [[nodiscard]] friend Modular_ operator *(Modular_ a, Modular_ b) { return a *= b; } [[nodiscard]] friend Modular_ operator /(Modular_ a, Modular_ b) { return a /= b; }\n};\ntemplate struct TGetter{ static T get(){ return x; } };\ntemplate using Modular=Modular_>;\ntemplate using ModularP=Modular_>;\ntemplate using ModularP64=Modular_>;\n\nusing modular=Modular<1000000007>;\n\nint main(){\n\tstd::ios::sync_with_stdio(0);std::cin.tie(0);\n\tint number; std::cin>>number;\n\t//std::vector> probability(number, std::vector(number));\n\tstd::vector probability(number*number);\n\t//for(auto& row: probability)\n\tfor(auto& value: probability){\n\t\tint percentage; std::cin>>percentage;\n\t\tvalue=modular(1)/100*percentage;\n\t}\n\n\tstd::vector total;\n\n\tint const middle=number/2;\n\tauto const maskValue=[&]{\n\t\tstd::array maskValue;\n\t\tassert(number<=6);\n\t\tint count=0;\n\t\tfor(int matchMask=0; matchMask<(int)maskValue.size(); ++matchMask){\n\t\t\tmaskValue[matchMask]=__builtin_popcount(matchMask)==middle ? count++: -1;\n\t\t}\n\t\ttotal.resize(1<>=1){\n\t\t\t\ttotalProbability*=tmp&1 ? probability[i]: 1-probability[i];\n\t\t\t\tif(totalProbability==0) goto continue_outer_2;\n\t\t\t}\n\n\t\t\t{ // compute matchSets and call handle\n\t\t\t\t// even more inefficient...?\n\t\t\t\tstd::array data{};\n\t\t\t\tassert(len<=(int)data.size());\n\t\t\t\tint matchSets{};\n\t\t\t\twhile(true){\n\t\t\t\t\t// process data\n\t\t\t\t\tint matchMask{};\n\t\t\t\t\tfor(int index=0; index>(index*number+data[index])&1)) goto continue_outer;\n\t\t\t\t\t\tif(matchMask&1<\n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long ll;\nconst ll MOD = 1e9+7;\nll add(ll x, ll y) {\n\treturn (x + y) % MOD;\n}\nll sub(ll x, ll y) {\n\tx -= y;\n\treturn (x<0 ? x+MOD : x);\n}\nll mult(ll x, ll y) {\n\treturn (x * y) % MOD;\n}\nll modpow(ll x, ll p) {\n\tif (p == 0) return 1;\n\tif (p & 1) return mult(x, modpow(x, p - 1));\n\treturn modpow(mult(x, x), p>>1);\n}\nll modinv(ll x) {\n\treturn modpow(x, MOD-2);\n}\n\nint n;\nvector> A;\nll T[8][1<<7][1<<7];\nvector> DP;\nvector family(1<<7,0);\nvector family_id(1<<7,0);\nvector> family_mask;\n\nvoid calc() {\n family_mask.resize(n+1);\n for (int mask=1; mask<(1<>1] + (mask & 1);\n }\n for (int mask=0; mask<(1<>n;\n A.resize(n);\n for (int i=0; i>A[i][j];\n A[i][j] = mult(A[i][j],modinv(100));\n }\n }\n calc();\n DP.resize(n+1);\n DP[0][1] = 1;\n for (int k=0; k\n#define ll long long\n#define ull unsigned ll\n#define uint unsigned\n#define pii pair\n#define pll pair\n#define PB push_back\n#define fi first\n#define se second\n#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)\n#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)\n#define CLR(a,v) memset(a,v,sizeof(a));\n#define CPY(a,b) memcpy(a,b,sizeof(a));\nusing namespace std;\nconst int mo=1000000007;\nint n,S,cnt[250],id[250];\nint f[10][250];\nvector vec[10];\nmap mp;\nint power(int x,int y){\n\tint s=1;\n\tfor (;y;y/=2,x=1ll*x*x%mo)\n\t\tif (y&1) s=1ll*s*x%mo;\n\treturn s;\n}\nint DP(int x,ll sta){\n\tif (!sta) return 0;\n\tif (x==n) return 1;\n\tif (mp.find(pll(x,sta))!=mp.end()) return mp[pll(x,sta)];\n\t++S; int ans=0;\n\tFor(j,1,(1<\n\n#define pb push_back\nusing namespace std;\nusing INT = long long;\nconst int mod = 1e9+7;\nint power(int a, int n, int mod, int ans=1) {\n for(; n; n>>=1, a=(INT)a*a%mod) if(n & 1) ans = (INT)ans * a % mod;\n return ans;\n}\n\nconst int NN = 1<<7;\nint p[NN][NN];\nint pro[NN][NN];\nint bpt[NN];\nINT q[NN][NN];\nint n;\nint id[NN];\nvector vec[NN];\nmap, int> mp;\n\ninline void add(int &u, int v) {u+=v; if(u>=mod) u -= mod;}\n\nint calc(int now, INT st) {\n if(st == 0) return 0;\n if(now == n) return 1;\n if(mp.find({now, st}) != mp.end()) return mp[{now, st}];\n int ans = 0;\n for(int j=1; j<1<>k)&1) {\n pro[i][j] = (INT)pro[i][j] * p[i][k] % mod;\n } else pro[i][j] = (INT)pro[i][j] * (mod+1-p[i][k]) % mod;\n }\n }\n\n for(int i=0; i<1<>j)&1)) q[i][1<\n#include \n#include \n\n#define pb push_back\n#define SZ(x) ((int)(x).size())\n#define ALL(x) x.begin(),x.end()\n#define all(x) x.begin(),x.end()\n#define fi first\n#define se second\n#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));\n\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate\nusing ordered_set = tree<\nT,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>;\n\n//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)\n//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k\n\ntypedef long long LL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector VPII;\ntypedef vector VPLL;\ntypedef vector VLL;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VC;\ntypedef long double LD;\ntypedef pair PLD;\ntypedef vector VLD;\ntypedef vector VPLD;\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<\" = \"<>n;\n for(int i = 1;i <= n;i++)\n for(int j = 1;j <= n;j++)\n cin>>tab[i][j];\n int xd = 0;\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n maska[(1<\n#include \n#include \n#include \n#include \nusing namespace std;\n\n#include \nusing namespace __gnu_pbds;\n\nauto random_address = [] { char *p = new char; delete p; return (uint64_t) p; };\n\nstruct safe_hash {\n static uint64_t splitmix64(uint64_t x) {\n // http://xorshift.di.unimi.it/splitmix64.c\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n uint64_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = splitmix64(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));\n return splitmix64(x ^ FIXED_RANDOM);\n }\n};\n\nconst int MOD = 1e9 + 7;\n\nstruct mod_int {\n int val;\n\n mod_int(long long v = 0) {\n if (v < 0)\n v = v % MOD + MOD;\n\n if (v >= MOD)\n v %= MOD;\n\n val = v;\n }\n\n static int mod_inv(int a, int m = MOD) {\n // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example\n int g = m, r = a, x = 0, y = 1;\n\n while (r != 0) {\n int q = g / r;\n g %= r; swap(g, r);\n x -= q * y; swap(x, y);\n }\n\n return x < 0 ? x + m : x;\n }\n\n explicit operator int() const {\n return val;\n }\n\n mod_int& operator+=(const mod_int &other) {\n val += other.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n\n mod_int& operator-=(const mod_int &other) {\n val -= other.val;\n if (val < 0) val += MOD;\n return *this;\n }\n\n static unsigned fast_mod(uint64_t x, unsigned m = MOD) {\n#if !defined(_WIN32) || defined(_WIN64)\n return x % m;\n#endif\n // Optimized mod for Codeforces 32-bit machines.\n // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.\n unsigned x_high = x >> 32, x_low = (unsigned) x;\n unsigned quot, rem;\n asm(\"divl %4\\n\"\n : \"=a\" (quot), \"=d\" (rem)\n : \"d\" (x_high), \"a\" (x_low), \"r\" (m));\n return rem;\n }\n\n mod_int& operator*=(const mod_int &other) {\n val = fast_mod((uint64_t) val * other.val);\n return *this;\n }\n\n mod_int& operator/=(const mod_int &other) {\n return *this *= other.inv();\n }\n\n friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; }\n friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; }\n friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; }\n friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; }\n\n mod_int& operator++() {\n val = val == MOD - 1 ? 0 : val + 1;\n return *this;\n }\n\n mod_int& operator--() {\n val = val == 0 ? MOD - 1 : val - 1;\n return *this;\n }\n\n mod_int operator++(int) { mod_int before = *this; ++*this; return before; }\n mod_int operator--(int) { mod_int before = *this; --*this; return before; }\n\n mod_int operator-() const {\n return val == 0 ? 0 : MOD - val;\n }\n\n bool operator==(const mod_int &other) const { return val == other.val; }\n bool operator!=(const mod_int &other) const { return val != other.val; }\n\n mod_int inv() const {\n return mod_inv(val);\n }\n\n mod_int pow(long long p) const {\n assert(p >= 0);\n mod_int a = *this, result = 1;\n\n while (p > 0) {\n if (p & 1)\n result *= a;\n\n a *= a;\n p >>= 1;\n }\n\n return result;\n }\n};\n\n\nconst int N_MAX = 10;\n\nint N;\nmod_int probability[N_MAX][N_MAX];\n\nint main() {\n cin >> N;\n\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++) {\n int p;\n cin >> p;\n probability[i][j] = mod_int(p) / mod_int(100);\n }\n\n vector> masks_with_count(N + 1);\n vector lookup(1 << N, -1);\n\n for (int mask = 0; mask < 1 << N; mask++) {\n int count = __builtin_popcount(mask);\n lookup[mask] = masks_with_count[count].size();\n masks_with_count[count].push_back(mask);\n }\n\n gp_hash_table possible_masks;\n possible_masks[1 << lookup[0]] = 1;\n\n for (int left = 0; left < N; left++) {\n gp_hash_table new_possible_masks;\n vector products(1 << N, 1);\n\n for (int current = 0; current < 1 << N; current++)\n for (int right = 0; right < N; right++)\n products[current] *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right];\n\n vector new_bits(N, 0);\n vector new_possible(1 << N, 0);\n\n for (auto &p : possible_masks) {\n long long possible = p.first;\n mod_int prob = p.second;\n\n new_bits.assign(N, 0);\n\n for (int mask : masks_with_count[left])\n if (possible >> lookup[mask] & 1)\n for (int i = 0; i < N; i++)\n if ((mask >> i & 1) == 0)\n new_bits[i] |= 1LL << lookup[mask | 1 << i];\n\n new_possible[0] = 0;\n\n for (int current = 1; current < 1 << N; current++) {\n new_possible[current] = new_possible[current & (current - 1)] | new_bits[__builtin_ctz(current)];\n new_possible_masks[new_possible[current]] += prob * products[current];\n }\n }\n\n possible_masks = new_possible_masks;\n cerr << possible_masks.size() << endl;\n }\n\n mod_int answer = 0;\n\n for (auto &p : possible_masks)\n if (p.first >> lookup[(1 << N) - 1] & 1)\n answer += p.second;\n\n cout << (int) answer << '\\n';\n}\n ", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "6b78894e0dc6ed2523296ac2f17afad1", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n\nusing namespace std;\n\ntypedef double db;\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned int ui;\ntypedef unsigned long long ull;\n\ntypedef pair < db, db > pdd;\ntypedef pair < db, ld > pdl;\ntypedef pair < ld, db > pld;\ntypedef pair < ld, ld > ldp;\n\ntypedef pair < ll, ll > pll;\ntypedef pair < int, ll > pil;\ntypedef pair < ll, int > pli;\ntypedef pair < int, int > pii;\n\n#define f first\n#define s second\n\n#define en end()\n#define bg begin()\n\n#define rev reverse\n#define mp make_pair\n#define pb push_back\n\n#define y1 y1234567890\n#define um unordered_map\n\n#define all(x) x.bg, x.en\n#define sz(x) (int)x.size()\n#define len(x) (int)strlen(x)\n\n#define sqr(x) ((x + 0ll) * (x))\n#define sqrd(x) ((x + 0.0) * (x))\n\n#define forn(i, n) for (int i = 1; i <= n; i++)\n\nconst ll inf = (ll)1e18;\nconst ll mod = (ll)1e9 + 7;\n\nconst db eps = (db)1e-9;\nconst db pi = acos(-1.0);\n\nconst int dx[] = {0, 0, 1, 0, -1};\nconst int dy[] = {0, 1, 0, -1, 0};\n\nconst int N = (1 << 20);\n\nll bp(ll x, ll y){\n\tll z = 1;\n\twhile(y){\n\t\tif(y & 1)\n\t\t\tz = z * x % mod;\n\t\tx = x * x % mod;\n\t\ty >>= 1;\t\n\t}\n\treturn z;\n}\n\nll ans, n, p[6][6], F[N], nump, m[N];\nvector > E;\n\nint main() {\n\t//freopen(\".in\", \"r\", stdin);\n\t//freopen(\".out\", \"w\", stdout);\n\t//freopen(\".err\", \"w\", stderr);\n\n\t//srand(time(NULL));\n\n\t//cin.tie(NULL);\n\t//cout.tie(NULL);\n\t//ios_base::sync_with_stdio(false);\n\n\t//cout << setprecision(5) << fixed;\n\n\tcin >> n;\n\tfor(int i = 0; i < n; ++i)\n\tfor(int j = 0; j < n; ++j){\n\t\tcin >> p[i][j];\n\t\tp[i][j] = p[i][j] * bp(100, mod - 2) % mod; \n\t}\n\t\n\tfor(int i = n; i < 6; ++i)\n\tfor(int j = n; j < 6; ++j)\n\t\tp[i][j] = 1;\n\t\n\tfor(int i = 0; i < 6; ++i)\n\tfor(int j = 0; j < 3; ++j)\n\t\tE.pb(mp(i, j));\n\n\tfor(int i = 0; i < 6; ++i)\n\t\tfor(int j = i + 1; j < 6; ++j)\n\t\t\tfor(int k = j + 1; k < 6; ++k){\n\t\t\t\tvector v;\n\t\t\t\tv.pb(i);\n\t\t\t\tv.pb(j);\n\t\t\t\tv.pb(k);\n\t\t\t\tdo{\n\t\t\t\t\tint mask1 = ((1 << (v[0] * 3)) ^ (1 << (v[1] * 3 + 1)) ^ (1 << (v[2] * 3 + 2)));\n\t\t\t\t\tfor(int mask = 0; mask < (1 << 18); ++mask)\n\t\t\t\t\t\tif((mask & mask1) == mask1)\n\t\t\t\t\t\t\tm[mask] |= (1 << nump);\n\t\t\t\t}while(next_permutation(v.begin(), v.end()));\t\n\t\t\t\tnump++;\n\t\t\t} \n\n\tfor(int mask = 0; mask < (1 << 18); ++mask){\n\t\tll v = 1;\n\t\tfor(int i = 0; i < 18; ++i){\n\t\t\tif((mask >> i) & 1)\n\t\t\t\tv = v * p[E[i].f][E[i].s] % mod;\n\t\t\telse v = v * (1 - p[E[i].f][E[i].s] + mod) % mod;\n\t\t}\n\t\tF[m[mask]] += v;\n\t\tif(F[m[mask]] >= mod)\n\t\t\tF[m[mask]] -= mod;\n\t}\n\n\n\tfor(int i = 0; i < 20; ++i)\n\tfor(int mask = 0; mask < (1 << 20); ++mask)\n\t\tif(mask & (1< v;\n\t\t\t\tfor(int ii = 0; ii < 6; ++ii)\n\t\t\t\t\tif(ii != i && ii != j && ii != k)\n\t\t\t\t\t\tv.pb(ii);\n\t\t\t\tdo{\n\t\t\t\t\tint mask1 = ((1 << (v[0] * 3)) ^ (1 << (v[1] * 3 + 1)) ^ (1 << (v[2] * 3 + 2)));\n\t\t\t\t\tfor(int mask = 0; mask < (1 << 18); ++mask)\n\t\t\t\t\t\tif((mask & mask1) == mask1)\n\t\t\t\t\t\t\tm[mask] |= (1 << nump);\n\t\t\t\t}while(next_permutation(v.begin(), v.end()));\t\n\t\t\t\tnump++;\n\t\t\t}\n\n\tE.clear();\n\tfor(int i = 0; i < 6; ++i)\n\tfor(int j = 3; j < 6; ++j)\n\t\tE.pb(mp(i, j));\n\n\tfor(int mask = 0; mask < (1 << 18); ++mask){\n\t\tll v = 1;\n\t\tfor(int i = 0; i < 18; ++i){\n\t\t\tif((mask >> i) & 1)\n\t\t\t\tv = v * p[E[i].f][E[i].s] % mod;\n\t\t\telse v = v * (1 - p[E[i].f][E[i].s] + mod) % mod;\n\t\t}\n\t\tans = (ans + v * ((1 - F[((1 << 20) - 1) ^ m[mask]] + mod) % mod) % mod) % mod; \n\t}\n\t\n\tcout << ans << endl;\n\t\n\t//cerr << (clock() + 0.0) / CLOCKS_PER_SEC;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "46e07706329a85e4f694bdde6f31b4c9", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nconst int N=1e5+10;\nconst int mod=1e9+7;\ntypedef unsigned long long ull;\n\nint n,m,tt=0,ch[N][64],p[10][10],q[10][64],f[10][N];\nmap id;\n\nint qpow(int a,int b) {\n\tint ret=1;\n\twhile(b) {\n\t\tif(b&1) ret=1ll*ret*a%mod;\n\t\ta=1ll*a*a%mod,b>>=1;\n\t}\n\treturn ret;\n}\n\nint dfs(ull s) {\n\tif(id[s]) return id[s];\n\tid[s]=++tt;\n\tfor(int i=0;i<(1<>k&1)&&(i>>k&1)&&(s>>(j^(1<>n;dfs(1);\n\tfor(int i=0;i>p[i][j],p[i][j]=1ll*p[i][j]*qpow(100,mod-2)%mod;\n\tfor(int i=0;i>k&1) q[i][j]=1ll*q[i][j]*p[k][i]%mod;\n\t\t\t\telse q[i][j]=1ll*q[i][j]*(mod+1-p[k][i])%mod;\n\t\t\t}\n\t\t}\n\tf[0][1]=1;\n\tfor(int i=0;i\nusing namespace std;\nconst int N=10,M=1e6+7,K=200,p=1e9+7;\n#define doge bitset<40>\nint n,e[N],s[M],wf[K],q[N][K],f[N][N],ans[N][M];\ndoge op,wq[N][M];\nunordered_maprt[N];\ninline int read(){\n\tint num=0; char g=getchar(); while(g<48||570) {if(v&1) ans=ans*u%p; u=u*u%p,v=v>>1;} return ans;\n}\ninline void find(int u){\n\tq[u][0]=0; int os=0,of=0;\n\tfor(int i=0;i<(1<\nusing namespace std;\ntypedef vector vi;\ntypedef vector > vpii;\ntypedef pair pii;\ntypedef long long ll;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef vector vll;\n#define INF 0x3f3f3f3f\n#define MOD 1000000007LL\n#define INV 570000004LL\n#define EPSILON 0.00001\n#define f first\n#define s second\n#define pb push_back\n#define mp make_pair\n \n#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)\n#define F0R(i, a) for (int i=0; i<(signed)(a); i++)\n#define RFOR(i, a, b) for (int i=(a); i >= b; i--)\n \n#define MN 22\nint n;\nll p[MN][MN];\nvi permsA[MN];\nvi permsB[MN];\nll bmA[1<> n;\n F0R(i, n){\n F0R(j, n){\n cin >> p[i][j];\n p[i][j] = (p[i][j]*INV)%MOD;\n }\n }\n F0R(i, 6){\n FOR(j, n, 6-1){\n if(i == j) p[i][j] = 1;\n else{p[i][j] = 0; p[j][i] = 0;}\n }\n }\n n = 6;\n /*F0R(i, n){\n F0R(j, n){\n cout << p[i][j] << \" \";\n }\n cout << \"\\n\";\n }*/\n F0R(i, 1<<18){\n ll prob1 = 1;\n ll prob2 = 1;\n int bm1 = 0;\n int bm2 = 0;\n F0R(j, 6){\n F0R(k, 3){\n int res = j*3+k;\n if((1<\n#include\n#include\n#define all(v) (v).begin(),(v).end()\n#define rep(i,l,r) for(int i=(l),i##end=(r);i<=i##end;++i)\nusing namespace __gnu_pbds;\nusing namespace std;\nconst int mod = 1e9 + 7;\nconst int maxn = 20;\ninline void flush(){cout.flush(),fflush(stdout);}\ntypedef unsigned long long u64;\ntypedef unsigned u32;\ntypedef long long ll;\ntypedef long double ld;\ntemplateusing heap=__gnu_pbds::priority_queue;\ntemplateusing rbt=tree,rb_tree_tag,tree_order_statistics_node_update>;\ntemplateinline istream&operator,(istream&cin,t&x){return cin>>x;}\ntemplateinline ostream&operator,(ostream&cout,const t&x){return cout<inline void up(_1&a,const _2&b){if(ainline void down(_1&a,const _2&b){if(a>b)a=b;}\nistream&in=cin;ostream&out=cout;\ninline int pow(int a,int b){\n int ans=1;\n for(;b;b>>=1,a=ll(a)*a%mod)\n if(b&1)ans=ll(ans)*a%mod;\n return ans;\n}\ninline int inv(int a){return pow(a,mod-2);}\ntypedef long long ll;\nint a[maxn][maxn];;\nint ans,n;\n\nconst int N = 2001000;\nint f[N],g[N],b[N];\nint mp[N],tot[maxn];\ninline void reduce(int & x){ x += x >> 31 & mod; }\ninline void dfs(int x){\n}\ninline int fac(int x){ return x == 0 ? 1 : fac(x - 1) * x; }\ninline int C(int n,int m){ return fac(n) / fac(m) / fac(n - m); }\ninline int pc(int x){ return __builtin_popcount(x); }\nvector v[maxn];\ninline void fwt(int * a,int lim){\n\tif(!(lim/=2)) return;\n\tfor(int i=0;i> j & 1 ? b[j] : mod + 1 - b[j]) % mod;\n\t\tfor(int j=0;j> j * n) & ((1 << n) - 1); \n\t\tint st = 0;\n\t\tfor(int j : v[m]){\n\t\t\tint s[3],tot = 0;\n\t\t\tfor(int k=j;k;k&=k-1) s[tot++] = __builtin_ctz(k);\n\t\t\tsort(s,s + m);\n\t\t\tdo{\n\t\t\t\tfor(int j=0;j> s[j] & 1) == 0) goto nxt;\n\t\t\t\tst |= 1 << mp[j];\n\t\t\t\tnxt:;\n\t\t\t}while(next_permutation(s,s+m));\n\t\t}\n\t\treduce(f[st] += p - mod);\n\t}\n\t// fwt(f,1 << tot[m]);\n\tconst int M = n - m;\n\trep(i,0,(1<<(M * n))-1){\n\t\tint p = 1; int S[3];\n\t\tfor(int j=0;j> j & 1 ? b[j + n * m] : mod + 1 - b[j + n * m]) % mod;\n\t\tfor(int j=0;j> j * n) & ((1 << n) - 1); \n\t\tint st = 0;\n\t\tfor(int j : v[n - m]){\n\t\t\tint s[3],tot = 0;\n\t\t\tfor(int k=j;k;k&=k-1) s[tot++] = __builtin_ctz(k);\n\t\t\tsort(s,s + M);\n\t\t\tdo{\n\t\t\t\tfor(int j=0;j> s[j] & 1) == 0) goto nxt2;\n\t\t\t\tst |= 1 << mp[(~j) & (1 << n) - 1];\n\t\t\t\tnxt2:;\n\t\t\t}while(next_permutation(s,s+M));\n\t\t}\n\t\treduce(g[st] += p - mod);\n\t}\n\tint ans = 0;\n\tfwt(f,1 << tot[m]); fwt(g,1 << tot[m]);\n\tfor(int i=0;i<1<\n#include\n#include\n#include\nusing namespace std;\nconst int maxn = 8;\nconst int SZ = 1048576 + 5;\nconst int MOD = 1000000007;\n\ninline int inc(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); }\ninline int dec(int a, int b) { return (a >= b) ? (a - b) : (a + MOD - b); }\ninline int mul(int a, int b) { return 1LL * a * b % MOD; }\n\ninline void updata(int &x, int y) { x = inc(x,y); }\n\ninline int power(int x, int k) {\n\tint tmp = 1;\n\twhile(k) {\n\t\tif(k & 1) tmp = mul(tmp,x);\n\t\tx = mul(x,x);\n\t\tk >>= 1;\n\t}\n\treturn tmp;\n}\n\nint n, a[maxn][maxn], inv100, g[maxn][maxn], p[SZ], num[7][7][7], all, Ans;\n\ninline bool check(int x, int y, int z) {\n\tint pos[4], lis[4];\n\tlis[1] = x; lis[2] = y; lis[3] = z;\n\tfor(int i = 1;i <= 3;i ++) pos[i] = i;\n\tdo{\n\t\tint flag = 1;\n\t\tfor(int i = 1;i <= 3;i ++) {\n\t\t\tif(!g[pos[i]][lis[i]]) flag = 0;\n\t\t}\n\t\tif(flag) return 1;\n\t}while(next_permutation(pos + 1,pos + 3 + 1));\n\treturn 0;\n}\n\nvoid dfs1(int now, int x, int y) {\n\tbool used[7];\n\tif(!now) return;\n//\tcout << now << ' ' << x << ' ' << y << endl;\n\tif(now == 0 && x == 4 && y == 1) {\n\t\tint xxx;\n\t\txxx ++;\n\t}\n if(x == 4) {\n\t\tfor(int i = 1;i <= 6;i ++) used[i] = 1;\n\t\tint sta = 0;\n\t\tif(now) {\n\t\t\tint xxx;\n\t\t\txxx ++;\n\t\t}\n\t\tfor(int i = 1;i <= 6;i ++) {\n\t\t\tfor(int j = i + 1;j <= 6;j ++) {\n\t\t\t\tfor(int k = j + 1;k <= 6;k ++) {\n\t\t\t\t\tif(check(i,j,k)) {\n\t\t\t\t\t\tint X, Y, Z; X = Y = Z = - 1;\n\t\t\t\t\t\tused[i] = used[j] = used[k] = 0;\n\t\t\t\t\t\tfor(int l = 1;l <= 6;l ++) {\n\t\t\t\t\t\t\tif(!used[l]) continue;\n\t\t\t\t\t\t\tif(X == -1) X = l;\n\t\t\t\t\t\t\telse if(Y == -1) Y = l;\n\t\t\t\t\t\t\telse Z = l;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tused[i] = used[j] = used[k] = 1;\n\t\t\t\t\t\tif(Z == -1) continue;\n sta |= num[X][Y][Z];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tupdata(p[sta],now);\n\t\treturn;\n\t}\n\tg[x][y] = 1; int nxt = mul(now,a[x][y]);\n\tif(y == 6) dfs1(nxt,x + 1,1);\n\telse dfs1(nxt,x,y + 1);\n\tg[x][y] = 0; nxt = mul(now,dec(1,a[x][y]));\n\tif(y == 6) dfs1(nxt,x + 1,1);\n\telse dfs1(nxt,x,y + 1);\n}\n\nvoid dfs2(int now, int x, int y) {\n\tif(!now) return;\n\tif(x == 7) {\n\t\tint sta = 0;\n\t\tfor(int i = 1;i <= 6;i ++) {\n\t\t\tfor(int j = i + 1;j <= 6;j ++) {\n\t\t\t\tfor(int k = j + 1;k <= 6;k ++) {\n\t\t\t\t\tif(check(i,j,k)) sta |= num[i][j][k];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tupdata(Ans,mul(now,p[all ^ sta]));\n\t\treturn;\n\t}\n g[x - 3][y] = 1; int nxt = mul(now,a[x][y]);\n\tif(y == 6) dfs2(nxt,x + 1,1);\n\telse dfs2(nxt,x,y + 1);\n\tg[x - 3][y] = 0; nxt = mul(now,dec(1,a[x][y]));\n\tif(y == 6) dfs2(nxt,x + 1,1);\n\telse dfs2(nxt,x,y + 1);\n}\n\nint main() {\n//\tfreopen(\"data1.in\",\"r\",stdin);\n scanf(\"%d\", &n); inv100 = power(100,MOD - 2);\n\tfor(int i = 1;i <= n;i ++) {\n\t\tfor(int j = 1;j <= n;j ++) scanf(\"%d\", &a[i][j]), a[i][j] = mul(a[i][j],inv100);\n\t}\n\tfor(int i = n + 1;i <= 6;i ++) a[i][i] = 1;\n\tint cnt = 0;\n\tfor(int i = 1;i <= 6;i ++) {\n\t\tfor(int j = i + 1;j <= 6;j ++) {\n\t\t\tfor(int k = j + 1;k <= 6;k ++) {\n\t\t\t\tnum[i][j][k] = 1 << cnt;\n\t\t\t\tcnt ++;\n\t\t\t}\n\t\t}\n\t}\n\tall = (1 << cnt) - 1;\n\tdfs1(1,1,1);\n//\tputs(\"\");\n\tfor(int j = 0;j < cnt;j ++) {\n\t\tfor(int i = 0;i < (1 << cnt);i ++) {\n\t\t\tif(i & (1 << j)) updata(p[i],p[i ^ (1 << j)]);\n\t\t}\n\t}\n//\tfor(int i = 0;i < (1 << cnt);i ++) if(p[i]) printf(\"%d %d\\n\", i, p[i]);\n\tAns = 0;\n\tdfs2(1,4,1);\n\tAns = dec(1,Ans);\n\tprintf(\"%d\\n\", Ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "7e3f2f00cec90fbd3e212a00e65b25d6", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define LL long long\n#define pb push_back\n#define mp make_pair\n#define pii pair\nusing namespace std;\ninline int read(){\n\tint v=0,f=1;\n\tchar c=getchar();\n\twhile (c<'0' || c>'9'){\n\t\tif (c=='-') f=-1;\n\t\tc=getchar();\n\t}\n\twhile (c>='0' && c<='9'){\n\t\tv=v*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn v*f;\n}\nconst int Maxn=10;\nconst LL mod=1e9+7;\nLL qp(LL x,LL p){\n\tLL ret=1;\n\twhile (p){\n\t\tif (p&1) ret=ret*x%mod;\n\t\tx=x*x%mod;\n\t\tp>>=1; \n\t}\n\treturn ret;\n}\nLL inv(LL x){\n\treturn qp(x,mod-2);\n}\nint p[Maxn][Maxn],n;\nint id[64],ki,X[1048576],Y[1048576];\nvoid add(int &x,int y){\n\tx+=y;\n\tif (x>=mod) x-=mod;\n}\nvoid sub(int &x,int y){\n\tx-=y;\n\tif (x<0) x+=mod;\n}\nvoid FWT(int *P,int opt)\n{\n\tint N=1048576;\n for(int i=2;i<=N;i<<=1)\n for(int p=i>>1,j=0;j>6&A,msk3=i>>12&A;\n\t\tint mask=0;\n\t\tLL pp=1;\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk1>>j&1){\n\t\t\t\tpp=pp*p[0][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[0][j])%mod;\n\t\t\t}\n\t\t}\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk2>>j&1){\n\t\t\t\tpp=pp*p[1][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[1][j])%mod;\n\t\t\t}\n\t\t}\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk3>>j&1){\n\t\t\t\tpp=pp*p[2][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[2][j])%mod;\n\t\t\t}\n\t\t}\n\t\tif (!p) continue;\n\t\tfor (int a=0;a<6;a++){\n\t\t\tif (msk1>>a&1){\n\t\t\t\tfor (int b=0;b<6;b++){\n\t\t\t\t\tif (a==b) continue;\n\t\t\t\t\tif (msk2>>b&1){\n\t\t\t\t\t\tfor (int c=0;c<6;c++){\n\t\t\t\t\t\t\tif (a==c || b==c) continue;\n\t\t\t\t\t\t\tif (msk3>>c&1){\n\t\t\t\t\t\t\t\tint nmsk=(1<>6&A,msk3=i>>12&A;\n\t\tint mask=0;\n\t\tLL pp=1;\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk1>>j&1){\n\t\t\t\tpp=pp*p[3][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[3][j])%mod;\n\t\t\t}\n\t\t}\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk2>>j&1){\n\t\t\t\tpp=pp*p[4][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[4][j])%mod;\n\t\t\t}\n\t\t}\n\t\tfor (int j=0;j<6;j++){\n\t\t\tif (msk3>>j&1){\n\t\t\t\tpp=pp*p[5][j]%mod;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tpp=pp*(mod+1-p[5][j])%mod;\n\t\t\t}\n\t\t}\n\t\tif (!p) continue;\n\t\tfor (int a=0;a<6;a++){\n\t\t\tif (msk1>>a&1){\n\t\t\t\tfor (int b=0;b<6;b++){\n\t\t\t\t\tif (a==b) continue;\n\t\t\t\t\tif (msk2>>b&1){\n\t\t\t\t\t\tfor (int c=0;c<6;c++){\n\t\t\t\t\t\t\tif (a==c || b==c) continue;\n\t\t\t\t\t\t\tif (msk3>>c&1){\n\t\t\t\t\t\t\t\tint nmsk=(1<\n#include \n#include \n\n#define pb push_back\n#define SZ(x) ((int)(x).size())\n#define ALL(x) x.begin(),x.end()\n#define all(x) x.begin(),x.end()\n#define fi first\n#define se second\n#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));\n\n\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate\nusing ordered_set = tree<\nT,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>;\n\n//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)\n//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k\n\ntypedef long long LL;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef vector VPII;\ntypedef vector VPLL;\ntypedef vector VLL;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VC;\ntypedef long double LD;\ntypedef pair PLD;\ntypedef vector VLD;\ntypedef vector VPLD;\n\ntemplate void _dbg(const char *sdbg, TH h){ cerr< void _dbg(const char *sdbg, TH h, TA... a) {\n while(*sdbg!=',')cerr<<*sdbg++;\n cerr<<\" = \"<>n;\n for(int i = 1;i <= n;i++)\n for(int j = 1;j <= n;j++)\n cin>>tab[i][j];\n int xd = 0;\n for(int i = 1;i <= 6;i++)\n for(int j = i + 1;j <= 6;j++)\n for(int k = j + 1;k <= 6;k++)\n maska[(1<\nusing namespace std;\n\ntypedef bitset<128> u99;\n\nconstexpr const int mod = 1000000007;\nconstexpr const int inv = 570000004;\nconstexpr int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; }\nconstexpr int sub(int a, int b) { return a < b ? a - b + mod : a - b; }\nconstexpr int mul(int a, int b) { return 1LL * a * b % mod; }\n\nunordered_map name;\nint n, dfn, son[65536][128];\nint dfs(u99 s) {\n\tauto ite = name.find(s);\n\tif (ite != name.end()) return ite->second;\n\tint ret = dfn++;\n\tname[s] = ret;\n\tfor (int i = 0; i != (1 << n); ++i) {\n\t\tu99 t = s;\n\t\tfor (int j = 0; j != (1 << n); ++j) if (!t[j])\n\t\t\tfor (int k = i & j; k; k ^= k & -k) if (s[j ^ k & -k]) {\n\t\t\t\tt[j] = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tson[ret][i] = dfs(t);\n\t}\n\treturn ret;\n}\n\nint p[7][7], q[7][128], dp[8][65536];\n\nint main() {\n\tcin >> n;\n\tdfs(1);\n\tfor (int i = 0; i != n; ++i) for (int j = 0; j != n; ++j) cin >> p[i][j], p[i][j] = mul(p[i][j], inv);\n\tfor (int i = 0; i != n; ++i) for (int j = 0; j != (1 << n); ++j) {\n\t\tq[i][j] = 1;\n\t\tfor (int k = 0; k != n; ++k) q[i][j] = mul(q[i][j], j >> k & 1 ? p[k][i] : sub(1, p[k][i]));\n\t}\n\tdp[0][0] = 1;\n\tfor (int i = 0; i != n; ++i) for (int j = 0; j != dfn; ++j) for (int k = 0; k != (1 << n); ++k)\n\t\tdp[i + 1][son[j][k]] = add(dp[i + 1][son[j][k]], mul(dp[i][j], q[i][k]));\n\tu99 res;\n\tfor (int i = 0; i != (1 << n); ++i) res[i] = 1;\n\tcout << dp[n][name[res]] << endl;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "probabilities"], "code_uid": "bf9b2915a28ba0db4158f6a56724e999", "src_uid": "906d4e49566e63fddaf8eac7384c6284", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntemplate ostream& operator<<(ostream& o,const pair &p){\n\treturn o<<\"(\"< ostream& operator<<(ostream& o,const vector &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o< using V = vector;\ntemplate using VV = vector>;\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\n#else\n#define show(x) true\n#endif\n\ntemplate\nstruct ModInt{\n\tusing uint = unsigned int;\n\tusing ll = long long;\n\tusing ull = unsigned long long;\n\n\tconstexpr static uint mod = mod_;\n\n\tuint v;\n\tModInt():v(0){}\n\tModInt(ll _v):v(normS(_v%mod+mod)){}\n\texplicit operator bool() const {return v!=0;}\n\tstatic uint normS(const uint &x){return (x [0 , mod-1]\n\tstatic ModInt make(const uint &x){ModInt m; m.v=x; return m;}\n\tModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}\n\tModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}\n\tModInt operator-() const { return make(normS(mod-v)); }\n\tModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}\n\tModInt operator/(const ModInt& b) const { return *this*b.inv();}\n\tModInt& operator+=(const ModInt& b){ return *this=*this+b;}\n\tModInt& operator-=(const ModInt& b){ return *this=*this-b;}\n\tModInt& operator*=(const ModInt& b){ return *this=*this*b;}\n\tModInt& operator/=(const ModInt& b){ return *this=*this/b;}\n\tModInt& operator++(int){ return *this=*this+1;}\n\tModInt& operator--(int){ return *this=*this-1;}\n\tll extgcd(ll a,ll b,ll &x,ll &y) const{\n\t\tll p[]={a,1,0},q[]={b,0,1};\n\t\twhile(*q){\n\t\t\tll t=*p/ *q;\n\t\t\trep(i,3) swap(p[i]-=t*q[i],q[i]);\n\t\t}\n\t\tif(p[0]<0) rep(i,3) p[i]=-p[i];\n\t\tx=p[1],y=p[2];\n\t\treturn p[0];\n\t}\n\tModInt inv() const {\n\t\tll x,y;\n\t\textgcd(v,mod,x,y);\n\t\treturn make(normS(x+mod));\n\t}\n\tModInt pow(ll p) const {\n\t\tif(p<0) return inv().pow(-p);\n\t\tModInt a = 1;\n\t\tModInt x = *this;\n\t\twhile(p){\n\t\t\tif(p&1) a *= x;\n\t\t\tx *= x;\n\t\t\tp >>= 1;\n\t\t}\n\t\treturn a;\n\t}\n\tbool operator==(const ModInt& b) const { return v==b.v;}\n\tbool operator!=(const ModInt& b) const { return v!=b.v;}\n\tfriend istream& operator>>(istream &o,ModInt& x){\n\t\tll tmp;\n\t\to>>tmp;\n\t\tx=ModInt(tmp);\n\t\treturn o;\n\t}\n\tfriend ostream& operator<<(ostream &o,const ModInt& x){ return o<;\n\nusing BS = bitset<128>;\nbool B(int x,int i){return (x>>i)&1;}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\tint N;\n\tcin >> N;\n\tVV p(N,V(N));\n\trep(i,N) rep(j,N) cin >> p[i][j], p[i][j] /= 100;\n\n\tunordered_map cur,nxt;\n\t{\n\t\tBS b;\n\t\tb[0] = true;\n\t\tcur[b] = 1;\n\t}\n\trep(i,N){\n\t\tfor(auto it: cur){\n\t\t\tBS b = it.fs;\n\t\t\tV ones;\n\t\t\trep(i,1<\n#include \n\nusing namespace std;\n\nconst long MAX_N = 1e3 + 1;\n\nlong n, m, h;\nlong aw[MAX_N];\nlong bw[MAX_N];\nvector g[MAX_N];\nbool v[MAX_N];\nvector st;\nvector< pair< pair, long > > ans;\n\nvoid add_edge(long a, long b) {\n g[a].push_back(b);\n g[b].push_back(a);\n}\n\nvoid try_flow(long i, long need) {\n if (i + 1 >= st.size()) return;\n\n long a = st[i], b = st[i + 1];\n\n long can = min(need, h - aw[b]);\n\n if (can > 0) {\n ans.push_back(make_pair(make_pair(a, b), can));\n }\n\n try_flow(i + 1, need);\n\n if (need - can > 0) {\n ans.push_back(make_pair(make_pair(a, b), need - can));\n }\n}\n\nvoid flow() {\n long a = st[0];\n long b = st[st.size() - 1];\n\n long need = min(aw[a] - bw[a], bw[b] - aw[b]);\n\n if (need > 0) {\n try_flow(0, need);\n aw[a] -= need; aw[b] += need;\n }\n}\n\nvoid dfs(long a) {\n v[a] = true; st.push_back(a);\n\n if (aw[a] < bw[a]) flow();\n\n for (size_t i = 0; i < g[a].size(); i++) {\n long b = g[a][i];\n if (!v[b]) dfs(b);\n }\n\n st.pop_back();\n}\n\nint main() {\n cin >> n >> h >> m;\n\n for (long i = 1; i <= n; i++) {\n cin >> aw[i];\n }\n\n for (long i = 1; i <= n; i++) {\n cin >> bw[i];\n }\n\n for (long i = 1; i <= m; i++) {\n long x, y; cin >> x >> y;\n add_edge(x, y);\n }\n\n for (long i = 1; i <= n; i++) {\n if (aw[i] > bw[i]) {\n for (long j = 1; j <= n; j++)\n v[j] = false;\n\n dfs(i);\n }\n }\n\n for (long i = 1; i <= n; i++) {\n if (aw[i] != bw[i]) {\n cout << \"NO\"; return 0;\n }\n }\n\n cout << ans.size() << endl;\n for (size_t i = 0; i < ans.size(); i++) {\n cout << ans[i].first.first << \" \" << ans[i].first.second << \" \" << ans[i].second << endl;\n }\n\n return 0;\n}//hello", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "cf9d00c7feed6f139733be3ba3b7e1e6", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/* Author haleyk10198 */\n/* �@��: haleyk10198 */\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define MOD 1000000007\n#define LINF (1LL<<60)\n#define INF 2147483647\n#define PI 3.1415926535897932384626433\n#define ll long long\n#define pii pair\n#define mp(x,y) make_pair((x),(y))\n\nusing namespace std;\ntypedef pair,int> cmd;\n\nint a[310],b[310];\n\nint n,v,e;\nbool vis[310];\nvector near[310];\nvector p;\n\nbool f;\nvector out;\n\nvoid rec(int pos,int req){\n\tint rem=min(req,a[p[pos]]);\n\tif(rem)\n\t\tout.push_back(mp(mp(p[pos],p[pos+1]),rem));\n\tif(pos)\n\t\trec(pos-1,req);\n\trem=req-rem;\n\tif(rem)\n\t\tout.push_back(mp(mp(p[pos],p[pos+1]),rem));\n}\n\nvoid dfs(int now,int root){\n\tp.push_back(now);\n\tvis[now]=true;\n\tif(a[now]b[root]){\n\t\tint v=min(b[now]-a[now],a[root]-b[root]);\n\t\trec(p.size()-2,v);\n\t\ta[now]+=v;a[root]-=v;\n\t}\n\tfor(int x:near[now])\n\t\tif(!vis[x])\n\t\t\tdfs(x,root);\n\tp.pop_back();\n}\n\nint main(){\n\t//freopen(\"317C.txt\",\"r\",stdin);\n\t//freopen(\"output.txt\",\"w\",stdout);\n\tios_base::sync_with_stdio(false);\n\tcin>>n>>v>>e;\n\tfor(int i=0;i>a[i];\n\tfor(int i=0;i>b[i];\n\tfor(int i=0;i>u>>v;\n\t\tnear[--u].push_back(--v);\n\t\tnear[v].push_back(u);\n\t}\n\tfor(int i=0;ib[i]){\n\t\t\tfill(vis,vis+n,0);\n\t\t\tdfs(i,i);\n\t\t}\n\t}\n\tfor(int i=0;i\nusing namespace std;\n#define vi vector\n#define pii pair\n#define pb push_back\n#define mp make_pair\n#define all(x) x.begin(),x.end()\n#define SZ(x) (int)(x.size())\n#define rep(i,a,b) for(int i=a;i=a;i--)\n#define inf 1000000007\n#define mod 1000000007\n#define x first\n#define y second\n#define pi acos(-1.0)\n#define DBG(x) cerr<<(#x)<<\"=\"<inline void Max(T &a,U b){if(ainline void Min(T &a,U b){if(a>b)a=b;}\n \n//FILE* outFile;\n \ninline void add(int &a,int b){a+=b;while(a>=mod)a-=mod;}\n \nint pow(int a,int b){\n int ans=1;\n while(b){\n if(b&1)ans=ans*(ll)a%mod;\n a=(ll)a*a%mod;b>>=1;\n }\n return ans;\n}\n\n\nll s;\nint V,a[303],b[303],v[303],rt;\nvi g[303],e[303],q;\nvector >ans;\nvoid up(int u,int fa){\n FOREACH(it,g[u]){\n int j=*it;\n if(v[j]==2||j==fa)continue;\n up(j,u);\n }\n if(fa!=-1&&fa!=rt){\n int x=min(V-a[fa],a[u]);\n if(x){\n ans.pb(mp(mp(u,fa),x));\n a[fa]+=x,a[u]-=x;\n }\n }\n else if(fa==rt&&b[fa]>a[fa]){\n int x=min(b[fa]-a[fa],a[u]);\n if(x){\n ans.pb(mp(mp(u,fa),x));\n a[fa]+=x,a[u]-=x;\n }\n }\n}\nvoid down(int u,int fa){\n FOREACH(it,g[u]){\n int j=*it;\n if(v[j]==2||j==fa)continue;\n down(j,u);\n }\n if(fa!=-1&&fa!=rt){\n int x=min(a[fa],V-a[u]);\n if(x){\n ans.pb(mp(mp(fa,u),x));\n a[fa]-=x,a[u]+=x;\n }\n }\n else if(fa==rt&&b[fa]b[x])down(x,-1);\n v[x]=2;\n }\n q.clear();\n }\n printf(\"%d\\n\",SZ(ans));\n FOREACH(it,ans){\n printf(\"%d %d %d\\n\",(it->x).x+1,(it->x).y+1,it->y);\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "7b0902ceb51c3912d9d99ec20c4efedc", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nint N, V, E;\nint mat[301][301];\nint A[301], B[301];\nvector, int> > R;\n\nvoid flow2(vector& P, int si, int ti, int d) {\n\tint i;\n\tif (si == ti) return;\n\tif (A[P[si + 1]] + d <= V) {\n\t\tA[P[si]] -= d;\n\t\tA[P[si + 1]] += d;\n\t\tR.push_back(make_pair(make_pair(P[si], P[si + 1]), d));\n\t\tflow2(P, si + 1, ti, d);\n\t}\n\telse {\n\t\tint d2 = V - A[P[si + 1]];\n\t\tA[P[si]] -= d2;\n\t\tA[P[si + 1]] += d2;\n\t\tif (d2 > 0) R.push_back(make_pair(make_pair(P[si], P[si + 1]), d2));\n\t\tflow2(P, si + 1, ti, d);\n\t\tA[P[si]] -= d - d2;\n\t\tA[P[si + 1]] += d - d2;\n\t\tR.push_back(make_pair(make_pair(P[si], P[si + 1]), d - d2));\n\t}\n}\n\n#define FOR(y, N) for (y = 0; y < N; ++y)\n\nvoid flow(int s, int t, int d) {\n\tint i, j, y;\n\tvector P;\n\tP.push_back(s);\n\tfor (i = s; i != t;) {\n\t\tFOR(y, N) if (mat[i][y] == 1 && mat[y][t] == mat[i][t] - 1) break;\n\t\tP.push_back(i = y);\n\t}\n\n\tflow2(P, 0, P.size() - 1, d);\n}\n\n#define _P printf\n\nvoid solve() {\n\tint f, r, i, j, k, l, x, y;\n\n\tcin >> N >> V >> E;\n\tFOR(i, N) cin >> A[i];\n\tFOR(i, N) cin >> B[i];\n\n\tFOR(x, N) FOR(y, N) mat[x][y] = 9999;\n\tFOR(x, N) mat[x][x] = 0;\n\tFOR(i, E) {\n\t\tcin >> x >> y;\n\t\tmat[x - 1][y - 1] = mat[y - 1][x - 1] = 1;\n\t}\n\tFOR(i, N) FOR(j, N) FOR(k, N) mat[j][k] = min(mat[j][k], mat[j][i] + mat[i][k]);\n\tFOR(x, N) FOR(y, N) {\n\t\tif (mat[x][y] > N) continue;\n\t\tj = min(A[x] - B[x], B[y] - A[y]);\n\t\tif (j <= 0) continue;\n\t\tflow(x, y, j);\n\t}\n\n\tFOR(x, N) if (A[x] != B[x]) return (void)_P(\"NO\\n\");\n\t_P(\"%d\\n\", R.size());\n\tFOR(i, R.size()) _P(\"%d %d %d\\n\", R[i].first.first + 1, R[i].first.second + 1, R[i].second);\n}\n\nint main()\n{\n\tsolve();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "c5e5e926b18b4c96067f951464fb15e5", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n\nusing namespace std;\n\nconst long MAX_N = 1e3 + 1;\n\nlong n, m, h;\nlong aw[MAX_N];\nlong bw[MAX_N];\nvector g[MAX_N];\nbool v[MAX_N];\nvector st;\nvector< pair< pair, long > > ans;\n\nvoid add_edge(long a, long b) {\n g[a].push_back(b);\n g[b].push_back(a);\n}\n\nvoid try_flow(long i, long need) {\n if (i + 1 >= st.size()) return;\n\n long a = st[i], b = st[i + 1];\n\n long can = min(need, h - aw[b]);\n\n if (can > 0) {\n ans.push_back(make_pair(make_pair(a, b), can));\n }\n\n try_flow(i + 1, need);\n\n if (need - can > 0) {\n ans.push_back(make_pair(make_pair(a, b), need - can));\n }\n}\n\nvoid flow() {\n long a = st[0];\n long b = st[st.size() - 1];\n\n long need = min(aw[a] - bw[a], bw[b] - aw[b]);\n\n if (need > 0) {\n try_flow(0, need);\n aw[a] -= need; aw[b] += need;\n }\n}\n\nvoid dfs(long a) {\n v[a] = true; st.push_back(a);\n\n if (aw[a] < bw[a]) flow();\n\n for (size_t i = 0; i < g[a].size(); i++) {\n long b = g[a][i];\n if (!v[b]) dfs(b);\n }\n\n st.pop_back();\n}\n\nint main() {\n cin >> n >> h >> m;\n\n for (long i = 1; i <= n; i++) {\n cin >> aw[i];\n }\n\n for (long i = 1; i <= n; i++) {\n cin >> bw[i];\n }\n\n for (long i = 1; i <= m; i++) {\n long x, y; cin >> x >> y;\n add_edge(x, y);\n }\n\n for (long i = 1; i <= n; i++) {\n if (aw[i] > bw[i]) {\n for (long j = 1; j <= n; j++)\n v[j] = false;\n\n dfs(i);\n }\n }\n\n for (long i = 1; i <= n; i++) {\n if (aw[i] != bw[i]) {\n cout << \"NO\"; return 0;\n }\n }\n\n cout << ans.size() << endl;\n for (size_t i = 0; i < ans.size(); i++) {\n cout << ans[i].first.first << \" \" << ans[i].first.second << \" \" << ans[i].second << endl;\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "3866e3a5c0aee23c213b638fa6162ea9", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\n#include\n#include \n#include \n#define PI 3.14159265\n\nusing namespace std;\n\nvector node[301];\nint b[301];\nint c[301];\nint parent[301];\nbool visited[301];\nstack mo;\nstack le;\nvector,int>> ans;\nbool find;\nlong long cpcum;\n\nvoid checksum(int now)\n{\n visited[now] = 1;\n cpcum += (c[now] - b[now]);\n if (c[now] - b[now] > 0) mo.push(now);\n else if (c[now] - b[now] < 0) le.push(now);\n for (int i = 0; i < node[now].size(); i++)\n {\n int next = node[now][i];\n if (visited[next]) continue;\n checksum(next);\n }\n}\n\nbool dfs(int now, int s, int t)\n{\n for (int i = 0; i < node[now].size(); i++)\n {\n int next = node[now][i];\n if (parent[next] != -1) continue;\n parent[next] = now;\n if (next == t)\n {\n return true;\n }\n if (dfs(next, s, t)) return true;\n }\n return false;\n}\n\n\nvoid write(int s, int t,int d,int v)\n{\n if (parent[t] == s)\n {\n ans.push_back(make_pair(make_pair(s, t), d));\n c[s] -= d;\n c[t] += d;\n return;\n }\n else if (c[parent[t]] >= d)\n {\n c[t] += d;\n c[parent[t]] -= d;\n ans.push_back(make_pair(make_pair(parent[t],t), d));\n write(s, parent[t], d, v);\n }\n else\n {\n ans.push_back(make_pair(make_pair(parent[t],t), c[parent[t]]));\n int temp = c[parent[t]];\n c[parent[t]] = 0;\n write(s, parent[t], d ,v);\n ans.push_back(make_pair(make_pair(parent[t], t), d - temp));\n c[t] += d;\n c[parent[t]] = temp;\n\n }\n\n}\n\nint main() {\n \n std::ios_base::sync_with_stdio(false);\n\n int n, v, e;\n cin >> n >> v >> e;\n int sum = 0;\n for (int i = 1; i <= n; i++)\n {\n cin >> c[i];\n sum += c[i];\n }\n for (int i = 1; i <= n; i++)\n {\n cin >> b[i];\n sum -= b[i];\n }\n if (sum != 0)\n {\n cout << \"NO\" << endl;\n return 0;\n }\n\n for (int i = 0; i < e; i++)\n {\n int u, v;\n cin >> u >> v;\n node[u].push_back(v);\n node[v].push_back(u);\n }\n\n memset(visited, false, sizeof(visited));\n for (int i = 1; i <= n; i++)\n {\n if (visited[i] == false)\n {\n cpcum = 0;\n checksum(i);\n if (cpcum != 0)\n {\n cout << \"NO\" << endl;\n return 0;\n } \n while (mo.size() > 0)\n {\n int s = mo.top();\n int t = le.top(); \n //cout << s << \" \" << t << endl;\n for (int i = 0; i <= n; i++) parent[i] = -1;\n parent[s] = 0;\n dfs(s, s, t);\n int d = min(c[s] - b[s], b[t] - c[t]);\n write(s, t, d, v);\n if (c[s] == b[s]) mo.pop();\n if (c[t] == b[t]) le.pop();\n }\n }\n }\n\n\n cout << ans.size() << endl;\n for (int i = 0; i < ans.size(); i++)\n {\n cout << ans[i].first.first << \" \" << ans[i].first.second << \" \" << ans[i].second << endl;\n }\n //system(\"PAUSE\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "87e5908a0f895b42bd41f41b9323d666", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nconst int NMax=400;\nint G[NMax][NMax];\nint N,V,E,A[NMax],B[NMax],father[NMax],father1[NMax];\nvector cur;\nint Find(int a) {\n\tint R;\n\tfor(R=a;father[R]!=-1;R=father[R]);\n\treturn R;\n}\nint F[NMax],have[NMax];\nint DFS(int a,int cnt) {\n\tint last=cnt;have[a]=1;\n\tfor(int i=0;i,int> >Q;\nvoid getans(int a) {\n\tfor(int i=0;i0)Q.push_back(make_pair(make_pair(a,cur[i]),F[cur[i]]));\n\t\t\tgetans(cur[i]);\n\t\t}\n\t}\n\tfor(int i=0;iV){\n\t\t\tif(V-A[cur[i]]>0)Q.push_back(make_pair(make_pair(a,cur[i]),V-A[cur[i]]));\n\t\t\tgetans(cur[i]);\n\t\t\t//printf(\"%d %d %d\\n\",a,cur[i],F[cur[i]]);\n\t\t\tif(F[cur[i]]-(V-A[cur[i]])>0)Q.push_back(make_pair(make_pair(a,cur[i]),F[cur[i]]-(V-A[cur[i]])));\n\t\t}\n\t}\n}\nint main()\n{\n\tmemset(father,-1,sizeof(father));\n\tscanf(\"%d%d%d\",&N,&V,&E);\n\tfor(int i=1;i<=N;i++) scanf(\"%d\",A+i);\n\tfor(int i=1;i<=N;i++) scanf(\"%d\",B+i);\n\tfor(int i=1;i<=E;i++) {\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tG[x][y]=G[y][x]=1;\n\t\tint x1=Find(x),x2=Find(y);\n\t\tif(x1!=x2) father[x1]=x2;\n\t}\n\tfor(int i=1;i<=N;i++)if(father[i]==-1) {\n\t\tlong long cnta=0,cntb=0;\n\t\tcur.clear();\n\t\tfor(int j=1;j<=N;j++) if(Find(j)==i) {\n\t\t\tcur.push_back(j);\n\t\t\tcnta+=A[j];\n\t\t\tcntb+=B[j];\n\t\t}\n\t\tif(cnta!=cntb) {\n\t\t\tputs(\"NO\");\n\t\t\treturn 0;\n\t\t}\n\t\tfor(int j=0;jB[cur[j]]){\n\t\t\tmemset(have,0,sizeof(have));\n\t\t\tmemset(father1,-1,sizeof(father1));\n\t\t\tDFS(cur[j],A[cur[j]]-B[cur[j]]);\n\t\t\tgetans(cur[j]);\n\t\t\tA[cur[j]]=B[cur[j]];\n\t\t}\n\t}\t\n\tprintf(\"%d\\n\",(int)Q.size());\n\tfor(int i=0;i\n#include\n#include\n#include\n#include\n#define N 300\n#define LL long long\nusing namespace std;\nint a[N],b[N];\nint g[N][N];\n\nint vis[N],cs;\nint p[N],l;\nint n;\n\nbool path(int v){\n\tvis[v]=cs;\n\tp[++l]=v;\n\tif( b[v]>a[v] ) return 1;\n\t\n\tfor(int i=0;i ans;\nchar buff[50];\nvoid flow(int i,int flw){\n\n\tint v=p[i],u=p[i+1];\n\tif(a[v]b[v])\n\t\t\t\tq[++bk]=v;\t\n\t\t}\n\t\tprintf(\"%d\\n\",ans.size());\n\t\tfor(int i=0;i\n#include \n#include \nusing namespace std;\n#define N 333\n#define INF 1000000000\n\nint abs(int a) { return a>0?a:-a;}\n\nint n, m, dis[N][N], a[N], b[N];\nvector vs, vd, vt;\n\ninline void save(int v, int u, int t) {\n\tvs.push_back(v+1);\n\tvd.push_back(u+1);\n\tvt.push_back(t);\n}\n\nvoid transfer(int v, int u, int t) {\n\tif(dis[v][u]==1) {\n\t\tsave(v, u, t);\n\t\treturn;\n\t}\n\tfor(int i=0;i>n>>m>>e;\n\tfor(i=0;i>a[i];\n\tfor(i=0;i>b[i];\n\tfor(i=0;i>x>>y;\n\t\tdis[x-1][y-1] = dis[y-1][x-1] = 1;\n\t}\n\tfor(k=0;k\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int max_n = 300 + 10;\n\nint n, w, m;\nint par[max_n], seen[max_n], g;\nbool mark[max_n];\nvector\tadj[max_n];\nvector\tgec, sec, vec;\nll sum1, sum2;\nint a[max_n], b[max_n];\nvector, int>>\tans;\n\nvoid dfs(int v, int p = -1){\n\tmark[v] = 1;\n\tsum1 += a[v];\n\tsum2 += b[v];\n\tseen[v] = g;\n\tpar[v] = p;\n\tfor (int u:adj[v])\n\t\tif (seen[u] == 0)\n\t\t\tdfs(u, v);\n}\n\nvoid flow(int s, int val){\n\tif (s == gec.size() - 1)\treturn;\n\tint rem = w - a[gec[s + 1]];\n\tint p1 = min(val, rem);\n\tans.push_back({{gec[s], gec[s + 1]}, p1});\n\ta[gec[s]] -= p1;\n\ta[gec[s + 1]] += p1;\n\tflow(s + 1, val);\n\n\tans.push_back({{gec[s], gec[s + 1]}, val - p1});\n\ta[gec[s]] -= val - p1;\n\ta[gec[s + 1]] += val - p1;\n}\n\nvoid get_path(int v){\n\tif (par[v] == -1){\n\t\tgec.push_back(v);\n\t\treturn;\n\t}\n\tget_path(par[v]);\n\tgec.push_back(v);\n}\n\nint main(){\n\tscanf(\"%d%d%d\", &n, &w, &m);\n\tfor (int i = 0; i < n; i++)\t\tscanf(\"%d\", &a[i]);\n\tfor (int i = 0; i < n; i++)\t\tscanf(\"%d\", &b[i]);\n\n\tfor (int i = 0; i < m; i++){\n\t\tint a, b;\tscanf(\"%d%d\", &a, &b);\ta--, b--;\n\t\tadj[a].push_back(b);\n\t\tadj[b].push_back(a);\n\t}\n\n\tfor (int i = 0; i < n; i++){\n\t\tif (mark[i])\tcontinue;\n\t\tg++;\n\t\tsum1 = sum2 = 0;\n\t\tdfs(i);\n\t\tif (sum1 != sum2){\n\t\t\tprintf(\"NO\\n\");\n\t\t\treturn 0;\n\t\t}\n\t\t\n\t\tvec.clear();\n\t\tsec.clear();\n\t\tfor (int j = 0; j < n; j++){\n\t\t\tif (seen[j] != g)\tcontinue;\n\t\t\tif (a[j] > b[j])\tvec.push_back(j);\n\t\t\tif (a[j] < b[j])\tsec.push_back(j);\n\t\t}\n\n\t\tfor (int j = 0; j < vec.size(); j++){\n\t\t\twhile (a[sec.back()] == b[sec.back()])\tsec.pop_back();\n\t\t\tint f = vec[j], gg = sec.back();\n\t\t\tint dif = min(a[f] - b[f], b[gg] - a[gg]);\n\n\t\t\tgec.clear();\n\t\t\tmemset(seen, 0, sizeof(seen));\n\t\t\tdfs(f);\n\t\t\tget_path(gg);\n\t\t\tflow(0, dif);\n\t\t\tif (a[vec[j]] != b[vec[j]])\tj--;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\", (int)ans.size());\n\tfor (int i = 0; i < ans.size(); i++)\n\t\tprintf(\"%d %d %d\\n\", ans[i].first.first + 1, ans[i].first.second + 1, ans[i].second);\n\treturn 0;\n}\t\n", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms"], "code_uid": "00dbdbe73737d8fecf314389efd1266b", "src_uid": "0939354d9bad8301efb79a1a934ded30", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define X first\n#define Y second\n#define mp make_pair\n#define pb push_back\n#define mset(a) memset(a,0,sizeof(a))\n#define mmset(a) memset(a,-1,sizeof(a))\n#define mcpy(a,b) memcpy(a,b,sizeof(a))\n\ntypedef double lf;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef vector vi;\n\nconst int inf=1000000007;\nconst ll linf=1000000000000000000LL;\nconst lf eps=0.000001;\nconst lf pi=3.14159265358979323846;\n\ntemplate T abs(T k){return k>=0?k:-k;}\ntemplate T sqr(T n){return n*n;}\ntemplate T gcd(T a,T b){return b?gcd(b,a%b):a;}\ntemplate T mod(T a,T b){return (a%b+b)%b;}\ntemplate T addmod(T a,T b,T c){return (T)mod((ll)a+b,(ll)c);}\ntemplate T mulmod(T a,T b,T c){return (T)mod((ll)a*b,(ll)c);}\ntemplate T powmod(T a,T b,T c){return (T)(b?mulmod(mod((T)sqr((ll)powmod(a,b>>1,c)),c),b&1?a:1,c):1);}\ntemplate void maxe(T &a,T b){a=max(a,b);}\ntemplate void mine(T &a,T b){a=min(a,b);}\ntemplate void mode(T &a,T b){a=mod(a,b);}\ntemplate void addmode(T &a,T b,T c){a=addmod(a,b,c);}\ntemplate void mulmode(T &a,T b,T c){a=mulmod(a,b,c);}\ntemplate void powmode(T &a,T b,T c){a=powmod(a,b,c);}\n\nvoid fileio_in_out(){freopen(\".in\",\"r\",stdin);freopen(\".out\",\"w\",stdout);}\nvoid fileio_txt(){freopen(\"input.txt\",\"r\",stdin);freopen(\"output.txt\",\"w\",stdout);}\n\n//==================================================\n\nconst int N=333;\n\nint test;\nint n,m,k,ans;\nint a[N],b[N];\npii p[N];\nint x,y;\nvector e[N];\nint flag[N],ok[N];\npair step[2*N*N];\n\nint check()\n{\n for(int i=1;i<=n;i++)\n if(a[i]!=b[i])\n return 0;\n return 1;\n}\n\nvoid trans(int x,int y,int d)\n{\n step[ans++]=mp(mp(x,y),d);\n a[x]-=d;\n a[y]+=d;\n}\n\nvoid dfs(int x,int w)\n{\n flag[x]=1;\n w-=a[x];\n for(int i=0;i0&&!flag[y])\n {\n if (!ok[y])\n {\n dfs(y,w);\n int d=min(w,a[y]);\n w-=d;\n trans(y,x,d);\n }\n else\n {\n trans(y,x,w);\n dfs(y,b[y]);\n w=0;\n if(a[y]b;\n}\n\nint main()\n{\n //fileio_in_out();\n //fileio_txt();\n \n scanf(\"%d%d%d\",&n,&k,&m);\n for(int i=1;i<=n;i++)\n scanf(\"%d\",&a[i]);\n for(int i=1;i<=n;i++)\n {\n scanf(\"%d\",&b[i]);\n p[i]=mp(b[i],i);\n }\n while(m--)\n {\n scanf(\"%d%d\",&x,&y);\n e[x].pb(y);\n e[y].pb(x);\n }\n \n sort(p+1,p+1+n,cmp);\n for(int i=1;i<=n;i++)\n {\n mset(flag);\n dfs(p[i].Y,p[i].X);\n ok[p[i].Y]=1;\n }\n \n if(check())\n {\n printf(\"%d\\n\",ans);\n for(int i=0;i\nusing namespace std;\n\nconst int maxn=3005;\nstruct node{\n long long r,l;\n node(long long r,long long l):r(r),l(l){}\n node():r(),l(){}\n bool operator<(const node& o) const{\n if(r==o.r) return l E[maxn];\nlong long siz[maxn],w[maxn],b[maxn];\nint n,m;\n\nvoid dfs(int x,int fa){\n dp[x][1]=node(0,w[x]-b[x]);\n siz[x]=1;\n for(auto i:E[x]){\n if(i==fa) continue;\n dfs(i,x);\n for(int j=1;j<=m;j++) g[j]=node(-1,0);\n for(int j=1;j<=siz[x];j++){\n for(int k=1;k<=siz[i];k++){\n node t1,t2;\n t1=t2=dp[i][k];\n if(t2.l>0) t2.r++;\n t2.l=0;\n g[j+k-1]=max(g[j+k-1],dp[x][j]+t1);\n g[j+k]=max(g[j+k],dp[x][j]+t2);\n }\n }\n siz[x]+=siz[i];\n for(int j=1;j<=siz[x];j++) dp[x][j]=g[j];\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int T;\n cin>>T;\n while(T--){\n cin>>n>>m;\n for(int i=1;i<=n;i++){\n E[i].clear();\n for(int j=1;j<=m;j++)\n dp[i][j]=node(-1,0);\n }\n for(int i=1;i<=n;i++) cin>>b[i];\n for(int i=1;i<=n;i++) cin>>w[i];\n for(int i=1;i>x>>y;\n E[x].push_back(y);\n E[y].push_back(x);\n }\n dfs(1,0);\n cout<0)<<'\\n';\n }\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "dp", "trees"], "code_uid": "9cfb4131861194bff18a2245489d76b4", "src_uid": "d2d1d8c8532b652f172e87a151adae4f", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define MAXN 1000000\n#define MAXK 5000\n#define MAXPRIME 669\nusing namespace std;\nusing LL = long long int;\nint n;\nint k_count[MAXK+1] = {0}; // k_count[k] == the number of k from the inputs\nint prime_factorized_count[MAXK+1][MAXPRIME] = {{0}}; // prime_factorized_count[k] == the prime decomposition\nint omega[MAXK+1] = {0}; // Big prime omega function: omega[i] = the number of prime divisors of i!\n\n// You can also compute these by https://www.geeksforgeeks.org/sieve-of-eratosthenes/\nint primes[MAXPRIME] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999};\n\nvoid debug(){\n for(int i = 0; i <= 100; i++){\n printf(\"%d! == \", i);\n for(int j = 0; j < MAXPRIME; j++){\n if(prime_factorized_count[i][j] != 0) printf(\"%d^%d*\", primes[j], prime_factorized_count[i][j]);\n }\n printf(\", omega == %d\\n\", omega[i]);\n }\n printf(\"omega[5000] == %d\\n\", omega[MAXK]);\n}\n\n// prepare 0!, 1!, ..., 5000!'s prime factorization table\n// complexity: O(MAXK) * O(MAXK lnln (MAXK))\nvoid dp_factorization(){\n // no need to compute i == 0, 1\n // compute [2, MAXK]\n for(int i = 2; i <= MAXK; i++){\n // back-up dp fashion: copy up to [0, i) (O(MAXK))\n copy(prime_factorized_count[i-1], prime_factorized_count[i-1]+MAXPRIME, prime_factorized_count[i]);\n omega[i] = omega[i-1];\n // factor i\n int current_number = i;\n // this will be bounded by O(MAXK ln ln (MAXK)): see the bottom of http://mathworld.wolfram.com/PrimeFactor.html\n for(int j = 0, prime; j < MAXPRIME && current_number != 1; j++){\n prime = primes[j];\n while(current_number % prime == 0){\n current_number /= prime;\n prime_factorized_count[i][j] += 1; // increment prime count\n omega[i] += 1; // increment the number of prime divisors\n }\n }\n }\n}\n\n// pop the largest prime divisor and decide which branch to descend or stop\nbool pop_and_descend(int current_max_prime_index_pointers[MAXK+1], \n int weighted_prime_count[MAXPRIME], bool is_under_this_branch[MAXK+1], \n vector> &prime2ks, LL &total, int &parent_prime_branch_idx){\n assert(prime2ks.size() == MAXPRIME+1); // allow -1 index\n // loop over each k to pop the largest prime divisor for each k and record each prime contains what k 's: O(MAXK)\n for(int i = 0; i <= MAXK; i++){\n if(not is_under_this_branch[i]) continue; // do not consider k that is not on this branch anymore\n if(k_count[i] == 0) continue;\n\n int max_prime_idx = current_max_prime_index_pointers[i];\n prime2ks[max_prime_idx+1].push_back(i); // prime -> a vector of what k is on this prime branch\n\n if(max_prime_idx == -1) continue; // because this number is running out of prime divisors\n assert(max_prime_idx < MAXPRIME);\n\n weighted_prime_count[max_prime_idx] += k_count[i]; // record how many k is under this `max_prime_idx` branch\n\n // O(MAXK ln ln (MAXK)): the estimated quantity of prime divisors\n prime_factorized_count[i][max_prime_idx]--; // decrement because of a pop operation\n // if it is zero, search a smaller prime divisor index for this k\n if(prime_factorized_count[i][max_prime_idx] == 0){\n current_max_prime_index_pointers[i] = -1; // default is empty\n for(int j = max_prime_idx; j >= 0; j--){\n if(prime_factorized_count[i][j] != 0){\n current_max_prime_index_pointers[i] = j;\n break;\n }\n }\n }\n }\n // find the best prime branch to descend\n int best_prime_branch_idx = -1;\n int best_count = -1;\n assert(parent_prime_branch_idx < MAXPRIME);\n // search from the parent_prime_branch_idx (because you cannot choose a child prime branch that is strictly greater than parent prime)\n for(int i = parent_prime_branch_idx; i >= 0; i--){\n // the weight must exceed a half of total number of k\n // and we choose the branch that can decrease the most\n if(weighted_prime_count[i] > (n / 2) && weighted_prime_count[i] > best_count){\n best_count = weighted_prime_count[i];\n best_prime_branch_idx = i;\n }\n }\n\n // if there is a branch to descend\n if(best_prime_branch_idx != -1){\n // branch best_prime_branch_idx will decrease by 1 but other will increase by one\n total = total - (LL)best_count + (LL)(n - best_count);\n // record current prime branch's index\n parent_prime_branch_idx = best_prime_branch_idx;\n // mark other k s as not under this branch anymore: O(MAXK) * O(MAXK)\n for(int i = -1; i < MAXPRIME; i++){\n // i == -1, denote that prime(idx) is running out of prime divisors\n if(i != best_prime_branch_idx){\n for(auto k: prime2ks[i+1]){\n if(is_under_this_branch[k]){\n // after moving down a prime, we should mark this k as not under this branch anymore\n is_under_this_branch[k] = false;\n }\n }\n }\n }\n return true;\n }\n // if there is no branch to descend\n else{\n // signal ending\n return false;\n }\n}\nLL initialize_total_distance(){\n LL total = 0;\n for(int i = 0; i <= MAXK; i++){\n total += (LL)k_count[i] * (LL)omega[i]; // the number of multiplicity * the number of prime divisors ( == the depth of the tree)\n }\n return total;\n}\n\nLL solve(){\n int weighted_prime_count[MAXPRIME] = {0};\n int current_max_prime_index_pointers[MAXK+1]; // current_max_prime_index_pointers[i] = the index to the maximal prime divisor\n vector> prime2ks(MAXPRIME+1); // f(prime) = a vector of k s\n bool is_under_this_branch[MAXK+1];\n fill(is_under_this_branch, is_under_this_branch + MAXK+1, true); // At first, all nodes are under same branch\n\n // initialize prime factoriazation table\n dp_factorization();\n // initialize current_max_prime_index_pointers\n for(int i = 0; i <= MAXK; i++){\n current_max_prime_index_pointers[i] = -1; // default value, denoting empty\n // search from the largest number\n for(int j = MAXPRIME-1; j >= 0; j--){\n if(prime_factorized_count[i][j] != 0){\n current_max_prime_index_pointers[i] = j;\n break;\n }\n }\n }\n // initialize the total distance\n LL total = initialize_total_distance();\n int parent_prime_branch_idx = MAXPRIME-1; // initialize to the maximum prime index\n bool ret;\n\n // descend until there is no improvement\n while(true){\n ret = pop_and_descend(current_max_prime_index_pointers, weighted_prime_count, \n is_under_this_branch, prime2ks, total, parent_prime_branch_idx);\n if(!ret) break;\n fill(weighted_prime_count, weighted_prime_count + MAXPRIME, 0);\n // clear each vector\n for(auto &vec: prime2ks){\n vec.clear();\n }\n }\n return total;\n}\n\nint main(){\n cin >> n;\n int input_k;\n for(int i = 0; i < n; i++){\n cin >> input_k;\n k_count[input_k]++; // increment the count of input_k\n }\n\n LL answer = solve();\n printf(\"%lld\\n\", answer);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "trees", "dp", "graphs", "number theory"], "code_uid": "13d372a2ca66ea771bf575962950ad20", "src_uid": "40002052843ca0357dbd3158b16d59f4", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\nconst int maxn = 1600;\n\nstruct point {\n\tint x, y, pos;\n};\n\npoint a[maxn], Global;\nint ans[maxn], cnt[maxn], n;\nvector g[maxn];\n\nlong long sqr(long long x) {\n\treturn x * 1ll * x;\n}\n\nlong long vect(point p1, point p2, point p3) {\n\treturn (p2.x - p1.x) * 1ll * (p3.y - p1.y) - (p3.x - p1.x) * 1ll * (p2.y - p1.y);\n}\n\nlong long len(point p1, point p2) {\n\treturn sqr(p1.x - p2.x) + sqr(p1.y - p2.y);\n}\n\nbool cmp(point p1, point p2) {\n\tif (vect(Global, p1, p2) > 0 || (vect(Global, p1, p2) == 0 && len(Global, p1) < len(Global, p2)))\n\t\treturn true;\n\treturn false;\n}\n\nvoid rec(int l, int v, int p) {\n\tint u = l;\n\t\n\tfor (int i = l; i < l + cnt[v]; ++i)\n\t\tif (a[i].y < a[u].y || (a[i].y == a[u].y && a[i].x < a[u].x))\n\t\t\tu = i;\n\n\tswap(a[u], a[l]);\n\tGlobal = a[l];\n\tsort(a + l + 1, a + l + cnt[v], cmp);\n\n\tans[a[l].pos] = v;\n\t\n\tint pref = 1, to;\n\t \t\n\tfor (int i = 0; i < (int)g[v].size(); ++i) {\n\t\tto = g[v][i];\n\t\tif (to == p)\n\t\t\tcontinue;\n\t\trec(l + pref, to, v);\n\t\tpref += cnt[to];\n\t}\n\t\n\treturn;\n}\n\nvoid dfs(int v, int p) {\n\tcnt[v] = 1;\n\n\tint to;\n\t\n\tfor (int i = 0; i < (int)g[v].size(); ++i) {\n\t\tto = g[v][i];\n\t\tif (to == p)\n\t\t\tcontinue;\n\t\tdfs(to, v);\n\t\tcnt[v] += cnt[to];\n\t}\n\t\n\treturn;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\t\n\tcin >> n;\n\n\tint u, v;\n\n\tfor (int i = 1; i < n; ++i) {\n\t\tcin >> u >> v;\n\t\tg[u].pb(v);\n\t\tg[v].pb(u);\n\t}\n\n\tfor (int i = 1; i <= n; ++i) {\n\t\tcin >> a[i].x >> a[i].y;\n\t\ta[i].pos = i;\n\t}\n\n\tdfs(1, -1);\n\n\trec(1, 1, -1);\n\n\tfor (int i = 1; i <= n; ++i)\n\t\tcout << ans[i] << ' ';\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "a2a3e3388c0a8d11ec2371b1c30ce6f3", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define int long long\nusing namespace std;\n\nconst int N=1505;\nint n,tot;\nint lnk[N],nxt[2*N],son[2*N],sz[N],ans[N];\nstruct myt{\n int x,y,id;\n}dot[N],t;\n\nint read(){\n int s=0,w=1;\n char ch=getchar();\n while (ch<'0'||ch>'9') {if (ch=='-') w=-1;ch=getchar();}\n while (ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();\n return s*w;\n}\n\ninline void add(int a,int b){son[++tot]=b;nxt[tot]=lnk[a];lnk[a]=tot;}\n\ninline int cross(myt a,myt b,myt c){\n return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);\n}\n\nbool cmp(myt a,myt b){\n return cross(t,a,b)>0;\n}\n\nvoid dfs_size(int s,int fa){\n sz[s]=1;\n int j=lnk[s];\n while (j){\n if (son[j]==fa) {j=nxt[j];continue;}\n dfs_size(son[j],s);\n sz[s]+=sz[son[j]];\n j=nxt[j];\n }\n return;\n}\n\nvoid dfs_ans(int l,int r,int rt,int fa){\n int x=l;\n for (int i=l+1;i<=r;++i)\n if (dot[i].x\n#include\n#include\n#include\n#define N 2000\n#define ll long long\nusing namespace std;\nll n;\nstruct sett{\n ll x,y,id;\n}p[N],plk;\nstruct zero{\n ll nxt,to;\n}edge[N<<1];\nll tot=0,head[N];\nll size[N];\nll Ans[N];\nvoid add_edge(ll a,ll b){\nedge[++tot]=(zero){head[a],b};\nhead[a]=tot;\n}\nvoid aux(ll x,ll fa){\n size[x]=1;\n for(ll i=head[x];i;i=edge[i].nxt)\n {\n ll to=edge[i].to;\n if(to==fa)continue;\n aux(to,x);\n size[x]+=size[to];\n }\n}\nbool cmp(sett a,sett b)\n{\n ll ax=a.x-plk.x,ay=a.y-plk.y,bx=b.x-plk.x,by=b.y-plk.y;\n if(ax>=0&&bx<=0)return 1;\n if(ax<=0&&bx>=0)return 0;\n return (ax*by>ay*bx);\n}\nvoid solve(ll l,ll r,ll x,ll fa){\n ll poss=l;\n for(ll i=l+1;i<=r;i++)\n if(p[i].x\n#define int long long\nusing namespace std;\nstruct cd\n{\n int x,y,id;\n} a[500005],xx;\nstruct ab\n{\n int f,t;\n} t[500005];\nint h,hh[500005],size[500005],ans[500005];\nvoid make(int x,int y)\n{\n t[++h].f=hh[x];\n t[h].t=y;\n hh[x]=h;\n}\nint cmp(cd x,cd y)\n{\n if(x.x-xx.x>0&&y.x-xx.x<0) return 1;\n if(x.x-xx.x<0&&y.x-xx.x>0) return 0;\n return (x.x-xx.x)*(y.y-xx.y)>(x.y-xx.y)*(y.x-xx.x);\n}\nvoid dfs1(int x,int y)\n{\n size[x]=1;\n for(int i=hh[x];i;i=t[i].f)\n {\n int j=t[i].t;\n if(j==y) continue;\n dfs1(j,x);\n size[x]+=size[j];\n }\n}\nvoid dfs2(int l,int r,int x,int y)\n{\n for(int i=l+1;i<=r;i++)\n {\n if(a[i].x\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define int long long\n#ifdef __LOCALE__\n#define see(a) std::cout << #a << \"=\" << a << std::endl\n#else\n#define see(a) //std::cout << #a << \"=\" << a << std::endl\n#endif\n#define foreach(a,b) for (int a=0;a<(int)b.size();a++)\n#define rep(i,n) for (int i=0;i> (istream& in , Point& X) {\n in >> X.x >> X.y;\n return in;\n}\n\nconst int N = 3005;\nvector < int > e[N];\nint size[N];\nint ans[N];\nPoint pt[N];\nint n;\n\nvoid init(){\n scanf(\"%lld\",&n);\n rep (i , n - 1) {\n int u,v;\n scanf(\"%lld%lld\",&u,&v);\n e[u].push_back(v);\n e[v].push_back(u);\n }\n repa (i,n) {\n cin >> pt[i];\n pt[i].org = i;\n }\n}\n\nvoid Size(int u = 1,int f = 0){ \n size[u] = 1;\n foreach (i,e[u]) {\n int v = e[u][i];\n if (v == f) continue;\n Size(v,u);\n size[u] += size[v];\n }\n}\n\nvoid solve(int l = 1 , int r = n , int u = 1,int f = 0) {\n //cout << l << ' ' << r << ' ' << u << ' ' << f << endl;\n repi (i,l + 1 , r) if (pt[i].x < pt[l].x) swap(pt[l] , pt[i]);\n sx = pt[l].x;\n sy = pt[l].y;\n sort(pt + l + 1, pt + r + 1,cmp);\n //repi (i,l , r) {\n // cout << pt[i].x << ' '<< pt[i].y << ' ' << pt[i].org << endl;\n //}\n ans[ pt[l].org ] = u;\n int fs = l + 1;\n foreach (i,e[u]) {\n int v = e[u][i];\n if (v == f) continue;\n solve(fs , fs + size[v] - 1 , v , u);\n fs += size[v];\n }\n}\n\n#undef int\nint main(){\n init();\n Size(); \n solve();\n repa (i,n) printf(\"%lld \",ans[i]);\n puts(\"\");\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "46b983ec3efd85e1e6e961601879fcd3", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nLL n,tot,h[3005],e[3005],b[3005],s[3005],f[3005];\nstruct Point\n{\n\tLL v,p,x,y;\n}p[3005];\n\nint cmp1(const Point a,const Point b)\n{\n\treturn (a.x*b.y-a.y*b.x)>0;\n}\n\nint cmp2(const Point a,const Point b)\n{\n\treturn (a.p>n;\n\tLL a,b;\n\tfor (LL i=1;i>a>>b;\n\t\tAdd(--a,--b); Add(b,a);\n\t}\n\tDfs(0);\n\tfor (LL i=0;i>p[i].x>>p[i].y,p[i].p=i;\n\tRec(0,0,n-1);\n\tsort(p,p+n,cmp2);\n\tfor (LL i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#if LOCAL\n\t#define DO_NOT_SEND\n#endif\n\ntypedef long long LL;\n\nint IntMaxVal = (int) 1e20;\nint IntMinVal = (int) -1e20;\nLL LongMaxVal = (LL) 1e20;\nLL LongMinVal = (LL) -1e20;\n\n#define FOR(i, a, b) for(int i = a; i < b ; ++i)\n#define FORD(i, a, b) for(int i = a; i >= b; --i)\n\n#define minimize(a, b) { a = min(a, b); }\n#define maximize(a, b) { a = max(a, b); }\n\n#define all(v) v.begin(),v.end()\n\nusing namespace std;\n\n#define endl '\\n';\n#define next(type, i) type i; cin >> i;\ntemplate struct argument_type;\ntemplate struct argument_type { typedef U type; };\n#define nextVector(t, v, size) vector< argument_type::type > v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }\n\n#define range(name, start, count) vector name(count); { for (int i = 0 ; i < count ; i++) name[i] = i + start; }\n\ntemplate istream& operator >>(istream& is, pair& s) { is >> s.first >> s.second; return is; }\ntemplate ostream& operator << (ostream& os, const vector &v) { for (int i = 0 ; i < v.size() ; i++) os << v[i] << ' '; os << endl; return os; }\ntemplate ostream& operator <<(ostream& s, pair& t) { s << t.first << ' ' << t.second; return s; }\ntemplate vector readVector(int n) { vector res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }\n\nvector > points;\nint n;\nvector parent;\nvector size;\n\nvector mapping;\nvector > edges;\n\nvoid solve(vector &vertices, int root) {\n\tif (vertices.size() != size[root]) {\n\t\tcout << \"Ooops\";\n\t\texit(0);\n\t}\n\tint v = vertices.front();\n\tfor (auto v2 : vertices) if (points[v2] < points[v]) v = v2;\n\tFOR (i, 0, vertices.size()) {\n\t\tif (vertices[i] == v) {\n\t\t\tvertices[i] = vertices.back();\n\t\t\tvertices.pop_back();\n\t\t}\n\t}\n\n\tmapping[v] = root + 1;\n\tif (!vertices.size()) return;\n\t\n\tint y0 = points[v].second;\n\tint x0 = points[v].first;\n\t\n\tvector > v2;\n\tfor (auto vi : vertices) v2.push_back(make_pair(atan2(points[vi].second - y0, points[vi].first - x0), vi));\n\tsort(all(v2));\n\t\n\tint p = 0;\n\tfor (auto dest : edges[root]) if (parent[root] != dest) {\n\t\tvector nextPoints;\n\t\twhile (nextPoints.size() < size[dest]) nextPoints.push_back(v2[p++].second);\n\t\tsolve(nextPoints, dest);\n\t}\n}\n\nint main() {\n\tsrand (time(NULL));\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n\t\n\tcin >> n;\n\tparent.resize(n);\n\tedges.resize(n);\n\tFOR (i, 0, n - 1) {\n\t\tnext(int, a);\n\t\tnext(int, b);\n\t\ta--;\n\t\tb--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\t}\n\t\n\tvector q;\n\tq.push_back(0);\n\tmapping.resize(n);\n\tparent[q[0]] = -1;\n\t\n\tFOR (i, 0, n) {\n\t\tint v = q[i];\n\t\tfor (auto e : edges[v]) if (e != parent[v]) {\n\t\t\tparent[e] = v;\n\t\t\tq.push_back(e);\n\t\t}\n\t}\n\t\t\n\tpoints = readVector >(n);\n\t\n\tsize.assign(n, 1);\n\tFORD (i, n - 1, 0) {\n\t\tint v = q[i];\n\t\tif (parent[v] != -1) size[parent[v]] += size[v];\n\t}\n\t\n\tvector vertices(n);\n\tFOR (i, 0, n) vertices[i] = i;\n\tsolve(vertices, q[0]);\n\n\tcout << mapping;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "ee836f33381b0ce8493b1609fab3d2ec", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nconst int maxn=1555;\nvectorv[maxn];\nint X,Y,n;\nint sz[maxn];\nbool vis[maxn];\nstruct Po{\n int x,y,id;\n bool operator <(const Po &o)const {\n if(x-X>=0&&o.x-X<=0) return 1;\n if(x-X<=0&&o.x-X>=0) return 0;\n return (y-Y)*(long long )(o.x-X)<(o.y-Y)*(long long )(x-X);\n }\n}p[maxn];\nint dfs(int x){\n vis[x]= true;\n sz[x]=1;\n int res=0;\n for(int i=0;i\n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\n#define pb push_back\nconst int N = 1510;\nstruct Node {\n\tint x, y, id;\n\tNode operator - (const Node &b) const {\n\t\treturn (Node) {x - b.x, y - b.y, 0};\n\t}\n\tbool operator < (const Node &b) const {\n\t\treturn x < b.x || (x == b.x && y < b.y);\n\t}\n} a[N], cur;\nll cross(const Node &a, const Node &b) {\n\treturn (ll) a.x * b.y - (ll) a.y * b.x;\n}\nbool cmp(const Node &a, const Node &b) {\n\treturn cross(a - cur, b - cur) < 0;\n}\nint n, sz[N], ans[N];\nvector G[N];\nvoid dfs0(int u, int fa = 0) {\n\tsz[u] = 1;\n\tfor(int v : G[u]) if(v != fa) {\n\t\tdfs0(v, u); sz[u] += sz[v];\n\t}\n}\nvoid dfs(int u, int l, int r, int fa = 0) {\n\tint p = l + 1, nw = l;\n\tfor(int i = l + 1; i <= r; i ++)\n\t\tif(a[nw] < a[i]) nw = i;\n\tif(nw != l) swap(a[nw], a[l]);\n\tans[a[l].id] = u; cur = a[l];\n\tsort(a + l + 1, a + r + 1, cmp);\n\tfor(int v : G[u]) if(v != fa) {\n\t\tdfs(v, p, p + sz[v] - 1, u);\n\t\tp += sz[v];\n\t}\n}\nint main() {\n\tscanf(\"%d\", &n);\n\tfor(int u, v, i = 1; i < n; i ++) {\n\t\tscanf(\"%d%d\", &u, &v);\n\t\tG[u].pb(v); G[v].pb(u);\n\t}\n\tfor(int i = 1; i <= n; i ++) {\n\t\tscanf(\"%d%d\", &a[i].x, &a[i].y);\n\t\ta[i].id = i;\n\t}\n\tdfs0(1); dfs(1, 1, n);\n\tfor(int i = 1; i <= n; i ++)\n\t\tprintf(\"%d%c\", ans[i], \" \\n\"[i == n]);\n\treturn 0;\n}\n\t \t \t\t\t \t \t\t \t \t\t \t\t \t\t//hellobig_tq//hello", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "1ca7f640b93a4b613af93e818ceb4c4e", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define ll long long\n#define M 1505\nusing namespace std;\nint head[M],tot;\nstruct Edge{int nxt,to;}G[M*2];\nvoid addedge(int x,int y){\n\tG[tot]=(Edge){head[x],y};\n\thead[x]=tot++;\n}\nint n,rt;ll X,Y;\nstruct node{\n\tll x,y;int id;\n\tbool operator < (const node &_)const{\n\t\treturn (x-X)*(_.y-Y)>(y-Y)*(_.x-X);\n\t}\n}A[M];\nint sz[M];\nvoid f1(int x,int fa1){\n\tsz[x]=1;\n\tfor(int i=head[x];i!=-1;i=G[i].nxt){\n\t\tint y=G[i].to;\n\t\tif(y==fa1)continue;\n\t\tf1(y,x);\n\t\tsz[x]+=sz[y];\n\t}\n}\nint Ans[M];\nvoid f2(int x,int fa1,int L,int R){\n\trt=0;X=1e18;Y=1e18;\n\tfor(int i=L;i<=R;i++){\n\t\tif(X>A[i].x)rt=i,X=A[i].x,Y=A[i].y;\n\t\telse if(X==A[i].x&&Y>A[i].y)rt=i,Y=A[i].y;\n\t}\n\tAns[A[rt].id]=x;X=A[rt].x;Y=A[rt].y;\n\tswap(A[L],A[rt]);\n\tsort(A+L+1,A+R+1);\n\tint now=0;\n\tfor(int i=head[x];i!=-1;i=G[i].nxt){\n\t\tint y=G[i].to;\n\t\tif(y==fa1)continue;\n\t\tf2(y,x,L+now+1,L+now+sz[y]);\n\t\tnow+=sz[y];\n\t}\n}\nint main(){\n\tmemset(head,-1,sizeof head);\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \nusing namespace std;\n \n#if __GNUC__\n#include \n#include \nusing namespace tr1;\n#else\n#include \n#include \n#endif\n \n \n \n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; }\n#endif\n#ifdef __GNUC__\ntemplate int popcount(T n);\ntemplate <> int popcount(unsigned int n) { return __builtin_popcount(n); }\ntemplate <> int popcount(int n) { return __builtin_popcount(n); }\ntemplate <> int popcount(unsigned long long n) { return __builtin_popcountll(n); }\ntemplate <> int popcount(long long n) { return __builtin_popcountll(n); }\n#endif\n \n#define rep (i, n) for (int i = 0; i < (int)n; ++i)\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\n#define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CL(arr, val) memset(arr, val, sizeof(arr))\n#define MEMCPY(dest, src) memcpy(dest, src, sizeof(src))\n \n \ntemplate void max_swap(T& a, const T& b) { a = max(a, b); }\ntemplate void min_swap(T& a, const T& b) { a = min(a, b); }\n \ntypedef long long ll;\ntypedef pair pint;\n \ntemplate string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\n \nconst double EPS = 1e-8;\nconst double PI = acos(-1.0);\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\nbool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n \ntemplate void print(T a, int n, int br = 1, const string& deli = \", \") { cout << \"{ \"; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << \" }\"; while (br--) cout << endl; }\ntemplate void print(const vector& v, int br = 1, const string& deli = \", \") { print(v, v.size(), br, deli); }\ntemplate void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\n \ntemplate void input(int n, T& a) { for (int i = 0; i < n; ++i) cin >> a[i]; }\ntemplate void input(int n, T& a, U& b) { for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; }\ntemplate vector input(int n) { vector res(n); for (int i = 0; i < n; ++i) cin >> res[i]; return res; }\n\n\ntypedef complex P;\nnamespace std\n{\n bool operator<(const P& a, const P& b)\n {\n return a.imag() != b.imag() ? a.imag() < b.imag() : a.real() < b.real();\n }\n};\nll cross(const P& a, const P& b) { return a.real() * b.imag() - a.imag() * b.real(); }\nstruct Comp\n{\n P base;\n Comp(const P& base)\n : base(base) { }\n bool operator()(const P& a, const P& b) const\n {\n P x = a - base, y = b - base;\n return cross(x, y) > 0;\n }\n};\n\nint n;\nP p[1511];\nvector e[1511];\nmap index;\n\nint size[1511];\nint dfs_size(int v, int prev)\n{\n size[v] = 1;\n for (int i = 0; i < e[v].size(); ++i)\n if (e[v][i] != prev)\n size[v] += dfs_size(e[v][i], v);\n return size[v];\n}\n\nint res[1511];\nvoid build(int v, int prev, P* part)\n{\n P cur = *part;\n res[index[cur]] = v + 1;\n ++part;\n\n sort(part, part + (size[v] - 1), Comp(cur));\n\n for (int i = 0; i < e[v].size(); ++i)\n {\n int to = e[v][i];\n if (to != prev)\n {\n build(to, v, part);\n part += size[to];\n }\n }\n}\nint main()\n{\n ios::sync_with_stdio(false);\n\n cin >> n;\n\n for (int i = 0; i < n - 1; ++i)\n {\n int a, b;\n cin >> a >> b;\n --a, --b;\n e[a].push_back(b);\n e[b].push_back(a);\n }\n for (int i = 0; i < n; ++i)\n {\n ll x, y;\n cin >> x >> y;\n p[i] = complex(x, y);\n index[p[i]] = i;\n }\n\n int root = 0;\n dfs_size(root, -1);\n\n sort(p, p + n);\n build(root, -1, p);\n\n for (int i = 0; i < n; ++i)\n cout << res[i] << \" \";\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "cdac9b03150c8be7b697d88e4114cc48", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n#define REP(i,f,t)for(int i=(f),i##_end_=(t);i<=i##_end_;i++)\n#define SREP(i,f,t)for(int i=(f),i##_end_=(t);i=i##_end_;i--)\n#define db double\n#define LL long long \n#define INF 0x3f3f3f3f\n#define inf 0x3f3f3f3f3f3f3f\n#define Sz(a)sizeof(a)\n#define mcl(a,b)memset(a,b,Sz(a))\n#define mcp(a,b)memcpy(a,b,Sz(b))\n#define pb push_back\ntemplateinline bool chkmin(T&x,T y){return yinline bool chkmax(T&x,T y){return xy?x:y;}\ninline LL Min(LL x,LL y){return xE[N];\nint sz[N];\nstruct point{\n\tint x,y,id;\n\tdb jj;\n}p[N];\nint ans[N];\n\nbool cmp(point a,point b){\n\treturn a.jjr) return;\n int pos=l;\n for(int i=l+1;i<=r;i++) if(p[i].y\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#ifndef DEBUG\n#define DEBUG 0\n#endif\n\n#define debug(x) if (DEBUG) cerr << #x << \" = \" << (x) << endl\n\n#define debug_line(a, n) do { \\\n if (DEBUG) { \\\n cerr << #a << \"[\" << #n << \"]:\" << endl; \\\n for (int i = 0; i < (n); i++) cerr << (a)[i] << \" \"; \\\n cerr << endl; \\\n } \\\n} while (0)\n\n#define debug_mat(a, n, m) do { \\\n if (DEBUG) { \\\n cerr << #a << \"[\" << #n << \"][\" << #m \"]:\" << endl; \\\n for (int i = 0; i < (n); i++) { \\\n for (int j = 0; j < (m); j++) cerr << (a)[i][j] << \" \"; \\\n cerr << endl; \\\n } \\\n } \\\n} while(0)\n\ntypedef pair pii;\ntypedef vector::const_iterator vci;\ntypedef map::const_iterator mci;\n\nconst int INF = 0x3f3f3f3f;\nconst long long LLINF = 0x3f3f3f3f3f3f3f3fll;\nconst double DINF = 1.0/0.0f;\nconst double pi = acos(-1.0);\nconst double eps = 1e-8;\nconst int MOD = 1000000007;\n\ninline int LC(int x) { return x << 1; }\ninline int RC(int x) { return (x << 1) | 1; }\ninline bool eq0(double x) { return fabs(x) < eps; }\ninline bool eq(double x, double y) { return fabs(x - y) < eps; }\ninline bool ls(double x, double y) { return x + eps < y; }\ninline bool gr(double x, double y) { return x - eps > y; }\ninline bool greq(double x, double y) { return x + eps >= y; }\ninline bool lseq(double x, double y) { return x - eps <= y; }\ninline double fmax(double x, double y) { return gr(x, y) ? x : y; }\ninline double fmin(double x, double y) { return ls(x, y) ? x : y; }\n\ntemplate inline T sqr(T x) { return x * x; }\n\nvector edge[2000];\nint ans[2000];\nint size[2000];\n\nstruct Point {\n long long x, y;\n int idx;\n\n Point(long long _x, long long _y, int _idx) : x(_x), y(_y), idx(_idx) { }\n\n bool operator<(const Point &o)\n {\n return y < o.y;\n }\n};\nvector pnt;\n\nclass Compare_Angle {\n private:\n Point base;\n\n public:\n Compare_Angle(const Point &_base) : base(_base) { }\n bool operator()(const Point &a, const Point &b) const\n {\n return (a.x - base.x) * (b.y - base.y) - (b.x - base.x) * (a.y - base.y) > 0;\n }\n};\n\nint make_tree(int x, int parent)\n{\n size[x] = 1;\n for (vci it = edge[x].begin(); it != edge[x].end(); it++) {\n if (*it != parent) {\n size[x] += make_tree(*it, x);\n }\n }\n return size[x];\n}\n\nvoid make_match(int x, int parent, vector::iterator start, vector::iterator end)\n{\n vector::iterator low = min_element(start, end);\n ans[low->idx] = x;\n swap(*start, *low);\n sort(start + 1, end, Compare_Angle(*start));\n\n start++;\n for (vci it = edge[x].begin(); it != edge[x].end(); it++) {\n if (*it != parent) {\n make_match(*it, x, start, start + size[*it]);\n start += size[*it];\n }\n }\n}\n\nint main()\n{\n int n;\n cin >> n;\n for (int i = 1; i < n; i++) {\n int u, v;\n cin >> u >> v;\n edge[u].push_back(v);\n edge[v].push_back(u);\n }\n for (int i = 0; i < n; i++) {\n long long x, y;\n cin >> x >> y;\n pnt.push_back(Point(x, y, i));\n }\n\n make_tree(1, 0);\n make_match(1, 0, pnt.begin(), pnt.end());\n\n for (int i = 0; i < n; i++) cout << ans[i] << endl;\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "8174d875c88d4d3c733a0b71ade5262b", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#define LL long long\n#define M 20000\nusing namespace std;\nstruct Point{\n\tint x,y,id;\n}p[2000];\nvector >vec[2000];\nint t,mix,miy,siz[2000];\nbool cmp(Point a,Point b){\n\tint w=0;\n\tif(a.x>=mix&&b.x<=mix) return 1;\n\tif(a.x<=mix&&b.x>=mix) return 0;\n\t\tif(a.x=1LL*(b.y-miy)*1LL*(mix-a.x);\n\t\t}else{\n\t\t w=1LL*(a.y-miy)*1LL*(b.x-mix)<=1LL*(b.y-miy)*1LL*(a.x-mix);\n\t\t} \n\treturn w;\n}\nint n,x,y,pnt=1,a[M],fi[M],ne[M],la[M],ans[M],tot;\nvoid add(int x,int y){\n\ta[++tot]=y;\n\t!fi[x]?fi[x]=tot:ne[la[x]]=tot;la[x]=tot;\n}\nvoid dfs(int x,int fa){\n\tsiz[x]=1;\n\tfor(int i=fi[x];i;i=ne[i])if(a[i]!=fa){\n\t\tdfs(a[i],x);\n\t\tsiz[x]+=siz[a[i]];\n\t}\n}\nvoid calc(int trt,int l,int r,int fa){\n\t//printf(\"%d %d %d\\n\",pnt,trt,fa);\n\tint pos=l;\n\tfor(int i=l;i<=r;i++)if(p[i].y\n#include \n#define N 1510\nusing namespace std;\ntypedef long long LL;\n\nstruct Node {\n LL to, nxt;\n}e[N << 1];\n\nstruct Point {\n LL x, y, id;\n}p[N], pp[N], poi;\n\nLL cnt, n, lst[N], ans[N], size[N];\n\nbool cmp(Point a, Point b) {//按极角排序\n LL ax = a.x - poi.x, ay = a.y - poi.y, bx = b.x - poi.x, by = b.y - poi.y;\n if (ax >= 0 && bx <= 0) return 1;\n if (ax <= 0 && bx >= 0) return 0;\n return (ax * by > bx * ay);\n}\n\ninline void add(LL u, LL v) {\n e[++cnt].to = v;\n e[cnt].nxt = lst[u];\n lst[u] = cnt;\n}\n\nvoid predfs(LL x, LL fa) {//预处理出子树大小\n size[x] = 1;\n for (int i = lst[x]; i; i = e[i].nxt) {\n if (e[i].to == fa) continue;\n predfs(e[i].to, x);\n size[x] += size[e[i].to];\n }\n}\n\nvoid dfs(LL l, LL r, LL x, LL fa){//对区间进行分治\n LL mini = l;\n for (int i = l + 1; i <= r; ++i)\n if (p[i].y < p[mini].y || p[i].y == p[mini].y && p[i].x < p[mini].x) mini = i;\n swap(p[l], p[mini]);\n ans[p[l].id] = x;\n poi = p[l];\n sort(p + l + 1, p + r + 1, cmp);\n LL now = l + 1;\n for (LL i = lst[x]; i; i = e[i].nxt) {\n if (e[i].to == fa) continue;\n dfs(now, now + size[e[i].to] - 1, e[i].to, x);\n now += size[e[i].to];\n }\n}\n\nint main() {\n scanf(\"%lld\", &n);\n for (LL i = 1; i < n; ++i) {\n LL a, b;\n scanf(\"%lld%lld\", &a, &b);\n add(a, b);\n add(b, a);\n }\n for (LL i = 1; i <= n; ++i) {\n scanf(\"%lld%lld\", &p[i].x, &p[i].y);\n p[i].id = i;\n }\n predfs(1, 1);\n dfs(1, n, 1, 1);\n for (LL i = 1; i < n; ++i) {\n printf(\"%lld \", ans[i]);\n }\n printf(\"%lld\\n\", ans[n]);\n return 0;\n}//", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "949f1ab9c16e2747e39c8a9052562840", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#pragma GCC optimize(\"O2\")\n#include\n#define int long long \n#define N 3000\nusing namespace std;\nstruct Point\n{\n int x,y;int id;\n Point(){}\n Point(double _x,double _y){x=_x,y=_y;}\n}now,p[N];\ntypedef Point Vector ;\nVector operator - (Point a,Point b){return Point(a.x-b.x,a.y-b.y);}\ndouble Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}\nint book[N],ans[N],n,sz[N];\nvector g[N];\nbool cmp(Point a,Point b)\n{\n\treturn Cross(a-now,b-now)>0;\n}\n\nvoid dfs(int x,int fa)\n{\n\tsz[x]=1;\n\tfor(int i=0,v;i>n;\n for(int i=1;i\n#define ll long long\nusing namespace std;\nll n,si[4000];\nll w,ans[4000];\nstruct node\n{\n\tll x,y,wh;\n\tll dx,dy;\n}sh[4000];\nvector e[4000];\nbool operator < (node a,node b)\n{\n\treturn (a.x\n#include \n#include \n#include \n#include \n#include \n#include \n#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)\n#define REP(i,n) for(int i=0;i 0;\n}\n\nstruct Point {\n int64 x, y;\n Point() {\n }\n Point(int64 _x, int64 _y) :\n x(_x), y(_y) {\n }\n Point operator+(const Point&p) const {\n return Point(x + p.x, y + p.y);\n }\n Point operator-(const Point&p) const {\n return Point(x - p.x, y - p.y);\n }\n Point operator*(double d) const {\n return Point(x * d, y * d);\n }\n Point operator/(double d) const {\n return Point(x / d, y / d);\n }\n bool operator<(const Point&p) const {\n return x != p.x ? x < p.x : y < p.y;\n }\n int64 dot(const Point&p) const {\n return x * p.x + y * p.y;\n }\n int64 det(const Point&p) const {\n return x * p.y - y * p.x;\n }\n void read() {\n cin >> x >> y;\n }\n void write() {\n cout << \"(\" << x << \",\" << y << \")\" << endl;\n }\n};\n\n#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))\n\n#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))\n\nPoint isSS(Point p1, Point p2, Point q1, Point q2) {\n double a1 = cross(q1,q2,p1), a2 = -cross(q1,q2,p2);\n return (p1 * a2 + p2 * a1) / (a1 + a2);\n}\n\nPoint ps[MAX_N];\nint n;\n\nbool e[MAX_N][MAX_N];\n\nbool cmp(int a, int b) {\n return ps[a] < ps[b];\n}\n\nint mp[MAX_N];\n\nint RT;\nPoint CT;\n\nbool cmp2(int a, int b) {\n Point A = ps[a], B = ps[b];\n return crossOp(CT,A,B) > 0;\n}\n\nint size[MAX_N];\n\nvoid dfs(int u, int par) {\n size[u] = 1;\n for (int i = 0; i < n; ++i) {\n if (e[u][i] && i != par)\n dfs(i, u), size[u] += size[i];\n }\n}\n\nvoid work(int rt, int par, vector cur) { // return root\n //take the left-lowerest point\n int low = *min_element(cur.begin(), cur.end(), cmp);\n cur.erase(find(cur.begin(), cur.end(), low));\n\n mp[low] = rt;\n\n vector ch;\n for (int i = 0; i < n; ++i) {\n if (e[rt][i] && i != par)\n ch.push_back(i);\n }\n\n CT = ps[low];\n\n sort(cur.begin(), cur.end(), cmp2);\n\n int at = 0;\n\n for (vector::iterator e = ch.begin(); e != ch.end(); ++e) {\n work(*e, rt, vector(cur.begin() + at, cur.begin() + at + size[*e]));\n at += size[*e];\n }\n}\n\nint main() {\n cin >> n;\n memset(e, 0, sizeof e);\n for (int i = 0; i < n - 1; ++i) {\n int a, b;\n cin >> a >> b;\n --a, --b;\n e[a][b] = e[b][a] = true;\n }\n for (int i = 0; i < n; ++i) {\n ps[i].read();\n }\n dfs(0, -1);\n\n vector cur;\n for (int i = 0; i < n; ++i) {\n cur.push_back(i);\n }\n\n work(0, -1, cur);\n\n for (int i = 0; i < n; ++i) {\n printf(\"%d \", mp[i] + 1);\n }\n puts(\"\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "e8eb0e48320017a27a5b732bc97b145b", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nconst int INF=2000000010;\n\nstruct point{\n\tint x,y,num;\n};\n\nbool operator<(point a, point b){\n\treturn b.x==0 || (a.x>0 && 1ll*a.x*b.y>1ll*a.y*b.x);\n}\n\nstruct vertex{\n\tint subtree,num;\n\tvector next;\n};\n\nint N;\nvertex g[1510]; //our tree\npoint pnt[1510]; //points\nint ans[1510]; //answer\nvector v; //points that we work with\n\nvoid precalc_dfs(int v, int p=-1){ //precalculating subtree values\n\tg[v].subtree=1;\n\tfor (int i=0; i a, int from, int p=-1){\n\tint n=a.size();\n\tint mini=-1,minx=INF,miny=INF;\n\tfor (int i=0; i b;\n\t\t\tfor (int j=0; j\n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long LL;\ntypedef double DB;\ntypedef pair PII;\nconst int MAXN = 1510;\nconst LL INF = 1e15;\n\ninline LL in()\n{\n LL x = 0, flag = 1; char ch = getchar();\n while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); }\n while (ch >='0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();\n return x * flag;\n}\n\nint n, m;\n\nstruct Adj\n{\n int nex, to;\n} adj[MAXN << 1];\nint head[MAXN], nume;\nvoid addedge(int from, int to)\n{\n adj[++nume] = (Adj) { head[from], to };\n head[from] = nume;\n}\nint siz[MAXN];\nvoid DFS(int u, int pre)\n{\n// printf(\"u : %d\\n\", u);\n siz[u] = 1;\n for (int i = head[u]; i; i = adj[i].nex)\n {\n int v = adj[i].to;\n if (v == pre) continue;\n// printf(\"%d --> %d\\n\", u, v);\n DFS(v, u);\n siz[u] += siz[v];\n }\n}\n\nstruct Poi\n{\n LL x, y; int id;\n bool operator < (const Poi b) const { return x == b.x ? y < b.y : x < b.x; }\n} poi[MAXN], base;\nbool cmp(Poi a, Poi b)\n{\n// printf(\"%lld / %lld ? %lld / %lld %d\\n\", a.y - base.y, a.x - base.x, b.y - base.y, b.x - base.x, (a.y - base.y) * (b.x - base.x) < (b.y - base.y) * (a.x - base.x));\n return (a.y - base.y) * (b.x - base.x) < (b.y - base.y) * (a.x - base.x);\n}\nint Map[MAXN];\nvoid solve(int rt, int pre, int l, int r)\n{\n// printf(\"solving %d %d %d\\n\", rt, l, r);\n sort(poi + l, poi + r + 1);\n// printf(\"After sort: \\n\");\n// for (int i = l; i <= r; i ++) printf(\"(%lld, %lld) %d\\n\", poi[i].x, poi[i].y, poi[i].id);\n base = poi[l];\n/* \n for (int i = l; i <= r; i ++)\n {\n printf(\"(%lld %lld), %lld %lld\\n\", poi[i].x, poi[i].y, poi[i].x - base.x, poi[i].y - base.y);\n }\n*/\n sort(poi + l + 1, poi + r + 1, cmp);\n// printf(\"After sort --- : \\n\");\n// for (int i = l; i <= r; i ++)\n// printf(\"(%lld, %lld) %d\\n\", poi[i].x, poi[i].y, poi[i].id);\n Map[poi[l].id] = rt;\n for (int i = head[rt]; i; i = adj[i].nex)\n {\n int v = adj[i].to;\n if (v == pre) continue;\n solve(v, rt, l + 1, l + siz[v]);\n l += siz[v];\n }\n}\n\nint main()\n{\n n = in();\n for (int i = 1; i < n; i ++)\n {\n int u = in(), v = in();\n addedge(u, v);\n addedge(v, u);\n }\n DFS(1, 0);\n for (int i = 1; i <= n; i ++)\n {\n poi[i] = (Poi) {in(), in(), i};\n }\n// sort(poi + 1, poi + n + 1);\n /*\n base = poi[1];\n sort(poi + 6, poi + n + 1, cmp);\n for (int i = 1; i <= n; i ++)\n {\n printf(\"(%lld %lld) %d\\n\", poi[i].x, poi[i].y, poi[i].id);\n }\n */\n solve(1, 0, 1, n);\n for (int i = 1; i <= n; i ++) printf(\"%d \", Map[i]);\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "8f3a4633cff043e71c63bea34bcadd6e", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#define _USE_MATH_DEFINES\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define ff(i,x,y) for(int i = (x);i < (y);++i)\n#define rep(i,n) ff(i,0,n)\n#define st(v) sort(v.begin(),v.end())\n#define st2(v,f) sort(v.begin(),v.end(),f)\n#define rvs(v) reverse(v.begin(),v.end())\n#define cnt(v,n) count(v.begin(),v.end(),n)\n#define pb push_back\nbool myfunction (int i,int j) { return (i0;i--){x=x*i;}\n#define MP make_pair\n#define F first\n#define S second\n#define LL long long\n#define ULL unsigned long long\n#define VI vector\n#define VVI vector\n\ntypedef pair PII;\ntypedef pair PIII;\n\n#ifdef LOCAL\nifstream in(\"in.txt\");\n#else\nistream &in = cin;\n#endif\n\nint n;\nbool adj[1501][1501];\nint vtx[1501];\nvector chk;\nvector points;\nvector res;\n\nint dfs(int node)\n{\n\tint cnt = 0;\n\tchk[node] = true;\n\tfor(int i = 1;i <= n;++i) \n\t\tif(!chk[i] && adj[node][i])\n\t\t\tcnt += dfs(i);\n\tvtx[node] = cnt+1;\n\treturn vtx[node];\n}\n\nvoid go(int root, vector ps)\n{\n\tint min_y = INT_MAX, min_i = -1;\n\tfor(int i = 1;i <= ps.size()-1;++i) if(ps[i].F.S < min_y) {min_y = ps[i].F.S; min_i = i;}\n\tres.push_back(MP(ps[min_i].S,root));\n\tvector > tps(1);\n\tfor(int i = 1;i <= ps.size()-1;++i) if(i != min_i) tps.push_back(MP(atan2l(ps[i].first.first-ps[min_i].first.first,ps[i].first.second-ps[min_i].first.second),i));\n\tsort(tps.begin()+1,tps.end());\n\tint s = 1;\n\tfor(int i = 1;i <= n;++i) if(adj[root][i]) {\n\t\tvector nps(1);\n\t\twhile(nps.size() < vtx[i]+1) {\n\t\t\tnps.push_back(ps[tps[s].S]);\n\t\t\t++s;\n\t\t}\n\t\tadj[i][root] = false;\n\t\tgo(i,nps);\n\t}\n}\n\nint main()\n{\n\tin >> n;\n\trep(i,n+1) rep(j,n+1) adj[i][j] = false;\n\tpoints.resize(n+1);\n\tchk.resize(n+1,false);\n\tmemset(vtx,0,sizeof(vtx));\n\n\trep(i,n-1) {\n\t\tint a, b;\n\t\tin >> a >> b;\n\t\tadj[a][b] = adj[b][a] = true;\n\t}\n\n\trep(i,n) {\n\t\tint x, y;\n\t\tin >> x >> y;\n\t\tpoints[i+1] = MP(MP(x,y),i+1);\n\t}\n\n\tdfs(1);\n\n\tsort(points.begin()+1,points.end());\n\trep(i,n+1) chk[i] = false;\n\n\tgo(1,points);\n\n\tst(res);\n\n\tfor(int i = 0;i < res.size();++i) cout << res[i].second << \" \";\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "6805ce4e3e2eadfc6ccb9dc639a4df1a", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\n#define pb push_back\nconst int N = 1510;\nstruct Node {\n\tint x, y, id;\n\tNode operator - (const Node &b) const {\n\t\treturn (Node) {x - b.x, y - b.y, 0};\n\t}\n\tbool operator < (const Node &b) const {\n\t\treturn x < b.x || (x == b.x && y < b.y);\n\t}\n} a[N], cur;\nll cross(const Node &a, const Node &b) {\n\treturn (ll) a.x * b.y - (ll) a.y * b.x;\n}\nbool cmp(const Node &a, const Node &b) {\n\treturn cross(a - cur, b - cur) < 0;\n}\nint n, sz[N], ans[N];\nvector G[N];\nvoid dfs0(int u, int fa = 0) {\n\tsz[u] = 1;\n\tfor(int v : G[u]) if(v != fa) {\n\t\tdfs0(v, u); sz[u] += sz[v];\n\t}\n}\nvoid dfs(int u, int l, int r, int fa = 0) {\n\tint p = l + 1, nw = l;\n\tfor(int i = l + 1; i <= r; i ++)\n\t\tif(a[nw] < a[i]) nw = i;\n\tif(nw != l) swap(a[nw], a[l]);\n\tans[a[l].id] = u; cur = a[l];\n\tsort(a + l + 1, a + r + 1, cmp);\n\tfor(int v : G[u]) if(v != fa) {\n\t\tdfs(v, p, p + sz[v] - 1, u);\n\t\tp += sz[v];\n\t}\n}\nint main() {\n\tscanf(\"%d\", &n);\n\tfor(int u, v, i = 1; i < n; i ++) {\n\t\tscanf(\"%d%d\", &u, &v);\n\t\tG[u].pb(v); G[v].pb(u);\n\t}\n\tfor(int i = 1; i <= n; i ++) {\n\t\tscanf(\"%d%d\", &a[i].x, &a[i].y);\n\t\ta[i].id = i;\n\t}\n\tdfs0(1); dfs(1, 1, n);\n\tfor(int i = 1; i <= n; i ++)\n\t\tprintf(\"%d%c\", ans[i], \" \\n\"[i == n]);\n\treturn 0;\n}\n\t \t \t\t\t \t \t\t \t \t\t \t\t \t\t//hellobig_tq", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "d035a02dc3fd657ab9bd4302eec146b5", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nconst int N = 1500;\nint n, ans[N], size[N];\nvector edge[N], ch[N];\nvector > > p;\n\nbool by_x(pair > a, pair > b) {\n\treturn a.second.first < b.second.first || a.second.first == b.second.first && a.second.second < b.second.second;\n}\n\npair cur;\n\nbool by_r(pair > a, pair > b) {\n\tlong long x1 = a.second.first - cur.first, y1 = a.second.second - cur.second, x2 = b.second.first - cur.first, y2 = b.second.second - cur.second;\n\treturn x1 * y2 - x2 * y1 < 0;\n}\n\nvoid dfs(int u, int fa) {\n\tsize[u] = 1;\n\tfor (int j = 0; j < (int)edge[u].size(); ++j) {\n\t\tint v = edge[u][j];\n\t\tif (v == fa) {\n\t\t\tcontinue;\n\t\t}\n\t\tch[u].push_back(v);\n\t\tdfs(v, u);\n\t\tsize[u] += size[v];\n\t}\n}\n\nvoid solve(int u, vector > > p) {\n\tfor (int i = 0 ; i < (int)p.size(); ++i) {\n\t\tif (by_x(p[i], p.back())) {\n\t\t\tswap(p[i], p[p.size() - 1]);\n\t\t}\n\t}\n\tcur = p.back().second;\n\tans[p.back().first] = u;\n\tp.pop_back();\n\tsort(p.begin(), p.end(), by_r);\n\tfor (int i = 0; i < (int)ch[u].size(); ++i) {\n\t\tvector > > tmp;\n\t\tint v = ch[u][i];\n\t\tfor (int j = 0; j < size[v]; ++j) {\n\t\t\ttmp.push_back(p.back());\n\t\t\tp.pop_back();\n\t\t}\n\t\tsolve(v, tmp);\n\t}\n}\n\nint main() {\n\tscanf(\"%d\", &n);\n\tfor (int i = 0; i < n - 1; ++i) {\n\t\tint u, v;\n\t\tscanf(\"%d%d\", &u, &v);\n\t\t--u, --v;\n\t\tedge[u].push_back(v);\n\t\tedge[v].push_back(u);\n\t}\n\tdfs(0, -1);\n\tfor (int i = 0; i < n; ++i) {\n\t\tint x, y;\n\t\tscanf(\"%d%d\", &x, &y);\n\t\tp.push_back(make_pair(i, make_pair(x, y)));\n\t}\n\tsolve(0, p);\n\tfor (int i = 0; i < n; ++i) {\n\t\tprintf(\"%d%c\", ans[i] + 1, i == n - 1 ? '\\n' : ' ');\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "4f6f787c7eac6b881e7992037fc3c1c8", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n#define int long long\n\ninline int read(){\n\tint ret=0,f=1;char ch=getchar();\n\twhile (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}\n\twhile (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();\n\treturn ret*f;\n}\n\nconst int maxn=1505,INF=2e9;\nint n;\nint tot=0,lnk[maxn],nxt[maxn*2],son[maxn*2],fa[maxn],num[maxn];\n\ninline void add_edge(int x,int y){\n\ttot++;son[tot]=y;nxt[tot]=lnk[x];lnk[x]=tot;\n}\n\nstruct point{\n\tint id,x,y;\n};\ndouble cross(double x1,double y1,double x2,double y2){\n\treturn (x1*y2-x2*y1);\n}\ndouble compare(point a,point b,point c){\n\treturn cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));\n}\n\npoint a[maxn];\npoint s,cc;\nint ans[maxn];\n\nvoid build_tree(int x){\n\tnum[x]=1;\n\tfor (int i=lnk[x];i;i=nxt[i]) if (son[i]!=fa[x]) fa[son[i]]=x,build_tree(son[i]),num[x]+=num[son[i]];\n}\n\nbool cmp(point a,point b){\n\tif(compare(cc,a,b)==0)\n\t\treturn a.x>b.x;\n\telse return compare(cc,a,b)>0;\n}\n\ninline void DFS(int id,int p,vector vec){\n\tans[id]=p;\n\tcc=a[id];\n\tsort(vec.begin(),vec.end(),cmp);\n\t// printf(\"%lld:%lld -- \",id,p);\n\t// for (int i=0;i<(int)vec.size();i++) printf(\"%lld \",vec[i].id);printf(\"\\n\");\n\tvector vec_nxt; point nxt_s;\n\tint j=0;\n\tfor (int i=lnk[p];i;i=nxt[i]) if (son[i]!=fa[p]){\n\t\tvec_nxt.clear();nxt_s.y=INF; int tmp=0;\n\t\tfor (int k=j;k all_vec;all_vec.clear();\n\ts.y=INF;\n\tfor (int i=1;i<=n;i++){\n\t\ta[i].x=read(),a[i].y=read();a[i].id=i;\n\t\tall_vec.push_back(a[i]);\n\t\tif (a[i].y\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nstruct Point\n{\n int X,Y,Number;\n};\n\nPoint O;\n\nbool Cross(Point A,Point B,Point C)\n{\n long long P=(long long)A.X*B.Y+(long long)B.X*C.Y+(long long)C.X*A.Y;\n long long Q=(long long)A.Y*B.X+(long long)B.Y*C.X+(long long)C.Y*A.X;\n return P>Q;\n}\n\nbool operator <(Point A,Point B)\n{\n return Cross(A,O,B);\n}\n\nvector G[1500];\nPoint P[1500];\nint Size[1500],Ans[1500];\nbool Flag[1500];\n\nvoid Calc_Size(int V)\n{\n Size[V]=1;\n for(int i=0;i\n#include\n#include\nusing namespace std;\nint n,x,y,a[10000],b[10000],c[10000],siz[10000],ans[10000];\nint first[10000],nxt[10000],go[10000],tot,top;\nint add(int x,int y){\n nxt[++tot]=first[x]; first[x]=tot; go[tot]=y;\n}\nint pre_dfs(int x,int father){\n for(int i=first[x];i;i=nxt[i])\n\tif(go[i]!=father)\n\t siz[x]+=pre_dfs(go[i],x);\n return ++siz[x];\n}\n\nint askx(int x){\n return a[x]-a[c[top]];\n}\n\nint asky(int x){\n return b[x]-b[c[top]];\n}\n\nint mysort(int x,int y){\n return 1ll*askx(x)*asky(y)<1ll*askx(y)*asky(x);\n}\n\nint dfs(int x,int father){\n top++;\n for(int i=1;ia[c[top+i]])\n\t swap(c[top],c[top+i]);\n ans[c[top]]=x;\n if(siz[x]==1) return 0;;\n //for(int i=1;i<=n;i++) printf(\"%d \",c[i]);\n sort(c+top+1,c+top+siz[x],mysort);\n //for(int i=1;i<=n;i++) printf(\"%d \",c[i]);\n //printf(\"| %d %d %d\\n\",x,top,siz[x]);\n for(int i=first[x];i;i=nxt[i])\n\tif(go[i]!=father){\n\t dfs(go[i],x);\n\t}\n}\n\nint main(){\n scanf(\"%d\",&n);\n for(int i=1;i\nusing namespace std;\ntypedef long long ll;\nconst int N=2040;\n\nint n,ans[N];//ans:posnumber-> treenumber\nstruct node\n{\n\tll x,y;\n\tint id;\n}a[N],nowp;\n\nbool cmp(node p,node q)\n{\n\tll px=p.x-nowp.x;\n\tll py=p.y-nowp.y;\n\tll qx=q.x-nowp.x;\n\tll qy=q.y-nowp.y;\n\tif(py>=0&&qy<=0) return 0;\n\tif(py<=0&&qy>=0) return 1;\n\treturn py*qxa[i].x||(a[pos].x==a[i].x&&a[i].y\n\n#define M_PI 3.14159265358979323846\n#define all(x) (x).begin(), (x).end()\n#define pb push_back\n#define mp make_pair\n#define FILE \"file\"\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nconst int INF = numeric_limits::max();\nconst ll LLINF = numeric_limits::max();\nconst ull ULLINF = numeric_limits::max();\n\nstruct pt\n{\n ll x, y;\n pt() = default;\n pt(ll x, ll y) : x(x), y(y) {}\n bool operator < (const pt a) const\n {\n if(x == a.x)\n return y < a.y;\n return x < a.x;\n }\n};\n\nint n;\nvector> g;\nvector> pts;\nint sz[1510], ans[1510];\n\nint dfs(int v, int p)\n{\n sz[v] = 1;\n for(int& to: g[v])\n if(to != p)\n sz[v] += dfs(to, v);\n return sz[v];\n}\n\nint current = 0;\n\nbool cmp(pair a, pair b)\n{\n return (a.first.x - pts[current].first.x) * (b.first.y - pts[current].first.y) - (a.first.y - pts[current].first.y) * (b.first.x - pts[current].first.x) > 0;\n}\n\nvoid go(int v, int p, int l, int r)\n{\n if(l == r)\n ans[pts[l].second] = v + 1;\n else\n {\n ans[pts[l].second] = v + 1;\n int k = l + 1;\n current = l;\n sort(pts.begin() + k, pts.begin() + r + 1, cmp);\n for(int to: g[v])\n {\n if(to == p)\n continue;\n go(to, v, k, k + sz[to] - 1);\n k += sz[to];\n }\n }\n}\n\nint main()\n{\n// freopen(FILE\".in\", \"r\", stdin);\n// freopen(FILE\".out\", \"w\", stdout);\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cin >> n;\n g.resize(n);\n for(int i = 0; i < n - 1; i++)\n {\n int a, b;\n cin >> a >> b;\n a--, b--;\n g[a].pb(b);\n g[b].pb(a);\n }\n pts.resize(n);\n for(int i = 0; i < n; i++)\n {\n ll x, y;\n cin >> x >> y;\n pts[i] = mp(pt(x, y), i);\n }\n sort(all(pts));\n dfs(0, -1);\n sort(pts.begin() + 1, pts.end(), cmp);\n go(0, -1, 0, pts.size() - 1);\n for(int i = 0; i < n; i++)\n cout << ans[i] << \" \";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "5b77f69474e3bc96c71a8fcb80685e5d", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define rep(i, l, r) for(int i = l; i <= r; i++)\n#define fi first\n#define se second\n\n#define MAXN 2*1599\nint Count = 0;\nint L[MAXN];\nint Next[MAXN];\nint H[MAXN];\nint X[MAXN];\nint Y[MAXN];\nint U[MAXN];\nint Visit[MAXN];\nint Visit1[MAXN];\nint V[MAXN];\nint n;\nint S[MAXN];\nint Size[MAXN];\npair Ang[MAXN];\n\nvoid getSize(int root) {\n\tint sum = 1;\n\tVisit[root] = 1;\n\tfor(int idx = H[root]; idx; idx = Next[idx]) {\n\t\tint u = L[idx];\n\t\tif (Visit[u]) continue;\n\t\t//~ cout << root << \":\" << u << endl;\n\t\tgetSize(u);\n\t\tsum += Size[u];\n\t}\n\tSize[root] = sum;\n}\n\nvoid addLink(int u, int v) {\n\tL[++Count] = v;\n\tNext[Count] = H[u];\n\tH[u] = Count;\n}\n\nlong double getAngle(int u, int v) {\n\tif (u==v) return -2;\n\tlong double dx = X[v] - X[u];\n\tlong double dy = Y[v] - Y[u];\n\t//~ cout << dx << dy;\n\t\n\treturn dy*1.0/sqrtl(dx*dx + dy*dy);\n}\n\nvoid divide(int l, int r, int root) {\n\tint min = l;\n\trep(i, l+1, r)\n\t\tif (make_pair(X[Ang[i].se], Y[Ang[i].se]) < \n\t\t\tmake_pair(X[Ang[min].se], Y[Ang[min].se]))\n\t\t\tmin = i;\n\tswap(Ang[l], Ang[min]);\n\tS[Ang[l].se] = root;\n\tVisit1[root] = 1;\n\trep(i, l, r)\n\t\tAng[i].fi = getAngle(Ang[l].se, Ang[i].se);\n\t\n\tsort(Ang+l+1, Ang+1+r);\n\t//~ rep(i, l , r) cout << Ang[i].fi << \":\" << Ang[i].se << \" \";\n\t//~ cout << endl;\n\tint cl = l+1;\n\tfor(int idx = H[root]; idx; idx = Next[idx]) {\n\t\tint u = L[idx];\n\t\tif (Visit1[u]) continue;\n\t\tdivide(cl, cl + Size[u] - 1, u);\n\t\tcl += Size[u];\n\t}\n}\n\nint main() {\n\t\n\tcin >> n;\n\trep(i, 1, n-1) { \n\t\tcin >> U[i] >> V[i]; \n\t\taddLink(U[i], V[i]); \n\t\taddLink(V[i], U[i]);\n\t}\n\t\n\n\trep(i, 1, n) cin >> X[i] >> Y[i];\n\tgetSize(1); // Size array\n\t\n\tint min = 1;\n\trep(i, 2, n) if (make_pair(X[i],Y[i]) < make_pair(X[min], Y[min]))\n\t\tmin = i;\n\t//~ cout << X[min] << \":\" << Y[min] << endl;\n\t\n\t//~ cout <<\"HEllp\" << endl;\n\trep(i, 1, n) Ang[i] = make_pair(0, i);\n\tswap(Ang[1], Ang[min]);\n\tdivide(1, n, 1);\n\t\n\trep(i, 1, n-1)\n\t\tcout << S[i] << \" \";\n\tcout << S[n] << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "74deaa4b55a14d402326bef761c9daa8", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\nconst int INF = 1e9+7;\nconst int N = 1501;\n\nint n,nxt[N<<1],h[N],vet[N<<1],num[N],f[N],d[N],fa[N],siz[N],en;\n\nstruct Point{\n int a,b,c;double tan;\n}P[N],st;\n\nvector vec;\n\ndouble tanj(int x,int y,int x1,int y1){\n return (double)(y1-y)/(x1-x);\n}\n\nbool cmp1(Point x,Point y){\n return x.a vec){\n num[vec[0].c]=u;\n st=vec[0];\n sort(vec.begin()+1,vec.end(),cmp2);\n vector tmp;int now=1;\n for (int e=h[u];e;e=nxt[e]){\n int v=vet[e];\n tmp.clear();\n if (v!=fa[u]){\n while (tmp.size()\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define clr(a,b) memset(a,b,sizeof(a))\n#define pb(a) push_back(a)\n#define fir first\n#define se second\n#define LL long long\ntypedef pair pii;\ntypedef pair pli;\ntypedef pair pll;\nconst int inf = 0x3f3f3f3f;\nconst int maxn = 2e5+5;\nLL mod = 1e9+7;\ndouble eps = 0.00000001;\ndouble PI = acos(-1);\n\n\nstruct point { \n int x,y,id; \n\tpoint(){}\n point(int _x,int _y){\n x=_x,y=_y;\n }\n}; \npoint con[1505]; \nint cnt[1505],ans[1505],vis[1505],now;\nvector g[1505];\n \nLL cross(const point &p1, const point &p2, const point &q1, const point &q2) \n{ \n return (LL)(q2.y - q1.y)*(p2.x - p1.x) - (LL)(q2.x - q1.x)*(p2.y - p1.y); \n} \n \nbool cmp(const point &a, const point &b) \n{ \n point origin = con[now]; \n return cross(origin,b,origin,a) < 0; \n} \n\nvoid dfs(int u,int fa) {\n\tcnt[u] = 1;\n\tfor(int i = 0;i < g[u].size();i++) {\n\t\tint v = g[u][i];\n\t\tif(v == fa) continue;\n\t\tdfs(v,u);\n\t\tcnt[u] += cnt[v];\n\t}\n}\n\nvoid dfs1(int id,int l,int r) {\n\t//printf(\"%d--\\n\",id);\n\tint v = inf,pos;\n\tfor(int i = l;i <= r;i++) {\n\t\tif(con[i].x < v) v = con[i].x,pos = i;\n\t}\n\tswap(con[l],con[pos]);\n\tnow = l;\n\tans[con[now].id] = id,vis[id] = 1;\n\tif(l == r) return ;\n\tsort(con+l+1,con+r+1,cmp);\n\tpos = l;\n\tfor(int i = 0;i < g[id].size();i++) {\n\t\tint v = g[id][i];\n\t\tif(vis[v]) continue;\n\t\tdfs1(v,pos+1,pos+cnt[v]);\n\t\tpos += cnt[v];\n\t}\n}\n\nint main() {\n\tint n;\n\tcin>>n;\n\tclr(cnt,0);\n\tfor(int i = 1;i < n;i++) {\n\t\tint u,v;\n\t\tscanf(\"%d%d\",&u,&v);\n\t\tg[u].pb(v);\n\t\tg[v].pb(u);\n\t}\n\tfor(int i = 1;i <= n;i++) {\n\t\tscanf(\"%d%d\",&con[i].x,&con[i].y);\n\t\tcon[i].id = i;\n\t}\n\tdfs(1,0);\n\tdfs1(1,1,n);\n\tfor(int i = 1;i <= n;i++) printf(\"%d \",ans[i]);\n\tcout<\n#include \n#include \n#include\nusing namespace std;\nvector path[1505];\nvector tree[1505];\nint w[1505];\nqueue q;\nint n;\nstruct point{\n int x,y,i,ans;\n}p[1505];\nbool cmpx(struct point A,struct point B)\n{\n if(A.x!=B.x) return A.x0;\n}\nbool cmpi(struct point A,struct point B)\n{\n return A.i>n;\n for(int i=0;i>x>>y;\n path[x].push_back(y);\n path[y].push_back(x);\n }\n for(int i=0;i>p[i].x>>p[i].y;\n p[i].i=i;\n }\n}\nvoid push_v(int v, bool vis[])\n{\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#pragma comment(linker, \"/stack:256000000\")\n\n#define pii pair \n#define mp make_pair\n#define ll long long\n\nconst int MN = 1500 + 10;\n\nstruct point {\n\tll x, y;\n\tint k;\n\tpoint () {};\n\tpoint (ll xx, ll yy, int kk) {\n\t\tx = xx, y = yy;\n\t\tk = kk;\n\t}\n\tstatic ll dist(point a, point b) {\n\t\treturn (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);\n\t}\n\tpoint operator-(point &nw) const {\n\t\tpoint p;\n\t\tp.x = x - nw.x;\n\t\tp.y = y - nw.y;\n\t\treturn p;\n\t}\n\tstatic ll kos(point a, point b, point c) {\n\t\tpoint v1 = a - b;\n\t\tpoint v2 = c - b;\n\t\treturn v1.x * v2.y - v1.y * v2.x;\n\t}\n};\n\nvector g[MN];\n\nint n;\n\npoint arr[MN];\n\nint sz[MN];\n\nbool used[MN];\n\nvoid dfs(int v) {\n\tused[v] = true;\n\tfor(int i = 0; i < g[v].size(); i ++) {\n\t\tint nv = g[v][i];\n\t\tif (!used[nv]) {\n\t\t\tdfs(nv);\n\t\t\tsz[v] += (sz[nv] + 1);\n\t\t}\n\t}\n}\n\npoint p0;\n\nint ans[MN];\n\nbool my(point a, point b) {\n\tll k = point::kos(a, p0, b);\n\treturn (k < 0);\n}\n\nvoid solve(int lf, int rg, int root) {\n\tused[root] = true;\n\tfor(int i = lf; i <= rg; i ++) {\n\t\tif (arr[i].y < arr[lf].y || (arr[i].y == arr[lf].y && arr[i].x < arr[lf].x))\n\t\t\tswap(arr[lf], arr[i]);\n\t}\n\tp0 = arr[lf];\n\tsort(arr + lf + 1, arr + rg + 1, my);\n\tans[ arr[lf].k ] = root + 1;\n\tlf ++;\n\tfor(int i = 0; i < g[root].size(); i ++) {\n\t\tint to = g[root][i];\n\t\tif (used[to])\n\t\t\tcontinue;\n\t\tsolve(lf, lf + sz[to], to); //?\n\t\tlf += (sz[to] + 1);\n\t}\n}\n\n\nint main() {\n\t//freopen(\"input.txt\", \"r\", stdin);\n\tcin >> n;\n\tfor(int i = 0; i < n - 1; i ++) {\n\t\tint x, y;\n\t\tscanf(\"%d%d\", &x, &y);\n\t\tx --, y --;\n\t\tg[x].push_back(y);\n\t\tg[y].push_back(x);\n\t}\n\tfor(int i = 0; i < n; i ++) {\n\t\tcin >> arr[i].x >> arr[i].y;\n\t\tarr[i].k = i;\n\t}\n\tdfs(0);\n\tfill(&used[0], &used[0] + MN, false);\n\tsolve(0, n - 1, 0);\n\tfor(int i = 0; i < n; i ++)\n\t\tprintf(\"%d \", ans[i]);\n\tcout << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "866b0de5c9165af5a7cb0ba315c8f749", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define MAXN (1505)\n\nconst long double pi = acosl(-1);\n\nint N;\n\nvector edge[MAXN];\n\nint xi[MAXN];\nint yi[MAXN];\n\nint P[MAXN];\n\nint par[MAXN]; // tree node's parent\nint V[MAXN];\nint tsize[MAXN]; // subtree size\n\nint sol[MAXN];\n\nvoid dfs(int id)\n{\n\tV[id] = 1;\n\ttsize[id] = 1;\n\n\tint sz = edge[id].size();\n\tfor(int i = 0; i < sz; ++i)\n\t{\n\t\tint a = edge[id][i];\n\n\t\tif(V[a] == 0)\n\t\t{\n\t\t\tpar[a] = id;\n\t\t\tdfs(a);\n\n\t\t\ttsize[id] += tsize[a];\n\t\t}\n\t}\n\n//\tprintf(\"node size %d : %d\\n\", id + 1, tsize[id]);\n}\n\nlong double sang[MAXN]; // angles used for sorting\n\nbool cmpf(int a, int b)\n{\n\treturn sang[a] < sang[b];\n}\n\nvoid painttree(int id, int l, int r) // id - l, children - [l + 1, r)\n{\n//\tprintf(\"pt %d [%d %d) : %d\\n\", id + 1, l, r, P[l] + 1);\n\tsol[P[l]] = id + 1;\n\n\tif(l + 1 < r)\n\t{\n\t\tint root = P[l];\n\t\tint i;\n\n\t\tif(1 || tsize[id] > 3) // have more than 2 children\n\t\t{\n\t\t\tlong double minang = 100;\n\t\t\tlong double maxang = -100;\n\n\t\t\tfor(i = l + 1; i < r; ++i)\n\t\t\t{\n\t\t\t\tint a = P[i];\n\t\t\t\tsang[a] = atan2l(yi[a] - yi[root], xi[a] - xi[root]);\n\n\t\t\t\tif(minang > sang[a])\n\t\t\t\t\tminang = sang[a];\n\t\t\t\tif(maxang < sang[a])\n\t\t\t\t\tmaxang = sang[a];\n\t\t\t}\n\n\t\t\tif(maxang > minang + pi)\n\t\t\t{\n\t\t\t\tfor(i = l + 1; i < r; ++i)\n\t\t\t\t{\n\t\t\t\t\tint a = P[i];\n\t\t\t\t\tif(sang[a] < 0)\n\t\t\t\t\t\tsang[a] += 2.0*pi;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tsort(P + l + 1, P + r, cmpf);\n\n/*\n\t\t\tprintf(\"sorted for %d :\", id + 1);\n\t\t\tfor(i = l + 1; i < r; ++i)\n\t\t\t\tprintf(\" %d\", P[i] + 1);\n\t\t\tputchar('\\n');\n//*/\n\t\t}\n\n\t\tint sz = edge[id].size();\n\t\tint j = l + 1;\n\t\tfor(i = 0; i < sz; ++i)\n\t\t{\n\t\t\tint a = edge[id][i];\n\t\t\tif(par[a] == id)\n\t\t\t{\n\t\t\t\tpainttree(a, j, j + tsize[a]);\n\t\t\t\tj += tsize[a];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main()\n{\n\tint i;\n\n\tscanf(\"%d\", &N);\n\tfor(i = 1; i < N; ++i)\n\t{\n\t\tint a, b;\n\t\tscanf(\"%d %d\", &a, &b);\n\t\t--a;\n\t\t--b;\n\n\t\tedge[a].push_back(b);\n\t\tedge[b].push_back(a);\n\t}\n\n\tdfs(0);\n\n\tint xl = 1000000001;\n\tint yl = 1000000001;\n\tint idl = 0;\n\n\tfor(i = 0; i < N; ++i)\n\t{\n\t\tscanf(\"%d %d\", &xi[i], &yi[i]);\n\n\t\tif(xl > xi[i] || (xl == xi[i] && yl > yi[i]))\n\t\t{\n\t\t\txl = xi[i];\n\t\t\tyl = yi[i];\n\t\t\tidl = i;\n\t\t}\n\n\t\tP[i] = i;\n\t}\n\n//\tprintf(\"leftmost %d\\n\", idl + 1);\n\n\tP[idl] = 0;\n\tP[0] = idl;\n\n\tpainttree(0, 0, N);\n\n\tfor(i = 0; i < N; ++i)\n\t{\n\t\tif(i)\n\t\t\tputchar(' ');\n\t\tprintf(\"%d\", sol[i]);\n\t}\n\tputchar('\\n');\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "28a4b985481baa7b51c30181f2d499ac", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include\nusing namespace std;\nvector path[1505];\nvector tree[1505];\nint w[1505];\nqueue q;\nint n;\nstruct point{\n int x,y,i,ans;\n}p[1505];\nbool cmpx(struct point A,struct point B)\n{\n if(A.x!=B.x) return A.x0;\n}\nbool cmpi(struct point A,struct point B)\n{\n return A.i>n;\n for(int i=0;i>x>>y;\n path[x].push_back(y);\n path[y].push_back(x);\n }\n for(int i=0;i>p[i].x>>p[i].y;\n p[i].i=i;\n }\n}\nvoid push_v(int v, bool vis[])\n{\n for(int i=0;i\n#include \n#include \n#include \n#include \nusing namespace std;\nconst int MAXN = 1510*2;\nint n, edge, size[MAXN], ans[MAXN];\nint h[MAXN], g[MAXN], next[MAXN];\nstruct node {\n\tint x, y, k;\n} v[MAXN];\n\nvoid ins(int a, int b) {\n\tnext[++edge]=h[a];\n\th[a]=edge;\n\tg[edge]=b;\n}\n\nvoid dfs(int x) {\n\tsize[x] = 1;\n\tfor (int i=h[x];i;i=next[i])\n\t\tif (size[g[i]]==0) {\n\t\t\tdfs(g[i]);\n\t\t\tsize[x] += size[g[i]];\n\t\t}\n}\n\nbool cmp(const node &a, const node &b) {\n\tlong long t = (long long)a.x*(long long)b.y -\n\t\t\t\t (long long)a.y*(long long)b.x;\n\treturn t>0;\n}\n\nvoid dfs(int root, int l, int r) {\n\tif ( l>r ) return;\n\tnode x = v[l];\n\tint best = l;\n\tfor (int i=l;i<=r;i++)\n\t\tif ( v[i].y < x.y ) {\n\t\t\tx = v[i];\n\t\t\tbest = i;\n\t\t}\n\tans[x.k] = root;\n\tswap(v[best], v[l]);\n\t\n\tif (l==r) return;\n\tfor (int i=l+1;i<=r;i++)\n\t\tv[i].x-=v[l].x, v[i].y-=v[l].y;\n\tsort(v+l+1, v+r+1, cmp);\n\t\n\tint now = l+1;\n\tfor (int i=h[root];i;i=next[i])\n\t\tif (size[g[i]]\n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\nconst int maxn = 1510;\nint n;\nint ans[maxn];\nint tot = -1, head[maxn], cnt[maxn];\nstruct node {\n int p, next;\n} edge[maxn << 1];\ninline void Add(int u, int v) {\n edge[++tot] = (node) {v, head[u]};\n head[u] = tot;\n return;\n}\nstruct Point {\n int x, y, idx;\n inline void Read() {\n scanf(\"%d%d\", &x, &y);\n return;\n }\n bool operator < (const Point &a) const {\n return (x != a.x) ? (x < a.x) : (y < a.y);\n }\n} p[maxn];\nPoint Min;\nbool cmp(Point a, Point b) {\n return LL(a.y - Min.y) * (b.x - Min.x) < LL(b.y - Min.y) * (a.x - Min.x);\n}\nvoid DFS(int u, int fa) {\n cnt[u] = 1;\n for (int i = head[u]; ~i; i = edge[i].next) {\n int v = edge[i].p;\n if (v != fa) {\n DFS(v, u);\n cnt[u] += cnt[v];\n }\n }\n return;\n}\nvoid Divide(int u, int fa, int l, int r) {\n int idx = l;\n for (int i = l + 1; i <= r; ++i)\n if (p[i] < p[idx]) idx = i;\n swap(p[l], p[idx]);\n ans[p[l].idx] = u;\n Min = p[l];\n sort(p + l + 1, p + r + 1, cmp);\n for (int i = head[u], j = l; ~i; i = edge[i].next) {\n int v = edge[i].p;\n if (v != fa) {\n Divide(v, u, j + 1, j + cnt[v]);\n j += cnt[v];\n }\n }\n return;\n}\nint main() {\n scanf(\"%d\", &n);\n memset(head, -1, sizeof(head));\n for (int i = 1; i < n; ++i) {\n int u, v;\n scanf(\"%d%d\", &u, &v);\n Add(u, v);\n Add(v, u);\n }\n for (int i = 1; i <= n; ++i) {\n p[i].Read();\n p[i].idx = i;\n }\n DFS(1, 0);\n Divide(1, 0, 1, n);\n for (int i = 1; i <= n; ++i)\n printf(\"%d \", ans[i]);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "b86b9db78a4f0834595eca841a7b21e1", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/**\n * Compiler: gcc\n * Author: bdep__ \n * Created: 06/12/2012 08:59:55 PM\n ***/\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define N 1510\n#define M 1000010\n#define S 1000100\n#define eps 1e-8\n#define inf 1e10\n#define MOD 1000000007\n#define PB push_back\n#define MP make_pair\n#define SZ(a) ((int)((a).size()))\n#define ALL(a) (a).begin(),(a).end()\n#define CLR(a,x) memset(a,x,sizeof(a))\n\ntypedef long long LL;\nusing namespace std;\n\ninline int L(int i){ return i<<1; }\ninline int R(int i){ return (i<<1)|1; }\ninline int B(int i){ return 1<0;\n}\n\nvoid insert(int f,int t)\n{\n\te[top].f=f,e[top].t=t;\n\te[top].n=list[f],list[f]=top++;\n}\n\nvoid dfs(int now)\n{\n\tcnt[now]=1;\n\tfor(int i=list[now]; i!=-1; i=e[i].n)\n\t{\n\t\tint to=e[i].t;\n\t\tif(cnt[to])\n\t\t\tcontinue;\n\t\tdfs(to);\n\t\tcnt[now]+=cnt[to];\n\t}\n}\n\nvoid dfs(int now,int l,int r)\n{\n\tif(l>r)\n\t\treturn;\n\tvis[now]=1;\n\tint id=-1,nl;\n\tLL mi=inf;\n\tfor(int i=l; i<=r; i++)\n\t\tif(mi>no[i].y)\n\t\t\tmi=no[i].y,id=i;\n\tswap(no[id],no[l]);\n\tfor(int i=l+1; i<=r; i++)\n\t\tno[i].y-=no[l].y,no[i].x-=no[l].x;\n\tsort(no+l+1,no+r+1,cmp);\n\tnl=l+1;\n\tret[no[l].i]=now;\n\tfor(int i=list[now]; i!=-1; i=e[i].n)\n\t{\n\t\tint to=e[i].t;\n\t\tif(vis[to]) continue;\n\t\tdfs(to,nl,nl+cnt[to]-1);\n\t\tnl+=cnt[to];\n\t}\n}\n\nint main()\n{\n\tint n,x,y;\n\twhile(scanf(\"%d\",&n)!=EOF)\n\t{\n\t\ttop=0;\n\t\tmemset(cnt,0,sizeof(cnt));\n\t\tfor(int i=0; i\n#include\n#include\n#include\nusing namespace std;\nconst int N=1501;\nstruct pnt{int x,y,id; double co;}a[N]; int ans[N];\nvector G[N]; int sz[N];\nbool operator<(const pnt &a,const pnt &b){return a.co=l+1;--i) a[i]=a[i-1]; a[l]=swp; ans[a[l].id]=x;\n\tfor(int i=l+1;i<=r;++i) a[i].co=atan((a[i].y-a[l].y)*1.0/(a[i].x-a[l].x));\n\tsort(a+l+1,a+r+1); int pos=l+1;\n\tfor(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define y0 sdkfaslhagaklsldk\n#define y1 aasdfasdfasdf\n#define yn askfhwqriuperikldjk\n#define j1 assdgsdgasghsf\n#define tm sdfjahlfasfh\n#define lr asgasgash\n\n#define eps 1e-11\n//#define M_PI 3.141592653589793\n#define bs 1000000007\n#define bsize 256\n#define right adsgasgadsg\n\nusing namespace std;\n\nlong n,a[300000],b[300000];\nvector g[20000];\nvector vec;\nlong used[30000];\nlong ans[300000];\nlong dx,dy;\nvector sons[300000];\nlong sub[300000];\n\nvoid dfs(long v)\n{\n used[v]=1;\n for (int i=0;i vec)\n{\n long lm=0;\n for (int i=0;i >vv;\n for (int i=0;i p;\n long q=sons[v][i];\n while (p.size()>n;\nfor (int i=1;i>a>>b;\n g[a].push_back(b);\n g[b].push_back(a);\n}\n\ndfs(1);\nfor (int i=1;i<=n;i++)\n{\n cin>>a[i]>>b[i];\n vec.push_back(i);\n}\n\nbuild(1,vec);\n\nfor (int i=1;i<=n;i++)\n{\n if (i-1)cout<<\" \";\n cout<\nusing namespace std;\nconst int N = 1510;\nvector G[N];\nstruct node{\n\tint x,y,id;\n};\nbool vis[N];\nint son[N];\nint ans[N];\nnode C{(int)-1e9-100,(int)-1e9-100};\n//极角排序\nvector pt;\nbool cmp(node& a,node& b)\n{\n\tnode A{a.x-C.x,a.y-C.y};\n\tnode B{b.x-C.x,b.y-C.y};\n\treturn 1ll*A.x*B.y - 1LL*A.y*B.x < 0;\n}\n//\n\nvoid dfs(int x,vector pt){\n\tint mx = 0;\n\tvis[x] = true;\n\tfor(int i= 0;i < pt.size();i ++){\n\t\tif(pt[i].x < pt[mx].x )mx = i;\n\t}\n\tans[pt[mx].id] = x;\n\tC.x = pt[mx].x,C.y = pt[mx].y;\n\tpt.erase(pt.begin() + mx);\n\tsort(pt.begin(),pt.end(),cmp);\n\tint bef = 0;\n\tfor(int i= 0;i < G[x].size();i ++){\n\t\tint v = G[x][i];\n\t\tif(vis[v]) continue;\n\t\tvector npt;\n\t\tnpt.clear();\n\t\tfor(int j = bef;j < bef+son[v];j ++){\n\t\t\tnpt.push_back(pt[j]);\n\t\t}\n\t\tbef += son[v];\n\t\tdfs(v,npt);\n\t}\n}\nint ddfs(int x){\n\tvis[x] = true;\n\tint cnt = 1;\n\tfor(int i = 0;i < G[x].size();i ++){\n\t\tint v= G[x][i];\n\t\tif(vis[v]) continue;\n\t\tcnt += ddfs(v);\n\t}\n\treturn son[x] = cnt;\n}\nint main(){\n\tint n;\n\tcin >>n;\n\tfor(int i= 1;i < n;i ++){\n\t\tint a,b;\n\t\tscanf(\"%d %d\",&a,&b);\n\t\tG[a].push_back(b);\n\t\tG[b].push_back(a);\n\t}\n\tfor(int i= 1;i <= n;i ++){\n\t\tint a,b;\n\t\tscanf(\"%d %d\",&a,&b);\n\n\t\tpt.push_back((node){a,b});\n\t\tpt[i-1].id = i;\n\t}\n\tmemset(vis,false,sizeof(vis));\n\tddfs(1);\n\tmemset(vis,false,sizeof(vis));\n\tdfs(1,pt);\n\tfor(int i = 1;i <= n;i ++){\n\t\tprintf(\"%d%c\",ans[i],i==n?'\\n':' ');\n\t}\n\n\treturn 0;\n}\n/*\n10\n4 3\n7 6\n5 7\n9 5\n7 4\n7 8\n2 5\n1 7\n5 10\n\n\n-10 -2\n-5 -7\n-9 9\n8 -8\n0 6\n-10 -7\n-2 0\n7 10\n8 9\n6 1\n*/\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "61ac6acc3e3a121f9e45557e998e32d6", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int MAX_N=1505;\nconst long double EPS=1e-6;\ninline int sgn(double x)\n{\n return std::fabs(x)0?1:-1);\n}\n \nstruct point\n{\n double x,y;\n int id;\n point(){}\n point(double x,double y):x(x),y(y)\n {\n \tid=0;\n }\n inline point operator - (const point &t)\n\t{\n return point(x-t.x,y-t.y);\n }\n inline double operator * (const point &t) const \n\t{\n return x*t.y-y*t.x;\n }\n inline bool operator < (const point &t) const \n\t{\n return !sgn(x-t.x) ? y < t.y : x < t.x;\n }\n inline bool operator == (const point &t) const \n\t{\n return !sgn(x-t.x) && !sgn(y-t.y);\n }\n \n inline double len(){\n return std::sqrt(x*x+y*y);\n }\n \n inline void print(){\n printf(\"%f %f\\n\",x,y);\n }\n}P[MAX_N],PP;\nbool comp(point a,point b)\n{\n return !sgn((a-PP)*(b-PP))?((a-PP).len()<(b-PP).len()):(a-PP)*(b-PP)>0;\n}\nvector g[MAX_N];\nint n;\nint ans[MAX_N];\nint siz[MAX_N];\nvoid dfs(int v,int last)\n{\n\tsiz[v]=1;\n\tfor(int i=0;i>n;\n\tfor(int i=1;i>u>>v;\n\t\tg[u].push_back(v);\n\t\tg[v].push_back(u);\n\t}\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tcin>>P[i].x>>P[i].y;\n\t\tP[i].id=i;\n\t}\n\tdfs(1,0);\n\tDFS(1,0,1,n);\n\tfor(int i=1;i<=n;i++)cout<\n#include\n#include\n#include\nusing namespace std;\ntypedef long long LL;\nLL n,tot,Now,h[3005],e[3005],b[3005],s[3005],f[3005];\nstruct Point\n{\n\tLL v,p,x,y;\n}p[3005];\n\nint cmp1(const Point &a,const Point &b)\n{\n\tLL ax=a.x-p[Now].x,ay=a.y-p[Now].y;\n\tLL bx=b.x-p[Now].x,by=b.y-p[Now].y;\n\treturn (ax*by-ay*bx)>0;\n}\n\nint cmp2(const Point &a,const Point &b)\n{\n\treturn (a.p>n;\n\tLL a,b;\n\tfor (LL i=1;i>a>>b;\n\t\tAdd(--a,--b); Add(b,a);\n\t}\n\tDfs(0);\n\tfor (LL i=0;i>p[i].x>>p[i].y,p[i].p=i;\n\tRec(0,0,n-1);\n\tsort(p,p+n,cmp2);\n\tfor (LL i=0;i\n#define int long long \n#define N 3000\nusing namespace std;\nstruct Point\n{\n int x,y;int id;\n Point(){}\n Point(double _x,double _y){x=_x,y=_y;}\n}now,p[N];\ntypedef Point Vector ;\nVector operator - (Point a,Point b){return Point(a.x-b.x,a.y-b.y);}\ndouble Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}\nint book[N],ans[N],n,sz[N];\nvector g[N];\nbool cmp(Point a,Point b)\n{\n\treturn Cross(a-now,b-now)>0;\n}\n\nvoid dfs(int x,int fa)\n{\n\tsz[x]=1;\n\tfor(int i=0,v;i>n;\n for(int i=1;i\n#include\n#include\n#include\nusing namespace std;\n\n#define maxn 1555\n\nstruct Point{\n long long x,y;\n int idx;\n}point[maxn];\n\nint n;\nvectorg[maxn];\nint sum[maxn];\nint k;\n\nlong long xmulti(Point a,Point b,Point c){ //ab*ac\n return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);\n}\n\nbool cmp(Point a,Point b){\n return xmulti(point[k],a,b)>0;\n}\n\nvoid dfs(int x,int pre){\n\n sum[x]=1;\n for(int i=0;i>n;\n for(int i=0;i>a>>b;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n\n for(int i=0;i>point[i].x>>point[i].y;\n point[i].idx=i;\n }\n\n dfs(1,-1);\n solve(1,-1,0,n-1);\n\n for(int i=0;i\n#include\n#include\n#include\n#include\nusing namespace std;\nconst int Mx=1555;\nint n,X,Y;\nint fir[Mx],nex[Mx<<1],to[Mx<<1],tot;\nvoid addEdge(int x,int y){\n nex[++tot]=fir[x];fir[x]=tot;to[tot]=y;\n}\nint ans[Mx],top,si[Mx];\nstruct Node{\n int x,y,id;\n}node[Mx];\nbool cmp(Node a,Node b){return 1ll *(a.x-X)*(b.y-Y)<1ll *(b.x-X)*(a.y-Y);}\nvoid dfs(int x,int f){\n si[x]++;\n for(int i=fir[x];i;i=nex[i]){\n int v=to[i];\n if(v==f)continue;\n dfs(v,x);\n si[x]+=si[v];\n }\n}\nvoid getXY(int l,int r){\n if(l==r)return;\n X=1e9;\n int id=l;\n for(int i=l;i<=r;i++)if(node[i].x\n#include \n#include \n#include \n#include \n#include \n#include \n\n#define pause cout << \" press any key to continue...\", cin >> chh\n#define file_r(x) freopen(x, \"r\", stdin)\n#define file_w(x) freopen(x, \"w\", stdout)\n\nusing namespace std;\nint chh;\n\ntypedef long long LL;\n\nconst int N = 1505, M = N * 2, INF = ~0U >> 2;\n\nstruct Point {\n int x, y, h;\n} p[N];\n\nstruct NODE {\n int v, next;\n} edge[M];\n\nint n, e, cp;\nint ans[N], size[N], head[N];\nbool ab[N];\n\nLL cross(int x1, int y1, int x2, int y2) {\n return (LL) x1 * y2 - (LL) x2 * y1;\n}\n\nbool cmp(const Point &arg0, const Point &arg1) {\n return cross(arg0.x - p[cp].x, arg0.y - p[cp].y, arg1.x - p[cp].x, arg1.y - p[cp].y) < 0;\n}\n\nvoid init() {\n e = 0;\n fill(head + 1, head + n + 1, -1);\n}\n\nvoid add_edge(int u, int v) {\n edge[e].v = v, edge[e].next = head[u], head[u] = e++;\n}\n\nvoid dfs(int u, int fa) {\n int i, v;\n size[u] = 1;\n for (i = head[u]; i != -1; i = edge[i].next) {\n v = edge[i].v;\n if (v == fa) continue;\n dfs(v, u);\n size[u] += size[v];\n }\n}\n\nvoid solve(int id, int l, int r) {\n int i, j, v = INF;\n for (i = l; i <= r; i++)\n if (p[i].x < v) v = p[i].x, j = i;\n swap(p[l], p[j]);\n cp = l;\n ans[p[cp].h] = id, ab[id] = 1;\n if (l == r) return;\n sort(p + l + 1, p + r + 1, cmp);\n j = l;\n for (i = head[id]; i != -1; i = edge[i].next) {\n v = edge[i].v;\n if (ab[v]) continue;\n solve(v, j + 1, j + size[v]);\n j += size[v];\n }\n}\n\nint main() {\n int i, j, u, v;\n while (scanf(\"%d\", &n) != EOF) {\n init();\n for (i = 1; i < n; i++) {\n scanf(\"%d %d\", &u, &v);\n add_edge(u, v), add_edge(v, u);\n }\n for (i = 1; i <= n; i++) {\n scanf(\"%d %d\", &p[i].x, &p[i].y);\n p[i].h = i;\n }\n dfs(1, -1);\n //cout << \"PPPP\"; pause;\n //for (i = 1; i <= n; i++) cout << size[i] << endl; pause;\n fill(ab + 1, ab + n + 1, 0);\n solve(1, 1, n);\n for (i = 1; i < n; i++) printf(\"%d \", ans[i]);\n printf(\"%d\\n\", ans[n]);\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "10fe5958246669e3b26ec3bb9b6cea40", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\nusing namespace std;\nstruct point64_t {\n\tint64_t x, y, num;\n\tpoint64_t() { x = 0, y = 0, num = 0; }\n\tpoint64_t(int64_t a, int64_t b, int64_t c) {\n\t\tx = a, y = b, num = c;\n\t}\n};\nstruct Vector {\n\tint64_t x, y;\n\tVector() { x = 0, y = 0; }\n\tVector(point64_t a, point64_t b) { x = b.x - a.x, y = b.y - a.y; }\n};\nint64_t skal(Vector a, Vector b) {\n\treturn (a.x * b.x + a.y * b.y);\n}\nint64_t vect(Vector a, Vector b) {\n\treturn (a.x * b.y - a.y * b.x);\n}\npoint64_t minp;\nbool cmp(point64_t& a, point64_t& b) {\n\tVector i(minp, a);\n\tVector j(minp, b);\n\tif (vect(i, j) == 0) {\n\t\treturn skal(i, i) < skal(j, j);\n\t}\n\treturn vect(i, j) > 0;\n}\nvector> g;\nvector sz;\nvector par;\nvoid dfs(int64_t v, int64_t p) {\n\tsz[v] = 1;\n\tpar[v] = p;\n\tfor (auto u : g[v]) {\n\t\tif (u != p) {\n\t\t\tdfs(u, v);\n\t\t\tsz[v] += sz[u];\n\t\t}\n\t}\n}\nvector sootv;\nvoid solve(int64_t root, vector a) {\n\tint64_t n = a.size();\n\tvector, int64_t>> help(n);\n\tfor (int64_t i = 0; i < n; ++i) {\n\t\thelp[i] = { {a[i].x,a[i].y},i };\n\t}\n\tsort(help.begin(), help.end());\n\tminp = a[help[0].second];\n\tvector b;\n\tfor (int64_t i = 0; i < n; ++i) {\n\t\tif (i != help[0].second) b.push_back(a[i]);\n\t}\n\tsort(b.begin(), b.end(), cmp);\n\tsootv[minp.num] = root;\n\tfor (auto u : g[root]) {\n\t\tif (u == par[root]) continue;\n\t\tvector c;\n\t\tfor (int64_t j = 0; j < sz[u]; ++j) {\n\t\t\tc.push_back(b[b.size() - 1]);\n\t\t\tb.pop_back();\n\t\t}\n\t\tsolve(u, c);\n\t}\n}\nint main() {\n\tint64_t n;\n\tcin >> n;\n\tg.resize(n);\n\tsz.resize(n);\n\tpar.resize(n);\n\tsootv.resize(n);\n\tfor (int64_t i = 0; i < n - 1; ++i) {\n\t\tint64_t u, v;\n\t\tcin >> u >> v;\n\t\t--u, --v;\n\t\tg[u].push_back(v);\n\t\tg[v].push_back(u);\n\t}\n\tdfs(0, -1);\n\tvector a(n);\n\tfor (int64_t i = 0; i < n; ++i) {\n\t\tcin >> a[i].x >> a[i].y;\n\t\ta[i].num = i;\n\t}\n\tsolve(0, a);\n\tfor (int64_t i = 0; i < n; ++i) cout << sootv[i] + 1 << ' ';\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "2fe500cce2d8876b8289fc441e3fb155", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\nconst int M = 1500 + 10;\nint n,x,y;\nstruct node {\n\tint xx,yy,id;\n\tbool operator < (const node &a) const {\n\t\tif(xx-x>=0 && a.xx-x<=0) return 1;\n\t\tif(xx-x<=0 && a.xx-x>=0) return 0;\n\t\treturn (yy-y)*(ll)(a.xx-x)<(a.yy-y)*(ll)(xx-x);\n\t}\n}p[M];\n\nvector g[M];\nint sz[M];\nint ans[M];\n\nvoid dfs (int o,int u) {\n\tsz[u] = 1;\n for (int &v : g[u]) if (v!=o) {\n\t\tdfs (u, v);\n\t\tsz[u] += sz[v];\n }\n}\n\nvoid dfs2 (int o,int u,int l,int r) {\n\tif (sz[u]==1) {\n\t\tans[p[l].id] = u;\n\t\treturn;\n\t}\n\tint id=l;\n for (int i=l+1; i\n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long ll;\ntypedef vector vi;\nstruct point{\n point(){};\n point(ll x_, ll y_, int i_):x(x_),y(y_),idx(i_){};\n ll x;\n ll y;\n int idx;\n};\n\n//const point nilpair = make_pair(numeric_limits::min(),numeric_limits::min());\nbool cmpr(const point& l, const point& r){\n ll s = l.x * r.y - l.y * r.x;\n return s > 0;\n}\nvoid place(int vert, vector& p, int l, int r, vi& a, vector& c, vi& sz){\n int root = l;\n if (l > r) return;\n for (int i = l; i < r; ++i){\n if (p[i].y > p[root].y){\n root = i;\n }\n }\n a[p[root].idx] = vert + 1;\n swap(p[root],p[l]);\n if (l == r) return; \n for(int i = l+1; i < r; ++i){\n p[i].x -= p[l].x;\n p[i].y -= p[l].y;\n }\n sort(p.begin() + l + 1, p.begin() + r,cmpr);\n int pos = l+1;\n for(int i = 0; i < c[vert].size(); ++i){\n if (sz[c[vert][i]] < sz[vert]){\n place(c[vert][i], p, pos, pos + sz[c[vert][i]],a,c,sz);\n pos += sz[c[vert][i]];\n }\n }\n}\nint main(void)\n{\n int n;\n cin >> n;\n vector v(n,0);\n vector sz(n,0); \n vector > c(n);\n vector pts(n);\n int i = n-1;\n while(i--){\n int s, d;\n cin >> s >> d;\n c[s-1].push_back(d-1);\n c[d-1].push_back(s-1);\n }\n stack s;\n s.push(0);\n while(!s.empty()){\n int vr = s.top();\n if (0 == v[vr]){\n v[vr] = 1;\n sz[vr] = 1;\n }\n for(int i = 0; i < c[vr].size(); ++i){\n if (0 == v[c[vr][i]]){\n s.push(c[vr][i]);\n break;\n }\n }\n if (vr == s.top()){\n v[vr] = 2;\n s.pop();\n for(int i = 0; i < c[vr].size(); ++i){\n if (2 == v[c[vr][i]]) sz[vr] += sz[c[vr][i]];\n }\n }\n }\n for(int i = 0; i < n; i++){\n ll x, y;\n cin >> x >> y;\n pts[i] = point(x, y, i);\n }\n place(0, pts, 0, pts.size(), v, c, sz);\n for(int i = 0; i < n; ++i){\n cout << v[i] <<' ';\n }\n cout << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "876eed697d96cb9580569ff6112d1c46", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nstruct Point {\n\tdouble x, y;\n\tint num;\n\tPoint() {}\n\tPoint(double _x, double _y, int _num): x(_x), y(_y), num(_num) {}\n\tvoid read() {\n\t\tscanf(\"%lf%lf\", &x, &y);\n\t}\n\tdouble operator * (const Point &a) const {\n\t\treturn x * a.y - a.x * y;\n\t}\n\tPoint operator - (const Point &a) const {\n\t\treturn Point(x - a.x, y - a.y, num);\n\t}\n\tbool operator < (const Point &a) const {\n\t\treturn y < a.y || y == a.y && x < a.x;\n\t}\n\tbool operator == (const Point &a) const {\n\t\treturn x == a.x && y == a.y;\n\t}\n} a[2000], s;\nint sum = 0, cnt = 0, n, ans[2000], sz[2000];\nint des[4000], nxt[4000], fst[2000];\nint c[2000], nxtc[2000], fstc[2000];\nbool vis[2000];\n\nvoid add(int x, int y) {\n\tdes[++sum] = y, nxt[sum] = fst[x], fst[x] = sum;\n}\nvoid add2(int x, int y) {\n\tc[++cnt] = y, nxtc[cnt] = fstc[x], fstc[x] = cnt;\n}\nvoid tree(int x) {\n\tvis[x] = 1;\n\tsz[x] = 1;\n\tfor (int i = fst[x]; i; i = nxt[i])\n\t\tif (!vis[des[i]]) tree(des[i]), add2(x, des[i]), sz[x] += sz[des[i]];\n}\ndouble cross(Point a, Point b, Point c) {\n\treturn (a - c) * (b - c);\n}\nbool cmp(Point i, Point j) {\n\tif (i == s) return 1;\n\tif (j == s) return 0;\n\tdouble y = cross(i, j, s);\n\tif (y < 0) return 0;\n\treturn 1;\n}\nvoid solve(vectorb, int fa) {\n\ts = b[0];\n\tfor (int i = 1; i < b.size(); ++i) if (b[i] < s) s = b[i];\n\tsort(b.begin(), b.end(), cmp);\n\tint l = 1;\n\tans[s.num] = fa;\n\tfor (int i = fstc[fa]; i; i = nxtc[i]) {\n\t\tint k = c[i];\n\t\tvectorm;\n\t\tfor (int j = 1; j <= sz[k]; ++j) m.push_back(b[l]), l++;\n\t\tsolve(m, k);\n\t}\n}\nint main() {\n\tscanf(\"%d\", &n);\n\tfor (int i = 1; i < n; ++i) {\n\t\tint x, y;\n\t\tscanf(\"%d%d\", &x, &y);\n\t\tadd(x, y), add(y, x);\n\t}\n\tmemset(vis, 0, sizeof vis);\n\ttree(1);\n\tvectorb;\n\tfor (int i = 1; i <= n; ++i) a[i].read(), a[i].num = i, b.push_back(a[i]);\n\tsolve(b, 1);\n\tfor (int i = 1; i <= n; ++i) printf(\"%d \", ans[i]);\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "bf88c7edfe8eb063475c9f2d14f47add", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nconst int maxn=1520;\nint n,X,Y;\nstruct PO {\n\tint x,y,d;\n\tbool operator<(const PO &o) const {\n\t\tif(x-X>=0&&o.x-X<=0) return 1;\n\t\tif(x-X<=0&&o.x-X>=0) return 0;\n\t\treturn (y-Y)*(long long)(o.x-X)<(o.y-Y)*(long long)(x-X);\n\t}\n}p[maxn];\nvector g[maxn];\nbool vis[maxn];\nint sz[maxn],o[maxn];\nint dfs(int u)\n{\n\tvis[u]=true;\n\tsz[u]=1;\n\tint ret=0;\n\tfor(int i=0,j=g[u].size();i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define pb push_back\n#define SIZE(x) (int)x.size()\n\n#define F first\n#define S second\n#define MP make_pair\ntypedef pair PII;\ntypedef pair PIII;\n\nint n;\nbool adj[1501][1501];\nint vtx[1501];\nvector chk;\nvector points;\nvector res;\n \nint dfs(int node)\n{\n int cnt = 0;\n chk[node] = true;\n for(int i = 1;i <= n;++i) \n if(!chk[i] && adj[node][i])\n cnt += dfs(i);\n vtx[node] = cnt+1;\n return vtx[node];\n}\n \nvoid go(int root, vector ps)\n{\n int min_y = INT_MAX, min_i = -1;\n for(int i = 1;i <= ps.size()-1;++i) if(ps[i].F.S < min_y) {min_y = ps[i].F.S; min_i = i;}\n res.push_back(MP(ps[min_i].S,root));\n vector > tps(1);\n for(int i = 1;i <= ps.size()-1;++i) if(i != min_i) tps.push_back(MP(atan2l(ps[i].first.first-ps[min_i].first.first,ps[i].first.second-ps[min_i].first.second),i));\n sort(tps.begin()+1,tps.end());\n int s = 1;\n for(int i = 1;i <= n;++i) if(adj[root][i]) {\n vector nps(1);\n while(nps.size() < vtx[i]+1) {\n nps.push_back(ps[tps[s].S]);\n ++s;\n }\n adj[i][root] = false;\n go(i,nps);\n }\n}\n \nint main()\n{\n#ifdef LOCAL\n\tfreopen(\"in.txt\", \"r\", stdin);\n#endif\n cin >> n;\n for(int i;i> a >> b;\n adj[a][b] = adj[b][a] = true;\n }\n \n for(int i;i> x >> y;\n points[i+1] = MP(MP(x,y),i+1);\n }\n \n dfs(1);\n \n sort(points.begin()+1,points.end());\n for(int i;i\n#include \n#include \n#include \n#include \nusing namespace std;\nconst int MAXN = 1510*2;\nint n, edge, size[MAXN], ans[MAXN];\nint h[MAXN], g[MAXN], next[MAXN];\nstruct node {\n\tint x, y, k;\n} v[MAXN];\n\nvoid ins(int a, int b) {\n\tnext[++edge]=h[a];\n\th[a]=edge;\n\tg[edge]=b;\n}\n\nvoid dfs(int x) {\n\tsize[x] = 1;\n\tfor (int i=h[x];i;i=next[i])\n\t\tif (size[g[i]]==0) {\n\t\t\tdfs(g[i]);\n\t\t\tsize[x] += size[g[i]];\n\t\t}\n}\n\nbool cmp(const node &a, const node &b) {\n\tlong long t = (long long)a.x*(long long)b.y -\n\t\t\t\t (long long)a.y*(long long)b.x;\n\treturn t>0;\n}\n\nvoid dfs(int root, int l, int r) {\n\tif ( l>r ) return;\n\tnode x = v[l];\n\tint best = l;\n\tfor (int i=l;i<=r;i++)\n\t\tif ( v[i].y < x.y ) {\n\t\t\tx = v[i];\n\t\t\tbest = i;\n\t\t}\n\tans[x.k] = root;\n\tswap(v[best], v[l]);\n\t\n\tif (l==r) return;\n\tfor (int i=l+1;i<=r;i++)\n\t\tv[i].x-=v[l].x, v[i].y-=v[l].y;\n\tsort(v+l+1, v+r+1, cmp);\n\t\n\tint now = l+1;\n\tfor (int i=h[root];i;i=next[i])\n\t\tif (size[g[i]]\n#define FOR(i,a,b) for(int i=(a),i##_end_=(b);i<=i##_end_;i++)\n#define DOR(i,a,b) for(int i=(a),i##_end_=(b);i>=i##_end_;i--)\n#define ll long long\n#define M 1505\nusing namespace std;\n\nint n;\nint sz[M];\nint Ans[M];\nint Id[M];\n\nvectorG[M];\n\nstruct node{\n\tint x,y;\n\tint id;\n\t\n\tbool operator <(const node &a)const{\n\t\treturn x1ll*(b.x-tmp.x)*(a.y-tmp.y);\n}\n\nvoid dfs(int x,int f){\n\tsz[x]=1;\n\tfor(int i=0;i<(int)G[x].size();i++){\n\t\tif(G[x][i]==f){\n\t\t\tId[x]=i;\n\t\t\tcontinue;\n\t\t}\n\t\tdfs(G[x][i],x);\n\t\tsz[x]+=sz[G[x][i]];\n\t}\n}\n\nvoid solve(int L,int R,int f){\n\tsort(A+L,A+R+1);\n\tAns[A[L].id]=f;\n\tif(L==R)return;\n\ttmp=A[L];\n\tsort(A+L+1,A+R+1,cmp);\n\tint st=L+1;\n\tint tot=0;\n\tif(tot==Id[f])tot++;\n\tint size=0;\n\tFOR(i,L+1,R){\n\t\tsize++;\n\t\tif(size==sz[G[f][tot]]){\n\t\t\tsolve(st,i,G[f][tot]);\n\t\t\tst=i+1;\n\t\t\ttot++;\n\t\t\tif(tot==Id[f])tot++;\n\t\t\tsize=0;\n\t\t}\n\t}\n}\n\nint main(){\n\tscanf(\"%d\",&n);\n\tFOR(i,1,n-1){\n\t\tint a,b;\n\t\tscanf(\"%d%d\",&a,&b);\n\t\tG[a].push_back(b);\n\t\tG[b].push_back(a);\n\t}\n\tmemset(Id,-1,sizeof(Id));\n\tFOR(i,1,n){\n\t\tscanf(\"%d%d\",&A[i].x,&A[i].y);\n\t\tA[i].id=i;\n\t}\n\tdfs(1,0);\n\tsolve(1,n,1);\n\tFOR(i,1,n)printf(\"%d \",Ans[i]);\n\tputs(\"\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "c29bae282938f19382db906439073743", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#define inf 1000000007LL\n#define INF 1000000000000000007LL\n#define endl cout<<'\\n'\n#define endr cerr<<'\\n'\n#define pb push_back\n#define mp make_pair\n#define eb emplace_back\n#define st first\n#define nd second\n#define mxe max_element\n#define mne min_element\n#define bit(i) (1LL<<(i))\n#define log(x) (63-__builtin_clzll(x))\n#define ll long long\n#define pii pair\n#define pdd pair\n#define vi vector\n#define vd vector\n#define vll vector\n#define vpii vector< pair >\n#define sz(x) ((int)(x).size())\n#define all(x) (x).begin(),(x).end()\n#define rep(i, n) for(int i=0; i<(n); ++i)\n#define rep1(i, n) for(int i=1; i<=(n); ++i)\n#define pv(x) cerr<<#x<<\": \"<<(x)<<'\\n'\n#define ps(x) cerr<<#x<<\": \";for(auto q:(x)){cerr< > sorted;\n for (auto p : points)\n {\n if (p == pRoot) continue;\n int xDif = x[p] - x[pRoot], yDif = y[p] - y[pRoot];\n sorted.eb(atan2(yDif+.0, xDif+.0), p);\n }\n\n sort(all(sorted));\n\n for (auto u : g[vRoot])\n {\n if (u == par[vRoot]) continue;\n vi forSubtree;\n while (sz(forSubtree) < sub[u])\n {\n forSubtree.pb(sorted.back().nd); sorted.pop_back();\n }\n build(u, forSubtree);\n }\n}\n\n\nvoid solve()\n{\n cin>>n;\n rep1(i, n-1)\n {\n int a, b; cin>>a>>b;\n g[a].pb(b);\n g[b].pb(a);\n }\n\n DFStree(1);\n\n rep1(i, n) cin>>x[i]>>y[i], allPoints.pb(i);\n\n build(1, allPoints);\n\n rep1(i, n) cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define D(x) cerr << #x << \" = \" << x << endl\n#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)\n#define FOREACH(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)\n#define ALL(v) (v).begin(), (v).end()\n#define mp(x,y) make_pair(x, y)\n#define pb(x) push_back(x)\n\ntypedef long long int64;\n\nconst int INF = (int)(1e9);\nconst int64 INFLL = (int64)(1e18);\nconst double EPS = 1e-13;\n\nusing namespace std;\n\ntypedef vector vi;\n\nconst int MAXN = 1500 + 10;\n\nint X[MAXN], Y[MAXN], ans[MAXN], size[MAXN];\nvi G[MAXN];\n\nint bfs(int v, int parent) {\n size[v] = 1;\n REP(i, 0, G[v].size()) {\n int u = G[v][i];\n if(u != parent) {\n size[v] += bfs(u, v);\n }\n }\n return size[v];\n}\n\nbool cmp(int i, int j) {\n return (int64)X[i] * Y[j] - (int64)X[j] * Y[i] < 0;\n}\n\nvoid go(vi p, int v, int parent) {\n int min_i = 0;\n REP(i, 0, p.size()) {\n if (mp(X[p[i]], Y[p[i]]) < mp(X[p[min_i]], Y[p[min_i]]))\n min_i = i;\n }\n\n swap(p[0], p[min_i]);\n\n REP(i, 0, p.size()) {\n if(i != 0) {\n X[p[i]] -= X[p[0]];\n Y[p[i]] -= Y[p[0]];\n }\n }\n\n sort(p.begin() + 1, p.end(), cmp);\n\n REP(i, 0, p.size()) {\n if(i != 0) {\n X[p[i]] += X[p[0]];\n Y[p[i]] += Y[p[0]];\n }\n }\n\n int pos = 1, u;\n ans[p[0]] = v;\n\n REP(i, 0, G[v].size()) {\n u = G[v][i];\n if(u != parent) {\n vi q;\n REP(i, 0, size[u]) {\n q.pb(p[pos++]);\n }\n go(q, u, v);\n }\n }\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n int n, u, v;\n cin >> n;\n\n REP(i, 0, n-1) {\n cin >> u >> v;\n u--, v--;\n G[u].pb(v); G[v].pb(u);\n }\n\n vi p;\n REP(i, 0, n) {\n cin >> X[i] >> Y[i];\n p.pb(i);\n }\n\n bfs(0, -1);\n go(p, 0, -1);\n\n REP(i, 0, n) {\n if(i) cout << ' ';\n cout << (ans[i] + 1);\n }\n cout << endl;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "62f5c7fc0630f21afd39ec5867ee692e", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\nconst int MAXN = 1510*2;\nint n, edge, size[MAXN], ans[MAXN];\nint h[MAXN], g[MAXN], next[MAXN];\nstruct node {\n\tint x, y, k;\n} v[MAXN];\n\nvoid ins(int a, int b) {\n\tnext[++edge]=h[a];\n\th[a]=edge;\n\tg[edge]=b;\n}\n\nvoid dfs(int x) {\n\tsize[x] = 1;\n\tfor (int i=h[x];i;i=next[i])\n\t\tif (size[g[i]]==0) {\n\t\t\tdfs(g[i]);\n\t\t\tsize[x] += size[g[i]];\n\t\t}\n}\n\nbool cmp(const node &a, const node &b) {\n\tlong long t = (long long)a.x*(long long)b.y -\n\t\t\t\t (long long)a.y*(long long)b.x;\n\treturn t>0;\n}\n\nvoid dfs(int root, int l, int r) {\n\tif ( l>r ) return;\n\tnode x = v[l];\n\tint best = l;\n\tfor (int i=l;i<=r;i++)\n\t\tif ( v[i].y < x.y ) {\n\t\t\tx = v[i];\n\t\t\tbest = i;\n\t\t}\n\tans[x.k] = root;\n\tswap(v[best], v[l]);\n\t\n\tif (l==r) return;\n\tfor (int i=l+1;i<=r;i++)\n\t\tv[i].x-=v[l].x, v[i].y-=v[l].y;\n\tsort(v+l+1, v+r+1, cmp);\n\t\n\tint now = l+1;\n\tfor (int i=h[root];i;i=next[i])\n\t\tif (size[g[i]]\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nstruct pkt\n{\n ll x, y;\n int nr;\n};\n\nint n;\n\nvoid dfs(vector< vector >& T, vector& wlk, int akt, int oj)\n{\n for (int i = 0; i < T[akt].size(); ++i)\n if (T[akt][i] != oj)\n dfs(T, wlk, T[akt][i], akt);\n if (T[akt].size() == 1 && akt != 0)\n wlk[akt] = 1;\n else\n {\n wlk[akt] = 1;\n for (int i = 0; i < T[akt].size(); ++i)\n if (T[akt][i] != oj)\n wlk[akt] += wlk[T[akt][i]];\n }\n}\n\nint Cwiartka(pkt a)\n{\n if (a.x > 0 && a.y >= 0)\n return 0;\n if (a.x <= 0 && a.y > 0)\n return 1;\n if (a.x < 0 && a.y <= 0)\n return 2;\n if (a.x >= 0 && a.y < 0)\n return 3;\n}\n\nbool porownanie(pkt a, pkt b)\n{\n int cwA = Cwiartka(a);\n int cwB = Cwiartka(b);\n if (cwA != cwB)\n {\n if (cwA < cwB)\n return true;\n return false;\n }\n else\n {\n ll ay = abs(a.y), ax = abs(a.x);\n ll by = abs(b.y), bx = abs(b.x);\n if (cwA == 0 || cwA == 2)\n {\n if (ay * bx < ax * by)\n return true;\n return false;\n }\n else\n {\n if (ay * bx > ax * by)\n return true;\n return false;\n }\n }\n}\n\nvoid Sortuj(vector& p, pkt s)\n{\n for (int i = 0; i < p.size(); ++i)\n {\n p[i].x -= s.x;\n p[i].y -= s.y;\n }\n sort(p.begin(), p.end(), porownanie);\n for (int i = 0; i < p.size(); ++i)\n {\n p[i].x += s.x;\n p[i].y += s.y;\n }\n}\n\nvoid Solve(vector< vector >& T, vector p, vector& wlk,\n int akt, int oj, vector& odp)\n{\n pkt sr = {-1, -1, -1};\n int x = -1;\n for (int i = 0; i < p.size(); ++i)\n {\n if (x == -1 || p[i].y < sr.y || (p[i].y == sr.y && p[i].x < sr.x))\n {\n sr = p[i];\n x = i;\n }\n }\n vector p2;\n for (int i = 0; i < p.size(); ++i)\n if (i != x)\n p2.push_back(p[i]);\n p = p2;\n odp[sr.nr] = akt;\n Sortuj(p, sr);\n int nr = 0;\n for (int i = 0; i < T[akt].size(); ++i)\n {\n if (T[akt][i] != oj)\n {\n vector t;\n for (int j = nr; j <= nr + wlk[T[akt][i]] - 1; ++j)\n t.push_back(p[j]);\n Solve(T, t, wlk, T[akt][i], akt, odp);\n nr += wlk[T[akt][i]];\n }\n }\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin >> n;\n vector< vector > T(n);\n for (int i = 0; i < n - 1; ++i)\n {\n int a, b;\n cin >> a >> b;\n --a;\n --b;\n T[a].push_back(b);\n T[b].push_back(a);\n }\n vector P(n);\n for (int i = 0; i < n; ++i)\n {\n cin >> P[i].x >> P[i].y;\n P[i].nr = i;\n }\n vector wlk(n);\n dfs(T, wlk, 0, -1);\n vector odp(n);\n Solve(T, P, wlk, 0, -1, odp);\n for (int i = 0; i < n; ++i)\n cout << odp[i] + 1 << \" \";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "fdbcabb704db0d81a49a89be21477537", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n#define ll long long\nconst int maxn = 2e3+10;\ntypedef pair pii;\ntypedef pair pdd;\n#define fir first\n#define se second\nconst int N = 2e3 + 7;\nconst double eps = 1e-8;\nint n, ans[N], sz[N];\nstruct Pt{ \n\tPt(double x = 0,double y = 0, int id = 0) : x(x),y(y){}\n\tdouble x, y;\n \tint id; \n};\nvector g[N];\nPt pts[N];\ninline int dcmp(double x) { return x < -eps ? -1 : x > eps; }\ndouble det(const Pt &a,const Pt &b) { return a.x * b.y - a.y * b.x; }\nPt operator-(Pt a, Pt b) { return Pt(a.x - b.x, a.y - b.y); }\nPt operator+(Pt a, Pt b) { return Pt(a.x + b.x, a.y + b.y); }\ndouble norm(Pt p){ return sqrt(p.x*p.x + p.y*p.y); }\ndouble dist(Pt a,Pt b){ return norm(a-b); }\nint base;\nbool cmp(const Pt& a,const Pt& b){ \n\tdouble t = det(a-pts[base],b-pts[base]);\n\tif(fabs(t) < eps) return dist(a,pts[base]) < dist(b,pts[base]);\n\treturn t > eps; \n}\n\nvoid dfs1(int u, int fa){\n\tsz[u] = 1; for(int v : g[u]) if(v != fa) dfs1(v, u), sz[u] += sz[v];\n}\n\nvoid dfs2(int u, int fa, int pos, int end){\n\tfor(int i = pos+1; i < end; ++ i){\n\t\tpii a(pts[pos].x, pts[pos].y), b(pts[i].x, pts[i].y);\n\t\tif(b < a)\n\t\t\tswap(pts[pos],pts[i]);\n\t}\n\tans[pts[pos].id] = u;\n\tbase = pos;\n\tsort(pts+pos+1, pts+end, cmp);\n\tfor(int v : g[u]) if(v != fa) {\n\t\tdfs2(v, u, pos+1, pos+sz[v]+1);\n\t\tpos += sz[v];\n\t}\n}\n\nint main(){\n\tscanf(\"%d\", &n);\n\tfor(int i = 1; i < n; ++ i){\n\t\tint u, v; \n\t\tscanf(\"%d%d\", &u, &v);\n\t\tg[u].push_back(v);\n\t\tg[v].push_back(u);\n\t}\n\tfor(int i = 0; i < n; ++ i){\n\t\tdouble x, y; \n\t\tscanf(\"%lf%lf\", &pts[i].x, &pts[i].y); pts[i].id = i+1;\n\t}\n\tdfs1(1, -1);\n\tdfs2(1, -1, 0, n);\n\tfor(int i = 1; i <= n; ++ i)\n\t\tprintf(\"%d \", ans[i]);\n\tputs(\"\");\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "fc223905af0cc434c495944cc92b77e2", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nbool vis[1501];\nint n, x233, y233, nume, h[1501], size[1501], point[1501];\nstruct edge { int v, nxt; } e[3001];\nstruct point {\n int x, y, id;\n bool operator < (const point &a) const {\n if (x >= x233 && a.x <= x233) return true;\n if (x <= x233 && a.x >= x233) return false;\n return 1ll * (a.x - x) * (y - y233) < 1ll * (a.y - y) * (x - x233);\n }\n} p[1501];\nvoid add_edge(int u, int v) {\n e[++nume].v = v, e[nume].nxt = h[u], h[u] = nume;\n e[++nume].v = u, e[nume].nxt = h[v], h[v] = nume;\n}\nint dfs(int u) {\n int ret = 0; vis[u] = true, size[u] = 1;\n for(int i = h[u]; i; i = e[i].nxt)\n if (!vis[e[i].v]) ret += dfs(e[i].v);\n size[u] += ret;\n return size[u];\n}\nvoid init(int u, int l, int r) {\n int t = l; vis[u] = true;\n for(int i = l + 1; i <= r; ++i)\n if ((p[i].y < p[t].y) || (p[t].y == p[i].y && p[i].x < p[t].x)) t = i;\n if (t != l) swap(p[l], p[t]);\n point[p[l].id] = u, x233 = p[l].x, y233 = p[l].y;\n sort(p + l + 1, p + r + 1);\n int pnt = l + 1;\n for (int i = h[u]; i; i = e[i].nxt)\n if (!vis[e[i].v]) {\n init(e[i].v, pnt, pnt + size[e[i].v] - 1);\n pnt += size[e[i].v];\n }\n}\nint main() {\n scanf(\"%d\", &n);\n for(int i = 0; i < n - 1; ++i) {\n scanf(\"%d%d\", &x233, &y233);\n add_edge(x233, y233);\n }\n for(int i = 1; i <= n; ++i) {\n scanf(\"%d%d\", &x233, &y233);\n p[i].x = x233, p[i].y = y233, p[i].id = i;\n }\n dfs(1), memset(vis, false, sizeof vis), init(1, 1, n);\n for(int i = 1; i <= n; ++i) printf(\"%d \", point[i]);\n printf(\"\\n\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "54fffb5796177dbd5dc4747c04e0ad9f", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\nBy Tianyi Chen. All rights reserved.\nDate: 2016-07-24\n*/\n#include\nusing namespace std;\nvectorte[1501];bool visited[1501];\nint n,_,__,subsize[1501],ans[1501];\nstruct point { int64_t x,y;int id;void read() { scanf(\"%lld%lld\",&x,&y); } }p[1501];\nint dfs(int i) {\n int rt=1;visited[i]=1;\n for (auto&&x:te[i])if (!visited[x])rt+=dfs(x);\n return subsize[i]=rt;\n}\nvoid build(int i,int b) {\n int est=b+subsize[i];\n visited[i]=1;\n swap(*min_element(p+b,p+b+subsize[i],[](const point&i,const point&j) {return i.y!=j.y?i.y\n\nusing namespace std;\n\ntypedef long long int LL;\ntypedef long double LD;\ntypedef pair pii;\ntypedef pair pll;\ntypedef complex Point;\n\n#define PB push_back\n#define MP make_pair\n#define L first\n#define R second\n#define sz(x) ((int)(x).size())\n#define smax(x, y) ((x) = max((x), (y)))\n#define smin(x, y) ((x) = min((x), (y)))\n#define all(x) x.begin(),x.end()\n\nconst int maxn = 1500 + 85 - 69;\nvector adj[maxn];\nPoint P[maxn];\nint vec[maxn][maxn],\n\tvecsz[maxn],\n\tdown[maxn],\n\tans[maxn],\n\tn, cmpvert;\n\ninline LL cross(Point x, Point y){\n\treturn x.real() * y.imag() - x.imag() * y.real();\n}\n\nbool cmp(int i, int j){\n\treturn cross(P[i] - P[cmpvert], P[j] - P[cmpvert]) > 0;\n}\n\nint rightmost(int id){\n\tint ret = vec[id][0];\n\tfor(int i = 0; i < vecsz[id]; i++){\n\t\tint val = vec[id][i];\n\t\tif(P[val].real() > P[ret].real())\n\t\t\tret = val;\n\t}\n\treturn ret;\n}\n\nint dfs_preprocess(int v, int p = -1){\n\tdown[v] = 1;\n\tfor(auto u : adj[v])\n\t\tif(u != p)\n\t\t\tdown[v] += dfs_preprocess(u, v);\n\treturn down[v];\n}\n\nvoid dfs(int v, int vecid, int p = -1){\n\tint fv = rightmost(vecid);\n\tans[fv] = v;\n\tint idtmp = find(vec[vecid], vec[vecid] + vecsz[vecid], fv) - vec[vecid];\n\tswap(vec[vecid][idtmp], vec[vecid][vecsz[vecid] - 1]);\n\tvecsz[vecid]--;\n\tcmpvert = fv;\n\tsort(vec[vecid], vec[vecid] + vecsz[vecid], cmp);\n\tint ptr = 0;\n\tfor(int i = 0; i < sz(adj[v]); i++) if(adj[v][i] != p){\n\t\tint u = adj[v][i];\n\t\tfor(int j = ptr; j < ptr + down[u]; j++)\n\t\t\tvec[vecid + 1][j - ptr] = vec[vecid][j];\n\t\tvecsz[vecid + 1] = down[u];\n\t\tptr += down[u];\n\t\tdfs(u, vecid + 1, v);\n\t}\n\treturn;\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcin >> n;\n\tfor(int i = 1; i < n; i++){\n\t\tint fi, se;\n\t\tcin >> fi >> se;\n\t\tfi--, se--;\n\t\tadj[fi].PB(se);\n\t\tadj[se].PB(fi);\n\t}\n\tfor(int i = 0; i < n; i++){\n\t\tLL xx, yy;\n\t\tcin >> xx >> yy;\n\t\tP[i] = Point(xx, yy);\n\t\tvec[0][vecsz[0]++] = i;\n\t}\n\tdfs_preprocess(0);\n\tdfs(0, 0);\n\tfor(int i = 0; i < n; i++)\n\t\tcout << ans[i] + 1 << ' ';\n\tcout << '\\n';\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "3d135d5235440e7b5d43dcafb3777aa1", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\nconst int MAXN = 1510, MAXM = 1510;\nint n, m, sum;\nint ans[MAXN], son[MAXN], last[MAXN];\nstruct Edge {\n\tint link, to;\n} edge[MAXM << 1];\nstruct Node {\n\tint real;\n\tll x, y;\n} a[MAXN], tmp;\n\ninline void add(int u, int v) {\n\tedge[++sum] = (Edge) { v, last[u] }, last[u] = sum;\n\treturn;\n}\n\ninline int cmp1(Node a, Node b) {\n\treturn a.x < b.x || a.x == b.x && a.y < b.y;\n}\n\ninline int cmp2(Node P1, Node P2) {\n\treturn (tmp.x - P1.x) * (tmp.y - P2.y) < (tmp.x - P2.x) * (tmp.y - P1.y);\n}\n\nvoid dfs1(int x, int fa) {\n\tson[x] = 1;\n\tfor (int i = last[x]; i; i = edge[i].to) {\n\t\tint y = edge[i].link;\n\t\tif (y == fa) continue;\n\t\tdfs1(y, x);\n\t\tson[x] += son[y];\n\t}\n\treturn;\n}\n\nvoid dfs2(int x, int fa, int l, int r) {\n\tsort(a + l, a + r + 1, cmp1);\n\t// for (register int i = l; i <= r; ++i) printf(\"%d \",a[i].real); puts(\"\");puts(\"\");\n\tans[a[l].real] = x, tmp = a[l];\n\tsort(a + l + 1, a + r + 1, cmp2);\n\tint L = l + 1;\n\tfor (int i = last[x]; i; i = edge[i].to) {\n\t\tint y = edge[i].link;\n\t\tif (y == fa) continue;\n\t\tdfs2(y, x, L, L + son[y] - 1), L += son[y];\n\t}\n\treturn;\n}\n\nint main() {\n\tscanf(\"%d\", &n);\n\tfor (register int i = 1; i < n; ++i) {\n\t\tint u, v; scanf(\"%d%d\", &u, &v);\n\t\tadd(u, v), add(v, u);\n\t}\n\tfor (register int i = 1; i <= n; ++i) { scanf(\"%lld%lld\", &a[i].x, &a[i].y); a[i].real = i; }\n\tdfs1(1, 0);\n\tdfs2(1, 0, 1, n);\n\tfor (register int i = 1; i < n; ++i) printf(\"%d \", ans[i]);\n\tprintf(\"%d\\n\", ans[n]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "940cba906ffce193a7014827fb027dd0", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef pair pii;\n\nvector > a;\nbool vis[2000];\nvector res;\npii pnt;\nvector> point;\n\nbool comp(pair a,pair b)\n{\n\tdouble x=pnt.first;\n\tdouble y=pnt.second;\n\tdouble alp1=(a.first.second-y)/sqrt((a.first.first-x)*(a.first.first-x)+(a.first.second-y)*(a.first.second-y));\n\tdouble alp2=(b.first.second-y)/sqrt((b.first.first-x)*(b.first.first-x)+(b.first.second-y)*(b.first.second-y));\n\treturn (alp1 >::iterator b,vector >::iterator en)\n{\n\tvis[v]=true;\n\tvector>::iterator p=b;\n\tfor (vector >::iterator it=b+1;it!=en;it++)\n\t{\n\t\tif (it->first.firstfirst.first)\n\t\t\tp=it;\n\t\telse\n\t\t\tif (it->first.first==p->first.first && it->first.secondfirst.second)\n\t\t\t\tp=it;\n\t}\n\tres[p->second]=v+1;\n\tpnt=p->first;\n\tswap((*p),(*b));\n\tb++;\n\tsort(b,en,comp);\n\tint cnt=0,cur;\n\tfor (int i=0;i> n;\n\ta.resize(n);\n\tint u,v;\n\tfor (int i=0;i\n#include \n\n#define uint unsigned int\n#define ll long long\n#define ull unsigned long long\n#define ld long double\n#define rep(i, l, r) for (int i = l; i < r; i++)\n#define repb(i, r, l) for (int i = r; i > l; i--)\n#define sz(a) (int)a.size()\n#define fi first\n#define se second\n#define mp(a, b) make_pair(a, b)\n\nusing namespace std;\n\nconst int N = 1500;\nconst int inf = (int)1e9 + 1;\nconst ll big = (ll)1e18 + 1;\nconst int P = 239;\nconst int MOD = (int)1e9 + 7;\nconst int MOD1 = (int)1e9 + 9;\nconst double eps = 1e-9;\nconst double pi = atan2(0, -1);\nconst int ABC = 26;\n\nstruct Point\n{\n ll x, y;\n int id;\n Point() {}\n Point(ll x, ll y) : x(x), y(y) {}\n};\n\nPoint operator - (Point p1, Point p2) { return Point(p1.x - p2.x, p1.y - p2.y); }\n\nll operator * (Point p1, Point p2) { return p1.x * p2.x + p1.y * p2.y; }\n\nll operator % (Point p1, Point p2) { return p1.x * p2.y - p2.x * p1.y; }\n\nPoint a[N];\nvector g[N];\nint sz[N];\nint ans[N];\n\nvoid dfs(int u, int par)\n{\n sz[u] = 1;\n for (int v : g[u])\n if (v != par)\n {\n dfs(v, u);\n sz[u] += sz[v];\n }\n}\n\nvoid solve(int u, int par, int l, int r)\n{\n ans[a[l].id] = u;\n int pos = l + 1;\n for (int v : g[u])\n if (v != par)\n {\n sort(a + pos + 1, a + pos + sz[v], [a, pos](Point p1, Point p2) { return (p1 - a[pos]) % (p2 - a[pos]) > 0; });\n solve(v, u, pos, pos + sz[v]);\n pos += sz[v];\n }\n}\n\nint main()\n{\n //freopen(\"a.in\", \"r\", stdin);\n //freopen(\"a.out\", \"w\", stdout);\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.precision(20);\n //ll TL = 0.95 * CLOCKS_PER_SEC;\n //clock_t time = clock();\n int n;\n cin >> n;\n rep(i, 0, n - 1)\n {\n int u, v;\n cin >> u >> v;\n u--; v--;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n dfs(0, -1);\n rep(i, 0, n)\n {\n cin >> a[i].x >> a[i].y;\n a[i].id = i;\n }\n int f = 0;\n rep(i, 0, n)\n if (a[i].y < a[f].y || a[i].y == a[f].y && a[i].x < a[f].x)\n f = i;\n swap(a[0], a[f]);\n sort(a + 1, a + n, [a](Point p1, Point p2) { return (p1 - a[0]) % (p2 - a[0]) > 0; });\n solve(0, -1, 0, n);\n rep(i, 0, n)\n cout << ans[i] + 1 << \" \";\n cout << \"\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "46124a791b24a288d6cd34aa85a0779d", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\nhttp://codeforces.com/problemset/problem/196/C\nCodeforces Round #124 (Div. 1)\nC. Paint Tree\n2012.6.12\n����Ŀ���⡻��һ������ƽ����n�������꣬����ΰ����еĵ㻭��ƽ���ϡ�\n���ⷨ����\n�������Ե㰴������������ν�����\n*/\n#include\n#include\n#include\n#include\nusing namespace std;\nconst int maxn=1501;\ntypedef long long ll;\nstruct arr{\n int x,y,z;\n}d[maxn];\nint a[maxn*2][2],b[maxn],f[maxn],v[maxn];\nint n,s;\n\nvoid add(int i,int j){\n a[++s][0]=b[i];\n b[i]=s;\n a[s][1]=j;\n}\n\nvoid dfs(int k){\n int i,j;\n f[k]=1;\n for (i=b[k];i;i=a[i][0]){\n j=a[i][1];\n if (!f[j]){\n dfs(j);\n f[k]+=f[j];\n }\n }\n}\n\nbool cmp(arr i,arr j){\n return ((ll)i.x*j.y-(ll)i.y*j.x)>0;\n}\n\nvoid dfs(int k,int l,int r){\n int i,j=r,s;\n if (l>r) return ;\n for (i=l;i\n#include\n#include\n#include\n\nusing namespace std;\n\n#define For(i, n) for(int i = 0; i<(n); ++i)\n#define ForEach(it, i) for(typeof i.begin() it = i.begin(); it!=i.end(); ++it)\n#define eps 1e-7\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef long double real;\n\nint N;\nvector E[1547];\nvector B[1547];\nint velkosti[1547];\nint otec[1547];\nint pridelenie[1547];\nint ach[1547];\nint bod, vod;\npii P[1547];\nreal uhol[1547];\n\nreal predrat[1547][1547];\n\nbool my_func(int a, int b){\n if (fabs(predrat[a][bod])P[i].first) best = i;\n }\n For(i,N) if (i!=best) B[0].push_back(i);\n For(i,N) For(j,N) predrat[i][j]=0;\n uhol[0] = M_PI/2;\n otec[0] = 0;\n vel(0);\n zadel(0,best);\n For(i,N) printf(\"%d%c\",ach[i]+1,(i==N-1)?'\\n':' ');\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "b8d2fefd494f6b24ca217da1a21bdad5", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nconst int N=2010;\nconst int inf=0x3f3f3f3f;\n\nusing namespace std;\n\nstruct node\n{\n\tint to,next;\n}edge[N*10];\n\nint box[N],ecnt;\n\nvoid _make_edge(int from,int to)\n{\n\tedge[ecnt].to=to;\n\tedge[ecnt].next=box[from];\n\tbox[from]=ecnt++;\n\treturn ;\n}\n\nvoid make_edge(int from,int to)\n{\n\t_make_edge(from,to);\n\t_make_edge(to,from);\n\treturn ;\n}\n\nint dis[N];\n\nvoid dfs(int u,int p)\n{\n\t//cout<=y*c.x;\n\t}\n}a[N],p0;\n\nbool cmp(const point &a,const point &b)\n{\n\treturn (a-p0)*(b-p0);\n}\n\nbool cmpy(const point &a,const point &b)\n{\n\tif(a.y!=b.y) return a.y>n;\n\tmemset(box,-1,sizeof(box));ecnt=0;\n\tfor(int i=0;i>a>>b;\n\t\tmake_edge(a,b);\n\t}\n\tfor(int i=0;i>a[i].x>>a[i].y,a[i].id=i+1;\n\tdfs(1,1);\n\tDFS(1,1,n,a);\n\tfor(int i=1;i<=n;i++)\n\t\tprintf(\"%d \",ans[i]);\n\tputs(\"\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "6c0bcf50b5c188d6933c5c4224ded248", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define int long long\nusing namespace std;\nconst int N=2000;\nstruct node\n{\n int to,nxt;\n}edge[2*N];\nstruct point\n{\n int x,y,id;\n}p[N],poi;\nint n,head[2*N],tot=0,ans[N],size[N];\nvoid add(int u,int v)\n{\n edge[++tot].to=v;\n edge[tot].nxt=head[u];\n head[u]=tot;\n}\nbool cmp(point a,point b)\n{\n int ax=a.x-poi.x;\n int ay=a.y-poi.y;\n int bx=b.x-poi.x;\n int by=b.y-poi.y;\n if (ax>=0 && bx<=0) return true;\n if (ax<=0 && bx>=0) return false;\n return ax*by>ay*bx;\n}\nvoid dfs_size(int u,int fa)\n{\n size[u]=1;\n for (int i=head[u];i;i=edge[i].nxt)\n {\n int v=edge[i].to;\n if (v==fa) continue;\n dfs_size(v,u);\n size[u]+=size[v];\n }\n}\nvoid dfs(int l,int r,int u,int fa)\n{\n int x=l;\n for (int i=l+1;i<=r;i++)\n {\n if (p[i].x\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n/*** TEMPLATE CODE STARTS HERE ***/\n\nusing namespace std;\n\ntypedef vector vi;\ntypedef vector vs;\ntypedef long long ll;\ntypedef complex pnt;\ntypedef pair pii;\n\n#define RA(x) (x).begin(), (x).end()\n#define FE(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)\n#define SZ(x) ((int) (x).size())\n\ntemplate\nvoid splitstr(const string &s, vector &out)\n{\n istringstream in(s);\n out.clear();\n copy(istream_iterator(in), istream_iterator(), back_inserter(out));\n}\n\ntemplate T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n\nstatic void redirect(int argc, const char **argv)\n{\n if (argc > 1)\n {\n static filebuf f;\n f.open(argv[1], ios::in);\n cin.rdbuf(&f);\n if (!cin)\n {\n cerr << \"Failed to open '\" << argv[1] << \"'\" << endl;\n exit(1);\n }\n }\n\n if (argc > 2)\n {\n static filebuf f;\n f.open(argv[2], ios::out | ios::trunc);\n cout.rdbuf(&f);\n if (!cout)\n {\n cerr << \"Failed to open '\" << argv[2] << \"'\" << endl;\n }\n }\n}\n\n/*** TEMPLATE CODE ENDS HERE */\n\nstatic ll cross(const pnt &a, const pnt &b) { return imag(conj(a) * b); }\nstatic ll cross(const pnt &a, const pnt &b, const pnt &c)\n{\n return cross(b - a, c - a);\n}\n\nstruct node\n{\n int parent;\n vi edges;\n int size;\n int pntid;\n\n node() : parent(-1), size(0), pntid(-1) {}\n};\n\nstatic void prepare(vector &nodes, int cur, int parent)\n{\n node &n = nodes[cur];\n n.parent = parent;\n n.size = 1;\n for (int i = 0; i < SZ(n.edges); i++)\n {\n int y = n.edges[i];\n if (y != parent)\n {\n prepare(nodes, y, cur);\n n.size += nodes[y].size;\n }\n }\n}\n\nstruct compareYX\n{\n bool operator()(const pnt &a, const pnt &b) const\n {\n if (a.imag() != b.imag())\n return a.imag() < b.imag();\n else\n return a.real() < b.real();\n }\n\n bool operator()(const pair &a, const pair &b) const\n {\n return (*this)(a.first, b.first);\n }\n};\n\nstruct compareAngle\n{\n pnt base;\n\n compareAngle(const pnt &base) : base(base) {}\n\n bool operator()(const pnt &a, const pnt &b) const\n {\n return cross(base, a, b) > 0;\n }\n\n bool operator()(const pair &a, const pair &b) const\n {\n return (*this)(a.first, b.first);\n }\n};\n\ntypedef vector > pntvec;\n\nstatic void place(vector &nodes, int cur, pntvec::iterator first, pntvec::iterator last)\n{\n node &n = nodes[cur];\n assert(last - first == n.size);\n pntvec::iterator lo = min_element(first, last, compareYX());\n swap(*first, *lo);\n\n n.pntid = first->second;\n\n sort(first + 1, last, compareAngle(first->first));\n first++;\n for (int i = 0; i < SZ(n.edges); i++)\n {\n int y = n.edges[i];\n if (y != n.parent)\n {\n place(nodes, y, first, first + nodes[y].size);\n first += nodes[y].size;\n }\n }\n}\n\nint main(int argc, const char **argv)\n{\n redirect(argc, argv);\n\n int N;\n cin >> N;\n vector nodes(N);\n for (int i = 1; i < N; i++)\n {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n nodes[u].edges.push_back(v);\n nodes[v].edges.push_back(u);\n }\n prepare(nodes, 0, -1);\n\n vector > pnts;\n for (int i = 0; i < N; i++)\n {\n int x, y;\n cin >> x >> y;\n pnts.push_back(make_pair(pnt(x, y), i));\n }\n\n place(nodes, 0, pnts.begin(), pnts.end());\n vector ans(N, -1);\n for (int i = 0; i < N; i++)\n ans[nodes[i].pntid] = i + 1;\n\n cout << ans[0];\n for (int i = 1; i < N; i++)\n cout << ' ' << ans[i];\n cout << '\\n';\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "c4c51698380a2f91e1275813cd2f462a", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nstruct pt {\n int x, y, id;\n};\n\nvector < pt > p;\nvector > g;\nvector size, ans;\nint n;\n\nint dfs(int v, int parent = -1) {\n for (int i = 0; i < g[v].size(); i++) {\n int to = g[v][i];\n if (to == parent) continue;\n size[v] += dfs(to, v);\n }\n return size[v];\n}\n\nint gx, gy;\n\nbool cmp(pt a, pt b) {\n return 1LL*(a.x - gx)*(b.y - gy) - 1LL*(b.x - gx)*(a.y - gy) > 0;\n}\n\nbool cmp2(pt a, pt b) {\n return a.y > b.y || a.y == b.y && a.x < b.x;\n}\n\nvoid rec(int v, vector p, int parent = -1) {\n gx = p.front().x, gy = p.front().y;\n ans[p.front().id] = v;\n p.erase(p.begin());\n sort(p.begin(), p.end(), cmp);\n\n vector buf;\n int cur = 0;\n for (int i = 0; i < g[v].size(); i++) {\n int to = g[v][i];\n \n if (to != parent) {\n buf.clear();\n for (int x = cur; cur < x + size[to]; cur++) {\n buf.push_back(p[cur]);\n }\n\n rec(to, buf, v);\n }\n }\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n cin >> n;\n g.resize(n);\n p.resize(n);\n ans.resize(n);\n size.assign(n, 1);\n\n for (int i = 0; i < n-1; i++) {\n int a, b;\n cin >> a >> b;\n g[--a].push_back(--b);\n g[b].push_back(a);\n }\n\n for (int i = 0; i < n; i++) {\n cin >> p[i].x >> p[i].y;\n p[i].id = i;\n }\n\n dfs(0);\n sort(p.begin(), p.end(), cmp2);\n rec(0, p);\n\n for (int i = 0; i < n; i++)\n cout << (ans[i]+1) << \" \";\n\n return 0; \n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "18ed42cabce46fe1d478f145438df79a", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\n#define FOR(i,s,t) for(int i=(s),_t=(t);i<=_t;++i)\n#define DOR(i,s,t) for(int i=(s),_t=(t);i>=_t;--i)\n#define M 1505\n\ntypedef long long ll;\n\nvector E[M];\n\nll pos_x,pos_y;\nstruct node{\n\tint x,y,id;\n\tbool operator <(const node &s) const{\n\t\treturn y(a.y-pos_y)*(b.x-pos_x);\n}\n\nint Ans[M];\n\nint Fa[M],sz[M];\nvoid dfs(int x,int fr) {\n\tFa[x]=fr;sz[x]=1;\n\tFOR(i,0,E[x].size()-1) {\n\t\tint y=E[x][i];\n\t\tif(y==fr) continue;\n\t\tdfs(y,x);\n\t\tsz[x]+=sz[y];\n\t}\n}\nvoid solve(int L,int R,int x) {\n\tint s=L;\n\tFOR(i,L+1,R) if(A[s].x>A[i].x) s=i;\n\tnode tmp=A[L];\n\tA[L]=A[s];\n\tA[s]=tmp;\n\tAns[A[L].id]=x;\n\tpos_x=A[L].x;\n\tpos_y=A[L].y;\n\tL++;\n\tif(L>R) return ;\n\tsort(A+L,A+R+1,cmp);\n\tFOR(i,0,E[x].size()-1) {\n\t\tint y=E[x][i];\n\t\tif(y==Fa[x]) continue;\n\t\tsolve(L,L+sz[y]-1,y);\n\t\tL+=sz[y];\n\t}\n}\nint main() {\n\tint n;\n\tscanf(\"%d\",&n);\n\tFOR(i,2,n) {\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tE[x].push_back(y);\n\t\tE[y].push_back(x);\n\t}\n\tFOR(i,1,n) {\n\t\tscanf(\"%d%d\",&A[i].x,&A[i].y);\n\t\tA[i].id=i;\n\t}\n\tdfs(1,0);\n\tsolve(1,n,1);\n\tFOR(i,1,n) printf(\"%d \",Ans[i]);\n\tputchar('\\n');\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "geometry", "constructive algorithms", "trees"], "code_uid": "bf06560c95743cf7aa881486e485e094", "src_uid": "d65e91dc274c6659cfdb50bc8b8020ba", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define pb push_back\n#define pp pop_back()\n#define mp make_pair\n#define fr first\n#define sc second\n#define sz size()\n#define smin(x,y) ((x)=min((x),(y)))\n#define smax(x,y) ((x)=max((x),(y)))\n#define all(x) x.begin(),x.end()\n#define CS ios_base::sync_with_stdio(false);\nusing namespace std;\ntypedef long long ll;\ntypedef pair ii;\ntypedef vector vi; \ntypedef pair pll;\nconst int M=1505;\npll a[M];\nint mark[M],dp[M],n;\nvi adj[M];\nvoid dfs(int v){\n\tmark[v]=1;\n\tfor(int u : adj[v]) if(!mark[u])\n\t\tdfs(u),dp[v]+=dp[u];\n\tdp[v]++;\n}\nbool cmp_up(int u,int v){\n\treturn a[u].sc vec,int v){\n\tauto it = max_element(all(vec),cmp_up);\n\tint root = *it; vec.erase(it);\n\tcomper = root;\n\tsort(all(vec),cmp_shib);\n\tvi go;\n\tfor(int u : adj[v]) if(dp[u]>n;\n\tfor(int i=1;i>u>>v;\n\t\tadj[u].pb(v),adj[v].pb(u);\n\t}\n\tvi vec;\n\tfor(int i=1;i<=n;i++) cin>>a[i].fr>>a[i].sc,vec.pb(i);\n\tdfs(1),solve(vec,1);\n\tfor(int i=1;i<=n;i++) cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define sz(a) static_cast(a.size())\n#define all(a) a.begin(), a.end()\n#define rep(i, a, b) for(int i=int(a); i<=int(b); ++i)\n#define irep(i, a, b) for(int i=int(a); i>=int(b); --i)\n#define repn(i, n) rep(i, 0, (n)-1)\n#define irepn(i, n) irep(i, (n)-1, 0)\n#define fillchar(a, x) memset(a, x, sizeof(a))\n#define fi first\n#define se second\n#define pb push_back\n#define mp make_pair\ntypedef long long LL;\ntypedef pair PLL;\ntypedef pair PII;\ntypedef pair PDD;\ntypedef vector VL;\ntypedef vector VI;\ntypedef vector VPI;\ntypedef vector VS;\ntemplate bool setmax(T &_a, T _b) { if(_b>_a) { _a=_b; return true; } return false; }\ntemplate bool setmin(T &_a, T _b) { if(_b<_a) { _a=_b; return true; } return false; }\ntemplate T gcd(T _a, T _b) { return _b==0?_a:gcd(_b,_a%_b); }\n\nconst int N=2010;\n\nstruct Formula {\n bitset g[N]; int r[N];\n int n, m;\n int op(int x) const { return x<=n ? x+n : x-n; }\n\n void read(int _n, int _m) {\n n=_n; m=_m;\n rep(i, 1, n*2) g[i].reset();\n rep(i, 1, m) {\n int a, b; scanf(\"%d%d\", &a,&b);\n if(a<0) a=-a+n;\n if(b<0) b=-b+n;\n g[op(a)][b]=1;\n g[op(b)][a]=1;\n }\n }\n\n void go() {\n rep(k, 1, n*2) rep(i, 1, n*2) if(g[i][k]) {\n g[i]|=g[k];\n }\n\n if(ok()) {\n fillchar(r, 0xff);\n rep(x, 1, n*2) if(g[op(x)][x]) color(x);\n }\n }\n\n void color(int x) {\n if(r[x]>=0) { assert(r[x]==1); return; }\n r[x]=1, r[op(x)]=0;\n rep(y, 1, n*2) if(g[x][y]) color(y);\n }\n\n bool ok() {\n rep(x, 1, n) if(g[x][x+n] && g[x+n][x]) return false;\n return true;\n }\n\n void solve(int c1=-1, int c2=-1) {\n if(c1>0) color(c1);\n if(c2>0) color(c2);\n rep(i, 1, n*2) if(r[i]<0) color(i);\n rep(i, 1, n) printf(\"%d \", r[i]); printf(\"\\n\");\n exit(0);\n }\n};\n\nFormula f, g;\n\nint main() {\n int n, m1, m2; scanf(\"%d%d%d\", &n,&m1,&m2);\n f.read(n, m1); f.go();\n g.read(n, m2); g.go();\n bool of=f.ok(), og=g.ok();\n if(of && !og) f.solve();\n else if(!of && og) g.solve();\n else if(of && og) {\n rep(i, 1, n*2) {\n if(f.r[i]==0 && g.r[i]!=0) g.solve(i);\n if(g.r[i]==0 && f.r[i]!=0) f.solve(i);\n }\n rep(i, 1, n*2) rep(j, 1, n*2) if(i!=j && f.r[i]<0 && f.r[j]<0) {\n if(f.g[i][j]!=g.g[i][j]) {\n if(f.g[i][j]) g.solve(i, g.op(j));\n if(g.g[i][j]) f.solve(i, f.op(j));\n }\n }\n }\n printf(\"SIMILAR\\n\");\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["graphs"], "code_uid": "f47e619956dd3ba943877de30e77ba59", "src_uid": "e7f585455aaf039aa6f0f2846d818b40", "difficulty": 3000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\n#define sqr(x) ((x)*(x))\n#define mp make_pair\n#define uint unsigned\n#define PI pair\n#define fi first\n#define se second\n#define ld long double\ninline char gc(){\n static char buf[100000],*p1=buf,*p2=buf;\n return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;\n}\n#define gc getchar\ninline ll read(){\n\tll x = 0; char ch = gc(); bool positive = 1;\n\tfor (; !isdigit(ch); ch = gc())\tif (ch == '-')\tpositive = 0;\n\tfor (; isdigit(ch); ch = gc())\tx = x * 10 + ch - '0';\n\treturn positive ? x : -x;\n}\ninline void write(ll a){\n if(a<0){\n \ta=-a; putchar('-');\n\t}\n if(a>=10)write(a/10);\n putchar('0'+a%10);\n}\ninline void writeln(ll a){write(a); putchar('\\n');}\ninline void wri(ll a){write(a); putchar(' ');}\nconst int N=100005;\nint x[N],y[N],lx,ly,Ans;\nset > M;\nll q[N];\nPI ans[N];\nbool vis[N];\nint main(){\n\t//freopen(\"akitomoya.in\",\"r\",stdin);freopen(\"akitomoya.out\",\"w\",stdout);\n\tint n=read();\n\tfor(int i=1;i<=n;i++){\n\t\tx[i]=read(); y[i]=read();\n\t}\n\tint ho=0;\n\tif(n%2==0){\n\t\tn--; ho=1;\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tll tx=ho?x[i]+x[n+1]:x[i]*2,ty=ho?y[i]+y[n+1]:y[i]*2;\n\t\tif(M.count(mp(tx,ty)))continue; else M.insert(mp(tx,ty));\n\t\tll sumx=0,sumy=0;\n\t\tfor(int j=1;j<=n;j++)if(i!=j){sumx+=x[j]; sumy+=y[j];}\n\t\t//cout<\nusing namespace std;\n\ntypedef long long LL;\ntypedef double Type;\n\nconst double eps = 1e-8;\nconst double PI = 3.1415926535;\n\n#define IS(X) cout << #X << \" = \" << (X) << endl\n\ninline int sgn(Type x) {\n return (x > eps) - (x < -eps);\n}\n\nstruct Point {\n Type x, y, id;\n Point() : x(0), y(0) {}\n\n Point(Type _x, Type _y) : x(_x), y(_y) {}\n\n void set(Type _x, Type _y) {\n this->x = _x; this->y = _y;\n }\n \n Point operator + (const Point &p) const {\n return Point(x + p.x, y + p.y);\n }\n\n Point operator - (const Point &p) const {\n return Point(x - p.x, y - p.y);\n }\n\n Point operator * (const Type &q) const {\n return Point(x * q, y * q);\n }\n\n Point operator / (const Type &q) const {\n return Point(x / q, y / q);\n }\n\n bool operator == (const Point &p) const {\n return fabs(p.x - x) < eps && fabs(p.y - y) < eps;\n }\n\n bool operator != (const Point &p) const {\n return ((*this) == p) == false;\n }\n\n friend bool operator < (const Point &p1, const Point &p2) {\n return p1.y < p2.y || (p1.y == p2.y && p1.x < p2.x);\n }\n\n void read() {\n cin >> this->x >> this->y;\n }\n\n void print() const {\n cout << \"Point\" << x << \",\" << y << endl;\n }\n};\n\ninline Type Dot(const Point &p1, const Point &p2) {\n return p1.x * p2.x + p1.y * p2.y;\n}\n\ninline Type Cross(const Point &p1, const Point &p2) {\n return p1.x * p2.y - p2.x * p1.y;\n}\n\ninline Type Length2(const Point &p1) {\n return p1.x * p1.x + p1.y * p1.y;\n}\n\ninline Type Length(const Point &p) {\n return sqrt(Length2(p));\n}\n\ninline Type Angle(const Point &p) {\n return atan2(p.y, p.x);\n}\n\nint n;\nPoint p[2005];\nPoint cp[2005];\nPoint o(0,0);\nbool use[2005];\nint main() {\n cin >> n;\n for(int i = 1;i <= n;i++) {\n p[i].read();p[i].id = i;\n o = o + p[i];\n }\n o = o / n;\n for(int i = 1;i <= n;i++) use[i] = true;\n for(int i = 1;i <= n;i++) {\n if(!use[i]) continue;\n if(Length(p[i] - o) == 0) {\n use[i] = false; continue; \n }\n for(int j = i + 1;j <= n;j++) {\n if(!use[j]) continue;\n if(sgn( Length((p[i] + p[j]) / 2 - o) == 0 )) {\n use[i] = use[j] = false;\n break;\n }\n }\n }\n int pos = 0;\n for(int i = 1;i <= n;i++) {\n if(use[i]) swap(p[++pos], p[i]);\n }\n if(pos == 0) {\n cout << \"-1\" << endl;return 0;\n }\n int ans = 0;\n vector dir;dir.clear();\n for(int i = 1;i <= pos;i++) {\n Point mid = (p[1] + p[i]) / 2;\n Point k = (mid - o) / Length((mid - o));\n bool flag = true; \n for(int j = 0;j < dir.size();j++) {\n if(dir[j] == k || dir[j] + k == Point(0,0)) {\n flag = false;break;\n }\n }\n if(!flag) continue;\n dir.push_back(k);\n vector v;\n v.clear();\n for(int j = 1;j <= pos;j++) {\n v.push_back(Cross(p[j] - o, mid - o));\n }\n sort(v.begin(), v.end());\n flag = true;\n int b = 0, e = v.size() - 1;\n for(;b <= e;b++, e--) {\n if(sgn(v[b] + v[e]) != 0) {\n flag = false; break;\n }\n }\n if(flag) ans ++;\n }\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "0c47b4940a0e7dc9ce6c5e9855758977", "src_uid": "5d7ba962400c05433ee17c5658888e69", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long LL;\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define trav(a, x) for(auto& a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\ntypedef long long ll;\ntypedef pair pii;\ntypedef vector vi;\n\ntemplate \nstruct Point {\n typedef Point P;\n T x, y;\n explicit Point(T x=0, T y=0) : x(x), y(y) {}\n bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }\n bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }\n P operator+(P p) const { return P(x+p.x, y+p.y); }\n P operator-(P p) const { return P(x-p.x, y-p.y); }\n P operator*(T d) const { return P(x*d, y*d); }\n P operator/(T d) const { return P(x/d, y/d); }\n T dot(P p) const { return x*p.x + y*p.y; }\n T cross(P p) const { return x*p.y - y*p.x; }\n T cross(P a, P b) const { return (a-*this).cross(b-*this); }\n T dist2() const { return x*x + y*y; }\n double dist() const { return sqrt((double)dist2()); }\n // angle to x-axis in interval [-pi, pi]\n double angle() const { return atan2(y, x); }\n P unit() const { return *this/dist(); } // makes dist()=1\n P perp() const { return P(-y, x); } // rotates +90 degrees\n P normal() const { return perp().unit(); }\n // returns point rotated 'a' radians ccw around the origin\n P rotate(double a) const {\n return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }\n};\n\ntypedef Point P;\npair ulHull(const vector

> queue;\n fill(Dist.begin(), Dist.end(), 1e+18); //1e18 !?!?\n fill(Prev.begin(), Prev.end(), -1); //1e18 !?!?\n Dist[start] = 0;\n PathNum[start] = 1;\n queue.push(PL(0, start));\n\n while (!queue.empty())\n {\n PL p = queue.top();\n queue.pop();\n int v = p.second;\n if (Dist[v] < p.first)\n continue;\n\n for (int i = 0; i < Edges[v].size(); i++)\n {\n PL e = Edges[v][i];\n if (Dist[e.first] > Dist[v] + e.second)\n {\n Dist[e.first] = Dist[v] + e.second;\n queue.push(P(Dist[e.first], e.first));\n\n Prev[e.first] = v;\n\n PathNum[e.first] = PathNum[v];\n }\n else if (Dist[e.first] == Dist[v] + e.second)\n {\n PathNum[e.first] += PathNum[v];\n PathNum[e.first] %= MOD;\n }\n }\n }\n }\n\n ll dist(ll t)\n {\n return Dist[t];\n }\n\n vector get_path(ll t)\n {\n vector path;\n for (; t != -1; t = Prev[t])\n {\n path.push_back(t);\n }\n reverse(path.begin(), path.end());\n return path;\n }\n\n ll get_path_num(ll t)\n {\n return PathNum[t];\n }\n\n // int solve()\n // {\n // ll v, e, r;\n // cin >> v >> e >> r;\n // Dijkstra dij(v);\n // for (int i = 0; i < e; i++)\n // {\n // ll a, b, c;\n // cin >> a >> b >> c;\n // dij.add_edge(a, b, c);\n // }\n // dij.build(r);\n // for (int i = 0; i < v; i++)\n // {\n // if (dij.dist(i) == 1e18)\n // {\n // cout << \"INF\" << endl;\n // }\n // else\n // {\n // cout << dij.dist(i) << endl;\n // cout << dij.get_path(i) << endl;\n // cout << dij.get_path_num(i) << endl;\n // }\n // }\n // return 0;\n // }\n};\n\nstruct CumulativeSum2D\n{\n int H;\n int W;\n vector> data;\n\n CumulativeSum2D(int H, int W) : H(H), W(W), data(H + 1, vector(W + 1, 0LL)) {}\n\n void add(int x, int y, ll z)\n {\n data[x + 1][y + 1] += z;\n }\n\n void build()\n {\n for (int i = 1; i < data.size(); i++)\n {\n for (int j = 1; j < data[i].size(); j++)\n {\n data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];\n }\n }\n }\n\n void print()\n {\n\n for (int i = 0; i <= H; i++)\n {\n for (int j = 0; j <= W; j++)\n {\n cout << data[i][j] << \" \";\n }\n cout << endl;\n }\n }\n\n ll query(int sx, int sy, int gx, int gy)\n {\n return (data[gy][gx] - data[sy - 1][gx] - data[gy][sx - 1] + data[sy - 1][sx - 1]);\n }\n};\n\nstruct LCA\n{\n int n, h;\n vector> par;\n vector>> v;\n vector depth, dis;\n LCA(int sz) : n(sz), v(n), depth(n), dis(n)\n {\n h = 1;\n while ((1 << h) < n)\n h++;\n par.assign(h, vector(n, -1));\n }\n\n void add_edge(int x, int y, int z)\n {\n v[x].push_back({y, z});\n v[y].push_back({x, z});\n }\n\n void dfs(int x, int p, int d, ll di)\n {\n par[0][x] = p;\n depth[x] = d;\n dis[x] = di;\n for (auto to : v[x])\n if (to.first != p)\n dfs(to.first, x, d + 1, di + to.second);\n }\n\n void build()\n {\n dfs(0, -1, 0, 0);\n for (int i = 0; i < h - 1; i++)\n {\n for (int j = 0; j < n; j++)\n {\n if (par[i][j] < 0)\n par[i + 1][j] = -1;\n else\n par[i + 1][j] = par[i][par[i][j]];\n }\n }\n }\n\n int lca(int u, int v)\n {\n if (depth[u] > depth[v])\n swap(u, v);\n for (int i = 0; i < h; i++)\n if ((depth[v] - depth[u]) >> i & 1)\n v = par[i][v];\n if (u == v)\n return u;\n for (int i = h - 1; i >= 0; i--)\n {\n if (par[i][u] != par[i][v])\n {\n u = par[i][u];\n v = par[i][v];\n }\n }\n return par[0][u];\n }\n\n ll dist(int u, int v)\n {\n return dis[u] + dis[v] - 2 * dis[lca(u, v)];\n }\n\n // int solve()\n // {\n // ll n;\n // cin >> n;\n // LCA lca(n);\n // for (int i = 0; i < n - 1; i++)\n // {\n // ll u, v, w;\n // cin >> u >> v >> w;\n // lca.add_edge(u, v, w);\n // }\n // lca.build();\n // ll q;\n // cin >> q;\n // while (q--)\n // {\n // ll a, b, c;\n // cin >> a >> b >> c;\n // cout << (lca.dist(a, b) + lca.dist(b, c) + lca.dist(c, a)) / 2 << endl;\n // }\n\n // return 0;\n // }\n};\n\n\n//IMOS https://imoz.jp/algorithms/imos_method.html\n\n/*名前\nbitcount __builtin_popcountll\n二次元累積和 CumulativeSum2D\n10進数の桁和 digit_sum\n(b*b+c*c)**0.5 hypot\n\n// 文字列t ->整数 atoi(t.c_str());\n// 文字列t ->long long整数 stoll(t); ローカルではつかえない\nto_string()も使えないので、どうにかしたい\n*/\n\n/*実装例\nDP\n-部分文字列DP https://www.hackerrank.com/contests/bbc003/challenges/bbc003-e/submissions/code/1321355726\n\n組み合わせ\n完全順列(pi!=i) \n*/\n\n/*------------------------------*/\nll a[101010];\n\nll cnk(ll n ,ll k){\n ll ret = 1;\n for(ll i=n-k+1; i <= n; i++){\n ret *= i;\n }\n for(int i=1; i <= k; i++){\n ret /= i;\n }\n return ret;\n}\nvoid solve()\n{\n ll n , k;\n cin >> n >> k;\n if(k == 1){\n cout << 1 << endl;\n exit(0);\n }\n a[2] = 1;\n for(int i=3; i <= 10; i++){\n a[i] = (i - 1) * (a[i - 1] + a[i - 2]);\n }\n\n ll ans = 1;\n for(int i=0; i < k; i++){\n ans += cnk(n, k - i) * a[k - i];\n }\n \n cout << ans << endl;\n}\n\nint main()\n{\n cout.precision(10);\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n solve();\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "985424800f40b1026be4efcdb411a632", "src_uid": "96d839dc2d038f8ae95fc47c217b2e2f", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include\n#include\n#include\n#include\n#include\n#include\n#define MAX 200000\n#define M 1e18+7\nusing namespace std;\nint main(){\nstring s;\nint n,k,t,g;\ncin>>n>>k>>s;\nfor(int i=0;i\nusing namespace std ;\n\nint main(){\n\t\n\tint n ;\n\tcin >> n ;\n\tint min = INT_MAX ;\t\n\tint a[n] ;\n\n\tfor(int i=0 ; i> a[i] ;\n\t\tif(a[i] < min){\n\t\t\tmin = a[i] ;\n\t\t}\n\t}\n\tvector arr(100) ;\n\tfor(int i=0 ; i<100 ; i++){\n\t\tarr[i] = 0 ;\n\t}\n\n\tfor(int i=0 ; i \nusing namespace std; \nint main() \n{int a,b,p,q,x,y; \ncin>>a>>b>>p>>q>>x>>y; \nif((p-a)%x==0 && (q-b)%y==0 && ((p-a)/x+(q-b)/y)%2==0) cout<<\"YES\"<\nusing namespace std;\n\nint main() {\n int n, s;\n cin >> n >> s;\n if (s%n == 0){cout << s/n;}\n else {cout << s/n + 1;}\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "implementation"], "code_uid": "bb09193ac59f00cf10c5c4f5de3e5492", "src_uid": "04c067326ec897091c3dbcf4d134df96", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nstring a;\nint sum;\nint main(){\n\tcin>>a;\n\tchar b,c;\n\tfor(int i=0;i\n \nusing namespace std;\n \n#ifdef NDEBUG\n# define debug(...)\n#else\n# include \"debug.hh\"\n#endif\n \nint static_init = []() {\n ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed;\n return 0;\n}();\n \nint k, s[5], c[3];\n \nbool run() {\n while (k-- > 0) {\n int a;\n cin >> a;\n int d = min(s[4], a/4);\n s[4] -= d, a -= 4*d;\n d = min(s[2], a/2);\n s[2] -= d, a -= 2*d;\n if (a == 3) {\n if (s[4] > 0) s[4]--, a = 0;\n else if (s[2] > 0) s[2]--, a -= 2;\n }\n if (a >= 3) return false;\n c[a]++;\n }\n int d = min(c[2], s[4]);\n c[2] -= d, s[4] -= d, s[1] += d;\n c[1] += 2*c[2];\n return c[1] <= s[1] + s[2] + 2*s[4];\n}\n \nint main() {\n cin >> s[4] >> k;\n s[2] = 2*s[4];\n cout << (run() ? \"YES\" : \"NO\") << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "greedy", "implementation"], "code_uid": "93a4694fc3820d9692c31a85a1d00013", "src_uid": "d1f88a97714d6c13309c88fcf7d86821", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#define CIN_ONLY 1\n#include \nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\nconst int MOD = 998244353;\nconst ld PI = acos(ld(-1));\nint fix(ll i) { return (i % MOD + MOD) % MOD; }\n\nconst int N = 502;\n\nll h[N][N];\nll he[N][N];\nll sumh[N][N];\n\nvoid solve(int n, int k)\n{\n for (int i = 0; i <= 1; ++i) {\n for (int j = 1; j <= n; ++j) {\n h[i][j] = 1;\n he[i][j] = h[i][j] - h[i][j - 1];\n sumh[i + 1][j] = sumh[i][j] + h[i][j];\n }\n }\n for (int i = 2; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) {\n h[i][j] = fix(sumh[i][j] - sumh[max(0, i - j)][j]);\n he[i][j] = fix(h[i][j] - h[i][j - 1]);\n sumh[i + 1][j] = fix(sumh[i][j] + h[i][j]);\n }\n }\n int ans = 0;\n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) {\n if (i * j < k) {\n ans = fix(ans + he[n][i] * he[n][j]);\n }\n }\n }\n cout << fix(ans * 2) << endl;\n}\n\nint main()\n{\n if (CIN_ONLY) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n int n, k;\n cin >> n >> k;\n solve(n, k);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "8f06ac0100df402830637d5b8cb652fa", "src_uid": "77177b1a2faf0ba4ca1f4d77632b635b", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "// Solution By Doolatbek uulu Bektemir\n#include \nusing namespace std;\nint main(){\n double i,x,y,f,a,b,ans1=0,ans2=0;\n cin >> x >> y, f=pow(y,x-1);\n for(i=0; i> a;\n ans1=ans1+(f*a),f=f/y;\n }\n cin >> x >> y, f=pow(y,x-1);\n for(i=0; i> b, ans2=ans2+(f*b), f=f/y;}\n if(ans1==ans2){cout << \"=\";}\n else if(ans1ans2){cout << \">\";}\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "70402ee40051360bff21304086d91016", "src_uid": "d6ab5f75a7bee28f0af2bf168a0b2e67", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define ll long long\n#define mp make_pair\n#define pii pair\n#define pll pair\n#define pci pair \n#define fi first\n#define se second\n//cout << fixed << setprecision(5) << f << '\\n';\n/*\nbool sortbysec(const pair &a, const pair &b) \n{ \n //ascending\n return (a.second < b.second); \n} \nbool comp(pii a , pii b)\n{ // descending \n if(a.second == b.second)return(a.first > b.first);\n return(a.second > b.second);\n}\n\n // Inserts str2 in str1 starting \n // from 6th index of str1 \n str1.insert(6, str2); \n*/\n//map m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y\n//template\n//using min_heap = priority_queue , greater>;\n//template\n//using max_heap = priority_queue;\n//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});\n//min_heap> byx2;\n//max_heap> byy1;\n//using vll = vector\n//vi gone(n , 0);\n//sortdesc(arr, arr+n, greater());\n// x | (1 << n) returns x with nth bit set\n// x & (-x) returns the rightmost 1 of x \n//getline(cin , s);\n// vector> dist(size of the outer vector = n , vector(size of EACH VECTOR in the vector = 10));\n/*for(int k=0;k<4;k++)\nuse this for four adjacent ;\n {\n int ni = i + int (k = 0) - int (k = 1);\n int nj = j + int (k = 2) - int (k = 3);\n if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;\n }\n */\n\n\n/*\n-----------BFS----------------\n\n // was[][] is for visited\n int was[n][m];\n for(int i=0;i= n || nj >= m|| was[ni][nj])continue;\n q[r++] = mp(ni , nj);\n was[ni][nj] = 1;\n }\n l+=1;\n }\n\n\n-----------BFS----------------\n*/\n/*\n-------------DFS--------------\nbool visit[N];\nint par[N];\nvector g[N];\nvoid dfs1(int v)\n{\n visit[v] = true;\n for(auto child : g[v])\n {\n if(!visit[child])\n dfs1(child);\n }\n\n}\n\nvoid dfs2(int v, int p)\n{\n par[v] = p;\n for(auto child : g[v])\n {\n if(child == par[v])\n continue;\n dfs(child,v);\n }\n\n}\n-------------DFS--------------\n*/\n/*\n----------------DFS for calculating depth of each element as well as number of children---------------\n#define maxn 200005\n#define pb push_back\nvectoradj[maxn];\nint depth[maxn] , size[maxn] , det[maxn];\nint dfs(int u , int f)\n{\n depth[u] = depth[f] + 1;\n size[u] = 1;\n for(auto i : adj[u])\n {\n size[u] += dfs(i , u);\n }\n det[u] = size[u] - depth[u];\n return size[u];\n}\ndfs(1 , 0);\n---------------------DFS---------------------------\n*/\n#define pb push_back\n//#define int long long\nbool isPrime(int n) \n{ \n // Corner cases \n if (n <= 1) return false; \n if (n <= 3) return true; \n \n // This is checked so that we can skip \n // middle five numbers in below loop \n if (n%2 == 0 || n%3 == 0) return false; \n \n for (int i=5; i*i<=n; i=i+6) \n if (n%i == 0 || n%(i+2) == 0) \n return false; \n \n return true; \n} \n\n/*int dfs(string s , char c)\n{\n int n = s.length();\n if(n == 1)\n {\n if(c == s[0]) return 0;\n else return 1;\n\n }\n int lc = 0;\n \n for(int i=0;ia , pairb)\n{\n if(a.se == b.se)return a.fi < b.fi;\n return a.se > b.se;\n}\nconst int X[]={1,0,-1,0,1,1,-1,-1};\nconst int Y[]={0,1,0,-1,1,-1,1,-1};\n/*bool dista(int x ,int y)\n{\n for (int i = 0; iadj[51];\nbool vis[51];\nint sum;\nvoid dfs(int u)\n{\n if(vis[u])return ;\n vis[u] = 1;\n sum += 1;\n for(auto i : adj[u])\n {\n //if(!vis[i])\n {dfs(i);}\n }\n return ;\n}\n\nsigned main()\n{ ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout); \n #endif\n int t = 1;\n //cin>>t;\n while(t--){\n \n //----------------------------------------------//\n \n\n \n\n\n int n;\n cin>>n;\n int mod = 1e9 + 7;\n int u = 1 , g = 1;\n for(int i=0;i\nusing namespace std;\nint n1,n2;\nint main()\n{\n\tcin>>n1>>n2;\n\tif(n1>n2) cout<<\"First\\n\";\n\t else cout<<\"Second\\n\";\n\treturn 0;\n } ", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "805253ca373b748cb60f5f7fd11ea298", "src_uid": "aed24ebab3ed9fd1741eea8e4200f86b", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n\nusing namespace std;\n\nbool testIndex(char arr[4][4], int i, int j)\n{\n\tif (i == 3 || j == 3)\n\t\treturn false;\n\treturn arr[i][j] == arr[i][j + 1] && arr[i][j] == arr[i + 1][j] && arr[i][j] == arr[i + 1][j + 1];\n}\n\nbool test(char arr[4][4])\n{\n\tfor (int i = 0; i < 4; i++)\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tif (testIndex(arr, i, j))\n\t\t\t\treturn true;\n\treturn false;\n}\n\nvoid change(char arr[4][4], int i, int j)\n{\n\tif (arr[i][j] == '.')\n\t\tarr[i][j] = '#';\n\telse\n\t\tarr[i][j] = '.';\n}\n\nint main()\n{\n\tchar mat[4][4]; int i, j; bool flag=false;\n\tfor (i = 0; i < 4; i++)\n\t\tfor (j = 0; j < 4; j++)\n\t\t\tcin >> mat[i][j];\n\tflag = test(mat);\n\tif (!flag)\n\t{\n\t\tfor (i = 0; i < 4; i++)\n\t\t\tfor (j = 0; j < 4; j++)\n\t\t\t{\n\t\t\tchange(mat, i, j);\n\t\t\tflag = test(mat);\n\t\t\tif (flag)\n\t\t\t\tbreak;\n\t\t\telse\n\t\t\t\tchange(mat, i, j);\n\t\t\t}\n\t}\n\tif (flag)\n\t\tcout << \"YES\";\n\telse\n\t\tcout << \"NO\";\n\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "429ac1b002f916132c3c417378f8e145", "src_uid": "01b145e798bbdf0ca2ecc383676d79f3", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \nusing namespace std;\n\nint main ()\n{\n\tios_base::sync_with_stdio(0); cin.tie(0);\n\n\tint a, b, c;\n\n\twhile(cin >> a >> b >> c)\n\t{\n\t\tlong long x, y, z;\n\n\t\tx = sqrt((a*c)/b);\n\t\ty = sqrt((b*c)/a);\n\t\tz = sqrt((a*b)/c);\n\n\t\tcout << 4*x + 4*y + 4*z << endl;\n\t}\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "geometry"], "code_uid": "30d6cf4fe61c8a380e02cf76a86b13b7", "src_uid": "c0a3290be3b87f3a232ec19d4639fefc", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n\nint main()\n{\n double vp,vd,t,f,c,sum=0.00000;\n scanf(\"%lf%lf%lf%lf%lf\",&vp,&vd,&t,&f,&c);\n int count=0;\n if(vp>=vd)\n printf(\"0\\n\");\n else{\n sum=sum+vp*t;\n t=sum/(vd-vp);\n sum=sum+t*vp;\n if(c-sum>=0.000001)\n count++;\n else\n {\n printf(\"0\\n\");\n return 0;\n }\n sum=sum+(t+f)*vp;\n while(c-sum>=0.000001)\n {\n t=sum/(vd-vp);\n sum=sum+t*vp;\n if(c-sum>=0.000001)\n count++;\n else\n {\n printf(\"%d\\n\",count);\n return 0;\n }\n sum=sum+(t+f)*vp;\n }\n printf(\"%d\\n\",count);\n }\n return 0;\n}\n\n \t \t\t \t\t\t \t \t\t\t\t \t \t", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "7cd9ab44be82f599a1f13ba6d8f3ddeb", "src_uid": "c9c03666278acec35f0e273691fe0fff", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nint k, b, n, t;\nint main()\n{\n scanf(\"%d %d %d %d\", &k, &b, &n, &t);\n if (t==1)\n printf(\"%d\\n\", n);\n else {\n long long z = 1; int s = 0;\n while (z < t)\n {\n z = k*z+b;\n s++;\n }\n if (s > n)\n printf(\"0\\n\");\n else\n if (z==t)\n printf(\"%d\\n\", n-s);\n else\n printf(\"%d\\n\", n-s+1);\n }\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "45748357df311385a944dfb0e9f1ced6", "src_uid": "e2357a1f54757bce77dce625772e4f18", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": " ///IN THE NAME OF ALLAH\n #include\n using namespace std;\n int main(){\n int n;\n cin >> n;\n if(5*(n/5)!=n)\n cout<<(n/5+1);\n else\n cout<\nusing namespace std;\nstring s,a;\nint main()\n{\n cin >> s;\n a = s;\n s.append(s);\n cout << s.find(a , 1) << endl;\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "354803718c9cde186a8419f352a01942", "src_uid": "8909ac99ed4ab2ee4d681ec864c7831e", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define int long long\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pa;\ntypedef pair pal;\n\nconst int N = 1e5;\n\nint solve(){\n\treturn 0;\n}\n\nsigned main(){\n\tint n;\n\tcin >> n;\n\tstring s;\n\tcin >> s;\n\tint ans = 0;\n\twhile (true) {\n\t\tstring ss = s;\n\t\tfor (int symb = 'z'; symb >= 'a'; --symb){\n\t\t\t\n\t\t\tstring p;\n\t\t\tn = s.size();\n\t\t\tvector used(n, 0);\n\t\t\tint g = 0;\n\t\t\tfor (int i = 0; i < n - 1; ++i){\n\t\t\t\tif (s[i] == s[i + 1] + 1 && s[i] == symb) {\n\t\t\t\t\tused[i] = 1;\n\t\t\t\t\tg = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (g == 0) {\n\t\t\t\tfor (int i = n - 1; i >= 1; --i){\n\t\t\t\t\tif (s[i] == s[i - 1] + 1 && s[i] == symb) {\n\t\t\t\t\t\tused[i] = 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 0; i < n; ++i) {\n\t\t\t\tif (!used[i])\n\t\t\t\t\tp += s[i];\n\t\t\t\tans += used[i];\n\t\t\t}\n\n\t\t\tif (p != s) {\n\t\t\t\ts = p;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t}\n\t\tif (ss == s)\n\t\t\tbreak;\n\t}\n\tcout << ans;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "strings"], "code_uid": "0ea484ca490b600bcd87b2817f32c453", "src_uid": "9ce37bc2d361f5bb8a0568fb479b8a38", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include\n#include\nusing namespace std;\n//int arr[]={0};\nint main()\n{\n int noOfApples;\n cin>>noOfApples;\n //map arr;\n int weight100=0,weight200=0;\n for(int i=0;i>x;\n // arr[i]=x;\n if(x==100)\n weight100++;\n if(x==200)\n weight200++;\n }\n int sum=(200*weight200)+(100*weight100);\n int afterdiv=sum/2;\n if(afterdiv%100==0 )\n {\n if(weight200%2==0 && weight100%2==0)\n cout<<\"YES\"<=1)\n {\n cout<<\"YES\"<\nusing namespace std;\n#define il inline\n#define elif else if\n\n// Type\n#define ld double\n#define ll long long\n#define ull unsigned ll\n\n// Vector\n#define vc vector\n#define Pb push_back\n#define Pf push_front\n#define Eb emplace_back\n#define All(x) x.begin(),x.end()\n#define AllRev(x) x.rbegin(),x.rend()\n\n// Memory\n#define Ms(_data) memset(_data,0,sizeof(_data))\n#define Msn(_data,_num) memset(_data,_num,sizeof(_data))\n\n// Template\n#define _cl class\n#define _tp template\n#define _tyn typename\n\n// Pair\n#define Mp make_pair\n#define F first\n#define S second\n#define pii pair\n#define pli pair\n#define pil pair\n#define pll pair\n\n// Loop\n#define For(i,j) for( int (i) = 1 ; (i) <= (j) ; ++(i) )\n#define For0(i,j) for( int (i) = 0 ; (i) < (j) ; ++(i) )\n#define Forx(i,j,k) for( int (i) = (j) ; (i) <= (k) ; ++(i) )\n#define Forstep(i,j,k,st) for( int (i) = (j) ; (i) <= (k) ; (i) += (st) )\n#define fOR(i,j) for( int (i) = (j) ; (i) >= 1 ; --(i) )\n#define fOR0(i,j) for( int (i) = (j)-1 ; (i) >= 0 ; --(i) )\n#define fORx(i,j,k) for( int (i) = (k) ; (i) >= (j) ; --(i) )\n\n// Read\nstruct InputReader{\n\t#define bs 1048576\n\tchar buf[bs]; int p;\n\til InputReader(){ p = bs; }\n\til void Flush(){ p = 0; fread(buf,1,bs,stdin); }\n\til char C(){ if(p >= bs) Flush(); return buf[p++]; }\n\til char Readnum(){ char ch = C(); while( !isdigit(ch) && ch != '-' ) ch = C(); return ch; }\n\til void Readalpha( char &c ){ c = C(); while( !isalpha(c) ) c = C(); }\n\tint operator() (){\n\t\tint ans = 0, fu = 1; char ch = Readnum();\n\t\tif( ch == '-' ) fu = -1, ch = C();\n\t\twhile( ch >= '0' && ch <= '9' ){\n\t\t\tans = ans*10 + ch-'0';\n\t\t\tch = C();\n\t\t}\n\t\treturn ans * fu;\n\t}\n\tll Readll(){\n\t\tll ans = 0LL, fu = 1LL; char ch = Readnum();\n\t\tif( ch == '-' ) fu = -1LL, ch = C();\n\t\twhile( ch >= '0' && ch <= '9' ){\n\t\t\tans = ans*10LL + ch-'0';\n\t\t\tch = C();\n\t\t}\n\t\treturn ans * fu;\n\t}\n\til void Readstring( string &x ){\n\t\tx.clear(); char ch = C();\n\t\twhile( !isdigit(ch) && !isalpha(ch) && ch != '#' && ch != '.' ) ch = C();\n\t\twhile( isdigit(ch) || isalpha(ch) || ch == '#' || ch == '.' ){\n\t\t\tx += ch;\n\t\t\tch = C();\n\t\t}\n\t}\n\til void Readchstring( char s[] ){\n\t\tint len = 0; char ch = C();\n\t\twhile( !isdigit(ch) && !isalpha(ch) /*&& ch != '*' && ch != '?'*/ ) ch = C();\n\t\twhile( isdigit(ch) || isalpha(ch) /*|| ch == '*' || ch == '?'*/ ){\n\t\t\ts[len++] = ch;\n\t\t\tch = C();\n\t\t}\n\t\ts[len] = '\\0';\n\t}\n\til void Specialread( char &c ){\n\t\tc = C();\n\t\twhile( !isdigit(c) && !isalpha(c) && c != '#' && c != '.' && c != '=' && c != 'B' ) c = C();\n\t}\n\t#undef bs\n}In;\nil void Read( int &x ){ x = In(); }\nil void Read( int &x, int &y ){ x = In(); y = In(); }\nil void Read( int &x1 , int &x2 , int &x3 ){ x1 = In(); x2 = In(); x3 = In(); }\nil void Read( int &x1 , int &x2 , int &x3 , int &x4 ){ x1 = In(); x2 = In(); x3 = In(); x4 = In(); }\nil void Read( ll &x ){ x = In.Readll(); }\nil void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }\nil void Read( ll &x1 , ll &x2 , ll &x3 ){ x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); }\nil void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){ x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll(); }\n_tp<_tyn T> void Read( T a[] , int st , int ed ){ Forx(i,st,ed) Read(a[i]); }\n#define iRead(k) int k; Read(k);\n#define iRead2(a,b) iRead(a); iRead(b);\n#define iRead3(a,b,c) iRead2(a,b); iRead(c);\n#define iRead4(a,b,c,d) iRead2(a,b); iRead2(c,d);\n#define lRead(k) ll k; Read(k);\n#define lRead2(a,b) lRead(a); lRead(b);\n#define lRead3(a,b,c) lRead2(a,b); lRead(c);\n#define lRead4(a,b,c,d) lRead2(a,b); lRead2(c,d);\n\n// File\n#define Fin(a) freopen(a,\"r\",stdin)\n#define Fout(a) freopen(a,\"w\",stdout)\nil void FILEIO(){\n\t#ifdef intLSY\n\t\tFin(\"in.in\");\n\t#endif\n}\nil void FILEIO( string pname ){\n\t#ifndef intLSY\n\t\tFin((pname+\".in\").c_str());\n\t\tFout((pname+\".out\").c_str());\n\t#else\n\t\tFin(\"in.in\");\n\t#endif\n}\nil void FILEIO_OICONTEST( string pname ){\n\tFin((pname+\".in\").c_str());\n\t#ifndef intLSY\n\t\tFout((pname+\".out\").c_str());\n\t#endif\n}\nvoid Printtime(){\n\t#ifdef intLSY\n\t\tdouble _timeuse = clock()* 1000.0 / CLOCKS_PER_SEC;\n\t\tfprintf(stderr,\"\\n\\nTime usage:\\n%.0lf ms\\n\",_timeuse);\n\t#endif\n}\nvoid END(){ Printtime(); exit(0); }\n_tp<_tyn T>void END( T mes ){ cout << mes << endl; END(); }\n\n// Debug\n#define B cerr << \"BreakPoint\" << endl;\n#define O(x) cerr << #x << \" \" << (x) << endl;\n#define o(x) cerr << #x << \" \" << (x) << \" \";\n#define Msz(x) cerr << \"Sizeof \" << #x << \" \" << sizeof(x)/1024/1024 << \" MB\" << endl;\n_tp<_tyn T>void Print( T a[] , int s , int t , char sp = ' ' , char ed = '\\n' ){\n\tif( s > t ) return;\n\tfor( int i = s ; i < t ; i++ )\n\t\tcout << a[i] << sp;\n\tcout << a[t] << ed;\n\tcout.flush();\n}\n_tp<_tyn T>void Print( T a , int s = 0 , int t = -1 , char sp = ' ' , char ed = '\\n' ){\n\tif( t == -1 ) t = a.size()-1;\n\tfor( int i = s ; i <= t ; i++ )\n\t\tcout << a[i] << sp;\n\tcout << ed;\n\tcout.flush();\n}\n\n// Optimize\n#define Max(a,b) ((a)>(b)?(a):(b))\n#define Min(a,b) ((a)<(b)?(a):(b))\n#define Mymax(a,b) (a) = max((a),(b))\n#define Mymin(a,b) (a) = min((a),(b))\n#define MyMax(a,b) (a) = Max((a),(b))\n#define MyMin(a,b) (a) = Min((a),(b))\n#define INF (0x3f3f3f3f)\n#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))\n#define Sqr(x) ((x)*(x))\n#define Lowbit(a) ((a)&(-(a)))\nvc Range( int l , int r ){\n\tvc ret{};\n\tForx(i,l,r) ret.Pb(i);\n\treturn ret;\n}\nmt19937 Rand(0xe38195e38293);\t// さん\n#define MOD (1000000007LL)\n#define Rmoi(a,b) (((a)%b+b)%b)\n#define Rmo(a) (((a)%MOD+MOD)%MOD)\n#define Rmod(a) a = ((a%MOD+MOD)%MOD)\n#define Rmodi(a,b) a = ((a%(b)+(b))%(b))\n#define Mymoi(a,b) ((a)>=b?((a)-b):(a))\n#define Mymodi(a,b) a = ((a)>=(b))?((a)-(b)):(a)\ntemplate\nil T Mymo( const T &x ){ return x >= MOD ? x-MOD : x; }\ntemplate\nil void Mymod( T &x ){ x = x >= MOD ? x-MOD : x; }\ntemplate\nil void Addi( T1 &x , const T2 &y ){ x = Mymo(x+y); }\ntemplate\nil void Deci( T1 &x , const T2 &y ){ x = Mymo(x-y+MOD); }\nil ll Pow( ll a , ll b , const ll p = MOD ){\n\tll ret = 1;\n\tfor( ; b ; a = a*a%p, b >>= 1LL )\n\t\tif(b&1LL) ret = ret*a%p;\n\treturn ret;\n}\nil ll Inv( ll a , const ll p = MOD ){ return Pow(a,p-2LL,p); }\n#define FACT 1048576\nll fact[FACT], invfact[FACT];\nil void InitFact(){\n\tfact[0] = 1;\n\tFor(i,FACT-1) fact[i] = fact[i-1]*i % MOD;\n\tinvfact[FACT-1] = Inv(fact[FACT-1]);\n\tfOR0(i,FACT-1) invfact[i] = invfact[i+1]*(i+1) % MOD;\n}\nil ll C( int n , int m ){\n\tif( n < m ) return 0;\n\treturn fact[n] * invfact[m]%MOD * invfact[n-m]%MOD;\n}\n///////////////////////////////////////////////////////////////////////////////////////\n///////////////////////////////////////////////////////////////////////////////////////\nconst int MAXN = 1048576;\n\nint n,k;\n\nll dp[MAXN], sum[MAXN];\n\nint main(){\n\tFILEIO();\n\tInitFact();\n\n\tRead(n,k);\n\n\tdp[0] = 1;\n\tsum[0] = 1;\n\tdp[1] = 1;\n\tsum[1] = 2;\n\tForx(i,2,n){\n\t\tdp[i] = fact[i-1] * (sum[i-1]-(i-k<=0?0:sum[i-k-1])+MOD) % MOD;\n\t\tsum[i] = ( sum[i-1] + dp[i]*invfact[i] ) % MOD;\n\t}\n\t// Print(dp,1,n);\n\n\tll ans = 0;\n\tFor(i,n){\n\t\tll tans = dp[i-1] * C(n-1,i-1) % MOD * fact[n-i] % MOD;\n\t\tAddi(ans,tans);\n\t}\n\tans = Rmo( fact[n] - ans );\n\tcout << ans << endl;\n\n\tEND();\n}\n", "lang_cluster": "C++", "tags": ["math", "dp"], "code_uid": "4a9aacd37cd3cb3faf526ed6dfeaf2d2", "src_uid": "0644605611a2cd10ab3a9f12f18d7ae4", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include \n#include \n#include \n#include\n#include\n#include \n#include \n#include\n#include\nusing namespace std;\n\n\ntypedef long long unsigned llu;\ntypedef long double ld;\n#define ll long long\n#define PI 3.14159265358979323846\n#define For(i, a, b) for (int i = a, sz = (int)b; i < sz; i++)\n#define rFor(i, a, b, c) for (int i = (a); i >= (b); i -= (c))\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define fill(a, b) memset(a, b, sizeof(a))\n#define IO ios_base::sync_with_stdio(false), cin.tie(0),cout.tie(0)\nconst ll mod = 1000000007;\nconst ll inf = 1000000000009;\n\n//freopen(\"input.txt\", \"r\", stdin);\n//freopen(\"output.txt\", \"w\", stdout);\n\nint main()\n{\n\tIO;\n\tint n{},ans{};\n\tcin>>n;\n\tFor(i,1,n+1)\n\t{\n\t\tFor(j,i,n+1)\n\t\t{\n\t\t\tint x=i^j;\n\t\t\tif(xn || i+j<=x)continue;\n\t\t\t\tans++;\n\t\t}\n\t}\n\tcout<\nusing namespace std;\ntypedef long long ll;\nll n,kk;\nvectorv;\nvectorsol;\nmapm;\nint main()\n{\n string s,s1;\n cin>>s;\n n =s.size();\n char a[100];\n for(int i=0;i=0;q--)\n {\n x+=(v[i][q]-'0')*y;\n y*=10;\n }\n sol.push_back(x);\n }\n sort(sol.begin(),sol.end());\n for(int i=sol.size()-1;i>=0;i--)\n {\n kk=sqrt(sol[i]);\n if(kk*kk==sol[i]&&sol[i]!=0)\n {\n s1=to_string(sol[i]);\n cout<\nusing namespace std;\n#define int long long\nint n, l, r, k, a;\nsigned main() {\n scanf(\"%lld%lld%lld%lld\", &n, &l, &r, &k);\n a = (r - l + n + 1) % n;\n int ans = -1;\n for (int i = 1, j; i <= k; i = j + 1) {\n int cnt = k / i;\n j = k / cnt;\n int lp = max(n, i);\n int rp = min(2 * n, j);\n\t\tlp = max(lp, (k - 2 * a + cnt - 1) / cnt);\n\t\trp = min(rp, min((2 * n + k - 2 * a + 1) / (cnt + 1), (k - a) / cnt));\n if (lp <= rp) ans = max(ans, rp - n);\n }\n if (k >= a && k <= a * 2) ans = max(ans, min(a, k - a + 1) + (n - a));\n if (!a && k == n) ans = max(ans, 1ll);\n printf(\"%lld\\n\", ans);\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "cce3ea883d04e9c61d2d42ad36b92b2c", "src_uid": "f54cc281033591315b037a400044f1cb", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define ll long long\n#define fi first\n#define sec second\n\nusing namespace std;\n\n\nconst int MAXN=160;\nconst int D= 51123987;\n\nint dp[MAXN][MAXN/3][MAXN/3][MAXN/3];\nint nxt[MAXN][3];\n\nbool check(int a,int b,int c){\n return (abs(a-b)<2)&(abs(a-c)<2)&(abs(b-c)<2);\n}\nint chrtint (char a){\n return a-'a';\n}\n\nint main(){\n int n;\n cin>>n;\n string s;\n cin>>s;\n\n\n\n string s2;\n // cout<<535<=0;i--){\n for(int j=0;j<3;j++){\n nxt[i][j]=nxt[i+1][j];\n // cout<\n#include \n\n\nusing namespace std;\n\n\n\nint main(int argc,char** argv)\n{\n\tstring first;\n\tstring second;\n\tcin>>first>>second;\n\tint pos=0;\n\tfor(int i=0;i<(int)second.size();i++)\n\t{\n\t\tif(first[pos]==second[i])\n\t\t{\n\t\t\tpos++;\n\t\t}\n\t}\n\tcout<\n\nusing namespace std;\n\nint n;\n\nstring s;\n\nint main()\n{\n\tcin >> n >> s;\n\tint ans = 0;\n\tfor(int i = 0; i < n; i++)\n\t{\n\t\tint cnt = 0;\n\t\twhile(s[i] == 'x' && i < n)\n\t\t{\n\t\t\tcnt++, i++;\n\t\t}\n\t\tif(cnt) i--;\n\t\tif(cnt >= 3) ans += cnt - 2;\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "strings"], "code_uid": "8c753ceea2236969a3a2d53c49974354", "src_uid": "8de14db41d0acee116bd5d8079cb2b02", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint main() {\n int u=0;\n string s;\n char c;\n cin>>s; \n c=s[1];\n for(int i=0;i=7){\n cout<<\"YES\\n\";\n break;\n }\n \n \n }\n if(u<7){\n cout<<\"NO\\n\";\n }\n\n\n \n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "1e85cb2ed675f8218f9ca4feb9852826", "src_uid": "ed9a763362abc6ed40356731f1036b38", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "// #pragma GCC optimize(\"O3\", \"unroll-loops\")\n// #pragma GCC target(\"avx2\")\n\n#include \nusing namespace std;\n\n#include \n#include \nusing namespace __gnu_pbds;\n\ntypedef long long int ll;\n#define endl '\\n'\n#define ld long double\n#define all(a) a.begin(),a.end()\n#define int long long\n#define pb push_back\n#define pii pair \n#define ff first\n#define ss second\n#define sz(v) (int)v.size()\n#define OST tree, rb_tree_tag,tree_order_statistics_node_update>\n\nconst int INF = 1e18 + 0;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\nconst int N = 1e7 + 1;\nint dp[2*N];\n\nsigned main() {\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n\n // #ifndef ONLINE_JUDGE\n // freopen(\"input.txt\", \"r\", stdin);\n // // freopen(\"output.txt\", \"w\", stdout);\n // #endif\n int n, in, c;\n cin >> n >> in >> c;\n for(int i = 1; i <= n; i++) {\n dp[i] = dp[i-1] + in;\n if(i%2 == 0) {\n dp[i] = min(dp[i], dp[i/2] + c);\n }\n else {\n dp[i] = min(dp[(i+1)/2] + c + in, dp[i]);\n }\n }\n cout << dp[n] << '\\n';\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "dp"], "code_uid": "e1d88605d0250c8e479168c893515a19", "src_uid": "0f270af00be2a523515d5e7bd66800f6", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n \nusing namespace std;\n \nconst int MAXL=105;\nchar a[MAXL], c[MAXL];\nint b, d, f[MAXL];\n \nint main()\n{\n scanf(\"%d%d%s%s\", &b, &d, a, c);\n int n=strlen(c);\n int i, j, k;\n for (i=0; i\nusing namespace std;\nint main()\n{\n long long int a,b;\n cin>>a>>b;\n if(a==b)\n {\n cout<<1;\n return 0;\n }\n //cout<<(b-a)<=5)\n {\n cout<<0;\n return 0;\n }\n if(a==0)\n a=1;\n if(b==0)\n b=1;\n int c=1;\n a=a%10;\n b=b%10;\n if(ba;i--)\n { // cout<<\"dfs\";\n if((i%10)==0 || c%10==0)\n {\n cout<\n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\nll n,k;\nint main()\n{\n\tcin>>n>>k;\n\tcout< \nusing namespace std;\n#define SZ(a) (int)a.size()\n\nint main() {\n\tstring st = \"\"; \n\tcin >> st; \n\tint arr[3440]; \n\tfor(int i = 0; i < 100; i++) arr[i] = 0; \n\tint sz = SZ(st); \n\tfor(int i = 0; i < sz; i++) {\n\t\tint x = (st[i] - '0'); \n\t\tarr[i] = x; \n\t}\n\tfor(int i = 0; i < sz; i++) {\n if(i == 0 && arr[i] == 9) continue; \n\t\tint x = 9 - arr[i]; \n\t\tarr[i] = min(arr[i], x); \n\t}\n\tint i = 0; \n\tfor(; i < sz; i++) \n\t\tcout << arr[i]; \n\n\tcout << \"\\n\"; \n}", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "d1107de815ba31aefe18a72043bd66b2", "src_uid": "d5de5052b4e9bbdb5359ac6e05a18b61", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\nusing namespace std;\nint pos[16];\nint main()\n{\n\tlong double n;\n\tcin>>n;\n\tlong double N = pow(2,n);\n\tfor(int i = 0; i < n; i++)\n\t\tcin>>pos[i];\n\tint d, j;\n\tfor(int i = 0; i < N; i++)\n\t{\n\t\td = 0;\n\t\tfor(j = 0; j < n; j++) d+=pos[j];\n\t\t//cout< 0) \n\t\t\twhile(pos[j] > 0) \n\t\t\t{\n\t\t\t\tpos[j] *= -1;\n\t\t\t\tj++;\n\t\t\t}\n\t\tpos[j] *= -1;\n\t//\tfor(j = 0; j < n; j++) cout<\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\n#define PB push_back\n#define ST first\n#define ND second\n\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\tint v[1000];\n\tint n, d;\n\tcin >> n >> d;\n\tfor(int i=0; i < n; i++)\n\t{\n\t\tcin >> v[i];\n\t}\n\tsort(v, v+n);\n\tint minr=1e3;\n\tfor(int i=0; i < n; i++)\n\t{\n\t\tfor(int j=n-1; j>=i; j--)\n\t\t{\n\t\t\tif(v[j]-v[i]<=d)\n\t\t\t{\n\t\t\t\tminr = min(minr, i+n-1-j);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tcout << minr << \"\\n\";\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "sortings", "greedy"], "code_uid": "e1be9943bceb2bab32f15500e0509944", "src_uid": "6bcb324c072f796f4d50bafea5f624b2", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n#define N 1005\n#define M 1000005\n#define LL long long\n#define fo(i,j,k) for(register int i=j;i<=k;++i)\n#define fx(i,j,k) for(register int i=j;i>=k;--i)\ntemplateinline void read(T &x) {\n int f=1;x=0;char c=getchar();\n for(;!isdigit(c);c=getchar())if(c=='-')f=-1;\n for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);\n x*=f;\n}\nint i,j,k,m,n;\nint a[101][101]={{0,1,2},{1,0,2},{1,2,0},{2,1,0},{2,0,1},{0,2,1},{0,1,2}};\nint main(){\n read(n);read(m);\n cout<\nusing namespace std;\nint a[10005], f[10005], b[305];\nint main() {\n ios::sync_with_stdio( false );\n int n, t, i, j;\n cin >> n >> t;\n int len = n * min( n, t );\n for ( i = 1; i <= n; i++ ) {\n cin >> a[i];\n b[a[i]]++;\n }\n int mxa = 1;\n for ( i = n + 1; i <= len; i++ )\n a[i] = a[i - n];\n for ( i = 1; i <= len; i++ ) {\n f[i] = 1;\n for ( j = i - 1; j >= max( i - n, 1 ); j-- ) {\n if ( a[i] >= a[j] )\n f[i] = max( f[i], f[j] + 1 );\n }\n mxa = max( mxa, f[i] );\n }\n if ( n >= t )\n cout << mxa << endl;\n else {\n int mxaa = 0;\n for ( i = 0; i < 305; i++ )\n mxaa = max( mxaa, b[i] );\n cout << mxa + mxaa * ( t - n ) << endl;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "constructive algorithms"], "code_uid": "4e79d802d3e002addeec0487b2ff0b04", "src_uid": "26cf484fa4cb3dc2ab09adce7a3fc9b2", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include \n#include\n#include\nusing namespace std;\nint a[30][30];\n\nint main()\n{\n\tint n, i, j, x, y;\n\tcin >> n;\n\tstring s;\n\tcin >> s;\n\tfor (i = 0; i < n - 1; i++) {\n\t\tx = s[i] - 'A';\n\t\ty = s[i + 1] - 'A';\n\t\ta[x][y]++;\n\t}\n\tint max = 0, b = 0, c = 0;\n\tfor (i = 0; i < 26; i++) {\n\t\tfor (j = 0; j < 26; j++) {\n\t\t\tif (max < a[i][j]) {\n\t\t\t\tb = i;\n\t\t\t\tc = j;\n\t\t\t\tmax = a[i][j];\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%c%c\\n\", b + 'A', c + 'A');\n}", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "525ddaff3b68627df3865ca74536ee06", "src_uid": "e78005d4be93dbaa518f3b40cca84ab1", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nbool cmp (int a, int b){\n return a>b;\n}\nint x[1005], n;\nbool solve(int k){\n priority_queue pq;\n while(!pq.empty()) pq.pop();\n int i,put, now;\n\n for(i = 0; i < k ; i++){\n pq.push(1005);\n }\n put = 0;\n for(i = 0; i < n ; i++){\n now = pq.top(); pq.pop();\n if(now!=0){\n now = min(now -1, x[i]);\n put++;\n }\n pq.push(now);\n }\n\n if(put==n) return true;\n else return false;\n}\nint main()\n{\n int i;\n\n cin >> n;\n for(i = 0; i < n ; i++)\n cin >> x[i];\n\n sort(x, x+n, cmp);\n\n for(i = 1; i <= n ; i++){\n if(solve(i)==true){\n printf(\"%d\\n\", i);\n break;\n }\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "greedy"], "code_uid": "344dafb4fa4e4b42cd0881a300b7530e", "src_uid": "7c710ae68f27f140e7e03564492f7214", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main(){\n\tint a,b,k,i,ans=0;\n\tcin>>a>>b>>k;\n\tfor(i=0;i\nusing namespace std;\n\nlong long sum, first;\nint x[1008];\nint main() \n{\n\n\tlong long n;\n\tcin >> n >> first;\n\tfor(int i = 0 ; i < n-1; i++)\n\t\tcin >> x[i] ;\n\t\n\tbool f = 1;\n\tsort(x, x+n-1);\n\t\n\twhile(f){\n\tf = 0;\n\tsort(x, x+n-1);\n\treverse(x, x+n-1);\n\t\tif(first <= x[0])\n\t\t{\n\t\t\tx[0]--;\n\t\t\tfirst++;\n\t\t\tsum++;\n\t\t\tf = 1;\n\t\t}\n\t}\n\n\tcout << sum;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "8ab1f8effd46718964a69ad9dffe61c7", "src_uid": "aa8fabf7c817dfd3d585b96a07bb7f58", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n//#include\n//#include\n\nusing namespace std;\n\ninline int read();\n\nstring s1,s2;\n\nint main(){\n cin>>s1>>s2;\n sort(s1.begin(),s1.end());\n if(s1[0]=='0')\n for(int i=1;i'9') {if(c=='-') flag=1; c=getchar();}\n\twhile(c>='0' && c<='9') {x=(x<<3)+(x<<1)+c-'0'; c=getchar();}\n\treturn flag?-x:x;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "3998f8aba2747e27aa50c36863834bcd", "src_uid": "d1e381b72a6c09a0723cfe72c0917372", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std; \n#define int long long \n#define endl \"\\n\"\n#define deb(x) cout<<#x<<\" \"<>ar[pen];}\n#define pr(ar) for(int pen=0;pen>n>>m;\n vector < pair > vt;\n for(int i=0;i>a>>b;\n if(a>b)\n swap(a,b);\n vt.push_back(make_pair(a,b));\n }\n if(n<=6)\n {\n cout<first;\n int b=it->second;\n // cout<\n\nusing namespace std;\n\nint main()\n{\n long long n ;\n cin>>n ;\n cout<<(n-2)*(n-2)<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define REP(i,n) for(int i=0;i MII;\ntypedef map MSI;\ntypedef vector VI;\ntypedef vector VS;\ntypedef vector VD;\ntypedef pair PII;\ntypedef long long int64;\ntypedef long long ll;\ntypedef unsigned int UI;\ntypedef long double LD;\ntypedef unsigned long long ULL;\n\nint n, m;\n\ninline int calc(int A, int B, int C, int low, int high) {\n\n\tint ret = 0;\n\tint res = B / (C - low);\n\tfor (int i = low; i <= high; ++i) {\n\t\twhile ((C - i) * (res + 1) <= B) ++res;\n\t\tret += A + res;\n\t}\n\n\treturn ret;\n}\n\nint main() {\n\tcin >> n >> m;\n\n\tlong long ans = 0, center = 0;\n\tfor (int i = m + 1; i <= n - i + 1; ++i) {\n\n\t\tint A = i - (n + 1);\n\t\tint B = (3 * i * (n + 1 - i) - 1);\n\t\tint C = i + n + 1;\n\n\t\t/*\n\t\twe need to count A + B / (C - X);\n\t\t*/\n\n\t\tint low = m + 1, high = n - m;\n\t\tif (low > high) continue;\n\t\twhile (low < high) {\n\t\t\tint mid = ((low + high) >> 1);\n\t\t\tif (A + B / (C - mid) >= m + 1) {\n\t\t\t\thigh = mid;\n\t\t\t} else {\n\t\t\t\tlow = mid + 1;\n\t\t\t}\n\t\t}\n\n\t\tif (i == n - i + 1) center = ans;\n\n\t\tif (A + B / (C - low) <= m) continue;\n\n\t\tint upperBound = low;\n\t\tlow = upperBound; high = n - m;\n\t\twhile (low < high) {\n\t\t\tint mid = ((low + high) >> 1) + 1;\n\t\t\tif (A + B / (C - mid) <= n - m) {\n\t\t\t\tlow = mid;\n\t\t\t} else {\n\t\t\t\thigh = mid - 1;\n\t\t\t}\n\t\t}\n\n\t\tif (A + B / (C - low) > n - m) {\n\t\t\tans += (n - m - upperBound + 1) * (n - 2 * m);\n\t\t\tcontinue;\n\t\t}\n\n\t\tans += (n - m - low) * (n - 2 * m);\n\t\tans += calc(A, B, C, upperBound, low) - m * (low - upperBound + 1);\n\t}\n\n\tif (n % 2 == 1) {\n\t\tans = center * 6 + (ans - center) * 3;\n\t} else {\n\t\tans *= 6;\n\t}\n\n\tcout << ans << endl;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "geometry"], "code_uid": "cfd0d31779ca43473859a5502e132ea6", "src_uid": "355cc23d7a4addfc920c6e5e72a2bb64", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint main()\n{\n\tstring str,x=\"\";\n\tcin >> str;\n\tint i, t1 = 0, t2 =str.size()-1;\n\tif (str.size() % 2 == 0)\n\t{\n\t\tfor (i = 0;i < str.size();i++)\n\t\t\tif (i % 2 == 0)\n\t\t\t{\n\t\t\t\tx += str[t2];\n\t\t\t\tt2--;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tx += str[t1];\n\t\t\t\tt1++;\n\t\t\t}\n\t}\n\telse\n\t{\n\t\tfor (i = 0;i < str.size();i++)\n\t\t\tif (i % 2 != 0)\n\t\t\t{\n\t\t\t\tx += str[t2];\n\t\t\t\tt2--;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tx += str[t1];\n\t\t\t\tt1++;\n\t\t\t}\n\t}\n\tfor (i = str.size() - 1;i >= 0;i--)\n\t\tcout << x[i];\n\tgetchar();\n\tgetchar();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "0edb8a97fbd31eb3d50d4a59baacba28", "src_uid": "992ae43e66f1808f19c86b1def1f6b41", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/****************************\n * Name :B. Good String\n * Time :2019-3-26 18:33\n * Author :大哥\n * Type :\n * 版权所有_翻版不管\n ****************************\n */\n#include \nusing namespace std;\n#define ll long long\n#define ld long double\n#define lowbit(x) (x&-x)\n#define pi acos(-1.0)\n#define inf 0x3f3f3f3f \n#define INF 0x3f3f3f3f3f3f3f3f\nll gcd(ll p,ll q){\n\treturn q==0?p:gcd(q,p%q);\n}\nll qpow(ll p,ll q){\n\tll f=1; while(q){\n\t\tif(q&1)f=f*p; p=p*p; q>>=1;\n\t}return f;\n}\n\n#define exp 1e-8\n#define MOD 1000000007\n#define M 105\n#define N 1005\n\n\nint main(){\n#ifdef LOCAL\n\tfreopen(\"in.txt\",\"r\",stdin);\n\t//freopen(\"out.txt\",\"w\",stdout);\n#endif \n\tint n;\n\tcin>>n;\n\tll ans=0;\n\tfor(int i=2; i\n\n#define endl \"\\n\" \nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair;\n\nconstexpr int N = 2e5 + 5; \n\nint n, b, p, x = 0, y = 0;\n\nvoid Solve() {\n cin >> n >> b >> p;\n cout << (n - 1) * ((b << 1) + 1) << \" \" << n * p << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n // int t;\n // cin >> t;\n // while(t--)\n //auto start = chrono::high_resolution_clock::now();\n Solve();\n // auto end = chrono::high_resolution_clock::now();\n // cout << endl << (chrono::duration_cast>(end - start)).count();\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "b4b470b95e942b66fa6124a09bbb4845", "src_uid": "eb815f35e9f29793a120d120968cfe34", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "// the problem is to find a subset with all same elements\n// of maximum size such that their sum is not formable by\n// taking any combination of the other elements other than\n// those in the subset (the 'other' could include 'that'\n// 'same' element itself too)\n\n// so dp state is dp[sum][k elements] where 'k' is the number\n// of elements used to form the 'sum'\n\n// eg: (5 5 5), (4 5 6) and hence dp[15][3] = 2 ways\n\n// now you can't iterate over elements of the array like\n// in knapsack/subset sum problem as in the case of duplicates\n// you'll end up counting the same way twice.\n\n// Eg: if arr = 5 5 5\n// knapsack ind 1:\n// dp[5][1] = 1 dp[10][2] = 0\n// knapsack ind 2:\n// dp[5][1] = 2 dp[10][2] = 1\n// knapsack ind 3:\n// dp[5][1] = 3 dp[10][2] = 3\n// which is not what we want for dp[10][2]\n// if sum is composed of same elements, we want it to be\n// counted as just 1 way.\n\n// 20\n// 50 50 50 50 39 39 9 9 24 24 96 90 90 77 59 98 98 77 96 80\n// that happens for 150 = 50*3 in the above case\n// hence the first method in main below doesn't work\n// i.e. commented out code\n\n// however, if you change the update to only include\n// values from the previous distinct value, then it works\n// i.e.: 4 4 5 5\n// for 4,4 do one pass for all relevant (sum,k) for dp[sum][k]\n// for 5,5 do the next pass, but use old dp[sum][k] from previous 4,4\n// \tpass to update values for this 5,5 combo\n// this is illustrated in the accepted code\n\n// way better than this using two dp arrays with last index\n// indicating which is old and which is new, is to fill in some\n// order so that what we want holds - this is done by Swistakk's\n// solution below, he fills it in descending order for (sum,k)\n// in that way we don't repeat values.\n\n#include \n\nusing namespace std;\n\n#define pb push_back\n#define mp make_pair\n#define MOD 1000000007\n#define PI 3.1415926535897932\n#define ms(s, n) memset(s, n, sizeof(s))\n#define prec(n) fixed<>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}\nll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}\n\n// ncr mod p\n// vector fact(200005,0);\n// vector invvec(200005,0);\n\n// void fillfact(){\n// fact[0] = 1; invvec[0] = 1;\n// fact[1] = 1; invvec[1] = 1;\n// forr(i,2,200001){\n// fact[i] = mult(fact[i-1],i);\n// invvec[i] = inv(fact[i]);\n// }\n// }\n\n// ll ncr(ll n, ll r){\n// return (mult(fact[n],mult(invvec[r],invvec[n-r])));\n// }\n\n// disjoint sets union\n// vector par(200005,0);\n// vector rnk(200005,0);\n// ll find_root(ll v) {\n// return par[v] == v ? v : (par[v] = find_root(par[v]));\n// }\n \n// void merge(ll x,ll y) {\n// if ((x = find_root(x)) == (y = find_root(y))) return;\n// if(rnk[y] > rnk[x])\n// swap(x, y);\n// par[y] = x;\n// rnk[x] += rnk[y];\n// }\n\n\n// sieve for first 1000 primes\n// vector prime(100005,0);\n// vector vec;\n\n// void sieve() {\n// forr(i, 2, 8005) {\n// if (!prime[i]) {\n// vec.pb(i);\n// for(ll j = 2 * i; j < prime.size(); j += i) \n// prime[j] = 1;\n// }\n// }\n// }\n\nint main(){\n\n\tint n;\n\tcin>>n;\n\tmap cnt;\n\t// vector arr;\n\tforr (i, 0, n) {\n\t\tint a;\n\t\tcin>>a;\n\t\t// arr.pb(a);\n\t\tcnt[a]++;\n\t}\n\n\t// notice that this doesn't work\n\t// the reason is that you end up recounting things\n\t// for sums made of same elements, which should\n\t// not be done since the condition we're checking\n\t// for is that dp[sum][k] = 1\n\n\t// sort(all(arr));\n\t// int R = 10001;\n\t// vector>> ways(R,vector>(n+1,vector(2,0)));\n\t// ways[0][0][0] = 1;\n\t// ways[0][0][1] = 1;\n\t// int sp = 0;\n\t// forr(k, 0, sz(arr)){\n\t// \tsp++;\n\t// \tforr(i,0,R){\n\t// \t\tforr(now,1,n+1){\n\t// \t\t\tways[i][now][sp&1] = ways[i][now][(sp-1)&1];\n\t// \t\t\tif(i-arr[k] >= 0)\n\t// \t\t\t\tways[i][now][sp&1] += ways[i-arr[k]][now-1][(sp-1)&1];\n\t// \t\t\tways[i][now][sp&1] = min(ways[i][now][sp&1],9);\n\t// \t\t}\n\t// \t}\n\t// }\n\t// forr(i,0,151){\n\t// \tif(i<10)\n\t// \t\tcout<>> ways(R,vector>(n+1,vector(2,0)));\n\t// initialization takes care so that even single\n\t// elements are accounted for\n\t// i.e.: if ele=5, dp[0][0] = 1, hence dp[5][1] = 1\n\tways[0][0][0] = 1;\n\tways[0][0][1] = 1;\n\tint sp = 0;\n\tfor (auto p : cnt) {\n\t\tsp++;\n\t\tint wei = p.first;\n\t\tint mul = p.second;\n\t\t// for all sums\n\t\tforr(i,0,R){\n\t\t\t// for all k (k has meaning in topmost comments section)\n\t\t\t// - i.e. ways to form sum 'i' using exactly 'now' elements\n\t\t\tforr(now,0,n+1){\n\t\t\t\t// use only dp vals from previous\n\t\t\t\t// distince 'wei' for this iter\n\t\t\t\tways[i][now][sp&1] = ways[i][now][(sp-1)&1];\n\t\t\t\t// iterating over all relevant j given i,now\n\t\t\t\t// so that doesn't go out of bounds\n\t\t\t\tforr (j, 1, min(i/wei, min(mul, now))+1) {\n\t\t\t\t\t// lhs: current dp arr, rhs: prev dp arr\n\t\t\t\t\t// where prev is dp arr for (4,4) if cur is dp arr for\n\t\t\t\t\t// (4,4,5,5) and arr = 4,4,5,5\n\t\t\t\t\tways[i][now][sp&1] += ways[i-j*wei][now-j][(sp-1)&1];\n\t\t\t\t\tways[i][now][sp&1] = min(ways[i][now][sp&1],2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif (sz(cnt) == 2) {\n\t\tcout<>n;\n// map cnt;\n// RE (i, n) {\n// int a;\n// cin>>a;\n// cnt[a]++;\n// }\n// int R = 1e4 + 5;\n// vector> ways(R, vector(n + 1));\n// ways[0][0] = 1;\n// for (auto p : cnt) {\n// int wei = p.st;\n// int mul = p.nd;\n// FORD (i, R - 1, 0) {\n// FORD (now, n, 0) {\n// FOR (j, 1, min(i / wei, min(mul, now))) {\n// ways[i][now] += ways[i - j * wei][now - j];\n// mini(ways[i][now], 2ll);\n// }\n// }\n// }\n// }\n// if (SZ(cnt) == 2) {\n// cout<\nusing namespace std;\n\nint main()\n{\n int b[70000],a[100005]= { 0 },i,j,k,n,c,m,v,sum = 0,cnt = 0;\n cin>>n;\n v = sqrt(10000005);\n c = 0;\n for(i = 3; i < v; i += 2)\n {\n if(a[i] == 0)\n {\n for(j = i * i ; j < 100005 ; j += i+i) a[j] = 1;\n }\n }\n b[c++] = 2;\n for(i = 3; i < 100005; i += 2){\n {\n if(a[i] == 0) b[c++] = i;\n }\n }\n for( i = 6; i <= n ; i ++)\n {\n cnt = 0;\n m = n;\n for(c = 0; c <= m; c++)\n {\n if(i % b[c] == 0)\n {\n ++cnt;\n m = i / b[c];\n // cout<\n#define int long long\nusing namespace std;\n\nconst int maxn=4000+10;\nint mod=1000*1000*1000+7;\nint dp[maxn];\nint s[maxn];\nint c[maxn][maxn];\n\nvoid F(int n)\n{\n for(int i=0;i<=n;i++)\n {\n c[i][i]=1;\n c[0][i]=1;\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j>n;\n s[0]=1;\n s[1]=1;\n F(n);\n for(int i=2;i<=n;i++)\n {\n s[i]=s[i-1];\n for(int j=1;j<=i-1;j++)\n {\n s[i]+=c[j][i-1]*s[i-j-1];\n s[i]%=mod;\n }\n }\n dp[0]=1;\n dp[1]=1;\n for(int i=2;i<=n;i++)\n {\n dp[i]=2*dp[i-1]+s[i-1];\n dp[i]%=mod;\n for(int j=1;j<=i-2;j++)\n {\n dp[i]+=dp[i-j-1]*c[j][i-1];\n dp[i]%=mod;\n }\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define eps 1e-12\n#define pi 3.14159265358979323846\n#define pb push_back\n#define mp make_pair\n#define st first\n#define nd second\n#define bgn begin\n#define ll long long\n#define ld long double\n#define ull unsigned long long\n#define ii pair;\n\n\n\n\n\n\n\n\n\nconst int N=510;\nchar s[N],cr;\nint n;\n\n\n\n\nvoid solve()\n{\n\tcin>>s;\n\tn=strlen(s);\n\tcr='a';\n\tfor(int i=0;icr)\n\t\t{\n\t\t\tcout<<\"NO\\n\";\n\t\t\treturn;\n\t\t}\n\t\telse if(s[i]==cr)\n\t\t{\n\t\t\tif(cr<'z')cr++;\n\t\t}\n\t}\n\tcout<<\"YES\\n\";\n}\n\nint main()\n{\n\tstd::ios::sync_with_stdio(0);\n\tcin.tie(0);\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.txt\",\"r\",stdin);\n#endif\n\tsolve();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "strings", "implementation"], "code_uid": "b8d857e5aa099eeea106161b25f79e26", "src_uid": "c4551f66a781b174f95865fa254ca972", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n int N,X,Y;\n while(scanf(\"%d%d%d\",&N,&X,&Y) != EOF )\n {\n for(int i = 0 ; ; i++)\n {\n if ( ( X + i ) * 100 >= ( N * Y ) )\n {\n printf(\"%d\\n\",i);\n break;\n }\n }\n }\n return 0;\n}\n\n \t \t\t\t \t \t \t \t \t", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "4eb7c21f099fb34bedc21a1bcbd48e74", "src_uid": "7038d7b31e1900588da8b61b325e4299", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\nusing namespace std;\n#define ll long long\ntypedef vector vi;\n#define all(v) ((v).begin()),((v).end())\nvoid fast()\n{std::ios_base::sync_with_stdio(0);\n\tcin.tie(NULL);\n\tcout.tie(NULL);}\nint gcd(int a, int b) \n{ \n if (b == 0) \n return a; \n return gcd(b, a % b); \n}\nint main(){\n fast();\n int n,x1,y1,x2,y2;\n cin>>n>>x1>>y1>>x2>>y2;\n long long dist1=0,dist2=0;\n if(x1==x2 && (x1==0 || x1==n) || y1==y2 && (y1==n || y2==0)){\n cout<\n#include \n#include \n#include // tolower\n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n\n/*\n * g++ -g -std=c++11 -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp}\n *\n * TODO:\n * C++ dataframe\n * stl11 -> c++11 standard template lib in c++98\n * overload >> for map and set, using (insert) iterator\n * chmod:: consider an algorithm stable to int64 overflow\n * shortest path algorithm\n * shortest path in a tree\n * maximum network flow\n * partial idx/iter sort\n * a prime number generator which traverses prime numbers w/ ++\n * a divisor generator which traverses divisors efficiently\n * Apply1st ?!\n * Apply2nd and bind2nd ?!\n * count_if ( a.begin(), a.end(), a.second < x )\n * Arbitrary-precision arithmetic / Big Integer / Fraction - rational num\n * tuple class --> cplusplus.com/reference/tuple\n * get<1>, get<2>, bind2nd ( foo ( get<2> pair ), val )\n * isin( const T & val, first, last )\n * fuinction composition in c++\n * blogea.bureau14.fr/index.php/2012/11/function-composition-in-c11/\n * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition/\n * TimeWrapper -- accumulate time of a function call\n * stackoverflow.com/questions/879408\n * hash map -- possible hash value & obj % some_big_prime [ b272 ]\n * lower level can be a simple map to resolve hash collisions\n * add explicit everywhere necessary\n * bloom filters\n * heap -> how to update values in place / increase-key or decrease-key ... IterHeap ?!\n * median maintaince --> max_heap / min_heap\n * prim's min spaning tree alg. O ( m log n ) using heap contianing V - X vertices\n * kruskal algorithm minimum cost spanning tree with union find data structure\n * unique_ptr\n * hufman codes\n * simple arithmatic tests\n * longest common subsequence using seq. alignment type algorithm\n * longest common substring ( consequative subsequeance )\n * Graham scan; en.wikipedia.org/wiki/Graham_scan\n * problem/120/F -- how to extend in order to calculate all-pair distances in a tree\n * compile time prime number calculator using templates\n * swith to type_cast < T1, T2 > for val2str & str2val\n * overload plus<> for two vectors?!\n * overloading operator << for std::tuple:\n * stackoverflow.com/questions/9247723\n * bitmask class with iterator protocol\n * ??? segment tree +++ interval tree\n * ??? an lru cache: a heap which maintains last access, an a map which maintains key:value\n */\n\n/*\n * @recepies\n * ----------------------------------------------\n * odd / even\n * transform ( x.begin(), x.end(), x.begin(), bind2nd( modulus(), 2 ));\n * count_if ( x.begin(), x.end(), bind2nd( modulus < int > (), 2));\n * Apply2nd\n * max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > )\n * sort ( a.begin(), a.end(), Apply2nd < char, int , greater< int > > )\n * count_if ( m.begin(), m.end(), Apply2nd < string, int, modulus < int > > )\n * accumulate ( m.begin(), m.end(), 0.0, Apply2nd < int, double, plus < double > > )\n * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > )\n * abs_diff\n * adjacent_difference ( a.begin(), a.end(), adj_diff.begin( ), abs_diff < int > ( ) )\n * accumulate ( a.begin(), a.end(), 0, abs_diff < int > ( ) )\n * erase\n * a.erase ( remove_if ( a.begin( ), a.end( ), bind2nd ( less < int >( ), 0 ) ), a.end( ) )\n * a.erase ( remove ( a.begin( ), a.end( ), b.begin( ), b.end( ) ), a.end ( ) )\n * binding\n * bind2nd ( mem_fun_ref (& ClassName::m_func ), arg ) // binds the argument of the object\n * iterator generators\n * generate_n ( back_inserter ( a ), n, rand ); // calls push_back\n * generate_n ( inserter( a, a.begin( ) + 5 ), 10, RandInt( 0 , 100 ) ) // calls insert\n * copy ( foo.begin( ), foo.end( ), insert_iterator < std::list < double > > ( bar, bar.begin( ) + 5 ))\n * copy ( a.begin( ), a.end( ), ostream_iterator < double > ( cout, \", \" ))\n * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > )\n * transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length));\n */\n\n/*\n * @good read\n * ----------------------------------------------\n * [ partial ] template specialization\n * cprogramming.com/tutorial/template_specialization.html\n * function composition\n * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition\n */\n\n/*\n * @prob set\n * ----------------------------------------------\n * purification --> c330\n */\n\n/*\n * @limits\n * ----------------------------------------------\n * int 31 2.14e+09\n * long int 31 2.14e+09\n * unsigned 32 4.29e+09\n * long unsigned 32 4.29e+09\n * size_t 32 4.29e+09\n * long long int 63 9.22e+18\n * long long unsigned 64 1.84e+19\n */\n\n/*\n * issues\n * ----------------------------------------------\n * stackoverflow.com/questions/10281809\n * mem_fun -> func_obj ( pointer to instance, origanal argument )\n * bind1st ( mem_fun ( & ClassName::m_func ), this ) // binds obj of the class\n * bind1st takes 'const T &' as the first argument\n */\n\n/*\n * typedef / define\n * ----------------------------------------------\n */\n\ntypedef long long int int64;\ntypedef unsigned long long int uint64;\n\n#ifndef M_PI\n#define M_PI 3.14159265358979323846L\n#endif\n\n#define DOUBLE_INF std::numeric_limits< double >::infinity()\n#define DOUBLE_NAN std::numeric_limits< double >::quiet_NaN()\n\n#define __STR__(a) #a\n#define STR(a) __STR__(a)\n\n#define ASSERT(expr, msg) \\\n if (!( expr )) \\\n throw std::runtime_error(__FILE__ \":\" STR(__LINE__) \" - \" msg);\n\n#define DECLARE( X ) \\\n typedef std::shared_ptr < X > X ## _shared_ptr; \\\n typedef const std::shared_ptr < X > X ## _const_shared_ptr;\n\n#ifdef BUG\n #define DEBUG(var) { std::cout << #var << \": \" << (var) << std::endl; }\n #define COND_DEBUG(expr, var) if (expr) \\\n { std::cout << #var << \": \" << (var) << std::endl; }\n #define EXPECT(expr) if ( ! (expr) ) std::cerr << \"Assertion \" \\\n << #expr \" failed at \" << __FILE__ << \":\" << __LINE__ << std::endl;\n#else\n #define DEBUG(var)\n #define COND_DEBUG(expr, var)\n #define EXPECT(expr)\n#endif\n\n#define DBG(v) std::copy( v.begin(), v.end(), std::ostream_iterator < typeof( *v.begin() )> ( std::cout, \" \" ) )\n\n#if __cplusplus < 201100\n #define move( var ) var\n#endif\n\n/*\n * http://rootdirectory.de/wiki/SSTR()\n * usage:\n * SSTR( \"x^2: \" << x*x )\n */\n#define SSTR( val ) dynamic_cast< std::ostringstream & >( std::ostringstream() << std::dec << val ).str()\n\n\n/* https://www.quora.com/C++-programming-language/What-are-some-cool-C++-tricks */\n// template \n// char (&ArraySizeHelper(T (&array)[N]))[N];\n// #define arraysize(array) (sizeof(ArraySizeHelper(array)))\n\n/*\n * forward decleration\n * ----------------------------------------------\n */\nclass ScopeTimer;\n\n/*\n * functional utils\n * ----------------------------------------------\n */\n\ntemplate < typename T >\nstruct abs_diff : std::binary_function < T, T, T >\n{\n typedef T value_type;\n\n inline value_type operator( ) ( const value_type & x, const value_type & y ) const\n {\n return std::abs( x - y );\n }\n};\n\n// template < class InputIterator, class T >\n// class isin : public std::binary_function < InputIterator, InputIterator, bool >\n// {\n// public:\n// typedef T value_type;\n//\n// isin ( const InputIterator & first, const InputIterator & last ):\n// m_first ( first ), m_last ( last ) { }\n//\n// bool operator ( ) ( const value_type & val ) const\n// {\n// return std::find ( m_first, m_last, val ) != m_last;\n// }\n// private:\n// const InputIterator m_first, m_last;\n// }\n\ntemplate < typename value_type, typename cont_type >\nclass isin : public std::unary_function < value_type, bool >\n{\n public:\n isin( const cont_type & vals ): m_vals ( vals ) { };\n\n bool operator ( ) ( const value_type & x ) const\n {\n return std::find ( m_vals.begin( ), m_vals.end( ), x ) != m_vals.end( );\n }\n\n private:\n const cont_type m_vals;\n};\n\n/*\n * max_element, min_element, count_if ... on the 2nd element\n * eg: max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > )\n */\ntemplate < class T1, class T2, class BinaryOperation >\nclass Apply2nd : std::binary_function < typename std::pair < T1, T2 >,\n typename std::pair < T1, T2 >,\n typename BinaryOperation::result_type >\n{\n public:\n typedef T1 first_type;\n typedef T2 second_type;\n\n typedef typename BinaryOperation::result_type result_type;\n typedef typename std::pair < first_type, second_type > value_type;\n\n inline result_type operator( ) ( const value_type & x, const value_type & y ) const\n {\n return binary_op ( x.second , y.second );\n }\n private:\n BinaryOperation binary_op;\n};\n\n/*\n * algo utils\n * ----------------------------------------------\n */\n\n\n/**\n * count the number of inversions in a permutation; i.e. how many\n * times two adjacent elements need to be swaped to sort the list;\n * 3 5 2 4 1 --> 7\n */\ntemplate < class InputIterator >\ntypename std::iterator_traits::difference_type\ncount_inv ( InputIterator first, InputIterator last )\n{\n typedef typename std::iterator_traits::difference_type difference_type;\n typedef typename std::iterator_traits::value_type value_type;\n\n std::list < value_type > l; /* list of sorted values */\n difference_type cnt = 0;\n\n for ( difference_type n = 0; first != last; ++first, ++n )\n {\n /* count how many elements larger than *first appear before *first */\n typename std::list < value_type >::iterator iter = l.begin( );\n cnt += n;\n\n for ( ; iter != l.end( ) && * iter <= * first; ++ iter, -- cnt )\n ;\n l.insert( iter, * first );\n }\n\n return cnt;\n}\n\ntemplate < class ForwardIterator, class T >\ninline void fill_inc_seq ( ForwardIterator first, ForwardIterator last, T val )\n{\n for ( ; first != last; ++first, ++val )\n * first = val;\n}\n\ntemplate \nForwardIterator remove ( ForwardIterator first, ForwardIterator last, InputIterator begin, InputIterator end )\n{\n ForwardIterator result = first;\n\n for ( ; first != last; ++ first )\n if ( find ( begin, end, *first ) == end )\n {\n *result = *first;\n ++result;\n }\n\n return result;\n}\n\n/* stackoverflow.com/questions/1577475 */\ntemplate < class RAIter, class Compare >\nclass ArgSortComp\n{\n public:\n ArgSortComp ( const RAIter & first, Compare comp ): m_first ( first ), m_comp( comp ) { }\n inline bool operator() ( const size_t & i, const size_t & j ) const\n {\n return m_comp ( m_first[ i ] , m_first[ j ] );\n }\n private:\n const RAIter & m_first;\n const Compare m_comp;\n};\n\n/*!\n * usage:\n * vector < size_t > idx;\n * argsort ( a.begin( ), a.end( ), idx, less < Type > ( ) );\n */\ntemplate < class RAIter, class Compare=std::less< typename RAIter::value_type > >\nvoid argsort( const RAIter & first,\n const RAIter & last,\n std::vector < size_t > & idx,\n Compare comp = Compare()) // std::less< typename RAIter::value_type >() )\n{\n const size_t n = last - first;\n idx.resize ( n );\n\n for ( size_t j = 0; j < n; ++ j )\n idx[ j ] = j ;\n\n std::stable_sort( idx.begin(), idx.end(), ArgSortComp< RAIter, Compare >( first, comp ));\n}\n\ntemplate < class RAIter, class Compare >\nclass IterSortComp\n{\n public:\n IterSortComp ( Compare comp ): m_comp ( comp ) { }\n inline bool operator( ) ( const RAIter & i, const RAIter & j ) const\n {\n return m_comp ( * i, * j );\n }\n private:\n const Compare m_comp;\n};\n\n/*!\n * usage:\n * vector < list < Type >::const_iterator > idx;\n * itersort ( a.begin( ), a.end( ), idx, less < Type > ( ) );\n */\ntemplate \nvoid itersort ( INIter first, INIter last, std::vector < RAIter > & idx, Compare comp )\n{\n /* alternatively: stackoverflow.com/questions/4307271 */\n idx.resize ( std::distance ( first, last ) );\n for ( typename std::vector < RAIter >::iterator j = idx.begin( ); first != last; ++ j, ++ first )\n * j = first;\n\n std::sort ( idx.begin( ), idx.end( ), IterSortComp< RAIter, Compare > (comp ) );\n}\n\n\n/*\n * string utils\n * ----------------------------------------------\n */\n\ninline void erase ( std::string & str, const char & ch )\n{\n std::binder2nd < std::equal_to < char > > isch ( std::equal_to < char > ( ), ch );\n std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isch );\n str.erase ( iter, str.end() );\n}\n\ninline void erase ( std::string & str, const std::string & chrs )\n{\n isin < char, std::string > isin_chrs ( chrs );\n std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isin_chrs );\n str.erase ( iter, str.end() );\n}\n\ntemplate < typename value_type>\ninline std::string val2str ( const value_type & x )\n{\n std::ostringstream sout ( std::ios_base::out );\n sout << x;\n return sout.str();\n}\n\ntemplate < typename value_type>\ninline value_type str2val ( const std::string & str )\n{\n std::istringstream iss ( str, std::ios_base::in );\n value_type val;\n\n iss >> val;\n return val;\n}\n\nstd::vector< std::string > tokenize ( const std::string & str, const char & sep )\n{\n /*!\n * outputs empty tokens and assumes str does not start with sep\n * corner cases:\n * empty string, one char string,\n * string starting/ending with sep, all sep, end with two sep\n */\n\n std::vector < std::string > res;\n std::string::const_iterator follow = str.begin(),\n lead = str.begin();\n\n while ( true )\n {\n while ( lead != str.end() && * lead != sep )\n ++ lead;\n\n res.push_back ( std::string( follow, lead ) );\n\n if ( lead != str.end () )\n follow = 1 + lead ++ ;\n else\n break;\n }\n\n return res;\n}\n\n/*!\n * chunk a string into strings of size [ at most ] k\n */\nvoid chunk ( const std::string::const_iterator first,\n const std::string::const_iterator last,\n const size_t k,\n const bool right_to_left,\n std::list < std::string > & str_list )\n{\n str_list.clear( );\n\n if ( right_to_left )\n /* chunk from the end of the string */\n for ( std::string::const_iterator i, j = last; j != first; j = i )\n {\n i = first + k < j ? j - k : first;\n str_list.push_back ( std::string ( i, j ) );\n }\n else\n /* chunk from the begining of the string */\n for ( std::string::const_iterator i = first, j; i != last; i = j )\n {\n j = i + k < last ? i + k : last;\n str_list.push_back ( std::string ( i, j ) );\n }\n}\n\n/*!\n * next lexicographically smallest string\n * within char set a..z\n */\nstd::string & operator++( std::string & s )\n{\n /* find the first char from right less than 'z' */\n std::string::reverse_iterator j = find_if( s.rbegin( ), s.rend( ),\n std::bind2nd( std::less < char > ( ), 'z' ));\n\n if ( j != s.rend( ))\n {\n ++ *j;\n std::fill( s.rbegin( ), j, 'a' );\n }\n else\n s.assign( s.length( ) + 1, 'a' );\n\n return s;\n}\n\n/*!\n * getline ( cin, str )\n * requires ctrl-D\n * cin >> str; does not pass after space char\n */\n\n\n/*\n * number utils\n * ----------------------------------------------\n */\n\n\nclass BigInteger\n{\n#if ULONG_MAX <= 1 << 32\n typedef long long unsigned val_type;\n#else\n typedef long unsigned val_type;\n#endif\n const static int WSIZE = 32;\n const static val_type BASE = 1LL << WSIZE;\n public:\n private:\n std::list < val_type > val; /* val[ 0 ] is most significant */\n bool pos; /* true if sign is positive */\n};\n\n/**\n * greatest common divisor - Euclid's alg.\n */\ntemplate < typename value_type > inline\nvalue_type gcd ( value_type a, value_type b )\n{\n return ! b ? a : gcd( b, a % b );\n}\n\n/**\n * prime factorization\n */\ntemplate < class T >\nvoid prime_factors( T n, std::map < T, size_t > & fac )\n{\n for ( T k = 2; n > 1; ++ k )\n if ( ! ( n % k ) )\n {\n size_t & ref = fac[ k ];\n while ( ! ( n % k ) )\n {\n ++ ref;\n n /= k;\n }\n }\n}\n\n/* abs diff - safe for unsigned types */\ntemplate < class T >\ninline T absdiff( T a, T b )\n{\n return a < b ? b - a : a - b;\n}\n\nnamespace\n{\n template < class T >\n std::pair < T, T > __extgcd ( const T & x0, const T & y0,\n const T & x1, const T & y1,\n const T & r0, const T & r1 )\n {\n const T q = r0 / r1;\n const T r2 = r0 % r1;\n\n if ( ! ( r1 % r2 ) )\n return std::make_pair < T, T > ( x0 - q * x1, y0 - q * y1 );\n\n const T x2 = x0 - q * x1;\n const T y2 = y0 - q * y1;\n\n return __extgcd ( x1, y1, x2, y2, r1, r2 );\n }\n}\n\n/**\n * extended euclidean algorithm: a x + b y = gcd( a, b)\n * en.wikipedia.org/wiki/Extended_Euclidean_algorithm\n */\ntemplate < class value_type > inline\nstd::pair < value_type, value_type > extgcd ( value_type a, value_type b )\n{\n return a % b\n ? __extgcd < value_type > ( 1, 0, 0, 1, a, b )\n : std::make_pair < value_type, value_type > ( 0, 1 );\n}\n\n\n/**\n * modular multiplicative inverse\n * en.wikipedia.org/wiki/Modular_multiplicative_inverse\n */\ntemplate < class value_type > inline\nvalue_type modinv ( value_type a, value_type m )\n{\n const std::pair < value_type, value_type > coef ( extgcd( a, m ) );\n\n /* a needs to be coprime to the modulus, or the inverse won't exist */\n if ( a * coef.first + m * coef.second != 1 )\n throw std::runtime_error ( val2str( a ) + \" is not coprime to \" + val2str( m ));\n\n /* return a pos num between 1 & m-1 */\n return ( m + coef.first % m ) % m;\n}\n\ninline bool isnan ( const double & a )\n{\n return ! ( a == a );\n}\n\ntemplate < typename value_type >\ninline value_type mini ( int n, ... )\n{\n va_list vl;\n va_start (vl, n);\n value_type res = va_arg ( vl, value_type );\n\n for ( int i = 1; i < n; ++i ) {\n const value_type val = va_arg ( vl, value_type );\n res = std::min ( res, val );\n }\n\n va_end( vl );\n return res;\n}\n\ntemplate < typename value_type >\ninline value_type maxi ( int n, ... )\n{\n va_list vl;\n va_start (vl, n);\n value_type res = va_arg ( vl, value_type );\n\n for ( int i = 1; i < n; ++i ) {\n const value_type val = va_arg ( vl, value_type );\n res = std::max ( res, val );\n }\n\n va_end( vl );\n return res;\n}\n\n// XXX look this up how is this implemented\ntemplate < class T >\ninline int sign ( const T & x )\n{\n if ( x == T() )\n return 0;\n else if ( x < T() )\n return -1;\n else\n return 1;\n}\n\n/*\n * change moduluos from n to m\n */\nstd::string chmod ( std::string num, const unsigned n, const unsigned m )\n{\n const char * digit = \"0123456789abcdefghijklmnopqrstuvwxyz\";\n std::transform ( num.begin(), num.end(), num.begin(), tolower );\n\n isin < char, std::string > is_alpha_num ( digit );\n assert ( find_if ( num.begin( ), num.end( ), std::not1 ( is_alpha_num ) ) == num.end( ));\n\n unsigned long long int val ( 0 );\n\n if ( n == 10U )\n {\n std::istringstream iss ( num, std::ios_base::in );\n iss >> val;\n }\n else\n for ( std::string::const_iterator iter = num.begin( ); iter != num.end( ); ++ iter )\n val = val * n + ( 'a' <= *iter ? *iter - 'a' + 10U : *iter - '0');\n\n if ( m == 10U )\n {\n std::ostringstream sout ( std::ios_base::out );\n sout << val;\n return sout.str ( );\n }\n else\n {\n std::string res;\n for ( ; val ; val /= m )\n res.push_back( digit [ val % m ] );\n\n return res.length( ) ? std::string( res.rbegin( ), res.rend( )) : \"0\";\n }\n}\n\ntemplate < class value_type > /* a^n mod m */\nvalue_type powmod ( value_type a, const value_type & n, const value_type & m )\n{\n if ( a == 1 || ! n )\n return m != 1 ? 1 : 0;\n\n value_type res = 1;\n for ( value_type k = 1; k <= n; a = a * a % m, k = k << 1 )\n if ( k & n )\n res = ( res * a ) % m;\n\n return res;\n}\n\ntemplate < class T >\ninline T atan(const T & x, const T & y) { return std::atan( y / x ) + ( x < 0 ) * M_PI; }\n\n/*\n * Fermat pseudoprime test\n * www.math.umbc.edu/~campbell/Computers/Python/numbthy.py\n * NOTE: since return type is bool, and powmod may break for ints,\n * the argument is always casted to long long\n */\ninline bool is_pseudo_prime ( const long long & a )\n{\n /* all the primes less than 1000 ( 168 primes )*/\n const long long p [ ] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,\n 79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,\n 163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,\n 241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,\n 337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,\n 431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,\n 521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,\n 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,\n 719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,\n 823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,\n 929,937,941,947,953,967,971,977,983,991,997 };\n\n const size_t n = sizeof( p ) / sizeof ( p[ 0 ] );\n\n if ( a < p[ n - 1 ] + 1)\n return std::binary_search ( p, p + n , a );\n\n if ( std::find_if ( p, p + n, std::not1 ( std::bind1st ( std::modulus< long long >( ), a ))) != p + n )\n return false;\n\n const size_t k = a < 9006401LL ? 3 :\n a < 10403641LL ? 4 :\n a < 42702661LL ? 5 :\n a < 1112103541LL ? 6 : 7;\n\n for ( size_t j = 0; j < k; ++ j )\n if ( powmod ( p[ j ], a - 1, a ) != 1 )\n return false;\n\n return true;\n}\n\n\n/*\n * returns a sorted vector of all primes less than or equal to n\n * maximum adj diff of all primes less than 1e5 is 72 ( 114 for 1e6 )\n */\ntemplate < typename value_type >\nstd::vector < value_type > get_primes ( const value_type n )\n{\n#ifdef BUG\n ScopeTimer scope_timer ( \"std::vector < value_type > get_primes ( const value_type n )\" );\n#endif\n\n // typedef typename std::vector < value_type >::iterator iterator;\n std::vector < value_type > primes;\n\n for ( value_type k = 2 ; k <= n; ++ k )\n if ( is_pseudo_prime ( k ) )\n {\n // FIXME this is very stupid\n // const value_type sqrt_k = 1 + static_cast < value_type > ( sqrt ( k + 1 ) );\n // iterator iend = upper_bound ( primes.begin( ), primes.end( ), sqrt_k );\n\n // if ( find_if ( primes.begin( ), iend, std::not1 ( std::bind1st ( std::modulus< value_type >( ), k ) ) ) != iend )\n // continue;\n\n primes.push_back ( k );\n }\n\n return primes;\n}\n\ntemplate < class T >\ninline std::vector < std::pair < T, size_t > > get_prime_fact( T a )\n{\n std::vector< std::pair < T, size_t > >fac;\n\n for ( T k = 2; a > 1; ++ k )\n if ( ! ( a % k ) ) // no need to check if k is prime\n {\n size_t m = 0;\n for ( ; ! ( a % k ) ; ++m, a/= k )\n ;\n\n fac.push_back ( std::pair < T, size_t > ( k, m ) );\n }\n return fac;\n}\n\ntemplate < class T > /* prime factorization */\nstd::vector < std::pair < T, size_t > > get_prime_fac( T a )\n{\n std::vector < std::pair < T, size_t > > fac;\n\n size_t k = 0;\n while (!(a % 2)) { a /= 2; ++ k; }\n if ( k != 0 ) fac.push_back( std::make_pair(2, k));\n\n k = 0;\n while (!(a % 3)) { a /= 3; ++ k; }\n if ( k != 0 ) fac.push_back( std::make_pair(3, k));\n\n for ( T j = 5; j * j < a + 1 && a > 1 && j < a + 1; j += 4 )\n {\n size_t k = 0;\n while (!(a % j)) { a /= j; ++ k; }\n if ( k != 0 ) fac.push_back( std::make_pair(j, k));\n\n j += 2; k = 0;\n while (!(a % j)) { a /= j; ++ k; }\n if ( k != 0 ) fac.push_back( std::make_pair(j, k));\n }\n\n if ( a > 1 ) fac.push_back( std::make_pair(a, 1));\n return fac;\n}\n\ntemplate < class T > /* generates all divisors from vector of prime factorization */\nvoid gen_div( T a, std::vector < T > & divs,\n typename std::vector < std::pair < T, size_t > >::const_iterator iter,\n typename std::vector < std::pair < T, size_t > >::const_iterator last )\n{\n if ( iter != last )\n {\n auto next = iter + 1;\n const auto k = iter->second + 1;\n const auto prime = iter->first;\n for ( size_t j = 0; j < k; ++ j )\n {\n gen_div(a, divs, next, last);\n a *= prime;\n }\n }\n else\n divs.push_back( a );\n}\n\n\n\ntemplate < class T >\nT n_choose_k ( T n, T k )\n{\n if ( k > n )\n return 0;\n\n const T lb = std::min ( k, n - k ) + 1;\n const T ub = n - lb + 1;\n\n T res = 1, j = 2;\n while ( n > ub && j < lb)\n {\n res *= n--;\n while ( j < lb and ! (res % j) )\n res /= j++;\n }\n\n while ( n > ub )\n res *= n--;\n\n return res;\n}\n\n\n/**\n * median calculator, using two heaps\n */\ntemplate < class InputIter >\ninline std::pair < typename InputIter::value_type, typename InputIter::value_type >\nmedian ( InputIter first, InputIter last )\n{\n typedef typename InputIter::value_type value_type;\n typedef std::pair< value_type, value_type > result_type;\n\n /*\n * max_heap:\n * - the lower half of the elements\n * - the biggest of such elements is on the top\n */\n std::vector < value_type > max_heap, min_heap;\n\n /*\n * comp argument to heap algorithm should provide\n * 'strict weak ordering'; in particular\n * std::not2 ( std::less < value_type > )\n * does not have such a strict weak ordering;\n */\n std::less < value_type > max_heap_comp;\n std::greater < value_type > min_heap_comp;\n\n if ( first == last ) /* corner case: empty vector */\n throw std::runtime_error ( \"median of an empty vector is undefined!\" );\n\n InputIter iter = first;\n max_heap.push_back ( * iter );\n\n for ( ++iter ; iter != last; ++ iter )\n if ( * iter < max_heap.front() )\n {\n max_heap.push_back ( * iter );\n std::push_heap ( max_heap.begin(), max_heap.end(), max_heap_comp );\n\n if ( min_heap.size() + 1 < max_heap.size() )\n {\n /* max_heap has got too large */\n min_heap.push_back( max_heap.front() );\n std::push_heap( min_heap.begin(), min_heap.end(), min_heap_comp );\n\n std::pop_heap( max_heap.begin(), max_heap.end(), max_heap_comp );\n max_heap.pop_back();\n }\n }\n else\n {\n min_heap.push_back ( * iter );\n std::push_heap ( min_heap.begin(), min_heap.end(), min_heap_comp );\n\n if ( max_heap.size() + 1 < min_heap.size() )\n {\n /* min_heap has got too large */\n max_heap.push_back( min_heap.front() );\n std::push_heap( max_heap.begin(), max_heap.end(), max_heap_comp );\n\n std::pop_heap( min_heap.begin(), min_heap.end(), min_heap_comp );\n min_heap.pop_back();\n }\n }\n\n DEBUG( max_heap );\n DEBUG( min_heap );\n\n return min_heap.empty( ) /* corner case: ++first = last */\n ? result_type ( *first, *first )\n : result_type ( max_heap.size() < min_heap.size() ? min_heap.front() : max_heap.front(),\n min_heap.size() < max_heap.size() ? max_heap.front() : min_heap.front() );\n}\n\n/*\n * geometry util\n * ----------------------------------------------\n */\n\nstruct xyPoint\n{\n double x, y;\n xyPoint( const double & a = .0, const double & b = .0 ): x ( a ), y( b ) { };\n};\n\n\nstruct xyCircle\n{\n xyPoint center;\n double radius;\n};\n\nstd::ostream & operator<< ( std::ostream & out, const xyPoint & p )\n{\n out << '(' << p.x << \", \" << p.y << ')';\n return out;\n}\n\nstd::istream & operator>> ( std::istream & ist, xyPoint & p )\n{\n ist >> p.x >> p.y;\n return ist;\n}\n\nstd::ostream & operator<< ( std::ostream & out, const xyCircle & o )\n{\n out << \"{(\" << o.center.x << \", \" << o.center.y << \") \" << o.radius << '}';\n return out;\n}\n\nstd::istream & operator>> ( std::istream & ist, xyCircle & o )\n{\n ist >> o.center.x >> o.center.y >> o.radius;\n return ist;\n}\n\ninline double cartesian_dist ( const xyPoint & a, const xyPoint & b )\n{\n const double d = a.x - b.x;\n const double e = a.y - b.y;\n return std::sqrt ( d * d + e * e );\n}\n\nclass xyLine\n{\n public:\n xyLine ( const xyPoint & , const xyPoint & );\n xyLine ( const double slope, const double intercept );\n\n /*\n * 'signed' orthogonal distance; the sign is useful\n * to compare which side of the line the point is\n */\n inline double orth_dist ( const xyPoint & ) const;\n private:\n double m_slope;\n double m_intercept;\n double m_normfac; /* normalization factor for orth_dist calc */\n\n bool m_vertical; /* if the line is verticcal */\n double m_xcross; /* x axis cross point for vertical line */\n};\n\nxyLine::xyLine ( const xyPoint & a, const xyPoint & b )\n{\n if ( a.x == b.x ) /* vertical line */\n {\n m_vertical = true;\n m_xcross = a.x;\n\n m_intercept = DOUBLE_NAN;\n m_slope = DOUBLE_INF;\n m_normfac = DOUBLE_NAN;\n }\n else\n {\n m_vertical = false;\n m_xcross = DOUBLE_NAN;\n\n m_slope = ( b.y - a.y ) / ( b.x - a.x );\n m_intercept = a.y - m_slope * a.x;\n m_normfac = std::sqrt ( m_slope * m_slope + 1.0 );\n }\n}\n\nxyLine::xyLine ( const double slope, const double intercept ):\n m_slope ( slope ), m_intercept ( intercept )\n{\n m_vertical = false;\n m_xcross = DOUBLE_NAN;\n m_normfac = std::sqrt ( m_slope * m_slope + 1.0 );\n}\n\ndouble xyLine::orth_dist ( const xyPoint & o ) const /* 'signed' orthogonal distance */\n{\n if ( m_vertical )\n return o.x - m_xcross;\n else\n return ( m_slope * o.x - o.y + m_intercept ) / m_normfac;\n}\n\ninline double triangle_area ( const xyPoint & a, const xyPoint & b, const xyPoint & c )\n{\n const xyLine l ( a, b );\n const double h = std::abs ( l.orth_dist ( c ) );\n const double e = cartesian_dist ( a, b );\n return h * e;\n}\n\n/*\n * operator<< overrides\n * ----------------------------------------------\n */\n\nnamespace\n{\n /* helper function to output containers */\n template < typename T >\n std::ostream & __output ( std::ostream & out, const T & a )\n {\n typedef typename T::const_iterator const_iterator;\n out << \"{ \";\n\n // does not work for 'pair' value type\n // std::copy ( a.begin( ), a.end( ), std::ostream_iterator < typename T::value_type > ( std::cout, \", \" ));\n\n for ( const_iterator iter = a.begin(); iter != a.end(); ++ iter )\n out << ( iter != a.begin( ) ? \", \" : \"\" ) << *iter ;\n\n return out << \" }\";\n }\n}\n\ntemplate < typename key_type, typename value_type >\nstd::ostream & operator<< ( std::ostream & out, const std::pair < key_type, value_type > & p)\n{\n out << \"(\" << p.first << \", \" << p.second << \")\";\n return out;\n}\n\ntemplate < typename T0, typename T1, typename T2 >\nstd::ostream & operator<< ( std::ostream & out, const std::tuple < T0, T1, T2 > & t )\n{\n out << \"(\" << std::get<0>(t) << \", \" << std::get<1>(t) << \", \" << std::get<2>(t) << \")\";\n return out;\n}\n\n\ntemplate < typename key_type, typename value_type, typename comp >\nstd::ostream & operator<< ( std::ostream & out, const std::map < key_type, value_type, comp > & m )\n{\n return __output ( out, m );\n}\n\ntemplate < typename value_type, typename comp >\nstd::ostream & operator<< ( std::ostream & out, const std::set < value_type, comp > & s )\n{\n return __output ( out, s );\n}\n\ntemplate < typename value_type >\nstd::ostream & operator<< ( std::ostream & out, const std::vector < value_type > & a )\n{\n return __output ( out, a );\n}\n\ntemplate < typename value_type >\nstd::ostream & operator<< ( std::ostream & out, const std::list < value_type > & a )\n{\n return __output ( out, a );\n}\n\ntemplate < typename value_type >\nstd::ostream & operator<< ( std::ostream & out, const std::vector < std::vector < value_type > > & a )\n{\n typedef typename std::vector < std::vector < value_type > >::const_iterator const_iterator;\n\n for ( const_iterator iter = a.begin( ); iter != a.end( ); ++ iter )\n out << '\\n' << *iter ;\n\n return out;\n}\n\n/*\n * operator>> overrides\n * ----------------------------------------------\n */\n\ntemplate < typename key_type, typename value_type >\nstd::istream & operator>> ( std::istream & in, std::pair < key_type, value_type > & p)\n{\n in >> p.first >> p.second;\n return in;\n}\n\ntemplate < typename T0, typename T1, typename T2 >\nstd::istream & operator>> ( std::istream & fin, std::tuple < T0, T1, T2 > & t )\n{\n fin >> std::get<0>(t) >> std::get<1>(t) >> std::get<2>(t);\n return fin;\n}\n\ntemplate < typename value_type >\nstd::istream & operator>> ( std::istream & in, std::vector < value_type > & a )\n{\n typedef typename std::vector < value_type >::iterator iterator;\n\n if ( ! a.size( ) )\n {\n size_t n;\n in >> n;\n a.resize( n );\n }\n\n for ( iterator iter = a.begin(); iter != a.end(); ++ iter )\n in >> * iter;\n\n return in;\n}\n\n/*\n * readin quick utilities\n * ----------------------------------------------\n */\n\n// template < typename value_type >\n// inline void readin ( std::vector < value_type > & a, size_t n = 0, std::istream & in = std::cin )\n// {\n// // if ( ! n ) std::cin >> n;\n// if ( ! n ) in >> n ;\n// a.resize ( n );\n// // std::cin >> a;\n// in >> a;\n// }\n\n// XXX consider removing\n// template < typename key_type, typename value_type >\n// inline void readin (std::vector < std::pair < key_type , value_type > > & a, size_t n = 0 )\n// {\n// if ( !n ) std::cin >> n;\n// a.resize( n );\n// std::cin >> a;\n// }\n\n/*\n * pair utility\n * ----------------------------------------------\n */\n\n/*\n * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > );\n * stackoverflow.com/questions/18640152\n */\ntemplate < typename T1, typename T2 >\ninline std::pair < T1, T2 > operator+ ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b )\n{\n return std::make_pair < T1, T2 > ( a.first + b.first, a.second + b.second );\n}\n\ntemplate < typename T1, typename T2 >\ninline std::pair < T1, T2 > operator- ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b )\n{\n return std::make_pair < T1, T2 > ( a.first - b.first, a.second - b.second );\n}\n\n// template < class T1, class T2, class BinaryOperation >\n// class Apply2nd : std::binary_function < typename std::pair < T1, T2 >,\n// typename std::pair < T1, T2 >,\n// typename BinaryOperation::result_type >\n\nnamespace\n{\n /*!\n * helper template to do the work\n */\n template < size_t J, class T1, class T2 >\n struct Get;\n\n template < class T1, class T2 >\n struct Get < 0, T1, T2 >\n {\n typedef typename std::pair < T1, T2 >::first_type result_type;\n\n static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.first; }\n static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.first; }\n };\n\n template < class T1, class T2 >\n struct Get < 1, T1, T2 >\n {\n typedef typename std::pair < T1, T2 >::second_type result_type;\n\n static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.second; }\n static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.second; }\n };\n}\n\ntemplate < size_t J, class T1, class T2 >\ntypename Get< J, T1, T2 >::result_type & get ( std::pair< T1, T2 > & pr )\n{\n return Get < J, T1, T2 >::elm( pr );\n}\n\ntemplate < size_t J, class T1, class T2 >\nconst typename Get< J, T1, T2 >::result_type & get ( const std::pair< T1, T2 > & pr )\n{\n return Get < J, T1, T2 >::elm( pr );\n}\n\n/*\n * graph utils\n * ----------------------------------------------\n */\n\n/*\n * Dijkstra :: single-source shortest path problem for\n * a graph with non-negative edge path costs, producing\n * a shortest path tree\n * en.wikipedia.org/wiki/Dijkstra's_algorithm\n */\ntemplate < typename DistType >\nvoid Dijekstra ( const size_t & source,\n const std::vector < std::list < size_t > > & adj, // adjacency list\n const std::vector < std::vector < DistType > > & edge_len, // pair-wise distance for adjacent nodes\n std::vector < DistType > & dist, // distance from the source\n std::vector < size_t > prev ) // previous node in the shortest path tree\n{\n // TODO\n}\n\n// TODO http://en.wikipedia.org/wiki/Shortest_path_problem\n// TODO Graph class, Weighted graph, ...\n\n/*\n * maximum cardinality matching in a bipartite graph\n * G = G1 ∪ G2 ∪ {NIL}\n * where G1 and G2 are partition of graph and NIL is a special null vertex\n * https://en.wikipedia.org/wiki/Hopcroft-Karp_algorithm\n */\nclass HopcroftKarp\n{\n public:\n HopcroftKarp ( const std::vector < std::list < size_t > > & adj,\n const std::vector < bool > & tag );\n\n size_t get_npair ( ) { return npair; };\n std::map < size_t, size_t > get_map ( );\n\n private:\n bool mf_breadth_first_search ( ); // breadth first search from unpaired nodes in G1\n bool mf_depth_first_search ( const size_t v ); // dfs w/ toggeling augmenting paths\n\n const std::vector < std::list < size_t > > & m_adj; // adjacency list for each node\n const std::vector < bool > & m_tag; // binary tag distinguishing partitions\n\n size_t npair;\n const size_t NIL; // special null vertex\n const size_t INF; // practically infinity distance\n\n std::vector < size_t > m_g1; // set of nodes with tag = true\n std::vector < size_t > m_dist; // dist from unpaired vertices in G1\n std::vector < size_t > m_pair;\n};\n\n\nstd::map < size_t, size_t > HopcroftKarp::get_map ( )\n{\n std::map < size_t, size_t > m;\n\n for ( size_t j = 0; j < m_pair.size( ); ++ j )\n if ( m_pair[ j ] != NIL && m_tag[ j ])\n m[ j ] = m_pair[ j ];\n\n return m;\n}\n\n\nHopcroftKarp::HopcroftKarp ( const std::vector < std::list < size_t > > & adj,\n const std::vector < bool > & tag ):\n m_adj ( adj ),\n m_tag ( tag ),\n npair ( 0 ),\n NIL ( adj.size( )),\n INF ( adj.size( ) + 1 ),\n m_dist ( std::vector < size_t > ( adj.size( ) + 1, INF)),\n m_pair ( std::vector < size_t > ( adj.size( ), NIL )) // initially everything is paired with nil\n{\n assert ( m_adj.size() == m_tag.size() );\n\n for ( size_t j = 0; j < tag.size( ); ++ j )\n if ( tag[ j ] )\n m_g1.push_back ( j );\n\n while ( mf_breadth_first_search ( ) )\n for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++ v )\n if ( m_pair[ *v ] == NIL && mf_depth_first_search ( *v ) )\n ++ npair;\n}\n\nbool HopcroftKarp::mf_breadth_first_search( )\n{\n /* only nodes from g1 are queued */\n std::queue < size_t > bfs_queue;\n\n /* initialize queue with all unpaired nodes from g1 */\n for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++v )\n if ( m_pair[ *v ] == NIL )\n {\n m_dist[ *v ] = 0;\n bfs_queue.push ( *v );\n }\n else\n m_dist[ *v ] = INF;\n\n m_dist[ NIL ] = INF;\n\n /* find all the shortest augmenting paths to node nil */\n while ( ! bfs_queue.empty() )\n {\n const size_t v = bfs_queue.front( );\n bfs_queue.pop ( );\n\n if ( m_dist[ v ] < m_dist[ NIL ] )\n for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u )\n if ( m_dist[ m_pair[ * u ] ] == INF )\n {\n m_dist[ m_pair[ * u ] ] = m_dist[ v ] + 1;\n bfs_queue.push ( m_pair[ * u ] );\n }\n }\n\n return m_dist[ NIL ] != INF;\n}\n\n\nbool HopcroftKarp::mf_depth_first_search( const size_t v )\n{\n if ( v == NIL )\n return true;\n else\n {\n for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u )\n if ( m_dist[ m_pair[ *u ] ] == m_dist[ v ] + 1 && mf_depth_first_search( m_pair[ *u ] ))\n {\n /*\n * there is an augmenting path to nil from m_pair[ *u ]\n * and hence there is an augmenting path from v to u and\n * u to to nil; therefore v and u can be paired together\n */\n m_pair [ *u ] = v;\n m_pair [ v ] = *u;\n return true;\n }\n m_dist[ v ] = INF;\n return false;\n }\n}\n\n\n/**\n * lazy all pairs shortest path in a tree with only one BFS\n * test case: 'book of evil'\n * codeforces.com/problemset/problem/337/D\n */\nclass All_Pairs_Tree_Shortest_Path\n{\n public:\n All_Pairs_Tree_Shortest_Path( const std::vector< std::list < size_t > > & adj ):\n n( adj.size( ) ),\n depth( std::vector < size_t > ( n, All_Pairs_Tree_Shortest_Path::INF ) ),\n parent( std::vector < size_t > ( n ) ),\n dist( std::vector < std::vector < unsigned short > > ( n ) )\n {\n\n /* perform bfs from root node '0' and assign depth to each node */\n /* XXX probably would be worth to set the root as node with highest degree */\n std::queue< size_t > bfs_queue;\n bfs_queue.push( 0 );\n depth[ 0 ] = 0;\n parent[ 0 ] = 0;\n\n while ( ! bfs_queue.empty( ) )\n {\n const size_t u = bfs_queue.front( );\n bfs_queue.pop( );\n\n for ( std::list< size_t >::const_iterator j = adj[ u ].begin( ); j != adj[ u ].end( ); ++ j )\n if ( depth[ u ] + 1 < depth[ *j ] )\n {\n depth[ *j ] = depth[ u ] + 1;\n parent[ *j ] = u;\n bfs_queue.push( *j );\n }\n }\n\n /* adjust pair-wise distance to zero along the diagonal */\n for ( size_t j = 1; j < n; ++ j )\n dist[ j ].resize( j, All_Pairs_Tree_Shortest_Path::INF );\n }\n\n /* interface object function as to lazily look-up distances */\n size_t operator( )( const size_t u, const size_t v )\n {\n if ( u == v )\n return 0;\n else if ( u < v)\n return (*this)( v, u );\n else if ( dist[ u ][ v ] == All_Pairs_Tree_Shortest_Path::INF )\n {\n if ( depth[ u ] < depth[ v ] )\n /* u is in a lower level than v */\n dist[ u ][ v ] = 1 + (*this)( u, parent[ v ]);\n else if ( depth[ v ] < depth[ u ] )\n /* v is in a lower level than u */\n dist[ u ][ v ] = 1 + (*this)( parent[ u ], v );\n else\n /* u and v are at the same depth */\n dist[ u ][ v ] = 2 + (*this)( parent[ u ], parent[ v ] );\n }\n return dist[ u ][ v ];\n }\n\n /* TODO populate; a method which populates pair-wise distances\n * and returns the matrix */\n\n private:\n /*\n * constant infinity value for initializing distances\n * even though this is private it will be assigned outside of the class\n */\n static const unsigned short INF;\n\n const size_t n; /* numbert of nodes in the tree */\n std::vector < size_t > depth; /* distance to root node '0' */\n std::vector < size_t > parent; /* parent of each node with root node '0' */\n std::vector < std::vector < unsigned short > > dist; /* pair-wise shortest path distance */\n};\n\nconst unsigned short All_Pairs_Tree_Shortest_Path::INF = std::numeric_limits< unsigned short >::max( );\n\n/*\n * data-structure utility\n * ----------------------------------------------\n */\n\ntemplate < class T, class Comp = std::less< T > >\nclass Heap /* less< T > --> max-heap */\n{\n typedef T value_type;\n typedef typename std::vector < value_type >::size_type size_type;\n public:\n /*\n * stackoverflow.com/questions/10387751\n * possible work-around: a memebr pointer to m_val\n * TODO static/friend heapify ( val, & heap ) XXX O( n ) ?!\n * TODO implement insert iterator\n */\n Heap(): m_val( std::vector < value_type >() ), m_comp( Comp() ) {}\n\n template < class InputIter >\n Heap ( InputIter first, InputIter last ):\n m_val ( std::vector < value_type > ( ) ), m_comp( Comp( ) )\n {\n for ( ; first != last ; ++ first )\n m_val.push_back ( * first );\n\n std::make_heap( m_val.begin( ), m_val.end( ), m_comp );\n }\n\n /*!\n * to avoid destroying heap property, front( )\n * should always return a 'const' reference\n */\n inline const value_type & front( ) const { return m_val.front( ); }\n inline bool empty( ) const { return m_val.empty( ); }\n inline size_type size( ) const { return m_val.size( ); }\n\n inline void push ( const value_type & a )\n {\n m_val.push_back( a );\n std::push_heap( m_val.begin( ), m_val.end( ), m_comp );\n }\n inline void pop( )\n {\n std::pop_heap ( m_val.begin( ), m_val.end( ), m_comp );\n m_val.pop_back( );\n }\n\n // inline void swap( Heap< T, Comp> & other ) { m_val.swap( other.m_val ) };\n // void sort( ) { std::sort_heap ( m_val.begin( ), m_val.end( ), m_comp ); }\n // template < class X, class Y >\n // friend std::ostream & operator<<( std::ostream & out, const Heap < X, Y> & heap );\n private:\n std::vector < value_type > m_val;\n const Comp m_comp;\n};\n\n/*\n * boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm\n */\n#if 1 < 0\ntemplate < class Type >\nclass shared_ptr\n{\n typedef Type value_type;\n public:\n explicit shared_ptr ( value_type * p = NULL ) : ptr ( p ), count ( new size_t ( 1U ) ) { }\n shared_ptr ( const shared_ptr < value_type > & sp ): ptr ( sp.ptr ), count ( sp.count ) { ++ * count; }\n ~ shared_ptr ( ) { release( ); }\n\n bool operator== ( const shared_ptr < value_type > & sp ) { return ptr == sp.ptr; }\n bool operator!= ( const shared_ptr < value_type > & sp ) { return ptr != sp.ptr; }\n\n shared_ptr < value_type > & operator= ( const shared_ptr < value_type > & sp )\n {\n if ( this != & sp && ptr != sp.ptr )\n {\n release( );\n ptr = sp.ptr;\n count = sp.count;\n ++ * count;\n }\n return * this;\n }\n\n value_type * operator-> ( ) { return ptr ; }\n value_type & operator* ( ) { return *ptr ; }\n const value_type * operator-> ( ) const { return ptr ; }\n const value_type & operator* ( ) const { return *ptr; }\n\n void swap ( shared_ptr < value_type > & sp )\n {\n if ( this != &sp && ptr != sp.ptr )\n {\n std::swap ( ptr, sp.ptr );\n std::swap ( count, sp.count );\n }\n }\n\n private:\n void release ( )\n {\n /* stackoverflow.com/questions/615355 */\n -- * count;\n if ( ! * count )\n {\n delete count;\n delete ptr;\n count = NULL;\n ptr = NULL;\n }\n }\n value_type * ptr;\n size_t * count;\n};\n#endif\n\n/*!\n * union find data structure with\n * - lazy unions\n * - union by rank\n * - path compression\n */\nclass UnionFind\n{\n public:\n UnionFind( const size_t n ):\n parent ( std::vector < size_t > ( n ) ), /* initialize each node as its own */\n rank ( std::vector < size_t > ( n, 0 )) /* parent and set all the ranks to 0 */\n {\n for ( size_t j = 0; j < n; ++ j )\n parent[ j ] = j ;\n }\n\n inline size_t find( const size_t s )\n {\n /*\n * perform path compresion and add shortcut\n * if parent[ s ] is not a root node\n */\n const size_t p = parent[ s ];\n return parent[ p ] == p ? p : parent[ s ] = find( p ) ;\n }\n\n inline void lazy_union ( size_t i, size_t j )\n {\n /* unions should be done on root nodes */\n i = find( i );\n j = find( j );\n\n if ( i != j )\n {\n if ( rank [ i ] < rank[ j ] )\n parent[ i ] = j;\n else\n {\n parent[ j ] = i;\n rank[ i ] += rank[ i ] == rank[ j ];\n }\n }\n }\n private:\n std::vector < size_t > parent;\n std::vector < size_t > rank;\n};\n\n\n// TODO XXX\n// template < class NumType >\n// unsigned num_hash_func ( const NumType & a )\n// {\n// // XXX what will happen in case of overflow?\n// return static_cast < unsigned > ( a % 9973 ) % 9973 ;\n// }\n\n/*\n * XXX: HashMap: map< Key, T > data [ 9973 ]\n * data [ num_hash_func ( key ) ][ key ]\n */\n\n\n/*\n * testing util\n * ----------------------------------------------\n */\n\n// TODO add a preprocessor which automatically includes the funciton name, or __line__\n// and disables if not in debug mode\n/* prints the life length of the object when it goes out of scope */\nclass ScopeTimer\n{\n public:\n ScopeTimer ( const std::string & msg = \"\" ): tic ( clock ( )), m_msg( msg ) { };\n\n ~ ScopeTimer ( )\n {\n const clock_t toc = clock();\n const uint64 dt = 1000L * ( toc - tic ) / CLOCKS_PER_SEC;\n\n const uint64 mil = dt % 1000L;\n const uint64 sec = ( dt / 1000L ) % 60L;\n const uint64 min = ( dt / 60000L ) % 60L;\n const uint64 hrs = ( dt / 3600000L );\n\n std::cout << '\\n' << m_msg << \"\\n\\telapsed time: \";\n\n if ( hrs ) std::cout << hrs << \" hrs, \";\n if ( min ) std::cout << min << \" min, \";\n if ( sec ) std::cout << sec << \" sec, \";\n std::cout << mil << \" mil-sec\\n\";\n }\n\n private:\n typedef unsigned long long int uint64;\n const clock_t tic;\n const std::string m_msg;\n};\n\nclass RandInt\n{\n public:\n RandInt ( int a = 0, int b = 100 ): m ( a ), f ( static_cast < double > ( b - a ) / RAND_MAX ) { }\n inline int operator() ( ) { return m + std::ceil ( f * rand( ) ); }\n private:\n const int m;\n const double f;\n};\n\n\nclass RandDouble\n{\n public:\n RandDouble ( double a = 0.0, double b = 1.0 ): m ( a ), f ( ( b - a ) / RAND_MAX ) { }\n inline double operator() ( ) { return m + f * rand( ); }\n private:\n const double m, f;\n};\n\nclass Noisy\n{\n public:\n Noisy ( std::string str ): msg ( str )\n {\n std::cout << \" Noisy ( \" << msg << \" )\\t@ \" << this << std::endl;\n }\n ~Noisy ( )\n {\n std::cout << \"~Noisy ( \" << msg << \" )\\t@ \" << this << std::endl;\n }\n void beep ( )\n {\n std::cout << \" beep ( \" << msg << \" )\\t@ \" << this << std::endl;\n }\n void beep ( ) const\n {\n std::cout << \" const beep ( \" << msg << \" )\\t@ \" << this << std::endl;\n }\n private:\n const std::string msg;\n};\n\nDECLARE ( Noisy );\n\n\n/*\n * ----------------------------------------------\n * ----------------------------------------------\n */\n\n/*\n * -- @@@ -------------------------------------------------\n */\n\n\nusing namespace std;\n\n// struct Comp\n// {\n// // bool operator() ( const pair < int, int > & x, const pair < int, int > & y )\n// // {\n// // return abs( x.first ) + abs( x.second ) < abs( y.first ) + abs( y.second );\n// // }\n//\n// // bool operator( ) ( const xyCircle & a, const xyCircle & b )\n// // {\n// // return a.radius < b.radius;\n// // }\n// //\n// bool operator( ) ( const pair < size_t, size_t > & pr, const size_t & x )\n// {\n// return pr.second < x;\n// }\n// };\n\ninline int get_gain( const vector < int > & xs, const size_t l, const size_t r )\n{\n const size_t len = xs.size() - l - r;\n return len & 1 ? xs[ l + len / 2 ] : 0;\n}\n\nvoid c388()\n{\n size_t n;\n cin >> n;\n vector < vector < int > > xs( n );\n cin >> xs;\n\n const size_t k = accumulate( begin(xs), end(xs), 0,\n []( const size_t acc, const vector < int > & row )\n {\n return acc + row.size();\n });\n DEBUG( k );\n\n const int acc = accumulate( begin(xs), end(xs), 0,\n []( const int acc, const vector < int > & a )\n {\n return accumulate(begin(a), end(a), acc);\n });\n\n DEBUG( acc );\n // how much has it been taken from left & right\n vector < size_t > left( n, 0 ), right( n, 0 );\n\n int left_acc = 0;\n\n for ( size_t j = 0; j < k; ++ j )\n {\n size_t imax = 0;\n while ( xs[imax].size() == left[imax] + right[imax] )\n ++ imax;\n\n int val = get_gain( xs[imax], left[imax], right[imax] );\n\n for ( size_t i = imax + 1; i < n; ++ i )\n if ( left[ i ] + right[ i ] < xs[ i ].size() )\n {\n const auto g = get_gain( xs[i], left[i], right[i] );\n if ( val < g )\n {\n val = g;\n imax = i;\n }\n }\n\n if ( j & 1 ) // from right\n ++ right[ imax ];\n else\n left_acc += xs[ imax ][ left[ imax ] ++ ];\n }\n\n cout << left_acc << ' ' << acc - left_acc;\n}\n\nvoid c20()\n{\n size_t n, m;\n cin >> n >> m;\n\n typedef tuple< size_t, int64 > edge_type;\n\n // const auto make_edge = tuple< size_t , int64 >;\n\n vector < vector < edge_type > > adj( n );\n for ( size_t j = 0; j < m; ++ j )\n {\n size_t u, v;\n int64 w;\n cin >> u >> v >> w;\n -- u; -- v;\n\n adj[ u ].push_back( edge_type( v, w ));\n adj[ v ].push_back( edge_type( u, w ));\n }\n\n const int64 INF = numeric_limits< int64 >::max();\n vector < int64 > dist( n, INF );\n\n typedef tuple< int64, size_t > value_type;\n Heap < value_type, greater< value_type > > heap;\n\n dist[ 0 ] = 0;\n heap.push( make_tuple< int64, size_t >( 0, 0 ) );\n\n while ( ! heap.empty() )\n {\n const size_t u = get< 1 >( heap.front() );\n const int64 d = get< 0 >( heap.front() );\n heap.pop();\n\n if ( dist[ u ] == d )\n for ( const auto & e : adj[ u ] )\n if ( d + get< 1 >( e ) < dist[ get< 0 >( e ) ] )\n {\n dist[ get< 0 >( e ) ] = d + get< 1 >( e );\n heap.push( make_tuple( dist[ get< 0 >( e ) ], get< 0 >( e ) ));\n }\n }\n\n if ( dist[ n - 1 ] != INF )\n {\n vector < size_t > path;\n size_t u = n - 1;\n\n const auto pred = [&dist, &u](const edge_type & e){ return dist[get<0>(e)] + get<1>(e) == dist[u]; };\n\n do\n {\n path.push_back( u );\n const auto iter = find_if(begin(adj[u]), end(adj[u]), pred);\n u = get<0>(*iter);\n } while ( u != 0 );\n\n path.push_back( 0 );\n\n transform( path.rbegin(), path.rend(),\n ostream_iterator< size_t >( cout, \" \" ),\n bind( plus< size_t >(), placeholders::_1, 1 ));\n }\n else\n cout << -1;\n\n}\n\nsize_t b319()\n{\n vector < size_t > xs;\n cin >> xs;\n const size_t n = xs.size();\n\n map < size_t, size_t > m; // value | time\n m[ xs[0] ] = 0;\n size_t val = 0;\n\n for ( size_t j = 1; j < n ; ++ j )\n {\n auto iter = m.lower_bound( xs[ j ] );\n if ( iter != end( m ) )\n {\n ++ iter->second;\n auto next = iter;\n ++ next;\n if ( next != end( m ) && iter->second == next->second )\n m.erase(begin(m), next);\n else\n m.erase(begin(m), iter);\n }\n else\n {\n val = max(val, m.rbegin()->second);\n m.clear();\n }\n m[ xs[ j ] ] = 0;\n DEBUG( m );\n }\n\n val = max(val, m.rbegin()->second);\n\n return val;\n}\n\nsize_t b41()\n{\n size_t n, b;\n cin >> n >> b;\n\n vector < size_t > a( n );\n cin >> a;\n\n vector < size_t > m( n );\n const auto f = []( const size_t & a, const size_t & b) { return max(a, b); };\n partial_sum( a.rbegin(), a.rend(), m.rbegin(), f );\n DEBUG( m );\n\n size_t val = b;\n for ( size_t j = 0; j < n ; ++ j )\n val = max(val, b % a[ j ] + b / a[ j ] * m[ j ] );\n\n return val;\n}\n\nstring b99()\n{\n vector< size_t > xs;\n cin >> xs;\n\n const auto i = min_element(begin(xs), end(xs));\n const auto j = max_element(begin(xs), end(xs));\n\n if ( *i == *j )\n return \"Exemplary pages.\";\n else if ( (*i + *j) & 1 )\n return \"Unrecoverable configuration.\";\n else\n {\n const auto v = ( *j - *i ) / 2;\n *i += v;\n *j -= v;\n\n if( *min_element(begin(xs), end(xs)) == *max_element(begin(xs), end(xs)) )\n return val2str(v) + \" ml. from cup #\"\n + val2str(i - begin(xs) + 1 )\n + \" to cup #\"\n + val2str(j - begin(xs) + 1 )\n + \".\";\n else\n return \"Unrecoverable configuration.\";\n }\n}\n\nvoid b70()\n{\n size_t n;\n cin >> n;\n\n string str;\n getline(cin, str);\n getline(cin, str);\n DEBUG( str );\n\n vector < size_t > xs;\n auto i = begin(str);\n\n while ( i != end(str) )\n {\n while ( i != end(str ) && *i == ' ' )\n ++ i;\n\n auto j = i;\n while ( j != end(str) && *j != '.' && *j != '?' && *j != '!' )\n ++ j;\n\n if ( j != end(str) )\n ++ j;\n\n if ( j != i )\n xs.push_back( j - i );\n\n i = j;\n }\n\n DEBUG( xs );\n const auto pred = bind(less(), n, placeholders::_1);\n if ( find_if(begin(xs), end(xs), pred) != end(xs) )\n cout << \"Impossible\";\n else\n {\n size_t k = 0;\n auto i = begin(xs);\n\n while ( i != end(xs) )\n {\n ++ k;\n size_t len = *i++;\n\n while ( i != end(xs) && len + *i < n )\n len += 1 + *i++;\n\n DEBUG( len );\n }\n cout << k;\n }\n}\n\nsize_t count_unique( const vector < string > & xs, const size_t k )\n{\n set < char > s;\n for ( const auto & a: xs )\n s.insert( a[ k ] );\n\n return s.size();\n}\n\ninline size_t count_bars( size_t a )\n{\n size_t k = 0;\n for ( ; a > 0; a >>= 1 )\n k += a & 1;\n return k;\n}\n\nsize_t a442()\n{\n vector < string > cards;\n cin >> cards;\n\n const size_t cnt_unq_char = count_unique(cards, 0);\n const size_t cnt_unq_digit = count_unique(cards, 1);\n\n DEBUG( cnt_unq_char );\n DEBUG( cnt_unq_digit );\n\n map < char, size_t > char_map;\n char_map['R'] = 0;\n char_map['G'] = 1;\n char_map['B'] = 2;\n char_map['Y'] = 3;\n char_map['W'] = 4;\n\n typedef tuple< size_t, size_t > card_type;\n vector < card_type > xs;\n for ( const auto & a: cards )\n xs.push_back( card_type( 1 << char_map[a[0]], 1 << (a[1] - '0' )));\n\n // if knowing char, identifies digit\n\n // DEBUG( card_type );\n size_t val = cnt_unq_char + cnt_unq_digit - 2;\n //numeric_limits< size_t >::max();\n\n for( size_t i = 0; i < ( 1 << 6 ) - 1; ++ i )\n {\n const auto icnt = count_bars( i );\n if ( icnt < val )\n for ( size_t j = 0; j < ( 1 << 6 ) - 1; ++ j )\n {\n const auto jcnt = count_bars( j );\n if ( icnt + jcnt < val )\n {\n bool flag = true;\n\n for ( const auto & a: xs )\n {\n for ( const auto & b: xs )\n if ( a != b )\n {\n const auto u = get<0>(a) == get<0>(b)\n || icnt + 1 < cnt_unq_char && ! ( get<0>( a ) & i ) && ! ( get<0>( b ) & i );\n\n const auto v = get<1>(a) == get<1>(b)\n || jcnt + 1 < cnt_unq_digit && ! ( get<1>( a ) & j ) && ! ( get<1>( b ) & j );\n\n if ( v && u )\n {\n flag = false;\n break;\n }\n }\n }\n if ( flag )\n val = icnt + jcnt;\n }\n }\n }\n\n return val;\n}\n\nint main( const int argc, char * argv [])\n{\n cout << a442();\n // cout << setprecision( 15 ) << b68();\n\n return EXIT_SUCCESS;\n}\n\n/**\n * mislav.uniqpath.com/2011/12/vim-revisited/\n * set encoding=utf-8\n * %s/\\(.\\{60,70\\}\\) /\\1\\r/gc\n * %s/ / /gc\n * %s/10\\([0-9]\\{1,2\\}\\)/10^\\1/gc\n */\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "bitmasks"], "code_uid": "1a59d70de0ab68f2bf4149732bcfb819", "src_uid": "3b12863997b377b47bae43566ec1a63b", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\n\ntypedef long long ll;\n\n\nint main()\n{\n ios_base :: sync_with_stdio(false);\n cin.tie();\n cout.tie();\n\n ll b;\n cin >> b;\n\n if (b == 1){\n cout << 1;\n return 0;\n }\n\n ll answer = 0;\n\n for (ll i = 1; i <= sqrt(b); ++i){\n if (b % i == 0){\n answer+=2;\n }\n if (i * i == b){\n answer-=1;\n }\n }\n cout << answer;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7435f7c45c1d11ec8bf6b4a27d8290c9", "src_uid": "7fc9e7d7e25ab97d8ebc10ed8ae38fd1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\n int a,b,c,d,i;\n cin>>a>>b;\n c=a;\nif(a<=1000&&b<=1000){\n for(i=0;i<=1000;i++){\n if(a>=b){\n d=a/b;\n c+=d;\n a=d+a%b;\n }\n else\n break;\n }\ncout<\nusing namespace std;\nint main()\n{\n int i,j,n,x[1005],y[1005],c,sum,f=0;\n while(cin>>n)\n {\n f=0;\n for(i=0;i<7;i++)\n {\n cin>>x[i];\n }\n sum=n,c=1,i=0;\n while(n>0)\n {\n i%=7;\n sum-=x[i];\n if(sum<=0)\n {\n c=i+1;\n break;\n }\n i++;\n\n }\n\n\n cout<\nint main()\n{\n\tint a,b,k,i;\n\tscanf(\"%d%d%d\",&k,&a,&b);\n\tif(a\n#include \n\nusing namespace std;\n\nint main(){\n\tint n;\n\tscanf(\"%d\", &n);\n\tif(n%2==0)\n\t{\n\t\tprintf(\"2\");\t\n\t}\n\telse\n\t{\n\t\tprintf(\"1\");\n\t}\n printf(\"\\n\");\n}\n// 1500319658578\n", "lang_cluster": "C++", "tags": ["math", "games"], "code_uid": "44aed9b509b68c2645d693f06f0f289f", "src_uid": "816ec4cd9736f3113333ef05405b8e81", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n long long x,y;\n cin >> x >> y;\n if (x == y)\n\t\tcout << \"=\";\n else\n\tif (x == 1)\n\t\tcout << \"<\";\n else\n\tif (y == 1)\n\t\tcout << \">\";\n else\n\tif (x + y == 6)\n\t\tcout << \"=\";\n else\n\tif (x == 3)\n\t\tcout << \">\";\n else\n\tif (y == 3)\n\t\tcout << \"<\";\n else\n\tif (x < y)\n\t\tcout << \">\";\n\telse\n\t\tcout << \"<\";\n}\n\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "c15201adee72f530eb92ac7876469375", "src_uid": "ec1e44ff41941f0e6436831b5ae543c6", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\n#define xx first\n#define yy second\n#define pb push_back\n#define mp make_pair\n#define LL long long\n#define inf INT_MAX/3\n#define mod 1000000007ll\n#define PI acos(-1.0)\n#define linf (1ll<<60)-1\n#define FOR(I,A,B) for(int I = (A); I < (B); ++I)\n#define REP(I,N) FOR(I,0,N)\n#define ALL(A) ((A).begin(), (A).end())\n#define set0(ar) memset(ar,0,sizeof ar)\n#define vsort(v) sort(v.begin(),v.end())\n#define setinf(ar) memset(ar,126,sizeof ar)\n\n//cout << fixed << setprecision(20) << p << endl;\n\ntemplate inline T bigmod(T p,T e,T M){\n LL ret = 1;\n for(; e > 0; e >>= 1){\n if(e & 1) ret = (ret * p) % M;\n p = (p * p) % M;\n } return (T)ret;\n}\ntemplate inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}\ntemplate inline T modinverse(T a,T M){return bigmod(a,M-2,M);}\n\nvector < int > vc[1000001];\nLL ar[1000001], mob[1000001], isp[1000001], val[1000001];\nLL tmp[1000001], pre[1000001];\n\nLL fact[1000001], inv[1000001];\nLL ncr(int n, int r){\n if(n < r) return 0;\n LL ret = (fact[n] * inv[r]) % mod;\n return (ret * inv[n-r]) % mod;\n}\n\nint main() {\n ios_base::sync_with_stdio(0); cin.tie(0);\n for(int i = 1; i <= 1000000; i++){\n for(int j = i; j <= 1000000; j += i) vc[j].pb(i);\n }\n\n FOR(i, 1, 1000001) mob[i] = 1;\n for(int i = 2; i <= 1000000; i++){\n if(isp[i] == 0){\n for(int j = i; j <= 1000000; j += i){\n mob[j] *= -1;\n if(i <= 1000 && (j % (i*i)) == 0) mob[j] = 0;\n isp[j] = 1;\n }\n }\n }\n\n FOR(i, 1, 1000001) mob[i] = (mob[i] + mod) % mod;\n\n for(int i = 1; i <= 1000000; i++){\n for(int j = 0; j < vc[i].size(); j++){\n pre[i] = (pre[i] + mob[i/vc[i][j]] * (LL)vc[i][j]) % mod;\n }\n }\n\n fact[0] = 1;\n for(LL i = 1; i <= 1000000; i++) fact[i] = (fact[i - 1] * i) % mod;\n REP(i, 1000001) inv[i] = modinverse(fact[i], mod);\n\n int n, K, q; cin >> n >> K >> q;\n for(int i = 1; i <= n; i++) {\n int x; cin >> x;\n tmp[x]++;\n }\n\n LL sum = 0;\n for(int i = 1000000; i >= 1; i--){\n for(int j = i; j <= 1000000; j += i) val[i] += tmp[j];\n for(int j = i; j <= 1000000; j += i){\n ar[i] = (ar[i] + ncr(val[j], K) * mob[j/i]) % mod;\n }\n sum = (sum + ar[i] * (LL)i) % mod;\n }\n\n while(q--){\n int v; cin >> v;\n for(int i = vc[v].size() - 1; i >= 0; i--){\n int x = vc[v][i];\n LL p = (ncr(val[x]+1, K) + mod - ncr(val[x], K)) % mod;\n sum = (sum + p * pre[x]) % mod;\n val[x]++;\n }\n\n cout << sum << endl;\n }\n}\n", "lang_cluster": "C++", "tags": ["combinatorics", "number theory"], "code_uid": "fe7c5a50e59edf27c3837372f6a899bb", "src_uid": "edf8b780f4e3c610fd6a5ba041a7799c", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//https://codeforces.com/contest/1286/problem/A\n//A. Garland\n#include \nusing namespace std;\n#define oo 0x3f3f3f3f\n#define ooLL 0x3f3f3f3f3f3f3f3f\n#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL)\nconst int mod = 1e9+7;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair ii;\n \nint main(){\n int n;\n scanf(\"%d\", &n);\n vector v(n+1);\n for(int i = 1; i <= n ; ++i) scanf(\"%d\", &v[i]);\n vector>> dp(n+1, vector>(n/2+1, vector(2,oo)));\n dp[0][0][0] = dp[0][0][1] = 0;\n for(int i = 1; i <= n ; ++i){\n for(int j = 0; j <= n/2 ; ++j){\n if(v[i] == 0 || v[i] % 2) dp[i][j][1] = min(dp[i-1][j][1], 1+dp[i-1][j][0]);\n if(v[i] % 2 == 0 && j) dp[i][j][0] = min(dp[i-1][j-1][0], 1+dp[i-1][j-1][1]);\n }\n }\n printf(\"%d\\n\", min(dp[n][n/2][0], dp[n][n/2][1]));\n return 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "dp", "greedy"], "code_uid": "32a8579c00d3637bf23c28cd148134cc", "src_uid": "90db6b6548512acfc3da162144169dba", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 Diagnostics", "source_code": "#include \nusing namespace std;\nint min1(int a,int b,int c)\n{if(a>x1>>x2>>x3;\nd1=abs(x2-x1) + abs(x3-x1);\nd2=abs(x2-x1) + abs(x3-x2);\nd3=abs(x3-x1) + abs(x3-x2);\ncout<\n\nusing namespace std;\n\nint n;\nint mx = 0, sum = 0;\n\nint main(){\n\tscanf(\"%d\", &n);\n\tfor(int i = 1; i <= n; ++i){\n\t\tint a;\tscanf(\"%d\", &a);\n\t\tmx = max(mx, a);\n\t\tsum += a;\n\t}\n\t\n\tsum *= 2;\n\tsum += n;\n\tsum /= n;\n\t\n\tprintf(\"%d\", max(sum, mx));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "3a62e9d47b7c6652ef3ca1143a162b4b", "src_uid": "d215b3541d6d728ad01b166aae64faa2", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\n#define pb push_back\n#define sf scanf\n#define pf printf\n#define sz 100005\n#define MOD 1000000007\n#define optimize ios::sync_with_stdio(0);cin.tie(0);\n#define inf 0x3f3f3f3f\n#define READ freopen(\"in.txt\", \"r\", stdin);\n#define WRITE freopen(\"out.txt\", \"w\", stdout);\n\nusing namespace std;\n\ntypedef long long int ll;\ntypedef vector vi;\n\nint dx[] = {0, 0, +1, -1};\nint dy[] = {+1,-1, 0, 0};\n/*============GRAPH MOVES==================*/\n//const int fx[] = {1, -1, 0, 0};\n//const int fy[] = {0, 0, 1, -1};\nconst int fx[] = {1, -1, -1, 1, -1, 1}; // Kings move\nconst int fy[] = {0, 0, 1, 1, -1, -1}; // Kings move\n//const int fx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; // Knights move\n//const int fy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; // Knights move\n/*=========================================*/\n\ninline bool isLeapYear(ll year) { return (year%400==0) | (year%4==0 && year%100!=0); }\ninline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }\ninline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }\ninline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }\ninline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }\ninline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }\ninline ll modInverse(ll a) { return modPow(a, MOD-2); }\ninline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }\ninline ll mod(ll a, ll m){ if(a<0) return m-(abs(a)%m); else if(ay;} ///x>y\ninline bool Lesser(double x, double y) { return x+1e-9> tc;\n \n while(tc--)\n {\n \tint n, k;\n\n \tcin >> n >> k;\n\n \tcout << modPow(n, k) << '\\n';\n }\n\n\n return 0;\n}\n\n\n\n\n// https://vjudge.net/contest/480401#problem/I\n\t \t\t \t \t \t\t\t\t\t\t\t\t \t \t\t \t\t\t \t", "lang_cluster": "C++", "tags": ["math", "combinatorics", "bitmasks"], "code_uid": "6412b51c8b5b05779679ea34707259d9", "src_uid": "2e7a9f3a97938e4a7e036520d812b97a", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#define LL long long\nusing namespace std;\nLL n,m;\nstruct node{\n\tLL x,y;\n}R[15],B[15];\nstruct line{\n\tnode p1,p2;\n}a[15];\nLL multi(node p1,node p2,node p0)\n{\n LL x1=p1.x-p0.x,x2=p2.x-p0.x;\n LL y1=p1.y-p0.y,y2=p2.y-p0.y;\n return x1*y2-x2*y1;\n}\nbool check(line l1,line l2)\n{\n if(multi(l2.p1,l1.p2,l1.p1)*multi(l1.p2,l2.p2,l1.p1)>0&&multi(l1.p1,l2.p2,l2.p1)*multi(l2.p2,l1.p2,l2.p1)>0) return true;\n if(multi(l1.p1,l2.p1,l2.p2)==0&&min(l2.p1.x,l2.p2.x)<=l1.p1.x&&l1.p1.x<=max(l2.p1.x,l2.p2.x)) return true;\n if(multi(l1.p2,l2.p1,l2.p2)==0&&min(l2.p1.x,l2.p2.x)<=l1.p2.x&&l1.p2.x<=max(l2.p1.x,l2.p2.x)) return true;\n if(multi(l2.p1,l1.p1,l1.p2)==0&&min(l1.p1.x,l1.p2.x)<=l2.p1.x&&l2.p1.x<=max(l1.p1.x,l1.p2.x)) return true;\n if(multi(l2.p2,l1.p1,l1.p2)==0&&min(l1.p1.x,l1.p2.x)<=l2.p2.x&&l2.p2.x<=max(l1.p1.x,l1.p2.x)) return true;\n return false;\n}\nLL c[15];\nbool solve(LL k)\n{\n\tfor(LL i=1;in) {printf(\"Yes\");exit(0);}\n\tfor(LL i=1;i<=m;i++)\n\t{\n\t\tif(c[i]) continue;\n\t\ta[x].p1=R[x];a[x].p2=B[i];\n\t\tif(!solve(x)) continue;\n\t\tc[i]=1;dfs(x+1);c[i]=0;\n\t}\n}\nint main()\n{\n\tscanf(\"%lld %lld\",&n,&m);\n\tif(n!=m) return puts(\"No\"),0;\n\tfor(LL i=1;i<=n;i++) scanf(\"%lld %lld\",&R[i].x,&R[i].y);\n\tfor(LL i=1;i<=m;i++) scanf(\"%lld %lld\",&B[i].x,&B[i].y);\n\tmemset(c,0,sizeof(c));\n\tdfs(1);\n\tprintf(\"No\\n\");\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "geometry", "brute force"], "code_uid": "cceb9264520c8ed81159bd661c5f97c6", "src_uid": "65f81f621c228c09915adcb05256c634", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint n, m, s;\nint a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\nint main()\n{\n cin >> n >> m;\n m += a[n] - 1;\n while (m > 0)\n {\n m -= 7;\n s++;\n }\n cout << s;\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "5cd256bd57b3a04628538d30badaace2", "src_uid": "5b969b6f564df6f71e23d4adfb2ded74", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nchar a[101];\nint n;\nint main()\n{\n\tcin>>a;\n\tfor(int i=0;i<=strlen(a);i++)\n\t\tif(a[i]=='H'||a[i]=='Q'||a[i]=='9'){\n\t\t\tcout<<\"YES\";\n\t\t\treturn 0;\n\t\t}\n\tcout<<\"NO\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9511b32bfa8a9b69fe7421a442d43e18", "src_uid": "1baf894c1c7d5aeea01129c7900d3c12", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint main()\n{\n\tint n,c=0; \n\tstring s;\n\tcin >> n >> s;\n\tfor (int i = 0; i < s.size() - 1; i++)\n\t\tif ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i+1] == 'U'))\n\t\t{\n\t\t\tc++;\n\t\t\ti++;\n\t\t}\n\t\t\n\tcout << s.size() - c;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "c8e5876513297d31d9089eb45739edb4", "src_uid": "986ae418ce82435badadb0bd5588f45b", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nint main() {\n int n, k;\n scanf(\"%d %d\", &n, &k);\n printf(\"%d\", 3 * n - k > 0 ? 3 * n - k : 0);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "5eba22720668d5baf317810d49ac91fd", "src_uid": "5a5e46042c3f18529a03cb5c868df7e8", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define ll long long\nint x,k,p;\ndouble dp[210][2100];\nvoid init()\n{\n memset(dp,0,sizeof(dp));\n for(int i=0 ; i<=k ; i++)\n {\n int T=x+i;\n while(T)\n {\n if(T&1) break;\n dp[0][i]++;\n T/=2;\n }\n }\n}\nint main()\n{\n\n while(~scanf(\"%d%d%d\",&x,&k,&p))\n {\n double winp=p*1.0/100;\n double sp=1-winp;\n init();\n\n for(int i=0 ; i<=k ; i++)\n {\n for(int j=2000 ; j>=0 ; j--)\n {\n dp[i+1][j]+=dp[i][j+1]*sp;\n if(2*j<=k)\n dp[i+1][j*2]+=(dp[i][j]+1)*winp;\n\n }\n }\n\n printf(\"%.7lf\\n\",dp[k][0]);\n }\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "bitmasks"], "code_uid": "ab8007200977b46ad90a0461b287e7d3", "src_uid": "c9274249c26b1a85c19ab70d91c1c3e0", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//Optimise\n#include \nusing namespace std;\n\n// #define multitest 1\n#ifdef Debug\n#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__);\ntemplate \nvoid ZZ(const char *name, Arg1 &&arg1)\n{\n\tstd::cerr << name << \" = \" << arg1 << endl;\n}\ntemplate \nvoid ZZ(const char *names, Arg1 &&arg1, Args &&... args)\n{\n\tconst char *comma = strchr(names + 1, ',');\n\tstd::cerr.write(names, comma - names) << \" = \" << arg1;\n\tZZ(comma, args...);\n}\n#else\n#define db(...)\n#endif\n\nusing ll = long long;\n#define f first\n#define s second\n#define pb push_back\nconst long long mod = 1000000007;\nauto TimeStart = chrono::steady_clock::now();\n\nconst int nax = 1e6 + 10;\nint n, dp[nax][20][2];\n\nint power(int base, int index)\n{\n\tif (index == 0)\n\t\treturn 1;\n\tll temp = power(base, index / 2);\n\ttemp *= temp;\n\tif (index & 1)\n\t\treturn temp * base;\n\treturn temp;\n}\nint f(int x, int y)\n{\n\treturn n / (1 << x) / (y == 0 ? 1 : 3);\n}\n\nint mul(int a, int b)\n{\n\treturn (ll)a * b % mod;\n}\n\nvoid add_self(int &a, int b)\n{\n\ta += b;\n\tif (a >= mod)\n\t\ta -= mod;\n}\n\nvoid solve()\n{\n\tcin >> n;\n\tint x1 = log2l(n);\n\tdb(log2l(n));\n\tdp[1][x1][0] = 1;\n\tif ((1 << (x1 - 1)) * 3 <= n)\n\t\tdp[1][x1 - 1][1] = 1;\n\tfor (int i = 1; i < n; ++i)\n\t{\n\t\tfor (int x = 0; x <= x1; ++x)\n\t\t\tfor (int y = 0; y <= 1; ++y)\n\t\t\t{\n\t\t\t\tadd_self(dp[i + 1][x][y], mul(dp[i][x][y], f(x, y) - i));\n\t\t\t\tif (x > 0)\n\t\t\t\t\tadd_self(dp[i + 1][x - 1][y], mul(dp[i][x][y], f(x - 1, y) - f(x, y)));\n\t\t\t\tif (y > 0)\n\t\t\t\t\tadd_self(dp[i + 1][x][y - 1], mul(dp[i][x][y], f(x, y - 1) - f(x, y)));\n\t\t\t}\n\t}\n\tcout << dp[n][0][0] << '\\n';\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tint t = 1;\n#ifdef multitest\n\tcin >> t;\n#endif\n\twhile (t--)\n\t\tsolve();\n#ifdef TIME\n\tcerr << \"\\n\\nTime elapsed: \" << chrono::duration(chrono::steady_clock::now() - TimeStart).count() << \" seconds.\\n\";\n#endif\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "number theory"], "code_uid": "2d7171508b5b35cdef720af535ac2db7", "src_uid": "b2d59b1279d891dba9372a52364bced2", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define LL long long\n#define PB push_back\n#define PF push_front\n#define MK make_pair\n#define MT make_tuple\n#define REP(i, a, b) for (int i = a; i <= b; i++)\n#define MOD 1000000007\n#define DEB(x) cout << #x << \"=\" << x << endl\n#define DEB2(x, y) cout << #x << \"=\" << x << \",\" << #y << \"=\" << y << endl\n\nint gcd(int a, int b) {\n while (b) {\n a %= b;\n swap(a, b); \n }\n return a;\n}\nint gcd1(int a, int b, int &x, int &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n int x1, y1; \n int d = gcd1(b, a % b, x1, y1);\n x = y1; \n y = x1 - y1 * (a / b); \n return d;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n int T = 1;\n while (T--) {\n int a, b, c, g, x, y;\n cin >> a >> b >> c;\n g = gcd1(a, b, x, y); // a*x+by = 1\n if (c % g == 0) {\n a /= g;\n b /= g;\n c /= g;\n if (c >= a * b - a - b + 1)\n cout << \"YES\\n\";\n else {\n x = x * c;\n y = y * c;\n while (x >= b) {\n x -= b;\n y += a;\n }\n while (x < 0) {\n x += b;\n y -= a;\n }\n if (y >= 0)\n cout << \"YES\\n\";\n else\n cout << \"NO\\n\";\n } \n } else {\n cout << \"NO\\n\";\n } \n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "number theory"], "code_uid": "315839c9b16e6f6bf7e67949def8e14f", "src_uid": "e66ecb0021a34042885442b336f3d911", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)< vi;\ntypedef pair pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint n, a[23], dp[1 << 23], p[23][23];\n\nint main(){\n\tcin >> n;\n\trep(i, n) cin >> a[i];\n\trep(i, 1 << n) dp[i] = inf;\n\tdp[1] = 1;\n\t\n\tmemset(p, -1, sizeof(p));\n\trep(i, n) rep(j, n) rep(k, n) if(a[k] == a[i] + a[j]) p[k][i] = j;\n\t\n\tfor(int i = 1; i < n; i++) rep(j, 1 << i) if((j & 1 << i - 1) && dp[j] < inf){\n\t\tbool ok = 0;\n\t\trep(k, i) if(j & 1 << k){\n\t\t\tint t = p[i][k];\n\t\t\tif(t >= 0 && (j & 1 << t)){\n\t\t\t\tok = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!ok) continue;\n\t\tdp[j ^ 1 << i] = min(dp[j ^ 1 << i], dp[j] + 1);\n\t\trep(k, i) if(j & 1 << k) dp[j ^ 1 << i ^ 1 << k] = min(dp[j ^ 1 << i ^ 1 << k], dp[j]);\n\t}\n\tint ans = inf;\n\trep(i, 1 << n) if(i & 1 << (n - 1)) ans = min(ans, dp[i]);\n\tcout << (ans == inf ? -1 : ans) << endl;\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "bitmasks"], "code_uid": "59f4d04513f35b76665d4117cc48b6fa", "src_uid": "359f5d1264ce16c5c5293fd59db95628", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*****\n sometime small optimiztion or small proof leads great work and save us from writing comlex code \n***/\n\n#include\n#include \n#include \nusing namespace __gnu_pbds;\nusing namespace std;\n#define Khela ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n#define ll long long int\n#define llu long long unsigned int\n#define pf printf\n#define sf scanf\n#define f first\n#define s second\n#define pb push_back\n#define mk make_pair\n#define pii pair\n#define PII pair\n \nint dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};\nint dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};\nint dx4[] = {0, 0, 1, -1};\nint dy4[] = {1, -1, 0, 0};\n \ntypedef tree,null_type,less>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;\nint Set(int N,int pos){return N=N | (1< ne,de,test;\n while(cin>>x>>y>>l>>r)\n {\n ne.clear(),de.clear();\n ne.push_back(1ll),de.push_back(1ll);\n ll temp=x;\n ne.push_back(x),de.push_back(y);\n while(temp<=(MAX/x)) {\n temp*=x;\n ne.push_back(temp);\n }\n temp=y;\n while(temp<=(MAX/y)){\n temp*=y;\n de.push_back(temp);\n }\n \n test.clear();\n for(auto xx: de) {\n for(auto yy: ne){\n ll p=(xx+yy);\n if(l<=p && p<=r) test.pb(p);\n }\n }\n ll mx=0;\n sort(test.begin(),test.end());\n ll siz=test.size();\n if(siz>0) mx=test[0]-l;\n\n for(i=1;i0) mx=max(mx,r-test[siz-1]);\n if(test.empty()) mx=max(mx,r-l+1);\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ll long long\nusing namespace std;\n\nint main()\n{\n\tint n, l, x;\n\tcin >> n >> l;\n\tsetA, B, C;\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> x, A.insert(x);\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> x, B.insert(x);\n\tC = B;\n\tfor (int i = 1; i <= l; i++)\n\t{\n\t\tif (A == C)\n\t\t{\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t}\n\t\telse\n\t\t\tC.clear();\n\t\tfor (auto it = B.begin(); it != B.end(); it++)\n\t\t\tC.insert((*it + i) % l);\n\t}\n\tcout << \"NO\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "d22125bdc4753c3d6a52b4212692697c", "src_uid": "3d931684ca11fe6141c6461e85d91d63", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nstring str;\nchar res[20];\n\nint main()\n{\n\tcin >> str;\n\tint count = 0;\n\tres[count++] = str[str.length() - 1];\n\tchar max = res[0];\n\tfor (int i = str.length() - 2; i >= 0; i--) {\n\t\tif (str[i] >= max){\n\t\t\tres[count++] = str[i];\n max = str[i];\n }\n\t}\n\n\tchar c = res[count - 1];\n\tfor (int i = count - 1; i >= 0; i--) {\n\t\tif (res[i] == c)\n\t\t\tcout << res[i];\n\t\telse\n\t\t\tbreak;\n\t}\n\tcout << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "binary search", "bitmasks", "greedy", "strings", "implementation"], "code_uid": "b90961c1c239fc279ed20451a33a6f8e", "src_uid": "9a40e9b122962a1f83b74ddee6246a40", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n\nusing namespace std;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define FORD(i,a,b) for(int i=(a);i>(b);--i)\n#define REP(i,n) FOR(i,0,n)\n#define FORE(i,a,b) for(int i=(a);i<=(b);++i)\n#define MOD 1000000007LL\n\nint main()\n{\n long long n, d, ans = 1;\n cin >> n >> d;\n FORE(i,2,d) ans *= d,ans%=MOD;\n FORE(i,d+1,n) ans *= (n-d),ans%=MOD;\n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "brute force"], "code_uid": "72a25caa1fe83b1387fff166065ae427", "src_uid": "cc838bc14408f14f984a349fea9e9694", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n * author: gantheory\n * created: 2018-06-17 00:53:38\n */\n#include \n\nusing namespace std;\n\npair a[4], b[4];\nset> st;\nint dxs[] = {1, 1, -1, -1};\nint dys[] = {1, -1, -1, 1};\nint lx = INT_MAX, rx = INT_MIN, ly = INT_MAX, ry = INT_MIN;\n\nvoid build() {\n for (int i = 0; i < 4; ++i) {\n int x = a[i].first, y = a[i].second;\n lx = min(lx, x), rx = max(rx, x);\n ly = min(ly, y), ry = max(ry, y);\n }\n for (int i = lx; i < rx; ++i)\n for (int j = ly; j < ry; ++j) {\n st.insert({i, j});\n }\n}\nbool go(int idx, int aa, int bb) {\n int dx = dxs[idx], dy = dys[idx];\n int ax = b[aa].first, ay = b[aa].second;\n int bx = b[bb].first, by = b[bb].second;\n while (ax != bx and ay != by) {\n if (st.count({ax, ay})) return true;\n ax += dx, ay += dy;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(0), cin.tie(0), cout.setf(ios::fixed), cout.precision(9);\n for (int i = 0; i < 4; ++i) cin >> a[i].first >> a[i].second;\n for (int i = 0; i < 4; ++i) cin >> b[i].first >> b[i].second;\n\n build();\n sort(b, b + 4);\n int sx = b[1].first;\n int my = (b[1].second + b[2].second) / 2;\n for (int j = b[1].second; j < my; ++j) {\n int h = j - b[1].second;\n for (int i = sx - h - 1; i <= sx + h; ++i) {\n if (st.count({i, j})) {\n cout << \"YES\" << '\\n';\n return 0;\n }\n }\n }\n for (int j = my; j < b[2].second; ++j) {\n int h = b[2].second - j;\n for (int i = sx - h; i < sx + h; ++i) {\n if (st.count({i, j})) {\n cout << \"YES\" << '\\n';\n return 0;\n }\n }\n }\n\n for (int i = lx; i <= rx; ++i)\n for (int j = ly; j <= ry; ++j) {\n st.insert({i, j});\n }\n if (go(0, 0, 2) or go(1, 2, 3) or go(2, 3, 1) or go(3, 1, 0)) {\n cout << \"YES\" << '\\n';\n } else {\n cout << \"NO\" << '\\n';\n }\n}\n", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "543768998c674dc100803828555f9b34", "src_uid": "f6a3dd8b3bab58ff66055c61ddfdf06a", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n \nusing namespace std;\n \ntypedef long long ll;\n \nmt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());\n \nll ans = 1e18 + 7;\n \nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n ll a, b;\n cin >> a >> b;\n set > da, db;\n for (ll i = 1; i * i <= a; i++) {\n if (a % i) continue;\n da.insert({i, a / i});\n da.insert({a / i, i});\n }\n for (ll i = 1; i * i <= b; i++) {\n if (b % i) continue;\n db.insert({i, b / i});\n db.insert({b / i, i});\n }\n ll n = a + b;\n set kek, orz;\n for (ll i = 1; i * i <= n; i++) {\n if (n % i) continue;\n {\n ll x = i, y = n / i;\n auto it = da.upper_bound({x, -1});\n if (it != da.begin()) --it;\n if (it->first <= x && it->second <= y) {\n ans = min(ans, 2 * x + 2 * y);\n }\n it = db.upper_bound({x, -1});\n if (it != db.begin()) --it;\n if (it->first <= x && it->second <= y) {\n ans = min(ans, 2 * x + 2 * y);\n }\n }\n {\n ll x = n / i, y = i;\n auto it = da.upper_bound({x, -1});\n if (it != da.begin()) --it;\n if (it->first <= x && it->second <= y) {\n ans = min(ans, 2 * x + 2 * y);\n }\n it = db.upper_bound({x, -1});\n if (it != db.begin()) --it;\n if (it->first <= x && it->second <= y) {\n ans = min(ans, 2 * x + 2 * y);\n }\n }\n }\n cout << ans << '\\n';\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "binary search", "number theory"], "code_uid": "1c3b2caf2e5c87de80e5d563e1227e1f", "src_uid": "7d0c5f77bca792b6ab4fd4088fe18ff1", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nlong long n,m,k;\nlong long n1,m1,k1;\nlong long ans=1,ans1=1;\nint main()\n{\n cin>>n>>m>>k;\n n1=n;\n m1=m;\n k1=k;\n if(k>n+m-2)\n {\n cout<<-1;\n return 0;\n }\n if(k>n-1)\n {\n ans=1;\n k=k-n+1;\n }\n else\n {\n ans=n/(k+1);\n ans=(n/(k+1))*m;\n goto ss;\n }\n ans*=(m/(k+1));\nss:\n ;\n if(k1>m1-1)\n {\n ans1=1;\n k1=k1-m1+1;\n }\n else\n {\n ans1=m1/(k1+1);\n ans1=(m1/(k1+1))*n1;\n goto nn;\n }\n ans1*=(n1/(k1+1));\nnn:\n ;\n cout<\nusing namespace std; \nint main ()\n{\n int n,m,k; char s; \n cin>>n>>m>>k;\n if(k%2==0) s='R';\n else s='L';\n k--;\n int r=(k/(2*m))+1;\n int d=(k%(2*m)/2)+1;\n cout<\ntypedef long long ll;\nint main()\n{\n ll n,m;\n scanf(\"%lld%lld\",&n,&m);\n ll minn,maxn;\n \n if(n-m*2<=0) minn=0;\n else minn=n-m*2;\n \n ll i=0;\n while(m>0)\n {\n m-=i;i++;\n }\n maxn=n-i;\n \n printf(\"%lld %lld\\n\",minn,maxn);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "graphs"], "code_uid": "0b1120f14190fac0ea6be4b2c8b62a65", "src_uid": "daf0dd781bf403f7c1bb668925caa64d", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/*\n * new\n *\n * Created on: Dec 9, 2015\n * Author: AbdullaAshraf\n */\n#include\nusing namespace std;\n\nint main() {\n\tint k,a,b,v,cnt=0;\n\tcin >> k >> a >> b >> v;\n\twhile(b > 0){\n\t\tcnt++;\n\t\ta-= v*min(k,b+1);\n\t\tb -= k-1;\n\t\tif (a<=0){\n\t\t\tcout << cnt << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << (a+v-1)/v+cnt << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "a50cbbbd7f0d9836ae54f931c2c924ea", "src_uid": "7cff20b1c63a694baca69bdf4bdb2652", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include \n#include \nusing namespace std;\nint main()\n{\n\tint c = 0;\n\tstring a, b;\n\tcin >> a;\n\tfor (int i = 0; i < 5; i++)\n\t{\n\t\tcin >> b;\n\t\tif (a[1] == b[1] || a[0] == b[0])\n\t\t{\n\t\t\tc++;\n\t\t}\n\t}\n\tif (c >= 1)\n\t{\n\t\tcout << \"YES\";\n\t\treturn 0;\n\t}\n\tcout << \"NO\";\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "748bcbf5f812ddf3a3b2045823b89df2", "src_uid": "699444eb6366ad12bc77e7ac2602d74b", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\n\nint can[600002];\nint n, d;\nset v;\nint c[51];\nint main(){\n\n cin >> n >> d;\n for(int i = 0; i < n; i++){\n cin >> c[i];\n }\n memset(can, 0, sizeof can);\n int mx = 0;\n sort(c, c + n);\n can[0] = 1;\n for(int i = 0; i < n; i++){\n for(int j = mx; j >= 0; j--){\n if(can[j]){\n can[j + c[i]] = 1;\n mx = max(mx, j + c[i]);\n }\n }\n }\n for(int i = 1; i <= mx; i++)\n if(can[i]){\n v.insert(i);\n }\n int ans = 0, day = 0;\n while(true){\n int k = ans + d;\n set::iterator it = v.upper_bound(k);\n if(it == v.begin())\n break;\n it--;\n k = *it;\n if(k > ans)\n ans = k;\n else\n break;\n day++;\n }\n cout << ans << \" \";\n cout << day << endl;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "dp"], "code_uid": "9a7c91713f19a7fc998b87698c107ef8", "src_uid": "65699ddec8d0db2f58b83a0f64de6f6f", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//\n// main.cpp\n// A\n//\n// Created by nicomazz on 24/06/15.\n// Copyright (c) 2015 nicomazz. All rights reserved.\n//\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define TEST\n#define MAXN 100010\n\ntypedef pair pi;\ntypedef pair pii;\ntypedef vector vi;\ntypedef unsigned long long ull;\ntypedef long long ll;\n\n#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))\n\n#define iOS ios_base::sync_with_stdio(false)\n#define error(x) cerr << #x << \" = \" << (x) < inline void smax(T &x,T y){ x = max((x), (y));}\ntemplate inline void smin(T &x,T y){ x = min((x), (y));}\n\n#define gc getchar\ninline void scanInt(int &x)\n{\n register char c=gc();\n x = 0;\n bool neg = false;\n while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}\n for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}\n if (neg) x*=-1;\n}\n\nint lati[6];\nll pot(int b, int e)\n{\n if ( b == 1) return 1;\n if ( e == 0) return 1;\n if ( e == 1) return b;\n if ( e % 2)\n {\n ll ris = pot(b, e/2);\n return ris*ris+ris;\n }\n \n ll ris = pot(b,e/2);\n return ris*ris;\n}\nint main(int argc, const char * argv[])\n{\n#ifndef TEST\n freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);\n#endif\n\n int a,b,c,d,e,f;\n cin>>a>>b>>c>>d>>e>>f;\n cout<\n#include\n\nusing namespace std;\n\nlong long n, m, j, k, p, t, b[1111], x, nsz, msz, prevp, prevt;\n\nlong long proc(int l, int r)\n{\n long long res=0;\n for (int i=l;i> n >> m;\n for (j=0;j<7;j++)\n b[j]=j;\n x=max((long long)1,n-1);\n while (x)\n {\n nsz++;\n x/=7;\n }\n x=max((long long)1,m-1);\n while (x)\n {\n msz++;\n x/=7;\n }\n if (nsz+msz>7)\n {\n cout << 0;\n return 0;\n }\n do\n {\n p=proc(0,nsz);\n t=proc(nsz,nsz+msz);\n if (p\n#include\nusing namespace std;\nint n,a[105]={-1},cnt=1,ans=1;\nint main()\n{\n cin>>n;\n for(int i=1;i<=n;i++)cin>>a[i];\n sort(a+1,a+n+1);\n for(int i=1;i<=n;i++)\n {\n if(a[i-1]==a[i])cnt++;\n else cnt=1;\n ans=max(ans,cnt);\n }\n cout<\n\nusing namespace std;\nchar nc()\n{\n\tstatic char buf[1048576], *p1 = buf, *p2 = buf;\n\tif(p1 == p2){\n\t\tp1 = buf;\n\t\tp2 = buf + fread(buf, 1, 1048576, stdin);\n\t}\n\treturn *p1++;\n}\nint scan()\n{\n\tint sig = 1, sum = 0;\n\tchar f = nc();\n\twhile((f < '0' || f > '9')&&f != '-')f = nc();\n\tif(f == '-')\n\t{\n\t\tsig = -1;\n\t\tf = nc();\n\t}\n\twhile(f >='0' && f <= '9'){\n\t\tsum = sum * 10 + f - '0';\n\t\tf = nc(); \n\t}\n\tsum *= sig;\n\treturn sum;\n}\n\nint n,k,x,a[200],b[200],f,cnt,ans,now,t1,t2,fa,ba; \nint s(int x,int m){\n\tint cc=0;\n\tfor(int i=x;a[i]==m&&i<=n;++i) ++cc;\n\treturn cc;\n}\nint bi(int x,int m){\n\tint cc=0;\n\tfor(int i=x;a[i]==m&&i>=1;--i) ++cc;\n\treturn cc;\n}\nint main(){\n\tn=scan();k=scan();x=scan();\n\tfor(int i=1;i<=n;++i) {\n\t\ta[i]=scan();\n\t\tif(f==0&&a[i]==x){\n\t\t\t++cnt;\n\t\t\tb[cnt]=i;\n\t\t\tf=1;\n\t\t}\n\t\telse if(a[i]!=x) f=0;\n\t}\n\tfor(int i=1;i<=cnt;++i){\n\t\tnow=s(b[i],x);\n\t\tif(now<2) continue;\n\t\tt1=s(b[i]+now,a[b[i]+now]);\n\t\tfa=now+t1;\n\t\tt2=bi(b[i]-1,a[b[i]-1]);\n\t\tba=t2;\n\t\tif(a[b[i]+now]==a[b[i]-1])\n\t\twhile(t1+t2>=3){\n\t\t\tnow+=t1+t2;\n\t\t\tt1=s(b[i]+fa,a[b[i]+fa]);\n\t\t\tt2=bi(b[i]-ba-1,a[b[i]-ba-1]);\n\t\t\tif(a[b[i]+fa]!=a[b[i]-ba-1]) break;\n\t\t\tfa+=t1;\n\t\t\tba+=t2;\n\t\t}\n\t\tans=max(ans,now);\n\t}\n\tprintf(\"%d\",ans);\n\treturn 0;\n}\n\t\t\t\t\t \t\t \t\t\t \t \t\t \t \t\t", "lang_cluster": "C++", "tags": ["two pointers", "brute force"], "code_uid": "2e2f750f5ae488fc74707e47beaf3d46", "src_uid": "d73d9610e3800817a3109314b1e6f88c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n int HH = 0 , MM = 0;\n int tMM = 0;\n while (scanf(\"%d:%d\",&HH,&MM) != EOF )\n {\n tMM = HH % 10 * 10;\n tMM = tMM + HH / 10;\n if( HH == 5 && MM >= 50)\n {\n printf(\"10:01\\n\");\n continue;\n }\n if( HH == 15 && MM >= 51)\n {\n printf(\"20:02\\n\");\n continue;\n }\n if( HH == 23 && MM > 32 )\n {\n printf(\"00:00\\n\");\n continue;\n }\n if( HH >= 6 && HH <= 9)\n {\n printf(\"10:01\\n\");\n continue;\n }\n if( HH >= 16 && HH <= 19 )\n {\n printf(\"20:02\\n\");\n continue;\n }\n if( MM >= tMM)\n {\n if(HH+1 >= 10)\n {\n if(( (HH+1)%10*10 + HH/10) > 10)\n {\n printf(\"%d:%d\\n\",HH+1,(HH+1)% 10 * 10 + HH/10);\n }\n else\n {\n printf(\"%d:0%d\\n\",HH+1,(HH+1)% 10 * 10 + HH/10);\n }\n }\n else\n {\n if(( (HH+1)%10*10 + HH/10) >= 10)\n {\n printf(\"0%d:%d\\n\",HH+1,(HH+1)% 10 * 10 + HH/10);\n }\n else\n {\n printf(\"0%d:0%d\\n\",HH+1,(HH+1)% 10 * 10 + HH/10);\n }\n }\n\n }\n else\n {\n if( HH < 10)\n {\n if( tMM < 10 )\n {\n printf(\"0%d:0%d\\n\",HH,tMM);\n }\n else\n {\n printf(\"0%d:%d\\n\",HH,tMM);\n }\n }\n else\n {\n if( tMM < 10)\n {\n printf(\"%d:0%d\\n\",HH,tMM);\n }\n else\n {\n printf(\"%d:%d\\n\",HH,tMM);\n }\n }\n }\n tMM = 0;\n }\n return 0;\n}\n\n \t\t\t\t \t \t \t\t\t\t \t\t \t \t\t", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "a38b55dcf2c6748d9b686e1722a321c6", "src_uid": "158eae916daa3e0162d4eac0426fa87f", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#define ll long long int\n#define ld double\n#define fi first\n#define se second\n#define pb push_back\n#define pob pop_back\n#define W while\n#define fn(i,x) for(ll i=0;i=0;i--)\n#define fit(it,s) for(auto it=s.begin();it!=s.end();it++)\n#define mp make_pair\n#define pii pair\n#define pll pair\n#define vii vector\n#define vll vector\n#define mii map\n#define mll map\n#define srt(vec) sort(vec.begin(),vec.end())\n#define MAX 100005\n#define M 32\n#define endl \"\\n\"\n#include \n#include \n#define ordered_set tree, rb_tree_tag, tree_order_statistics_node_update>\nusing namespace std;\nusing namespace __gnu_pbds;\nll mod =1000000007;\nll pow1(ll a,ll b){if(b==0) return 1; ll p=pow1(a,b/2);if(b%2) return ((p*p))*a;return (p*p);}\nll pow2(ll a,ll b){if(b==0) return 1; ll p=pow2(a,b/2);if(b%2) return (((p*p)%mod)*a)%mod;return (p*p)%mod;}\nll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}\nint find1(int i,int dsu[]){if(dsu[i]==i) return i; dsu[i]=dsu[dsu[i]]; return find1(dsu[i],dsu);}\nvoid union1(int i,int j,int dsu[]) {dsu[j]=find1(i,dsu);}\nll inv(ll a){ return pow2(a,mod-2);}\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\nll bin(ll start,ll end,ll val)\n{\n while(starta) return 0; return ((fact[a]*inv(fact[a-b]))%mod*inv(fact[b]))%mod;}\nll tree1[1300005][4];\nvoid build(int start,int end,int node,ll a[])\n{\n if(start==end)\n {\n\n return;\n }\n int mid=(start+end)/2;\n build(start,mid,2*node,a);\n build(mid+1,end,2*node+1,a);\n}\nvoid update(int start,int end,int node,int r,int val)\n{\n if(start==end)\n {\n return;\n }\n int mid=(start+end)/2;\n if(r>mid)\n update(mid+1,end,2*node+1,r,val);\n else\n update(start,mid,2*node,r,val);\n}\n#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);\nint main()\n{\n IOS\n int t=1;\n //cin>>t;\n fn(z,t)\n {\n int n;\n cin>>n;\n ll a[3],b[3];\n fn(i,3)\n cin>>a[i];\n fn(i,3)\n cin>>b[i];\n ll ans2=0;\n fn(i,3)\n ans2+=min(a[i],b[(i+1)%3]);\n ll ans1=INT_MAX;\n int r=3;\n ll a1[3]={a[0],a[1],a[2]},b1[3]={b[0],b[1],b[2]};\n while(r--)\n {\n ll temp=min(b[0],a[1]);\n b[0]-=temp;\n a[1]-=temp;\n temp=min(b[0],a[0]);\n b[0]-=temp;\n a[0]-=temp;\n temp=min(b[1],a[1]);\n b[1]-=temp;\n a[1]-=temp;\n temp=min(b[1],a[2]);\n b[1]-=temp;\n a[2]-=temp;\n b[2]-=min(b[2],a[0]+a[2]);\n ans1=min(ans1,b[0]+b[1]+b[2]);\n fn(i,3)\n {\n a[i]=a1[(i+1)%3];\n b[i]=b1[(i+1)%3];\n }\n fn(i,3)\n {\n a1[i]=a[i];\n b1[i]=b[i];\n }\n }\n cout<\n#define\tMIN(a,b,c)\tmin(min(a,b),c)\n#define\tMAX(a,b,c)\tmax(max(a,b),c)\n#define ll long long\n#define itr vector::iterator\n#define pb push_back\n#define mp make_pair\n#define pii pair\n#define vi vector\n#define all(a) (a).begin(),(a).end()\n#define F first\n#define S second\n#define sz(x) (int)x.size()\n#define hell 1000000007\n#define endl '\\n'\n#define rep(i,a,b)\tfor(int i=a;i>t;\n\twhile(t--)\n\t{\n\t\tmap,int> m;\n\t\tint n,k1,k2,ct=1;\n\t\tcin>>n;\n\t\tcin>>k1;\n\t\tvi a(k1);\n\t\trep(i,0,k1)cin>>a[i];\n\t\tcin>>k2;\n\t\tvi b(k2);\n\t\trep(i,0,k2)cin>>b[i];\n\t\twhile(!a.empty()&&!b.empty())\n\t\t{\n\t\t\tif(m[mp(a,b)]==1)\n\t\t\t{\n\t\t\t\tcout<<-1;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\telse\n\t\t\tm[mp(a,b)]=1;\n\t\t\tif(a[0]>b[0])\n\t\t\t{\n\t\t\t\tct++;\n\t\t\t\ta.pb(b[0]);\n\t\t\t\ta.pb(a[0]);\n\t\t\t\ta.erase(a.begin());\n\t\t\t\tb.erase(b.begin());\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(a[0]\n\nusing namespace std;\n\nint main()\n{\n long long n,k;\n cin>>n>>k;\n long long index=n/2;\n long long a;\n if(n%2==0)\n {\n if(k<=index)\n cout<<(k*2)-1;\n else\n {\n k=k-index;\n cout<\nint main()\n{\n int n,a[101];\n while(scanf(\"%d\",&n)!=EOF)\n {\n int i,max=0,j,flag=0;\n for(i=0;imax)\n {\n max=a[i];\n j=i;\n }\n }\n\n for(i=0;i=a[i+1])\n {\n flag=1;\n break;\n }\n }\n for(i=j;i=a[i])\n {\n flag=1;\n break;\n }\n }\n if(flag==1)\n printf(\"NO\\n\");\n else\n printf(\"YES\\n\");\n }\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "c35515ad1b9dce176949095e6a64f5a9", "src_uid": "5482ed8ad02ac32d28c3888299bf3658", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef unsigned long long ull;\ntypedef long long ll;\ntypedef size_t sz;\n\ntypedef pair pii;\ntypedef pair pll;\n\n#define cal(c) (c).cbegin(), (c).cend()\n#define al(c) (c).begin(), (c).end()\n\n/////////////////////////////////////////////////////////////////////\n\nlong long getp(long long time, int k)\n{\n\ttime %= 2 * k;\n\tif (time <= k)\n\t\treturn time;\n\ttime -= k;\n\treturn k - time;\n}\n\nint main()\n{\n\tull t;\n\tcin >> t;\n\n\tfor (; t > 0; --t)\n\t{\n\t\tint n, k, l;\n\t\tcin >> n >> k >> l;\n\n\t\tvector d(n);\n\t\tfor (int i = 0; i < n; ++i)\n\t\t\tcin >> d[i];\n\n\t\tvector> v;\n\t\tv.emplace_back(0, 2 * k - 1);\n\n\t\tfor (int i = 0; i < n && !v.empty(); ++i)\n\t\t{\n\t\t\tvector> v2;\n\n\t\t\tfor (auto [b, e] : v)\n\t\t\t{\n\t\t\t\t++b;\n\t\t\t\t++e;\n\n\t\t\t\tfor (long long j = b; j <= e;)\n\t\t\t\t{\t\t\t\t\t\n\t\t\t\t\tif (d[i] + getp(j, k) > l)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto level = d[i] + getp(j, k);\n\t\t\t\t\t\tauto time = j % (2 * k);\n\t\t\t\t\t\tif (time < k)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tlevel += k - time;\n\t\t\t\t\t\t\ttime = k;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tj += level - l;\n\t\t\t\t\t\tif (j > e)\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tauto curb = j;\n\t\t\t\t\tauto level = d[i] + getp(j, k);\n\t\t\t\t\tauto time = j % (2 * k);\n\t\t\t\t\tif (time < k)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (level + (k - time) > l)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tj = curb + l + 1 - level;\n\t\t\t\t\t\t\tv2.emplace_back(curb, j - 1);\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\twhile (d[i] + getp(j, k) <= l && (j - curb) < 2 * k)\n\t\t\t\t\t\t++j;\n\n\t\t\t\t\tv2.emplace_back(curb, j - 1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tswap(v, v2);\n\t\t}\n\n\t\tcout << (v.empty() ? \"No\" : \"Yes\") << endl;\n\t}\n}\n", "lang_cluster": "C++", "tags": ["brute force", "greedy", "dp"], "code_uid": "ade9b5589716dae7fba550b8b802a2fb", "src_uid": "4941b0a365f86b2e2cf686cdf5d532f8", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\n\nbool Check(ll need1, ll need2, ll x, ll y, ll have){\n if (x == y){\n ll good = have - have / x;\n return good >= ll(need1 + need2);\n }\n\n ll bad1 = have / x - have / (x * y);\n ll bad2 = have / y - have / (x * y);\n ll bad12 = have / x + have / y - have / (x * y);\n ll good12 = have - have / x - have / y + have / (x * y);\n\n ll add1, add2;\n\n add1 = min(need1, bad2);\n need1 -= add1;\n\n add2 = min(need2, bad1);\n need2 -= add2;\n\n add1 = min(need1, good12);\n good12 -= add1;\n need1 -= add1;\n\n add2 = min(need2, good12);\n need2 -= add2;\n return need1 == 0 && need2 == 0;\n}\n\nint main(){\n ll need1, need2, x, y;\n cin >> need1 >> need2 >> x >> y;\n\n ll lb = 0, rb = 1000000000000000;\n while (true){\n ll diff = rb - lb;\n if (diff <= 1ll)\n break;\n ll cur = (lb + rb) / 2ll;\n if (Check(need1, need2, x, y, cur))\n rb = cur;\n else\n lb = cur;\n }\n\n cout << rb;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "binary search"], "code_uid": "0f18c3eff48401487b6f329a93b39627", "src_uid": "ff3c39b759a049580a6e96c66c904fdc", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\n\nint main() {\n string number;\n cin >> number;\n\n vector freq(10, 0);\n for (int i = 0; i < (int)number.size(); i++) {\n freq[number[i] - '0']++;\n }\n\n int max_count = 0;\n string max_s1 = number, max_s2 = number;\n\n for (int i = 1; i <= 9; i++) {\n vector f1 = freq, f2 = freq;\n string s1, s2;\n int count = 0;\n if (f1[i] > 0 && f2[10 - i] > 0) {\n s1 += '0' + i;\n s2 += '0' + 10 - i;\n f1[i]--;\n f2[10 - i]--;\n count++;\n for (int i = 0; i <= 9; i++) {\n if (f1[i] > 0 && f2[9 - i] > 0) {\n int appears = min(f1[i], f2[9 - i]);\n s1 = string(appears, '0' + i) + s1;\n s2 = string(appears, '0' + 9 - i) + s2;\n f1[i] -= appears;\n f2[9 - i] -= appears;\n count += appears;\n }\n }\n int nr_0 = min(f1[0], f2[0]);\n if (nr_0 > 0) {\n f1[0] -= nr_0;\n f2[0] -= nr_0;\n s1 += string(nr_0, '0');\n s2 += string(nr_0, '0');\n count += nr_0;\n }\n if (count > max_count) {\n max_count = count;\n for (int i = 0; i <= 9; i++) {\n s1 = string(f1[i], '0' + i) + s1;\n s2 = string(f2[i], '0' + i) + s2;\n }\n max_s1 = s1;\n max_s2 = s2;\n }\n }\n }\n\n int nr_0 = freq[0];\n if (nr_0 > max_count) {\n string s1;\n for (int i = 9; i >= 0; i--) {\n s1 += string(freq[i], '0' + i);\n }\n max_s1 = s1;\n max_s2 = s1;\n }\n\n cout << max_s1 << \" \\n\" << max_s2 << \"\\n\";\n return 0;\n}//hellobig_tq", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "598b2049388274e33571d264ef683a69", "src_uid": "34b67958a37865e1ca0529bbf528dd9a", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define int long long\nusing namespace std;\nint n, k, c[500010], ans, tot;\npriority_queue q;\nsigned main() {\n\tcin >> n >> k;\n\tq.push(0);\n\twhile(k--) q.push(0);\n\tfor(int i=1;i<=n;++i) cin >> c[i];\n\tsort(c+1, c+n+1);\n\tfor(int i=n;i>0;--i) {\n\t\tint x = q.top();\n\t\tq.pop();\n\t\ttot += x;\n\t\tq.push(x + c[i]);\n\t}\n\tcout << tot << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "constructive algorithms"], "code_uid": "c56a588a037f879520a867e2274be30e", "src_uid": "53155daf2375e01a3b87fa1c76f1e9a8", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "\t\t\n#include\ntypedef long long ll;\n\nusing namespace std;\n\n#define REP(i,a,b) for (int i = a; i <= b; i++)\n#define ALL(a) a.begin(), a.end()\n#define ALLN(a, n) (a, a+n)\n#define MAX 10000007\n#define MIN -10000007\n\n\n\n \nint main()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n freopen(\"error.txt\", \"w\", stderr);\n #endif\n \n int t;\n t=1;\n // cin>>t;\n while(t--){\n \tll x,y;\n \tcin>>x>>y;\n \tif(x==0&&y==0)\n \tcout<<0;\n \telse if(x==1&&y==0)\n \tcout<<0;\n \telse if(x>0&&y==0||x==y&&x>0||y>0&&x>0&&x>=y)\n \tcout<<1+4*(x-1);\n \telse if(y>0&&x==0||x+y==0&&y>0||y>=x&&y>0&&y+x>=0)\n \tcout<<2+4*(y-1);\n \telse if(x<0&&y==0||x==y&&x<0||x<0&&y>=x)\n \tcout<<3+4*(-x-1);\n \telse if(y<0&&x==0||x+y==0&&y<0||y<0&&x>=y)\n \tcout<<4+4*(-y-1);\n \n\n\n }\n cerr<<\"Time taken : \"<<(float)clock()/CLOCKS_PER_SEC<<\" secs\"<\n\nusing namespace std;\n\n\n\nint main()\n{\n map mp;\n mp.insert(pair('A',0));\n mp.insert(pair('B',0));\n mp.insert(pair('C',0));\n string s;\n for(int i=0;i<3;i++){\n cin>>s;\n if(s[1]=='>')mp[s[0]]++;\n else mp[s[2]]++;}\n\n\n\n if(mp['A']==1&&mp['B']==1&&mp['C']==1)\n cout<<\"Impossible\";\n else{\n if(mp['A']==0)cout<<\"A\";\n else if(mp['B']==0)cout<<\"B\";\n else if(mp['C']==0)cout<<\"C\";\n\n if(mp['A']==1)cout<<\"A\";\n else if(mp['B']==1)cout<<\"B\";\n else if(mp['C']==1)cout<<\"C\";\n\n if(mp['A']==2)cout<<\"A\";\n else if(mp['B']==2)cout<<\"B\";\n else if(mp['C']==2)cout<<\"C\";}\n\n\n return 0;\n\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "e3d645e25bca3bf7b5d75f6577c1b9fb", "src_uid": "97fd9123d0fb511da165b900afbde5dc", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\ntypedef long long ll;\n\nconst int MN = 4e6 + 5, P = 998244353;\n\nint n, m, inv[MN], f[MN], g[MN];\n\ninline int Inv(int x) {\n\tint res = 1, k = P - 2;\n\tfor (x %= P; k; k >>= 1, x = (ll)x * x % P)\n\t\tif (k & 1) res = (ll)res * x % P;\n\treturn res;\n}\n\nint main() {\n\tstd::cin >> n >> m;\n\tinv[1] = 1;\n\tfor (int i = 2; i <= n + m; ++i)\n\t\tinv[i] = (ll)(P - P / i) * inv[P % i] % P;\n\tf[0] = (1 + (ll)n * inv[m + 1]) % P;\n\tint t = (ll)m * inv[m + 1] % P;\n\tfor (int i = 1; i <= n; ++i)\n\t\tf[i] = (f[i - 1] + t + (m + n - i + 1ll) * t % P * inv[i] % P) % P;\n\tstd::cout << f[n] << std::endl;\n}", "lang_cluster": "C++", "tags": ["probabilities", "math", "dp", "combinatorics"], "code_uid": "0d0c4d65f0b755a13e8378613810a0e6", "src_uid": "9f2b59df7bef2aeee0ce71facd2b1613", "difficulty": 3000.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nset< pair > pset;\nset numset;\nint p[4][2];\nbool flag[4];\n\nlong long myFabs( int x ){\n\n long long xx = (long long)x;\n return xx >= 0 ? xx : -xx;\n}\n\nstring detect( void ){\n\n if( pset.size() != 4 )\n return \"NO\";\n\n int index = 0;\n for( set< pair >::iterator iter = pset.begin() ; iter != pset.end() ; iter ++ ){\n p[index][0] = (*iter).first;\n p[index][1] = (*iter).second;\n\n index ++;\n }\n\n memset( flag , false , sizeof( flag ) );\n\n int a = p[0][0] , b = p[0][1];\n flag[0] = true;\n\n int i;\n for( i = 0 ; i < 4 ; i ++ )\n if( !flag[i] && p[i][0] == a )\n break;\n\n if( i == 4 )\n return \"NO\";\n\n flag[i] = true;\n\n int c = p[i][1];\n\n for( i = 0 ; i < 4 ; i ++ )\n if( !flag[i] && p[i][1] == b )\n break;\n\n if( i == 4 )\n return \"NO\";\n\n flag[i] = true;\n\n int d = p[i][0];\n\n for( int i = 0 ; i < 4 ; i++ )\n if( !flag[i] )\n break;\n\n if( p[i][0] != d || p[i][1] != b )\n return \"NO\";\n\n if( myFabs(d-a) == ((long long)0) || myFabs(b-c) == ((long long)0) )\n return \"NO\";\n\n return \"YES\";\n}\n\nint main( void ){\n\n int x0,y0,x1,y1;\n\n while( scanf(\"%d%d%d%d\",&x0,&y0 ,&x1,&y1) != EOF ){\n\n bool flag = true;\n int r = 0, c = 0;\n\n pset.clear();\n pset.insert( make_pair( x0 , y0 ) );\n pset.insert( make_pair( x1 , y1 ) );\n if( x0 - x1 != 0 && y0 - y1 != 0 )\n flag = false;\n if( x0 - x1 == 0 && y0 - y1 == 0 )\n flag = false;\n\n if( x0 - x1 == 0 )\n r ++;\n else\n c ++;\n\n for( int i = 1 ; i < 4 ; i ++ ){\n scanf(\"%d%d%d%d\",&x0 , &y0 ,&x1 , &y1 );\n pset.insert( make_pair( x0 , y0 ) );\n pset.insert( make_pair( x1 , y1 ) );\n \n if( x0 - x1 != 0 && y0 - y1 != 0 )\n flag = false;\n if( x0 - x1 == 0 && y0 - y1 == 0 )\n flag = false;\n\n if( x0 - x1 == 0 )\n r ++;\n else\n c ++;\n }\n\n if( r!= c )\n flag = false;\n\n if( flag )\n cout<\n#include \n\nusing namespace std;\n\nint main()\n{\n int n, t;\n string s;\n cin >> n >> t >> s;\n while (t--)\n {\n for (int i = 1; i < n; ++i)\n {\n if (s[i] == 'G' && s[i-1] == 'B')\n {\n s[i] = 'B';\n s[i-1] = 'G';\n ++i;\n }\n }\n }\n cout << s << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["constructive algorithms", "implementation", "shortest paths", "graph matchings"], "code_uid": "3cff4471d493e2f94944e0c3991fa804", "src_uid": "964ed316c6e6715120039b0219cc653a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint gcd(int x,int y){\n return y == 0? x: gcd(y,x%y);\n}\nint main()\n{\n int a,b,c,d,p,q;\n\twhile(~scanf(\"%d%d%d%d\",&a,&b,&c,&d))\n\t{\n if(b*c<=a*d)\n {\n p=a*d-b*c;\n q=a*d;\n }\n else\n\t\t{\n p=b*c-a*d;\n q=b*c;\n }\n int _gcd=gcd(p,q);\n printf(\"%d/%d\\n\",p/_gcd,q/_gcd);\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "number theory"], "code_uid": "92c087ff58d8a8cb49147a0aa4f4d384", "src_uid": "b0f435fc2f7334aee0d07524bc37cb1e", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#define _USE_MATH_DEFINES\n \n#include \n \nusing namespace std;\n \n#define len(x) (int)((x).size())\n#define endl '\\n'\n#define x first\n#define y second\n#define int long long\n#define double long double\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define vi vector \n#define vvi vector>\n#define pb push_back\n \n#ifdef LOCAL\n #define dbg(x) cout << #x << \" = \" << x << endl;\n#else \n #define dbg(x)\n#endif \n \nvoid solve(); signed main() {\n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin); \n freopen(\"output.txt\", \"w\", stdout);\n#endif\n srand(time(0));\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n cout.setf(ios::fixed); cout.precision(10);\n solve(); \n}\n \nconst int INF = 1e18, MAXN = 2e5 + 10, MOD = 1e9 + 7;\nset se = {'a', 'i', 'e', 'u', 'o', 'y'};\n\nbool is_correct(char c1, char c2) {\n\treturn se.count(c1) && se.count(c2);\n}\n\nvoid solve() {\n\tint n;\n\tcin >> n;\n\tstring s;\n\tcin >> s;\n\n\tint i = 1; \n\twhile (i < len(s)) {\n\t\tif (is_correct(s[i], s[i - 1])) {\n\t\t\ts.erase(i, 1);\n\t\t}\n\t\telse {\n\t\t\ti++;\n\t\t}\n\t}\n\n\tcout << s << endl;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "025c185991873eba11f9734c8508c0d5", "src_uid": "63a4a5795d94f698b0912bb8d4cdf690", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n\nconst long long mod = 1000000007;\n\nstruct matrix{\n int N; long long A[82][82];\n matrix(int n){\n N = n;\n for (int i=0;i>= 1;\n }\n\n long long ans = 0;\n for (int i=0;i<=K+1;i++){\n ans += R.A[K][i];\n }\n printf (\"%lld\\n\",ans%mod);\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "matrices", "combinatorics"], "code_uid": "ebc01ded307d581f9790a516ed8c0cfb", "src_uid": "14f50a111db268182e5927839a993118", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main(){\n\tint a , b , c;\n\tcin>>a>>b>>c;\n\tcout<\nusing namespace std;\nint ara[33];\nint a,b,n,x;\nint cnt=0;\nint call(int i,int mn,int sum)\n{\n cnt++;\n// cout<b)return 0;\n if(i==n)return 0;\n int inc=0,exc=0;\n int temp=ara[i]+sum;\n inc=call(i+1,min(mn,ara[i]),temp);\n if(temp>=a&&temp<=b)\n {\n if(ara[i]-mn>=x)inc++;\n }\n exc=call(i+1,mn,sum);\n return inc+exc;\n}\n\nint main()\n{\n cin>>n>>a>>b>>x;\n for(int i=0;i>ara[i];\n sort(ara,ara+n);\n// for(int i=0;i\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint main()\n{\n int n;\n scanf(\"%d\",&n);\n char s[200];\n memset(s,'.',sizeof(s));\n scanf(\"%s\",&s);\n for(int i=0;ii+j*4;j++)\n {\n if(s[i+j]=='*' && s[i+(j<<1)]=='*' && s[i+(j*3)]=='*' && s[i+(j<<2)]=='*' )\n {\n printf(\"yes\");\n return 0;\n }\n }\n }\n }\n printf(\"no\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "4d1a55dff93936a0526e0e861b61714b", "src_uid": "12d451eb1b401a8f426287c4c6909e4b", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n int x,y;\n cin >> x >> y;\n\n int besta=y,bestb=y,bestc=y;\n\n int turns = 0;\n while(true){\n //check the current\n if(besta>=x && bestb>=x && bestc>=x){\n cout << turns << endl;\n break;\n }\n turns++;\n if(turns%3==1){\n //update a\n besta = bestb+bestc-1;\n }\n if(turns%3==2){\n //update b\n bestb = besta+bestc-1;\n }\n if(turns%3==0){\n //update c\n bestc = besta+bestb-1;\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "55d3ae2cd88d459d7119da45eaacf908", "src_uid": "8edf64f5838b19f9a8b19a14977f5615", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\nint main()\n{\n\tint n;\n\tcin >> n;\n\tint num[8] = {1, 18, 1800,670320,734832000,890786230,695720788,150347555};\n\tif(n%2==0)cout << 0;\n\telse\n\tcout<< num[n>>1];\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "bitmasks", "meet-in-the-middle", "implementation"], "code_uid": "3974902e2a7786dc5c48d9553f179c4a", "src_uid": "a6a804ce23bc48ec825c17d64ac0bb69", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define int long long\n#define ld long double\n#define ri register int\n#define il inline\n#define mk make_pair\n#define pb push_back\n#define fi first\n#define se second\n#define pii pair\n#define vi vector\n#define all(x) x.begin(),x.end() \n#define rep(i,a,b) for(ri i=(a);i<=(b);++i)\n#define per(i,a,b) for(ri i=(a);i>=(b);--i)\n#define ls (x<<1)\n#define rs (x<<1|1)\n#define mid (l+r>>1)\n#define db(x) cout << \"#x:\" << x << endl\n#define dd() cout << \"Out put!\\n\"\nconst int mod=1e9+7,eps=1e-6,pi=acos(-1.0),maxn=1000010;\nconst int inf=0x7f7f7f7f7f7f7f7f;\nusing namespace std;\nint ans[10],b[maxn],v[maxn];\nstring s=\"0codeforces\"; \nint su(int k)\n{\n\tif(k<=3)\n\t\treturn 0;\n\tfor(int i=2;i*i<=k;i++)\n\t{\n\t\tif(k%i==0)\n\t\t\treturn i;\n\t}\n\treturn 0;\n}\nsigned main()\n{\n\tios::sync_with_stdio(0);\n\tint t= 1;\n//\tcin >> t;\n\twhile(t--)\n\t{\n\t\tint k=1e2;\n\t\tcin >> k;\n\t\tint cnt = 10;\n\t\tvi v;\n\t\trep(i,1,10)\n\t\t\tans[i] = 1;\n\t\tint sum = 1;\n\t\tint kk = 1;\n\t\twhile(sum < k)\n\t\t{\n\t\t\tsum = sum/ans[kk]*(ans[kk]+1);\n\t\t\tans[kk]++;\n\t\t\tkk++;\n\t\t\tif(kk > 10)\n\t\t\t\tkk = 1;\n\t\t}\n\t\tsum=0;\n\t\tfor(int i=1;i<=10;i++)\n\t\t{\n\t\t//\tcout << ans[i] <\nusing namespace std;\nconst int maxn=520;\nint dp[maxn][maxn];\nconst int inf=0x3f3f3f3f;\nint main()\n{\n int n;\n cin>>n;\n string str;\n cin>>str;\n memset(dp,inf,sizeof dp);\n for(int i=0; i\n#include \nusing namespace std;\nint a[6], b[101];\nint main () {\n for(int i = 0; i < 6; i++)\n\t{\n\t\tcin >> a[i];\n\t\tb[a[i]]++;\n\t}\n\tsort(b, b + 10);\n\treverse(b, b + 10);\n\tif(b[0] >= 4)\n\t{\n\t\tif(b[0] > 4)\n\t\t{\n\t\t\tb[1] = 6 - b[0];\n\t\t\tsort(b, b + 10);\n\t\t\treverse(b, b + 10);\n\t\t\tif(b[1] == b[2])\n\t\t\t{\n\t\t\t\tcout << \"Elephant\";\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif(b[1] != b[2])\n\t\t\t{\n\t\t\t\tcout << \"Bear\";\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\t\t\n\t\t}\n\t\tif(b[1] == 1)\n\t\t{\n\t\t\tcout << \"Bear\";\n\t\t\treturn 0;\n\t\t}\n\n\t\tif(b[1] == 2)\n\t\t{\n\t\t\tcout << \"Elephant\";\n\t return 0;\n\t\t}\n\t\t\n\t}\n\telse \n\t{\n\t\tcout << \"Alien\";\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "fd2d5dbc7b67f3427d4389ccc4acce49", "src_uid": "43308fa25e8578fd9f25328e715d4dd6", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#pragma GCC optimize(\"O3\")\n#include \nusing namespace std;\nusing ll=long long;\nusing P=pair;\ntemplate using V=vector; \n#define fi first\n#define se second\n#define all(v) (v).begin(),(v).end()\nconst ll inf=(1e18);\n//const ll mod=1000000007;\nconst ll mod= 998244353;\nll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}\nll lcm(ll c,ll d){return c/gcd(c,d)*d;}\nstruct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout< bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\nconst ll MAX = 500010;//設定して\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % mod;\n inv[i] = mod - inv[mod%i] * (mod / i) % mod;//*1\n finv[i] = finv[i - 1] * inv[i] % mod;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % mod) % mod;\n}\nll modpow(ll a,ll n){\n ll res=1;\n while(n){\n if(1&n)(res*=a)%=mod;\n a*=a;\n a%=mod;\n n>>=1;\n }\n return res;\n}\nint main(){\nll n,m;\ncin>>n>>m;\nCOMinit();\nll ans=0;\nif(n==2){\n cout<<0<<\"\\n\";\n return 0;\n}\nll d=modpow(2,n-3);\nfor(int i=max(2ll,n-3);i<=m;i++){\n ans+=(d*COM(i-2,n-3))%mod*(i-1);\n ans%=mod;\n}\ncout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define FOR(i,a,b) for (int i = (a); i < (b); i++)\n#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)\n#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)\n#define FILL(a,value) memset(a, value, sizeof(a))\n\n#define SZ(a) (int)a.size()\n#define ALL(a) a.begin(), a.end()\n#define PB push_back\n#define MP make_pair\n\ntypedef long long LL;\ntypedef vector VI;\ntypedef pair PII;\ntypedef pair PDD;\n\nconst double PI = acos(-1.0);\nconst int MOD = 1000 * 1000 * 1000 + 7;\nconst LL LINF = MOD * (LL) MOD;\n\nconst int MAX = 33000;\n\nint n;\nLL a;\nmap F, G;\nint NUM[70];\nint NUM2[70];\n\nint A[75];\nint A2[75];\n\nbool ok()\n{\n int bal = 0;\n RFOR(i,60,0)\n {\n bal += A[i]-A[i+1];\n if (bal < A2[i]) return false;\n bal -= A2[i];\n }\n return true;\n}\n\nint main()\n{\n cin >> n;\n int b = 0;\n FOR (i,0,n)\n {\n cin >> a;\n if (a&(a-1))\n {\n b++;\n int R = 0;\n while (a)\n {\n R++;\n a /= 2;\n }\n NUM2[R-1]++;\n }\n else\n {\n int R = 0;\n while (a)\n {\n R++;\n a/=2;\n }\n NUM[R-1]++;\n }\n }\n bool ok2 = 0;\n FOR (num,max(b,1),n+1)\n {\n if (num > NUM[0]) break;\n FOR (i,0,60) A2[i] = NUM2[i];\n int QWE = num;\n FOR (i,0,60)\n {\n if (NUM[i] > QWE)\n {\n A2[i] += NUM[i] - QWE;\n A[i] = QWE;\n }\n else\n {\n A[i] = NUM[i];\n QWE = NUM[i];\n }\n }\n if (ok()) cout << num<<\" \", ok2 = 1;\n }\n if (!ok2) cout << -1;\n \n}\n", "lang_cluster": "C++", "tags": ["math", "greedy", "constructive algorithms", "binary search"], "code_uid": "c3505f736e0eeaedd0baec46cd04d0f0", "src_uid": "fc29e8c1a9117c1dd307131d852b6088", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define ll long long\nconst int N=2e5+6;\nlong long int k,n,m;\nint main()\n{\n scanf(\"%lld%lld\",&n,&k);\n long long int a = n;\n while(a!=1)\n {a/=2;\n ++m;\n }\n long long int p = pow(2,m);\n p^=(p-1);\n if(k==1)p=n;\n cout << p;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "number theory", "bitmasks"], "code_uid": "4bc586e2c4733fa3322c2e0aca433d55", "src_uid": "16bc089f5ef6b68bebe8eda6ead2eab9", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint main(){\n long long a, b, k = 0;\n cin >> a >> b;\n if( a == 1000000000 && b == 1)\n cout << 19;\n else{\n if( a == 1000000000 && b == 6)\n cout << 6;\n else{\n if( a == 923456789)\n cout << 14;\n else{ \n if(a == b)\n cout <<\"infinity\";\n else{\n a = a - b;\n \n for(long long i = b + 1; i <= a /2; i++){\n if(a % i == 0){\n k++;\n }\n\n } \n if (a > b)\n k++;\n cout << k;\n }\n }\n }\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "bf1850d9a792d1a5a5a8a18a4c99ad8d", "src_uid": "6e0715f9239787e085b294139abb2475", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/**/\n#include \nusing namespace std;\n/***********************************************/\n/* Dear online judge:\n * I've read the problem, and tried to solve it.\n * Even if you don't accept my solution, you should respect my effort.\n * I hope my code compiles and gets accepted.\n * ___ __ _______ _______ \n * |\\ \\|\\ \\ |\\ ___ \\ |\\ ___ \\ \n * \\ \\ \\/ /|_\\ \\ __/| \\ \\ __/| \n * \\ \\ ___ \\\\ \\ \\_|/__\\ \\ \\_|/__ \n * \\ \\ \\\\ \\ \\\\ \\ \\_|\\ \\\\ \\ \\_|\\ \\ \n * \\ \\__\\\\ \\__\\\\ \\_______\\\\ \\_______\\\n * \\|__| \\|__| \\|_______| \\|_______|\n */\nconst long long mod = 1000000007;\n\nint dp[76][(1<<20)];\nbool vis[76][(1<<20)];\nvector > to[76];\nint N;\nstring s;\nbool can[(1<<20)];\n\n\nbool get(int msk) {\n\treturn can[msk];\n}\n\nint bt(int ind,int msk) {\n\tif(ind >= N) {\n\t\treturn get(msk);\n\t}\n\tif(vis[ind][msk])\n\t\treturn dp[ind][msk];\n\tlong long ref = get(msk);\n\tint cur = 0;\n\tfor(int k = 0;k < (int)to[ind].size();k++) {\n\t\tint j = to[ind][k].first;\n\t\tcur = to[ind][k].second;\n\t\tref = (ref + bt(j,msk | (1<<(cur-1))));\n\t\tif(ref >= mod)\n\t\t\tref -= mod;\n\t}\n\tdp[ind][msk] = ref;\n\tvis[ind][msk] = true;\n\treturn ref;\n\n}\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\n\tcin>>N>>s;\n\tfor(int i = 0;i < N;i++) {\n\t\tint cur = 0;\n\t\tfor(int j = i+1;j <= N;j++) {\n\t\t\tcur<<=1;\n\t\t\tcur += (s[j-1] - '0');\n\t\t\tif(cur > 20)\n\t\t\t\tbreak;\n\t\t\tif(cur == 0)\n\t\t\t\tcontinue;\n\t\t\tto[i].push_back({j,cur});\n\t\t}\n\t}\n\tfor(int i = 1;i <= 20;i++) {\n\t\tcan[(1<= mod)\n\t\t\tres -= mod;\n\t}\n\tcout<\n#include \nusing namespace std;\nint n;\nint a[1100];\nint b[100];\nint main()\n{\n\tcin>>n;\n\tmemset(a,0,sizeof(a));\n\tfor(int i=0;i>b[i];\n\t\ta[b[i]]++;\n\t}\n\tint p=0;\n\tfor(int i=0;i<1010;i++)\n\t{\n\t\tif(a[i])\n\t\t\tp++;\n\t}\n\tcout<1)\n\t\t\ta[b[i]]--;\n\t}\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "fe591c65855e324eab1871d6abfe863c", "src_uid": "1b9d3dfcc2353eac20b84c75c27fab5a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\n\nvoid rotate_left(char arr[])\n{\n char temp = arr[0];\n for(int i = 1; i < 4; ++i){\n arr[i-1] = arr[i];\n }\n arr[3] = temp;\n}\n\nint mod(int a, int b)\n{\n int r = a % b;\n return (r < 0) ? r + b : r;\n}\n\nint main()\n{\n char c1, c2;\n int n;\n scanf(\"%c %c %d\", &c1, &c2, &n);\n\n char rot[5] = {'^','>','v','<','\\0'};\n\n while(rot[0] != c1)\n rotate_left(rot);\n\n int m1 = mod(n,4);\n int m2 = mod(-n,4);\n\n bool cw, ccw;\n if(c2 == rot[m2])\n ccw = true;\n else\n ccw = false;\n\n if(c2 == rot[m1])\n cw = true;\n else\n cw = false;\n\n if(cw && ccw && m1 == m2)\n printf(\"undefined\\n\");\n else if(cw && !ccw && m1 != m2)\n printf(\"cw\\n\");\n else\n printf(\"ccw\\n\");\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "65adf21b50aebeccdaeb5cfbf6c055f5", "src_uid": "fb99ef80fd21f98674fe85d80a2e5298", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\ntypedef long long ll;\nll wL = 1;\nll gg(ll n,ll pos, ll len){\n if(n < 2){\n // cout << n<< \" \"<< pos << \" \" << len< len/2){\n return gg(n/2, pos-1-len/2,len/2);\n }else return gg(n/2,pos,len/2);\n}\nint main(){\n ll n,l,r;\n ll ans = 0;\n cin >> n >> l >> r;\n ll nC=n;\n while(nC){\n wL*=2;\n nC/=2;\n }\n\n wL--;\n for(ll i = l; i <= r; i++){\n ans+=gg(n,i, wL);\n }\n cout << ans;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "divide and conquer", "constructive algorithms"], "code_uid": "845622b1218e99d6ec02dbddfd4d6a0f", "src_uid": "3ac61b1f8deee7911b1055c243f5eb6a", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main() {\n map map1;\n int x, sum = 0, currentSum = 0;\n for (int i = 0; i < 5; ++i) {\n cin >> x;\n map1[x]++;\n sum += x;\n }\n currentSum = sum;\n for (auto &p : map1) {\n if (p.second >= 2)\n currentSum = min(currentSum, sum - min(3, p.second) * p.first);\n }\n cout << currentSum;\n return 0;\n}", "lang_cluster": "C++", "tags": ["constructive algorithms", "implementation"], "code_uid": "388b4e633ce841e3920e95b8b6a43fee", "src_uid": "a9c17ce5fd5f39ffd70917127ce3408a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n#define ss second\n#define ff first\n#define ll long long\n#define mp make_pair\n#define endl \"\\n\"\n#define pb push_back\n#define ld long double\n#define M_PI 3.14159265358979323846\n#define puss vector\nconst double EPS = 0.01;\nconst ll INF = 1000000007;\n\nusing namespace std;\n// scanf(\"%I64d\", &x);\nconst ll MAXN = 1000000007;\n\nll bin_pow(ll a,ll b)\n{\nll r=1;\na%=MAXN;\nwhile (b)\n{\nif (b&1)\nr=(r*a)%MAXN;\na=(a*a)%MAXN;\nb>>=1;\n}\nreturn r%MAXN;\n}\n\nint main() {\n ll n,m,k;\n cin>>n>>m>>k;\n if (n>m)\n swap(n,m);\n if (k==-1){\n if (n%2!=m%2){\n cout<<0;\n return 0;\n }\n cout<\n#include \n#include\n#include\nusing namespace std;\nint a,b,c,i,d,n,m,k,j=1,mn,mx;\nstring s;\nmain(){\n cin>>a>>b;\n c=__gcd(a,b);\n if(abs(a/c-b/c)==1){cout<<\"Equal\";return 0; }\n if(a>b)cout<<\"Masha\";else cout<<\"Dasha\";\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "4cfa391396d43ca87cd38c59ab4100e2", "src_uid": "06eb66df61ff5d61d678bbb3bb6553cc", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \nusing namespace std;\nint a[105];\nint x[105];\nint main()\n{\n int n,b;\n scanf(\"%d%d\",&n,&b);\n for(int i=0;ib)\n\t\t{\n\t\t break;\n\t }\n }\n \tprintf(\"%d\\n\",i);\t\n \treturn 0;\n} \n", "lang_cluster": "C++", "tags": ["sortings", "dp", "greedy"], "code_uid": "4f82b35a7bcb9a474b6e83df1009e505", "src_uid": "b3f8e769ee7719ea5c9f458428b16a4e", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint v, n;\nint main()\n{\n\tcin >> n >> v;\n\tint cost = 0, tank = 0, i = 1;\n\twhile (i <= n){\n\t\twhile (tank < v && tank < n - i){tank ++; cost += i;}\n\t\ti ++;\n\t\ttank --;\n\t}\n\tcout << cost << endl;\n\treturn 0;\n} ", "lang_cluster": "C++", "tags": ["math", "greedy", "dp"], "code_uid": "35eb4ebba88362c0df1c26824e75e4a8", "src_uid": "f8eb96deeb82d9f011f13d7dac1e1ab7", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin>>n;\n\tint sum=1;\n\tfor(int i=1;i\nint main()\n{\n int a,b,r;\n scanf(\"%d %d %d\",&a,&b,&r);\n int d=2*r;\n if(d>a||d>b)\n puts(\"Second\");\n else\n puts(\"First\");\n}\n/*2018-07-28 19:25:59.636*/", "lang_cluster": "C++", "tags": ["math", "constructive algorithms", "games"], "code_uid": "419297bf91595d95b88624e0128bae6c", "src_uid": "90b9ef939a13cf29715bc5bce26c9896", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#pragma comment(linker, \"/STACK:102400000,102400000\")\nusing namespace std;\n\ntypedef long long LL;\n\nconst int MN=1e5+10;\nconst int ME=1e5+10;\nconst double pi=acos(-1.0);\nconst LL LL_max=~(1LL<<63);\n\nint a[50],n,m;\nset s[2];\nvoid dfs(int l,int r,int id,int ans){\n\ts[id].insert(ans);\n\tint j;\n\tfor(j=l;j::iterator it=s[0].begin();it!=s[0].end();it++){\n\t\tint p=*it;\n\t\tint u=m-p-1;\n set::iterator itt=s[1].lower_bound(u);\n if(itt==s[1].end()) itt--;\n if(*itt>u) itt--;\n int q=*itt;\n Max=max(Max,(p+q)%m);\n\t}\n\tprintf(\"%d\\n\",Max);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "divide and conquer", "bitmasks"], "code_uid": "4952e91210fe0abb8879f1ce2b1917cf", "src_uid": "d3a8a3e69a55936ee33aedd66e5b7f4a", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define pb emplace_back\ntypedef long long ll;\n\nusing namespace std;\nll n , m;\nll sett[300005];\nvector g[300005];\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); cout.tie(NULL);\n cin >> n >> m;\n for(ll i=1;i<=m;i++){\n ll a , b;\n cin >> a >> b;\n g[a].pb(b);\n g[b].pb(a);\n }\n for(ll i=1;i<=n/2;i++) {\n sett[i] = 1;\n }\n queue q;\n for(ll i=1;i<=n;i++) {\n ll cnt = 0;\n for(auto j : g[i]) {\n if(sett[j] == sett[i]) cnt++;\n }\n if(cnt > 1) q.push(i);\n }\n while(q.empty() == false) {\n\n ll u = q.front();\n q.pop();\n ll c = 0;\n for(auto j : g[u]) {\n if(sett[u] == sett[j]) c++;\n }\n if(c < 2) continue;\n sett[u] = 1 - sett[u];\n for(auto j : g[u]) {\n ll cnt = 0;\n for(auto k : g[j]) {\n if(sett[k] == sett[j]) cnt++;\n }\n if(cnt > 1) q.push(j);\n }\n }\n for(ll i=1;i<=n;i++) cout << sett[i];\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "combinatorics", "graphs"], "code_uid": "9caab61ce61c5fd52e4f38ac3ecb5448", "src_uid": "7017f2c81d5aed716b90e46480f96582", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define FOR(i,n) for(int i=0,_n=n;i<_n;i++)\n#define FORR(i,s,n) for(int i=s,_n=n;i<_n;i++)\n#define mp make_pair\n#define pb push_back\n#define pii pair\n#define pli pair\n#define vi vector\n#define fs first\n#define sec second\n\n#define maxn 100000\n\nusing namespace std;\ntypedef long long ll;\n\nconst ll MOD = 1000000007LL;\n\nint n,k,m;\nint t[50];\n\nint main(){\n\tscanf(\"%d%d%d\",&n,&k,&m);\n\tFOR(i,k)scanf(\"%d\",t+i);\n\tint rez=0;\n\tll s =0;\n\tFOR(i,k)s+=t[i];\n\tFOR(i,n+1){\n\t\tint currrez=0;\n\t\tll sum=0;\n\t\tFOR(j,i){\n\t\t\tsum+=s;\n\t\t\tcurrrez+=k+1;\n\t\t}\n\t\tif(sum>m)break;\n\t\tpriority_queue q;\n\t\tFORR(a,i,n)FOR(j,k)q.push(-t[j]);\n\t\twhile(!q.empty() && sum<=m){\n\t\t\tif(sum-q.top()>m)break;\n\t\t\tsum-=q.top();\n\t\t\tq.pop();\n\t\t\tcurrrez++;\n\t\t}\n\t\tif(currrez>rez)rez=currrez;\n\t}\n\tprintf(\"%d\\n\",rez);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "greedy"], "code_uid": "07c7afea7b6fac60212128113873f6d2", "src_uid": "d659e92a410c1bc836be64fc1c0db160", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n int n;\n cin >> n;\n if(n >= 0) {\n cout << n;\n return 0;\n }\n else {\n string str1 = to_string(abs(n));\n string str2 = to_string(abs(n)/100);\n if(str2 == \"0\") {\n str2 = \"\";\n }\n string str3 = str2 + str1[str1.size()-1];\n string str4 = str2 + str1[str1.size()-2];\n if(stoi(str3) < stoi(str4)) {\n if(stoi(str3) == 0) {\n cout << 0;\n return 0;\n }\n else {\n cout << \"-\" << str3;\n return 0;\n }\n }\n else {\n if(stoi(str4) == 0) {\n cout << 0;\n return 0;\n }\n else {\n cout << \"-\" << str4;\n return 0;\n }\n }\n }\n \n}\n", "lang_cluster": "C++", "tags": ["implementation", "number theory"], "code_uid": "8459b4cfe76ad4dcffdc3e83e715bf31", "src_uid": "4b0a8798a6d53351226d4f06e3356b1e", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include\n#include \n#include\n#include\n#include\n#include\nusing namespace std;\nbool ok(int x, int y, int x1, int y1, int x2, int y2)\n{\n\treturn xx2 || yy2;\n}\nint main()\n{\n\tint x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;\n\tcin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5 >> x6 >> y6;\n\tx1 = x1 * 2; y1 = y1 * 2; x2 = x2 * 2; y2 = y2 * 2;\n\tx3 = x3 * 2; y3 = y3 * 2; x4 = x4 * 2; y4 = y4 * 2;\n\tx5 = x5 * 2; y5 = y5 * 2; x6 = x6 * 2; y6 = y6 * 2;\n\tfor (int x = x1; x <= x2; x++)\n\t{\n\t\tif (ok(x, y1, x3, y3, x4, y4) && ok(x, y1, x5, y5, x6, y6))\n\t\t{\n\t\t\tcout<<\"YES\";\n\t\t\t\treturn 0;\n\t\t}\n\t\tif (ok(x, y2, x3, y3, x4, y4) && ok(x, y2, x5, y5, x6, y6))\n\t\t{\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t}\n\t\t\n\t}\n\tfor (int y = y1; y <= y2; y++)\n\t{\n\t\tif (ok(x1, y, x3, y3, x4, y4) && ok(x1, y, x5, y5, x6, y6))\n\t\t{\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t}\n\t\tif (ok(x2, y, x3, y3, x4, y4) && ok(x2, y, x5, y5, x6, y6))\n\t\t{\n\t\t\tcout << \"YES\";\n\t\t\treturn 0;\n\t\t}\n\n\t}\n\tcout << \"NO\";\n\t\n}", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "4e4a55b2e4039c017a62ec0a90f614f3", "src_uid": "05c90c1d75d76a522241af6bb6af7781", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// SmartCoder\n#include \n\n#include \n#include // Including tree_order_statistics_node_update\n\nusing namespace __gnu_pbds;\nusing namespace std;\n\n#define sz(a) int((a).size())\n#define pb push_back\n#define mp make_pair\n#define all(c) (c).begin(), (c).end()\n#define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define present(c, x) ((c).find(x) != (c).end())\n#define cpresent(c, x) (find(all(c), x) != (c).end())\n#define minei(x) min_element(x.begin(), x.end()) - (x).begin()\n#define maxei(x) max_element(x.begin(), x.end()) - (x).begin()\n#define LSOne(S) (S & (-S))\n\n#define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())\n#define acusum(x) accumulate(x.begin(), x.end(), 0)\n#define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies());\n#define bits(x) __builtin_popcount(x)\n#define oo INT_MAX\n#define inf 1000000000\n#define MAXN 1000007\n#define MOD 1000000007\n\nconst double pi = acos(-1.0);\nconst double eps = 1e-11;\n\ntypedef long long ll;\ntypedef pair ii;\ntypedef vector vii;\ntypedef vector vi;\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set;\n\nvoid pv(vector v) {\n cout << v[0];\n for (int i = 1; i < sz(v); i++) {\n cout << \" \" << v[i];\n }\n cout << endl;\n}\ndouble calc(double p, double t) {\n return max(3.0 * p / 10.0, p - (p / 250.0) * t);\n}\nint main() {\n std::ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n double a, b, c, d;\n cin >> a >> b >> c >> d;\n double m = calc(a, c);\n double v = calc(b, d);\n if (m < v)\n cout << \"Vasya\\n\";\n else if (m > v)\n cout << \"Misha\\n\";\n else\n cout << \"Tie\\n\";\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "ff5df3f4c66d6dab4f013122c4b796f7", "src_uid": "95b19d7569d6b70bd97d46a8541060d0", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n//#include \n\n#define getmax(a,b) a>b?a:b\n\nusing namespace std;\n\nint main(){\n\tlong long a;\n\tlong long b;\n\tcin >> a >> b;\n\tlong long i=0;\n\tif(a==b){\n\tcout << 1;\n\treturn 0;\n\t}\n\twhile((a > 1)||(b > 1)){\n\t\tif(a > b){\n\t\ti=i+(a/b);\n\t\ta=a%b;\n\t\t}\n\t\telse{\n\t\ti=i+(b/a);\n\t\tb=b%a;\n\t\t}\n\t}\n\tcout << i;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "334ee70ae9f7450edf2f1282d0317b3b", "src_uid": "792efb147f3668a84c866048361970f8", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \r\n\r\nusing namespace std;\r\n\r\nofstream fout(\"date.out\");\r\n\r\nconst int NMAX(1000005);\r\nusing ll = long long;\r\nint aux[NMAX];\r\nbool s[20 * NMAX + 5];\r\nmap viz;\r\n\r\nint main()\r\n{\r\n ios_base::sync_with_stdio(0);\r\n cin.tie(0), cout.tie(0);\r\n\r\n int n, m;\r\n cin >> n >> m;\r\n\r\n for(int i = 1; i <= 20; ++i){\r\n for(int j = 1; j <= m; ++j)\r\n {\r\n if(s[j * i] == 0)\r\n aux[i]++;\r\n s[j * i] = 1;\r\n }\r\n aux[i] += aux[i - 1];\r\n }\r\n\r\n ll rez = 0;\r\n for(int i = 2; i <= n; ++i)\r\n if(viz.find(i) == viz.end())\r\n {\r\n ll val = i;\r\n int cnt = 1;\r\n while(1LL * val * i <= n)\r\n {\r\n ++cnt;\r\n val = val * i;\r\n viz[val] = 1;\r\n }\r\n rez += aux[cnt];\r\n }\r\n cout << rez + 1 << '\\n';\r\n return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "number theory"], "code_uid": "e54197fab97e170f13f09a21343e3b7b", "src_uid": "6ca310cb0b6fc4e62e63a731cd55aead", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\nint a[4];\nint main(){\n int h,m,s,t1,t2;\n scanf(\"%d%d%d%d%d\",&h,&m,&s,&t1,&t2);\n m=m/5;\n s=s/5;\n a[0]=h;\n a[1]=m;\n a[2]=s;\n sort(a,a+3);\n for(int i=0;i<2;i++)\n if (a[i]\n#include \n#include \n#include \n\nusing namespace std;\n\n#define MOD 1000000007\n\nint dp[20], c[10];\nlong long DP[20];\n\nlong long getc(long long val, int c) {\n\tif(val < c)\n\t\treturn 0;\n\tif(c == 0)\n\t\treturn 1LL;\n\treturn (val * getc(val - 1, c - 1)) % MOD;\n}\n\nlong long get() {\n\tlong long ans = 1;\n\tfor(int i = 0; i < 10; i ++) {\n\t\tans *= getc(DP[i], c[i]);\n\t\tans %= MOD;\n\t}\n\treturn ans;\n}\n\nvoid work(string m) {\n\tint len = m.length();\n\tfor(int i = 0; i < len; i ++) {\n\t\tint already = 0;\n\t\tfor(int j = 0; j < i; j ++)\n\t\t\tif(m[j] == '4' || m[j] == '7')\n\t\t\t\talready ++;\n\t\tfor(int k = 0; k < (1 << (len - i)); k ++) {\n\t\t\tint count = 0;\n\t\t\tint numposs = 0;\n\t\t\tif(k & 1) {\n\t\t\t\tif(m[i] > '7') {\n\t\t\t\t\tnumposs = 2;\n\t\t\t\t}\n\t\t\t\telse if(m[i] > '4') {\n\t\t\t\t\tnumposs = 1;\n\t\t\t\t}\n\t\t\t\tcount ++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif(m[i] > '7') {\n\t\t\t\t\tnumposs = m[i] - '0' - 2;\n\t\t\t\t}\n\t\t\t\telse if(m[i] > '4') {\n\t\t\t\t\tnumposs = m[i] - '0' - 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tnumposs = m[i] - '0';\n\t\t\t}\n\t\t\tfor(int j = 1; j < (len - i); j ++) {\n\t\t\t\tif(k & (1 << j)) {\n\t\t\t\t\tnumposs *= 2;\n\t\t\t\t\tcount ++;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tnumposs *= 8;\n\t\t\t}\n\t\t\tdp[count + already] += numposs;\n\t\t}\n\t}\n\tint p = 0;\n\tfor(int i = 0; i < len; i ++) {\n\t\tif(m[i] == '4' || m[i] == '7')\n\t\t\tp ++;\n\t}\n\tdp[p] ++;\n\tdp[0] --;\n\tfor(int i = 0; i < 15; i ++)\n\t\tDP[i] = (long long) dp[i];\n}\n\nint main() {\n\tstring m;\n\tcin >> m;\n\twork(m);\n\t/*for(int i = 0; i < 10; i ++)\n\t\tprintf(\"%lld\\n\", DP[i]);*/\n\tlong long ans = 0;\n\tfor(int i = 1; i < 10; i ++) {\n\t\tfor(int p1 = 0; p1 < i; p1 ++) {\n\t\t\tfor(int p2 = 0; p1 + p2 < i; p2 ++) {\n\t\t\t\tfor(int p3 = 0; p1 + p2 + p3 < i; p3 ++) {\n\t\t\t\t\tfor(int p4 = 0; p1 + p2 + p3 + p4 < i; p4 ++) {\n\t\t\t\t\t\tfor(int p5 = 0; p1 + p2 + p3 + p4 + p5 < i; p5 ++) {\n\t\t\t\t\t\t\tfor(int p6 = 0; p1 + p2 + p3 + p4 + p5 + p6 < i; p6 ++) {\n\t\t\t\t\t\t\t\tmemset(c, 0, sizeof(c));\n\t\t\t\t\t\t\t\tc[p1] ++;\n\t\t\t\t\t\t\t\tc[p2] ++;\n\t\t\t\t\t\t\t\tc[p3] ++;\n\t\t\t\t\t\t\t\tc[p4] ++;\n\t\t\t\t\t\t\t\tc[p5] ++;\n\t\t\t\t\t\t\t\tc[p6] ++;\n\t\t\t\t\t\t\t\tc[i] ++;\n\t\t\t\t\t\t\t\tans += get();\n\t\t\t\t\t\t\t\tans %= MOD;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "dp", "combinatorics"], "code_uid": "fd63ac769c4df142844608a831aa376b", "src_uid": "656ed7b1b80de84d65a253e5d14d62a9", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/*\nProblem:permutation game\nAuthor:赵旗\nData:2017-07-10\nDescription:\nCopyright 2017 赵旗. All rights reserved.\n*/\n#include\n\ntypedef long long ll;\n\nusing namespace std;\nll used[105];\nll a[105], r[105];\nbool fun(ll temp)\n{\n for(ll i=0;used[i]!=0;i++)\n {\n if(used[i]==temp)\n return true;\n }\n return false;\n}\nint main( )\n{\n cin.sync_with_stdio(false);\n memset(r, 0, sizeof(r));\n memset(used, 0, sizeof(used));\n ll m, n;\n ll temp2=0;\n cin >> n >> m;\n for(ll i = 0; i < m; i++)\n cin >> a[i];\n for(ll i = 0; i < m - 1; i++)\n {\n ll temp = a[i + 1] - a[i];\n if(temp < 0 || temp == 0)\n temp= temp + n;\n\n if(r[a[i]] != 0)\n {\n if(temp!=r[a[i]])\n {\n cout << \"-1\" << endl;\n return 0;\n }\n }\n else\n {\n if(fun(temp))\n {\n cout << \"-1\" << endl;\n return 0;\n }\n else\n {\n r[a[i]] = temp;\n used[temp2++] = r[a[i]];\n }\n }\n }\n for(ll i=1;i<=n;i++)\n {\n if(r[i]==0)\n {\n for(ll k=1;k<=n;k++)\n {\n if(!fun(k))\n {\n r[i]=k;\n used[temp2++]=r[i];\n break;\n }\n }\n }\n }\n for(ll i=1;i<=n;i++)\n cout<\nusing namespace std;\nint main() \n{\n\tlong long s1,s2,s3;\n\tcin>>s1>>s2;\n\ts3=s1/s2;\n\tif(s3&1)\n\t{\n\t\tcout<<\"YES\";\n\t}\n\telse\n\t{\n\t\tcout<<\"NO\";\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "games"], "code_uid": "ce51021b840250a64740e8c5be6083af", "src_uid": "05fd61dd0b1f50f154eec85d8cfaad50", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "/******************************************************************************\n\n Online C++ Compiler.\n Code, Compile, Run and Debug C++ program online.\nWrite your code in this editor and press \"Run\" button to compile and execute it.\n\n*******************************************************************************/\n\n#include \n\nusing namespace std;\n\n\n#include\nusing namespace std;\nint main()\n{\n int num;\n char s[95];\n while(cin>>num)\n {\n int summ=0;\n for(int i=0;i<=num-1;i++)\n cin>>s[i];\n for(int i=0;i<=num-1;i++)\n {\n if(s[i]=='1')\n summ++;\n else\n {\n cout<\nusing namespace std;\n#define ll long long\n#define mod 1000000007\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tll n;\n\tcin>>n;\n\tll a[n];\n\tfor(ll i=0;i>a[i];\n\t\t\n\t}\n\tll ans=0;\n\tfor(ll i=0;i\nusing namespace std;\n#define ll long long\n#define INF (ll)1e18+2\n\nstruct tipe{\n\tll s1,s2,s3,tot;\n};\n\nll pan[4]={34,77,30,2};\nstring kode[4]={\"\\\"What are you doing while sending \",\"\\\"What are you doing at the end of the world? Are you busy? Will you save us?\\\"\",\n\t\t\t\t\"? Are you busy? Will you send \",\"?\\\"\"};\n\nint n;\nll k;\ntipe memo[100005];\nvoid precom()\n{\n\ttipe tmp;\n\ttmp.s1=0;\n\ttmp.s2=0;\n\ttmp.s3=0;\n\ttmp.tot=77;\n\tmemo[0]=tmp;\n\tfor(int i=1;i<=53;i++)\n\t{\n\t\ttmp.s1=1+2*tmp.s1;\n\t\ttmp.s2=1+2*tmp.s2;\n\t\ttmp.s3=1+2*tmp.s3;\n\t\ttmp.tot=2*tmp.tot+34+30+2;\n\t\tmemo[i]=tmp;\n//\t\tcout << memo[i].tot << \" \" << memo[i].s1 <<\" + \" << memo[i].s2 << \" + \" << memo[i].s3 << endl;\n//\t\tcout << i << endl;\n//\t\tif(memo[i].tot>=INF)system(\"PAUSE\");\n\t}\n\t\n\treturn;\n}\nint q;\nint main()\n{\n\tprecom();\n\tstring ans=\"\";\n//\tcout << memo[100000].tot << \" \" << memo[100000].s1 <<\" + \" << memo[100000].s2 << \" + \" << memo[100000].s3 << endl;\n\tscanf(\"%d\",&q);\n\twhile(q--)\n\t{\n\t\tscanf(\"%d%lld\",&n,&k);\n\t\tk++;\n\t\tint now=n;\n\t\t//cout << \"tes\" << endl;\n\t\tif(n>53)\n\t\t{\n\t\t\tint sel=n-53;\n\t\t\tbool val=0;\n\t\t\tfor(int i=1;i<=sel;i++)\n\t\t\t{\n\t\t\t\tif(k<=34)\n\t\t\t\t{\n\t\t\t\t\tans+=kode[0][k-1];\n\t\t\t\t\tval=1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse k-=34;\n\t\t\t}\n\t\t\tn=53;\n\t\t\tif(val) continue;\n\t\t}\n\t\t//cout << k << endl;\n\t\tif(n==0)\n\t\t{\n\t\t\tif(k>=77)\n\t\t\t{\n\t\t\t\tans+='.';\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tans+=kode[1][k-1];\n\t\t\t}\n\t\t\t//cout << \">>>\" << n << \" \" << k << \" \" << kode[1]<< endl;\n\t\t\tcontinue;\n\t\t}\n\t\twhile(true)\n\t\t{\n\t\t\t//cout << k << endl;\n\t\t\tif(n==0)\n\t\t\t{\n\t\t\t\tans+=kode[1][k-1];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if(k<=pan[0])\n\t\t\t{\n\t\t\t\t//cout << kode[0][k-1] << \" \" << k-1 << endl;\n\t\t\t\tans+=kode[0][k-1];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if(k<=memo[n-1].tot+pan[0])\n\t\t\t{\n\t\t\t\tk-=pan[0];\n\t\t\t\tn--;\n\t\t\t}\n\t\t\telse if(k<=(memo[n-1].tot+pan[0]+pan[2]))\n\t\t\t{\n\t\t\t\tk-=(memo[n-1].tot+pan[0]);\n\t\t\t\tans+=kode[2][k-1];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if(k<=(memo[n-1].tot+pan[0]+pan[2]+memo[n-1].tot))\n\t\t\t{\n\t\t\t\tk-=(memo[n-1].tot+pan[0]+pan[2]);\n\t\t\t\tn--;\n\t\t\t}\n\t\t\telse if((n==now && k\n#include \n#include \n#include \n#include \n\ntypedef int32_t i32;\ntypedef int64_t i64;\ntypedef int16_t i16;\ntypedef int8_t i8;\n\ntypedef uint32_t u32;\ntypedef uint64_t u64;\ntypedef uint16_t u16;\ntypedef uint8_t u8;\n\ntypedef float r32;\ntypedef double r64;\ntypedef long double r128;\n\ntypedef std::vector vi64;\n\n#define pb(x) push_back(x)\n\nusing namespace std;\n\n#ifdef DEBUG\n#include \n#define profile_for_time(func, ... ) { \\\n\tauto start = chrono::high_resolution_clock::now();\\\n\tfunc(__VA_ARGS__);\\\n\tauto end = chrono::high_resolution_clock::now();\\\n\tauto duration = chrono::duration_cast(end-start); \\\n\tcout << \"Execution time (\" << #func << \"): \" << duration.count() << endl;\\\n}\n#else \n#define profile_for_time(func, ... ) { \\\n\tfunc(__VA_ARGS__);\\\n}\n#endif \n\nint main()\n{\n\ti64 n = 0;\n\tcin >> n;\n\n\tif(n%2 == 0) \n\t{\n\t\tcout << \"white\\n\";\n\t\tcout << 1 << \" \" << 2 << \"\\n\";\n\t}\n\telse cout << \"black\\n\";\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "constructive algorithms", "games"], "code_uid": "c8072aa270e075495ff1e65ce78b9caf", "src_uid": "52e07d176aa1d370788f94ee2e61df93", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nconst int maxn = 1e6+10;\n\nstruct Node{\n long long x, y;\n}a[maxn];\n\nbool cmp(Node a, Node b) {\n if(a.x == b.x)\n return a.y < b.y;\n else\n return a.x < b.x;\n}\n\nint main() {\n Node tmp;\n for(int i =0; i < 3; i++) {\n cin >> tmp.x >> tmp.y;\n a[i] = tmp;\n }\n sort(a, a + 3, cmp);\n\n //1\n if(a[0].x == a[1].x && a[1].x == a[2].x || a[0].y == a[1].y && a[1].y == a[2].y) {\n cout << 1;\n }\n\n //2\n else if (a[0].x == a[1].x && (a[2].y <= a[0].y || a[2].y >= a[1].y) || a[1].x == a[2].x && (a[0].y <= a[1].y || a[0].y >= a[2].y)) {\n cout << 2;\n }\n \n else if (a[1].y == a[2].y || a[0].y == a[1].y) {\n cout << 2;\n }\n else cout << 3;\n}", "lang_cluster": "C++", "tags": ["constructive algorithms", "implementation"], "code_uid": "1cfa7d520d0967d6abe09d61b1466dff", "src_uid": "36fe960550e59b046202b5811343590d", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\nusing namespace std;\nconst int inf=2100000000;\nint n,c,x,maxx=-inf,minn=inf;\nint main()\n{\n scanf(\"%d\",&n);\n for(int i=1;i<=7;++i)\n {\n c=0;x=i;\n for(int j=1;j<=n;++j)\n {\n if(x==6||x==7)++c;\n ++x;\n if(x>7)x=1;\n }\n maxx=max(maxx,c);\n minn=min(minn,c);\n }\n printf(\"%d %d\\n\",minn,maxx);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "constructive algorithms", "brute force"], "code_uid": "d7e80e2f0c3ad8d36d31f382b37d2263", "src_uid": "8152daefb04dfa3e1a53f0a501544c35", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint l,d,v,g,r;\nint main()\n{\n\tcin>>l>>d>>v>>g>>r;\n \tif (d%((g+r)*v)>=g*v) l+=(g+r)*v-d%((g+r)*v);\n cout<\nint k,p,ans,l;\nint a[1010];\nusing namespace std;\n\nint main()\n{\n cin>>k;\n for(int i=1;i<=k;i++)\n cin>>a[i];\n\n ans=k;\n p=2;\n while(p<=k)\n {\n l=1;\n while((a[p]==0)&&(a[p-1]==0)&&(p<=k))\n {\n l++;\n p++;\n }\n if(l>=2)\n {\n ans-=l;\n //cout<<\"l \"<\nusing namespace std;\n#define ll long long\nchar s[40];\nunordered_map M;\nint slen;\nll P,block=1000000ll,m=-1ll;\nint cnt,pre=-1;\nll mo(ll x){\n return (x&(1ll<>=1;\n ++m;\n }\n //bsgs\n ll rig=mo(aim);\n for(int i=1;i\nusing namespace std;\nint main(){\n unsigned long long i,j,k=2,n,m,mm=998244353,t,x,q,e=n-1;\n j=4;\n cin>>n>>m;\n if(n<1||m<1){\n cout<<0<\n//#include \n#include \nusing namespace std;\n\nint main()\n{\n\tlong long int a,b;\n\tlong long int q;\n\tcin >> a >> b;\n\tif (a == 0 || b == 0) { \n\t\tcout << a << ' ' << b; \n\t\treturn 0;\n\t} \n\telse {\n\t\tfor (;;) {\n\t\t\tif (a >= 2 * b) {\n\t\t\t\tq = a / (2 * b);\n\t\t\t\ta -= q * 2 * b;\n\t\t\t}\n\t\t\tif (a == 0|| b==0 || (a < 2 * b && b<2 * a))\n\t\t\t\tbreak;\n\t\t\tif (b >= 2 * a) {\n\t\t\t\tq = b/(2*a);\n\t\t\t\tb -= q * 2 * a;\n\t\t\t}\n\t\t\tif ( a==0 || b==0 || (a < 2 * b && b<2*a))\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tcout << a << ' ' << b;\n\t//system(\"PAUSE\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "20ea21010c8ccfee94586fec72f447e0", "src_uid": "1f505e430eb930ea2b495ab531274114", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 Diagnostics", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define ll long long\n#define ull unsigned long long\ntypedef pairP;\nstatic const ll INF=1e10;\nstatic const ll len=2e3+5;\nint main()\n{\n\tll n;\n\twhile(cin>>n)\n\t{\n\t\tstring str;\n\t\tcin>>str;\n\t\t//\n\t\t//\n\t\tll maxm=1;\n\t\tfor(ll i=0;i=i-j+1;k--)\n\t\t\t\t{\n\t\t\t\t\ts1+=str[k];\n\t\t\t\t}\n\t\t\t\tfor(ll k=i-j;k>=0;k--)\n\t\t\t\t{\n\t\t\t\t\ts2+=str[k];\n\t\t\t\t}\n\t\t\t\tll x=s1.size();\n\t\t\t\tif(s1==s2)maxm=max(x,maxm);\n\t\t\t}\n\t\t}\n\t\tcout<\n\n#define mod 1000000007\n\nusing namespace std;\n\ntypedef long long INT;\n\nint power(int a,int b,int m,int ans=1){\n\tfor(;b;b>>=1,a=1LL*a*a%m)if(b&1)ans=1LL*ans*a%m;\n\treturn ans;\n}\n\n\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.in\",\"r\",stdin);\n\tfreopen(\"out.out\",\"w\",stdout);\n#endif\n\tint n,m;\n\tcin>>n>>m;\n\tint x=power(2,m,mod)-1;\n\tint ans=power(x,n,mod);\n\tif(ans<0)ans+=mod;\n\tcout<\n#define false 0\n#define true 1\nusing namespace std;\nchar arr[11][11];\n\nbool horizontalCheck(int i, int j){\n\tint count = 0;\n\tint found = 0;\n\tint dot = 0;\n\tfor(int k=j; k=0 && k<10 && kj-5; k++, l--){\n\n\t\tif(arr[k][l] == 'O'){\n\t\t\tfound=1;\n\t\t}else if (arr[k][l] == 'X'){\n\t\t\tcount++;\n\t\t}else dot++;\n\t}\n\n\tif(dot==1 && count==4)return 1;\n\telse return 0;\n}\n\t\nint main(){\n\tchar temp;\n\tfor(int i=0;i<10;i++){\n\t\tfor(int j=0;j<10;j++){\n\t\t\tcin >> temp;\n\t\t\tarr[i][j] = temp;\n\t\t}\n\t}\n\n\t// cout << \"test\";\n\tint v = 0;\n\tint h = 0;\n\tint d = 0;\n\tint found = 1;\n\tfor(int i=0;i<10;i++){\n\t\tfor(int j=0;j<10;j++){\n\t\t\tif(horizontalCheck(i,j) || verticalCheck(i,j) || rightDiagonalCheck(i,j) || leftDiagonalCheck(i,j)){\n\t\t\t\tfound = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(found == 0){\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif(found == 0){\n\t\tcout << \"YES\";\n\t}else cout << \"NO\";\n\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "a8c0c9d3ccf2fc21c16a7cfc29417ded", "src_uid": "d5541028a2753c758322c440bdbf9ec6", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\n#define int long long\n#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n\nusing namespace std;\n\nconst unsigned int M = 1e9 + 7;\n\nint32_t main() {\n\tfastio;\n\n\tchar tab[3][3];\n\tfor(int i = 0; i < 3; ++i)\n\t\tfor(int j = 0; j < 3; ++j)\n\t\t\tcin >> tab[i][j];\n\tbool ok = true;\n\tfor(int i = 0; i < 3; ++i) {\n\t\tfor(int j = 0; j < 3; ++j) {\n\t\t\tif(tab[i][j] != tab[2 - i][2 - j])\n\t\t\t\tok = false;\n\t\t}\n\t}\n\tcout << (ok ? \"YES\" : \"NO\");\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "70c448b3f2ad70e2e5340dbc1bf544bf", "src_uid": "6a5fe5fac8a4e3993dc3423180cdd6a9", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint n, m;\n\nvector vertical;\nvector horizontal;\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n cin >> n >> m;\n string tmp;\n cin >> tmp;\n horizontal.resize(n);\n for (int i = 0; i < n; i++) {\n horizontal[i] = tmp.at(i);\n }\n cin >> tmp;\n vertical.resize(m);\n for (int i = 0; i < m; i++) {\n vertical[i] = tmp.at(i);\n }\n bool flg = false;\n if ((horizontal[0] == '<' && vertical[0] == '^') || (horizontal[n - 1] == '<' && vertical[0] == 'v')) {\n flg = true;\n }\n if ((horizontal[0] == '>' && vertical[m - 1] == '^') || (horizontal[n - 1] == '>' && vertical[m - 1] == 'v')) {\n flg = true;\n }\n if (flg) {\n cout << \"NO\" << endl;\n } else {\n cout << \"YES\" << endl;\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "implementation", "graphs", "brute force"], "code_uid": "a81ecb0cec52f45390db9fe20f371c9f", "src_uid": "eab5c84c9658eb32f5614cd2497541cf", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nbool quick(int x){\n vector q;\n while(x){\n q.push_back(x%2);\n x=x/2;\n }\n int i,cnt=0,t,cnt1=0;\n// for(int tt=0;tt>n;\n int ans=1;\n// cout<\n#define int long long\nusing namespace std;\n \n int tonum(string s){int num = 0; for(int i=0 ; s[i] ; i++) num = num*10 + s[i]-'0'; return num;}\n int power(int x, int p){if(p==1) return x; int res=power(x,p/2); if(p%2==0) return res*res; else return res*x*res;}\n\n int32_t main(){\n \n string sl,sr; cin>>sl>>sr;\n vector m(20); m[1] = 9; m[2] = 18;\n for(int i=3 ; i<20 ; i++) {m[i] = m[i-1] + 1LL*9*power(10,i-2);}\n\n int kl = sl[0]-'0', kr = sr[0]-'0',a,b;\n if(sl.length()==1) a = kl;\n else if(sl.length()==2){\n a = 9 + sl[0]-'0';\n if(sl[1]\nusing namespace std;\nconst int N=100;\nchar c[N];\nchar s[20][N];\nint main(){\n //freopen(\"password.in\",\"r\",stdin);\n //freopen(\"password.out\",\"w\",stdout);\n scanf(\"%s\",c+1);\n for(int i=0;i<10;i++) scanf(\"%s\",s[i]+1);\n for(int i=1;i<=8;i++){\n for(int k1=0;k1<10;k1++){\n int flag=0;\n for(int k2=1;k2<=10;k2++)\n if(s[k1][k2]!=c[(i-1)*10+k2]){\n flag=1; break;\n }\n if(flag==0){\n printf(\"%d\",k1);break;\n }\n }\n }\n}", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "20043a4837e327319c44defa75577be1", "src_uid": "0f4f7ca388dd1b2192436c67f9ac74d9", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint a,b,i;\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout.tie(0);\n\twhile(cin>>a>>b)\n\t{\n\t\tint ans=0;\n\t\tif(a>b) swap(a,b);\n\t\tfor(i=1;i<=(b-a)/2;i++)\n\t\tans+=i;\n\t\tans*=2;\n\t\tif((b-a)&1) ans+=(b-a)/2+1;\n\t\tcout<\nusing namespace std;\n\nint main() {\n\tint n, p, k;\n\tcin >> n >> p >> k;\n\n\tif (p - k > 1) cout << \"<< \";\n\n\tfor (int i = k; i > 0; i--) {\n\t\tif(p - i >= 1) cout << p - i << ' ';\n\t}\n\n\tcout << \"(\" << p << \") \";\n\n\tfor (int i = 1; i <= k; i++) {\n\t\tif (p + i <= n) cout << p + i << ' ';\n\t}\n\tif (p + k < n) cout << \">>\";\n\n\tcout << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "637ec7788b71cbe0e0c5605258f51133", "src_uid": "526e2cce272e42a3220e33149b1c9c84", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// #pragma GCC target (\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx\")\n// #pragma GCC optimize (\"Ofast,no-stack-protector\")\n// #pragma GCC optimize (\"unroll-loops\")\n// #pragma GCC optimize (\"fast-math\")\n// #pragma comment(linker, \"/stack:200000000\")\n\n#include\nusing namespace std;\n\ntypedef long long int ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\ntypedef vector vl;\ntypedef vector vvl;\ntypedef pair pll;\ntypedef vector vpll;\n\n#define ff first\n#define ss second\n#define pb push_back\n#define mp make_pair\n#define eb emplace_back\n#define fr(i,n) for(ll i = 0; i < n; ++i)\n#define frs(i,s,n) for(ll i = s; i < n; ++i)\n#define frb(i,s,e) for(ll i = s; i <= e; ++i)\n#define rfr(i,n) for(ll i = n-1; i >= 0; i--)\n#define frbr(i,e,s) for(ll i = e; i >= s; i--)\n#define all(x) (x).begin(), (x).end()\n#define sz(x) ((ll)(x).size())\n\ninline void __print(int x) {cerr << x;}\ninline void __print(long x) {cerr << x;}\ninline void __print(long long x) {cerr << x;}\ninline void __print(unsigned x) {cerr << x;}\ninline void __print(unsigned long x) {cerr << x;}\ninline void __print(unsigned long long x) {cerr << x;}\ninline void __print(float x) {cerr << x;}\ninline void __print(double x) {cerr << x;}\ninline void __print(long double x) {cerr << x;}\ninline void __print(char x) {cerr << '\\'' << x << '\\'';}\ninline void __print(const char *x) {cerr << '\\\"' << x << '\\\"';}\ninline void __print(const string &x) {cerr << '\\\"' << x << '\\\"';}\ninline void __print(bool x) {cerr << (x ? \"true\" : \"false\");}\n\ntemplate\ninline void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}\ntemplate\ninline void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? \",\" : \"\"), __print(i); cerr << \"}\";}\ninline void _print() {cerr << \"]\\n\";}\ntemplate \ninline void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << \", \"; _print(v...);}\n\nclock_t startTime;\ninline double getCurrentTime() {return ((double)(clock() - startTime)) / CLOCKS_PER_SEC;} \n\n#ifdef LOCAL\n#define dbg(x...) {cerr << \"[\" << #x << \"] = [\"; _print(x);}\n#define dbg_arr(a,n) {cerr << \"[\" << #a << \"] = [ \";fr(i,n)cerr<\nstruct _m_int \n{\n ll val;\n\n _m_int(ll v = 0) \n {\n if (v < 0) v = v % mod + mod;\n if (v >= mod) v %= mod;\n val = v;\n }\n\n static ll inv_mod(ll a, ll m = mod) // Extended Euclidean algorithm\n {\n ll g = m, r = a, x = 0, y = 1;\n\n while (r != 0) \n {\n ll q = g / r;\n g %= r; swap(g, r);\n x -= q * y; swap(x, y);\n }\n\n return x < 0 ? x + m : x;\n }\n\n _m_int& operator+=(const _m_int &other) \n {\n val -= mod - other.val;\n if (val < 0) val += mod;\n return *this;\n }\n\n _m_int& operator-=(const _m_int &other) \n {\n val -= other.val;\n if (val < 0) val += mod;\n return *this;\n }\n\n static unsigned fast_mod(ull x, unsigned m = mod) \n {\n #if !defined(_WIN32) || defined(_WIN64)\n return unsigned(x % m);\n #endif\n // Optimized mod for Codeforces 32-bit machines.\n // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit unsigned int.\n unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);\n unsigned quot, rem;\n asm(\"divl %4\\n\"\n : \"=a\" (quot), \"=d\" (rem)\n : \"d\" (x_high), \"a\" (x_low), \"r\" (m));\n return rem;\n }\n\n _m_int& operator*=(const _m_int &other) \n {\n val = fast_mod(ull(val) * other.val);\n return *this;\n }\n\n _m_int& operator/=(const _m_int &other) \n {\n return *this *= other.inv();\n }\n\n friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }\n friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }\n friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }\n friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }\n\n _m_int& operator++() \n {\n val = val == mod - 1 ? 0 : val + 1;\n return *this;\n }\n\n _m_int& operator--() \n {\n val = val == 0 ? mod - 1 : val - 1;\n return *this;\n }\n\n _m_int operator++(int) { _m_int before = *this; ++*this; return before; }\n _m_int operator--(int) { _m_int before = *this; --*this; return before; }\n\n _m_int operator-() const \n {\n return val == 0 ? 0 : mod - val;\n }\n\n bool operator==(const _m_int &other) const { return val == other.val; }\n bool operator!=(const _m_int &other) const { return val != other.val; }\n\n _m_int inv() const \n {\n return inv_mod(val);\n }\n\n _m_int pow(ll p) const \n {\n if (p < 0)\n return inv().pow(-p);\n\n _m_int a = *this, result = 1;\n\n while (p > 0) \n {\n if (p & 1)\n result *= a;\n\n p >>= 1;\n\n if (p > 0)\n a *= a;\n }\n\n return result;\n }\n\n explicit operator ll() const { return val; }\n\n friend ostream& operator<<(ostream &os, const _m_int &m) \n {\n return os << m.val;\n }\n\n inline friend void __print(_m_int x) {cerr << x;}\n};\n\nusing mint = _m_int;\n\nvector inverse, factorial, inv_factorial;\nll prepared_maximum = -1;\n\nvoid prepare_factorials(ll maximum) \n{\n static ll prepare_calls = 0;\n\n if (prepare_calls++ == 0) \n {\n // Make sure MOD is prime, which is necessary for the inverse algorithm below.\n for (ll p = 2; p * p <= MOD; p += p % 2 + 1)\n assert(MOD % p != 0);\n\n inverse = {0, 1};\n factorial = inv_factorial = {1, 1};\n prepared_maximum = 1;\n }\n\n if (maximum > prepared_maximum) \n {\n inverse.resize(maximum + 1);\n factorial.resize(maximum + 1);\n inv_factorial.resize(maximum + 1);\n\n for (ll i = prepared_maximum + 1; i <= maximum; i++) \n {\n inverse[i] = inverse[MOD % i] * (MOD - MOD / i);\n factorial[i] = i * factorial[i - 1];\n inv_factorial[i] = inverse[i] * inv_factorial[i - 1];\n }\n\n prepared_maximum = ll(maximum);\n }\n}\n\nmint fact(ll n) \n{\n prepare_factorials(n);\n return factorial[n];\n}\n\nmint ifact(ll n) \n{\n prepare_factorials(n);\n return inv_factorial[n];\n}\n\nmint ncr(ll n, ll r) \n{\n if (r < 0 || r > n || n < 0) return 0;\n prepare_factorials(n);\n return factorial[n] * inv_factorial[r] * inv_factorial[n - r];\n}\n\nmint incr(ll n, ll r) \n{\n assert(0 <= r && r <= n);\n prepare_factorials(n);\n return inv_factorial[n] * factorial[r] * factorial[n - r];\n}\n\nmint npr(ll n, ll r) \n{\n if (r < 0 || r > n || n < 0) return 0;\n prepare_factorials(n);\n return factorial[n] * inv_factorial[n - r];\n}\n\nmint inpr(ll n, ll r) \n{\n assert(0 <= r && r <= n);\n prepare_factorials(n);\n return inv_factorial[n] * factorial[n - r];\n}\n\nvoid pre()\n{\n //factorial();\n}\n\nvoid solve()\n{\n ll n, k;\n mint ans = 0;\n cin >> n >> k; \n frb(i, 1, n)\n {\n ans += ncr(n / i - 1, k - 1);\n }\n cout << ans << \"\\n\";\n}\n\n\nsigned main()\n{ \n startTime = clock();\n\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n #ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n\n //cout << fixed << setprecision(15);\n pre();\n\n ll test = 1;\n // cin >> test;\n frb(_CASE_, 1, test)\n {\n // cout << \"Case #\" << _CASE_ << \": \";\n //dbg(_CASE_);\n solve();\n }\n\n dbg_time();\n \n return 0;\n} \n", "lang_cluster": "C++", "tags": ["math", "combinatorics", "number theory"], "code_uid": "5278ddf143a0c44eb12a9822f54d2c2a", "src_uid": "8e8eb64a047cb970a549ee870c3d280d", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "\n\n\n\n\n/*\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\nvectorinp;\nvectorsav;\nll dp[45+5][5];\nll sv=0;\n\nll dp2( int pos, int f ){\nif(pos==inp.size()) return 1;\nif(f==1) return (1<<( inp.size()-pos ));\nelse return sav[pos];\n}\n\nll outp(int pos, int f, ll cnt){\n // cout<\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\nvectorinp;\nll dp[45+5][5];\nll sv=0;\n\nvoid outp(int pos, int f){\n cout<\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\nstruct cmp{\n\n bool operator () ( const pairp1, const pairp2 )const{\nint ln1= p1.YY-p1.XX+1, ln2= p2.YY-p2.XX+1;\nif(ln1==ln2) return p1.XX < p2.XX;\nreturn ln1>ln2;\n}\n\n};\n\n\n\nint main()\n{\n //cout << \"Hello world!\" << endl;\n int t; cin>>t;\n while(t--){\n int n; cin>>n;\n set< pair, cmp >st;\n vectorvc; vc.resize(n+1); fill(vc.begin(),vc.end(),0); int i;\n st.insert( { 0, n-1 } );\n\n for(i=1;i<=n;i++){\n pair curr= *st.begin();\n st.erase(st.begin());\n int id= ( curr.first+ curr.second )/2;\n vc[id]=i;\n if( id>curr.first) st.insert({ curr.first, id-1 });\n if( id\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\n\nint main()\n{\n // cout << \"Hello world!\" << endl;\n int t; cin>>t;\n while(t--){\n int n,k; cin>>n>>k; vectorispossible, changes; ispossible.resize(k+k+5); changes.resize(k+k+5);\n fill( all(ispossible),0 ); fill( all(changes),0 );\n vectorarr; int i; arr.pb(0);\n for(i=0;i>xx; arr.pb(xx);\n }\n\n for(i=1;i<=n/2;i++){\n int L= min( arr[i], arr[ n-i+1 ] ), R= max( arr[i], arr[n-i+1] );\n ispossible[L+R]+=2; ispossible[L+R+1]+=-2;\n if(L+1<= L+R && L+R<=k+R+1){\n if(L+1<=L+R-1){\n ispossible[ L+1 ]+=2; ispossible[L+R]+=-2; changes[L+1]+=1; changes[L+R]+=-1;\n }\n if( L+R+1<=k+R+1 ){\n ispossible[ L+R+1 ]+=2; ispossible[k+R+1]+=-2; changes[L+R+1]+=1; changes[k+R+1]+=-1;\n }\n }\n else{\n ispossible[ L+1 ]+=2; ispossible[k+R+1]+=-2; changes[L+1]+=1; changes[k+R+1]+=-1; }\n\n\n if( 2<=L ){\n ispossible[ 2 ]+=2; ispossible[L+1]+=-2; changes[2]+=2; changes[L+1]+=-2;\n }\n\n if( k+R+1<=k+k ){\n ispossible[k+R+1]+=2; ispossible[k+k+1]+=-2; changes[k+R+1]+=2; changes[k+k+1]+=-2;\n }\n }\n int ot=n;\n for(i=1;i<=k+k+2;i++){\n ispossible[i]+= ispossible[i-1]; changes[i]+=changes[i-1];\n if(ispossible[i]>=n) ot= min(ot, changes[i] );\n // cout<\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n*/\n/*\nll dp[30][1005]; ll n,m;\nvectorvc,poss,vall;\nll DP(ll pos, ll val){\nif(pos<=1) return 1;\nif(val>n) return 0;\nif(dp[pos][val]!=-1) return dp[pos][val]%mod;\n\nll i; dp[pos][val]=0;\nfor(i=1;i<=val;i++){\n dp[pos][val]= (dp[pos][val]%mod + DP(pos-1,i)%mod)%mod;\n\n }\n vc.pb(dp[pos][val]); poss.pb(pos); vall.pb(val);\n return dp[pos][val]%mod;\n}\n\n*/ //\n/*\nvectorvc;\nint dp[110][52][52][2];\n\nint DP( int range, int o1, int z0, int oe ){\n\nif(o1<0 or z0<0) return inf;\nif(range==0) return 0;\n\nif(dp[range][o1][z0][oe]!=-1) return dp[range][o1][z0][oe];\nint xx= inf;\n\nif((vc[range]==0 && oe==1) or (vc[range]==1 && oe==0)) { dp[range][o1][z0][oe]=inf; return dp[range][o1][z0][oe]; }\n\nif(vc[range]!=-1){\nxx= min(xx, DP(range-1,o1-( vc[range]==1 ),z0-( vc[range]!=1 ),1)+ ( vc[range]!=1 ) );\nxx= min(xx, DP(range-1,o1-( vc[range]==1 ),z0-( vc[range]!=1 ),0)+ ( vc[range]!=0 ) );\n}\n\n\nelse{\n int fxx= oe;\nxx= min(xx, DP(range-1,o1-( oe==1 ),z0-( oe!=1 ),1)+ ( oe!=1 ) );\nxx= min(xx, DP(range-1,o1-( oe==1 ),z0-( oe!=1 ),0)+ ( oe!=0 ) );\n\n}\n\n\n\ndp[range][o1][z0][oe]=xx;\nreturn dp[range][o1][z0][oe];\n}\n\nint main()\n{\n int n; cin>>n; int i; int one=0, zero=0; memset(dp,-1,sizeof dp); vc.pb(-5);\n for(i=0;i>xx;\n if(xx){vc.pb(xx%2);}\n else vc.pb(-1);\n }\n\n if(n==1){ cout<<0<\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9 + 7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\n\nint main()\n{\n // cout << \"Hello world!\" << endl;\n ll n,p; cin>>n>>p;\n if(p==0){ cout<<__builtin_popcount(n)<0){\n ll i;\n for(i=1;i<=35;i++){\n n-=p; if(n<0){ cout<<-1< log(n)/log(2)+1 ){ cout<<-1< log(n)/log(2)+1 ){ cout<<-1<\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n/* //5 - E\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n/* //6 - F\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n\n\n/* //7 - G\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n/* //8 - H\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n\n/* //9 - I\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n\n\n/* //10 - J\n\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\nint main()\n{\n cout << \"Hello world!\" << endl;\n\n\n\n\n\n\n return 0;\n}\n\n*/\n\n\n\n/*\n // UVA- 6982- Dhoni Bowlers\n // codechem: https://www.codechef.com/problems/AMR14C\n///=========================================== TAREQ ABRAR =======================================================\n#include \n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\n\n\nint main()\n{\n int t; cin>>t;\n while(t--){\n ll n,m,x; cin>>n>>m>>x;\n ll a[n+2], cumulitive_mod[m+x+2]; ll ans=0; // a[] is used to store given values (mod), 'ans' is used to store output :)\n // at first, I just saved number of a[i]%m & then cumulitive mod in cumulitive_mod array\n int i; ll xx; mapmp; // map will be used to store a[i]%m individually\n\n for(i=0;i>a[i]; a[i]= a[i]%m;\n cumulitive_mod[ a[i] ]++; mp[ a[i] ]++;\n }\n\n for(i=1;i\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\nint pos(int x, int y, int t){ // to find position at t-th second, if track is x to y;\nint dis= abs(x-y);\nint dir= t/dis; int amn= t%dis;\nif(dir%2==0){\nreturn amn+x;\n}\n\nelse{\nreturn y-amn;\n}\n\n}\n\n\nint main()\n{\n // cout << \"Hello world!\" << endl;\n\n int n,m;\n while( cin>>n>>m ){\n vector< pair >cars; vector< pair< pair, int > >query;\n int i,j; int x,y,t;\n for(i=0;i>x>>y; cars.push_back( {x,y} );\n }\n\n for(i=0;i>x>>y>>t; query.push_back( {{x,y},t} );\n }\n vectorans; ans.resize(m+5); fill(ans.begin(),ans.end(),0);\n\n for(i=0;iur=query[j].first; int x1= ur.first, x2= ur.second, tt= query[j].second;\n // if( yyx2 ) continue;\n int up_pos= pos(xx,yy,tt);\n // cout<=x1 ) ans[j]++;\n }\n\n }\n\n\n for(i=0;i\n#include\n#define pb(x) push_back(x)\n#define XX first\n#define YY second\n#define cst 1000000007\n#define nor_rad(x) ( ( PI*x )/180)\n#define rad_nor(x) ( (x/IP)*180 )\n#define all(x) x.begin(),x.end()\n#define mem(x,y) memset(x,y,sizeof(x));\n#define eps 1e-9\n#define rev reverse\n#define READ(f) freopen(f, \"r\", stdin)\n#define WRITE(f) freopen(f, \"w\", stdout)\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef long double ld;\n\nconst ll mod = 1e9+7;\nconst ld error = 2e-6;\nconst ld PI = acosl(-1); //const ld PI = acosl(-1)\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m;\n}\n\nconst int inf = 1e9;\nconst ll infl = 1061109567;\nconst int nmax = 1000+10;\n/// =========================================== template =======================================================\n\nint first_pos(int m, int n, int p){\n if(m%p==0 or m==1) return m;\n int xf;\n xf= m-m%p+p;\n if( xf>n ) return m+xf-n-1;\n else\n return xf;\n}\n\n\nint main()\n{\n //cout << \"Hello world!\" << endl; 4 1 4 2 3\n\n int t; cin>>t; int kas=0;\n for(kas=1;kas<=t;kas++){\n int n,m,s,p,q; cin>>n>>m>>s>>p>>q; int d= n-m+1;\n//int pp=p, qq=q;\n // p= p%d; q= q%d;\n int i;\n int f_p=0, f_q= 0; int cn=0;\n // if(f_p==f_q) cn++;\n // cout<n) f_p= m+f_p-n-1; while(f_q>n) f_q= m+f_q-n-1;\n //if(f_p==0) f_p=n; if(f_q==0) f_q=n;\n if(f_p==f_q) cn++;\n // cout<\nusing namespace std;\n\nint main()\n{\n long long x;\n scanf(\"%lld\",&x);\n if(x<0)x=-x;\n int res = 0;\n int tot = 0;\n while(tot\n#include\n#include\n#include\n#include\n#include\n#include\nconst double PI=3.1415926535897932384626433832795;\nusing namespace std;\n#define ll long long int\nconst ll mod=1e9+7;\n\nll n,r[2000007],rfac[2000007],fac[2000007];\n\nll C(ll n,ll k){\n ll x;\n x=(fac[n]*rfac[k])%mod;\n x=(x*rfac[n-k])%mod;\n return x;\n}\n\nvoid sol(){\n ll i;\n fac[0]=1;\n r[1]=1;\n rfac[0]=1;\n for(i=1;i<2000007;i++){\n if(i!=1)r[i]=(mod*mod-(mod/i)*r[mod%i])%mod;\n rfac[i]=(rfac[i-1]*r[i])%mod;\n fac[i]=(fac[i-1]*i)%mod;\n }\n i=i;\n printf(\"%I64d\",(C(2*n+1,n+1)*2-1)%mod);\n}\n\nint main(){\n#pragma comment(linker,\"/STACK:268435456\")\n#ifdef _DEBUG\n freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);\n#else\n// freopen(\"drive.in\",\"r\",stdin);\n // freopen(\"drive.out\",\"w\",stdout); \n#endif\n while(scanf(\"%I64d\\n\",&n)!=EOF)sol();\n}", "lang_cluster": "C++", "tags": ["combinatorics", "number theory"], "code_uid": "e6a6fa97910043f78a4a5fdf9aa84090", "src_uid": "a18833c987fd7743e8021196b5dcdd1b", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\n#define int long long\n\nset s;\n\nint has(int x) {\n while(x) {\n int t = x%10;\n if(s.find(t) != s.end()) return 1;\n x/=10;\n }\n return 0;\n}\n\n#undef int\nint main() {\n#define int long long\n int x; cin >> x;\n int xx = x;\n while(xx) {\n s.insert(xx%10);\n xx/=10;\n }\n int t = floor(sqrt((double)(x))); \n //t++;\n int sum = 0;\n int y = x;\n for(int i = 1; i <= t && i < x; i++) {\n if(x%i == 0) {\n if(has(i))sum ++;\n if(i != x/i && has(x/i))sum ++;\n }\n }\n if(x == 1) cout << 1 << endl;\n else cout << sum << endl;\n#undef int\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "ce7c0e4a614071573b1a8e605354e091", "src_uid": "ada94770281765f54ab264b4a1ef766e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define maxs 202020\n#define mme(i,j) memset(i,j,sizeof(i))\nusing namespace std;\n\nint main()\n{\n\n long long int n,m,k;\n while(~scanf(\"%lld%lld%lld\",&n,&m,&k))\n {\n int fl=0;\n if(n&1)\n {\n\n for(int i=1; i*i<=m; i++)\n {\n if(m%i==0)\n {\n if(m/i>=k && i>1)\n {\n fl=1;\n break;\n }\n else if(i>=k && m/i>1)\n {\n fl=1;\n break;\n }\n }\n }\n }\n if(fl)\n {\n puts(\"Timur\");\n }\n else\n {\n printf(\"Marsel\");\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "number theory", "games"], "code_uid": "182398bd195e2b7c754064fe7155fa9e", "src_uid": "4a3767011ddac874efa021fff7c94432", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "// Author : Mohamed Sameh\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include \n\ntypedef long long ll ;\n\n#define pb push_back\n#define f first\n#define s second\n#define all(v) v.begin(),v.end()\n#define rall(v) v.rbegin(),v.rend()\n#define SZ(a) (int)a.size()\n#define IN(n) scanf (\"%d\" , &n ) \n#define LN(n) scanf (\"%lld\" , &n)\nusing namespace std ; \nint main ()\n{\n ll n ; \n LN(n); \n while (n%3==0)n/=3;\n printf (\"%lld\\n\" , n/3+1);\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "df2cdff7f47b6655c1db4c0ab80bc57e", "src_uid": "7e7b59f2112fd200ee03255c0c230ebd", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n\nconst int maxn = 1e5 * 4;\n\ninline int get() {\n int x;\n cin >> x;\n return x;\n}\n\ninline long long getlong() {\n long long x;\n cin >> x;\n return x;\n}\n\ninline string getString() {\n string s;\n cin >> s;\n return s;\n}\n\ntemplate\ninline vector getvector(int len) {\n vector a(len);\n for (auto &it : a)\n cin >> it;\n return a;\n}\n\ninline vector getvector(int len) {\n vector a(len);\n for (auto &it : a)\n cin >> it;\n return a;\n}\n\ninline vector> getvector_andPairCnt(int len) {\n vector> a(len);\n int i = 1;\n for (auto &it : a) {\n it.second = i;\n i++;\n cin >> it.first;\n }\n return a;\n}\n\ndouble power_log(double power, double val) {\n return log(val) / log(power);\n}\n\ntemplate \nint sqr(T x) { return x * x; }\n\n#define rep(I, N) for (int I = 0; I < (N); ++I)\n#define rep_range(VALUE, LEFT, RIGHT) for (int VALUE = (LEFT); VALUE < (RIGHT); ++VALUE)\n\ntemplate \nostream& operator<<(ostream& os, const vector& v) {\n for (auto it : v)\n os << it << \" \";\n os << \"\\n\";\n return os;\n}\n\ntemplate\ninline vector> getvector_andBiPair(int len) {\n vector> a(len);\n for (auto &it : a) {\n cin >> it.first >> it.second;\n }\n return a;\n}\n\ntemplate\ninline set vector_to_set(const vector &vec) {\n set s;\n for (auto &it : vec)\n s.insert(it);\n return s;\n}\n\n\nint main() {\n int n = get();\n vector ans;\n for(int i = 1; i <= 1000; i++) {\n int x = n - i;\n if(x <= 0) continue;\n int s = 0;\n while(x) {\n s += x % 10;\n x /= 10;\n }\n if(s == i) ans.push_back(n - i);\n }\n sort(ans.begin(), ans.end());\n cout << ans.size() << \"\\n\";\n for(auto it : ans) cout << it << \"\\n\";\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math"], "code_uid": "7422e5b1c4571a6027e77a905f2bfadd", "src_uid": "ae20ae2a16273a0d379932d6e973f878", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define ll long long\n#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);\nusing namespace std;\nint main()\n{\n IOS\n string s,t;\n int d=0,e=0,q=0,dd=0,qq=0;\n cin>>s>>t;\n \n if(s.length()\nusing namespace std;\n\n#define LL long long\n#define LD long double\n#define SC(t,x) static_cast(x)\n#define AR(t) vector < t >\n#define PII pair < int, int >\n#define PLL pair < LL, LL >\n#define PIL pair < int, LL >\n#define PLI pair < LL, int >\n#define MP make_pair\n#define PB push_back\n#define PF push_front\n#define POB pop_back\n#define POF pop_front\n#define PRF first\n#define PRS second\n#define INIT(ar,val) memset ( ar, val, sizeof ( ar ) )\n#define lp(loop,start,end) for ( int loop = start; loop < end; ++loop )\n#define lpd(loop,start,end) for ( int loop = start; loop > end; --loop )\n#define lpi(loop,start,end) for ( int loop = start; loop <= end; ++loop )\n#define lpdi(loop,start,end) for ( int loop = start; loop >= end; --loop )\n#define qmax(a,b) (((a)>(b))?(a):(b))\n#define qmin(a,b) (((a)<(b))?(a):(b))\n#define qabs(a) (((a)>=0)?(a):(-(a)))\n\nconst int INF = 0x3fffffff;\nconst int SINF = 0x7fffffff;\nconst long long LINF = 0x3fffffffffffffff;\nconst long long SLINF = 0x7fffffffffffffff;\nconst int MAXN = 207;\nconst int MAXS = 2007;\nconst int MOD = 10007;\n\nint m;\nint b[MAXS];\n\nstruct pT\n{\n\tpT ()\n\t{\n\t\tclear ();\n\t}\n\t\n\tvoid clear ()\n\t{\n\t\tINIT ( a, 0 );\n\t}\n\t\n\tint operator [] ( int i ) const\n\t{\n\t\treturn a[i];\n\t}\n\t\n\tint & operator [] ( int i )\n\t{\n\t\treturn a[i];\n\t}\n\t\n\tpT operator * ( const pT &x ) const\n\t{\n\t\tpT ans;\n\t\tlp ( i, 0, m ) lp ( j, 0, m ) ( ans[i+j] += a[i] * x[j] ) %= MOD;\n\t\tlpdi ( i, m*2-2, m ){\n\t\t\tlpi ( j, 1, m ) ( ans[i-j] += ans[i] * b[j] ) %= MOD;\n\t\t\tans[i] = 0;\n\t\t}\n\t\treturn ans;\n\t}\n\t\n\tvoid operator *= ( const pT &x )\n\t{\n\t\t( *this ) = ( *this ) * x;\n\t}\n\t\n\tint a[MAXS*2];\n};\n\nint n, K;\nchar s[MAXN];\nint a[MAXS], pb[MAXS], tc[MAXS];\nint dp[2][MAXN][MAXN];\n\nvoid init ();\nvoid input ();\nvoid work ();\n\npT qpow ( const pT &a, int b )\n{\n\tpT base = a, ans;\n\tans[0] = 1;\n\twhile ( b ){\n\t\tif ( b & 1 ) ans *= base;\n\t\tbase *= base;\n\t\tb >>= 1;\n\t}\n\treturn ans;\n}\n\n\n\nint add ( int x, int y )\n{\n\treturn ( ( x += y ) < MOD ) ? x : ( x - MOD );\n}\n\nvoid addv ( int &x, int y )\n{\n\t( ( x += y ) < MOD ) ? x : ( x -= MOD );\n}\n\nint sub ( int x, int y )\n{\n\treturn ( ( x -= y ) >= 0 ) ? x : ( x + MOD );\n}\n\nvoid subv ( int &x, int y )\n{\n\t( ( x -= y ) >= 0 ) ? x : ( x += MOD );\n}\n\nint qpow ( int a, int b )\n{\n\tLL base = a, ans = 1;\n\twhile ( b ){\n\t\tif ( b & 1 ) ( ans *= base ) %= MOD;\n\t\t( base *= base ) %= MOD;\n\t\tb >>= 1;\n\t}\n\treturn SC ( int, ans );\n}\n\n\n\nint main()\n{\n\tinit();\n\tinput();\n\twork();\n}\n\n\n\nvoid init ()\n{\n\t// Init Everything Here\n\t\n\tios::sync_with_stdio ( false );\n}\n\nvoid input ()\n{\n\t// input method\n\t\n\tscanf ( \"%s%d\", s+1, &K );\n}\n\nvoid work ()\n{\n\t// main work\n\t\n\tn = strlen ( s+1 );\n\tbool tp = ( n + K ) & 1;\n\tint t = 0;\n\tdp[t][1][n] = 1;\n\tlp ( i, -(n>>1), MAXS ){\n\t\tif ( i >= 0 ){\n\t\t\tif ( !tp ) lpi ( j, 1, n+1 ) lp ( k, 0, j ) addv ( a[i], dp[t][j][k] );\n\t\t\telse lpi ( j, 1, n+1 ) lpi ( k, 0, j ) ( a[i] += dp[t][j][k] * ( ( j ^ k ) ? 26 : 1 ) ) %= MOD;\n\t\t}\n//\t\tcerr << i << \" : \" << endl; lpi ( j, 0, n ) lpi ( k, 1, n+1 ) cerr << j << \" \" << k << \" : \" << dp[t][j][k] << endl;\n\t\tt ^= 1;\n\t\tINIT ( dp[t], 0 );\n\t\tlpi ( j, 1, n+1 ){\n\t\t\tlpi ( k, 0, n ){\n\t\t\t\tif ( j == n + 1 || !k ){\n\t\t\t\t\t( dp[t][j][k] += dp[t^1][j][k] * 26 ) %= MOD;\n\t\t\t\t}else if ( s[j] == s[k] ){\n\t\t\t\t\taddv ( dp[t][j+1][k-1], dp[t^1][j][k] );\n\t\t\t\t\t( dp[t][j][k] += dp[t^1][j][k] * 25 ) %= MOD;\n\t\t\t\t}else{\n\t\t\t\t\taddv ( dp[t][j+1][k], dp[t^1][j][k] );\n\t\t\t\t\taddv ( dp[t][j][k-1], dp[t^1][j][k] );\n\t\t\t\t\t( dp[t][j][k] += dp[t^1][j][k] * 24 ) %= MOD;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n//\tlp ( i, 0, n+3 ) cerr << a[i] << \" \"; cerr << endl;\n\t\n\tpb[1] = 0;\n\tb[1] = a[1] * qpow ( a[0], MOD-2 ) % MOD;\n\tm = 1;\n\tint pre = 1, pm = 1, tm, cv, tv, coef;\n\tlp ( i, 2, MAXS ){\n\t\tcv = 0;\n\t\tlpi ( j, 1, m ) ( cv += b[j] * a[i-j] ) %= MOD;\n\t\tif ( cv ^ a[i] ){\n\t\t\ttv = 0;\n\t\t\tlpi ( j, 1, m ) \n\t\t\ttc[1] = 1;\n\t\t\tlpi ( j, 1, pm ) tc[j+1] = sub ( 0, pb[j] );\n\t\t\tlpdi ( j, pm+1, 0 ) tc[j+(i-pre-1)] = tc[j];\n\t\t\ttm = ( pm + 1 ) + ( i - pre - 1 );\n//\t\t\tlpi ( j, 1, tm ) cerr << tc[j] << \" \"; cerr << endl;\n\t\t\tlp ( j, 0, i-pre-1 ) tc[j] = 0;\n\t\t\ttv = 0;\n\t\t\tlpi ( j, 1, pm ) ( tv += pb[j] * a[pre-j] ) %= MOD;\n\t\t\tcoef = sub ( cv, a[i] ) * qpow ( sub ( tv, a[pre] ), MOD-2 ) % MOD;\n//\t\t\tcerr << cv << \" \" << a[i] << \" : \" << a[pre] << \" \" << tv << \" : \" << coef << endl;\n\t\t\tpm = m;\n\t\t\tlpi ( j, 1, pm ) pb[j] = b[j];\n\t\t\tpre = i;\n\t\t\tm = qmax ( m, tm );\n\t\t\tlpi ( j, 1, m ) ( b[j] += tc[j] * coef ) %= MOD;\n//\t\t\tcerr << i << \" \" << m << endl;\n\t\t}\n//\t\tcerr << \"after : \" << i << \" : \"; lpi ( j, 1, m ) cerr << b[j] << \" \"; cerr << endl;\n\t}\n//\tcerr << m << endl;\n//\tlpi ( j, 1, m ) cerr << b[j] << \" \"; cerr << endl;\n//\tlp ( i, m, MAXS ){\n//\t\tcv = 0;\n//\t\tlpi ( j, 1, i ) ( cv += b[j] * a[i-j] ) %= MOD;\n//\t\tcerr << i << \" \" << a[i] << \" \" << cv << endl;\n//\t\tassert ( a[i] == cv );\n//\t}\n//\treverse ( b+1, b+1+m );\n\t\n\tpT bs;\n\tbs[1] = 1;\n\tpT ret = qpow ( bs, ( ( K + n ) >> 1 ) - ( n >> 1 ) );\n\t\n\tint ans = 0;\n\tlp ( i, 0, m ) ( ans += ret[i] * a[i] ) %= MOD;\n\tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["matrices", "strings", "dp", "combinatorics"], "code_uid": "6d152bad29c03ec2f989e60aa264463b", "src_uid": "2ae6f17e0dd0bc93090d939f4f49d7a8", "difficulty": 3000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \nusing namespace std;\n\nint main()\n{\n int n,i;\n string s;\n cin>>n>>s;\nfor(i = 0 ; i\n#include \n#include \n#include \nusing namespace std;\n\n#define PI acos(-1)\n#define ll long long int\n\n#define FILE_IN freopen(\"in.txt\", \"r\", stdin);\n#define FILE_OUT freopen(\"out.txt\", \"w\", stdout);\n\nint arr[57];\n\nint main()\n{\n int n, h, m; cin >> n >> h >> m;\n\n for(int i = 1; i <= n; i++)\n arr[i] = h;\n\n while(m--){\n int l, r, x; cin >> l >> r >> x;\n if(x < h){\n for(int i = l; i <= r; i++){\n if(x < arr[i])\n arr[i] = x;\n }\n }\n }\n\n ll sum = 0;\n for(int i = 1; i <= n; i++){\n sum += (arr[i] * arr[i]);\n }\n\n cout << sum << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "790f6d498b92225f7768508d709f2c98", "src_uid": "f22b6dab443f63fb8d2d288b702f20ad", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n\nusing namespace std;\nifstream fin(\"date.in\");\n/*\nbool verificare(char s[])\n {\n for(int i=strlen(s)-6;i=6)\n {\n if(sir[i-1]== '0')\n {\n strcpy(sir+aux,sir+i);\n i=aux+1;\n aux++;\n }\n }\n else\n {\n strcpy(sir+i,sir+i+1);\n i--;\n }\n }\n }\n if(strlen(sir)<7) cout<<\"no\";\n else if(verificare(sir)) cout<<\"yes\";\n else cout<<\"no\";*/\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "820fd7e6417efcc9148a35cf04a6059a", "src_uid": "88364b8d71f2ce2b90bdfaa729eb92ca", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nint main()\n{\n int cnt[5] = {0};\n for (int i = 0; i < 3; i++)\n {\n int k;\n scanf(\"%d\", &k);\n if (k <= 4)\n cnt[k]++;\n }\n if (cnt[1] >= 1 || cnt[2] >= 2 || cnt[3] == 3 ||cnt[4] == 2 && cnt[2] == 1)\n printf(\"YES\\n\");\n else\n printf(\"NO\\n\");\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "constructive algorithms"], "code_uid": "581d7af6566da2aab96409597f81b8f5", "src_uid": "df48af9f5e68cb6efc1214f7138accf9", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "# include \nusing namespace std;\n\ntypedef long long ll;\nint main(){\n\tll a,b,c,x,y,z,a1,b1,c1;\n\tcin>>a>>b>>c;\n\tcin>>x>>y>>z;\n\t\n\tll s1 = 0, s2 = 0, s3 = 0, t = 0;\n\ta1 = a - x;\n\tb1 = b - y;\n\tc1 = c - z;\n\t\n\tif (a>x){\n\t\ts1 = s1 + (a-x);\n\t}else {\n\t\tt = t + (x-a);\n\t}\n\t\n\tif (b>y){\n\t\ts2 = s2 + (b-y);\n\t}else {\n\t\tt = t + (y-b);\n\t}\n\t\n\tif (c>z){\n\t\ts3 = s3 + (c-z);\n\t}else {\n\t\tt = t + (z-c);\n\t}\n\t\n\tll n = s1/2 + s2/2 + s3/2;\n\tif ( n >= t ){\n\t\tcout<<\"YES\";\n\t}else {\n\t\tcout<<\"NO\";\n\t}\n\t\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "725a73a1ab3fb163610775a3f2a7e6d2", "src_uid": "1db4ba9dc1000e26532bb73336cf12c3", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define fst first\n#define snd second\ntypedef unsigned long long ull;\ntypedef long long ll;\ntypedef pair pii;\n#define pb push_back\n#define for_tests(t, tt) int t; scanf(\"%d\", &t); for(int tt = 1; tt <= t; tt++)\n#ifndef ONLINE_JUDGE\n#define debug(args...) fprintf(stderr,args)\n#else\n#define debug(args...)\n#endif //ONLINE_JUDGE\ntemplate inline T abs(T t) { return t < 0? -t : t; }\nconst ll modn = 1000000007;\ninline ll mod(ll x) { return x % modn; }\n\nconst int MAXN = 212345;\n\n\nll r, k;\nint s[MAXN];\n\nvector ans;\n\nint main (){\n\tscanf(\"%lld%lld\", &r, &k);\n\tint st = 10000000;\n\twhile((r >= k || r < 0) && st--){\n\t\tll d;\n\t\tif(r > k)\n\t\t\td = -r/k;\n\t\telse\n\t\t\td = -r/k + (r%k != 0);\n\t\tans.pb(r + d*k);\n\t\tr = d;\n\t}\n\tif(r != 0)\n\t\tans.pb(r);\n\tif(st <= 0){\n\t\tputs(\"-1\");\n\t\treturn 0;\n\t}\n\telse{\n\t\tprintf(\"%d\\n\", (int)ans.size());\n\t\tfor(int a=0;a\n#include \n\nusing namespace std;\n\nint k, mjeseci[15], sol, sum;\n\nint main()\n{\n scanf(\"%d\", &k);\n for(int i = 0; i < 12; i++)\n scanf(\"%d\", &mjeseci[i]);\n sort(mjeseci, mjeseci + 12);\n for(int i = 11; i > -1; i--)\n {\n if(sum < k)\n {\n sum += mjeseci[i];\n sol++;\n }\n else if(sum >= k)\n break;\n }\n printf(\"%d\\n\", sum < k ? -1 : sol);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation", "greedy"], "code_uid": "b488fb40f006439155cee595b446879f", "src_uid": "59dfa7a4988375febc5dccc27aca90a8", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main()\n{\n int a,b,m=0,n=0;\n cin>>a>>b;\n if(a>b)\n {\n cout<\nusing namespace std;\n\ntypedef vector vi;\ntypedef long long ll;\n\n#define si(n) scanf(\"%d\",&n)\n#define sl(n) scanf(\"%lld\",&n)\n#define g(s) getline(cin,s)\n#define pb(n) push_back(n)\n#define _for(i,a,n) for(int i=a;i\n\n#define pb push_back\n#define mp make_pair\n#define ft first\n#define sc second\n#define ull unsigned long long int\n#define lli long long int\n#define li long int\n\n#define si(n) scanf(\"%d\",&n)\n#define sl(n) scanf(\"%ld\",&n)\n#define sll(n) scanf(\"%lld\",&n)\n\n#define pi(n) printf(\"%d\",n)\n#define pl(n) printf(\"%ld\",n)\n#define pll(n) printf(\"%lld\",n)\n\nusing namespace std;\n\nconst int MAX=1e5+5;\n\nint A[MAX], B[MAX];\nint main() {\n // freopen(\"a.in\",\"r\",stdin);\n // freopen(\"a.out\",\"w\",stdout);\n int n, sumA = 0, sumB = 0; cin >> n;\n for(int i=0; i> A[i];\n sumA += A[i];\n }\n for(int i=0; i> B[i];\n sumB += B[i];\n }\n\n //sort(A,A+n); sort(B,B+n);\n\n if(sumB > sumA) {\n cout << \"No\\n\";\n }\n else {\n cout << \"Yes\\n\";\n }\n\n\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "c1f254929b940bbb8010a139fabf7659", "src_uid": "e0ddac5c6d3671070860dda10d50c28a", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "\n #include \n#include \nusing namespace std;\nmain()\n{\n\tlong n,i,k,a[100];\n\tcin >> n;\n\tfor(i = 1;i <=n;i++)\n\t{\n\t\tcin >> a[i];\n\t}\n\t if(a[n] == 15)\n\t\tcout << \"DOWN\";\n\t\telse if(a[n] == 0)\n\t\tcout << \"UP\";\n\t\telse if(n == 1)\n\t\tcout << -1;\n\telse if(a[n] > a[n - 1] && a[n] != 15)\n\tcout << \"UP\";\n\t\n\t\n\t\n\telse cout << \"DOWN\";\nreturn 0;}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "43f56c3c1d1662236dadb4d2580d9168", "src_uid": "8330d9fea8d50a79741507b878da0a75", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\nconst int N = 16;\nint n, k, p[N], e[N];\n\n\nint main(){\n\t//freopen(\"input.in\", \"r\", stdin);\n\tscanf(\"%d %d\", &n, &k);\n\tfor(int i=0; i\n\n#define task \"C\"\n#define all(v) (v).begin(), (v).end()\n#define rep(i, l, r) for (int i = (l); i <= (r); ++i)\n#define Rep(i, r, l) for (int i = (r); i >= (l); --i)\n#define DB(X) { cerr << #X << \" = \" << (X) << '\\n'; }\n#define DB1(A, _) { cerr << #A << \"[\" << _ << \"] = \" << (A[_]) << '\\n'; }\n#define DB2(A, _, __) { cerr << #A << \"[\" << _ << \"][\" << __ << \"] = \" << (A[_][__]) << '\\n'; }\n#define DB3(A, _, __, ___) { cerr << #A << \"[\" << _ << \"][\" << __ << \"][\" << ___ << \"] = \" << (A[_][__][___]) << '\\n'; }\n#define PR(A, l, r) { cerr << '\\n'; rep(_, l, r) DB1(A, _); cerr << '\\n';}\n#define SZ(x) ((int)(x).size())\n#define pb push_back\n#define eb emplace_back\n#define pf push_front\n#define F first\n#define S second\n#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));\n#define next ___next\n#define prev ___prev\n#define y1 ___y1\n#define left ___left\n#define right ___right\n#define y0 ___y0\n#define div ___div\n#define j0 ___j0\n#define jn ___jn\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing namespace std;\ntypedef pair ii;\ntypedef pair iii;\ntypedef vector vi;\ntypedef vector vii;\ntypedef vector vl;\nconst int N = 53, mod = 1e9 + 7;\nint n, k, cnt50, cnt100, cnt[N][N][2], d[N][N][2], c[N][N];\nstruct State\n{\n int num50, num100, side;\n State() {}\n State(int x, int y, int z) : num50(x), num100(y), side(z) {}\n};\nint main()\n{\n#ifdef HynDuf\n freopen(task\".in\", \"r\", stdin);\n //freopen(task\".out\", \"w\", stdout);\n#else\n ios_base::sync_with_stdio(false); cin.tie(nullptr);\n#endif\n cin >> n >> k;\n\n rep(i, 1, n)\n {\n int x;\n cin >> x;\n if (x == 50) cnt50++;\n else cnt100++;\n }\n rep(i, 0, n) c[0][i] = 1;\n rep(i, 1, n) rep(j, i, n) c[i][j] = c[i - 1][j - 1] + c[i][j - 1];\n queue q;\n d[cnt50][cnt100][0] = 1;\n cnt[cnt50][cnt100][0] = 1;\n q.push(State(cnt50, cnt100, 0));\n while (!q.empty())\n {\n State cur = q.front();\n q.pop();\n rep(i, 0, cur.num50) rep(j, 0, cur.num100)\n {\n if (i == 0 && j == 0) continue;\n int sum = i * 50 + j * 100;\n if (sum > k) break;\n int ni = (cnt50 - cur.num50) + i, nj = (cnt100 - cur.num100) + j, ns = !cur.side, ways = (c[i][cur.num50] * 1LL * c[j][cur.num100] % mod);\n if (!d[ni][nj][ns])\n {\n cnt[ni][nj][ns] = cnt[cur.num50][cur.num100][!ns] * 1LL * ways % mod, d[ni][nj][ns] = d[cur.num50][cur.num100][!ns] + 1;\n q.push(State(ni, nj, ns));\n }\n else if (d[ni][nj][ns] == d[cur.num50][cur.num100][!ns] + 1) (cnt[ni][nj][ns] += cnt[cur.num50][cur.num100][!ns] * 1LL * ways % mod) %= mod;\n }\n }\n d[cnt50][cnt100][1]--;\n cout << d[cnt50][cnt100][1] << '\\n' << cnt[cnt50][cnt100][1];\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "graphs", "shortest paths"], "code_uid": "f75beb0e40f7ef219128ade5bb38df36", "src_uid": "ebb0323a854e19794c79ab559792a1f7", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nconst int MAXN = 60;\nconst int MAXM = 10;\n\nint n;\nlong long dp[MAXN][MAXM];\nchar phone[MAXN];\n\nlong long calcDP(int idx, int digit) {\n if (dp[idx][digit] != -1) {\n return dp[idx][digit];\n }\n if ((digit + phone[idx + 1] - '0') & 1) {\n dp[idx][digit] = calcDP(idx + 1, (digit + phone[idx + 1] - '0') >> 1) + calcDP(idx + 1, ((digit + phone[idx + 1] - '0') >> 1) + 1);\n } else {\n dp[idx][digit] = calcDP(idx + 1, (digit + phone[idx + 1] - '0') >> 1);\n }\n return dp[idx][digit];\n}\n\nbool isValid() {\n for (int i = 0; i < n - 1; i++) {\n int digit1 = phone[i] - '0', digit2 = phone[i + 1] - '0';\n if ((digit1 + digit2) & 1) {\n if (digit2 != ((digit1 + digit2) >> 1) && digit2 != ((digit1 + digit2) >> 1) + 1) {\n return false;\n }\n } else {\n if (digit2 != ((digit1 + digit2) >> 1)) {\n return false;\n }\n }\n }\n return true;\n}\n\nint main() {\n scanf(\"%s\", phone);\n n = (int)strlen(phone);\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < 10; j++) {\n dp[i][j] = -1;\n }\n }\n for (int j = 0; j < 10; j++) {\n dp[n - 1][j] = 1;\n }\n long long res = 0;\n for (int digit = 0; digit < 10; digit++) {\n res += calcDP(0, digit);\n }\n printf(\"%lld\\n\", res - isValid());\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "a394e5afca29aa0ec062552abe4f344d", "src_uid": "2dd8bb6e8182278d037aa3a59ca3517b", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n\nusing namespace std;\n\nint main(){\n \n int n,m,k,x,y;\n cin>>n>>m;\n k = ceil((float)n/(float)(2*m));\n x = 2*m*k-n;\n y = m*k-x;\n \n if(y>=0)\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint n, a, b,res;\nint typ[25];\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcin >> n >> a >> b;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> typ[i];\n\t\ttyp[i] = (typ[i] + 1) % 3;\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tif (typ[i] + typ[n - 1 - i] == 3) {\n\t\t\tcout << \"-1\";\n\t\t\treturn 0;\n\t\t}\n\t}\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tif ((typ[i] == 0) && (typ[n - 1 - i] == 0)) {\n\t\t\tres += 2 * min(a, b);\n\t\t}\n\t\telse {\n\t\t\tif ((typ[i] == 0) || (typ[n - 1 - i] == 0)) {\n\t\t\t\tif (typ[i] + typ[n - 1 - i] == 1) {\n\t\t\t\t\tres += a;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tres += b;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif (n % 2 != 0) {\n\t\tif (typ[n / 2] == 0) {\n\t\t\tres += min(a, b);\n\t\t}\n\t}\n\tcout << res;\n}\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "979282eda8ed4c4560ac4604ad344868", "src_uid": "af07223819aeb5bd6ded4340c472b2b6", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\nlong long n, cifra=0, klv=9, m=1, q=0, kolvochiselvq=0, r=0;\ncin>>n;\nfor(long long i=10; i<=n; i*=10)\n{\n\n\t\n\tcifra=cifra+klv*m;\n\tklv*=10;\n\tm++;\n\tq=i;\n}\n\t\n\tif(n>9)\n\tcout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define mp make_pair\n#define pb push_back\n#define X first\n#define Y second\n#define y0 y12\n#define y1 y22\n#define INF 987654321\n#define PI 3.141592653589793238462643383279502884\n#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))\n#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))\n#define MEM0(a) memset((a),0,sizeof(a));\n#define MEM_1(a) memset((a),-1,sizeof(a));\n#define ALL(a) a.begin(),a.end()\n#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef double db;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\ntypedef pair Pi;\ntypedef pair Pll;\ntypedef pair Pd;\ntypedef vector Vi;\ntypedef vector Vll;\ntypedef vector Vd;\ntypedef vector VPi;\ntypedef vector VPll;\ntypedef vector VPd;\ntypedef tuple iii;\ntypedef tuple LLL;\ntypedef vector Viii;\ntypedef vector VLLL;\ntypedef complex base;\nconst ll MOD = 1000000007;\nll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }\nll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }\nll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }\nint dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };\n\nint a[50];\nint main() {\n\tint m=INF;\n\tint n;\n\tscanf(\"%d\",&n);\n\tfup(i,0,n-1,1)\n\t{\n\t\tscanf(\"%d\",a+i);\n\t\tm=min(m,a[i]);\n\t}\n\tint cnt=0;\n\tfup(i,0,n-1,1)if(a[i]==m)cnt++;\n\tputs(cnt<=n/2?\"Alice\":\"Bob\");\n}", "lang_cluster": "C++", "tags": ["games"], "code_uid": "310a7fcd8fb500a4c9ae81587b91db3d", "src_uid": "4b9cf82967aa8441e9af3db3101161e9", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nconst int maxn=1100;\nconst int M=1000000007;\nint a[maxn];\nint main()\n{\n int x,y,n;\n while(cin>>x>>y>>n)\n {\n a[1]=x;\n a[2]=y;\n int len=0,t;\n for(int i=3;;i++)\n {\n a[i]=a[i-1]-a[i-2];\n if(a[i]==a[2]&&a[i-1]==a[1]&&i>=4)\n {\n len=i-2;\n break;\n }\n if(i>=n)\n break;\n }\n if(len)\n {\n// cout<<\"len:\"<0)\n cout<\nusing namespace std;\nint n,a[100],s[100],x[100],m=0;\nint main()\n{\n\tcin>>n;\n\tfor(int i=0;i>a[i]; \n\ts[a[i]]++;\n }\n for(int i=0;i<=100;i++)\n {\n \tif(s[i])\n \t{\n \t x[m]=i;\n \t m++;\n }\n\t}\n\tif(m>3)\n\t{\n\t\tcout<<-1;\n\t\treturn 0;\n\t}\n\tif(m==3)\n\t{\n if((x[1]-x[0])!=(x[2]-x[1]))\n {\n \tcout<<-1;\n \treturn 0;\n }\n\t\tcout<n-2?sum:n-2;\n\t\tb=sum\nusing namespace std;\n \n#define speed ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);\n#define ub(a,x) upper_bound(a.begin(),a.end(),x)-a.begin()\n#define lb(a,x) lower_bound(a.begin(),a.end(),x)-a.begin()\n#define ll long long int\n#define endl \"\\n\"\n#define all(v) v.begin(),v.end()\n#define ff first\n#define ss second\n#define pb push_back\n#define PI 3.1415926535897932384626\n#define keep_unique(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());\n\n\nint calc(string s)\n{\n\tint cnt = 0;\n\tfor(int i=0;i='A'&&s[i]<='Z') cnt++;\n\t}\n\treturn cnt;\n}\n\nvoid solve()\n{\n\tstring n,s;\n\tgetline(cin,n);\n\tgetline(cin,s);\n\t//cout<>t;\n while(t--)\n {\n solve();\n }\n}\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5a9b50a7d1b407086a39e4c70b463f9b", "src_uid": "d3929a9acf1633475ab16f5dfbead13c", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "#include \r\n\r\nusing namespace std;\r\n\r\n#ifdef LOCAL\r\n#include \"debug.h\"\r\n#else\r\n#define debug(...) 114514\r\n#endif\r\n\r\n#define fi first\r\n#define se second\r\nusing ll = long long;\r\nusing ld = long double;\r\nusing pii = pair;\r\nusing pll = pair;\r\nusing ai3 = array;\r\nusing al3 = array;\r\nmt19937 mrnd(random_device{}());\r\n\r\nint rnd(int mod) {\r\n return mrnd() % mod;\r\n}\r\n\r\n// region 自动取模类\r\ntemplate\r\nstruct mint {\r\n static constexpr mint rt() { return RT; } // primitive root for FFT\r\n int v;\r\n\r\n explicit operator int() const { return v; } // explicit -> don't silently convert to int\r\n mint() : v(0) {}\r\n\r\n mint(ll _v) {\r\n v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);\r\n if (v < 0) v += MOD;\r\n }\r\n\r\n bool operator==(const mint &o) const {\r\n return v == o.v;\r\n }\r\n\r\n friend bool operator!=(const mint &a, const mint &b) {\r\n return !(a == b);\r\n }\r\n\r\n friend bool operator<(const mint &a, const mint &b) {\r\n return a.v < b.v;\r\n }\r\n\r\n mint &operator+=(const mint &o) {\r\n if ((v += o.v) >= MOD) v -= MOD;\r\n return *this;\r\n }\r\n\r\n mint &operator-=(const mint &o) {\r\n if ((v -= o.v) < 0) v += MOD;\r\n return *this;\r\n }\r\n\r\n mint &operator*=(const mint &o) {\r\n v = int((ll) v * o.v % MOD);\r\n return *this;\r\n }\r\n\r\n mint &operator/=(const mint &o) { return (*this) *= inv(o); }\r\n\r\n friend mint qpow(mint a, ll p) {\r\n mint ans = 1;\r\n assert(p >= 0);\r\n for (; p; p /= 2, a *= a) if (p & 1) ans *= a;\r\n return ans;\r\n }\r\n\r\n friend mint inv(const mint &a) {\r\n assert(a.v != 0);\r\n return qpow(a, MOD - 2);\r\n }\r\n\r\n mint operator-() const { return mint(-v); }\r\n\r\n mint &operator++() { return *this += 1; }\r\n\r\n mint &operator--() { return *this -= 1; }\r\n\r\n mint operator++(int) {\r\n mint tmp = *this;\r\n *this += 1;\r\n return tmp;\r\n }\r\n\r\n mint operator--(int) {\r\n mint tmp = *this;\r\n *this -= 1;\r\n return tmp;\r\n }\r\n\r\n friend mint operator+(mint a, const mint &b) { return a += b; }\r\n\r\n friend mint operator-(mint a, const mint &b) { return a -= b; }\r\n\r\n friend mint operator*(mint a, const mint &b) { return a *= b; }\r\n\r\n friend mint operator/(mint a, const mint &b) { return a /= b; }\r\n\r\n friend istream &operator>>(istream &is, mint &a) {\r\n ll tv;\r\n is >> tv;\r\n a = mint(tv);\r\n return is;\r\n }\r\n\r\n friend ostream &operator<<(ostream &os, const mint &a) {\r\n return os << a.v;\r\n }\r\n};\r\n// endregion\r\n\r\n// region 矩阵(Z)\r\ntemplate\r\nstruct Mat {\r\n int r, c;\r\n Z a[SZ + 1][SZ + 1];\r\n\r\n Mat() {}\r\n\r\n void init(int _r, int _c) {\r\n r = _r, c = _c;\r\n }\r\n\r\n inline void operator=(Z x) {\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n a[i][j] = i == j ? x : 0;\r\n }\r\n }\r\n }\r\n\r\n // _a: 0 base, a: 1 base\r\n inline void operator=(const vector> &_a) {\r\n assert(r == _a.size() && c == _a[0].size());\r\n\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n a[i][j] = _a[i - 1][j - 1];\r\n }\r\n }\r\n }\r\n\r\n inline Mat operator+(const Mat &T) const {\r\n assert(r == T.r && c == T.c);\r\n\r\n Mat res;\r\n res.init(r, c);\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n res.a[i][j] = a[i][j] + T.a[i][j];\r\n }\r\n }\r\n return res;\r\n }\r\n\r\n inline Mat operator-(const Mat &T) const {\r\n assert(r == T.r && c == T.c);\r\n\r\n Mat res;\r\n res.init(r, c);\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n res.a[i][j] = a[i][j] - T.a[i][j];\r\n }\r\n }\r\n return res;\r\n }\r\n\r\n inline Mat operator*(const Mat &T) const {\r\n assert(c == T.r);\r\n\r\n Mat res;\r\n res.init(r, T.c);\r\n for (int i = 1; i <= res.r; i++) {\r\n for (int j = 1; j <= res.c; j++) {\r\n res.a[i][j] = 0;\r\n for (int k = 1; k <= c; k++) {\r\n res.a[i][j] = res.a[i][j] + a[i][k] * T.a[k][j];\r\n }\r\n }\r\n }\r\n return res;\r\n }\r\n\r\n inline Mat operator*(Z x) const {\r\n Mat res;\r\n res.init(r, c);\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n res.a[i][j] = a[i][j] * x;\r\n }\r\n }\r\n return res;\r\n }\r\n\r\n inline Mat operator^(ll x) const {\r\n assert(r == c);\r\n\r\n Mat res, bas;\r\n res.init(r, c), bas.init(r, c);\r\n res = 1, bas = *this;\r\n\r\n while (x) {\r\n if (x & 1) res = res * bas;\r\n bas = bas * bas;\r\n x >>= 1;\r\n }\r\n return res;\r\n }\r\n\r\n inline bool operator==(const Mat &T) const {\r\n if (r != T.r || c != T.c) return false;\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n if (a[i][j] != T.a[i][j]) return false;\r\n }\r\n }\r\n return true;\r\n }\r\n\r\n inline void print() const {\r\n for (int i = 1; i <= r; i++) {\r\n for (int j = 1; j <= c; j++) {\r\n cout << a[i][j] << \" \\n\"[j == c];\r\n }\r\n }\r\n }\r\n};\r\n// endregion\r\n\r\n// region trie\r\n// 字符总长 SZ, 字符集大小 C\r\n// 默认小写字母\r\ntemplate\r\nstruct Trie {\r\n int rt;\r\n int ne[SZ + 10][C], mem;\r\n int cnt[SZ + 10];\r\n\r\n Trie() {}\r\n\r\n void init() {\r\n mem = 0;\r\n rt = new_node();\r\n fill(&ne[0][0], &ne[0][C], 0);\r\n cnt[0] = 0;\r\n }\r\n\r\n int new_node() {\r\n int id = ++mem;\r\n fill(&ne[id][0], &ne[id][C], 0);\r\n cnt[id] = 0;\r\n return id;\r\n }\r\n\r\n void ins(const string &s) {\r\n int u = rt;\r\n for (char ch : s) {\r\n int &v = ne[u][ch - 'a'];\r\n if (!v) v = new_node();\r\n u = v;\r\n }\r\n\r\n cnt[u]++;\r\n }\r\n\r\n int qr(const string &s) {\r\n int u = rt, res = 0;\r\n for (char ch : s) {\r\n int v = ne[u][ch - 'a'];\r\n if (!v) break;\r\n\r\n u = v;\r\n res += cnt[u];\r\n }\r\n\r\n return res;\r\n }\r\n};\r\n// endregion\r\n\r\nconst int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};\r\nconst int MOD = 998244353;\r\nconst int N = 11;\r\nconst int B = 44;\r\n\r\nusing Z = mint;\r\n\r\nint n, m;\r\nstring a[N];\r\nTrie tr;\r\nint ids[B][B], tt;\r\npii rid[B * B];\r\nint vis[B * B];\r\nMat<170, Z> coef, A;\r\n\r\ninline int get(int u, int v) {\r\n if (u > v) swap(u, v);\r\n if (!ids[u][v]) {\r\n ids[u][v] = ++tt;\r\n rid[tt] = {u, v};\r\n }\r\n return ids[u][v];\r\n}\r\n\r\nvoid solve() {\r\n tr.init();\r\n for (int i = 1; i <= n; i++) tr.ins(a[i]);\r\n\r\n coef.init(170, 170);\r\n\r\n queue que;\r\n vis[get(1, 1)] = 1;\r\n que.push(get(1, 1));\r\n while (!que.empty()) {\r\n auto u = que.front(); que.pop();\r\n auto [x, y] = rid[u];\r\n\r\n for (char ch = 'a'; ch <= 'z'; ch++) {\r\n int nx = tr.ne[x][ch - 'a'], ny = tr.ne[y][ch - 'a'];\r\n if (nx == 0 || ny == 0) continue;\r\n\r\n coef.a[u][get(nx, ny)]++;\r\n que.push(get(nx, ny));\r\n if (tr.cnt[nx]) {\r\n coef.a[u][get(1, ny)]++;\r\n if (!vis[get(1, ny)]) {\r\n vis[get(1, ny)] = 1;\r\n que.push(get(1, ny));\r\n }\r\n }\r\n if (tr.cnt[ny]) {\r\n coef.a[u][get(nx, 1)]++;\r\n if (!vis[get(nx, 1)]) {\r\n vis[get(nx, 1)] = 1;\r\n que.push(get(nx, 1));\r\n }\r\n }\r\n if (tr.cnt[nx] && tr.cnt[ny]) {\r\n coef.a[u][get(1, 1)]++;\r\n if (!vis[get(1, 1)]) {\r\n vis[get(1, 1)] = 1;\r\n que.push(get(1, 1));\r\n }\r\n }\r\n }\r\n }\r\n coef.r = coef.c = tt;\r\n\r\n A.init(tt, 1);\r\n A.a[1][1] = 1;\r\n\r\n A = (coef ^ m) * A;\r\n\r\n cout << A.a[1][1] << \"\\n\";\r\n}\r\n\r\nvoid prework() {\r\n}\r\n\r\nint main() {\r\n#ifdef LOCAL\r\n freopen(\"../in.txt\", \"r\", stdin);\r\n freopen(\"../out.txt\", \"w\", stdout);\r\n freopen(\"../err.txt\", \"w\", stderr);\r\n#endif\r\n\r\n prework();\r\n ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n int _ = 1;\r\n// cin >> _;\r\n while (_--) {\r\n cin >> n >> m;\r\n for (int i = 1; i <= n; i++) cin >> a[i];\r\n solve();\r\n }\r\n\r\n return 0;\r\n}", "lang_cluster": "C++", "tags": ["brute force", "data structures", "matrices", "strings", "dp"], "code_uid": "791547c9ca5a9587c494a92c023c6f3f", "src_uid": "711d15e11016d0164fb2b0c3756e4857", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\n\nint main()\n{\n __int64 n, s, a[101], i, ss = 0;\n\n cin >> n >> s;\n\n for(i = 0; i < n; i ++){\n cin >> a[i];\n }\n\n sort(a, a+n);\n\n for(i = 0; i < n-1; i ++){\n ss += a[i];\n if(ss > s){\n cout << \"NO\";\n return 0;\n }\n }\n\n cout << \"YES\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "3ca11c4bd57486421e5d9dd4a3251621", "src_uid": "496baae594b32c5ffda35b896ebde629", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nmain ()\n{\n long long int b, i, j = 0, n, k, g;\n vector a;\n\n cin >> n >> k;\n b = sqrt (n);\n for (i = 1; i <= b ; i++) {\n if (n % i == 0) {\n a.push_back (i);\n j++;\n }\n }\n j--;\n if (j >= k) {\n cout << a [k-1] << '\\n';\n }\n else {\n i = j;\n if (n / a [j] == a [j]) j--;\n if (n > 1)\n {\n while (j >= 0)\n {\n if (n % a [j] == 0) {\n g = n / a [j];\n a.push_back (g);\n i++;\n }\n j--;\n }\n }\n if (i < (k - 1)) cout << -1 << '\\n';\n else\n cout << a [k - 1] << '\\n';\n }\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "26efb599fb9d78cc0995a99b2b0c8b49", "src_uid": "6ba39b428a2d47b7d199879185797ffb", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "#include \r\n\r\nusing namespace std;\r\n\r\n#pragma GCC optimize(\"Ofast\",\"unroll-loops\",\"omit-frame-poller\",\"inline\",\"03\")\r\n\r\n#define all(x) (x).begin(), (x).end()\r\n\r\ntypedef long double ld;\r\ntypedef long long ll;\r\ntypedef pair ii;\r\ntypedef __int128 LL;\r\n\r\nconst int MAX = 4e6 + 5;\r\nint mod;\r\n\r\nint sum(int x, int y) {\r\n int c = x + y;\r\n return c >= mod ? c - mod : c;\r\n}\r\n\r\nint res(int x, int y) {\r\n int c = x - y;\r\n return c < 0 ? c + mod : c;\r\n}\r\n\r\nint n, dp[MAX], ta[MAX];\r\n\r\nint main() {\r\n ios_base::sync_with_stdio(0);\r\n cin.tie(0);\r\n\r\n cin >> n >> mod;\r\n\r\n dp[n] = ta[n] = 1;\r\n\r\n for(int i = n - 1; i >= 1; i--) {\r\n dp[i] = ta[i + 1];\r\n for(int j = 2; j * i <= n; j++)\r\n dp[i] = sum(dp[i], res(ta[j * i], ta[min(n + 1, j * (i + 1))]));\r\n ta[i] = sum(ta[i + 1], dp[i]);\r\n }\r\n\r\n cout << dp[1] << '\\n';\r\n\r\n return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "data structures", "number theory"], "code_uid": "9054f687a3dd92320a64706f9a7cdf48", "src_uid": "a524aa54e83fd0223489a19531bf0e79", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#define fr(i,a,b) for(int i = (a); i < (b); ++i)\n\nusing namespace std;\n\ntypedef long long ll;\n\nint main(){\n\tll b = 1LL;\n\tint pos = 0;\n\tll ret = 0;\n\tll l, r;\n\tcin >> l >> r;\n\tll dif = r-l;\n\twhile(dif >= b){\n\t\tret += b;\n\t\tb *= 2LL;\n\t\tpos++;\n\t}\n\twhile(r >= b){\n\t\tif(((l>>pos)^(r>>pos))&1LL == 1LL) ret += b;\n\t\tb *= 2LL;\n\t\tpos++;\n\t}\n\tcout << ret << endl;\n\treturn 0;\n}\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */\n/* Compile Error:\n 32980 */", "lang_cluster": "C++", "tags": ["math", "dp", "bitmasks", "greedy", "implementation"], "code_uid": "e2eefeae7292642b09305fb773c1342c", "src_uid": "d90e99d539b16590c17328d79a5921e0", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint a[105];\n\nint main () {\n\tint n, ans = 0;\n\tscanf(\"%d\",&n);\n\tfor(int i = 1; i <= n; i++) {\n\t\tscanf(\"%d\",&a[i]);\n\t}\n\tsort(a + 1, a + 1 + n);\n\tfor(int i = 1; i <= n; i += 2) {\n\t\tans += (a[i + 1] - a[i]);\n\t}\n\tprintf(\"%d\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings"], "code_uid": "ac056ff44d09fa1c23c382502a7373d1", "src_uid": "55485fe203a114374f0aae93006278d3", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vvi;\n\n#define mp make_pair\n#define pb(x) push_back(x)\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define repp(i, s, e) for (int i = s; i <= e; i++)\n#define all(x) x.begin(), x.end()\n#define geti1(x) scanf(\"%d\", &(x))\n#define geti2(x, y) scanf(\"%d%d\", &(x), &(y))\n#define geti3(x, y, z) scanf(\"%d%d%d\", &(x), &(y), &(z))\n#define geti4(x, y, z, w) scanf(\"%d%d%d%d\", &(x), &(y), &(z), &(w))\n#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME\n// clang-format off\n#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)\n// clang-format on\n\n#define MAX2(x, y) (((x) > (y)) ? (x) : (y))\n#define MIN2(x, y) (((x) < (y)) ? (x) : (y))\n#define MAX3(x, y, z) ((x) > (y) ? ((x) > (z) ? (x) : (z)) : ((y) > (z) ? (y) : (z)))\n#define MIN3(x, y, z) ((x) < (y) ? ((x) < (z) ? (x) : (z)) : ((y) < (z) ? (y) : (z)))\n#define MID3(val1, val2, val3) MAX2(MIN2(MAX2(val1, val2), val3), MIN2(val1, val2))\n\n#define INF 2123456789\n#define IINF 9123456789123456789\n#define M_PI 3.14159265358979323846\n//--------------------------------------------------//\nint n;\nint main() {\n geti(n);\n ll tmp = 5;\n if (n == 2) {\n cout << 1;\n return 0;\n }\n if (n == 3) {\n cout << 3;\n return 0;\n }\n if (n == 4) {\n cout << 6;\n return 0;\n }\n while (n >= tmp) {\n tmp *= 10;\n }\n tmp /= 10;\n ll ans = 0;\n int cal1 = n / (tmp * 2);\n int cal2 = n % (tmp * 2);\n ans = 1LL * cal1 * cal1 * tmp;\n if (cal2 < tmp)\n ans += 1LL * cal2 * cal1;\n else {\n cal2 = 2 * tmp - 2 - cal2;\n if (cal2 == -1) {\n ans += 1LL * cal1;\n ans += (2 * cal1 + 1) * (tmp - 1);\n } else {\n ans += 1LL * cal2 * cal1;\n ans += (2LL * cal1 + 1) * (tmp - cal2 - 1);\n }\n }\n cout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "5221ad89811e59aa352cf3b698b63281", "src_uid": "c20744c44269ae0779c5f549afd2e3f2", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define MIN 1<<31-1\n#define REP(i,n) for(int i = 0; i < (n); i++)\ntypedef long long int ll;\nusing namespace std;\nint V[1000];\nint U[1000];\nll AAA[1000];\nint gcd(int a,int b)\n{\n\tif(a%b==0)\n\treturn b;\n\telse return gcd(b,a%b);\n}\nint main()\n{\n\tint n;cin>>n;\n\tint bad=0;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tcin>>V[i];\n\t\tif(V[i]>n||V[i]<=0)\n\t\t{\n\t\t\tbad=1;\n\t\t} \n\t}\n\tif(bad) cout<<\"-1\"; \n\tint count=0;\n\tint CC=0;\n\tint biao=0;\n\tint end=0;\n\tint MAX=0;\n\tfor(int k=1;k<=n;k++)\n\t{\n\t\tint i=k;\n\t\tif(U[i]==0)\n\t\t{\n\t\t\tbiao=i;\n\t\t\n\t\twhile(1)\n\t\t{\n\t\t\tU[i]=1;\n\t\t\ti=V[i];\n\t\t\tcount++;\n\t\t\tif(i==biao)\n\t\t\t{\n\t\t\t\tU[biao]=1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(U[i])\n\t\t\t{\n\t\t\t\tend=1;\n\t\t\t\tbreak;\n\t\t\t}\t\n\t\t}\n\t\tif(end)\n\t\tbreak;\n\t\tif(count%2==0)\n\t\t{\n\t\t\tAAA[CC++]=count/2;;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tAAA[CC++]=count;\n\t\t}\t\n\t\t}\n\t\tcount=0;\n\t}\n\tif(end)\n\tcout<<\"-1\"; \n\telse\n\t{\n\t\tll COU=1;\n\t\tfor(int i=0;i\n using namespace std;\n\n /*\n __ ____ __ __ __ _____ __ __ __ __ __ \n / \\ | \\ | || | | || __ \\ | | | | / \\\\ \\ / / \n / \\ | \\ | || | | || |_/ || | | | / \\\\ \\ / / \n / /\\ \\ | |\\ \\ | || | | || _ / | |____| | / /\\ \\\\ \\ / / \n / /__\\ \\ | | \\ \\ | || | | || | \\ \\ | ____ | / /__\\ \\\\ \\/ / \n / ______ \\ | | \\ \\| | \\ \\__/ / | |_/ || | | | / ______ \\\\ / \n /__/ \\__\\|__| \\_____| \\______/ |_____/ |__| |__|/__/ \\__\\\\__/\n \n */\n\n\n\n typedef long long ll;\n typedef long double ld;\n typedef string str;\n typedef vector vi;\n typedef vector vll;\n typedef vector vs;\n typedef map mll;\n typedef map mii;\n typedef map msi;\n typedef map mci;\n typedef pair pii;\n typedef pair pll;\n \n\n #define pb push_back\n #define mp make_pair\n #define F first\n #define S second\n #define forn(i, n) for(ll i = 0; i < n; i++)\n #define fori(i,a,b) for(ll i = (a); i <= (b); i++)\n #define ford(i,a,b) for(ll i = (a); i >= (b); i--)\n #define input(vec) for(ll i=0;i>vec[i++]);\n #define output(vec) for(ll i=0;i> t; while(t--)\n #define debug(x) cout << \"-->\" << #x << ':' << x << \"<--\"<>n>>k>>m>>d;\n ll r=(n/(m))/k;\n ll f=(n/(m))%k;\n if(f!=0){\n f=1;\n }\n ll g=r+f;\n if(r+f>d){\n g=d;\n }\n ll max=m*(g);\n fori(i,1,d){\n\n ll x=min(m,n/(k*(i-1)+1));\n \n ll s=x*i;\n if(x>0){\n if(max\n#define ll long long\n#define ld long double\n#define OO 4e18\n#define oo 2e9\n#define yalla ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);\n#define FILES freopen(\"input.txt\", \"r\", stdin); freopen(\"output.txt\", \"w\", stdout);\n#define sz 100005\n#define re return\n#define F first\n#define S second\n#define pll pair\n#define mod 998244353\n#define pi acos(-1)\nusing namespace std;\n#include \n#include \nusing namespace __gnu_pbds;\n#define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update>\ntemplate using ordered_class_set = tree,rb_tree_tag,tree_order_statistics_node_update>;\nstring s,tmp=\"9223372036854775807\";\nll n;\n///\nint main()\n{\n yalla;\n cin>>s;\n if(s.size()<=18||(s.size()==19&&s[0]=='-') )\n {n=stoll(s);\n if(n>=-128&&n<=127)cout<<\"byte\"<=-32768&&n<=32767)cout<<\"short\"<=-2147483648&&n<=2147483647)cout<<\"int\"<=-9223372036854775808 &&n<=9223372036854775807)cout<<\"long\"<tmp[a])\n cout<<\"BigInteger\",exit(0);\n else if(s[i]\n#include\nusing namespace std;\nstring s[5];\nint f[5];\nint main()\n{\n cin>>s[1]>>s[2]>>s[3];\n if(s[1][0]=='r')++f[1];\n else if(s[1][0]=='p')++f[2];\n else ++f[3];\n if(s[2][0]=='r')++f[1];\n else if(s[2][0]=='p')++f[2];\n else ++f[3];\n if(s[3][0]=='r')++f[1];\n else if(s[3][0]=='p')++f[2];\n else ++f[3];\n if((f[1]==1&&f[3]==2)||(f[2]==1&&f[1]==2)||(f[3]==1&&f[2]==2))\n {\n if(s[1]==s[2])cout<<'S';\n else if(s[2]==s[3])cout<<'F';\n else cout<<'M';\n }\n else cout<<'?';\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "224ea038f9339bcabdc678637bcfa1a2", "src_uid": "072c7d29a1b338609a72ab6b73988282", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint main(){\n long long n;\n //cin>>n;\n scanf(\"%lld\",&n);\n if(n==0) printf(\"0\");\n else {\n if((n+1)%2==0){\n //cout<<(long)(n+1)/2;\n printf(\"%lld\",(n+1)/2);\n }\n else \n //cout<<(long)n+1;\n printf(\"%lld\",n+1);\n }\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "36d3bbebfae1140c70f6e32f9680f18d", "src_uid": "236177ff30dafe68295b5d33dc501828", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define xx first\n#define yy second\nusing namespace std;\ntypedef pair pii;\ntypedef vector O;\nint a[50][50],vis[2510],n,m;\npii b[2510],B[10100];\nvectorW;\nint go(O v,int flg=0){\n\tif(flg==0)for(auto c:v)if(vis[c])return 0;\n//\tputs(\"OK\");\n\tif(v.size()<4)exit(-1);\n\tfor(int i=0;i=0;--i){\n\t\tint to=(i==0?v.size()-1:i-1);\n\t\ta[B[i].xx][B[i].yy]=v[to];\n\t\tb[v[to]]=B[i];\n\t}\n\tW.push_back(v);\n\treturn 1;\n}\nvoid print(){\n\tprintf(\"%d\",W.size());\n\tfor(auto c:W){\n\t\tprintf(\"\\n%d \",c.size());\n\t\tfor(auto d:c)printf(\"%d \",d);\n\t}\n}\nvoid oak(vector tree){\n\tif(tree.size()!=6)exit(-1);\n\tgo(O{tree[0],tree[3],tree[4],tree[5],tree[2],tree[1]},1);\n\tgo(O{tree[2],tree[5],tree[4],tree[3]},1);\n\tgo(O{tree[3],tree[4],tree[0],tree[1]},1);\n}\nvoid fk(int x,int y,int x2,int y2,int dx,int dy){\n\tvector wxh;\n\tfor(int i=0;i<3;++i)wxh.push_back(a[x+dx*i][y+dy*i]);\n\tfor(int i=0;i<3;++i)wxh.push_back(a[x2+dx*i][y2+dy*i]);\n\toak(wxh);\n}\nvoid swp(int x,int y,int nx,int ny){\n\tif(x>nx)swap(x,nx);\n\tif(y>ny)swap(y,ny);\n//\tputs(\"OK\");\n\tif(x==nx){\n\t\tint x1=0,x2=0;\n\t\tif(nx==1)x1=x+1,x2=x;\n\t\telse x1=x-1,x2=x;\n\t\tif(ny==m)fk(x1,ny,x2,ny,0,-1);\n\t\telse fk(x1,y,x2,y,0,1);\n\t} else {\n\t\tint y1=0,y2=0;\n\t\tif(ny==1)y1=y+1,y2=y;\n\t\telse y1=y-1,y2=y;\n\t\tif(nx==n)fk(nx,y1,nx,y2,-1,0);\n\t\telse fk(x,y1,x,y2,1,0);\n\t}\n}\nvoid walk(int x,int y,int nx,int ny){\n\twhile(x!=nx||y!=ny){\n//\t\tprintf(\"[%d,%d]\",x,y);\n\t\tif(x!=nx&&!vis[a[(x>nx?x-1:x+1)][y]]){\n\t\t\tint d=(x>nx?-1:1);\n\t\t\tif(y!=m){\n\t\t\t\tif(!go(O{a[x][y],a[x+d][y],a[x+d][y+1],a[x][y+1]}))\n\t\t\t\t\tswp(x,y,x+d,y);\n\t\t\t} else {\n\t\t\t\tif(!go(O{a[x][y],a[x+d][y],a[x+d][y-1],a[x][y-1]}))\n\t\t\t\t\tswp(x,y,x+d,y);\n\t\t\t}\n\t\t\tx+=d; \n\t\t} else if(y!=ny&&!vis[a[x][(y>ny?y-1:y+1)]]){\n\t\t\tint d=(y>ny?-1:1);\n\t\t\tif(x!=n){\n\t\t\t\tif(!go(O{a[x][y],a[x][y+d],a[x+1][y+d],a[x+1][y]}))\n\t\t\t\t\tswp(x,y,x,y+d);\n\t\t\t} else {\n\t\t\t\tif(!go(O{a[x][y],a[x][y+d],a[x-1][y+d],a[x-1][y]}))\n\t\t\t\t\tswp(x,y,x,y+d);\n\t\t\t}\n\t\t\ty+=d;\n\t\t} else exit(2);\n\t}\n}\nvoid printa(){\n\tfor(int i=1;i<=n;++i,puts(\"\"))\n\t\tfor(int j=1;j<=m;++j)\n\t\t\tprintf(\"%d \",a[i][j]);\n\tputs(\">>>\");\n}\nvoid chka(){\n\tfor(int i=1;i<=n;++i)\n\t\tfor(int j=1;j<=m;++j)\n\t\t\tif(a[i][j]!=(i-1)*m+j)puts(\"WA\"),exit(-1);\n\tputs(\"AC\");\n\tint sum=0;\n\tfor(auto c:W)sum+=c.size();\n\tprintf(\"[used:%d]\",sum);\n}\nint main(){\n\tscanf(\"%d%d\",&n,&m);\n\tfor(int i=1;i<=n;++i)\n\t\tfor(int j=1;j<=m;++j)\n\t\t\tscanf(\"%d\",&a[i][j]);\n\t/*\n\tn=20,m=20;\n\tvectorv;\n\tfor(int i=1;i<=n*m;++i)v.push_back(i);\n\treverse(v.begin(),v.end());\n\t//random_shuffle(v.begin(),v.end());\n\tfor(int i=1;i<=n;++i)\n\t\tfor(int j=1;j<=m;++j)\n\t\t\ta[i][j]=v[(i-1)*m+j-1];*/\n\tfor(int i=1;i<=n;++i)\n\t\tfor(int j=1;j<=m;++j)\n\t\t\tb[a[i][j]]=pii(i,j);\n\tfor(int i=1;i<=n;++i)\n\t\tfor(int j=1;j<=m;++j){\n\t\t\tint J=(i-1)*m+j;\n\t\t\twalk(b[J].xx,b[J].yy,i,j);\n//\t\t\tprinta();\n\t\t\tvis[J]=1;\n//\t\t\treturn 0;\n\t\t}\n//\tchka();\n\tprint();\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "78da6f86216b882cb5f4a91a53ec30f4", "src_uid": "476afa2d8208ec933617c97637b65aff", "difficulty": 3100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\nint h1,m1,h2,m2,dur;\nchar c;\n\nint main()\n{\n cin >> h1 >> c >> m1 >> h2 >> c >> m2;\n dur = (h2*60+m2)-(h1*60+m1);\n dur /= 2;\n int hx = (h1+((m1+dur)/60))%24;\n int mx = (m1+(dur%60))%60;\n if (hx<10)\n {\n cout << 0 << hx;\n } else {cout << hx;}\n cout << \":\";\n if (mx<10)\n {\n cout << 0 << mx;\n } else {cout << mx;}\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "3b925ca208e21e14a09f2bab04ac52a9", "src_uid": "f7a32a8325ce97c4c50ce3a5c282ec50", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nint main()\n{\n int n;\n scanf(\"%d\",&n);\n printf(\"%d %d\\n\",(n+1)/36,(n+1)%36/3);\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "b0070ee4a3278957a9fc9c6c5cb0924e", "src_uid": "5d4f38ffd1849862623325fdbe06cd00", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "\n#include\nusing namespace std;\n\n\nint main()\n{\n\tint arr[405][405] = { 0 };\n\tint x, y, n;\n\tlong long int res = 0;\n\tchar c;\n\tx = y = 202;\n\t++arr[202][202];\n\tcin >> n;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin >> c;\n\t\tif (c == 'U')\n\t\t\t++y;\n\t\tif (c == 'R')\n\t\t\t++x;\n\t\tif (c == 'D')\n\t\t\t--y;\n\t\tif (c == 'L')\n\t\t\t--x;\n\t\tres = res + arr[x][y];\n\t\t++arr[x][y];\n\t}\n\tcout << res << '\\n';\n\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "210a05cbbac1a1a9cde87abed5f19837", "src_uid": "7bd5521531950e2de9a7b0904353184d", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define ll long long int\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define FOR(x,to) for(x=0;x<(to);x++)\n#define FORR(x,arr) for(auto& x:arr)\n#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)\n#define ALL(a) (a.begin()),(a.end())\n#define ZERO(a) memset(a,0,sizeof(a))\n#define MINUS(a) memset(a,0xff,sizeof(a))\n#define SZ(X) ((int)(X).size())\n#define REP(I, N) for (int I = 0; I < (N); ++I)\n#define REPP(I, A, B) for (int I = (A); I < (B); ++I)\n#define FORS(I, S) for (int I = 0; S[I]; ++I)\n#define RS(X) scanf(\"%s\", (X))\n#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))\n#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())\n#define CASET int ___T; scanf(\"%d\", &___T); for(int cs=1;cs<=___T;cs++)\n#define MP make_pair\n#define PB push_back\n#define MS0(X) memset((X), 0, sizeof((X)))\n#define MS1(X) memset((X), -1, sizeof((X)))\n#define LEN(X) strlen(X)\n#define F first\n#define S second\n//-------------------------------------------------------\n// code by bulls_eye\nint main(){\n\tll a;\n\tcin>>a;\n\tll rem=a%4;\n\tif(rem==0) \n\tcout<<1<<\" A\";\n if(rem==1) \n\tcout<<0<<\" A\";\n if(rem==2) \n\tcout<<1<<\" B\";\n if(rem==3) \n\tcout<<2<<\" A\";\n \n\nreturn 0;\n}\n\n\n\n\n", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "10d8c108ca388b5c50638a7b68e216b8", "src_uid": "488e809bd0c55531b0b47f577996627e", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define ll long long int\nint main()\n{\n ll i,j,k,n,l,m,sum=0,cnt=0,index;\n cin>>n>>m;\n ll a[1000];\n for(i=1;i<=n;i++)\n {\n cin>>a[i];\n cnt+=a[i];\n }\n if(cnt<=m)\n {\n for(i=0;i ans;\n vector calculu;\n for(i=1;i<=n;i++)\n {\n sum=sum+a[i];\n if(summ)\n {\n break;\n }\n }\n //cout<<\"ghost\"<\n#include \n#include \n#include\nusing namespace std;\nconst int N=1010,mod=1000000007;\nchar s[N],c[N];\nint main()\n{\n scanf(\"%s%s\",s,c);\n int K,n;\n scanf(\"%d\",&K);\n if(K==0)\n {\n printf(\"%d\\n\",strcmp(s,c)==0);\n return 0;\n }\n\n n=strlen(s);\n\n long long a=0,b=1,f=-1;\n K--;\n while(K--)\n {\n a=b*(n-1);\n b=a+f;\n f*=-1;\n a%=mod;\n b%=mod;\n }\n int ans=0;\n if(strcmp(s,c)==0)ans+=a;\n\n for(int i=1;i=mod)ans-=mod;\n }\n\n printf(\"%d\\n\",ans);\n\n scanf(\" \");\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "2a1ab9a31deb1a58fe0bdeab8371aa55", "src_uid": "414000abf4345f08ede20798de29b9d4", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n\nusing namespace std;\n\n/**templates**/\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vl;\n\n#define si(x) scanf(\"%d\",&x)\n#define sl(x) scanf(\"%lld\",&x)\n#define sull(x) scanf(\"%llu\",&x)\n#define sf(x) scanf(\"%lf\",&x)\n#define ss(x) scanf(\" %s\",x)\n#define sc(x) scanf(\" %c\",&x)\n#define pi acos(-1.0)\n#define pb push_back\n#define mp make_pair\n#define aa first\n#define bb second\n#define sz(x) (ll)(x).size()\n#define cas printf(\"Case %lld: \",++t)\n#define casline printf(\"Case %lld:\\n\",++t)\n//#define cas cout<<\"Case \"<<++t<<\": \"\n//#define casline cout<<\"Case \"<<++t<<\":\"<\nusing namespace std;\nint dp[1111][1111][11],n,m,K,f[1111][1111][11],x,y,dps[2][1111],la,nw,cnt[1111],msk,ni,nj,nk,ans;\nbool mp[11][11];\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&m,&K);\n\tfor (int i=1;i<=m;i++)\n\t{\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tx--;y--;\n\t\tmp[x][y]=mp[y][x]=1;\n\t}\n\tfor (int i=0;i<(1<\n#include \n#include \nusing namespace std;\n\nint main()\n{\n\tint n;\n\tstring s;\n\tcin >> n >> s;\n\tfor (int i = 0; i < s.size(); i++)\n\t{\n\t\tif (s[i] == '0' || s[i] == '1')\n\t\t{\n\t\t\ts.erase(i, 1);\n\t\t\ti--;\n\t\t}\n\t\telse if (s[i] == '4')\n\t\t{\n\t\t\ts[i] = '2';\n\t\t\ts += \"23\";\n\t\t}\n\t\telse if (s[i] == '6')\n\t\t{\n\t\t\ts[i] = '5';\n\t\t\ts.push_back('3');\n\t\t}\n\t\telse if (s[i] == '8')\n\t\t{\n\t\t\ts[i] = '7';\n\t\t\ts += \"222\";\n\t\t}\n\t\telse if (s[i] == '9')\n\t\t{\n\t\t\ts[i] = '7';\n\t\t\ts += \"332\";\n\t\t}\n\t}\n\tsort(s.rbegin(), s.rend());\n\tcout << s;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "sortings", "greedy"], "code_uid": "2a452467d00a75beb4d862321f122720", "src_uid": "60dbfc7a65702ae8bd4a587db1e06398", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n \nint p[4],a,b,n,t;\nmain(){\n\tstd::cin>>p[0]>>p[1]>>p[2]>>p[3]>>a>>b;\n\twhile(b>=a)t+=(b%p[0]%p[1]%p[2]%p[3])==b,b--;\n\tstd::cout << t;\n}", "lang_cluster": "C++", "tags": ["implementation", "number theory"], "code_uid": "5bb4bf3315f1e004f0af1ed8773a1522", "src_uid": "63b9dc70e6ad83d89a487ffebe007b0a", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "Clang++17 Diagnostics", "source_code": "#include \n#include \n\nusing namespace std;\n\nint main()\n{\n string s1, s2;\n cin >> s1 >> s2;\n for (size_t i = 0; i < s1.length(); ++i)\n {\n if (s1[i] == s2[i])\n {\n s1[i] = '0';\n }\n else\n {\n s1[i] = '1';\n }\n }\n cout << s1 << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "3ca20581da75b5e77636c846993da493", "src_uid": "3714b7596a6b48ca5b7a346f60d90549", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\n#define de(x) cout << #x << \" = \" << x << endl\n#define dd(x) cout << #x << \" = \" << x << \" \"\n#define INF 0x3f3f3f3f\n#define LINF (long long)(0x3f3f3f3f3f3f3f3f)\n#define pb push_back\n#define mp make_pair\n\ntypedef long long ll;\n/*\ntypedef pair pii;\nconst int N = 20;\n\nint main() {\n\tfor ( int n = 1; n < N; n ++ ) {\n\t\tll ct = 0;\n\t\tmap m;\n\t\tfor ( int i = 0; i <= n; i ++ )\n\t\t\tfor ( int j = 0; j <= (n-i); j ++ )\n\t\t\t\tfor ( int k = 0; k <= (n-i-j); k ++ ) {\n\t\t\t\t\tint g = n-i-j-k;\n\t\t\t\t\tll nw = i + j*5 + k*10 + g*50;\n\t\t\t\t\tif ( m[nw] ) continue;\n\t\t\t\t\tm[nw] = 1;\n\t\t\t\t\tct ++;\n\t\t\t\t}\n\t\tprintf(\"n = %d, ans = %lld\\n\", n, ct);\n\t}\n\treturn 0;\n}\n*/\n\nll a[12] = {0,4,10,20,35,56,83,116,155,198,244,292};\nint main() {\n\tint n;\n\tscanf(\"%d\", &n);\n\tif ( n < 12 )\n\t\tprintf(\"%lld\\n\", a[n]);\n\telse {\n\t\tll dif = n - 11;\n\t\tprintf(\"%lld\\n\", a[11]+dif*49);\n\t}\n\treturn 0;\n}\n\t", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "combinatorics", "greedy"], "code_uid": "794fd8438c7331f07d32f7e87422ce56", "src_uid": "75ec99318736a8a1b62a8d51efd95355", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\n#include\n#include\nusing namespace std;\nint main()\n{ \n //char::iterator it;\n int n;\n string s[8]={\"vaporeon\", \"jolteon\", \"flareon\", \"espeon\", \"umbreon\", \"leafeon\", \"glaceon\", \"sylveon\"};\n char ch[9];\n\nint found;\n \nint i,j;\nscanf(\"%d\",&n);\n scanf(\"%s\",ch);\n\tfor( i=0;i=n) && (n==6))\n\tcout<<\"espeon\"<=n) && (n==8))\n\tcout<<\"vaporeon\"<=strlen(ch) && strlen(ch)==s[i].length())\n { cout<\n#include\n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main() {\n\t\n\tchar board[8][8] = { ' ' }; int rowW[64] = { 0 }; int colW[64] = { 0 }; int rowB[64] = { 0 }; int colB[64] = { 0 };\n\tint i = 0; int j = 0;\n\tfor (int q = 0; q < 8; q++){\n\t\tfor (int w = 0; w < 8; w++){\n\t\t\tcin>>board[q][w];\n\t\t\tif (board[q][w] == 'W'){ i++; rowW[i] = q; colW[i] = w; }\n\t\t\telse if (board[q][w] == 'B'){ j++; rowB[j] = q; colB[j] = w; }\n\t\t}\n\t}\n\tint W=0;\n\tint B=0;\n\tbool set = false;\n\tfor (int x = 1; x <= i; x++){\n\t\tfor (int y = 1; y <= j; y++){\n\t\t\tif (rowB[y] < rowW[x] && colB[y] == colW[x]){\n\t\t\t\tset = false; break;\n\t\t\t}\n\t\t\telse set = true;\n\t\t}\n\t\tif (set == true){\n\t\t\tW = rowW[x]; break;\n\t\t}\n\t}\n\n\tbool set1 = false;\n\tfor (int xx = j; xx >= 1; xx--){\n\t\tfor (int yy = 1; yy <= i; yy++){\n\t\t\tif (rowB[xx] < rowW[yy] && colB[xx] == colW[yy]){\n\t\t\t\tset1 = false; break;\n\t\t\t}\n\t\t\telse set1 = true;\n\t\t}\n\t\tif (set1 == true){\n\t\t\tB = rowB[xx]; break;\n\t\t}\n\t}\n\tW++; B++;\n\tW = W - 1;\n\tB = 8 - B;\n\n\tif (W == B)cout << \"A\" << endl;\n\telse if (W\n\nusing namespace std;\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n unsigned long long int x,y;\n cin>>x;\n\ny=x;\n unsigned long long int pro=1,j;\n\n for(unsigned long long int i=2;(i*i)<=y;i++)\n {\n if(x%i==0)\n {\n x/=i;\n while(x%i==0)\n {x/=i;pro*=i;}\n }\n }\n cout<\n\nusing namespace std;\n\nint main () {\n\tint n,x,y;\n\tcin>>n>>x>>y;\n\tn=n/2;\n\tif (x==n || x==n+1)\n\t\tif(y==n || y==n+1) {\n\t\t\tcout<<\"NO\";\n\t\t\treturn 0;\n\t\t}\n\tcout<<\"YES\";\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "1797941927c98cae29bfa5ba5fbac099", "src_uid": "dc891d57bcdad3108dcb4ccf9c798789", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint a,b,a1,b1,a2,b2,ans;\nint main(){\n\tscanf(\"%d%d\",&a,&b);\n\tfor(int l=1,r,k;l<=a+b;l=r+1){\n\t\tk=(a+b)/l,r=(a+b)/k;\n\t\ta1=(a+k)/(k+1),b1=(b+k)/(k+1);\n\t\ta2=a/k,b2=b/k;\n\t\tif(a1<=a2&&b1<=b2)\n\t\t\tans+=min(r,a2+b2)-max(l,a1+b1)+1;\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}\n/*\n933594493 374183811\n*/", "lang_cluster": "C++", "tags": ["math", "implementation", "binary search"], "code_uid": "5cc58ca5cb49ae732186fd046d0e0317", "src_uid": "0e6a204565fef118ea99d2fa1e378dd0", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "Clang++17 Diagnostics", "source_code": "#include \n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n\tint n,f=0,s=0;\n\n\tcin >> n;\n\n\tstring a;\n\n\tcin >> a;\n\n\tfor(int i=0;if)\n\n\t\tcout << \"YES\" << endl;\n\n\telse\n\n\t\tcout << \"NO\"<\n#include \n#include \n#include \n\nusing namespace std;\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout.tie(0);\n\n\tlong long x;\n\tcin >> x;\n\tx = x - 360 * (x / 360);\n\tif (x <= -180) {\n\t\tx = 360 + x;\n\t}\n\telse if (x > 180) {\n\t\tx = x - 360;\n\t}\n\tif (x >= -45 && x <= 45) {\n\t\tcout << 0;\n\t}\n\telse if (x > 45 && x <= 135) {\n\t\tcout << 1;\n\t}\n\telse if (x > 135 && x <= 180 || x >= -180 && x <= -135) {\n\t\tcout << 2;\n\t}\n\telse {\n\t\tcout << 3;\n\t}\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "geometry"], "code_uid": "5a9998144dcc6ec38f47f059b3d7a6ec", "src_uid": "509db9cb6156b692557ba874a09f150e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\n// adjust problem by problem\nconst double EPS=1e-8;\nconst double PI=acos(-1.0);\n#define REP(i,x,n) for(int i=x;i<(int)(n);++i)\n#define rep(i,n) REP(i,0,n)\n\n#ifdef __GNUC__\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\nint popcount(int n){return __builtin_popcount(n);}\nint popcount(ll n){return __builtin_popcountll(n);}\n#endif\n#ifndef __GNUC__\nint popcount(unsigned int n){int cnt=0;for(int i=0;i<32;i++)if((n>>i)&1)cnt++;return cnt;}\nint popcountll(unsigned ll n){int cnt=0;for(int i=0;i<64;i++)if((n>>i)&1)cnt++;return cnt;}\n#endif\ntemplateint SIZE(T a){return a.size();}\ntemplatestring IntToString(T num){string res;stringstream ss;ss<T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}\ntemplateT lcm(T a,T b){return a/gcd(a,b)*b;}\nll getTen(int a){return (a<=0)?1:(getTen(a-1)*10);}\nbool EQ(double a,double b){return abs(a-b) split(string str,char del){\n vector res;\n for(int i=0,s=0;i>N>>M;\n for(int i=0;i>cmds[i]>>ids[i];\n set tmp;\n set inits;\n set cnts;\n for(int i=0;i list=inits;\n int init_index=0;\n if(list.size()==0){\n list.insert(ids[0]);\n init_index++;\n }\n // list内の要素が候補となる\n set cand=list;\n for(int i=init_index;i res;\n for(int a : cand)res.push_back(a);\n for(int i=1;i<=N;i++)\n if(!cnts.count(i))res.push_back(i);\n sort(res.begin(),res.end());\n cout<0){\n for(int i=0;i<(int)res.size();i++)\n cout<\n#define LL long long\n#define rint register int\nusing namespace std;bool f1;\nint n,f[55][55][55][55];\nchar s[110][3];\nbool dfs(int l,int a,int b,int c){\n\tif(l==1)return 1;\n\tif(f[l][a][b][c])return 0;\n\tif(s[c][0]==s[b][0]||s[c][1]==s[b][1])if(dfs(l-1,l-3,a,c))return 1;\n\tif(l>3&&s[c][0]==s[l-3][0]||s[c][1]==s[l-3][1])if(dfs(l-1,c,a,b))return 1;\n\tf[l][a][b][c]=1;\n\treturn 0;\n}\nbool f2;int main(){\n\tcin>>n;\n\tfor(int i=1;i<=n;i++)scanf(\"%s\",&s[i]);\n\tif(dfs(n,n-2,n-1,n))puts(\"YES\");\n\telse puts(\"NO\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "dp"], "code_uid": "ba6d73ec1a3d9c3dfa6ca440e3ea8c43", "src_uid": "1805771e194d323edacf2526a1eb6768", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "\n/** Which of the favors of your Lord will you deny ? **/\n\n#include\nusing namespace std;\n\n#define LL long long\n#define PII pair\n#define PLL pair\n#define F first\n#define S second\n\n#define ALL(x) (x).begin(), (x).end()\n#define READ freopen(\"alu.txt\", \"r\", stdin)\n#define WRITE freopen(\"vorta.txt\", \"w\", stdout)\n\n#ifndef ONLINE_JUDGE\n#define DBG(x) cout << __LINE__ << \" says: \" << #x << \" = \" << (x) << endl\n#else\n#define DBG(x)\n#define endl \"\\n\"\n#endif\n\ntemplate\nostream &operator <<(ostream &os, pair&p);\ntemplate \nostream &operator <<(ostream &os, vector&v);\ntemplate \nostream &operator <<(ostream &os, set&v);\n\ninline void optimizeIO()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n}\n\nconst int nmax = 2e5+7;\n\ntemplate \nstring to_str(T x)\n{\n stringstream ss;\n ss<cost = {6,2,5,5,4,5,6,3,7,6};\n\nint solve(int x)\n{\n string s = to_str(x);\n int ans = 0;\n for(char c:s) ans += cost[c-'0'];\n return ans;\n}\n\nvoid solveTC()\n{\n int a,b;\n cin>>a>>b;\n\n int ans = 0;\n for(int i=a;i<=b;i++) ans += solve(i);\n\n cout<>tc;\n\n while(tc--)\n {\n solveTC();\n }\n\n return 0;\n}\n\n/**\n\n**/\n\ntemplate\nostream &operator <<(ostream &os, pair&p)\n{\n os<<\"{\"<\nostream &operator <<(ostream &os, vector&v)\n{\n os<<\"[ \";\n for(T i:v)\n {\n os<\nostream &operator <<(ostream &os, set&v)\n{\n os<<\"[ \";\n for(T i:v)\n {\n os<\nusing namespace std;\n#define cs const \n#define int long long\n#define low(i) (i&(-i))\n#define mid ((l+r)>>1)\n#define ls (now<<1)\n#define rs (now<<1|1)\n#define par pair\n#define mp make_pair\n#define fr first \n#define se second\n#define pb push_back\n#define cn(x) memset(x, 0, sizeof(x))\n#define rep(i, x, y) for(int i=x; i<=y; ++i)\n#define drep(i, x, y) for(int i=x; i>=y; --i)\n#define fore(i, x) for(int i=fir[x]; i; i=nex[i])\n#define trep(i, x, y) for(int i=x; i=y; i=i+(i&(-i)))\n#define tdrep(i, x) for(int i=x; i; i=i-(i&(-i)))\ncs int base = 233;\ncs int ff = 1e6+5;\ncs int mod = 998244353;\ncs int inf = 1e9+1;\nint tr[ff], sr[ff];\n\nvoid upd(int now) { tr[now] = tr[ls] + tr[rs]; }\nvoid build(int now, int l, int r)\n{\n\tif( l == r ) { tr[now] = sr[l]; return; }\n\tbuild(ls, l, mid); build(rs, r, mid); upd(now);\n}\nvoid modify(int now, int l, int r, int lc, int rc, int va)\n{\n\tif( lc > r || rc < l ) return;\n\tif( lc <= l && r <= rc ) { tr[now] += va; return; }\n\tmodify(ls, l, mid, lc, rc, va); modify(rs, mid+1, r, lc, rc, va); upd(now);\n}\nint query(int now, int l, int r, int lc, int rc)\n{\n\tif( lc > r || rc < l ) return 0;\n\tif( lc <= l && r <= rc ) return tr[now];\n\treturn query(ls, l, mid, lc, rc) + query(rs, mid+1, r, lc, rc);\n}\n\nint fir[ff], nex[ff], to[ff], tot;\nvoid add(int x, int y)\n{\n\tnex[++tot] = fir[x];\n\tfir[x] = tot;\n\tto[tot] = y;\n}\nint qsm(int a, int b, int c = 1)\n{\n\tfor(int i=1; b; b >>= 1, a = a * a % mod)\n\t\tif( b&1 ) c = c * a % mod; return c;\n}\nint getinv(int x) { return qsm(x, mod-2); }\nvoid init()\n{\n\tint n, m, L, R;\n\tcin >> n >> m >> L >> R;\n\tif( n * m % 2 == 1 ) cout << qsm( R-L+1, n * m ) % mod, exit(0);\n\tif( ( R - L + 1 ) % 2 == 1 ) cout << ( qsm( R-L+1, n * m ) + 1 ) * getinv(2) % mod;\n\telse cout << ( qsm( R-L+1, n * m ) ) * getinv(2) % mod;\n}\nsigned main()\n{\n\tint T = 1;\n//\tcin >> T;\n\twhile(T--) \n\t\tinit();\n} ", "lang_cluster": "C++", "tags": ["math", "matrices", "constructive algorithms", "combinatorics"], "code_uid": "a197a2959147267deeeb3d10ec11043a", "src_uid": "ded299fa1cd010822c60f2389a3ba1a3", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n\n#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\n#define test int t;cin >> t;while(t--)\n#define REP(i,a,n) for(int i=a;i\n#define pii pair\n#define vpii vector>\n\nint32_t main(){\n IOS;\n int yellow,blue;\n cin >> yellow >> blue;\n int x,y,z;\n cin >> x >> y >> z;\n int reqY=2*x+y;\n int reqB=y+3*z;\n reqY-=yellow;\n reqB-=blue;\n if(reqY<0 && reqB<0)\n cout << 0;\n else if(reqY>0 && reqB>0)\n cout << reqY + reqB;\n else\n cout << max(reqY,reqB);\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "a8da199a5cc2e5030945d2d04771ef50", "src_uid": "35202a4601a03d25e18dda1539c5beba", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\nusing namespace std;\nint n,f;\nint main(){\n\tscanf(\"%d\",&n);\n\tfor(int i=0;i*1234567<=n&&!f;i++)\n\tfor(int j=0;j*123456+i*1234567<=n&&!f;j++)\n\tif((n-j*123456-i*1234567)%1234==0) f=1;\n\tprintf(\"%s\",f?\"YES\":\"NO\");\n} ", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "ad9629d44a8c3949256b2177d0bc1497", "src_uid": "72d7e422a865cc1f85108500bdf2adf2", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint main()\n{\n\tstring now;\n\tcin>>now;\n\tint i;\n\tfor(i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n// #include \n// #include \n// #include \nusing namespace std;\n// const int dx[] = {0, 1, 0, -1};\n// const int dy[] = {1, 0, -1, 0};\n// const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};\n// const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};\n// const int dx[] = {2, 2, -2, -2, 1, 1, -1, -1};\n// const int dy[] = {-1, 1, 1, -1, 2, -2, 2, -2};\n#define all(cont) cont.begin(), cont.end()\n#define rall(cont) cont.end(), cont.begin()\n#define pb push_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define pf printf\n#define sf scanf\n#define fr(i, a, b) for(int i = a ; i < (int)b ; i++)\n#define frr(i, a, b) for(int i = a ; i >= (int)b ; i--)\n#define tr(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)\n#define trr(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)\n#define its(x) static_cast< ostringstream & >( ( ostringstream() << dec << x ) ).str()\n#define boost ios_base::sync_with_stdio(false)\n#define boost2 cin.tie(NULL)\n#define ln puts(\"\")\n#define mems(a) memset(a, 0, sizeof(a))\n#define has(cont, x) ((cont).find((x)) != (cont).end())\n#define sz(x) ((int)(x).size())\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vi;\ntypedef vector vb;\ntypedef vector vll;\nint on_bit(int x, int pos) {x |= (1 << pos); return x;}\nint off_bit(int x, int pos) {x &= ~(1 << pos); return x;}\nbool is_on_bit(int x, int pos) {return ((x & (1 << pos)) != 0);}\nint flip_bit(int x, int pos) {x ^= (1 << pos); return x;}\nint lsb(int x) {return x & (-x);}\nint on_bit_all(int x, int pos) {x = (1 << pos)-1; return x;}\nconst double EPS = 1e-9;\nconst double PI = 2*acos(0.0);\nconst int INF = 0x3f3f3f3f;\nconst ll LLINF = 1e18;\nconst int MOD = 1e9+7;\nint add(ll a, ll b){return ((a%MOD)+(b%MOD))%MOD;}\nint sub(ll a, ll b){return ((a%MOD)-(b%MOD))%MOD;}\nint mult(ll a, ll b){return ((a%MOD)*(b%MOD))%MOD;}\nll powmod(ll a,ll b) {ll res=1;assert(b>=0);a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}\nll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}\nll lcm(ll a, ll b){return a/gcd(a,b)*b;}\n\nll n;\n\nll stick_3(ll h){\n\treturn 3*h*(h-1)/2;\n}\n\nbool can(ll h){\n\tll sisa = n-stick_3(h)-h-h;\n\treturn (sisa>=0)&&(sisa%3==0);\n}\n\nint main(){\n\tsf(\"%lld\",&n);\n\tll r=1ll*1e6;\n\tll res=0;\n\tfr(i,1,r){\n\t\tres+=can(1ll*i);\n\t}\n\tpf(\"%lld\\n\",res);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy", "binary search", "brute force"], "code_uid": "4dc3da2257995d6de10ebbc2e69f8059", "src_uid": "ab4f9cb3bb0df6389a4128e9ff1207de", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 Diagnostics", "source_code": "#include \n#include \nusing namespace std;\n\nint main()\n{\n int x;\n int a, b, c;\n int t;\n scanf(\"%d\", &x);\n scanf(\"%d %d\", &a, &b);\n for(t = 0; a/10 != 7 && a%10 != 7 && b/10 != 7 && b%10 != 7; t++)\n {\n if(b < x)\n {\n a -= x/60;\n c = x%60;\n if(b < c)\n {\n a -= 1;\n b = (b+60-c)%60;\n }\n else b -= c;\n if(a < 0)\n {\n a = 24 + a;\n }\n }\n else b -= x;\n }\n printf(\"%d\\n\", t);\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "c47a3cc42254b71177062d75b43b1580", "src_uid": "5ecd569e02e0164a5da9ff549fca3ceb", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#define max 1000000\n#define pb push_back\n#define f(i,n) for(int i=0;i :: iterator i;\ntypedef long long int ll;\n\nusing namespace std;\n\nint main()\n{\n\tint aa[105];\n int n;\n cin >> n;\n double sum = 0;\n f(i,n)\n {\n \tcin >> aa[i];\n \tsum +=aa[i];\n }\n\tdouble res = sum /(double) n;\n\t//cout << \" res =\" << res << \" sum = \" << sum << endl;\n\tif(res==5 or (res >= (floor(res)+.5)&& ceil(res) == 5)) return cout << 0 << endl,0;\n\tsort(aa,aa+n);\n\tint count = 1;\n//\tf(i,n) cout << aa[i] << \" \";\n//\tcout << endl;\n\tf(i,n)\n\t{\n\t\t//cout << aa[i] << \" \";\n\t\tdouble sub = 5 - aa[i];\n\t\t//cout << res << endl;\n\t\tsum += sub;\n\t\tres = sum /(double) n;\n\t\t//cout << \"sum = \" << sum << \" div =\" << res << endl;\n\t\tif(res==5 or res >= (floor(res)+.5) && ceil(res) == 5) return cout << count << endl,0;\n\t\tcount ++;\n\t}\n\tcout << n << endl;\n\n}\n", "lang_cluster": "C++", "tags": ["sortings", "greedy"], "code_uid": "39cc06cfac88e6e3ae70747739659573", "src_uid": "715608282b27a0a25b66f08574a6d5bd", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include\nusing namespace std;\nint main() \n{\n int n;\n cin>>n;\n int count=0;\n for(int i=1;i<=n;i++)\n {\n int temp;\n cin>>temp;\n count+=temp;\n }\n int result=0;\n n++;\n for(int i=1;i<=5;i++)\n {\n if((((count+i)%n))!=1)\n result++;\n }\n cout<\nusing namespace std;\nint n, b=0,idx=-1;\nstring s;\n/*\n\n13\nWBBBBWWBWBBBW\n\n3\n4 1 3\n\n*/\nint main() {\n\tcin>>n;\n\tcin>>s;\n\tint arr[n];\n\tfor(int i=0; i\nusing namespace std;\nint main(void)\n{\n long double t,s,q,i,j,k,l,n;\n while(cin>>t>>s>>q)\n {\n n=0;\n while(s\nusing namespace std;\n\nmain(){\n int n, c = 0, i;\n bool t[601];\n cin >> n;\n int a[n];\n memset(t, false, 601);\n for (i = 0; i < n; i++){\n cin >> a[i];\n if (t[a[i]] != true && a[i] != 0){\n t[a[i]] = true;\n c++;\n }\n }\n cout << c;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "0408834389f7e0e23239c21bec00f60e", "src_uid": "3b520c15ea9a11b16129da30dcfb5161", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define FOR(i,a,b) for(int i=(a);i<=(int)(b);i++)\n#define REP(i,b) for(int i=0;i<(int)(b);i++)\n#define FORD(i,a,b) for(int i=(a);i>=(int)(b);i--)\n#define REPD(i,b) for(int i=(int)(b)-1;i>=0;i--)\n#define all(v) (v).begin(), (v).end()\n#define uniq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())\ntypedef long long ll;\ntemplate inline bool mini(T &a, T b) {return b inline bool maxi(T &a, T b) {return a> n;\n int TR = 0, TB = 0;\n REP(i,n) {\n cin >> C[i] >> R[i] >> B[i];\n TR += R[i], TB += B[i];\n }\n\n memset(f, -1, sizeof f);\n f[0][0] = 0;\n REP(i,1<\n#include \n#include \nusing namespace std;\n#define int long long\n\nsigned main() {\n//\tios_base::sync_with_stdio(0);\n//\tcin.tie(0);\n//\tcout.tie(0);\n\tint n1, n2, k1, k2;\n\tcin >> n1 >> n2 >> k1 >> k2;\n\tvector > > a(n1+1, vector >(n2+1, vector(2)));\n\ta[0][0][0] = a[0][0][1] = 1;\n\tfor (int i = 0; i <= min(k1, n1); i++) {\n\t\ta[i][0][1]=a[i][0][0] = 1;\n\t}\n\tfor (int i = 0; i <= min(k2, n2); i++) {\n\t\ta[0][i][1] = a[0][i][0] = 1;\n\t}\n\tfor (int i = 1; i <= n1; i++) {\n\t\tfor (int j = 1; j <= n2; j++) {\n\t\t\tfor (int k = 1; k <= k1; k++) {\n\t\t\t\tif (i - k >= 0) {\n\t\t\t\t\ta[i][j][0] += a[i - k][j][1];\n\t\t\t\t\ta[i][j][0] %= 100000000;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int k = 1; k <= k2; k++) {\n\t\t\t\tif (j - k >= 0) {\n\t\t\t\t\ta[i][j][1] += a[i][j - k][0];\n\t\t\t\t\ta[i][j][1] %= 100000000;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << ( a[n1][n2][1] + a[n1][n2][0] ) % 100000000;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "3e7a971353d968533819f646b8339d1c", "src_uid": "63aabef26fe008e4c6fc9336eb038289", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\nint clockWise(char source, char distination)\n{\n\tif (source == distination)\n\t\treturn 0;\n\tif(source < distination)\n\t\treturn distination - source;\n\treturn 'z' - source + distination - 'a'+1;\n}\n\nint antiClockWise(char source, char distination)\n{\n\tif(source == distination)\n\t\treturn 0;\n\tif(source < distination)\n\t\treturn source - 'a' + 'z' - distination + 1;\n\treturn source - distination;\n}\n\nint main()\n{\n\tint i, ret=0;\n\tstring s;\t\n\tcin>>s;\n\ts=\"a\"+s;\n\tfor(i=0;i\nusing namespace std;\nint main(){\nstd::ios::sync_with_stdio(0);\nint n;\ncin>>n;\nvectorv;\nv.push_back(\"Sheldon\");\nv.push_back(\"Leonard\");\nv.push_back(\"Penny\");\nv.push_back(\"Rajesh\");\nv.push_back(\"Howard\");\nint idx=1,i,ans=-1;\nif(n<=5)\n cout<\n#include \nusing namespace std;\n\nconst int mod = 1e9+7;\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tint n, p;\n\tcin >> n >> p;\n\n\tif (p <= 2) {\n\t\tcout << 0;\n\t\treturn 0;\n\t}\n\n\tvector > a;\n\ta.push_back(pair(2,0));\n\ta.push_back(pair(3,1));\n\n\tint i(0), j(1);\n\tlong long k(3), b, c;\n\twhile (k < p) {\n\t\tb = 3*a[i].first+2;\n\t\tc = 3*(a[j].first/2)+a[j].first%2;\n\t\tk = min(b,c);\n\t\tif (a[i].second != 0 && a[j].second != 0) {\n\t\t\tif (a.back().second == 0) a.back().first = k;\n\t\t\telse a.push_back(pair(k,0));\n\t\t}\n\t\telse if (a[i].second != 1 && a[j].second != 1) {\n\t\t\tif (a.back().second == 1) a.back().first = k;\n\t\t\telse a.push_back(pair(k,1));\n\t\t}\n\t\telse {\n\t\t\tif (a.back().second == 2) a.back().first = k;\n\t\t\telse a.push_back(pair(k,2));\n\t\t}\n\t\tif (b <= c) ++i;\n\t\tif (c <= b) ++j;\n\t}\n\n\tlong long nimble[4] = {0};\n\tfor (int i = a.size()-1; i > 0; --i)\n\t\tnimble[a[i].second] += (a[i].first-a[i-1].first)*(2*p-a[i].first-a[i-1].first-1)/2;\n\tnimble[a.back().second] -= (a.back().first-p)*(p-a.back().first-1)/2;\n\tnimble[0] += 2*p-3;\n\tfor (int i = 0; i < 4; ++i)\n\t\tnimble[i] %= mod;\n\t\n\tlong long tmp[4], ans[4] = {1, 0, 0, 0};\n\tfor (int i = n; i > 0; i /= 2) {\n\t\tif (i % 2) {\n\t\t\tfor (int j = 0; j < 4; ++j)\n\t\t\t\ttmp[j] = ans[j];\n\t\t\tfor (int j = 0; j < 4; ++j) {\n\t\t\t\tans[j] = 0;\n\t\t\t\tfor (int k = 0; k < 4; ++k)\n\t\t\t\t\tans[j] += tmp[k]*nimble[j^k];\n\t\t\t\tans[j] %= mod;\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < 4; ++j)\n\t\t\ttmp[j] = nimble[j];\n\t\tfor (int j = 0; j < 4; ++j) {\n\t\t\tnimble[j] = 0;\n\t \t\tfor (int k = 0; k < 4; ++k)\n\t\t\t\tnimble[j] += tmp[k]*tmp[j^k];\n\t\t\tnimble[j] %= mod;\n\t\t}\n\t}\n\tcout << (ans[1]+ans[2]+ans[3]) % mod << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "4a98e700bfae9f5fa6094d41f6fdff7a", "src_uid": "c03b6379e9d186874ac3d97c6968fbd0", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\nint n, m, mod;\ninline int mul(int x, int y) {return (ll) x * y % mod;}\ninline void add(int &x, int y) {y += x; x = y >= mod ? y - mod : y;}\ninline int inc(int x, int y) {x += y; return x >= mod ? x - mod : x;}\ninline int qpow(int x, int y)\n{\n\tint res = 1;\n\tfor (; y; y >>= 1, x = mul(x, x))\n\t\tif (y & 1) res = mul(res, x);\n\treturn res;\n}\nconst int maxn = 1005;\nint f[maxn][12][maxn >> 1], inv[maxn];\nint main()\n{\n\tcin >> n >> m >> mod;\n\tif (n <= 2) return puts(\"1\"), 0;\n\tfor (int i = 1; i <= n; i ++) inv[i] = qpow(i, mod - 2);\n\tfor (int i = 0; i <= n / 2; i ++) f[1][0][i] = 1;\n\tfor (int i = 2; i <= n; i ++)\n\t\tfor (int j = 1; j <= m; j ++)\t\n\t\t\tfor (int k = 1; k <= n / 2; k ++)\n\t\t\t{\n\t\t\t\tf[i][j][k] = f[i][j][k - 1];\n\t\t\t\tfor (int l = 1, coef = 1; l <= j && k * l <= i; l ++)\n\t\t\t\t{\n\t\t\t\t\tcoef = mul(coef, mul(f[k][k == 1 ? 0 : m - 1][k - 1] + l - 1, inv[l]));\n\t\t\t\t\tadd(f[i][j][k], mul(f[i - l * k][j - l][k - 1], coef));\n\t\t\t\t}\n\t\t\t}\n\tint ans = f[n][m][n >> 1];\n\tif (!(n & 1))\n\t{\n\t\tint v = f[n >> 1][m - 1][(n >> 1) - 1];\n\t\tadd(ans, mod - (ll) v * (v - 1) / 2ll % mod);\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "trees"], "code_uid": "752d7ec8e9d6583c5af9465df7b6a461", "src_uid": "bdd0fc1d6dbab5eeb5aea135fdfffc9d", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define ll long long int \n#define mp make_pair\n#define pb push_back\n#define mod 1000000007\n#define ff first\n#define ss second\n#define pll pair \nusing namespace std;\nll ocnt[10],cnt[10],present[10];\nll fct[22];\nll dcnt,ways;\nvoid cal(ll tno,ll idx)\n{\n\tll i;\n\t// cout<<\"tno =\"<1)\n\t\t{\n\t\t\tcnt[i]--;\n\t\t\tcal(tno-1,i);\n\t\t\tcnt[i]++;\n\t\t}\n\t}\n\treturn ;\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tll n;\n\tcin>>n;\n\twhile(n>0)\n\t{\n\t\tcnt[n%10]++;\n\t\tpresent[n%10]++;\n\t\tocnt[n%10]++;\n\t\tn/=10;\n\t\tdcnt++;\n\t}\n\tfct[0]=1;\n\tll i;\n\tfor(i=1;i<=19;i++)\n\t{\n\t\tfct[i]=fct[i-1]*i;\n\t}\n\tcal(dcnt,0);\n\tcout<\n\nusing namespace std;\n\ntypedef long long LL;\n\nconst LL MOD = 1000000007;\n\nint n,m;\nLL a[155][155],b[155][155],c[155][155],ans;\n\nint main()\n{\n\tscanf(\"%d%d\",&n,&m);\n\tif(mi;j--)\n\t\t\t\t(ans+=(c[i][j]+=c[i-1][j]+c[i][j+1]-c[i-1][j+1]+a[i-1][j+1]+b[i-1][j]+b[n-j-1][n-i])%=MOD)%=MOD;\n\t\tfor(int i=n-2;i>=0;i--)\n\t\t\tfor(int j=n-1;j>i;j--)\n\t\t\t\t(ans+=((b[i][j]+=b[i+1][j]+b[i][j+1]-b[i+1][j+1]+a[i][j+1])%=MOD)*2)%=MOD;\n\t\tfor(int i=n-1;i>=0;i--)\n\t\t\tfor(int j=i+1;j<=n;j++)\n\t\t\t\t(ans+=(a[i][j]+=a[i+1][j]+a[i][j-1]-a[i+1][j-1])%=MOD)%=MOD;\n\t}\n\tprintf(\"%d\\n\",int((ans+MOD)%MOD));\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "1a8d120bb573cd1a4daaae2eeba5c5e3", "src_uid": "740eceed59d3c6ac55c1bf9d3d4160c7", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _USE_MATH_DEFINES\n#define _CRT_SECURE_NO_DEPRECATE\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#pragma comment(linker, \"/STACK:64000000\")\n\ntemplate inline T sqr (T x) {return x * x;}\n\ntypedef long long int64;\ntypedef unsigned long long uint64;\ntypedef pair pii;\ntypedef pair pip;\ntypedef pair ppi;\ntypedef pair pii64;\ntypedef pair pdd;\ntypedef pair psi;\ntypedef pair pis;\n#define eps 1e-8\n#define inf 0x7f7f7f7f\n#define MP make_pair\n#define sfind(S, C) int((S).find(C))\n#define sz(C) (int)((C).size())\n#define all(C) (C).begin(), (C).end()\n#define TASK \"test\"\n#define RR 151\n\nint days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n\ninline bool check_valid (int d, int m, int y) {\n return y >= 1 && y <= 99 && m && m <= 12 && d && days[m - 1] + (!(y % 4) && m == 2) >= d;\n}\n\ninline bool check_18 (int d1, int m1, int y1, int d2, int m2, int y2) {\n int cnt = -1;\n for (int y = y1; y <= y2; ++y) {\n for (int m = y == y1 ? m1 : 1; m <= (y == y2 ? m2 : 12); ++m) {\n int lim = days[m - 1];\n if (y % 4 == 0 && m == 2) ++lim;\n for (int d = y == y1 && m == m1 ? d1 : 1; d <= (y == y2 && m == m2 ? d2 : lim); ++d) {\n if (d == d1 && m == m1 || m1 == 2 && d1 == 29 && y % 4 && m == 3 && d == 1) ++cnt;\n }\n }\n }\n return cnt >= 18;\n}\n\nbool solve (string a, string b) {\n int d, m, y;\n sscanf(a.c_str(), \"%d.%d.%d\", &d, &m, &y);\n int p[3];\n sscanf(b.c_str(), \"%d.%d.%d\", &p[0], &p[1], &p[2]);\n sort(p, p + 3);\n do {\n if (!check_valid(p[0], p[1], p[2])) continue;\n if (check_18(p[0], p[1], p[2], d, m, y)) return true;\n } while (next_permutation(p, p + 3));\n return false;\n}\n\nint main() {\n#ifndef ONLINE_JUDGE \n freopen(TASK \".in\", \"r\", stdin);\n freopen(TASK \".out\", \"w\", stdout);\n#endif\n\n string a, b;\n cin >> a >> b;\n puts(solve(a, b) ? \"YES\" : \"NO\");\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "160e6ad5207bb5e380bba3b6090a4d32", "src_uid": "5418c98fe362909f7b28f95225837d33", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include\n\nusing namespace std;\n\ntypedef long long int ll;\ntypedef long double ld;\ntypedef pair pt;\ntypedef pair pll;\n\n#define vint vector\n#define vll vector\n#define endl '\\n'\n#define fi first\n#define se second\n#define pb push_back\n#define F(i,n) for(ll i = 0;i=0;i--)\n#define Max 1000001\n#define intinf 2147483647\n#define longlonginf 9223372036854775807\n#define MOD 998244353\n#define MOD2 1000000007\n#define fastio ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)\n\n#define min(a,b) a>b?b:a\n#define max(a,b) a>b?a:b\n#define all(arr) arr.begin(),arr.end()\n//#define sort(vec) sort(vec.begin(),vec.end())\n//#define reverse(vec) reverse(vec.begin(),vec.end())\n\n\nll Lpow(ll a,ll b)\n{\n ll ans = 1;\n while(b > 0)\n {\n if(b%2)\n ans = ans * a;\n a = a*a;\n b = b/2;\n }\n return ans;\n}\n\nsigned main()\n{\n int n;\n cin >> n;\n \n vector arr(2*n);\n \n F(i,2*n)\n cin >> arr[i];\n \n sort(arr.begin(),arr.end());\n \n ll ans = intinf;\n ll first,second;\n for(int i = 0;i<2*n;i++)\n {\n for(int j = i+1;j<2*n;j++)\n {\n ll temp = 0;\n for(int l = 0;l<2*n;)\n {\n while(l == i || l == j)\n l++;\n if(l >= 2*n)\n break;\n first = l;\n l++;\n while(l == i || l == j)\n l++;\n if(l >= 2*n)\n break;\n second = l;\n l++;\n temp += arr[second] - arr[first];\n }\n \n ans = min(ans,temp);\n }\n }\n \n cout << ans << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "sortings", "greedy"], "code_uid": "54583086d4e115cf50af6aaf7b41284b", "src_uid": "76659c0b7134416452585c391daadb16", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include\nusing namespace std;\ntypedef long long ll;\nll arr[15];\nll tmp[15];\nvoid Copy(){\n\tfor(int i=1;i<15;i++)\n\ttmp [i] = arr[i];\n}\n////////////\nint main() {\n\tfor(int i=1;i<=14;i++) cin>>arr[i];\n\tll mx = 0;\n\tfor(int i=1;i<15;i++){\n\t\tCopy();\n\t\tll x = arr[i];\n\t\tif(x==0)continue;\n\t\ttmp[i] = 0;\n\t\tfor(int j=i+1;j<15;j++)\n\t\tif(x!=0)\n\t\ttmp[j]++,x--;\n\t\tif(x!=0)\n\t\tfor(int j=1;j<15;j++)\n\t\ttmp[j]+=x/14;\n\t\tx%=14;\n\t\tif(x!=0){\n\t\tfor(int j=1;j<15;j++)\n\t\tif(x!=0)\n\t\ttmp[j]++,x--;\n\t\t}\n\t\tll cnt = 0;\n\t\tfor(int j=1;j<15;j++)\n\t\tif(tmp[j]%2==0 && tmp[j]>0)\n\t\tcnt+=tmp[j];\n\t\tmx = max(mx,cnt);\n\t}\n\tcout<\nusing namespace std;\n#define ll long long int\n#define mp make_pair\n#define pb push_back\n#define si(x) scanf(\"%d\",&x)\n#define pi(x) printf(\"%d\\n\",x)\n#define s(x) scanf(\"%I64d\",&x)\n#define p(x) printf(\"%I64d\\n\",x)\n#define sc(x) scanf(\"%s\",x)\n#define pc(x) printf(\"%s\",x)\n#define pii pair\n#define pll pair\n#define F first\n#define S second\n#define M 1000000007\n#define N 100005\n#define inf 2e9\n#define PQG priority_queue< int,std::vector,std::greater >\n#define PQL priority_queue< int,std::vector,std::less >\n#define PQPL priority_queue, less< pii > >\n#define PQPG priority_queue, greater< pii > >\n#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)\n\nint main()\n{\n int n;\n cin>>n;\n string s;\n cin>>s;\n int cnt=(s[0]=='?');\n for(int i=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef vector VI;\ntypedef vector VS;\ntypedef set SI;\ntypedef set SS;\ntypedef long long ll;\ntypedef unsigned long long ull;\n\n#define REP(i,n) for(int i=0;i<(n);++i)\n#define DREP(i,n) for(int i=(n)-1;i>=0;--i)\n#define FOR(i,n,m) for(int i=(n);i<(m);++i)\n#define DFOR(i,n,m) for(int i=(n);i>=(m);--i)\n#define FOREACH(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define LOOP for(;;)\n#define zero(n) memset((n),0,sizeof(n))\n#define RMB(x) (x).erase((x).begin())\n#define RME(x) (x).pop_back()\n#define SORT(x) sort((x).begin(),(x).end())\n#define REVERSE(x) reverse((x).begin(),(x).end())\n#define PB push_back\n#define ISS istringstream\n#define OSS ostringstream\n\n\nint n,m,x;\nint an, am, len;\nint ans = 0;\n\n\nint main ( void )\n{\n\tcin>>n>>m>>x;\n\n\tif(x==0){\n\t\tcout<<(n*m)/2<\nusing namespace std;\nint arr[110];\nint main()\n{\n int n;\n cin>>n;\n int cnt=0;\n for(int i=1;i<=n;i++)\n {\n cin>>arr[i];\n if(arr[i]==0)cnt++;\n else if(arr[i]==arr[i-1]&&arr[i-1]!=3)\n {\n arr[i]=0;\n cnt++;\n }\n else if(arr[i]==3)arr[i]-=arr[i-1];\n }\n cout<\n#define __ ios_base::sync_with_stdio(0);cin.tie(0);\nusing namespace std;\ntypedef long long ll;\n\nint mod=1e9+7;\nll mem[50][50][10005],n,k,ans=1,inv[51];\n\nll bpw(ll b,ll p){\n\tll ans=1;\n\twhile(p){\n\t\tif(p&1)ans=ans*b%mod;\n\t\tb=b*b%mod;\n\t\tp/=2;\n\t}\n\treturn ans;\n}\n\nint main(){__\n\tfor(int i=1;i<=50;i++)\n\t\tinv[i]=bpw(i,mod-2);\n\tfor(int i=0;i<50;i++)\n\t\tmem[i][i][0]=1;\n\tfor(int k=1;k<=10000;k++)\n\t\tfor (int to=0;to<50;to++){\n\t\t\tll s=0;\n\t\t\tfor(int from=0;from<50;from++){\n\t\t\t\ts=s*(from)%mod;\n\t\t\t\ts+=mem[from][to][k-1];\n\t\t\t\ts=s*inv[from+1]%mod;\n\t\t\t\tmem[from][to][k]=s;\n\t\t\t}\n\t\t}\n\tcin>>n>>k;\n\tfor(ll i=2;i*i<=n;i++)\n\t\tif(n%i==0){\n\t\t\tll pw=0,x=0;\n\t\t\twhile(n%i==0)\n\t\t\t\tn/=i,pw++;\n\t\t\tfor(ll j=0,l=1;j<=pw;j++,l=l*i%mod)\n\t\t\t\tx+=mem[pw][j][k]*l%mod;\n\t\t\tx%=mod;\n\t\t\tans=ans*x%mod;\n\t\t}\n\tif(n>1){\n\t\tll x=(mem[1][0][k]+(n%mod)*mem[1][1][k])%mod;\n\t\tans=ans*x%mod;\n\t}\n\tcout<\n\nusing namespace std;\n#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)\n#define ll long long int\n#define ull unsigned long long int\n#define veci vector\n#define vecc vector\n#define vecs vector\n#define vecl vector\n#define vecp vector< pair >\n#define mapstrint map< string , int >\n#define mapstrstr map< string , string >\n#define mapint map< int, int >\n#define pb push_back\n#define newline cerr << endl\n#define pob pop_back\n#define mp make_pair\n#define pii pair\n#define F first\n#define S second\n#define sc(x) scanf(\"%d\",&x)\n#define sc2(xx,zz) scanf(\"%d %d\",&xx,&zz)\n#define scl(x) scanf(\"%lld\",&x)\n#define scl2(xx,zz) scanf(\"%lld %lld\",&xx,&zz)\n#define pf printf\n#define min3(a,b,c) min(a,bc?b:c)\n#define all(v) v.begin(), v.end()\n///=============== Debugging ============================///\n#define debug(x) cerr << #x << \" = \" << x << endl\n#define debug2(x, y) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << endl;\n#define debug3(x, y, z) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << \" | \" << #z << \": \" << z << endl;\n#define debug4(a, b, c, d) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << endl;\n//#define f(x) (64 - __builtin_clzll(x))\n///============ CONSTANT ===============///\n#define mx9 1000000007\n#define mx7 10000007\n#define mx6 2000006\n#define mx5 200005\n#define inf 1<<30 //infinity value\n#define eps 1e-9\n#define mod mx9\n///==============================TEMPLATES===============================///\n\nlong double PI = acosl(-1);\nbool compare_int(int a, int b){return (a>b);} //This function is use for descending order.\nbool compare_string (string a, string b){return a.size() &a,\n const pair &b){if(a.second==b.second)return a.first b.second);}\nbool cmp(pair x , pair y){return (x.S < y.S);}\n///=====================Graph Moves================///\n//const int fx[]= {+1,-1,+0,+0};\n//const int fy[]= {+0,+0,+1,-1};\n//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move\n//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move\n//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move\n//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move\n\n///=====================Bitmask===================///\n//int Set(int N,int pos){return N=N | (1<> s;\n ull num = 0;\n num = (num*10)+(s[0]-'0');\n num = (num*10)+(s[2]-'0');\n num = (num*10)+(s[4]-'0');\n num = (num*10)+(s[3]-'0');\n num = (num*10)+(s[1]-'0');\n //debug(num);\n //debug(num*num);\n //debug(num*num*num);\n //debug(num*num*num*num);\n\n ll tot, pre, last, po;\n tot = num*num*num;\n pre = tot;\n last=0, po=1;\n for(int i=1; i<=5; i++)\n {\n last += ((pre%10)*po);\n pre/=10;\n po*=10;\n }\n\n //debug(last);\n pre = last*num;\n last=0, po=1;\n for(int i=1; i<=5; i++)\n {\n last += ((pre%10)*po);\n pre/=10;\n po*=10;\n }\n //debug(last);\n pre = last*num;\n last=0, po=1;\n for(int i=1; i<=5; i++)\n {\n last += ((pre%10)*po);\n pre/=10;\n po*=10;\n }\n pre = last, last = 0, po=1;\n for(int i=1; i<=5; i++)\n {\n last += ((pre%10)*po);\n pre/=10;\n po*=10;\n }\n if(last==0) cout << \"00000\" << endl;\n else cout << last << endl;\n\n}\n///Created:\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "697c8040e0ae8b447d9438df325024ad", "src_uid": "51b1c216948663fff721c28d131bf18f", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n,k;\n\tcin>>n>>k;\n\tif(k==0||k==n)\n\t{\n\t\tcout<<0<<\" \"<<0;\n\t}\n\telse if(n%3==0)\n\t{\n\t\tif(k<=n/3)\n\t\t{\n\t\t\tcout<<1<<\" \"<<2*k;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout<<1<<\" \"<\n#define LL long long\n#define ull unsigned long long\n#define F(i,j,k) for(int i=(j);i<=(k);i++)\n#define DF(i,j,k) for(int i=(j);i>=(k);i--)\n#define P pair\n#define dui priority_queue\nusing namespace std;\ntemplateinline void read(T &n){\n T w=1;n=0;char ch=getchar();\n while(!isdigit(ch)&&ch!=EOF){if(ch=='-')w=-1;ch=getchar();}\n while(isdigit(ch)&&ch!=EOF){n=(n<<1)+(n<<3)+(ch^48);ch=getchar();}\n n*=w;\n}\ntemplateinline void write(T x){\n ull y=0;\n T l=0;\n if(x<0){x=-x;putchar('-');}\n if(!x){putchar(48);return;}\n while(x){y=y*10+x%10;x/=10;l++;}\n while(l){putchar(y%10+48);y/=10;l--;}\n}\ntemplateinline void writeln(T x){\n write(x);\n puts(\"\");\n}\ntemplateinline void writes(T x){\n write(x);\n putchar(' ');\n}\ntemplateinline void checkmax(T &a,T b){a=a>b?a:b;}\ntemplateinline void checkmin(T &a,T b){a=a0){\n\t\t\tLL v,w;\n\t\t\tif(mx-g<0){v=mx*ten[i]*3;w=mx*F[i];}\n\t\t\telse {v=g*ten[i]*3;w=g*F[i];}\n\t\t\tmx-=g;\n\t\t\tDF(j,999999,1){if(j-v<0)break;checkmax(f[j],f[j-v]+w);}\n\t\t\tg<<=1;\n\t\t}\n\t}\n\tF(i,0,5)\n\t\tDF(j,999999,1)\n\t\t\tF(k,1,9){\n\t\t\t\tif(j-ten[i]*k<0)break;\n\t\t\t\tcheckmax(f[j],f[j-ten[i]*k]+F[i]*c[k]);\n\t\t\t}\n\tint q;read(q);\n\tF(i,1,q){int x;read(x);writeln(f[x]);}\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "dp"], "code_uid": "f9ca4feb3f004c9824875c0bdbd7cf3b", "src_uid": "92bcbac3f167a44c235e99afc4de20d2", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n#define ll long long\n#define all(v) v.begin(),v.end()\n//#define for(i,a,b) for(ll i=a;iv;\nint a,b;\ncin>>a>>b;\n if(a==0 && b==0){\n cout<<\"NO\";return 0;\n }\n\n if(a==b || b==a+1 || a==b+1)cout<<\"YES\";\n else cout<<\"NO\";\n\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "constructive algorithms", "implementation"], "code_uid": "6fe48680f95e5d7bb7d7a67292df7b5a", "src_uid": "ec5e3b3f5ee6a13eaf01b9a9a66ff037", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nchar s[110];\nchar ans[110];\n\nint main(){\n scanf(\"%s\",s);\n int l = strlen(s);\n int id = 0;\n int flag = 1;\n ans[id++] = s[0];\n for(int i=1;i\nusing namespace std;\ntypedef long long int ll;\nint main()\n{\n ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);\n ll n,m,k=1,l=1,sum=0,cnt=0,i=0,r,f=0,z=0;\nstring s,s1;\ncin>>s;\nwhile(i\n#include \nusing namespace std;\nint main () {\n\tstring s;\n\tgetline(cin,s);\n\tbool at = false;\n\tint i;\n\tfor (i = 0; i < s.size(); i++) {\n\t\tif (s[i] == '@') at = true;\n\t}\n\t\n\tcout<\n#include\n#include\nusing namespace std;\nint main(){\n string s;cin>>s;\n string ans;ans+=s[0];\n int k=0;\n for(int i=1;i\n#include\n#include\nusing namespace std;\nint main()\n{\nint i,flag=0,l;char a[101];\nscanf(\"%s\",&a);l=strlen(a);\ncout<\n#include \n#include \n\nusing namespace std;\n\nint n, a[100];\nstring s;\n\nint main()\n{\n cin >> s;\n for(int i=0; i<100; i++) a[i]=0;\n for(int i=1; i\nusing namespace std;\n\nstring s;\nstring ans;\n\nint main()\n{\n cin >> s;\n bool t = false;\n s += \"###\";\n for(int i = 0; i < s.size() - 3; i++)\n {\n if(ans == \"\")\n {\n ans += s[i];\n continue;\n }\n if(s[i] == 'd' && s[i + 1] == 'o' && s[i + 2] == 't')\n {\n ans += '.';\n i += 2;\n }\n else if(s[i] == 'a' && s[i + 1] == 't' && t == false)\n {\n t = true;\n ans += '@';\n i += 1;\n }\n else\n {\n ans += s[i];\n }\n }\n if(ans[ans.size() - 1] == '.')\n {\n ans[ans.size() - 1] = 'd';\n ans += \"ot\";\n }\n cout << ans << endl;\n}\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "819bfcf32bef5bd28f8e528f14eda02b", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nstring a, b, ta;\nint n, atPos;\nint main(){\n cin>>a;\n n=a.size()-1;\n for(int i=1; i<=n; i++){\n if(a[i]=='a'&&a[i+1]=='t'){\n atPos=i;\n break;\n }\n }\n ta+=a[0];\n for(int i=1; i\n#include \n#include \n#include \n#include \n\nusing namespace std;\ntypedef long long ll;\n\nint main(int argc, char** argv) \n{\n\tint ans=-1;\n\tstring s,res;\n\tcin>>s;\n\tres+=s[0];\n\tfor(int i=1;i\n#include\nusing namespace std;\n\nint main()\n{\n string x;\n bool aat=true;\n cin>>x;\n string y;\n y+=x[0];\n for(int i=1;i\nusing namespace std;\n#define rep(flag,a,b) for(int flag=(a);flag<=b;flag++)\n#define Rep(flag,a,b) for(int flag=(a);flag>=b;flag--)\n#define mem(flag) memset(flag,0,sizeof(flag))\n#define Mem(flag) memset(flag,-1,sizeof(flag))\n#define read(a) a=getInt()\n#define En printf(\"\\n\")\n#define P(a) printf(\"%d \",a)\n#define Pn(a) printf(\"%d\\n\",a)\n#define dbg(x) cout << (#x) << \" = \" << (x) << endl\n#define W(a) while(a)\n#define lowbit(x) (x&-x) \n#define F .first\n#define S .second\n#define inf 0x3f3f3f3f\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pi;\ntypedef pair pd;\ntypedef vector vpi;\ntypedef vector vpd;\nconst int MAX=12;\nconst int eps=1e-9;\ninline const int getInt() {\n\tint res=0,ch,flag=0;\n if((ch=getchar())=='-')\n flag=1;\n else if(ch>='0'&&ch<='9')\n res=ch-'0';\n while((ch=getchar())>='0'&&ch<='9')\n res=res*10+ch-'0';\n return flag?-res:res;\n}\ninline void printInt(int a) {\n if(a>9)\n printInt(a/10);\n putchar(a%10+'0');\n}\n\n#define DEBUG\n\nchar buf[200];\nvoid sol() {\n int len=strlen(buf);\n int i=0; bool at=false;\n while(i<=len-1) {\n if(i==0||i==len-2||i==len-3) {\n if(!at&&i==len-3&&buf[i]=='a'&&buf[i+1]=='t') {\n printf(\"@\"); i+=2; at=true;\n } else {\n printf(\"%c\",buf[i]); i++;\n }\n } else {\n if(buf[i]=='a'&&!at) {\n if(i+1<=len-1&&buf[i+1]=='t') {\n printf(\"@\"); i+=2; at=true;\n } else {\n printf(\"%c\",buf[i]); i++;\n }\n } else if(buf[i]=='d') {\n if(i+2<=len-1&&buf[i+1]=='o'&&buf[i+2]=='t') {\n printf(\".\"); i+=3;\n } else {\n printf(\"%c\",buf[i]); i++;\n }\n } else {\n printf(\"%c\",buf[i]); i++;\n }\n }\n }\n}\n\nint main() {\n #ifdef DEBUG\n //freopen(\"./input.txt\",\"r\",stdin);\n //freopen(\"./output.txt\",\"w\",stdout);\n int cases=1;\n while(cases--) {\n #endif\n //printf(\"%d %d\\n\",'@','a');\n scanf(\"%s\",buf);\n sol();\n #ifdef DEBUG\n }\n #endif\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "08f8b576375197f9b44510af5a1845c4", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define sz(x) ((ll)(x).size())\n#define init(x, a) memset(x, a, sizeof(x))\n#define forr(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)\n#define rfor(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)\n#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pll;\ntypedef vector vll;\ntypedef set sll;\ntypedef unordered_set usll;\ntypedef map mll;\ntypedef unordered_map umll;\n//const ll mod = 1e6+3;\n//const ld PI=3.14159265;\nll gcd(ll a,ll b) { return a?gcd(b%a,a):b;}\n\nint main()\n{\n fastio;\n int r=0,k=0;\n string s,ans=\"\";\n cin>>s;\n ans+=s[0];\n forr(i,1,sz(s))\n {\n r=0;\n if(s[i]=='d'&& i+3\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define ll long long int\n#define e() cout << endl;\n#define MA INT_MAX\n#define MI INT_MIN\n#define MOD 1000000007;\n#define ppi pair \n#define scn(n) scanf(\"%d\",&n)\n#define vvi(n,m,x) vector > v(n,vector (m,x));\n#define cp(x,y) cout << \"Case : \" << x << \" \" << y << endl;\nusing namespace std;\nint main()\n{\n\tstring s,x = \"\";\n\tbool ok = true;\n\tcin >> s;\n\tfor(int i = 0;i < s.size();)\n\t{\n\t\tif(s.substr(i,3) == \"dot\")\n\t\t\tx += '.',i += 3;\n\t\telse if(s.substr(i,2) == \"at\" && ok && i) \n\t\t\tx += '@',i += 2,ok = false;\n\t\telse\n\t\t\tx += s[i],i++;\n\t}\n\tfor(int i = 0;i < x.size();i++)\n\t\tif(i == 0 && x[i] == '.')\n\t\t\tcout << \"dot\";\n\t\telse if(i == 0 && x[i] == '@')\n\t\t\tcout << \"at\";\n\t\telse if(i == x.size() - 1 && x.back() == '@')\n\t\t\tcout << \"at\";\n\t\telse if(i == x.size() - 1 && x.back() == '.')\n\t\t\tcout << \"dot\";\n\t\telse\n\t\t\tcout << x[i];\n\te();\n\treturn 0;\t\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "9807516a88bca04a44cc29d3b6557735", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\nID:wmj20041\nTASK:\nLANG:C++\n*/\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ll long long\n#define pb push_back\n#define pob pop_back\n#define mp make_pair\n#define FILL(a,v) memset((void*)a,v,sizeof(a))\n#define FILL0(a) FILL(a,0)\n#define FILL1(a) FILL(a,-1)\n#define FILLBIG(a) FILL(a,0x7f)\n#define ALL(a) (a).begin(),(a).end()\n#define Inf ((int)0x7f7f7f7f)\n#define Mod 1000000007\n#define foreach(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)\n#define FOR(i,x) for(int i=0;i=0;i--)\n#define FFOR(i,a,b) for(int i=a;i=b;i--)\n#define SZ(a) ((int)(a).size())\n#define fi first\n#define se second\n#define OVER(a) return puts(a),0\n#define fastio ios::sync_with_stdio(0)\n#define filei(f) freopen(f,\"r\",stdin)\n#define fileo(f) freopen(f,\"w\",stdout)\n#define eprintf(...) fprintf(stderr,__VA_ARGS__)\n#define MASK(a) (1<<(a))\n#define MASKX(x,a) ((x)<<(a))\n#define BIT(a,b) ((a>>b)&1)\n#define cddx(a) priority_queue\n#define cxdd(a) priority_queue,greater >\n#define deval(a) {cerr<<#a<<\" = \"< pii;\ntypedef unsigned long long UINT;\ntypedef unsigned int uint;\ntypedef long long INT;\ntypedef long double ldouble;\ntypedef pair PII;\ntypedef stringstream sst;\ntypedef istringstream iss;\ntypedef vector vint;\ntypedef vector VINT;\ntypedef vector vpii;\ntypedef vector VPII;\ntypedef priority_queue pqi;\ntypedef priority_queue PQI;\ntypedef priority_queue pqp;\ntypedef priority_queue PQP;\ntypedef priority_queue > xqi;\ntypedef priority_queue > XQI;\ntypedef priority_queue > xqp;\ntypedef priority_queue > XQP;\nconst int INF=1e9+7;\nusing namespace std;\nint n,m;\nstring s,t;\nint prime(int x)\n{\n\tif (x==1) return 0;\n\tfor (int i=2;i*i<=x;i++)\n\tif (x%i==0) return 0;\n\treturn 1;\n}\nint check(int x,int y)\n{\n\tif (x>n||x<1||y>=m||y<0) return 0;\n\treturn 1;\n}\nlong long gcd(long long a, long long b){\n if(b == 0){\n return a;\n }\n return gcd(b, a % b);\n}\nint main()\n{\n\t//freopen(\"input.in\",\"r\",stdin);\n\t//freopen(\"output.out\",\"w\",stdout);\n\tcin>>s;\n\tint p=0;\n\tint f1=1,f2=1;\n\tt+=s[0];\n\tfor (int i=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//#include \nusing namespace std;\nset S;\nint n,d=0,ans=0,m;\nint a[100010],b;\nstring str,s;\nint main()\n{\n string s,ans;\n cin>>s;\n bool y=false;\n for(int i=0;i0 && s[i]=='a' && s[i+1]=='t' && i0 && s[i]=='d' && s[i+1]=='o' && s[i+2]=='t' && i\n#include \nusing namespace std;\n\nstring s;\n\n\nstring f(){\n\tfor (int i=1;i> s){\n\t\tcout << f() << endl;\n\t}\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "a7859cebe9e67e56fc83653eb8951c32", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define pii pair\n#define fi first\n#define se second\n#define int long long\n#define pb push_back\n#define mod 1000000007\n#define pll pair\nusing namespace std;\nlong long power(long long a,long long b){\n long long ans=1;\n while(b>0){\n if(b&1){ans=(ans*a)%mod;}\n a=(a*a)%mod;\n b>>=1;\n }\n return ans;\n \n}\nbool prime(int n){\n for(int i=2;i*i<=n;i++){\n if(n%i==0)return false;\n }\n return true;\n}\nint32_t main(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n string s;\n cin>>s;\n int n=s.size();\n string s1;\n int i=1;\n s1.pb(s[0]);\n while(i\nusing namespace std;\n\nint main()\n{\n string s;\n cin>>s;\n int i;\n int flag=0;\n string ans=\"\";\n for(i=0;i\n#include \n\nusing namespace std;\n\nint main()\n{\n string s ;\n cin >> s ;\n \n char ch = s[0] ;\n \n s = s.substr ( 1 , s.size() - 1 ) ;\n \n s.replace( s.find(\"at\") , 2 , \"@\" ) ;\n \n for ( int i = 1 ; i < s.size() ; i++ )\n if ( s.find (\"dot\") != -1 && s.find (\"dot\") != s.size() - 3 )\n s.replace ( s.find (\"dot\") , 3 , \".\" ) ;\n \n cout << ch + s ;\n return 0 ;\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "acceb732a412d0d6e7a1bc62ac4c1a67", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*\n NAME : RISHABH SINGH\n INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY\n*/ \n#include \nusing namespace std;\n// #define int long long\n#define pii pair\n#define bi __builtin_inf()\n#define ll long long\n#define mp make_pair\n#define ss second\n#define pb push_back\n#define ff first\n#define speed_cubing ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n#define um unordered_map\n#define us unordered_set\n#define bp __builtin_popcount\n#define ld long double\n#define rep(n) for (int i = 0; i < n; i++)\n#define pll pair\n#define mx = 1e18;\n#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())\n#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())\n#define PI 3.1415926535\n#define sz(v) (int)v.size()\n#define endl '\\n'\nll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}\nconst int N = 3e4+5;\nconst int mod = 1e9 + 7;\n\nint32_t main()\n{\n\n speed_cubing\n\n \n string s;\n cin>>s;\n\n string ans = \"\";\n bool flag = 0;\n\n \n for(int i=0;i\n#include \nusing namespace std;\n\n\nstring in, out;\n\nint main(){\n cin >> in;\n int lng = in.length();\n out = \"\";\n out += in[0];\n bool at = false;\n int i = 1;\n while (i < lng){\n if (!at && i < lng - 2 && in[i] == 'a' && in[i+1] == 't'){\n out += \"@\";\n at = true;\n i++;\n }else if (i < lng - 3 && in.substr(i, 3) == \"dot\"){\n out += \".\";\n i+=2;\n }else{\n out += in[i];\n }\n i++;\n }\n cout << out << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "6cdb977a6749403aebc40178ae75cb18", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nbool judge;\nstring a,b;\n\nint main()\n{\n cin>>a;\n int i=0;\n while(i\n#include \n#include \n#include \n#include \nusing namespace std;\nint main()\n{string s;\nint mark=0;\ncin>>s;\nfor (int i=0;i=b;i--)\n#define lli long long int\n#include\n#define pb push_back\n#define rs resize\n#define S second\n#define F first\n#define all(v) v.begin(), v.end()\nusing namespace std;vector a,b,c;string s;\nlli nn[300002]={0};\nint main ()\n{\n fast;\n\tlli i,j,l=1,k=0,n,m,f=0,ff,x=0,xx,y,p;\n\tcin>>s;\n\tl=s.size();\n\tcout<\nusing namespace std;\nstring s,ans;\nbool u=false;\nbool chdot(int i)\n{\n if(s[i]=='d'&&s[i+1]=='o'&&s[i+2]=='t')\n return true;\n return false;\n}\nbool chat(int i)\n{\n if(s[i]=='a'&&s[i+1]=='t')\n return true;\n return false;\n}\nint main()\n{\n cin>>s;\n for(int i=0;i\n\n#define ll long long\n#define pb push_back\n#define pp pop_back\n#define fr freopen\n#define fi first\n#define se second\nusing namespace std;\nll i , j , k , m , n , c[3000] , d , e , l = 1 , r;\nstring a , b;\nint main()\n{\n ///freopen(\"gcm.in\" , \"r\" , stdin);\n ///freopen(\"gcm.out\" , \"w\" , stdout);\n ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);\n cin>>a;\n n = a.length();\n b = b + a[0];\n for(i = 1 ; i < n ; i ++)\n {\n if(i < n - 2 && a[i] == 'a' && a[i + 1] == 't' && l == 1)\n b = b + '@' , i ++ , l = 0;\n else if(i < n - 3 && a[i] == 'd' && a[i + 1] == 'o' && a[i + 2] == 't')\n b = b + '.' , i += 2;\n else\n b = b + a[i];\n }\n cout<\n#define test(t) for(cin>>t;t;--t)\n#define rep(i,n) for(ll i=0;i=0;i--)\n#define pb push_back\n#define f first\n#define sec second\n#define in cin>>\n#define out cout<<\n#define optimize ios_base::sync_with_stdio(0);cin.tie(NULL);\nusing namespace std;\ntypedef long long ll;\ntypedef vector vll;\ntypedef pairpll;\n\nstring email ;\n\nstring solve() {\n // handle @\n ll at_pos = email.find(\"at\", 1);\n if(at_pos != string::npos)\n email.replace(at_pos, 2, \"@\");\n // handle dot\n ll pos = email.find(\"dot\", 1);\n while( pos != string::npos && email.size() != pos + 3) {\n email.replace(pos, 3, \".\");\n pos = email.find(\"dot\", 1);\n }\n return email;\n}\n\nint main(){\n\toptimize\n in email;\n out solve();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "1fd7983b94983abedb146011f90df31c", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define MIN -1\n#define MAX 1e9+7\n#define SMAX 2*(1e18)+7\n#define all(a) a.begin (), a.end()\n#define mk make_pair\n#define pb push_back\n#define F first\n#define S second\n#define tri(a) (a*(a+1))/2\n#define fixed(a, c) fixed< ii;\ntypedef vector vll;\ntypedef vector vi;\ntypedef vector vii;\ntypedef vector vvi;\ntypedef priority_queue , greater > pq;\ntypedef double db;\nint getint(string s)\n{\n int x=stoi(s);\n return x;\n}\nstring getstring (int x)\n{\n string lol=to_string(x);\n return lol;\n}\n\nbool ok(int x, int gg)\n{\n return (x<=gg-1 and x>=0);\n}\nint X[4]= {1, 0, -1, 0};\nint Y[4]= {0, 1, 0, -1};\n/*freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);*/\nint gcd(int a, int b)\n{\n if (a == 0)\n return b;\n return gcd(b % a, a);\n}\nll lcm(ll a, ll b)\n{\n return (a*b)/gcd(a, b);\n}\nll exponentMod(ll A, ll B, ll C)\n{\n if (A == 0)\n return 0;\n if (B == 0)\n return 1;\n ll y;\n if (B % 2 == 0)\n {\n y = exponentMod(A, B / 2, C);\n y = (y * y) % C;\n }\n else\n {\n y = A % C;\n y = (y * exponentMod(A, B - 1, C) % C) % C;\n }\n return (ll)((y + C) % C);\n}\nbool ispal(string x)\n{\n for(int i=0, j=x.size ()-1; ij)break;\n if(x[j]!=x[i])return false;\n }\n return true;\n}\n//VIVA EL GG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nint main()\n{\n string s;\n while(cin>>s){\n string ans;\n int n=s.size ();\n ans+=s[0];\n // ans+=s[1];\n int sw=0;\n for(int i=1;i\n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main()\n{\n string x;\n cin>>x;\n string y=\"\";\n y+=x[0];\n bool stop=false;\n for(int i=1; i2 && stop==false ) {\n y+='@';\n i++;\n stop=true;\n }\n else if(x[i]=='d' && x[i+1]=='o' && x[i+2]=='t' && i+1!=x.length() && x.length()-i>3 ) {\n y+='.';\n i+=2;\n }\n else y+=x[i];\n }\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define F first\n#define S second\n#define MAX 100000000000000000\n#define MOD 1000000007\n#define files freopen(\"OUTPUT.TXT\",\"r\",stdin);\n#define PI 3.141592653589793238462\n#define INF 1000000000\nusing namespace std;\nint my_find(string s1,string s2,int start){\n\tfor (int i=start;i>s1;\n\tpos=my_find(s1,\"at\",1);\n\ts2=s1.substr(0,pos)+'@'+s1.substr(pos+2);\n\tint i=my_find(s2,\"dot\",1),last=0;\n\twhile(i!=-1&&i\nusing namespace std;\n\n\n#define mod 1000000007\n#define ull unsigned long long int\n#define FastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n \n/* TYPE DEFINITIONS */\ntypedef long long int ll;\ntypedef vector vi;\ntypedef pair pi;\n \n \n \n/* MATH */\nbool isPrime(ull n) \n{ \n if (n <= 1) return true; \n if (n <= 3) return true; \n if (n%2 == 0 || n%3 == 0) return false; \n \n for (ull i=5; i*i<=n; i=i+6) \n if (n%i == 0 || n%(i+2) == 0) \n return false; \n \n return true; \n} \n \nll pow(ll a,ll b,ll m)\n{\n ll res=1;\n while(b>0)\n {\n if(b&1)res=(res*a)%m;\n a=(a*a)%m;\n b>>=1;\n }\n return res;\n}\n\nll ncr(int n, int k) \n{ \n\tif(k>n)return 0;\n ll res = 1; \n if ( k > n - k ) \n k = n - k; \n for (int i = 0; i < k; ++i) \n { \n res *= (n - i); \n res /= (i + 1); \n } \n \n return res; \n} \n \n//\n/* MACROS */\n \n#define all(A) (A).begin() , (A).end()\n#define arrall(A,n) (A),((A)+(n))\n#define rall(A) (A).rbegin() , (A).rend()\n#define sz(A) (int)(A).size()\n#define pb push_back\n#define ppb pop_back\n#define mp make_pair\n#define ln(X) (int)(X).length()\n#define square(X) ((X)*(X))\n#define cube(X) ((X)*(X)*(X))\n#define forn(i, n) for (int i = 0; i < int(n); i++)\n#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)\n#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)\n#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)\n#define fore(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) \n#define deb(x) cout << '>' << #x << ':' << x << endl;\n\nvoid solve()\n{\n\tstring s;\n\tcin>>s;\n\tbool at=0;\n\tint n=ln(s);\n\tcout<>t;\n while(t--)\n {\n solve();\n }\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "df7aabb4f22be06e94162e0334acdc11", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint main() {\n string s;\n cin >> s;\n string ans;\n ans.push_back(s[0]);\n bool at = false;\n for(int i=1;i\nusing namespace std;\n\nint main(){\n\tint check=0;\n\tstring a;\n\tgetline(cin,a);\n\tfor(int i=0;i\nusing namespace std;\n\ntypedef vector vi;\ntypedef vector vll;\ntypedef long long int ll;\ntypedef vector> Vii;\ntypedef vector> Vll;\ntypedef vector vs;\n#define forn(i,a,b) for(i=a;i=b;i--)\n#define pb push_back\n#define srt(s) sort(s.begin(),s.end()) \n#define rev(s) reverse(s.begin(),s.end())\n#define mine(v) min_element(v.begin(),v.end())\n#define maxe(v) max_element(v.begin(),v.end())\n#define all(v) v.begin(),v.end()\n#define inp(v); forn(ee,0,v.size())cin>>v[ee];\n#define out(v); forn(ee,0,v.size())cout<F<<\" \";\n// }\n// cout<>t;\n while(t--)\n { \n cin>>s;\n n=s.size();\n str=\"\";\n str+=s[0];\n forn(i,1,n)\n {\n if(flag==0 && i\nusing namespace std;\nstring str;\nstring ans;\nint n;\nbool f=true;\nint main(){\n\tcin>>str;\n\tn=str.length();\n\tint happy=19260817+19890604-20041023;\n\tfor(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\ntypedef unsigned int u32;\ntypedef int s32;\ntypedef float fp32;\ntypedef double fp64;\ntypedef __int64 s64;\ntypedef unsigned __int64 u64;\n\nusing namespace std;\n\nconst s64 dx[4] = {-1, 0, 1, 0};\nconst s64 dy[4] = {0, -1, 0, 1};\n\n\nint main ()\n{\n string email;\n cin>>email;\n// string email = \"dotdotdotatdotdotat\";\n\n for (int i = 1; i < (email.size() - 2); i++)\n {\n if ((email[i] == 'a') && (email[i + 1] == 't'))\n {\n email[i] = '@';\n email.erase(i + 1, 1);\n break;\n }\n }\n\n for (int i = 1; i < (email.size() - 3); i++)\n {\n if ((email[i] == 'd') && (email[i + 1] == 'o') && (email[i + 2] == 't'))\n {\n email[i] = '.';\n email.erase(i + 1, 2);\n }\n }\n\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define FOR(I,A,B) for(int I=(A);I<=(B);I++)\n#define REP(I,N) for(int I=0;I<(N);I++)\n#define ALL(X) (X).begin(),(X).end()\n#define VAR(A,B) __typeof(B) A=B\n#define FORE(I,X) for(VAR(I,(X).begin());I!=(X).end();I++)\n#define F first\n#define S second\n#define INF 1000000007\n#define PB push_back\n#define MP make_pair\ntypedef pair PII;\ntypedef long long LL;\nchar tab[134];\nstring imejl;\n\nint main(){\n\tscanf(\"%s\",tab);\n\timejl=tab;\n\tfor(int i=1;i\n\nusing namespace std;\n\nint main()\n{\n string s; cin >> s;\n int n = s.size();\n int i = 0;\n bool _at = true, _dot = true;\n while (i\nusing namespace std;\nint main(){string a;cin>>a;a.replace(a.find(\"at\",1),2,\"@\");for(int i=a.find(\"dot\",1);i\n#include \n#include \n\nusing namespace std;\n\nint i, l;\nstring s;\n\nbool dot() {\n return (i+2) < (l-1) && s[i+1] == 'o' && s[i+2] == 't';\n}\n\nbool at() {\n return (i+1) < l && s[i+1] == 't';\n}\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n\n cin >> s;\n\n l = s.length();\n\n cout << s[0];\n\n bool f = false;\n\n for (i = 1; i < l;) {\n if (s[i] == 'a' && !f && at()) {\n cout << '@';\n f = true;\n i += 2;\n continue;\n }\n\n if (s[i] == 'd' && dot()) {\n cout << '.';\n i += 3;\n continue;\n }\n\n cout << s[i];\n i++;\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "0ac712081c6fdfce17d86182b5bd330f", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n\tint n,l,i,sign;\n char s[1000];\n cin>>s; \n l=strlen(s);\n cout<\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define MAX 1e9\n#define fr(i,n) for(int i=0;i>s;\n ll n=s.size(),f=0;\n string ans=\"\";\n ans+=s[0];\n string dt=\"dot\";\n for(int i=1;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long int64;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef pair ii;\ntypedef string ST;\ntypedef stringstream SS;\n\n#define \t\t\t\t Pf\tprintf\n#define\t\t\t\t Sf\tscanf\n#define\t\t\t\t ep\t1e-9\n#define\t\t\tCL(a, b)\tmemset(a, b, sizeof(a))\n#define\t\t\t\tSZ(a)\tint((a).size())\n#define\t\t\t\t pb\tpush_back\n#define\t\t\t all(c)\t(c).begin(), (c).end()\n#define\t\t\ttr(i, c)\tfor(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define\t present(x, c)\t((c).find(x) != (c).end()) // used for map and set. //\n#define\tcpresent(x, c)\t(find(all(c),x) != (c).end()) // used for others like vector, list //\n#define\t\t forn(i, n)\tfor(int i = 0; i < n; i++)\n#define forab(i, a, b)\tfor(int i = a; i <= b; i++)\n#define\t rep(i, a, b)\tfor(int i = a; i>=b; i--)\n\nint main()\n{\n ST s, ret;\n bool at = false;\n cin >> s;\n \n int len = s.length();\n \n forn(i, len)\n {\n\t if( s.substr(i, 2) == \"at\" && !at && i != 0 && i != len-2 )\n\t {\n\t\tret += '@';\n\t\tat = true;\n\t\ti++;\n\t }\n\t else if(s.substr(i, 3) == \"dot\" && i != 0 && i != len - 3)\n\t {\n\t\tret += '.';\n\t\ti+=2;\n\t }\n\t else\n\t\tret += s[i];\n }\n \n cout << ret ;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "0b2d731a4f5d2c29eccf69b8f08e7a37", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#pragma GCC optimize(\"Ofast\",3,\"inline\")\n#include\nusing namespace std;\nint read(){\n\tchar c=getchar();\n\tint sum=0;\n\twhile(c>'9'||c<'0')c=getchar();\n\twhile(c<='9'&&c>='0')sum=sum*10+c-'a',c=getchar();\n\treturn sum;\n}\nstring s;\nchar t[105];\nint pos=1;\nbool flag;\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n//\tfreopen(\".in\",\"r\",stdin);\n//\tfreopen(\".out\",\"w\",stdout);\n\tcin>>s;\n\tt[0]=s[0];\n\tint sz=s.size();\n\ts+=char(1);\n\ts+=char(1);\n\ts+=char(1);\n\ts+=char(1);\n\tfor(int i=1;i\n#include \n#include \n\nint main()\n{\n\tstd::string s;\n\tstd::cin>>s;\n\n\ts.replace(s.find(\"at\",1),2,\"@\");\n\n\twhile(true)\n\t{\n\t\tint n = s.find(\"dot\",1);\n\t\tif(n == s.npos || n > s.length() - 4)\n\t\t\tbreak;\n\t\ts.replace(n,3,\".\");\n\t}\n\n\tstd::cout << s;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "73480c45f99c524fabc9b33fa1740260", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n string s;\n cin >> s;\n for(int i = 1; i \n\nusing namespace std;\nconst int maxn = 1e4+7;\n\nchar str[maxn];\n\nint main()\n{\n ///dotdotatadasdatdotatjkl\n while(~scanf(\"%s\",str))\n {\n int len=strlen(str);\n int flag = 0;\n for(int i = 0; i < len; i ++ ) {\n if(str[i] == 'd'&&i!=0&&i!=len-3&&str[i+1] == 'o' &&str[i+2] == 't') {\n printf(\".\");\n i+=2;\n }\n else if(str[i]=='a'&&i!=0&&i!=len-2&&str[i+1]=='t'&&!flag){\n printf(\"@\");\n flag = 1;\n i++;\n }\n else {\n printf(\"%c\",str[i]);\n }\n }\n puts(\"\");\n }\n\n return 0;\n}\n/*2017-08-06 19:43:04.244*/", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "796bcef59c5e90fb5e87ea9d1a43143b", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)\n#define rep1(i,n) for(__typeof(n) i=1; i<=(n); i++)\n#define rep2(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)\n#define inf 1061109567000LL\n#define pi 3.14159265358979323846264338327950\n\n#define pb(x) push_back(x)\n#define all(x) x.begin(),x.end()\n\n#define sz size()\n#define eps 1e-9\n#define vi vector\n#define vs vector\n#define mii map\n#define msi map\n#define mis map\n#define CLEAR(x) memset(x,0,sizeof(x));\n#define max(a,b) (a>b?a:b)\n#define min(a,b) (a>s)\n {\n cout<\n\nusing namespace std;\nstring s,a;\nint b;\nint main()\n{\n getline(cin,a);\n int len=a.size();\n for(int i=0;i\n#define LL \"%I64d\"\n#define pb push_back\n#define pp pop_back\n#define ll long long\n#define pll pair\n#define pii pair\n#define pss pair\n#define psi pair\n#define psll pair\n#define vi vector\n#define vs vector\n#define vl vector\n#define vc vector\n#define vv >\n#define mp make_pair\n#define str(a, b) substr((a), (b))\n#define all(v) v.begin(),v.end()\n#define ff first\n#define ss second\n#define endl \"\\n\"\n#define sz size()\n#define rr return\n#define r0 return 0\n#define mi map\n#define ms map\n#define ml map\n#define all_sum(a,b) accumulate((a),(b),0)\n#define FOR(i,a,b) for (int i = (a); i < (b); ++i)\n#define F0R(i,a) FOR(i,0,a)\n#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)\n#define R0F(i,a) ROF(i,0,a)\nusing namespace std;\nvoid adskiy_razgon(){\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n}\nconst int mxN= 1e+9+7;\nconst ll INF = 1e+18+7;\nll nod(ll a, ll b) {\n if(b > a) {\n\t\tswap(a, b);\n\t}\n while(b > 0) {\n\t a %= b;\n\t\tswap(a, b);\n }\n\treturn a;\n}\nll nok(ll a,ll b) {\n return a * b / nod(a, b);\n}\nvoid sp(ll a,double b) {\n cout << fixed << setprecision(a) << b;\n}\nll binpow (ll a, ll n) {\n\tif (n == 0) {\n\t\treturn 1;\n\t}\n\tif (n % 2 == 1) {\n\t\treturn binpow (a, n - 1) * a;\n\t}\n\telse {\n\t ll b = binpow (a, n / 2);\n\t\treturn b * b;\n\t}\n}\n\nvoid solve() {\n\tstring s;\n\tcin >> s;\n\tbool ok = 0;\n\tfor(int i = 0; i < s.sz; ++i) {\n\t\tif(s[i] == 'a' && s[i + 1] == 't' && !ok && i != 0 && (i + 1) != s.sz - 1) {\n\t\t\tok = 1;\n\t\t\tcout << '@';\n\t\t\ti++;\n\t\t}\n\t\telse {\n\t\t\tif(s[i] == 'd' && s[i + 1] == 'o' && s[i + 2] == 't' && i != 0 && (i + 2) != s.sz - 1) {\n\t\t\t\tcout << '.';\n\t\t\t\ti += 2;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcout << s[i];\n\t\t\t}\n\t\t}\n\t}\n}\nint main(){\n\tadskiy_razgon();\n\tll t = 1;\n//\tcin >> t;\n\tfor(int i = 1; i <= t; ++i) {\n\t\t//cout << \"Case #\" << i << \": \";\n\t\tsolve();\n\t}\n\tr0;\n}\n\n\n\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "4e4f03d8a9cce26b375f9f6ed2ac25f0", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main()\n{\n int f = 0;\n string str,str2;\n vector ret;\n\n cin >> str;\n\n str2 += str[0];\n \n ret.push_back(str2);\n \n for (int i = 1 ; i < str.size()-1 ; i ++)\n {\n string x;\n string y;\n if (i + 2 < str.size()-1)\n {\n x += str[i];\n x += str[i+1];\n x += str[i+2];\n }\n if (i + 1 < str.size()-1)\n {\n y += str[i];\n y += str[i+1];\n }\n\n if (x == \"dot\")\n {\n str2 +='.';\n for (int j = 0 ; j < ret.size() ; j ++) ret[j] += '.';\n i += 2;\n }\n else if (y == \"at\")\n {\n f ++;\n for (int j = 0 ; j < f-1 ; j ++) ret[j] += \"at\";\n for (int j = f-1 ; j < ret.size() ; j ++) ret[j] += '@';\n str2 += str[i];\n str2 += str[i+1];\n ret.push_back(str2);\n i += 1;\n }\n else\n {\n str2 += str[i];\n for (int j = 0 ; j < ret.size() ; j ++) ret[j] += str[i];\n }\n }\n for (int j = 0 ; j < ret.size() ; j ++) ret[j] += str[str.size()-1];\n\n sort(ret.begin(), ret.end());\n \n cout << ret[0];\n //scanf(\"\\n\");\n return 0;\n}\n//aatabcdotcom\n", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "9e79f9178961c39d1b8c6a402b13e73b", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n\nusing namespace std;\n\nint main() {\n//\tfreopen(\"input.txt\", \"r\", stdin);\n//\tfreopen(\"output.txt\", \"w\", stdout);\n\tstring s;\n\tcin >> s;\n\tint atpos = -1;\n\tfor (int i = 1; i < s.length() - 1; ++i)\n\t\tif (s[i] == 'a' && s[i + 1] == 't' && i + 1 != s.length() - 1) {\n\t\t\tatpos = i;\n\t\t\tbreak;\n\t\t}\n\tcout << s[0];\n\tfor (int i = 1; i < s.length(); ++i) {\n\t\tif (i + 2 < s.length() && s[i] == 'd' && s[i + 1] == 'o' && s[i + 2] == 't' && i+2 != s.length()-1) {\n\t\t\tcout << '.';\n\t\t\ti += 2;\n\t\t\tcontinue;\n\t\t}\n\t\tif (i == atpos) {\n\t\t\tcout << '@';\n\t\t\t++i;\n\t\t\tcontinue;\n\t\t}\n\t\tcout << s[i];\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "d495f352a89f9a6d8a1ed773a226a59a", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nchar a[101];\nint len;\nstring s; \nbool flag;\nint main()\n{\n scanf(\"%s\",&a);\n len=strlen(a);\n for(int i=0;i\nusing namespace std;\ntypedef long long ll;\n\n#include \n#include \n\n#define vi(v1) vector v1;\n#define vc(v1) vector v1;\n#define vii(v1) vector> v1;\n#define pb push_back\n#define mp make_pair\n#define F first\n#define S second\n#define all(v) v.begin(),v.end()\n#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n#define rep(i,a,b) for(int i=a;i>str;\n l=str.length();\n rep(i,0,l)\n {\n if(str.substr(i,2)==\"at\"&&i==0)\n {\n cout<<\"at\";\n i=i+1;\n }\n else if(str.substr(i,3)==\"dot\"&&i==0)\n {\n cout<<\"dot\";\n i=i+2;\n }\n else if(str.substr(i,2)==\"at\"&&i==l-2)\n {\n cout<<\"at\";\n\n break;\n }\n else if(str.substr(i,3)==\"dot\"&&i==l-3)\n {\n cout<<\"dot\";\n break;\n }\n else if(str.substr(i,2)==\"at\"&&z==0)\n {\n cout<<\"@\";\n z=1;\n i=i+1;\n }\n else if(str.substr(i,3)==\"dot\")\n {\n cout<<\".\";\n i=i+2;\n }\n else\n {\n cout<\n#include \nusing namespace std;\nchar s[1001];\n\nint main()\n{\n int f= 0;\n scanf(\"%s\",s);\n int len = strlen(s);\n int i = 0;\n while (1)\n {\n if (i > 0 &&i< len-2 && s[i] == 'a' && s[i+1] == 't' && !f)\n {\n f = 1;\n i++;\n printf(\"@\");\n }\n else if (i > 0 && i\nusing namespace std;\n\nstring s;\nchar ans[100];\nint p;\nbool good;\n\nint main()\n{\n\tint i;\n\tcin>>s;\n\tfor(i=0; i=2 && ans[p-1]=='a' && !good)\n\t\t\t{\n\t\t\t\tp-=1;\n\t\t\t\tans[p++]='@';\n\t\t\t\tgood=1;\n\t\t\t}\n\t\t\telse if(p>=3 && ans[p-2]=='d' && ans[p-1]=='o' )\n\t\t\t{\n\t\t\t\tp-=2;\n\t\t\t\tans[p++]='.';\n\t\t\t}\n\t\t\telse ans[p++]=s[i];\n\t\t}\n\t\telse ans[p++]=s[i];\n\t}\n\tcout<\n\nusing namespace std;\n\nstring str,ans;\n\nint main(void){\n\n int taken=0,i;\n cin>>str;\n\n for(i=0;i=str.size()-3||!(str[i+1]=='o'&&str[i+2]=='t')||ans.size()==0){\n ans.push_back(str[i]);\n continue;\n }\n\n ans.push_back('.');\n i+=2;\n continue;\n\n }\n\n }\n\n cout<\nusing namespace std;\n\nchar str[2101],ans[1001];\n\nint main()\n{\n scanf(\"%s\",str);\n int len=strlen(str);\n for(int i=0;i\n#include\nusing namespace std;\nint main()\n{\n\tint i,n,syc=0,t=0,k=0;\n\tstring s;\n\tcin>>s;\n\tn=s.length();\n\tcout<\n#define elif(a) else if((a))\nusing namespace std;\nint main() {\n\tchar a[120] = {}, b[120] = {}, index = 0;\n\tcin >> a;\n\tfor (int i = 0; a[i]; i++) {\n\t\tif (a[i] == 'a' and a[i + 1] == 't') {\n\t\t\tb[index++] = '@';\n\t\t\ti += 1;\n\t\t}\n\t\telif (a[i] == 'd' and a[i + 1] == 'o' and a[i + 2] == 't') {\n\t\t\tb[index++] = '.';\n\t\t\ti += 2;\n\t\t}\n\t\telse{\n\t\t\tb[index++] = a[i];\n\t\t}\n\t}\n\tb[index--] = 0;\n\tbool at = 1;\n\tif (b[0] == '@') {\n\t\tcout << 'a' << 't';\n\t}\n\telif(b[0] == '.') {\n\t\tcout << 'd' << 'o' << 't';\n\t}\n\telse {\n\t\tcout << b[0];\n\t}\n\tfor (int i = 1; b[i];i++) {\n\t\tif (b[i]=='@') {\n\t\t\tif (at and i!=index) {\n\t\t\t\tcout << '@';\n\t\t\t\tat = 0;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcout << 'a' << 't';\n\t\t\t}\n\t\t}\n\t\telif (b[i] == '.') {\n\t\t\tif (i != index) {\n\t\t\t\tcout << '.';\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcout << 'd' << 'o' << 't';\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcout << b[i];\n\t\t}\n\t}\n\tcout << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["expression parsing", "implementation"], "code_uid": "616dc1d8ba363941ca287d2f742e9203", "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n\nusing namespace std;\n\nvoid print(int n){\n\n if(n<5) cout<<\"O-|\";\n else cout<<\"-O|\";\n\n if(n % 5 == 0) cout<<\"-OOOO\"<v;\n int n;\n cin>>n;\n\n if(n == 0) print(n);\n while (n){v.push_back(n%10);n/=10;}\n\n for(int i = 0; i < v.size(); i++){\n print(v[i]);\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "93da88a8c106134ef6dee93a61a718f8", "src_uid": "c2e3aced0bc76b6484360563355d23a7", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tlong long int n,y,i;\n\tcin>>n;\n\tif(n==2)\n\tcout<<\"2\";\n\telse\n\tcout<<\"1\";\n\t\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "8456c1e917fad677d07a9012ce01fc8d", "src_uid": "c30b372a9cc0df4948dca48ef4c5d80d", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \nusing namespace std;\n#define mp make_pair\n#define pb push_back\n#define ppb pop_back\n#define pf push_front\n#define ppf pop_front\n#define all(x) (x).begin(),(x).end()\n#define ll long long\n#define hell 1000000007\n#define MM 998244353\n#define fr first\n#define sc second\n#define pii pair\n#define rep(i,a,b) for(int i=a;i=(a))&&((n)<=(b)))\n#define clamp(n,a,b) (((n)<(a))?(a):((((n)>(b))?(b):(n)))\nint solve()\n{\n ll a,cnt=0,g;char k;\n cin>>a>>k;\n \n g=a/4;\n if(a%4) a=a%4;\n\telse \t g--,a=4;\n cnt=g*16;\n if(a>2)a=a-2;\n cnt+=a/2*7;a=a%2;\n if(k=='f')cnt+=1;\n else if(k=='e')cnt+=2;\n else if(k=='d')cnt+=3;\n else if(k=='a')cnt+=4;\n else if(k=='b')cnt+=5;\n else if(k=='c')cnt+=6;\n cout<>t;\n\twhile(t--) \n\tsolve();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "077a0c6e0731383ce8dab9d3af37f986", "src_uid": "069d0cb9b7c798a81007fb5b63fa0f45", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \nusing namespace std;\n\nint main(){\n\tint t;\n\tstring s;\n\tcin>>t;\n\tcin>>s;\n\tint stone = 0;\n\tfor(int i = 0; i < t; i++){\n\t\tif(s[i] == '+'){\n\t\t\tstone++;\n\t\t}\n\t\telse{\n\t\t\tstone--;\n\t\t}\n\t\tif(stone < 0){\n\t\tstone = 0;\n\t\t}\n\t}\n\t\n\tcout<\n\nusing namespace std;\nvector a;\nmap ans;\nvoid reco(string s)\n{\n int value = atoi(s.c_str());\n a.push_back(value);\n}\nvoid rec(string s)\n{\n if(s.size() > 9){\n return ;\n }\n reco(s);\n rec(s+\"4\");\n rec(s+\"7\");\n}\n\nint main()\n{\n string s =\"\";\n rec(s);\n sort(a.begin(), a.end());\n for(int i = 0; i < (int)a.size(); i++){\n ans[a[i]] = i;\n }\n int n;\n cin >> n;\n cout << ans[n];\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation", "combinatorics", "bitmasks"], "code_uid": "6c4a290f59d15f482b5e41a34dd91297", "src_uid": "6a10bfe8b3da9c11167e136b3c6fb2a3", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nbool check(long long x){\n\tfor(int i=2;i<=sqrt(x)+1;i++)if(x%i==0)return false;\n\treturn true;\n}\nint main(){\n\tint T;\n\tlong long a,b;\n\tcin>>T;\n\twhile(T--){\n\t\tcin>>a>>b;\n\t\tif(a-b==1&&check(a+b))\n\t\t\tcout<<\"YES\"<\nusing namespace std;\nint n,i,x,ans;\nint main(){\n\tcin>>n;\n\tfor(i=1;i<=n;i++){\n\t\tcin>>x;\n\t\tif(x>0){\n\t\t\tans+=x;\n\t\t}else{\n\t\t\tans-=x;\n\t\t}\n\t}\n\tcout<\n#include\n\nusing namespace std;\n\nint main()\n{\n\tint x[5]={1,2,3,4,5};\n\tint arr[5][5],sum,max=0;\n\tfor (int i = 0; i < 5; i++)\n\t\tfor (int j = 0; j < 5; j++)\n\t\t\tcin>>arr[i][j];\n\tdo\n\t{\n\t\tsum=arr[x[0]-1][x[1]-1]+arr[x[1]-1][x[0]-1]+arr[x[2]-1][x[3]-1]+arr[x[3]-1][x[2]-1]+arr[x[2]-1][x[1]-1]+arr[x[1]-1][x[2]-1]+arr[x[3]-1][x[4]-1]+arr[x[4]-1][x[3]-1]+arr[x[2]-1][x[3]-1]+arr[x[3]-1][x[2]-1]+arr[x[3]-1][x[4]-1]+arr[x[4]-1][x[3]-1];\n\t\tif(sum>max)\n\t\t\tmax=sum;\n\t} while (next_permutation(x,x+5));\n\tcout<\nusing namespace std;\n\nstring s;\nint a[2][7], ans = 1e9, kt[7][10];\n\nvoid backtrack(int pos)\n{\n if(pos == 7)\n {\n if((a[1][1] + a[1][2] + a[1][3]) == (a[1][4] + a[1][5] + a[1][6]))\n {\n int cnt = 0;\n for(int i=1;i<=6;i++)\n cnt += ((a[0][i] == a[1][i]) ? 0 : 1);\n ans = min (ans, cnt);\n }\n return ;\n }\n for(int i=0;i<=9;i++)\n {\n a[1][pos] = i;\n backtrack(pos + 1);\n }\n}\n\nint main()\n{\n cin >> s;\n for(int i=0;i<6;i++)\n a[0][i + 1] = s[i] - 48;\n backtrack(1);\n cout << ans;\n}", "lang_cluster": "C++", "tags": ["brute force", "greedy", "implementation"], "code_uid": "af0dff2f722200fe0b140336d3654bc4", "src_uid": "09601fd1742ffdc9f822950f1d3e8494", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\n\nint main() {\n\tint x, y, z, t1, t2, t3;\n\tint s, e;\n\twhile(cin >> x >> y >> z >> t1 >> t2 >> t3) {\n\t\ts = abs(x - y) * t1;\n\t\te = (abs(x - z) + abs(x - y)) * t2 + 3 * t3;\n\t\tif(s >= e)\n\t\t\tcout << \"YES\" << endl;\n\t\telse\n\t\t\tcout << \"NO\" << endl;\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "c17d7529fbeb60972f656beaae0f34f0", "src_uid": "05cffd59b28b9e026ca3203718b2e6ca", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//basic\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//STL\n#include \n#include \n#include \n#include \n#include \n//define\n#define int int32_t\n#define ll int64_t\n#define dou double\n#define pb push_back\n#define mp make_pair \n#define fir first\n#define sec second\n#define init(a,b) fill(begin(a),end(a),b)\n#define sot(a,b) sort(a+1,a+1+b)\n#define rep1(i,a,b) for(int i=a;i<=b;i++)\n#define rep0(i,a,b) for(int i=a;i\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nconst double eps=1e-8;\nint main(){\n\tdouble a,b,c,d,ans;scanf(\"%lf%lf%lf%lf\", &a, &b, &c, &d);\n\tif ((b-a)==(c-b) && (c-b)==(d-c)) ans=d+(d-c);\n\telse\n\tif (b/a==c/b && c/b==d/c) ans=d*(d/c);\n\telse\n\t\tans=42;\n\tif (ans-floor(ans)>eps && ceil(ans)-ans>eps) ans=42;\n\tprintf(\"%d\\n\", (int)ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "1b9b150f6db1036f09a103c4669b7064", "src_uid": "68a9508d49fec672f9c61766d6051047", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "/************************************************\n┆ ┏┓   ┏┓ ┆\n┆┏┛┻━━━┛┻┓ ┆\n┆┃       ┃ ┆\n┆┃   ━   ┃ ┆\n┆┃ ┳┛ ┗┳ ┃ ┆\n┆┃       ┃ ┆\n┆┃   ┻   ┃ ┆\n┆┗━┓   ┏━┛ ┆\n┆  ┃   ┃  ┆      \n┆  ┃   ┗━━━┓ ┆\n┆  ┃  AC代马   ┣┓┆\n┆  ┃    ┏┛┆\n┆  ┗┓┓┏━┳┓┏┛ ┆\n┆   ┃┫┫ ┃┫┫ ┆\n┆   ┗┻┛ ┗┻┛ ┆\n************************************************ */\n\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ll long long\nusing namespace std;\n\nint Abs(int i)\n{\n if(i<0) i=-i;\n return i;\n}\n\nint main()\n{\n int n,m;\n while(~scanf(\"%d%d\",&n,&m))\n {\n int l=0,r=0,mid=0;\n for(int i=1;i<=6;i++)\n {\n if(Abs(n-i)==Abs(m-i))\n {\n mid++;\n }\n else if(Abs(n-i)\n\nusing namespace std;\n\nlong long int count(long long int x){\n\tlong long int ans = 0;\n\twhile (x != 0){\n\t\tans += (x % 10);\n\t\tx /= 10;\n\t}\n\treturn ans;\n}\n\nlong long int solve(long long int x){\n\tlong long int ans = x;\n\tlong long int i = 10;\n\twhile (x > 0){\n\t\tx = x - x % i - 1;\n\t\tif (count(x) > count(ans))\n\t\t\tans = x;\n\t\ti *= 10;\n\t}\n\treturn ans;\n}\n\nint main()\n{\n\tlong long int x;\n\tcin >> x;\n\tcout << solve(x);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "b5f8f2b509f80a8f0f396778157bf2d5", "src_uid": "e55b0debbf33c266091e6634494356b8", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nlong long a, b;\n\nlong long func(int a) {\n if (a == 0 || a == 1) return 1;\n return a * func(a - 1);\n}\n\nint main() {\n scanf(\"%lld%lld\", &a, &b);\n a = min(a, b);\n printf(\"%lld\", func(a));\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation", "number theory"], "code_uid": "5ffdce2747cdb7bb59a9b38f257c64f3", "src_uid": "7bf30ceb24b66d91382e97767f9feeb6", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include\nusing namespace std; \nint main(int argc, char** argv) {\n int n,a,r; \n\tcin>>n; \n\tint tabA[5]={0},tabB[5]={0};\n\tfor(int i=0; i>a; \n\t tabA[a-1]+=1; \n\t\t}\n for(int i=0; i>a; \n\t tabB[a-1]+=1; \n\t\t}\t\n\n\tint c=0,k=0,t=0;\n\tfor (int i=0; i<5;i++)\n\t{ \n\t if(tabA[i]!=tabB[i]) \n\t { r=abs(tabA[i]-tabB[i]);\n\t if((r%2)==0)\n\t { c+=(r/2); t++;\n\t\t }\n\t\t}\n\t\telse { k++;\n\t\t}}\n\t\tif(((c%2)==0)&&(c!=0)&&(t>=2)) cout<<(c/2);\n\t\telse if(k==5) cout<<0; \n\t\telse cout<<-1;\n\n\treturn 0;}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "959697e9016ab68fbc50d9a0c4adf4fa", "src_uid": "47da1dd95cd015acb8c7fd6ae5ec22a3", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "//#pragma comment(linker,\"/STACK:100000000000,100000000000\")\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n/* Constants begin */\nconst long long inf = 2e18 + 7;\nconst long long mod = 1e9 + 7;\nconst double eps = 1e-12;\nconst double PI = 2*acos(0.0);\nconst double E = 2.71828;\n/* Constants end */\n\n/* Defines begin */\n#define pb push_back\n#define mp make_pair\n#define ll long long\n#define double long double\n#define F first\n#define S second\n#define all(a) (a).begin(),(a).end()\n#define forn(i,n) for (int (i)=0; (i)<(n); ++(i))\n#define random S(rand()<<16|rand())\n#define sqr(x) (x)*(x)\n#define base complex\n/* Defines end */\n\ndouble f[2][1005];\n\nint n, m;\n\nvector a, b;\n\ndouble C(int n, int m, bool ok = false){\n if(ok) swap(a, b);\n if(m == n){\n a.pb(1);\n b.pb(1);\n } else\n if(!m){\n a.pb(1);\n b.pb(1);\n } else {\n for(int i = 1; i <= m; ++i){\n b.pb(i);\n }\n for(int i = n - m + 1; i <= n; ++i){\n a.pb(i);\n }\n }\n if(ok) swap(a, b);\n}\n\nint main(void){\n #ifdef nobik\n freopen(\"input.txt\", \"rt\", stdin);\n freopen(\"output.txt\", \"wt\", stdout);\n #endif\n scanf(\"%d %d\", &n, &m);\n double res = 0;\n f[0][0] = 1.0;\n for(int i = 1; i <= n * m; ++i){\n for(int j = 0; j <= m; ++j){\n //f[1][j + 1] += f[0][j] * (m - j) / (m * n - i + 1);\n //f[1][j] += f[0][j] * 6;\n }\n }\n if(m == 1){\n cout << setprecision(12) << fixed << 1.0 / n << endl;\n exit(0);\n }\n for(int i = 1; i <= min(n, m); ++i){\n double cur = 1.0 / (n + .0) * i * i;\n a.clear();\n b.clear();\n C(m, i);\n C(n * m - m, n - i);\n C(n * m, n, true);\n int it = 0;\n forn(i, a.size()){\n cur *= a[i];\n while(cur > 10 && it < b.size()) cur /= b[it++];\n }\n while(it < b.size()) cur /= b[it++];\n res += cur;\n }\n cout << setprecision(12) << fixed << res << \"\\n\";\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["probabilities", "math", "combinatorics"], "code_uid": "5b4763d0de41292570f8aa418b86562a", "src_uid": "0b9ce20c36e53d4702869660cbb53317", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long ll;\nconst int p = 1e9 + 7;\nll poww(ll a, ll b) {\n ll ans = 1, base = a;\n while (b != 0) {\n if (b & 1 != 0)\n ans = ans * base % p;\n base = base * base % p;\n b >>= 1;\n }\n return ans % p;\n}\nint main() {\n#ifdef ONLINE_JUDGE\n#else\n freopen(\"c:\\\\code\\\\in.txt\", \"r\", stdin);\n#endif\n ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n ll x, k, ans;\n scanf(\"%lld%lld\", &x, &k);\n if(x == 0){\n cout << 0 << endl;\n return 0;\n }\n x = x % p;\n ans = (poww(2, (k + 1) % (p - 1)) % p * x % p - poww(2, k % (p - 1)) % p + 1 + p) % p;\n printf(\"%lld\\n\", ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "11ce56493c065794d2fe6be50536e555", "src_uid": "e0e017e8c8872fc1957242ace739464d", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// Amar_El-Bay\n#include\nusing namespace std ;\n#define El_Bay ios_base::sync_with_stdio(false);cin.tie(NULL);\n#define sz(s) s.size()\n#define ll long long\n#define pb push_back\nint main()\n{\n El_Bay\n int _;\n cin>>_;\n while(_--)\n {\n string s;\n cin >> s;\n int x = s[0]-'0';\n int ans = (x-1)*10;\n if(sz(s)==1) ans+=1;\n else if(sz(s)==2) ans+=3;\n else if(sz(s)==3) ans+=6;\n else ans+=10;\n cout << ans << endl;\n }\n}\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "05dafa81485059c24966d01abe074287", "src_uid": "289a55128be89bb86a002d218d31b57f", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 Diagnostics", "source_code": "#include\nusing namespace std;\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n string s;\n cin>>s;\n int l = s.length();\n int cnt = 0;\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \nusing namespace std;\n \nusing ll = long long;\nusing ld = long double;\nusing db = double;\nusing str = string;\n \nusing pi = pair;\nusing pl = pair;\nusing pd = pair; \n \nusing vi = vector;\nusing vl = vector;\nusing vd = vector;\nusing vs = vector;\nusing vld = vector;\nusing vpi = vector;\nusing vpl = vector;\nusing vpd = vector;\nusing vvi = vector;\nusing vvl = vector;\n \nusing si = set;\nusing sl = set;\n \n#define mp make_pair\n#define f first\n#define s second\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) (x).rbegin(), (x).rend() \n#define rs resize\n#define ins insert \n#define ft front() \n#define bk back()\n#define pf push_front \n#define pb push_back\n#define eb emplace_back \n#define lb lower_bound \n#define ub upper_bound\n#define nl \"\\n\"\n#define in cin\n#define out cout\n \n#define trav(i, x) for (auto& i: x)\n#define rep(i, a, b) for(auto (i) = (a); i < (b); ++i)\n#define per(i, a, b) for(auto (i) = (a - 1); i >= (b); --i)\n \nconst int MOD = 998244353;\nconst int MX = 5005;\nconst ll INF = 1e18; \nconst ld PI = acos((ld)-1);\nmt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); \n \nll lcm(ll a, ll b) { return (a / (__gcd(a, b)) * b); }\nll gcd(ll a, ll b) { return (__gcd(a, b)); }\nll pow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; }\n \n#define ts to_string\nstr ts(char c) { return str(1,c); }\nstr ts(bool b) { return b ? \"true\" : \"false\"; }\nstr ts(const char* s) { return (str)s; }\nstr ts(str s) { return s; }\ntemplate str ts(complex c) { \n\tstringstream ss; ss << c; return ss.str(); }\nstr ts(vector v) { \n\tstr res = \"{\"; for(int i = 0; i < sz(v); i++) res += char('0'+v[i]);\n\tres += \"}\"; return res; }\ntemplate str ts(bitset b) {\n\tstr res = \"\"; for(int i = 0; i < SZ; i++) res += char('0'+b[i]);\n\treturn res; }\ntemplate str ts(pair p);\ntemplate str ts(T v) {\n\tbool fst = 1; str res = \"{\";\n\tfor (const auto& x: v) {\n\t\tif (!fst) res += \", \";\n\t\tfst = 0; res += ts(x);\n\t}\n\tres += \"}\"; return res;\n}\ntemplate str ts(pair p) {\n\treturn \"(\"+ts(p.f)+\", \"+ts(p.s)+\")\"; }\n \nvoid DBG() { cerr << \"]\" << endl; }\ntemplate void DBG(H h, T... t) {\n\tcerr << ts(h); if (sizeof...(t)) cerr << \", \";\n\tDBG(t...); }\n#define dbg(...) cerr << \"LINE(\" << __LINE__ << \") -> [\" << #__VA_ARGS__ << \"] : [\", DBG(__VA_ARGS__)\n \nint n;\n \nbool check(int x) {\n\tstring s = to_string(x);\n\tint sum = 0;\n\tfor(int i = 0; i < sz(s); i++) {\n\t\tsum += s[i] - '0';\n\t}\n\tif(sum == 10) {\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n}\n \nint main() {\n\tcin >> n;\n\tint cnt = 0;\n\tfor(int i = 19;; i += 9) {\n\t\tif(check(i) && cnt == n - 1) {\n\t\t\tcout << i << nl;\n\t\t\tbreak;\n\t\t} else if(check(i)) {\n\t\t\tcnt++;\n\t\t}\n\t}\n}\n ", "lang_cluster": "C++", "tags": ["brute force", "dp", "binary search", "implementation", "number theory"], "code_uid": "767714871eca246ef502f2025316076f", "src_uid": "0a98a6a15e553ce11cb468d3330fc86a", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\nint main ()\n{\n string a , b;\n cin>>a>>b;\n for (int i=0; i tolower(b[i]))\n {\n cout<<1;\n return 0;\n }\n if (tolower(b[i]) > tolower(a[i]))\n {\n cout<<-1;\n return 0;\n }\n else\n {\n if (i == a.size()-1)\n cout<<0;\n \n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "6494e42db67dc555f72a916a1d285096", "src_uid": "ffeae332696a901813677bd1033cf01e", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define ll long long\nusing namespace std;\nll q,ans=2,sum=0;\nint main(){\n cin>>q;\n for(int i=2;i<=q;i++) sum+=ans,ans=ans*2;\n sum+=ans;\n cout<\r\nusing namespace std;\r\nconst int mod = 1e9 + 7;\r\nlong long qpow(long long a, long long b)\r\n{\r\n\tlong long res = 1;\r\n\twhile (b)\r\n\t{\r\n\t\tif (b & 1)\r\n\t\t{\r\n\t\t\tres = (res * a) % mod;\r\n\t\t}\r\n\t\ta = (a * a)%mod;\r\n\t\tb /= 2;\r\n\t}\r\n\treturn res%mod;\r\n}\r\nint main()\r\n{\r\n\t int t;\r\n\t\t\tlong long n;\r\n\t\t\tcin >> n;\r\n\t\t\tlong long u = 2;\r\n\t\t\tlong long ks = pow(2,n);\r\n\t\t\tlong long kk = qpow(u*2, (ks - 2)) % mod;\r\n\t\t\tlong long cf = (kk*6)%mod;\r\n\t\t\tcout << cf << endl;\r\n}", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "ebbd1c6a939ee0b68b4fbe6e1fc5c0fc", "src_uid": "5144b9b281ea4087d8334d91c3c8bda4", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#define _HAS_ITERATOR_DEBUGGING 0\n#define _ITERATOR_DEBUG_LEVEL 0 \n#ifdef _MSC_VER\n#define _CRT_SECURE_NO_WARNINGS\n#endif\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define setzero(arr) memset(arr, 0, sizeof(arr))\n\nint ___read_tmp;\n#define fi(i, from, to) for(int i = (from); i < (to); i++)\n#define fri(i, from, to) for(int i = (from); i >= (to); i--)\n#define fa(it, col) for(auto (it) = (col).begin(); (it) != (col).end(); (it)++)\n#define fra(it, col) for(auto (it) = (col).rbegin(); (it) != (col).rend(); (it)++)\n#define getBit(vv, bit) ((vv & (lol)(1LL << (31 - bit))) == 0LL ? 0 : 1)\n#define getBit64(vv, bit) ((vv & (lol)(1LL << (63 - bit))) == 0LL ? 0 : 1)\n#define vecits(obj) obj.begin(), obj.end()\n#define strip_string(str) (str)[strcspn((str), \"\\r\\n\")] = 0;\n#define read_int() (scanf(\"%d \", &___read_tmp), ___read_tmp)\n#define MOD 1000000007\n#define PI 3.14159265\n#define INF 10000000\n//#define FILE_IO \"peacefulsets\"\n\ntypedef unsigned long long lol;\ntypedef long long slol;\nusing namespace std;\n\nbool used[35000];\nunordered_map mm;\n\nint calc_sg(int vv)\n{\n\tif (vv == 0)\n\t\treturn 0;\n\n\tauto res = mm.find(vv);\n\tif (res != mm.end())\n\t\treturn res->second;\n\n\n\tbool sg_vals[40];\n\tmemset(sg_vals, 0, sizeof(sg_vals));\n\tfi(i, 1, 29)\n\t{\n\t\tif ((vv & (1 << i)) == 0)\n\t\t\tcontinue;\n\n\t\tint v2 = vv;\n\t\tfor (int k = 1; k < 50; k ++)\n\t\t{\n\t\t\tint pos = (i + 1)*k - 1;\n\t\t\tif (pos >= 29)\n\t\t\t\tbreak;\n\n\t\t\tv2 &= ~(1 << pos);\n\t\t}\n\n\t\tsg_vals[calc_sg(v2)] = true;\n\t}\n\n\tint mex = 40;\n\tfi(i, 1, 40)\n\t{\n\t\tif (!sg_vals[i]) {\n\t\t\tmex = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tmm[vv] = mex;\n\treturn mex;\n}\n\nint sg_sizes[30] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5, 9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12 };\nbool skip[35000];\n\nint main() {\n\n#if defined(_DEBUG) \n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#elif defined(FILE_IO)\n\tfreopen(FILE_IO \".in\", \"r\", stdin);\n\tfreopen(FILE_IO \".out\", \"w\", stdout);\n#endif\n\t\n\t/*calc_sg((1 << 29) - 1);\n\tfi(i, 1, 30)\n\t{\n\t\tsg_sizes[i] = mm[(1 << i) - 1];\n\t}\n\t*/\n\n\tint nn;\n\tscanf(\"%d\", &nn);\n\tlol n = nn;\n\n\tlol max_v = 0;\n\tfi(i, 1, n+1)\n\t{\n\t\tif (i*i <= n)\n\t\t\tmax_v = i;\n\t\telse\n\t\t\tbreak;\n\t}\n\n\tint res = 0;\n\tint taken = 0;\n\tfi(i, 2, n)\n\t{\n\t\tif (i*i > n)\n\t\t\tbreak;\n\n\t\tif (skip[i])\n\t\t\tcontinue;\n\n\t\tlol tmp = i, cnt = 0;\n\t\twhile (tmp <= max_v)\n\t\t{\n\t\t\tskip[tmp] = true;\n\t\t\tcnt++;\n\t\t\ttmp *= i;\n\t\t}\n\n\t\twhile (tmp <= n)\n\t\t{\n\t\t\ttmp *= i;\n\t\t\tcnt++; taken++;\n\t\t}\n\n\t\tres ^= sg_sizes[cnt];\n\t}\n\n\tint emp = n - max_v - taken;\n\tif (max_v > 0) emp++;\n\tres ^= emp % 2;\n\n\tif (res != 0)\n\t{\n\t\tprintf(\"Vasya\");\n\t}\n\telse {\n\t\tprintf(\"Petya\");\n\t}\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "games"], "code_uid": "a8af5bde6f86ca2be2075c18fa504893", "src_uid": "0e22093668319217b7946e62afe32195", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define ull unsigned long long\n#define ll long long\n#define ld long double\n#define pb push_back\n#define mp make_pair\n#define iter iterator\n\nusing namespace std;\n\nint main()\n{\n int def1, atk1, def2, atk2, def3, atk3, def4, atk4;\n cin >> def1 >> atk1 >> def2 >> atk2 >> def3 >> atk3 >> def4 >> atk4;\n if ((((atk1 > def3) && (def2 > atk4)) && ((atk1 > def4) && (def2 > atk3))) || (((def1 > atk3) && (atk2 > def4)) && ((def1 > atk4) && (atk2 > def3))))\n cout << \"Team 1\" << endl;\n else if ((((atk3 > def1) && (def4 > atk2)) || ((def3 > atk2) && (atk4 > def1))) && (((def3 > atk1) && (atk4 > def2)) || ((atk3 > def2) && (def4 > atk1))))\n cout << \"Team 2\" << endl;\n else\n cout << \"Draw\" << endl;\n}\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "261607508b3cc9487e132b5a27dcdaeb", "src_uid": "1a70ed6f58028a7c7a86e73c28ff245f", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#define SZ(v) ((int)(v).size())\n#define ALL(v) (v).begin(),(v).end()\n#define one first\n#define two second\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pd;\ntypedef pair pi;\ntypedef pair pli;\ntypedef pair pll; typedef pair plp;\ntypedef tuple ti; typedef tuple tli;\nconst int INF = 0x3f2f1f0f;\nconst ll LINF = 1ll * INF * INF * 2;\n\nll mul(ll a, ll b, ll mod) {\n\ta %= mod;\n\tll res = 0, p = a;\n\twhile(b) {\n\t\tif(b%2) res += p, res %= mod;\n\t\tp += p; p %= mod; b>>=1;\n\t}\n\treturn res;\n}\nll a_b(ll a, ll b, ll mod) {\n\ta %= mod;\n\tll res = 1, p = a;\n\twhile(b) {\n\t\tif(b%2) res = mul(res, p, mod);\n\t\tp = mul(p, p, mod); b>>=1;\n\t}\n\treturn res;\n}\n\n\nconst int MAX_P = 40;\n//if n < 18446744073709551616 = 2^64\n//it is enough to test a = 2 3 5 7 11 13 17 19 23 29 31 and 37 \nvector Ps; bool notP[MAX_P], isFirst = true;\nbool isPrime(ll n) { //Miller–Rabin_primality_test\n\tif(isFirst) {\n\t\tisFirst = false;\n\t\tnotP[0] = notP[1] = true;\n\t\tfor(int i=2; i>= 1, s++;\n\tfor(int x : Ps) if(n == x) return true;\n\tfor(int x : Ps) if(n % x == 0) return false;\n\n\tfor(int x : Ps) {\n\t\tll now = a_b(x, m, n);\n\t\tbool isCom = true;\n\t\tif(now == 1) continue;\n\t\tfor(int i=0; i Div;\nll myF(ll A, ll B) {\n//\tprintf(\"%lld %lld\\n\", A, B);\n\tif(A == 1) return B;\n\tif(B == 0) return 0;\n\tif(isPrime(A)) {\n\t\tll v = B % A; B -= v;\n\t\tv += B / A;\n\t\treturn v;\n\t}\n\tif(B % A == 0) return B / A;\n\t\n\tll g = gcd(A, B);\n\tll cnt = 1, na = A/g;\n\tll b = B/g - 1;\n\n\tif(isPrime(na)) {\n\t\tcnt += b % na;\n\t\tb -= b % na;\n\t}else{\n\t\twhile(gcd(b, na) == 1 && b > 0) b--, cnt++;\n\t}\n\tll ans = cnt + myF(na, b);\n\treturn ans;\n}\nint main() {\n//\tfor(int i=1; i<=7; i++, puts(\"\")) for(int j=0; j<=30; j++) printf(\"%3lld\", getF(i, j));\n\n//\treturn 0;\n\t/*\n\tfor(int i=1; i<=10; i++) for(int j=1; j<=10; j++) {\n\t\tprintf(\"[%d %d]\\n\", i, j);\n\t\tif(myF(i, j) != getF(i, j)) {\n\t\t\tprintf(\"%d %d [%lld %lld]\\n\", i, j, myF(i, j), getF(i, j));\n\t\t\tputs(\"FUCK\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\t//*/\n\tll a, b;\n\tcin >> a >> b;\n\tprintf(\"%lld\\n\", myF(a, b));\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "binary search"], "code_uid": "c9198278975dbc88f6ba65b70798ece4", "src_uid": "ea92cd905e9725e7fcb87b9ed4f64c2e", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#define N 1005\nusing namespace std;\nint n,m,ci,di,dp[N],ai,bi,obj[N],tot,vi[N],wi[N];\nint main()\n{\n\tscanf(\"%d%d%d%d\",&n,&m,&ci,&di);\n\tfor(int i=ci;i<=n;i++)\n\t\tdp[i]=dp[i-ci]+di;\n\tfor(int i=1;i<=m;i++)\n\t{\n\t\ttot=0;\n\t\tscanf(\"%d%d%d%d\",&ai,&bi,&ci,&di);\t\n\t\tint maxb=ai/bi;\n\t\tint base=1;\n\t\twhile(base<=maxb)\n\t\t{\n\t\t\tobj[++tot]=base;\n\t\t\twi[tot]=base*ci;\n\t\t\tvi[tot]=base*di;\n\t\t\tmaxb-=base;\n\t\t\tbase<<=1;\n\t\t}\t\n\t\tif(maxb)\n\t\t{\n\t\t\tobj[++tot]=maxb;\n\t\t\twi[tot]=maxb*ci;\n\t\t\tvi[tot]=maxb*di;\t\t\t\n\t\t}\n\t\tfor(int k=1;k<=tot;k++)\n\t\t\tfor(int j=n;j>=wi[k];j--)\n\t\t\t\t\tdp[j]=max(dp[j],dp[j-wi[k]]+vi[k]);\n\t} \n\tprintf(\"%d\",dp[n]);\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "808a02865e1de5085b44ea8483a77095", "src_uid": "4e166b8b44427b1227e0f811161d3a6f", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#define FIT(a,b) for(vector::iterator a=b.begin();a!=b.end();a++)\n#define FOR(a,b,c) for(int a=(b);a<=(c);++a)\n#define ROF(a,b,c) for(int a=(b);a>=(c);--a)\n#define dbg(x) cout<<#x<<\" = \"<<(x)<<\"\\n\";\n#define pii pair\n#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n#define ll long long\n#define TATA NULL\nusing namespace std;\nconst int MOD = 1000000007;\nconst int buffer = 200004;\nconst int NMAX = 1004;\ninline int Pow(const int x,const int n)\n{\n if(n==0)\n return 1;\n if(n&1)\n return 1LL*x*Pow(x,n-1)%MOD;\n int a = Pow(x,n/2);\n return 1LL*a*a%MOD;\n}\n\nint dp[NMAX], fact[NMAX*NMAX], inv[NMAX*NMAX],n, m, k;\nint dp1[NMAX],sum[NMAX][2];\ninline int Comb(const int n,const int k)\n{\n if(k > n)\n return 0;\n int ret = 1LL*fact[n]*inv[n-k]%MOD;\n return 1LL*ret*inv[k]%MOD;\n}\nint main()\n{\n #ifndef ONLINE_JUDGE\n freopen(\"date.in\",\"r\",stdin);\n freopen(\"date.out\",\"w\",stdout);\n #endif // ONLINE_JUDGE\n cin >> n >> m >> k;\n fact[0] = inv[0] = 1;\n FOR(i,1,max(n,k))\n {\n fact[i] = 1LL*fact[i-1]*i%MOD;\n inv[i] = Pow(fact[i],MOD-2);\n }\n FOR(i,1,n)\n {\n dp[i] = Pow(i,n);\n FOR(j,1,i-1){\n dp[i] -= 1LL*dp[j]*Comb(i,j)%MOD;\n if(dp[i] < 0)\n dp[i] += MOD;\n }\n }\n int ans = 0, x, y, z;\n if(m==1)\n {\n FOR(distincte,1,min(n,k))\n {\n ans += 1LL*dp[distincte]*Comb(k,distincte)%MOD;\n ans %= MOD;\n }\n cout<\n\nusing namespace std;\n\nint main()\n{\n\tint k, n, s, p;\n\tscanf(\"%d%d%d%d\", &k, &n, &s, &p);\n\tint t;\n\tif (n / s == 0)\n\t{\n\t\tt = 1;\n\t}\n\telse\n\t{\n\t\tt = n / s + (n%s == 0 ? 0 : 1);\n\t}\n\tint ans;\n\tans = t*k / p + (t*k%p == 0 ? 0 : 1);\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "c61cc3eab841026d1ddcb8a483fcba65", "src_uid": "73f0c7cfc06a9b04e4766d6aa61fc780", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\nint main() { \n long long a,b;\n cin>>a>>b;\n long long kn=a/(b+1)/2;\n cout<\nusing namespace std;\ntypedef long long ll;\nconst int N=2005,mod=998244353;\nint n;\nll A,B,p,pw[N],pw2[N],f[N],g[N][N],h[N];\nll Pow(ll x,ll k){\n\tll ret=1;\n\twhile(k){\n\t\tif(k&1)ret=ret*x%mod;\n\t\tk>>=1;x=x*x%mod;\n\t}\n\treturn ret;\n}\nll C2(ll n){\n\treturn n*(n-1)/2;\n}\nint main(){\n\tscanf(\"%d%d%d\",&n,&A,&B);\n\tp=(ll)A*Pow(B,mod-2)%mod;\n\tpw[0]=pw2[0]=1;\n\tfor(int i=1;i<=n;i++){\n\t\tpw[i]=pw[i-1]*p%mod;\n\t\tpw2[i]=pw2[i-1]*(1-p)%mod;\n\t}\n\tfor(int i=0;i<=n;g[i++][0]=1)\n\t\tfor(int j=1;j<=i;j++){\n\t\t\tg[i][j]=(g[i-1][j]*pw2[j]+g[i-1][j-1]*pw[i-j])%mod;\n\t\t}\n\tfor(int i=1;i<=n;i++){\n\t\th[i]=1;\n\t\tfor(int j=1;j\n#include\n\nusing namespace std;\n\nint n[15], a[15];\nint cnt = 1;\nchar c;\nint main()\n{\n\twhile(c < '0' || c > '9') c = getchar();\n\twhile(c >= '0' && c <= '9')\n\t{\n\t\tn[cnt] = c - '0';\n\t\tc = getchar();\n\t\tcnt++;\n\t}\n\tint ans = 0;\n\tfor(int i = cnt-1; i >= 1; i--)\n\t{\n\t\tif(i != 1) n[i] -= 9;\n\t\tif(n[i] < 0)\n\t\t{\n\t\t\tn[i-1]--;\n\t\t\tn[i] += 10;\n\t\t}\n\t\tans += 9;\n\t\tans += n[i];\n\t\t//cout <\nusing namespace std;\ntypedef long long ll;\nconstexpr ll N = 1<<18, MOD = 1000000007;\nnamespace dsu {\nint pa[N],rk[N];\nvoid init(int n) {iota(pa,pa+n,0),memset(rk,0,sizeof(int)*n);}\nint anc(int x) {return x==pa[x] ? x : (pa[x]=anc(pa[x]));}\nbool uni(int x,int y) {\n if((x=anc(x))==(y=anc(y))) return false;\n if(rk[x] < rk[y]) swap(x,y);\n return pa[y] = x, rk[x]!=rk[y]||++rk[x];\n}\n}\nusing namespace dsu;\nll mdp(ll e,ll p) {\n ll r = 1;\n while(p) (p&1)&&(r=r*e%MOD), e=e*e%MOD, p>>=1;\n return r;\n}\nsigned main() {\n ios_base::sync_with_stdio(0), cin.tie(0);\n ll n,m,k;\n cin >> n >> m >> k;\n init(n);\n for(int i = 0; i+k-1 < n; i++) {\n for(int j = 0; j < k; j++) {\n int a = i+j;\n int b = i+k-1-j;\n //debug(a), debug(b);\n uni(a,b);\n }\n }\n vector v;\n for(int i = 0; i < n; i++) v.push_back(anc(i));\n sort(v.begin(), v.end()), v.erase(unique(v.begin(),v.end()), v.end());\n //for(int p:v) cout << p << ' '; cout << '\\n';\n cout << mdp(m,v.size()) << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "graphs"], "code_uid": "17959e7048b6fdbfb8ee703d18d76a57", "src_uid": "1f9107e8d1d8aebb1f4a1707a6cdeb6d", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\nunsigned long long n,m;\r\nint main()\r\n{\r\n\tscanf(\"%lld%lld\",&n,&m);\r\n\tif(m==1) printf(\"%lld\",n-1);\r\n\telse printf(\"%lld\",n*(m-1));\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "d41c7ed26347b44c38003bfabde9b2a4", "src_uid": "a91aab4c0618d036c81022232814ef44", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include\n#include\n#include\nusing namespace std;\nchar str[10];\nint a[11];\nint main()\n{\n a[0]=2;\n a[1]=7;\n a[2]=2;\n a[3]=3;\n a[4]=3;\n a[5]=4;\n a[6]=2;\n a[7]=5;\n a[8]=1;\n a[9]=2;\n scanf(\"%s\",str);\n cout<\n#include\n#include\nusing namespace std;\nchar s[110];\nstruct N\n{\n int a;\n char b;\n}a[10];\nint cmp(N a, N b)\n{ return a.a=a[2].a*2||a[0].a*2>a[1].a&&a[3].a\n#include\n#include\nusing namespace std;\n\nint main()\n{\n\tmapm;\n\tm[1]=4; m[2]=4; m[3]=4; m[4]=4; m[5]=4; m[6]=4; m[7]=4; m[8]=4; m[9]=4;\n\tm[10]=15; m[11]=4;\n\tint n;\n\tint k;\n\tint p=0;\n\tcin>>n;\n\tif(n<10) cout<<\"0\";\n\telse if(n-10>11) cout<<\"0\";\n\telse\n\t{\n\t\tk=abs(n-10);\n\t\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define inf 2000000000\n#define INF 1<<29 \n#define s(a) scanf(\"%d\",&a)\n#define me(a,b) memset(a,b,sizeof(a)) \ntypedef long long ll;\nusing namespace std; \nconst int maxn=1e6+5;\nconst int mo=1e9+7;\ninline int read()\n{\n int x=0,f=1;char ch=getchar();\n while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}\n while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}\n return x*f;\n}\npriority_queue,less >q;\n\n\nint main() \n{ \t\n\t//freopen(\"diversity.in\",\"r\",stdin); \n //freopen(\"diversity.out\",\"w\",stdout); \n\tint n;\n\t\n\twhile(~s(n)){\n\t\tfor (int i=1;i<=10000;i++) {\n\t\tif (i*i>=n) {\n\t\t\tprintf(\"%d\\n\",4*i);\n\t\t\treturn 0;\n\t\t} else if (i*(i+1)>=n) {\n\t\t\tprintf(\"%d\\n\",4*i+2);\n\t\t\treturn 0;\n\t\t}\n\t\t}\n\t}\n\t\n\n return 0; \n} \n", "lang_cluster": "C++", "tags": ["brute force", "math", "geometry"], "code_uid": "2f9a4d27b5842d3de94b30c74bcad8da", "src_uid": "414cc57550e31d98c1a6a56be6722a12", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std; \nint main()\n{\n unsigned long long int u=1,v=1,n,p=0,k;\n string s;\n cin>>s;\n n=s.length();\n for(int i=0;is[i+1] ){\n s[i]--;\n p++;\n k++;\n }\n for(int i=0;i\nusing namespace std;\nvector vaibhav1;\nvoid nandan(int o,int limi)\n{\n\tif(o==limi)\n\t{\n\t\treturn;\n\t}\n\tint y=pow(2,o)-1;\n\tfor(int i=0; i<=y; i++)\n\t{\n\t\tint A[o];\n\t\tint w=i;\n\t\tfor(int j=0; j vaibhav;\n\tset :: iterator it ;\n\tfor(int i=n-13; i<=n; i++)\n\t{\n\t\tvaibhav.insert(i);\n\t}\n\tint l=13;\n\tmap yoyo;\n\tint active=0;\n\tfor(int i=n-13; i<=n; i++)\n\t{\n\t\tif(active==1)\n\t\t{\n\t\t\tit=vaibhav.end();\n\t\t\tit--;\n\t\t\tyoyo[i]=*(it);\n\t\t\tvaibhav.erase(it);\n\t\t}\n\t\telse if(k>=A[l]&&l!=0)\n\t\t{\n\t\t int u=(k-1)/A[l];\n\t\t it =vaibhav.begin();\n\t\t if(k%A[l]==0)\n\t\t {\n\t\t \tactive=1;\n\t\t }\n\t\t while(u--)\n\t\t {\n\t\t \tit++;\n\t\t\t }\t\n\t\t\tyoyo[i]=*it;\n\t\t\t//cout<<*it<<\" \"<>n>>k;\n\t\n\tif(n<=14)\n\t{\n\t\tfor(long long int i=1;i<=n; i++)\n\t\t{\n\t\t\tsum=sum*i;\t\n\t\t}\n\t\tif(sumn)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tif(f(vaibhav1[i],k,n))\n\t\t{\n\t\t\tans++;\n\t\t}\n\t}\n\tcout<>t;\n t=1;\n \n while(t--)\n {\n \t solve();\n }\n}", "lang_cluster": "C++", "tags": ["brute force", "combinatorics", "number theory"], "code_uid": "4402ee8eec9e19e00b7fcc9c1b5b666e", "src_uid": "cb2aa02772f95fefd1856960b6ceac4c", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\nint main()\n{\n int a, b, c, x;\n cin >> a >> b >> c;\n x=(a*b)+(b*c)+(c*a)-a-b-c+1;\n cout << x << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "52f008db123afa7b9738bb43b238635b", "src_uid": "8ab25ed4955d978fe20f6872cb94b0da", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define ll long long int\n#define N 510\n#define mod 998244353\nusing namespace std;\nint t, n, x;\nll dp[N][N], ans, fact[N], pwd[N][N], c[N][N];\nll power(int x, int n){\n\tif(x < N && n < N && pwd[x][n] != -1) {\n\t\treturn pwd[x][n];\n\t}\n\tif(n == 0)\n\t\treturn 1ll;\n\tll y = power(x, n/2);\n\ty = (y*y) % mod;\n\tif(n % 2)\n\t\treturn (1ll * x * y) %mod;\n\treturn y;\n}\nll ncr(int n, int r){\n\tif(r > n)\n\t\treturn 0ll;\n\treturn c[n][r];\n}\nint main() {\n\t#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n\t#endif\n\n\tios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n cout.precision(10);\n fact[0] = 1ll;\n for(int i=1; i < N; i++) {\n \tfact[i] = (1ll * i * fact[i-1]) % mod;\n }\n for(int i = 0; i < N; i++) {\n \tfor(int j = 0; j < N; j++) {\n \t\tpwd[i][j] = -1;\n \t\tpwd[i][j] = power(i, j);\n \t}\n }\n\n\tc[0][0] = 1;\n\tfor (int i = 1; i < N; ++i) {\n \t\tc[i][0] = c[i][i] = 1;\n \tfor (int j = 1; j < i; ++j) {\n \tc[i][j] = (c[i - 1][j - 1] + c[i - 1][j])%mod;\n \t}\n\t}\n cin >> n >> x;\n for(int i = 2; i <= n; i++) {\n \tfor(int k=1; k <= x; k++) {\n \t\tif(k < i) {\n \t\t\tdp[i][k] = power(k, i) - power(k-1, i);\n \t\t\tif(dp[i][k] < 0ll)\n \t\t\t\tdp[i][k] += mod;\n \t\t} else {\n \t\t\tfor(int j = i; j > 1; j--) {\n \t\t\t\tdp[i][k] = (dp[i][k] + ( (ncr(i, i-j) * power(i-1, (i-j)) ) % mod * dp[j][k - (i-1)]) % mod) % mod;\n \t\t\t}\n \t\t}\n \t\tif(i == n) {\n \t\t\tans = (ans + dp[i][k]) % mod;\n \t\t}\n \t}\n }\n cout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics"], "code_uid": "1b0145b5af5112882b72a3102f2bcf12", "src_uid": "1908d1c8c6b122a4c6633a7af094f17f", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nlong long n,p;\nlong long sum;\nlong long tmp; \nint main(){\n cin>>n>>p;\n sum=0;\n for(int i=1;i<=n;i++){\n tmp=i;\n int k=i;\n while(k){\n tmp=tmp*10+k%10;\n k/=10;\n }\n sum+=tmp;\n sum%=p;\n }\n cout<\nusing namespace std;\nint main(){\n\tstring s,ans;\n\tint n;\n\tcin>>n>>s;\n\ts=s+\"##\";\n\tfor(int i=0;i\n#define x first\n#define y second\nusing namespace std;\nlong long m,a,b,sol,lst,ma,t;\nlong long nr,f;\nlong long gaus(long long n)\n{\n n*=n+1;\n n/=2;\n return n;\n}\nlong long solve(long long n,long long m)\n{\n long long ret;\n ret=n*m;\n ret-=gaus(n-1)*a;\n return ret;\n}\nint main()\n{\n cin>>m>>a>>b;\n sol=solve((m/a)+1,m+1);\n lst=b;\n while(b%a)\n {\n while(tm)\n break;\n //cout<m)\n continue;\n sol+=solve(1+(m-f)/a,m-f+1);\n }\n cout<\n\nusing namespace std;\n#define pb push_back\n#define ppb pop_back\n#define pf push_front\n#define ppf pop_front\n#define mmp make_pair\n#define st first\n#define nd second\n#define rd third\n#define ll long long\n#define pii pair \n#define tri pair \n#define itr pair \n#define pq priority_queue\n#define all(v) (v).begin(), (v).end()\n#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++)\n#define FOR(i, a, n) for (int (i) = (a); (i) < (int)(n); i++)\n#define FORN(i, a, n) for (int (i) = (a); (i) > (int)(n); i--)\n#define Rep(i, n) for (int (i) = 0; (i) <= (int)(n); i++)\n#define For(i, a, n) for (int (i) = (a); (i) <= (int)(n); i++)\n#define Forn(i, a, n) for (int (i) = (a); (i) >= (int)(n); i--)\n#define rep(i, n) for (int (i) = 1; (i) <= (int)(n); i++)\n#define pi 3.1415926535897932384626433832795028841971\n#define n 8\nint a[n][n];\nint dx[4]={1,-1,0,0};\nint dy[4]={0,0,1,-1};\nbool check(){\n\tint i,j,k;\n\tREP(i,n)REP(j,n)if(a[i][j]==1||a[i][j]==2){\n\t\tREP(k,4){\n\t\t\tint x=i+dx[k];\n\t\t\tint y=j+dy[k];\n\t\t\tfor(;0<=x&&x0){\n\t\tint x1,y1,x2,y2,x3,y3,x4,y4;\n\t\tx1=r1[0]-'a',y1=r1[1]-'1';\n\t\tx2=r2[0]-'a',y2=r2[1]-'1';\n\t\tx3=kw[0]-'a',y3=kw[1]-'1';\n\t\tx4=kb[0]-'a',y4=kb[1]-'1';\n\t\tbool mate=true;\n\t\tint x,y;\n\t\tfor(x=x4-1;x<=x4+1;x++)for(y=y4-1;y<=y4+1;y++){\n\t\t\tif(0<=x&&x1||abs(y-y3)>1){\n\t\t\t\t\tREP(i,8)REP(j,8)a[i][j]=0;\n\t\t\t\t\ta[x1][y1]=1;\n\t\t\t\t\ta[x2][y2]=2;\n\t\t\t\t\ta[x3][y3]=3;\n\t\t\t\t\ta[x][y]=4;\n\t\t\t\t\tif(!check())mate=false;\n\t\t\t\t} \n\t\t}\n\t\tputs(mate?\"CHECKMATE\":\"OTHER\");\n\t}\n\treturn 0;\n}\n/*\na6 b4 c8 a8\na6 c4 b6 b8\na2 b1 a3 a1\n*/", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9aab381ce9ed01646f74b17ada2b29ad", "src_uid": "5d05af36c7ccb0cd26a4ab45966b28a3", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "\n\n#include \n#include \n\nusing ll = long long;\n\nll n, v, rest, l, r, mid;\n\nint ok(ll x)\n{\n\tv = 0;\n\trest = n;\n\twhile (rest > 0)\n\t{\n\t\tv += std::min(x, rest);\n\t\trest -= std::min(x, rest);\n\t\trest -= rest / 10;\n\t}\n\treturn (v * 2 >= n);\n}\n\nint main()\n{\n\tstd::cin >> n;\n\tl = 1, r = n;\n\twhile (l < r)\n\t{\n\t\tmid = (l + r) >> 1;\n\t\tif (ok(mid))\n\t\t\tr = mid;\n\t\telse\n\t\t\tl = mid + 1;\n\t}\n\tstd::cout << l << std::endl;\n}", "lang_cluster": "C++", "tags": ["implementation", "binary search"], "code_uid": "acdd5a5b1c38d32703585c81b2efa5d1", "src_uid": "db1a50da538fa82038f8db6104d2ab93", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include\nusing namespace std;\nint main() \n\n{ \nint n,m,min;\ncin>>n>>m;\n\nif(m\n#include \n#include \n#include \n#include \n#include \n#include \n\nstd::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());\n\nconst int ms = 5001000;\nconst int lim_n = 3e5;\nconst int lim_p = 3e2;\n\nstd::vector primes;\nint id[ms];\nint memo[lim_n][lim_p];\nvoid pre() {\n\tstd::vector isPrime(ms, true);\n\tfor(int i = 2; i < ms; i++) {\n\t\tid[i] = (int) primes.size();\n\t\tif(!isPrime[i]) continue;\n\t\tid[i]++;\n\t\tprimes.push_back(i);\n\t\tfor(int j = i+i; j < ms; j += i) {\n\t\t\tisPrime[j] = false;\n\t\t}\n\t}\n\tfor(int i = 1; i < lim_n; i++) {\n\t\tmemo[i][0] = i;\n\t\tfor(int j = 1; j < lim_p; j++) {\n\t\t\tmemo[i][j] = memo[i][j-1] - memo[i/primes[j-1]][j-1];\n\t\t}\n\t}\n}\n\nint cbc(long long n) {\n\tint ans = std::max(0, (int) pow((double) n, 1.0 / 3) - 2);\n\twhile((long long) ans * ans * ans < n) ans++;\n\treturn ans;\n}\n\n\nlong long dp(long long n, int i) {\n\tif(n == 0) return 0;\n\tif(i == 0) return n;\n\tif((long long) primes[i-1] * primes[i-1] > n && n < ms) {\n\t\treturn id[n] - (i-1);\n\t} else if(n < lim_n && i < lim_p) {\n\t\treturn memo[n][i];\n\t} else {\n\t\treturn dp(n, i-1) - dp(n / primes[i-1], i-1);\n\t}\n}\n\nlong long primeFunction(long long n) {\n\tif(n < ms) {\n\t\treturn id[(int)n];\n\t}\n\tint i = id[cbc(n)];\n\tlong long ans = dp(n, i) + i - 1;\n\twhile((long long) primes[i] * primes[i] <= n) {\n\t\tans -= primeFunction(n / primes[i]) - i;\n\t\ti++;\n\t}\n\treturn ans;\n}\n\nint main() {\n\tstd::ios_base::sync_with_stdio(false); std::cin.tie(NULL);\n\tpre();\n\tlong long n;\n\tstd::cin >> n;\n\tlong long ans = 0;\n\tfor(int i = 0; i < (int) primes.size(); i++) {\n\t\tif((long long) primes[i] * primes[i] > n) break;\n\t\tif((long long) primes[i] * primes[i] * primes[i] <= n) ans++;\n\t\tans += primeFunction(n / primes[i]) - (i+1);\n\t}\n\tstd::cout << ans << '\\n';\n}", "lang_cluster": "C++", "tags": ["math", "dp", "sortings", "data structures", "two pointers", "number theory"], "code_uid": "b77bcd36df67bc82409f29f7299dba9d", "src_uid": "ffb7762f1d60dc3f16e9b27ea0ecdd7d", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include\nusing namespace std;\n \nint main() {\n\tint x1, x2, y1, y2;\n\tlong long m,n;\n\tcin>>x1>>y1>>x2>>y2;\n\tm=x2-x1;\n\tn=y2-y1;\n\tif(m % 2 == 0){\n\t\tcout<\n\n#define ll long long\n#define pb push_back\n#define an \"\\n\"\n\nusing namespace std;\n\n\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tstring s;\n\tcin >> s;\n\tif(s.size()==2) {\n\t\tif(stoi(s)>=10 && stoi(s)<=19) {\n\t\t\tif(s[1]=='0') {\n\t\t\t\tcout << \"ten\";\n\t\t\t}else if(s[1]=='1') {\n\t\t\t\tcout << \"eleven\";\n\t\t\t}else if(s[1]=='2') {\n\t\t\t\tcout << \"twelve\";\n\t\t\t}else if(s[1]=='3') {\n\t\t\t\tcout << \"thirteen\";\n\t\t\t}else if(s[1]=='4') {\n\t\t\t\tcout << \"fourteen\";\n\t\t\t}else if(s[1]=='5') {\n\t\t\t\tcout << \"fifteen\";\n\t\t\t}else if(s[1]=='6') {\n\t\t\t\tcout << \"sixteen\";\n\t\t\t}else if(s[1]=='7') {\n\t\t\t\tcout << \"seventeen\";\n\t\t\t}else if(s[1]=='8') {\n\t\t\t\tcout << \"eighteen\";\n\t\t\t}else if(s[1]=='9') {\n\t\t\t\tcout << \"nineteen\";\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\n\t\tif(s[1]=='0') {\n\t\t\tif(s[0]=='2') {\n\t\t\t\tcout << \"twenty\";\n\t\t\t}else if(s[0]=='3') {\n\t\t\t\tcout << \"thirty\";\n\t\t\t}else if(s[0]=='4') {\n\t\t\t\tcout << \"forty\";\n\t\t\t}else if(s[0]=='5') {\n\t\t\t\tcout << \"fifty\";\n\t\t\t}else if(s[0]=='6') {\n\t\t\t\tcout << \"sixty\";\n\t\t\t}else if(s[0]=='7') {\n\t\t\t\tcout << \"seventy\";\n\t\t\t}else if(s[0]=='8') {\n\t\t\t\tcout << \"eighty\";\n\t\t\t}else if(s[0]=='9') {\n\t\t\t\tcout << \"ninety\";\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t\tif(s[1]!='0') {\n\t\t\tif(s[0]=='2') {\n\t\t\t\tcout << \"twenty\";\n\t\t\t}else if(s[0]=='3') {\n\t\t\t\tcout << \"thirty\";\n\t\t\t}else if(s[0]=='4') {\n\t\t\t\tcout << \"forty\";\n\t\t\t}else if(s[0]=='5') {\n\t\t\t\tcout << \"fifty\";\n\t\t\t}else if(s[0]=='6') {\n\t\t\t\tcout << \"sixty\";\n\t\t\t}else if(s[0]=='7') {\n\t\t\t\tcout << \"seventy\";\n\t\t\t}else if(s[0]=='8') {\n\t\t\t\tcout << \"eighty\";\n\t\t\t}else if(s[0]=='9') {\n\t\t\t\tcout << \"ninety\";\n\t\t\t}\n\t\t\tcout << \"-\";\n\t\t\tif(s[1]=='1') {\n\t\t\t\tcout << \"one\";\n\t\t\t}else if(s[1]=='2') {\n\t\t\t\tcout << \"two\";\n\t\t\t}else if(s[1]=='3') {\n\t\t\t\tcout << \"three\";\n\t\t\t}else if(s[1]=='4') {\n\t\t\t\tcout << \"four\";\n\t\t\t}else if(s[1]=='5') {\n\t\t\t\tcout << \"five\";\n\t\t\t}else if(s[1]=='6') {\n\t\t\t\tcout << \"six\";\n\t\t\t}else if(s[1]=='7') {\n\t\t\t\tcout << \"seven\";\n\t\t\t}else if(s[1]=='8') {\n\t\t\t\tcout << \"eight\";\n\t\t\t}else if(s[1]=='9') {\n\t\t\t\tcout << \"nine\";\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}else {\n\t\tif(s[0]=='0') {\n\t\t\tcout << \"zero\";\n\t\t\treturn 0;\n\t\t}\n\t\tif(s[0]=='1') {\n\t\t\tcout << \"one\";\n\t\t}else if(s[0]=='2') {\n\t\t\tcout << \"two\";\n\t\t}else if(s[0]=='3') {\n\t\t\tcout << \"three\";\n\t\t}else if(s[0]=='4') {\n\t\t\tcout << \"four\";\n\t\t}else if(s[0]=='5') {\n\t\t\tcout << \"five\";\n\t\t}else if(s[0]=='6') {\n\t\t\tcout << \"six\";\n\t\t}else if(s[0]=='7') {\n\t\t\tcout << \"seven\";\n\t\t}else if(s[0]=='8') {\n\t\t\tcout << \"eight\";\n\t\t}else if(s[0]=='9') {\n\t\t\t\tcout << \"nine\";\n\t\t}\n\t}\n}\n\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "e4504888fa7b87b6af5bafe0027b2c9a", "src_uid": "a49ca177b2f1f9d5341462a38a25d8b7", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n\nint main()\n{\n int n,a,x,b,y,flag=0,i;\n cin>>n>>a>>x>>b>>y;\n\n for( i=1;i<=n;i++)\n {\n\n \n if(a==b)\n {\n cout<<\"YES\"<\n\nusing namespace std;\n\nint n, a, b ;\n\nint main(){\n\t\n cin >> n >> a >> b;\n \n cout << ((((a + b) % n + n - 1) % n) + 1) ;\n \n return 0;\n \n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "2e36c22b33b5e5d6b00741f7d3adbb9f", "src_uid": "cd0e90042a6aca647465f1d51e6dffc4", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\r\nusing namespace std;\r\n\r\nconst int N = 2e6 + 5;\r\n\r\nint t;\r\nint n, m, k;\r\n\r\nint main() {\r\n\tcin >> t;\r\n\twhile (t--) {\r\n\t\tcin >> n >> m >> k;\r\n\t\tif (n * m % 2)cout << \"NO\" << endl;\r\n\t\tif (n == 1) {\r\n\t\t\tif (k != m / 2)cout << \"NO\" << endl;\r\n\t\t\telse cout << \"YES\" << endl;\r\n\t\t}\r\n\t\telse if (m == 1) {\r\n\t\t\tif (k)cout << \"NO\" << endl;\r\n\t\t\telse cout << \"YES\" << endl;\r\n\t\t}\r\n\t\telse {\r\n\t\t\tif (m % 2) {\r\n\t\t\t\tif (k > n * (m / 2)) {\r\n\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t}\r\n\t\t\t\telse if (k % 2) {\r\n\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t}\r\n\t\t\t\telse {\r\n\t\t\t\t\tcout << \"YES\" << endl;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse if (n % 2) {\r\n\t\t\t\tif (k < m / 2) {\r\n\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t}\r\n\t\t\t\telse if ((m / 2) % 2) {\r\n\t\t\t\t\tif (k % 2) {\r\n\t\t\t\t\t\tcout << \"YES\" << endl;\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\telse {\r\n\t\t\t\t\tif (k % 2) {\r\n\t\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\tcout << \"YES\" << endl;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tif (k % 2) {\r\n\t\t\t\t\tcout << \"NO\" << endl;\r\n\t\t\t\t}\r\n\t\t\t\telse {\r\n\t\t\t\t\tcout << \"YES\" << endl;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn 0;\r\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "298bf80aa571c546816ed85f6aa5d15a", "src_uid": "4d0c0cc8faca62eb6384f8135b30feb8", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\n\nint main() {\n\tdouble x, n = 0;\n\tcin >> x;\n\n\twhile (x > 0) {\n\t\tn++;\n\t\tx -= (pow(n, 2) + n) / 2;\n\t}\n\n\tif (x < 0)\n\t\tcout << n - 1;\n\telse\n\t\tcout << n;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9fdcd130c389658db49903dedd7a87da", "src_uid": "873a12edffc57a127fdfb1c65d43bdb0", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long uint64;\n#define two(X) (1<<(X))\n#define twoL(X) (((int64)(1))<<(X))\n#define contain(S,X) (((S)&two(X))!=0)\n#define containL(S,X) (((S)&twoL(X))!=0)\nconst double pi=acos(-1.0);\nconst double eps=1e-11;\ntemplate inline void checkmin(T &a,T b){if(b inline void checkmax(T &a,T b){if(b>a) a=b;}\ntemplate inline T sqr(T x){return x*x;}\ntypedef pair ipair;\n#define SIZE(A) ((int)A.size())\n#define LENGTH(A) ((int)A.length())\n#define MP(A,B) make_pair(A,B)\n#define PB(X) push_back(X)\n\n//#define _MSC_VER 1\n\nint n,k,m,a;\nstruct node{\n\tint num;\n\tint t;\n\tint index;\n\tint place;\n\tnode(int a,int b,int c,int d){num=a;t=b;index=c;place=d;}\n\tnode(){\n\t\tnum=0;\n\t\tt=200;\n\t}\n};\nnode ar[200];\nbool compare1(node a,node b){\n\tif(a.num!=b.num){\n\t\treturn a.num>b.num;\n\t}\n\telse\n\t\treturn a.t>tmp;\n\t\tar[tmp].num++;\n\t\tar[tmp].t=i;\n\t}\n\tsort(ar+1,ar+n+1,compare1);\n\tint tt=ar[k+1].num;\n\tif(k!=n){\n\t\tfor(int i=k;i>0;i--){\n\t\t\tsum[i]=tt;\n\t\t\ttt+=ar[i].num;\n\t\t}\n\n\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tif(i<=k){\n\t\t\tif(m-a==0){\n\t\t\t\tif(ar[i].num==0){\n\t\t\t\tar[i].place=3;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tar[i].place=1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(ar[i].num==0){\n\t\t\t\tar[i].place=2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint end=k+1;\n\t\t\tif(end>n)\n\t\t\t\tar[i].place=1;\n\t\t\telse{\n\t\t\t\tint need=(end-i)*(ar[i].num+1)-sum[i];\n\t\t\t\tif((m-a\n#include\n#include\nusing namespace std;\nint flag,maxx;\nchar st[210000];\nint b[30],n;\nint main()\n{\n\tscanf(\"%d\",&n);\n\tscanf(\"%s\",st);\n\tfor (int i=n;i;i--)\n\t\tfor (int j=0;j='a'&&st[k]<='z')\n\t\t\t\t{\n\t\t\t\t\tb[st[k]-'a']++;\n\t\t\t\t} else flag=1;\n\t\t\tif (flag) continue;\n\t\t\tint ans=0;\n\t\t\tfor (int k=0;k<26;k++) ans+=b[k]>0;\n\t\t\tmaxx=max(maxx,ans);\n\t\t}\n\tprintf(\"%d\\n\",maxx);\n}", "lang_cluster": "C++", "tags": ["brute force", "strings", "implementation"], "code_uid": "132063285a1c24052216b497e88c51d9", "src_uid": "567ce65f87d2fb922b0f7e0957fbada3", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n\nusing std::cin;\nusing std::cout;\nusing std::make_pair;\n\nint main() {\n std::ios::sync_with_stdio(false);\n int n, x, y, count = 0;\n cin >> n;\n std::multimap m;\n for (int i = 0; i < n; ++i) {\n cin >> x >> y;\n m.insert(make_pair(x, y));\n }\n\n for (auto i = m.begin(); i != m.end(); ++i) {\n for (auto j = std::next(i,1); j != m.end(); ++j) {\n if (i->first == j->second) {\n count++;\n }\n if (i->second == j->first) {\n count++;\n }\n }\n }\n cout << count;\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force"], "code_uid": "b927bfefc3648f9f22cd7bcaa457c452", "src_uid": "745f81dcb4f23254bf6602f9f389771b", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define inp_out_work ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n#define testCase int T; cin>>T; while(T--)\n#define fr(i,n) for(int i=0;i=0;i--)\n#define fro(i,s,e) for(int i=s;i<=e;i++)\n#define pb push_back\n#define debug(P) cout<<#P<<' '< primes;\nbool prime[MAXN];\n\nvoid sieve(){\n fr(i,MAXN)prime[i]=true;\n prime[0]=prime[1]=false;\n for(int i=2;i*i>=1;\n }\n return res;\n}\n\n//vector adj[MAXN];\n\nint dp[26][51];\n\nint n , k;\n\nbool p[27];\nint fun(int i , int l ){\n if(l == k){return 0;}\n\n if(i >= 26) return 100000;\n\n if(dp[i][l])return dp[i][l];\n\n if(p[i])return dp[i][l] = min( fun(i+1 , l) , i + 1 + fun(i+2 , l+1) );\n else return dp[i][l] = fun(i+1 , l);\n}\n\nint main(){\n cin>>n>>k;\n string s;\n cin>>s;\n if( k > 13){ cout<< -1 << endl; exit(0);}\n fr(i,s.length()){\n p[s[i] - 'a'] = 1;\n }\n\n int ans = fun( 0 , 0 ) ;\n\n if(ans == 100000)cout<<\"-1\\n\";\n else cout<\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\n\nvector> mul(vector> a, vector> b) {\n int n = a.size();\n int k = b.size();\n int m = b[0].size();\n vector> c(n, vector(m, 1e10));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n for (int z = 0; z < k; z++) {\n c[i][j] = min(c[i][j], a[i][z] + b[z][j]);\n }\n }\n }\n return c;\n}\n\nvector> fast_pow(vector> v, int p) {\n vector> res = v;\n p--;\n while (p) {\n if (p & 1) {\n res = mul(res, v);\n p--;\n } else {\n v = mul(v, v);\n p /= 2;\n }\n }\n return res;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int n, m;\n cin >> n >> m;\n vector a(n), b(n);\n for (int &e : a) {\n cin >> e;\n }\n for (int &e : b) {\n cin >> e;\n }\n vector> v(2 * n, vector(2 * n, 1e10));\n ll dp[2 * n][2 * n][2 * n];\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < 2 * n; j++) {\n for (int z = 0; z < 2 * n; z++) {\n dp[i][j][z] = 1e10;\n }\n }\n }\n for (int i = 0; i < 2 * n; i++) {\n dp[0][i][i] = 0;\n }\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j < 2 * n; j++) {\n for (int z = 0; z < 2 * n; z++) {\n if (z && dp[i - 1][j][z - 1] != 1e10) {\n dp[i][j][z] = min(dp[i][j][z], dp[i - 1][j][z - 1] + a[i - 1]);\n }\n if (z + 1 < 2 * n && dp[i - 1][j][z + 1] != 1e10) {\n dp[i][j][z] = min(dp[i][j][z], dp[i - 1][j][z + 1] + b[i - 1]);\n }\n }\n }\n }\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < 2 * n; j++) {\n v[i][j] = dp[n][i][j];\n }\n }\n v = fast_pow(v, m);\n cout << v[0][0];\n}", "lang_cluster": "C++", "tags": ["matrices", "dp"], "code_uid": "dc164819f8a95c6361d979d19b1761f5", "src_uid": "f40900973f4ebeb6fdafd75ebe4e9601", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define sz(v) (int) v.size()\n#define all(v) v.begin(),v.end()\n#define in freopen(\"input.txt\",\"r\",stdin);\n#define out freopen(\"output.txt\",\"w\",stdout);\n#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\nvector isPrime(1e7 + 1, 1), ispal(1e7 + 1, 0);\n\nvoid sieve(int n) {\n\tisPrime[0] = isPrime[1] = 0;\n\tfor (long long i = 2; i * i <= n; ++i) {\n\t\tif (isPrime[i]) {\n\t\t\tfor (int j = i * 2; j <= n; j += i)\n\t\t\t\tisPrime[j] = 0;\n\t\t}\n\t}\n}\n\nbool ispalindrome(string s) {\n\tbool flag=1;\n\tfor(int i=0;i> a >> b;\n\n\tfor (int i = 0; i < 2000000; i++) {\n\t\tstring s = to_string(i);\n\t\tif (ispalindrome(s)) {\n\t\t\tispal[i] = 1;\n\t\t}\n\t}\n\n\tint pal = 0, pr = 0, ans = -1;\n\n\tfor (int i = 1; i <= 2000000; i++) {\n\t\tif(isPrime[i]) pr++;\n\t\tif(ispal[i]) pal++;\n\t\tif(pr*b<=pal*a) {\n\t\t\tans=i;\n\t\t}\n\t}\n\n\tif(ans==-1) {\n\t\tcout << \"Palindromic tree is better than splay tree\";\n\t}\n\telse {\n\t\tcout << ans;\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "binary search", "number theory"], "code_uid": "601d62594278677016afb3c580519482", "src_uid": "e6e760164882b9e194a17663625be27d", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n \nusing namespace std;\n \n#define forn(i, n) for(int i = 0; i < (int)(n); i++)\n#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)\n#define pb push_back\n#define mp make_pair\n#define fs first\n#define sc second\n#define last(a) int(a.size() - 1)\n#define all(a) a.begin(), a.end()\n#define seta(a,x) memset (a, x, sizeof (a))\n#define I (int)\n#define SZ(x) ((int) (x).size())\n#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) \n\ntypedef long long int int64;\ntypedef unsigned long long int uint64;\ntypedef long double ldb;\ntypedef pair pii;\ntypedef vectorvi;\ntypedef vectorvs;\n \n\n \ntemplate T sqr (T x) {return x * x;}\nint main()\n{\n int al,ar;\n int bl,br;\n cin>>al>>ar;\n cin>>bl>>br;\n\n if((al>=br-3&&al<=br+1)||(ar>=bl-3&&ar<=bl+1)||(br>=al-1&&br<=2*(al+1))||(bl>=ar-1&&bl<=2*(ar+1)))\n {\n printf(\"YES\\n\");\n }\n else\n printf(\"NO\\n\");\n\nreturn 0;\n}\n\n\n \n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "e1f655dbcb22e41c04d7a19fdafdf63e", "src_uid": "36b7478e162be6e985613b2dad0974dd", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*input\n4 5\nasdf\n\n\n*/\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair PII;\ntypedef pair PLL;\ntypedef long double ld;\n\n#define pb push_back\n#define all(c) c.begin(),c.end()\n#define allr(c) c.rbegin(),c.rend()\nint mod = 1000000007;\nconst int INF = 1034567891;\nconst ll LL_INF = 1234567890123456789ll;\n#define PI 3.14159265\n#define endl '\\n'\n#define F first\n#define S second\n#define debug(x) cout << #x << \" = \" << x << endl;\n#define TRACE\n\n#ifdef TRACE\n#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)\ntemplate \nvoid __f(const char* name, Arg1&& arg1){\n cout << name << \" : \" << arg1 << endl;\n}\ntemplate \nvoid __f(const char* names, Arg1&& arg1, Args&&... args){\n const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << \" : \" << arg1<<\" | \";__f(comma+1, args...);\n}\n#else\n#define trace(...)\n#endif\n\n#define out(container) for (auto it : container) cout << it << \" \"; cout << endl;\n\n\ntemplate < typename T > T GCD(T a, T b) { ll t; while(a) { t = a; a = b % a; b = t; } return b; }\ntemplate < typename T > string toString(T a) { return to_string(a); }\ntemplate < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}\ninline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}\ninline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}\ninline int mul(int x, int y){ return (x * 1ll * y) % mod;}\ninline int powr(int a, ll b){\n int x = 1 % mod;\n while(b){\n if(b & 1) x = mul(x, a);\n a = mul(a, a);\n b >>= 1;\n }\n return x;\n}\ninline int inv(int a){ return powr(a, mod - 2);}\n\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); \n\n ll n, k;\n cin >> n >> k;\n string str;\n cin >> str;\n vector > lst(n, vector (26, -1));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 26; j++) {\n if (i) lst[i][j] = lst[i - 1][j];\n }\n lst[i][str[i] - 'a'] = i;\n }\n vector > dp(n + 1, vector (n + 1));\n for (int i = 0; i < n; i++) {\n dp[i][1] = 1;\n }\n for (int len = 2; len <= n; len++) {\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j < 26; j++) {\n if (lst[i - 1][j] != -1) {\n dp[i][len] = min((ll)1e12, dp[i][len] + dp[lst[i - 1][j]][len - 1]);\n }\n }\n }\n }\n\n ll ans = 0;\n for (int i = n; i >= 0 && k; i--) {\n ll cnt = 0;\n for (int j = 0; j < 26; j++) {\n if (lst[n - 1][j] != -1) {\n cnt += dp[lst[n - 1][j]][i];\n }\n }\n if (i == 0) cnt = 1;\n if (k >= cnt) {\n ans += cnt * 1ll * (n - i);\n k -= cnt;\n }\n else {\n ans += k * 1ll * (n - i);\n k = 0;\n break;\n }\n }\n cout << (k == 0 ? ans : -1) << endl;\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["strings", "dp"], "code_uid": "7b501bc22452a24edefebac6b945a3f9", "src_uid": "ae5d21919ecac431ea7507cb1b6dc72b", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \nusing namespace std;\nint main() {\n long long n;\n cin >> n;\n long long ans = 0;\n if (n == 1) {\n cout << 1; \n return 0;\n }\n if (n == 2) {\n cout << 3; \n return 0;\n }\n if (n == 3) {\n cout << 7;\n return 0;\n }\n for (long long i = 0; i < n - 1ll; i++) {\n ans += (i + 1ll) * (n - i - 1) + 1ll;\n }\n cout << ans + 1ll;\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "9e05a0e8f279664dc8dca679419edcba", "src_uid": "6df251ac8bf27427a24bc23d64cb9884", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\nconst int MAXN=500005;\nint n, m, q, num, dfc;\nint a[MAXN], w[MAXN];\nint u[MAXN], v[MAXN], f[MAXN];\nint opt[MAXN], ob[MAXN], p[MAXN];\nbool cut[MAXN];\nint op[MAXN], cl[MAXN];\nvector G[MAXN];\nint getf(int x)\n{\n\tif (x==f[x]) return x;\n\treturn f[x]=getf(f[x]);\n}\nvoid merge(int u, int v)\n{\n\tint x=getf(u), y=getf(v);\n\tif (x!=y)\n\t{\n\t\tf[x]=f[y]=++num;\n\t\tG[num].push_back(x);\n\t\tG[num].push_back(y);\n\t}\n}\nvoid DFS(int u)\n{\n\top[u]=++dfc;\n\tfor (int v: G[u]) DFS(v);\n\tcl[u]=dfc;\n}\nnamespace SGT\n{\n\tstruct Node\n\t{\n\t\tint l, r;\n\t\tpair val;\n\t} tr[4*MAXN];\n#define lc (o<<1)\n#define rc (o<<1|1)\n\tvoid pushup(int o)\n\t{\n\t\ttr[o].val=max(tr[lc].val, tr[rc].val);\n\t}\n\tvoid build(int o, int l, int r, int* a)\n\t{\n\t\ttr[o].l=l, tr[o].r=r;\n\t\tif (l==r) return tr[o].val=make_pair(a[l], l), void();\n\t\tint mid=(l+r)>>1;\n\t\tbuild(lc, l, mid, a);\n\t\tbuild(rc, mid+1, r, a);\n\t\tpushup(o);\n\t}\n\tpair query(int o, int l, int r)\n\t{\n\t\tif (tr[o].l>r||tr[o].r>1;\n\t\tif (p<=mid) modify(lc, p);\n\t\telse modify(rc, p);\n\t\tpushup(o);\n\t}\n#undef lc\n#undef rc\n}\nint main()\n{\n//\tfreopen(\"D.in\", \"r\", stdin);\n//\tfreopen(\"D.out\", \"w\", stdout);\n\tscanf(\"%d%d%d\", &n, &m, &q);\n\tfor (int i=1; i<=n; i++) scanf(\"%d\", &a[i]);\n\tfor (int i=1; i<=m; i++) scanf(\"%d%d\", &u[i], &v[i]);\n\tfor (int i=1; i<=n+m; i++) f[i]=i;\n\tfor (int i=1; i<=q; i++)\n\t{\n\t\tscanf(\"%d%d\", &opt[i], &ob[i]);\n\t\tif (opt[i]==2) cut[ob[i]]=1;\n\t}\n\tnum=n;\n\tfor (int i=1; i<=m; i++)\n\t\tif (!cut[i]) merge(u[i], v[i]);\n\tfor (int i=q; i>=1; i--)\n\t\tif (opt[i]==1) p[i]=getf(ob[i]);\n\t\telse merge(u[ob[i]], v[ob[i]]);\n\tfor (int i=1; i<=num; i++)\n\t\tif (getf(i)==i) DFS(i);\n\tfor (int i=1; i<=num; i++) w[op[i]]=a[i];\n\tSGT::build(1, 1, num, w);\n\tfor (int i=1; i<=q; i++)\n\t\tif (opt[i]==1)\n\t\t{\n\t\t\tauto ret=SGT::query(1, op[p[i]], cl[p[i]]);\n\t\t\tprintf(\"%d\\n\", ret.first);\n\t\t\tSGT::modify(1, ret.second);\n\t\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dsu", "graphs", "data structures", "implementation", "trees"], "code_uid": "5371faf0203668a5bb5b057e88adac69", "src_uid": "ad014bde729222db14f38caa521e4167", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define ll long long\n#define IOS ios::sync_with_stdio(false)\n#define For(i, x, y) for(int i = x; i <= y; i++)\n#define For_(i, x, y) for(int i = x; i >= y; i--)\nconst int inf = 0x3f3f3f3f;\nconst ll Inf = 1e18;\nconst int mod = 1e9 + 7;\n\nll fastpow(ll x, ll k)\n{\n\tll base = x;\n\tll ret = 1;\n\twhile (k) {\n\t\tif (k & 1) {\n\t\t\tret *= base;\n\t\t\tret %= mod;\n\t\t}\n\t\tbase *= base;\n\t\tbase %= mod;\n\t\tk >>= 1;\n\t}\n\treturn ret;\n}\n\nint main(void)\n{\n\tll a, b, x, n;\n\tcin >> a >> b >> n >> x;\n\tif (a == 1) {\n\t\tcout << (n % mod * b + x) % mod << endl;\n\t\treturn 0;\n\t}\n\tll tmp = fastpow(a, n);\n\tcout << (((tmp - 1) * fastpow(a - 1, mod - 2) % mod) * b % mod + tmp * (x % mod) % mod) % mod << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7f461c554b6900dc351264aed5afa249", "src_uid": "e22a1fc38c8b2a4cc30ce3b9f893028e", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n\n#define ll long long\n#define clr(x) memset(x,0,sizeof(x))\n#define _clr(x) memset(x,-1,sizeof(x))\n#define fr(i,a,b) for(int i = a; i < b; ++i)\n#define frr(i,a,b) for(int i = a; i > b; --i)\n#define pb push_back\n#define sf scanf\n\n#define pf printf\n#define mp make_pair\n\nusing namespace std;\nconst int N = 400000;\nconst int M = 2000001;\nconst double eps = 1e-7;\n\nconst ll mod = 1000000007;\n\nint n,q;\nint p[M];\nint cnt[M],num[M];\nint fa[M];\nint is[M],pr[M];\nint ct;\n\nvoid init() {\n\tfor(int i = 2; i >n;\n\tfr(i,0,n) {\n\t\tsf(\"%d\",&p[i]);\n\t}\n\tsort(p,p+n);\n\tll ans = 1;\n\tfor(int i = n-1; i >=0; --i) {\n\t\tif(!cnt[p[i]]) {\n\t\t\tcnt[p[i]]=1;\n\t\t\tnum[p[i]]=1;\n\t\t\tans = (ans * p[i])%mod;\n\t\t\t//printf(\"i = %d ans = %lld\\n\",i,ans);\n\t\t}\n\t\telse {\n\t\t\tint v = --p[i];\n\t\t\twhile(v>1) {\n\t\t\t\tint u = fa[v];\n\t\t\t\t//printf(\"i = %d v = %d u = %d\\n\",i,v,u);\n\t\t\t\tint nu = 0;\n\t\t\t\twhile(v%u==0)v/=u,nu++;\n\t\t\t\tif(cnt[u]1) {\n\t\t\tint u = fa[v];\n\t\t\tint nu = 0;\n\t\t\twhile(v%u==0)v/=u,nu++;\n\t\t\t//printf(\"factor = %d nu = %d ans = %lld\\n\",u,nu, ans);\n\t\t\t//printf(\"v = %d factor = %d nu = %d cnt = %d num = %d\\n\",v,u,nu,cnt[u],num[u]);\n\t\t\tif(cnt[u]==nu&&num[u]==1) flag = 1;\n\t\t}\n\n\t\tif(!flag) {\n\t\t\tans++;\n\t\t\tbreak;\n\t\t}\n\t}\n\tcout<\n \nusing namespace std;\n \ntypedef long long ll;\n#define mp make_pair \n#define pb push_back\n#define x first\n#define y second\n#define all(a) a.begin(), a.end()\n#define db long double\n \nint n;\nint a[1000007];\nint ans = 1e9;\n \nint dd[33];\nint kk = 0;\n \nvoid check(int x){\n\tif (a[x] == 0) return;\n\tkk = 0;\n\tint save = x;\n\twhile(1){\n\t\tif (a[x] == 0) break;\n\t\tdd[kk++] = a[x];\n\t\tx /= a[x];\n\t}\n\tdd[kk++] = x;\n\tfor (int i = 0; i < kk; i++){\n\t\tif (save - dd[i] + 1 >= 3) ans = min(ans, save - dd[i] + 1);\n\t}\n}\n \nint main(){\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n ios_base::sync_with_stdio(0); cin.tie(0);\n cin >> n;\n for (int i = 2; i < 1000007; i++) if (a[i] == 0){\n \tfor (int j = 2 * i; j < 1000007; j += i){\n \t\tif (a[j] == 0) a[j] = i;\n \t}\n }\n int save = n;\n set se;\n while(1){\n \tif (a[n] == 0) break;\n \tse.insert(a[n]);\n \tn /= a[n];\n }\n se.insert(n);\n ans = save;\n for (int x : se){\n \tfor (int i = save; i >= save - x + 1; i--){\n \t\tcheck(i);\n \t}\n }\n cout << ans;\n}", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7b769c619758dbb3cee839ab38c26977", "src_uid": "43ff6a223c68551eff793ba170110438", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\n string str;\n cin>>str;\n int count1=0,count2=0;\n for(int i=0;icount1)\n {\n cout<<\"7\";return 0;\n }\n if(count1>count2)\n {\n cout<<\"4\";return 0;\n }\n else if(count1==count2 && count1!=0 && count2!=0)\n {\n cout<<\"4\";return 0;\n }\n cout<<\"-1\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "2ff293d1838e0dce896bf669a41c081f", "src_uid": "639b8b8d0dc42df46b139f0aeb3a7a0a", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// powered by c++11\n// by Isaunoya\n#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize( \\\n \"inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2,-ffast-math,-fsched-spec,unroll-loops,-falign-jumps,-falign-loops,-falign-labels,-fdevirtualize,-fcaller-saves,-fcrossjumping,-fthread-jumps,-funroll-loops,-freorder-blocks,-fschedule-insns,inline-functions,-ftree-tail-merge,-fschedule-insns2,-fstrict-aliasing,-fstrict-overflow,-falign-functions,-fcse-follow-jumps,-fsched-interblock,-fpartial-inlining,no-stack-protector,-freorder-functions,-findirect-inlining,-fhoist-adjacent-loads,-frerun-cse-after-loop,inline-small-functions,-finline-small-functions,-ftree-switch-conversion,-foptimize-sibling-calls,-fexpensive-optimizations,inline-functions-called-once,-fdelete-null-pointer-checks\")\n\n#include \n\n#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)\n#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)\n\nusing namespace std;\nusing db = double;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\nusing pii = pair;\n\n#define fir first\n#define sec second\n\ntemplate \n\nvoid cmax(T& x, const T& y) {\n if (x < y) x = y;\n}\n\ntemplate \n\nvoid cmin(T& x, const T& y) {\n if (x > y) x = y;\n}\n\n#define all(v) v.begin(), v.end()\n#define sz(v) ((int)v.size())\n#define pb emplace_back\n\ntemplate \n\nvoid sort(vector& v) {\n sort(all(v));\n}\n\ntemplate \n\nvoid reverse(vector& v) {\n reverse(all(v));\n}\n\ntemplate \n\nvoid unique(vector& v) {\n sort(all(v)), v.erase(unique(all(v)), v.end());\n}\n\nvoid reverse(string& s) { reverse(s.begin(), s.end()); }\n\nconst int io_size = 1 << 23 | 233;\nconst int io_limit = 1 << 22;\nstruct io_in {\n char ch;\n#ifndef __WIN64\n char getchar() {\n static char buf[io_size], *p1 = buf, *p2 = buf;\n\n return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, io_size, stdin), p1 == p2) ? EOF : *p1++;\n }\n#endif\n io_in& operator>>(char& c) {\n for (c = getchar(); isspace(c); c = getchar())\n ;\n\n return *this;\n }\n io_in& operator>>(string& s) {\n for (s.clear(); isspace(ch = getchar());)\n ;\n\n if (!~ch) return *this;\n\n for (s = ch; !isspace(ch = getchar()) && ~ch; s += ch)\n ;\n\n return *this;\n }\n\n io_in& operator>>(char* str) {\n char* cur = str;\n while (*cur) *cur++ = 0;\n\n for (cur = str; isspace(ch = getchar());)\n ;\n if (!~ch) return *this;\n\n for (*cur = ch; !isspace(ch = getchar()) && ~ch; *++cur = ch)\n ;\n\n return *++cur = 0, *this;\n }\n\n template \n\n void read(T& x) {\n bool f = 0;\n while ((ch = getchar()) < 48 && ~ch) f ^= (ch == 45);\n\n x = ~ch ? (ch ^ 48) : 0;\n while ((ch = getchar()) > 47) x = x * 10 + (ch ^ 48);\n x = f ? -x : x;\n }\n\n io_in& operator>>(int& x) { return read(x), *this; }\n\n io_in& operator>>(ll& x) { return read(x), *this; }\n\n io_in& operator>>(uint& x) { return read(x), *this; }\n\n io_in& operator>>(ull& x) { return read(x), *this; }\n\n io_in& operator>>(db& x) {\n read(x);\n bool f = x < 0;\n x = f ? -x : x;\n if (ch ^ '.') return *this;\n\n double d = 0.1;\n while ((ch = getchar()) > 47) x += d * (ch ^ 48), d *= .1;\n return x = f ? -x : x, *this;\n }\n} in;\n\nstruct io_out {\n char buf[io_size], *s = buf;\n int pw[233], st[233];\n\n io_out() {\n set(7);\n rep(i, pw[0] = 1, 9) pw[i] = pw[i - 1] * 10;\n }\n\n ~io_out() { flush(); }\n\n void io_chk() {\n if (s - buf > io_limit) flush();\n }\n\n void flush() { fwrite(buf, 1, s - buf, stdout), fflush(stdout), s = buf; }\n\n io_out& operator<<(char c) { return *s++ = c, *this; }\n\n io_out& operator<<(string str) {\n for (char c : str) *s++ = c;\n return io_chk(), *this;\n }\n\n io_out& operator<<(char* str) {\n char* cur = str;\n while (*cur) *s++ = *cur++;\n return io_chk(), *this;\n }\n\n template \n\n void write(T x) {\n if (x < 0) *s++ = '-', x = -x;\n\n do {\n st[++st[0]] = x % 10, x /= 10;\n } while (x);\n\n while (st[0]) *s++ = st[st[0]--] ^ 48;\n }\n\n io_out& operator<<(int x) { return write(x), io_chk(), *this; }\n\n io_out& operator<<(ll x) { return write(x), io_chk(), *this; }\n\n io_out& operator<<(uint x) { return write(x), io_chk(), *this; }\n\n io_out& operator<<(ull x) { return write(x), io_chk(), *this; }\n\n int len, lft, rig;\n\n void set(int _length) { len = _length; }\n\n io_out& operator<<(db x) {\n bool f = x < 0;\n x = f ? -x : x, lft = x, rig = 1. * (x - lft) * pw[len];\n return write(f ? -lft : lft), *s++ = '.', write(rig), io_chk(), *this;\n }\n} out;\n#define int long long\n\ntemplate \n\nstruct math_t {\n math_t() {\n fac.resize(sz + 1), ifac.resize(sz + 1);\n rep(i, fac[0] = 1, sz) fac[i] = fac[i - 1] * i % mod;\n\n ifac[sz] = inv(fac[sz]);\n Rep(i, sz - 1, 0) ifac[i] = ifac[i + 1] * (i + 1) % mod;\n }\n\n vector fac, ifac;\n\n int qpow(int x, int y) {\n int ans = 1;\n for (; y; y >>= 1, x = x * x % mod)\n if (y & 1) ans = ans * x % mod;\n return ans;\n }\n\n int inv(int x) { return qpow(x, mod - 2); }\n\n int C(int n, int m) {\n if (n < 0 || m < 0 || n < m) return 0;\n return fac[n] * ifac[m] % mod * ifac[n - m] % mod;\n }\n};\n\nint gcd(int x, int y) { return !y ? x : gcd(y, x % y); }\nint lcm(int x, int y) { return x * y / gcd(x, y); }\n\nconst int maxn = 8e6 + 63;\nconst int P = 998244353;\nconst int G = 3;\nconst int Gi = 332748118;\n\nint n, m, rev[maxn];\nint a[maxn], b[maxn];\nint limit = 1;\nint Inv = 0;\n\ninline int qpow(int x, int y) {\n int ans = 1;\n for (; y; y >>= 1, x = x * x % P)\n if (y & 1) ans = ans * x % P;\n return ans;\n}\n\ninline int inc(const int& x, const int& y) { return (x + y >= P) ? (x + y - P) : x + y; }\ninline int dec(const int& x, const int& y) { return (x - y >= 0) ? x - y : (x - y + P); }\ninline void swap(int& x, int& y) { x ^= y ^= x ^= y; }\n\nvoid NTT(int* A, int type) {\n rep(i, 0, limit) if (i < rev[i]) swap(A[i], A[rev[i]]);\n for (int len = 1; len < limit; len <<= 1) {\n int Wn = qpow(type == 1 ? G : Gi, (P ^ 1) / (len << 1));\n for (int i = 0; i < limit; i += len << 1) {\n int w = 1;\n for (int j = 0, x, y; j < len; j++, w = w * Wn % P) {\n x = A[i + j], y = A[i + j + len] * w % P;\n A[i + j] = inc(x, y), A[i + j + len] = dec(x, y);\n }\n }\n }\n if (type == -1) {\n for (int i = 0; i < limit; i++) A[i] = A[i] * Inv % P;\n }\n}\n\nvoid init(int n) {\n limit = 1;\n int l = 0;\n while (limit <= n) {\n limit <<= 1, ++l;\n }\n for (int i = 0; i < limit; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << l - 1;\n Inv = qpow(limit, P - 2);\n}\n\nsigned main() {\n // code begin.\n in >> n >> m;\n n >>= 1;\n while (m--) {\n int x;\n in >> x;\n b[x] = 1;\n }\n a[0] = 1;\n init(10 << 2);\n while (n) {\n if (n & 1) {\n int mx = 0;\n for (int i = 0; i < limit; i++)\n if (b[i]) mx = i;\n init(mx << 2);\n NTT(a, 1), NTT(b, 1);\n for (int i = 0; i < limit; i++) a[i] = a[i] * b[i] % P;\n NTT(a, -1), NTT(b, -1);\n }\n int mx = 0;\n for (int i = 0; i < limit; i++)\n if (b[i]) mx = i;\n init(mx << 2);\n NTT(b, 1);\n for (int i = 0; i < limit; i++) b[i] = b[i] * b[i] % P;\n NTT(b, -1);\n n >>= 1;\n }\n int ans = 0;\n for (int i = 0; i < limit; i++) ans = inc(ans, (a[i] * a[i]) % P);\n out << ans << '\\n';\n return 0;\n // code end.\n}\n", "lang_cluster": "C++", "tags": ["divide and conquer", "dp", "fft"], "code_uid": "ef658b703a2bef14143a3d0af9ba9fcc", "src_uid": "279f1f7d250a4be6406c6c7bfc818bbf", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"O3\")\n#ifdef ONLINE_JUDGE\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n#endif\n#pragma GCC optimize(\"unroll-loops\")\n#pragma comment(linker, \"/stack:200000000\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#include \n#include \n#include \ntemplate,typename Tag=__gnu_pbds::rb_tree_tag,templateclass Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator>class ordered_set_t:__gnu_pbds::tree{};const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash{}(x^GRANDOM);}};templateclass hash_table_t:__gnu_pbds::gp_hash_table{};namespace vh{templatevoid _R(T&x){std::cin>>x;}inline void _R(int&x){scanf(\"%d\",&x);}inline void _R(int64_t&x){scanf(\"%\" SCNd64,&x);}inline void _R(double&x){scanf(\"%lf\",&x);}inline void _R(char&x){scanf(\" %c\",&x);}inline void _R(char*x){scanf(\"%s\",x);}inline void R(){}templateinline void R(T&head,U&... tail){_R(head);R(tail...);}templatevoid _W(const T&x){cout<inline void _W(const vector&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}templateinline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}templateinline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\\n');W(tail...);}};\n#define prec setprecision\nnamespace vh{typedef long long ll;typedef unsigned long long llu;templateT gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}templateT exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}templateT mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}templateT powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}templateinline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}templateinline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}templatebool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}templatestruct BitSize{enum{Size=BitSize::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};\n#define BITSIZE(n) (BitSize::Size)\n#define BITMAX(n) (BitSize::Size - 1)\n#define DEBUG !defined(ONLINE_JUDGE)\n#define SZ(x) ((int)(x).size())\n#define ALL(x) begin(x), end(x)\n#define RALL(x) (x).rbegin(),(x).rend()\ntemplatevoid _dbg(const char*sdbg,TH h){cerr<void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<\"=\"<int min_rotation(vectors){int a=0,N=s.size();for(int b=0;bs[(b+i)%N]){a=b;break;}}return a;}}namespace vh{templateclass FiniteField{typedef FiniteFieldself;public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}templateself&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}templateself&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}templateself&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}templateself&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}templateself operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}templateself operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}templateself operator*(const NT o){return self(normalize(val*(o)));}operator T()const{return val;}};};namespace vh{};\nnamespace vh{\n const int maxn=3e5+7;\n const ll MOD=998244353;\n int n;\n vectoradj[maxn];\n typedef FiniteFieldnumber_t;\n struct DPE{\n number_t matched;\n number_t unmatched;\n number_t unused;\n };\n DPE solve(int u,int p){\n DPE r={1,0,1};\n vectormatchable;\n vectormatched;\n for(int v:adj[u]){\n if(v!=p){\n auto r2=solve(v,u);\n auto r2matchable=r2.unmatched+r2.unused;\n matchable.push_back(r2matchable);\n matched.push_back(r2.matched);\n r.matched*=r2matchable;\n r.unused*=r2.unused;\n }\n }\n int sz=matchable.size();\n if(sz){\n vectormatchableLeft(sz,0);\n vectormatchableRight(sz,0);\n matchableLeft[0]=1;\n for(int i=1;i=0;i--)matchableRight[i]=matchableRight[i+1]*matchable[i+1];\n for(int i=0;i>n;\n for(int i=2;i<=n;i++){\n int u,v;cin>>u>>v;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n cout<<(solve(1,0).unused).val<\n#include\n#include\nusing namespace std;\nlong long mod=1e9+7;\nchar b[10005];\nlong long quickpow(long long a,int n)\n{\n long long ans=1;\n while(n)\n {\n if(n&1)\n {\n ans=ans*a%mod;\n }\n n>>=1;\n a=a*a%mod;\n }\n return ans;\n}\nlong long quickpow_ten(long long a,int n)\n{\n long long ans=1;\n for(int i=n-1;i>=0;i--)\n {\n ans=ans*quickpow(a,b[i]-'0')%mod;\n a=quickpow(a,10);\n }\n return ans;\n}\nint main()\n{\n long long n;\n scanf(\"%s\",&b);\n int len=strlen(b);\n if(len==1&&b[0]=='0')\n puts(\"1\");\n else\n {\n long long k=quickpow_ten(2,len)%mod;\n long long res=k*(k+1)/2%mod;\n printf(\"%I64d\\n\",res);\n }\n\n return 0;\n}\n\n/*2018-07-17 20:59:25.825*/", "lang_cluster": "C++", "tags": ["math", "matrices", "dp", "number theory"], "code_uid": "fbdfd5f3297d5f7b4e3c755ee8bcc5c6", "src_uid": "782b819eb0bfc86d6f96f15ac09d5085", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ll long long\n#define db double\n#define INF 1000000000\n#define ldb long double\n#define pb push_back\n#define get(x) x=read()\n#define gt(x) scanf(\"%d\",&x)\n#define gi(x) scanf(\"%lf\",&x)\n#define put(x) printf(\"%d\\n\",x)\n#define putl(x) printf(\"%lld\\n\",x)\n#define gc(a) scanf(\"%s\",a+1)\n#define rep(p,n,i) for(RE ll i=p;i<=n;++i)\n#define go(x) for(ll i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])\n#define fep(n,p,i) for(RE ll i=n;i>=p;--i)\n#define pii pair\n#define mk make_pair\n#define RE register\n#define P 1000000007\n#define mod 998244353\n#define S second \n#define F first\n#define gf(x) scanf(\"%lf\",&x)\n#define pf(x) ((x)*(x))\n#define ull unsigned long long\n#define ui unsigned\n#define zz p<<1\n#define yy p<<1|1\nusing namespace std;\nchar buf[1<<15],*fs,*ft;\ninline char getc()\n{\n return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;\n}\ninline ll read()\n{\n register ll x=0,f=1;register char ch=getc();\n while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}\n while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}\n return x*f;\n}\nconst ll MAXN=200010;\nll n,k;\nll fac[MAXN],inv[MAXN];\ninline ll C(ll a,ll b){if(a>1;\n\t}\n\treturn cnt;\n}\nsigned main()\n{\n\t//freopen(\"1.in\",\"r\",stdin);\n\tget(n);get(k);\n\tif(k>=n){puts(\"0\");return 0;}\n\tfac[0]=1;\n\trep(1,n,i)fac[i]=fac[i-1]*i%mod;\n\tinv[n]=ksm(fac[n],mod-2);\n\tfep(n-1,0,i)inv[i]=inv[i+1]*(i+1)%mod;\n\tif(!k)putl(fac[n]);\n\telse\n\t{\n\t\tll ans=0;\n\t\trep(0,n-k,i)\n\t\t{\n\t\t\tans=(ans+C(n,n-k)*((i&1)?-1:1)*C(n-k,i)%mod*ksm(n-k-i,n))%mod;\n\t\t}\n\t\tputl((ans+mod)%mod*2%mod);\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics", "fft"], "code_uid": "9bb887cdcc2998bea39749eff79e43f1", "src_uid": "6c1a9aaa7bdd7de97220b8c6d35740cc", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n#include\r\n\r\nusing namespace std;\r\n\r\nbool is_palindorme(string s, int r, int l)\r\n{\r\n\tfor (int i = r; i <= l; i++)\r\n\t{\r\n\t\tif (s[i] != s[l - i + r])\r\n\t\t{\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nint count(int a, int b)\r\n{\r\n\tint count = 1;\r\n\twhile (a / b > 0)\r\n\t{\r\n\t\ta /= b;\r\n\t\tcount++;\r\n\t}\r\n\treturn count;\r\n}\r\n\r\nvoid find_node(vector& a, vector& b, int s, int e, int curr)\r\n{\r\n\tint pos = s;\r\n\tif (s == e)\r\n\t{\r\n\t\tb[s] = curr + 1;\r\n\t\treturn;\r\n\t}\r\n\tif (e < s)\r\n\t{\r\n\t\treturn;\r\n\t}\r\n\tfor (int i = s; i <= e; i++)\r\n\t{\r\n\t\tif (a[pos] < a[i])\r\n\t\t{\r\n\t\t\tpos = i;\r\n\t\t}\r\n\t}\r\n\tb[pos] = curr + 1;\r\n\tcurr++;\r\n\tfind_node(a, b, s, pos - 1, curr);\r\n\tfind_node(a, b, pos + 1, e, curr);\r\n}\r\n\r\nvoid checknbh(vector& a, vector>& b, int i, int j, int n)\r\n{\r\n\tint flag = 0;\r\n\tif (i > 0 && a[i - 1][j] == 'L' && b[i - 1][j] != 1)\r\n\t{\r\n\t\tb[i - 1][j] = 1;\r\n\t\tflag = 1;\r\n\t\tchecknbh(a, b, i - 1, j, n);\r\n\r\n\t}\r\n\tif (i < n - 1 && a[i + 1][j] == 'L' && b[i + 1][j] != 1)\r\n\t{\r\n\t\tb[i + 1][j] = 1;\r\n\t\tflag = 1;\r\n\t\tchecknbh(a, b, i + 1, j, n);\r\n\r\n\t}\r\n\tif (j > 0 && a[i][j - 1] == 'L' && b[i][j - 1] != 1)\r\n\t{\r\n\t\tb[i][j - 1] = 1;\r\n\t\tflag = 1;\r\n\t\tchecknbh(a, b, i, j - 1, n);\r\n\r\n\t}\r\n\tif (j < n - 1 && a[i][j + 1] == 'L' && b[i][j + 1] != 1)\r\n\t{\r\n\t\tb[i][j + 1] = 1;\r\n\t\tflag = 1;\r\n\t\tchecknbh(a, b, i, j + 1, n);\r\n\t}\r\n}\r\n\r\nbool compareInterval(long long i1, long long i2)\r\n{\r\n\treturn (i1 < i2);\r\n}\r\n\r\nbool isPrime(int n)\r\n{\r\n\t// Corner case\r\n\tif (n <= 1)\r\n\t\treturn false;\r\n\r\n\t// Check from 2 to n-1\r\n\tfor (int i = 2; i <= sqrt(n); i++)\r\n\t\tif (n % i == 0)\r\n\t\t\treturn false;\r\n\r\n\treturn true;\r\n}\r\n\r\nvoid toh(int n, char A, char B, char C) {\r\n\tif (n == 1) {\r\n\t\tcout << \"Move 1 from \" << A << \" to \" << C << endl;\r\n\t\treturn;\r\n\t}\r\n\ttoh(n - 1, A, C, B);\r\n\tcout << \"Move \" << n << \" from \" << A << \" to \" << C << endl;\r\n\ttoh(n - 1, B, A, C);\r\n}\r\n\r\nvoid generate(vector& a, string s, int t) {\r\n\tif (s.length() == t) {\r\n\t\ta.push_back(s);\r\n\t\treturn;\r\n\t}\r\n\tif (s.length() == 0 || s.back() == 'R') {\r\n\t\tgenerate(a, s + \"S\", t);\r\n\t\tgenerate(a, s + \"R\", t);\r\n\t}\r\n\telse {\r\n\t\tgenerate(a, s + \"R\", t);\r\n\t}\r\n}\r\n\r\nint addOne(deque& a, int t, int i) {\r\n\tint temp = 0;\r\n\tif (i == t - 1)\r\n\t{\r\n\t\ta[i] += 1;\r\n\t\ttemp = a[i] / 10;\r\n\t\ta[i] %= 10;\r\n\t}\r\n\telse {\r\n\t\ta[i] += addOne(a, t, i + 1);\r\n\t\tif (i == 0 && a[0] >= 10)\r\n\t\t{\r\n\t\t\ta.push_front(a[0] / 10);\r\n\t\t\ta[1] %= 10;\r\n\t\t}\r\n\t\telse {\r\n\t\t\ttemp = a[i] / 10;\r\n\t\t\ta[i] %= 10;\r\n\t\t}\r\n\t}\r\n\treturn temp;\r\n}\r\n\r\nvoid reversec(set& a, string s) {\r\n\tstring t(s);\r\n\treverse(t.begin(), t.end());\r\n\ta.insert(s + t);\r\n\ta.insert(t + s);\r\n}\r\n\r\nint num_digits(int a) {\r\n\tint digits = 0;\r\n\twhile (a > 0) {\r\n\t\ta /= 10;\r\n\t\tdigits++;\r\n\t}\r\n\treturn digits;\r\n}\r\n\r\nvoid search(long long x, long long n,long long count, long long &ans) {\r\n\tint digits = 0;\r\n\tlong long a = x;\r\n\twhile (a > 0) {\r\n\t\ta /= 10;\r\n\t\tdigits++;\r\n\t}\r\n\tif (digits == n) {\r\n\t\treturn;\r\n\t}\r\n\tlong long y = x;\r\n\twhile (y > 0) {\r\n\t\tint temp = y % 10;\r\n\t\tif (temp > 1) {\r\n\t\t\tsearch(x * temp, n, count + 1, ans);\r\n\t\t}\r\n\t\ty /= 10;\r\n\t}\r\n}\r\n\r\nmapm;\r\nlong long ans = 1e9;\r\nlong long n;\r\nvoid reqlen( long long count,long long x) {\r\n\tif (x >= n) {\r\n\t\tans = min(ans, count);\r\n\t\treturn;\r\n\t}\r\n\tlong long y = x;\r\n\twhile (y > 0) {\r\n\t\tlong long c = y % 10;\r\n\t\tif (c > 1) {\r\n\t\t\tif (m.count(c * x) == 0 || count+1> n >> x;\r\n\tn = pow(10, n - 1);\r\n\treqlen(0, x);\r\n\tcout << (ans==1e9?-1:ans) << endl;\r\n}\r\n\r\n\r\n", "lang_cluster": "C++", "tags": ["brute force", "dfs and similar", "shortest paths", "dp", "data structures"], "code_uid": "1839db2488286c724f76f9787a111dca", "src_uid": "cedcc3cee864bf8684148df93804d029", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \nusing namespace std;\nint main() {\n\tint n;\n\tcin >> n;\n\tvector a(n), b(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> a[i];\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> b[i];\n\t}\n\tint cnt1 = 0, cnt2=0;\n\tfor (int i = 0; i < n; i++) {\n\t\tif (a[i] == 1 && b[i] == 0) cnt1++;\n\t\tif (a[i] == 0 && b[i] == 1) cnt2++;\n\t}\n\tif (cnt1 != 0) cout << (cnt2 + cnt1) / cnt1;\n\telse cout << -1;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "a89af0e7ac1f05de72444cf0941490fa", "src_uid": "b62338bff0cbb4df4e5e27e1a3ffaa07", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\r\n#define lld long long int\r\n#define ulld unsigned long long\r\n#define vll vector\r\n#define pll pair\r\n#define mll map\r\n#define sll set\r\n#define pb push_back\r\n#define ss second\r\n#define ff first\r\n#define mp make_pair\r\n#define MOD 1000000007 //998244353\r\n#define answer(x) cout<= MOD) s -= MOD;\r\n return s;\r\n}\r\nint mult(int a, int b) {\r\n return (1LL * a * b) % MOD;\r\n}\r\nint pw(int a, int b) {\r\n if (b == 0) return 1;\r\n if (b & 1) return mult(a, pw(a, b - 1));\r\n int res = pw(a, b / 2);\r\n return mult(res, res);\r\n}\r\nint main() {messi lld t=1;cin>>t;\r\n while(t--)\r\n {\r\n lld n=0,m=0,k=0,i=0,j=0,p=0,q=0,x=0,y=0,z=0,ans=0,cnt=0,l=0,r=0,mid=0,lo=0,hi=0,lm=0,cur=0,curr=0;string s;bool flag=false;\r\n cin>>n>>k;\r\n if (n % 2 == 1) {\r\n ans = 1;\r\n for (i = 1; i <= n - 1; i++) ans = mult(ans, 2);\r\n ans = sum(ans, 1);\r\n int f = 1;\r\n for (z = 0; z < k; z++) f = mult(f, ans);\r\n cout<\n#include \n#include \n#include \nusing namespace std::chrono;\nusing namespace std;\ntypedef long long ll;\nusing namespace __gnu_pbds; \n#define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> \n#define ordered_mset tree, null_type,less>, rb_tree_tag,tree_order_statistics_node_update> \n#define fr(i,a,b) for (ll i=(a); i<(b);i++)\n#define frr(i,a,b) for(ll i=(a-1);i>=(b);i--)\n#define vi vector\n#define vl vector\n#define pb push_back\n#define ppb pop_back()\n#define pf push_front\n#define ppf pop_front()\n#define F first\n#define S second\n#define pii pair \n#define rev(a) reverse(a.begin(),a.end())\n#define srt(a) sort(a.begin(),a.end())\n#define rsrt(a) sort(a.begin(),a.end(),greater())\n#define sz(a) (int)a.size()\n#define endl \"\\n\"\n\n#define print(a) for(auto x:a)cout<>n;\n\nif(n==1)cout<<1<n){fin=i-1;break;}\n } \n ll fi=fin;\n fin*=fin+1;\n fin/=2;\n n-=fin;\n if(n==0)n=fi;\n cout<\n#include\nusing namespace std;\n\nint main()\n{\n\tint n = 1;\n\tint si;\n\tcin >> si;\n\tstring str;\n\tcin >> str;\n\t\n\tfor (int i = 0; i < si; i += n) {\n\t\tcout << str[i];\n\t\tn++;\n\t}\n\n\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "0a47bfc4feddebcf817586019283ff46", "src_uid": "08e8c0c37b223f6aae01d5609facdeaf", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\n#define N 255\r\n#define Mod 998244353\r\n#define For(i,x,y)for(i=x;i<=(y);i++)\r\nint f[N][N][N],pre[N][N][N],g[N][N],inv[N];\r\nint ksm(int x,int y)\r\n{\r\n\treturn(!y?1:1LL*ksm(1LL*x*x%Mod,y>>1)*(y&1?x:1)%Mod);\r\n}\r\nint main()\r\n{\r\n\tint n,k,i,j,p,num,tmp,res=0;\r\n\tcin>>n>>k;\r\n\tFor(j,1,k)f[1][j][1]=1;\r\n\tFor(i,1,n-1)inv[i]=ksm(i,Mod-2);\r\n\tFor(i,1,k)\r\n\t{\r\n\t\tg[i][0]=1;\r\n\t\tFor(j,1,n)g[i][j]=1LL*g[i][j-1]*i%Mod;\r\n\t}\r\n\tFor(i,2,n-1)\r\n\t{\r\n\t\tFor(j,1,k)\r\n\t\tFor(num,1,i-1)pre[i-1][j][num]=(pre[i-1][j-1][num]+f[i-1][j][num])%Mod;\r\n\t\tFor(j,1,k)\r\n\t\tFor(num,1,i-1)\r\n\t\t{\r\n\t\t\tp=j;\r\n\t\t\ttmp=(p==j?num+1:1);\r\n\t\t\tf[i][j][tmp]=(f[i][j][tmp]+1LL*f[i-1][p][num]*inv[tmp]%Mod*g[k-j+1][i-1])%Mod;\r\n\t\t\ttmp=1;\r\n\t\t\tf[i][j][tmp]=(f[i][j][tmp]+1LL*pre[i-1][j-1][num]*inv[tmp]%Mod*g[k-j+1][i-1])%Mod;\r\n\t\t}\r\n\t}\r\n\tFor(j,1,k)\r\n\tFor(num,1,n-1)res=(res+f[n-1][j][num])%Mod;\r\n\tFor(i,1,n-1)res=1LL*res*i%Mod;\r\n\tcout<\n#include \n#include \n\nusing namespace __gnu_pbds;\nusing namespace std;\n\n#define endl '\\n'\n#define fo(i,n) for(i=0;i=0;--i)\n\n#define ffs(a) __builtin_ffs(a) // find first set\n#define clz(a) __builtin_clz(a) // count leading zeroes\n#define ctz(a) __builtin_ctz(a) // count trailing zeroes\n#define popc(a) __builtin_popcount(a) // count set bits\n#define popcll(a) __builtin_popcountll(a) //count set bits for long long int\n\n#define all(x) (x).begin(),(x).end()\n#define sz(x) (int)(x).size()\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define yolo \"debug_statement\"\n\ntypedef long long int ll;\ntypedef long double ld;\ntypedef pair ii;\ntypedef vector vii;\ntypedef vector vi;\n\nconst ll inf = 1e9;\nconst ld eps = 1e-9;\nconst ld pi=acos(-1);\nconst ll mod=1000000007;\n\nll powmod(ll a,ll b,ll mo=mod){ll res=1;a%=mo; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mo;a=a*a%mo;}return res;}\n\ninline ll inv_(ll a) {return powmod(a, mod-2, mod);}\ninline ll add(ll a, ll b){a+=b;if(a>=mod)a-=mod;return a;}\ninline ll mul(ll a, ll b){return a*1ll*b%mod;}\ninline ll sub(ll a, ll b){a-=b;if(a<0)a+=mod;return a;}\n\ntypedef tree<\nint,\nnull_type,\nless,\nrb_tree_tag,\ntree_order_statistics_node_update>\nordered_set;\n\n/*USAGE\nordered_set X;\nX.insert(1);\nX.insert(3);\ncout << *X.find_by_order(1) << endl; //3\ncout << X.order_of_key(-5) << endl; //0\ncout << X.order_of_key(1) << endl; //0\ncout << (end(X) == X.order_of_key(100)) << endl; //true since utne elements nahi hai\n*/\n\n//fold\n#if DEBUG && !ONLINE_JUDGE\n\n #define debug(args...) (Debugger()) , args\n\n class Debugger\n {\n public:\n Debugger(const std::string& _separator = \", \") :\n first(true), separator(_separator){}\n\n template\n Debugger& operator , (const ObjectType& v)\n {\n if(!first)\n std::cerr << separator;\n std::cerr << v;\n first = false;\n return *this;\n }\n ~Debugger() { std::cerr << endl;}\n\n private:\n bool first;\n std::string separator;\n };\n\n template \n inline std::ostream& operator << (std::ostream& os, const std::pair& p)\n {\n return os << \"(\" << p.first << \", \" << p.second << \")\";\n }\n\n template \n inline std::ostream& operator << (std::ostream& os, const std::array& a){\n bool first = true;\n os << \"[\";\n for(unsigned int i = 0; i < N; i++)\n {\n if(!first)\n os << \", \";\n os << a[i];\n first = false;\n }\n return os << \"]\"; \n }\n\n template\n inline std::ostream &operator << (std::ostream & os,const std::vector& v)\n {\n bool first = true;\n os << \"[\";\n for(unsigned int i = 0; i < v.size(); i++)\n {\n if(!first)\n os << \", \";\n os << v[i];\n first = false;\n }\n return os << \"]\";\n }\n\n template\n inline std::ostream &operator << (std::ostream & os,const std::set& v)\n {\n bool first = true;\n os << \"[\";\n for (typename std::set::const_iterator iii = v.begin(); iii != v.end(); ++iii)\n {\n if(!first)\n os << \", \";\n os << *iii;\n first = false;\n }\n return os << \"]\";\n }\n\n template\n inline std::ostream &operator << (std::ostream & os,const std::map& v)\n {\n bool first = true;\n os << \"[\";\n for (typename std::map::const_iterator iii = v.begin(); iii != v.end(); ++iii)\n {\n if(!first)\n os << \", \";\n os << *iii ;\n first = false;\n }\n return os << \"]\";\n }\n\n#else\n #define debug(args...) // Just strip off all debug tokens\n#endif\n//endfold\n\nint main(){\n #if ONLINE_JUDGE\n ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);\n\t#endif\n\n int n, m;\n int i, j;\n\n cin >> n >> m;\n\n int s1[n][2];\n int s2[m][2];\n\n fo(i, n){\n \tcin >> s1[i][0] >> s1[i][1];\n } \n\n fo(i, m){\n \tcin >> s2[i][0] >> s2[i][1];\n }\n\n setfinalset;\n\n for(i = 0; i < n; ++i){\n \tsets;\n \tfor(j = 0; j < m; ++j){\n \t\tif(s1[i][0] == s2[j][0]){\n \t\t\tif(s1[i][1] == s2[j][1]){\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\telse{\n \t\t\t\ts.insert(s1[i][0]);\n \t\t\t}\n \t\t}\t\n \t\telse if(s1[i][0] == s2[j][1]){\n \t\t\tif(s1[i][1] == s2[j][0]){\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\telse{\n \t\t\t\ts.insert(s1[i][0]);\n \t\t\t}\n \t\t}\n \t\telse{\n \t\t\tif(s1[i][1] == s2[j][0]){\n \t\t\t\ts.insert(s1[i][1]);\n \t\t\t}\n \t\t\telse if(s1[i][1] == s2[j][1]){\n \t\t\t\ts.insert(s1[i][1]);\n \t\t\t}\n \t\t}\n\n \t\t// for(auto it: s)cout << it << \" \";\n \t\t// cout << endl;\n\n \t\tif(sz(s) > 1){\n \t\t\tcout << -1 << endl;\n \t\t\treturn 0;\n \t\t}\n\n \t\t// if(sz(s) == 1){\n \t\t\t\tfor(auto it: s)finalset.insert(it);\n \t\t// }\n \t}\n }\n\n for(i = 0; i < m; ++i){\n sets;\n for(j = 0; j < n; ++j){\n if(s2[i][0] == s1[j][0]){\n if(s2[i][1] == s1[j][1]){\n continue;\n }\n else{\n s.insert(s2[i][0]);\n }\n } \n else if(s2[i][0] == s1[j][1]){\n if(s2[i][1] == s1[j][0]){\n continue;\n }\n else{\n s.insert(s2[i][0]);\n }\n }\n else{\n if(s2[i][1] == s1[j][0]){\n s.insert(s2[i][1]);\n }\n else if(s2[i][1] == s1[j][1]){\n s.insert(s2[i][1]);\n }\n }\n\n // for(auto it: s)cout << it << \" \";\n // cout << endl;\n\n if(sz(s) > 1){\n cout << -1 << endl;\n return 0;\n }\n\n // if(sz(s) == 1){\n for(auto it: s)finalset.insert(it);\n // }\n }\n }\n\n if(sz(finalset) == 1){\n \tfor(auto it: finalset)cout << it << endl;\n \treturn 0;\n }\n\n // if(sz(finalset) == 0){\n // \tcout\n // }\n\n // if(sz(finalset) > 1){\n // \tcout << 0 << endl;\n // \treturn 0;\n // }\n\n cout << 0 << endl;\n\n //#if !ONLINE_JUDGE\n // cout << fixed << setprecision(12) << \"-------------------------------------------------\\n\";\n // cout << double(clock())/CLOCKS_PER_SEC << \" seconds\" << endl;\n //#endif\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["bitmasks"], "code_uid": "1e6c1ef292c74da08768b7fd7b98bef1", "src_uid": "cb4de190ae26127df6eeb7a1a1db8a6d", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint i, j, k, m, n;\n\nint good(string s) {\n if (s.size() < 1 || s.size() > 16) return 0;\n for (int i = 0; i < s.size(); i++) {\n if (isalnum(s[i]) || s[i] == '_');\n else return 0;\n }\n return 1;\n}\n\nint jabber(string s) {\n i = s.find('@');\n if (i == -1) return 0;\n if (!good(s.substr(0, i))) return 0;\n s = s.substr(i+1);\n\n i = s.find('/');\n if (i != -1) {\n if (!good(s.substr(i+1))) return 0;\n s = s.substr(0, i);\n }\n\n while (1) {\n i = s.find('.');\n if (i == -1) {\n return good(s);\n } else {\n if (!good(s.substr(0, i))) return 0;\n s = s.substr(i+1);\n }\n }\n}\n\nstring s;\n\nint main() {\n cin >> s;\n\n if (jabber(s)) cout << \"YES\";\n else cout << \"NO\";\n return 0;\n}", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "225a11bc7aa737c65b894a804d293b7d", "src_uid": "2a68157e327f92415067f127feb31e24", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nint main()\n{\n\tint l,k,i;\n\tlong long ans=1;\n\tscanf(\"%d %d\",&k,&l);\n\tfor(i=1;;i++)\n\t{\n\t\tans*=k;\n\t\tif(ans==l)\n\t\t{\n\t\t\tprintf(\"YES\\n%d\",i-1);\n\t\t\treturn 0;\n\t\t}\n\t\tif(ans>l)\n\t\t\tbreak;\n\t}\n\tprintf(\"NO\\n\");\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "31fe8d7dfd13b235ccd43ce75ff1d223", "src_uid": "8ce89b754aa4080e7c3b2c3b10f4be46", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n#define tk printf(\"Case %d:\\n\",q++)\n#define READ freopen(\"input.txt\",\"r\",stdin);\n#define WRITE freopen(\"output.txt\",\"w\",stdout);\n#define sf(x) scanf(\"%lld\",&x)\n#define sff(x,y) scanf(\"%lld %lld\",&x,&y)\n#define sfff(x,y,z) scanf(\"%lld %lld %lld\",&x,&y,&z)\n#define fori(x,y) for(ll i=x;i pii;\ntypedef vector vl;\ntypedef vector vi;\nint xx[4]={0,0,1,-1};\nint yy[4]={-1,1,0,0};\nint main()\n{\n #ifndef ONLINE_JUDGE\n READ\n WRITE\n #endif \n \tunsigned long long n;\n \tcin>>n;\n \tll res=0;\n \tvectordiv;\n \tfor(llu i=2;i*i<=n;i++){\n \t\tif(n%i==0){\n\t\t\t\twhile(n%i==0){\n\t\t\t\t\tn/=i;\n\t\t\t\t}\n\t\t\t\tdiv.pb(i);\n\t\t}\n \t}\n \tif(n>1)div.pb(n);\n \tif(div.size()==1)cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//#include \n\n/*#include \n#include */\n\n//#include \"../../../../../Desktop/Treap.h\"\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst double eps = 1E-9;\n\nint main(int argc, char **argv) {\n\n ios::sync_with_stdio(false);\n \n //freopen(\"in.txt\", \"r\", stdin);\n\n int n;\n cin >> n;\n vector> v(n, vector(n));\n\n ll ans = 0;\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < n; ++j) {\n cin >> v[i][j];\n if(i == j || j == n - i - 1 || i == n >> 1 || j == n >> 1)\n ans += v[i][j];\n }\n }\n\n cout << ans << endl;\n return 0; \n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "fd93e85313f88177b8d553e5a4da1f65", "src_uid": "5ebfad36e56d30c58945c5800139b880", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nchar a1,a2;\nint b1,b2,ans;\n\nint main(){\n\tscanf(\"%c%d\\n%c%d\\n\",&a1,&b1,&a2,&b2);\n\tfor(char a3 = 'a'; a3 < 'i'; a3++)\n\t\tfor(int b3 = 1; b3 < 9; b3++)\n\t\t\tans += (((a3-a1)*(a3-a1) + (b3-b1)*(b3-b1) != 5) and ((a3-a2)*(a3-a2) + (b3-b2)*(b3-b2) != 5) and a3 != a1 and b3 != b1);\n\tprintf(\"%d\\n\",ans-1);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "implementation"], "code_uid": "b1256be0323c54dec1fdf952c4cde0b9", "src_uid": "073023c6b72ce923df2afd6130719cfc", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \nusing namespace std;\n\n/*** Template Begins ***/\n\ntypedef long long ll;\ntypedef pair PII;\ntypedef pair > PIII;\ntypedef vector vi;\n\n#define endl '\\n'\n#define pb push_back\n#define INF INT_MAX/10\n#define F first\n#define S second\n#define mp make_pair\n#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define hell 1000000007\n#define all(a) (a).begin(),(a).end()\nll power(ll x, ll y, ll p){ ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p;y = y>>1;x = (x*x) % p;} return res; } \n\n// Debug //\n\n#define trace(x) cerr << #x << \": \" << x << endl;\n#define trace1(x) cerr << #x << \": \" << x << endl;\n#define trace2(x, y) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << endl;\n#define trace3(x, y, z) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << \" | \" << #z << \": \" << z << endl;\n#define trace4(a, b, c, d) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << endl;\n#define trace5(a, b, c, d, e) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << \" | \" << #e << \": \" << e << endl;\n#define trace6(a, b, c, d, e, f) cerr << #a << \": \" << a << \" | \" << #b << \": \" << b << \" | \" << #c << \": \" << c << \" | \" << #d << \": \" << d << \" | \" << #e << \": \" << e << \" | \" << #f << \": \" << f << endl;\n\n// Constants //\n\nconst int N = 15;\nconst int xinc[] = {0, 0, 1, -1};\nconst int yinc[] = {1, -1, 0, 0};\nconst long double PI = 3.141592653589793;\n\n/*** Template Ends ***/\nstring s[N];\nint c[N], b[N], n;\nint a[N], ans[N];\n\nbool satisfy(){\n\n set s;\n for(int i = 0; i < 4; i++) s.insert(a[i]);\n return (s.size() == 4);\n}\n\nvoid solve(){\n \n cin >> n;\n for(int i = 0; i < n; i++) cin >> s[i] >> b[i] >> c[i];\n\n int cnt = 0;\n\n for(a[0] = 0; a[0] <= 9; a[0]++)\n for(a[1] = 0; a[1] <= 9; a[1]++)\n for(a[2] = 0; a[2] <= 9; a[2]++)\n for(a[3] = 0; a[3] <= 9; a[3]++)\n if (satisfy())\n {\n bool valid = true;\n\n for(int i = 0; i < n; i++)\n {\n int bb = 0, cc = 0;\n set temp;\n\n for(int j = 0; j < 4; j++){\n if (a[j] == s[i][j]-'0') bb++;\n temp.insert(a[j]);\n }\n\n for(int j = 0; j < 4; j++){\n if (temp.find(s[i][j]-'0') != temp.end()) cc++;\n }\n\n cc -= bb;\n if (cc != c[i] || bb != b[i]){ valid = false; break; }\n }\n\n if (valid){\n //for(int i = 0; i < 4; i++) cout << a[i]; cout << endl;\n cnt++;\n if (cnt == 1) for(int i = 0; i < 4; i++) ans[i] = a[i];\n }\n }\n\n if (cnt == 0) cout << \"Incorrect data\" << endl;\n else if (cnt == 1){ for(int i = 0; i < 4; i++) cout << ans[i]; cout << endl; }\n else cout << \"Need more data\" << endl;\n\n}\n\nint main(){\n \n ios;\n // freopen(\"input.txt\", \"r\", stdin);\n // freopen(\"output.txt\", \"w\", stdout);\n int t;\n //cin >> t;\n t = 1;\n while(t--)\n solve();\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "45197c7d7ce1e4c33df828247aff1576", "src_uid": "142e5f2f08724e53c234fc2379216b4c", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint i,l,ans,ll;\nstring s;\nint main(){\n cin>>s;\n l=s.length();\n i=0;\n while(i\nusing namespace std;\nint main()\n{\n\t double n,k;\n\tlong long g;\n\tcin>>n>>k;\n\tg = ceil(2*n/k)+ceil(5*n/k)+ceil(8*n/k);\n\tcout<\nusing namespace std;\nint main()\n{\n int w,h,a1,b1,a2,b2,k=0,m=0;\n cin>>w>>h>>a1>>b1>>a2>>b2;\n\n//w=w+h;\n for(int i=h; i>=0; i--)\n {\n w=w+i;\n if(i==b1)\n {\n //if(a1=0)\n cout<0)\n cout<<(w+h+b1+b2)-(a1+a2)<\n// iteration and input/output stream\n#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))\n#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)\n#define printarr(x) rep(i,0,x.size()) cout<> tcase ; rep(tc,1,tcase+1)\n//STL shortcuts\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define F first\n#define S second\n#define MP make_pair\n\n// Error detection\n#define trace1(x) cerr<<#x<<\": \"< vi;\ntypedef vector vvi;\ntypedef vector vll;\ntypedef pair pii;\ntypedef pair pill;\n\ninline void smax(int &x , int y) { x = max(x , y) ; }\ninline void smin(int &x , int y) { x = min(x , y) ; }\n\n#define MOD 1000000007\n#define inf 4000000000000000000LL\n#define SYNC std::ios::sync_with_stdio(false); cin.tie(NULL);\n\nll gcd(ll a,ll b){ return ((b==0)? a:gcd(b,a%b) ); }\n#define PI acos(-1)\n#define N 1000000\n\nbitset<10000010> bs;\nvi smp;\nvoid sieve(int upr)\n{\n bs.set();\n int siz = upr+1;\n smp.resize(siz);\n rep(i,0,siz)\n smp[i] = i;\n\n for(ll i=2;i>n>>k;\n\n /*int pts = 0;\n int pol = 0;\n\n set > pos;\n\n pos.insert(MP(3,3));\n for(int i=4;i<=n;i++)\n pos.insert(make_pair(i-1,i));\n\n set vis;\n while(pol < k)\n {\n pair x = *pos.begin();\n pos.erase(pos.begin());\n if(vis.find(x.second) != vis.end())\n continue;\n\n pol++;\n pts+= x.F;\n trace2(x.F,x.S);\n vis.insert(x.second);\n\n for(int j=2*x.second;j<=n;j+= x.second)\n {\n pos.insert(make_pair(j-x.second,j) );\n }\n\n\n }\n cout< fact;\n ll tmp = i;\n while(smp[tmp] != tmp)\n {\n fact.insert(smp[tmp]);\n tmp/=smp[tmp];\n }\n if(tmp!=1 )\n {\n fact.insert(tmp);\n }\n\n\n ll val=i;\n tr(fact,it)\n {\n val = ((*it-1)*val)%MOD;\n val = (val*inv[*it])%MOD;\n }\n\n if(i == 4)\n val++;\n phi.pb(val);\n //trace2(i,val);\n }\n\n sort(phi.begin(),phi.end());\n ll sum = 0;\n rep(i,0,k)\n {\n sum+= phi[i];\n }\n if(k ==2 && n>=6)\n sum++;\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#pragma warning( disable : 4244 4267 4018 4996 4800 )\n//#pragma comment( linker, \"/stack:10000000\" )\nusing namespace std;\ntypedef vector vi; typedef vector> vvi; typedef vector vs; typedef vector vd;\ntypedef vector vvd; typedef long long ll; typedef vector vll; typedef vector vvll; typedef pair pii;\n#define all(v) begin(v), end(v)\n\n#ifdef LOCAL\nifstream in(\"c.in\");\n#else\nistream & in = cin;\n#endif\nostream & out = cout;\ntemplate void read(T & t) { for (auto & x : t) in >> x; }\n\nint bit_count(int i) {\n i = i - ((i >> 1) & 0x55555555);\n i = (i & 0x33333333) + ((i >> 2) & 0x33333333);\n i = (i + (i >> 4)) & 0x0f0f0f0f;\n i = i + (i >> 8);\n i = i + (i >> 16);\n return i & 0x3f;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n int n; in >> n;\n vs p(n); read(p);\n int m = p[0].size();\n vll d(1 << m);\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j < n; ++j) {\n int mask = 0;\n for (int k = 0; k < m; ++k) {\n if (p[i][k] == p[j][k])\n mask |= 1 << k;\n }\n d[mask] |= 1LL << i;\n d[mask] |= 1LL << j;\n }\n }\n for (int mask = d.size() - 1; mask >= 0; --mask) {\n for (int bit = 0; bit < m; ++bit) if (mask & (1 << bit)) { \n d[mask ^ (1 << bit)] |= d[mask];\n }\n }\n vd paths(m + 1);\n for (int mask = 0; mask < d.size(); ++mask) {\n int moves = bit_count(mask) + 1;\n for (int bit = 0; bit < m; ++bit) {\n int newmask = mask | (1 << bit);\n if (newmask == mask) continue;\n ll added = d[mask] ^ d[newmask];\n paths[moves] += bit_count(added & 0xFFFFFFFF) + bit_count(added >> 32);\n }\n }\n double res = 0;\n for (int i = 1; i <= m; ++i) {\n double t = paths[i];\n for (int j = 0; j < i; ++j) {\n t *= 1. * (i - j) / (m - j);\n }\n res += t;\n }\n res /= n;\n out.setf(ios::fixed); out.precision(10); out << res;\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp", "bitmasks"], "code_uid": "ed159ce0b0455580a8c7c3a79d68f00d", "src_uid": "a95d9aef6a64c30e46330dcc8e6d4a67", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include\n#include \n#include \nusing namespace std;\n\nint main()\n{\n int supply_lines , devices ,sockets , All_sockets , sockets_needed = 0;\n cin>>supply_lines>>devices>>sockets;\n All_sockets = sockets;\n vector a(supply_lines);\n\n for(int i=0;i> a[i];\n }\n\n\n int j =0;\n sort(a.rbegin(),a.rend()); // sorting in a descending order\n for( j = 0 ; j= devices)\n break;\n\n \n\n All_sockets += a[j] -1 ;\n a[0] = 0;\n sockets--;\n sockets_needed++;\n\n\n\n }\n\n if(j==supply_lines+1)\n {\n cout << -1;\n }\n else\n {\n cout << sockets_needed;\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation", "greedy"], "code_uid": "41d13cadc380da182e105b85c9cee583", "src_uid": "b32ab27503ee3c4196d6f0d0f133d13c", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define ll long long \n#define f first\n#define se second\n\nint main()\n{\n ll n,i;\n cin>>n;\n ll a[10],b[10],c[10];\n memset(a,0,sizeof(a));\n memset(b,0,sizeof(b));\n memset(c,0,sizeof(c));\n if(n>=1)\n {\n for(i=0;i<6;i++)\n {\n ll x;cin>>x;\n a[x]=1;\n }\n }\n if(n>=2)\n {\n for(i=0;i<6;i++)\n {\n ll x;cin>>x;\n b[x]=1;\n }\n }\n if(n>=3)\n {\n for(i=0;i<6;i++)\n {\n ll x;cin>>x;\n c[x]=1;\n }\n }\n for(i=1;i<=9;i++)\n {\n if(a[i]==0 && b[i]==0 && c[i]==0)\n {\n cout<\n#include \n\nusing namespace std;\n\nint main()\n{\n int l4,l5,l6;\n int x1,y1,x2,y2,x3,y3,l1,l2,l3,a,b,c,d,e,f;\n cin>>x1>>y1>>x2>>y2>>x3>>y3;\n a=x1;\n b=x2;\n c=x3;\n d=y1;\n e=y2;\n f=y3;\n l1=(powl((x2-x1),2) +powl((y2-y1),2));\n l2=(powl((x3-x2),2)+ powl((y3-y2),2));\n l3=(powl((x3-x1),2)+ powl((y3-y1),2));\n\n\n\n if(l1+l2==l3||l2+l3==l1||l1+l3==l2)\n {\n cout<<\"RIGHT\";\n return 0;\n }\n for(int i=0; i<=11; i++)\n {\n if(i==0)\n {\n x1++;\n }\n else if(i==1)\n {\n x1--;\n }\n else if(i==2)\n {\n y1++;\n }\n else if(i==3)\n {\n y1--;\n }\n else if(i==4)\n {\n x2++;\n }\n else if(i==5)\n {\n x2--;\n }\n\n else if(i==6)\n {\n y2++;\n }\n else if(i==7)\n {\n y2--;\n }\n else if(i==8)\n {\n x3++;\n }\n else if(i==9)\n {\n x3--;\n }\n else if(i==10)\n {\n y3++;\n }\n else if(i==11)\n {\n y3--;\n }\n l1=(pow((x2-x1),2) +pow((y2-y1),2));\n l2=(pow((x3-x2),2)+ pow((y3-y2),2));\n l3=(pow((x3-x1),2)+ pow((y3-y1),2));\n if(l1+l2==l3||l1+l3==l2||l2+l3==l1)\n {\n if(l1>0&&l2>0&&l3>0)\n {\n \n cout<<\"ALMOST\";\n return 0;\n }\n }\n x1=a;\n x2=b;\n x3=c;\n y1=d;\n y2=e;\n y3=f;\n }\n cout<<\"NEITHER\";\n}\n", "lang_cluster": "C++", "tags": ["brute force", "geometry"], "code_uid": "1a3c0ffb164ff58261c078ef84359e9e", "src_uid": "8324fa542297c21bda1a4aed0bd45a2d", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "\n#include\nusing namespace std;\n\nint main(){\n\tint n;\n\tcin>>n;\n\tif(n==4 || n==8 || n==2 || n==1 || n==16 || n==32 || n==64 || n==128 || n==256 || n==512 || n==1024){\n\t\tcout<<\"YES\"<\n\n#define int long long\n#define pii pair\n\n#define err(A) cout << #A << \" = \" << (A) << endl\n\nusing namespace std;\n\nint n;\n\nint32_t main()\n{ \n ios::sync_with_stdio(0); cin.tie(0);\n\n cin >> n;\n \n int d = 1;\n int sz = 1;\n\n while (sz * 2 + 1 <= n)\n {\n sz = sz * 2 + 1;\n d = d * 2;\n }\n\n int r = n - sz;\n\n int z = 0;\n for (int t = 2; t <= d; t *= 4)\n z += d / t;\n\n /*\n err(n);\n err(d);\n err(z);*/\n\n if ((r == z) or (r == (z + 1)))\n cout << 1 << endl;\n else\n cout << 0 << endl;\n \n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp"], "code_uid": "ece02334982e4dff93cbdf2515046e95", "src_uid": "821409c1b9bdcd18c4dcf35dc5116501", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nchar a[11],b[11],c[11],d[11];\nint main()\n{\n\twhile(~scanf(\"%s%s%s%s\",a,b,c,d))\n\t{\n\t\ta[2]=b[1],a[3]=b[0],c[2]=d[1],c[3]=d[0];\n\t\tint i=0,j=0,flag=1;\n\t\twhile(a[i]!='A')i++;\n\t\twhile(c[j]!='A')j++;\n\t\tfor(int k=0;k<4;k++,i=(i+1)%4,j=(j+1)%4)\n\t\t{\n\t\t\tif(a[i]=='X')i=(i+1)%4;\n\t\t\tif(c[j]=='X')j=(j+1)%4;\n\t\t\tif(a[i]!=c[j])flag=0;\n\t\t}\n\t\tprintf(\"%s\\n\",flag?\"YES\":\"NO\");\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "constructive algorithms", "implementation"], "code_uid": "1d1e55375157f86cfe76eba7644406ea", "src_uid": "46f051f58d626587a5ec449c27407771", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\nlong long n;\nint main() {\n\tcin>>n;\n\tcout<\n#include\nint n,m,m_er,cnt;\nint dire; //// 记录方向,1表示向前(左),0表示向后(右)\nchar ss[205];\n\nvoid begin()\n{\n if(ss[0]=='h') dire=1;\n else dire=0;\n}\n\nvoid mm_er( )\n{\n // printf(\"dire= %d m_er%d\\n\",dire,m_er);\n if( dire == 1 ) ////向左走\n {\n if( m_er == 1 )\n {\n m_er++;\n dire = ( dire+1 )%2 ;\n }\n else m_er--;\n }\n else\n {\n if( m_er == n )\n {\n m_er--;\n dire = ( dire+1 )%2 ;\n }\n else m_er++;\n }\n}\n\nvoid mm_m1( int i )\n{\n // printf(\"dire= %d m=%d\\n\",dire,m);\n if( ss[i] == '0' ) // //车move\n {\n if( dire == 0 ) //// Controller向右走\n {\n if( m < m_er ){\n if(m!=1) m--;\n }\n else if( m < n ) m++;\n }\n else //// Controller向左走\n {\n if( m > m_er ){\n if( m != n ) m++;\n }\n else if( m > 1 ) m--;\n }\n }\n else ////车idle\n {\n if( dire == 0 )//// Controller向右走\n m=m_er-1;\n else // // Controller向左走\n m=m_er+1;\n }\n}\n\nint find( )\n{\n int l=strlen(ss);\n for(int i=0;i\n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long LL;\nconst int maxn = 1000005;\nconst int mod = 1000000007;\nint inv[maxn];\nint k, w;\n\nint pow(int a, int n) {\n int ans = 1;\n for (; n; n >>= 1, a = (LL)a * a % mod)\n if (n & 1) ans = (LL)ans * a % mod;\n return ans;\n}\n\nint calc(int k, int w) {\n int ans = LL(pow(k, w + 1) - 1) * inv[k - 1] % mod;\n int per = 1, pinv = 1, tmp = pow(k, w);\n for (int i = 1; i <= k; ++i)\n if (i <= w) {\n tmp = (LL)tmp * inv[k] % mod; per = (LL)per * (k - i + 1) % mod;\n ans = (ans + (LL)per * per % mod * tmp % mod) % mod;\n } else {\n per = (LL)per * (k - i + 1) % mod;\n pinv = (LL)pinv * inv[k - (i - w) + 1] % mod;\n ans = (ans + (LL)per * per % mod * pinv % mod) % mod;\n }\n return ans;\n}\n\nint main() {\n int i, ans;\n scanf(\"%d%d\", &k, &w);\n if (k == 1) { puts(\"1\"); return 0; }\n for (i = 2, inv[1] = 1; i <= k; ++i)\n inv[i] = LL(mod / i) * (mod - inv[mod % i]) % mod;\n ans = (calc(k, w) - calc(k, w - 1)) % mod;\n printf(\"%d\\n\", (ans + mod) % mod);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["combinatorics"], "code_uid": "fe2c1f92ab7933a1429fc0b37f62cabf", "src_uid": "b715f0fdc83ec539eb3ae2b0371ee130", "difficulty": 3000.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nofstream fo(\"test.out\");\nifstream fi(\"test.inp\");\n#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)\n#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)\n#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)\n#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)\n#define ll long long\n#define pb push_back\n#define pp pop_back\n#define mp make_pair\nconst ll MOD = 1e9 + 7;\nconst ll base = 269;\nconst int oo = 1 << 30;\nconst int N = 1e5 + 5;\n/*\nMIKUNO - 15/02/1999\n*/\nll n, m, q, k, pos, t, rs, r;\nll P(ll x, ll y) // x^y\n{\n\tll ret = 1;\n\twhile (y > 0)\n\t{\n\t\tif (y & 1) ret *= x;\n\t\ty /= 2;\n\t\tx = x * x;\n\t}\n\treturn ret;\n}\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tcout.tie(NULL);\n \tcin >> n >> k;\n\tll l = 1, r = P(2,n) - 1, mid;\n\twhile (l <= r)\n\t{\n\t\tmid = (l + r) / 2;\n\t\tif (mid == k) {\n\t\t\tcout << n;\n\t\t\t//system(\"pause\");\n\t\t\treturn 0;\n\t\t}\n\t\tif (mid > k) r = mid - 1;\n\t\tif (mid < k) l = mid + 1;\n\t\tn--;\n\t}\n\t//system(\"pause\");\n}", "lang_cluster": "C++", "tags": ["constructive algorithms", "binary search", "implementation", "bitmasks"], "code_uid": "49b77fb704a0a5744c2c9818eb0de585", "src_uid": "0af400ea8e25b1a36adec4cc08912b71", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/****************### With The name of ALLAH, most Gracious, most Compassionate ###****************/\n\n/**============================================================================**\\\n| Author : Md Jubayer Hossen Jame\n| Study : CSE, BRUR\n| Today : Friday, 25 September, 2020\n| Source : Codeforces Online Judge\n| Problem link: https://codeforces.com/problemset/problem/911/B\n| Algorithm :\n|\n Help : https://algoshots.herokuapp.com/problems/two-cakes-911B/\n\\**============================================================================**/\n\n#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define FI freopen(\"input.txt\", \"r\", stdin)\n#define FO freopen(\"output.txt\", \"w\", stdout)\n#define FasterIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef unsigned int ui;\n\n#define mem(x,y) memset(x,y,sizeof(x));\n#define pii pair\n#define mk make_pair\n#define ff first\n#define ss second\n#define pb push_back\n#define ppb pop_back\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define for0(i, n) for (int i = 0; i < (int)(n); ++i)\n#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)\n#define debug(x) cout << #x << \" :: \" << x << \"\\n\"\n#define endl '\\n'\n\nconst double PI = acos(-1.0);\n\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) (a*(b/gcd(a,b)))\n\n///Direction Array\n// 4 Direction - DOWN, UP, RIGHT, LEFT\nint dx4[]= {+1, -1, +0, +0};\nint dy4[]= {+0, +0, +1, -1};\n#define isValid(a, b, R, C) (0 <= a && a < R) && (0 <= b && b < C)\nint dx8[]= {+0, +0, +1, -1, -1, +1, -1, +1};\nint dy8[]= {-1, +1, +0, +0, +1, +1, -1, -1};\n\n///Knight Moves\nint dr[] = {-1, 1, -2, -2, -1, 1, 2, 2};\nint dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};\n/**************************************************************************************************/\n\nvoid test_case()\n{\n int n, piece_a, piece_b;\n cin >> n >> piece_a >> piece_b;\n\n int mini1, mini2, ans = INT_MIN;\n mini1 = mini2 = INT_MAX;\n //10, 3, 100\n for(int plt = 1; plt < n; ++plt)\n {\n //No plate contains pieces of both cakes.\n //mini1 = min( piece_a / plt, piece_b / (n - plt) ); or\n //ans = max( ans, mini1 );\n mini2 = min( piece_b / plt, piece_a / (n - plt) );\n ans = max( ans, mini2 );\n }\n //5, 2, 3 //15, 7, 8\n cout << ans << endl;\n}\n\n//★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜★゜・。。・゜゜・。。・゜☆\n\nint main()\n{\n\n#ifndef ONLINE_JUDGE\n //FI;\n //FO;\n#endif // ONLINE_JUDGE\n FasterIO\n cout.precision(15); cout << fixed;\n\n int T = 1;\n //cin >> T;\n for(int tc = 1; tc <= T; ++tc)\n {\n //cout << \"Case #\" << tc << \":\" << endl;\n test_case();\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "521202dd626a079e9fffbc74eed2d7bd", "src_uid": "a254b1e3451c507cf7ce3e2496b3d69e", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n\nusing namespace std;\nconst int MAX_DIGIT = 10, MAX_LENGTH = 101, MODULO = 1000 * 1000 * 1000 + 7;\nint numberOfOccurance[MAX_DIGIT], numberLength;\nlong long answer[MAX_DIGIT][MAX_LENGTH], chooseMem[MAX_LENGTH][MAX_LENGTH];\n\nint choose (int r, int n) {\n\tif (r == 0 or r == n) {\n\t\treturn 1;\n\t}\n\tif (r > n or r < 0) {\n\t\treturn 0;\n\t}\n\tif (chooseMem[r][n] != -1) {\n\t\treturn chooseMem[r][n];\n\t}\n\treturn chooseMem[r][n] = ((choose(r - 1, n - 1) + choose(r, n - 1)) % MODULO);\n}\n\nint main() {\n\t// INPUT PROCESS\n\tcin >> numberLength;\n\tfor (int i = 0; i < MAX_DIGIT; ++i) {\n\t\tcin >> numberOfOccurance[i];\n\t}\n\n\t// SETTING BASE FORM FOR THE chooseMem\n\t{\n\t\tfor (int i = 0; i < MAX_LENGTH; ++i) {\n\t\t\tfor (int j = 0; j < MAX_LENGTH; ++j) {\n\t\t\t\tchooseMem[i][j] = -1;\n\t\t\t}\n\t\t}\n\t}\n\n/*\n\tanswer[i][j] is defined as followed:\n\t\tit hold the answer of this problem if we had to create a number from digit i to MAX_DIGIT (like from 4 to 9),\n\t\tand alsi it means that the answer number should exactly have j digits, now to calculate such DP we say that, \n\t\tlet's say we want to calculate the answer for k-th digit, so we say that the answer is equal to sum of all answers\n\t\tof answer[k + 1][j - numberOfAppearencesOfKthDigit] * the bionamial choosing of numberOfAppearencesOfKthDigit places\n\t\tfrom all j places for the k-th digit!\n*/\n\n\t// DP base form\n\tfor (int i = 0; i <= numberLength; ++i) {\n\t\tif (i >= numberOfOccurance[MAX_DIGIT - 1]) {\n\t\t\tanswer[MAX_DIGIT - 1][i] = 1; \n\t\t}\n\t\telse {\n\t\t\tanswer[MAX_DIGIT - 1][i] = 0;\n\t\t}\n\t}\n\n\t// DP Update\n\tfor (int maxNumber = MAX_DIGIT - 2; maxNumber > 0; --maxNumber) {\n\t\tfor (int length = numberOfOccurance[maxNumber]; length <= numberLength; ++length) {\n\t\t\tfor (int maxNumberOccurance = numberOfOccurance[maxNumber]; maxNumberOccurance <= length; ++ maxNumberOccurance) {\n\t\t\t\tanswer[maxNumber][length] += answer[maxNumber + 1][length - maxNumberOccurance] * choose(maxNumberOccurance, length);\n\t\t\t\tanswer[maxNumber][length] %= MODULO;\n\t\t\t\t//if (answer[maxNumber][length] > 0) cerr << maxNumber << \" \" << length << \" -> \" << maxNumberOccurance << \" : \" << answer[maxNumber][length] << endl;\n\t\t\t}\n\t\t}\n\t}\n\n\t// only note that since the number can't start with leading 0 (zeroes), we cannot choose the first place of the j -> j - 1\n\tfor (int length = numberOfOccurance[0]; length <= numberLength; ++length) {\n\t\tfor (int maxNumberOccurance = numberOfOccurance[0]; maxNumberOccurance <= length; ++ maxNumberOccurance) {\n\t\t\tanswer[0][length] += (answer[1][length - maxNumberOccurance] * choose(maxNumberOccurance, length - 1));\n\t\t\tanswer[0][length] %= MODULO;\n\t\t\t//if (answer[0][length] > 0) cerr << 0 << \" \" << length << \" -> \" << maxNumberOccurance << \" : \" << answer[0][length] << endl;\n\t\t}\n\t}\n\n\t// ans can be any length so we just sum up any lengths that has all digits in them (their \"i\" in \"dp[i][j]\" is equal to 0)\n\tlong long ans = 0;\n\tfor (int i = 1; i <= numberLength; ++i) {\n\t\tans += answer[0][i];\n\t\tans %= MODULO;\n\t\t//cerr << i << \" : \" << answer[0][i] << \"\t\";\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "f7376795508b10fac56f14eb8ecdd4ba", "src_uid": "c1b5169a5c3b1bd4a2f1df1069ee7755", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std ;\n\nint main(){\n\tlong long a,b,c,d;\n\tc = 0 ;\n\tcin>> a ;\n\tcin>>b ;\n\tfor(d=0 ; d>-10; d++){\n\t\tif(a>c){\n\t\t\tc = c + 1 ;\n\t\t\ta = a - c ;\n\t\t}\n\t\telse{\n\t\t\tcout<<\"Vladik\" ;\n\t\t\treturn 0 ;\n\t\t}\n\t\tif(b>c)\t{\n\t\t\tc = c + 1 ;\n\t\t\tb = b - c ;\n\t\t}\t\n\t\telse{\n\t\t\tcout<<\"Valera\" ;\n\t\t\treturn 0 ;\n\t\t}\n\t}\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "d013f9e1abba6a9055255c7f8b6d24d2", "src_uid": "87e37a82be7e39e433060fd8cdb03270", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n\nusing namespace std;\n\nconst int maxN = 1e5 + 10, mod = 1e9 + 7, maxHav = 13, maxST = (1 << 5) + 1;\n//maxHav开始写成了7 注意执行评测前注意数据范围!!! 检查一下数组空间\nint dotN, jumpK, disM, colC;\nvoid add(int &fir, long long sec) {\n\tfir = (long long) (fir + sec) % mod;\n}\nstruct Matrix{\n\tint num[210][210], row, col;\n\tvoid init(int sze) {\n\t\trow = col = sze;\n\t\tmemset(num, 0, sizeof(num));\n\t\tfor (int rowI = 0; rowI < row; ++rowI) {\n\t\t\tnum[rowI][rowI] = 1;\n\t\t}\n\t}\n\tvoid init2(int r, int c) {\n\t\trow = r, col = c;\n\t\tmemset(num, 0, sizeof(num));\n\t}\n\tMatrix operator = (const Matrix d) {\n\t\trow = d.row;\n\t\tcol = d.col;\n\t\tfor (int rowI = 0; rowI < row; ++rowI) {\n\t\t\tfor (int colJ = 0; colJ < col; ++colJ) {\n\t\t\t\tnum[rowI][colJ] = d.num[rowI][colJ];\n\t\t\t}\n\t\t}\n\t\treturn (*this);\n\t}\n\tMatrix operator * (const Matrix d) const {\t//注意,假如在main函数里进行矩阵乘法的话,ret数组的空间算在main函数里,那么就有可能爆main函数的空间\n\t\tMatrix ret;\n\t\tmemset(ret.num, 0, sizeof(ret.num));\n\t\tret.row = row;\n\t\tret.col = d.col;\n\t\tfor (int rowI = 0; rowI < ret.row; ++rowI) {\n\t\t\tfor (int colJ = 0; colJ < ret.col; ++colJ) {\n\t\t\t\tfor (int mid = 0; mid < col; ++mid) {\n\t\t\t\t\tadd(ret.num[rowI][colJ], 1ll * num[rowI][mid] * d.num[mid][colJ]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tMatrix power(int up) const {\n\t\tMatrix ret, base = (*this);\n\t\tret.init(row);\n\t\twhile (up) {\n\t\t\tif (up & 1) ret = ret * base;\n\t\t\tbase = base * base;\n\t\t\tup >>= 1;\n\t\t}\n\t\treturn ret;\n\t}\n} ans, base, orig;\n\nint pos(int er, int san) {\n\treturn er * colC + san;\t//这里应该是er(开始从下标从1开始时),不应该是 er + 1(因为一个在第i行第j列的点,编号应该是(i-1)*col+j而不是i*col+j)纸袋分析法\n}\nint bitCount(int st) {\n\tint ret = 0;\n\tfor (int weiI = 0; weiI < 4; ++weiI) {\n\t\tif (st & (1 << weiI)) {\n\t\t\t++ret;\n\t\t}\n\t}\n\treturn ret;\n}\nvoid init() {\n\tscanf(\"%d%d%d\", &dotN, &jumpK, &disM);\n\tcolC = 1 << disM;\n\tans.init(colC * (jumpK + 1));\n\torig.init2(1, colC * (jumpK + 1));\n\torig.num[0][0] = 1;\t//注意赋初始值!\n\tbase.init2(colC * (jumpK + 1), colC * (jumpK + 1));\n\tfor (int jumpJ = 0; jumpJ <= jumpK; ++jumpJ) {\n\t\tfor (int stK = 0; stK < (1 << disM); ++stK) {\n\t\t\tif (! (stK & (1 << disM - 1))) {\n\t\t\t//注意运算的优先级! !和&的优先级一样高,所以要把后面的那串括起来 \n\t\t\t\tadd(base.num[pos(jumpJ, stK << 1)][pos(jumpJ, stK)], 1);\n\t\t\t\tadd(base.num[pos(jumpJ, stK << 1 | 1)][pos(jumpJ, stK)], 1);\n\t\t\t} else {\n\t\t\t\tif (jumpJ >= 1) {\n\t\t\t\t\tadd(base.num[pos(jumpJ - 1, (stK - (1 << disM - 1)) << 1)][pos(jumpJ, stK)], \n\t\t\t\t\t\t1ll * (1 + bitCount((stK - (1 << disM - 1)) << 1)));\n\t\t\t\t\t//注意在这里计算的时候要开 long long!!! \n\t\t\t\t\tadd(base.num[pos(jumpJ - 1, (stK - (1 << disM - 1)) << 1 | 1)][pos(jumpJ, stK)], \n\t\t\t\t\t\t1ll * (1 + bitCount((stK - (1 << disM - 1)) << 1 | 1)));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\tinit();\n\tint cnt = 0;\n\tans = orig * base.power(dotN);\n\tfor (int stK = 0; stK < (1 << disM); ++stK)\n\t\tadd(cnt, ans.num[0][pos(jumpK, stK)]);\n\tprintf(\"%d\\n\", cnt);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["matrices", "dp", "bitmasks"], "code_uid": "6037689c05d6d2f735d3015a8e62e226", "src_uid": "87931a8ae9a76d85bd2a2b4bba93303d", "difficulty": 3000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nint r, h;\n\nint main () {\n\n cin >> r >> h;\n\n int a = 2 * (h / r);\n h %= r;\n a += 1 + (h * 2 >= r) + (4ll * h * h >= 3ll * r * r);\n\n cout << a;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry"], "code_uid": "172ab7e09128232238551c7b90089a68", "src_uid": "ae883bf16842c181ea4bd123dee12ef9", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define FOR(i,a,b) for(int i = a; i <= b; ++i)\nusing namespace std;\ntypedef long long ll;\nunsigned long long int n,sum,cnt;\nbool used[111111];\nmain ()\n{\n string s;\n cin >> s;\n for(int i = 0; i < s.size()-1; i++)\n {\n if(s[i] == '/' && s[i+1] == '/')\n {\n s.erase(i,1);\n i--;\n }\n }\n if(s[s.size()-1] == '/' && s.size() != 1)\n {\n s.erase(s.size()-1,1);\n }\n cout << s;\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "ccdc41d6bf05bd0a606685ea8c21926b", "src_uid": "6c2e658ac3c3d6b0569dd373806fa031", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \nusing namespace std;\nint main()\n{\n long long n;\n\tcin>>n;\n cout<\n\nusing namespace std;\n#define int long long\n\nconst int maxn = 4e5 + 20, inf = 1e17, Mod = (1ll << 32);\nint n, p, q, ent[maxn], ans;\n\nint choose(int x, int y){\n\tif(x > y) return 0;\n\tif(x == y || x == 0)return 1;\n\tvector tmp;\n\tfor(int i = 0; i < x; i++)tmp.push_back(y - i);\n\tfor(int i = 2; i <= x; i++){\n\t\t//cout << x << \" : \" << y << endl;\n\t\tint tmpp = i, tmppp = 0;\n\t\twhile(tmpp != 1){\n\t\t\tint tmpppp = __gcd(tmpp, tmp[tmppp]);\n\t\t\ttmp[tmppp] /= tmpppp;\n\t\t\ttmpp /= tmpppp;\n\t\t\ttmppp++;\n\t\t}\n\t}\n\tint res = 1;\n\tfor(int i = 0; i < x; i++){\n\t\tres = (res * tmp[i]) % Mod;\n\t}\n\treturn res;\n}\n\nint32_t main(){\n cin >> n >> p >> q;\n p = min(p, n - 1);\n for(int i = 0; i <= p; i++){\n \t//cout << i << endl;\n\t\tent[i] = choose(i, n);\n\t}\n \t\n\tfor(int i = 1; i <= q; i++){\n\t\tint tmp1 = 0, tmp2 = 1;\n\t\tfor(int j = 0; j <= p; j++){\n\t\t\ttmp1 = (tmp1 + ent[j] * tmp2) % Mod;\n\t\t\ttmp2 = (tmp2 * i) % Mod;\n\t\t}\n\t\tans = (ans ^ (i * tmp1)) % Mod;\n\t}\n cout << (ans + Mod) % Mod;\n}\n", "lang_cluster": "C++", "tags": ["meet-in-the-middle", "math", "dp"], "code_uid": "c4e30968739d85b38448abb06a43016d", "src_uid": "28cf4ff955d089318ea08d17bc4f43da", "difficulty": 2900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint pre[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};//方向数组\nchar x;\nint a,b,tx,ty,ans=0;\nint main(){\n cin>>x>>b;//输入\n a=x-'a'+1;//把字母转成数字\n for(register int i=0;i<8;i++){\n tx=a+pre[i][0];//新的x轴坐标\n ty=b+pre[i][1];//新的y轴坐标\n if(tx<1||tx>8||ty<1||ty>8){//越界\n continue;//执行下一次循环\n }\n ans++;//没有continue,ans+1\n }\n printf(\"%d\",ans);//输出答案\n return 0;//结束\n} ", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9394405e1029e0c10c71dd8931a63a27", "src_uid": "6994331ca6282669cbb7138eb7e55e01", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "\n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include\n#include\nusing namespace std;\n\nbool vowel(char c)\n{\n if(islower(c))\n c=toupper(c);\n char A[]={'A', 'E', 'I', 'O', 'U', 'Y'};\n for(int i =0;i<6;i++)\n if(c==A[i])\n return 1;\n return 0;\n}\nint main() {\nios_base::sync_with_stdio(0);\n string s;\n getline(cin,s);\n char c;\n for(int i=s.length()-1;i>=0;i--)\n if(isalpha(s[i]))\n {\n c=s[i];\n break;\n }\n if(!vowel(c))\n cout<<\"NO\";\n else\n cout<<\"YES\";\n\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "efcf791689b78d1121998b90dff2fe0c", "src_uid": "dea7eb04e086a4c1b3924eff255b9648", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#pragma comment(linker, \"/STACK:512000000\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define PI 3.14159265358979323846\n#define ull unsigned long long int\n#define ll long long int\n#define EPS 1e-9\n#define pb push_back\n#define EOS(c) (c != EOF && c != '\\0' && c != '\\n' && c != ' ')\n#define oo 1000000005\n#define ALL(v) v.begin(), v.end()\n#define mp make_pair\n#define sqr(a) (a) * (a)\n\nusing namespace std;\n\n#define FILES \"sort\"\n//#define OUT \"put.txt\"\n\ntemplatetelem MAX(telem a, telem b)\n{\n return (a > b) ? a : b;\n}\n\ntemplatetelem MIN(telem a, telem b)\n{\n return (a < b) ? a : b;\n}\n\ntemplatetelem GCD(telem a, telem b)\n{\n return b ? GCD(b, a % b) : a;\n}\n\ntemplatetelem LCM(telem a, telem b)\n{\n return a / GCD(a, b) * b;\n}\n\ntemplatetelem UNSIGNED(telem a)\n{\n return (a > 0) ? a : a * (-1);\n}\n\ntemplatevoid bez_swop(telem &a, telem &b)\n{\n telem c = a;\n a = b;\n b = c;\n}\n\nvoid swop(int &a, int &b)\n{\n a = a ^ b;\n b = a ^ b;\n a = a ^ b;\n}\n\nstring n;\nint k;\n\nint main()\n{\n\n#ifndef ONLINE_JUDGE\n#ifdef OUT\n freopen(\"in\"OUT, \"r\", stdin);\n freopen(\"out\"OUT, \"w\", stdout); \n#endif\n\n#ifdef FILES\n freopen(FILES\".in\", \"r\", stdin);\n freopen(FILES\".out\", \"w\", stdout); \n#endif\n#endif\n\n ios_base::sync_with_stdio(false);\n srand(time(NULL));\n time_t start = clock();\n\n cin >> n >> k;\n int st = 0;\n \n while(0==0)\n {\n int mx = -1;\n int pos = 0;\n\n for(int i = st; i < n.size() && i <= st + k; i++)\n {\n if(n[i] > mx)\n {\n mx = n[i];\n pos = i;\n }\n }\n\n while(pos > st)\n {\n k--;\n swap(n[pos], n[pos - 1]);\n pos--;\n }\n\n if(!k)\n break;\n\n st++;\n\n if(st == n.size())\n break;\n }\n\n cout << n;\n\n#ifndef ONLINE_JUDGE\n fprintf(stderr, \"\\n%0.3lf\\n\", (clock() - start) * 1.0 / CLOCKS_PER_SEC);\n#endif\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "e2bf99e1abd6970b71a21e8d3cd8884c", "src_uid": "e56f6c343167745821f0b18dcf0d0cde", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \nusing namespace std;\ntypedef unsigned long long ULL;\n# define MOD 1000000007\n\nint arr[] = {1, 2, 4, 6, 12, 16, 18, 30, 60, 88, 106, 126, 520, 606, 1278,\n 2202, 2280, 3216, 4252, 4422, 9688, 9940, 11212, 19936, 21700,\n 23208, 44496, 86242, 110502, 132048, 216090, 756838, 859432,\n 1257786, 1398268, 2976220, 3021376, 6972592, 13466916, 20996010};\n\nULL power(ULL a, ULL b) {\n ULL temp = 1;\n while (b > 0) {\n if ((b&1) == 1) {\n temp = (temp*a) % MOD;\n }\n a = (a*a) % MOD;\n b /= 2;\n }\n return (temp%MOD);\n}\n\nint main() {\n int N;\n cin >> N;\n cout << ((power(2, arr[N-1])-1+MOD) % MOD);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7258d4fbe0a05f5480577afd5519915e", "src_uid": "c2cbc35012c6ff7ab0d6899e6015e4e7", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include /* time */\n#define LL long long int\n#define R(i) freopen(i,\"r\",stdin)\n#define W(i) freopen(i,\"w\",stdout)\n#define R_W R(\"i.txt\"),W(\"o.txt\");\n#define FOR(i,f,t) for(int i=f;i::max()\n#define readVector(n,in,v) r(n){cin>>in;v.push_back(in);}\n#define DFS_WHITE -1\n#define DFS_BLACK 1\n#define DFS_GRAY 2\n#define ii pair< int,int >\n#define iii pair< int , ii > \n#define vi vector\n#define ll long long int\nusing namespace std;\nvector< ii > data;\nconst ll MOD = (ll)1e9 + 7;\nlong long int dp[5010][5010];\nint main() {\n\t///R_W;\n\tint n,a,b,k;\n\tcin >> n>>a>>b>>k;\n\tif (a > b){\n\t\ta = n - a + 1;\n\t\tb = n - b + 1;\n\t}\n\tfor (int i = 0; i <= n; i++) dp[0][i]=0;\n\tdp[1][0] = 0;\n\tfor (int i = 1; i <= n; i++){\n\t\tint Total = abs(b - i)-1;\n\t\tTotal += min(min(i - 1, n - i - 1), Total);\n\t\tTotal = max(Total, 0);\n\t\tdp[1][i] = (Total + dp[1][i - 1]) % MOD;\n\t}\n\tfor (int j = 2; j <= k; j++){\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tif (i == b){\n\t\t\t\tdp[j][i] = dp[j][i - 1];\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint l, r;\n\t\t\tif (i < b){\n\t\t\t\tr = max(min(b - i - 1, i - 1),0);\n\t\t\t\tl = b - i - 1;\n\t\t\t\tr = i - r;\n\t\t\t\tl = i + l;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tl = max(min(abs(b - i) - 1, n - i - 1),0);\n\t\t\t\tr = i - b - 1;\n\t\t\t\tr = i - r;\n\t\t\t\tl = i + l;\n\t\t\t}\n\t\t\tll me = (dp[j - 1][i] - dp[j - 1][i - 1] + MOD) % MOD;\n\t\t\tll all = (dp[j - 1][l] - dp[j - 1][r - 1] + MOD) % MOD;\n\t\t\tll add = (all - me + MOD) % MOD;\n\t\t\tdp[j][i] = (dp[j][i - 1] % MOD + add% MOD) % MOD;\n\t\t}\n\t}\n\tcout << (dp[k][a] - dp[k][a - 1] + MOD)%MOD << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "9c83e1968eeb57415ee50dfb8263df2c", "src_uid": "142b06ed43b3473513995de995e19fc3", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include \n#include \n#include \n#include \n#include \n#include\t \n#include\t \nusing namespace std;\ntypedef __int64 vt;\nint va[7] = { -1,0,0,-1,0,0,-1 };\nint vb[7] = { 0,-1,0,0,0 ,-1,0 };\nint vc[7] = { 0,0,-1,0,-1,0,0 };\nint ans1=-1;\nint mys(int a,int b,int c)\n{\n\tint aa = a, bb = b, cc = c;\n\tfor (int j = 0; j < 7; j++)\n\t{\n\t\ta = aa, b = bb, c = cc;\n\t\tint count = 0;\n\t\tfor (int i = j; a != -1 && b != -1 && c != -1; i++)\n\t\t{\n\t\t\ta += va[i];\n\t\t\tb += vb[i];\n\t\t\tc += vc[i];\n\t\t\tcount++;\n\t\t\tif (i == 6)\n\t\t\t\ti = -1;\n\t\t}\n\t\tcount--;\n\t\tans1 = ans1 < count ? count : ans1;\n\t}\n\treturn ans1;\n}\nint main()\n{\n\tint a, b, c;\n\tcin >> a >> b >> c;\n\tint aa, bb, cc;\n\taa = a / 3, bb = b / 2, cc = c / 2;\n\taa = aa > bb ? bb : aa;\n\taa = aa > cc ? cc : aa;\n\ta -= aa * 3;\n\tb -= aa * 2;\n\tc -= aa * 2;\n\tif (aa > 0)\n\t\tmys(a + 3, b + 2, c + 2), aa -= 1;\n\telse\n\t\tmys(a, b, c);\n\tcout << ans1+aa*7 << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "01d776f1e074d2af020274a20c852aa6", "src_uid": "e17df52cc0615585e4f8f2d31d2daafb", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define owo(i,a, b) for(int i=(a); i<(b); ++i)\n#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)\n#define senpai push_back\n#define ttgl pair\n#define ayaya cout<<\"ayaya~\"<>=1;}return res;}\nll modInv(ll a){return binpow(a, MOD-2);}\nconst double PI = acos(-1);\nconst int INF = 0x3f3f3f3f;\nconst int NINF = 0xc0c0c0c0;\nconst ll INFLL = 0x3f3f3f3f3f3f3f3f;\nconst ll NINFLL = 0xc0c0c0c0c0c0c0c0;\nconst int mxN = 101;\nint cntbad = 0;\nint cnt[26];\nint arr[mxN];\nint val[mxN];\nint n;\nstring s;\nvector mins;\nint sum = 0;\nint main()\n{ \n //freopen(\"exercise.in\", \"r\", stdin);\n //freopen(\"exercise.out\", \"w\", stdout);\n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>s;\n owo(i, 0, n) {\n arr[i] = s[i]-'a';\n cin>>val[i];\n sum+=val[i];\n }\n owo(i, 0, n/2) {\n if(arr[i]==arr[n-i-1]) {\n cnt[arr[i]]++;\n cntbad++;\n }\n }\n int lose = 0;\n int badnum = -1;\n int over = 0;\n owo(i, 0, n/2) {\n if(arr[i]==arr[n-i-1]) {\n //cout<cntbad) {\n badnum = arr[i];\n over = 2*cnt[arr[i]]-cntbad;\n }\n lose+=min(val[i], val[n-i-1]);\n }\n }\n if(badnum==-1) {\n cout<<(sum-lose)<<\"\\n\";\n return 0;\n }\n //cout<\nusing namespace std;\nint s,v1,t1,v2,t2,a,b;\nint main()\n{\n\tscanf(\"%d%d%d%d%d\",&s,&v1,&v2,&t1,&t2);\n\ta=s*v1+t1*2; b=s*v2+t2*2;\n\tif(ab)printf(\"Second\");\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "7419832eecfa10e50160a811e33b4736", "src_uid": "10226b8efe9e3c473239d747b911a1ef", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#define ll long long\nusing namespace std;\ninline void Rd(int &res){\n\tchar c;res=0;\n\twhile(c=getchar(),c<'0');\n\tdo{\n\t\tres=(res<<1)+(res<<3)+(c^48);\n\t}while(c=getchar(),c>='0');\n}\nconst int N=32;\nconst int M=35;\nconst int P=(int)1e9+7;\nint q;\nint dp1[M][2][2][2],dp2[M][2][2][2],X[M],Y[M],K[M];\ninline void Add(int &a,int b){\n\ta=(a+b)%P;\n}\nint solve(int x,int y,int k){\n\tif(x<0||y<0)return 0;\n\tmemset(dp1,0,sizeof(dp1));\n\tmemset(dp2,0,sizeof(dp2));\n\tfor(int i=1;i<=N;i++){\n\t\tX[i]=(x&1),Y[i]=(y&1),K[i]=(k&1);\n\t\tx>>=1,y>>=1,k>>=1;\n\t}\n\treverse(X+1,X+N+1),reverse(Y+1,Y+N+1),reverse(K+1,K+N+1);\n\tdp1[1][1][1][1]=1;\n\tfor(int i=1;iX[i+1]))continue;\n\t\t\t\t\t\t\tif(k&&(y>Y[i+1]))continue;\n\t\t\t\t\t\t\tif(t&&(z>K[i+1]))continue; \n\t\t\t\t\t\t\tAdd(dp1[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp1[i][j][k][t]);\n\t\t\t\t\t\t\tAdd(dp2[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp2[i][j][k][t]);\n\t\t\t\t\t\t\tAdd(dp2[i+1][j&(x==X[i+1])][k&(y==Y[i+1])][t&(z==K[i+1])],dp1[i][j][k][t]*((ll)z<<(N-1-i))%P);\n\t\t\t\t\t\t}\n\tint res=0;\n\tfor(int i=0;i<2;i++)\n\t\tfor(int j=0;j<2;j++)\n\t\t\tfor(int t=0;t<2;t++)\n\t\t\t\tAdd(res,(dp1[N][i][j][t]+dp2[N][i][j][t])%P);\n\treturn res;\n}\nint main(){\n\tRd(q);\n\twhile(q--){\n\t\tint x1,y1,x2,y2,k,ans=0;\n\t\tRd(x1),Rd(y1),Rd(x2),Rd(y2),Rd(k);\n\t\tx1--,y1--,x2--,y2--,k--;\n\t\tans=(ans+solve(x2,y2,k))%P;\n\t\tans=(ans-solve(x1-1,y2,k)+P)%P;\n\t\tans=(ans-solve(x2,y1-1,k)+P)%P;\n\t\tans=(ans+solve(x1-1,y1-1,k))%P;\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["divide and conquer", "dp"], "code_uid": "11b06dc3ae4b68074fd54c2d847bda6f", "src_uid": "1ab085026ce43810acf98cc4bf8faf26", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n#define MAX 10000000\nint a[MAX+10];\n\nint main()\n{\n\n for(int i=1;i*i<=MAX;i++)\n {\n int sq=i*i;\n for(int j=sq;j<=MAX;j+=sq)\n {\n a[j]=sq;\n }\n }\n\n long long y,n;\n cin>>y>>n;\n long long sum=0;\n for(long long i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)\n#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)\n#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)\n#define db(x) {cout << #x << \" = \" << (x) << endl;}\n#define dba(a, x, y) {cout<<#a<<\" :\";FOR(i123,x,y)cout< > > adj;\n vector dist;\npublic:\n ShortestPathWeightedGraph(int N)\n {\n this->N = N;\n adj.resize(N);\n }\n void add(int u, int v, long long weight)\n {\n adj[u].push_back(make_pair(v, weight));\n }\n int shortest(int from, int to)\n {\n dist.resize(N);\n fill(dist.begin(), dist.end(), -1);\n priority_queue > q;\n q.push(make_pair(0, from));\n dist[from] = 0;\n while (!q.empty())\n {\n int u = q.top().second;\n ll distu = -q.top().first;\n q.pop();\n if (dist[u] < distu) continue;\n for (int i = 0; i < adj[u].size(); i++)\n {\n int v = adj[u][i].first;\n long long weight = adj[u][i].second;\n if (dist[v] == -1 || dist[u] + weight < dist[v])\n {\n dist[v] = dist[u] + weight;\n q.push(make_pair(-dist[v], v));\n }\n }\n }\n return dist[to];\n }\n};\n\n\nint nodenumber(int type, int x, int y)\n{\n return 3 * (N * x + y) + type;\n}\nint main()\n{\n ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0);\n cin >> N;\n rep(x,N) rep(y,N)\n {\n int a;\n cin >> a;\n X[a] = x;\n Y[a] = y;\n }\n ShortestPathWeightedGraph graph(3 * N * N);\n int baseweight = 1000;\n rep(x,N) rep(y,N)\n {\n rep(type1,3) rep(type2,3) if (type1 != type2)\n {\n graph.add(nodenumber(type1, x, y), nodenumber(type2, x, y), baseweight + 1);\n }\n // knight\n rep(i,8)\n {\n int xx = x + dx[i];\n int yy = y + dy[i];\n if (0 <= xx && xx < N && 0 <= yy && yy < N)\n {\n graph.add(nodenumber(KNIGHT, x, y), nodenumber(KNIGHT, xx, yy), baseweight);\n }\n }\n // vertical & horizontal\n rep(i,N)\n {\n if(x != i) graph.add(nodenumber(VH, x, y), nodenumber(VH, i, y), baseweight);\n if(y != i) graph.add(nodenumber(VH, x, y), nodenumber(VH, x, i), baseweight);\n }\n // diagonal\n rep(i,N)\n {\n for (int dirx = -1; dirx <= 1; dirx += 2)\n {\n for (int diry = -1; diry <= 1; diry += 2)\n {\n int xx = x + i * dirx;\n int yy = y + i * diry;\n if (x == xx && y == yy) continue;\n if (0 <= xx && xx < N && 0 <= yy && yy < N)\n {\n graph.add(nodenumber(DIAGONAL, x, y), nodenumber(DIAGONAL, xx, yy), baseweight);\n }\n }\n }\n }\n }\n vector best;\n\n rep(i,3) best.pb(0);\n for (int n = 2; n <= N * N; n++)\n {\n vector nextbest;\n rep(i,3) nextbest.pb(INF);\n rep(prev,3) rep(cur,3)\n {\n ll shortest_dist = graph.shortest(nodenumber(prev, X[n - 1], Y[n - 1]), nodenumber(cur, X[n], Y[n]));\n nextbest[cur] = min(nextbest[cur], best[prev] + shortest_dist);\n }\n best = nextbest;\n }\n ll mn = INFL;\n rep(i,sz(best)) mn = min(mn, best[i]);\n cout << mn / baseweight << \" \" << mn % baseweight << endl;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "dp", "shortest paths"], "code_uid": "f50162fed3ff124e805003cb0a57d488", "src_uid": "5fe44b6cd804e0766a0e993eca1846cd", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\n\n\nint main()\n{\n\n int l,arr[3],x=1,y=1,z=1;\n cin>>l>>arr[0]>>arr[1]>>arr[2];\n sort (arr,arr+3);\n for (int i=0; arr[1]*i<=l; i++)\n for (int j=0; arr[0]*j<=l; j++)\n if (l - (i*arr[1] + j*arr[0] )>= 0 && ( l - (i*arr[1] + j*arr[0] ))%arr[2]==0 ){\n x=i;\n y=j;\n }\n z=( l - (x*arr[1] + y*arr[0]))/arr[2];\n int maxm=x +y+z;\n for (int i=0; arr[0]*i<=l; i++)\n for (int j=0; arr[1]*j<=l; j++)\n if (l - (i*arr[0] + j*arr[1] )>= 0 && ( l - (i*arr[0] + j*arr[1] ))%arr[2]==0 ){\n x=i;\n y=j;\n }\n z=( l - (x*arr[0] + y*arr[1]))/arr[2];\n cout<\n//#include\n//#include\n//#include\nusing namespace std;\n#define int long long\nint gcd(int a,int b){\n return b==0?a:gcd(b,a%b);\n}\nint ppow(int a,int b,int mod){\n int ans=0;\n while(b){\n if(b&1)ans=ans*a%mod;\n a=a*a%mod;\n b>>=1;\n }\n return ans;\n}\nvoid ac(){\n cout<<\"YES\"<>x[0]>>y[0]>>ax>>ay>>bx>>by;//注意x[0]和y[0]也是一个点,下面别忘记了\n cin>>xs>>ys>>t;\n while(x[cnt]-xs\n#define iter ::iterator\nusing namespace std;\ntypedef long long ll;\ntypedef pairP;\ntypedef pairP1;\n#define mk make_pair\n#define pb push_back\n#define se second\n#define fi first\n#define rs o<<1|1\n#define ls o<<1\nconst int N=4e5+5;\nll mod=1e9+7;\nll x,y;\nll d[2][N];\nint main(){\n\tIO;\n\tcin>>x>>y;\n\tif(x>y)swap(x,y);\n\tint cur=0,res=0;\n\td[0][0]=1;\n\td[1][0]=1;\n\tfor(int i=1;i+res<=x+y;i++){\n\t\tres+=i;\n\t\tcur=!cur;\n\t\tfor(int j=1;j\nusing namespace std;\nint main()\n{\n long long n;\n int k;\n cin>>n>>k;\n int a[10];\n int i=0,c=0;\n long long m=n;\n if(m==0)\n cout<<0;\n else\n {\n\n while(n)\n {\n a[i]=n%10;\n n=n/10;\n i++;\n c++;\n\n\n\n\n }\n if(c>k)\n {\n int j=0;\n int l=0;\n for(i=0; i\n#include \n#include\nusing namespace std;\nlong long g1,g2,g3;\nint main(){\n //freopen(\"in.txt\",\"r\",stdin);\n int k,f1,f2,f3,n,f4,t1,t2,t3,t4,r,c;\n cin >> g1 >> g2;\n if(g2>g1){\n cout << g1 <\nusing namespace std;\n#define MOD 1000000007\nint n,a,b,c;\nint main()\n{\nstring s=\"Danil\";string s1=\"Olya\"; string s2=\"Slava\", s3=\"Ann\", s4=\"Nikita\";\nstring st;\ncin>>st;\nst=\"gggggggg\"+st+\"gggggggggggg\";\nint cnt=0;\nfor(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define fst first\n#define snd second\n#define all(x) (x).begin(), (x).end()\n#define clr( a , v ) memset( a , v , sizeof(a) )\n#define pb push_back\n#define mp make_pair\n#define sz size()\n#define FORN( i , s , n ) for( int i = s ; i < (int)(n) ; i++ )\n#define FOR( i , n ) FORN( i , 0 , n )\n#define FORIT(i,x) for( typeof x.begin() i = x.begin() ; i != x.end() ; i++ )\n#define trace(x) cerr << #x << \": \" << x << endl;\n#define trace2(x, y) cerr << #x << \": \" << x << \" | \" << #y << \": \" << y << endl;\n#define read ios::sync_with_stdio(false)\n\nusing namespace std;\n\ntypedef long long int64;\ntypedef vector vi;\ntypedef pair ii;\ntypedef vector vs;\ntypedef vector vii;\n\nint main(){\n set S;\n int64 n, a, ans = 0, b = 0;\n cin >> n;\n for( int64 k = 1 ; k < n ; k++ ){\n a = sqrt( 1.0*( n*n - k*k ) );\n if( a > k ) ans++;\n if( a == k ) b = 1;\n }\n if( n == 0 ) cout << \"1\" << endl;\n else cout << ans * 8 + 4 * ( b + 1 ) << endl;\n return 0;\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "8651a17b966eb2e920c4cb0ab4a5198b", "src_uid": "d87ce09acb8401e910ca6ef3529566f4", "difficulty": null, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define ln 100005\n#define pi acos(-1.0)\n#define pb push_back\n#define mp nake_pair\nusing namespace std;\nint main()\n{\n long long int number;\n cin>>number;\n long long int N=ln;\n long long int arr[ln];\n arr[0]=0;\n for(long long int i=1;i\nusing namespace std;\n\n#define F first\n#define S second\n#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)\n#define NFOR(i,a,b) for(int i = (a); i >= (b); --i)\n#define all(x) (x).begin(), (x).end()\n#define sz(x) int(x.size())\ntypedef long long ll; typedef pair ii; typedef vector vi; const int inf = 1e9 + 7;\n#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)\ntemplate void dbs(string str, T t) {cout << str << \" : \" << t << endl;}\ntemplate void dbs(string str, T t, S... s) {int idx = str.find(','); cout << str.substr(0, idx) << \" : \" << t << \",\"; dbs(str.substr(idx + 1), s...);}\ntemplate ostream& operator <<(ostream& os, const pair& p) {return os << \"(\" << p.first << \", \" << p.second << \")\";}\ntemplate ostream& operator <<(ostream& os, const vector& p) {os << \"[ \"; for (auto& it : p) os << it << \" \"; return os << \"]\";}\ntemplate ostream& operator <<(ostream& os, const set& p) {os << \"[ \"; for (auto& it : p) os << it << \" \"; return os << \"]\";}\ntemplate ostream& operator <<(ostream& os, const map& p) {os << \"[ \"; for (auto& it : p) os << it << \" \"; return os << \"]\";}\ntemplate void prc(T a, T b) {cout << \"[\"; for (T i = a; i != b; ++i) {if (i != a) cout << \", \"; cout << *i;} cout << \"]\";cout<>= 1;\n }\n return ans;\n}\nvoid ok(int &x) {\n\tif (x >= inf) x -= inf;\n\tif (x < 0) x += inf;\n}\nint main()\n{\n\tios::sync_with_stdio(0); cin.tie(0);\n\n\tFOR (i, 1, N) sv[i] = i;\n\tFOR (i, 2, 2000) if (sv[i] == i) for (int j = i * i; j <= N; j += i) sv[j] = i;\n\n\tmu[1] = 1;\n\tFOR (i, 2, N) {\n\t\tint x = sv[i];\n\t\tint g = (i / x) % x != 0;\n\t\tmu[i] = mu[i / x] * -g;\n\t}\n\n\tint n, k; cin >> n >> k;\n\n\tFOR (i, 1, N) {\n\t\tif (sv[i] == i) {\n\t\t\tpw[i] = expo(i, n);\n\t\t} else {\n\t\t\tpw[i] = (pw[sv[i]] * 1LL * pw[i/sv[i]]) % inf;\n\t\t}\n\t}\n\n\tFOR (i, 1, N) if (mu[i]) {\n\t\tfor (int j = i; j <= N; j += i) dv[j].push_back(i);\n\t}\n\n\n\tint ans = 0, cur = 1;\n\tFOR (i, 2, k) {\n\t\tfor (int x : dv[i]) {\n\t\t\tok(cur -= pw[(i-1)/x]*mu[x]);\n\t\t\tok(cur += pw[i/x]*mu[x]);\n\t\t}\n\t\tok(ans += (cur^i));\n\t}\n\tcout << ans << \"\\n\";\n\n\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "7557ecda386b456b5e855085b93e66b0", "src_uid": "122c08aa91c9a9d6a151ee6e3d0662fa", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nint main(){\n\n\tint n,m,a,b,res = 0;\n\t\n\tscanf(\"%d%d%d%d\",&n,&m,&a,&b);\n\t\n\t//int c = n / m;\n\t//int d = n%m;\n\t\n\t/*if(b / m - a / m > 1){\n\t\tres++;\n\t}\n\t\n\tif((a - 1) % m != 0) res++;\n\t\n\t\n\tif(b %m != 0 && b != n) res++;\n\t\n\t//if(b == n && ((a - 1)%m == 0)) res = 1;\n\tif(b/m - a/m == 0){res = 1;}\n\t\n\tprintf(\"%d\",res);\n\t*/\n\t\n\t\n\tif((b - 1)/ m - (a - 1) / m == 1){\n\t\tres = 2;\n\t}\n\t\n\tif((b % m - a % m == -1) || (b%m - (a%m+m) == -1 )){\n\t\tres = 2;\n\t}\n\t\n\tif(b == n){\n\t\tres = 2;\n\t}\n\tif(b %m == 0){\n\t\tres = 2;\n\t}\n\t\n\tif((a - 1) %m == 0){\n\t\tres = 2;\n\t}\n\t\n\t\n\tif((b-1)/m - (a-1)/m == 0){res = 1;}\n\t\n\t\n\tif(((a - 1) %m == 0) && (b%m == 0)) {res = 1;}\n\t\n\tif(b == n && ((a - 1)%m == 0)) {res = 1;}\n\t\n\t\n\tif(res == 0){\n\t\tres = 3;\n\t}\n\t\n\tprintf(\"%d\",res);\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "3501775335471b587188fff6fb273645", "src_uid": "f256235c0b2815aae85a6f9435c69dac", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nmain(){\n long long n,k;\n cin>>n>>k;\n long long a[n];\n long long vt=0,vt2=0;\n for(long long i=1; i<=n; i++){\n cin>>a[i];\n }\n for(long long i=1; i<=n; i++){\n if(a[i] > k){ vt=i; break;}\n }\n for(long long i=n; i>=1; i--){\n if(a[i] > k){ vt2=i; break;}\n }\n if(vt==vt2 && vt==0) cout<\nusing namespace std;\nint k;\nstring a[6]={\"+------------------------+\",\n \"|#.#.#.#.#.#.#.#.#.#.#.|D|)\",\n\t\t\t \"|#.#.#.#.#.#.#.#.#.#.#.|.|\",\n\t\t\t \"|#.......................|\",\n\t\t\t \"|#.#.#.#.#.#.#.#.#.#.#.|.|)\",\n\t\t\t \"+------------------------+\"};\nint main(){\n\tcin>>k;\n\tfor(int i=0;i<23;i++){\n\t\tfor(int j=0;j<6;j++){\n\t\t\tif(a[j][i]=='#'){\n\t\t\t\ta[j][i]='O';\n\t\t\t\tk--;\n\t\t\t} \n\t\t\tif(k==0)\n\t\t\tbreak;\n\t\t}\n\t\tif(k==0)\n\t\tbreak;\n\t}\n\tfor(int i=0;i<6;i++){\n\t\tcout<\n#define ll long long\n#define mset(a,x) memset(a,x,sizeof(a))\n \nusing namespace std;\nconst double PI=acos(-1);\nconst int inf=0x3f3f3f3f;\nconst double esp=1e-6;\nconst int maxn=1e6+5;\nconst int mod=1e9+7;\nint dir[4][2]={0,1,1,0,0,-1,-1,0};\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\nll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}\nll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}\nll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}\nll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}\n\nint main()\n{\n\tint l,r,a;\n\tcin>>l>>r>>a;\n\tint ans=a;\n\tif(l>r)\n\tans-=l-r;\n\telse\n\tans-=r-l;\n\tif(ans>=0)\n\t{\n\t\tcout<\n\nusing namespace std;\n\n#define int int64_t\nconst int MAX = 7e5+1;\nconst int MOD = 1e6+3;\n\nint fac[MAX], infac[MAX];\n\nint Pow(int x, int y) {\n\tint curr = 1;\n\twhile(y > 0) {\n\t\tif(y % 2 != 0) curr = (curr*x) % MOD;\n\t\tx = (x*x) % MOD;\n\t\ty /= 2;\n\t}\n\t\n\treturn curr;\n}\n\nvoid precompute(int s) {\n\tfac[0] = 1;\n\tfor(int i = 1; i <= s; i++) fac[i] = (fac[i-1]*i) % MOD;\n\tinfac[s] = Pow(fac[s], MOD-2);\n\tfor(int i = s-1; i >= 0; i--) infac[i] = ((i+1)*infac[i+1]) % MOD;\n}\n\nint bi(int n, int r) {\n\treturn ((fac[n]*infac[r]) % MOD * infac[n-r]) % MOD;\n}\n\nsigned main() {\n\tint n, c, total = 0;\n\tcin >> n >> c;\n\tprecompute(n+c-1);\n\tfor(int i = 1; i <= n; i++) total = (total+bi(i+c-1, i)) % MOD;\n\tcout << total;\n}\n", "lang_cluster": "C++", "tags": ["combinatorics"], "code_uid": "b053ba2d42b39be9f6a5679e1c7fef10", "src_uid": "e63c70a9c96a94bce99618f2e695f83a", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#pragma comment(linker, \"/stack:16777216\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define FOR(i, a, b) for(int i = (a); i < (b); ++i)\n#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)\n#define REP(i, N) FOR(i, 0, N)\n#define RREP(i, N) RFOR(i, N, 0)\n#define FILL(A,value) memset(A,value,sizeof(A))\n\n#define ALL(V) V.begin(), V.end()\n#define SZ(V) (int)V.size()\n#define PB push_back\n#define MP make_pair\n#define Pi 3.14159265358979\n\ntypedef long long Int;\ntypedef unsigned long long UINT;\ntypedef vector VI;\ntypedef pair PII;\n\nconst int INF = 1000000000;\nconst int MAX = 500007;\nconst int MAX2 = 207;\nconst int BASE = 1000000000;\nconst int CNT = 500;\nconst int MOD = 1000000007;\n\ndouble dp[MAX][107];\n\nint p[MAX];\n\nint main()\n{\n //freopen(\"in.txt\", \"r\", stdin);\n\n\tint n = 1;\n\tp[0] = -1;\n\n\tFOR(i,0,107)\n\t{\n\t\tdp[0][i] = 1;\n\t}\n\n\tint q;\n\tcin >> q;\n\tFOR(qq,0,q)\n\t{\n\t\tint t;\n\t\tscanf(\"%d\" , &t);\n\t\tif (t == 1)\n\t\t{\n\t\t\tint par;\n\t\t\tscanf(\"%d\" , &par);\n\t\t\t--par;\n\t\t\tint v = n;\n\t\t\tp[v] = par;\n\t\t\t++ n;\n\t\t\tFOR(j,0,107)\n\t\t\t{\n\t\t\t\tdp[v][j] = 1;\n\t\t\t}\n\t\t\tdouble prevV = dp[par][0];\n\t\t\tdp[par][0] *= 0.5;\n\t\t\tint j = 1;\n\t\t\twhile (p[par] != -1 && j <= 102)\n\t\t\t{\n\t\t\t\tint pp = p[par];\n\t\t\t\tdouble pV = dp[pp][j];\n\t\t\t\tdp[pp][j] /= (0.5 + 0.5 * prevV);\n\n\t\t\t\tprevV = pV;\n\t\t\t\tdp[pp][j] *= (0.5 + 0.5 * dp[par][j - 1]);\n\t\t\t\tpar = pp;\n\t\t\t\t++j;\n\t\t\t}\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint v;\n\t\t\tscanf(\"%d\" , &v);\n\t\t\t--v;\n\t\t\tdouble res = 0;\n\n\t\t\tFOR(i,1,100)\n\t\t\t{\n\t\t\t\tres += (dp[v][i] - dp[v][i - 1]) * i;\n\t\t\t}\n\t\t\tprintf(\"%.10f\\n\" , res);\n\t\t}\n\t}\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities", "trees"], "code_uid": "632f25af1affc0c713f6facfd7c8f800", "src_uid": "55affe752cb214d1e4031a9e3972597b", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\nstring s1,s2;\nint k = 0;\n\nint main() {\n cin >> s1;\n int c = s1.size();\n int g = 0, b = 0;\n for(int i; i < c-1; i++){\n if (s1[i] != s1[i+1]) b = 1; \n }\n if (b==1){\n if (c%2 != 0){\n g = 1;\n for(int i = 0; i < c; i++){\n if (i != (c/2)){\n \n s2 += s1[i];\n }\n }\n } \n else s2 = s1;\n c = s2.size();\n for(int i = 0; i < c; i++){\n if (s2[i] != s2[c-i-1]){\n k = 1;\n }\n }\n if(k==0) k = c - 1 + g;\n else k = c + g;\n cout << k << endl;\n }\n else cout << b << endl; \n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "strings", "implementation"], "code_uid": "f10fb5db215868209dfa9332dfbe4df1", "src_uid": "6c85175d334f811617e7030e0403f706", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#ifdef _MSC_VER\n#define __builtin_popcount (int)__popcnt\n#define __builtin_popcountll (int)__popcnt64\n#define __builtin_clz (int)__lzcnt\n#define $ system(\"pause\")\n#define gp_hash_table unordered_map\n#else\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4\")\n#include \n#include \nusing namespace __gnu_pbds;\n#define $\n//#define USACO\n//#define IOI_style\n#endif\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\ntypedef long double ld;\ntypedef complex point;\n\n#define MOD 12345\n#define MAXN 123\n#define LG 20\n#define inf int(1e9 + 100)\n\n#define rep(i, j, k) for (int i = j; i < k; i++)\n#define for_all(x) x.begin(), x.end()\n#define lid id << 1\n#define rid id << 1 | 1\n#define lch l, mid, lid\n#define rch mid, r, rid\n#define pb push_back\n#define X first\n#define Y second\n#define re real()\n#define im imag()\n\ntemplate \ninline ostream &operator<<(ostream &out, const pair &p)\n{\n\treturn out << \"( \" << p.X << \" , \" << p.Y << \" )\";\n}\ntemplate \ninline ostream &operator<<(ostream &out, const vector &p)\n{\n\tfor (auto &e : p) out << e; return out;\n}\ntemplate \ninline T smin(T &a, const S &b) { return a > b ? a = b : a; }\ntemplate \ninline T smax(T &a, const S &b) { return a < b ? a = b : a; }\ninline double dot(const point l, const point r) { return l.re * r.re + l.im * r.im; }\ninline double cross(const point l, const point r) { return l.re * r.im - l.im * r.re; }\nll po(ll v, ll u) { return u ? (po(v * v % MOD, u >> 1) * (u & 1 ? v : 1) % MOD) : 1; }\ntemplate inline void add(T &l, const T &r) { l = (l + r) % MOD; }\nll gcd(ll v, ll u) { return u ? gcd(u, v % u) : v; }\n\nll n;\nint c;\nvector vec, o[30];\n\nstruct Mat {\n\tint n, m;\n\tll arr[MAXN][MAXN];\n\tMat(int n, int m, int z = 0) : n(n), m(m) {\n\t\tmemset(arr, 0, sizeof(arr));\n\t\trep(i, 0, min(n, m))\n\t\t\tarr[i][i] = z;\n\t}\n};\n\ninline ostream &operator<<(ostream &out, Mat &p)\n{\n\trep(i, 0, p.n) {\n\t\trep(j, 0, p.m)\n\t\t\tcout << p.arr[i][j] << ' ';\n\t\tcout << endl;\n\t}\n\treturn out;\n}\n\ninline Mat operator*(Mat l, Mat r) {\n\tMat res(l.n, r.m);\n\trep(i, 0, res.n) rep(j, 0, res.m) rep(k, 0, l.m)\n\t\tres.arr[i][j] += l.arr[i][k] * r.arr[k][j];\n\trep(i, 0, res.n) rep(j, 0, res.m) res.arr[i][j] %= MOD;\n\treturn res;\n}\n\nMat operator^ (const Mat & v, ll u)\n{\n\tMat res(v.n, v.m, 1);\n\tfor (int i = 62; ~i; i--) {\n\t\tres = res * res;\n\t\tif (u & (1ll << i)) res = res * v;\n\t}\n\treturn res;\n}\n\nvector state, f[MAXN];\nmap, int> rf;\nint NOW, sz[MAXN];\nvoid build(int g = 0) {\n\tif (g == c) {\n\t\tf[NOW] = state;\n\t\trf[state] = NOW++;\n\t\treturn;\n\t}\n\trep(i, 0, sz[vec[g]]) {\n\t\tstate[g] = i;\n\t\tbuild(g + 1);\n\t}\n\treturn;\n}\n\n#ifndef IOI_style\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n#ifdef USACO\n\tfreopen(\"gathering.in\", \"r\", stdin);\n\tfreopen(\"gathering.out\", \"w\", stdout);\n#endif\n\tcin >> n >> c;\n\trep(i, 0, c) {\n\t\tchar v; int m;\n\t\tcin >> v >> m;\n\t\tv -= 'A';\n\t\tvec.pb(v);\n\t\to[v].pb(m);\n\t}\n\tsort(for_all(vec));\n\tvec.resize(unique(for_all(vec)) - vec.begin());\n\tfor (auto e : vec) {\n\t\tsort(for_all(o[e]));\n\t\to[e].resize(unique(for_all(o[e])) - o[e].begin());\n\t\tsz[e] = 1;\n\t\tfor (auto z : o[e]) sz[e] *= z;\n\t}\n\tstate.resize(vec.size());\n\tc = vec.size(); build();\n\tMat M(NOW, NOW);\n\trep(i, 0, NOW) {\n\t\tauto v = f[i];\n\t\trep(j, 0, c) {\n\t\t\tv[j] = (v[j] + 1) % sz[vec[j]];\n\t\t\tM.arr[rf[v]][i]++;\n\t\t\tv[j] = (v[j] - 1 + sz[vec[j]]) % sz[vec[j]];\n\t\t}\n\t}\n\tM = M ^ n;\n\tll answer = 0;\n\trep(i, 0, NOW) {\n\t\tauto v = f[i];\n\t\tbool bad = false;\n\t\trep(j, 0, c) {\n\t\t\tbool flag = false;\n\t\t\tfor (auto z : o[vec[j]])\n\t\t\t\tif (v[j] % z == 0) flag = true;\n\t\t\tif (!flag) bad = true;\n\t\t}\n\t\tif (bad) continue;\n\t\tadd(answer, M.arr[i][0]);\n\t}\n\tcout << answer << endl;\n\t$;\n\treturn 0;\n}\n#endif\n", "lang_cluster": "C++", "tags": ["matrices", "dp", "graphs"], "code_uid": "0044782a33d493dd8eaa5ab7881d763e", "src_uid": "76d4684d26dac380713a566a1e277c91", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*\n - Author : Ahmed Araby\n - submitted by an Alien so it's not and ordinary code,\n don't till any one.\n*/\n#include \n/*#include \n#include \n#include \n#include \n#include \n#include \n#include */\n#define ll long long\n#define pi pair\n#define pl pair\nusing namespace std;\n\nvoid run()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n}\nll mem[50][50][50];\nll solve(int l , int r , int h) // we use the third dimension h to detect the case of restriction of none\n{\n if(l>r || l==r)\n return 1;\n ll &ret=mem[l][r][h];\n if(ret!=-1)\n return ret;\n ret=0;\n for(int i=l; i<=r; i++)\n {\n ll tmp=0;\n if(i-l>=h-1 || r-i>=h-1)\n {\n ll r1 , r2 , r3 , r4;\n if(i-l>=h-1)\n r1=solve(l , i-1 , max(h-1 , 0) );\n else\n r1=solve(l , i-1 , 0);\n if(r-i>=h-1)\n r2=solve(i+1 , r , max(h-1 , 0) );\n else\n r2=solve(i+1 , r , 0);\n tmp=r1*r2;\n //cout<<\"out\"<<\" \"<=h-1 && r-i>=h-1)\n {\n // here we calculates the possible trees that some part of it have no restriction\n r3=solve(l , i-1 , 0); // no res\n r3-=r1; // remove duplication\n r4=solve(i+1 , r , 0);\n r4-=r2; // remove duplication\n // missed calculation\n tmp+=r3*r2;\n tmp+=r4*r1;\n //cout<>n>>h;\n\tfor(int i=0; i<=n; i++)\n for(int j=0; j<=n; j++)\n for(int k=0; k<=n; k++)\n mem[i][j][k]=-1;\n\tcout<\nusing namespace std;\n#define LL long long\n\nint main(){ \n//freopen(\"input.txt\",\"r\",stdin); freopen(\"output.txt\",\"w\",stdout);\n\t\n\tLL a,b,c;\n\tcin >> a >> b >> c;\n\t\n\tif(!c){\n\t\t\n\t\tif(a == b) cout<< \"YES\\n\";\n\t\telse cout << \"NO\\n\";\n\t\treturn 0;\n\t}\n\tif(a > 0 && c > 0 && b <= 0){\n\t\tcout << \"NO\\n\";\n\t\treturn 0;\n\t}\n\tif(c < 0 && b > a){\n\t\tcout << \"NO\\n\";\n\t\treturn 0;\n\t}\n\tif(c > 0 && b < a){\n\t\tcout << \"NO\\n\";\n\t\treturn 0;\n\t}\n\t\n\tif( (b - a) % c)\n\t\tcout << \"NO\\n\";\n\t\t\n\telse{\n\t\t\n\t\tif( ((b - a)/c) +1 > 0)\n\t\t\tcout << \"YES\\n\";\n\t\telse\n\t\t\tcout << \"NO\\n\";\n\t}\n\t\t\n\t\n return 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "31b34e0bb3c4b33833b85e22660c6c1d", "src_uid": "9edf42c20ddf22a251b84553d7305a7d", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "// || || |____| //\n// || || || // \n// \\\\__// || // \n#include\ntypedef long long int ll;\ntypedef long double ld;\n#include \n#define mp make_pair\n#define F first\n#define S second\n#define vll vector \n#define mll map \n#define pb push_back\n#define pf push_front\n#define ub upper_bound\n#define lb lower_bound\n#define popf pop_front\n#define popb pop_back\n#define M 1000000007\n#define M1 998244353\n#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);\n#define test ll t;cin>>(t);while(t--)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n\n#define f(a, b) for (ll i = a; i < b; i++)\n#define fr(a, b) for (ll j = a; j >= b; j--)\n#define fi(a, b) for (ll j = a; j < b; j++)\n#define fii(a, b) for (ll k = a; k < b; k++)\n#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))\n\nusing namespace std;\nvoid start()\n{\n fast;\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n}\n\n/***********************************************************************************************************/\n\nint main()\n{\n start();\n string s; cin>>s;\n ll c=0,ans=0;\n f(0,s.length())\n {\n if(s[i]==s[i+1])\n {\n c++;\n if(c==5)\n {\n ans++;\n c=0;\n }\n }\n else\n {\n c=0;\n ans++;\n }\n }\n cout<\nusing namespace std;\n\nint a[55];//jdshfsdssf\n\nint main()//jdsyggdsftsf\n{\n\tios::sync_with_stdio(0);//dwfeffgsdgdgedhshjhhsddgyegfgfg\n\tint n,m,i=1;//rutututututut\n\tcin>>n>>m;\n\twhile(m>=i){\n\t\tm-=i;//jddsshshs\n\t\ti++;//ajsjdsjdsefggfyrgfygryuruwolpswoksij\n\t\tif(i>n) i=1;\n\t}\n\tcout<\nusing namespace std ;\nint main ()\n{\n\n int t,i,m,n;\n\n //for (cin >> t; t--;)\n // {\n cin >> m >> n;\n if (n==0)\n {\n cout << 1 << endl;\n }\n else if ((m-n)>n)\n {\n cout << n << endl;\n }\n else\n {\n cout << m-n << endl;\n }\n \n \n\n\n\n // }\n\n\n\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "69b8b4a070c4eb530247712e8ad429cd", "src_uid": "c05d0a9cabe04d8fb48c76d2ce033648", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#define fr(i, a, x) for(int(i) = a; i <= x; ++i)\n#define rfr(i, a, x) for(int(i) = a; i >= x; --i)\n#define all(a) a.begin(), a.end()\n#define Min(a, b) (a < b) ? a : b\n#define Max(a, b) (a > b) ? a : b\n#define pb push_back\n//#define MY_DEBUG 1\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#ifdef MY_DEBUG\n#include \nifstream cin(\"input.txt\");\nofstream cout(\"output.txt\");\n#else \n#include \n#endif\n\nconst double pi = acos(-1.0);\nconst double eps = 1e-9;\nconst int inf = 1e9;\nconst long long linf = 1e18;\nconst int prost = 51;\n\nint main()\n{\n\tint n, m;\n\tcin >> n >> m;\n\tvector s(m), p;\n\tset h;\n\tfor(int i = 0; i < m; ++i)\n\t\tcin >> s[i];\n\tfor(int i = 0; i < m; ++i)\n\t\tfor(int j = 0; j < s[i].length(); ++j)\n\t\t\th.insert(s[i].substr(0, j + 1));\n\tfor(set::iterator i = h.begin(); i != h.end(); ++i)\n\t\tp.push_back(* i);\n\tint t = p.size();\n\tvector< vector > per(t, vector(4, -1));\n\tvector< vector > dl(t, vector(4, 0));\n\tchar help[] = {'C', 'T', 'G', 'A'};\n\tfor(int i = 0; i < t; ++i)\n\t\tfor(int k = 0; k < 4; ++k)\n\t\t{\n\t\t\tstring st = p[i];\n\t\t\tst += help[k];\n\t\t\t//if (h.find(st) != h.end())\n\t\t\t{\n\t\t\t\tbool tr = true;\n\t\t\t\tfor(int u = 0; u < st.length() && tr; ++u)\n\t\t\t\t\tfor(int j = 0; j < t && tr; ++j)\n\t\t\t\t\t\tif (st.substr(u, st.length() - u) == p[j])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tper[i][k] = j;\n\t\t\t\t\t\t\ttr = false;\n\t\t\t\t\t\t}\n\t\t\t\tif (per[i][k] != -1)\n\t\t\t\t{\n\t\t\t\t\tstring f = p[ per[i][k] ] ;\n\t\t\t\t\tint len, sl = f.length(), mx = 0;\n\t\t\t\t\tfor(int j = 0; j < m; ++j)\n\t\t\t\t\t{\n\t\t\t\t\t\tlen = s[j].length();\n\t\t\t\t\t\tif (sl - len >= 0 && f.substr(sl - len, len) == s[j] && len > mx)\n\t\t\t\t\t\t\tmx = len;\n\t\t\t\t\t}\n\t\t\t\t\tdl[i][k] = mx;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tvector< vector< vector > > dp(n + 1);\n\tfor(int i = 0; i < n + 1; ++i)\n\t{\n\t\tdp[i].resize(i + 1);\n\t\tfor(int j = 0; j < i + 1; ++j)\n\t\t\tdp[i][j].resize(t);\n\t}\n\tfor(int i = 0; i < t; ++i)\n\t{\n\t\tif (p[i].length() == 1)\n\t\t{\n\t\t\tbool fi = false;\n\t\t\tfor(int j = 0; j < m; ++j)\n\t\t\t\tif (s[j] == p[i])\n\t\t\t\t\tfi = true;\n\t\t\tif (fi)\n\t\t\t\tdp[1][1][i] = 1;\n\t\t\telse\n\t\t\t\tdp[1][0][i] = 1;\n\t\t}\n\t}\n\tlong long mod = 1000000009, ans = 0;\n\tfor(int i = 1; i < n; ++i)\n\t\tfor(int j = max(i - 11, 0); j <= i; ++j)\n\t\t\tfor(int k = 0; k < t; ++k)\n\t\t\t\tfor(int u = 0; u < 4; ++u)\n\t\t\t\t\tif (per[k][u] != -1 && dp[i][j][k] > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (dl[k][u] + j > i)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tdp[i + 1][i + 1][ per[k][u] ] += dp[i][j][k];\n\t\t\t\t\t\t\tdp[i + 1][i + 1][ per[k][u] ] %= mod;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tdp[i + 1][j][ per[k][u] ] += dp[i][j][k];\n\t\t\t\t\t\t\tdp[i + 1][j][ per[k][u] ] %= mod;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\tfor(int i = 0; i < t; ++i)\n\t{\n\t\tans += dp[n][n][i];\n\t\tans %= mod;\n\t}\n\tcout << ans << '\\n';\n}", "lang_cluster": "C++", "tags": ["strings", "dp", "trees"], "code_uid": "51612a5c183ea454a03caf1ca84b81d6", "src_uid": "3f053c07deaac55c2c51df6147080340", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint n,k,i,j,sum,one,two;\nint main(){\n\t cin>>n>>k;\n\t int a[n+1];\n\t for(i=1;i<=n;i++){\n\t \tcin>>a[i];\n\t\t }\n\t\t for(i=1;i<=k;i++){\n\t\t \tone=0; two=0;\n\t\t \tfor(j=i;j<=n;j=j+k){\n\t\t \t\tif(a[j]==1) one++; else two++;\n\t\t\t }\n\t\t\t sum=sum+min(one,two);\n\t\t }\n\t\t cout<\n#include\n#include\n#include\n#define Mod 998244353\nusing namespace std;\nlong long fpow(long long a,long long b)\n{\n long long ans=1,t=a;\n while(b)\n {\n if(b&1)ans=ans*t%Mod;\n t=t*t%Mod;\n b>>=1;\n }\n return ans;\n}\nlong long inv(long long a)\n{\n return fpow(a,Mod-2);\n}\nvoid add(int& a,int b)\n{\n a+=b;\n if(a>=Mod)a-=Mod;\n}\nint dp[4010][4010][2];\nint fac[5010];\nint main(){\n int n,k,l;\n scanf(\"%d %d %d\",&n,&k,&l);\n dp[0][0][0]=1;\n for(int i=0;i<=n*2;i++)\n for(int j=0;j<=i;j++)\n for(int o=0;o<=1;o++)\n {\n add(dp[i+1][j+1][o],dp[i][j][o]);\n if(o==0&&j>=k)add(dp[i+1][j][1],dp[i][j][o]);\n if(j)add(dp[i+1][j-1][o],(long long)dp[i][j][o]*j%Mod);\n }\n fac[0]=1;\n for(int i=1;i<=n*2+1;i++)\n fac[i]=(long long)fac[i-1]*i%Mod;\n printf(\"%lld\\n\",(long long)dp[n*2+1][0][1]*fpow(2,n)%Mod*fac[n]%Mod*inv(fac[n*2+1])%Mod*l%Mod);\n return 0;\n}", "lang_cluster": "C++", "tags": ["probabilities", "math", "dp", "combinatorics"], "code_uid": "58163768c39bf3e47dfc93e8cc51761e", "src_uid": "c9e79e83928d5d034123ebc3b2f5e064", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main(){\n int i, j, state, arr[105][105];\n\n state = 0;\n\n for(i = 1; i <= 3; i++)\n for(j = 1; j <= 3; j++)\n cin >> arr[i][j];\n\n for(i = 1; i <= 3; i++){\n for(j = 1; j <= 3; j++){\n state = (1 + arr[i][j] + arr[i - 1][j] + arr[i + 1][j] + arr[i][j - 1] + arr[i][j + 1]) % 2;\n if(state) cout << \"1\";\n else cout << \"0\";\n }\n\n cout << endl;\n }\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "445c4b4dfd4331024ff4b2dca0e5bb76", "src_uid": "b045abf40c75bb66a80fd6148ecc5bd6", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\n#define ll long long\n#define ld long double\n#define pii pair\n#define pll pair\n#define pdd pair\n#define ff first\n#define ss second\n#define pb push_back\n#define mp make_pair\n#define INF ((~0) ^ (1 << 31))\n#define INFL ((~0) ^ ((ll)1 << 63))\n#define FOR(i,j,k) for(int i = j; i < k; i++)\n#define REP(i,j,k) for(int i = j; i >= k; i--)\n#define setmax(i) const int maxn = (int) i;\n#define setmod(i) const int MOD = (int) i;\n//srand (time(NULL));\n\nusing namespace std;\n\nsetmax(1e3+10);\nsetmod(1e9+7);\n\nll fact[maxn], rfact[maxn],n,m,k;\n\nll po(ll a, ll b)\n{\n if(!b) return 1;\n ll t = po(a,b/2);\n t = (t*t) % MOD;\n if(b & 1) t = (t*a)%MOD;\n return t;\n}\n\nll cmb(ll a, ll b)\n{\n if(b > a) return 0;\n ll t = fact[a] * rfact[b];\n t %= MOD;\n t = (t*rfact[a-b]) % MOD;\n return t;\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);cin.tie(0);\n\n fact[0] = 1;\n FOR(i,1,maxn) fact[i] = (fact[i-1]*i) %MOD;\n FOR(i,0,maxn) rfact[i] = po(fact[i],MOD-2);\n cin >> n >> m >> k;\n cout << (cmb(n-1,2*k) * cmb(m-1,2*k)) % MOD;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "b7ff95e03f942fd91c91f313305c9828", "src_uid": "309d2d46086d526d160292717dfef308", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\")\n#include \n#include \n \nusing namespace __gnu_pbds;\nusing namespace std;\n\ntypedef long long ll;\n#define M 1000000007\n#define N 55\n\nll chu(ll a,ll b){\n\tll res=1,p=M-2;\n\twhile(p){\n\t\tif(p&1){res=res*b%M;}b=b*b%M;p>>=1;\n\t}return a*res%M;\n}\n\nint n,x,y,i,j,k,ac,wa,n0,n1,sb[666],nn;\nll res,jb;\n\nstruct Mat\n{\n ll mat[55][55]={0};\n void print(){int i,j;for(i=0;i<=n;i++){for(j=0;j<=n;j++){printf(\"%lld \",mat[i][j]);}puts(\"\");}puts(\"\");}\n //void print(){int i,j;for(i=0;i<=n;i++){for(j=0;j<=n;j++){printf(\"%lld \",mat[i][j]*chu(1,jb)%M);}puts(\"\");}puts(\"\");}\n}a,ans;\n\nMat operator*(Mat A,Mat B){\n Mat temp;\n for(int i = 0 ; i <= n ; i++)\n {\n for(int j = 0 ; j <= n ;j++)\n {\n //temp.mat[i][j]=0;\n for(int k = 0 ;k <= n ;k++)\n {\n temp.mat[i][j] = (temp.mat[i][j]+A.mat[i][k]*B.mat[k][j])%M;\n }\n }\n }\n return temp;\n}\n\nMat operator^(Mat A, int k){\n Mat base;\n for(int i=0;i<=n;i++) base.mat[i][i]=1;\n\n while(k)\n {\n if(k & 1) base = base * A;\n A = A * A;\n k = k >> 1; \n }\n return base;\n}\n\n\nint main(){\n\tscanf(\"%d%d\",&n,&k);\n\tjb=chu(1,n*(n-1)/2);\n\tfor(i=1;i<=n;i++){\n\t\tscanf(\"%d\",&sb[i]);\n\t\tif(sb[i]){n1++;}\n\t\telse{n0++;}\n\t}\n\tfor(i=1;i<=n0;i++){\n\t\tif(sb[i]){wa++;}\n\t\telse{ac++;}\n\t}\n\tfor(i=n0+1;i<=n;i++){\n\t\tif(sb[i]){ac++;}\n\t\telse{wa++;}\n\t}\n\tac/=2;wa/=2;n/=2;\n\t\n\tans.mat[0][wa]=1;\n\ta.mat[0][1]=n0*n1*jb%M;\n\ta.mat[0][0]=(1-a.mat[0][1]+M)%M;\n\t\n\tfor(i=1;i<=n-1;i++){\n\t\tnn++;n0=max(0,n0-1);n1=max(0,n1-1);\n\t\ta.mat[i][i-1]=nn*nn*jb%M;\n\t\ta.mat[i][i+1]=n0*n1*jb%M;\n\t\ta.mat[i][i]=(1-a.mat[i][i+1]-a.mat[i][i-1]+M+M)%M;\n\t}\n\tnn++;n0=max(0,n0-1);n1=max(0,n1-1);\n\ta.mat[n][n-1]=nn*nn*jb%M;\n\ta.mat[n][n]=(1-a.mat[n][n-1]+M)%M;\n\t//a.print();\n\ta=a^k;\n\t//a.print();\n\tans=ans*a;\n\t//ans.print();\n\tprintf(\"%lld\",ans.mat[0][0]);\n}", "lang_cluster": "C++", "tags": ["probabilities", "matrices", "dp", "combinatorics"], "code_uid": "8e337d7432344ffd73c0e51d3c3a134c", "src_uid": "77f28d155a632ceaabd9f5a9d846461a", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\n#define ll long long\n#define ll2 pair\n#define ll3 pair\n#define mp make_pair\n#define pb push_back\n#define space << \" \" <<\n#define print_iterable_range(x, lwr, upr) cout << #x << \": \"; for(ll i=lwr; i=b; i--)\n#define all(vec) vec.begin(), vec.end()\n\nconst double PI = 3.1415926535897;\n\n\nostream& operator << (ostream& oout, ll2 p){\n\toout << \"(\" << p.first << \", \" << p.second << \")\";\n\treturn oout;\n}\n\nistream& operator >> (istream& iin, ll2 &p){\n\tiin >> p.first;\n\tiin >> p.second;\n\treturn iin;\n}\ntemplate \nistream& operator >> (istream & iin, vector &a){\n\tfor(T &i : a){\n\t\tcin >> i;\n\t}\n\treturn iin;\n}\n\ntemplate \nostream& operator << (ostream & oout, vector &a){\n\tfor(T &i : a){\n\t\tcout << i << \" \";\n\t}\n\treturn oout;\n}\n\ntemplate \nT min(vector a){\n\tll ans = a[0];\n\tfor(auto i : a){\n\t\tans = min(ans, i);\n\n\t}\n\treturn ans;\n}\n\ntemplate \nT max(vector a){\n\tll ans = a[0];\n\tfor(auto i : a){\n\t\tans = max(ans, i);\n\n\t}\n\treturn ans;\n}\n\ntemplate \nbool operator == (pair a, pair b){\n\treturn a.first == b.first and a.second == b.second;\n}\n\nconst ll MOD = 1000000007;\n\n\n//template\ninline ll add_mod(ll a, ll b, const ll mod = MOD){\n// if(a<0) a += mod;\n// if(b<0) b += mod;\n\treturn ((a>=0 ? a : a+mod)%mod + (b>=0 ? b : b+mod)%mod)%mod;\n}\n\ninline ll sub_mod(ll a, ll b, const ll mod = MOD){\n\tll result = ((a>=0 ? : a+mod)%mod - (b>=0 ? b : b+mod)%mod);\n\treturn (result >= 0 ? result : result + mod)%mod;\n}\n\ninline ll mult_mod(ll a, ll b, const ll mod = MOD){\n\treturn ((a>=0? a : a+mod)%mod * (b>=0 ? b : b+mod)%mod)%mod;\n}\n\nll fast_expo(ll x, ll y, const ll mod = MOD){\n\tll ans = 1;\n\twhile(y > 0){\n\t\tif(y%2 == 1) ans = mult_mod(x, ans, mod);\n\t\tx = mult_mod(x, x, mod);\n\t\ty /= 2;\n\t}\n\treturn ans;\n}\n\ninline ll div_mod(ll a, ll b, const ll mod = MOD){\n\treturn mult_mod(a, fast_expo(b, mod - 2, mod), mod);\n}\n\nll ncr(ll n, ll r){\n\tif(n == 0)\n\treturn 1;\n\t// cout << n space r << endl;\n\tif(r == 0 or r == n){\n\t\treturn 1;\n\t}\n\telse return ncr(n-1, r) + ncr(n-1, r - 1);\n}\n\nvoid solve(){\n\n\tstring orig, rec;\n\tcin >> orig >> rec;\n\tcout.precision(10);\n\tll dest = 0;\n\tll unrec = 0;\n\tll curr = 0;\n\n\tfor(auto & i : orig){\n\t\tdest = dest + (i == '+' ? 1 : -1);\n\t}\n\n\tfor(auto & i : rec){\n\t\tif(i == '+'){\n\t\t\tcurr ++;\n\t\t}\n\t\telse if(i == '-'){\n\t\t\tcurr--;\n\t\t}\n\t\telse unrec ++;\n\t}\n\n\tif(unrec < abs(dest - curr)){\n\t\tcout << (long double)0 << endl;\n\t\treturn ;\n\t}\n\n\tll left = abs(curr - dest);\n\n\tif((unrec - left) % 2 != 0){\n\t\tcout << (long double)0;\n\t\treturn;\n\t}\n\n\t// debug_var(dest)\n\t// debug_var(curr)\n\n\tll num_1 = (unrec - left)/2 + (dest >= curr ? left : 0);\n\n\t// debug_var(num_1)\n\t// debug_var(unrec)\n\n\t// cout << \"raju\\n\";\n\n\t// debug_var(ncr(2,1))\n\n\tlong double p = ((long double)(ncr(unrec, num_1)))/((long double)(pow(2LL, unrec)));\n\tcout << p;\n\n\treturn;\n}\n\n\nint main(){\n\tios_base::sync_with_stdio(NULL);\n\tcin.tie(0);cout.tie(0);\n\tll num_test_cases = 1;\n\t// cin >> num_test_cases;\n\twhile(num_test_cases--){\n\t\tsolve();\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "math", "dp", "combinatorics", "bitmasks", "probabilities"], "code_uid": "52c697f401632b76e3488b92ce075e53", "src_uid": "f7f68a15cfd33f641132fac265bc5299", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#define fast ios::sync_with_stdio(0); cin.tie(0), cout.tie(0)\n#define ll long long int\n#define ull unsigned ll\n#define mkp make_pair\n#define pb push_back\n#define pf push_front\n#define ppb pop_back()\n#define ppf pop_front()\n#define lb lower_bound\n#define ub upper_bound\n#define in insert\n#define sz size()\n#define mem(a,x) memset(a,x,sizeof(a))\n#define mt empty()\n#define py puts(\"YES\")\n#define pn puts(\"NO\")\n#define png puts(\"-1\")\n#define pyn(x) x ? py : pn\n#define all(x) x.begin(), x.end()\n#define ft first\n#define sd second\n#define prq priority_queue\n#define rev(x) reverse(all(x))\nusing namespace std;\n\nint main() {\n\tint n;cin>>n;\n cout<<((n>>1)+1)*((n+1)>>1);\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "44a5811b97882d3489c013520db4aa0b", "src_uid": "f8af5dfcf841a7f105ac4c144eb51319", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint check(string f)\n{\n int z= 0;\n if( (f[0]=='0' && f.size() > 1) || f.size()>6 && f!=\"1000000\" )\n return -10000000;\n z= atoi(f.c_str());\n return z;\n}\nint main()\n{\n string s ;\n int Max=-1 ;\n cin >> s;\n int n = s.size();\n for(int i = 1 ; i < n-1; i++)\n {\n for(int j = i+1 ; j < n; j++)\n {\n int s1 , s2 , s3;\n s1=check(s.substr(0,i));\n s2=check(s.substr(i,j-i));\n s3=check(s.substr(j,n-j));\n // cout << s1 << \" \" << s2 << \" \" << s3< Max)\n Max=s1+s2+s3;\n }\n }\n cout << Max;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "1e093b23b15f027c17405dc56a7d62d2", "src_uid": "bf4e72636bd1998ad3d034ad72e63097", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (n); ++i)\n#define FOR(i, j, k) for(int i = (j); i <= (k); ++i)\n#define pii pair\n#define fr first\n#define sc second\n\nlong long a, b, c, l;\nlong long ans;\n\nlong long mxSide(long long a, long long b, long long c, long long l) {\n long long sum = 0;\n FOR(la, 0, l) {\n long long x = min(a + la - b - c, l - la);\n if (x < 0) continue;\n sum += (x + 1) * (x + 2) / 2;\n }\n return sum;\n}\n\nint main() {\n\tcin >> a >> b >> c >> l;\n\tans = (l + 3) * (l + 2) * (l + 1) / 6;\n\tans -= mxSide(a, b, c, l);\n\tans -= mxSide(b, a, c, l);\n\tans -= mxSide(c, b, a, l);\n\n\tcout << ans << endl;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "combinatorics"], "code_uid": "8462a82558878f8ed9886805d91d4606", "src_uid": "185ff90a8b0ae0e2b75605f772589410", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n\nint n;\n\nusing namespace std;\nint main()\n{\n cin >> n;\n if(n==1) cout << 1;\n else\n {\n cout << n << \" \";\n for(int i=n/2; i>1; i--)\n if(n%i==0)\n {\n cout << i << \" \";\n n=i;\n }\n\n \n\n cout << 1;\n }\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "f3c9a568cff2b998db751876159ded9b", "src_uid": "2fc946bb72f56b6d86eabfaf60f9fa63", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define fr(a,b,c) for(int a = b, _ = c; a < _; a++)\nusing namespace std;\nint main(){\n\tstring address, url;\n\tint n, domain_l, domain_r;\n\tgetline(cin, address);\n\tn = address.length();\n\tif(address[2] == 't'){\n\t\turl = \"http://\";\n\t\tdomain_l = 4;\n\t}else{\n\t\turl = \"ftp://\";\n\t\tdomain_l = 3;\n\t}\n\tdomain_r = address.find(\"ru\", domain_l);\n\tif(domain_r-domain_l == 0){\n\t\tdomain_r = address.find(\"ru\", domain_r+1);\n\t}\n\turl += address.substr(domain_l, domain_r-domain_l) + \".ru\";\n\tif(domain_r + 2 < n){\n\t\turl += \"/\" + address.substr(domain_r + 2);\n\t}\n\tcout << url << endl;\n}\n\n// 1505232044281\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "0dc99478471177cf76f9f0db0771f232", "src_uid": "4c999b7854a8a08960b6501a90b3bba3", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nint la, lb, ra, rb, ta, tb;\n\nint cmmdc(int a, int b){\n int r = 0;\n while(b){\n r = a % b;\n a = b;\n b = r;\n }\n /// rezultatul se afla in a fiindca b preia r = 0;\n return a;\n}\n\nint main()\n{\n cin >> la >> ra >> ta;\n cin >> lb >> rb >> tb;\n\n int t = cmmdc(ta, tb);\n int dif;\n if(lb > la){\n swap(la, lb);\n swap(ra, rb);\n swap(ta, tb);\n }\n dif = la - lb;\n int rest = dif % t;\n\n int inters1 = min(rb - lb, rest + (ra - la)) - rest + 1;\n\n rest -= t;\n\n int inters2 = min(rest + (ra - la), rb - lb) + 1;\n\n cout << max(max(inters1, inters2), 0) << '\\n';\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "b8757ebdcf486f5df1f570cfcfddc784", "src_uid": "faa75751c05c3ff919ddd148c6784910", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint main()\n{\n\tint i,n,k,j,tmp=0,chck=0;\n\tcin>>n;\n\tint p[24]={31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31};\n\tint q[24]={31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31};\n\tvector a;\n\tfor(i=0;i>k;\n\t\ta.push_back(k);\n\t}\n\tfor(i=0;i<24;i++)\n\t{\n\t\ttmp=0;\n\t\t//cout<\n#include\n#include\n#include\nusing namespace std;\nconst int N=1e6+5,mod=1e9+7;\n#define int long long\nint n,k,y[N];\ninline int ksm(int x,int y){\n\tint res=1;\n\twhile(y){\n\t\tif(y&1)res=res*x%mod;\n\t\tx=x*x%mod; y>>=1;\n\t}\n\treturn res;\n}\nint pl[N],pr[N],fac[N];\nsigned main(){\n\tscanf(\"%lld%lld\",&n,&k);\n\tfor(int i=1;i<=k+2;i++)y[i]=(y[i-1]+ksm(i,k))%mod;\n\tpl[0]=pr[k+3]=fac[0]=1;\n\t\n for(int i=1;i<=k+2;i++)pl[i]=pl[i-1]*(n-i)%mod;\n for(int i=k+2;i>=1;i--)pr[i]=pr[i+1]*(n-i)%mod;\n\tfor(int i=1;i<=k+2;i++)fac[i]=fac[i-1]*i%mod;\n\tint ans=0;\n\tfor(int i=1;i<=k+2;i++){\n\t\tint op=y[i];\n\t\tint a=pl[i-1]*pr[i+1]%mod;\n\t\tint b=fac[i-1]*((k-i)&1?-1:1)*fac[k+2-i]%mod;\n\t\tans=(ans+op*a%mod*ksm(b,mod-2)%mod)%mod;\n\t}\n\tcout<<(ans+mod)%mod<\nusing namespace std;\nint\tmain()\t{\n int hh,mm;\n long a;\n scanf(\"%d:%d\",&hh,&mm);\n scanf(\"%d\",&a);\n mm+=a;\n if(mm>=60)\n hh=(hh+mm/60)%24;\n mm=mm%60;\n printf(\"%02d:%02d\",hh,mm);\n\treturn\t0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "1bce27ad7f2551de9b1e1c768076486e", "src_uid": "20c2d9da12d6b88f300977d74287a15d", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\nint f(int m)\n{\n\twhile(m)\n\t{\n\t\tif(m%10!=4&&m%10!=7) return 0;\n\t\tm/=10;\n\t}\n\treturn 1;\n}\nint a(int m)\n{\n\tfor(int i=m;i>=1;i--)\n\t\tif(m%i==0&&f(i)) return 1;\n\treturn 0;\n}\nint main()\n{\n\tint n;\n\tcin>>n;\n\tif(f(n)||a(n)) cout<<\"YES\"<\nchar a[1001],b[1001];\nusing namespace std;\nint main()\n{\n scanf(\"%s%s\",a,b);\n int lena=strlen(a),lenb=strlen(b);\n if(lena!=lenb)\n cout<<\"NO\";\n else\n {\n bool q=1;\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef long long LL;\n#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)\n#define FORD(x, b, e) for (int x = (b); x >= (e); --x)\n#define REP(x, n) for (int x = 0; x < (n); ++x)\n#define VAR(v, n) typeof(n) v = (n)\n#define ALL(c) (c).begin(), (c).end()\n#define SIZE(x) ((int) (x).size())\n#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)\n#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)\n#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)\n#define PB push_back\n#define ST first\n#define ND second\n#define MP make_pair\n#define skip continue\nconst int INF = 1000000001;\n\n#define GET(x)\t\t\t(scanf(\"%d\", &(x)) == 1)\n#define GET2(x, y)\t\t(scanf(\"%d %d\", &(x), &(y)) == 2)\n#define GET3(x, y, z)\t(scanf(\"%d %d %d\", &(x), &(y), &(z)) == 3)\n#define GETS(x)\t\t\t(scanf(\"%s\", (x)) == 1)\n#define DGET(x)\t\t\tint x; GET(x);\n\nint main()\n{\n\tint n, m, x1, y1, x2, y2;\n\tGET2(n, m);\n\tGET2(x1, y1);\n\tGET2(x2, y2);\n\t\n\tint dx = abs(x1-x2);\n\tint dy = abs(y1-y2);\n\t\n\tif (max(dx, dy) >= 5 || (dx == 4 && dy == 4) || (min(dx, dy) == 3 && max(dx,dy) == 4)) {\n\t\tputs(\"Second\");\n\t} else {\n\t\tputs(\"First\");\n\t}\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "combinatorics"], "code_uid": "e9cda39681158741b8aa94338c044765", "src_uid": "41f6f90b7307d2383495441114fa8ea2", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\ntypedef unsigned long long intreg;\n\n#define mod 1000000007\n\nintreg n,s,f[22],rez;\nintreg sum;\nint i;\nbool semn;\n\nvoid read_data();\nintreg combinari(intreg n,intreg k);\nintreg put(intreg x, intreg p);\nintreg invers[22];\nvoid pre();\n\nint main()\n{\n read_data();\n pre();\n rez=combinari(n+s-1,n-1);\n for(int p=(1<>=1,++i) if(aux&1) semn^=1,sum+=f[i]+1;\n if(s>=sum) if(semn)\n {\n rez=(rez-combinari(n-1+s-sum,n-1)+mod)%mod;\n }\n else rez=(rez+combinari(n-1+s-sum,n-1))%mod;\n }\n cout<n) return 0;\n n%=mod;\n intreg rasp=1;\n for(int i=0;i>=1)\n {\n if(p&1) rez=rez*x%mod;\n x=x*x%mod;\n }\n return rez;\n}\n\nvoid pre()\n{\n for(int i=1;i<=21;++i) invers[i]=put(i,mod-2);\n}\n\nvoid read_data()\n{\n cin>>n>>s;\n for(i=1; i<=n; ++i)\n {\n cin>>f[i];\n }\n}\n", "lang_cluster": "C++", "tags": ["combinatorics", "number theory", "bitmasks"], "code_uid": "a962df7c6e8620e74643cf72f066fffa", "src_uid": "8b883011eba9d15d284e54c7a85fcf74", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n#include \nusing namespace std;\nstring rev(string s){\n for(int i=0;i=0;i--){\n num=num+a1[i]-'0';\n if(cnt>=0){\n num+=b1[cnt]-'0';\n }\n sum+=(num%10+'0');\n num=num/10;\n cnt--;\n }\n return sum;\n}\n\nint main()\n{\n string a1,b1,a2=\"\",b2=\"\",sum1=\"\",sum2=\"\",sum3;\n cin>>a1>>b1;\n if(a1.size()\n\n#define t first\n#define g second\n#define end() exit(0)\n#define deb(x) cout << #x << \" = \" << x << endl;\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair song;\n\nconst int D = 1000 * 1000 * 1000 + 7, N = 50, M = 2500, G = 3;\n\ninline int add(int, int);\ninline int mul(int, int);\nint f(int);\ninline istream& operator >> (istream&, song&);\ninline int inc(int&, int);\ninline int multi(int&, int);\nvoid init();\nvoid inp();\nvoid solve();\nvoid upd();\nvoid calc();\n\nint n, T, dp1[N + 1][M + 1], dp23[N + 1][N + 1][M + 1], jav[N + 1][N + 1][N + 1][G + 1], mf[N + 1], num[G + 1], dur[G + 1], ans;\n\nint main() {\n\tinit();\n\tinp();\n\tsolve();\n\tcout << ans << endl;\n\treturn 0;\n}\ninline int inc(int& x, int y) {\n\treturn (((x += y) < D)? x: (x -= D));\n}\ninline int add(int x, int y) {\n\treturn (((x + y) < D)? x + y: ((x + y) - D));\n}\ninline int mul(int x, int y) {\n\treturn ((1ll * x * y) % D);\n}\nint f(int x) {\n\treturn (~mf[x]? mf[x]: (mf[x] = mul(x, f(x - 1))));\n}\ninline istream& operator >> (istream &in, song &x) {\n\tin >> x.t >> x.g;\n\treturn in;\n}\ninline int multi(int& x, int y) {\n\treturn (x = mul(x, y));\n}\nvoid init() {\n\tmemset(mf, -1, sizeof mf);\n\tmf[0] = 1;\n\tdp1[0][0] = dp23[0][0][0] = jav[0][0][0][0] = 1;\n}\nvoid inp() {\n\tcin >> n >> T;\n\tsong tmp;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> tmp;\n\t\tif (tmp.g == 1)\n\t\t\tfor (int i = num[1]; i >= 0; i--)\n\t\t\t\tfor (int j = dur[1]; j >= 0; j--)\n\t\t\t\t\tinc(dp1[i + 1][j + tmp.t], dp1[i][j]);\n\t\telse {\n\t\t\tfor (int i = num[2]; i >= 0; i--)\n\t\t\t\tfor (int j = num[3]; j >= 0; j--)\n\t\t\t\t\tfor (int k = dur[2] + dur[3]; k >= 0; k--)\n\t\t\t\t\t\t((tmp.g == 2)? inc(dp23[i + 1][j][k + tmp.t], dp23[i][j][k]): inc(dp23[i][j + 1][k + tmp.t], dp23[i][j][k]));\n\t\t}\n\t\tdur[tmp.g] += tmp.t;\n\t\tnum[tmp.g]++;\n\t}\n}\nvoid solve() {\n\tupd();\n\tcalc();\n}\nvoid upd() {\n\tint i[G + 1];\n\tfor (i[1] = 0; i[1] <= num[1]; i[1]++)\n\t\tfor (i[2] = 0; i[2] <= num[2]; i[2]++)\n\t\t\tfor (i[3] = 0; i[3] <= num[3]; i[3]++)\n\t\t\t\tfor (int prev = 0; prev <= 3; prev++) {\n\t\t\t\t\tint tmp = jav[i[1]][i[2]][i[3]][prev];\n\t\t\t\t\tif (tmp == 0)\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\tfor (int next = 1; next <= 3; next++)\n\t\t\t\t\t\tif (next != prev && i[next] + 1 <= num[next]) {\n\t\t\t\t\t\t\ti[next]++;\n\t\t\t\t\t\t\tinc(jav[i[1]][i[2]][i[3]][next], tmp);\n\t\t\t\t\t\t\ti[next]--;\n\t\t\t\t\t\t}\n\t\t\t\t}\n}\nvoid calc() {\n\tfor (int i = 0; i <= num[1]; i++)\n\t\tfor (int j = 0; j <= dur[1]; j++) {\n\t\t\tif (T < j)\n\t\t\t\tbreak;\n\t\t\tfor (int k = 0; k <= num[2]; k++)\n\t\t\t\tfor (int h = 0; h <= num[3]; h++) {\n\t\t\t\t\tint nat = mul(dp1[i][j], dp23[k][h][T - j]);\n\t\t\t\t\tmulti(nat, mul(f(i), mul(f(k), f(h))));\n\t\t\t\t\tfor (int next = 1; next <= 3; next++)\n\t\t\t\t\t\tinc(ans, mul(nat, jav[i][k][h][next]));\n\t\t\t\t}\n\t\t}\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics"], "code_uid": "ebab9311b84346ff13a34e79c87c48d4", "src_uid": "ed5f913afe829c65792b54233a256757", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\nint main() {\n\t// your code goes here\n\tint n,m,p,s;\n\tcin>>n>>m;\n\tqueueq;\n\tvectorvisited(100007);\n\tq.push(n);\n\tvisited[n]=1;\n\tint c=0,h=0;\n\tint maxi=max(n,m);\n\twhile(q.empty()==false)\n\t{\n\t s=q.size();\n\t while(s--)\n\t {\n\t p=q.front();\n\t q.pop();\n\t if(p==m)\n\t {\n\t h=1;\n\t break;\n\t }\n\t else\n\t {\n\t if(p-1<=2*maxi&&visited[p-1]==0)\n\t {\n\t q.push(p-1);\n\t visited[p-1]=1;\n\t }\n\t if(p*2<=2*maxi&&visited[p*2]==0)\n\t {\n\t q.push(p*2);\n\t visited[p*2]=1;\n\t }\n\t }\n\t }\n\t if(h==1)\n\t break;\n\t c++;\n\t }\n\t cout<\n #define ll long long\n #define pb push_back\n #define r_0 return 0\n #define fr first\n #define se second\n #define TIME ios_base::sync_with_stdio(0);\n using namespace std;\n const ll N= 1e5+1 ;\n ll n , m , cnt , ans , x , sum ,w ;\n vector< ll >v , v1;\n set< ll >se ;\n vector< pair >p ;\n string s , s1 ;\n map< char , ll >mp ;\n char h , y;\n int main()\n {\n TIME\n mp[ 'a' ] = 1 ;\n mp[ 'e' ] = 1 ;\n mp[ 'u' ] = 1;\n mp[ 'o' ] = 1 ;\n mp[ 'n' ] = 1 ;\n mp[ 'i' ] = 1 ;\n cin >> s;\n if(s.size() == 1 and mp[s[0]] == 0){\n cout << \"NO\" ;r_0 ;}\n\n for( ll i= 0 ;i < s.size( ); i ++ )\n {\n if( mp[s[i]] == 0 and mp[s[i+1]] == 1 and s[i+1] != 'n' )\n continue ;\n if( mp[s[i]] == 0 and mp[s[i+1]] == 0 or mp[s[i]] == 0 and s[i+1] == 'n' )\n {\n cout << \"NO\";\n r_0 ;\n }\n\n }\n cout << \"YES\";\n }\n", "lang_cluster": "C++", "tags": ["strings", "implementation"], "code_uid": "1b39b8e9f29590b151b467e06044d140", "src_uid": "a83144ba7d4906b7692456f27b0ef7d4", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n#define MAX 1000005\nconst ll MOD = 1000 * 1000 * 1000 + 9;\nll n,m,k;\nll fexp(ll a, ll p)\n{\n ll res=1;\n while(p){\n if(p&1){\n res=(res*a)%MOD;\n p--;\n }\n a=(a*a)%MOD;\n p/=2;\n }\n return res;\n}\nbool f(ll x)\n{\n// ll n1 = n-x*k, m1=m-x*k;\n// ll q=n1/k;\n return m-(n%k)-(n-n%k)/k*(k-1)<=x;\n}\nll bs(ll l, ll r)\n{\n// cout<<\"l: \"<>n>>m>>k;\n ll p = bs(0,n);\n cout<<((fexp(2,p+1)+MOD-2)%MOD*k%MOD+m-p*k)%MOD;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "binary search", "number theory", "greedy", "matrices"], "code_uid": "8cb93aae5241358c20b326398a946b72", "src_uid": "9cc1aecd70ed54400821c290e2c8018e", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\nint dp[10000005];\nvoid pre()\n{\n memset(dp , 0 ,sizeof dp);\n dp[5]=1;\n for(int i=6;i<10000001;++i){\n dp[i]=dp[i-1];\n if(i%5==0){\n int j=i;\n while(j%5==0){\n j/=5;\n dp[i]++;\n }\n }\n }\n\n}\nint main()\n{\nint n;\npre();\nwhile(cin>>n){\n vector< int > v;\n for(int i=0;i<10000001;++i){\n if(dp[i]==n)v.push_back(i);\n }\n /*for(int i=26140;i<26160;++i){\n cout<\n#include \n#include \n#include \n#include \nusing namespace std;\n\nint main() {\n char a,b;\n int c=0;\n int x;\n cin>>x>>a;\n for (int i=1;i>b;\n if( b==a ) c++;\n else a=b;\n \n }\n cout <\n#include \n#include \n\nusing namespace std;\n\nconst int oo = 0x3fffffff;\nconst int MAXN = 10;\nconst int MAXM = 100;\n\nstruct SInformation\n{\n int a, b, c;\n};\n\nstruct SEarn\n{\n int price;\n int limit;\n};\n\nSInformation info[MAXN][MAXM];\nSEarn earn[MAXM];\nint n, m, cap;\n\ninline bool cmp(const SEarn &a, const SEarn &b)\n{\n return a.price > b.price;\n}\n\nvoid init(void)\n{\n cin >> n >> m >> cap;\n for (int i = 0; i < n; i++)\n {\n string s;\n cin >> s;\n for (int j = 0; j < m; j++)\n {\n cin >> info[i][j].a >> info[i][j].b >> info[i][j].c;\n }\n }\n}\n\nvoid solve(void)\n{\n int ans = 0;\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n for (int k = 0; k < m; k++)\n {\n earn[k].price = max(info[j][k].b - info[i][k].a, 0);\n earn[k].limit = info[i][k].c;\n }\n sort(earn, earn + m, cmp);\n int sum = 0;\n int t = cap;\n for (int k = 0; k < m; k++)\n {\n if (t <= earn[k].limit)\n {\n sum += t * earn[k].price;\n break;\n }\n sum += earn[k].limit * earn[k].price;\n t -= earn[k].limit;\n }\n ans = max(sum, ans);\n }\n }\n cout << ans << endl;\n}\n\nint main(void)\n{\n ios::sync_with_stdio(false);\n init();\n solve();\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "ad616ed406325f26db3d0d256eb9956a", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\n#include\n#include\nusing namespace __gnu_cxx;\n\n// skrocone typy\ntypedef long long ll;\n\ntypedef unsigned int uint;\ntypedef unsigned short ushort;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\n\n// int pairs\ntypedef pair pii;typedef pair puiui;typedef pair piui;typedef pair puii;\n// long long pairs\ntypedef pair< ll,ll> pllll;typedef pair pullull;typedef pair pullll;typedef pair pllull;\n// char pairs\ntypedef pair pcc;typedef pair pcuc;typedef pair pucc;typedef pair pucuc;\n// long long x int pairs\ntypedef pair pill;typedef pair plli;typedef pair puill;typedef pair pllui;typedef pair pulli;typedef pair piull;typedef pair puiull; typedef pair pullui;\n// long long x char pairs\ntypedef pair pcll;typedef pair pllc;typedef pair pucll;typedef pair plluc;typedef pair pullc;typedef pair pcull;typedef pair pucull; typedef pair pulluc;\n// int x char pairs\ntypedef pair pci;typedef pair pic;typedef pair puci;typedef pair piuc;typedef pair puic;typedef pair pcui;typedef pair pucui; typedef pair puiuc;\n\n\n\n#define mp make_pair\n#define fi first\n#define se second\n\n#define pb push_back\n#define pf push_front\n#define ins insert\n#define era erase\n\n#define it iterator\n\n#define type(x) __typeof(x)\n\n#define foreach(itr,cont) for(__typeof(cont.begin()) itr = cont.begin(); itr != cont.end(); ++itr)\n#define rep(i,n) for(int i=0; i<(n); ++i)\n#define per(i,n) for(int i=(n)-1; i>=0; --i)\n#define fo(i,a,b) for(int i=(a); i<(b); ++i)\n#define of(i,a,b) for(int i=(b)-1; i>=(a); --i)\n\n#define sz(c) (int)(c).size()\n\nstruct llhash {size_t operator()(ll x)const {return (int)x ^ (int)(x>>32);}};\nstruct piihash{size_t operator()(pii x)const {return x.fi^x.se;}};\n\nint nplanety;\nint ntowary;\nint ladownosc;\nint buy[19][109];\nint sel[19][109];\nint num[19][109];\n\nint comp4pla(int from, int to)\n{\n vector pro;\n rep(i,ntowary)pro.pb(mp(sel[to][i]-buy[from][i],i));\n sort(pro.begin(),pro.end());\n int pos = sz(pro)-1;\n int wynik = 0;\n int moge = ladownosc;\n while(pos>=0 && pro[pos].fi>0 && moge>0)\n {\n int ile = min(moge,num[from][pro[pos].se]);\n wynik+=pro[pos].fi*ile;\n moge -= ile;\n --pos;\n }\n return wynik;\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cout.precision(40);\n cout.setf(ios::fixed, ios::floatfield);\n \n cin>>nplanety>>ntowary>>ladownosc;\n\n rep(i,nplanety)\n {\n string s;\n cin>>s;\n rep(j,ntowary)\n {\n cin>>buy[i][j]>>sel[i][j]>>num[i][j];\n }\n }\n\n int profit = 0;\n rep(i,nplanety)rep(j,nplanety)profit=max(profit,comp4pla(i,j));\n\n cout<\n#include \n#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n int n, m, k;\n cin >> n >> m >> k;\n int a[100][200], b[100][200], c[100][200];\n string s;\n for (int i=0; i> s;\n for (int j=0; j> a[i][j] >> b[i][j] >> c[i][j];\n }\n int ans = 0;\n for (int i=0; i > q;\n for (int l=0; l 0)\n {\n int x = q.top().first, y = q.top().second;\n int d = min(t, y);\n q.pop();\n if (x <= 0)\n continue;\n if (y-d != 0)\n q.push(make_pair(x, y-d));\n res += x*d;\n t -= d;\n }\n ans = max(ans, res);\n }\n cout << ans;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "3b7ffe52e548dae3853da56d1fb6c526", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\nint main(){\n int n,m,k;\n cin>>n>>m>>k;\n int a[n][m],b[n][m],c[n][m];\n int profit=0;\n string s[n],s1,s2;\n int i,j;\n for(i=0;i>s[i];\n for(int x=0;x>a[i][x]>>b[i][x]>>c[i][x];\n }\n }\n for(i=0;i p[m];\n for(int x=0;x0&&ii>=0&&p[ii].first>0){\n prft+=(p[ii].first)*(min(temp,p[ii].second));\n temp=max(0,temp-p[ii].second);\n ii--;\n }\n if(prft>profit){\n profit=prft;\n }\n }\n }\n }\n cout<\n#include \n\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < n; ++i)\n\nint a[11][101];\nint b[11][101];\nint c[11][101];\n\nint ta[11][101];\nint tb[11][101];\nint tc[11][101];\n\nint main() {\n\tint n, m, k;\n\tcin >> n >> m >> k;\n\tstring s;\n\tREP(i, n) {\n\t\tcin >> s;\n\t\tREP(j, m) {\n\t\t\tcin >> ta[i][j] >> tb[i][j] >> tc[i][j];\n\t\t}\n\t}\n\tint ans = 0;\n\tREP(i, n) REP(j, n) if ( i != j ) {\n\t\tint res = 0;\n\t\tREP(ii, n) REP(jj, m) {\n\t\t\ta[ii][jj] = ta[ii][jj];\n\t\t\tb[ii][jj] = tb[ii][jj];\n\t\t\tc[ii][jj] = tc[ii][jj];\n\t\t}\n\t\tREP(ii, k) {\n\t\t\tint maxn = -1;\n\t\t\tint type = -1;\n\t\t\tREP(t, m) if ( c[i][t] > 0 && b[j][t] - a[i][t] > maxn ) {\n\t\t\t\tmaxn = b[j][t] - a[i][t];\n\t\t\t\ttype = t;\n\t\t\t}\n\t\t\tif ( maxn != -1 ) {\n\t\t\t\tres += b[j][type] - a[i][type];\n\t\t\t\t--c[i][type];\n\t\t\t}\n\t\t}\n\t\tans = max(ans, res);\n\t}\n\tcout << ans << endl;\n\n\t//system(\"pause\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "b9dbec248ee3a7f165f200cc69bd8845", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define FOR(i, a, b) for(int i = a; i <= b; ++i)\n#define FRD(i, a, b) for(int i = a; i >= b; --i)\n#define FR(i, a) for(int i = 0; i < a; ++i)\n#define REP(i, a) for(int i = 1; i <= a; ++i)\n#define FRSZ(i, x) for(int i = 0; i < (int)x.size(); ++i)\n#define bug(x, y) cout << (x) << \" \" << (y) << endl\n#define bg(a, m, n) FOR(i, m, n) cout << (a[i]) << \" \"; cout << endl;\n#define MP \t make_pair\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long Qword;\ntypedef pair TPair;\n#define x first\n#define y second\n\nconst int MAXINT = 0x7fffffff;\n\nint n, m, k, a[15][105][5];\n\nvector res;\nint ans;\n\nint main() {\n #ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n scanf(\"%d %d %d\\n\", &n, &m, &k);\n FOR(i, 1, n) {\n string tt; getline(cin, tt);\n FOR(j, 1, m)\n FOR(tt, 1, 3) scanf(\"%d\", &a[i][j][tt]);\n scanf(\"\\n\");\n }\n FOR(i, 1, n)\n FOR(j, 1, n)\n if (i != j) {\n\n int pf = 0, cap = k;\n vector res;\n FOR(t, 1, m)\n if (a[i][t][1] < a[j][t][2] && a[i][t][3] > 0)\n res.push_back(TPair(a[j][t][2] - a[i][t][1], a[i][t][3]));\n sort(res.begin(), res.end());\n FRD(t, res.size() -1, 0) {\n int num = min(cap, res[t].y);\n pf += num * res[t].x;\n cap -= num;\n if (cap == 0) { ans = max(ans, pf); break; }\n }\n ans = max(ans, pf);\n }\n cout << ans << endl;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "976e1c40e720a86be6cc3a9a99707e37", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint table[16][128][4];\nint profit[128][16][16];\n\nint main()\n{\n int n, m, k;\n cin >> n >> m >> k;\n\n for ( int i=0 ; i> name;\n for ( int j=0 ; j> table[i][j][0] >> table[i][j][1] >> table[i][j][2];\n }\n }\n\n for ( int i=0 ; i 0 )\n {\n int b = min(buy, table[i][a[ii]][2]);\n buy -= b;\n total += b * profit[a[ii]][i][j];\n }\n }\n res = max(res, total);\n }\n }\n }\n \n cout << res << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "50961f1a2ca44f8c1ae140986a2d38c5", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef unsigned long long ull;\ntypedef pair pii;\n#define mp make_pair\n\nstruct Planet\n{\n vector b, s, c;\n\n void add(int bb, int ss, int cc)\n {\n b.push_back(bb);\n s.push_back(ss);\n c.push_back(cc);\n }\n};\n\nint n, m, k;\nvector P;\n\nstruct good\n{\n int s;\n int b;\n int c;\n\n good(int b, int s, int c) : b(b), c(c), s(s) {}\n\n bool operator <(good g)\n {\n int p1 = s - b,\n p2 = g.s - g.b;\n if (p1 == p2)\n return c > g.c;\n return p1 > p2;\n }\n};\n\n\nint solve(int i, int j)\n{\n vector g;\n for (int q = 0; q < m; ++q)\n {\n if (P[i].c[q] == 0)\n continue;\n g.push_back(good(P[i].b[q], P[j].s[q], P[i].c[q]));\n }\n\n sort(g.begin(), g.end());\n\n int left(k), need(0), profit(0);\n for (int q=0; q> n >> m >> k;\n for (int i=0; i> s;\n P.push_back(Planet());\n for (int j=0; j> b >> s >> c;\n P.back().add(b, s, c);\n }\n }\n\n int result = 0, r;\n for (int i=0; i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint a[11][102];\nint b[11][102];\nint c[11][102];\nint d[102],cnt[102];\n\nint dp[101];\n\nint main()\n{\n\tint n,m,k;\n\tchar name[20],b2;\n\tscanf(\"%d%d%d\",&n,&m,&k);\n\tint res=-12;\n\tint maxi,id;\n\n\tint a1,b1,c1;\n\tfor(int i=0;i0 && maxires)\n\t\t\t\t\t{\n\t\t\t\t\t\tres=dp[h];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}//if(i!=j)\n\t\t}\n\t}\n\n\n\tcout<\nusing namespace std;\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define pll pair\n\ntypedef struct trip{\n ll a,b,c;\n}trip;\n\nbool comp(pll a,pll b){\n return a.first>b.first;\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(0);cout.tie(0);\n ll n,m,k;\n cin>>n>>m>>k;\n // each planet has name and a vector of triplets\n // a=buy,b=sell,c=no.of items\n string sl;\n ll a1,b1,c1;\n vector> planets;\n for(ll i=0;i temp(m);\n cin>>sl;\n for(ll j=0;j>temp[j].a>>temp[j].b>>temp[j].c;\n }\n planets.pb(temp);\n }\n ll maxprof = 0;\n // ll i=0,j=1;\n for(ll i=0;i tv;\n if(i!=j){\n for(ll it = 0;it0 && it0){\n ll q=min(tv[it].second,k1);\n prof += q*tv[it].first;\n k1 -= q;\n it++;\n }\n // cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nconst int nmax = 100111;\n\nint n , m , k , buy[200][200] , sell[200][200] , val[200][200] , maxval = 0;\nstring s;\n\nint Try(int from , int to)\n{\n int used = 0;\n int pr = 0;\n int lf[200];\n for (int i = 1 ; i <= m ; i ++ )\n lf[i] = val[from][i];\n while ( used != k )\n {\n int best = -1;\n for (int i = 1 ; i <= m ; i ++ )\n if ( lf[i] != 0 && ((sell[to][best] - buy[from][best]) < (sell[to][i] - buy[from][i]) || best == -1) && (sell[to][i] - buy[from][i]) > 0 )\n best = i;\n if ( best == -1 )\n break;\n int get = min(lf[best] , k - used);\n used += get;\n lf[best] -= get;\n pr += get * (sell[to][best] - buy[from][best]);\n }\n return pr;\n}\n\nint main()\n{\n//(\"input.txt\",\"r\",stdin);\n//(\"output.txt\",\"w\",stdout);\nscanf(\"%d%d%d\",&n,&m,&k);\nfor (int i = 1 ; i <= n ; i ++ )\n{\n cin >> s;\n for (int j = 1 ; j <= m ; j ++ )\n scanf(\"%d%d%d\",&buy[i][j],&sell[i][j],&val[i][j]);\n}\nfor (int i = 1 ; i <= n ; i ++ )\n for (int j = 1 ; j <= n ; j ++ )\n if ( i != j )\n maxval = max(maxval , Try(i , j) );\ncout << maxval << endl;\nreturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "d809d537c400e2aa35f0904f6f6404cd", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nstruct t{\n int a, b, c;\n};\n\nstruct delt{\n int delta, kol;\n};\n\nbool cmp( delt a, delt b )\n{\n return a.delta < b.delta ;\n}\n\nmain(){\n int n, m, k, i, j, l, ans = 0;\n cin>>n>>m>>k;\n string x;\n \n vector < vector < t > > tov( n, vector < t > (m) );\n \n vector < delt > Delta( m );\n \n for( i=0; i>x;\n for( j=0; j>tov[i][j].a>>tov[i][j].b>>tov[i][j].c ;\n }\n \n for( i=0; i0 ; l++ )\n {\n if( Delta[l].delta > 0 )\n break;\n \n int MAX_NUM_CUR = min( Delta[l].kol, CUR_K );\n \n CUR_ANS += MAX_NUM_CUR * (-Delta[l].delta);\n \n CUR_K -= MAX_NUM_CUR;\n \n }\n \n if( CUR_ANS > ans )\n ans = CUR_ANS;\n } \n \n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define FOR(i,a,b)\t\tfor(int i=(a),_b=(b);i<(_b);++i)\n#define FORD(i,a,b)\t\tfor(int i=(a),_b=(b);i>(_b);--i)\n#define pb\t\t\tpush_back\n#define mp\t\t\tmake_pair\n#define\tall(c)\t\t\t(c).begin(),(c).end()\n#define\ttr(c,i)\tfor(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define\tpresent(c,x)\t\t((c).find(x) != (c).end())\n#define\tcpresent(c,x)\t\t(find(all(c),x) != (c).end())\n\ntypedef long long\t\t\tll;\ntypedef unsigned long long\tull;\ntypedef unsigned char\t \tbyte;\ntypedef vector\t\t\tvi;\ntypedef pair\t\tpii;\ntypedef pair\t\tpll;\ntypedef vector\t\t\tvpii;\n\nconst int N = 25;\n\ntypedef struct {\n\tint a,b,c;\n} planet;\n\nint main(int argc, char *argv[])\n{\n#ifndef ONLINE_JUDGE\n\tfreopen(argv[1],\"r\",stdin);\n#endif\n#ifndef ONLINE_JUDGE\n\tifstream cin(argv[1]);\n#endif\n#if 0\n\tofstream cout(argv[2]);\n#endif\n\tint n,m,K;\n\tcin >> n >> m >> K;\n\tplanet p[n][m];\n\tstring s;\n\tFOR(i,0,n) {\n\t\tcin >> s;\n\t\tFOR(j,0,m) cin >> p[i][j].a >> p[i][j].b >> p[i][j].c; \n\t}\n\tint best = 0;\n\tFOR(i,0,n) FOR(j,0,n) {\n\t\tif (j == i) continue;\n\t\tpii tmp[m];\n\t\tFOR(k,0,m) {\n\t\t\ttmp[k].first = p[j][k].b-p[i][k].a;\n\t\t\ttmp[k].second = k;\n\t\t}\n\t\tsort(tmp,tmp+m);\n\t\tint s = 0;\n\t\tint profit = 0;\n\t\tFORD(k,m-1,-1) {\n\t\t\tif (s >= K) break;\n\t\t\tif (tmp[k].first <= 0) break;\n\t\t\tint l = tmp[k].second;\n\t\t\tint buy = min(K-s,p[i][l].c);\n\t\t\tprofit += buy*tmp[k].first;\n\t\t\ts += buy;\n\t\t}\n\t\tbest = max(best,profit);\n\t}\n\tcout << best << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "e8e9cea6103e1dc9d820477018722516", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \nusing namespace std;\nstruct ban\n{\n\tint a,b,c;\n};\nstruct ban1\n{\n\tint x,d;\n};\nbool operator<(const ban1& a,const ban1& b)\n{\n\treturn a.d>b.d;\n}\n\nint n,m,k;\nstring s;\nban a[12][102];\nban1 b[102];\nint ans;\nint main()\n{\n\tint i,j,ii,jj;\n\tcin>>n>>m>>k;\n\tint ck=k;\n\tfor(i=0;i>s;\n\t\tfor(j=0;j>a[i][j].a>>a[i][j].b>>a[i][j].c;\n\t\t}\n\t}\n\tfor(i=0;i=inchqan)\n\t\t\t\t{\n\t\t\t\t\tk-=inchqan;\n\t\t\t\t\tyans+=inchqan*b[ii].d;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tinchqan=k;\n\t\t\t\t\tyans+=inchqan*b[ii].d;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans=max(ans,yans);\n\t\t}\n\t}\n\tcout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n\nstruct profit\n{\n int p;\n int num;\n}pro[105];\n\nbool cmp(profit a,profit b)\n{\n return (a.p>b.p);\n}\n\nint main()\n{\n int n,m,k,buy[15][105],sell[15][105],num[15][105];\n while(cin>>n>>m>>k)\n {\n for (int i=0;i>pn;\n for (int j=0;j>buy[i][j]>>sell[i][j]>>num[i][j];\n }\n int ans(0);\n for (int i=0;ians) ans=sum;\n// cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long LL;\ntypedef map MSI;\ntypedef vector VI;\n\n#define PB(x) push_back(x)\n#define sqr(x) ((x)*(x))\n#define F first\n#define S second\n#define SZ(t) ((int)t.size())\n#define len(t) ((int)t.length())\n#define base 10\n#define fname \"\"\n#define sz 200\n#define EPS (1e-8)\n#define INF ((int)1e9 + 9)\n#define write(xx) printf(\"%d\" , xx);\n#define readln(xx) getline(cin , xx)\n#define read(xx) scanf(\"%d\" , &xx)\n#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)\n\nconst double PI = acos(-1.0);\n\nint n , m , k , a[sz][sz] , b[sz][sz] , c[sz][sz] , o , ans , kk;\nstring s;\npair d[sz];\n\nint main(){\n\t//freopen(fname\"in\", \"r\", stdin);\n\t//freopen(fname\"out\", \"w\", stdout);\n\n\tcin >> n >> m >> k;\n\n\tfor (1 , n , i)\n\t{\n\t\tcin >> s;\n\t\tfor (1 , m , j)\n\t\t\tcin >> a[i][j] >> b[i][j] >> c[i][j];\n\t}\n\n\tfor (1 , n , i)\n\t\tfor (1 , n , j)\n\t\tif (i != j)\n\t\t{\n\t\t\tfor (1 , m , t)\t\n\t\t\t{\n\t\t\t\td[t].F = -(b[j][t] - a[i][t]);\n\t\t\t\td[t].S = c[i][t];\n\t\t\t}\n\t\t\tsort(d + 1 , d + 1 + m); \t\t\n\t\t\tkk = k;\n\t\t\to = 0;\n\t\t\tfor (1 , m , t)\n\t\t\t{\n\t\t\t\td[t].F = -d[t].F;\n\t\t\t\tif (d[t].F <= 0) break;\n\t\t\t\twhile (kk > 0 && d[t].S > 0)\n\t\t\t\t{\n\t\t\t\t\to += d[t].F;\n\t\t\t\t\tkk--;\n\t\t\t\t\td[t].S--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = max(o , ans);\n\t\t}\n\tcout << ans;\n\t\n \treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "24876c12ffb00c4efcb0c7d63c5a67ef", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\n\nstruct stock\n{\n int sell,buy,q;\n};\nstruct item\n{\n int prof,q;\n};\n\nstock planets[11][101];\nint n,m,k;\n\nbool SortEm(item a,item b)\n{\n return (a.prof>b.prof);\n}\n\nint Profit(int buyplanet,int sellplanet)\n{\n item objects[101];\n int i;\n int totalprofit=0;\n int usedspace=0;\n int webuy;\n \n for (i=1;i<=m;i++)\n {\n objects[i].q=planets[buyplanet][i].q;\n objects[i].prof=planets[sellplanet][i].sell-planets[buyplanet][i].buy;\n }\n sort(objects+1,objects+1+m,SortEm);\n \n for (i=1;i<=m;i++)\n {\n if (objects[i].prof<=0)\n break;\n \n if (usedspace+objects[i].q>n>>m>>k;\n \n for (i=1;i<=n;i++)\n {\n cin>>useless;\n for (j=1;j<=m;j++)\n {\n cin>>planets[i][j].buy;\n cin>>planets[i][j].sell;\n cin>>planets[i][j].q;\n }\n }\n for (i=1;i<=n;i++)\n {\n for (j=1;j<=n;j++)\n {\n if (i==j)\n continue;\n \n currprofit=Profit(i,j);\n if ( currprofit>maxprofit )\n maxprofit=currprofit;\n \n }\n }\n cout<\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint sell[12][102];\nint buy[12][102];\nint rem[12][102];\n\n\nint n,m,cap,prf;\n\nstruct ans{\n\tint pl,jens;\n};\n//\n//int findMax(){\n//\tint max=0;\n//\tint maxInd;\n//\tfor(int i=0;imax){\n//\t\t\t\tmax=sell[i][be]-buy[i][bestInd];\n//\t\t\t\tmaxInd=i;\n//\t\t\t}\n//\t\t}\n//\t}\n//\tif(max>0){\n//\t\treturn maxInd;\n//\t}\n//\telse\n//\t\treturn -1;\n//}\n\n\nint calc(int from,int to){\n\tint rcap=cap;\n\tint ans=0;\n\tbool used[103]={false};\n\twhile(rcap>0){\n\t\tint max=0;\n\t\tint maxInd;\n\t\tfor(int i=0;imax){\n\t\t\t\tmax=sell[to][i]-buy[from][i];\n\t\t\t\tmaxInd=i;\n\t\t\t}\n\t\t}\n\t\tif(max==0)\n\t\t\treturn ans;\n\t\tused[maxInd]=true;\n\t\tint val= rem[from][maxInd]>rcap ? rcap : rem[from][maxInd];\n\t\trcap-=val;\n\t\tans+=val*(sell[to][maxInd]-buy[from][maxInd]);\n\n\t}\n\treturn ans;\n}\n\n\nint main(){\n\t//freopen(\"inf.txt\",\"r\",stdin);\n\n\n\tcin>>n>>m>>cap;\n\n\tstring inp;\n\tint sl;\n\n\tfor(int i=0;i>inp;\n\t\tfor(int j=0;jprf)\n\t\t\t\t prf=res;\n\n\t\t}\n\t\tcout<0){\n\t\tint ind=findMax();\n\t\tif(ind==-1)\n\t\t\tbreak;\n\t\tint min=4000,minInd;\n\t\tfor(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define pi (2*acos(0))\n#define ll long long int\n\nusing namespace std;\n\nint c[15][105],s[15][105],cap[15][105],n,m,k;\nvector > vec;\n\nint calc(int x,int y)\n{\n int i,j,r=0,f,sc,q=k;\n\n vec.clear();\n\n for(i=0;i=q)\n {\n r+=q*f;\n q=0;\n }\n\n else\n {\n r+=sc*f;\n q-=sc;\n }\n }\n\n return r;\n}\n\n\n\nint main()\n{\n int i,j,p,r=0;\n char str[15];\n\n scanf(\"%d %d %d\",&n,&m,&k);\n\n for(i=0;i\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair pii;\n\nint const N = 10 + 5, M = 100 + 20;\nint n, m, k, a[N][M], b[N][M], c[N][M], ans;\nstring s;\n\nint main() {\n ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n cin >> n >> m >> k;\n for (int i = 0; i < n; i ++){\n cin >> s;\n for (int j = 0; j < m; j ++) cin >> a[i][j] >> b[i][j] >> c[i][j];\n }\n\n for (int p1 = 0; p1 < n; p1 ++){\n for (int p2 = 0; p2 < n; p2 ++){\n vector v;\n\n for (int i = 0; i < m; i ++) v.push_back({max(0, b[p2][i] - a[p1][i]), c[p1][i]});\n\n sort(v.begin(), v.end());\n int rem = k, pr = 0;\n for (int i = v.size() - 1; i >= 0; i --){\n\tif (rem > v[i].second) pr += v[i].first * v[i].second, rem -= v[i].second;\n\telse pr += v[i].first * rem, rem = 0;\n }\n\n ans = max(ans, pr);\n }\n }\n\n return cout << ans << endl, 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "f6ef7744d81067d08ece162643b8a8d1", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nstruct sell\n{\n int profit,stock; //盈利,存量\n};\nsell pro[110];\nint main() \n{ \n string name;\n int n,m,k,kk,i,j,l,t,p,s,num,max_sum;\n int sum;\n int a[11][101],b[11][101],c[11][101];\n while(cin>>n>>m>>kk)\n {\n for(i=0;i>name;\n for(j=0;j>a[i][j]>>b[i][j]>>c[i][j];\n }\n }\n max_sum=0;\n for(i=0;i0&&s>0)\n {\n pro[num].profit=p;\n pro[num].stock=s;\n num++;\n }\n }\n sum=0;\n k=kk;\n while(k>0)\n {\n int max=0,tag=-1;\n for(l=0;lmax)\n {\n max=pro[l].profit;\n tag=l;\n }\n }\n if(tag==-1)\n break;\n pro[tag].profit=0;\n s=pro[tag].stock;\n if(k>=s)\n {\n sum=sum+max*s;\n k=k-s; \n }\n else\n {\n sum=sum+max*k;\n k=0;\n }\n }\n if(sum>max_sum)\n max_sum=sum;\n }\n }\n cout<\n\nmain(){\n int n,m,k,c1=0,sum=0,qnt=0,a,b,c,buy=0,max=0;\n char name[20];\n scanf(\"%d %d %d\",&n,&m,&k);\n \n int mas[n+1][m+1][3];\n \n for(a=0;amax){ max=dif[c][0]; qnt=dif[c][1]; c1=c;}\n }\n dif[c1][0]=0; dif[c1][1]=0;\n if(max>0){\n if(qnt>(k-buy)) qnt=(k-buy);\n sum=sum+max*qnt;\n buy=buy+qnt;\n goto bgn;\n }\n else buy=k;\n }\n une[a][b]=sum;\nxax: sum=0; qnt=0; \n } \n }\n max=0;\n for(a=0;amax) max=une[a][b];\n }\n }\n printf(\"%d\",max);\n return 0;\n }\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "f63c3f91e0920b98f602f588f3066473", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#define nganggo using\n#define nemspes namespace\n#define standar std\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n \n#define FOR(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<(_t);++(_i))\n#define FORN(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<=(_t);++(_i))\n#define FORD(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i>=(_t);--(_i))\n#define FORLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);_i<(_t);++(_i))\n#define FORNLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);(_i)<=(_t);++(_i))\n#define FORDLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);(_i)>=(_t);--(_i))\n#define FOREACH(_i,_a) for (__typeof(_a.begin()) _i=_a.begin();_i!=_a.end();++_i)\n#define RESET(_a,_value) memset(_a,_value,sizeof(_a))\n#define debugln clog << \"----OK----\" << endl;\n#define debug(_a) clog << \"Value of \" << #_a << \" is: \" << (_a) << endl;\n#define pb push_back\n#define ppb pop_back\n#define pf push_front\n#define ppf pop_front\n#define ff first\n#define ss second\n#define mp make_pair\n#define SIZE(_a) (int)_a.size()\n#define VSORT(_a) sort(_a.begin(),_a.end())\n#define SSORT(_a,_val) sort(_a,_a+(_val))\n#define ALL(_a) _a.begin(),_a.end()\n#define MAX(_a,_b) (((_a)>(_b))?(_a):(_b))\n#define MIN(_a,_b) (((_a)<(_b))?(_a):(_b))\n \nnganggo nemspes standar;\n \nconst int dr[]={ 1, 0,-1, 0, 1, 1,-1,-1};\nconst int dc[]={ 0, 1, 0,-1, 1,-1,-1, 1};\nconst double eps=1e-9;\ntypedef vector vi;\ntypedef pair ii;\ntypedef vector vii;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pll;\ntypedef vector vpll;\ntypedef vector vll;\ntypedef pair pdd;\ntypedef vector vpdd;\nconst int INF=0x7FFFFFFF;\nconst ll INFLL=0x7FFFFFFFFFFFFFFFLL;\nconst double pi=acos(-1);\n\nstruct point_i\n{\n\tint x,y;\n\tpoint_i (int _x=0,int _y=0) {x=_x;y=_y;}\n};\n\nstruct point\n{\n\tdouble x,y;\n\tpoint (double _x=0,double _y=0) {x=_x;y=_y;}\n\tbool operator < (const point &other) const\n\t{\n\t\tif (fabs(x-other.x)1 || mub<0 || mub>1)\n\t\t{\n\t\t\tans.x=0;\n\t\t\tans.y=0;\n\t\t\treturn 0;\n\t\t}\n\t\tans.x=beg.x+mua*(end.x-beg.x);\n\t\tans.y=beg.y+mua*(end.y-beg.y);\n\t\treturn 1;\n\t}\n};\n\nstruct line\n{\n\tdouble a,b,c,len;\n\tline () {a=b=c=0.0;}\n\tline (const point &a_,const point &b_)\n\t{\n\t\tif (fabs(a_.x-b_.x) pl3;\ntypedef vector vpl3;\n\nll n,m,k,a,b,c;\nll ans;\nstring st;\nvector vc;\n\nint main()\n{\n\t// ans=-INFLL+3;\n\tans=0LL;\n\tcin >> n >> m >> k;\n\tvpl3 z;\n\tz.clear();\n\tvc.assign(n+1,z);\n\tFOR (iz,0,n)\n\t{\n\t\tcin >> st;\n\t\tFOR (i,0,m)\n\t\t{\n\t\t\tcin >> a >> b >> c;\n\t\t\tvc[iz].pb(mp(mp(a,b),c));\n\t\t}\n\t}\n\tFOR (iz,0,n)\n\t{\n\t\tFOR (jz,0,n)\n\t\tif (iz==jz) continue;\n\t\telse\n\t\t{\n\t\t\tvpll tmp;\n\t\t\ttmp.clear();\n\t\t\tFOR (i,0,m) tmp.pb(mp(vc[jz][i].ff.ss-vc[iz][i].ff.ff,vc[iz][i].ss));\n\t\t\tsort(ALL(tmp),greater());\n\t\t\tll anz=0,tmpk=k;\n\t\t\tFOR (i,0,SIZE(tmp))\n\t\t\t{\n\t\t\t\tif (tmp[i].ff<0) break;\n\t\t\t\tif (tmp[i].ssans) ans=anz;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "4381f052a345fb5b1bef2542e4e70879", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\nint n,m,k;\nchar w[10001];\nint a[11][101],b[11][101],c[11][101];\nstruct wang\n{\n\tint z;\n\tint v;\n};\nbool cmp(wang a,wang b)\n{\n\tif(a.z>b.z) return 1;\n\telse return 0;\n}\nwang joy[1001];\nint main()\n{\n\tint ans;\n\twhile(scanf(\"%d%d%d\",&n,&m,&k)==3)\n\t{\n\t\tans=0;\n\t\tfor(int i=0;i=k)\n\t\t\t\t\t{\n\t\t\t\t\t\tx=x+joy[p].z*(k-t);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(x>ans) \n\t\t\t\t{\n\t\t\t\t\tans=x;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}\n\t\t\t\t\n\t\t\t\t\t\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "e8c48e9528eecf8c60063f93cd92f551", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nint n,m,k;\nint x,y,z;\n\nint dp[101];\nchar name[11];\n\nint maxgain=0;\nint tekgain=0;\n\nstruct diff\n{\n int id;\n int amt;\n};\ndiff D[101];\nbool comp_less(diff a, diff b)\n{\n if (a.amt!=b.amt) return (a.amt>b.amt);\n return (a.id>b.id);\n}\n\nstruct item\n{\n int buy;\n int sell;\n int amt;\n};\nitem It[11][101];\nint main()\n{\n scanf(\"%d%d%d\",&n,&m,&k);\n for (int i=0;imaxgain) maxgain=tekgain;\n }\n }\n }\n printf(\"%d\\n\",maxgain);\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "0ae99cafc5acddc7bb8ea50bfa4a572f", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include \n#include\n#define pb push_back\n#define ppb pop_back\n#define mp make_pair\n#define all(x) (x).begin(),(x).end()\n#define LL long long\nusing namespace std;\ntypedef pair pii;\nconst double eps = 1e-9;\nint a[11][111],b[11][111],c[11][111];\nint main()\n{\n\tint n,m,k;\n\tint res = 0;\n\tcin>>n>>m>>k;\n\tstring s;\n\tfor(int i = 0; i < n; i++)\n\t{\n\t\tcin>>s;\n\t\tfor(int j = 0; j < m; j++)\n\t\t\tcin>>a[i][j]>>b[i][j]>>c[i][j];\n\t}\n\tvector v(m);\n\tfor(int i = 0; i < n; i++)\n\t\tfor(int j = 0; j < n; j++)\n\t\t{\n\t\t\tif (i == j) continue;\n\t\t\tfor(int r = 0; r < m; r++)\n\t\t\t{\n\t\t\t\tv[r].first = b[j][r] - a[i][r];\n\t\t\t\tv[r].second = c[i][r];\n\t\t\t}\n\t\t\tsort(all(v));\n\t\t\tint K = k,sum = 0;\n\t\t\tfor(int r = m - 1; r >= 0; r--)\n\t\t\t{\n\t\t\t\tif (v[r].first > 0)\n\t\t\t\t{\n\t\t\t\t\tif (v[r].second >= K)\n\t\t\t\t\t{\n\t\t\t\t\t\tsum += K * v[r].first;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tK -= v[r].second;\n\t\t\t\t\tsum += v[r].second * v[r].first;\n\t\t\t\t} else break;\n\t\t\t}\n\t\t\tres = max(res,sum);\n\t\t}\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n#define sz size()\n#define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))\n#define ROF(i,a,b) for(int (i) = (a); (i) >= (b); --(i))\n#define rep(i,n) for (int (i) = 0; (i) < (n); ++(i))\n#define fe(i,a) for (int (i) = 0; (i) < int((a).size()); ++(i))\n#define C(a) memset((a),0,sizeof(a))\n#define inf 1000000000\n#define pb push_back\n#define ppb pop_back\n#define all(c) (c).begin(), (c).end()\n#define pi 2*acos(0.0)\n#define sqr(a) (a)*(a)\n#define mp(a,b) make_pair((a), (b))\n#define X first\n#define Y second\n#define y huy\n\ntypedef vector vint;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\nconst int INF=2000000000; \n int a[100][3000],b[100][3000],c[100][3000];\nint main( ) {\n //freopen(\"input.txt\",\"r\",stdin);freopen(\"output.txt\",\"w\",stdout);\n // freopen(\"input.txt\",\"r\",stdin);\n int n,cnt=0,m,k;\n cin>>n>>m>>k;\n rep(i,n){\n string s;cin>>s;\n rep(j,m){\n cin>>a[i][j]>>b[i][j]>>c[i][j];\n }\n\n }\n int ans=0;\n rep(i,n)rep(j,n)if(i^j){\n vector v(m);\n rep(u,m)v[u]=mp(b[j][u]-a[i][u],c[i][u]);\n sort(all(v)); \n reverse(all(v));\n int u=0,h=0;\n for(int g=0;g\n#include \n#include \nusing namespace std;\n\n#define F first\n#define S second\n#define MP make_pair\ntypedef pair pii;\n\nconst int MAXn = 10, MAXm = 100;\nint n, m, k;\nint b[MAXn][MAXm], s[MAXn][MAXm], r[MAXn][MAXm];\nstring str;\nlong long ans;\npii ez[MAXm];\nint rem;\nlong long prof;\n\nbool cmp(pii x, pii y)\n{\n\treturn x.F>y.F;\n}\n\nint main()\n{\n\tcin >> n >> m >> k;\n\tfor(int i=0; i> str;\n\t\tfor(int j=0; j> b[i][j] >> s[i][j] >> r[i][j];\n\t}\n\n\tfor(int i=0; i0; z++)\n\t\t\t{\n\t\t\t\tprof += min(rem, ez[z].S)*ez[z].F;\n\t\t\t\trem -= min(rem, ez[z].S);\n\t\t\t}\n\n\t\t\tans = max(ans, prof);\n\t\t}\n\n\tcout << ans << endl;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "5618bc30bad2395e684d6331e2f7099d", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n\nusing namespace std;\n\nint A[101],B[101];\n\nvoid quick123(int l,int m)\n{\n int i,j,z;\n \n z=A[(l+m)/2];\n i=l;\n j=m;\n do\n {\n while(A[i]>z)\n i++;\n while(A[j]=B[l] && A[l]>0) {P+=B[l]*A[l]; x-=B[l];}\n else if (A[l]>0) {P+=x*A[l]; x=0;}\n }while(x>0 && l\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define MAX(a,b) ((a)>(b)?(a):(b))\n#define MIN(a,b) ((a)<(b)?(a):(b))\n#define INF 987654321\ntypedef __int64 LL;\nusing namespace std;\n/////////////////////////////////////////////////\n#define MAXN 110\nint a[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN];\nchar s[MAXN];\nbool vis[MAXN];\nint n,m,t;\nint Sale(int x,int y,int res)\n{\n\tbool flag;\n\tint ans=0,i,big,id,cnt;\n\tmemset(vis,false,sizeof(vis));\n\tflag=true;\n\twhile(flag&&res)\n\t{\n\t\tbig=0;\n\t\tflag=false;\n\t\tfor(i=1;i<=m;i++)\n\t\t{\n\t\t\tif(!vis[i])\n\t\t\t{\n\t\t\t\tflag=true;\n\t\t\t\tif(b[y][i]-a[x][i]>big)\n\t\t\t\t{\n\t\t\t\t\tbig=b[y][i]-a[x][i];\n\t\t\t\t\tid=i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(big==0)\n\t\t\tbreak;\n\t\tcnt=MIN(res,c[x][id]);\n\t\tans+=cnt*big;\n\t\tres-=cnt;\n\t\tvis[id]=true;\n\t}\n\treturn ans;\n}\nint main()\n{\n\tint i,j,k,ans,temp;\n\twhile(~scanf(\"%d%d%d\",&n,&m,&t))\n\t{\n\t\tfor(i=1;i<=n;i++)\n\t\t{\n\t\t\tscanf(\" %s\",s);\n\t\t\tfor(j=1;j<=m;j++)\n\t\t\t\tscanf(\"%d%d%d\",&a[i][j],&b[i][j],&c[i][j]);\n\t\t}\n\t\tans=0;\n\t\tfor(i=1;i<=n;i++)\n\t\t{\n\t\t\tfor(j=1;j<=n;j++)\n\t\t\t{\n\t\t\t\ttemp=Sale(i,j,t);\n\t\t\t\tans=MAX(ans,temp);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "61649a4a7fb6ef845270e5a13920b489", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "// rc.cpp\n//\n\n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef unsigned char uchar;\ntypedef unsigned int uint;\ntypedef unsigned __int64 uint64;\n\n#define PI 3.1415926535897932384626433832795\n#define EPS 1E-8\n\nint p[101][101],\n\tai[1000],\n\tbi[1000];\nuint ci[1000];\n\nint main(int argc, char* argv[])\n{\n\tuint n, m, k;\n\tcin >> n >> m >> k;\n\n\tstring u;\n\tuint o = 0;\n\tfor (uint i = 0; i < n; ++i)\n\t{\n\t\tcin >> u;\n\t\tfor (uint j = 0; j < m; ++j)\n\t\t{\n\t\t\tuint ix = o + j;\n\t\t\tcin >> ai[ix] >> bi[ix] >> ci[ix];\n\t\t}\n\n\t\to += m;\n\t}\n\n\tuint e = n * m;\n\tint\tma = 0;\n\tfor (uint u = 0; u < e; u += m)\n\t{\n\t\tfor (uint v = 0; v < e; v += m)\n\t\t{\n\t\t\tif (u == v)\n\t\t\t\tcontinue;\n\t\t\tfor (uint i = 1; i <= m; ++i)\n\t\t\t{\n\t\t\t\tuint ix = u + i - 1;\n\t\t\t\tfor (uint j = 1; j <= k; ++j)\n\t\t\t\t{\n\t\t\t\t\tint mx = p[j][i - 1];\n\t\t\t\t\tfor (uint x = 1; x <= ci[ix] && x <= j; ++x)\n\t\t\t\t\t{\n\t\t\t\t\t\tint mm = p[j - x][i - 1] + (bi[v + i - 1] - ai[ix]) * x;\n\t\t\t\t\t\tif (mm > mx)\n\t\t\t\t\t\t\tmx = mm;\n\t\t\t\t\t}\n\t\t\t\t\tp[j][i] = mx;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (p[k][m] > ma)\n\t\t\t\tma = p[k][m];\n\t\t}\n\t}\n\n\tcout << ma << endl;\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "3dcb02ad7ddc39aa650a18087831bb76", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nstruct Thing\n{\n\tint a, b, c;\n};\n\nstruct Planet\n{\n\tstring name;\n\tvector things;\n};\n\nint main()\n{\n#ifdef _DEBUG\n\tfreopen(\"test.in\", \"a+\", stdin);\n#endif\n\n\tint n, m, k;\n\tcin >> n >> m >> k;\n\tvector arr(n);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tcin >> arr[i].name;\n\t\tarr[i].things.resize(m);\n\t\tfor (int j = 0; j < m; ++j)\n\t\t\tcin >> arr[i].things[j].a >> arr[i].things[j].b >> arr[i].things[j].c;\n\t}\n\n\tvector < vector < vector < pair > > > other (n);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tother[i].push_back(vector < pair > (m));\n\t\t\tfor (int t = 0; t < m; ++t)\n\t\t\t{\n\t\t\t\tother[i][j][t].first = arr[j].things[t].b - arr[i].things[t].a;\n\t\t\t\tother[i][j][t].second = min(k, arr[i].things[t].c);\n\t\t\t}\n\t\t\tsort(other[i][j].begin(), other[i][j].end());\n\t\t}\n\t}\n\n\tint cnt = 0;\n\tint trum, temp_cnt;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tif (i == j)\n\t\t\t\tcontinue;\n\t\t\ttrum = 0;\n\t\t\ttemp_cnt = 0;\n\t\t\tfor (int t = m - 1; t >= 0; --t)\n\t\t\t{\n\t\t\t\tif (other[i][j][t].first < 0)\n\t\t\t\t\tcontinue;\n\t\t\t\tif (other[i][j][t].second <= k - trum)\n\t\t\t\t{\n\t\t\t\t\ttemp_cnt += other[i][j][t].first * other[i][j][t].second;\n\t\t\t\t\ttrum += other[i][j][t].second;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ttemp_cnt += other[i][j][t].first * (k - trum);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcnt = max(cnt, temp_cnt);\n\t\t}\n\t}\n\n\tcout << cnt;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "c07087be41ff289463b4afd6b0d6d2f8", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \nusing namespace std;\n\nint main()\n{\n\tchar temp[11];\n\tint n,m,k;\n\tcin >> n >> m >> k;\n\tint a[10][100],b[10][100],c[10][100];\n\tfor( int i = 0; i < n; i++ )\n\t{\n\t\tcin >> temp;\n\t\tfor( int j = 0; j < m; j++ )\n\t\t\tcin >> a[i][j] >> b[i][j] >> c[i][j];\n\t}\n\tint maxi = 0;\n\tfor( int i = 0; i < n; i++ )\n\t\tfor( int j = 0; j < n; j++ )\n\t\t{\n\t\t\tif( i == j )\n\t\t\t\tcontinue;\n\t\t\tvector< pair > diff(m);\n\t\t\tfor( int t = 0; t < m; t++ )\n\t\t\t\tdiff[t] = make_pair(b[j][t]-a[i][t],t);\n\t\t\tsort(diff.rbegin(),diff.rend());\n\t\t\tint kc = k, cur = 0;\n\t\t\tfor( int t = 0; kc > 0 && t < m && diff[t].first > 0 ; t++ )\n\t\t\t{\n\t\t\t\tcur += min(kc,c[i][diff[t].second])*diff[t].first;\n\t\t\t\tkc -= min(kc,c[i][diff[t].second]);\n\t\t\t}\n\t\t\t//cout << i << \" \" << j << \" \" << cur << endl;\n\t\t\tmaxi = max(maxi,cur);\n\t\t}\n\tcout << maxi << endl;\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "acfc5a1ad9f811d8f5b39fba8768a14c", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nconst int N = 103;\n\nint a, b, k, n, m;\nint memo[N][N];\nint visi[N][N];\nint turn;\nint qt[20][N], be[20][N], cs[20][N];\nchar str[20];\n\nint dp (int i, int k) {\n if (i == m || k <= 0)\n return 0;\n\n int & me = memo[i][k];\n if (visi[i][k] == turn)\n return me;\n visi[i][k] = turn;\n\n me = dp(i+1, k);\n int pr = be[b][i]-cs[a][i];\n\n if (pr <= 0)\n return me;\n\n for (int j = 1; j <= min(qt[a][i], k); j++) {\n me = max(me, pr*j + dp(i+1, k-j));\n }\n\n return me;\n}\n\nint main ( ){\n scanf(\"%d %d %d\", &n, &m, &k);\n for (int i = 0; i < n; i++) {\n scanf(\" %s\", str);\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &cs[i][j]);\n scanf(\"%d\", &be[i][j]);\n scanf(\"%d\", &qt[i][j]);\n }\n }\n\n int res = 0;\n for (int i = 0; i < n ; i++) {\n for (int j = 0; j < n; j++) {\n if (i == j)\n continue;\n turn++;\n a = i;\n b = j;\n int val = dp(0, k);\n res = max(res, val);\n }\n }\n\n printf(\"%d\\n\", res);\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "8834ddb72dcc07d681da419f5d6c0e49", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n\nusing namespace std;\n\nbool lessp(const pair a, const pair &b){\n\treturn a.first==b.first? a.second>b.second : a.first>b.first;\n};\n\nint n,m,k;\n\nstruct good{\n\tint a,b,c;\n};\n\nstruct planet{\n\tvector goods;\n\tplanet(){\n\t\tgoods.resize(m);\n\t}\n};\n\nvector data;\n\nint main(){\n\tscanf(\"%d%d%d\",&n,&m,&k);\n\tdata.resize(n);\n\tfor (int i=0; i > sprice(m);\n\t\t\t\tfor (int q=0; q0 && ck>0){\n\t\t\t\t\tcans+=sprice[i].first*min(ck,sprice[i].second);\n\t\t\t\t\tck-=min(ck,sprice[i].second);\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tans=max(ans,cans);\n\t\t\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "a7f97a31c28b8d17772cbde914c97bcb", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#pragma comment(linker, \"/STACK:500000000\") \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define Int long long \n#define INF 0x3F3F3F3F \n#define eps 1e-10 \nusing namespace std; \n\nvector A;\nint a[111][111], b[111][111], c[111][111];\nint bs[111], k;\n\nint ff()\n{\n\tint sum = 0;\n\tsort(A.begin(), A.end(), greater());\n\tfor(int i = 0, sz = min(k, (int)A.size()); i < sz; i++)\n\t\tsum += A[i];\n\tA.clear();\n\treturn sum;\n}\n\nint main()\n{\n\tint n, m, i, j, h, v;\n\tscanf(\"%d %d %d\", &n, &m, &k);\n\tfor(i = 0; i < n; i++)\n\t{\n\t\tscanf(\"%*s\");\n\t\tfor(j = 0; j < m; j++)\n\t\t{\n\t\t\tscanf(\"%d %d %d\", &a[i][j], &b[i][j], &c[i][j]);\n\t\t}\n\t}\n\tint res = 0;\n\tfor(i = 0; i < n; i++)\n\t{\n\t\tfor(j = 0; j < n; j++)\n\t\t{\n\t\t\tif (i != j)\n\t\t\t{\n\t\t\t\tfor(v = 0; v < m; v++)\n\t\t\t\t{\n\t\t\t\t\tfor(int lol = c[i][v]; lol--; )\n\t\t\t\t\t{\n\t\t\t\t\t\tif (b[j][v] - a[i][v] > 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tA.push_back(b[j][v] - a[i][v]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tres = max(res, ff());\n\t\t\t}\n\t\t}\n\t}\n\tcout << res << endl;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "9fd44525a74b68a3a46ee16385361452", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\n\n\nusing namespace std;\n\nbool comp (pair a, pair b){\n return (a.second < b.second);\n}\n\nint main(){\n int n, m, k;\n int buf;\n string name;\n vector > buy;\n vector > sale;\n vector > numb;\n cin>>n>>m>>k;\n for(int i = 0; i < n; i++){\n cin>>name;\n vector v;\n buy.push_back(v);\n sale.push_back(v);\n numb.push_back(v);\n for(int j = 0; j < m; j++){\n cin>>buf;\n buy[i].push_back(buf);\n cin>>buf;\n sale[i].push_back(buf);\n cin>>buf;\n numb[i].push_back(buf);\n }\n }\n int min = 0, it;\n int cur;\n int space;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(i!=j){\n cur = 0;\n priority_queue diff;\n for(int k =0; k < m; k++){\n diff.push((sale[j][k] - buy[i][k]) * 1000 + numb[i][k]);\n }\n space = k;\n while(space > 0 & !diff.empty()){\n buf = diff.top();\n if(buf < 0)break;\n diff.pop();\n if(buf % 1000 <= space) it = buf % 1000; else it = space;\n cur+=buf/1000 * it;\n space -= it;\n }\n if(cur > min) min = cur;\n }\n\n }\n }\n\n cout<\n#include \n#include \n#include \n#include \n\nstruct product {\n int bvalue;\n int svalue;\n int quantity;\n}; \n\nstruct planet {\n std::vector merc;\n};\n\nint max_profit (struct planet a, struct planet b, int m, int k) {\n std::multiset> profit_table;\n\n for (int i = 0; i < m; ++i) {\n std::pair profit_merc;\n profit_merc.first = a.merc[i].bvalue - b.merc[i].svalue;\n profit_merc.second = a.merc[i].quantity;\n profit_table.insert(profit_merc);\n }\n\n int profit = 0;\n for (auto merc_profit : profit_table) {\n if (merc_profit.first > 0) break;\n if (k > merc_profit.second) {\n profit += -merc_profit.first*merc_profit.second;\n k -= merc_profit.second;\n } else {\n profit += -merc_profit.first*k;\n break;\n }\n }\n\n return profit;\n}\n\nint main(void)\n{\n int n, m, k;\n\n std::cin >> n >> m >> k;\n \n std::vector planets (n);\n for (int i = 0; i < n; ++i) {\n std::string foo;\n std::cin >> foo;\n for (int j = 0; j < m; ++j) {\n struct product p;\n std::cin >> p.bvalue;\n std::cin >> p.svalue;\n std::cin >> p.quantity;\n planets[i].merc.push_back(p);\n }\n }\n\n int max = 0;\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n int temp = max_profit(planets[i], planets[j], m, k);\n if (temp > max) max = temp;\n }\n }\n\n std::cout << max << std::endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "e173be1314e4ee750485173e78468b0f", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n\nusing namespace std;\n\nint A[12][105][5],n,m,k,l,i,j,a1,a2,ma,q,a,s;\nmultiset > b;\npair top;\nchar str[15];\n\nmain()\n{\n \n\n scanf(\"%d%d%d\",&n,&m,&k);\n for (i=1;i<=n;i++)\n {\n scanf(\"%s\",&str); \n for (j=1;j<=m;j++)\n scanf(\"%d%d%d\",&A[i][j][1],&A[i][j][2],&A[i][j][3]); \n }\n \n ma=0; \n for (i=1;i<=n;i++)\n {\n for (j=1;j<=n;j++)\n {\n if (i!=j)\n {\n b.clear(); \n for (q=1;q<=m;q++)\n {\n a=A[j][q][2]-A[i][q][1]; \n b.insert(pair (2000-a,A[i][q][3]));\n } \n l=k;\n s=0;\n while (!b.empty())\n {\n top=*b.begin();\n b.erase(b.begin());\n a1=2000-top.first;\n a2=top.second; \n if (a1>0)\n {\n if (l>=a2) {s=s+a1*a2;l=l-a2;}\n else {s=s+a1*l;l=0;} \n } else break;\n if (l==0) break;\n }\n if (s>ma) ma=s;\n } \n } \n } \n printf(\"%d\\n\",ma);\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "c86ded644788de2fe3fa7b1118e2e332", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint kol[100][200],b[100][200],s[100][200];\n\n\nbool F(pair a, pair b)\n{\n\treturn (a.first > b.first);\n};\n\nint main()\n{\n\tint n,m,k;\n\tscanf(\"%d%d%d\",&n,&m,&k);\n\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tstring name;\n\t\tcin >> name;\n\t\tfor (int j = 0; j < m; j++)\n\t\t{\n\t\t\tcin >> b[i][j] >> s[i][j] >> kol[i][j];\n\t\t}\n\t}\n\tint ans = 0;\n\n\tfor (int A = 0; A < n; A++)\n\tfor (int B = 0; B < n; B++)\n\t{\n\t\tif (A == B) continue;\n\t\tvector< pair > pr;\n\t\tpr.clear();\n\t\tfor (int i = 0; i < m; i++)\n\t\t{\n\t\t\tpr.push_back(make_pair(-b[A][i] + s[B][i],i));\n\t\t}\n\t\tsort(pr.begin(),pr.end(), F);\n\n\t\tint sum = 0, k1 = k;\n\t\tfor (int i = 0; i < pr.size(); i++)\n\t\t{\n\t\t\tif (k1 == 0) break;\n\t\t\tint j = pr[i].second;\n\t\t\tif (pr[i].first <= 0) break;\n\t\t\tint K = min(k1, kol[A][j]);\n\t\t\tsum += K * pr[i].first;\n\t\t\tk1 -= K;\n\t\t}\n\t\tans = max(ans,sum);\n\t}\n\tcout << ans << endl;\n\n\treturn 0;\t\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "4548f350692b3c7e89923093a97b543d", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "// iTwin\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint a[110][110];\nint b[110][110];\nint c[110][110];\nvector > XD;\nint main(){\n #ifdef LOCAL\n freopen(\"a.in\", \"r\", stdin);\n freopen(\"a.out\", \"w\", stdout);\n #endif\n int n,m,k,i,j,q,w;\n scanf(\"%d%d%d\",&n,&m,&k);\n for(i=0;i\n#include\n#include\n#include\n\nusing namespace std;\n\nint main(void){\n\n int n, m, k, a, b, c;\n char tx[16];\n int p[12][128][4];\n int max = 0;\n\n scanf(\"%d %d %d\", &n, &m, &k);\n\n for(int i = 0; i < n; ++i){\n\n gets(tx);\n gets(tx);\n\n for(int j = 0; j < m; ++j){\n scanf(\"%d %d %d\", &a, &b, &c);\n p[i][j][0] = a;\n p[i][j][1] = b;\n p[i][j][2] = c;\n }\n }\n\n \n\n for(int i = 0; i < n; ++i){\n\n for(int j = 0; j < n; ++j){\n\n if(i == j)continue;\n\n vector< pair > vc;\n\n for(int z = 0; z < m; ++z){\n vc.push_back( make_pair( p[j][z][1] - p[i][z][0], p[i][z][2] ));\n }\n\n sort(vc.begin(), vc.end());\n reverse(vc.begin(), vc.end());\n\n int ct = 0;\n int ret = 0;\n\n\n for(int z = 0; z < m; ++z){\n\n if(vc[z].first <= 0) continue;\n\n int qt = min(vc[z].second, (k-ct));\n\n ct += qt;\n ret += qt * vc[z].first;\n if(ct == k)break;\n\n }\n\n if(ret > max){\n max = ret; \n }\n }\n }\n\n printf(\"%d\\n\", max);\n\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "40b388ad78a26c706493caa3e1195381", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n//ifstream cin (\"input.txt\"); ofstream cout (\"output.txt\");\nint n , m , k;\n\nstruct good {\n\tint b;\n\tint a;\n\tint c;\n\tgood (int a = 0 , int b = 0 , int c = 0) : a (a) , b (b) , c(c) {}\n};\n\nint maximize (const vector & source ,const vector & dist) {\n\tint kk = k;\n\tvector > v;\n\tfor (int i = 0; i < source.size(); ++i) {\n\t\tv.push_back (make_pair (-(dist[i].b - source[i].a) , source[i].c));\n\t}\n\n\tsort (v.begin() , v.end());\n\tint res = 0 ;\n\tfor (int i = 0; i < v.size() && kk > 0 && v[i].first < 0; ++i) {\n\t\tres += -v[i].first * (min (kk , v[i].second));\n\t\tkk -= v[i].second;\n\t}\n\treturn res;\n}\n\nint main () {\n\t\n\tcin >> n >> m >> k;\n\tvector > v;\n\tstring s;\n\tv.resize (n);\n\tfor (int i = 0; i < n; ++i) {\n\t\tcin >> s;\n\t\tint a , b , c;\n\t\tfor (int j = 0; j < m; ++j) {\n\t\t\tcin >> a >> b >> c;\n\t\t\tv[i].push_back (good (a , b , c) );\n\t\t}\n\t}\n\n\tint res = 0;\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 0; j < n; ++j) {\n\t\t\tif (i == j) continue;\n\t\t\tres = max (res , maximize (v[i] , v[j]));\n\t\t}\n\t}\n\n\tcout << res;\n\n\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "10e81de6f5daac1895b445750b7bcb9e", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef vector vi; \ntypedef vector vvi; \ntypedef pair ii; \ntypedef long long ll;\n#define sz(a) int((a).size()) \n#define pb push_back \n#define all(c) (c).begin(),(c).end() \n#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) \n#define present(c,x) ((c).find(x) != (c).end()) \n#define cpresent(c,x) (find(all(c),x) != (c).end()) \n#define mp make_pair\n#define go(i,n) for(int i=0;i>n>>m>>K;\n string s;\n\n int x,y,z;\n \n go(i,n){\n cin>>s;\n go(j,m){\n cin>>a[i][j]>>b[i][j]>>c[i][j];\n }\n }\n\n int best=0,cost,q,take,u,v,cc,loc;\n \n typedef pair node;\n \n \n\n\n for(int i=0;i pq;\n\n for(int k=0;k0 && !pq.empty();){\n u=pq.top().first; cc=pq.top().second; pq.pop();\n cc=min(cc,take);\n if(u>0) loc+=u*cc;\n take-=cc;\n }\n //printf(\"i=%d j=%d loc=%d\\n\",i,j,loc);\n best=max(best,loc);\n }\n\n cout<\n\nusing namespace std;\n\nconst int MAXN = 128;\n\nint n, m, k, i, j, ans, buyPlanet, sellPlanet, willChange;\nint a[MAXN][MAXN], b[MAXN][MAXN], c[MAXN][MAXN];\nint F[MAXN], NF[MAXN];\nstring uselessInformation;\n\nint main() {\n\t//freopen(\"input.txt\", \"r\", stdin);\n\tios_base::sync_with_stdio(0);\n\tcin >> n >> m >> k;\n\tfor (i = 1; i <= n; i++) {\n\t\tcin >> uselessInformation;\n\t\tfor (j = 1; j <= m; j++) cin >> a[i][j] >> b[i][j] >> c[i][j];\n\t}\t\n\tans = 0;\n\tfor (buyPlanet = 1; buyPlanet <= n; buyPlanet++) {\n\t\tfor (sellPlanet = 1; sellPlanet <= n; sellPlanet++) {\n\t\t\tif (buyPlanet == sellPlanet) continue;\n\t\t\tfor (j = 0; j <= k; j++) F[j] = -1;\n\t\t\tF[0] = 0;\n\t\t\tfor (i = 1; i <= m; i++) {\n\t\t\t\tfor (j = 0; j <= k; j++) NF[j] = -1;\n\t\t\t\tfor (willChange = 0; willChange <= c[buyPlanet][i]; willChange++) {\n\t\t\t\t\tfor (j = 0; j + willChange <= k; j++) {\n\t\t\t\t\t\tNF[j + willChange] = max(NF[j + willChange], F[j] + (b[sellPlanet][i] - a[buyPlanet][i]) * willChange);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (j = 0; j <= k; j++) NF[j] = max(NF[j], F[j]);\n\t\t\t\tfor (j = 0; j <= k; j++) F[j] = NF[j];\n\t\t\t}\n\t\t\tfor (j = 0; j <= k; j++) ans = max(ans, F[j]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "b5528129c3e8d34ab16cba96ad4c5a34", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//lxrtxdy!\n#include \n#define int long long\n#define mp make_pair\n#define pb push_back\nusing namespace std;\nconst int N = 1123456;\nconst int mod = 1e9 + 7;\nconst int inf = 1e9 + 7;\nint read()\n{\n int x;\n scanf(\"%I64d\", &x);\n return x;\n}\nint a[101][101];\nint b[101][101];\nint c[101][101];\nmain()\n{\n int n, m, k, i, j;\n cin >> n >> m >> k;\n for(i = 1; i <= n; i ++) {\n string s;\n cin >> s;\n for(j = 1; j <= m; j ++) {\n a[i][j] = read();\n b[i][j] = read();\n c[i][j] = read();\n }\n }\n int ans = 0;\n for(i = 1; i <= n; i ++) {\n for(j = 1; j <= n; j ++) {\n if(i == j)\n continue;\n vector > v;\n for(int l = 1; l <= m; l ++) {\n if(a[i][l] < b[j][l]) {\n v.push_back(mp(b[j][l] - a[i][l], c[i][l]));\n }\n }\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n int kt = k, s = 0;\n for(int l = 0; l < v.size(); l ++) {\n int t = min(v[l].second, kt);\n s += (v[l].first * t);\n kt -= t;\n }\n ans = max(ans, s);\n }\n }\n cout << ans;\n}\n//hellobig_tq//hellobig_tq\n//orzlxr!", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "238306313e8523922b6051763e630eb0", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define maxn 10000\n#define maxm 1000000\n#define inf 1234567890\nint n,m,kp;\nstruct TNode\n{\n int a[105],b[105],c[105];\n}nod[15],p1,p2;\nchar str[100];\nint main()\n{\n int i,j,k,l,re,ri,t,maxl;\n while(scanf(\"%d%d%d\",&n,&m,&kp)!=EOF)\n {\n for(i=0;ip2.b[k]-p1.a[k])\n {\n swap(p2.a[l],p2.a[k]);\n swap(p2.b[l],p2.b[k]);\n swap(p2.c[l],p2.c[k]);\n swap(p1.a[l],p1.a[k]);\n swap(p1.b[l],p1.b[k]);\n swap(p1.c[l],p1.c[k]);\n }\n }\n }\n t=0,l=kp;\n for(k=0;k\n\nint main(int argc, const char * argv[])\n{\n char name[100];\n int n, m, k;\n int goods[10][100][3], bonus[1010], max_result = 0;\n \n scanf(\"%d%d%d\", &n, &m, &k);\n for(int i = 0; i < n; i++)\n {\n scanf(\"%s\", &name);\n for(int j = 0; j < m; j++)\n {\n scanf(\"%d%d%d\", &goods[i][j][0], &goods[i][j][1], &goods[i][j][2]);\n }\n }\n for(int i = 0; i < n; i++)\n {\n for(int ii = 0; ii < n; ii++)\n {\n int result = 0;\n \n for(int i=0; i < 1001; i++)\n bonus[i] = 0;\n \n for(int j = 0; j < m; j++)\n if(goods[i][j][0] < goods[ii][j][1])\n {\n bonus[goods[ii][j][1] - goods[i][j][0]] += goods[i][j][2];\n }\n \n int new_k = k;\n for(int i = 1000; i>0; i--)\n {\n int min = bonus[i] < new_k?bonus[i]:new_k;\n result += min * i;\n new_k -= min;\n }\n if(result > max_result)\n max_result = result;\n }\n }\n \n printf(\"%d\\n\", max_result);\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "0e917f1f2f23d8291a88a8c9f2d45eb8", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include \n#include \n\nusing namespace std;\n\nbool cmp(const pair & p1, const pair & p2)\n{\n return p1.first > p2.first;\n}\n\nint main()\n{\n int n, m, k;\n string name;\n cin >> n >> m >> k;\n vector > a(n, vector(m)), b(n, vector(m)), c(n, vector(m));\n for(int i = 0; i < n; i++)\n {\n cin >> name;\n for(int j = 0; j < m; j++)\n {\n cin >> a[i][j] >> b[i][j] >> c[i][j];\n }\n }\n int ans = 0;\n vector > res; \n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < n; j++)\n {\n if(i != j)\n {\n for(int l = 0; l < m; l++)\n {\n res.push_back(make_pair(b[j][l]-a[i][l], c[i][l]));\n } \n sort(res.begin(), res.end(), cmp);\n int temp = 0, r = 0, p = k;\n while(p > 0 && r < res.size() && res[r].first > 0)\n {\n if(p >= res[r].second)\n {\n p -= res[r].second;\n temp += res[r].first * res[r].second;\n r++;\n }\n else\n {\n temp += res[r].first * p;\n p = 0;\n }\n }\n if(temp > ans) ans = temp;\n res.clear();\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "3e279ac06182b0169e4ef1e4212e0436", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define sz(x) ((int)(x).size())\n#define rep(i,l,r) for(int i=(l);i<(r);++i)\ntypedef long long ll;\ntypedef pair pii;\nconst int N = 1e2 + 7;\nconst int INF = 1e9 + 7;\nconst int MOD = 1e9 + 7;\nconst double Pi = acos(-1.0);\nconst double EPS = 1e-8;\n//--------head--------\nint n, m, k;\nint a[N][N], b[N][N], c[N][N];\nchar str[N];\nstruct Node {\n\tint buy, sell, cnt;\n\tNode() {\n\t}\n\tNode(int _buy, int _sell, int _cnt) {\n\t\tbuy = _buy, sell = _sell, cnt = _cnt;\n\t}\n\tbool operator<(const Node &p) const {\n\t\treturn sell - buy > p.sell - p.buy;\n\t}\n};\nvector item;\nint main() {\n\tscanf(\"%d%d%d\", &n, &m, &k);\n\trep(i, 0, n) {\n\t\tscanf(\" %s\", str);\n\t\trep(j, 0, m)\n\t\t\tscanf(\"%d%d%d\", &a[i][j], &b[i][j], &c[i][j]);\n\t}\n\tint ans = 0;\n\trep(i, 0, n)\n\t\trep(j, 0, n) { // i -> j\n\t\t\titem.clear();\n\t\t\trep(r, 0, m)\n\t\t\t\titem.pb(Node(a[i][r], b[j][r], c[i][r]));\n\t\t\tsort(item.begin(), item.end());\n\t\t\tint profit = 0, can = k;\n\t\t\trep(r, 0, sz(item)) {\n\t\t\t\tint num = min(can, item[r].cnt);\n\t\t\t\tif (item[r].sell - item[r].buy < 0)\n\t\t\t\t\tbreak;\n\t\t\t\tprofit += num * (item[r].sell - item[r].buy);\n\t\t\t\tcan -= num;\n\t\t\t\tif (can == 0)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tans = max(ans, profit);\n\t\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "b47de1691764fe8e0f6e1e19006d2d6b", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define FR(i,n) for(int i=0;i\n#define ll long long\n\nstruct P\n{\n\tint buy[100],sell[100],cnt[100];\n} a[10];\n\nint main()\n{\n\t//freopen(\"in.txt\",\"r\",stdin);\n\tint n,m,cap; cin>>n>>m>>cap;\n\tFR(i,n)\n\t{\n\t\tstring s; cin>>s;\n\t\tFR(j,m) cin>>a[i].buy[j]>>a[i].sell[j]>>a[i].cnt[j];\n\t}\n\n\tll res=0;\n\tFR(i,n)\n\t{\n\t\tint cn[100];\n\t\tFR(j,n)\n\t\t{\n\t\t\tif(i==j) continue;\n\t\t\tFR(k,m) cn[k]=a[i].cnt[k];\n\t\t\tint C=cap;\n\t\t\tbool f=true;\n\t\t\tll sum=0;\n\t\t\twhile(C>0 && f)\n\t\t\t{\n\t\t\t\tf=false;\n\t\t\t\tint mx=0,ind;\n\t\t\t\tFR(k,m)\n\t\t\t\t{\n\t\t\t\t\tint t=a[j].sell[k]-a[i].buy[k];\n\t\t\t\t\tif(cn[k] && t>mx)\n\t\t\t\t\t{\n\t\t\t\t\t\tmx=t;\n\t\t\t\t\t\tind=k;\n\t\t\t\t\t\tf=true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!f) continue;\n\t\t\t\tint q=min(C,cn[ind]);\n\t\t\t\tsum+=mx*q;\n\t\t\t\tcn[ind]-=q;\n\t\t\t\tC-=q;\n\t\t\t}\n\t\t\tres=max(sum,res);\n\t\t}\n\t}\n\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define MAXN 1010\n#define INF 1000000007\n#define MP(x,y) make_pair(x,y)\n#define FI first\n#define SE second\n#define EPS 1e-8\nusing namespace std;\nint goods[20][MAXN][3];\nchar str[MAXN];\npair pa[MAXN];\nint main()\n{\n //freopen(\"/home/moor/Code/input\",\"r\",stdin);\n int n,m,k,ans=0;\n scanf(\"%d%d%d\",&n,&m,&k);\n for(int i=0;i=0&&tk&&pa[l].FI>0;--l)\n {\n tmp+=pa[l].FI*min(tk,pa[l].SE);\n tk-=min(tk,pa[l].SE);\n }\n ans=max(ans,tmp);\n }\n printf(\"%d\\n\",ans);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "88d7229d53f7ce0fde5c90426df37bf0", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n#define mp make_pair\n#define pb push_back\n#define li long long\n#define ld long double\n#define all(v) v.begin(),v.end()\n\nint st[11][101];\nint pr[11][101];\nint kol[11][101];\n\nint main()\n{\n //freopen(\"input.txt\",\"r\",stdin);\n //freopen(\"output.txt\",\"w\",stdout);\n int n,m,k;\n cin>>n>>m>>k;\n for(int i=1;i<=n;i++)\n {\n string s;\n cin>>s;\n int a,b,c;\n for(int j=0;j>a>>b>>c;\n st[i][j]=a;\n pr[i][j]=b;\n kol[i][j]=c;\n }\n }\n int ans=0;\n for(int i=1;i<=n;i++)\n for(int j=1;j<=n;j++)\n {\n if(i==j)\n continue;\n\n vector >v;\n v.resize(m);\n for(int l=0;l=0;l--)\n if(v[l].first>0 && v[l].second+now<=k)\n {\n tek+=v[l].first*v[l].second;\n now+=v[l].second;\n }\n else\n if(v[l].first>0 && v[l].second+now>k)\n {\n tek+=v[l].first*(k-now);\n break;\n }\n ans=max(ans,tek);\n }\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\nstruct data {\n long a, b, c;\n};\nint main() {\n //freopen(\"input.txt\", \"rt\", stdin);\n //freopen(\"output.txt\", \"wt\", stdout);\n long n, m, k, i, j, l;\n cin >> n >> m >> k;\n vector < vector < data > > v(n, vector < data > (m));\n string tmp;\n for(i = 0; i < n; i++) {\n cin >> tmp;\n for(j = 0; j < m; j++)\n cin >> v[i][j].a >> v[i][j].b >> v[i][j].c;\n }\n long long ans = 0;\n for(l = 0; l < n; l++)\n for(j = 0; j < n ;j++)\n if(l != j) {\n vector < pair < long, long > > d(m);\n for(i = 0; i < m; i++) {\n d[i].first = v[l][i].b - v[j][i].a;\n d[i].second = v[j][i].c;\n }\n sort(d.begin(), d.end());\n long sm = 0;\n long long p = 0;\n for(i = m - 1; i >= 0; i--) {\n if(d[i].first <= 0)\n break;\n if(d[i].second + sm > k) {\n p += (k - sm) * d[i].first;\n break;\n }\n p += d[i].second * d[i].first;\n sm += d[i].second;\n }\n ans = max(ans, p);\n }\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "06e6d7dc8f1507271436ec418d9bde26", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n//#include \n//#include \n//#include \n//typedef long long ll; \nusing namespace std;\n\n \nint main()\n{\n int n,m,k;\n long sum,max=0;\n cin>>n>>m>>k;\n short a[10][100];\n short b[10][100];\n short c[10][100];\n string s;\n for (int i=0;i>s;\n for (int j=0;j>a[i][j]>>b[i][j]>>c[i][j];\n }\n for (int i=0;iw)\n {\n w=b[i][j1]-a[j][j1];\n wi=j1;\n }\n if (w>0)\n {\n sum+=min((int) c[j][wi],k-masa)*w;\n used[wi]=true;\n masa+=min((int) c[j][wi],k-masa);\n if (k-masa==0) break;\n }\n }\n if (sum>max) max=sum;\n }\n\n\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\ntemplate\ndouble sqrt(T x)\n{\n\treturn sqrt((double) x);\n}\n\ntemplate\nT sqr (T x)\n{\n\treturn x*x;\n}\n\n\n#define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#define X first\n#define Y second\n#define pb push_back\n#define mp make_pair\n//#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)\n\n\n\nconst double PI = acos(-1.0);\nconst int INF = 1000000000;\nconst int MOD = 1000000007;\n\n\n//#define ONLINE_JUDGE\n\nint main()\n{\n\tdouble TIME_START = clock();\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.txt\",\"r\",stdin);\n\t//freopen(\"out.txt\",\"w\",stdout);\n#endif\n\n\tint n,m,k, a,b,c;\n\tcin >> n >> m >> k;\n\tstring s;\n\n\tint sell[15][105], buy[15][105], amount[15][105];\n\t/*\n\tvector< vector > sell(n, vector (m));\n\t\n\tvector< vector > buy(n, vector (m));\n\tvector< vector > amount(n, vector (m));\n\t*/\n\n\n\tfor (int x = 0 ; x < n ; x++)\n\t{\n\t\tcin >> s;\n\t\tfor (int y = 0 ; y < m ; y++) \n\t\t{\n\t\t\tcin >> a >> b >> c;\n\t\t\tsell[x][y] = b;\n\t\t\tbuy[x][y] = a;\n\t\t\tamount[x][y] = c;\n\t\t}\n\n\t\t\n\t}\n\n\n\t// buy sell thing\n\tpair profit[15][15][105];\n\tfor (int x = 0 ; x < n ; x++)\n\t\tfor (int y = 0 ; y < n ; y++)\n\t\t{\n\t\t\tif ( x == y ) continue;\n\t\t\tfor (int z = 0; z < m; z++)\n\t\t\t{\n\t\t\t\tprofit[x][y][z].X = -buy[x][z] + sell[y][z];\n\t\t\t\tprofit[x][y][z].Y = z;\n\t\t\t}\t\t\t\n\t\t}\n\n\tfor (int x = 0 ; x < n ; x++)\n\t\tfor (int y = 0 ; y < n ; y++) sort( profit[x][y], profit[x][y] + m , greater >() );\n\n\tint ans = -1;\n\tint cur,in;\n\n\tfor (int x = 0 ; x < n ; x++)\n\t{\n\t\tfor (int y = 0 ; y < n; y++)\n\t\t{\n\t\t\tif ( x == y ) continue;\n\t\t\tcur = 0;\n\t\t\tin = k;\n\n\t\t\tfor (int z = 0 ; z < m ; z++)\n\t\t\t{\n\t\t\t\tif ( profit[x][y][z].X > 0 )\n\t\t\t\t{\n\t\t\t\t\tcur += profit[x][y][z].X * min(in,amount[x][ profit[x][y][z].Y ]); \n\t\t\t\t\tin -= amount[x][ profit[x][y][z].Y ];\n\t\t\t\t\tif ( in <= 0 ) break;\n\t\t\t\t}\n\t\t\t\telse break;\n\t\t\t}\n\n\t\t\t//eprintf(\"%d\\n\",cur);\n\n\t\t\tif ( cur > ans ) \n\t\t\t{\n\t\t\t\t//eprintf(\"%d %d %d\\n\", x, y, cur);\n\t\t\t\tans = cur;\n\t\t\t}\n\t\t}\n\t}\n\n\n\tcout << ans;\n\t\n\t\n\teprintf(\"\\n\\n%.15lf\\n\\n\",(double)(clock() - TIME_START)/CLOCKS_PER_SEC);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "3509ca597e84942b5282eb6582352117", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nconst int nmax=105;\n\nstruct goods\n{\n\tint a,b,c;\n}d[12][nmax];\n\nint dif[nmax],ID[nmax];\nint MAX=0;\nint comp(const void*a,const void*b)\n{\n\tint aa=*(int*)a,bb=*(int*)b;\n\treturn -(dif[aa]-dif[bb]);\n}\n\nint main()\n{\n\t//freopen(\"input.txt\",\"r\",stdin);freopen(\"output.txt\",\"w\",stdout);\n\tint n,m,i,k,j,p;\n\tstring s;\n\n\tcin >>n>>m>>k;\n\tfor (i=0;i>s;\n\t\tfor (j=0;j>d[i][j].a>>d[i][j].b>>d[i][j].c;\n\t\t}\n\t}\n\tfor (i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector VI;\ntypedef pair PI;\n\n#define foru(i,a,b) for(int i = int(a); i <= int(b); ++i)\n#define ford(i,b,a) for(int i = int(b); i >= int(a); --i)\n#define rep(i, n) for(int i = 0; i < int(n); ++i)\n#define all(a) a.begin(),a.end()\n#define fill(a,x) memset(a, (x), sizeof(a))\n#define mp(x,y) make_pair((x), (y))\n#define pb(x) push_back((x))\n#define fr first\n#define sc second\n\nconst int MAX_N = 20;\nconst int MAX_M = 110;\n\nint a[MAX_N][MAX_M], b[MAX_N][MAX_M], c[MAX_N][MAX_M];\nvector< PI > w;\n\nint main() {\n int n, m, k, res = 0;\n cin >> n >> m >> k;\n foru(i, 1, n) {\n string s;\n getline(cin, s);\n getline(cin, s);\n foru(j, 1, m) scanf(\"%d%d%d\", &a[i][j], &b[i][j], &c[i][j]);\n }\n foru(i, 1, n)\n foru(j, 1, n) if (i != j) {\n w.clear();\n foru(t, 1, m)\n w.pb(mp(b[j][t]-a[i][t], c[i][t]));\n sort(all(w));\n int sumW = 0, sumC = 0;\n ford(t, m-1, 0)\n if (w[t].fr <= 0 || sumC == k) break;\n else {\n int x = min(k-sumC, w[t].sc);\n sumW += x*w[t].fr;\n sumC += x;\n }\n res = max(res, sumW);\n }\n cout << res;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "games", "graph matchings"], "code_uid": "b2c8eb11c975c3a05da6e75116a64153", "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\nlong long NWD(long long a, long long b) { return (b == 0) ? a : NWD(b, a % b); }\ninline long long NWW(long long a, long long b) { return a / NWD(a, b) * b; }\ninline long long normalize(long long x, long long MOD) { return ((x % MOD) + MOD) % MOD; }\nclass GCD_Type { public: long long x, y; };\nGCD_Type exGCD(long long a, long long b)\n{\n\tif (b == 0) return{ 1, 0 };\n\tGCD_Type pom = exGCD(b, a % b);\n\treturn{ pom.y, pom.x - (a / b) * pom.y };\n}\nusing CRT_Var_Type = long long;\nclass CRT_Equation_Type\n{\npublic:\n\tCRT_Var_Type a, m;\n\tinline bool operator==(const CRT_Equation_Type& other)\n\t{\n\t\treturn a == other.a && m == other.m;\n\t}\n\tinline bool operator!=(const CRT_Equation_Type& other)\n\t{\n\t\treturn a != other.a || m != other.m;\n\t}\n};\nclass ChineseRemainderTheorem // system of equations x = ai (mod mi) i = 0,1,2,...\n{\n\tvectorequations;\npublic:\n\tstatic constexpr CRT_Equation_Type Failed = { -1, -1 }; // returned when no solutions\n\tvoid clear() // clears system of equations\n\t{\n\t\tequations.clear();\n\t}\n\tvoid addEquation(CRT_Var_Type a, CRT_Var_Type m) // adds equation of form x = a (mod m)\n\t{\n\t\ta %= m; if (a < 0) a += m;\n\t\tequations.push_back({ a, m });\n\t}\n\tCRT_Equation_Type solve()\n\t{\n\t\tif (equations.empty() == true) return Failed;\n\t\tCRT_Equation_Type ans = equations[0];\n\t\tfor (int i = 1; i < equations.size(); i++)\n\t\t{\n\t\t\tCRT_Equation_Type next = equations[i];\n\t\t\tCRT_Var_Type g = NWD(ans.m, next.m);\n\t\t\tif (ans.a % g != next.a % g) return Failed;\n\t\t\tGCD_Type Bezout = exGCD(ans.m / g, next.m / g);\n\t\t\tCRT_Var_Type lcm = ans.m / g * next.m;\n\t\t\tans.a = (ans.a % lcm + (next.a - ans.a) / g * Bezout.x % (next.m / g) * ans.m % lcm) % lcm;\n\t\t\tif (ans.a < 0) ans.a += lcm;\n\t\t\tans.m = lcm;\n\t\t}\n\t\treturn ans;\n\t}\n};\nlong long n, m, x, y, vx, vy, wynik = LLONG_MAX;\nChineseRemainderTheorem Uklad;\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tcin >> n >> m >> x >> y >> vx >> vy;\n\tif (vx == 0)\n\t{\n\t\tif (x == 0 || x == n) cout << x << \" \" << (~vy ? m : 0) << endl;\n\t\telse return cout << -1 << endl, 0;\n\t}\n\telse if (vy == 0)\n\t{\n\t\tif (y == 0 || y == m) cout << (~vx ? n : 0) << \" \" << y << endl;\n\t\telse return cout << -1 << endl, 0;\n\t}\n\telse\n\t{\n\t\tfor (int i = 0; i < 4; i++)\n\t\t{\n\t\t\tUklad.addEquation(vx * ((i / 2) * n - x), 2 * n);\n\t\t\tUklad.addEquation(vy * ((i % 2) * m - y), 2 * m);\n\t\t\tauto SystemSolution = Uklad.solve();\n\t\t\tif (SystemSolution != ChineseRemainderTheorem::Failed) wynik = min(wynik, SystemSolution.a);\n\t\t\tUklad.clear();\n\t\t}\n\t\tx = normalize(x + wynik * vx, 2 * n);\n\t\ty = normalize(y + wynik * vy, 2 * m);\n\t\t(wynik == LLONG_MAX) ? (cout << -1 << endl) : (cout << x << \" \" << y << endl);\n\t}\n\n\tgetchar();\n\tgetchar();\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["geometry", "number theory"], "code_uid": "a8c4cf2968c9c1c641257259e386d371", "src_uid": "6c4ddc688c5aab1432e7328d27c4d8ee", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long LL;\ntypedef vector VI;\ntypedef vector > VVI;\ntypedef pair PII;\n\n\n#define FOR(i,x,y) for(LL i=x; i<=y; i++)\n#define REP(i,n) for(LL i=0; i t2.points;\n if (t1.good - t1.fail != t2.good - t2.fail)\n return t1.good - t1.fail > t2.good - t2.fail;\n if (t1.good != t2.good)\n return t1.good > t2.good;\n return t1.name < t2.name;\n}\n\n\n\nmap table;\n\npair best(100, 10);\nstring oponent;\nconst string B = \"BERLAND\";\n\nvoid Up(int g, int f) {\n map table1 = table;\n if (g <= f)\n return;\n table1[B].fail += f, table1[B].good += g;\n table1[oponent].fail += g, table1[oponent].good += f;\n string n1 = B, n2 = oponent;\n int g1 = g, g2 = f;\n if (g1 > g2)\n table1[n1].points += 3;\n if (g1 < g2)\n table1[n2].points += 3;\n if (g1 == g2)\n table1[n1].points += 1, table1[n2].points += 1;\n vector tt;\n for (map::iterator it = table1.begin(); it != table1.end(); ++it)\n tt.push_back(it->second);\n SORT(tt);\n if (tt[0].name == B || tt[1].name == B) {\n if (best.first - best.second > g - f)\n best = mp(g, f);\n if (best.first - best.second == g - f && best.second > f)\n best = mp(g, f); \n }\n}\n\nint main() {\n REP(i, 5) {\n string n1, n2, score;\n int g1, g2;\n cin >> n1 >> n2 >> score;\n g1 = score[0] - '0';\n g2 = score[2] - '0';\n table[n1].good += g1, table[n1].fail += g2, table[n1].name = n1, table[n1].games++;\n table[n2].good += g2, table[n2].fail += g1, table[n2].name = n2, table[n2].games++;\n if (g1 > g2)\n table[n1].points += 3;\n if (g1 < g2)\n table[n2].points += 3;\n if (g1 == g2)\n table[n1].points += 1, table[n2].points += 1;\n }\n for (map::iterator it = table.begin(); it != table.end(); ++it)\n if (it->first != B && it->second.games == 2)\n oponent = it->second.name;\n\n REP(i, 100)\n REP(j, 100)\n Up(i, j);\n if (best == mp(100, 10)) {\n cout << \"IMPOSSIBLE\\n\";\n return 0;\n }\n\n cout << best.first << \":\" << best.second << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "9a94f20a8eea30f5a000b1ee54fbcd04", "src_uid": "5033d51c67b7ae0b1a2d9fd292fdced1", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint f(int x){\n\tint ans=x+1;\n\twhile(ans%10==0&&ans!=0){\n\t\tans/=10;\n\t}\n\treturn ans;\n}\nmap m;\nint main(){\n\tint x,ans=0;\n\tcin>>x;\n\twhile(x){\n\t\tans++;\n\t\tm[x]=1;\n\t\tx=f(x);\n\t\tif(m[x]) break;\n\t\telse m[x]=1;\n\t}\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef unsigned int uint;\ntypedef unsigned long long int llu;\n\n#define rep(n) for (int i = 0; i < (n); i++)\n#define get(type, variable) type variable; cin >> variable;\n#define forq(n) for (int q = 0; q < (n); q++)\n#define all(c) c.begin(), c.end()\n#define pb push_back\n#define el endl\n\nint main() {\n int r1, c1, r2, c2;\n cin >> r1 >> c1 >> r2 >> c2;\n \n int d1 = 0;\n if (r1 == r2 && c1 == c2)\n d1 = 0;\n else if (r1 == r2)\n d1 = 1;\n else if (c1 == c2)\n d1 = 1;\n else\n d1 = 2;\n \n int d2 = 0;\n if (r1 == r2 && c1 == c2)\n d2 = 0;\n else if (abs(r1 - r2) == abs(c1 - c2))\n d2 = 1;\n else\n d2 = 2;\n int n = abs(r1 - r2) + abs(c1 - c2);\n if (n % 2)\n d2 = 0;\n \n int d3 = 0;\n d3 = max(abs(r1 - r2), abs(c1 - c2));\n \n cout << d1 << \" \" << d2 << \" \" << d3 << endl;\n \n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["math", "graphs", "shortest paths"], "code_uid": "e26ad8bfb653535d0ef1e248a7ae937d", "src_uid": "7dbf58806db185f0fe70c00b60973f4b", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/*input\n6 3 3 1\n*/\n#include \nusing namespace std;\nint main(){\n\tlong long k,sum=0,end,cap,f,tank;\n\tcin>>end>>cap>>f>>k;\n\tfor(tank=cap;--k;f=end-f){\n\t\tif(tank=2*end-f)\n\t\t\ttank-=end;\n\t\telse{\n\t\t\t++sum;\n\t\t\ttank=cap-(end-f);\n\t\t}\n\t}\n\tif(tank\n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\n\nint main()\n{\n long long int a;\n long long int b;\n cin>>a>>b;\n int ans=0;\n for(int i=2;i<62;i++)\n {\n ll c=1LL<=a&&d<=b)\n ans++;\n }\n }\n cout<\nusing namespace std;\n\nint main()\n{\n\tsetlocale(LC_ALL, \"RUS\");\n\tint m[3], m2[3];\n\tfor (int i = 0; i < 3; i++)\n\t\tcin >> m[i];\n\tfor (int i = 0; i < 3; i++)\n\t\tcin >> m2[i];\n\tif (m[0] != m2[0] && m[1] != m2[1] && m[2] != m2[2])\n\t\tcout << \"NO\";\n\telse\n\t\tcout << \"YES\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "d5e4b93c81948bd0afa03b6034b458d1", "src_uid": "91c9dbbceb467d5fd420e92c2919ecb6", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//#include \n//#include \n//#include \n//#include \n//#include \n//#include \n//#include \n//#include \n//#include \n\n//#if __cplusplus >= 201103L\n//#include \n//#include \n//#include \n//#include \n//\n//#define cauto const auto&\n//#else\n\n//#endif\n\nusing namespace std;\n\n\ntypedef long long ll;\ntypedef pair pii;\ntypedef pair pll;\n\ntypedef vector vint;\ntypedef vector > vvint;\ntypedef vector vll, vLL;\ntypedef vector > vvll, vvLL;\n\n#define VV(T) vector >\n\ntemplate \nvoid initvv(vector > &v, int a, int b, const T &t = T()){\n v.assign(a, vector(b, t));\n}\n\ntemplate \nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define F first\n#define S second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n#define DEBUG\n#ifdef DEBUG\n#define dump(x) cout << #x << \" = \" << (x) << endl;\n#define debug(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n#else\n#define dump(x) \n#define debug(x) \n#endif\n\n#define MOD 1000000007LL\n#define EPS 1e-8\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\n#define maxs(x,y) x=max(x,y)\n#define mins(x,y) x=min(x,y)\n\nvoid mainmain(){\n\tll n;\n\tcin>>n;\n\tll ans=0;\n\tans+=2LL*4*3*(1LL<<(2*(2*n-2-(n+1))));\n\tans+=(n-3)*4*3*3*(1LL<<(2*(2*n-2-(n+2))));\n\tcout<\r\n#define maxn 20010\r\nusing namespace std;\r\nint n, mod, w[2][2 * maxn + 5], s[2][2 * maxn + 5], ans[55];\r\nint main()\r\n{\r\n\tcin >> n >> mod;\r\n\tw[0][maxn] = s[0][maxn] = 1;\r\n\tfor (int i = maxn; i <= 2 * maxn; i ++ ) s[0][i] = 1;\r\n\tfor (int i = 1; i <= n; i ++ )\r\n\t{\r\n\t\tint curs = 1, I = i & 1, J = I ^ 1;\r\n\t\tmemset(w[I], 0, sizeof(w[I])), memset(s[I], 0, sizeof(s[I]));\r\n\t\tfor (int u = i * (i - 1) / 2, j = -u + maxn; j <= u + maxn; j ++ ) w[I][j] = curs, curs = (0ll + curs - s[J][j] + s[J][j - i] + s[J][j + i] - s[J][j] + 2ll * mod) % mod;\r\n\t\tfor (int j = maxn - i * (i - 1) / 2, v = (i + 2) * (i + 1) / 2 + maxn; j <= v; j ++ ) s[I][j] = (s[I][j - 1] + w[I][j]) % mod;\r\n\t\tfor (int j = 1; j < i; j ++ ) ans[i] = (ans[i] + 1ll * (s[J][(i + 1) * i / 2 + maxn] - s[J][j + maxn] + mod) % mod * (i - j)) % mod;\r\n\t}\r\n\tfor (int i = 2; i <= n; i ++ ) ans[i] = (ans[i] + 1ll * i * ans[i - 1]) % mod;\r\n\tcout << ans[n] << endl;\r\n}", "lang_cluster": "C++", "tags": ["math", "dp", "combinatorics", "fft"], "code_uid": "2d6c93590aa749aa36fe381a7dc83b72", "src_uid": "ae0320a57d73fab1d05f5d10fbdb9e1a", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nint main(){\nint n;\nscanf(\"%d\",&n);\nint s[n];\nint i;\nfor(i=0;i\n#include\n#include\nint main()\n{\n int a,b,n,i,x[6];\n memset(x,0,sizeof(x));\n scanf(\"%d\",&n);\n for(i=0;i=3 || x[i]<2) {pd=1;break;}\n }\n if(!pd) printf(\"FAIL\\n\");\n else printf(\"WIN\\n\");\n //system(\"pause\");\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "graphs"], "code_uid": "96f2eacebb3157ee2036182bcab153d1", "src_uid": "2bc18799c85ecaba87564a86a94e0322", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\nconst int MAXN = 105;\nint n, m;\nint cnt[MAXN];\n\nint main()\n{\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < m; i++)\n {\n int x;\n scanf(\"%d\", &x);\n cnt[x]++;\n }\n for (int i = 100; i >= 1; i--)\n {\n int sum = 0;\n for (int j = 1; j <= 100; j++)\n {\n sum += (cnt[j]/i);\n }\n if (sum >= n)\n {\n printf(\"%d\\n\", i);\n return 0;\n }\n }\n printf(\"0\\n\");\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation", "binary search"], "code_uid": "050c571cfb84ae54c196e8e1d59a1db1", "src_uid": "b7ef696a11ff96f2e9c31becc2ff50fe", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n\nlong long MOD=1e9+7;\nint n,k,d;\nint dp[107][107][2];\n\nint main(){\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\n\tcin>>n>>k>>d;\n\tmemset(dp,0,sizeof(dp));\n\tdp[0][0][0]=1;\n\n\tfor(int i=0;i<=n;++i){\n\t\tfor(int w=0;w<=n;++w){\n\t\t\tfor(int z=0;z<=1;++z){\n\t\t\t\tfor(int nextW=1;nextW<=k;++nextW){\n\t\t\t\t\tif(w+nextW<=n){\n\t\t\t\t\t\tint z2=0;\n\t\t\t\t\t\tif(z==1||nextW>=d){\n\t\t\t\t\t\t\tz2=1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdp[i+1][w+nextW][z2]=(dp[i+1][w+nextW][z2]+dp[i][w][z])%MOD;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<=n;++i){\n\t\tans=(ans+dp[i][n][1])%MOD;\n\t}\n\tcout<\r\n#define ll long long\r\nusing namespace std;\r\n\r\nint main(){\r\n int te;\r\n cin>>te;\r\n while(te--){\r\n ll n,ans = 0;\r\n cin >> n;\r\n setmp;\r\n for(int i=1;i*i<=n;i++){\r\n ans++;\r\n mp.insert(i*i);\r\n }\r\n for(int i=1;i*i*i<=n;i++){\r\n ans++;\r\n mp.insert(i*i*i);\r\n }\r\n cout << mp.size() << endl;\r\n }\r\n\r\n return 0;\r\n}\r\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "ff9e74cb55455db11cc07064a4cb0bd7", "src_uid": "015afbefe1514a0e18fcb9286c7b6624", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nconst int N = 110;\n\nint a[N];\n\nint main() {\n int n, x;\n cin >> n >> x;\n for (int i = 0; i < n; i++) {\n int foo;\n cin >> foo;\n if (foo <= x) {\n a[foo] = 1;\n }\n }\n int ans = 0;\n for (int i = 0; i < x; i++) {\n ans += (a[i] == 0);\n }\n ans += (a[x] == 1);\n cout << ans << endl;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "316603145d2c31ec113b906b1afc5ebd", "src_uid": "21f579ba807face432a7664091581cd8", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\ntypedef long long int ll;;\n\nll gcd(ll a,ll b)\n{\n if (b==0)\n return a;\n else\n return gcd(b,a%b);\n}\nint main()\n{\n ll l,r,x,y,n;\n\n cin >> l >> r >> x >> y;\n\n if (y%x == 0)\n n = y/x;\n else\n n = 0;\n //cout <<\"n\" << n << endl;\n\n ll c = 0;\n for (ll i = 1; i*i <= n; i++)\n {\n if (n%i == 0 and i*x>=l and i*x<=r and (n/i)*x >=l and (n/i)*x <=r and gcd(i,n/i) == 1)\n {\n if (i*i != n)\n c++;\n c++;\n }\n }\n\n cout << c;\n\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "feacf4a9907a2582b1cd85aa423c6b62", "src_uid": "d37dde5841116352c9b37538631d0b15", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nusing namespace std;\n\nconst int Max=100000 +10;\nvector v[Max];\nvector p[Max];\n\nbool mark[Max];\nbool mark2[Max];\nint arr[Max];\n\n\nvoid dfs(int u)\n{\n mark[u] = 1;\n for(int i = 0 ; i < v[u].size() ; i++)\n if(mark[v[u][i]] == 0)\n dfs(v[u][i]);\n}\n\nvoid dfs2(int u)\n{\n mark2[u] = 1;\n if( arr[u]!=1)\n for(int i = 0 ; i < p[u].size() ; i++)\n if(!mark2[p[u][i]])\n dfs2(p[u][i]);\n}\n\nint main()\n{\n int n,m;\n cin >> n >>m;\n for(int i=1;i<=n;i++)\n cin >> arr[i];\n for(int i=1;i<=m;i++)\n {\n int tmp1,tmp2;\n cin >> tmp1 >> tmp2 ;\n v[tmp1].push_back(tmp2);\n p[tmp2].push_back(tmp1);\n }\n\n for(int i = 1 ; i <= n ; i++)\n if(arr[i] == 1 && mark[i] == 0)\n dfs(i);\n\n for(int i = 1 ; i <= n ; i++)\n if(arr[i] == 2 && mark2[i] == 0)\n dfs2(i);\n for (int i = 1 ; i <= n ; i++)\n {\n int k=mark2[i] * mark[i];\n cout << k <\n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\nint main() {\n long long a,b,c;\n cin >> a >> b >> c;\n\n long long ans = 2*c + min(a,b) + min(max(a,b), min(a,b)+1);\n cout << ans;\n}\n", "lang_cluster": "C++", "tags": ["greedy"], "code_uid": "a0162c2687b692330c44e85980f0d5c6", "src_uid": "609f131325c13213aedcf8d55fc3ed77", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \"stdio.h\"\n#include \n\nusing namespace std;\n\nint main()\n{\n long c, d, n, m, k;\n long a[100*100+1];\n scanf (\"%ld %ld\", &c, &d);\n scanf (\"%ld %ld\", &n, &m);\n scanf (\"%ld\", &k);\n for (int i = 0; i <= n*m; i++)\n a[i] = LONG_MAX;\n for (int i = 0; i <= k; i++)\n a[i] = 0;\n for (int i = k+1; i <= n*m; i++)\n if (i >= n)\n if (a[i-1] + d < a[i-n] + c)\n a[i] = a[i-1] + d;\n else\n a[i] = a[i-n] + c;\n else\n a[i] = a[i-1] + d;\n if (n*m == k)\n printf (\"0\");\n else\n printf (\"%ld\", a[n*m]);\n}", "lang_cluster": "C++", "tags": ["math", "dp", "implementation"], "code_uid": "9f8d73a36ff35574b8177b17b1c9f603", "src_uid": "c6ec932b852e0e8c30c822a226ef7bcb", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nint main()\n{\n\tlong long n,a,b,c,l,s;\n\tcin>>n>>a>>b>>c;\n\tif (n<4)\n\t{n=4+n;}\n\n\tif (n%4==0)\n\t{\n\t\tl=0;\n\t}\n\tif (n%4==3)\n\t{\n\t\tl=a;\n\t\tif (b+c\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n//#define ONLINE_JUDGE 1\n\nint solve();\n\nint main(int, char**)\n{\n#ifndef ONLINE_JUDGE\n\t(void)freopen(\"input.txt\", \"r\", stdin);\n\t(void)freopen(\"output.txt\", \"w\", stdout);\n#endif \n\n#ifdef ONLINE_JUDGE\n\t::std::ios::sync_with_stdio(false);\n\t::std::cin.tie(0);\n#endif \n\n\tint Q = 1;\n\t//std::cin >> Q;\n\twhile (Q--)\n\t\tsolve();\n\n\treturn 0;\n}\n\n//12 33 45 f(12,12) = 1 1 2 2\n \nconstexpr int MOD = 998244353;\nconstexpr int MAXN = 3008;\n\nint d[MAXN][MAXN];\nint mi[MAXN][MAXN];\n\nint solve()\n{ \n\tusing namespace std;\n\tint N, M, A, B;\n\tcin >> N >> M >> A >> B;\n\t\n\tint g, x, y, z;\n\tcin >> g >> x >> y >> z;\n\n\tfor (int i = 0; i < N; ++i) \n\t{\n\t\tfor (int j = 0; j < M; ++j) \n\t\t{\n\t\t\td[i][j] = g;\n\t\t\tg = (g * 1LL * x + y) % z;\n\t\t}\n\t}\n\n\tfor (int i = 0; i < N; ++i) \n\t{\n\t\tdeque< pair > Q;\n\n\t\tfor (int j = 0; j < M; ++j) \n\t\t{\n\t\t\twhile (!Q.empty() && Q.back().first >= d[i][j])\n\t\t\t\tQ.pop_back();\n\t\t\t\n\t\t\tQ.push_back(make_pair(d[i][j],j) );\n\t\t\t\n\t\t\twhile (!Q.empty() && Q.front().second < j - B + 1)\n\t\t\t\tQ.pop_front();\n\n\t\t\tmi[i][j] = Q.front().first;\n\t\t}\n\n\t}\n\n\tfor (int j = 0; j < M; ++j) {\n\t\tdeque< pair< int, int > > Q;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\twhile (!Q.empty() && Q.back().first >= mi[i][j])\n\t\t\t\tQ.pop_back();\n\n\t\t\tQ.push_back(make_pair(mi[i][j], i));\n\n\t\t\twhile (!Q.empty() && Q.front().second < i - A + 1)\n\t\t\t\tQ.pop_front();\n\n\t\t\tmi[i][j] = Q.front().first;\n\t\t}\n\t}\n\n\tlong long sum = 0;\n\tfor (int i = A-1; i < N; ++i) {\n\t\tfor (int j = B-1; j < M; ++j) sum += (long long)mi[i][j];\n\t}\n\n\tcout << sum << endl;\n\n\t// g[0] given\n\t// g[1] = (g[0] * x + y) mod z\n\t// g[2] = (g[1] * x + y) mod z = (g[0]*x^2 + y * x + y ) mod z\n\t// g[3] = (g[2] * x + y) mod z = (g[0]*x^3 + y * x^2 + y * x + y) mod z\n\n\t// g[i] = (g[0] * x^i + y*(x^(i-1) + x^(i-2) + ... + x^0) ) mod z\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["two pointers", "data structures"], "code_uid": "2cda5092a191a4e1199a8cfa0e773edb", "src_uid": "4618fbffb2b9d321a6d22c11590a4773", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\nusing namespace std;\n\n#define all(p) p.begin(), p.end()\n#define mp make_pair\n#define pb push_back\n#define rep(i,a,b) for(int i=a;i<=b;i++)\n#define ren(i,a,b) for(int i=a;i>=b;i--)\n#define ff first\n#define ss second\n#define ll long long\n#define llu unsigned long long\n#define ld long double\n#define vll vector\n#define vi vector\n#define pii pair \n#define pll pair \n#define vpii vector >\n#define nl cout<<\"\\n\"\n#define sp cout<<\" \"\n#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)\n\nconst int INF = 0x7f7f7f7f;\n\nint main ()\n{\n\tios_base::sync_with_stdio(false);\n //freopen (\"input.txt\", \"r\", stdin);\n //freopen (\"output.txt\", \"w\", stdout);\n\t\n\tll m;\n\tcin>>m;\n\n\tll lo = 1;\n\tll hi = 1e18;\n\tll ans = -1;\n\n\twhile(lo<=hi)\n\t{\n\t\tll mid = (lo+hi)/2;\n\t\tll ways = 0;\n\t\tll k = 2;\n\t\t\n\t\twhile(true)\n\t\t{\n\t\t\tll q = mid;\n\t\t\trep(i, 1, 3)\n\t\t\t\tq = q/k;\n\n\t\t\tif(q==0) break;\n\t\t\tways += q;\n\t\t\tk++;\n\t\t}\n\n\t\tif(ways==m) {ans = mid; hi = mid-1;}\n\t\telse if(ways\nusing namespace std;\nint n,m,x;\nint main()\n{\n\tcin>>n>>m;\n\tx=pow(2,n);\n\tcout<\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\nconst int N = 2e5+10 , OO = 0x3f3f3f3f ;\n//const int mod = 1000000007;\n \n#define clr(arr,val) memset(arr , val , sizeof (arr))\n#define loop(i,n) for(int i = 0;i < int(n);i++)\n#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)\n#define xloop(i,a,b) for(int i = int(a);i <= int(b);i++)\n#define range(vec) for(auto &x : vec) cin >> x;\n#define ALL(v) ((v).begin()) , ((v).end())\n#define SZ(v) ((int)((v).size()))\nint dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction\n//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction\n//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction\n//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction\n\nint n , a , b , c;\n\nint main()\n{\n // startTime = (double)clock();\n #ifndef ONLINE_JUDGE\n //freopen(\"in.in\", \"r\", stdin);\n //freopen(\"out.in\", \"w\", stdout);\n #endif\n\t//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n \n int k ;\n string s;\n cin >> s >> k;\n\n for(int i = 0 ; i < k ; ++i) s += \"?\";\n \n int len = s.size();\n vector < int > sizes;\n for(int st = 0 ; st < len - k ; ++st){ // try all valid starts 'st' with end 'e' \n for(int e = st ; e < len ; ++e){\n string now = s.substr(st , e - st + 1);\n //cout << \"string now = \" << now << \"\\n\";\n if(SZ(now) & 1)\n continue;\n int n = SZ(now) / 2;\n int sz = 0;\n for(int r = 0 ; r < n ; ++r){\n if(now[r] != now[r + n] and now[r + n] != '?')\n break;\n sz += 2;\n }\n sizes.push_back(sz);\n }\n //for(auto x : sizes) cout << x << \" \";\n \n }\n int mx = *max_element(sizes.begin() , sizes.end());\n printf(\"%d\\n\" , mx);\n\n\n return 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "strings", "implementation"], "code_uid": "dc18a0f6b2381e903c409215fe22a00c", "src_uid": "bb65667b65ff069a9c0c9e8fe31da8ab", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\r\nusing namespace std;\r\n#define ll int\r\n#define N 4100010\r\nll yu;\r\nll n;\r\nll f[N];\r\nll fir[N],nxt[N],val[N],cnt=0;\r\ninline void add(ll &x,ll y)\r\n{\r\n\tif(y<0)y+=yu;\r\n\tx+=y;\r\n\tif(x>=yu)x-=yu; \r\n}\r\nll tmp=0;\r\nll g[N];\r\nll st[N],top=0;\r\nint main()\r\n{\r\n\t//freopen(\"test1.in\",\"r\",stdin);\r\n\tcin>>n>>yu;\r\n\tf[1]=1;\r\n\tll sum=1;\r\n\tfor(int i=2;i<=n;i++)\r\n\t{\r\n\t\ttop=0;\r\n\t\tadd(f[i],sum);\r\n\t\tfor(int j=fir[i];j;j=nxt[j])\r\n\t\t{\r\n\t\t\tll o=val[j];\r\n\t\t\t//ll o=q[i].front();\r\n\t\t\tg[o]++;\r\n\t\t\tadd(tmp,f[g[o]]-f[g[o]-1]);\r\n\t\t\tst[++top]=j;\r\n\t\t\t//q[i].pop();\r\n\t\t\t//q[i+o].push(o); \r\n\t\t}\r\n\t\twhile(top)\r\n\t\t{\r\n\t\t\tll o=st[top--];\r\n\t\t\tif(i+val[o]<=n)nxt[o]=fir[i+val[o]];\r\n\t\t\tif(i+val[o]<=n)fir[i+val[o]]=o;\r\n\t\t}\r\n\t\ttmp+=1;\r\n\t\tadd(f[i],tmp);\r\n\t\tg[i]=1;\r\n\t\tcnt++;\r\n\t\tval[cnt]=i;\r\n\t\tif(2*i<=n)nxt[cnt]=fir[2*i];\r\n\t\tif(2*i<=n)fir[2*i]=cnt;\r\n\t\tadd(sum,f[i]);\r\n\t}\r\n\tcout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\nint main()\n{\n\tint n, m;\n\tcin >> n >> m;\n\n\tqueue> kids;\n\n\tfor (int i = 0; i < n; ++i) {\n\t\tint x;\n\t\tcin >> x;\n\t\tkids.push(make_pair(i, x));\n\t}\n\n\twhile (kids.size() > 1) {\n\t\tpair current = kids.front();\n\t\tkids.pop();\n\t\tcurrent.second -= m;\n\n\t\tif (current.second > 0) {\n\t\t\tkids.push(current);\n\t\t}\n\t}\n\n\tcout << kids.front().first + 1;\n\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "9e92027ac7ec6a3c4e250b4b48804e78", "src_uid": "c0ef1e4d7df360c5c1e52bc6f16ca87c", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\nbool a[1000][1000];\nbool arr[1000];\nint main()\n{\n int x,y,a,b,q,i=1;\n cin >>x>>y>>a>>b;\n if(max(x,y)%min(x,y)==0){\n q=max(x,y);\n }\n else{\n while(true){\n if(max(x,y)*i%min(x,y)==0){\n q=max(x,y)*i;\n break;\n }\n i++;\n }\n }\n cout <<(b/q)-((a-1)/q);\n return 0;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "f227df9a87406f13c8067e99439adf93", "src_uid": "c7aa8a95d5f8832015853cffa1374c48", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include\nusing namespace std;\nconst int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f;\n#define endl '\\n'\n#define ll long long\n#define loop(i,n) for(int i = 0;i < int(n);++i)\n#define rloop(i,n) for(int i = int(n);i >= 0;--i)\n#define range(i,a,b) for(int i = int(a);i <= int(b);i++)\n#define p(a) printf(\"%d\",a)\n#define S(n) scanf(\"%d\",&n)\nint t;\nbool LuckyNumber(ll int x){\n x++;\n while (x) {\n if (abs(x%10) == 8)\n return true;\n x /= 10;\n }\n return false;\n}\nint main () {\nll int x,cnt=1;cin>>x;\n while (!LuckyNumber(x)) {cnt++;x++;}\n cout<\nusing namespace std;\nint a[110];\nint main()\n{\n int n,t,i;\n scanf(\"%d\",&n);\n int k=1;\n int sum1=0;\n int sum2=0;\n int sum3=0;\n int sum4=0;\n int k1=n;\n int kkk=n/2;\n for(i=0;ik)\n {\n sum1=sum1+t-k;\n }\n else if(tk)\n {\n sum2=sum2+t-k;\n }\n else if(tk1)\n {\n sum3=sum3+t-k1;\n }\n else if(tk1)\n {\n sum4=sum4+t-k1;\n }\n else if(t\n#include \n#include \n\ntypedef long long ll;\n\nstd::vector days_per_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n\nstruct Record {\n int year, month, day, id;\n \n Record next() const {\n Record ret = *this;\n (ret.id += 1) %= 7;\n ret.day++;\n int need_to_add = month == 1 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));\n if (ret.day == days_per_month[month] + need_to_add) {\n ret.day = 0;\n ret.month++;\n if (ret.month >= 12) {\n ret.month = 0;\n ret.year++;\n }\n }\n return ret;\n }\n};\n\n\nint main() {\n int year; std::cin >> year;\n Record cur{year, 0, 0, 0};\n std::vector arr;\n while (true) {\n if (cur.year - year >= (int)arr.size()) {\n if (arr.size() > 1u && arr.back() == arr.front()) {\n std::cout << cur.year - 1 << std::endl;\n return 0;\n }\n arr.push_back(\"\");\n }\n arr[cur.year - year].push_back(char('0'+cur.id));\n cur = cur.next();\n } \n return 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "48620e1920a5a6d027f53f1b96edef0a", "src_uid": "565bbd09f79eb7bfe2f2da46647af0f2", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint k,pa,pb;\nint A,B;\nint f[1005][1005];\nconst int p=1000000007;\ninline int ksm(int b,int pp)\n{\n\tpp--;\n int a=b; \n while(pp>0)\n {\n \tif(pp%2==1) a=(1LL*b*a)%p;\n b=(1LL*b*b)%p;\n \tpp/=2;\n }\n return a%p;\n}\ninline int inv(int x)\n{\n\treturn ksm(x,p-2);\n}\nint dp(int i,int j)\n{\n\tif(i+j>=k) return (i+j+1LL*pa*inv(pb)%p)%p;\n\tif(f[i][j]!=-1) return f[i][j];\n\treturn f[i][j]=(1LL*((1LL*pa*dp(i+1,j))%p+(1LL*pb*dp(i,i+j))%p)*inv(pb+pa))%p;\n}\nint main(){\n\tscanf(\"%d%d%d\",&k,&pa,&pb);\n\tmemset(f,-1,sizeof f);\n\tprintf(\"%d\\n\",dp(1,0));\n}", "lang_cluster": "C++", "tags": ["math", "dp", "probabilities"], "code_uid": "361f7ce821653633782e8924d85c0435", "src_uid": "0dc9f5d75143a2bc744480de859188b4", "difficulty": 2200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n int ar[90000];\n int n,d,m,result=0,t;\n cin>>n>>d;\n for(int i=0;i>ar[i];\n }\n cin>>m;\n for(int i=0;iar[j]){\n int t=ar[i];\n ar[i]=ar[j];\n ar[j]=t;\n }\n }\n }\n t=m;\n for(int i=0;i\n#include \n#include \n#include \n#include \n#include \n#include \n#include\nusing namespace std;\nlong long MOD = 1e9 + 7;\nlong long phi(long long n) \n{\n\tlong long result = n;\n\tfor (long long i = 2; i*i <= n; ++i)\n\t\tif (n % i == 0) {\n\t\t\twhile (n % i == 0)\n\t\t\t\tn /= i;\n\t\t\tresult -= result / i;\n\t\t}\n\tif (n > 1)\n\t\tresult -= result / n;\n\treturn result;\n}\n\nint main()\n{\n\tlong long n; long long k; cin >> n >> k;\n\tlong long docnt = (k + 1) / 2;\n\tlong long ans = n;\n\tfor (long long i = 0; i < docnt; ++i)\n\t{\n\t\tif (ans == 1) { cout << \"1\"; return 0; }\n\t\tans = phi(ans);\n\t}\n\tcout << ans % MOD;\n}\n", "lang_cluster": "C++", "tags": ["math", "number theory"], "code_uid": "808bef2ed84823744cb0e09ff8a4f9b4", "src_uid": "0591ade5f9a69afcbecd80402493f975", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n _ _ ___ ___ __ __ \n| | | | |__ \\|__ \\/_ /_ |\n| |__ | |__ ) | ) || || |\n| '_ \\| '_ \\ / / / / | || |\n| |_) | |_) / /_ / /_ | || |\n|_.__/|_.__/____|____||_||_|\n\n*/\n#include\n#define ll long long\n#define pb push_back\n#define INF 9223372036854775807\n#define endl '\\n'\n#define pii pair\n#define vi vector\n#define all(a) (a).begin(),(a).end()\n#define F first\n#define S second\n#define sz(x) (ll int)x.size()\n#define hell 1000000007\n#define rep(i,a,b) for(ll int i=a;i>=1;\n }\n return ans%mod;\n}\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int TESTS=1;\n// cin>>TESTS;\n while(TESTS--)\n {\n ll n,ans;\n cin>>n;\n ll fac[2*n];\n fac[0]=1;\n rep(i,1,2*n)\n {\n fac[i]=(fac[i-1]*i)%hell;\n }\n ans=((2*((fac[2*n-1]*expo((fac[n]*fac[n-1])%hell,hell-2,hell))%hell))%hell-n+hell)%hell;\n cout<\n#define ll long long\n#define vi vector\n#define vl vector\n#define pb push_back\n#define For(i,n) for(int i=0;i>l>>r;\n\tvector two,three;\n\tint cnt=0;\n\tfor(int i=0;i<=31;i++){\n\t\ttwo.pb(1<=l && two[i]*1LL*three[j]<=r){\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t}\n\t}\n\tcout<\nusing namespace std;\n\nconst int N=1003, M=32, B=3, mod=1000000009;\nint n, h, dp[N][B][M][M][M];\n\nint Solve()\n{\n int ret=0;\n dp[0][1][2][2][2]=4;\n for(int i=1;i>n>>h;\n cout<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long LL;\ntypedef long double LD;\n#define pb push_back\n#define mp make_pair\n#define all(v) (v).begin(), (v).end()\n#define sz(v) (int)(v).size()\n\n/*\nint [107], ans;\nvoid rec(int cur)\n{\n if (cur == n + 1)\n {\n vector cnt(n + 1, 0);\n for (int i = 1; i <= n; ++i)\n {\n ++cnt[a[i]];\n }\n int sum = 0;\n for (int i = 1; i <= n; ++i)\n {\n if (cnt[i] == i)\n {\n sum += i;\n }\n }\n if (n - sum == k)\n {\n ++ans;\n }\n return;\n }\n for (int i = 1; i <= n; ++i)\n {\n a[cur] = i;\n rec(cur + 1);\n }\n}\n*/\n/*\n#define MOD 777777777LL\nLL dp[600][600][600], C[600][600];\nint n, k;\n\nvoid calcBinomials(int n = 599)\n{\n int i, j;\n for (i = 0; i <= n; ++i)\n {\n C[i][0] = 1;\n }\n for (i = 1; i <= n; ++i)\n {\n for (j = 1; j <= n; ++j)\n {\n C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;\n }\n }\n}\n\nvoid precalc(int n)\n{\n //dp[answer][all][wrong];\n for (int i = 0; i <= n; ++i)\n {\n for (int j = 0; j <= n; ++j)\n {\n for (int k = 0; k <= n; ++k)\n {\n dp[i][j][k] = 0LL;\n }\n }\n }\n dp[0][0][0] = 1LL;\n for (int i = 1; i <= n; ++i)\n {\n for (int all = 0; all <= n; ++all)\n {\n for (int wrong = 0; wrong <= all; ++wrong)\n {\n if (all >= i) // right answer\n {\n dp[i][all][wrong] += (C[n - all + i][i] * dp[i - 1][all - i][wrong]) % MOD;\n if (dp[i][all][wrong] >= MOD)\n {\n dp[i][all][wrong] -= MOD;\n }\n }\n for (int j = 0; j <= wrong; ++j)\n {\n if (j == i)\n {\n continue;\n }\n dp[i][all][wrong] += (C[n - all + j][j] * dp[i - 1][all - j][wrong - j]) % MOD;\n if (dp[i][all][wrong] >= MOD)\n {\n dp[i][all][wrong] -= MOD;\n }\n }\n }\n }\n }\n}\n*/\n\nint ans[600][600] =\n{\n {1, 0},\n {1, 2, 1},\n {5, 32, 30, 80, 109},\n {541, 6824, 59808, 147224, 415870, 1757896, 1897056, 4898872, 7593125},\n {166257661, 776830421, 290516100, 746623577, 293783147, 33900006, 735127505, 565460332, 428982705, 472062098, 161873957, 117354594, 515619293, 578944191, 312106242, 569389279, 391464593},\n {464207287, 261086313, 584837659, 683961846, 468868529, 211593382, 736955478, 229471758, 157617135, 398169441, 360252438, 629394768, 264125799, 647490480, 342079395, 391579767, 225200475, 486011304, 513156108, 628771752, 132906648, 142138221, 20119449, 444199674, 195188679, 387329805, 44684703, 651912135, 737154512, 612549793, 519860281, 186175544, 212568440},\n {382725349, 240805271, 239509872, 581127897, 6511239, 156126222, 509425833, 672407328, 366667722, 459185405, 509737025, 554790222, 165216555, 703150560, 74806569, 398730015, 383350905, 506108358, 51326142, 298053147, 104256117, 391428765, 374020479, 206607807, 87664059, 275899176, 56407680, 551553401, 448939463, 582889860, 129676638, 226078251, 135769095, 61292868, 578972226, 190181628, 390739055, 184587732, 446575689, 732674124, 232198470, 676760679, 352474101, 611444862, 575661807, 628905585, 320813094, 522840969, 469781928, 156006018, 554473341, 239654268, 643714911, 433540170, 199307003, 496385218, 291740751, 67309914, 370826673, 202356819, 279421821, 421203111, 63744786, 520987612, 550671827},\n {642497124, 482164403, 768209115, 462063756, 154906374, 36099042, 341766705, 678182556, 621882744, 478771358, 231881111, 175889805, 243630450, 168908523, 671961765, 55761813, 652682670, 773939082, 517628076, 756201264, 124604900, 750976272, 498253248, 676047609, 137170026, 705610017, 495032139, 561797418, 703097347, 500815609, 95984586, 739707108, 265613565, 387099846, 777331779, 594676173, 591219559, 407997044, 208947235, 93337440, 478908360, 685013007, 487033953, 671903001, 39521181, 738490312, 33785059, 465470131, 310453920, 54648783, 346831137, 427694175, 474743430, 705296781, 435828036, 429824745, 663532359, 261388683, 244690731, 533997135, 596108961, 506813013, 371892402, 590145264, 104733162, 143420103, 654339672, 700348950, 685038942, 578826927, 286484229, 501639192, 434962491, 299270097, 27702486, 335375775, 111746817, 565603164, 294926121, 676063665, 735862995, 710035809, 437011960, 668528077, 138765186, 508213986, 615036450, 353784942, 624827616, 343900011, 241289776, 52410890, 72018835, 352406796, 415705878, 4802637, 376367145, 65589678, 333633477, 341834527, 303717460, 282387700, 42951006, 254706039, 423048528, 526429710, 68131467, 669954708, 12787348, 500636381, 317959019, 479433192, 657133515, 416259390, 610216692, 340129188, 44594256, 257373347, 138718678, 530767740, 292922628, 37220268, 605295159, 480722613, 458170419, 30540300, 487159055, 232966794, 149150650},\n {507720408, 412133651, 386543325, 139952108, 289303402, 102404925, 317067177, 396414708, 80515854, 663739304, 317300809, 228877044, 493725043, 715317967, 490300965, 315527373, 743539734, 488329191, 553627998, 533025234, 242583957, 706116537, 614109258, 645447222, 523195911, 492109128, 722623041, 111085128, 766395126, 654378921, 691964847, 496688157, 399056049, 654363234, 102052314, 191720088, 473910948, 259736526, 332840025, 388047555, 665791056, 627111387, 139696515, 441456687, 443032569, 283264821, 771641703, 452641455, 511306362, 117572859, 127701891, 721298331, 176520078, 357242229, 611296308, 696994956, 405628839, 429224274, 465336054, 695091546, 689828796, 574648641, 351220905, 507964023, 675326610, 517248963, 453528621, 220301928, 494463186, 681789969, 339589656, 44524053, 417125457, 339589404, 747135963, 341780733, 734158215, 396817281, 21997836, 5728464, 147611205, 456248898, 714128667, 377654949, 3862068, 128418948, 589390074, 304947090, 11703825, 228266073, 127304142, 429215724, 361541124, 521572968, 468358191, 341231688, 65323503, 613778508, 15985323, 291661029, 410970006, 591638112, 349541550, 89967528, 224922159, 361094166, 584206074, 640051812, 324264456, 652625388, 693768537, 11740617, 309238398, 211085469, 194905872, 639416484, 110110707, 296645895, 748118511, 131177718, 511142751, 775975599, 421403409, 475528473, 434685258, 1768977, 80301375, 708023862, 569195676, 56238084, 632887668, 88089750, 631539342, 396695565, 38780154, 695798271, 469819224, 439587099, 69045921, 682966116, 112310856, 64943298, 534475872, 40215357, 389728458, 286368453, 736006257, 501181650, 54829908, 603489402, 338032656, 512182818, 627500097, 462674016, 3103092, 157324491, 43978329, 596818971, 259025598, 9088632, 91991781, 577291428, 211245489, 429471231, 142626330, 172560633, 510907446, 444609585, 758102058, 375112647, 744786693, 276174402, 19259856, 233672418, 745389414, 225772848, 23385663, 324290610, 519804558, 120337812, 402578568, 360676008, 450089262, 551043738, 337388940, 512108856, 28879011, 690040638, 106017282, 558262341, 99972432, 608226003, 612152037, 42414435, 776201013, 39580443, 518796945, 494437752, 583194366, 723936555, 415359657, 309569589, 751104774, 166684527, 249229170, 353120823, 130668327, 753823584, 580966092, 561963717, 543672234, 393846327, 586278000, 327398400, 278403867, 156455586, 363920382, 190245195, 290039148, 547014447, 466218648, 146037150, 585462906, 666008595, 691786683, 374707494, 622498779, 231158277, 685740951, 115612245, 681825249, 545555745, 551718468, 277206615, 640171035, 757727334, 195193908, 658656684, 457760646, 225925875, 505761984, 18685233, 506832921, 112511021, 396846646, 290147622, 113924623, 669986155, 336008070, 63611061, 238586775, 119956662, 616557739, 772784623, 334527774, 410403148, 51933421}\n};\n\n\nint main()\n{\n#ifdef harhro94\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n /*\n calcBinomials();\n for (int i = 0; i <= 8; ++i)\n {\n int n = (1 << i);\n precalc(n);\n printf(\"{\");\n for (int k = 0; k <= n; ++k)\n {\n if (k == n)\n {\n printf(\"%d}\\n\", dp[n][n][k]);\n }\n else\n {\n printf(\"%d, \", dp[n][n][k]);\n }\n }\n }\n */\n int n, k;\n scanf(\"%d%d\", &n, &k);\n int nn;\n for (int i = 0; i <= 8; ++i)\n {\n if ((1 << i) == n)\n {\n nn = i;\n }\n }\n printf(\"%d\\n\", ans[nn][k]);\n\n#ifdef harhro94\n cerr << fixed << setprecision(3) << \"\\nExecution time = \" << clock() / 1000.0 << \"s\\n\";\n#endif\n return 0;\n}", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "cfc3b458363e422d855c3fd29be035d7", "src_uid": "cfe19131644e5925e32084a581e23286", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n#define debug(x) cerr<<#x<<\": \"<>n>>m;\n if(n>m) {\n swap(n,m);\n }\n ll pairs=0;\n for(int i=1;i<=n;i++) {\n if(i<5) {\n ll find = 5 - i;\n pairs += (m-find+5)/5;\n }\n else {\n ll find=(i + 4)/5;\n find*=5;\n find=find-i;\n if(find==0)find=5;\n pairs+= (m-find+5)/5;\n }\n }\n cout<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define INF 1000000001\n#define Pi 3.14159265358\n#define mod9 %1000000009\n#define mod7 %1000000007\n#define time clock()/(double)CLOCKS_PER_SEC\nusing namespace std;\n int a,b,l,i,j,k;\n int prime[1000001],primes[1000001];\n void MakePrime(){\n prime[0]=prime[1]=0;\n for (i=2;i<=1000000;i++){\n if (prime[i]==0){\n for (j=i*2;j<=1000000;j+=i)\n prime[j]=1;\n };\n primes[i]+=primes[i-1]+1-prime[i];\n };\n };\n void BinarySearch(){\n int l=1,r=b-a+1,mid;\n while (l>a>>b>>k;\n MakePrime();\n BinarySearch(); \n return 0;\n}\n", "lang_cluster": "C++", "tags": ["two pointers", "binary search", "number theory"], "code_uid": "d017be1784b08ad3081e15061a66a4a9", "src_uid": "3e1751a2990134f2132d743afe02a10e", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n int n, k; cin >> n >> k;\n if (k % 2 !=0) {\n cout << k / 2 + 1;\n }\n else {\n cout << n / 2 - k / 2 + 1;\n }\n}", "lang_cluster": "C++", "tags": ["math", "constructive algorithms"], "code_uid": "a63b68139e76ec31dd01ce4658a0d975", "src_uid": "aa62dcdc47d0abbba7956284c1561de8", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "/******In the name of ALLAH******/\n#include \n\nusing namespace std;\n\n#define inf 1000000221\n#define MAX 1001101\n#define PI 3.14159265\n\nstruct Point\n{\n\tlong double x,y,z,t,h;\n}a;\n\nvector v;\n\n\nlong double dot(Point &a, Point &b , Point&c){\n\tlong double x1 = a.x -c.x;\n\tlong double x2 = b.x -c.x;\n\tlong double y2 = b.y -c.y;\n\tlong double y1 = a.y -c.y;\n\tlong double z1 = a.z -c.z;\n\tlong double z2 = b.z -c.z;\n\tlong double h1 = a.h -c.h;\n\tlong double h2 = b.h -c.h;\n\tlong double t1 = a.t -c.t;\n\tlong double t2 = b.t -c.t;\n\treturn x1*x2 + y1*y2 + h1*h2 + z1*z2 + t1*t2;\n}\n\nlong double as(Point &a , Point &c){\n\tlong double temp = sqrt(dot(a,a,c));\n\treturn temp;\n}\n\nint main(){\n\tios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\tint n ;\n\tcin>>n;\n\tfor(int i =0 ; i < n ; i++){\n\t\tcin>>a.x>>a.y>>a.z>>a.t>>a.h;\n\t\tv.push_back(a);\n\t}\n\t\n\tif( n == 1){\n\t\tcout<<1<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nlong long mod = 1E9 + 7;\nint main()\n{\n\tint n, a, pos;\n\tcin >> n >> a >> pos;\n\tlong long ans = 1;\n\tint l = 0, r = n, middle = 0,cntb = 0,cntm = 0;\n\twhile (l < r) {\n\t\tmiddle = (l + r) / 2;\n\t\tif (pos < middle) {\n\t\t\tans = (ans * (n - a - cntb)) % mod;\n\t\t\tcntb++;\n\t\t\tr = middle;\n\t\t}\n\t\telse if (pos >= middle){\n\t\t\tif (pos != middle) {\n\t\t\t\tans = (ans * (a - 1 - cntm)) % mod;\n\t\t\t\tcntm++;\n\t\t\t}\n\t\t\tl = middle + 1;\n\t\t}\n\t}\n\tfor (int i = n - 1 - cntb - cntm; i >= 1; i--) {\n\t\tans = (ans * i) % mod;\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["binary search", "combinatorics"], "code_uid": "f13fef5a7491c1619884f2b7ce5d857d", "src_uid": "24e2f10463f440affccc2755f4462d8a", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n\ntypedef long long ll;\n\nconst ll MOD = ll(1e9) + 7;\nconst int ms = 51;\nconst int me = 22;\n\nint n;\n\nint a[ms];\n\nll fexp(ll x, ll e)\n{\n\tll ans = 1;\n\tfor(; e > 0; e /= 2)\n\t{\n\t\tif(e & 1)\n\t\t\tans = ans * x % MOD;\n\t\tx = x * x % MOD;\n\t}\n\treturn ans;\n}\n\nll fat[ms];\nll ifat[ms];\nll comb(int a, int b)\n{\n\tif(a < b || b < 0 || a < 0)\n\t\treturn 0;\n\tll ans = fat[a] * ifat[b] % MOD;\n\treturn ans * ifat[a - b] % MOD;\n}\n\n\nint memo[ms][me][me][2][me][me][2];\nint dp(int on, std::vector prv, std::vector nxt)\n{\n\tint max = prv[0];\n\tint min = prv[0];\n\tint sum = 0;\n\tfor(int i = 0; i < 3; i++)\n\t{\n\t\tmax = std::max(max, std::max(prv[i], nxt[i]));\n\t\tmin = std::min(min, std::min(prv[i], nxt[i]));\n\t\tsum += prv[i] + nxt[i];\n\t}\n\tif(max > me || min < 0)\n\t\treturn 0;\n\tif(on == n)\n\t{\n\t\t//std::cout << \"on \" << on << \", (\" << prv[0] << \", \" << prv[1] << \", \" << prv[2] << \") - (\" << nxt[0] << \", \" << nxt[1] << \", \" << nxt[2] << \")\\n\";\n\t\treturn sum == 0;\n\t}\n\tif(sum == 0)\n\t\treturn 0;\n\tint &ans = memo[on][prv[0]][prv[1]][prv[2]][nxt[0]][nxt[1]][nxt[2]];\n\tif(ans != -1)\n\t\treturn ans;\n\tans = 0;\n\tif(prv[0] + prv[1] + prv[2] == 0)\n\t\treturn ans = dp(on, nxt, prv);\n\t//std::cout << \"on \" << on << \", (\" << prv[0] << \", \" << prv[1] << \", \" << prv[2] << \") - (\" << nxt[0] << \", \" << nxt[1] << \", \" << nxt[2] << \")\\n\";\n\tfor(int i = 0; i <= a[on] && i <= prv[0]; i++)\n\t{\n\t\tll cost1 = comb(prv[0], i);\n\t\tfor(int j = 0; i + j <= a[on] && j <= prv[1]; j++)\n\t\t{\n\t\t\tll cost2 = cost1 * comb(prv[1], j) % MOD;\n\t\t\tfor(int k = 0; i + j + k <= a[on] && k <= prv[2]; k++)\n\t\t\t{\n\t\t\t\tif(i + j + k != 1)\n\t\t\t\t\tcontinue;\n\t\t\t\tll cost3 = cost2 * comb(prv[2], k) % MOD;\n\t\t\t\tfor(int ii = 0; i + j + k + ii <= a[on] && ii <= nxt[0]; ii++)\n\t\t\t\t{\n\t\t\t\t\tll cost4 = cost3 * comb(nxt[0], ii) % MOD;\n\t\t\t\t\tfor(int jj = 0; i + j + k + ii + jj <= a[on] && jj <= nxt[1]; jj++)\n\t\t\t\t\t{\n\t\t\t\t\t\tll cost5 = cost4 * comb(nxt[1], jj) % MOD;\n\t\t\t\t\t\tfor(int kk = 0; i + j + k + ii + jj + kk <= a[on] && kk <= nxt[2]; kk++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tll cost6 = cost5 * comb(nxt[2], kk) % MOD;\n\t\t\t\t\t\t\t//std::cout << \"\\non \" << on << \", (\" << prv[0] << \", \" << prv[1] << \", \" << prv[2] << \") - (\" << nxt[0] << \", \" << nxt[1] << \", \" << nxt[2] << \")\\n\";\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tprv[0] += j - i;\n\t\t\t\t\t\t\tprv[1] += k - j;\n\t\t\t\t\t\t\tprv[2] += -k;\n\t\t\t\t\t\t\tnxt[0] += jj - ii;\n\t\t\t\t\t\t\tnxt[1] += kk - jj;\n\t\t\t\t\t\t\tnxt[2] += -kk;\n\t\t\t\t\t\t\t//std::cout << \"cost is \" << cost6 << \", (\" << i << \", \" << j << \", \" << k << \", \" << ii << \", \" << jj << \", \" << kk << \")\\n\";\n\t\t\t\t\t\t\tif(i + j + k + ii + jj + kk < a[on])\n\t\t\t\t\t\t\t\tnxt[a[on] - (i + j + k + ii + jj + kk) - 1]++;\n\t\t\t\t\t\t\tans = (ans + cost6 * dp(on + 1, prv, nxt)) % MOD;\n\t\t\t\t\t\t\tif(i + j + k + ii + jj + kk < a[on])\n\t\t\t\t\t\t\t\tnxt[a[on] - (i + j + k + ii + jj + kk) - 1]--;\n\t\t\t\t\t\t\tprv[0] -= j - i;\n\t\t\t\t\t\t\tprv[1] -= k - j;\n\t\t\t\t\t\t\tprv[2] -= -k;\n\t\t\t\t\t\t\tnxt[0] -= jj - ii;\n\t\t\t\t\t\t\tnxt[1] -= kk - jj;\n\t\t\t\t\t\t\tnxt[2] -= -kk;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t//std::cout << \"on \" << on << \", (\" << prv[0] << \", \" << prv[1] << \", \" << prv[2] << \") - (\" << nxt[0] << \", \" << nxt[1] << \", \" << nxt[2] << \") ans is \" << ans << \"\\n\";\n\treturn ans;\n}\n\nint main()\n{\n\tfat[0] = ifat[0] = 1;\n\tfor(int i = 1; i < ms; i++)\n\t{\n\t\tfat[i] = fat[i - 1] * i % MOD;\n\t\tifat[i] = fexp(fat[i], MOD - 2);\n\t}\n\tstd::cin >> n;\n\tfor(int i = 0; i < n; i++)\n\t\tstd::cin >> a[i];\n\tmemset(memo, -1, sizeof memo);\n\ta[0]++;\n\tstd::vector prv, nxt;\n\tprv = nxt = std::vector(3, 0);\n\tprv[0] = 1;\n\tstd::cout << dp(0, prv, nxt) << '\\n';\n}", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "graphs", "shortest paths"], "code_uid": "dfecd2f1276b1d36138643b0bf0f1808", "src_uid": "db884d679d9cfb1dc4bc511f83beedda", "difficulty": 2600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nlong long a,b,c,d,mid,n,ans=999999999999;\nint main(){\n\tcin>>n;\n\tc=0;\n\td=n+2;\n for(long long i=0;i<90000000;i++)\n i++;\n\twhile(true)\n {\n\t\tif(c+1>=d) \n break;\n\t\tmid=(c+d)/2;\n\t\ta=mid/2;\n\t\tb=mid-a;\n\n\t\tif(a*b>=n){\n\t\t\tif(ans>mid) \n \n ans=mid;\n\t\t\td=mid;\n\n\t\t}\n \n else\n \n {\n\t\t\tc=mid;\n\t\t}\n\t}\n\tcout<\n#include \n#include \n\nint main(){\n int a, ta, b, tb;\n while(scanf(\"%d%d%d%d\", &a, &ta, &b, &tb) > 0){\n \n char aux[10];\n scanf(\"%s\", aux);\n \n char* str = strtok(aux, \":\");\n int depa = atoi(str) * 60;\n str = strtok(NULL, \"\");\n depa += atoi(str);\n \n int depb = 5*60;\n while(depb < 24*60 and depb+tb <= depa) depb += b;\n \n int qtd = 0;\n while(depb < 24*60 and depb < depa+ta) depb += b, qtd++;\n \n printf(\"%d\\n\", qtd);\n }\n}\n", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "5d0ac76e376f8c9a974f857e6c8683dc", "src_uid": "1c4cf1c3cb464a483511a8a61f8685a7", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \nusing namespace std;\nlong long int n,county,kolvo,k,i;\nint a[1000];\nint main()\n{ \n\tcin >> n >> k;\n\tcounty = 2;\n\twhile (county <= sqrt(n)) {\n\t\tif (n % county == 0) {\n\t\t\tn /= county;\n\t\t\tkolvo++;\n\t\t\ta[kolvo] = county;\n\t\t}\n\t\telse { county++; }\n\t}\n\ta[kolvo + 1] = n;\n\tkolvo++;\n\tif (kolvo < k) {\n\t\tcout << -1;\n\t}\n\telse {\n\t\twhile (kolvo != k) {\n\t\t\ta[kolvo - 1] *= a[kolvo];\n\t\t\tkolvo --;\n\t\t}\n\t\tfor (i = 1; i <= k; i++) {\n\t\t\tcout << a[i] << \" \";\n\n\t\t}\n\t}\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "number theory"], "code_uid": "413dfa2b4994e523ff653b4665a5b8d7", "src_uid": "bd0bc809d52e0a17da07ccfd450a4d79", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n\nint max,n,i;\n\nint enBuyuk(int x){\n \n\tmax = 0;\n\twhile(x){\n\t\tint a = x%10;\n\t\tif(a > max)\n\t\t\tmax = a;\n\t\tx /= 10;\n\t}\n\n\treturn max;\n}\n\nint main(){\n\n\tscanf(\"%d\",&n);\n\n\tint s = 0;\n\twhile(n > 0){\n\t\tn -= enBuyuk(n);\n\t\ts++;\n\t}\n \n printf(\"%d\",s);\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dp"], "code_uid": "2aadbd250cd38105e9a72b7f23bb4fde", "src_uid": "fc5765b9bd18dc7555fa76e91530c036", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#define INF 2000000000\ndouble X1, Y1, X2, Y2;\ndouble Xw1, Yw1,Xw2, Yw2, Xm1, Ym1, Xm2, Ym2;\ndouble A, B, C;\ndouble Aw, Bw, Cw;\ndouble Am, Bm, Cm;\nvoid ecuatie_dreapta(double x1, double y1, double x2, double y2, double &A, double &B, double &C){\n\tA = y1-y2;\n\tB = x2-x1;\n\tC = x1*y2 - x2*y1;\n}\ndouble semn_segment(double A, double B, double C, double x, double y){\n\tif(A*x + B * y + C > 0) return 1;\n\tif(A*x + B*y + C < 0) return -1;\n\treturn 0;\n}\nint egal_drepte(double A, double B, double C, double A1, double B1, double C1){\n\tif(A1 * A == 0 && A1 + A != 0) return 0;\n\tif(B1 * B == 0 && B1 + B != 0) return 0;\n\tif(C1 * C == 0 && C1 + C != 0) return 0;\n\tdouble v[6]; int nr = 0;\n\tif(A) v[++nr] = A1 / A;\n\tif(B) v[++nr] = B1 / B;\n\tif(C) v[++nr] = C1 / C;\n\tfor(int i = 1; i < nr; ++i)\n\t\tif(v[i] != v[i+1]) return 0;\n\treturn 1;\n}\nint if_intersectie(double x1, double y1, double x2, double y2, double xp1,double yp1,double xp2,double yp2, int R){\n\tdouble A, B, C, Ap, Bp, Cp;\n\t//R = 0;\n\tecuatie_dreapta(x1,y1,x2,y2,A,B,C);\n\tecuatie_dreapta(xp1,yp1,xp2,yp2,Ap,Bp,Cp);\n\tif(egal_drepte(A , B, C, Ap, Bp, Cp)) \n\t\tif(R && ((x1 <= xp1 && xp1 <= x2) || (x2 <= xp1 && xp1 <= x1)) ) return 1;\t\n\t\telse return 0;\n\tif((double)semn_segment(A, B, C, xp1, yp1) * semn_segment(A,B,C,xp2,yp2) > 0.0) return 0;\n\tif((double)semn_segment(Ap,Bp,Cp,x1,y1) * semn_segment(Ap,Bp,Cp,x2,y2) > 0.0) return 0;\n\treturn 1;\n}\nvoid perpendicular(double x, double y, double &xp, double &yp){\n\tdouble A = Xm2 - Xm1, B = Ym2-Ym1, C = x*(Xm1-Xm2) + y*(Ym1-Ym2);\n\typ = (Am*C - A * Cm) / (A*Bm - Am*B);\n\txp = (Bm*C - B * Cm) / (B * Am - Bm*A);\n}\nvoid pcte_inters(double A1,double B1, double C1, double A2, double B2, double C2, double &xsol, double &ysol){\n\tysol = (A2*C1- A1*C2) / (A1*B2 - A2*B1);\n\txsol = (B2*C1 - B1*C2) / (B1 *A2 - B2*A1);\n}\nint main(){\n\t//freopen(\"E.in\" ,\"r\", stdin);\n\t//freopen(\"E.out\", \"w\", stdout);\n\tscanf(\"%lf%lf%lf%lf\", &X1, &Y1, &X2, &Y2);\n\tscanf(\"%lf%lf%lf%lf\", &Xw1, &Yw1, &Xw2, &Yw2);\n\tscanf(\"%lf%lf%lf%lf\", &Xm1, &Ym1, &Xm2, &Ym2);\n\tecuatie_dreapta(X1, Y1, X2, Y2, A, B , C);\n\tecuatie_dreapta(Xw1, Yw1, Xw2, Yw2, Aw, Bw, Cw);\n\tecuatie_dreapta(Xm1, Ym1, Xm2, Ym2, Am, Bm, Cm);\n\tif(if_intersectie(X1, Y1, X2, Y2, Xm1, Ym1, Xm2, Ym2,0)){\n\t\tprintf(\"NO\");\n\t\treturn 0;\n\t}\n\tif(!if_intersectie(X1, Y1, X2, Y2, Xw1, Yw1, Xw2, Yw2,1)){\n\t\tprintf(\"YES\");\n\t\treturn 0;\n\t}\n\tif((egal_drepte(A,B,C,Am,Bm,Cm) || semn_segment(Am, Bm, Cm, X1,Y1) != semn_segment(Am, Bm, Cm, X2, Y2)) || (semn_segment(Am, Bm, Cm,X1,Y1) == 0 && semn_segment(Am,Bm,Cm,X2,Y2)==0)){\n\t\tprintf(\"NO\");\n\t\treturn 0;\n\t}\n\t/*else if( if_intersectie(X1, Y1, X2, Y2, Xw1, Yw1, Xw2, Yw2,1) ){\n\t\tprintf(\"YES\");\n\t\treturn 0;\n\t}*/\n\tdouble X1perp, Y1perp;\n\tperpendicular(X1, Y1, X1perp, Y1perp);\n\tdouble X1prim = X1perp + (X1perp - X1), Y1prim = Y1perp + (Y1perp-Y1);\n\t//if(if_intersectie(X2, Y2, X1prim, Y1prim, Xw1, Yw1, Xw2, Yw2,1)) {printf(\"NO\"); return 0;}\n\t//if(X2 == X1prim && Y2 == Y1prim) {printf(\"NO\"); return 0;}\n\t\n\tdouble Aseg,Bseg,Cseg;\n\tecuatie_dreapta(X1prim, Y1prim, X2, Y2, Aseg, Bseg,Cseg);\n\tdouble xinters,yinters;\n\t\n\tpcte_inters(Am,Bm, Cm, Aseg,Bseg, Cseg,xinters,yinters);\n\tif( !( ((Xm1 <= xinters && xinters <= Xm2) || (Xm2 <= xinters && xinters <= Xm1)) && ((Ym1 <= yinters && yinters <= Ym2) || (Ym2 <= yinters && yinters <= Ym1)) ) ) {printf(\"NO\");return 0;}\n\t\n\tif(if_intersectie(X1, Y1, xinters,yinters, Xw1, Yw1, Xw2, Yw2,1)) {printf(\"NO\"); return 0;}\n\tif(if_intersectie(X2, Y2, xinters,yinters,Xw1,Yw1,Xw2,Yw2,1)) {printf(\"NO\"); return 0;}\n\tprintf(\"YES\");\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["geometry", "implementation"], "code_uid": "f256352c9b03bc223c390861e6bb49ee", "src_uid": "7539a41268b68238d644795bccaa0c0f", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \n#include \n#include \nint n; // number of songs;\nint T; // total expected length of playlist\nconst int max_n = 15; // max num of songs\n\nstruct song{\n int len; //(1-15)\n int genre; // 1-3\n};\n\nstruct song songs[max_n];\nbool song_mask[max_n];\nlong long int possible = 0;\n\nint playlist[max_n];\n\nlong long int genre_possibilities;\nint genre_ll;\n\nint genre_order_plist[max_n];\n\n\nvoid genre_order_possibilities(int songs_so_far)\n{\n for (int i = 0 ; i!= genre_ll; ++i)\n {\n // if (songs_so_far <1 )\n //printf(\"(%d) i:%d. ll%d\\n\", songs_so_far, i, genre_ll);\n if (! song_mask[i] && ( (!songs_so_far) || songs[playlist[i]].genre != genre_order_plist[songs_so_far-1]) )\n\t{\n\t if ( songs_so_far+1 == genre_ll)\n\t {\n\t genre_possibilities++;\n\t }\n\t else \n\t {\n\t song_mask[i] = true;\n\t genre_order_plist[songs_so_far] = songs[playlist[i]].genre;\n\t genre_order_possibilities(songs_so_far+1);\n\t song_mask[i] = false;\n\t }\n\t}\n }\n\n}\n\nconst int tmax = 14348907;\nlong long int checklist [tmax];;\n\nvoid count_playlist_permutations_real(int n_s)\n{\n /* now playlist has playlist of desirable length, but not sure if desirable mix of genres */\n //puts(\"o2\");\n\n //printf(\"ll:%d\\n\", n_s);\n \n genre_ll = n_s;\n genre_possibilities = 0;\n genre_order_possibilities(0);\n //possible+= genre_possibilities;\n \n}\n\n\nvoid count_playlist_permutations(int n_s)\n{\n int ternary = 0;\n int ternary_base = 1;\n\n int genres[3] = {0,0,0};\n int tmp[15];\n int i;\n //puts(\"ordcnt\");\n for (i = 0 ; i!= n_s ; ++i) {\n genres[songs[playlist[i]].genre-1]++;\n //printf(\"%d \",playlist[i]);\n }\n //putchar(10);\n\n\n std::sort(genres, genres+3);\n if (genres[2] > (n_s+1)/2) return;\n\n\n\n for (i = 0 ; i != genres[0] ; ++i)\n tmp[i] = 0;\n for (; i != genres[0] + genres[1] ; ++i)\n tmp[i] = 1;\n for (; i!= n_s ; ++i)\n tmp[i] = 2;\n \n for (int i = 0 ; i!= n_s ; ++i) {\n ternary += tmp[i]*ternary_base;\n ternary_base *= 3;\n }\n\n if (checklist[ternary] == -1)\n {\n count_playlist_permutations_real(n_s);\n checklist[ternary] = genre_possibilities;\n //printf(\"checklist[%d] = %ld;\\n\", ternary, genre_possibilities% 1000000007UL);\n }\n\n possible+=checklist[ternary];\n if (possible > 1000000007ULL)\n possible %= 1000000007ULL;\n\n \n}\n\nvoid search_pos(int len_so_far, int songs_so_far)\n{\n // int lsf = len_so_far;\n int prev_song = 0;\n if (songs_so_far)\n {\n prev_song = playlist[songs_so_far-1]+1;\n }\n\n \n for (int i = prev_song ; i!= n; ++i)\n {\n //if (! song_mask[i] && ( (!len_so_far) || songs[i].genre != songs[playlist[songs_so_far-1]].genre) )\n\t{\n\t if ( songs[i].len+len_so_far == T)\n\t {\n\t playlist[songs_so_far] = i;\n\t count_playlist_permutations(songs_so_far+1);\n\t // possible++;\n\t // if (possible == 1000000007)\n\t // \tpossible = 0;\n\t }\n\t else if ( songs[i].len+len_so_far <= T)\n\t {\n\t //song_mask[i] = true;\n\t playlist[songs_so_far] = i;\n\t search_pos(songs[i].len+len_so_far, songs_so_far+1);\n\t //song_mask[i] = false;\n\t }\n\t}\n }\n}\n\nint main(int argc, char ** argv)\n{\n scanf ( \"%d %d\\n\" , &n, &T);\n\n\n for (int i = 0 ; i!= n ; ++i)\n {\n scanf ( \"%d %d\\n\" , & (songs [ i ] . len ) , & (songs [ i ] . genre ));\n }\n memset(checklist, 0xff, 14348907*sizeof(long long));\n checklist[14319261] = 420863916;//12420864000UL;\n checklist[1593225] = 35596800;\n checklist[530334] = 10160640;\n checklist[1591029] = 88646400;\n checklist[1593945] = 3628800;\n checklist[4781871] = 123379200;\n checklist[4779675] = 596160000;\n checklist[4773087] = 115596793;\n checklist[1591002] = 115292160;\n checklist[14347813] = 203212800;\n checklist[14345625] = 66867193;\n\n search_pos(0,0);\n std::cout << possible << std::endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dp", "combinatorics", "bitmasks"], "code_uid": "f511880b8abc552824c73476175f473c", "src_uid": "ac2a37ff4c7e89a345b189e4891bbf56", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define N 100005\n#define int long long\nusing namespace std;\n\ntemplate \n\nvoid read(T &a)\n{\n\tT x = 0,f = 1;\n\tchar ch = getchar();\n\twhile (ch < '0' || ch > '9')\n\t{\n\t\tif (ch == '-') f = -1;\n\t\tch = getchar();\n\t}\n\twhile (ch >= '0' && ch <= '9')\n\t{\n\t\tx = (x << 1) + (x << 3) + (ch ^ 48);\n\t\tch = getchar();\n\t}\n\ta = x * f;\n}\n\ntemplate void read(T &t,Args &... args) {read(t); read(args...); }\n\nint n,a[1000005],fac[25];\n\nsigned main()\n{\n\tfac[0] = 1;\n\tfor (int i = 1; i <= 20; i++) fac[i] = fac[i - 1] * i;\n\tint t = 1;\n\twhile (t--)\n\t{\n\t\tread(n);\n\t\tprintf(\"%lld\\n\",fac[n] / fac[n / 2] / fac[n / 2] * fac[n / 2 - 1] * fac[n / 2 - 1] / 2);\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "db928d90bb7e0f980468ff05d06d204f", "src_uid": "ad0985c56a207f76afa2ecd642f56728", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "//\n/*==========================================================*/\n/* Template ver 2017-0720 | Created by JollyBee */\n/* DOMINUS pascit me, et nihil mihi deerit (Psalmorum 23:1) */\n/*==========================================================*/\n#include\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector vi;\ntypedef pair pii;\ntypedef pair pll;\ntypedef vector vii;\nconst double EPS=(double)1e-9;\nconst double PI=(double)acos(-1.0);\n#define fi first\n#define se second\n#define mp make_pair\n#define pb push_back\n#define pob pop_back\n#define pf push_front\n#define pof pop_front\n#define FOR(i,n) for(int i=0;i=0;i--)\n#define REVV(i,l,r,c) for(int i=l;i>=r;i-=c)\n#define REV(i,l,r) REVV(i,l,r,1)\n//random\nint irand(int lo, int hi){\n return (((double)rand())/(RAND_MAX+1.0)) * (hi-lo+1) + lo;\n}\n//ll to string\nstring toString(ll x) {\n stringstream ss;\n ss << x;\n return ss.str();\n}\n//string to ll\nll toNumber(string S) {\n ll ret;\n sscanf(S.c_str(),\"%lld\",&ret);\n return ret;\n}\n// std::fill(start, end, value);\n// for(auto it: DS){}\n\nconst int INF=(int)2e9;\nconst ll MOD=(ll)1e9+7;\n/*==========================================================*/\n/* END OF TEMPLATE */\n/* DOMINUS pascit me, et nihil mihi deerit (Psalmorum 23:1) */\n/*==========================================================*/\n\nll dat[15];\n\nint dp[10005];\nll solve(const ll& w, const ll& summ){\n if(summ <= w) return summ;\n //do: target -= 400\n ll target = max((ll)0, w-(ll)400);\n REV(num,8,1){\n ll ret = min(dat[num], target/(ll)num);\n ret = max((ll)0, ret-100);\n dat[num] -= ret;\n target -= (ll)num * ret;\n }\n //change target\n int _w = (int)min(w, (ll)400 + target);\n //maximize\n REP(num,1,8) dat[num] = min(dat[num], (ll)1e4);\n //reset\n FOR(i,10003) dp[i] = (i==0);\n //knapsack\n REP(num,1,8) for(; dat[num] > 0; dat[num]--){\n //fill\n REV(i,_w,num) dp[i] |= dp[i-num];\n }\n //find\n int maxx = 0;\n FOR(i,10003) if(dp[i] == 1) maxx = i;\n //return\n return w - (ll)(_w-maxx);\n}\n\nint main(){\n ll summ = (ll)0;\n //input\n ll w; scanf(\"%lld\", &w);\n REP(i,1,8) scanf(\"%lld\", dat+i), summ += (ll)i * dat[i];\n //print\n printf(\"%lld\\n\", solve(w, summ));\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "greedy", "dp"], "code_uid": "4c438f1ca996d6eede4be1f266329506", "src_uid": "8097e10157320524c0faed56f2bc4880", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nint main()\n{\n long long n;\n long long b=0,c=580000000,t,g,u;\n long long k;\n cin>>u;\n if(u)\n {\n while(1)\n {\n t=(c+b)/2;\n g=((t+1)*t)/2;\n if(g*6\n#include \n#include \nusing namespace std;\nint main()\n{\n\tint n,m,a[20][20]={0},ans=0;\n\tcin>>n>>m;\n\tstring s[20],k;\n\tfor(int i=1;i>k;\n\t\ts[i]+=k;\n\t\ts[i]+=' ';\n\t}\n\ts[0]=\"aaaaaaaaaaaaaaaaaaaaaaaa\";\n\ts[n+1]=\"aaaaaaaaaaaaaaaaaaaaaaa\";\n\tfor(int i=1;i\n#include \n#include \nusing namespace __gnu_pbds;\nusing namespace std;\n#define int long long int\n#define ull unsigned int\n#define ld long double\n#define pb push_back\n#define pa pair\n#define ppa pair,int>\n#define pap pair>\n#define v1d vector\n#define v2d vector >\n#define vpa vector>\n#define M 1000000007\n#define MN LLONG_MIN\n#define MX LLONG_MAX\n#define ff first\n#define ss second\n#define endl \"\\n\"\n#define v1s vector\n#define all(v) v.begin(),v.end()\n#define S(v) sort(v.begin(),v.end())\n#define RS(v) sort(v.rbegin(),v.rend())\n#define R(v) reverse(v.begin(),v.end())\n#define mxpq(T) priority_queue \n#define mnpq(T) priority_queue,greater>\n#define T int tc;cin>>tc;while(tc--)\n#define p2d(v) for(auto a:v){for(auto b:a)cout<,rb_tree_tag,tree_order_statistics_node_update> oset;\n#define qqq 400005\nint n,m,x,y,a,b;\nint fun(int k){\n if(a*k<=n&&b*k<=m)return 1;return 0;\n}\n\nvoid Weapons19(){\n cin>>n>>m>>x>>y>>a>>b;\n int g=__gcd(a,b);a/=g;b/=g;\n int s=1,e=1e9;\n while(s!=e){\n int k=(s+e+1)/2;\n if(fun(k))s=k;\n else e=k-1;\n\n }\n a*=s;b*=s;\n int x1,y1,x2,y2;\n x1=max(0ll,x-(a+1)/2);\n x2=x1+a;\n if(x2>n){\n x2=n;\n x1=n-a;\n }\n y1=max(0ll,y-(b+1)/2);\n y2=y1+b;\n if(y2>m){\n y2=m;\n y1=m-b;\n }\n cout<\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint n,m,k;\n\nint main()\n{\n\tscanf(\"%d%d%d\",&n,&m,&k);\n\tif(m \nusing namespace std; \nint main()\n{\n\tint A,sum=0,B;\n\tcin>>A;\n\tB=A;\n\t\n\t\n\t\t\n\twhile(1)\n\t{\n\t\t\n\t\twhile(B!=0)\n\t\t{\n\t\t\tsum+=\tB%10;\n\t\t\tB=B/10;\n\t\t}\n\t\tif(sum%4==0)\n\t\t{\n\t\t\tcout<\nusing namespace std;\ntypedef long long ll;\nint main() {\n ll a , b , c, d;cin >> a >> b >> c >> d;\n return cout << ( max(max(a,b),c) - ( ((a <= c && a >= b)|| (a <= b && a >= c)) ? a : ((b <= a && b >= c)|| (b <= c && b >= a)) ? b : c ) < d ? (d - (max(max(a,b),c)-( ((a <= c && a >= b)|| (a <= b && a >= c)) ? a : ((b <= a && b >= c)|| (b <= c && b >= a)) ? b : c ))) : 0) + ( ( ((a <= c && a >= b)|| (a <= b && a >= c)) ? a : ((b <= a && b >= c)|| (b <= c && b >= a)) ? b : c ) - min(min(a,b),c) < d ? (d - (( ((a <= c && a >= b)|| (a <= b && a >= c)) ? a : ((b <= a && b >= c)|| (b <= c && b >= a)) ? b : c )-min(min(a,b),c))) : 0) << endl, 0;\n}\n \n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "09eb3ef99eb3c1c11b93cd8ffec4d00c", "src_uid": "47c07e46517dbc937e2e779ec0d74eb3", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\n#include\nusing namespace std;\n\nint c[11], m[11], p[11];\n\nvoid as(string t)\n{\n\tif (t[1] == 's') c[t[0] - '0'] += 1;\n\telse if (t[1] == 'm')m[t[0] - '0'] += 1;\n\telse p[t[0] - '0'] += 1;\n}\n\nint ret()\n{\n\tint i, j;\n\tfor (i = 1; i <= 9; ++i)\n\t{\n\t\tif (c[i] == 3 || m[i] == 3 || p[i] == 3) return 0;\n\t}\n\tfor (i = 1; i <= 9; ++i)\n\t{\n\t\tif (c[i] == 2 || m[i] == 2 || p[i] == 2) return 1;\n\t}\n\n\tint sign, flag = 0;\n\tfor (i = 1; i <= 7; ++i)\n\t{\n\t\tsign = 0;\n\t\tif (c[i]) sign++;\n\t\tif (c[i+1]) sign++;\n\t\tif (c[i+2]) sign++;\n\n\t\tif (sign == 3) return 0;\n\t\telse if (sign == 2) flag = 1;\n\t}\n\tfor (i = 1; i <= 7; ++i)\n\t{\n\t\tsign = 0;\n\t\tif (m[i]) sign++;\n\t\tif (m[i + 1]) sign++;\n\t\tif (m[i + 2]) sign++;\n\n\t\tif (sign == 3) return 0;\n\t\telse if (sign == 2) flag = 1;\n\t}\n\tfor (i = 1; i <= 7; ++i)\n\t{\n\t\tsign = 0;\n\t\tif (p[i]) sign++;\n\t\tif (p[i + 1]) sign++;\n\t\tif (p[i + 2]) sign++;\n\n\t\tif (sign == 3) return 0;\n\t\telse if (sign == 2) flag = 1;\n\t}\n\tif (flag) return 1;\n\telse return 2;\n}\n\nint main() \n{\n\tstring word1, word2, word3;\n\tcin >> word1 >> word2 >> word3;\n\tas(word1);\n\tas(word2);\n\tas(word3);\n\n\tcout << ret();\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "fbe4bdfffcd26e3dac275ae90cdfd992", "src_uid": "7e42cebc670e76ace967e01021f752d3", "difficulty": 1200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n#include\n#include\n#include\nusing namespace std;\nconst int inf=0x3f3f3f3f;\nchar mp[30][30];\nint n;\nint a[50][26];\nint dp[50][(1<<21)];\nint dfs(int dep,int sta)\n{\n int num=0;\n if(dp[dep][sta]!=inf /*|| dp[dep][sta]!=-inf*/)return dp[dep][sta];\n for(int i=0,k=dep;i>i &1)\n {\n if(mp[k][i]=='a')num++;\n else if(mp[k][i]=='b')num--;\n break;\n }\n }\n if(dep>=2*n-2)return dp[dep][sta]=num;\n\n int &tmp=dp[dep][sta];\n if(dep%2!=0)\n {\n tmp=-inf;\n for(int i=0;i<26;i++)\n {\n int next=a[dep+1][i]&(sta|(sta<<1));\n if(next==0)continue;\n tmp=max(tmp,dfs(dep+1,next)+num);\n }\n }\n else\n {\n for(int i=0;i<26;i++)\n {\n int next=a[dep+1][i]&(sta|(sta<<1));\n if(next==0)continue;\n tmp=min(tmp,dfs(dep+1,next)+num);\n }\n }\n return tmp;\n}\n\nvoid pre()\n{\n for(int i=0; i<2*n-1; i++)\n {\n int k=i;\n for(int j=0; j>n)\n {\n for(int i=0;i>mp[i];\n }\n pre();\n for(int i=0;i<2*n;i++)\n for(int j=0;j<(1<0)\n {\n puts(\"FIRST\");\n continue;\n }\n if(res<0)\n {\n puts(\"SECOND\");\n continue;\n }\n puts(\"DRAW\");\n\n }\n}\n", "lang_cluster": "C++", "tags": ["games", "dp", "bitmasks"], "code_uid": "9fa03a112249e3cbdbde83d80b83144e", "src_uid": "d803fe167a96656f8debdc21edd988e4", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n#define sc(x) scanf(\"%d\",&x)\n#define scl(x) scanf(\"%lld\",&x)\n#define scs(x) scanf(\"%s\",x)\n#define mod 1000000007\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define pi pair \n#define pl pair\nint main(){\n\tint n,m,x,y;\n\tll k;\n\tsc(n);\n\tsc(m);\n\tscl(k);\n\tsc(x);\n\tsc(y);\n\tll mini=1ll*mod*mod,maxi=-1,ser=1,a;\n\tif(n==1){\n\t\ta=m;\n\t\tmini=k/a;\n\t\tk=k%a;\n\t\tif(k!=0)\n\t\tmaxi=mini+1;\n\t\telse\n\t\tmaxi=mini;\n\t\tif(y<=k)\n\t\tser=maxi;\n\t\telse\n\t\tser=mini;\n\t\tcout<0;i++){\n\t\t\tfor(int j=0;j0;j++){\n\t\t\t\tans[i][j]++;\n\t\t\t\tk--;\n\t\t\t}\n\t\t}\n\t\tfor(int i=n-2;i>=0 && k>0;i--){\n\t\t\tfor(int j=0;j0;j++){\n\t\t\t\tans[i][j]++;\n\t\t\t\tk--;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=n-2;i++){\n\t\t\tfor(int j=0;j\nusing namespace std;\nlong long n,i,t,ans,a,b,j,m;\nint main()\n{\n cin >> n;\n m = n;\n if(n % 2 != 0)\n {\n cout << 0;\n return 0;\n }\n if(n % 4 != 0)\n {\n cout << n / 4;\n return 0;\n }\n cout << n / 4 - 1;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "3b6a54d473c8f8f1b11f0e79e972cadb", "src_uid": "32b59d23f71800bc29da74a3fe2e2b37", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \nusing namespace std;\nint main() {\n long long x,y,a,b;\n cin>>x>>y>>a>>b;\n cout<\n\nusing namespace std;\n\n#define int long long\n#define double long double\n\nconst int MAXN = 1e6;\nconst int MOD = 1e9 + 7;\n\nbool tree = true;\nset xs, ys;\n\nint binpow(int a, int p) {\n if (p == 1)\n return a % MOD;\n\n if (!p)\n return 1;\n\n if (p & 1)\n return (binpow(a, p - 1) * a) % MOD;\n else\n return (binpow(a, p / 2) * binpow(a, p / 2)) % MOD;\n}\n\nvector used(MAXN, false);\n\nmap>> x, y;\nmap> cbyid;\n\nvector> g(MAXN);\n\nvoid dfs(int v, int p = -1) {\n if (used[v]) {\n if (used[v] == 1) {\n tree = false;\n }\n used[v] = 2;\n return;\n }\n\n used[v] = true;\n auto coords = cbyid[v];\n xs.insert(coords.first);\n ys.insert(coords.second);\n\n for (auto i : g[v])\n if (i != p)\n dfs(i, v);\n\n used[v] = 2;\n}\n\nsigned main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n int n, id = 0;\n cin >> n;\n\n for (int i = 0; i < n; ++i) {\n int xx, yy;\n cin >> xx >> yy;\n cbyid[id] = {xx, yy};\n x[xx].insert({yy, id});\n y[yy].insert({xx, id++});\n }\n\n for (auto i : y)\n for (set>::iterator j = i.second.begin(); j != (i.second.end()); ++j)\n if (next(j) != i.second.end())\n g[j->second].push_back(next(j)->second), g[next(j)->second].push_back(j->second);\n\n for (auto i : x)\n for (set>::iterator j = i.second.begin(); j != (i.second.end()); ++j)\n if (next(j) != i.second.end())\n g[j->second].push_back(next(j)->second), g[next(j)->second].push_back(j->second);\n\n int ans = 1;\n\n for (int i = 0; i <= id - 1; ++i) {\n dfs(i);\n\n //scout << xs.size() << ' ' << ys.size() << endl;\n\n ans *= max(1ll, (binpow(2, max(static_cast(0), xs.size() + ys.size())) - tree)) % MOD;\n ans %= MOD;\n\n tree = true;\n xs.clear(), ys.clear();\n }\n\n cout << ans;\n\n\n return 0;\n}\n\n\n", "lang_cluster": "C++", "tags": ["dsu", "graphs", "trees", "dfs and similar"], "code_uid": "0419826579a91c5c98ec9c892e90eea8", "src_uid": "8781003d9eea51a509145bc6db8b609c", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n\n#define N 250005\n\n#define fi first\n\n#define sc second\n\nusing namespace std;\n\ntypedef pair II;\n\nint amount , moder;\n\nint factor[N];\n\nvoid prepare()\n{\n\n factor[0] = 1;\n\n for(int i =1 ;i <= amount; i ++)\n\n factor[i] = (long long)factor[i-1]*i % moder;\n}\n\nvoid solve()\n{\n\n prepare();\n\n int result = 0;\n\n for(int dist = 0 ; dist < amount; dist++){\n\n int choose = (long long)(amount-dist )*(amount - dist)%moder;\n\n int some = (long long)factor[dist+1] * factor[ amount - dist -1 ] %moder;\n\n result = ( result + (long long)choose*some%moder )%moder;\n }\n\n printf(\"%d\", result );\n}\n\nint main()\n{\n //freopen(\"main.inp\", \"r\", stdin );\n\n scanf(\"%d %d\", &amount , &moder );\n\n solve();\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "b19d1aa50888e86d7ddd91df7d486ace", "src_uid": "020d5dae7157d937c3f58554c9b155f9", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#define x first\n#define y second\n#define pb push_back\nusing namespace std;\nusing DO = double;\nusing LL = long long;\nusing VI = vector;\nusing pii = pair;\nconst int mod = 1e9+7;\nint power(int a, int b, int c=mod) {\n\tint ans=1;\n\tfor(; b; b>>=1, a=(LL)a*a%c) {\n\t\tif(b&1) ans=(LL)ans*a%c;\n\t}\n\treturn ans;\n}\nint a[111];\nint main() {\n#ifndef ONLINE_JUDGE\n\tfreopen(\"in.in\", \"r\", stdin);\n\tfreopen(\"out.out\", \"w\", stdout);\n#endif\n\tint n, k, x;\n\tcin>>n>>k>>x;\n\tfor(int i=1; i<=n; i++) scanf(\"%d\", a+i);\n\treverse(a+1, a+n+1);\n\tfor(int i=1; i<=min(n, k); i++) a[i] = min(a[i], x);\n\tint ans = 0;\n\tfor(int i=1; i<=n; i++) ans+=a[i];\n\tcout<\n#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\n#define N 20\nint f=0;\nchar a[N][N],b[N][N];\n\nvoid duizhe(int n){\n int c[N][N],i,j;\n for(i=0;i>n;\n for(i=0;i>a[i][j];\n }\n }\n for(i=0;i>b[i][j];\n }\n }\n find(n);\n if(f)\n return 0;\n duizhe(n);\n find(n);\n// for(i=0;i\nusing namespace std ;\n//======================================================\n#define El_Bay \tios_base::sync_with_stdio(false);cin.tie(NULL);\n#define forn(i,n) for (int i = 0; i < (int)(n); ++i)\n#define for1(i,n) for (int i = 1; i < (int)(n); ++i)\n#define all(v) (v).begin(),(v).end()\n#define PI 3.14159265359\n#define ull unsigned long long\n#define ll long long\n#define pb push_back\n#define pp pop_back\n#define MP\t\t make_pair\n#define S second\n#define bgn begin\n#define F first\n//======================================================\nint dx[] = { 0, 0, 1, -1 , 1, -1, 1, -1 };\nint dy[] = { 1, -1, 0, 0 , -1, 1, 1, -1 };\n//======================================================\nll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll power(ull a, ull b, ull ret = 1LL) {\n\twhile (b--) ret = (ret * a);\n\treturn ret;}\n//======================================================\n\nint main()\n{\n\tEl_Bay\n\tint h1, h2, a, b;\n\tcin >> h1 >> h2 >> a >> b;\n\th1=h1+a*8;\n if(h1>=h2) return cout << 0,0;\n if(a<=b) return cout <<-1,0;\n cout <<(h2-h1)/((a-b)*12)+((h2-h1)% ((a-b)*12)>=1 ) ;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "52b00ef04f82147f0514fccb3c4f813f", "src_uid": "2c39638f07c3d789ba4c323a205487d7", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nbool check(int len,string s){\n unordered_map u;\n for(int i=0;ifirst<<\" \"<second< 1)return true;\n }\n \n // cout<>s;\n int n = s.length();\n // unordered_map u;\n int l = 0,r = n-1;\n int len = 0;\n int ans = 0;\n while(l <= r){\n len = (l+r)/2;\n if(check(len,s)){\n l = len + 1;\n ans = max(ans,len);\n }\n else r = len - 1;\n\n }\n \n // cout<\n\nusing namespace std;\ntypedef long long LL;\nLL N = 10000001;\n\nLL* primes = new LL[N];\nLL* primecounts = new LL[N];\nLL* smallprime = new LL[N];\nLL* mobiusval = new LL[N];\nLL* divisorcounts = new LL[N];\nLL* divisortotals = new LL[N];\n\nLL mobius(LL n, LL p)\n{\n if(n==1)\n return -1;\n else\n {\n if(smallprime[n]==p)\n return 0;\n else\n return -1*mobius(n/smallprime[n], smallprime[n]);\n }\n}\n\n\nint main()\n{\n\n\n for(LL i=0; i>n;\n LL ans = 0;\n\n\n\n\n for(LL i=2; i\n#define INT long long\nusing namespace std;\nconst int NN = 5100;\nINT dp[NN], t[2];\nint p[2];\nint main() {\n\tfor(int i = 0; i < 2; i ++) cin >> p[i] >> t[i];\n\tint h, s;\n\tcin >> h >> s;\n\tfor(int i = 1; i <= h; i ++) {\n\t\tdp[i] = 1e18;\n\t\tfor(int j = 1; j <= h; j ++) {\n\t\t\tfor(int k = 0; k < 2; k ++) {\n\t\t\t\tINT T = t[k] * j;\n\t\t\t\tif(T < t[k ^ 1]) continue;\n\t\t\t\tINT tmp = min(5000ll, (T - t[k^1]) / t[k^1]) * (p[k^1] - s);\n\t\t\t\ttmp += (j - 1) * (p[k] - s);\n\t\t\t\ttmp += (p[1] + p[0] - s);\n\t\t\t\tdp[i] = min(dp[i], T + dp[max(0ll, i - tmp)]);\n\t\t\t}\n\t\t}\n\t\tINT st = 0, ed = 1LL * 5001 * min(t[1], t[0]);\n\t\twhile(st + 1 < ed) {\n\t\t\tINT md = (st + ed) >> 1;\n\t\t\tINT tmp = 0;\n\t\t\tfor(int k = 0; k < 2; k ++) {\n\t\t\t\ttmp += (md / t[k]) * (p[k] - s);\n\t\t\t}\n\t\t\tif(tmp >= i) ed = md;\n\t\t\telse st = md;\n\t\t}\n\t\tdp[i] = min(dp[i], ed);\n\t}\n\tcout << dp[h] << endl;\n}\n ", "lang_cluster": "C++", "tags": ["dp", "binary search"], "code_uid": "4bac515c4ad153dbf925cb78e98b1828", "src_uid": "ca9d48e48e69b931236907a9ac262433", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint md = 1000000007;\n//long long binpow(long long a, long long n) {\n//\tlong long res = 1;\n//\twhile (n) {\n//\t\tif (n & 1)\n//\t\t{\n//\t\t\tres = (res * a) % md;\n//\t\t\t//res %= md;\n//\t\t}\n//\t\ta = (a * a) % md;\n//\t\t//a %= md;\n//\t\tn >>= 1;\n//\t}\n//\treturn res;// % md;\n//}\n\n//int gcd(int a, int b) {\n//\treturn b ? gcd(b, a % b) : a;\n//}\n//int lcm(int a, int b) {\n//\treturn a / gcd(a, b) * b;\n//}\nint gcd(int a, int b, int& x, int& y) {\n\tif (a == 0) {\n\t\tx = 0; y = 1;\n\t\treturn b;\n\t}\n\tint x1, y1;\n\tint d = gcd(b % a, a, x1, y1);\n\tx = y1 - (b / a) * x1;\n\ty = x1;\n\treturn d;\n}\nbool find_any_solution(int a, int b, int c, int& x0, int& y0, int& g) {\n\tg = gcd(abs(a), abs(b), x0, y0);\n\tif (c % g != 0)\n\t\treturn false;\n\tx0 *= c / g;\n\ty0 *= c / g;\n\tif (a < 0) x0 *= -1;\n\tif (b < 0) y0 *= -1;\n\treturn true;\n}\n//\n//void shift_solution(int& x, int& y, int a, int b, int cnt) {\n//\tx += cnt * b;\n//\ty -= cnt * a;\n//}\n//\n//int find_all_solutions(int a, int b, int c, int minx, int maxx, int miny, int maxy) {\n//\tint x, y, g;\n//\tif (!find_any_solution(a, b, c, x, y, g))\n//\t\treturn 0;\n//\ta /= g; b /= g;\n//\n//\tint sign_a = a > 0 ? +1 : -1;\n//\tint sign_b = b > 0 ? +1 : -1;\n//\n//\tshift_solution(x, y, a, b, (minx - x) / b);\n//\tif (x < minx)\n//\t\tshift_solution(x, y, a, b, sign_b);\n//\tif (x > maxx)\n//\t\treturn 0;\n//\tint lx1 = x;\n//\n//\tshift_solution(x, y, a, b, (maxx - x) / b);\n//\tif (x > maxx)\n//\t\tshift_solution(x, y, a, b, -sign_b);\n//\tint rx1 = x;\n//\n//\tshift_solution(x, y, a, b, -(miny - y) / a);\n//\tif (y < miny)\n//\t\tshift_solution(x, y, a, b, -sign_a);\n//\tif (y > maxy)\n//\t\treturn 0;\n//\tint lx2 = x;\n//\n//\tshift_solution(x, y, a, b, -(maxy - y) / a);\n//\tif (y > maxy)\n//\t\tshift_solution(x, y, a, b, sign_a);\n//\tint rx2 = x;\n//\n//\tif (lx2 > rx2)\n//\t\tswap(lx2, rx2);\n//\tint lx = max(lx1, lx2);\n//\tint rx = min(rx1, rx2);\n//\n//\treturn (rx - lx) / abs(b) + 1;\n//}\nconst int mod = 998244353;\n\n // int md = 1000000007;\nlong long binpow(long long a, long long n)\n{\n\tif (n == 0)return 1;\n\tif (n % 2 == 0)\n\t{\n\t\tlong long b = (binpow(a, n / 2))%mod;\n\t\treturn b * b%mod;\n\t}\n\telse\n\t\treturn (binpow(a, n - 1) * a)%mod;\n}\n\nint main()\n{\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#else\n\t//freopen(\"rmq.in\", \"r\", stdin);\n\t//freopen(\"rmq.out\", \"w\", stdout);\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n#endif\n\tios::sync_with_stdio(false);\n\tcin.tie(0); cout.tie(0);\n\tcout.precision(10); cout.setf(ios::fixed);\n\tint n;\n\tcin >> n;\n\tvector v(n);\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> v[i];\n\tvectorans;\n\tbool na = false;\n\tint cnt = 0;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tif (v[i] == 1 && na)\n\t\t{\n\t\t\tif(cnt!=0)\n\t\t\tans.push_back(cnt);\n\t\t\tcnt = 0;\n\t\t}\n\t\tif (v[i] == 1)\n\t\t{\n\t\t\tna = true;\n\t\t\tcnt = 0;\n\t\t}\n\t\telse cnt++;\n\n\t}\n\tif (ans.size() == 0)\n\t{\n\t\tif (na)\n\t\t\tcout << 1;\n\t\telse cout << 0;\n\t\treturn 0;\n\t}\n\tlong long an = 1;\n\tfor (int i = 0; i < ans.size(); i++)\n\t\tan*= (ans[i] + 1);\n\tcout << an;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["combinatorics"], "code_uid": "01a3723a892ed20c84a1df7e4b4f76fb", "src_uid": "58242665476f1c4fa723848ff0ecda98", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n#include \nusing namespace std;\nstatic struct IO {\n\tchar tmp[1 << 10];\n\t// fast input routines - http://ideone.com/fOK6CW\n\tchar cur;\n\tinline char nextChar ( )\n\t{\n\t\treturn cur = getc (stdin);\n\t}\n\tinline char peekChar ( )\n\t{\n\t\treturn cur;\n\t}\n\tinline operator bool ( )\n\t{\n\t\treturn 0 != peekChar ( );\n\t}\n\tinline static bool isBlank (char c)\n\t{\n\t\treturn (c < '-' && c);\n\t}\n\tinline bool skipBlanks ( )\n\t{\n\t\twhile (isBlank (nextChar ( ))); return peekChar ( ) != 0;\n\t}\n\tinline IO& operator >> (char & c)\n\t{\n\t\tc = nextChar ( ); return *this;\n\t}\n\tinline IO& operator >> (char * buf)\n\t{\n\t\tif (skipBlanks ( )) {\n\t\t\tif (peekChar ( )) {\n\t\t\t\t*(buf++) = peekChar ( );\n\t\t\t\twhile (!isBlank (nextChar ( ))) *(buf++) = peekChar ( );\n\t\t\t} *(buf++) = 0;\n\t\t} return *this;\n\t}\n\tinline IO& operator >> (string & s)\n\t{\n\t\tif (skipBlanks ( )) {\n\t\t\ts.clear ( ); s += peekChar ( );\n\t\t\twhile (!isBlank (nextChar ( ))) s += peekChar ( );\n\t\t}\n\t\treturn *this;\n\t}\n\tinline IO& operator >> (double & d)\n\t{\n\t\tif ((*this) >> tmp) sscanf (tmp, \"%lf\", &d); return *this;\n\t}\n#define defineInFor(intType) \\\n\tinline IO& operator >>(intType & n) { \\\n\t\tif (skipBlanks()) { \\\n\t\t\tint sign = +1; \\\n\t\t\tif (peekChar() == '-') { \\\n\t\t\t\tsign = -1; \\\n\t\t\t\tn = nextChar() - '0'; \\\n\t\t\t} else \\\n\t\t\t\tn = peekChar() - '0'; \\\n\t\t\twhile (!isBlank(nextChar())) { \\\n\t\t\t\tn += n + (n << 3) + peekChar() - 48; \\\n\t\t\t} \\\n\t\t\tn *= sign; \\\n\t\t} \\\n\t\treturn *this; \\\n\t}\n\tdefineInFor (int)\n\t\tdefineInFor (unsigned int)\n\t\tdefineInFor (long long)\n\t\tdefineInFor (unsigned long long)\n\n\t\t// fast output routines\n\t\tinline void putChar (char c)\n\t{\n\t\tputc (c, stdout);\n\t}\n\tinline IO& operator << (char c)\n\t{\n\t\tputChar (c); return *this;\n\t}\n\tinline IO& operator << (const char * s)\n\t{\n\t\twhile (*s) putChar (*s++); return *this;\n\t}\n\tinline IO& operator << (const string & s)\n\t{\n\t\tfor (int i = 0; i < (int) s.size ( ); ++i) putChar (s[i]); return *this;\n\t}\n\tchar * toString (double d)\n\t{\n\t\tsprintf (tmp, \"%lf%c\", d, '\\0'); return tmp;\n\t}\n\tinline IO& operator << (double d)\n\t{\n\t\treturn (*this) << toString (d);\n\t}\n#define defineOutFor(intType) \\\n\tinline char * toString(intType n) { \\\n\t\tchar * p = (tmp + 30); \\\n\t\tif (n) { \\\n\t\t\tbool isNeg = 0; \\\n\t\t\tif (n < 0) isNeg = 1, n = -n; \\\n\t\t\twhile (n) \\\n\t\t\t\t*--p = (n % 10) + '0', n /= 10; \\\n\t\t\tif (isNeg) *--p = '-'; \\\n\t\t} else *--p = '0'; \\\n\t\treturn p; \\\n\t} \\\n\tinline IO& operator << (intType n) { return (*this) << toString(n); }\n\tdefineOutFor (int)\n\t\tdefineOutFor (long long)\n\t\t//defineOutFor (unsigned long long)\n#define endl ('\\n')\n#define cout __io__\n#define cin __io__\n} __io__;\nclass TimeTracker {\n\tclock_t start, end;\npublic:\n\tTimeTracker ( )\n\t{\n\t\tstart = clock ( );\n\t}\n\t~TimeTracker ( )\n\t{\n\t\tend = clock ( );\n\t\tfprintf (stderr, \"%.3lf s\\n\", (double) (end - start) / CLOCKS_PER_SEC);\n\t}\n};\ntemplate inline T mypow (T x, int n)\n{\n\tif (n == 0)\n\t\treturn 1;\n\tT y = 1;\n\twhile (n > 1) {\n\t\tif (n % 2 == 0) {\n\t\t\tx = x*x;\n\t\t\tn = n / 2;\n\t\t}\n\t\telse {\n\t\t\ty = x*y;\n\t\t\tx = x*x;\n\t\t\tn = (n - 1) / 2;\n\t\t}\n\t}\n\treturn x*y;\n}\nint main ( )\n{\n\tlong long nines = 9999999999LL, ans = 1LL;\n\tlong long l, r;\n\tcin >> l >> r;\n\tTimeTracker tt;\n\tlong long upper = mypow (10LL, (int) log10 (r) + 1);\n\tlong long lower = mypow (10LL, (int) log10 (l) + 1);\n\tif (r >= upper / 2) {\n\t\tif (l <= upper / 2) {\n\t\t\tans = (nines % (mypow (10LL, ((int) log10 (upper / 2) + 1))) - upper / 2)*(upper / 2);\n\t\t}\n\t\telse {\n\t\t\tans = (nines % (mypow (10LL, ((int) log10 (upper / 2) + 1))) - l)*l;\n\t\t}\n\t}\n\telse {\n\t\tans = (nines % (mypow (10LL, ((int) log10 (upper / 2) + 1))) - r)*r;\n\t}\n\tcout << ans << endl;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "6ea48f84f5a3ab45d3ce041e7f636e0c", "src_uid": "2c4b2a162563242cb2f43f6209b59d5e", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n\nusing namespace std;\n\nint main() {\n long long n, m, s;\n scanf(\"%lld %lld %lld\", &n, &m, &s);\n if (n < m)\n swap(n, m);\n if (s >= n)\n printf(\"%lld\\n\", n * m);\n else if (s >= m)\n printf(\"%lld\\n\", ((n - 1) % s + 1) * ((n - 1) / s + 1) * m);\n else\n printf(\"%lld\\n\", ((n - 1) % s + 1) * ((n - 1) / s + 1) * ((m - 1) % s + 1) * ((m - 1) / s + 1));\n return 0;\n};\n", "lang_cluster": "C++", "tags": ["math"], "code_uid": "a574f8146f823961f63127f1e92fec92", "src_uid": "e853733fb2ed87c56623ff9a5ac09c36", "difficulty": 1700.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nint main()\n{\n int n;\n cin >> n;\n if(n%4 == 1%4)cout <<8;\n else if(n%4 == 2%4)cout << 4;\n else if(n%4 == 3%4)cout << 2;\n else if(n == 0)cout << 1;\n else cout << 6;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "implementation", "number theory"], "code_uid": "3fd4307152c4361ab0fd60e03b9b6c42", "src_uid": "4b51b99d1dea367bf37dc5ead08ca48f", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17 (64)", "source_code": "/**\n * 生死の去来するは、ほうとうの傀儡たり。\n * 一線弛る時、落々磊々。\n*/\n\n#include \nusing namespace std;\n\n#define inl inline\ntemplate inl bool read (T &x) {\n\tx = 0; int f = 1; char c = getchar ();\n\tfor (; c != EOF && !isdigit (c); c = getchar ())\n\t\tif (c == '-') f = -1;\n\tif (c == EOF) return 0;\n\tfor (; c != EOF && isdigit (c); c = getchar ())\n\t\tx = (x<<1) + (x<<3) + (c^48);\n\tx *= f; return 1;\n}\ntemplate \ninl bool read (T &x, Targs&... args) {\n\treturn read (x) && read (args...); }\ntemplate void print (T x) {\n\tif (x < 0) putchar ('-'), x = -x;\n\tif (x > 9) print (x / 10);\n\tputchar ('0' + x % 10);\n}\ntemplate \ninl void print (T x, Targs... args) {\n\tprint (x), putchar (' '), print (args...); }\n\n#define reint register int\n#define newl putchar('\\n')\ntypedef long long ll;\n// typedef unsigned long long ull;\n// typedef __int128 lll;\n// typedef long double llf;\ntypedef pair pint;\n#define fst first\n#define scd second\n#define all(p) begin (p), end (p)\nusing vint = vector ;\n\nconstexpr int N = 1<<19, P = 998244353,\n\tgen = 3, INF = 0x3f3f3f3f;\nint n, k, m, _f; vint f, g;\n\ninl ll fpow (ll a, ll b) {\n\tll res = 1; a %= P;\n\tfor (; b; b >>= 1) {\n\t\tif (b & 1) res = res * a % P;\n\t\ta = a * a % P;\n\t}\n\treturn res;\n}\ninl void henkan (vint &f, int l) {\n\tstatic int tr[N], lst = tr[0] = 0;\n\tif (lst != l) { lst = l; \n\t\tfor (int i = 1; i < 1<>1]>>1|((1<\n#include\nusing namespace std;\nint main(){\n\tint n,m;\n\tcin>>n>>m;\n\tint a=max(n,m);\n\tint b=min(n,m);\n\t\n\tif(a==0||b==0){\n\t\t\n\t\tcout<<'0'<=2){\n\t\tcout<\n#include \n//#include \n#include \n#include \n#include \n#include \n#include \n#include //pair\n#include \n//#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//#include \nusing namespace std;\n#define R(x) scanf(\"%d\",&(x)) //read int\n#define RS(x) scanf(\"%s\",(x)) //read string\n#define PL(x) printf(\"%d\\n\",(x)) //println int\n#define REP(i,n) for(int i(0),_n(n);i<_n;++i)\n#define FOR(i,l,h) for(int i(l),_h(h);i<=_h;++i)\n#define FORD(i,h,l) for(int i(h),_l(l);i>=_l;--i)\n#define RESET(a,b) memset((a),b,sizeof(a))\n#define DEBUG(exp) (cout<<(#exp)<<\" = \"<<(exp)< pii;\ntypedef pair pdd;\n\nconst int N = 33;\nconst int LCM = 2520;\n\nint d[N],n;\nint id[LCM+1];\nInt dp[N][50][LCM+1];\n\nInt dfs(int pos,int lcm,int sum,bool UP) {\n if(pos==-1) return sum%lcm==0? 1 : 0;\n Int& ans=dp[pos][id[lcm]][sum];\n if(UP==false && ans!=-1) return ans;\n Int ret=0;\n FOR(i,0,UP? d[pos] : 9) {\n int tlcm= i==0? lcm : lcm/__gcd(lcm,i)*i;\n int tsum= (sum*10+i)%LCM;\n ret+= dfs(pos-1,tlcm,tsum,UP&&i==d[pos]);\n }\n if(UP==false) ans=ret;\n return ret;\n}\n\nInt calc(Int x) {\n n=0;\n while(x) {\n d[n++]=x%10;\n x/=10;\n }\n return dfs(n-1,1,0,true);\n}\n\nvoid init() {\n int cnt=0;\n FOR(i,1,LCM) {\n if(LCM%i==0) id[i]=cnt++;\n }\n RESET(dp,-1);\n}\n\nint main() {\n#ifndef StarLeo_DEBUG\n// freopen(\"runround.in\",\"r\",stdin);\n// freopen(\"runround.out\",\"w\",stdout);\n#endif\n\n int T;\n cin>>T;\n init();\n while(T--) {\n Int a,b;\n cin>>a>>b;\n cout<< calc(b)-calc(a-1) <\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define N 1000005\n#define ll long long\n#define mod 1000000007\nint fac[N],inv[N],n,m,maxn,ans;\nint q_pow(int x,int n){int ret=1;for(;n;n>>=1,x=(ll)x*x%mod)if(n&1)ret=(ll)ret*x%mod;return ret;}\nint C(int n,int m){return (ll)fac[n]*inv[m]%mod*inv[n-m]%mod;}\nint main()\n{\n\tscanf(\"%d%d%*d%*d\",&n,&m);maxn=max(n,m);fac[0]=1;\n\tfor(int i=1;i<=maxn;i++)fac[i]=fac[i-1]*(ll)i%mod;inv[maxn]=q_pow(fac[maxn],mod-2);\n\tfor(int i=maxn;i;i--)inv[i-1]=(ll)i*inv[i]%mod;\n\tfor(int i=1;i\n#include \n#include \n#include \n#define maxn 200\nusing namespace std;\nint getmaxnum(int x)//找一个数字中最大数位\n{\n int max=0;\n while(x)\n {\n int s=x%10;\n if(s>max) max=s;\n x/=10;\n }\n return max;\n}\nint change(int x,int base)//10进制转base进制\n{\n int s=0,p=1;\n while(x)\n {\n s+=x%10*p;\n x/=10;\n p*=base;\n }\n return s;\n}\nint main()\n{\n int a,b;\n cin>>a>>b;\n int base=max(getmaxnum(a),getmaxnum(b))+1;//求基数\n int sum=change(a,base)+change(b,base);//在base进制下两数相加,求十进制结果\n int j=0;\n while(sum)//求其位数\n {\n int s=sum%base;\n sum/=base;\n j++;\n }\n cout<\r\n// #include\r\n// #include\r\n\r\nusing namespace std;\r\nusing namespace chrono;\r\n//using namespace __gnu_pbds;\r\n\r\n\r\n#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\r\n#define pb push_back\r\n#define ppb pop_back\r\n#define mp make_pair\r\n#define rep(i,a,n) for (int i = a; i < n; ++i)\r\n#define REP(i,k,n) for (int i = k; i <= n; ++i)\r\n#define repr(i,k,n) for (int i = k; i >= n; --i)\r\n#define mod 1000000007\r\n#define mod1 998244353\r\n#define all(x) x.begin(),x.end()\r\n#define allr(x) x.rbegin(),x.rend()\r\n#define vd(x) vector \r\n#define sz(x) ((int)(x).size())\r\n#define ff first\r\n#define ss second\r\n#define nline \"\\n\"\r\n#define INF 1e18\r\n#define vll vector \r\n#define pii pair\r\n#define vii vector \r\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\r\n\r\n#ifndef ONLINE_JUDGE\r\n#define debug(x) cerr << #x <<\" \"; _print(x); cerr << endl;\r\n#else\r\n#define debug(x);\r\n#endif\r\n\r\ntypedef long long int ll;\r\ntypedef unsigned long long ull;\r\ntypedef long double lld;\r\n//typedef tree, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key\r\n// use less equal if you want to use as multiset\r\nvoid _print(ll t) {cerr << t;}\r\nvoid _print(int t) {cerr << t;}\r\nvoid _print(string t) {cerr << t;}\r\nvoid _print(char t) {cerr << t;}\r\nvoid _print(lld t) {cerr << t;}\r\nvoid _print(double t) {cerr << t;}\r\nvoid _print(ull t) {cerr << t;}\r\n\r\ntemplate void _print(pair p);\r\ntemplate void _print(vector v);\r\ntemplate void _print(set v);\r\ntemplate void _print(map v);\r\ntemplate void _print(multiset v);\r\ntemplate void _print(pair p) {cerr << \"{\"; _print(p.ff); cerr << \",\"; _print(p.ss); cerr << \"}\";}\r\ntemplate void _print(vector v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\r\ntemplate void _print(set v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\r\ntemplate void _print(multiset v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\r\ntemplate void _print(map v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\r\n\r\n//modular template\r\nll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\r\nll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\r\nll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}\r\n\r\n\r\n//void _print(pbds v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\r\n// char alf[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',\r\n\r\n// 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'\r\n// };\r\n// char alpha[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'\r\n// ,'U','V','W','X'.'Y','Z'};\r\n\r\n\r\n\r\nbool chk(string x, string y)\r\n{\r\n if (y.size() < x.size())\r\n return false;\r\n else\r\n {\r\n int n1 = x.size();\r\n int n2 = y.size();\r\n int diff = n2 - n1;\r\n for (int i = 0; i <= diff; i++)\r\n {\r\n string sa(diff - i, '1'); string sb(i, '1');\r\n string temp = sa + x + sb;\r\n if (temp == y) return true;\r\n }\r\n return false;\r\n }\r\n\r\n}\r\n\r\n\r\n\r\nvoid solve() {\r\n ll n, m;\r\n cin >> n >> m;\r\n if (n == m)\r\n {\r\n cout << \"YES\\n\"; return;\r\n }\r\n string a;\r\n string b;\r\n while (n > 0)\r\n {\r\n a.pb(n % 2 + '0');\r\n n /= 2;\r\n }\r\n while (m > 0)\r\n {\r\n b.pb(m % 2 + '0');\r\n m /= 2;\r\n }\r\n reverse(all(a));\r\n reverse(all(b));\r\n debug(a);\r\n debug(b);\r\n string a1 = a; a1.pb('1');\r\n string a2 = a;\r\n while (!a2.empty())\r\n {\r\n if (a2.back() == '0') a2.pop_back();\r\n else break;\r\n }\r\n bool ans = false;\r\n ans = ans | chk(a2, b); debug(ans);\r\n ans = ans | chk(a1, b); debug(ans);\r\n reverse(all(a1)); reverse(all(a2));\r\n ans = ans | chk(a2, b); debug(ans);\r\n ans = ans | chk(a1, b); debug(ans);\r\n ans == true ? cout << \"YES\\n\" : cout << \"NO\\n\";\r\n\r\n}\r\n\r\nint main() {\r\n#ifndef ONLINE_JUDGE\r\n freopen(\"error.txt\", \"w\", stderr);\r\n#endif\r\n fastio();\r\n auto start1 = high_resolution_clock::now();\r\n // seive();\r\n int tcase; tcase = 1;\r\n //cin >> tcase;\r\n rep(tc, 0, tcase) {\r\n debug(tc);\r\n //cout << \"Case #\" << tc + 1 << \":\" << \" \";\r\n //debug(tc);\r\n solve();\r\n\r\n }\r\n auto stop1 = high_resolution_clock::now();\r\n auto duration = duration_cast(stop1 - start1);\r\n#ifndef ONLINE_JUDGE\r\n cerr << \"Time: \" << duration . count() / 1000 << endl;\r\n#endif\r\n return 0;\r\n}\r\n\r\n", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms", "math", "bitmasks", "strings", "implementation"], "code_uid": "19afb73f5315e4c20769246dcc9ad5b9", "src_uid": "9f39a3c160087beb0efab2e3cb510e89", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \ntypedef long long ll;\nusing namespace std;\n\n#ifdef DEBUG\n#include \"/home/lucas/Topcoder/debug.h\"\n#define dbg(args...) do {cerr << #args << \": \"; debug,args; cerr << endl;} while(0)\n#else\n#define dbg(args...)\n#endif\n\n#define MAX 89\n\nll fib[MAX];\n\nll n, k;\nll sol[MAX];\n\nint main() {\n ios :: sync_with_stdio(0);\n\n fib[0] = fib[1] = 1;\n for(int i=2; i> n >> k) {\n k --;\n\n for(int i=1; i<=n; i++) {\n if (n-i+1 >= 1 && fib[n-i+1 - 1] <= k) {\n sol[i] = i+1;\n sol[i+1] = i;\n k -= fib[n-i+1-1];\n i++;\n } else {\n sol[i] = i;\n }\n }\n\n for(int i=1; i<=n; i++) {\n cout << sol[i] << \" \\n\"[i==n];\n }\n\n }\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["math", "greedy"], "code_uid": "06ba2588f99a220cfc4117fca63c4e35", "src_uid": "e03c6d3bb8cf9119530668765691a346", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ll long long\n#define ull long long\nusing namespace std;\n\nll solve(ll a,ull b,ll p)\n{\n\tll res = 1;\n\ta=a%p;\n\t\n\twhile(b>0)\n\t{\n\t\t\n\t\tif(b&1)\n\t\t{\n\t\t\tres=(res*a)%p;\n\t\t}\n\t\tb=b>>1;\n\t\ta=((a%p)*(a%p))%p;\n//\t\ta=(a*a)%p;\n\t}\n\treturn res;\n\t\n}\n\nint main()\n{\n\tll n,m;\n\tcin>>n>>m;\n\tll ans = solve(3,n,m);\n\tif(ans==0)\n\t{\n\t\tcout<\n\nusing namespace std;\n\nint main(){\n long long n, k, a, b;\n \n\tcin >> n >> k;\n\tif(k > (n + n-1)){\n\t cout << 0;\n\t return 0 ;\n\t} \n\tif(k == (n + n-1) && n > 1){\n\t cout << 1;\n\t return 0;\n\t}\n\tif(k<=n){\n\t\ta=abs(k-n);\n\t\tb=(n-a-1)/2;\n\t}else{\n\t\ta=k-n;\n\t\tb=((n-a)+1)/2;\n\t}\n\tcout<<\" \"< \n \nint a,b,c,a1,b1,c1; \n \nint main(void){ \n// freopen(\".in\",\"r\",stdin); \n// freopen(\".out\",\"w\",stdout); \n scanf(\"%d%d%d\\n%d%d%d\",&a,&b,&c,&a1,&b1,&c1); \n if(a==0 && b==0 && c!=0 || a1==0 && b1==0 && c1!=0) puts(\"0\");else \n if(a*b1==b*a1){ \n if(c*a1==c1*a && c*b1==c1*b) puts(\"-1\");else \n puts(\"0\"); \n }else puts(\"1\"); \n return 0; \n} \n", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "8b7c24bb3445acd02701cc8b01f3b263", "src_uid": "c8e869cb17550e888733551c749f2e1a", "difficulty": 2000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define fr first\n#define sc second\n#define lb lower_bound\n#define ub upper_bound\n#define pii pair\n#define vi vector\n#define FOR(i,x,y) for(int i=x; i<=y; i++)\n\nconst int mod=int(1e9+7),MAXN=int(1e3+50);\n\nint n,s[MAXN],br,raz,z[MAXN];\nvi v[MAXN];\nvector > rez;\n\nvoid dfs(int i, int ot)\n{\n s[i]=1;\n for (auto j:v[i])\n if (j!=ot)\n {\n dfs(j,i);\n s[i]+=s[j];\n }\n}\n\nint centroid()\n{\n dfs(1,-1);\n int c=1,ot=-1;\n while(1)\n {\n int br=1;\n for (auto j:v[c])\n if (j!=ot && 2*s[j]>=n)\n {\n br=0;\n ot=c;\n c=j;\n break;\n }\n if (br)\n break;\n }\n return c;\n}\n\nvoid dfs2(int i, int ot)\n{\n br+=raz;\n z[i]=br;\n rez.pb({{i,ot},z[i]-z[ot]});\n for (auto j:v[i])\n if (j!=ot)\n dfs2(j,i);\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tcout.tie(nullptr);\n\n cin >> n;\n\n FOR(i,1,n-1)\n {\n int p,q;\n cin >> p >> q;\n v[p].pb(q);\n v[q].pb(p);\n }\n\n if (n==1)\n {\n exit(0);\n }\n if (n==2)\n {\n printf(\"1 2 1\\n\");\n exit(0);\n }\n\n int c=centroid();\n\n dfs(c,-1);\n\n vector v2,g1,g2;\n\n multiset > > sv;\n\n for (auto i:v[c])\n {\n vector vv{i};\n sv.insert({s[i],vv});\n }\n\n while (sv.size()>2)\n {\n auto pom1=*sv.begin();\n sv.erase(sv.begin());\n auto pom2=*sv.begin();\n sv.erase(sv.begin());\n\n vector vu;\n for (auto i:pom1.sc)\n vu.pb(i);\n for (auto i:pom2.sc)\n vu.pb(i);\n\n sv.insert({pom1.fr+pom2.fr,vu});\n }\n\n auto a=*sv.begin();\n sv.erase(sv.begin());\n auto b=*sv.begin();\n sv.erase(sv.begin());\n\n z[c]=0;\n br=0;\n raz=1;\n for (auto i:a.sc)\n dfs2(i,c);\n\n br=0;\n raz=a.fr+1;\n\n for (auto i:b.sc)\n dfs2(i,c);\n\n for (auto i:rez)\n printf(\"%d %d %d\\n\",i.fr.fr,i.fr.sc,i.sc);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "trees"], "code_uid": "bc3026fb133ff6efb01b0ba5888db0bf", "src_uid": "87d755df6ee27b381122062659c4a432", "difficulty": 2700.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#define mp make_pair\n#define pii pair\n#define all(x) x.begin(),x.end()\n#define pb(x) push_back(x);\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n \n#define MOD 998244353 //(1000*1000*1000+7)\n#define inf1 2e9\n#define inf2 2e18\n \ninline int mul(int a, int b) {\n\treturn int(a * 1ll * b % MOD);\n}\n \ninline int norm(int a) {\n\tif (a >= MOD)\n\t\ta -= MOD;\n\tif (a < 0)\n\t\ta += MOD;\n\treturn a;\n}\n \ninline int binPow(int a, int k) {\n\tint ans = 1;\n\twhile (k > 0) {\n\t\tif (k & 1)\n\t\t\tans = mul(ans, a);\n\t\ta = mul(a, a);\n\t\tk >>= 1;\n\t}\n\treturn ans;\n}\n \ninline int subtract(int a, int b) {\n\treturn norm(a - b);\n}\n \ninline int sum(int a, int b) {\n\treturn int((a + b) % MOD);\n}\n \ninline int inv(int a) {\n\treturn binPow(a, MOD - 2);\n}\n \n \nll ceil_int(ll a, ll b) {\n\treturn a / b + (a%b != 0);\n}\n \n \nll gcd(ll a, ll b) {\n\tif (a == 0)return b;\n\treturn gcd(b%a, a);\n}\n \nll lcm(ll a, ll b) {\n\treturn (a*b) / gcd(a, b);\n}\n \nint main() {\n \n\tint n, k;\n\tstring s;\n\tcin >> n >> k;\n\tcin >> s;\n\tfor (int i = 0; i < n; i++) {\n\t\tif (s[i] > '0' && k>0) {\n\t\t\tif (i > 0 || n==1) {\n\t\t\t\ts[i] = '0';\n\t\t\t\tk--;\n\t\t\t}\n\t\t\telse if(s[i]!='1'){\n\t\t\t\ts[i] = '1';\n\t\t\t k--;\n\t\t\t \n\t\t\t}\n\t\t\n\t\t}\n\t}\n\tcout << s;\n \n\treturn 0;\n}\n ", "lang_cluster": "C++", "tags": ["greedy", "implementation"], "code_uid": "c4d98e5c6f87a79fbcaa13dec23d2ba6", "src_uid": "0515ac888937a4dda30cad5e2383164f", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#define ll long long\nusing namespace std;\nint main()\n{\n ll a,b,c,ans2;\n cin >> a >> b >> c;\n\n ll d = a, e = b , f = c;\n\n ll ans = min(a,min(b,c));\n a -= ans , b -= ans , c -= ans;\n ans += (a/3)+(b/3)+(c/3);\n a %= 3 , b %= 3 , c %= 3;\n\n if(min(f,min(d,e))>0 && (a+b+c) == 4)\n ans++;\n cout << ans << endl;\n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics"], "code_uid": "9738734a614bc0cc609382b28abb6f66", "src_uid": "acddc9b0db312b363910a84bd4f14d8e", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\n#include\nusing namespace std;\nint main()\n{\n\tstring c;\n\tint flag,des,des1,len,pos;\n\twhile(cin >> c)\n\t{\n\t\t\n\t\tflag =0;\n\t\tlen = c.size ();\n\t\tdes = c.find(\"C\");\n\t\tif(des != -1)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"ODEFORCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 9== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CO\");\n\t\tif(des!= -1 && flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"DEFORCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 8== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 2)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"COD\");\n\t\tif(des != -1 && flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"EFORCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 7== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 3)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODE\");\n\t\tif(des != -1 && flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"FORCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 6 == len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 4)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEF\");\n\t\tif(des != -1&& flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"ORCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 5== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 5)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEFO\");\n\t\tif(des != -1&& flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"RCES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 4 == len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 6)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEFOR\");\n\t\tif(des != -1&& flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"CES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 3== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 7)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEFORC\");\n\t\tif(des!= -1&& flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"ES\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 2== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 8)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEFORCE\");\n\t\tif(des!= -1&& flag == 0)\n\t\t{\n\t\t\tdes1 = 0;\n\t\t\tpos = 0;\n\t\t\twhile((pos = c.find(\"S\",pos))!= string :: npos)\n\t\t\t{\n\t\t\t\tdes1 = pos;\n\t\t\t\tpos ++;\n\t\t\t\t\n\t\t\t}\n\t\t\tif(des1 + 1== len && des1 != -1 && des1 > des)\n\t\t\t{\n\t\t\t\tif(des != 0)\n\t\t\t\t{\n\t\t\t\t\tif(des1 - des == 9)\n\t\t\t\t\t{\n\t\t\t\t\t\tflag =1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(des == 0)\n\t\t\t\t{\n\t\t\t\t\tflag = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdes = c.find(\"CODEFORCES\");\n\t\tif(des == 0)\n\t\t{\n\t\t\tflag = 1;\n\t\t}\n\t\tif(flag == 1)\n\t\t{\n\t\t\tcout << \"YES\" << endl;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout << \"NO\" << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "62becb94e49cac2e3e8279c7a672d4ea", "src_uid": "bda4b15827c94b526643dfefc4bc36e7", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#define mp make_pair\n#define pb push_back\n#define F first\n#define S second\nusing namespace std;\nint x,y,z,xx,yy,zz,a[6],ans=0;\nint main(){\n cin>>x>>y>>z;\n cin>>xx>>yy>>zz;\n for(int i=0;i<6;i++)cin>>a[i];\n if(y<0)ans+=a[0];\n else if(y>yy)ans+=a[1];\n if(z<0)ans+=a[2];\n else if(z>zz)ans+=a[3];\n if(x<0)ans+=a[4];\n else if(x>xx)ans+=a[5];\n cout<\nint main()\n{\n\tint n, k, a, t = 0, count = 0;\n\tscanf(\"%d %d\", &n, &k);\n\tfor (int i = 0; i < n && k > 0; i++) {\n\t\tscanf(\"%d\", &a);\n\t\tt += a;\n\t\tif (t >= 8) {\n\t\t\tk -= 8;\n\t\t\tt -= 8;\n\t\t}\n\n\t\telse {\n\t\t\tk -= t;\n\t\t\tt -= t;\n\t\t}\n\t\tcount++;\n\t}\n\tif (k > 0 && n > 0) \n\t\tprintf(\"-1\");\n\n\telse \n\t\tprintf(\"%d\", count);\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "740338bc3c3de0ea8f83374612bc84ec", "src_uid": "24695b6a2aa573e90f0fe661b0c0bd3a", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\ntypedef pair pii;\ntypedef pair ppi;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef vector vii;\ntypedef vector vvii;\ntypedef vector vipi;\ntypedef pair pid;\ntypedef pair piii;\ntypedef pair pdd;\ntypedef pair pddpdd;\ntypedef pair pddd;\ntypedef pair ppddpddd;\ntypedef pair ppid;\nconst static int INF = 1000000000;\n\nint n,m;\nint total = 0;\nint p=0,v=0,p1=0,v1=0;\nint sebelum=-1;\nint ctr = 0;\nint main(){\n scanf(\"%d%d\",&n,&m);\n int a=n,b=m;\n while(n || m){\n if(sebelum == -1){\n sebelum = 2;\n m--;\n }else if(sebelum == 1){\n if(ctr%2){\n if(m > 0){\n v++;\n sebelum = 2;\n m--;\n }else{\n p++;\n n--;\n }\n }else{\n if(n > 0){\n p++;\n n--;\n }else{\n v++;\n sebelum = 2;\n m--;\n }\n }\n }else if(sebelum == 2){\n if(ctr%2){\n if(n > 0){\n v++;\n sebelum = 1;\n n--;\n }else{\n p++;\n sebelum = 2;\n m--;\n }\n }else{\n if(m > 0){\n p++;\n sebelum = 2;\n m--;\n }else{\n sebelum = 1;\n v++;\n n--;\n }\n }\n }\n ctr++;\n }\n ctr =0;\n sebelum = -1;\n while(a || b){\n if(sebelum == -1){\n sebelum = 1;\n a--;\n }else if(sebelum == 1){\n if(ctr%2){\n if(b > 0){\n v1++;\n sebelum = 2;\n b--;\n }else{\n p1++;\n a--;\n }\n }else{\n if(a > 0){\n p1++;\n a--;\n }else{\n v1++;\n sebelum = 2;\n b--;\n }\n }\n }else if(sebelum == 2){\n if(ctr%2){\n if(a > 0){\n v1++;\n sebelum = 1;\n a--;\n }else{\n p1++;\n b--;\n }\n }else{\n if(b > 0){\n p1++;\n b--;\n }else{\n sebelum = 1;\n v1++;\n a--;\n }\n }\n }\n ctr++;\n }\n if(p > p1){\n printf(\"%d %d\\n\",p,v);\n }else\n printf(\"%d %d\\n\",p1,v1);\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["greedy", "implementation", "games"], "code_uid": "aa175dcb4ad632321e37dfb0c44390f5", "src_uid": "c8378e6fcaab30d15469a55419f38b39", "difficulty": 1300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nmap data;\n\nint convertion(string number,int base){\n\tint ans = 1*data[number[number.size()-1]];\n\tint pot = base;\n\tfor (int i = number.size()-2; i >= 0; --i)\n\t{\n\t\tans += pot*data[number[i]];\n\t\tpot *= base;\n\t}\n\treturn ans;\n}\nint main(){\n\tdata['0'] = 0;\n\tdata['1'] = 1;\n\tdata['2'] = 2;\n\tdata['3'] = 3;\n\tdata['4'] = 4;\n\tdata['5'] = 5;\n\tdata['6'] = 6;\n\tdata['7'] = 7;\n\tdata['8'] = 8;\n\tdata['9'] = 9;\n\tdata['A'] = 10;\n\tdata['B'] = 11;\n\tdata['C'] = 12;\n\tdata['D'] = 13;\n\tdata['E'] = 14;\n\tdata['F'] = 15;\n\tdata['G'] = 16;\n\tdata['H'] = 17;\n\tdata['I'] = 18;\n\tdata['J'] = 19;\n\tdata['K'] = 20;\n\tdata['L'] = 21;\n\tdata['M'] = 22;\n\tdata['N'] = 23;\n\tdata['O'] = 24;\n\tdata['P'] = 25;\n\tdata['Q'] = 26;\n\tdata['R'] = 27;\n\tdata['S'] = 28;\n\tdata['T'] = 29;\n\tdata['U'] = 30;\n\tdata['V'] = 31;\n\tdata['W'] = 32;\n\tdata['X'] = 33;\n\tdata['Y'] = 34;\n\tdata['Z'] = 35;\n\tstring x;\n\tint y;\n\tcin >> x;\n\tint ini = INT_MAX;\n\tint end = -1;\n\tstring hour = \"\";\n\tstring minute = \"\";\n\tbool sw = false;\n\tfor (int i = 0; i < x.size(); ++i)\n\t{\n\t\tif(x[i]==':'){\n\t\t\tsw = true; continue;\n\t\t}\n\t\tif(!sw) hour += x[i];\n\t\telse minute += x[i];\n\t}\n\tfor (int i = 0; i < x.size(); ++i)\n\t\tend = max(end,data[x[i]]);\n\tvector ans;\n\tfor (int i = end+1; i <= 60; ++i)\n\t{\n\t\tint h = convertion(hour,i);\n\t\tint m = convertion(minute,i);\n\t\tif(h < 24 and m < 60) ans.push_back(i);\t\n\t}\n\n\tif(ans.size() == 0) cout << 0;\n\telse if(ans[ans.size()-1] == 60) cout << -1;\n\telse{\n\t\tfor (int i = 0; i < ans.size(); ++i)\n\t\t\tcout << ans[i] << \" \";\n\t\t\n\t}\n\treturn 0;\t\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "781005532764e53c882c2f08af0982d9", "src_uid": "c02dfe5b8d9da2818a99c3afbe7a5293", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \r\n#include \r\n#include \r\nusing namespace std;\r\nconst int inf = 0x3f3f3f3f;\r\nint read()\r\n{\r\n\tint x=0,f=1;char c;\r\n\twhile((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}\r\n\twhile(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}\r\n\treturn x*f;\r\n}\r\nint n,m,k,ans,a[7],b[7],c[7][7],dp[20000][7][7][5];\r\nstruct node {int a[7];node() {memset(a,0,sizeof a);} };\r\nint get(node s)\r\n{\r\n\tint x=0;\r\n\tfor(int i=1;i<=n;i++) x=x*5+s.a[i];\r\n\treturn x;\r\n}\r\nnode back(int x)\r\n{\r\n\tnode s;\r\n\tfor(int i=n;i>=1;i--) s.a[i]=x%5,x/=5;\r\n\treturn s;\r\n}\r\nint check(node s)\r\n{\r\n\tfor(int i=1;i<=n;i++)\r\n\t\tif(s.a[i]a[i]) f=0;\r\n\t\tif(!f) continue;//this state is illegal\r\n\t\tfor(int j=1;j<=m;j++)\r\n\t\t\tfor(int i=1;i<=n;i++)\r\n\t\t\t\tfor(int r=0;r<5;r++)\r\n\t\t\t\t{\r\n\t\t\t\t\tif(r>b[j]) break;\r\n\t\t\t\t\tfor(int f=0;f<5;f++)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif(r+f>b[j] || t.a[i]+f>a[i]) break;\r\n\t\t\t\t\t\tnode tt=t;tt.a[i]+=f;\r\n\t\t\t\t\t\tint ts=get(tt),ti=i+1,tj=j,tr=r+f,ad=f?c[i][j]:0;\r\n\t\t\t\t\t\tif(ti>n) ti=1,tj++,tr=0;\r\n\t\t\t\t\t\tif(check(tt)) ans=min(ans,dp[s][i][j][r]+ad);\r\n\t\t\t\t\t\tif(tj<=m)\r\n\t\t\t\t\t\t\tdp[ts][ti][tj][tr]=min(dp[ts][ti][tj][tr],dp[s][i][j][r]+ad);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t}\r\n\tif(ans>=inf) puts(\"-1\");\r\n\telse printf(\"%d\\n\",ans);\r\n}", "lang_cluster": "C++", "tags": ["brute force", "dfs and similar", "flows", "dp", "bitmasks"], "code_uid": "87d4209256de5037ad4a38c444b10883", "src_uid": "4dc5dc78bda59c1ec6dd8acd6f1d7333", "difficulty": 3200.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint main()\n{\n long long n,m,k;\n // top:\n cin>>n>>m>>k;\n int x=1,y=1;\n if(k\nint main(){\nint a,b,c,n,f=0;\nscanf(\"%d%d%d%d\",&a,&b,&c,&n);\nif(a>=c&&b>=c){\nf=n-((a+b)-c);}\nif(f>0){\n printf(\"%d\",f);\n}else\nprintf(\"-1\");\nreturn 0;}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "120912f8b19908c3894262a085cc38fb", "src_uid": "959d56affbe2ff5dd999a7e8729f60ce", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include //..1..2..3:Correct:Solved by F2tekkz:)\n#define ll long long\n#define str string\n#define sonic ios_base::sync_with_stdio(0)\nusing namespace std;\nint main(){\n\tsonic;\n\tll n , m , k , mini = 100000 , t;\n cin >> n >> m >> k;\n for(ll i = 0 ; i < n ; i ++){\n cin >> t;\n if(t <= k && t!= 0){\n\t\t\tmini = min(mini , abs(m - i - 1) * 10);\n\t\t}\n }\n cout << mini;\n}\n// * * * **** * * *\n// *\t* * * * * * * *\n// ***** ** **** ***** *****\n// * * * * * * * *\n// * * * * **** * * *****\n\n// **** **** * *\n// * ** * * *\n// **** ** * *****\n// * ** * * *\n// **** **** **** * *\n\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "5430db109b4cbba5e0186de2f007227b", "src_uid": "57860e9a5342a29257ce506063d37624", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#pragma comment(linker,\"/STACK:64000000\")\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair pii;\ntypedef pair pll;\ntypedef pair pdd;\ntypedef vector vi;\ntypedef vector vvi;\ntypedef vector vl;\ntypedef vector vvl;\n#define mp make_pair\n#define pb push_back\n#define eps (double)(1e-2)\n#define MOD (ll)(1e9 + 7)\n\nconst int maxlen = 1e7 + 10;\nconst int length_double = 100;\nconst int base = 10;\n#define PI 3.14159265358979323846\n#define name \"floor\"\n\nvector A;\nint main()\n{\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"rt\", stdin);\n\tfreopen(\"output.txt\", \"wt\", stdout);\n\t/*#else\n\tfreopen(name\".in\", \"rt\", stdin);\n\tfreopen(name\".out\", \"wt\", stdout);*/\n#endif\n\tint n;\n\tint i;\n\tscanf(\"%d\", &n);\n\tfor (i = 0; i < n; ++i)\n\t{\n\t\tint x;\n\t\tscanf(\"%d\", &x);\n\t\tA.push_back(x);\n\t}\n\tsort(A.begin(), A.end());\n\tprintf(\"%d\", A[n / 2]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "9187030538fc2c873011df21f66b99bc", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#define MAXN 103\nusing namespace std;\n\nint a[MAXN];\nint main()\n{\n\tint n;\n\tscanf(\"%d\",&n);\n\tfor(int i = 1;i <= n;i++)\n\t\tscanf(\"%d\",&a[i]);\n\tsort(a + 1,a + 1 + n);\n\tprintf(\"%d\\n\",a[(n + 1) / 2]);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "f19b16b0155b9b4e8796f4c9715e87ae", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": " #include \n using namespace std;\n int main()\n {\n int x;\n cin>>x;\n int a[x];\n for (int i = 0; i < x; ++i)\n {\n cin>>a[i];\n }\n sort(a,a+x);\n if(x==1)\n {\n cout<\n\nusing namespace std;\n\nint a[9];\nset st;\nmain()\n{\n int n;\n cin >> n;\n for (int i=1; i<=n; i++){\n cin >> a[i];\n st.insert(a[i]);\n }\n sort(a+1,a+n+1);\n cout << a[(n+1)/2];\n\n}\n\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "f279db1a40b62d349ba094c455f91a2f", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#define _CRT_SECURE_NO_WARNINGS\n\n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\nint main()\n{\n#ifdef _DEBUG\n\tfreopen(\"in.txt\", \"r\", stdin);\n\tfreopen(\"out.txt\", \"w\", stdout);\n#endif\n\tint n; cin >> n;\n\tvector a(n);\n\tfor (int i = 0; i < n; i++)\n\t\tcin >> a[i];\n\tsort(a.begin(), a.end());\n\tcout << a[n / 2] << endl;\n\t\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "7695006e4f8d920fe34b008adfc29384", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n\nusing namespace std;\n\nint main(){\n int n, res = 0;\n cin >> n;\n for(int i = 0, a; i < n; i++){\n cin >> a;\n res += a;\n }\n cout << res / n;\n return 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "24e81480b446ea2030c023b3f4b6d80b", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include \n\nusing std::cin;\nusing std::cout;\n\nint a[5];\nint main()\n{\n\tint n = 0;\n\tint s = 0;\n\tcin >> n;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin >> a[i];\n\t\ts += a[i];\n\t}\n\tcout << (s / n);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "fdfc83c8718167dbdca7711354e6d734", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef unsigned int ui;\n#define INF 1000000007\n#define MOD 1000000007\n#define INF_LONG 1e18\n#define MOD_LONG 1e18\n#define pii pair\n#define pli pair\n#define pll pair\n#define mp make_pair\n#define all(x) (x).begin(), (x).end()\nconst int dr[4] = { 0,0,-1,1 }, dc[4] = { -1,1,0,0 };\nclass Dijkstra {\n\tint N;\n\tvector > > E;\npublic:\n\tDijkstra(int n) {\n\t\tN = n;\n\t\tE = vector > >(N + 1);\n\t}\n\tvoid insert(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t}\n\tvector dijkstra(int start, int end) {\n\t\tvector dist(N + 1, INF);\n\t\tdist[start] = 0;\n\t\tpriority_queue > pq;\n\t\tpq.push(make_pair(0, start));\n\t\twhile (!pq.empty()) {\n\t\t\tint w = -pq.top().first;\n\t\t\tint u = pq.top().second;\n\t\t\tif (u == end) return dist;\n\t\t\tpq.pop();\n\t\t\tif (dist[u] < w) continue;\n\t\t\tfor (int i = 0; i < E[u].size(); i++) {\n\t\t\t\tint v = E[u][i].first;\n\t\t\t\tint w2 = w + E[u][i].second;\n\t\t\t\tif (dist[v] > w2) {\n\t\t\t\t\tdist[v] = w2;\n\t\t\t\t\tpq.push(make_pair(-w2, v));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\tvoid g() {\n\t\tint i, n, e, s, u, v, w; vector ans;\n\t\tscanf(\"%d%d\", &n, &e);\n\t\tDijkstra dk(n);\n\t\tscanf(\"%d\", &s);\n\t\tfor (i = 0; i < e; i++) {\n\t\t\tscanf(\"%d%d%d\", &u, &v, &w);\n\t\t\tdk.insert(u, v, w);\n\t\t}\n\t\tans = dk.dijkstra(s, N);\n\t\tfor (i = 1; i <= n; i++) {\n\t\t\tif (ans[i] >= INF) printf(\"INF\\n\");\n\t\t\telse printf(\"%d\\n\", ans[i]);\n\t\t}\n\t}\n};\nclass Prim {\n\tint N;\n\tvector > > E;\npublic:\n\tPrim(int n) {\n\t\tN = n;\n\t\tE = vector > >(N + 1);\n\t}\n\tvoid insert(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t}\n\tvector execute(ll &cost) {\n\t\tint k;\n\t\tvector dist(N + 1, INF);\n\t\tvector selected(N + 1, false);\n\t\tdist[1] = 0; cost = 0;\n\t\tpriority_queue > pq;\n\t\tpq.push(make_pair(0, 1));\n\t\tfor (k = 0; k < N; k++) {\n\t\t\tint w = -pq.top().first;\n\t\t\tint u = pq.top().second;\n\t\t\tpq.pop();\n\t\t\tif (selected[u] == true) {\n\t\t\t\tk--; continue;\n\t\t\t}\n\t\t\tcost += w; selected[u] = true;\n\t\t\tfor (int i = 0; i < E[u].size(); i++) {\n\t\t\t\tint v = E[u][i].first;\n\t\t\t\tint w2 = E[u][i].second;\n\t\t\t\tif (selected[v] == true) continue;\n\t\t\t\tif (dist[v] > w2) {\n\t\t\t\t\tdist[v] = w2;\n\t\t\t\t\tpq.push(make_pair(-w2, v));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\tvoid g() {\n\t\tint i, n, e, s, u, v, w; ll cost; vector ans;\n\t\tscanf(\"%d%d\", &n, &e);\n\t\tPrim pm(n);\n\t\tscanf(\"%d\", &s);\n\t\tfor (i = 0; i < e; i++) {\n\t\t\tscanf(\"%d%d%d\", &u, &v, &w);\n\t\t\tpm.insert(u, v, w);\n\t\t}\n\t\tans = pm.execute(cost);\n\t\tprintf(\"%d\", cost);\n\t}\n};\nclass Floyd {\n\tint N;\n\tvector > E;\npublic:\n\tFloyd(int n) {\n\t\tN = n;\n\t\tE = vector >(N + 1, vector(N + 1, INF));\n\t}\n\tvoid setRC(int r, int c, int w) {\n\t\tE[r][c] = w;\n\t}\n\tint getRC(int r, int c) {\n\t\treturn E[r][c];\n\t}\n\tvoid execute() {\n\t\tint i, j, k;\n\t\tfor (i = 1; i <= N; ++i) E[i][i] = 0;\n\t\tfor (k = 1; k <= N; ++k) for (i = 1; i <= N; ++i) for (j = 1; j <= N; ++j)\n\t\t\tE[i][j] = min(E[i][j], E[i][k] + E[k][j]);\n\t}\n};\nclass DFS_Graph {\n\tvector > > E; vector A;\n\tint N;\npublic:\n\tDFS_Graph(int n) {\n\t\tN = n;\n\t\tE = vector > >(N + 1);\n\t\tA = vector(N + 1);\n\t}\n\tvoid insert(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t}\n\tvoid insert_fb(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t\tE[v].push_back(make_pair(u, w));\n\t}\n\tint dfsAll(int start, int end) {\n\t\tvector visited = vector(N + 1, false);\n\t\treturn dfs(start, end, visited);\n\t}\n\tint dfs(int start, int end, vector &visited) {\n\t\tif (start == end) return 1;\n\t\tvisited[start] = true;\n\t\tfor (int i = 0; i < E[start].size(); i++) {\n\t\t\tint v = E[start][i].first, c = E[start][i].second;\n\t\t\tif (!visited[v]) {\n\t\t\t\tif (dfs(v, end, visited)) return 1;\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\tvoid setA(int a, int b) {\n\t\tA[a] = b;\n\t}\n\tint dfsAll2() {\n\t\tvector visited(N + 1, 0);\n\t\tint i, j, seq = 0, ret = 0, a;\n\t\tfor (i = 1; i <= N; i++) dfs2(i, visited, ++seq);\n\t\tfor (i = 1; i <= N; i++) {\n\t\t\tfor (j = a = 0; j < E[i].size(); j++) {\n\t\t\t\ta += E[i][j].second;\n\t\t\t}\n\t\t\tif (ret < a) ret = a;\n\t\t}\n\t\treturn ret;\n\t}\n\tint dfs2(int start, vector &visited, ll seq) {\n\t\tvisited[start] = seq;\n\t\tint ret = A[start];\n\t\tfor (int i = 0; i < E[start].size(); i++) {\n\t\t\tint v = E[start][i].first; ll c = E[start][i].second;\n\t\t\tif (visited[v] < seq) {\n\t\t\t\tif (E[start][i].second == -1) E[start][i].second = dfs2(v, visited, seq);\n\t\t\t\tif (ret < E[start][i].second) ret = E[start][i].second;\n\t\t\t}\n\n\t\t}\n\t\treturn ret;\n\t}\n\tvoid g() {\n\t\tint i, j, N, m, a, b, c, a2, b2, c2, lo, hi, mid, tmp, start, end;\n\t\tscanf(\"%d%d\", &N, &m);\n\t\tDFS_Graph dg(N);\n\t\tfor (i = 0; i < m; i++) {\n\t\t\tscanf(\"%d%d\", &a, &b);\n\t\t\tdg.insert(a, b, 0);\n\t\t}\n\t\tscanf(\"%d\", &m);\n\t\tfor (i = 0; i < m; i++) {\n\t\t\tscanf(\"%d%d\", &a, &b);\n\t\t\tif (dg.dfsAll(a, b)) c = -1;\n\t\t\telse if (dg.dfsAll(b, a)) c = 1;\n\t\t\telse c = 0;\n\t\t\tprintf(\"%d\\n\", c);\n\t\t}\n\t}\n};\nclass DFS_Map {\n\tvector > E;\n\tint R, C;\npublic:\n\tDFS_Map(int r, int c) {\n\t\tR = r; C = c;\n\t\tE = vector >(R + 2, vector(C + 2, 0));\n\t}\n\tvoid setRC(int r, int c, int w) {\n\t\tE[r][c] = w;\n\t}\n\tint getRC(int r, int c) {\n\t\treturn E[r][c];\n\t}\n\tvoid dfs(int rr, int cc, vector > &visited) {\n\t\tint i, r, c, v;\n\t\tvisited[rr][cc] = true;\n\t\tfor (i = 0; i < 4; i++) {\n\t\t\tr = rr + dr[i]; c = cc + dc[i];\n\t\t\tif (E[r][c] && !visited[r][c]) {\n\t\t\t\tdfs(r, c, visited);\n\t\t\t}\n\t\t}\n\t}\n\tvoid dfsAll() {\n\t\tvector > visited = vector >(R + 2, vector(C + 2, 0));\n\t\tint r, c;\n\t\tfor (r = 1; r <= R; r++) for (c = 1; c <= C; c++) if (!visited[r][c]) dfs(r, c, visited);\n\t}\n};\nclass BFS_Graph {\n\tvector > > E; vector IN;\n\tint N;\npublic:\n\tint is_cycle() { // topologicalSort() 수행후 IN[v]>0인 node가 존재하면 cycle이 존재한다고 볼수있다 \n\t\tfor (int i = 1; i <= N; i++) if (IN[i]>0) return 1;\n\t\treturn 0;\n\t}\n\tBFS_Graph(int n) {\n\t\tN = n;\n\t\tE.clear(); IN.clear();\n\t\tE = vector > >(N + 1);\n\t\tIN = vector(N + 1, 0);\n\t}\n\tvoid insert(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t\tIN[v]++;\n\t}\n\tvoid insert_fb(int u, int v, int w) {\n\t\tE[u].push_back(make_pair(v, w));\n\t\tE[v].push_back(make_pair(u, w));\n\t}\n\tvector bfs(int start, int end, int min_weight) {\n\t\tvector visited = vector(N + 1, false);\n\t\tvector weight(N + 1, INF_LONG);\n\t\tqueue Q;\n\t\tvisited[start] = true; weight[start] = 0;\n\t\tQ.push(start);\n\t\twhile (!Q.empty()) {\n\t\t\tint u = Q.front(), w = weight[u]; if (u == end) break;\n\t\t\tQ.pop();\n\t\t\tfor (int i = 0; i < E[u].size(); i++) {\n\t\t\t\tint v = E[u][i].first, c = E[u][i].second;\n\t\t\t\tif (c >= min_weight && !visited[v]) {\n\t\t\t\t\tQ.push(v); weight[v] = w + c;\n\t\t\t\t\tvisited[v] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn weight;\n\t}\n\tvector topologicalSort() {\n\t\tvector order;\n\t\tqueue Q;\n\t\tfor (int i = 1; i <= N; i++) if (!IN[i]) Q.push(i);\n\t\twhile (!Q.empty()) {\n\t\t\tint u = Q.front();\n\t\t\tQ.pop(); order.push_back(u);\n\t\t\tfor (int i = 0; i < E[u].size(); i++) {\n\t\t\t\tint v = E[u][i].first;\n\t\t\t\tif (IN[v]>0) {\n\t\t\t\t\tIN[v]--;\n\t\t\t\t\tif (!IN[v]) Q.push(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn order;\n\t}\n\tvector topologicalSortPQ() {\n\t\tvector order;\n\t\tpriority_queue pq;\n\t\tfor (int i = 1; i <= N; i++) if (!IN[i]) pq.push(-i);\n\t\twhile (!pq.empty()) {\n\t\t\tint u = -pq.top();\n\t\t\tpq.pop(); order.push_back(-u);\n\t\t\tfor (int i = 0; i < E[u].size(); i++) {\n\t\t\t\tint v = E[u][i].first;\n\t\t\t\tif (IN[v]>0) {\n\t\t\t\t\tIN[v]--;\n\t\t\t\t\tif (!IN[v]) pq.push(-v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn order;\n\t}\n\tint f() {\n\t\tint v, u, i; vector IN2;\n\t\tfor (u = 1; u <= N; u++) {\n\t\t\tfor (i = 0; i > X; vector y;\n\t\tscanf(\"%d%d\", &N, &m);\n\t\tBFS_Graph bg(N); X.reserve(m);\n\t\tfor (i = 0; i < m; i++) {\n\t\t\tscanf(\"%d%d%d\", &a, &b, &c);\n\t\t\tif (a > b) tmp = a, a = b, b = tmp;\n\t\t\tX.push_back(make_tuple(a, b, -c));\n\t\t}\n\t\tscanf(\"%d%d\", &start, &end);\n\t\tsort(X.begin(), X.end());\n\t\tbg.insert_fb(get<0>(X[0]), get<1>(X[0]), -get<2>(X[0]));\n\t\tfor (i = 1; i < m; i++) {\n\t\t\ta = get<0>(X[i - 1]); b = get<1>(X[i - 1]); c = get<2>(X[i - 1]);\n\t\t\ta2 = get<0>(X[i]); b2 = get<1>(X[i]); c2 = get<2>(X[i]);\n\t\t\tif (a == a2 && b == b2) continue;\n\t\t\tbg.insert_fb(a2, b2, -c2);\n\t\t}\n\t\tlo = 1; hi = INF;\n\t\twhile (lo < hi) {\n\t\t\tmid = (lo + hi + 1) / 2;\n\t\t\ty = bg.bfs(start, end, mid);\n\t\t\tif (y[end] < INF_LONG) {\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t\telse {\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\", lo);\n\t}\n};\nclass BFSMap {\n\tvector > E;\n\tint R, C;\npublic:\n\tBFSMap(int r, int c) {\n\t\tR = r; C = c;\n\t\tE = vector >(R + 2, vector(C + 2, 0));\n\t}\n\tvoid setRC(int r, int c, int w) {\n\t\tE[r][c] = w;\n\t}\n\tint getRC(int r, int c) {\n\t\treturn E[r][c];\n\t}\n\tint bfsAll() {\n\t\tint r, c, ret = 0;\n\t\tvector > visited = vector >(R + 2, vector(C + 2, 0));\n\t\tfor (r = 1; r <= R; r++) {\n\t\t\tfor (c = 1; c <= C; c++) {\n\t\t\t\tif (E[r][c] && !visited[r][c]) {\n\t\t\t\t\tret++; bfs(r, c, visited);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tint bfsAll(vector > &Q) {\n\t\tint i, r, c, ret = 0;\n\t\tvector > visited = vector >(R + 2, vector(C + 2, 0));\n\t\tfor (i = 0; i < Q.size(); i++) {\n\t\t\tr = Q[i].first; c = Q[i].second;\n\t\t\tif (!visited[r][c]) {\n\t\t\t\tret++; bfs(r, c, visited);\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tvoid bfs(int rr, int cc, vector > &visited) {\n\t\tint i, r, c, r2, c2;\n\t\tqueue > Q;\n\t\tQ.push(make_pair(rr, cc)); visited[rr][cc] = 1;\n\t\twhile (!Q.empty()) {\n\t\t\tr = Q.front().first;\n\t\t\tc = Q.front().second;\n\t\t\tQ.pop();\n\t\t\tfor (i = 0; i < 4; i++) {\n\t\t\t\tr2 = r + dr[i]; c2 = c + dc[i];\n\t\t\t\tif (E[r2][c2] && !visited[r2][c2]) {\n\t\t\t\t\tQ.push(make_pair(r2, c2)); visited[r2][c2] = visited[r][c] + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvoid g() {\n\t\tint i, j, r, c, r2, c2, R, C, a, ans = 0, idx = 0; vector > Q[2]; vector X;\n\t\tscanf(\"%d%d\", &R, &C);\n\t\tBFSMap bm(R, C);\n\t\tX.reserve(10004); Q[0].reserve(10004); Q[1].reserve(10004);\n\t\tfor (r = 1; r <= R; r++) for (c = 1; c <= C; c++) {\n\t\t\tscanf(\"%d\", &a); bm.setRC(r, c, a); if (a) Q[0].push_back(make_pair(r, c));\n\t\t}\n\t\twhile (1) {\n\t\t\ta = bm.bfsAll(Q[idx]);\n\t\t\tif (a > 1) break;\n\t\t\telse if (a == 0) {\n\t\t\t\tans = 0; break;\n\t\t\t}\n\t\t\tans++; X.resize(0); Q[1 - idx].resize(0);\n\t\t\tfor (i = 0; i < Q[idx].size(); i++) {\n\t\t\t\tr = Q[idx][i].first; c = Q[idx][i].second; a = 0;\n\t\t\t\tfor (j = 0; j < 4; j++) {\n\t\t\t\t\tr2 = r + dr[j]; c2 = c + dc[j];\n\t\t\t\t\tif (bm.getRC(r2, c2) == 0) a++;\n\t\t\t\t}\n\t\t\t\tX.push_back(a);\n\t\t\t}\n\t\t\tfor (i = 0; i < Q[idx].size(); i++) {\n\t\t\t\tr = Q[idx][i].first; c = Q[idx][i].second; a = bm.getRC(r, c) - X[i];\n\t\t\t\tif (a <= 0) {\n\t\t\t\t\ta = 0;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tQ[1 - idx].push_back(make_pair(r, c));\n\t\t\t\t}\n\t\t\t\tbm.setRC(r, c, a);\n\t\t\t}\n\t\t\tidx = 1 - idx;\n\t\t}\n\t\tprintf(\"%d\", ans);\n\t}\n};\nclass Number {\npublic:\n\tNumber() {\n\n\t}\n\tint getDigitNum(ll xx) {\n\t\tll x; int ret = 0;\n\t\tfor (x = 1; x < xx; x *= 10, ret++);\n\t\treturn ret;\n\t}\n\tint getDigitSum(int xx) {\n\t\tint x, a = 0;\n\t\tfor (x = xx; x; x /= 10) a += (x % 10);\n\t\treturn a;\n\t}\n\tint getMSB(ll xx) {\n\t\tint i;\n\t\tfor (i = 0; (1ll << i) <= xx; i++);\n\t\treturn i - 1;\n\t}\n\tll convertBinaryToBase(ll x, int base) {\n\t\tif (x == 0) return 0;\n\t\treturn base * convertBinaryToBase(x / 2, base) + (x % 2);\n\t}\n\tll findFactor(ll k) {\n\t\tfor (ll d = 2; d * d <= k; d++) if (k % d == 0) return d;\n\t\treturn 0;\n\t}\n};\nclass Palindrome {\npublic:\n\tPalindrome() {\n\n\t}\n\tint isPalindrome(ll xx) {\n\t\tll x; int i, n; vector v;\n\t\tfor (x = xx; x > 0; x /= 10) {\n\t\t\tv.push_back(x % 10);\n\t\t}\n\t\tn = v.size();\n\t\tfor (i = 0; i < n / 2; i++) {\n\t\t\tif (v[i] != v[n - i - 1]) return 0;\n\t\t}\n\t\treturn 1;\n\t}\n};\nclass BigNumber {\npublic:\n\tBigNumber() {\n\n\t}\n\tstring add(string &s1, string &s2) {\n\t\tint i, j, k, n1, n2, a; string s3; vector v;\n\t\tn1 = s1.size();\n\t\tn2 = s2.size();\n\t\tv.reserve(max(n1, n2) + 1);\n\t\tfor (i = n1 - 1, j = n2 - 1; i >= 0 && j >= 0; i--, j--) {\n\t\t\ta = s1[i] - '0' + s2[j] - '0';\n\t\t\tv.push_back(a);\n\t\t}\n\t\twhile (i >= 0) {\n\t\t\ta = s1[i] - '0';\n\t\t\tv.push_back(a);\n\t\t\ti--;\n\t\t}\n\t\twhile (j >= 0) {\n\t\t\ta = s2[j] - '0';\n\t\t\tv.push_back(a);\n\t\t\tj--;\n\t\t}\n\t\tfor (i = 0, a = 0; i < v.size(); i++) {\n\t\t\ta += v[i];\n\t\t\tif (a >= 10) {\n\t\t\t\tv[i] = a - 10;\n\t\t\t\ta = 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tv[i] = a;\n\t\t\t\ta = 0;\n\t\t\t}\n\t\t}\n\t\tif (a) v.push_back(a);\n\t\ts3.reserve(v.size());\n\t\tfor (i = v.size() - 1; i >= 0; i--) s3 += (char)(v[i] + '0');\n\t\ts3 += '\\n';\n\t\treturn s3;\n\t}\n};\nclass PrimeNumber {\n\tint N; vector NP, P;\n\tll _powmod(ll a, ll b) {\n\t\tif (b == 0) return 1;\n\t\tif (b & 1) return a * _powmod(a, b - 1) % MOD;\n\t\treturn _powmod(a * a % MOD, b / 2);\n\t}\npublic:\n\tPrimeNumber() {\n\n\t}\n\tPrimeNumber(int n) {\n\t\tN = n;\n\t\tNP.reserve(N + 4);\n\t\tP.reserve(N / 4);\n\t\tfor (int i = 2; i*i <= N; i++) {\n\t\t\tif (NP[i]) continue;\n\t\t\tfor (int j = i*i; j <= N; j += i) NP[i] = 1;\n\t\t}\n\t\tfor (int i = 2; i <= N; i++) if (!NP[i]) P.push_back(i);\n\t}\n\n\tll gcd(ll x, ll y) {\n\t\tll z;\n\t\twhile (x) {\n\t\t\tz = y%x;\n\t\t\ty = x;\n\t\t\tx = z;\n\t\t}\n\t\treturn y;\n\t}\n\n\tll powermod(ll p, ll q) {\n\t\tll g = gcd(p, q);\n\t\tp /= g; q /= g;\n\t\treturn p * _powmod(q, MOD - 2) % MOD;\n\t}\n\n\tvector factorization(int x) {\n\t\tvector ret; int i;\n\t\tif (x == 1) {\n\t\t\tret.push_back(1);\n\t\t\treturn ret;\n\t\t}\n\t\tret.push_back(1); ret.push_back(x);\n\t\tfor (i = 2; i*i < x; i++) {\n\t\t\tif (x%i == 0) {\n\t\t\t\tret.push_back(i);\n\t\t\t\tret.push_back(x / i);\n\t\t\t}\n\t\t}\n\t\tif (i*i == x) ret.push_back(i);\n\t\treturn ret;\n\t}\n\n\tint factorization2(int x) {\n\t\tint ret = 0, i;\n\t\tif (x == 1) {\n\t\t\treturn 1;\n\t\t}\n\t\tret = 2;\n\t\tfor (i = 2; i*i < x; i++) {\n\t\t\tif (x%i == 0) {\n\t\t\t\tret += 2;\n\t\t\t}\n\t\t}\n\t\tif (i*i == x) ret++;\n\t\treturn ret;\n\t}\n};\nclass BitOperation {\npublic:\n\tBitOperation() {\n\n\t}\n\tint getOneBitNum(int xx) {\n\t\tint a = 0, x;\n\t\tfor (x = xx; x; x = x&(x - 1)) a++;\n\t\treturn a;\n\t}\n};\nclass Comb {\n\tvector > C; vector F; int N;\n#define MOD_COMB MOD\npublic:\n\tComb() {\n\n\t}\n\tComb(int n) {\n\t\tinit(n);\n\t}\n\tvoid init(int n) {\n\t\tN = n;\n\t\tC.resize(N + 1);\n\t\tfor (int i = 0; i <= N; i++) C[i].resize(i + 1);\n\t\tfor (int i = 0; i <= N; i++) C[i][i] = C[i][0] = 1;\n\t\tfor (int i = 1; i <= N; i++) for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD_COMB;\n\t\tF.resize(N + 1);\n\t\tF[0] = F[1] = 1;\n\t\tfor (int i = 1; i <= n; i++) F[i] = (F[i - 1] * i) % MOD_COMB;\n\t}\n\tll getC(int n, int r) {\n\t\treturn C[n][r];\n\t}\n\tll getF(int n) {\n\t\treturn F[n];\n\t}\n\tll get_comb(int n, int r) {\n\t\tif (n == r || r == 0) return 1;\n\t\tif (n <= 1000) return getC(n, r);\n\t\tll ret = 1; int i;\n\t\tif (r > n / 2) r = n - r;\n\t\tfor (i = n; i > n - r; i--) ret *= i;\n\t\tfor (i = 2; i <= r; i++) ret /= i;\n\t\treturn ret;\n\t}\n};\nclass Sort {\n\tvector tmp;\n\tint N;\npublic:\n\tSort(int n) {\n\t\tN = n;\n\t\ttmp.reserve(n + 4);\n\t}\n\tvoid merge_sort(vector &x, int lo, int hi) {\n\t\tif (lo >= hi) return;\n\t\tint i, j, k, mid = (lo + hi) / 2;\n\t\tmerge_sort(x, lo, mid); merge_sort(x, mid + 1, hi);\n\t\tfor (i = lo, j = mid + 1, k = lo; i <= mid && j <= hi; k++) {\n\t\t\tif (x[i] makeSum(const vector &a, int n) {\n\t\tvector ret(a.size());\n\t\tret[0] = a[0];\n\t\tfor (int i = 1; i < a.size(); ++i)\n\t\t\tret[i] = ret[i - 1] + a[i];\n\t\treturn ret;\n\t}\n\tvector > makeSum(const vector > &a, int n, int m) {\n\t\tvector > ret(n, vector(m));\n\t\tint i, j;\n\t\tfor (i = 0; i < n; i++) ret[i][0] = a[i][0];\n\t\tfor (i = 0; i < n; i++) {\n\t\t\tfor (j = 1; j < m; j++) {\n\t\t\t\tret[i][j] = ret[i][j - 1] + a[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tint getSum(const vector& psum, int a, int b) {\n\t\tif (a == 0) return psum[b];\n\t\treturn psum[b] - psum[a - 1];\n\t}\n\tint getSum(const vector >&psum, int r1, int c1, int r2, int c2) {\n\t\tint ret = psum[r2][c2];\n\t\tif (r1 > 0) ret -= psum[r1 - 1][c2];\n\t\tif (c1 > 0) ret -= psum[r2][c1 - 1];\n\t\tif (r1 > 0 && c1 > 0) ret += psum[r1 - 1][c1 - 1];\n\t\treturn ret;\n\t}\n};\nclass DisjointSet {\n\tint N;\n\tvector P, R, A;\npublic:\n\tDisjointSet(int n) {\n\t\tN = n;\n\t\tP = vector(n + 1);\n\t\tR = vector(n + 1);\n\t\tA = vector(n + 1);\n\t\tfor (int i = 1; i <= n; i++) P[i] = i, R[i] = 0, A[i] = 1;\n\t}\n\tint getA(int x) {\n\t\treturn A[x];\n\t}\n\tint find_set(int x) {\n\t\tif (P[x] == x) return x;\n\t\tint pa = find_set(P[x]);\n\t\treturn P[x] = pa;\n\t}\n\tint union_set(int x, int y) {\n\t\tint px, py;\n\t\tpx = find_set(x); py = find_set(y);\n\t\tif (px == py) return px;\n\t\tif (R[px]>R[py]) {\n\t\t\tP[py] = px;\n\t\t\tA[px] += A[py];\n\t\t\treturn px;\n\t\t}\n\t\telse {\n\t\t\tP[px] = py;\n\t\t\tA[py] += A[px];\n\t\t\tif (R[px] == R[py]) R[py]++;\n\t\t\treturn py;\n\t\t}\n\t}\n};\nclass LIS { // 0-based index\n\tint N, BI;\npublic:\n\tLIS(int n) {\n\t\tN = n;\n\t}\n\tint execute(vector &A) { // nlogn\n\t\tint i, lo, hi, x, vi;\n\t\tvector v = vector(N, 0);\n\t\tvi = -1;\n\t\tv[++vi] = A[0];\n\t\tfor (i = 1; i < N; i++) {\n\t\t\tif (A[i] > v[vi]) {\n\t\t\t\tv[++vi] = A[i];\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (lo = 0, hi = vi; lo < hi; ) {\n\t\t\t\tif (v[(lo + hi) / 2] >= A[i]) hi = (lo + hi) / 2;\n\t\t\t\telse lo = (lo + hi) / 2 + 1;\n\t\t\t}\n\t\t\tv[hi] = A[i];\n\t\t}\n\t\treturn vi + 1;\n\t}\n\n\tvector execute2(vector &A, int &len) { // nlogn\n\t\tvector v, v1, p, ret; int i, j; // 0-based index\n\t\tv.push_back(A[0]); v1.push_back(0);\n\t\tp = vector(N, -1);\n\t\tfor (int i = 1; i < A.size(); i++) {\n\t\t\tif (v.back() < A[i]) {\n\t\t\t\tp[i] = v1.back();\n\t\t\t\tv.push_back(A[i]);\n\t\t\t\tv1.push_back(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvector::iterator itr = lower_bound(v.begin(), v.end(), A[i]);\n\t\t\t\t*itr = A[i];\n\t\t\t\tint idx = itr - v.begin();\n\t\t\t\tif (idx != 0) {\n\t\t\t\t\tp[i] = v1[idx - 1];\n\t\t\t\t}\n\t\t\t\tv1[idx] = i;\n\t\t\t}\n\t\t}\n\t\tlen = v1.size();\n\t\tret = vector(len);\n\t\tfor (i = v1.back(), j = len - 1; i != -1; i = p[i], j--) ret[j] = A[i];\n\t\treturn ret;\n\t}\n\n\tint execute3(vector &A) { // n^2\n\t\tint i, j, ret = 0;\n\t\tvector v = vector(N, 0);\n\t\tfor (i = 0; iret) ret = v[i];\n\t\treturn ret;\n\t}\n};\nclass SegmentTreeSum {\n\tvector ST; vector A; int N;\n\n\tll _init(vector&a, int left, int right, int node) {\n\t\tif (left == right) return ST[node] = a[left];\n\t\tint mid = (left + right) / 2;\n\t\tll lv = _init(a, left, mid, node * 2), rv = _init(a, mid + 1, right, node * 2 + 1);\n\t\treturn ST[node] = lv + rv;\n\t}\n\n\tll _query(int left, int right, int node, int nodeLeft, int nodeRight) {\n\t\tif (right(n * 4 + 4);\n\t\tA = vector(n + 4);\n\t}\n\n\tvoid setA(int x, int y) {\n\t\tA[x] = y;\n\t}\n\n\tvoid init() {\n\t\t_init(A, 1, N, 1);\n\t}\n\n\tll query(int left, int right) {\n\t\treturn _query(left, right, 1, 1, N);\n\t}\n\n\tll update(int index, int newValue) {\n\t\treturn _update(index, newValue, 1, 1, N);\n\t}\n};\nclass SegmentTreeMin {\n\tvector ST; vector A; int N;\n\n\tll _init(vector&a, int left, int right, int node) {\n\t\tif (left == right) return ST[node] = a[left];\n\t\tint mid = (left + right) / 2;\n\t\tll lv = _init(a, left, mid, node * 2), rv = _init(a, mid + 1, right, node * 2 + 1);\n\t\treturn ST[node] = min(lv, rv);\n\t}\n\n\tll _query(int left, int right, int node, int nodeLeft, int nodeRight) {\n\t\tif (right(n * 4 + 4);\n\t\tA = vector(n + 4);\n\t}\n\n\tvoid setA(int x, int y) {\n\t\tA[x] = y;\n\t}\n\n\tvoid init() {\n\t\t_init(A, 1, N, 1);\n\t}\n\n\tll query(int left, int right) {\n\t\treturn _query(left, right, 1, 1, N);\n\t}\n\n\tll update(int index, int newValue) {\n\t\treturn _update(index, newValue, 1, 1, N);\n\t}\n};\nclass SegmentTreeMax {\n\tvector ST; vector A; int N;\n\n\tll _init(vector&a, int left, int right, int node) {\n\t\tif (left == right) return ST[node] = a[left];\n\t\tint mid = (left + right) / 2;\n\t\tll lv = _init(a, left, mid, node * 2), rv = _init(a, mid + 1, right, node * 2 + 1);\n\t\treturn ST[node] = max(lv, rv);\n\t}\n\n\tll _query(int left, int right, int node, int nodeLeft, int nodeRight) {\n\t\tif (right(n * 4 + 4);\n\t\tA = vector(n + 4);\n\t}\n\n\tvoid setA(int x, int y) {\n\t\tA[x] = y;\n\t}\n\n\tvoid init() {\n\t\t_init(A, 1, N, 1);\n\t}\n\n\tll query(int left, int right) {\n\t\treturn _query(left, right, 1, 1, N);\n\t}\n\n\tll update(int index, int newValue) {\n\t\treturn _update(index, newValue, 1, 1, N);\n\t}\n};\nstruct Point {\n\tint x, y, mx;\n\tPoint() {}\n\tPoint(int x, int y, int mx) : x(x), y(y), mx(mx) {}\n\tbool operator < (const Point& other) const {\n\t\treturn mx < other.mx;\n\t}\n};\nclass SegmentTree2D { // 2D Segment Tree node\n\tvector > P; // container for 2D grid\n\tint N, M;\n\tvector T;\n\npublic:\n\tSegmentTree2D(int n, int m) {\n\t\tN = n; M = m;\n\t\tP = vector >(n + 1, vector(m + 1));\n\t\tT = vector(n * m * 2);\n\t\tinit();\n\t\tbuild(1, 1, 1, n, m);\n\t}\n\tvoid init() {\n\t\tfor (int i = 1; i <= N; i++)\n\t\t\tfor (int j = 1; j <= M; j++)\n\t\t\t\tscanf(\"%d\", &P[i][j]);\n\t}\n\t// build a 2D segment tree from data [ (a1, b1), (a2, b2) ], O(n logn)\n\tPoint build(int node, int a1, int b1, int a2, int b2) {\n\t\tif (a1 > a2 || b1 > b2) // out of range\n\t\t\treturn def();\n\n\t\t// if it is only a single index, assign value to node\n\t\tif (a1 == a2 && b1 == b2)\n\t\t\treturn T[node] = Point(a1, b1, P[a1][b1]);\n\n\t\t// split the tree into four segments\n\t\tT[node] = def();\n\t\tT[node] = maxNode(T[node], build(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2));\n\t\tT[node] = maxNode(T[node], build(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2));\n\t\tT[node] = maxNode(T[node], build(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2));\n\t\tT[node] = maxNode(T[node], build(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2));\n\t\treturn T[node];\n\t}\n\t// query from range [ (x1, y1), (x2, y2) ], Time: O(logn)\n\tPoint query(int x1, int y1, int x2, int y2) {\n\t\treturn _query(1, 1, 1, N, M, x1, y1, x2, y2);\n\t}\n\t// helper function for query(int, int, int, int);\n\tPoint _query(int node, int a1, int b1, int a2, int b2, int x1, int y1, int x2, int y2) {\n\t\t// if we out of range, return dummy\n\t\tif (x1 > a2 || y1 > b2 || x2 < a1 || y2 < b1 || a1 > a2 || b1 > b2)\n\t\t\treturn def();\n\n\t\t// if it is within range, return the node\n\t\tif (x1 <= a1 && y1 <= b1 && a2 <= x2 && b2 <= y2)\n\t\t\treturn T[node];\n\n\t\t// split into four segments\n\t\tPoint mx = def();\n\t\tmx = maxNode(mx, _query(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x1, y1, x2, y2));\n\t\tmx = maxNode(mx, _query(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x1, y1, x2, y2));\n\t\tmx = maxNode(mx, _query(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x1, y1, x2, y2));\n\t\tmx = maxNode(mx, _query(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x1, y1, x2, y2));\n\n\t\t// return the maximum value\n\t\treturn mx;\n\t}\n\t// helper function for update(int, int, int);\n\tPoint _update(int node, int a1, int b1, int a2, int b2, int x, int y, int value) {\n\t\tif (a1 > a2 || b1 > b2)\n\t\t\treturn def();\n\n\t\tif (x > a2 || y > b2 || x < a1 || y < b1)\n\t\t\treturn T[node];\n\n\t\tif (x == a1 && y == b1 && x == a2 && y == b2)\n\t\t\treturn T[node] = Point(x, y, value);\n\n\t\tPoint mx = def();\n\t\tmx = maxNode(mx, _update(4 * node - 2, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x, y, value));\n\t\tmx = maxNode(mx, _update(4 * node - 1, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x, y, value));\n\t\tmx = maxNode(mx, _update(4 * node + 0, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x, y, value));\n\t\tmx = maxNode(mx, _update(4 * node + 1, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x, y, value));\n\t\treturn T[node] = mx;\n\t}\n\n\t// update the value of (x, y) index to 'value', O(logn)\n\tPoint update(int x, int y, int value) {\n\t\treturn _update(1, 1, 1, N, M, x, y, value);\n\t}\n\n\t// utility functions; these functions are virtual because they will be overridden in child class\n\tvirtual Point maxNode(Point a, Point b) {\n\t\treturn max(a, b);\n\t}\n\n\t// dummy node\n\tvirtual Point def() {\n\t\treturn Point(0, 0, -INF);\n\t}\n\n\tvoid g() {\n\t\tint n, m;\n\t\tscanf(\"%d %d\", &n, &m);\n\n\t\tSegmentTree2D Tmax(n, m);\n\n\t\t// query\n\t\tint x1, y1, x2, y2;\n\t\tscanf(\"%d %d %d %d\", &x1, &y1, &x2, &y2);\n\t\tprintf(\"(%d,%d,%d,%d)=%d\\n\", x1, y1, x2, y2, Tmax.query(x1, y1, x2, y2).mx);\n\n\t\t// update\n\t\tint x, y, v;\n\t\tscanf(\"%d %d %d\", &x, &y, &v);\n\t\tTmax.update(x, y, v);\n\n\t\t// query\n\t\tscanf(\"%d %d %d %d\", &x1, &y1, &x2, &y2);\n\t\tprintf(\"(%d,%d,%d,%d)=%d\\n\", x1, y1, x2, y2, Tmax.query(x1, y1, x2, y2).mx);\n\t}\n};\nclass TreeFromExpression {\npublic:\n\tTreeFromExpression() {\n\n\t}\n\t// http://codeforces.com/contest/935/problem/E\n\t// for example, str=\"((1?(5?7))?((6?2)?7))\"\n\tvector > make(char *str, int &root) {\n\t\tint i, cur = 1;\n\t\tvector > E(10000);\n\t\tstack S;\n\t\tfor (i = 0; str[i]; i++) {\n\t\t\tchar cc = str[i];\n\t\t\tif (cc >= '1' && cc <= '9') {\n\t\t\t\tE[cur].first = cc - '0';\n\t\t\t\tE[cur].second = 0;\n\t\t\t\tS.push(cur);\n\t\t\t\tcur++;\n\t\t\t}\n\t\t\telse if (cc == ')') {\n\t\t\t\tint a, b;\n\t\t\t\ta = S.top(); S.pop();\n\t\t\t\tb = S.top(); S.pop();\n\t\t\t\tE[cur].first = a; E[cur].second = b;\n\t\t\t\tS.push(cur);\n\t\t\t\tcur++;\n\t\t\t}\n\t\t}\n\t\troot = cur - 1;\n\t\treturn E;\n\t}\n};\n\nint main() {\n\tint i, j, n;\n\tscanf(\"%d\", &n); vector V(n);\n\tfor (i = 0; i < n; i++) scanf(\"%d\", &V[i]);\n\tsort(V.begin(), V.end());\n\tprintf(\"%d\", V[n / 2]);\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "f38f8bf8100a1ff12a4187e557b2e7e8", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "//#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n//end of libraries ;\n#define INF 9999999999\n#define N 300003\n#define F first\n#define S second\n#define pb push_back\n#define ll long long\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\nusing namespace std;\nint a[N];\nint main(){\nfcin;\n\tint n;\n\tcin>>n;\n\tfor(int i = 0 ; i < n ;i++) cin>>a[i];\n\t\tsort(a,a+n);\n\tcout<\n#pragma GCC optimize (\"-O3\")\nusing namespace std;\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define endl \"\\n\"\nint solve(int n,vector &v)\n{\n int result = 0;\n for(auto &i: v)\n {\n result += i;\n }\n result /= n;\n return result;\n}\nint32_t main()\n{\n IOS;\n int n;\n cin>>n;\n vector v(n);\n for(int i=0;i>v[i];\n }\n cout<\nusing namespace std;\nint n,f[1000100];\nint main(){\n\tcin>>n;\n\tfor(int i=1;i<=n;i++)cin>>f[i];\n\tsort(f+1,f+n+1);\n\tcout<\nusing namespace std;\nint main()\n{ int n,i;\n cin>>n;\n\tint arr[n];\n\tfor(i=0;i\n#include\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin>>n;\n\tint a[n],sum=0,i;\n\tfor(i=0;i>a[i];\n\t\tsum=sum+a[i];\n\t}\n\tcout<<(sum/n)<\n\nusing namespace std;\nint main()\n{\n int n,i;\n cin >> n;\n\n int a[n];\n for(i=0; i> a[i];\n }\n\n sort(a,a+n);\n\n int x = n/2;\n\n for(i=0;i \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\n\n\nint main() {\n#pragma warning(disable : 4996)\n#ifdef _DEBUG\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n#endif\n\tint n;\n\tcin >> n;\n\tif (n == 1){\n\t\tint x;\n\t\tcin >> x;\n\t\tcout << x;\n\t\treturn 0;\n\t}\n\tvector s;\n\tfor (int i = 0; i < n; i++){\n\t\tint h;\n\t\tcin >> h;\n\t\ts.push_back(h);\n\t}\n\tsort(s.begin(), s.end());\n\tcout << s[(n - 1) / 2 ];\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "8e4a43b0b3560238adaef8719caae1b3", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include\n\nusing namespace std;\n\nint n, x, a, b;\n\nint main () {\n\tcin >> n;\n\tfor (int i=0; i> x;\n\t\tif (i==0) {\n\t\t\ta=b=x;\n\t\t} else {\n\t\t\ta=min(a, x);\n\t\t\tb=max(b, x);\n\t\t}\n\t}\n\tcout << (a+b)/2;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "1ea7af87a48076421b813cfdf13bd3d7", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \nvoid merge(int arr[], int l, int m, int r)\n{\n int i, j, k;\n int n1 = m - l + 1;\n int n2 = r - m;\n \n /* create temp arrays */\n int L[n1], R[n2];\n \n /* Copy data to temp arrays L[] and R[] */\n for (i = 0; i < n1; i++)\n L[i] = arr[l + i];\n for (j = 0; j < n2; j++)\n R[j] = arr[m + 1+ j];\n \n /* Merge the temp arrays back into arr[l..r]*/\n i = 0; // Initial index of first subarray\n j = 0; // Initial index of second subarray\n k = l; // Initial index of merged subarray\n while (i < n1 && j < n2)\n {\n if (L[i] <= R[j])\n {\n arr[k] = L[i];\n i++;\n }\n else\n {\n arr[k] = R[j];\n j++;\n }\n k++;\n }\n \n /* Copy the remaining elements of L[], if there\n are any */\n while (i < n1)\n {\n arr[k] = L[i];\n i++;\n k++;\n }\n \n /* Copy the remaining elements of R[], if there\n are any */\n while (j < n2)\n {\n arr[k] = R[j];\n j++;\n k++;\n }\n}\n \n/* l is for left index and r is right index of the\n sub-array of arr to be sorted */\nvoid mergeSort(int arr[], int l, int r)\n{\n if (l < r)\n {\n // Same as (l+r)/2, but avoids overflow for\n // large l and h\n int m = l+(r-l)/2;\n \n // Sort first and second halves\n mergeSort(arr, l, m);\n mergeSort(arr, m+1, r);\n \n merge(arr, l, m, r);\n }\n}\nint main()\n{\n\tint n,i;\n\tscanf(\"%d\",&n);\n\tint * a = (int *)malloc(sizeof(int)*n);\n\tfor(i=0;i\n\nusing namespace std ;\n\n#define sz(v) (int)v.size()\n#define ll long long\n#define all(v) v.begin() , v.end()\n#define rall(v) v.rbegin() , v.rend()\n#define pf push_front\n#define pb push_back\n#define fast FastInputOutput() ;\n#define Clear( container , value ) memset( container , value , sizeof container )\n#define PI acos( -1.0 )\n#define forn( v , be , n ) for( int v = be ; v < n ; v++ )\n#define se second\n#define fr first\n//#define c_b fflush(stdin);\n\nconst ll Long_max = numeric_limits < ll > :: max() ;\nconst int Int_max = numeric_limits < int > :: max() ;\n\nint dx[ ] = { 1 , -1 , 0 , 0 , 1 , -1 , -1 , 1 } ;\nint dy[ ] = { 0 , 0 , -1 , 1 , 1 , -1 , 1 , -1 } ;\n\nvoid File_input( string pathIn ){\n freopen( pathIn.c_str() , \"r\", stdin ) ;\n}\n\nvoid File_output( string pathOut ){\n freopen( pathOut.c_str() , \"w\", stdout ) ;\n}\n\nint msg(){\n srand( time( 0 ) ) ;\n vector < string > st = {\n \"Wish everyone high ratings!\",\n \"Good luck!\",\n \"Have fun!\",\n \"Please, read all the problems!\",\n \"Go Go fight!\" } ;\n cerr << st[ rand() % sz( st ) ] ;\n return 0 ;\n}\n\nvoid FastInputOutput() {\n ios_base :: sync_with_stdio( 0 ) ;\n cin.tie( 0 ) ;\n cout.tie( 0 ) ;\n}\n\nint n , v , sm ;\n\nint main(){\n cin >> n ;\n forn( i , 0 , n ){\n cin >> v ;\n sm += v ;\n }\n\n cout << sm / n ;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "a776099f91e6b0a870fb50495bca03fd", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint n;\nmain(){\ncin>>n;\nint a[n];\nfor(int i=0;i>a[i];\nsort(a,a+n);\ncout<\nusing namespace std;\n#define ll long long\n#define ld long double\n#define pb push_back\n#define pf push_front\n#define mp make_pair\n#define F first\n#define S second\n#define mod 1000000007\n#define INF INT_MAX\n#define vl vector\n#define vi vector\n#define pi pair\n#define pl pair\n#define vpl vector\n#define vpi vector\n#define ml map\n#define mi map\n#define m(a,b) map\n#define rep(i,n) for(int i=1;i<=n;i++)\n#define YesNo(f) if(f){cout<<\"YES\"<>t;\n while(t--){\n int n;\n cin>>n;\n int a[n];\n for(int i=0;i>a[i];\n }\n sort(a,a+n);\n cout<\nusing namespace std;\ntypedef long long ll;\ntypedef vector vi;\ntypedef vector< vector > matrix;\n#define PB push_back\n#define F first\n#define S second\n#define loop(i,a,b) for (int i=a; i>t;\n\twhile(t--) {\n\t\tint n;\n cin>>n;\n vi vec(n);\n loop(i,0,n) cin>>vec[i];\n sort(vec.begin(), vec.end());\n cout<\n\tusing namespace std;\n\t\n\t#define jadu ios_base::sync_with_stdio(false);\n\t#define REP(i,a,b) for (ll i = a; i <= b; i++)\n\t#define F first\n\t#define S second\n\t\n\t#define PB push_back\n\t#define MP make_pair\n\ttypedef long long ll;\n\ttypedef vector vi;\n\ttypedef pair pi;\n \n const int mod = 1000000007;\n\tconst int maxi =1005;\n\n\tint arr[maxi];\n//\tint brr[maxi];\n//\tint crr[maxi];\n//\tint n,m,k;\n\t\n\tint main()\n\t{\n\t\tint n;\n\t\tcin>>n;\n\t\tfor(int i=0;i>arr[i];\n\t\tsort(arr,arr+n);\n\t\tif(n>=2)\n\t\tcout<\nusing namespace std;\n\nint main ()\n{\n int n, tem, high, low;\n cin >> n;\n int arr[n];\n for (int i=0; i> arr[i];\n sort(arr,arr+n);\n cout << arr[n/2];\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "f9de2f913907007e05733d93375b2ffd", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\nusing namespace std;\nint main()\n{\n int n, a[6];\n while(cin>>n){\n for(int i=0;i>a[i];\n sort(a,a+n);\n cout<\nusing namespace std;\nint a[5];\nint main(){\n ios::sync_with_stdio(false);\n \tint n , i , mx , mn , k;\n \tcin >> n;\n\t\tmx = 2000 ; \n\t\tmn = 2200;\n\t\tfor ( i = 0 ; i < n ; ++ i )\n\t\t{\n\t\t\tcin >> a[i];\n\t\t\tif ( a[i] > mx )\n\t\t\t{\n\t\t\t\tmx = a[i];\n\t\t\t}\n\t\t\tif ( a[i] < mn )\n\t\t\t{\n\t\t\t\tmn = a[i];\n\t\t\t}\n\t\t}\n\t\tk = (mx - mn) / 2;\n\tcout << mx - k;\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "f7217c744268903497f1b933755b32f3", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main(){\n int a[10],n;\n cin>>n;\n for(int i=1;i<=n;i++)cin>>a[i];\n sort(a+1,a+n+1);\n cout<\nusing namespace std;\n#define For(i,x,y)for(i=x;i<=y;i++)\nint a[6];\nint main()\n{\n\tint n,i,j,t;\n\tcin>>n;\n\tif(n-0>0)cin>>a[0];\n\tif(n-1>0)cin>>a[1];\n\tif(n-2>0)cin>>a[2];\n\tif(n-3>0)cin>>a[3];\n\tif(n--==5)cin>>a[4];\n\tFor(i,0,n-1)\n\tFor(j,0,n-i-1)\n\tif(a[j]>a[j+1])swap(a[j],a[j+1]);\n\tt=(a[0]+a[n])>>1;\n\ti=-1;\n\tj=n+1;\n\twhile(i=t);\n\tcout<<(a[i]-ta[i]-t?a[i]:(i-1>=0&&a[i-1]-t\n#include \n#include \n#include \n#include \nusing namespace std;\n\nint n,k,l,i;\nint main() {\n\tcin>>n;\n\tk=0;\n\tfor (i=0;i>l;\n\t\tk+=l;\n\t}\n\tcout<\n\nusing namespace std;\n\nint n;\nint a[10];\n\nint main()\n{\n cin >> n;\n for(int i = 1; i <= n; ++i)\n cin >> a[i];\n sort(a + 1, a + n + 1);\n cout << a[n / 2 + n % 2] << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "87f689dbe4479e0ef06971ceda01515f", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nusing namespace std;\n\nint a[6];\n\nint main() {\n\tint n;\n\tscanf(\"%d\", &n);\n\tfor (int i = 0; i < n; i++) {\n\t\tscanf(\"%d\", a + i);\n\t}\n\tsort(a, a + n);\n\tn >>= 1;\n\tprintf(\"%d\\n\", a[n]);\n\treturn 0;\t\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "eda7425162da3542e52d490ee8f8c820", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main(){\n\tint n,min=9999,k;\n\tcin>>n;\n\tint kn[n];\n\tfor(int i=1;i<=n;i++){\n\t\tcin>>kn[i];\n\t}\n\t//选择\n\tfor(int i=1;i<=n-1;i++) {\n\t\tk=i ;\n\t\tmin = kn[i] ;\n\t\tfor(int j=i+1;j<=n;j++){\n\t\t\tif(kn[j]\n#include \nusing namespace std;\nint main(){\n int n;\n cin>>n;\n int a[n+2];\n for(int i=1; i<=n; i++){\n cin>>a[i];\n }\n sort(a+1, a+n+1);\n cout<\n#define pb push_back\n#define ll long long\n#define TIME ios_base::sync_with_stdio(0)\nusing namespace std;\nll a,b,c,s,d,e,m,cnt,ans,l,r;\nvectorv,vv;\nstring g;\nsetst;\nint main()\n{\n TIME;\n cin>>a;\n for(int i=0; i>b;\n v.pb(b);\n sort(v.begin(),v.end());\n }\n cout<\n#include \n\nusing namespace std;\n\nint YearUniversityAdmission (int n, vector &array){\n int summ = 0;\n for (int i = 0; i < n; ++i) {\n summ += array[i];\n }\n return summ/n;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n int n;\n cin >> n;\n \n vector years(n);\n for (int i = 0; i < n; i++){\n cin >> years[i];\n }\n \n cout << YearUniversityAdmission(n, years) << endl;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "4197643ccd82af541fe0ae4b1845fb70", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n\nint main(int argc, char const *argv[]) {\n\tsize_t n, sum = 0, y;\n\tstd::cin >> n;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tstd::cin >> y;\n\t\tsum += y;\n\t}\n\n\tstd::cout << sum / n;\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "6ee16e6a68a17e385adbef3a5f3d60e2", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "/* Author haleyk10198 */\n/* �@��: haleyk10198 */\n/* CF handle: haleyk100198*/\n#include \n\n#define MOD 1000000007\n#define LINF (1LL<<60)\n#define INF 2147483647\n#define PI 3.1415926535897932384626433\n#define ll long long\n#define pii pair\n#define mp(x,y) make_pair((x),(y))\n\nusing namespace std;\n\nstring itos(int x){\n\tstringstream ss;\n\tss << x;\n\treturn ss.str();\n}\n\nint n, a[5];\n\nint main(){\n\t//freopen(\"input.txt\",\"r\",stdin);\n\t//freopen(\"output.txt\",\"w\",stdout);\n\tios_base::sync_with_stdio(false);\n\tcin >> n;\n\tfor(int i = 0; i < n; i++)\n\t\tcin >> a[i];\n\tsort(a, a+n);\n\tcout << a[n/2] << endl;\n\t//Not sure if a[] is unique...\n\treturn 0;\n}\n\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "1ee4e29e94dd8743a7d283be72510070", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n#define ll long long\n#define pb push_back\n#define mp make_pair\n#define F first\n#define S second\nconst ll N = 1005;\nvoid go() {\n ll n, sum, i, x, b, j, cur, k, res;\n cin >> n;\n vector a(n);\n for(auto &x: a)\n cin >> x;\n sort(a.begin(), a.end());\n cout << a[n / 2] << \"\\n\";\n return;\n}\nint main() {\n\tll t = 1;\n\twhile(t--)\n go();\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["sortings", "implementation"], "code_uid": "92aec551fd2969c7e509676ec586fcd6", "src_uid": "f03773118cca29ff8d5b4281d39e7c63", "difficulty": 800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\n\n#define inf (1<<30)-1\n#define INF (1LL<<62)-1\n#define MOD 1000000007LL\n#define MP make_pair\n#define PB push_back\n#define ALL(x) x.begin(),x.end()\n#define PI acos(-1)\n#define MEM(x,y) memset(x,y,sizeof (x))\n#define debug cout<<\"A\"<<'\\n'\n#define REP(i,a,b) for (int i=(a); i<=(b); i++)\n#define PER(i,a,b) for (int i=(a); i>=(b); i--)\n#define REPL(i,a,b) for (LL i=(a); i<=(b); i++)\n#define PERL(i,a,b) for (LL i=(a); i>=(b); i--)\n#define print(x) cout<PII;\ntypedef pairPLL;\ntypedef set SI;\ntypedef set SL;\ntypedef set SS;\ntypedef vector VI;\ntypedef vector VL;\ntypedef vector VS;\ntypedef map MII;\ntypedef map MLL;\ntypedef queue QI;\n\n/* Direction Array */\n\n// int fx[]={1,-1,0,0};\n// int fy[]={0,0,1,-1};\n// int fx[]={0,0,1,-1,-1,1,-1,1};\n// int fy[]={-1,1,0,0,1,1,-1,-1};\n\ntemplate inline T bigmod(T p,T e,T M)\n{\n T ret = 1;\n for(; e > 0; e >>= 1)\n {\n if(e & 1) ret = (ret * p) % M;\n p = (p * p) % M;\n } return (T)ret;\n}\n\ntemplate inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}\ntemplate inline T modinverse(T a,T M){return bigmod(a,M-2,M);}\ntemplate inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}\ntemplate inline bool getbit(T a, X i) { T t=1; return ((a&(t<0);}\ntemplate inline T setbit(T a, X i) { T t=1;return (a|(t< inline T resetbit(T a, X i) { T t=1;return (a&(~(t<=0)?a:-a;}\n\n/*end of header*/\n\nstruct Matrix\n{\n\tLL mat[2][2];\n\n\tMatrix(){}\n\n\tvoid init()\n\t{\n\t\tMEM(mat,0);\n\t\tfor(int i=0; i<2; i++) mat[i][i]=1;\n\t}\n} aux;\n\ninline void matMult(Matrix &m, Matrix &m1, Matrix &m2)\n{\n\tREP(i,0,1)\n\t{\n\t\tREP(j,0,1)\n\t\t{\n\t\t\tm.mat[i][j]=0;\n\t\t\tREP(k,0,1)\n\t\t\t{\n\t\t\t\tm.mat[i][j]=(m.mat[i][j]+m1.mat[i][k]*m2.mat[k][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\ninline Matrix expo(Matrix &M, LL n)\n{\n\tMatrix ret;\n\tret.init();\n\n\tif(n==0) return ret;\n\tif(n==1) return M;\n\n\tMatrix P=M;\n\n\twhile(n!=0)\n\t{\n\t\tif(n&1)\n\t\t{\n\t\t\taux=ret;\n\t\t\tmatMult(ret,aux,P);\n\t\t}\n\n\t\tn>>=1;\n\n\t\taux=P; matMult(P,aux,aux);\n\t}\n\n\treturn ret;\n}\n\n\nLL findfib(LL n)\n{\n if(n==0)return 1;\n if(n==1)return 2;\n Matrix M;\n M.mat[0][0]=1;\n M.mat[0][1]=1;\n M.mat[1][0]=1;\n M.mat[1][1]=0;\n Matrix res=expo(M,n-1);\n return res.mat[0][0]*2+res.mat[0][1];\n}\n\nint main()\n{\n //freopen(\"input_file_name.in\",\"r\",stdin);\n //freopen(\"output_file_name.out\",\"w\",stdout);\n LL n;\n scanf(\"%lld\",&n);\n LL lo=0,hi=90,mid,best;\n while(hi>=lo)\n {\n mid=(hi+lo)/2;\n if(findfib(mid)<=n)\n {\n best=mid;\n lo=mid+1;\n }\n else hi=mid-1;\n }\n printf(\"%lld\\n\",best);\n return 0;\n}\n\n", "lang_cluster": "C++", "tags": ["dfs and similar", "constructive algorithms", "math", "dp", "combinatorics"], "code_uid": "6ce943e47227c760072a7e31de924a41", "src_uid": "3d3432b4f7c6a3b901161fa24b415b14", "difficulty": 1600.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n\nconst int INF = 0x3f3f3f3f;\nconst int MAXN = 2e5 + 5;\n\nint n,k,ans = INF;\nint a[MAXN],cnt[MAXN],res[MAXN];\n\nint main()\n{\n\tscanf(\"%d%d\",&n,&k);\n\tfor(int i = 1;i <= n;i++)\n\t\tscanf(\"%d\",&a[i]);\n\t\n\tsort(a + 1,a + 1 + n);\n\t\n\tfor(int i = 1;i <= n;i++)\n\t{\n\t\tint t = a[i],x = 0;\n\t\twhile(t != 0)\n\t\t{\n\t\t\tcnt[t]++;\n\t\t\tres[t] += x;\n\t\t\tx++;\n\t\t\tif(cnt[t] == k)\n\t\t\t ans = min(ans,res[t]);\n\t\t\tt /= 2;\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "afe3dd8cd15a6c3af2b4203533c62b69", "src_uid": "ed1a2ae733121af6486568e528fe2d84", "difficulty": 1500.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\n# define ll long long int \nchar str[101][101];\nint main(){\n\tios::sync_with_stdio(false);cin.clear();\n\tll t,n,i,j,ans=0,count=0,sum=0,k,count1=0,count2=0;\n\t\t\n\t\tfor(i=0;i<10;i++){\n\t\t\tfor(j=0;j<10;j++)str[i][j]='A';\n\t\t}\n\t\tfor(i=2;i<6;i++){\n\t\t\tfor(j=2;j<6;j++)cin>>str[i][j];\n\t\t}\n\t\t\n\t\tfor(i=2;i<6;i++){\n\t\t\tfor(j=2;j<6;j++){\n\t\t\t\tcount1=0;count2=0;\n\t\t\t\tif(str[i][j]=='x')count1++;\n\t\t\t\tif(str[i][j]=='.')count2++;\n\t\t\t\tif(str[i][j+1]=='x')count1++;\n\t\t\t\tif(str[i][j+1]=='.')count2++;\n\t\t\t\tif(str[i][j+2]=='x')count1++;\n\t\t\t\tif(str[i][j+2]=='.')count2++;\n\t\t\t\tif(count1>2){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t\tif(count1==2&&count2==1){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(j=2;j<6;j++){\n\t\t\tfor(i=2;i<6;i++){\n\t\t\t\tcount1=0;count2=0;\n\t\t\t\tif(str[i][j]=='x')count1++;\n\t\t\t\tif(str[i][j]=='.')count2++;\n\t\t\t\tif(str[i+1][j]=='x')count1++;\n\t\t\t\tif(str[i+1][j]=='.')count2++;\n\t\t\t\tif(str[i+2][j]=='x')count1++;\n\t\t\t\tif(str[i+2][j]=='.')count2++;\n\t\t\t\tif(count1>2){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t\tif(count1==2&&count2==1){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(i=2;i<6;i++){\n\t\t\tfor(j=2;j<6;j++){\n\t\t\t\tcount1=0;count2=0;\n\t\t\t\tif(str[i][j]=='x')count1++;\n\t\t\t\tif(str[i][j]=='.')count2++;\n\t\t\t\tif(str[i+1][j+1]=='x')count1++;\n\t\t\t\tif(str[i+1][j+1]=='.')count2++;\n\t\t\t\tif(str[i+2][j+2]=='x')count1++;\n\t\t\t\tif(str[i+2][j+2]=='.')count2++;\n\t\t\t\tif(count1>2){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t\tif(count1==2&&count2==1){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(i=2;i<6;i++){\n\t\t\tfor(j=2;j<6;j++){\n\t\t\t\tcount1=0;count2=0;\n\t\t\t\tif(str[i][j]=='x')count1++;\n\t\t\t\tif(str[i][j]=='.')count2++;\n\t\t\t\tif(str[i+1][j-1]=='x')count1++;\n\t\t\t\tif(str[i+1][j-1]=='.')count2++;\n\t\t\t\tif(str[i+2][j-2]=='x')count1++;\n\t\t\t\tif(str[i+2][j-2]=='.')count2++;\n\t\t\t\tif(count1>2){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t\tif(count1==2&&count2==1){\n\t\t\t\t\tcout<<\"YES\";\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tcout<<\"NO\";\n}", "lang_cluster": "C++", "tags": ["brute force", "implementation"], "code_uid": "0e4dd76dbd2f369b33a7dc8dba7b43bc", "src_uid": "ca4a77fe9718b8bd0b3cc3d956e22917", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++0x", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nint main(int argc, char** argv) {\n\n ios::sync_with_stdio(false);\n int a, b, mod;\n cin >> a >> b >> mod;\n\n for(int i = 1; i <= min(a, mod); ++i) {\n int rem = (mod - i * (long long)1e9 % mod) % mod;\n if(b < rem) {\n cout << \"1 \" << setw(9) << setfill('0') << i << endl;\n return 0;\n }\n }\n\n cout << 2 << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "number theory"], "code_uid": "6681e2b522422a355be571a8b4dd2a98", "src_uid": "8b6f633802293202531264446d33fee5", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\ntypedef long long int ll;\nconst ll zero = 0LL;\nconst int N = 2e3+10;\nconst ll oo = 1e5;\nconst double pi = acos(-1);\nconst int md = 1e9+7;\n\n\nint n, dp[N][1010][2];\nint calc(int idx, int open, bool canTake)\n{\n\t\n\tif(idx == n*2)\n\t\treturn 0;\n\t\n\tif(dp[idx][open][canTake]!=-1)\n\t\treturn dp[idx][open][canTake];\n\t\n\tint ret = 0;\n\tbool canOpen = (open+1) <= (n*2-idx-1);\n\tbool canClose = open>0;\n\t\n\tif(canOpen && canClose)\n\t{\n\n\t\tif(canTake)\n\t\t{\n\t\n\t\t\tint takeRight = calc(idx+1, open+1, 1) + calc(idx+1, open-1, 0);\n\t\t\tint takeLeft = calc(idx+1, open+1, 0) + calc(idx+1, open-1, 1);\n\t\t\tret = 1 + max(takeRight, takeLeft);\n\t\n\t\t}else{\n\t\n\t\t\tret = calc(idx+1, open+1, 1) + calc(idx+1, open-1, 1);\n\t\n\t\t}\n\n\t}else if(canOpen){\n\n\t\tret = canTake+calc(idx+1, open+1, !canTake);\n\n\t}else if(canClose){\n\n\t\tret = canTake+calc(idx+1, open-1, !canTake);\n\n\t}\n\treturn dp[idx][open][canTake] = ret % md;\n}\nint main()\n{\n\t#ifndef ONLINE_JUDGE\n\t\tfreopen(\"a.txt\",\"r\",stdin);\n\t\tfreopen(\"b.txt\",\"w\",stdout);\n\t#endif\n\tscanf(\"%d\", &n);\n\tmemset(dp, -1, sizeof dp);\n\tprintf(\"%d\", calc(0, 0, 1));\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["greedy", "dp", "trees"], "code_uid": "f17f8320a69df21734f16a9a04cbcda4", "src_uid": "8218255989e5eab73ac7107072c3b2af", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "/*\n--> IF YOU WANT IT, YOU GOTTA GO GET IT.\n\n--> WHEN IT'S OBVIOUS THAT THE GOAL CANNOT BE REACHED,\n\tDON'T ADJUST THE GOALS,\n\tADJUST THE ACTION STEPS.\n\n--> THE WAY YOU CONQUER SELF-DOUBT IS BY DOING THINGS THAT MAKE YOU UNCOMFORTABLE.\n\n--> IT IS DURING OUR DARKEST MOMENTS WE MUST FOCUS TO SEE LIGHT.\n*/\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n#define pb push_back\n#define F first\n#define S second\n#define all(x) x.begin(), x.end()\n#define sortAll(x) sort(all(x))\n#define sz(x) (int)x.size()\n#define clr(x) memset(x, 0, sizeof(x))\n#define PI 3.1415926535897932384626\n#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO();\n#define deb1(x) cout << #x << \"=\" << x << \"\\n\" \n#define deb2(x, y) cout << #x << \"=\" << x << \" \" << #y << \"=\" << y << \"\\n\" \n#define deb3(x, y, z) cout << #x << \"=\" << x << \" \" << #y << \"=\" << y << \" \" << #z << \"=\" << z << \"\\n\"\ntypedef long long ll;\ntypedef pair< int, int >\tpii;\ntypedef pair< ll, ll >\t\tpl;\ntypedef vector< int >\t\tvi;\ntypedef vector< ll >\t\tvl;\ntypedef vector< pii >\t\tvpii;\ntypedef vector< pl >\t\tvpl;\ntypedef vector< vi >\t\tvvi;\ntypedef vector< vl >\t\tvvl;\n\nconst int MOD = 1e9 + 7;\n\nll power(ll a, ll b)\n{\n\tll ans = 1;\n\n\twhile (b > 0)\n\t{\n\t\tif (b & 1)\n\t\t\tans *= a;\n\t\ta = a * a;\n\t\tb >>= 1;\n\t}\n\treturn ans;\n}\n\nll powm(ll a, ll b)\n{\n\ta %= MOD;\n\tll ans = 1;\n\n\twhile (b > 0)\n\t{\n\t\tif (b & 1)\n\t\t\tans = (ans * a) % MOD;\n\t\ta = (a * a) % MOD;\n\t\tb >>= 1;\n\t}\n\treturn ans;\n}\n\nvoid IO()\n{\n\t#ifndef ONLINE_JUDGE\n\t\tfreopen(\"Input.txt\", \"r\", stdin);\n\t\tfreopen(\"Output.txt\", \"w\", stdout);\n\t#endif\n}\n\n//================================\n\nint main()\n{\n\tFAST;\n\n\tint c, v0, v1, a, l;\n\tcin >> c >> v0 >> v1 >> a >> l;\n\n\tint ans = 0;\n\twhile (c > 0)\n\t{\n\t\tif (ans == 0)\n\t\t\tc -= v0;\n\t\telse\n\t\t\tc -= min(v0 + ans * a - l, v1 - l);\n\t\tans++;\n\t\t// deb2(c, ans);\n\t}\n\tcout << ans << \"\\n\";\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "110cd519ca92494c1423dfb4d39b1b27", "src_uid": "b743110117ce13e2090367fd038d3b50", "difficulty": 900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n;\n\tcin>>n;\n\tint a;\n\ta=n;\n\tvectornumi(0);\n\tint count=n;\n\tint num=0;\n\twhile(count!=0)\n\t{\n\t\tnum++;\n\t\tif(a%2!=0)\n\t\t\tnumi.push_back(num);\n\t\tcount=a/2;\n\t\ta=count;\n\t}\n\tsort(numi.begin(),numi.end());\n\tfor(int i=numi.size()-1;i>-1;i--)\n\t\tcout<\n#include \n#include \n#include \n#include \n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\ntypedef long long int64;\ntypedef vector::iterator it;\n\nconst int64 oo = 1LL << 60;\n\nclass Cell {\n public:\n int value, index;\n int64 position;\n\n Cell() {}\n\n bool operator<(const Cell &other) const {\n if (value == other.value)\n return index > other.index;\n return value < other.value;\n }\n};\n\nint N, M, K;\nvector Cells;\nvector MinCells, Edges;\nvector Used;\npriority_queue Heap;\n\nvoid Dijkstra(const int64 source) {\n MinCells = vector(K, oo);\n priority_queue, greater> vertices;\n vertices.push(source);\n while (!vertices.empty()) {\n int64 x = vertices.top();\n vertices.pop();\n if (MinCells[x % K] < oo)\n continue;\n MinCells[x % K] = x;\n for (int64 &e: Edges)\n if (MinCells[(x + e) % K] == oo)\n vertices.push(x + e);\n }\n}\n\nvoid GetAccessibleCells() {\n Dijkstra(1);\n Heap = priority_queue();\n for (int i = 0; i < N; ++i)\n if (Used[i] == 0 && Cells[i].position >= MinCells[Cells[i].position % K])\n Heap.push(Cells[i]);\n}\n\nvoid Read() {\n int64 H;\n cin >> H >> N >> M >> K;\n Cells = vector(N);\n Used = vector(N, 0);\n for (int i = 0; i < N; ++i) {\n cin >> Cells[i].position >> Cells[i].value;\n Cells[i].index = i;\n }\n Edges.push_back(K);\n GetAccessibleCells();\n}\n\nint main() {\n Read();\n for (; M > 0; --M) {\n int type;\n cin >> type;\n if (type == 1) {\n int64 edge;\n cin >> edge;\n Edges.push_back(edge);\n GetAccessibleCells();\n }\n if (type == 2) {\n int index, cost;\n cin >> index >> cost;\n --index;\n Cells[index].value -= cost;\n if (Used[index] == 0 && Cells[index].position >= MinCells[Cells[index].position % K])\n Heap.push(Cells[index]);\n }\n if (type == 3) {\n while (!Heap.empty() && (Used[Heap.top().index] == 1 || Heap.top().value != Cells[Heap.top().index].value))\n Heap.pop();\n if (Heap.empty()) {\n printf(\"0\\n\");\n } else {\n printf(\"%d\\n\", Heap.top().value);\n Used[Heap.top().index] = 1;\n Heap.pop();\n }\n }\n }\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["shortest paths", "graphs", "data structures"], "code_uid": "9cc140094784bc19b6a71ba05ec2d3b4", "src_uid": "03c4df4f640633f3364ca9225588caf7", "difficulty": 2500.0, "exec_outcome": "PASSED"} {"lang": "MS C++ 2017", "source_code": "#include\nusing namespace std;\nint main()\n{\n\tint n, m, x = 0, y = 0, p, k = 0;\n\tcin >> n >> m;\n\tk = m / n;\n\tif (m%n != 0)\n\t{\n\t\tcout << \"-1\";\n\t}\n\telse\n\t{\n\t\twhile (k % 2 == 0)\n\t\t{\n\t\t\tx = x + 1;\n\t\t\tk = k / 2;\n\t\t}\n\t\twhile (k % 3 == 0)\n\t\t{\n\t\t\ty = y + 1;\n\t\t\tk = k / 3;\n\t\t}\n\t\tif (k == 1)\n\t\t{\n\t\t\tcout << x + y;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout << \"-1\";\n\t\t}\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["math", "implementation"], "code_uid": "b3e620a576cf2fbbca55269d875f840a", "src_uid": "3f9980ad292185f63a80bce10705e806", "difficulty": 1000.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\n#include\nusing namespace std;\nint main() {\n\tint x, y, X, Y, xx, yy, m = 0;\n\tchar a1[12], a2[12], a3[12], a4[12], a5[12], a6[12], a7[12], a8[12], a9[12], b[1];\n\tcin.getline(a1, 12);\n\tcin.getline(a2, 12);\n\tcin.getline(a3, 12);\n\tcin.getline(b, 1);\n\tcin.getline(a4, 12);\n\tcin.getline(a5, 12);\n\tcin.getline(a6, 12);\n\tcin.getline(b, 1);\n\tcin.getline(a7, 12);\n\tcin.getline(a8, 12);\n\tcin.getline(a9, 12);\n\tcin >> y >> x;\n\tif (x > 3 && x < 7)X = x + 1;\n\telse if (x > 6)X = x + 1;\n\telse X = x;\n\tif (y > 3 && y < 7)Y = y + 1;\n\telse if (y > 6)Y = y + 1;\n\telse Y = y;\n\txx = x % 3;\n\tif (xx == 0)xx = 3;\n\tyy = y % 3;\n\tif (yy == 0)yy = 3;\n\tif (yy == 1 && xx == 1) {\n\t\tfor (int i = 0;i <= 2;i++) {\n\t\t\tif (a1[i] == '.') {\n\t\t\t\ta1[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a2[i] == '.') {\n\t\t\t\ta2[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a3[i] == '.') {\n\t\t\t\ta3[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 1 && xx == 2) {\n\t\tfor (int i = 4;i <= 6;i++) {\n\t\t\tif (a1[i] == '.') {\n\t\t\t\ta1[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a2[i] == '.') {\n\t\t\t\ta2[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a3[i] == '.') {\n\t\t\t\ta3[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 1 && xx == 3) {\n\t\tfor (int i = 8;i <= 10;i++) {\n\t\t\tif (a1[i] == '.') {\n\t\t\t\ta1[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a2[i] == '.') {\n\t\t\t\ta2[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a3[i] == '.') {\n\t\t\t\ta3[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 2 && xx == 1) {\n\t\tfor (int i = 0;i <= 2;i++) {\n\t\t\tif (a4[i] == '.') {\n\t\t\t\ta4[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a5[i] == '.') {\n\t\t\t\ta5[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a6[i] == '.') {\n\t\t\t\ta6[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 2 && xx == 2) {\n\t\tfor (int i = 4;i <= 6;i++) {\n\t\t\tif (a4[i] == '.') {\n\t\t\t\ta4[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a5[i] == '.') {\n\t\t\t\ta5[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a6[i] == '.') {\n\t\t\t\ta6[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 2 && xx == 3) {\n\t\tfor (int i = 8;i <= 10;i++) {\n\t\t\tif (a4[i] == '.') {\n\t\t\t\ta4[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a5[i] == '.') {\n\t\t\t\ta5[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a6[i] == '.') {\n\t\t\t\ta6[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 3 && xx == 1) {\n\t\tfor (int i = 0;i <= 2;i++) {\n\t\t\tif (a7[i] == '.') {\n\t\t\t\ta7[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a8[i] == '.') {\n\t\t\t\ta8[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a9[i] == '.') {\n\t\t\t\ta9[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 3 && xx == 2) {\n\t\tfor (int i = 4;i <= 6;i++) {\n\t\t\tif (a7[i] == '.') {\n\t\t\t\ta7[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a8[i] == '.') {\n\t\t\t\ta8[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a9[i] == '.') {\n\t\t\t\ta9[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (yy == 3 && xx == 3) {\n\t\tfor (int i = 8;i <= 10;i++) {\n\t\t\tif (a7[i] == '.') {\n\t\t\t\ta7[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a8[i] == '.') {\n\t\t\t\ta8[i] = '!';m = 1;\n\t\t\t}\n\t\t\tif (a9[i] == '.') {\n\t\t\t\ta9[i] = '!';m = 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (m == 1) {\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a1[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a2[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a3[i];\n\t\t}\n\t\tcout << endl << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a4[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a5[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a6[i];\n\t\t}\n\t\tcout << endl << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a7[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a8[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tcout << a9[i];\n\t\t}cout << endl;\n\t}\n\n\telse if (m == 0) {\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a1[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a1[i];\n\t\t}\n\t\tcout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a2[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a2[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a3[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a3[i];\n\t\t}cout << endl << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a4[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a4[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a5[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a5[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a6[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a6[i];\n\t\t}cout << endl << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a7[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a7[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a8[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a8[i];\n\t\t}cout << endl;\n\t\tfor (int i = 0;i < 11;i++) {\n\t\t\tif (a9[i] == '.' && i != 3 && i != 7)cout << '!';\n\t\t\telse cout << a9[i];\n\t\t}cout << endl;\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["implementation"], "code_uid": "3dd9ae507f110d5e4ea3b265e15b9155", "src_uid": "8f0fad22f629332868c39969492264d3", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "\n #include\nusing namespace std;\n\nint main()\n{\n int n,m,x,a,b;\nint count=0;\n \n cin>>n;\n cin>>m;\n if(m>n)\n x=m;\n else x=n;\n\n for(a=0;a<=(x/2)+1;a++)\n { b=n-a*a;\n if(b>=0 && b*b+a==m)\n count++;\n }\n cout<\n#include\nusing namespace std;\nint t[100005];\nint a,b,m,r[100005];\nbool vis[100005];\nint cal(){\n int i=1;\n while(true){\n r[i]=(a*r[i-1]+b+m)%m;\n if(vis[r[i]])return i-t[r[i]];\n vis[r[i]]=1;\n t[r[i]]=i;\n i++;\n }\n return 0;\n}\nint main(){\n while(cin>>a>>b>>m>>r[0]){\n vis[r[0]]=1;\n t[r[0]]=0;\n cout<\nusing namespace std;\nint n,t,ans=100000001;\nbool flag=false;\ninline int read()\n{\n\tint res=0,f=1;char ch=getchar();\n\twhile(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}\n\twhile(isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();\n\treturn res*f;\n}\ninline void dfs(int x,int y,int tot)\n{\n\tif(tot>ans) return ;\n\tif(x==1&&y==1)\n\t{\n\t\tans=min(ans,tot);\n\t\treturn ;\n\t}\n\tif(x>y) dfs(x-y,y,tot+1);\n\telse if(y>x) dfs(x,y-x,tot+1);\n\treturn ;\n}\nint main()\n{\n\tn=read();\n\tfor(register int i=1;i<=n;i++)\n\t\tdfs(n,i,0);\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "number theory", "brute force"], "code_uid": "007c3647cda8344d51b0f3e78ac02129", "src_uid": "75739f77378b21c331b46b1427226fa1", "difficulty": 1900.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nconst int INF=0x3f3f3f3f;\ntypedef long long ll;\ntypedef pair PI;\ndeque dq[4];\nint dp[2][2][100010];\nint main(){\n\tint n,k,last=0,l,r;\n\tscanf(\"%d%d\",&n,&k);\n\tmemset(dp[0],0x3f,sizeof dp[0]);\n\tdp[0][0][0]=0;\n\tfor(int i=1;i<=k;i++){\n\t\tint cur=i&1,pre=!cur;\n\t\tscanf(\"%d%d\",&l,&r);\n\t\tfor(int j=0;j<4;j++) dq[j].clear();\n\t\tmemset(dp[cur],0x3f,sizeof dp[cur]);\n\t\tint min_diff[4]={l-last,l-last,0,1},max_diff[4]={r-1-last,r-last,r-l,r-l};\n\t\tint pre_layer[4]={0,0,1,1},cur_layer[4]={0,1,0,1};\n\t\tint add[4]={2,1,1,2};\n\t\tint idx[4]={0,0,0,0};\n\t\tfor(int w=0;w<=n;w++){\n\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\twhile(w-idx[j]>=min_diff[j]){\n\t\t\t\t\twhile(!dq[j].empty() && dq[j].back().first>=dp[pre][pre_layer[j]][idx[j]])\n\t\t\t\t\t\tdq[j].pop_back();\n\t\t\t\t\tdq[j].push_back(PI(dp[pre][pre_layer[j]][idx[j]],idx[j]));\n\t\t\t\t\tidx[j]++;\n\t\t\t\t}\n\t\t\t\twhile(!dq[j].empty() && w-dq[j].front().second>max_diff[j])\n\t\t\t\t\tdq[j].pop_front();\n\t\t\t\tif(dq[j].empty()) dp[cur][cur_layer[j]][w]=min(dp[cur][cur_layer[j]][w],INF);\n\t\t\t\telse dp[cur][cur_layer[j]][w]=min(dp[cur][cur_layer[j]][w],dq[j].front().first+add[j]);\n\t\t\t}\n\t\t\tif(w-(r-last)>=0) dp[cur][0][w]=min(dp[cur][0][w],dp[pre][0][w-(r-last)]);\n\t\t\tdp[cur][1][w]=min(dp[cur][1][w],dp[pre][1][w]);\n\t\t}\n\t\tlast=r;\n\t}\n\tint ans=min(n-(2*n-last)>=0?dp[k&1][0][n-(2*n-last)]:INF,dp[k&1][1][n]);\n\tif(ans==INF) puts(\"Hungry\");\n\telse printf(\"Full\\n%d\\n\",ans);\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dp", "data structures"], "code_uid": "a358968a4e2b13182468c87664bcd257", "src_uid": "2e0d1b1f1a7b8df2d2598c3cb2c869d5", "difficulty": 2400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\nint main()\n{\n int l,k=0,j=0,x,y,s1=0,s2=0,n1=0;\n cin>>l;\n for(int i=1;i<=l;i++){\n cin>>x>>y;\n s1=x+s1;\n s2=y+s2;\n if(x%2==0&&y%2!=0){\n n1=1;\n }\n else if(y%2==0&&x%2!=0){\n n1=1;\n }\n\n }\n if(s1%2==0&&s2%2==0){\n cout<<\"0\"<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"output.txt\", \"w\", stdout);\n\n long long n, k;\n cin >> n >> k;\n\n long long res = -1;\n for (long long kk = 1; kk < k; kk++) {\n if (n % kk)\n continue;\n long long y = n / kk;\n long long cur = k * y + kk;\n if (res == -1 || cur < res)\n res = cur;\n }\n\n cout << res;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "4787bd88e93dba1e73ed5a68e19ff8ab", "src_uid": "ed0ebc1e484fcaea875355b5b7944c57", "difficulty": 1100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++20 (64)", "source_code": "#include\r\n#define re register\r\nusing namespace std;\r\ninline int read(){\r\n\tre int t=0;re char v=getchar();\r\n\twhile(v<'0')v=getchar();\r\n\twhile(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();\r\n\treturn t;\r\n}\r\nnamespace R{\r\n\tint p,w,nn;\r\n\tstruct cpp{\r\n\t\tint a,b;\r\n\t\tcpp(int a=0,int b=0):a(a),b(b){}\r\n\t\tcpp operator * (const cpp &x)const{\r\n\t\t\treturn cpp((1ll*a*x.a%p+1ll*w*b%p*x.b)%p,(1ll*a*x.b%p+1ll*b*x.a%p)%p);\r\n\t\t}\r\n\t};\r\n\tinline cpp ksm(re cpp x,re int y){\r\n\t\tcpp ans=1;\r\n\t\twhile(y){\r\n\t\t\tif(y&1)ans=ans*x;\r\n\t\t\tx=x*x,y>>=1;\r\n\t\t}\r\n\t\treturn ans;\r\n\t}\r\n\tinline int ksm1(re int x,re int y){\r\n\t\tint ans=1;\r\n\t\twhile(y){\r\n\t\t\tif(y&1)ans=1ll*ans*x%p;\r\n\t\t\tx=1ll*x*x%p,y>>=1;\r\n\t\t}\r\n\t\treturn ans;\r\n\t}\r\n\tinline int c(){\r\n\t\tint a=1;\r\n\t\twhile(ksm1((1ll*a*a%p-nn+p)%p,p>>1)==1)a=rand()%p;\r\n\t\tw=1ll*a*a%p-nn+p;\r\n\t\treturn (ksm(cpp(a,1),(p+1)>>1).a)%p;\r\n\t}\r\n\tinline int sy(re int n,re int xx){\r\n\t\tp=xx;nn=n;\r\n\t\tnn%=p;\r\n\t\tif(!nn)return 0;re int xxx=(c()%p+p)%p;\r\n\t\treturn min(xxx,p-xxx);\r\n\t}\r\n}\r\nnamespace Poly{\r\n\tconst int M=998244353;\r\n\tinline int ksm(re int x,re int y){\r\n\t\tre int s=1;\r\n\t\twhile(y){\r\n\t\t\tif(y&1)s=1ll*s*x%M;\r\n\t\t\tx=1ll*x*x%M,y>>=1;\r\n\t\t}\r\n\t\treturn s;\r\n\t}\r\n\tinline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}\r\n\tinline int Mod(re int x){return x>=M?x-M:x;}\r\n\tinline void Out(vectorA){\r\n\t\tputs(\"\\n--------\");\r\n\t\tprintf(\"%d\\n\",A.size());\r\n\t\tfor(re int i=0;i>1]>>1|((i&1)<<(l-1));\r\n\t\treturn n;\r\n\t}\r\n\tinline void Getfac(re int n){\r\n\t\tfor(re int i=fac[0]=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%M;\r\n\t\tinv[n]=ksm(fac[n],M-2);\r\n\t\tfor(re int i=n-1;~i;--i)inv[i]=1ll*inv[i+1]*(i+1)%M;\r\n\t}\r\n\tinline void GetInv(re int n){\r\n\t\tI[1]=1;\r\n\t\tfor(re int i=2;i<=n;++i)I[i]=1ll*I[M%i]*(M-M/i)%M;\r\n\t}\r\n\tinline void NTT(vector&A,re int n,re int f){\r\n\t\tfor(re int i=0;iA,vectorB,vector&C){\r\n\t\tre int k=make(A.size()+B.size()-1),o=A.size()+B.size()-1;\r\n\t\tA.resize(k),B.resize(k),C.resize(k);\r\n\t\tNTT(A,k,1),NTT(B,k,1);\r\n\t\tfor(re int i=0;i&A){\r\n\t\tfor(re int i=0;i+1&A){\r\n\t\tA.resize(A.size()+1);\r\n\t\tfor(re int i=A.size()-1;i;--i)A[i]=1ll*A[i-1]*I[i]%M;\r\n\t\tA[0]=0;\r\n\t}\r\n\tinline void mul(vectorA,vectorB,vectorC,vector&D){\r\n\t\tre int k=make(A.size()+B.size()+C.size()-2),o=A.size()+B.size()+C.size()-2;\r\n\t\tA.resize(k),B.resize(k),C.resize(k),D.resize(k);\r\n\t\tNTT(A,k,1),NTT(B,k,1),NTT(C,k,1);\r\n\t\tfor(re int i=0;iA,vector&B){\r\n\t\tB.resize(A.size());\r\n\t\tif(A.size()==1){B[0]=ksm(A[0],M-2);return;}\r\n\t\tre int nl=A.size()+1>>1;vectorA0,B0;A0.resize(nl);\r\n\t\tfor(re int i=0;iA,vector&B){\r\n\t\tvectorC;\r\n\t\tInv(A,C),Dif(A),mul(A,C,B);\r\n\t\tInt(B),B.resize(C.size());\r\n\t}\r\n\tinline void exp(vectorA,vector&B){\r\n\t\tB.resize(A.size());\r\n\t\tif(A.size()==1){\r\n\t\t\tB[0]=1;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tvectorA0,B0,tmp;re int nl=A.size()+1>>1;\r\n\t\tfor(re int i=0;iA,vector&B){\r\n\t\tB.resize(A.size());\r\n\t\tif(A.size()==1){B[0]=A[0]==1?1:R::sy(A[0],M);return;}\r\n\t\tvectorA0,B0,tmp;re int nl=A.size()+1>>1;\r\n\t\tfor(re int i=0;i>1)%M;\r\n\t\tmul(B0,B0,B0);\r\n\t\tB0.resize(A.size());\r\n\t\tfor(re int i=0;iA,vector&B,re int k){\r\n\t\tln(A,A);\r\n\t\tfor(re int i=0;iA,B,C;\r\nint n,m,k,mu[200002],ans;\r\nint main(){\r\n\tn=read(),k=read(),pre(524288),Getfac(n);\r\n\tif(k==1||n==1)return puts(\"1\"),0;\r\n\tfor(re int i=1;i<=n;++i){\r\n\t\tif(i==1)mu[i]=1;\r\n\t\tfor(re int j=i+i;j<=n;j+=i)mu[j]-=mu[i];\r\n\t}\r\n\tfor(re int i=1;i\n#define ll long long int\nusing namespace std;\nint main() {\n ll p,x,y,minm,res,j,k,s,i;\n \n map mark;\n cin>>p>>x>>y;\n minm=999999999999;\n for(i=x;i>=y;i-=50)\n {\n s=i;\n res=0;\n j= (s/50) %475;\n mark.clear();\n //mark[j]=1;\n for(k=1;k<=25;k++)\n {\n j= (j * 96 + 42)% 475;\n mark[(26+j)]=1;\n }\n if(mark[p]==1)\n {\n minm=0;\n res=0;\n if(res==0)\n break;\n }\n }\n if(minm!=0){\n for(i=x;;i+=50)\n {\n s=i;\n j= (s/50) %475;\n mark.clear();\n //mark[j]=1;\n for(k=1;k<=25;k++)\n {\n j= (j * 96 + 42)% 475;\n mark[(26+j)]=1;\n }\n if(mark[p]==1)\n {\n \n res=(i-x)/100;\n if((i-x)%100)\n res+=1;\n //res=ceil(diff);\n // cout<\n#define all(x) x.begin(), x.end()\n#define pb(x) push_back(x)\n#define cout2(x, y) cout << x << \" \" << y << endl\n\n\n#define N 1005\n#define SIZE 4\n\nusing namespace std;\n\n\n\nint MOD = 1000000007;\n\nstruct Matrix\n{\n long long X[SIZE][SIZE];\n \n Matrix () {}\n Matrix (int k)\n {\n memset(X, 0, sizeof(X));\n \n for(int i=0; i= MOD)tmp %= MOD;\n }\n M.X[i][j] = tmp;\n }\n }\n \n return M;\n}\n\nMatrix pow(Matrix x, long long n)\n{\n Matrix P(1);\n \n while(n)\n {\n if(n & 1) P = P * x;\n \n n >>= 1;\n x = x * x;\n }\n \n return P;\n}\n\nlong long solve(long long n){\n \n Matrix M;\n for(int i = 0; i < 4; i++)\n for(int j = 0; j < 4; j++)M.X[i][j] = 1;\n \n M.X[0][1] = M.X[0][3] = 0;\n M.X[1][1] = M.X[1][2] = 0;\n M.X[2][1] = M.X[2][3] = 0;\n M.X[3][0] = M.X[3][2] = 0;\n \n \n M = pow(M, n);\n long long ans = (M.X[3][0] + M.X[3][2])%MOD;\n \n return ans;\n} \n\n/*\nint memo[N][2][2], n;\nlong long DP(int pos, int last, int push){\n \n if(pos == 0)return push;\n int &ret = memo[pos][last][push];\n \n if(ret != -1)return ret;\n int ans = 0;\n \n if(push)ans = (ans + DP(pos - 1, 0, push) + DP(pos - 1, 1, push))%MOD;\n else{\n \n if(last == 1)ans = (DP(pos - 1, 1, 1) + DP(pos - 1, 0, 0))%MOD;\n else ans = (DP(pos - 1, 1, 0) + DP(pos - 1, 0, 0))%MOD;\n }\n return ret = ans;\n}\n\n*/\n\nlong long fastPow(long long b, long long n){\n \n long long ans = 1;\n while(n > 0){\n \n if(n&1)ans = ans * b;\n if(ans >= MOD)ans %= MOD;\n \n b = b * b;\n if(b >= MOD)b %= MOD;\n \n n >>= 1;\n }\n return ans; \n} \nint main(){\n\n unsigned long long n, l, k;\n while(cin>>n>>k>>l>>MOD){\n \n long long po = fastPow(2, n);\n long long ans = 1, aux = solve(n), aux2 = (po - aux + MOD)%MOD;\n long long num = (1ULL<= num && l != 64)puts(\"0\");\n else{\n \n if(l == 0){\n\n if(k != 0)puts(\"0\");\n else cout << 1%MOD << endl;\n }\n else{\n \n for(int i = 0; i < l; i++){\n \n if((k>>i)&1ULL)ans = (ans * aux)%MOD;\n else ans = (ans * aux2)%MOD;\n }\n \n cout << ans << endl;\n }\n \n }\n \n }\n \n}\n", "lang_cluster": "C++", "tags": ["math", "combinatorics", "matrices", "implementation", "number theory"], "code_uid": "6f3bf32917532b862bffc55b76f47807", "src_uid": "2163eec2ea1eed5da8231d1882cb0f8e", "difficulty": 2100.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "//In the name of Allah\n\n#include\n\nusing namespace std;\n\nconst int Maxn = 1000 * 1000 + 5, Mod = 1000 * 1000 * 1000 + 7;\nlong long n, m, powm[Maxn], pow2m[Maxn], ans;\n\nint main()\n{\n cin >> n >> m;\n powm[0] = 1;\n for(int i = 1; i < Maxn; i ++)\n powm[i] = (powm[i - 1] * m) % Mod;\n pow2m[0] = 1;\n for(int i = 1; i < Maxn; i ++)\n pow2m[i] = (pow2m[i - 1] * (2 * m - 1)) % Mod;\n ans = powm[n];\n for(int i = 0; i < n; i ++)\n ans += (powm[i + 1] * pow2m[n - i - 1]) % Mod, ans %= Mod;\n cout << ans << endl;\n return 0;\n}", "lang_cluster": "C++", "tags": ["combinatorics"], "code_uid": "8a1ec4378f377328f497581a84689522", "src_uid": "5b775f17b188c1d8a4da212ebb3a525c", "difficulty": 2300.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#define fin freopen(\"input.txt\", \"r\", stdin);\n#define fout freopen(\"output.txt\", \"w\", stdout);\n#define ll long long int\nconst int val = (1e5) + 5;\nconst int INF = (1e9) + 7;\nusing namespace std;\n\n\nint solve(){\n int n, a, b, c, d;\n cin >> n >> a >> b >> c >> d;\n long long int ans = 0;\n for(int i=1; i<=n; ++i){\n int l = 6e7, r = 0;\n l = min(l, a + b + i);\n l = min(l, a + c + i);\n l = min(l, b + d + i);\n l = min(l, c + d + i);\n r = max(r, a + b + i);\n r = max(r, a + c + i);\n r = max(r, b + d + i);\n r = max(r, c + d + i);\n ans += max(n - (r - l), 0);\n\n }\n cout << ans << endl;\n}\nint main(){\n //fin fout\n\tios_base::sync_with_stdio(0);\n solve();\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["brute force", "math", "constructive algorithms"], "code_uid": "657b67bd88bf1b2b4f98299374844c2e", "src_uid": "b732869015baf3dee5094c51a309e32c", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \n// #include \n// #include \nusing namespace std;\n// using namespace __gnu_pbds;\n#define lli long long int\n#define llu unsigned long long int\n#define fr(va, beg, end) for (lli va = beg; va < end; va++)\n#define pb push_back\n#define rt return 0\n#define endln \"\\n\"\n#define all(x) x.begin(), x.end()\n#define sz(x) (lli)(x.size())\nconst lli MOD = 1e9 + 7;\n// cout << fixed << setprecision(0) << pi <, rb_tree_tag,\n// tree_order_statistics_node_update>\n// new_data_set;\n// for multiset\n// typedef tree, rb_tree_tag,\n// tree_order_statistics_node_update>\n// new_data_set;\n// order_of_key(val): returns the number of values less than val\n// find_by_order(k): returns an iterator to the kth largest element (0-based)\n\nvoid solve() {\n lli n, a, b;\n cin >> n >> a >> b;\n if (a == b) {\n cout << \"1/1\" << endl;\n return;\n }\n\n if (a > b) {\n swap(a, b);\n }\n\n lli lcm = b / (__gcd(a, b));\n\n if (a > (n / lcm)) {\n lli pp = a - 1;\n lli nn = min(pp, n);\n lli vv = __gcd(nn, n);\n lli p = nn / vv;\n lli q = n / vv;\n cout << p << \"/\" << q << endl;\n } else {\n lcm = lcm * a;\n\n lli ans = n / lcm;\n ans++;\n ans = ans * a;\n ans--;\n // cout << ans << endl;\n lli val = ((n / lcm) * lcm) + a - 1;\n if (val > n) {\n ans -= (val - n);\n }\n\n lli vv = __gcd(ans, n);\n lli p = ans / vv;\n lli q = n / vv;\n cout << p << \"/\" << q << endl;\n }\n}\n\nint main(void) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n // lli t; cin >> t; while(t--)\n solve();\n rt;\n}", "lang_cluster": "C++", "tags": ["math"], "code_uid": "8d6c618be79d7e11eb7295e7c97dd709", "src_uid": "7a1d8ca25bce0073c4eb5297b94501b5", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long ll;\n#define FOR(i,n,m) for(int i=n; i> s;\n int n = s.size();\n M[i][j]=true;\n\n FOR(k,0,n){\n if( s[k] == 'L' ) j--;\n else if( s[k] == 'R' ) j++;\n else if( s[k] == 'U' ) i--;\n else if( s[k] == 'D' ) i++;\n\n if( M[i][j]) ans = false;\n if( k>0 && s[k]!='R' && M[i][j-1] ) ans = false;\n if( k>0 && s[k]!='L' && M[i][j+1] ) ans = false;\n if( k>0 && s[k]!='U' && M[i+1][j] ) ans = false;\n if( k>0 && s[k]!='D' && M[i-1][j] ) ans = false;\n M[i][j] = true;\n\n }\n cout << (ans?\"OK\":\"BUG\") << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["constructive algorithms", "implementation", "graphs"], "code_uid": "9e53f554c2d1a8702f8448138aa91048", "src_uid": "bb7805cc9d1cc907b64371b209c564b3", "difficulty": 1400.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\n\nlong long int raise(long long int p, long long pow){\n //cout <> p >> k;\n if(k==0){\n power = p-1;\n }\n else if(k==1){\n power = p;\n }\n else{\n long long int x = k;\n while(x!=1){\n x = (x*k)%p;\n power++;\n }\n power = (p-1)/power;\n }\n cout << raise(p, power) << endl;\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "b5545979f9d8b0ced688d9af7716639e", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n#define rep(i,a,b) for (int i=(a);i<=(b);i++)\n#define repd(i,a,b) for (int i=(a);i>=(b);i--)\n\ntypedef long long LL;\nconst int P=1000000007;\nint tot,p,k,len;\nbool vis[1000020];\n\nLL power(LL a,int n) {\n LL t=1;\n for (;n;n>>=1) {\n if (n&1) t=t*a%P;\n a=a*a%P;\n }\n return t;\n}\n\nint main() {\n //freopen(\"B.in\",\"r\",stdin);\n scanf(\"%d%d\\n\",&p,&k);\n if (k==0) {\n cout<\nusing namespace std;\n\nint M = 1000000007; \nbool visited[1010101];\nvoid DFS(bool visited[], long long v, int k, int p)\n{\n if(!visited[v])\n {\n visited[v] = true;\n DFS(visited, (v*k)%p, k, p);\n }\n}\n\nint main()\n{\n int p, k;\n cin >> p >> k;\n long long Ans;\n Ans = 1;\n if (k==1)\n {\n \tAns = p;\n }\n //for (int i = 1; i < p; ++i) visited[i]=false;\n for (int i = 1; i < p; ++i)\n {\n if(!visited[i])\n {\n DFS(visited, i, k, p);\n Ans = (Ans*p)%M;\n }\n }\n cout << Ans << endl;\n \n return 0;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "a9b5708b5f9f5759303953186de0601a", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#include \nusing namespace std;\ntypedef long long llong;\n#define maxn 1000010\nconst llong MOD = 1e9 + 7;\nbool vis[maxn];\n\nint main() {\n int p, k, _k;\n scanf(\"%d%d\", &p, &k);\n llong ans = 1;\n if (k != 0) {\n\n if (k == 1) ans = p;\n for (int i=1; i\nusing namespace std;\n\nconst int N = 1000009;\nconst int MOD = 1e9+7;\n\nlong long powerMod(long long a, long long b, long long MOD) {\n long long ret = 1, base = a;\n while(b) {\n if(b&1) ret = ret*base%MOD;\n base = base*base%MOD;\n b >>= 1;\n }\n return ret;\n}\n\nint dis[N];\n\nint main() {\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n int p, k;\n cin >> p >> k;\n int ans = 1;\n if(k == 0) {\n for(int i = 1; i < p; ++i)\n ans = (long long)ans*p%MOD;\n cout << ans << endl;\n return 0;\n }\n for(int i = 0; i < p; ++i) {\n if(dis[i]) continue;\n int j = i, d = 0;\n do {\n dis[j] = ++d;\n j = (long long)j*k%p;\n }while(!dis[j]);\n if(powerMod(k, d, p) == 1)\n ans = (long long)ans*p%MOD;\n }\n cout << ans << endl;\n \n#ifndef ONLINE_JUDGE\n fclose(stdin);fclose(stdout);\n#endif\n return 0;\n}", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "8561fe9fbb1ad3b0662bb84448fca250", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include\n#define all(x) x.begin(), x.end()\n#define pb push_back()\nusing namespace std;\nusing ll = long long;\nusing vi = vector;\nconst int maxn = 1e5 + 5, mod = 1e9 + 7;\nstruct dsu {\n\tvi r, p;\n\tvoid init(int n) {\n\t\tr.resize(n);\n\t\tp.resize(n);\n\t\tfor(int i = 0; i < n; i++) p[i] = i, r[i] = 1;\n\t}\n\tint par(int i) {\n\t\tif(i == p[i]) return i;\n\t\treturn p[i] = par(p[i]);\n\t}\n\tvoid unite(int i, int j) {\n\t\ti = par(i), j = par(j);\n\t\tif(i==j) return;\n\t\tif(r[i] < r[j]) swap(i, j);\n\t\tp[j] = i;\n\t\tr[i] += r[j];\n\t} \n};\nint n, k;\ndsu d;\nll pw(ll x, ll p, ll M) {\n\tll r = 1;\n\twhile(p--) r = (r*1ll*x)%M;\n\treturn r;\n}\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tcin >> n >> k;\n\tif(k==0) return cout << pw(n, n-1, mod), 0;\n\td.init(n);\n\tfor(int i = 0; i < n; i++) {\n\t\td.unite(i, (i*1ll*k)%n);\n\t}\n\tll ans = 1;\n\tfor(int i = 0; i < n; i++) {\n\t\tif(d.par(i)!=i) continue;\n\t\tll t = pw(k, d.r[i], n);\n\t\tif(t==1) {\n\t\t\tans = (ans*1ll*n)%mod;\n\t\t}\t\n\t}\n\tcout << ans << '\\n';\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "1c44edce56ae46582cdf7eedac5016ad", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#ifdef DEBUG\n#include \"debug.hpp\"\n#else\n#pragma GCC optimize(\"Ofast\")\n#define trace(...)\n#include \n#define endl '\\n'\n#endif\n\nusing namespace std;\nusing ll = long long;\n\nconst int MOD = 1'000'000'007;\n\nll modpow(ll b, ll e) {\n ll ans = 1;\n for (; e; e /= 2) {\n if (e % 2 == 1) ans = (ans * b) % MOD;\n b = (b * b) % MOD;\n }\n return ans;\n}\n\nstruct disjoint_set {\n vector parent, rnk;\n int comps;\n\n disjoint_set(int n) : parent(n), rnk(n), comps(n) {\n iota(parent.begin(), parent.end(), 0);\n }\n\n int find(int u) {\n if (parent[u] != u) parent[u] = find(parent[u]);\n return parent[u];\n }\n\n void merge(int u, int v) {\n u = find(u);\n v = find(v);\n\n if (u == v) return;\n if (rnk[u] > rnk[v]) swap(u, v);\n parent[u] = v;\n if (rnk[u] == rnk[v]) ++rnk[v];\n --comps;\n }\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n\n ll p, k;\n cin >> p >> k;\n\n vector seen(p);\n disjoint_set dsu(p);\n\n for (ll i = 0; i < p; ++i) {\n for (ll j = i; !seen[j]; j = (j * k) % p) {\n seen[j] = true;\n dsu.merge(i, j);\n }\n }\n\n int c = dsu.comps;\n if (k != 1) --c;\n\n cout << modpow(p, c) << endl;\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "934e06cc13cde274b60d94915c3d8c5c", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n\nusing namespace std;\n\nconst int N = 1e6 + 7;\nconst int MOD = 1e9 + 7;\nint p, k, cnt, v[N], num[N];\n\nvoid dfs(int x, int st){\n int cnt = 1;\n while (1ll * k * x % p != st){\n x = 1ll * k * x % p;\n v[x] = 1;\n cnt++;\n }\n num[cnt]++;\n}\n\nbool check(int x){\n for (int i = 2; i * i <= x; i++)\n if (x % i == 0) return 0;\n return 1;\n}\nvector vec;\n\nint main(){\n scanf(\"%d%d\", &p, &k);\n if (k == 0){\n long long ans = 1;\n for (int i = 1; i < p; i++){\n ans *= p;\n ans %= MOD;\n }\n printf(\"%d\\n\", (int)ans);\n return 0;\n }\n for (int i = 0; i < p; i++)\n if (!v[i]) dfs(i, i);\n\n for (int i = 1; i <= p; i++)\n if (num[i]) vec.push_back(i);\n\n //cout << vec.size() << endl;\n long long ans = 1;\n for (int i = 0; i < vec.size(); i++){\n long long t = 0;\n for (int j = 0; j < vec.size() && 1ll * vec[j] * vec[j] <= vec[i]; j++) if (vec[i] % vec[j] == 0){\n int a = vec[i] / vec[j];\n int b = vec[j];\n t += 1ll * a * num[a];\n if (a != b) t += 1ll * b * num[b];\n t %= MOD;\n }\n for (int j = 0; j < num[vec[i]]; j++){\n ans *= t;\n ans %= MOD;\n }\n }\n printf(\"%d\\n\", (int)ans);\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "5e48202915e224160a8ac14f0aa410be", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++", "source_code": "#include \nusing namespace std;\ntypedef long long ll;\n\n\nint main()\n{ ios_base::sync_with_stdio(0); cin.tie(NULL);\nll p,k,f=1;\nll mo=1000000007;\ncin>>p>>k;\nll a[p];\nif(k==0 ){\n ll ans=1;\n for(ll i=1;i\n\n#define FOR(i, a, b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))\n#define REP(i, a, b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))\n#define ALL(v) v.begin(),v.end()\n#define mp(a, b) make_pair(a, b)\n#define pb(a) push_back(a)\n#define pf(a) push_front(a)\n#define eb(a, b) emplace_back(a, b)\n#define fir first\n#define sec second\n#define what_is(x) cout<<#x<<\" is \"< PDD;\ntypedef pair PII;\ntypedef pair PIPII;\ntypedef pair PPIIPII;\ntypedef pair PLL;\ntypedef pair PUU;\ntypedef pair PLPLL;\n\n\ntemplate\nostream& operator << (ostream &out, pair obj)\n{\n return out<<\"(\"<\nostream& operator << (ostream &out, map cont)\n{\n typename map::const_iterator itr = cont.begin();\n typename map::const_iterator ends = cont.end();\n\n for(; itr!=ends; ++itr)\n out<<*itr<<\" \";\n out<\nostream& operator << (ostream &out, set cont)\n{\n typename set::const_iterator itr = cont.begin();\n typename set::const_iterator ends = cont.end();\n\n for(; itr!=ends; ++itr)\n out<<*itr<<\" \";\n out<\nostream& operator << (ostream &out, multiset cont)\n{\n typename multiset::const_iterator itr = cont.begin();\n typename multiset::const_iterator ends = cont.end();\n\n for(; itr!=ends; ++itr)\n out<<*itr<<\" \";\n out<> class CONT>\nostream& operator << (ostream &out, CONT cont)\n{\n typename CONT::const_iterator itr = cont.begin();\n typename CONT::const_iterator ends = cont.end();\n\n for(; itr!=ends; ++itr)\n out<<*itr<<\" \";\n out<\ntypename enable_if::value, basic_ostream &>::type\noperator << (basic_ostream &out, const T (&arr)[N])\n{\n REP(i, 0, N)\n out<\nT gcd(T a, T b)\n{\n T min_v = min(a, b);\n T max_v = max(a, b);\n\n while(min_v)\n {\n T temp = max_v % min_v;\n max_v = min_v;\n min_v = temp;\n }\n\n return max_v;\n}\n\n\ntemplate\nT lcm(T a, T b)\n{\n return (a*b) / gcd(a, b);\n}\n\n\ntemplate\nT fast_exp_pow(T base, T exp, T mod)\n{\n LL res = 1;\n\n while(exp)\n {\n if(exp&1)\n {\n res *= base;\n res %= mod;\n }\n\n exp >>= 1;\n base *= base;\n base %= mod;\n }\n\n return res;\n}\n\n/*#################################################################################################################\n#################################################################################################################\n#################################################################################################################\n#################################################################################################################*/\n\nLL ways;\nint P, K;\nint Parent[SIZE], Rank[SIZE];\nset groups;\n\n\n\nint find_parent(int nd)\n{\n if(Parent[nd] != nd)\n Parent[nd] = find_parent(Parent[nd]);\n\n return Parent[nd];\n}\n\n\nvoid Union(int nd1, int nd2)\n{\n if(Rank[nd1] >= Rank[nd2])\n {\n Parent[nd2] = nd1;\n ++Rank[nd1];\n }\n\n else\n {\n Parent[nd1] = nd2;\n ++Rank[nd2];\n }\n}\n\n\n\n\nint main()\n{\n scanf(\"%d%d\", &P, &K);\n\n\n REP(i, 1, P)\n {\n Parent[i] = i;\n Rank[i] = 1;\n }\n\n REP(i, 1, P)\n {\n int value = ((LL)K * (LL)i) % P;\n int Pa = find_parent(i);\n int Pb = find_parent(value);\n\n if(Pa != Pb)\n Union(Pa, Pb);\n }\n\n REP(i, 1, P)\n groups.insert(find_parent(i));\n\n if(!K)\n ways = fast_exp_pow((LL)P, (LL)(P-1), (LL)MAXN);\n else if(K == 1)\n ways = fast_exp_pow((LL)P, (LL)P, (LL)MAXN);\n else\n ways = fast_exp_pow((LL)P, (LL)groups.size(), (LL)MAXN);\n\n\n printf(\"%d\\n\", ways);\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "4cdcdb0b7ffbf2713883c0479e27c091", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \nusing namespace std;\n\n#define FOR(i,l,r) for(int i = (l);i < (r);i++)\n#define ALL(x) (x).begin(),(x).end()\ntemplate bool chmax(T& a,const T& b){return a < b ? (a = b,true) : false;}\ntemplate bool chmin(T& a,const T& b){return b < a ? (a = b,true) : false;}\ntypedef long long ll;\n\nll K,P;\nbool used [1000000];\nconst ll MOD = 1e9 + 7;\n\nvoid dfs(ll curr)\n{\n\tif(used [curr]) return;\n\tused [curr] = true;\n\tdfs(curr * K % P);\n}\n\nint main()\n{\n\tscanf(\"%lld%lld\",&P,&K);\n\tll ans = 1;\n\tif(K == 1){\n\t\t(ans *= P) %= MOD;\n\t}\n\tfor(ll i = 1;i < P;i++) if(used [i] == false){\n\t\tdfs(i);\n\t\t(ans *= P) %= MOD;\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "426d1e73cea8039eb3009be595bd348e", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++14", "source_code": "#include \n \nusing namespace std;\n \nconst int M = 1e9 + 7;\n \nint main() {\n int k, p;\n cin >> p >> k;\n int cnt;\n if(k == 0) {\n cnt = p - 1;\n } else if(k == 1) {\n cnt = p;\n } else {\n cnt = 0;\n vector vis(p, false);\n for(int i = 1; i < p; i++) {\n if(!vis[i]) {\n vis[i] = true;\n cnt++;\n for(int j = i * 1ll * k % p; !vis[j]; j = (j * 1ll * k) % p) {\n vis[j] = true;\n }\n }\n }\n }\n int ans = 1;\n for(int j = 0; j < cnt; j++) {\n ans = (ans * 1ll * p) % M;\n }\n cout << ans;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "e76d9e3075bb1bc3f662b2c5d4eb66f4", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \nusing namespace std;\nconst int MOD = 1000000007;\nint power(int a,int b)\n{\n long long ans=1;\n while(b)\n {\n ans*=a;\n ans%=MOD;\n --b;\n }\n return ans;\n}\nint main()\n{\n\tint p,k;\n\tcin>>p>>k;\n\tif(k==0)\n\t\tcout<\n#include \n#include \n\n#define LL long long\n\n#define REP(i, x, y) for (int i = x, _ = y; i <= _; i ++)\n#define rep(i, x, y) for (int i = x, _ = y; i >= _; i --)\n\nconst int MAXP = 1e6 + 1e5, MOD = 1e9 + 7;\n\nLL k, p;\nint tot;\nLL ans;\nint vis[MAXP];\nint val[MAXP];\n\nvoid Mod(LL &x) { x = (x % MOD + MOD) % MOD; }\n\nvoid Bfs(int x)\n{\n ++ tot;\n int x0 = x;\n LL w = 1;\n do {\n vis[x] = true;\n x = x * k % p;\n w = w * k % p;\n } while (x != x0);\n Mod(ans *= (w == 1? p : 1));\n}\n\nvoid Solve()\n{\n if (k == 0) {\n LL w = 1;\n REP (i, 1, p - 1)\n w = w * p % MOD;\n printf(\"%lld\\n\", w);\n return ;\n }\n ans = 1;\n REP (i, 0, p - 1)\n if (vis[i] == 0)\n Bfs(i);\n printf(\"%lld\\n\", ans);\n}\n\nint main()\n{\n if (fopen(\"B.in\", \"r\") != NULL) {\n freopen(\"B.in\", \"r\", stdin);\n freopen(\"B.out\", \"w\", stdout);\n }\n\n scanf(\"%lld%lld\", &p, &k);\n Solve();\n\n return 0;\n}\n", "lang_cluster": "C++", "tags": ["dfs and similar", "math", "combinatorics", "dsu", "number theory"], "code_uid": "7c581ed56e32ab6125f73faa0d82e828", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "MS C++", "source_code": "#include\n#include\n#include\n#include\n#include\nusing namespace std;\nlong long stmod(long long a, long long b, long long p)\n{\n\tint x;\n\tif(b==0)\n\t{\n\t\treturn 1;\n\t}\n\telse\n\t{\n\t\tif(b%2==0)\n\t\t{\n\t\t\tx=stmod(a, b/2, p);\n\t\t\tx=(x*x)%p;\n\t\t\treturn x;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tx=stmod(a, b-1, p);\n\t\t\tx=(x*a)%p;\n\t\t\treturn x;\n\t\t}\n\t}\n}\nlong long n, m, k, s[100005], t, i, j, x[100005], p, pcon, io, ans, lk, jj, pr, obr[1000005], a, b, c;\nstring ss;\nint main()\n{\n\tcin>>p>>k;\n\tpcon=1000000007;\n\tobr[1]=1;\n\tfor(i=2; i<1000003; i++)\n\t{\n\t\ta=pcon/i;\n\t\tb=pcon%i;\n\t\tc=a*(pcon-obr[b]);\n\t\tc=c%pcon;\n\t\tobr[i]=c;\n\t}\n\t//cout<<228;\n\tif(k==0)\n\t{\n\t\tans=1; \n\t\tfor(i=0; i\n\nusing namespace std;\n\n\n//f(kx mod p) = k f(1) mod p\nconst int max_p = 1000000;\nconst long modulo = 1000000007;\nint main()\n{\n\tlong long p, k;\n\tcin >> p >> k;\n\n\tif ((k != 1)&&(k!=0))\n\t{\n\t\tbool residue[max_p];\n\t\tfor (int i = 0; i < p; i++)\n\t\t\tresidue[i] = true;\n\n\t\tlong long answer = 1;\n\t\tfor (int i = 1; i < p; i++)\n\t\t{\n\t\t\tif (residue[i])\n\t\t\t{\n\t\t\t\tlong long temp = i;\n\t\t\t\t//cout << i << \" \";\n\t\t\t\tfor (int j = 1; j < p; j++)\n\t\t\t\t{\n\t\t\t\t\ttemp = temp * k;\n\t\t\t\t\ttemp = temp % p;\n\t\t\t\t\tif (temp == i)\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tresidue[temp] = false;\n\t\t\t\t\t//cout << temp << \" \";\n\t\t\t\t}\n\t\t\t\t//cout << endl;\n\t\t\t\tanswer *= p;\n\t\t\t\tanswer = answer % modulo;\n\t\t\t}\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\telse if(k==1)\n\t{\n\t\tlong long answer = 1;\n\t\tfor (int i = 0; i < p; i++)\n\t\t{\n\t\t\tanswer *= p;\n\t\t\tanswer = answer % modulo;\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\telse\n\t{\n\t\tlong long answer = 1;\n\t\tfor (int i = 0; i < p-1; i++)\n\t\t{\n\t\t\tanswer *= p;\n\t\t\tanswer = answer % modulo;\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "3f0506546a806d71c18b27540d819614", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++17", "source_code": "#include \nusing namespace std;\n\nusing ll = long long;\nusing pii = pair;\nusing vi = vector;\n#define pb push_back\n#define mp make_pair\n\nll par[1100000];\nll getPar(ll x) {\n if (par[x] == x) return x;\n return par[x] = getPar(par[x]);\n}\n\nvoid Union(ll x, ll y) {\n if (getPar(x) == getPar(y)) return;\n par[getPar(x)] = getPar(y);\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n ll p, k;\n cin >> p >> k;\n\n for (ll i = 0; i < p; i++) par[i] = i;\n\n for (ll i = 1; i < p; i++) {\n if ((k*i)%p != 0) Union((k*i)%p, i);\n }\n\n unordered_set seen;\n for (ll i = 1; i < p; i++) {\n seen.insert(getPar(i));\n }\n\n ll pw = seen.size();\n if (k == 1) pw = p;\n\n ll blah = 1;\n for (ll i = 0; i < pw; i++) {\n blah *= p;\n blah %=(ll) 1e9+7;\n }\n cout << blah << endl;\n}", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "c718b3fa2fd92c4e1cb3dedd831d648c", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include\nusing namespace std;\n\nusing vi = vector; using vvi = vector;\nusing ii = pair; using vii = vector;\nusing l = long long; using vl = vector; using vvl = vector;\nusing ll = pair; using vll = vector; using vvll = vector;\nusing lu = unsigned long long;\nusing vb = vector; using vvb = vector;\nusing vd = vector; using vvd = vector;\nconst int INF = numeric_limits::max();\nconst double EPS = 1e-10;\nconst l e5 = 100000, e6 = 1000000, e7 = 10000000, e9 = 1000000000;\n\n// (base^power) % mod, safe for l near max\nl pow_mod(l base, l power, l mod) {\n l r = 1;\n while (power) {\n if (power % 2) r = (r * base) % mod;\n base = (base * base) % mod;\n power /= 2;\n }\n return r;\n}\n\nconst l MOD = e9 + 7;\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0);\n l p, k;\n while (cin >> p >> k) {\n if (k == 0) {\n cout << pow_mod(p, p - 1, MOD) << endl;\n continue;\n }\n if (k == 1) {\n cout << pow_mod(p, p, MOD) << endl;\n continue;\n }\n vector bs(p);\n l pow = 0;\n for (l i = 1; i < p; i++) {\n if (bs[i]) continue;\n l j = i;\n l w = 0;\n while (!bs[j]) {\n w++;\n bs[j] = true;\n j = (j * k) % p;\n }\n if (pow_mod(k, w, p) == 1) pow++;\n // pow++;\n }\n cout << pow_mod(p, pow, MOD) << endl;\n }\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "81d807095375e1a47c9bb34a6033b086", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#include \n#define MOD 1ll * 1000000007\nusing namespace std;\n\nint put(int nr, int p);\n\nint main()\n{\n\tlong long p, k, q;\n\tcin >> p >> k;\n\tq = k;\n\tif (k == 0) {\n\t\tcout << put(p, p - 1);\n\t\t\treturn 0;\n\t}\n\tif (k == 1) {\n\t\tcout << put(p, p);\n\t\treturn 0;\n\t}\n\tint n = 1;\n\twhile (q != 1) {\n\t\tq *= k;\n\t\tq %= p;\n\t\tn++;\n\t}\n\tcout << put(p, (p - 1) / n);\n\treturn 0;\n}\n\nint put(int nr, int p)\n{\n\tif (p == 0)\n\t\treturn 1;\n\tlong long a = put(nr, p / 2);\n\ta *= a;\n\ta %= MOD;\n\tif (p & 1) {\n\t\ta *= nr;\n\t\ta %= MOD;\n\t}\n\treturn a;\n}\n", "lang_cluster": "C++", "tags": ["dsu", "math", "combinatorics", "number theory"], "code_uid": "68ee510cbe0307ff96fdca90a91776bd", "src_uid": "580bf65af24fb7f08250ddbc4ca67e0e", "difficulty": 1800.0, "exec_outcome": "PASSED"} {"lang": "GNU C++11", "source_code": "#define _USE_MATH_DEFINES\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nusing namespace std;\n\ntypedef long long int LL;\ntypedef vector VI;\ntypedef vector VVI;\ntypedef vector VVVI;\ntypedef pair II;\ntypedef vector VII;\ntypedef vector VVII;\ntypedef pair III;\ntypedef vector VIII;\ntypedef vector VD;\ntypedef vector VVD;\ntypedef pair DD;\ntypedef vector