bc#88
Find Q
这个题就是看在一个串里面有多少只有q的串;先对有n个连续的q的串里面有多少个q的串进行达标,发现:a[ n ] = n + (n -1) + (n -2) + ...... + 1;打表完成之后,然后对字符串进行遍历,一有连续的多少个的话就加起来;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef __int64 ll;
const int maxn = 100000 + 10;
ll a[maxn];
void Init()
{
a[1] = 1;
for(int i = 2; i <= maxn; i++)
{
a[i] = a[i - 1] + i;
}
}
int main()
{
Init();
int Tcase;
scanf("%d",&Tcase);
for(int ii = 1; ii <= Tcase ; ii ++)
{
char s[maxn];
scanf("%s",s);
int len = strlen(s);
// cout << s << " " << len << endl;
ll ans = 0;
for(int i = 0 ; i < len ; i ++)
{
if(s[i] == 'q')
{
for(int j = i ;j <= len ;j ++)
{
if(s[j] != 'q')
{
ans += a[j - i];
// cout << j - i <<endl;
i = j - 1;
break;
}
}
}
}
cout << ans <<endl;
}
return 0;
}
题意:看将这个数组分成平均的,看每种分法分成之后的每一组的个数;
思路:先用map记录数组中不同数的个数,然后对总的求gcd,代表这个这些数都能除,这是一种情况,然后其他情况就是找出他们的公因子的因数,然后进行操作;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
typedef __int64 ll;
const int maxn = 100000 + 10;
int gcd(int x,int y)
{
if(y == 0)
return x;
return gcd(y,x % y);
}
int main()
{
int Tcase;
scanf("%d", &Tcase);
for(int ii = 1; ii <= Tcase ;ii ++)
{
int n;
scanf("%d",&n);
map<int,int>m;
for(int i = 1; i <= n ; i++)
{
int x;
scanf("%d",&x);
m[x] ++;
}
map<int,int>::iterator it;
ll ans ;
for(it = m.begin() ; it != m.end() ; it ++)
{
if(it == m.begin())
{
ans = it -> second;
}
else
{
ans = gcd(ans ,it -> second);
}
}
int anss[maxn];
memset(anss, 0,sizeof(anss));
// cout << anss[0] <<endl;
int temp = 0;
for(it = m.begin() ; it != m.end() ; it ++)
{
anss[temp] += (it -> second) / ans;
}
// cout << anss[0] <<endl;
temp ++;
for(int i = 2 ; i <= ans ; i ++)
{
if(ans % i ==0)
{
anss[temp ++] = anss[0] * i;
}
}
for(int i =0 ;i < temp ; i ++)
{
if(i < temp - 1)
{
cout << anss[i] <<" ";
}
else cout << anss[i] << endl;
}
}
return 0;
}