5 2 -2 0 2 0 -2 0 5 2 3 2 3 3 3 3
12 5
#include <cstdio> #include <iostream> #include <cstring> using namespace std; #define N 100000 + 5 #define LL long long int a[N]; int D1[N], D2[N]; int d1, d2; int n; int main() { while(~scanf("%d%d%d", &n, &d1, &d2)) { for(int i = 1; i <= n; i++) scanf("%d", a + i); D1[1] = D2[1] = 1; int cn1 = 1, cn2 = 1; for(int i = 2; i <= n; i++) { if(a[i] == a[i - 1] + d1) cn1++; else cn1 = 1; D1[i] = cn1; if(a[i] == a[i - 1] + d2) cn2++; else cn2= 1; D2[i] = cn2; } // for(int i = 1; i <= n; i++) // cout<<D1[i]<<" "; // cout<<endl; // // for(int i = 1; i <= n; i++) // cout<<D2[i]<<" "; // cout<<endl; LL ans = 0; for(int i = 1; i <= n; i++) { if(D1[i] >= 2) { ans += D1[i]; } else if(D2[i] >= 2) { int t = i - D2[i] + 1; ans += (D1[t] + D2[i] - 1); //cout<<i<<": "<<D1[t] + D2[i]<<endl; } else ans += 1; } printf("%I64d\n", ans); } } /* 9 1 -1 1 2 3 4 5 4 3 2 1 */
3 3 1 2 3 -1 3 2 1
1HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
#include <cstdio> #include <iostream> #include <cstring> using namespace std; #define N 100 + 5 #define LL long long const int mod = 1000000000 + 7; int n, m; int f[N][N]; int cnt; int mul_pow(int a, int k) { int res = 1; while(k) { if(k & 1) res = ((long long)res * a) % mod; a = ((long long)a * a) % mod; k >>= 1; } return res; } int fact(int a) { int res = 1; for(int i = 1; i <= a; i++) res = ((LL)res * i) % mod; return res; } bool h[N]; int op[N]; int main() { while(~scanf("%d%d", &n, &m)) { memset(op, 0, sizeof op); int t; cnt = 0; for(int i = 1; i <= m; i++) { scanf("%d", &t); if(t == -1) { op[i] = 1; cnt++; } else { f[i][1] = t; for(int j = 2; j <= n; j++) scanf("%d", &f[i][j]); } } int flag = 0; for(int i = 1; i <= m; i++) { memset(h, false, sizeof h); if(op[i] == 0) for(int j = 1; j <= n; j++) { t = f[i][j]; if(h[t] || t > n || t <= 0) { //cout<<i<<" "<<j<<endl; flag = 1; break; } h[t] = true; } } if(flag) { printf("0\n"); continue; } flag = 0; if(cnt == 0) { for(int i = 1; i <= n; i++) { t = i; for(int j = m; j >= 1; j--) t = f[j][t]; if(t != i) { flag = 1; break; } } if(flag) printf("0\n"); else printf("1\n"); } else { t = fact(n); int ans = mul_pow(t, cnt - 1); printf("%d\n", ans); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dojintian/article/details/47754535