LightOJ1236 Pairs Forming LCM (Unique decomposition theorem)

Topic link

Title

Use a more efficient method to calculate the result of the program shown in the figure, requiring i<=j and LCM(i,j)=n, find the logarithm that satisfies the condition.

Ideas

According to lcm(a,b)=p1^max(a1,b1)×p2 ^ max(a2,b2)×…×pn ^ max(an,bn), we can know that when ai=ei, bi can be taken as [0,ei] Any number in the range, the same is true when bi=ei, there are 2*(ei+1) kinds of results. But when the three are the same, there will be duplication, so it needs to be subtracted. In all the remaining cases, except for (n, n), the others appear twice, so add one and divide by two to get the result.

Code

#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int P=139;
const int mod=998244353;
const int maxn=1e7+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
bool vis[maxn];
ll n;
int t,ans,cnt,prime[1000005];
void eul(ll n)
{
    
    
	cnt=0;
	for(ll i=2;i<=n;i++)
	{
    
    
		if(!vis[i])
			prime[++cnt]=i;
		for(ll j=1;j<=cnt&&i*prime[j]<=n;j++)
		{
    
    
			vis[i*prime[j]]=1;
			if(i%prime[j]==0)
				break;
		}
	}
}
int main()
{
    
    
	eul(maxn);
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
    
    
    	ans=1;
    	scanf("%lld",&n);
    	for(int i=1;i<=cnt&&prime[i]*prime[i]<=n;i++)
    	{
    
    
    		if(n%prime[i]==0)
    		{
    
    
    			int tmp=0;
    			while(n%prime[i]==0)
    			{
    
    
    				n/=prime[i];
    				tmp++;
				}
				ans*=(2*tmp+1);
			}
		}
		if(n>1)
			ans*=(2*1+1);
		printf("Case %d: %d\n",ca,(ans+1)/2);
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/WTMDNM_/article/details/108725924