Codeforces Round #213 (Div. 2)(矩陣)
剛開始題意理解錯了,wa了一次
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int N,K,a;
int vis[10];
bool is_good(int m)
{
memset(vis,0,sizeof(vis));
while(m>0)
{
int x=m%10;
vis[x]++;
m/=10;
}
for(int i=0;i<=K;i++)
if(!vis[i]) return false;
return true;
}
int main()
{
scanf("%d%d",&N,&K);
int ans=0;
while(N--)
{
scanf("%d",&a);
if(is_good(a)) ans++;
}
cout<<ans<<endl;
return 0;
}
B. The Fibonacci Segment
求最長的斐波那契數列
遍歷一遍,如果滿足則更新最大值,並儲存下到當前的位置
#include<iostream>
#include<cstdio>
using namespace std;
int n;
int a[100010];
void solve()
{
int ans=2,i=3,x=1;
while(i<=n)
{
if(a[i]!=a[i-1]+a[i-2])
{
x=i-1;
i++;
continue;
}
ans=max(ans,i-x+1);
i++;
}
cout<<ans<<endl;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
if(n<=2)
{
cout<<n<<endl;
return 0;
}
solve();
return 0;
}
C. Matrix題意:給你一個a,和一串數字s。讓你求由b[i,j] = s[i]*s[j],構成的陣列矩陣中和等於a的子矩陣的個數。
觀察一下,矩陣其實很特殊。可以提取出每行的係數。
a1*(a1,a2,..,an)
a2*(a1,a2,..,an)
an*(a1,a2,..,an)
用sum[i]表示數列a的前i項和。
那麼任意一個子矩陣和可以表示為(sum[col1]-sum[col0])*(sum[row1]-sum[row0]) (col0<y<=col1,row0<x<=row1)
然後n^2列舉統計就好了,注意一些細節
考慮一個矩形(x, y, z, t)的sum和等於 (s[x] + s[x+1] + ... +s[y]) * (s[z] + s[z+1] + ... s[t]),所以處理出所有子序列的和即可,有些小細節要注意比如說0。
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
LL hash[36010],sum[4040];
int a;
string s;
int main()
{
cin>>a>>s;
int len=s.size();
memset(sum,0,sizeof(sum));
memset(hash,0,sizeof(hash));
for(int i=0;i<len;i++)
sum[i+1]=sum[i]+s[i]-'0';
for(int i=1;i<=len;i++)
for(int j=0;j<i;j++)
hash[sum[i]-sum[j]]++;
LL cnt=0L;
if(a==0)
{
for(int i=0;i<=sum[len];i++)
cnt+=hash[0]*hash[i];
}
for(int i=1;i<=sum[len];i++)
if(a%i==0&&a/i<=sum[len])
cnt+=hash[a/i]*hash[i];
cout<<cnt<<endl;
return 0;
}
相關文章
- Codeforces Round #213 (Div. 2) A. Good NumberGo
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)
- Codeforces Round #173 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round 945 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 965 (Div. 2)