【乱搞】USACO-cha1-sec1.3 Preface Numbering

Combination Lock

Farmer John's cows keep escaping from his farm and causing mischief. To try and prevent them from leaving, he purchases a fancy combination lock to keep his cows from opening the pasture gate.

Knowing that his cows are quite clever, Farmer John wants to make sure they cannot easily open the lock by simply trying many different combinations. The lock has three dials, each numbered 1..N (1 <= N <= 100), where 1 and N are adjacent since the dials are circular. There are two combinations that open the lock, one set by Farmer John, and also a "master" combination set by the lock maker. The lock has a small tolerance for error, however, so it will open even if the numbers on the dials are each within at most 2 positions of a valid combination.

For example, if Farmer John's combination is (1,2,3) and the master combination is (4,5,6), the lock will open if its dials are set to (1,N,5) (since this is close enough to Farmer John's combination) or to (2,4,8) (since this is close enough to the master combination). Note that (1,5,6) would not open the lock, since it is not close enough to any one single combination.

Given Farmer John's combination and the master combination, please determine the number of distinct settings for the dials that will open the lock. Order matters, so the setting (1,2,3) is distinct from (3,2,1).

PROGRAM NAME: combo

INPUT FORMAT:

Line 1:The integer N.
Line 2:Three space-separated integers, specifying Farmer John's combination.
Line 3:Three space-separated integers, specifying the master combination (possibly the same as Farmer John's combination).

SAMPLE INPUT (file combo.in):

50
1 2 3
5 6 7

INPUT DETAILS:

Each dial is numbered 1..50. Farmer John's combination is (1,2,3), and the master combination is (5,6,7).

OUTPUT FORMAT:

Line 1:The number of distinct dial settings that will open the lock.

SAMPLE OUTPUT (file combo.out):

249
————————————————————————————————————————————————————————————————————————————————————————————————
思路:纯暴力就可以了。我的做法应该是某种推公式。
代码如下:
/*
ID: j.sure.1
PROG: combo
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111;
int five;
int a[N], b[3], n;
bool vis[2][3][N];

int main()
{
	freopen("combo.in", "r", stdin);
	freopen("combo.out", "w", stdout);
	while(scanf("%d", &n)!=EOF) {
        memset(vis, 0, sizeof(vis));
        memset(b, 0, sizeof(b));
        int x;
        for(int i = 0; i < 2; i++) {
            for(int j = 0; j < 3; j++) {
                scanf("%d", &x);
                five = n > 5 ? 5 : n;//容错范围是5个数字以内,但是如果N不够5,就是N了
                int k = (n+x-2) % n;//起点
                while(five--) {//从起点开始扫five个数
                    if(k > n-1) k = 0;
                    vis[i][j][k] = 1;
                    k++;
                }
            }
        }
        for(int j = 0; j < 3; j++) {
            for(int k = 0; k < n; k++) {
                if(vis[0][j][k] && vis[1][j][k]) 
                    b[j]++;//这是重复的方案
            }
        }
        five = n > 5 ? 5 : n;
        five = five * five * five * 2;//最大的可能不会超过250
        printf("%d\n", five - b[0]*b[1]*b[2]);
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值