#110. Multiplication against RMB,

Source: Internet
Author: User

#110. Multiplication against RMB,
#110. Multiplication back-to-source memory limit: 256 MiB time limit: 1000 ms standard input and output question type: traditional evaluation method: text comparison uploaded by: Menci submit Record statistics test data question description

This is a template question.

Given positive integers n nn and p pp, evaluate all numbers in 1 percentile n 1 \ sim n1 limit n in the sense of p pp.

Input Format

One row has two positive integers, n nn and p pp.

Output Format

Line n nn, line I ii is a positive integer, indicating the multiplication inverse element of I ii in the sense of p pp.

Sample Input
10 13
Sample output
179108112534
Data range and prompt

1 ≤ n ≤ 3 × 106, n <p <20000528 1 \ leq n \ leq 3 \ times 10 ^ 6, n <p <200005281 ≤ n ≤ 3 × 106, n <p <20000528
P pp is the prime number.

Because p is a prime number, we can easily think of a fast power. However, the last point of the quick power will include TLE 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 # include <cstdlib> 8 # define lli long int 9 using namespace std; 10 const lli MAXN = 10001; 11 void read (lli & n) 12 {13 char c = '+'; lli x = 0, flag = 1; 14 while (c <'0' | c> '9') 15 {c = getchar (); if (c = '-') flag =-1 ;} 16 while (c> = '0' & c <= '9') 17 {x = x * 10 + c-48; c = getchar ();} 18 n = (x * flag); 19} 20 lli n, mod; 21 lli fastpow (lli x, lli n) 22 {23 lli ans = 1; 24 (; n;) 25 {if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod, n = n> 1 ;} 26 return ans; 27} 28 int main () 29 {30 read (n); read (mod); 31 for (lli I = 1; I <= n; I ++) 32 printf ("% lld \ n", fastpow (I, mod-2) % mod); 33 return 0; 34}KSM

Then I read a great blog.

See a recursive formula:

Ans [I] = (mod-mod/I) * ans [mod % I] % mod;

Although I don't know what it means, it should be able to launch reverse yuan very quickly ,,

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #include<cstdlib> 8 #define lli long long int  9 using namespace std;10 const lli MAXN=10000001;11 void read(lli &n)12 {13     char c='+';lli x=0,flag=1;14     while(c<'0'||c>'9')15     {c=getchar();if(c=='-')flag=-1;}16     while(c>='0'&&c<='9')17     {x=x*10+c-48;c=getchar();}18     n=(x*flag);19 }20 lli n,mod;21 int ans[MAXN];22 int main()23 {24     read(n);read(mod);25     ans[1]=1;26     printf("1\n");27     for(int i=2;i<=n;i++)28     {29         ans[i]=(mod-mod/i)*ans[mod%i]%mod;30         printf("%d\n",ans[i]);31     }32     return 0;33 } 

 

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.