国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Aladdin and the Flying Carpet [整數分解]

2019-11-08 20:02:42
字體:
來源:轉載
供稿:網友

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

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.

Output

For each case, PRint the case number and the number of possible carpets.

Sample Input

2 10 2 12 2

Sample Output

Case 1: 1 Case 2: 2

解題報告

n=pa11pa22...pakk 那么,約數個數 ∑d|n1=(a1+1)(a2+1)...(ak+1)

這個題在卡質因數分解,得先打個質數表以減少分解時的冗余操作

代碼

超時代碼

#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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤岗市| 房山区| 伽师县| 桂阳县| 察隅县| 绵竹市| 新兴县| 云林县| 邻水| 根河市| 云和县| 彭州市| 林口县| 平舆县| 沾化县| 婺源县| 富裕县| 新竹市| 西昌市| 丰县| 百色市| 泉州市| 抚州市| 普兰县| 正定县| 湟中县| 福清市| 长葛市| 龙陵县| 娱乐| 汾西县| 新泰市| 藁城市| 馆陶县| 沐川县| 大英县| 英德市| 祁连县| 潞西市| 秦安县| 房产|