Decomposition of the quality factor (linear sieve)

 

https://ac.nowcoder.com/acm/contest/923/B

I was a spicy chicken, but now I know linear sieve,

Ideas: for each of the factorial of a number, opening a number of one-dimensional array of each digital recording appears, the first one-dimensional difference had accumulated number of occurrences of each digital factorial of a number, and then use linear sieve, What is the smallest known prime factors of each number is in the process of comparison, we can compare descending, if found different numbers for the same number of times it appears, we can put this in the form of digital divide is the product of two numbers (minimum quality factor is known for each fixed number), if the same numbers do not have control of the last digits in the traversed for one to see whether each position is the same on it.

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int v[maxn],prime[maxn];
int a[maxn],b[maxn];
int da[maxn],db[maxn];
void primes(int n)
{
    memset(v,0,sizeof(v));
    int m=0;
    for(int i=2; i<=n; i++)
    {
        if(v[i]==0)
        {
            v[i]=i;
            prime[++m]=i;
        }
        for(int j=1; j<=m; j++)
        {
            if(prime[j]>v[i]||prime[j]>n/i) break;
            v[i*prime[j]]=prime[j];
        }
    }
}
int main()
{
    primes(100000);
    int t;
    scanf("%d",&t);
    for(int it=1; it<=t; it++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(da,0,sizeof(da));
        memset(db,0,sizeof(db));

        for(int i=1; i<=n; i++)
        {
            int t;
            scanf("%d",&t);
            if(t>1)
            {
                da[2]++;
                da[t+1]--;
            }
        }
        for(int i=1; i<=m; i++)
        {
            int t;
            scanf("%d",&t);
            if(t>1)
            {
                db[2]++;
                db[t+1]--;
            }
        }
        for(int i=2; i<=1e5; i++)
        {
            da[i]+=da[i-1];
            db[i]+=db[i-1];
            a[i]+=da[i];
            b[i]+=db[i];
        }
        for(int i=1e5; i>=2; i--)
        {
            if(a[i]!=b[i])
            {
                int zy1=i/v[i];
                int zy2=i/zy1;
                int ta=a[i];
                int tb=b[i];
                a[i]=b[i]=0;
                a[zy1]+=ta;
                a[zy2]+=ta;
                b[zy1]+=tb;
                b[zy2]+=tb;
            }
        }
        int flag=1;
        for(int i=2; i<=1e5; i++)
        {
            if(a[i]!=b[i])
            {
//            printf("%d %d\n",a[i],b[i]);
                flag=0;
                break;
            }
        }
        if(flag)
            printf("equal\n");
        else
            printf("unequal\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/dongdong25800/p/11067710.html