EOJ Monthly 2017.12 题解 3449. 唐纳德和他的数学老师

题目链接:http://acm.ecnu.edu.cn/problem/3449/

Time limit per test: 1.0 seconds

Memory limit: 256 megabytes

唐纳德是一个数学天才。有一天,他的数学老师决定为难一下他。他跟唐纳德说:「现在我们来玩一个游戏。这个游戏总共 n 轮,每一轮我都会给你一个数(第 i 轮给出的数是 ai )。你每次要回答一个数,是我给出的这个数的质因数,并且你说出的数不能重复。」

因为数学老师是刻意为难,所以这个游戏很有可能不可能进行到最后。但是聪明的数学老师早就已经知道这个游戏最多能进行几轮了。现在他把问题抛给了你,想看看你知不知道。

注意, 1 不是质数。

Input

输入具有如下形式:

na1 a2  an

第一行一个整数 n ( 1n3 000 )。

第二行 n 个整数用空格隔开, a1,a2,,an ( 2ai106 )。

Output

输出游戏最多能进行几轮。

Examples

Input
3
7 6 3
Output
3
Input
5
2 2 2 2 2
Output
1

题解:

二分图最大匹配问题,对左边的n轮和右边的质因数连边。因为游戏必须一轮一轮进行下去,所有,当有一轮不匹配时就必须要退出了。

求质因数用质因数分解。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;
int n,a[3030],tot,q[3030];
int pre[1030000]; //质数最大有可能达到1e6
struct edge
{
    int to,next;
}e[6000000]; //边的数量可能达到6e6
int head[10000],cnt,vis[1030000]; 
void add_edge(int u,int v)
{
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
bool match(int x)
{
    for(int i=head[x];i!=-1;i=e[i].next)if(!vis[e[i].to])
    {
        vis[e[i].to]=1;
        if(pre[e[i].to]==-1 || match(pre[e[i].to]))
        {
            pre[e[i].to]=x;
            return true;
        }
    }
    return false;
}
void factor(int t)
{
    tot=0;
    int now=t;
    int tmp=(int)(sqrt(double(t))+1);
    for(int i=2;i<=tmp;i++)
    {
        if(now%i==0)
        {
            a[++tot]=i;
        }
        while(now%i==0)
        {
            now/=i;
        }
    }
    if(now!=1)
        a[++tot]=now;
}

int main()
{
    while(scanf("%d",&n)==1)
    {
        int ans=0;
        memset(pre,-1,sizeof(pre));
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        cnt=-1;
        for(int i=1;i<=n;i++)
            scanf("%d",&q[i]);
        for(int i=1;i<=n;i++)
        {
            int t=q[i];
            factor(t);
            for(int j=1;j<=tot;j++)
            {
                add_edge(2*i+2,a[j]); //左边的顶点编号不能用质数
            }
            memset(vis,0,sizeof(vis));
            if(match(2*i+2)) //左边的顶点编号不能用质数
                ans++;
            else
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}



代码:代码:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值