sgu 160 Magic Multiplying Machine
来源:互联网 发布:油皮面膜知乎 编辑:程序博客网 时间:2024/06/07 07:22
160. Magic Multiplying Machine
time limit per test: 1 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard input
output: standard output
output: standard output
Well known Las-Vegas casino "Big Jo" has recently introduced the new playing machine, called Magic Multiplying Machine (MMM). MMM has N levers and one big red button. Each lever is marked with some integer number ranging from 1 to M, thus i-th lever is marked with number ai.
A player who wishes to play on MMM inserts a coin into a special slot. After that she chooses some levers and pulls them. After she has made her choice, she presses the big red button. MMM blinks all its lights, rings its bells, plays diRerent tunes and after that declares whether the player has won the game.
The algorithm for determining whether the player has won is the following. If the player has pulled some subset S in {1, 2, ... ,N} of levers, her score is the product of numbers that these levers are marked with, taken modulo M (if the player has pulled no levers, her score is 1):
The player has won the game if her score is maximal possible for the given machine.
Given the description of some MMM determine which levers should be pulled to win the game.
A player who wishes to play on MMM inserts a coin into a special slot. After that she chooses some levers and pulls them. After she has made her choice, she presses the big red button. MMM blinks all its lights, rings its bells, plays diRerent tunes and after that declares whether the player has won the game.
The algorithm for determining whether the player has won is the following. If the player has pulled some subset S in {1, 2, ... ,N} of levers, her score is the product of numbers that these levers are marked with, taken modulo M (if the player has pulled no levers, her score is 1):
The player has won the game if her score is maximal possible for the given machine.
Given the description of some MMM determine which levers should be pulled to win the game.
Input
The first line of the input file contains two integer numbers 1 <= N <= 10000 and 2 <= M <= 1 000. The second line contains N integer numbers a1, a2, ..., aN ranging from 1 to M.
Output
In the first line of the output file print one integer number - the score that should be obtained to win the game. In the second line print in ascending order the numbers of levers that should be pulled to obtain this score (levers are numbered starting from one). If there are several solutions, output any one.
Sample test(s)
Input
Sample input #1
4 6
1 2 3 4
Sample input #2
4 4
4 4 4 4
4 6
1 2 3 4
Sample input #2
4 4
4 4 4 4
Output
Sample output #1
4
1 4
Sample output #2
1
4
1 4
Sample output #2
1
Note
You can download pdf version of this task here
[submit]
[forum]
#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))#define FOR(a,b) for(int a=1;a<=(b);(a)++)using namespace std;int const nMax = 1010;int const base = 10;typedef int LL;typedef pair<LL,LL> pij;// std::ios::sync_with_stdio(false);set<int> Q;set<int> :: iterator it,be,ed;pij s[nMax];int n,m;int a,b;bool vis[nMax];vector<int> add;void output(int a){ if(a==-1) return ; Q.insert(s[a].second); output(s[a].first); return ;}int main(){ scanf("%d%d",&n,&m); CLR(vis,0); Q.clear(); FOR(i,n) { scanf("%d",&a); a%=m; add.clear(); if(!vis[a]){ vis[a]=true; s[a]=make_pair(-1,i); add.push_back(a); } for(it=Q.begin();it!=Q.end();it++) { b=(*it)*a%m; if(!vis[b]) { vis[b]=true; s[b]=make_pair(*it,i); add.push_back(b); } } for(int j=0;j<add.size();j++) Q.insert(add[j]); } for(int i=m-1;i>=0;i--)if(vis[i]){ if(i==0){ return printf("1\n"),0; } printf("%d\n",i); Q.clear(); output(i); break; } for(it=Q.begin();it!=Q.end();it++){ printf("%d ",*it); } printf("\n"); return 0;}
- sgu 160 Magic Multiplying Machine
- SGU 160 Magic Multiplying Machine(dp)
- sgu160:Magic Multiplying Machine
- sgu160 Magic Multiplying Machine DP
- sgu 119 Magic Pairs
- SGU 119 Magic Pairs
- [SGU]119. Magic Pairs
- sgu 119 Magic Pairs
- SGU 119 Magic Pairs
- SGU 109 Magic of David Copperfield II
- [SGU]109. Magic of David Copperfield II
- sgu 109 Magic of David Copperfield II
- SGU 119. Magic Pairs 同余+枚举
- SGU 109 Magic of David Copperfield II
- SGU 109 Magic of David Copperfield II
- SGU 119 Magic Pairs(数论)
- sgu 160
- SGU 109 Magic of David Copperfield II 奇偶性
- qt控件的使用
- Servlet实现文件下载以及遇到的问题
- Liunx java-eclipse下C++的so库的调用(上)
- Windows8 Metro快捷键大全
- c语言函数可变参数列表
- sgu 160 Magic Multiplying Machine
- 单例模式——php单例模式的运用,用单例模式设计实现多例
- Ubuntu 12.04 笔记本关闭独立显卡
- Workflow Command - WFLOAD
- rsa算法的python实现
- Ubuntu实践(2):在线升级最新版本
- 进实验室半年了,目前做的一些工作,路漫漫其修远兮
- 测试数据
- 人生之第一个java class程序于今晚生成了[中达打印标签流水号]