QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#735064#5460. Sum of NumbersqtoqWA 43ms7552kbC++174.2kb2024-11-11 16:58:362024-11-11 16:58:36

Judging History

你现在查看的是最新测评结果

  • [2024-11-11 16:58:36]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:7552kb
  • [2024-11-11 16:58:36]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;

template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef ONPC
  #define deb(...) cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
  #pragma GCC optimize("O3,unroll-loops")
  #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  #define deb(...)
#endif

// c++ short types
#define vt vector
//typedef long long ll;
typedef long double ld;

void whattime() { cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; }
// const int mod = 1e9 + 7;
const int mod = 998244353;
const int inf = 1e9;
const int64_t infll = 1e13;
bool debug = false;
const ld eps = 1e-9;
const ld pi = acos(-1);
mt19937_64 rng((int) chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5, K = 10;
int subvalue[maxn][K];

struct num {
  const int base = 1000'000'000;
  vt<int> elems;
  num(int x = 0) {
    elems = {x};
  }
  num(int l, int r) {
    for(int i = r; i >= l; i -= 9) {
      if(i - 8 >= l) {
        elems.push_back(subvalue[i-8][9]);
      } else {
        elems.push_back(subvalue[l][i-l+1]);
      }
    }
    if(elems.empty()) {
      elems = {0};
    }
  }
  void asd() {
    while(not elems.size() > 1 and elems.back() == 0) {
      elems.pop_back();
    }
    if(elems.empty()) {
      elems.push_back(0);
    }
  }
  void add(num &b) {
    int carry = 0;
    for(int i = 0; i < max(elems.size(), b.elems.size()) || carry > 0; ++i) {
      if(elems.size() == i) {
        elems.push_back(0);
      }
      elems[i] += carry + (i < b.elems.size() ? b.elems[i] : 0);
      carry = elems[i] >= base;
      if(carry) {
        elems[i] -= base;
      }
      if(false and carry == 0 and i >= b.elems.size()) {
        break;
      }
    }
  }
  void print() {
    asd();
    for(int i = elems.size() - 1; i >= 0; --i) {
      cout << elems[i];
    }
    cout << '\n';
  }
};

bool cmp(num &a, num &b) {
  a.asd();
  b.asd();
  if(a.elems.size() != b.elems.size()) {
    return a.elems.size() < b.elems.size();
  }
  for(int i = a.elems.size()-1; i>=0; --i) {
    if(a.elems[i] != b.elems[i]){
      return a.elems[i] < b.elems[i];
    }
  }
  return false;
}

void solve() {
  int n, k;
  if(debug) {
    n = 2e5;
    k = 6;
  } else {
    cin >> n >> k;
  }
  string s;
  if(debug) {
    for(int i = 0; i < n; ++i) {
      s += (char)('1' + rand() % 9);
    }
  } else {
    cin >> s;
  }
  for(int i = 0; i < n; ++i) {
    for(int j = 1; j < K; ++j) {
      subvalue[i][j] = stoi(s.substr(i, j));
    }
  }
  num res(0, s.size()-1);
  vt<int> z = {0};
  num a = {0,0};
  num b = {1,1};
  auto Dfs = [&](auto self, int i = 0, int len = 0) -> void {
    if(i == k) {
      int tot = 0;
      for(auto x: z) {
        tot += x;
      }
      // (k+1) * x + tot = n
      // x = (n-tot) / (k+1)
      if((n-tot) % (k+1) == 0) {
        int x = (n-tot) / (k + 1);
        bool flag = true;
        for(auto y: z) {
          flag = flag and y + x > 0;
        }
        if(flag) {
          num val = {0};
          int last = 0;
          for(auto y: z) {
            num tmp = {last, y + x + last - 1};
            val.add(tmp);
            last += y + x;
          }
          if(cmp(val, res)) {
            swap(res.elems, val.elems);
          }
        }
      }
      return ;
    }
    for(int x = -1; x <= 1; ++x) {
      z.push_back(len + x);
      self(self, i + 1, len + x);
      z.pop_back();
    }
  };
  Dfs(Dfs);
  res.print();
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int tt = 1;;
	if(debug) {
    tt = 1;
	} else {
		cin >> tt;
	}

	for(int t = 0; t < tt; ++t) {
		solve();
	}
	
#ifdef ONPC
	whattime();
#endif
	
	return 0;
}


详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3544kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 43ms
memory: 7552kb

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

286183755510664079479706773787991386068676466159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
13308978966559747740355864065449074348428350483364112711104278364830634579508738245622889343640965465374923674183...

result:

wrong answer 1st lines differ - expected: '286183755510664079479706773787...6909797866802717925250679901255', found: '286183755510664079479706773787...6909797866802717925250679901255'