Codeforces Round 142 (Div. 2)
写了但是一直忘了更新。
现在A了3题。
###A.Dragons
简单题,贪心,题意打龙升能力值,问最后是否能打败所有的龙,按力量从小到大排序,尽量打败更多的龙即可。
###B. T-primes
题意为如果一个数,除自己本身和1以外,仅能被一个数整除,就把这个数叫做T-primes。
现在给一个数,求它是不是T-prime。
简单来说就是求这个数的是不是一个数k的平方,且k为素数。
素数直接套用的模板。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> using namespace std ;bool is[1000020 ];__int64 prm[500000 ]; __int64 getprm (__int64 n) { __int64 i, j, k = 0 ; __int64 s, e = (__int64)(sqrt (0.0 + n) + 1 ); memset (is, true , sizeof (is)); prm[k++] = 2 ; is[0 ] = is[1 ] = false ; for (i = 4 ; i < n; i += 2 ) is[i] = false ; for (i = 3 ; i < e; i += 2 ) if (is[i]) { prm[k++] = i; for (s = i * 2 , j = i * i; j < n; j += s) is[j] = false ; } for ( ; i < n; i += 2 ) if (is[i]) prm[k++] = i; return k; } int main () { __int64 n, x, tmp1, tmp2, k; k = getprm(1000010 ); cin >> n; for (__int64 i = 1 ; i <= n; i++) { cin >> x; tmp1 = sqrt (x); tmp2 = tmp1 * tmp1; if (tmp2 == x && is[tmp1]) cout << "YES" << endl ; else cout << "NO" << endl ; } return 0 ; }
###C. Shifts
题意是给一个矩阵,矩阵的行能够右滚动。
求最后这个矩阵能不能变形为其中有一列全为1,求最小的移动次数,不行则输出-1。
具体做法是在每一行中,对于每一列都有一个1使用最少的移动次数移动到这里。
可以将 0 1 1 0 这样的一行看为 0 1 1 0 【0 1 1 0】 0 1 1 0,化成这样来看,哪个1离哪一列最近就清晰了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> using namespace std ;int n, m;int rec[10005 ], sum[10005 ];int main () { bool flag = true ; int l, r, t; cin >> n >> m; for (int i = 0 ; i < n && flag; i++) { t = 1 ; getchar(); for (int j = 0 ; j < m; j++) { if (getchar() == '1' ) rec[t++] = j; } if (t == 1 ) flag = false ; else { int num = 0 ; rec[0 ] = rec[t - 1 ] - m; rec[t] = rec[1 ] + m; for (int j = 0 ; j < m ; j++) { l = rec[num + 1 ] - j; r = j - rec[num]; if (l == 0 ) num++; sum[j] += min(l, r); } } } if (flag) printf ("%d\n" , *min_element(sum,sum+m)); else printf ("-1\n" ); return 0 ; }