ZOJ 3868 GCD expectation and BC39 HDU 5212 Code

Source: Internet
Author: User
Tags acos mul cmath

The type of the two topics feel the same is the use of the idea of the repulsion to push forward and then go heavy.

HDU 5212 Test Instructions:

The number of n is calculated for each of the n numbers, and F (i), f (i) = GCD (A[i], a[j]) * (GCD (a[i], a[j])-1).

Consider the AI with AJ coprime when the value of f () is equal to 0 no need to calculate. Just calculate the number of all pairs of gcd with I as well (1<=i <=10000).


#include <algorithm> #include <cstdio> #include <vector> #include <cmath> #include <queue > #include <set> #include <map> #include <cstring> #include <cstdlib> #include <iostream >//#include <bits/stdc++.h> #define MAX 0x3f3f3f3f#define N 10005#define mod 10007#define Lson o<<1, L, m#d  Efine Rson o<<1|1, m+1, R#define Mem (a) memset (a, 0, sizeof (a)) typedef long Long ll;using namespace Std;const double    Pi = ACOs ( -1.0); int n, V[n], Dp[n], X;int main () {//freopen ("In.txt", "R", stdin);  while (CIN >> N) {mem (v);  MEM (DP);  int mx = 1;  for (int i = 0; i < n; i++) {scanf ("%d", &x);  v[x]++; mx = max (mx, x);} int ans = 0;for (int i = mx; i >= 1; i--) {Dp[i] = 0;int cnt = 0;for (int j = i; j <= mx; j + = i) {cnt + = V[j];if (J &G T i) {Dp[i] = (Dp[i]-dp[j])% mod + mod;dp[i]%= mod;} Dp[i] = (Dp[i] + cnt * CNT%mod)%mod;int mul = i * (i-1)%mod;ans + = mul * Dp[i];ans%= mod;} cout << ans << endl;} return 0;} 

Zhejiang University race GCD expectation

Test instructions is the K-square of the corresponding GCD for all subsets of the n number. The method is to enumerate 1 to the maximum value as GCD, how many multiples of I are counted as TMP, 2^tmp-1 must contain all the GCD as a subset of I but there may be gcd than I large, so to subtract I of twice times and more than the number of reverse can be counted out.


#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <vector> #include <cmath> #include <queue> #include <set> #include <map > #define N 1000005#define mod 998244353#define MAX 0x3f3f3f3f#define Lson o<<1, L, M#define Rson o<<1|1, m+  1, R#define Mem (a) memset (A,0,sizeof (a)) typedef long Long ll;using namespace std;const double pi = ACOs ( -1.0); int n, V[n], X, MX, k;ll dp[n];ll POW (ll A, ll b) {ll res = 1;while (b) {if (b & 1) res = res * a%mod;b/= 2;a = a * a%mod;} return res;} ll solve () {ll res = 0;for (int i = mx; i > 0; i--) {Dp[i] = 0;ll cnt = 0;for (int j = i; j <= mx; j + = i) {cnt + = V[j ];if (J > i) dp[i] = ((Dp[i]-dp[j])%mod + mod)%mod; }dp[i] + = POW (2, CNT)-1;dp[i]%= mod;res = (res%mod + dp[i] * POW (i, k)%mod)%mod;} return res;}    int main () {//freopen ("In.txt", "R", stdin); int T;  Cin >> T;  while (t--) {cin >> n >> K;  mx = 0; MeM (v);  for (int i = 0; i < n; i++) {scanf ("%d", &x);  v[x]++; mx = max (mx, x);}    cout << Solve () << Endl;} return 0;}





ZOJ 3868 GCD expectation and BC39 HDU 5212 Code

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.