Codeforces Round #166 (Div. 2)

A  Beautiful Year

题意

给一个四位数y(1000 <= y <= 9000) 求第一个比y大的每一位都不同的数(尼玛怎么这么绕。。。)

解法:

这题目真是水的不行。。。直接暴力。 1min 过掉还是很爽的

Code:

  1. int x;  
  2. bool use[10];  
  3. bool check(int y){  
  4.     RST(use);  
  5.     while(y){  
  6.         if(use[y%10]) return false;  
  7.         use[y %10] = 1;  
  8.         y /= 10;  
  9.     }  
  10.     return true;  
  11. }  
  12. void solve(){  
  13.     x++;  
  14.     while(!check(x)) x++;  
  15.     OT(x);  
  16. }  
  17. int main(){  
  18.     while(cin >>x) solve();  
  19.   
  20. }  

B  Prime Matrix

题意:

给一个N * M 的矩阵,每次可以给一个格子 + 1 , 要求某行 或者 某列都是质数,求最少操作次数。

解法:

暴力筛出1 ~ 1e5 + 3 (100003是质数)然后暴力。

Code:

  1. const int N = 1000;  
  2. int a[N][N];  
  3. SI hash;  
  4. SI :: iterator iter;  
  5. int n , m;  
  6. void solve(){  
  7.     REP_2(i , j , n , m) {  
  8.         RD(a[i][j]);  
  9.         iter = hash.lower_bound(a[i][j]);  
  10.         a[i][j] = *iter - a[i][j];  
  11.     }  
  12.     int ans = INF;  
  13.     REP(i , n){  
  14.         int now = 0;  
  15.         REP(j , m) now += a[i][j];  
  16.         checkMin(ans , now);  
  17.     }  
  18.     REP(i , m){  
  19.         int now = 0;  
  20.         REP(j , n) now += a[j][i];  
  21.         checkMin(ans , now);  
  22.     }  
  23.     OT(ans);  
  24. }  
  25. int main(){  
  26.     getPrime();  
  27.     hash.clear();  
  28.     REP_1(i , prime[0]){  
  29.         hash.insert(prime[i]);  
  30.     }  
  31.     while(cin >> n >>m) solve();  
  32.   
  33. }  

For More:

如果每次的操作是把一行或者一列+1呢?

C  Secret

题意:

n个数(1 - n)分成k堆,每堆至少三个数,要求不能是等差数列。求一种构造。

解法:

乱搞,最简单的肯定是 1-k k-1 1-k 1 1 1 1  1
我的解法是 1-k k 1-(k-1) 1-k 1 1 1 1 1...

Code:

  1. const int N = 1e6 + 9;  
  2. int a[N] , n , k;  
  3. void solve(){  
  4.     if (k * 3 > n){  
  5.         puts("-1");  
  6.         return;  
  7.     }  
  8.     RST(a);  
  9.     int head = 0;  
  10.     REP_1(i , k){  
  11.         a[head++] = i;  
  12.     }  
  13.     REP_1(i , k){  
  14.         int now = i + 1;  
  15.         if (now == k + 1) now = 1;  
  16.         a[head++] = now;  
  17.     }  
  18.     REP_1(i , k - 1){  
  19.         a[head++] = i;  
  20.     }  
  21.     REP(i , n){  
  22.         if (i) printf(" ");  
  23.         if (a[i] == 0) printf("%d", k);  
  24.         else printf("%d" , a[i]);  
  25.     }  
  26.     puts("");  
  27.   
  28. }  
  29. int main(){  
  30.     while(cin >> n >> k) solve();  
  31. }  

D  Good Substrings

题意:

给一个最大长度是1500的字符串,问有多少个不同的子串能满足某些字母出现次数 <= k

解法:

暴力枚举 + 字符串暴力hash。一开始竟然挂了。。

Code:

  1. string str , v;  
  2. int k;  
  3. set<ULL> hash;  
  4. int main(){  
  5.     cin >> str >> v >> k;  
  6.     hash.clear();  
  7.     int len = str.length();  
  8.     REP_C(i , len){  
  9.         int bad = 0;  
  10.         ULL now = 0;  
  11.         FOR(j , i , len){  
  12.             bad += v[str[j] - 'a'] == '0';  
  13.             if (bad > k) break;  
  14.             now = now * 27 + str[j];  
  15.             hash.insert(now);  
  16.         }  
  17.     }  
  18.     OT(SZ(hash));  
  19. }  
刚看到了能卡掉字典序hash的blog,Mark:http://hi.baidu.com/sillycross/item/386b1e172389391fb98a1ab1

神数据:

[plain]  view plain copy print ?
  1. eoyirpkwgpvvwzaaaaaaaaaaaaaa  
  2. 11111111111111111111111111  
  3. 1  

E  Three Horses

题意:

给n个数,和上限m。代表有n张牌正反面分别是(1 , a[i])有三种操作:
1、(a,b)  -> (a + 1 , b + 1)
2、 a、b都是偶数 (a / 2 , b / 2)
3、(a , b) , (b , c) -> (a , c)
求能构造的(x , y) (x,y <= m)有多少个

解法:

观察 牌的差可以发现,操作1 差不变,操作2可以把差翻倍,操作3可以把差加减。于是乎只用枚举出GCD(a[i] - 1) 所有的,枚举约数那么所有的差就是约数的2^k倍

Code:

  1. VI divs;  
  2. int n , m;  
  3. int main(){  
  4.     RD(n , m);  
  5.     int d = 0;  
  6.     DO(n) d = GCD(RD() - 1 , d);  
  7.     for(int i = 1 ; i * i <= d ; ++i){  
  8.         if (d % i == 0){  
  9.             divs.PB(i);  
  10.             if (i * i != d) divs.PB(d / i);  
  11.         }  
  12.     }  
  13.     LL ans = 0;  
  14.     ECH(it , divs)  
  15.         if ((*it) & 1)  
  16.             for(int i = 0 ; ((LL)(*it) << i) < m ; ++i)  
  17.                 ans += m - ((LL)(*it) << i);  
  18.     OT(ans);  
  19. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值