我是靠谱客的博主 可靠星星,这篇文章主要介绍Fxx and game(搜索加剪枝或者单调队列加dp) Fxx and game,现在分享给大家,希望可以做个参考。

Fxx and game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1446    Accepted Submission(s): 378


Problem Description
Young theoretical computer scientist Fxx designed a game for his students.

In each game, you will get three integers  X,k,t .In each step, you can only do one of the following moves:

1.X=Xi(0<=i<=t) .

2. if  k|X,X=X/k .

Now Fxx wants you to tell him the minimum steps to make  X  become 1.
 

Input
In the first line, there is an integer  T(1T20)  indicating the number of test cases.

As for the following  T  lines, each line contains three integers  X,k,t(0t106,1X,k106)

For each text case,we assure that it's possible to make  X  become 1。
 

Output
For each test case, output the answer.
 

Sample Input
  
  
2 9 2 1 11 3 3
 

Sample Output
  
  
4 3
 
代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int a,b,c;
int flag;
int vis[1000010];
int scan()
{
    int res=0,flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else  if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+(ch-'0');
    return flag?-res:res;

}
//void out (int a)
//{
//    if(a<0)
//    {
//        putchar('-');
//        a=-a;
//    }
//    if(a>=10)
//    {
//        out(a/10);
//        putchar(a%10+'0');
//    }
//}
struct qq
{
    int x,y;
} q1,w1;
void  bfs(int a)
{
    q1.x=a;
    q1.y=0;
    queue<qq>w;
    while(!w.empty())
    {
        w.pop();
    }
    w.push(q1);
    while(!w.empty())
    {
        w1=w.front();
        if(w1.x==1)
        {
            flag=w1.y;
            return ;
        }
        w.pop();
        if(w1.x%b==0&&vis[w1.x/b]==0)
        {
            q1.x=w1.x/b;
            q1.y=w1.y+1;
            vis[w1.x/b]=1;
            w.push(q1);
        }
        int minn=min(c,w1.x-1);
        for(int i=minn; i>=1; i--)
        {

            if(w1.x-i>=1&&vis[w1.x-i]==0)
            {
                q1.x=w1.x-i;
                q1.y=w1.y+1;
                vis[w1.x-i]=1;
                w.push(q1);
            }
            else
                break;

        }
    }
}
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
//        int a,b,c;
        memset(vis,0,sizeof(vis));
        a=scan();
        b=scan(),c=scan();
//        scanf("%d%d%d",&a,&b,&c);
        if(b==1)
        {
            int ss=0;
            if((a-1)%c!=0)
                ss=1;
            printf("%dn",(a-1)/c+ss);
            continue;
        }
        else  if(c==0)
        {
            int sum=0;
            while(a!=1)
            {
                a/=b;
                sum++;
            }
            printf("%dn",sum);
            continue;
        }
        vis[a]=1;
        bfs(a);
//        out(flag);
//        printf("n");
        printf("%dn",flag);
    }
}
这里有一个剪枝很重要,就是遍历那个减得那个状态的时候,当发现小的元素已经存在的时候,后面就不必要再遍历了,直接跳出 来就好了!!!!!厉害厉害!!!!

最后

以上就是可靠星星最近收集整理的关于Fxx and game(搜索加剪枝或者单调队列加dp) Fxx and game的全部内容,更多相关Fxx内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部