华为笔试部分题解。。
1.题意:问到最后一个的最短步数,要求第一步不能超过len/2
考虑到第一步不能超过len/2,我就直接用暴力dfs了(没想到更好的)。。
还有一个点就是它需要恰好到达最后一个,不能超过(leetcode某题是可以超过)
AC代码:
#笔试题目##华为##题解##include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#pragma warning(disable:4996)
#define ll long long
using namespace std;
vector<int>anss;
vector<int> a;
void dfs(int pos,int dep) {
if (pos > a.size() - 1)
return ;
if (pos == a.size() - 1) {
anss.push_back(dep);
return;
}
int mxlen;
if (pos == 0) {
mxlen = a.size() / 2;
for (int i = 1; i < mxlen; ++i) {
dfs(i, dep + 1);
}
}else {
mxlen = a.size();
dfs(pos + a[pos], dep + 1);
}
}
int main() {
anss.clear();
a.clear();
int i = 0;
do {
cin >> i;
a.push_back(i);
} while (getchar() != '\n');
dfs(0, 0);
sort(anss.begin(), anss.end());
if (anss.size() == 0)
cout << -1 << endl;
else
cout << anss[0] << endl;
return 0;
}
2. Polya定理。。。好像是裸题,但是一开始没过,因为没考虑到mod,后面加了一个逆元就过了。。。代码就不贴了(用的模板) 
