akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007

int main(){
    int n,m,a,b;
    bool ok = true;
    cin >> n >> m;
    vector<vector<int>> g(n);

    for(int i = 0;i < m;i++){
        cin >> a >> b;
        g[a - 1].push_back(b - 1);
        g[b - 1].push_back(a - 1);
    }

    vector<int> dist(n, -1);
    vector<int> prev(n, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (int nv : g[v]) {
            if (dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            prev[nv] = v;
            que.push(nv);
        }
    }
    for (int v = 0; v < n; ++v){
        if(dist[v] == -1){
          ok = !ok;
          break;
        }
    }
    cout << (ok ? "Yes" : "No") << endl;
    if(ok){
        for (int v = 1; v < n; ++v) cout << prev[v] + 1 << endl;
    }
}


Measured memory_bytes: 10724
#include <bits/stdc++.h>
using namespace std;

int main(){
	int N, ans = 1;
	cin >> N;
	int H[N];
	for(int i = 0; i < N; i++)
		cin >> H[i];
	for(int i = 1; i < N; i++){
		int b = 1;
		for(int j = 0; j < i; j++){
			if(H[i] < H[j])
				b *= 0;
		}
		if(b)
			ans++;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for ( int i = 0; i < (int)(n); i++ )

int main( void )
{
    string s;
    cin >> s;
    int ramenPrice = 700;
    rep(i,3)
        if ( s[i] == 'o' )
            ramenPrice += 100;
    
    cout << ramenPrice << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N,Q;
  int64_t Z=0;
  cin>>N;
  vector<int64_t> p(100001);
  for(int i=0;i<N;i++){
    int64_t a;
    cin>>a;
    Z+=a;
    p.at(a)++;
  }
  cin>>Q;
  for(int i=0;i<Q;i++){
    int64_t b,c,d;
    cin>>b>>c;
    d=p.at(b);
    Z+=(c-b)*d;
    p.at(b)=0;
    p.at(c)+=d;
    cout<<Z<<endl;
  }
 
}

Measured memory_bytes: 4044
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

typedef pair<string, int> P;
map<string, int> m;

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

	queue<P> q;
	q.push(P("01234567", 0));
	m["01234567"] = 0;
	while(!q.empty()) {
		P p = q.front();
		q.pop();
		if(m[p.first] != p.second) continue;
		string s = p.first;
		int d[] = { 1, -1, 4, -4 };
		for(int i = 0; i < 8; i++) {
			if(s[i] != '0') continue;
			for(int j = 0; j < 4; j++) {
				if(i + d[j] < 0 || 8 <= i + d[j]) continue;
				if(i == 3 && d[j] == 1 || i == 4 && d[j] == -1) continue;

				swap(s[i], s[i + d[j]]);

				if(m.find(s) == m.end() || p.second + 1 < m[s]) {
					m[s] = p.second + 1;
					q.push(P(s, p.second + 1));
				}

				swap(s[i], s[i + d[j]]);
			}
		}
	}

	string s;
	while(getline(cin, s)) {
		string ss;
		for(int i = 0; i < 8; i++) {
			ss += s[2 * i];
		}
		cout << m[ss] << endl;
	}
}

Measured memory_bytes: 5244
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,INF=0x3f3f3f3f;
int n,m,a[N],fa[N],vis[N],mi[N];
typedef pair<int,int> pii;
priority_queue<pii, vector<pii >, greater<pii > >Q;
int find(int x)
{
    return x==fa[x]?x:(fa[x]=find(fa[x]));
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0; i<n; i++)
        cin>>a[i],fa[i]=i,mi[i]=INF;
    for(int i=0,u,v; i<m; i++)
    {
        cin>>u>>v,u=find(u),v=find(v);
        if(u!=v)fa[u]=v;
    }
    for(int i=0; i<n; i++)find(i);
    for(int i=0; i<n; i++)
        mi[fa[i]]=min(a[i],mi[fa[i]]),Q.push(make_pair(a[i],fa[i]));
    sort(fa,fa+n);
    int tn=unique(fa,fa+n)-fa;
    if(tn==1)
        cout<<0;
    else if((tn-1)*2>n)
        cout<<"Impossible";
    else
    {
        long long s=0;
        for(int i=0; i<tn; i++)
            s+=mi[fa[i]];
        while(!Q.empty())
        {
            if(tn==2)break;
            pii t=Q.top();
            Q.pop();
            if(!vis[t.second])
            {
                vis[t.second]=1;
                continue;
            }
            s+=t.first,--tn;
        }
        cout<<s;
    }
    return 0;
}

Measured memory_bytes: 2804
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int a[maxn];
int n;
const int inf=0x3f3f3f3f;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	ll cnt=0;
	for(int l=1,r=0,now=0;l<=n;l++)
	{
		while(r<n)
		{
			if(now&a[r+1]) break;
			r++;
			now|=a[r];
			//cnt++;
		}
		cnt+=r-l+1;
		now^=a[l];
		//if(r>n) continue;
	}
	printf("%lld\n",cnt);
	return 0;
}


Measured memory_bytes: 1024
from collections import Counter

n = int(input())
m=list(map(int,input().split()))
d=Counter(m)
k=0
for c in d:
    if d[c]>=2:
        k+=d[c]*(d[c]-1)//2
