CSES - Putka Open 2015 – 5/6 - Results
Submission details
Task:Käännöt
Sender:
Submission time:2015-11-08 05:35:45 +0200
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:15:16: error: 'assert' was not declared in this scope
   assert(n <= 3);
                ^

Code

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

const uint64_t MOD=1000*1000*1000+7;

uint64_t pairs(uint64_t a){return a?((a-1)*(a-2)/2+a-1):0;}

int main(){
  std::string s;std::cin>>s;
  std::reverse(s.begin(),s.end());
  uint64_t n=s.size();

  assert(n <= 3);
  for (char c:s) if (c < '1' || c > '9') throw c;

  std::vector<uint64_t>pow10(n);pow10[0]=1;
  for(unsigned i=1;i<n;++i)pow10[i]=(pow10[i-1]*10)%MOD;

  uint64_t sum1=0;
  for(unsigned i=0;i<n;++i){
    for(unsigned j=i;j<n;++j){
      std::reverse(&s[i],&s[j+1]);
      for(unsigned k=0;k<n;++k)
	(sum1+=(s[k]-'0')*pow10[k])%=MOD;
      std::reverse(&s[i],&s[j+1]);
    }
  }

  auto pow10_range=pow10;
  for(unsigned i=1;i<n;++i)(pow10_range[i]+=pow10_range[i-1])%=MOD;

  std::vector<uint64_t>T(n);
  T[n-1]=T[0]=pow10_range[n-1];
  for(unsigned i=1;i<(n+1)/2;++i)
    T[n-i-1]=T[i]=(T[i-1]+pow10_range[n-i-1]-pow10_range[i-1])%MOD;

  uint64_t sum=0;
  for(unsigned k=0;k<n;++k){
    sum+=(s[k]-'0')*pow10[k]*(pairs(k+1)+pairs(n-k));
    sum%=MOD;
    sum+=(s[k]-'0')*T[k];
    sum%=MOD;
  }
  if (n == 3) std::cout << sum1 << '\n';
  else std::cout << sum1 << '\n';
}