[Luogu 3811] [TEMPLATE] multiplication inverse element, luogu3811 Multiplication

Source: Internet
Author: User

[Luogu 3811] [TEMPLATE] multiplication inverse element, luogu3811 Multiplication
Background

This is a template question.

Description

Evaluate 1 ~ for given n and p ~ All integers in n represent the multiplication inverse element in the modulo p.

Input/Output Format

Input Format:

N, p in a row

Output Format:

N rows. line I indicates the inverse element of I in the p sense.

Input and Output sample input sample #1:
10 13
Output sample #1:
179108112534
Description

1 ≤ n ≤ 3 × 106, n <p <20000528

Enter pp as the prime number.

Ferma's theorem:

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define ll long long 6 using namespace std; 7 int n,p; 8 int fast(ll a,ll b){ 9     ll ans=1;10     while(b){11         if(b&1)ans=(ans*a)%p;12         a=(a*a)%p;13         b>>=1;14     }15     return ans;16 }17 int main(){18     scanf("%d%d",&n,&p);19     for(int i=1;i<=n;i++)20         printf("%d\n",fast(i,p-2));21     return 0;22 }

Extended Euclidean:

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 void ex_gcd(int a,int b,int &x,int &y){ 7     if(b==0){x=1,y=0;return;} 8     ex_gcd(b,a%b,x,y); 9     int tmp=x;x=y;y=tmp-(a/b)*y;10 }11 int main(){12     int a,b,x,y;13     scanf("%d%d",&a,&b);14     for(int i=1;i<=a;i++){15         ex_gcd(i,b,x,y);16         printf("%d\n",(x+b)%b);17     }18     return 0;19 }

Linear computing:

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int ans[3000010],n,p; 7 int main(){ 8     scanf("%d%d",&n,&p); 9     ans[1]=1;puts("1");10     for(int i=2;i<=n;i++){11         ans[i]=(long long)(p-p/i)*ans[p%i]%p;12         printf("%d\n",ans[i]);13     }14     return 0;15 }

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.