prompt
string
response
string
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
# input n = int(input()) mod = pow(10, 9) + 7 c = [] dp = [0] * (n + 1) stone_history = dict() for i in range(n): stone = int(input()) if len(c) == 0: dp[i + 1] = 1 else: if stone in stone_history and c[-1] != stone: dp[i + 1] = dp[i] + stone_history[stone] else: ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys input = sys.stdin.readline INF = 10 ** 9 MOD = 10 **9 + 7 N = int(input()) C = [int(input()) for _ in range(N)] lst = [INF] * (2 * 10 ** 5 + 10) #各番号がどこにあるかを管理するリスト ans = [1] * N lst[C[-1]] = N - 1 for i in range(N - 2, -1, -1): c = C[i] if lst[c] == INF or lst[c] == i + 1: ans[i] = ans...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
# coding: utf-8 # Your code here! n=int(input()) c = [int(input()) for i in range(n)] r = [-1]*n d=dict() for i, cc in enumerate(c): if cc in d: r[i] = d[cc] d[cc] = i else: d[cc] = i #print(c) #print(r) dp=[1]*n dp[0]=1 for i in range(1,n): if r[i] == -1: dp[i] = dp[i-1] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from collections import defaultdict as dd d = dd(int) r = 1 p = "" for _ in range(int(input())): c = input() if c != p: r = (r + d[c]) % (10**9 + 7) d[c] = r p = c print(r)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
MOD = 10**9 + 7 N = int(input()) stones = [int(input()) for _ in range(N)] dp = [0] * (N+1) rightmost = [0] * (2 * 10**5 + 1) dp[0] = 1 for i, c in enumerate(stones): dp[i+1] += dp[i] dp[i+1] %= MOD if rightmost[c] not in [0, i]: dp[i+1] += dp[rightmost[c]] dp[i+1] %= MOD rightmost[c] = ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> #define For(a,b,c) for(int a=b;a<=c;++a) #define Dor(a,b,c) for(int a=b;a>=c;--a) using namespace std; const int N=200007,P=1000000007; int A[N],F[N],G[N]; int main () { int n; scanf("%d",&n); For(i,1,n) scanf("%d",&A[i]); n=unique(A+1,A+1+n)-A-1; F[0]=1; For(i,1,n) { G[A[i]]=(G[A[i]]+F...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define maxn 200005 #define Mod 1000000007 #define ll long long #define Mod 1000000007 using namespace std; inline int read(){ int x=0,c;while(!isdigit(c=getchar())); while(x=x*10+c-'0',isdigit(c=getchar())); return x; } int n,a[maxn],f[maxn],ans; int main(){ n=read(); for(int i=1;i<=n;i++)...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); in...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<cstdio> #include<vector> #define ll long long #define rep(i, l, r) for(int i=l; i<=r; ++i) #define N 200005 #define mod 1000000007 using namespace std; int t; int n; int a[N]; int dp[N]; int f[N]; int main() { scanf("%d", &n); rep(i, 1, n) scanf("%d", &a[i]); dp[0]=1; rep(i, 1, n){ dp[i]=0; if(i==1 || ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from bisect import bisect_left N=int(input()) C=[int(input()) for i in range(N)] mod=10**9+7 data=[[] for i in range(max(C)+1)] for i in range(N): data[C[i]].append(i) dp=[0]*N dp[0]=1 for i in range(1,N): h=data[C[i]] b=bisect_left(h,i) if b==0: dp[i]=dp[i-1] elif C[i]==C[i-1]: dp[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int NS = (int)2e5+4, MOD = (int)1e9+7; int N, arr[NS]; int last[NS]; int d[NS]; int main(){ scanf("%d", &N); for(int i=1;i<=N;++i) scanf("%d", arr+i); d[0] = 1; for(int i=1;i<=N;++i){ d[i] = d[i-1]; if(last[arr[i]]&&last[arr[i]]<i-1) d[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> using namespace std; #define mod 1000000007 int l[200005],dp[200005]; int main() { int n; scanf("%d",&n); dp[0]=1; for (int i=1;i<=n;i++) { int a; scanf("%d",&a); dp[i]=dp[i-1]; if (l[a] && l[a]!=i-1) dp[i]=(dp[i]+dp[l[a]])%mod; l[a]=i; } printf("%d",dp[n]); }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) MODN = int(1e9 + 7) D = {} X = [0] * N for i in range(N): c = int(input()) if not c in D: X[i] = 1 if i == 0 else X[i-1] D[c] = i continue X[i] = (X[i-1] + X[D[c]]) % MODN if D[c] < i-1 else X[i-1] D[c] = i print(X[N-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] p = [0 for _ in range((2 * 10**5) + 1)] dp = [0 for _ in range(N)] p[C[-1]] = -1 dp[-1] = 1 for i in range(2, N + 1): if p[C[-i]] == 0 or p[C[-i]] == -i + 1: dp[-i] = dp[-i + 1] else: dp[-i] = (dp[-i + 1] + dp[p[C[-i]]]) % (10**9 + 7) p[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys def input(): return sys.stdin.readline().strip() def mapint(): return map(int, input().split()) sys.setrecursionlimit(10**9) N = int(input()) mod = 10**9+7 last = {} dp = [0]*(N+1) dp[0] = 1 for i in range(1, N+1): dp[i] = dp[i-1] c = int(input()) if c in last and last[c]+1<i: dp[i] += ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
def main(): n, *cs = map(int, open(0)) fwd = [0] * n mod = 10 ** 9 + 7 d = {} for i, c in enumerate(cs[::-1], start=1): f = d.get(c, 0) if f: fwd[n - i] = f d[c] = n - i dp = [0] * (n + 1) dp[0] = 1 for i, fwd in enumerate(fwd, start=1): dp[i] += dp[i - 1] % mod if fwd > i: dp[fwd + 1] += dp[i] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) dp = [1 for i in range(0,n+1)] c = list() c.append(-1) d = {} for i in range(1,n+1): c.append(int(input())) for i in range(1,n+1): if c[i] in d: j = d[c[i]] d[c[i]] = i if c[i-1] != c[i]: dp[i] = dp[i-1]+dp[j] continue else: d[c[i]] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys input = sys.stdin.readline N = int(input()) C = [int(input()) for _ in range(N)]+[-1] l = [] for i in range(N): if C[i]!=C[i+1]: l.append(C[i]) if len(l)==1: print(1) exit() dp = [0]*(len(l)) acc = [0]*(2*10**5+100) dp[0] = 1 acc[l[0]] = 1 acc[l[1]] = 1 MOD = 10**9+7 for i in ran...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#!/usr/bin/env python # -*- coding: utf-8 -*- def main(): value = 1 skips = {} prev = -1 for _ in range(int(input())): n = int(input()) if n == prev: continue prev = n if n in skips: value += skips[n] skips[n] = value print(value % int(1e+9 + 7), flush=True) if __name...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int MOD = 1e9 + 7; int n, m; int a[N], b[N], f[N], lp[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (a[i] != a[i - 1]) { b[++m] = a[i]; } } f[1] = 1; lp[b[1]] = 1; for ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N=int(input()) C=[int(input()) for _ in range(N)] C=[0]+[c for i,c in enumerate(C) if i==0 or C[i-1]!=c] MOD=10**9+7 #dp[i]=インデックスiまででの塗り方の数 dp=[0]*(len(C)) dp[0]=1 import collections memo=collections.defaultdict(int) for i in range(1,len(dp)): dp[i]=(dp[i-1]+memo[C[i]])%MOD memo[C[i]]=(memo[C[i]]+dp[i-1])%MOD...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> using namespace std; const int mod=1000000007; int n,x,f[1<<20],lst[1<<20]; int main(){ scanf("%d",&n); f[0]=1; for (int i=1;i<=n;++i){ scanf("%d",&x); f[i]=f[i-1]; if (lst[x]&&lst[x]!=i-1){ f[i]=(f[i]+f[lst[x]])%mod; } lst[x]=i; } printf("%d\n",f[n]); return 0; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
nn=int(input()) import sys input=sys.stdin.readline cc=[int(input()) for _ in range(nn)] c=[cc[0]] for i in range(1,nn): if cc[i]!=c[-1]: c.append(cc[i]) n=len(c) mod=10**9+7 dp=[0]*(n+1) sm=[0]*(max(c)+1) dp[0]=1 for i in range(n): x=c[i] dp[i+1]+=sm[x]+dp[i] sm[x]+=dp[i] dp[i]%=mod dp[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
a = int(input()) C = list() last = [-1] * 2 * 100002 for i in range(a): C.append(int(input())) ans = [1] * a last[C[0]] = 0 # print(last,ans) for i in range(1, a): ans[i] = ans[i - 1] if C[i] == C[i - 1]: continue if last[C[i]] != -1: ans[i] += ans[last[C[i]]] last[C[i]] = i print(...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) oth = [int(input()) for _ in range(n)] mod = 10**9 + 7 l = list() for x in oth: if l and l[-1] == x: continue l.append(x) lenl = len(l) last = [-1] * 200001 l2 = [-1] * 200001 for i, a in enumerate(l): l2[i] = last[a] last[a] = i dp = [0] * lenl dp[0] = 1 for i in range(1, lenl...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<iostream> using namespace std; long mod=1e9+7,dp[2<<17]; int c[2<<17],pr[2<<17]; int main() { int n;cin>>n; dp[0]=1; for(int i=0;i++<n;) { int C;cin>>C; dp[i]=dp[i-1]+(pr[C]!=0&&pr[C]<i-1?dp[pr[C]]:0); dp[i]%=mod; pr[C]=i; } cout<<dp[n]<<endl; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> using namespace std; int n,c[200001],s[200001]; long long dp[200001],INF=1000000007; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>c[i]; if(c[i]==c[i-1])i--,n--; }dp[0]=1; for(int i=1;i<=n;i++){ dp[i]=(dp[i-1]+s[c[i]])%INF; s[c[i]]+=dp[i-1]; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<cstdio> #include<algorithm> #define MOD 1000000007 #define MAXN 200006 using namespace std; int n,last[MAXN],a; long long d[MAXN]; int main() { scanf("%d",&n); d[0]=1; for(int i=1;i<=n;i++) { scanf("%d",&a); d[i]=d[i-1]; if(last[a]!=i-1&&last[a]!=0) d[i]=(d[i]+d[last[a]])%MOD; last[a]=i; } printf...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from collections import defaultdict d = defaultdict(int) D = defaultdict(lambda:1) N = int(input()) ans = 1 C = int(input()) d[C] += 1 c = C for i in range(N - 1): C = int(input()) d[C] += 1 if C == c: pass else: if d[C] >= 2: ans = (ans + D[C]) % 1000000007 D[C] = ans c = C print(ans)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> #include <vector> using namespace std; int main(){ int n, temp, pre; vector<int> c(1, 0); vector<int> a(300000, 0); vector<int> p(300000, -1); a[0] = 1; cin >> n; for(int i = 1;i<=n;i++){ cin >> temp; if(pre == temp) a[i] = a[i-1]; else if(p[temp] == -1){ a[i] = a[i-1]; p[temp] =...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] before = [-1] * (2 * 10**5 + 1) count = [0] * (N + 1) count[-1] = 1 mod = 10**9 + 7 for i in range(N): count[i] = count[i - 1] if 0 <= before[C[i]] < i - 1: count[i] += count[before[C[i]]] count[i] %= mod before[C[i]] = i print(count[N - ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n=int(input()) s=[[]for i in range(2*10**5+1)] d=[1] s[int(input())].append(0) for i in range(1,n): j=int(input()) t=0 s[j].append(i) if len(s[j])>1: if s[j][-2]!=s[j][-1]-1: t+=d[i-1]+d[s[j][-2]] else: t+=d[i-1] else: t+=d[i-1] d.append(t) print(...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N=int(input()) mod=10**9+7 dp=[0]*N idx=dict() idx[int(input())]=0 dp[0]=1 for i in range(1,N): C=int(input()) if C in idx.keys(): if idx[C]<i-1: dp[i]=(dp[idx[C]]+dp[i-1])%mod else: dp[i]=dp[i-1] else: dp[i]=dp[i-1] idx[C]=i print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int N=2e5 +10; long long f[N], g[N]; int main() { long long MOD=1e9+7; int n; cin>>n; int prv=0; f[0]=1; for(int i=1, x; i<=n; ++i) { cin>>x; f[i]=f[i-1]; if (x!=prv) { f[i]=(f[i]+g[x])%MOD; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<iostream> using namespace std; #define M cin>> int s[200001]; int main() { int n,c,l,z,x,h=1e9+7; M n; M z; c=l=s[z] = 1; for(int i = 2; i <= n; i ++) { M x; if(x!=z) { c+=s[x]; c%=h; s[x]+=l; s[x]%=h; } l=c; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for i in range(N)] MOD = 10**9+7 dp = [0] * N dp[0] = 1 bak = {C[0]:0} for i,c in enumerate(C): if i==0: continue dp[i] = dp[i-1] if c in bak and bak[c] < i-1: dp[i] += dp[bak[c]] dp[i] %= MOD bak[c] = i print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n=int(input()) c=[int(input())for i in range(n)] l=[-1]*(max(c)+1) d=[0]*(n+1) d[0]=1 for i in range(1,n+1): d[i]=d[i-1];C=l[c[i-1]] if i-1!=C!=-1:d[i]+=d[C] l[c[i-1]]=i;d[i]%=(10**9+7) print(d[n])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from collections import defaultdict N = int(input()) p = 10 ** 9 + 7 C = [0] * (N + 1) D = defaultdict(lambda: -1) DP = [[0, 0] for i in range(N + 1)] DP[0] = [1, 0] for i in range(1, N + 1): C[i] = int(input()) DP[i][0] = sum(DP[i - 1]) % p if C[i] != C[i - 1]: DP[i][1] = sum(DP[D[C[i]]]) if D[C[i]] != -1 el...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int N=2e5+5,YL=1e9+7; inline int MO(const int &x){return x>=YL?x-YL:x;} int lst[N],n,f[N],s[N]; inline int in(){int k;cin>>k;return k;} int main() { ios::sync_with_stdio(0); cin>>n;f[0]=1; for(int i=1;i<=n;++i) { ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define M 200005 using namespace std; const int mod=1e9+7; int n,c[M]; int dp[M],val[M]; void Add(int &x,int y){ x+=y; if(x>=mod)x-=mod; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&c[i]); dp[0]=1;c[0]=-1; for(int i=1;i<=n;i++){ if(c[i]!=c[i-1])Add(val[c[i]],dp[i-1]); ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
MOD=10**9+7 N=int(input()) a=[int(input()) for i in range(N)] last={} tmp={} ans=1 x=a[0] last[x]=0 tmp[x]=1 for i in range(1,N): x=a[i] last.setdefault(x,i) tmp.setdefault(x,0) if last[x]!=i-1: ans+=tmp[x] last[x]=i tmp[x]=ans%MOD ans=ans%MOD print(ans)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> #define MOD 1000000007 using namespace std; int C[200005]; int pre[200005]; long dp[200005]; int main() { int n, c; cin >> n; dp[0] = 1; for(int i = 1; i <= n; ++i) { cin >> c; if (pre[c] > 0 && pre[c] != i - 1) { dp[i] = dp[i - 1] + dp[pre[c]]; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import bisect MOD=10**9+7 N=int(input()) clist=[] for i in range(N): c=int(input()) clist.append(c) #print(clist) c2list=clist[:1] for i in range(1,N): if clist[i]!=c2list[-1]: c2list.append(clist[i]) #print(c2list) N2=len(c2list) cldic={} dp=[0]*(N2-1)+[1] for i in range(N2): c=c2list[i] dp[i]=dp[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; int n,a[200010],ji[200010]; map<int,int> mp; const long long mod=1e9+7; long long dp[200010]; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); if(!mp[a[i]]) ji[i]=0; else ji[i]=mp[a[i]]; mp[a[i]]=i; } dp[1]=1; for(int i=2;i<=n;++...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] dp = [0] * N dp[0] = 1 d = {C[0]:0} for i in range(1, N): if C[i] in d and C[i] != C[i-1]: dp[i] = (dp[i-1] + dp[d[C[i]]]) % (10**9+7) else: dp[i] = dp[i-1] d[C[i]] = i print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.util.Scanner; public class Main { static int[] a; static int n; static int[] streak; static long MOD = 1000000007; static long[] dp; static long get(int i) { if(i<0||i>=n)return 1; else return dp[i]; } static void test() { int n = 1000; System.out.println(n); for(int i = 0; i < n; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) c = [int(input()) for i in range(n)] num,x = 1,[0]*(200001) c.append(0) x[c[0]]+=1 for i in range(1,n): if c[i]-c[i-1]: x[c[i]] = (x[c[i]]+num)%(10**9+7) if c[i]-c[i+1]: num = x[c[i]] else: num = 0 print(num)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> using namespace std; using LL = long long; constexpr int maxn = 240000; constexpr LL mod = 1000000007; int c[maxn], p[maxn], f[maxn]; int main(){ int n, m = 0; scanf("%d", &n); for(int i = 1; i <= n; i += 1) scanf("%d", c + i); for(int i = 1; i <= n; i += 1) if(c[i] != c[i - 1]) c[m += 1] = ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> #include <vector> using namespace std; vector<int> v,w[200010]; long long dp[200010],sum[200010] = {},mod = 1000000007; int main(){ int i,n; cin >> n; for(i=0;i<n;i++){ int c; cin >> c; if(v.size()==0 || v.back()!=c) v.push_back(c); } n = v.size(); dp[0] = 1; for(i=1;i<=n;i++){ int x = ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int N = 200005, MOD = 1E9 + 7; int n, u, cur = 1, lst = 0, dp[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> u; if (u != lst) { lst = u; cu...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD=1e9+7; int main(){ int N;cin>>N; int C[200010]; ll dp[200010]; for (int i=0;i<N;++i){int c;cin>>c;C[i]=c;} ll cum[200010]; memset(cum, 0, sizeof(cum)); dp[0]=1; cum[C[0]]=1; if(N==1) {cout << 1 << endl;return 0;} for...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); final int M = 1_000_000_007; int N = scanner.nextInt(); int[] c = new int[N]; for (int i = 0; i < N; i++) c[i] = scanner.nextInt(); HashM...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int _=2e5+5,yl=1e9+7; void inc(int &x,int y){x+=y;if(x>=yl)x-=yl;} int n,a[_],g[_],f[_]; int main(){ ios::sync_with_stdio(false); cin>>n;f[0]=1; for(int i=1;i<=n;++i)cin>>a[i]; for(int i=1;i<=n;++i){ f[i]=f[i-1]; if(a[i]!=a[i-1])inc(f[i],...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
mod=(10**9)+7 a=[0]*200005 dp=[0]*200005 s=[0]*200005 cnt=0 n=int(input())+1 for i in range (1, n): x=int(input()) if x!=a[cnt]: cnt=cnt+1 a[cnt]=x cnt+=1 dp[0]=1 for i in range (1, cnt): dp[i]=dp[i-1] dp[i]=(dp[i]+s[a[i]])%mod s[a[i]]=(s[a[i]]+dp[i-1])%mod print(dp[cnt-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys from collections import defaultdict N = int(input()) C = [0] for _ in range(N): c = int(sys.stdin.readline()) if C[-1] == c: continue else: C.append(c) ans = 1 dic = defaultdict(lambda: 0) mod = 10 ** 9 + 7 for c in C: a = dic[c] dic[c] += ans ans = (ans + a) % mod ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) mod = pow(10, 9) + 7 x = 0 c = [] for _ in range(n): y = int(input()) if not x == y: c.append(y) x = y m = len(c) last = [-1] * (2 * pow(10, 5) + 5) dp = [0] * m dpsum = [0] * m for i in range(m): if not last[c[i]] == -1: dp[i] = dpsum[last[c[i]]] + 1 dpsum[i] = dp[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys input = sys.stdin.readline MOD = 10**9 + 7 N = int(input()) C = [int(input()) for _ in range(N)] dp = [0]*(N+1) c_to_i = [-1]*(2*10**5+1) dp[0] = 1 c_to_i[C[0]] = 0 for i,c in enumerate(C[1:],1): prev_i = c_to_i[c] dp[i] = dp[i-1] if prev_i != i-1: dp[i] += dp[prev_i] dp[i] %= ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> using namespace std; const int mod = 1e9+7,N=2e5+5; typedef long long ll; long long a[N],f[N],c[N]; int main(){ int n; cin>>n; f[0]=1; for(int i=1;i<=n;++i){ cin>>a[i]; if(a[i]==a[i-1]){ f[i]=f[i-1]; continue; } f[i]=(f[i-1]+c[a[i]])%mod; (c[a[i]]+=f[i-1])%=m...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <iostream> using namespace std; const long long N=200200; const long long p=1e9+7; long long a[N],n,b[N],f[N]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } f[0] = 1; for (int i = 1; i <= n; i++) { if (a[i] == a[i - 1]) { f[i] = f[i - 1]; } else { f[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n, *cs = map(int, open(0)) fwd = [0] * n mod = 10 ** 9 + 7 d = {} for i, c in enumerate(cs[::-1], start=1): f = d.get(c, 0) if f: fwd[n - i] = f d[c] = n - i dp = [0] * (n + 1) dp[0] = 1 for i in range(1, n + 1): dp[i] += dp[i - 1] dp[i] %= mod # 隣だったら無視 if fwd[i - 1] > i: dp[fwd[i - 1] + 1] += dp[i] print(d...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define M 200005 #define Mod 1000000007 using namespace std; int id=0,a[M],n; long long dp[M],la[M];//到第i位的不同序列 最后以x结尾的序列 int main(){ memset(la,0,sizeof(la)); scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)if(a[i]!=a[i-1])a[++id]=a[i]; dp[0]=1; for(int i=1...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#!/usr/bin/env python3 MOD = 10**9 + 7 N = int(input()) C = [int(input()) for _ in range(N)] dic = dict() dp = [None] * N for i, x in enumerate(C): if x in dic: dp[i] = dic[x] dic[x] = i dp2 = [0] * N dp2[0] = 1 for i in range(1, N): dp2[i] = dp2[i - 1] if dp[i] is not None and i - 1 != dp[i]:...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=200050; const ll MOD=1e9+7; int n; int a[N],p[N]; ll f[N]; int main(){ scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]); f[0]=0;f[1]=1; memset(p,0,sizeof(p));p[a[1]]=1; for (int i=2;i<=n;++i){ f[i]=f[i-1]; if (a[i]!=a[i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#!/usr/bin python3 # -*- coding: utf-8 -*- mod = 10**9+7 n = int(input()) seen = {} # dp[i]: 左からi番目まで見た際の場合の数 dp = [0] * (n+1) dp[0] = 1 for i in range(1, n+1): ci = input() j = seen.get(ci, 0) if j == 0: #新しい色の追加 dp[i]+=dp[i-1] elif j == i-1: #直前と同じ dp[i]+=dp[i-1] e...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define maxn 200010 #define mod 1000000007 using namespace std; int a[maxn], f[maxn], sum[maxn]; int main() { int n, cur = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (cur == 0 || x != a[cur]) a[++cur] = x; } f[0] = 1; for (int i = 1; i <= cur; i++)...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); long mod = (long)1e9+7; int[] left = new int[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) c = [int(input()) for _ in range(n)] mod = 10 ** 9 + 7 color_sum = {i: 0 for i in range(10 ** 6)} dp = [1 for _ in range(n+1)] dp[0] = 1 dp[1] = 1 color_sum[c[0]] = 1 for i in range(2, n+1): dp[i] = dp[i-1] if c[i-2] == c[i-1]: continue dp[i] += color_sum[c[i-1]] dp[i] %= mo...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+10,mo=1e9+7; long long f[N]; int las[N]; int main() { int n; scanf("%d",&n); f[0]=1; for (int i=1;i<=n;i++) { int x; scanf("%d",&x); f[i]=f[i-1]; if (las[x]&&las[x]<i-1) f[i]=(f[i]+f[las[x]])%mo; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);i++) using namespace std; int a[200010],b[200010]; int f[200010],g[200010]; int n,m; int main(){ cin>>n; rep(i,1,n)cin>>a[i]; rep(i,1,n)if(i==0||a[i]!=a[i-1])m++,b[m]=a[i]; n=m; rep(i,1,n)a[i]=b[i]; f[0]=1LL,g[a[1]]=1LL; rep(i,1,n)f[i]=g[a[i]],g[a[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
mod = 1000000007 N = int(input()) C = [] for i in range(N): c = int(input()) if i == 0 or C[-1] != c: C.append(c) cases = [1] * (len(C) + 1) indexes = dict() for i in range(1, 1 + len(C)): c = C[i - 1] if c not in indexes: cases[i] = cases[i - 1] else: cases[i] = (cases[i - ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] MOD = 10**9 + 7 if N <= 2: print(1) exit() dp = [1,1] last_idx = [-1]*(N+10) last_idx[C[0]] = 0 last_idx[C[1]] = 1 for i in range(2, N): if C[i-1] == C[i] or last_idx[C[i]] == -1: dp.append(dp[-1] % MOD) else: dp.append((dp[-1] + d...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n=int(input()) c=[int(input()) for _ in range(n)] color_sum=[0]*200001 dp=[0]*(n+1) dp[0]=1 dp[1]=1 color_sum[c[0]]+=1 # i個目まで考えたときの解。求めるのはdp[n] # dp[i]:i個目より前で、i個目と同じ色の石があるところをjとして、dp[j-1]の和+dp[i-1] for i in range(1,n): #二個目からn-1個目の計算をする。 dp[i+1]=dp[i] if c[i]==c[i-1]: continue dp[i+1]+=color_sum[c[i]] col...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] mod = int(1e9+7) from itertools import groupby x = [0] + [k for k, g in groupby(C)] dp = [0]*(len(x)) # i個目までで何通りあるか dp[0] = 1 memo = {} for i in range(1, len(x)): c = x[i] memo[c] = (memo.get(c,0) + dp[i-1])%mod dp[i] = memo[c] print(dp[-1])
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys from collections import defaultdict n = int(input()) stones = [-1] for line in sys.stdin: c = int(line) if stones[-1] == c: continue stones.append(c) dp = 1 prev = defaultdict(lambda: 0) MOD = 10 ** 9 + 7 for s in stones[1:]: dp = (dp + prev[s]) % MOD prev[s] = dp print(dp)
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; #define ll long long ll mod = 1e9+7; int main(){ int N; cin >> N; vector<ll> dp(N+1, 0); dp[0] = 1; vector<int> pre(200001, -1); for (int i = 1; i <= N; i++) { int C; cin >> C; dp[i] += dp[i-1]; if (pre[C] >= 1 && pr...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; int pre = -1; long long ans = 1; map<int, long long> mp; for(int i = 0; i < N; ++i){ int x; cin >> x; if(x == pre)continue; pre = x; mp[x] += ans % 1000000007; ans = mp[x] % 1000000007; } cout << ans << endl; r...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from collections import defaultdict N = int(input()) l = [int(input()) for _ in range(N)] dp = [0] * (N+1) pos = defaultdict(lambda: -1) dp[0] = 1 MOD = 10**9+7 for i in range(1, N+1): p = pos[l[i-1]] if i-p>1 and p!=-1: dp[i] += dp[p] pos[l[i-1]] = i dp[i] += dp[i-1] dp[i] %= MOD pr...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) c = [int(input()) for _i in range(n)] c.append(-1) dic = {} dp = [0 for _i in range(n+1)] dp[n] = 1 mod = 10**9 +7 for i in range(n): if c[i-1] != c[i]: if c[i] in dic: dp[i] = (dp[i-1] + dic[c[i]])%mod else: dp[i] = dp[i-1] dic[c[i]] = dp[i] else...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys N = int(input()) C = [] pre = 0 for _ in range(N): c = int(sys.stdin.readline()) if pre == c: continue else: C.append(c) pre = c ans = 1 dic = {} for c in C: if c in dic.keys(): a = dic[c] dic[c] += ans ans += a else: dic[c] = ans ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
n = int(input()) c = [0] * (n + 1) for i in range(1, n + 1): c[i] = int(input()) lc = [0] * (2 * 10**5 + 1) dp = [0] * (n + 1) dp[1] = 1 lc[c[1]] = 1 for i in range(2, n + 1): if lc[c[i]] == i - 1: dp[i] = dp[i - 1] else: dp[i] = dp[i - 1] + dp[lc[c[i]]] lc[c[i]] = i print(dp...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [] tmp_b = -1 for _ in range(N): tmp_n = int(input()) if tmp_n != tmp_b: C.append(tmp_n) tmp_b = tmp_n SUM = 1 DP_dict = {} for c in C: if c not in DP_dict.keys(): DP_dict[c] = SUM else: SUM += DP_dict[c] DP_dict[c] = SUM print(SUM % (10**9+7))
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
N = int(input()) C = [int(input()) for _ in range(N)] MOD = 10**9 + 7 dp = [0] * (N + 1) colorToIndex = [-1] * (max(C) + 10) colorToIndex[C[0]] = 0 dp[0] = 1 for i, color in enumerate(C[1:], start=1): prevIndex = colorToIndex[color] dp[i] = dp[i - 1] if prevIndex != i - 1: dp[i] += dp[prevIndex] ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<cstdio> const int mod=1000000007; const int N=200050; int a[N],p[N],f[N]; int main(){ int n=0,m,i,x; scanf("%d",&m); for(i=1;i<=m;++i){ scanf("%d",&x); if(!n||x!=a[n])a[++n]=x; } f[1]=1;p[a[1]]=1; for(i=2;i<=n;++i){ f[i]=(f[i-1]+f[p[a[i]]])%mod; p[a[i]]=i; } printf("%d\n",f[n]); return 0; }
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 int main(){ long Nc;cin>>Nc; vector<long>C; for(long i=0;i<Nc;i++){ long c;cin>>c;c--; if(i==0||c!=C[C.size()-1])C.push_back(c); } long N=C.size();Nc=200000; vector<long>DP(N,1),L(Nc,0); for(long i=0;i<N;i++){ if(i!=0)DP[i]=...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define maxn 200005 using namespace std; typedef long long ll; const ll mod=1e9+7; int n,c[maxn],t[maxn]; ll dp[maxn],calc[maxn]; void init(){ scanf("%d",&n); int top=0; for(int i=1;i<=n;i++){ scanf("%d",&c[i]); if(c[i]!=c[i-1]) t[++top]=c[i]; } dp[0]=1ll; for(int i=1;i<=top;i++){ ca...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
MOD = 1000000007 N = int(input()) C = [int(input()) for _ in range(N)] prev = [None for _ in range(max(C)+1)] dp = [0 for _ in range(N+1)] dp[0] = 1 for i in range(N): if prev[C[i]] is None or prev[C[i]]==i: dp[i+1] = dp[i] else: dp[i+1] = dp[i] + dp[prev[C[i]]] dp[i+1] %= MOD prev[C[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Input...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.util.*; import java.io.*; import java.math.*; public class Main{ static void solve(){//Here is the main function int N = nextInt(); int mod = 1000000007; int[] dp = new int[200005]; dp[0] = 1; int mae = 0; for(int i = 0; i < N; i++){ int C = nextInt(); if(C == mae){ continue; } ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20 , mod = 1e9 + 7; int n , dp[maxn] , c[maxn] , a[maxn]; long long res; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } dp[0] = 1; for (int i = 1; i <= n; i++) { if (a[i] != a[i - 1]) { c[a[i]] += dp[i - 1]; ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
from collections import defaultdict N = int(input()) C = [int(input()) for i in range(N)] MOD = 10 ** 9 + 7 dp = [0] * (N + 1) dp[0] = 1 D = defaultdict(int) for i, c in enumerate(C, start=1): dp[i] += dp[i - 1] if D[c] > 0 and D[c] != i - 1: dp[i] += dp[D[c]] dp[i] %= MOD D[c] = i print(dp[-...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; const int MX = 200000, md = 1000000007; int c[MX], f[MX], g[MX]; int main() { int n; ignore = scanf("%d", &n); for (int i = 0; i < n; i++) { ignore = scanf("%d", c + i); c[i]--; } g[c[0]]++; for (int i = 0; i < n; i++) { f[i] = g[c[i]]; if (i + 1 < n && ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+5; const int mod=1e9+7; int cnt[N],a[N]; int main(){ int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); n=unique(a+1,a+n+1)-a-1; int ans=1; for(int i=1;i<=n;i++){ int t=an...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> #define ll long long #define Mod 1000000007 #define M 200005 using namespace std; int n,A[M],las[M],W[M],dp[M]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&A[i]); for(int i=1;i<=n;i++){ las[i]=W[A[i]]; W[A[i]]=i; // cerr<<i<<" "<<las[i]<<endl; } dp[0]=1; for(int i=1;i...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include<bits/stdc++.h> using namespace std; const int mod=1e9+7; int a[200010],f[200010],c[200010]; int n; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); } f[0]=1; for (int i=1;i<=n;i++) { f[i]=f[i-1]; if (c[a[i]]&&c[a[i]]!=i-1) f[i]=(f[i]+f[c[a[i]]])%mod; c[a[i]]=i; } cout ...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import sys MAX_INT = int(10e10) MIN_INT = -MAX_INT mod = 1000000007 def IL(): return list(map(int,input().split())) def SL(): return input().split() def I(): return int(sys.stdin.readline()) def S(): return input() N = I() C = [I() for i in range(N)] dic = {} ans = 0 tmp = "None" for i in C: if i == tmp: conti...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; typedef long long ll; int c[200019]; int main() { int last,x,n; ll ans; cin >> n; ans=1; last=-1; for(int i=0; i<n; i++){ cin >> x; if(x==last){continue;} last=x; ans = (ans+c[x])%1000000007; c[x]=ans; } cout << ans << endl; return...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
#include <bits/stdc++.h> using namespace std; using lint = long long; const int MAXN = 500005; const int mod = 1e9 + 7; int n, a[MAXN]; lint dp[MAXN]; map<int, lint> mp; int main(){ scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); dp[n + 1] = 1; for(int i=n; i; i--){ dp[i] = dp[i + 1]; if(a[i] != a[...
Problem: There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i. Snuke will perform the following operation zero or more times: * Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possibl...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author cunbidun */ public class M...