Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.io.*;
- import java.math.BigInteger;
- import java.util.*;
- import static java.lang.Math.*;
- public class A1 implements Runnable{
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok = new StringTokenizer("");
- void init() throws FileNotFoundException{
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter("output.txt");
- }
- String readString() throws IOException{
- while(!tok.hasMoreTokens()){
- tok = new StringTokenizer(in.readLine());
- }
- return tok.nextToken();
- }
- int readInt() throws IOException{
- return Integer.parseInt(readString());
- }
- long readLong() throws IOException{
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException{
- return Double.parseDouble(readString());
- }
- public static void main(String[] args){
- new Thread(null, new A1(), "", 256 * (1L << 20)).start();
- }
- public void run(){
- try{
- long t1 = System.currentTimeMillis();
- init();
- solve();
- out.close();
- long t2 = System.currentTimeMillis();
- System.err.println("Time = "+(t2-t1));
- }catch (Exception e){
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- class XY{
- long x, y;
- public XY(long x, long y) {
- this.x = x;
- this.y = y;
- }
- }
- long p;
- void solve() throws IOException{
- int n = readInt();
- p = readInt();
- long[] a = new long[n];
- for (int i = 0; i < n; i++){
- a[i] = readLong();
- }
- long[] f = new long[n];
- int[] pow = new int[n];
- pow[0] = pow[1] = 0;
- f[0] = f[1] = 1;
- for (int i = 2; i < n; i++){
- int x = i;
- pow[i] = pow[i-1];
- while (x % p == 0){
- x /= p;
- pow[i]++;
- }
- f[i] = f[i-1] * x % p;
- }
- long fn = f[n-1];
- long pn = pow[n-1];
- long s = 0;
- n--;
- for (int i = 0; i <= n; i++){
- if (pn == pow[i] + pow[n-i]){
- long d = fn * rev(f[i]) % p * rev(f[n-i]) % p * a[i] % p;
- if (((n - i) & 1) == 0){
- s = (s + d) % p;
- }else{
- s = (s - d + p) % p;
- }
- }
- }
- out.print(s);
- }
- long rev(long n){
- long x = xy(n, p).x;
- if (x <= 0){
- long k = -x / p + 1;
- x = p * k + x;
- }
- return x % p;
- }
- XY xy(long a, long b){
- if (a == 0){
- return new XY(0, 1);
- }
- XY xy = xy(b % a, a);
- return new XY(xy.y - b / a * xy.x, xy.x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment