Bzoj2749 [haoi2012] Aliens

Source: Internet
Author: User

Number theory is a good question !!!

First, the formula given by the question, for prime number x> 2, Phi (x) will become a lot 2... while PHI (2) = 1

In YY, we can find the number of 2 resulting from the decomposition of each prime number, and the number of 2 is ans.

So we make f [I] to indicate that I is decomposed into several 2: This process is similar to the prime screening method.

I is a prime number, F [I] = f [I-1]; otherwise, f [I * prime [J] = f [I] + F [prime [J] (<-- this is the legendary pure O (n) linear screening)

In addition, if n is an odd number at the beginning of a period, ANS must + 1 because the first step is required to change to 2.

 1 /************************************************************** 2     Problem: 2749 3     User: rausen 4     Language: C++ 5     Result: Accepted 6     Time:256 ms 7     Memory:1704 kb 8 ****************************************************************/ 9  10 #include <cstdio>11 #include <cmath>12 #include <algorithm>13  14 using namespace std;15 typedef long long LL;16 const int Maxn = 100005;17 LL f[Maxn + 10], ans, del;18 int T, P, Q, m, p[5000], cnt;19 bool x[Maxn + 10];20  21 void pre_work(){22     int t;23     f[1] = 1;24     for (int i = 2; i <= Maxn; ++i){25         if (!x[i])26             p[++cnt] = i, f[i] = f[i - 1];27         for (int j = 1; j <= cnt && i * p[j] <= Maxn; ++j){28             t = i * p[j];29             x[t] = 1;30             f[t] = f[i] + f[p[j]];31             if (!(i % p[j])) break;32         }33     }34 }35  36 int main(){ 37     scanf("%d", &T);38     pre_work();39     while (T--){40         scanf("%d", &m);41         ans = 0, del = 1;42         for (int i = 1; i <= m; ++i){43             scanf("%d%d", &P, &Q);44             if (P == 2) del = 0;45             ans += f[P] * Q;46         }47         printf("%lld\n", ans + del);48     }49     return 0;50 }
View code

Bzoj2749 [haoi2012] Aliens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.