Abelian Period

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值