Advertisement
Kevin_Zhang

Untitled

Apr 24th, 2020
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define pb emplace_back
  5. const int maxn = 100010, maxm = 105, mod = 1e9+7;
  6. int p;
  7. int tar[maxm], tl, n;
  8. vector<int> edge[maxn];
  9. int a[maxn];
  10. ll res;
  11. int c[maxn], cl;
  12. bool check() {
  13.     assert(c[0]);
  14.     __int128 a = 0, b = 0;
  15.     for (int i = 0;i < tl;++i) a = a * p + tar[i];
  16.     for (int i = 0;i < cl;++i) b = b * p + c[i];
  17.     //cerr << "A " << (ll)a << " B " << (ll)b << '\n';
  18.     return b >= a;
  19.  
  20.  
  21.  
  22. //
  23. //  for (int i = 0;i < tl;++i)
  24. //      cerr << tar[i] << ' ';
  25. //  cerr << '\n';
  26. //  for (int i =0;i < cl;++i)
  27. //      cerr << c[i] << ' ';
  28. //  cerr << '\n';
  29.     if (p < 0 && (~tl&1)) {
  30.         // target is negative
  31.         if (cl&1) return true;
  32.         // Mine is positive
  33.         if (cl < tl) return true;
  34.         // Mine is bigger
  35.         if (cl > tl) return false;
  36.         // Mine is smaller
  37.         for (int i = 0;i < cl;++i)
  38.             if (c[i] != tar[i])
  39.                 return (~i&1) ? c[i] > tar[i] : c[i] < tar[i];
  40.         return true;
  41.     }
  42.     else {
  43.         // target is positive
  44.  
  45.         if (p < 0 && (~cl&1)) return false;
  46.         // Mine negative
  47.         if (cl < tl) return false;
  48.         if (cl > tl) return true;
  49.         for (int i = 0;i < cl;++i)
  50.             if (c[i] != tar[i])
  51.                 return (p<0 && (i&1)) ? -c[i] > -tar[i] : c[i] > tar[i];
  52.         return true;
  53.     }
  54. }
  55. void dfs(int now) {
  56.     //cerr << "DFS " << now << '\n';
  57.     c[cl++] = a[now];
  58.     int v = check();
  59.     //cerr << (v ? "GOOD " : " BAD" ) << "\n\n";
  60.     res += v;
  61.     for (int u : edge[now])
  62.         dfs(u);
  63.     --cl;
  64. }
  65.  
  66. void solve() {
  67.     for (int i = 0;i < n;++i)
  68.         if (a[i]) dfs(i);
  69.     if (tl == 1 && tar[0] == 0)
  70.         res += count(a, a+n, 0);
  71. }
  72.  
  73. signed main() {
  74.     ios_base::sync_with_stdio(0), cin.tie(0);
  75.     cin >> n >> p >> tl;
  76.     for (int i = 0;i < tl;++i)
  77.         cin >> tar[i];
  78.     for (int i = 0;i < n;++i)
  79.         cin >> a[i];
  80.     int E;
  81.     cin >> E;
  82.     for (int a, b;E--;) {
  83.         cin >> a >> b;
  84.         edge[a].pb(b);
  85.     }
  86.     solve();
  87.     cout << res % mod << '\n';
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement