[HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin)

Description

Eric has a long rope whose length is N, now he wants to mark on the rope with different colors. The way he marks the rope is:
1. He will choose a color that hasn’t been used
2. He will choose a length L (N>L>1) and he defines the mark’s value equals L
3. From the head of the rope, after every L length, he marks on the rope (you can assume the mark’s length is 0 )
4.
When he chooses the length L in step 2, he has made sure that if he
marks with this length, the last mark will be at the tail of the rope
Eric
is a curious boy, he want to choose K kinds of marks. Every two of the
marks’ value are coprime(gcd(l1,l2)=1). Now Eric wants to know the max
K. After he chooses the max K kinds of marks, he wants to know the max
sum of these K kinds of marks’ values.
You can assume that Eric always can find at least one kind of length to mark on the rope.

Solution

质因数分解一下就好了,应该是道模板题

然而我忘记还有质因数只有一个的情况,L不能等于N啊

QvQ拍了好久都没找到错在哪,后来随手输了个1024发现事情有蹊跷…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
LL T,ans=0;
vector<LL>v;
map<LL,LL>num;
LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):a;
}
LL mul(LL a,LL b,LL p)
{
    LL res=0;
    while(b)
    {
        if(b&1)res=(res+a)%p;
        a=(a+a)%p;b>>=1;
    }
    return res;
}
LL pow(LL a,LL n,LL p)
{
    LL res=1;
    while(n)
    {
        if(n&1)res=mul(res,a,p);
        a=mul(a,a,p);n>>=1;
    }
    return res;
}
bool check(LL a,LL n,LL m,LL cnt)
{
    LL x=pow(a,m,n),y=x;
    for(int i=1;i<=cnt;i++)
    {
        x=mul(x,x,n);
        if(x==1&&y!=1&&y!=x-1)return 1;
        y=x;
    }
    return x!=1;
}
bool Miller_Rabin(LL n)
{
    if(n==2)return 1;
    if(n<=1||n&1==0)return 0;
    LL m=n-1,cnt=0;
    while(m&1==0)cnt++,m>>=1;
    for(int i=1;i<=7;i++)
    {
        if(check(rand()%(n-1)+1,n,m,cnt))
        return 0;
    }
    return 1;
}
LL rho(LL n,LL c)
{
    LL i=1,k=2,x=rand()%(n-1)+1,y=x,d;
    while(1)
    {
        x=(mul(x,x,n)+c)%n;
        d=x>y?gcd(x-y,n):gcd(y-x,n);
        if(d>1)return d;
        if(y==x)return n;
        if(i==k)y=x,k<<=1;
        i++;
    }
}
void solve(LL n)
{
    if(n==1)return;
    if(Miller_Rabin(n))
    {
        if(!num[n])
        {v.push_back(n),ans++,num[n]=1;}
        num[n]*=n;
        return;
    }
    LL p=n;
    while(p==n)p=rho(n,rand()%(n-1)+1);
    solve(p);solve(n/p);
}
int main()
{
    scanf("%lld",&T);
    while(T--)
    {
        LL n;
        scanf("%lld",&n);
        v.clear(),num.clear(),ans=0;
        solve(n);
        if(v[0]==n)ans--;
        printf("%lld ",ans);
        LL sum=0;
        for(int i=0;i<ans;i++)
        sum+=num[v[i]];
        if(ans==1)sum/=v[0];
        printf("%lld\n",sum);
    }
    return 0;
}
时间: 2024-10-14 04:01:47

[HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin)的相关文章

随机素数测试(Miller_Rabin算法)和求整数素因子(Pollard_rho算法)

POJ1811 给一个大数,判断是否是素数,如果不是素数,打印出它的最小质因数 随机素数测试(Miller_Rabin算法) 求整数素因子(Pollard_rho算法) 科技题 1 #include<cstdlib> 2 #include<cstdio> 3 const int maxn=10005; 4 const int S=20; 5 int tot; 6 long long n; 7 long long factor[maxn]; 8 long long muti_mod(

hdu 1201 18岁生日 (简单题)

18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18281    Accepted Submission(s): 5776 Problem Description Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他

hdu 1195 Open the Lock (bfs+优先队列)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4253    Accepted Submission(s): 1858 Problem Description Now an emergent task for you is to open a password lock. The password is c

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 1978 How many ways(记忆化)

Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标有的能量. 2.机器人只能向右或者向下走,并且每走一步消耗一单位能量. 3.机器人不能在原地停留. 4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量. 如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)

hdu 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10853    Accepted Submission(s): 6676 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? M

HDU 2553 N皇后问题(详细题解)

这是一道深搜题目!问题的关键是在剪枝. 下面我们对问题进行分析: 1.一行只能放一个皇后,所以我们一旦确定此处可以放皇后,那么该行就只能放一个皇后,下面的就不要再搜了. 2.每一列只能放一个皇后,所以我们下次搜索就不要再搜已经放过的皇后了. 3.斜的45°线也只能放一个. 综上如何才能最快速的确定一列和45°是否用过这个是个关键步骤,一旦此步骤确定我们就可以很快的进行搜索了. 我们用三个数组来保存他的每一个状态及(三个方向 ↑ ) 但是如果我们保存↑(每一列方向上的皇后)是非常容易保存的 但是保

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出