USACO 2017 US Open Contest, Platinum Problem 3. COWBASIC
来源:互联网 发布:c语言if语句多个条件 编辑:程序博客网 时间:2024/05/14 20:36
Bessie has invented a new programming language, but since there is no compiler yet, she needs your help to actually run her programs.
COWBASIC is a simple, elegant language. It has two key features: addition and MOO loops. Bessie has devised a clever solution to overflow: all addition is done modulo 109+7109+7. But Bessie’s real achievement is the MOO loop, which runs a block of code a fixed number of times. MOO loops and addition can, of course, be nested.
Given a COWBASIC program, please help Bessie determine what number it returns.
INPUT FORMAT (file cowbasic.in):
You are given a COWBASIC program at most 100 lines long, with each line being at most 350 characters long. A COWBASIC program is a list of statements.
There are three types of statements:
<variable> = <expression><literal> MOO { <list of statements>}RETURN <variable>
There are three types of expressions:
<literal><variable>( <expression> ) + ( <expression> )
A literal is a positive integer at most 100,000.
A variable is a string of at most 10 lowercase English letters.
It is guaranteed that no variable will be used or RETURNed before it is defined. It is guaranteed that RETURN will happen exactly once, on the last line of the program.
OUTPUT FORMAT (file cowbasic.out):
Output a single positive integer, giving the value of the RETURNed variable.
Scoring
In 20 percent of all test cases - MOO loops are not nested.
In another 20 percent of all test cases - The program only has 1 variable. MOO loops can be nested.
In the remaining test cases, there are no further restrictions.
SAMPLE INPUT:
x = 110 MOO { x = ( x ) + ( x )}RETURN x
SAMPLE OUTPUT:
1024
This COWBASIC program computes 210210.
SAMPLE INPUT:
n = 1nsq = 1100000 MOO { 100000 MOO { nsq = ( nsq ) + ( ( n ) + ( ( n ) + ( 1 ) ) ) n = ( n ) + ( 1 ) }}RETURN nsq
SAMPLE OUTPUT:
4761
This COWBASIC program computes (105∗105+1)2(105∗105+1)2 (modulo 109+7109+7).
Problem credits: Jonathan Paulson
矩阵幂
数组用c++容器方便很多,忽略里面的一些坏的写法,开始是用数组在写
#include<iostream>#include<string>#include<sstream>#include<vector>#include<cstring>#include<map>#include<array>#define MOD 1000000007using namespace std;typedef array<array<long long ,105>,105> ar;typedef array<long long,105> ar1;map<string, int> V;//编号ar1 var;//0代表常数1vector<string> all;//所有语句int v=0;stringstream ss;string st,str1;//memsetvoid f(ar &x) { for (int i = 0; i <= v; ++i) for (int j = 0; j <= v; ++j) x[i][j] = 0;}//memset的效果void getarr(string &s,ar &re) { ss.clear(); ss.str(s); ss >> str1; int k=V[str1]; while (ss >> str1) { if (str1 != "=" && str1 != "(" &&str1 != ")" && str1 != "+") { if (str1[0] >= '0' && str1[0] <= '9') re[k][0] += stoi(str1); else re[k][V[str1]]++; } } for (int i = 0; i <= v; ++i) if (i != k) re[i][i] = 1;}//提要要用的两种矩阵乘法A(v,v)*B(v,v)和A(v,v)*B(v)void mularr1(ar &a) { ar1 tmp = {}; for (int i = 0; i <= v; ++i) { tmp[i] = var[i]; var[i] = 0; } for (int i = 0; i <= v; ++i) for (int k = 0; k <= v; ++k) var[i] = (var[i]+a[i][k] * tmp[k])%MOD;}void mularr(ar a, ar b, ar &re) { f(re); for (int i = 0; i <= v; ++i) for (int j = 0; j <= v; ++j) for (int k = 0; k <= v; ++k) re[i][j] = (re[i][j]+a[i][k] * b[k][j])%MOD;}//矩阵幂void poww(ar a, int b, ar &re) { f(re); for (int i = 0; i <= v; ++i) re[i][i] = 1; while (b ) { if (b & 1) mularr(a,re,re); mularr(a, a, a); b >>=1; }}//处理循环void MOO(int &b, ar &x) { ss.clear(); ss.str(all[b]); int k; ss >> k; ar xx; ar ans = {}; for (int i = 0; i <= v; ++i) ans[i][i] = 1; while (1) { f(xx); if (all[++b].find("{") != string::npos) MOO(b, xx); else if (all[b].find("}") != string::npos) break; else getarr(all[b],xx); mularr(xx, ans, ans); } poww(ans, k, x);}int main() { freopen("cowbasic.in","r",stdin); freopen("cowbasic.out", "w", stdout); getline(cin, st); //输入,计数变量个数 while (st[0] != 'R') { all.push_back(st); if (st.find("=")!=st.npos) { ss.clear(); ss.str(st); ss >> str1; if (!V[str1]) V[str1] = ++v; } getline(cin, st); } var[0] = 1; ar x; //处理开始 for (int i = 0; i < all.size(); ++i) { f(x); if (all[i].find("{")!=string::npos) MOO(i, x); else getarr(all[i], x); mularr1(x); } cout << var[V[string(st, 7, st.size())]] << endl;}
- USACO 2017 US Open Contest, Platinum Problem 3. COWBASIC
- USACO 2016 US Open Contest, Gold Problem 3. 248
- USACO 2017 US OPEN PLATINUM题目翻译(未完)
- USACO 2015 US OPEN CONTEST,Silver Division Solution
- USACO 2015 US OPEN CONTEST,Gold Division Solution
- jzoj 5113 【usaco2017_Mar Platinum】COWBASIC
- USACO 2015 US OPEN BRONZE 铜组 题解
- USACO 2016 US Open【262144游戏】
- USACO 2016 February Contest, Bronze Problem 3. Load Balancing
- USACO 2016 February Contest, Silver Problem 3. Milk Pails
- USACO 2017 January Contest, Gold Problem 1. Balanced Photo
- USACO 2017 January Contest, Gold Problem 2. Hoof, Paper, Scissors
- USACO US OPEN 2015 BRONZE 三四题 C++翻译代码
- USACO DEC12,Platinum
- BZOJ 4778 Usaco2017 Open COWBASIC 矩阵乘法
- bzoj4778[Usaco2017 Open]COWBASIC 矩阵乘法
- USACO 2016 JANUARY CONTEST, BRONZE PROBLEM 3. MOWING THE FIELD(收割庄稼)
- 【C++心路历程33】【USACO 2016 February Contest, Bronze Problem 3.】 Load Balancing(加难)
- Android官方DataBinding(七):BindingMethods与BindingMethod
- 程序员的第一份工作
- cocos2dx屏幕截图
- React-native 环境配置window端mac端
- opencv中的开运算,闭运算,形态学梯度,顶帽和黑帽
- USACO 2017 US Open Contest, Platinum Problem 3. COWBASIC
- Struts基本执行流程
- 迭代地删除文件夹及其下的所有文件
- 日语入门学习,五十音图日语基础知识
- 双端队列
- 三种简单排序
- 剑指offer面试题4
- java应用中Pattern和Matcher的使用
- muduo源码学习(17)-定时器实现