国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商丘市| 武平县| 江津市| 韶关市| 天水市| 南开区| 德令哈市| 逊克县| 自贡市| 乃东县| 嘉义县| 拜城县| 凭祥市| 札达县| 当阳市| 黄骅市| 广汉市| 华亭县| 竹山县| 定西市| 定襄县| 东海县| 巨野县| 浦县| 麟游县| 富宁县| 台安县| 徐州市| 枣庄市| 遂平县| 含山县| 海盐县| 怀仁县| 谢通门县| 会昌县| 清镇市| 江孜县| 丘北县| 贺州市| 高要市| 渝中区|