結果

問題 No.1019 最小格子三角形
ユーザー beetbeet
提出日時 2020-03-30 09:57:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 652 ms / 2,000 ms
コード長 2,870 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 202,524 KB
実行使用メモリ 59,080 KB
最終ジャッジ日時 2023-09-02 08:03:06
合計ジャッジ時間 19,301 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 454 ms
58,816 KB
testcase_01 AC 444 ms
58,760 KB
testcase_02 AC 439 ms
58,760 KB
testcase_03 AC 450 ms
58,772 KB
testcase_04 AC 456 ms
59,080 KB
testcase_05 AC 449 ms
58,776 KB
testcase_06 AC 443 ms
58,808 KB
testcase_07 AC 454 ms
58,768 KB
testcase_08 AC 453 ms
58,772 KB
testcase_09 AC 460 ms
58,776 KB
testcase_10 AC 452 ms
58,776 KB
testcase_11 AC 453 ms
58,760 KB
testcase_12 AC 452 ms
58,964 KB
testcase_13 AC 602 ms
58,808 KB
testcase_14 AC 642 ms
58,772 KB
testcase_15 AC 633 ms
58,764 KB
testcase_16 AC 605 ms
58,820 KB
testcase_17 AC 624 ms
58,884 KB
testcase_18 AC 603 ms
58,776 KB
testcase_19 AC 637 ms
58,764 KB
testcase_20 AC 606 ms
58,884 KB
testcase_21 AC 627 ms
58,828 KB
testcase_22 AC 634 ms
59,048 KB
testcase_23 AC 642 ms
58,776 KB
testcase_24 AC 646 ms
58,964 KB
testcase_25 AC 652 ms
58,768 KB
testcase_26 AC 634 ms
58,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}

//INSERT ABOVE HERE
const int MAX = 1e6 + 10;
vector<int> dp[MAX];


using ll = long long;
ll naive(ll n){
  ll ans=0;
  for(ll p1=-n;p1<=n;p1++){
    for(ll p2=-n;p2<=n;p2++){
      for(ll q1=-n;q1<=n;q1++){
        for(ll q2=-n;q2<=n;q2++){
          if(p1*p1+p2*p2>n) continue;
          if(q1*q1+q2*q2>n) continue;
          if(p1*q2-p2*q1!=1) continue;
          ans+=abs(p1)+abs(p2)+abs(q1)+abs(q2);
        }
      }
    }
  }
  return ans;
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  using ll = long long;
  ll n;
  cin>>n;

  // cout<<naive(n)<<endl;

  for(ll i=2;i<MAX;i++){
    if(!dp[i].empty()) continue;
    for(ll j=i;j<MAX;j+=i)
      dp[j].emplace_back(i);
  }

  using M = Mint<int, 998244353>;
  M ans{0};

  for(ll i=1;i*i<=n;i++){
    ll j=sqrtl(n-i*i);
    while(i*i+(j+1)*(j+1)<=n) j++;
    while(i*i+j*j>n) j--;


    ll m=dp[i].size();
    ll s=1<<m;
    ll cnt=0;
    for(ll b=0;b<s;b++){
      ll prod=1;
      for(ll k=0;k<m;k++)
        if((b>>k)&1) prod*=dp[i][k];
      if(__builtin_parity(b)) cnt-=j/prod;
      else cnt+=j/prod;
    }

    ans+=M(i*4)*M(6)*M(cnt);
  }

  ans*=M(2);
  ans+=M(8);
  cout<<ans<<newl;
  return 0;
}
0