SGU 160.Magic Multiplying Machine

时间限制:0.5s

空间限制6M

题意:

       给出n个(1<=n<=10000)1~m(2<m<1000)范围内的数,选择其中任意个数,使它们的 乘积 模m 最大,输出最大的分数,和选择的数的编号。



Solution:

              DP,

              从第一个数开始,f[]记录当前有哪些数可以得到.如果k可以得到令f[k]=1;

              再记录路径,和更新ans。

              如果单纯使用二重循环将是N*M 的复杂度。有很大可能超过0.5s的时限。

              于是这里使用数组实现的记录了哪些数可以得到的链表,p是链表头。

code(31ms AC)

#include<cstdio>
int n, m, x;
int g[10009], pr[1009][2], f[1009][2];
void write (int x) {
	if (pr[x][0] != 0) write (pr[x][0]);
	printf ("%d ", pr[x][1]);
}
int main() {
	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) scanf ("%d", &x), g[i] = x % m;
	int p = 0, ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = p; j != 0; j = f[j][1]) {
			int tem = (g[i] * j) % m;
			if (tem > 1 && !f[tem][0]) {
				f[tem][0] = 1; f[tem][1] = p;
				p = tem, pr[tem][0] = j, pr[tem][1] = i;
				if (tem > ans) ans = tem;
			}
		}
		if (!f[g[i]][0]) {
			f[g[i]][0] = 1, f[g[i]][1] = p;
			p = g[i], pr[g[i]][1] = i;
			if (g[i] > ans) ans = g[i];
		}
	}
	if (ans > 0) {
		printf ("%d\n", ans);
		write (ans);
		return 0;
	}
	puts ("1");
	return 0;
}

  

  

SGU 160.Magic Multiplying Machine

时间: 2024-10-15 03:11:28

SGU 160.Magic Multiplying Machine的相关文章

sgu100~199题解

老东西了..发上来吧.. Sgu题解系列  南开中学邹事成 100:A+B略 101:Domino 给n块多米诺骨牌,每张骨牌两端各有从1到6的一个数字,现在要把这些骨牌排成一列,使相邻的两块骨牌相对的面所写的数字一样. 可以把每一块多米诺骨牌想象成一条边,把面上写的数字抽象成点,比如一块骨牌正面写的1反面写的2就想象成连了一条从1到2的边,那么这就是求一条有重边的欧拉回路了,dfs一下即可. 102:Coprimes给定n求从1到n中与n互质的数的个数. 可以把n质因数分解后直接代入欧拉函数.

Uncompressing Linux___ done, booting the kernel_tekkamanninja-ChinaUnix博客

今天用主线Linux内核移植到MINI6410,主线内核2.6.37.1基本已经支持了MINI6410的板子,所以移植到能够启动起来的阶段很简单,但是在移植的时候还是出现了一个比较常见的问题: MINI6410 # bootm 0x50008000 ## Booting kernel from Legacy Image at 50008000 ... Image Name: Linux-2.6.37.1 Image Type: ARM Linux Kernel Image (uncompress

Magic Pairs - SGU 119(同余)

题目大意:如果A0*X + B0*Y能够整除 N,求出来多有少A*X+B*Y 也能够整除去N,求出所有的A,B(0<=A,B<N) 分析:有条件可以知道 A*X+B*Y = K *(A0*X + B0*Y)% N ====> (K * A0) % N = A, (K * B0) % N = B, (k=[0....N)). ps.记得排序去重复...... 代码如下: ============================================================

SGU Magic Pairs

A0x + B0y = kn Ax + By = k'n 左差得 (A - A0)x + (B -B0)y = 0(mod n) 所以只要枚举A0, B0的倍数就行了.. 公式就是 ( (i*a)%n, (i*b)%n ), i =0, 1, ... , n-1 i*a, i*b如果大于n的话 不会影响结果, 因为对n取模 那一部分都约去了.. 1 /*Author :usedrose */ 2 /*Created Time :2015/7/24 14:55:16*/ 3 /*File Name

The Aggregate Magic Algorithms

http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that create and collect algorithms of all types (here are a few WWW sites). Unfortunately, in building systems hardware and software, we in The Aggregate o

hdu5067Harry And Dig Machine(TSP旅行商问题)

题目链接: huangjing 题意:给出一幅图,图中有一些点,然后从第1个点出发,然后途径所有有石头的点,最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思路: 状态转移方程 DP[k][i|base[k]]=min(DP[k][i|base[k]],DP[j][i]+dis[j][k]) DP[J][I]表示从起点到j点在i状态下的最小距离...DP[j][i]+dis[j][k]表从j到k的距离...时间复杂度是(n?m+(t2)?(2t)),那么

(最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

You're given a matrix A of size n?×?n. Let's call the matrix with nonnegative elements magic if it is symmetric (so aij?=?aji), aii?=?0 and aij?≤?max(aik,?ajk) for all triples i,?j,?k. Note that i,?j,?k do not need to be distinct. Determine if the ma

Machine and Deep Learning with Python

Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstitions cheat sheet Introduction to Deep Learning with Python How to implement a neural network How to build and run your first deep learning network Neur

Getting started with machine learning in Python

Getting started with machine learning in Python Machine learning is a field that uses algorithms to learn from data and make predictions. Practically, this means that we can feed data into an algorithm, and use it to make predictions about what might