Helvetic Coding Contest 2018 D1

五分快三公式計算方法發表於2021-02-26

problem The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form .

To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!

Input The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) – the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.

Output Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).

Example input 4 (99+98)/97 (26+4)/10 (12+33)/15 (5+1)/7

output 1 2 2 1

Note In the sample testcase, the second and the third ship will both end up at the coordinate 3.

Note that this problem has only two versions – easy and hard.

solution 使用分數類+字串hash判重

code example

include

using namespace std; typedef long long ll; int debug_num=0;

define debug cout<<"debug "<<++debug_num<<" :"

unordered_map mp;

const int maxn=2e5+10;

ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b); }

struct Fraction{ ll num; ll den; Fraction (ll num=0,ll den=1){ if(den<0){ num=-num; den=-den; } assert(den != 0); ll g=gcd(abs(num),den); this->num=num/g; this->den=den/g; } Fraction operator + (const Fraction &o) const{ return Fraction(num*o.den+den*o.num,den*o.den); } Fraction operator - (const Fraction &o) const{ return Fraction(num*o.den-den*o.num,den*o.den); } Fraction operator * (const Fraction &o) const{ return Fraction(num*o.num,den*o.den); } Fraction operator / (const Fraction &o) const{ return Fraction(num*o.den,den*o.num); } bool operator < (const Fraction &o) const{ return num*o.den

int main() { //freopen("in.txt","r",stdin); //ios::sync_with_stdio(false); int m; cin>>m; //scanf("%d",&m); for(int i=0;i<m;++i){ ll a,b,c; //scanf("(%d+%d)/%d",&a,&b,&c); //cout<<a<<" "<<b<<" "<<c<<endl; string s; cin>>s; ll sum=0; int j; for(j=1;j'; tp=no[i].den; while(tp){//逆序就逆序吧... s+=tp%10+'0'; tp/=10; } mp[s]++; //cout<'; tp=no[i].den; while(tp){//逆序就逆序吧... s+=tp%10+'0'; tp/=10; } cout<

相關文章