Skip to content

Commit e9631e8

Browse files
committed
Merge branch 'master' of github.com:aggarwaldeepak/competitive-programming
2 parents b601d3b + d6745ec commit e9631e8

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

AndrewAndCity-h.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define mod 1000000007
4+
5+
int main(){
6+
int t;
7+
cin>>t;
8+
while(t--){
9+
int n,i;
10+
long long int arr[100010],left[100010],right[100010],ans=0;
11+
cin>>n;
12+
for(i=0;i<n;i++)
13+
cin>>arr[i];
14+
left[0]=arr[0];
15+
for(i=1;i<n;i++)
16+
left[i]=max(left[i-1],arr[i]);
17+
right[n-1]=arr[n-1];
18+
for(i=n-2;i>=0;i--){
19+
right[i]=max(right[i+1],arr[i]);
20+
}
21+
for(i=0;i<n;i++){
22+
int f=min(left[i],right[i]);
23+
ans+= (f-arr[i] + mod)%mod;
24+
ans%=mod;
25+
}
26+
cout<<ans<<"\n";
27+
}
28+
}

CRT-h.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int gcdExtended(int a, int b, int *x, int *y)
5+
{
6+
if (a == 0)
7+
{
8+
*x = 0;
9+
*y = 1;
10+
return b;
11+
}
12+
13+
int x1, y1;
14+
int gcd = gcdExtended(b%a, a, &x1, &y1);
15+
*x = y1 - (b/a) * x1;
16+
*y = x1;
17+
18+
return gcd;
19+
}
20+
21+
int FindModInverse(int remain,int num){
22+
int x,y;
23+
gcdExtended(remain,num,&x,&y);
24+
return x;
25+
}
26+
27+
int main(){
28+
int remainders[100010]={},numbers[100010]={},n;
29+
long long int product=1,ans=0;
30+
cin>>n;
31+
for(int i=0;i<n;i++){
32+
cin>>numbers[i]>>remainders[i];
33+
product*=numbers[i];
34+
}
35+
for(int i=0;i<n;i++){
36+
int invAnswer=FindModInverse(product/numbers[i],numbers[i]);
37+
if(invAnswer<0)
38+
invAnswer+=numbers[i];
39+
// cout<<invAnswer<<" ";
40+
ans+=(product/numbers[i])*remainders[i]*invAnswer;
41+
}
42+
cout<<ans%product;
43+
return 0;
44+
}

MAIN74-h.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
#define mod 1000000007
5+
int size=2;
6+
7+
8+
void multiply(long long int a[][3],long long int b[][3]){
9+
int i,j,k;
10+
long long int ans[3][3]={};
11+
for(i=0;i<size;i++){
12+
for(j=0;j<size;j++){
13+
for(k=0;k<size;k++){
14+
ans[i][j]=(ans[i][j] + a[i][k]*b[k][j])%mod;
15+
ans[i][j]=ans[i][j]%mod;
16+
}
17+
}
18+
}
19+
for(i=0;i<size;i++){
20+
for(j=0;j<size;j++){
21+
a[i][j]=ans[i][j];
22+
}
23+
}
24+
25+
}
26+
27+
void power(long long int second[][3],long long int n){
28+
long long int current[3][3]={},i,j;
29+
for(i=0;i<size;i++){
30+
for(j=0;j<size;j++)
31+
current[i][j]=second[i][j];
32+
}
33+
if(n<=1){
34+
return;
35+
}
36+
if(n&1){
37+
power(second,n-1);
38+
multiply(second ,current);
39+
}
40+
else{
41+
power(second,n/2);
42+
multiply(second,second);
43+
}
44+
}
45+
46+
47+
int main(){
48+
int t;
49+
cin>>t;
50+
while(t--){
51+
long long int n;
52+
cin>>n;
53+
if(n==0)
54+
{cout<<"0\n";continue;}
55+
if(n==1)
56+
{cout<<"2\n";continue;}
57+
else{
58+
long long int myArray[3][3];
59+
myArray[0][0]=1;myArray[0][1]=1;myArray[1][0]=1;myArray[1][1]=0;
60+
power(myArray,n+2);
61+
cout<<myArray[0][0]%mod<<"\n";
62+
}
63+
}
64+
return 0;
65+
}

0 commit comments

Comments
 (0)