for i in range(n):
    l=d[m[i]]
    if l>=2:
        z=k
        z+=(l-1)*(l-2)//2-l*(l-1)//2
        print(z)
    else:
        print(k)
    

Measured memory_bytes: 110088
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

string STR;
bool N, S, E, W;


int main() {
	cin >> STR;
	
	for(char c : STR) {
		if(c == 'N')
			N = true;
		if(c == 'E')
			E = true;
		if(c == 'W')
			W = true;
		if(c == 'S')
			S = true;
	}
	
	if((N + S) % 2 || (E + W) % 2) {
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}
	
	
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>

using namespace std;

bool prNum(int n){
	if(n<2)return false;
	for(int i=2; i<=sqrt(n); i++){
		if(n%i==0)return false;
	}
	return true;
}

int main(){
	int n;
	while(cin>>n){
		//cout<<n<<endl;
		for(int i=n-1; i>1; i--){
			if(prNum(i)){
				cout<<i<<" ";
				break;
			}
		}
		for(int i=n+1; ; i++){
			if(prNum(i)){
				cout<<i<<endl;
				break;
			}
		}
		if(cin.eof())break;
	}

}

Measured memory_bytes: 1160
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <math.h>
#include <set>
using namespace std;
#define rep(i,n) for(int i = 0; i < n ; i++)
#define pb push_back
const static int INF = 100000;


int string_to_int(std::string str){
  int ret;
  std::stringstream ss;
  ss << str;
  ss >> ret;
  return ret;
}

//文字列を任意の文字で分割できる
//split("文字列","分解したい文字",入れる変数)
template <typename List>
void split(const std::string& s, const std::string& delim, List& result)
{
    result.clear();

    using string = std::string;
    string::size_type pos = 0;

    while(pos != string::npos )
    {
        string::size_type p = s.find(delim, pos);

        if(p == string::npos)
        {
            result.push_back(s.substr(pos));
            break;
        }
        else {
            result.push_back(s.substr(pos, p - pos));
        }

        pos = p + delim.size();
    }
}


int main() {
  string s; cin >> s;
  vector<string> after;
  split(s,",",after);

  cout << after[0] << " " << after[1] << " " << after[2] << endl;

  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
double x[100000], maxn;int  cnt;
int main() {
	while (cin >> x[cnt]) {
		cnt++;
	}
	for (int i = 0; i < cnt; i++) {
		for (int j = i; j < cnt; j++) {
			maxn = max(maxn, abs(x[i] - x[j]));
		}
	}
	cout << maxn << endl;
}

Measured memory_bytes: 1280
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

n = int(input())
if n == 1:
    print(0)
else:
    l = factorization(n)
    dic = []
    for i in range(len(l)):
        tmp = l[i][0]
        dic.append(tmp)
        while n >= tmp:
            tmp *= l[i][0]
            if n < tmp:
                break
            dic.append(tmp)
    dic.sort()
    cnt = 0
    while n != 1:
        flag = True
        for i in range(len(dic)):
            if n%dic[i] == 0:
                n //= dic[i]
                cnt += 1
                dic.pop(i)
                flag = False
                break
        if flag:
            break
    print(cnt)

Measured memory_bytes: 63464
import Control.Monad (fmap, replicateM)
import Data.Array (listArray, elems, (//), (!))
import Data.List (foldl')

main = input >>= output . solve

input :: IO (Int, [(Int, Int)])
input = do
    w <- readLn
    n <- readLn
    ps <- map toPair `fmap` replicateM n getLine
    return (w,ps)
    where
    toPair s = (read a, read b)
        where
        (a,(_:b)) = break (==',') s

output :: [Int] -> IO ()
output ns = mapM_ print ns

solve :: (Int, [(Int, Int)]) -> [Int]
solve (w,ps) = elems $ foldl' applySwap (listArray (1,w) [1..]) ps
    where
    applySwap ns (a,b) = ns // [(a,ns!b), (b,ns!a)]

Measured memory_bytes: 4112
#include <bits/stdc++.h>

using namespace std;

#define lint long long
#define P pair<int, int>

int main(){
    int R, G, B, N;

    cin >> R >> G >> B >> N;


    map<lint, int> lis;
    for(int i = 0; i <= N / G + 1; ++i){
        for(int j = 0; j <= N / B + 1; ++j){
            lis[i * G + j * B]++;
        }
    }

    lint ans = 0;

    for(int i = 0; i <= N / R + 1; ++i){
        int tmp = N - i * R;

        ans += lis[tmp];
    }

    cout << ans << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;

#define wait() system("pause")
#define int long long

const int maxn = 303;

int mat[maxn][maxn];
int n;

bool isshit() {
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			for(int k=1; k<=n; k++) {
				if(mat[i][k] + mat[k][j] < mat[i][j]) return true;
			}
		}
	} return false;
}

int32_t main () {
	cin>>n;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			cin>>mat[i][j];
		}
	}
	if(isshit()) {
		puts("-1"); return 0;
	}
	int ans = 0;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			bool flag = true; for(int k=1; k<=n; k++) {
				if(i==j or i==k or j==k) continue;
				if(mat[i][k] + mat[k][j] == mat[i][j]) flag = false;
			} if(flag == true) ans += mat[i][j];
		}
	}
	cout<<(int)(ans>>1)<<endl;
	return 0;
}

