每周一赛(E题,广搜求方案)

Description

In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This xis an integer number which is a prime factor of A (please note that 1 and A are not being considered as a factor of A). Now, your task is to find the minimum number of transformations required to transform s to another integer number t.

Input

Input starts with an integer T (≤ 500), denoting the number of test cases.

Each case contains two integers: s (1 ≤ s ≤ 100) and t (1 ≤ t ≤ 1000).

Output

For each case, print the case number and the minimum number of transformations needed. If it‘s impossible, then print -1.

Sample Input

2

6 12

6 13

Sample Output

Case 1: 2

Case 2: -1

题意:

例如:6+3=9,9+3=12加了两次

6+3=9,9+3=12,12的质因数只有2,3所以这种方案不行

6+2=8,8+2=10,10的质因数只有2,5所以不行

所以例二输出-1

利用搜索的方法,每次都枚举当前数的所有质因数,而且这里不需要标记,直到当前记录值等于目标值,这时也不要返回,用一个开始赋值很大的数来不断地更新最小值。

这么一来的话,就真的是每种情况都得枚举到了,这是会超时的!虽然我特意舍弃DFS而用了BFS还是不能幸免~~~~~

所以要进行优化,用一个开始赋值非常大的数组,然后每次记录当前入队列的节点他的当前值是什么,记下他的当前走了几步,后面每次当一个节点进队列时,就可以判断一下

他当前的步数是否小于以前走过的,如果小于就入队列,不小于就不进,这样就减少了很多毫无意义的尝试了

最后不得不说一句,做质因数标记那个数组在程序输入之前自动做好就行了,也花不了多少时间,而我竟然多次一举,去写了个辅助程序........................

#include"iostream"
#include"algorithm"
#include"cstring"
#include"cstdio"
#include"queue"
using namespace std;
int book[1010];
int mark[1010];
struct node
{
int as;
int step;
};

const int maxn=1000000000;
int ans=1000000000;
int flag;
int c;
int a;
int b;
int step=0;

void BFS()
{
	memset(mark,0x3f,sizeof(mark));
	queue<struct node> que;
	struct node cc,e,t;
	cc.as=a;
	cc.step=0;
	que.push(cc);
   while(!que.empty())
   {
	   e=que.front();
	   que.pop();
	   if(e.as==b)
	   {
          if(ans>e.step) ans=e.step;
	   }
      for(int i=2;i<e.as;i++)
      {
          if(e.as%i) continue;
          if(book[i]!=1) continue;
         //cout<<"iqian"<<i<<endl;
		//  cout<<i<<endl;
		  if(mark[e.as+i]>e.step+1)
		  {
		  t=e;
		  t.as+=i;
		  if(t.as>b) continue;
		  t.step++;
		  mark[t.as]=t.step;
		  que.push(t);
		  }
      }
   }
}

