标签:
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.
Sample Input |
Output for Sample Input |
2 10 2 12 2 |
Case 1: 1 Case 2: 2 |
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; const int maxn=1e6+5; bool vis[maxn+10]; int prim[maxn/10]; LL a,b; int t,cnt=0; void init() { int m=sqrt(maxn+0.5); for(int i=2;i*i<=maxn;i++) if(!vis[i]) for(int j=i*i;j<=maxn;j+=i) vis[j]=1; for(int i=2;i<=maxn;i++) if(!vis[i]) prim[cnt++]=i; } LL solve() { LL ans=1,temp=a; if(a/b<b) return 0; for(int i=0;i<cnt&&prim[i]*prim[i]<=a;i++) { int c=0; while(a%prim[i]==0) { c++; a/=prim[i]; } ans*=(c+1); } if(a>1) ans<<=1; ans>>=1;//有重复 for(int i=1;i<b;i++) if(temp%i==0) ans--; return ans; } int main() { int cas=1; init(); cin>>t; while(t--) { cin>>a>>b; cout<<"Case "<<cas++<<": "<<solve()<<endl; } return 0; }Light OJ 1236:
Find the result of the following code:
long long pairsFormLCM( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
for( int j = i; j <= n; j++ )
if( lcm(i, j) == n ) res++; //
lcm means least common multiple
return res;
}
A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).
For each case, print the case number and the value returned by the function ‘pairsFormLCM(n)‘.
Sample Input |
Output for Sample Input |
15 2 3 4 6 8 10 12 15 18 20 21 24 25 27 29 |
Case 1: 2 Case 2: 2 Case 3: 3 Case 4: 5 Case 5: 4 Case 6: 5 Case 7: 8 Case 8: 5 Case 9: 8 Case 10: 8 Case 11: 5 Case 12: 11 Case 13: 3 Case 14: 4 Case 15: 2 |
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; const int maxn=1e7+5; bool vis[maxn+10]; int prim[maxn/10]; int cnt=0; void init() { for(int i=2;i*i<=maxn;i++) if(!vis[i]) for(int j=i*i;j<=maxn;j+=i) vis[j]=1; for(int i=2;i<=maxn;i++) if(!vis[i]) prim[cnt++]=i; } LL t,n; int main() { init();int cas=1; scanf("%lld",&t); while(t--) { scanf("%lld",&n); LL ans=1; for(int i=0;i<cnt&&prim[i]*prim[i]<=n;i++) { if(n%prim[i]==0) { int c=0; while(n%prim[i]==0) { c++; n/=prim[i]; } ans*=(2*c+1); } } if(n>1) ans*=3; printf("Case %d: %d\n",cas++,(ans+1)/2); } return 0; }
Light OJ 1341 Aladdin and the Flying Carpet
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/43853391