Measured memory_bytes: 896
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[False]*N
k=0
while 1:
	newlight=A[k]
	if k==1:
		ans=0
		break
	elif result[k]:
		ans=1
		break
	else:
		result[k]=True
		k=newlight
if ans:
	print(-1)
else:print(sum(result))

Measured memory_bytes: 7852
#include<bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for(int (i) = (n); (i) < (m); (i)++)
#define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x) int x; cin >> (x)
#define ii2(x, y) int x, y; cin >> (x) >> (y)
#define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x)?("Yes"):("No")) << endl
#define YN(x) cout << ((x)?("YES"):("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)

inline void logger(){ std::cout << " [LOGGER] " << endl; }
template<typename A, typename... B>
void logger(const A& a, const B&... b){
    cout << a << " , "; logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 200005;

/* ...o(^-^)o... */

int main(){
    int n, a; cin >> n >> a;
    if(n % 500 <= a) out("Yes"); else out("No");
}

Measured memory_bytes: 3588
#include <bits/stdc++.h> using namespace std;
int main (){
	int A; int B; int K;
	scanf("%d%d%d", &A, &B, &K);
	for (int i = K; i>=1; i--)
    {
        if (K>B){
            break;
        }
        if (K>B-A){
            break;
        }
        printf("%d\n", A);
        A++;
        if (A>=B-K+1)
        {
            break;
        }
    }
    for (int i = 1;i<=K;i++)
    {
        if (K>B || K>B-A)
        {
            for (int j =A; j<=B; j++)
            {
                 printf("%d\n", j);
            }
            break;
        }
        else
        {
                 printf("%d\n", B-K+i);
            }

    }
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

map <ll, ll> dp;
ll n, a, b, c, d;

ll check(ll m){
	if(dp.find(m) != dp.end())
		return dp[m];
	ll aa,bb,cc,dd = LLONG_MAX;
	if(m%2 == 0){
		aa = a + check(m/2);
	}else{
		aa = min(a + d + check(m/2), a + d + check((m+1)/2));
	}
	if(m%3 == 2){
		bb = b + d + check((m+1)/3);
	}else{
		bb = b + d*(m%3) + check(m/3);
	}
	if((m%5 == 4) || (m%5 == 3)){
		cc = c + d*(5 - (m%5)) + check((m+4)/5);
	}else{
		cc = c + d*(m%5) + check(m/5);
	}
	if(m < LLONG_MAX/d){
		dd = m*d;
	}
	dp[m] = min({aa,bb,cc,dd});	
	return dp[m];
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	int t; cin >> t;
	while(t--){
		cin >> n;
		cin >> a >> b >> c >> d;
		dp.clear();
		dp[0] = 0;
		dp[1] = d;
		cout << check(n) << '\n';
	}
	return 0;
}



Measured memory_bytes: 4404
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;
    int a=0, b;
    cin>>s;
    for (int i=0; i<s.size(); i++) a+=(s[i]-'0');
    b=stoi(s);
    if (b%a==0) cout<<"Yes\n";
    else cout<<"No\n";
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        sc.close();

        if (k % n == 0 || k % m == 0) {
            System.out.println("Yes");
        } else {
            int judge = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    int temp = i * m - j * i + j * (n - i);
                    if (temp == k) {
                        judge = 1;
                    }
                }
            }
            if (judge == 1) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}


Measured memory_bytes: 23764
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1<<29);

int main() {

    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    
    REP(i, n) {
        if (a[i] % 2 == 0) {
            if (a[i] % 3 != 0 && a[i] % 5 != 0) {
                cout << "DENIED" << endl;
                return 0;
            }
        }
    }

    cout << "APPROVED" << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define INF 9223372036854775807LL
#define inf 1000000007
#define SYOU(x) setprecision(x+1)
#define abs(x,y) (max(x,y)-min(x,y))
#define lol long long
#define mp make_pair
#define fi first
#define se second
using namespace std;
lol i[10000];
lol p[1000];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a,b,c,d,e=0,f=0;
  string s,w;
  int ans=0;
  cin >>a>>b>>c>>d;
  e=c-a,f=d-b;
  cout <<c-f<<' '<<d+e<<' ';
  cout <<c-f-c+a<<' '<<d+e-d+b<<'\n';
  return (0);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define __ <<' '<<
#define ___ <<' '
#define bash push_back
#define ALL(x) x.begin(),x.end()
//#define int long long

struct IoSetup {IoSetup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout <<fixed<<setprecision(12);
    cerr <<fixed<<setprecision(12);
}}IoSetup;

using ll = long long;
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int SMOD = 1000000007;
constexpr int NMOD = 998244353;
constexpr int dx[]={1,0,-1,0,1,1,-1,-1};
constexpr int dy[]={0,1,0,-1,-1,1,-1,1};

inline bool inside(int x,int y,int w,int h){return (x>=0 && y>=0 && x<w && y<h);}
template<class T>bool chmax(T &a, const T &b){return(a<b?a=b,true:false);}
template<class T>bool chmin(T &a, const T &b){return(a>b?a=b,true:false);}
inline int qp(int a,ll b,int mo){ll ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}



signed main() {

    int a, b, c, d, e;

    cin >> a >> b >> c >> d >> e;

    cout << c*60 + d - a*60 - b - e << endl;


  
    return 0;
}



Measured memory_bytes: 3616
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#include <deque>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;

#define INF 1e18
#define int long long

