It’s said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery.
Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised himself as Aladdin’s uncle, found a strange magical flying carpet at the entrance. There were some strange creatures guarding the entrance of the cave. Aladdin could run, but he knew that there was a high chance of getting caught. So, he decided to use the magical flying carpet. The carpet was rectangular shaped, but not square shaped. Aladdin took the carpet and with the help of it he passed the entrance.
Now you are given the area of the carpet and the length of the minimum possible side of the carpet, your task is to find how many types of carpets are possible. For example, the area of the carpet 12, and the minimum possible side of the carpet is 2, then there can be two types of carpets and their sides are: {2, 6} and {3, 4}.
Input starts with an integer T (≤ 4000), denoting the number of test cases.
Each case starts with a line containing two integers: a b (1 ≤ b ≤ a ≤ 1012) where a denotes the area of the carpet and b denotes the minimum possible side of the carpet.
For each case, PRint the case number and the number of possible carpets.
2 10 2 12 2
Case 1: 1 Case 2: 2
這個題在卡質因數分解,得先打個質數表以減少分解時的冗余操作
超時代碼
#include<stdio.h>#include<string.h>#include<algorithm>#define LL __int64LL p[64],s,k;int N,ans;void dfs(LL a,LL b,int cnt){ if(a>=k) ans++; for(int i=cnt;i<N;i++){ if(b%p[i]==0){ LL tmp=a*p[i],tmpb=b/p[i]; if(tmpb>=k&&tmp<tmpb){ dfs(tmp,tmpb,i); } } }}void solve(LL n){ ans=N=0; for(LL i=2;i*i<n;i++) if(n%i==0){ p[N++]=i; while(n%i==0) n/=i; } if(n>1) p[N++]=n; dfs(1,s,0);}int main(){ int T; scanf("%d",&T); for(int t=1;t<=T;t++){ scanf("%lld%lld",&s,&k); solve(s); printf("Case %d: %d/n",t,ans); } return 0;}方法一,對上面代碼優化后
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#define MAX_N 1000100#define LL __int64#define p_i prime[i]LL p[64],s,k;int N,ans;bool vis[MAX_N];int prime[MAX_N],pN;void init(){ pN=0; for(int i=2;i<MAX_N;i++) if(!vis[i]){ prime[pN++]=i; for(LL k=(LL)i*i;k<MAX_N;k+=i) vis[k]=true; }}void dfs(LL A,LL B,int cnt){ if(A>=k) ans++; for(int i=cnt;i<N;i++){ if(B%p[i]==0){ LL a=A*p[i],b=B/p[i]; if(b>=k&&a<b){ dfs(a,b,i); } } }}void solve(LL n){ ans=N=0; for(int i=0;(LL)p_i*p_i<=n;i++) if(n%p_i==0){ p[N++]=p_i; while(n%p_i==0) n/=p_i; } if(n>1) p[N++]=n; dfs(1,s,0);}int main(){// FILE *fp;// fp=fopen("out.txt","wt+"); int T;init(); scanf("%d",&T); for(int t=1;t<=T;t++){ scanf("%lld%lld",&s,&k); if(s==1&&k==1) ans=0; //特殊情況 else solve(s); printf("Case %d: %d/n",t,ans);// fprintf(fp,"Case %d: %d/n",t,ans); } return 0;}方法二,使用上面提到的那個公式
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#define MAX_N 1000100#define LL __int64#define p_i prime[i]LL p[64],s,k;int N,ans;bool vis[MAX_N];int prime[MAX_N],pN;void init(){ pN=0; for(int i=2;i<MAX_N;i++) if(!vis[i]){ prime[pN++]=i; for(LL k=(LL)i*i;k<MAX_N;k+=i) vis[k]=true; }}void dfs(LL A,LL B,int cnt){ ans--; for(int i=cnt;i<N;i++){ if(B%p[i]==0){ LL a=A*p[i],b=B/p[i]; if(a<b&&a<k){ dfs(a,b,i); } } }}void solve(LL n){ ans=1;N=0; for(int i=0;(long long)p_i*p_i<=n;i++) if(n%p_i==0){ p[N++]=p_i; int tmp=1; while(n%p_i==0) n/=p_i,tmp++; ans*=tmp; } if(n>1) p[N++]=n,ans*=2;// LL tmp=sqrt(s);// if(tmp*tmp==s) ans++; ans/=2; dfs(1,s,0); if(k==1) ans++;}int main(){ int T;init(); scanf("%d",&T); for(int t=1;t<=T;t++){ scanf("%lld%lld",&s,&k); solve(s); printf("Case %d: %d/n",t,ans); } return 0;}新聞熱點
疑難解答