Submission #13427984


Source Code Expand

Copy
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod =  998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

template<int mod>
struct ModInt {
    long long x;
 
    ModInt() : x(0) {}
    ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    explicit operator int() const {return x;}
 
    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }
 
    ModInt operator-() const { return ModInt(-x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
 
    ModInt inverse() const{
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return ModInt(u);
    }

    ModInt power(long long p) const{
        int a = x;
        if (p==0) return 1;
        if (p==1) return ModInt(a);
        if (p%2==1) return (ModInt(a)*ModInt(a)).power(p/2)*ModInt(a);
        else return (ModInt(a)*ModInt(a)).power(p/2);
    }

    ModInt power(const ModInt p) const{
        return ((ModInt)x).power(p.x);
    }

    friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
        return os << p.x;
    }
    friend istream &operator>>(istream &is, ModInt<mod> &a) {
        long long x;
        is >> x;
        a = ModInt<mod>(x);
        return (is);
    }
};

using modint = ModInt<mod>;



template<typename T>
vector<T> divisor_list(T x){
    vector<T> res;
    for(T i=1;i*i<=x;i++){
        if (x%i==0) {
            res.push_back(i);
            if (i*i!=x) res.push_back(x/i);
        }
    }
    return res;
}

int n;
string s;
modint f[500010],g[500010],w[500010],S[500010];

string inverse(const string &t){
    string t_="";
    rep(i,t.length()){
        if(t[i]=='0') t_+="1";
        if(t[i]=='1') t_+="0";
    }
    return t_;
}

string string_copy(const string &t,int k){
    string t_="";
    string inv_t=inverse(t);
    //cout << t << " " << inv_t << endl;
    rep(i,k){
        if(i%2==0)t_+=t;
        else t_+=inv_t;
    }
    return t_;
}

void solve(){
    cin >> n;
    cin >> s;
    rep(i,n){
        S[i+1]=(modint)2*S[i]+(s[i]-'0');
    }
    Rep(i,1,n+1){
        if(i==1) w[1]=1;
        else w[i]*=-1;
        for(int j=2*i;j<=n;j+=i){
            w[j]+=w[i];
        }
    }
    vector<int> vs,d_list=divisor_list(2*n);
    for(int d:d_list){
        if((2*n/d)%2==1){
            vs.push_back(d);
        }
    }
    for(int v:vs){
        f[v]=S[v/2];
        string t=string_copy(s.substr(0,v/2),2*n/v);
        //cout << v << " " << s << " " << t << endl;
        if(s>=t) f[v]+=1;
        for(int d=v;d<=2*n;d+=v){
            g[d]+=w[d/v]*f[v];
        }
    }
    modint ans=0;
    for(int v:vs){
        //cout << v << " " << f[v] << " " << g[v] << endl;
        ans+=g[v]*(modint)(v);
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

Submission Info

Submission Time
Task C - Division by Two with Something
User Chanyuh
Language C++14 (GCC 5.4.1)
Score 800
Code Size 4751 Byte
Status AC
Exec Time 29 ms
Memory 16788 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 49
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 21 ms 16724 KB
02.txt AC 17 ms 16788 KB
03.txt AC 16 ms 16724 KB
04.txt AC 28 ms 16788 KB
05.txt AC 20 ms 16724 KB
06.txt AC 22 ms 16596 KB
07.txt AC 17 ms 16596 KB
08.txt AC 14 ms 16368 KB
09.txt AC 18 ms 16788 KB
10.txt AC 25 ms 16596 KB
11.txt AC 23 ms 16724 KB
12.txt AC 18 ms 16660 KB
13.txt AC 17 ms 16724 KB
14.txt AC 29 ms 16788 KB
15.txt AC 21 ms 16724 KB
16.txt AC 22 ms 16596 KB
17.txt AC 18 ms 16596 KB
18.txt AC 15 ms 16368 KB
19.txt AC 19 ms 16660 KB
20.txt AC 26 ms 16596 KB
21.txt AC 21 ms 16724 KB
22.txt AC 17 ms 16788 KB
23.txt AC 16 ms 16724 KB
24.txt AC 28 ms 16788 KB
25.txt AC 20 ms 16724 KB
26.txt AC 21 ms 16596 KB
27.txt AC 17 ms 16596 KB
28.txt AC 14 ms 16368 KB
29.txt AC 18 ms 16788 KB
30.txt AC 25 ms 16596 KB
31.txt AC 21 ms 16724 KB
32.txt AC 17 ms 16660 KB
33.txt AC 16 ms 16724 KB
34.txt AC 28 ms 16788 KB
35.txt AC 20 ms 16724 KB
36.txt AC 21 ms 16596 KB
37.txt AC 17 ms 16596 KB
38.txt AC 14 ms 16368 KB
39.txt AC 18 ms 16660 KB
40.txt AC 25 ms 16596 KB
41.txt AC 20 ms 16724 KB
42.txt AC 17 ms 16660 KB
43.txt AC 6 ms 15872 KB
44.txt AC 6 ms 15872 KB
45.txt AC 6 ms 15872 KB
46.txt AC 6 ms 15872 KB
s1.txt AC 6 ms 15872 KB
s2.txt AC 6 ms 15872 KB
s3.txt AC 6 ms 15872 KB