signed main() {
    int h, w, n; cin >> h >> w >> n;
    vector<int> a(n);
    queue<int> q;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        for(int j = 0; j < a[i]; j++) {
            q.push(i + 1);
        }
    }

    vector<vector<int>> ans(h, vector<int> (w, 0));
    for(int i = 0; i < h; i++) {
        if(i % 2 == 0) {
            for(int j = 0; j < w; j++) {
                ans[i][j] = q.front();
                q.pop();
            }
        }
        else {
            for(int j = w - 1; j >= 0; j--) {
                ans[i][j] = q.front();
                q.pop();
            }
        }
    }

    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;

int main(){
	string s; cin>>s;
	vector<char> ns={'P'};
	vector<char> ew={'P'};
	int k=s.size();
	rep(i,k){
		if(s[i]=='N') ns.push_back('N');
		if(s[i]=='S') ns.push_back('S');
		if(s[i]=='E') ew.push_back('E');
		if(s[i]=='W') ew.push_back('W');
	}
  
	if(ns.size()==2||ew.size()==2) {cout<<"No"<<endl; return 0;}
	bool bns=false, bew=false;
	if(ns[ns.size()-2]!=ns[ns.size()-1]) bns=true;
	if(ew[ew.size()-2]!=ew[ew.size()-1]) bew=true;
	if(bns&&bew) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main()
{
	long long x;
    cin >> x;
    
    long long step =0;
    long long p =100;
    while(p<x)
    {
		p += p/100;
        step++;
    }
    cout << step << endl;
	return 0;
}

Measured memory_bytes: 3596
n = int(input())
d = dict()
d[0] = [0,0,0]
can = True
for i in range(n):
    l = list(map(int, input().split()))
    d[i+1] = l
# print(d[2])
for j in range(n):
    # print(d[j][2])
    t = d[j+1][0]-d[j][0]
    x = d[j+1][1]-d[j][1]
    y = d[j+1][2]-d[j][2]
    # print(t,x,y)
    c = t - (abs(x) + abs(y))
    # print(c)
    if c < 0 or c%2 == 1:
        can = False
        break
        
if can:
    print("Yes")
else:
    print("No")
    

Measured memory_bytes: 64344
import itertools

n,m = map(int,input().split())

n_p = 1
m_p = 1

for i in range(1,n+1):
    n_p *= i
    n_p %= (10**9+7)

for i in range(1,m+1):
    m_p *= i
    m_p %= (10**9+7)


if abs(n-m) >= 2:
    print(0)

elif abs(n-m) == 1:
    print((n_p*m_p)%(10**9+7))

else:
    print((2*n_p*m_p)%(10**9+7))
    

Measured memory_bytes: 3060
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	static final boolean debugMode = false;

	static final long DIV_INF = 998244353l;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int k = readInt(sc);
		String s= sc.next();
		
		if(s.length() <= k)
			System.out.println(s);
		else
			System.out.println(s.substring(0, k) + "...");

	}

	static long div(long x) {
		return x % DIV_INF;
	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static Long readLong(Scanner sc) {
		return Long.parseLong(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
}

class KrmlUtil {
	// a >= b
	static int gcd(int a, int b) {
		while (true) {
			if (b == 0)
				break;

			int mod = a % b;
			a = b;
			b = mod;
		}
		return a;
	}

	// a >= b
	static int lcm(int a, int b) {
		return a * b / gcd(a, b);
	}

	// a >= b
	static long gcd(long a, long b) {
		while (true) {
			if (b == 0)
				break;

			long mod = a % b;
			a = b;
			b = mod;
		}
		return a;
	}

	// a >= b
	static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	static BigInteger gcd(BigInteger a, BigInteger b) {
		return a.gcd(b);
	}

	static BigInteger lcm(BigInteger a, BigInteger b) {
		return a.divide(a.gcd(b)).multiply(b);
	}
}

class Pair {
	private int x;
	private int y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Pair() {

	}

	public Pair(int x, int y) {
		this.x = x;
		this.y = y;
	}

}



Measured memory_bytes: 38532
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rep1(i,n) for(int i=1;i<=(int)(n);++i)
#define rep11(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define repm(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int a[3][3],n,b;
bool c[3][3];

signed main() {
    rep(i,3) rep(j,3) cin >> a[i][j];
    cin >> n;
    rep(i,n) {
        cin >> b;
        rep(j,3) rep(k,3) if (a[j][k] == b) c[j][k] = true;
    }
    rep(i,3) {
        bool tmp = true;
        rep(j,3) tmp &= c[i][j];
        if (tmp) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    rep(i,3) {
        bool tmp = true;
        rep(j,3) tmp &= c[j][i];
        if (tmp) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    bool tmp = true;
    rep(i,3) tmp &= c[i][i];
    if (tmp) {
        cout << "Yes" << endl;
        return 0;
    }
    tmp = true;
    rep(i,3) tmp &= c[i][2-i];
    if (tmp) {
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD =   1000000007;

bool is_square(int n){
    int tmp = sqrt(n);
    if(tmp * tmp == n){
        return true;
    }
    else{
        return false;
    }
}

int main()
{
#ifdef MYLOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
    int n, d; cin >> n >> d;
    vector< vector<int> > x(n);
    for(int i = 0; i < n; ++i){
        vector<int> tmp(d);
        for(int j = 0; j < d; ++j){
            cin >> tmp[j];
        }
        x[i] = tmp;
    }

    int ans = 0;
    for(int i = 0; i < n; ++i){
        for(int j = i + 1; j < n; ++j){
            int tmp = 0;
            for(int k = 0; k < d; ++k){
                tmp += ( x[i][k] - x[j][k] ) * ( x[i][k] - x[j][k] );
            }
            if(is_square(tmp)){
                ++ans;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>

int maxhap(int X, int num){
  if(X<num){
    return 0;
  }else{
  int i=1;
  while(num*i<=X){
    i++;
  }
  return i-1;
 }
}


int main(){
  int X;
  std::cin >> X;

  int a=maxhap(X, 500);

  int rest=X - 500 * a;

  int b=maxhap(rest, 5);

  std::cout << 1000*a+5*b << "\n";

  return 0;
}

Measured memory_bytes: 256
n=gets.to_i
number=[*1..100010]
all_list=Array.new(100010,1)
#エラトステネスのふるい
all_list[0]=0
all_list[1]=0
for i in 0..all_list.length**0.5-1
   if all_list[i]==0
       next
    end
   
   for j in i*2..all_list.length-1
       if j%i==0
           all_list[j]=0
        end
    end
   
end

#p all_list
check=all_list.length-1
while check>=2
    if all_list[check]==0
        check-=1
        next
    end
    
    v=(check+1).div(2)
    if all_list[v]==1
        check-=1
        next
    else
       all_list[check]=0
       check-=1
    end
       
 
end

#累積和
s=[]
s[0]=0
for  i in 0..all_list.length-1
    
s[i+1]=s[i]+all_list[i]
end

#p s[0..100]
n.times.each do
    m=gets.split.map(&:to_i)
   v=(s[m[1]+1]-s[m[0]])
   p v
end

Measured memory_bytes: 18172
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
#define INF __INT32_MAX__
#define LINF __LONG_LONG_MAX__

int getcurr(int prev, int next) {
    int ans = -1;
    rep(i, 3) {
        if (i != prev && i != next) {
            ans = i;
            break;
        }
    }
    return ans;
}

int main() {
    int N, K; cin >> N >> K;
    vi score(3); rep(i, 3) cin >> score[i];
    string T; cin >> T;
    vi ct(T.size());
    rep(i, T.size()) {
        switch (T.at(i)) {
            case 'r':
                ct[i] = 0;
                break;
            case 's':
                ct[i] = 1;
                break;
            case 'p':
                ct[i] = 2;
                break;
        }
    }

    ll ans = 0;
    // Tの添字はK未満でループする
    for (int a = 0; a < K; a++) {
        int prev = -1;
        for (int b = a; b < N; b += K) {

            if (ct[b] == prev) {
                if (b + K < N) {
                    prev = getcurr(ct[b], ct[b + K]);
                }
            } else {
                prev = ct[b];
                int j = (ct[b] + 2) % 3;
                ans += score[j];
            }
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 3844
#include <bits/stdc++.h>
using namespace std;

#define times(n, i)      uptil(0, n, i)
#define rtimes(n, i)     downto((n) - 1, 0, i)
#define upto(f, t, i)    for(int i##_to_ = (t), i = (f); i <= i##_to_; i++)
#define uptil(f, t, i)   for(int i##_to_ = (t), i = (f); i <  i##_to_; i++)
#define downto(f, t, i)  for(int i##_to_ = (t), i = (f); i >= i##_to_; i--)
#define downtil(f, t, i) for(int i##_to_ = (t), i = (f); i >  i##_to_; i--)
typedef long double LD;
#define long long long

#if defined(EBUG) && !defined(ONLINE_JUDGE)
#define debug true
#define ln << endl
#else
#define debug false
#define ln << '\n'
#endif
#define tb << '\t'
#define sp << ' '



signed main() { // long: 64bit
    if(!debug) {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }

    string s; cin >> s;

    cout << s[0] << (s.size() - 2) << s.back();

    return 0;
}


Measured memory_bytes: 256
// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define sz(a)         int((a).size())
#define rep(i,n)      for(int(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(int(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>
#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

const int INF = 1e9;

// code
// ------------------------------------------------
int main() {

  string s;
  cin >> s;

  int n = stoi(s);

  int sum = 0;
  rep(i,sz(s))
  {
    string ss = s.substr(i,1);
    sum += stoi(ss);
  }

  if(n % sum == 0) cout << "Yes" << endl;
  else cout << "No" << endl;



  return 0;
}
// funcの実体
// ------------------------------------------------
int CalcSumOfDigit(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(97 <= *itr && *itr <= 122)
    {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(65 <= *itr && *itr <= 90)
    {
      *itr = *itr + 32;
    }
  }
  return str;
}


Measured memory_bytes: 256
N = int(input())
A = [int(x) for x in input().split()]

def main():
    A.sort()
    max_num = A[len(A) - 1]
    mid_num = max_num / 2

    curr = A.pop(0)
    d = abs(mid_num - curr)
    for a in A:
        next_d = abs(mid_num - a)
        if d <= next_d:
            break
        curr = a
        d = next_d
    print(max_num, curr)

if __name__ == "__main__":
    main()


Measured memory_bytes: 14060
/*
 * Author       : YangDavid
 * Created Time : 2019年09月27日 星期五 14时16分24秒
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 102, MOD = 1000000007;
int add(int x, int y) { return x+y >= MOD ? x+y-MOD : x+y; }
int mul(int x, int y) { return 1LL * x * y % MOD; }
void upd(int &x, int y) { x += y; if(x >= MOD) x -= MOD; }

int dp[maxn][maxn], n, C[maxn][maxn];

int main() {
    scanf("%d", &n);
    C[0][0] = 1;
    for(int i = 1; i < 70; ++i) {
        C[i][0] = 1;
        for(int j = 1; j <= i; ++j)
            C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
    }
    dp[1][0] = dp[1][1] = 1;
    for(int i = 1; i < n; ++i) {
        for(int j = 0; j <= i; ++j) {
            for(int x = 0; x+i <= n; ++x) { // x +, y -
                // expect (j + (x+y-j)/2) pos, change |j+(x+y-j)/2 - x|
                for(int y = 0; x+y+i <= n; ++y) if(x+y>0 && (x+y-j)%2==0 && (x+y-j>=0)) {
                    upd(dp[i+x+y][abs(j+(x+y-j)/2-x)], mul(dp[i][j], mul(C[i+x+y][x+y], C[x+y][x])));
                }
            }
        }
    }
    printf("%d\n", dp[n][0]);

    return 0;
}


Measured memory_bytes: 256
def frog2():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    inf = 10**9 + 7
    dp = [inf]*n
    dp[0] = 0
    for i, height in enumerate(h):
        if i == 0:
            continue
        else:
            choices = max(i - k, 0)
            dp[i] = min([dpt + abs(height - ht) for dpt, ht in zip(dp[choices:i], h[choices:i])])

    print(dp[-1])


if __name__ == '__main__':
    frog2()


Measured memory_bytes: 13980
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll inf =1LL << 62;

char land[12][12];

void dfs(int x,int y){
	int dx[4]={1,0,-1,0};
	int dy[4]={0,1,0,-1};
	land[x][y]='0';
	rep(i,4){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(0<=nx&&nx<12&&0<=ny&&ny<12&&land[nx][ny]=='1')
			dfs(nx,ny);
	}
}

int main(void){
	int tmp;
	while(true){
		int ans=0;
		rep(i,12){ 
			if(scanf("%s",&land[i])==-1)
				return 0;
		}
		rep(i,12)rep(j,12){
			if(land[i][j]=='1'){
				ans++;
				dfs(i,j);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

Measured memory_bytes: 1144
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 55
#define LL long long
LL s[N][N];
char a[N],b[N],c[N];
int main()
{
    int i,j,k1,k2,n,t;
    LL ans;
    scanf("%d%s",&n,a);
    ans=0;
    for(t=0;t<(1<<n);t++)
    {
        k1=0;
        k2=0;
        for(j=0;j<n;j++)
        {
            if(t&1<<j)
                b[k1++]=a[j];
            else
                c[k2++]=a[j];
        }
        b[k1]='\0';
        c[k2]='\0';
        memset(s,0,sizeof(s));
        s[0][0]=1;
        for(i=0;i<=k1;i++)
        for(j=0;j<=k2;j++)
        {
            if(a[n*2-1-i-j]==b[i])
                s[i+1][j]+=s[i][j];
            if(a[n*2-1-i-j]==c[j])
                s[i][j+1]+=s[i][j];
        }
        ans=ans+s[k1][k2];
    }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 128
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
    	Scanner sc = new Scanner(System.in);
    	int N = sc.nextInt();
    	int cnt = 0;
    	for(int i=3; i>=0; i--) {
    		int answer = (int) (N/(Math.pow(10, i)));
    		N %= Math.pow(10, i);
    		if(answer == 2) {
    			cnt++;
    		}
    	}
	    System.out.println(cnt);
    }
}

Measured memory_bytes: 23636
#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define forv(v,a) for(auto &v:a)
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define all(a) a.begin(),a.end()
int re(){int x=0; char c; bool neg=false;while(c!='-'&&(c<'0'||'9'<c)) c=getchar();if (c=='-'){neg=true; c=getchar();}while ('0'<=c && c<='9'){x=x*10+c-'0';c=getchar();}if (neg) x=-x;return x;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ran(int l, int r) {return l+rng()%(r-l+1);}
typedef long long ll;
const int N=100010;
int n;
int a[N],f[N];
bool dd[N];
queue<int> q;
void bfs()
{
    while (!q.empty())
    {
        int u=q.front(); q.pop();
        forinc(i,1,2) if (u+i<=n && f[u+i]>f[u]+abs(a[u]-a[u+i]))
        {
            f[u+i]=f[u]+abs(a[u]-a[u+i]);
            if (!dd[u+i]) dd[u+i]=true,q.push(u+i);
        }
    }
}
int main()
{
    //freopen("frog1.inp","r",stdin);
    cin>>n;
    forinc(i,1,n) cin>>a[i];
    memset(f,127,sizeof(f));
    f[1]=0; q.push(1); dd[1]=false;
    bfs();
    cout<<f[n];
}


Measured memory_bytes: 1152
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, K = LI()
A = LI()

if K > 50:
    print(" ".join([str(N) for i in range(N)]))
    sys.exit()

for i in range(K):
    A_new = [0 for i in range(N)]
    imos_table = [0 for i in range(N)]

    for j in range(N):
        left = max(0, j - A[j])
        right = j + A[j] + 1
        imos_table[left] += 1
        if right >= N:
            pass
        else:
            imos_table[right] -= 1
    
    tmp = 0
    for j in range(N):
        tmp += imos_table[j]
        A_new[j] += tmp

    A = copy.deepcopy(A_new)

print(" ".join(map(str, A)))
    

Measured memory_bytes: 185940
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
int main(){
int n, a, b;
    cin >> n;
    if(n%2){
      cout << "Hello World" << endl;
    }else{
      cin >> a >> b;
      cout << a+b << endl;
    }

}


Measured memory_bytes: 256
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define pb push_back
#define __V vector
#define all(x) x.begin(), x.end()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
}
template<typename X, typename Y>
istream& operator>>(istream& in, pair<X, Y> &a) {
	in >> a.first >> a.second;
	return in;
}
template<typename T>
void getv(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv(vector<T>& v, int n, Ns ... ns) {
	v.resize(n);
	for (auto& i : v)
		getv(i, ns...);
}
template<typename T>
void getv1(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv1(vector<T>& v, int n, Ns ... ns) {
	v.resize(n + 1);
	for (int i = 1; i <= n; i++)
		getv(v[i], ns...);
}
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
inline void getch(char &x) {
	while (x = getchar_unlocked(), x < 33) {
		;
	}
}
inline void getstr(string &str) {
	str.clear();
	char cur;
	while (cur = getchar_unlocked(), cur < 33) {
		;
	}
	while (cur > 32) {
		str += cur;
		cur = getchar_unlocked();
	}
}
template<typename T> inline bool sc(T &num) {
	bool neg = 0;
	int c;
	num = 0;
	while (c = getchar_unlocked(), c < 33) {
		if (c == EOF)
			return false;
	}
	if (c == '-') {
		neg = 1;
		c = getchar_unlocked();
	}
	for (; c > 47; c = getchar_unlocked())
		num = num * 10 + c - 48;
	if (neg)
		num *= -1;
	return true;
}
typedef unsigned long long ull;
typedef long long ll;
typedef float ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
	doin();
	ll a, b, t;
	cin >> a >> b;
	cout << max({a+a-1, a+b, b+b-1});
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long   // aaaaaaaaaaaaaaaaaa
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define in(s,x) s.find(x) != s.end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff)/sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<pair<int,int>> VPII;
const ll mod=1000000007;
const ll INF= 1LL<<60-1;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
template<typename T, typename U>ostream& operator << (ostream& os, pair<T, U>& pair_var)
{os << "(" << pair_var.first << ", " << pair_var.second << ")";return os;}

// head


int m,n,k,res,tmp,tmp2,tmp3,h,x,y;
VI a;
deque<PII> que;
string s;
//VI b;
vector<PII> g[2100600];
int b[2100000];
int c[210000] = {};
int rui[210000];
set<int> ch;

signed main() {
	cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>s>>n;
  int flag;
  flag = 1;
  rep(i,1,s.length()){
    if(s[i]!= s[i-1]) flag = 0;
  }
  if(flag) {cout<<s.length()*n/2<<endl; exit(0);}
  tmp=0;
  if(s[0] != s[s.length()-1]){
  rep(i,0,s.length()-1){
    if(s[i]==s[i+1] ){
      tmp += 1;
      i+=1;
    }
  }
  tmp*= n;
  }
  else{
  tmp2=1;
  tmp3 =1;
  rep(i,0,s.length()){
    if(s[i]!= s[i+1]) break;
    tmp2+=1;
    x=i+1;
  }
  per(i,0,s.length()-1){
    if(s[i]!= s[i-1]) break;
    tmp3+=1;
    y=i-1;
  }
  
  rep(i,x+1,y){
    if(s[i]==s[i+1] ){
      tmp += 1;
      i+=1;
    }
  }
  tmp*=n;
  tmp+= tmp2/2+tmp3/2+(tmp2+tmp3)/2*(n-1);
  }
  cout << tmp << endl;
}


Measured memory_bytes: 50816
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <math.h>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <functional>

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i,n,m) for(ll i=(ll)(m);i<(ll)(n);++i)
#define REP(i,n) FOR(i,n,0)
#define IREP(i,n) for(ll i=(ll)(n);i>=0;--i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

int main()
{
	string W;
	cin >> W;
	int a = 0;
	while (true)
	{
		string s;
		cin >> s;
		if (s == "END_OF_TEXT")break;
		REP(i, s.length())
		{
			s[i] = tolower(s[i]);
		}
		if (s == W)a++;
	}
	cout << a << endl;
	return 0;
}


Measured memory_bytes: 3208
character(10**5) S
integer,allocatable,dimension(:)::A
integer rcnt,lcnt
read*,S
allocate( A(len_trim(S)) )
A=0

rcnt=0
do i=1,len_trim(S)
  if(S(i:i)=="R")then
    rcnt=rcnt+1
  else
    if(S(i-1:i-1)=="R")then
      A(i-1)=A(i-1)+(rcnt+1)/2
      A(i)=A(i)+rcnt/2
    endif
    rcnt=0
  endif
end do
lcnt=0
do i=len_trim(S),1,-1
  if(S(i:i)=="L")then
    lcnt=lcnt+1
  else
    if(S(i+1:i+1)=="L")then
      A(i+1)=A(i+1)+(lcnt+1)/2
      A(i)=A(i)+lcnt/2
    endif
    lcnt=0
  endif
end do
write(*,'(i0)', advance='no') A(1)
do i=2,len_trim(S)
  write(*,'(x,i0)', advance='no')A(i)
end do
  write(*,*)
end

Measured memory_bytes: 1188
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    long long k;    
    cin>>n>>k;
    int ans = 0;
    vector<int> tel(n+1);
    for(int i=1; i<=n; i++){
        cin>>tel[i];
    }
    map<int,int> visit;
    vector<int> loop(0);
    int i=1;
    int loop_begin;
    while(true){
        if(visit.count(i)){
            loop_begin = i;
            break;
        }
        visit[i]=1;
        loop.push_back(i);
        i=tel[i];
    }
    int to_loop=0;
    while(true){
        if(loop[to_loop]==loop_begin) break;
        else to_loop++;
    }

    long long mod = loop.size()-to_loop;
    if(k>=to_loop){
        k = (k-to_loop)%mod;
        cout << loop[to_loop+k] << endl;
    }
    else cout << loop[k] << endl;
}

Measured memory_bytes: 13840
#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int A[1000001];
int insu[1000001]={};
bool prime_factor_decomp(ll n) {
    ll m = n;
    for (ll i = 2; i * i <= n; i++) {
        if (m % i != 0) continue;
        int c = 0;
        while (m % i == 0) c++, m /= i;
        insu[i]++;
		if(insu[i]>=2) return 0;
        //e.push_back(c);
    }
    if (m > 1) {
        insu[m]++;
		if(insu[m]>=2) return 0;
        //e.push_back(1);
    }
	return 1;
    //return mp(p, e);
}
ll gcd(ll x,ll y){
	if(y==0) return x;
	return gcd(y,x%y);
}
int main(){
	int N;
	scan(N);
	rep(i,N) scan(A[i]);
	bool flag=1;
	rep(i,N){
		if(A[i]!=1){
			bool f=prime_factor_decomp(A[i]);
			if(f==0){
				flag=0;
				break;
			}
		}
	}
	if(flag){
		prin("pairwise coprime");
	}
	else{
		int g=0;
		rep(i,N) g=gcd(g,A[i]);
		if(g==1) prin("setwise coprime");
		else prin("not coprime");
	}
	return 0;
}


Measured memory_bytes: 7784
using System;

namespace ABC049C
{
    class Program
    {
        public static string target;
        public static string[] dream = {"dream", "dreamer", "erase", "eraser"};
        static void Main(string[] args)
        {
            target = Console.ReadLine();
//                Console.WriteLine("{0}",target);
//            string[] dream = new string[4] {"dream", "dreamer", "erase", "eraser"}; 
            if( daydream(0) ) {
                Console.WriteLine("YES");
            } else {
                Console.WriteLine("NO");
            }
        }
        public static Boolean daydream(int start){
//                   Console.WriteLine("X {0} {1}", s, target);
//                   Console.WriteLine("{0}", target.IndexOf("erase"));
            Boolean f = false;
            if(start == target.Length) return true;
            for(int i = 0; i < 4; i++) {
               if (dream[i].Length <= target.Length - start) {
//                   Console.WriteLine("1X {0} {1}", start, target.Substring(start, dream[i].Length) );
                   if( dream[i] == target.Substring(start, dream[i].Length)){
//                       Console.WriteLine("2X {0}", i);  
                     f = daydream(start + dream[i].Length );
                     if (f) return true;
                   } 
                }
            }
            return f;
        }
    }
}


Measured memory_bytes: 14420
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
char s[101000];
int f;
void F(int x1, int x2)   // 0ÀÇ 1 Ñò  ÀÇ¼Ù ÑòÕæ
{
	char S[101000];
	int len = strlen(s);
	S[0] = x1==1?'S':'W';
	S[1] = x2==1?'S':'W';
	for (int i = 2; i <= len+1; i++)
	{
		int k = i;
		if (k==len+1)
			k = 1;
		if (S[i-1]=='S')
		{
			if (s[k-1]=='o')
			{
				if (S[i-2]=='S')
					S[i]='S';
				else
					S[i]='W';
			}
			if (s[k-1]=='x')
			{
				if (S[i-2]=='S')
					S[i]='W';
				else
					S[i]='S';
			}
		}
		if (S[k-1]=='W')
		{
			if (s[k-1]=='o')
			{
				if (S[i-2]=='S')
					S[i] = 'W';
				else
					S[i] = 'S';
			}
			if (s[k-1]=='x')
			{
				if (S[i-2]=='S')
					S[i] = 'S';
				else
					S[i] = 'W';
			}
		}
	}
	if (S[len]==S[0]&&S[len+1]==S[1])
	{
		for (int i = 0; i < len; i++)
			printf("%c", S[i]);
		printf("\n");
		f=1;
	}
	return;
}
int main(void)
{
	int n;
	scanf("%d", &n);
	scanf(" %s", s);
	f=0;
	for (int i = 1; i >= 0; i--)
	{
		for (int j = 1; j >= 0; j--)
		{
			F(i,j);
			if (f==1)
				break;
		}
		if (f==1)
			break;
	}
	if (f==0)
		printf("-1\n");
	return 0;
}

Measured memory_bytes: 512