Problem:
B： えびちゃんと数列 - Ebi-chan and Integer Sequences -
問題
えびちゃんは数列が好きです。中でも等差数列が特に好きです。
今回は以下の条件を満たす数列を作ることにしました。
長さ
n
の等差数列である
数列の
i
番目の要素を
s_i
と定める時、全ての
s_i (1 \leq i \leq n)
が
0 \leq s_i \leq m
を満たす整数である
上の条件を満たすような数列はいくつあるでしょうか？
ただし答えは非常に大きくなることがあるので、
10^9 + 7
で割った余りを出力してください。
入力形式
入力は一行で与えられる。
n
m
n
は数列の長さを表す。
制約
1 \leq n \leq 10^{15}
0 \leq m \leq 10^{15}
出力形式
条件を満たす等差数列の数を
10^9 + 7
で割った余りを一行に出力せよ。
入力例1
3 9
出力例1
50
入力例2
10000000000 10000000000
出力例2
999999942
入力は32bit整数には収まりきらないことに注意してください。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

template<int mod> struct ModInt {
	using ll = long long;
	int x;
	template<class T>
	ModInt(T a) {
		x = a % mod;
		if (x < 0) {
			x += mod;
		}
	}
	ModInt() : x(0) {}
	inline ModInt& operator+=(const ModInt& rhs)
	{
		(*this) += rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator+=(const T rhs)
	{
		x += rhs % mod;
		if (x < 0) x += mod;
		x %= mod;
		return *this;
	}
	inline ModInt& operator-=(const ModInt& rhs)
	{
		(*this) -= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator-=(const T rhs)
	{
		x -= rhs % mod;
		if (x < 0) x += mod;
		x %= mod;
		return *this;
	}
	inline ModInt& operator*=(const ModInt& rhs)
	{
		(*this) *= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator*=(const T rhs)
	{
		ll res = (ll) x * (rhs % mod);
		x = res % mod;
		if (x < 0) x += mod;
		return *this;
	}
	inline ModInt& operator/=(const ModInt& rhs)
	{
		(*this) /= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator/=(const T rhs)
	{
		int t = rhs % mod;
		if (t < 0) t += mod;
		ll res = modpow(t);
		(*this) *= res;
		return *this;
	}
	inline ModInt& operator=(const ModInt& rhs)
	{
		(*this) = rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator=(const T rhs)
	{
		x = rhs % mod;
		if (x < 0) x += mod;
		return *this;
	}
	inline int operator==(const ModInt& rhs) const
	{
		return (*this) == rhs.x;
	}
	template<class T>
	inline int operator==(const T rhs) const
	{
		ModInt t(rhs);
		return (*this).x == t.x;
	}
	inline int operator!=(const ModInt& rhs) const
	{
		return (*this) != rhs.x;
	}
	inline int operator!=(const int rhs) const
	{
		ModInt t(rhs);
		return (*this).x != t.x;
	}
	inline ModInt operator++(int unused)
	{
		ModInt res((*this).x);
		++(*this);
		return res;
	}
	inline ModInt& operator++()
	{
		(*this) += 1;
		return (*this);
	}
	inline ModInt operator--(int unused)
	{
		ModInt res((*this).x);
		--(*this);
		return res;
	}
	inline ModInt& operator--()
	{
		(*this) -= 1;
		return (*this);
	}
	inline ModInt operator+() const
	{
		return (*this);
	}
	inline ModInt operator-() const
	{
		return (*this).x ? ModInt(mod - (*this).x) : ModInt(0);
	}
	template<class T>
	int modpow(const T val, int p = mod - 2)
	{
		if (p == 0) return 1;
		if (p % 2) return (long long) val * modpow(val, p-1) % mod;
		long long t = modpow(val, p/2);
		int res = t * t % mod;
		return res;
	}
	operator int() const
	{
		return x;
	}
	friend ostream& operator<<(ostream& lhs, const ModInt& rhs)
	{
		lhs << rhs.x;
		return lhs;
	}
	friend istream& operator>>(istream& lhs, ModInt& rhs)
	{
		long long t;
		lhs >> t;
		rhs.x = t % mod;
		if (rhs.x < 0) rhs += mod;
		return lhs;
	}
	friend const ModInt operator+(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) += rhs;}
	template<class T>
	friend const ModInt operator+(const ModInt& lhs, const T rhs)        {return ModInt(lhs) += rhs;}
	template<class T>
	friend const ModInt operator+(T lhs, const ModInt& rhs)              {return ModInt(lhs) += rhs;}
	friend const ModInt operator-(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) -= rhs;}
	template<class T>
	friend const ModInt operator-(const ModInt& lhs, const T rhs)        {return ModInt(lhs) -= rhs;}
	template<class T>
	friend const ModInt operator-(T lhs, const ModInt& rhs)              {return ModInt(lhs) -= rhs;}
	friend const ModInt operator*(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) *= rhs;}
	template<class T>
	friend const ModInt operator*(const ModInt& lhs, const T rhs)        {return ModInt(lhs) *= rhs;}
	template<class T>
	friend const ModInt operator*(T lhs, const ModInt& rhs)              {return ModInt(lhs) *= rhs;}
	friend const ModInt operator/(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const ModInt operator/(const ModInt& lhs, const T rhs)        {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const ModInt operator/(T lhs, const ModInt& rhs)              {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const int    operator==(T lhs, const ModInt& rhs)             {return ModInt(lhs) == rhs;}
	template<class T>
	friend const int    operator!=(T lhs, const ModInt& rhs)             {return ModInt(lhs) != rhs;}
};
using modint = ModInt<1000000007>;
//using modint = ModInt<998244353>;

modint calc()
{
	long long n, m;
	cin >> n >> m;
	if (n == 1) return modint(m+1);
	modint x = m / (n-1);
	modint M = m;
	modint N = n;
	M *= n;
	M /= N;
	N /= m;
	N *= M;
	M = -M;
	M = M * -1;
	N = N * -m;
	N = N / -M;
	N--;
	N += 1;
	++M;
	M -= 1;
	N = N++;
	M = ++M;
	M -= 1;
	N = N--;
	M = --M;
	M += 1;
	swap(N, M);
	swap(M, N);
	for (int i = 1; i < 1000000; i++) {
		M += i;
		N /= -i;
	}
	for (int i = 1; i < 1000000; i++) {
		M -= i;
		N *= -i;
	}
	for (int i = 1; i < 1000000; i++) {
		M += -i;
		N /= i;
	}
	for (int i = 1; i < 1000000; i++) {
		M -= -i;
		N *= i;
	}
	return ((M + 1) + (M - x * (N - 1) + 1)) * (x + 1) - M - 1;
}

int main()
{
	cout << calc() << endl;
}
accuracy: 1
time_ms: 360
memory_kb: 3112
final_rank: 1

code_snippet: #include <iostream>
using namespace std;

int main(){

  long long N,M;
  cin >> N >> M;
  long long answer = 0;
  long long c = 1000000000 + 7;
  //?????????1?????´???
  if(N == 1){
    answer = ((M % c) + 1) % c;
  }
  else{
    //??¬??????0?????´???
    answer = ( (M % c) + 1) % c;
    //??¬??????+d or -d?????´???(d>0)
    //+d?????´??????????????????2???????????°??????
    //??????N?????°??????????????¨???????????????(N-1)d
    //?????£??? 0???(N-1)d ??????  M-(N-1)d???M?????§
    //M+1-(N-1)d?????¨????????¨????????\???
    //M < (N-1)d????????????
    for(int d=1;;d++){
      if(M / (N-1) < d) break;
      answer += (M - (N - 1) * d) % c;
      answer ++;
      answer += (M - (N - 1) * d) % c;
      answer ++;
      answer = answer % c;
    }
  }
  
  cout << answer << endl;
}
accuracy: 0.6190476190476191
time_ms: 890
memory_kb: 3068
final_rank: 5

code_snippet: #include <iostream>
using namespace std;

int main(){

  long long N,M;
  cin >> N >> M;
  long long answer = 0;
  long long c = 1000000000 + 7;
  //?????????1?????´???
  if(N == 1){
    answer = (M + 1) % c;
  }
  else{
    //??¬??????0?????´???
    answer = (M + 1) % c;
    //??¬??????+d or -d?????´???(d>0)
    //+d?????´??????????????????2???????????°??????
    //??????N?????°??????????????¨???????????????(N-1)d
    //?????£??? 0???(N-1)d ??????  M-(N-1)d???M?????§
    //M+1-(N-1)d?????¨????????¨????????\???
    //M < (N-1)d????????????
    for(int d=1;;d++){
      if(M / (N-1) < d) break;
      answer += (M - (N - 1) * d) % c;
      answer ++;
      answer += (M - (N - 1) * d) % c;
      answer ++;
      answer = answer % c;
    }
  }
  
  cout << answer << endl;
}
accuracy: 0.6190476190476191
time_ms: 890
memory_kb: 3068
final_rank: 5

code_snippet: #include <iostream>
using namespace std;

int main(){

  long long N,M;
  cin >> N >> M;
  long long answer = 0;
  long long c = 1000000000 + 7;
  //?????????1?????´???
  if(N == 1){
    answer = (M + 1) % c;
  }
  else{
    //??¬??????0?????´???
    answer = (M + 1) % c;
    //??¬??????+d or -d?????´???(d>0)
    //+d?????´??????????????????2???????????°??????
    //??????N?????°??????????????¨???????????????(N-1)d
    //?????£??? 0???(N-1)d ??????  M-(N-1)d???M?????§
    //M+1-(N-1)d?????¨????????¨????????\???
    //M < (N-1)d????????????
    for(int d=1;;d++){
      if(M / (N-1) < d) break;
      answer += (M - (N - 1) * d) % c;
      answer ++;
      answer += (M - (N - 1) * d) % c;
      answer ++;
    }
    answer = answer % c;
  }
  
  cout << answer << endl;
}
accuracy: 0.6190476190476191
time_ms: 290
memory_kb: 3068
final_rank: 2

code_snippet: #include <iostream>
using namespace std;

int main(){

  long long N,M;
  cin >> N >> M;
  long long answer = 0;
  long long c = 1000000000 + 7;
  //?????????1?????´???
  if(N == 1){
    answer = (M + 1) % c;
  }
  else{
    //??¬??????0?????´???
    answer = (M + 1) % c;
    //??¬??????+d or -d?????´???(d>0)
    //+d?????´??????????????????2???????????°??????
    //??????N?????°??????????????¨???????????????(N-1)d
    //?????£??? 0???(N-1)d ??????  M-(N-1)d???M?????§
    //M+1-(N-1)d?????¨????????¨????????\???
    //M < (N-1)d????????????
    for(int d=1;;d++){
      if(M / (N-1) < d) break;
      answer += (2 * (M ) + 2 - ((N ) - 1) * 2 * d) % c;
    }
    answer = answer % c;
  }
  
  cout << answer << endl;
}
accuracy: 0.6190476190476191
time_ms: 290
memory_kb: 3068
final_rank: 2

code_snippet: #include <iostream>
using namespace std;

int main(){

  long long N,M;
  cin >> N >> M;
  long long answer = 0;

  //?????????1?????´???
  if(N == 1){
    answer = (M + 1) % (1000000000 + 7);
  }
  else{
    //??¬??????0?????´???
    answer = (M + 1) % (1000000000 + 7);
    //??¬??????+d or -d?????´???(d>0)
    //+d?????´??????????????????2???????????°??????
    //??????N?????°??????????????¨???????????????(N-1)d
    //?????£??? 0???(N-1)d ??????  M-(N-1)d???M?????§
    //M+1-(N-1)d?????¨????????¨????????\???
    //M < (N-1)d????????????
    for(int d=1;;d++){
      if(M / (N-1) < d) break;
      answer += (2 * M + 2 - (N - 1) * 2 * d) % (1000000000 + 7);
    }
    answer = answer % (1000000000+7);
  }
  
  cout << answer << endl;
}
accuracy: 0.6190476190476191
time_ms: 290
memory_kb: 3068
final_rank: 2