Problem Statement | |||||||||||||
|
NOTE: This problem statement contains superscripts that may not display properly if viewed outside of the applet. You are given two geometric progressions S1 = (b1*q1i, 0 <= i <= n1-1) and S2 = (b2*q2i, 0 <= i <= n2-1). Return the number of distinct integers that belong to at least one of these progressions. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
| - | b1 and b2 will each be between 0 and 500,000,000, inclusive. | ||||||||||||
| - | q1 and q2 will each be between 0 and 500,000,000, inclusive. | ||||||||||||
| - | n1 and n2 will each be between 1 and 100,500, inclusive. | ||||||||||||
Examples | |||||||||||||
| 0) | |||||||||||||
| |||||||||||||
| 1) | |||||||||||||
| |||||||||||||
| 2) | |||||||||||||
| |||||||||||||
| 3) | |||||||||||||
| |||||||||||||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
【题解】
分解质因数后判断究竟是否相同。
只需以为枚举。
【代码】
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int a[100][3],b[100][3],t[100][3];
class GeometricProgressions
{
public:
int count(int, int, int, int, int, int);
};
void prime(int a[][3],int &tot,int x,int y)
{
tot=0;
int i;
for (i=2;i*i<=max(x,y);i++)
{
if (x<=1 && y<=1) return;
if (x && x%i==0)
{
a[++tot][0]=i;
while (x%i==0)
{
x/=i;
a[tot][1]++;
}
}
if (y && y%i==0)
{
if (a[tot][0]!=i) a[++tot][0]=i;
while (y%i==0)
{
y/=i;
a[tot][2]++;
}
}
}
if (x<y)
{
if (x!=1)
a[++tot][0]=x,a[tot][1]=1;
if (y!=1)
{
if (a[tot][0]!=y) a[++tot][0]=y;
a[tot][2]=1;
}
}
else {
if (y!=1)
a[++tot][0]=y,a[tot][2]=1;
if (x!=1)
{
if (a[tot][0]!=x) a[++tot][0]=x;
a[tot][1]=1;
}
}
}
int GeometricProgressions::count(int b1, int q1, int n1, int b2, int q2, int n2)
{
int i,j,t1,t2,ans,tt;
bool ff;
if (b2==0 || q2<=1)
{
swap(b1,b2);swap(q1,q2);swap(n1,n2);
}
if (b1==0 || q1<=1)
{
set<long long> v;
v.insert(b1);
if (n1>0) v.insert(b1*q1);
long long cur=q2;
for (i=0;i<n2;i++)
{
v.insert(b2*cur);
cur*=q2;
if (cur>500000000)
return v.size()+n2-i-1;
}
return v.size();
}
prime(a,t1,q1,b1);
prime(b,t2,q2,b2);
ans=n1+n2;
if (t1!=t2) return ans;
for (i=1;i<=t1;i++)
if (a[i][0]!=b[i][0]) return ans;
if (b[1][1]==0)
{
swap(n1,n2);
memcpy(t,a,sizeof(a));
memcpy(a,b,sizeof(b));
memcpy(b,t,sizeof(t));
}
for (i=0;i<n1;i++)
{
tt=a[1][1]*i+a[1][2]-b[1][2];
if (tt%b[1][1]!=0) continue;
if (tt<0) continue;
tt/=b[1][1];
if (tt>=n2) continue;
ff=true;
for (j=2;j<=t1;j++)
if (a[j][1]*i+a[j][2]!=b[j][1]*tt+b[j][2])
{
ff=false;
break;
}
if (ff) ans--;
}
return ans;
}
int main()
{
GeometricProgressions a;
cout << a.count(3,4,100500,48,1024,1000);
}
本文介绍了一种算法,用于计算两个几何级数中不同整数的数量。通过质因数分解并比较来确定两个级数间的交集大小。
561

被折叠的 条评论
为什么被折叠?



