HDU 3049 Data Processing

Source: Internet
Author: User

P is a prime number, so there are:

(1). A/B % P = (a % (B * p)/B) % P;

(2). A/B % P = A * (B ') % P where B' is the inverse element of B for P

Method 1: # include <stdio. h> int main () {int I, t, x, V = 1, n, m, ans; long P, A [51000], sum; scanf ("% d", & T); While (t -- & scanf ("% d", & N) {A [0] = 1; sum = 0; P = 1000003; p * = N; for (I = 1; I <= 40000; I ++) {A [I] = 2 * A [I-1]; if (A [I]> = p) A [I]-= P ;}for (I = 0; I <n; I ++) {scanf ("% d", & X); sum + = A [X]; If (sum> = P) sum-= P;} ans = sum/N; printf ("case % d: % d \ n", V ++, ANS) ;}} Method 2: # include <stdio. h> # inclu De <math. h> # define Nmax 1000003 # define nnum 40001int num [nnum], X, Y; int extend_gcd (int A, int B) {If (B = 0) {x = 1, y = 0; return a;} int d = extend_gcd (B, A % B); int Tx = x; X = y; y = TX-A/B * Y; return D;} void Init () {int I, Te; for (I = 0, TE = 1; I <nnum; I ++) {num [I] = tE; Te = tE * 2% Nmax ;}} int main () {# ifndef online_judgefreopen ("t.txt", "r ", stdin); # endifint t, n, I, J, K; long res; Init (); While (scanf ("% d", & T )! = EOF) {for (I = 1; I <= T; I ++) {scanf ("% d", & N); For (j = 0, res = 0; j <n; j ++) {scanf ("% d", & K); Res + = num [k]; If (RES> = Nmax) {res-= Nmax ;}} extend_gcd (n, Nmax); X = (X % Nmax + Nmax) % Nmax; Res = res * x % Nmax; printf ("case % d: % i64d \ n", I, Res) ;}} return 0 ;}

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.