原来字符串转数字可以
sscanf(string_name,interger_type,interger_name);
原来把带空格的字符串转成一系列数字可以直接插入到输入流中 我还是太Naive了
istringstream in(string);
不过为什么这是千分题啊
首先把答案为1 2的判掉
然后矩阵乘法加速 如果每次矩阵内的答案数量相同 那可以
O(1)
求
否则肯定长度为x的答案增加很快 那我们直接递推就可以了
#define FORALL for (int i=0; i<n; i++) for (int j=0; j<n; j++)
#define ll long long
struct matrix{
ll a[11][11],n,m;
ll sum;
matrix(){memset(a,0,sizeof(a)),n=m=0,sum=0;};
matrix operator *(matrix & b) {
matrix tmp;
for (int i=0; i<n; i++)
for (int j=0; j<b.m; j++) {
for (int k=0; k<m; k++)
tmp.a[i][j]+=a[i][k]*b.a[k][j];
tmp.sum+=tmp.a[i][j];
}
tmp.n=n,tmp.m=b.m;
return tmp;
}
void out() {
puts("--------------");
cout << n << " " << m << endl;
for (int i=0; i<n; i++,puts(""))
for (int j=0; j<m; j++) cout << a[i][j] << " ";
puts("***--------***");
}
};
class Flags {
public:
long long numStripes(string nn, vector<string> band) {
long long tot=0;
int n=band.size(),x,tmp=0;
sscanf(nn.c_str(),"%lld",&tot);
tot-=n;
if (tot<=0) return 1;
matrix ban;
FORALL ban.a[i][j]=1;
for (int i=0; i<n; i++) {
istringstream in(band[i]);
while (in >> x) ban.a[x][i]=ban.a[i][x]=0;
}
ban.n=ban.m=n;
FORALL if (ban.a[i][j]) tmp++;
ban.sum=tmp;
if (tmp>=tot) return 2;
matrix ban2=ban*ban;
if (ban2.sum==ban.sum) {
ll cnt=tot/ban.sum;
while (cnt*ban.sum<tot) cnt++;
return cnt+1;
}
int ans=3;
ll s=ban.sum+ban2.sum;
while (s<tot) {
ban2=ban2*ban;
s+=ban2.sum;
ans++;
}
return ans;
}
};