int main()
{
    int n,f;
book[1]=1;
book[2]=1;
book[3]=1;
book[5]=1;
book[7]=1;
book[11]=1;
book[13]=1;
book[17]=1;
book[19]=1;
book[23]=1;
book[29]=1;
book[31]=1;
book[37]=1;
book[41]=1;
book[43]=1;
book[47]=1;
book[53]=1;
book[59]=1;
book[61]=1;
book[67]=1;
book[71]=1;
book[73]=1;
book[79]=1;
book[83]=1;
book[89]=1;
book[97]=1;
book[101]=1;
book[103]=1;
book[107]=1;
book[109]=1;
book[113]=1;
book[127]=1;
book[131]=1;
book[137]=1;
book[139]=1;
book[149]=1;
book[151]=1;
book[157]=1;
book[163]=1;
book[167]=1;
book[173]=1;
book[179]=1;
book[181]=1;
book[191]=1;
book[193]=1;
book[197]=1;
book[199]=1;
book[211]=1;
book[223]=1;
book[227]=1;
book[229]=1;
book[233]=1;
book[239]=1;
book[241]=1;
book[251]=1;
book[257]=1;
book[263]=1;
book[269]=1;
book[271]=1;
book[277]=1;
book[281]=1;
book[283]=1;
book[293]=1;
book[307]=1;
book[311]=1;
book[313]=1;
book[317]=1;
book[331]=1;
book[337]=1;
book[347]=1;
book[349]=1;
book[353]=1;
book[359]=1;
book[367]=1;
book[373]=1;
book[379]=1;
book[383]=1;
book[389]=1;
book[397]=1;
book[401]=1;
book[409]=1;
book[419]=1;
book[421]=1;
book[431]=1;
book[433]=1;
book[439]=1;
book[443]=1;
book[449]=1;
book[457]=1;
book[461]=1;
book[463]=1;
book[467]=1;
book[479]=1;
book[487]=1;
book[491]=1;
book[499]=1;
book[503]=1;
book[509]=1;
book[521]=1;
book[523]=1;
book[541]=1;
book[547]=1;
book[557]=1;
book[563]=1;
book[569]=1;
book[571]=1;
book[577]=1;
book[587]=1;
book[593]=1;
book[599]=1;
book[601]=1;
book[607]=1;
book[613]=1;
book[617]=1;
book[619]=1;
book[631]=1;
book[641]=1;
book[643]=1;
book[647]=1;
book[653]=1;
book[659]=1;
book[661]=1;
book[673]=1;
book[677]=1;
book[683]=1;
book[691]=1;
book[701]=1;
book[709]=1;
book[719]=1;
book[727]=1;
book[733]=1;
book[739]=1;
book[743]=1;
book[751]=1;
book[757]=1;
book[761]=1;
book[769]=1;
book[773]=1;
book[787]=1;
book[797]=1;
book[809]=1;
book[811]=1;
book[821]=1;
book[823]=1;
book[827]=1;
book[829]=1;
book[839]=1;
book[853]=1;
book[857]=1;
book[859]=1;
book[863]=1;
book[877]=1;
book[881]=1;
book[883]=1;
book[887]=1;
book[907]=1;
book[911]=1;
book[919]=1;
book[929]=1;
book[937]=1;
book[941]=1;
book[947]=1;
book[953]=1;
book[967]=1;
book[971]=1;
book[977]=1;
book[983]=1;
book[991]=1;
book[997]=1;
    cin>>n;
    f=1;
    while(n--)
    {

      cin>>a>>b;
      c=b-a;
      if(c<0) cout<<"Case "<<f++<<": "<<-1<<endl;
      else
      {
          BFS();
      if(ans!=1000000000) cout<<"Case "<<f++<<": "<<ans<<endl;
      else cout<<"Case "<<f++<<": "<<-1<<endl;
	  ans=1000000000;
	  }
    }
return 0;
}
时间: 2024-08-11 05:34:59

每周一赛(E题,广搜求方案)的相关文章

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 Alice and Bob Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*....

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

CSU 1425 NUDT校赛 I题 Prime Summation

这个题本来有希望在比赛里面出了的 当时也想着用递推 因为后面的数明显是由前面的推过来的 但是在计算的时候 因为判重的问题 ...很无语.我打算用一个tot[i]来存i的总种树,tot[i]+=tot[j]//j为可以由j推到i的一系列数,但这样是不对的,会产生大量重复计算... 看了下标程才发现要用二维来计算出种类总数,f[i][j]+=sum(f[i-j][k]) 表示在推i数的时候,第一个素数为j的种类数,注意j一定为素数,而且k不能大于j...标程里面处理的比较简练,就学了下他的写法. 至

sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)

Pixel density Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pixel density is a measurement of the resolution of devices in various contexts; typically computer displays, image scanners, and digital camera image s

sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)

Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game named "Fruit Ninja"?Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based Android devices) is a video game de

HDUOJ-------2493Timer(数学 2008北京现场赛H题)

Timer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 445    Accepted Submission(s): 90 Problem Description Recently, some archaeologists discovered an ancient relic on a small island in the Pa