codeforcesAIM Tech Round 3(扩欧,乱搞)


A:略

B:求在数轴上的n个点当中,所有点到这个点距离之和最小的点。

知道这个知识点,就是求中位数

C:给出n,求用1~n*n 来构造一个n*n的矩阵,要保证横之和为odd,竖之和为odd,主对角线和为odd

n<=49;保证n为奇数。。


首先49^2太大,没法搜索,想不到简单的方法。然后我就想,是不是可能这个图是根据一定的规律构造出来的呢?就想找构造方法

然后我又发现,因为只是求保证是奇数,实际上一个点上的数起作用的只是它的奇偶性。

对于样例:


3
2 1 4
3 5 7
6 9 8
构造出来就是
0 1 0
1 1 1
0 1 0
 

感觉到这里就比较简单了,分析一下,猜一猜就好了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cctype>
using namespace std;
const int inf=0x3f3f3f3f;
int a[55][55];

int main()
{
	int n;
	scanf("%d",&n);
	for (int i=1;i<=n/2+1;i++)
	{
		int mid=n/2+1;
		a[i][mid]=1;
		int k=1;
		while (k<i)
		{
			a[i][mid+k]=1;
			a[i][mid-k]=1;
			k++;
		}		
		int j=n-i+1;
		a[j][mid]=1;
		k=1;
		while (k<i)
		{
			a[j][mid+k]=1;
			a[j][mid-k]=1;
			k++;
		}
	}
	/*for (int i=1;i<=n;i++)
	{	
		for (int j=1;j<=n;j++) printf("%d ",a[i][j]);
		printf("\n");
	}*/
	int odd=1,even=2;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
		{
			if (a[i][j])
			{
				a[i][j]=odd;
				odd+=2;
			}
			else 
			{
				a[i][j]=even;
				even+=2;
			}
		}
	for (int i=1;i<=n;i++)
	{	
		for (int j=1;j<=n;j++) printf("%d ",a[i][j]);
		printf("\n");
	}
	return 0;
}


D. Two Arithmetic Progressions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers xsuch that L ≤ x ≤ R and x = a1k' + b1 = a2l' + b2, for some integers k', l' ≥ 0.

Input

The only line contains six integers a1, b1, a2, b2, L, R(0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R).

Output

Print the desired number of integers x.

Examples
input
2 0 3 3 5 21
output
3
input
2 4 3 0 6 17
output
2

很明显的扩欧可做,但是细节真的太难处理了,所以D比E过的还少。。。先把超时的程序交上来吧,主要是因为加的话会被卡超时,但是如果直接计算,真心不会。。bug死我了。。


当然如果直接计算用到除法的话,注意,可以转换成浮点数运算,然后floor向下取整,避免误差

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cctype>
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll a1,b1,a2,b2,L,R;

void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
	if (b==0){d=a;x=1;y=0;return ;}
	exgcd(b,a%b,d,y,x);y-=x*(a/b);
}
ll rt;
ll work(ll a,ll b,ll c)
{
	ll x,y,d;
	exgcd(a,b,d,x,y);
	x=x*(c/d);
	ll k=b/d;
	
	ll ans=0;
	x=((x%k)+k)%k;
	y=(c-a*x)/b;
	
	while (1)
	{
		if (a1*x+b1>=L&&a1*x+b1<=R&&y<=0) ans++;
		x+=b/d;
		y-=a/d;
		if (a1*x+b1>R) break;
	}
	
	return ans;
}
int main()
{
	scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a1,&b1,&a2,&b2,&L,&R);
	 
	ll ans=work(a1,a2,b2-b1);
	
	printf("%I64d",ans);
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值