CodeForces - 1485D Multiples and Power Differences (构造+lcm)

CodeForces - 1485D Multiples and Power Differences (构造+lcm)

原题链接

思路:

考虑最特殊的情况:
b [ i ] [ j ] = ∑ x = 1 16 l c m ( x ) b[i][j]=\sum_{x=1}^{16}{lcm(x)} b[i][j]=x=116lcm(x)
这样构造出来一定满足前两个条件。

至于第三个条件,可以将矩阵b按照奇偶分类,奇数时b[i] [j]的取值如上,偶数时b[i] [j]的取值要加上a[i] [j] 的四次方。这样对于任何一个数来说,第三个条件都是符合的。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){
    
    ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}return x*f;}
ll ksm(ll a,ll b,ll p){
    
    ll res=1;while(b){
    
    if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=1e6+7;
int a[510][510],b[510][510];
int gcd(int a,int b){
    
    
	return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b){
    
    
	return a/gcd(a,b)*b;
}
void solve(){
    
    
	int n=read(),m=read();
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			a[i][j]=read();
	int t=1;
	for(int i=2;i<=16;i++)
		t=lcm(t,i);
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=m;j++)
			if((i+j)%2) cout<<t<<" ";
			else cout<<t+a[i][j]*a[i][j]*a[i][j]*a[i][j]<<" ";
		puts("");
	}
		
}

int main(){
    
    
	int T=1;
	while(T--) solve(); 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45675097/article/details/113800396