如何解决3356中超时的问题?

lawson edited 4 年,10 月前

如何解决超时的问题?

#include<iostream>
using namespace std;

int main(){
 int q,n;
 int count=0;
 int flag = 0;
 cin >> q >> n;
 if(q==1){
  for(int i = 1;i<=n;i++){
   if(n%i==0) count++;
  }
//  cout << count << endl;
 }
 else{
  for(int a = 1;a<=n;a++)
  {
   for(int b = 1;b <= n;b++)
   {
    for(int x = 1; x<=n;x++)
    {
	for(int y = 1;y<=n ;y++)
	{
	 if(a*x+b*y == n)
	 {	  
//	  cout << "a = " << a<<",b="<<b<< ",x="<<x<<",y="<<y<<endl;    
	  flag = 1;//标识这个(a,b)对已经完成使命
	  count++;
	  break;	  
	 }
	 else if(a*x+b*y > n) break;//直接跳出循环
	}
	if(flag == 1) {flag = 0;break;}
    }
   }
  }
} cout <<count << endl;
 
}

Comments