Guest User

Untitled

a guest
Nov 6th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.*;
  3. import java.math.BigInteger;
  4. import java.util.*;
  5. import static java.lang.Math.*;
  6.  
  7. public class A1 implements Runnable{
  8.    
  9.     BufferedReader in;
  10.     PrintWriter out;
  11.     StringTokenizer tok = new StringTokenizer("");
  12.    
  13.     void init() throws FileNotFoundException{
  14.         in = new BufferedReader(new FileReader("input.txt"));
  15.         out = new PrintWriter("output.txt");
  16.     }
  17.    
  18.     String readString() throws IOException{
  19.         while(!tok.hasMoreTokens()){
  20.             tok = new StringTokenizer(in.readLine());
  21.         }
  22.         return tok.nextToken();
  23.     }
  24.    
  25.     int readInt() throws IOException{
  26.         return Integer.parseInt(readString());
  27.     }
  28.    
  29.     long readLong() throws IOException{
  30.         return Long.parseLong(readString());
  31.     }
  32.    
  33.     double readDouble() throws IOException{
  34.         return Double.parseDouble(readString());
  35.     }
  36.    
  37.     public static void main(String[] args){
  38.         new Thread(null, new A1(), "", 256 * (1L << 20)).start();
  39.     }
  40.    
  41.     public void run(){
  42.         try{
  43.             long t1 = System.currentTimeMillis();
  44.             init();
  45.             solve();
  46.             out.close();
  47.             long t2 = System.currentTimeMillis();
  48.             System.err.println("Time = "+(t2-t1));
  49.         }catch (Exception e){
  50.             e.printStackTrace(System.err);
  51.             System.exit(-1);
  52.         }
  53.     }
  54.    
  55.     class XY{
  56.        
  57.         long x, y;
  58.  
  59.         public XY(long x, long y) {
  60.             this.x = x;
  61.             this.y = y;
  62.         }
  63.        
  64.        
  65.     }
  66.    
  67.     long p;
  68.    
  69.     void solve() throws IOException{
  70.         int n = readInt();
  71.         p = readInt();
  72.         long[] a = new long[n];
  73.         for (int i = 0; i < n; i++){
  74.             a[i] = readLong();
  75.         }
  76.         long[] f = new long[n];
  77.         int[] pow = new int[n];
  78.         pow[0] = pow[1] = 0;
  79.         f[0] = f[1] = 1;
  80.         for (int i = 2; i < n; i++){
  81.             int x = i;
  82.             pow[i] = pow[i-1];
  83.             while (x % p == 0){
  84.                 x /= p;
  85.                 pow[i]++;
  86.             }
  87.             f[i] = f[i-1] * x % p;
  88.         }
  89.         long fn = f[n-1];
  90.         long pn = pow[n-1];
  91.         long s = 0;
  92.         n--;
  93.         for (int i = 0; i <= n; i++){
  94.             if (pn == pow[i] + pow[n-i]){
  95.                 long d = fn * rev(f[i]) % p * rev(f[n-i]) % p * a[i] % p;
  96.                 if (((n - i) & 1) == 0){
  97.                     s = (s + d) % p;
  98.                 }else{
  99.                     s = (s - d + p) % p;
  100.                 }
  101.             }
  102.         }
  103.         out.print(s);
  104.     }
  105.    
  106.     long rev(long n){
  107.         long x = xy(n, p).x;
  108.         if (x <= 0){
  109.             long k = -x / p + 1;
  110.             x = p * k + x;
  111.         }
  112.         return x % p;
  113.     }
  114.    
  115.     XY xy(long a, long b){
  116.         if (a == 0){
  117.             return new XY(0, 1);
  118.         }
  119.         XY xy = xy(b % a, a);
  120.         return new XY(xy.y - b / a * xy.x, xy.x);
  121.     }
  122.    
  123. }
Advertisement
Add Comment
Please, Sign In to add comment