蓝桥算法训练营—提高组—2023年2.12日

ck 2023-02-12 23:52:39

P1890 gcd区间

P1890 gcd区间 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <bits/stdc++.h>
#define x first
#define y second
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;

typedef pair<int,int> PII;
typedef pair<char,int> PCI;
typedef long long LL;
typedef unsigned long long ULL;

const int N=1000+10 ,M= N * N + 10, INF = 0x3f3f3f3f ,mod =1e9 + 7 ;

int n,m;

struct node{
int l,r,v;
}tr[N * 4 ];

int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}

void pushup(int u)
{
tr[u].v = gcd(tr[u<<1].v,tr[u<<1|1].v);
}
void build(int u,int l,int r)
{
if(l == r) {
int t;cin>>t;
tr[u]={l,r,t};
}
else
{
tr[u] = {l,r};
int mid = l + r >> 1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
pushup(u);
}

}

int query(int u,int l,int r)
{
if(tr[u].l >= l && tr[u].r <= r)
{
return tr[u].v;
}

int mid = tr[u].l + tr[u].r >> 1 ;

int v=0;
if(l<=mid ) v =query(u<<1,l,r);
if(r > mid ) v =gcd(v,query(u<<1|1,l,r));

return v;
}

void solve()
{
cin>>n>>m;
build(1,1,n);

while(m -- )
{
int x,y;cin>>x>>y;
cout << query(1,x,y) << endl;
}
}

int main()
{
ios
int T=1;
// 	cin>>T;
while(T -- )
{
solve();
}

return 0;
}

P1405 苦恼的小明

P1405 苦恼的小明 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <bits/stdc++.h>
#define x first
#define y second
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;

typedef pair<int,int> PII;
typedef pair<char,int> PCI;
typedef long long LL;
typedef unsigned long long ULL;

const int N=1e7+10 , INF = 0x3f3f3f3f ;

int n,m;

int phi(int x)
{
LL res =x ;
for(int i=2;i<=x/i;i++)
if(x % i ==0 )
{
res = res /i * (i-1);
while(x % i == 0) x /=i;
}
if(x > 1) res = res / x * (x-1) ;
return res;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
LL qmi(LL a,LL b,int mod)
{
LL res =1;
while(b)
{
if(b & 1) res = (res * a) % mod;
a = (a * a ) % mod;
b>>=1;
}
return res;
}

int a[N];

int dfs(int x,int mod)
{
if(x == n ) return a[x]=a[x] % mod;

LL t = phi(mod);
LL t1=dfs(x+1,t);

if(gcd(a[x],mod) == 1 )
return a[x] = qmi(a[x],t1,mod);
else
{
if(a[x+1] > t ) return a[x] =qmi(a[x],t1+t,mod);
return a[x] = qmi(a[x],a[x+1],mod);
}

}

void solve()
{
cin>>n;
for(int i=1;i<=n;i ++ ) cin>> a[i];
int mod=10007;
cout << dfs(1,mod) << endl;
}

int main()
{
ios
LL T=1;
// 	cin>>T;
while(T -- )
{
solve();
}

return 0;
}

...全文
8 回复 打赏 收藏 举报

4.3w+

2023-02-12 23:52

刷题！