USACO----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,3,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: 76875131
PROG: combo
LANG: C++11
*/

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
 {
    ofstream fout ("combo.out");
    ifstream fin ("combo.in");

    int iMax;
    fin>>iMax;
    if(iMax <= 5)    //当N不大于5时,所有组合均可以打开锁
      fout<< iMax * iMax * iMax<<endl;
    else
    {
       //N大于5时,组合数量 = 农民设置的密码及误差的所有可能性 + 锁匠设置的密码及误差的所有可能性 - 重复的数目
        vector<int> vecF(3), vecM(3), vecS(3);
        fin>>vecF[0]>>vecF[1]>>vecF[2]>>vecM[0]>>vecM[1]>>vecM[2];
        int iTotal = 5 * 5 * 5 + 5 * 5 * 5;
        for(int i = 0; i != 3; ++i)
        {
           int big = vecF[i], small = vecM[i];
           if(big < small) swap(big, small);
           int m = big - small < small + iMax - big ? big - small : small + iMax - big;    //循环的嘛,所以差值取不同方向计算中的值小的那一个
           if(m < 5) vecS[i] = 5 - m;    //重复的数目为5减去两个数值的差值,差值不小于5时就不会有重复
        }
        int same = 1;
        for(auto iter : vecS)
          same *= iter;    //三个重复数量相乘即为重复的可能性
        fout<<iTotal - same<<endl;
    }

    fin.close();
    fout.close();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值