problem 250

Problem Statement

Vasa likes to construct sequences of numbers. If you tell him a positive integer n, he will begin a new sequence by writing the integer n as its first element. He will then repeat the following steps:

  1. If the last number in his sequence is a prime, he terminates the sequence.
  2. If he already wrote n elements without finding a prime, he becomes bored and leaves.
  3. Otherwise, he computes the next element of the sequence as the sum of squares of digits of the last element. For example, 4001 will be followed by 4^2 + 0^2 + 0^2 + 1^2 = 17, and 17 will be followed by 1^2 + 7^2 = 50.

Find out what happens for the given int n. If Vasa eventually becomes bored and leaves, return -1. Otherwise, return the length of the generated sequence.

Definition

  • ClassExploringNumbers
  • MethodnumberOfSteps
  • Parametersint
  • Returnsint
  • Method signatureint numberOfSteps(int n)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Notes

  • A prime number is a positive integer with exactly two positive integer divisors: 1 and itself. The first few primes are 2, 3, 5, 7, 11. Note that 1 is not a prime.

Constraints

  • n will be between 1 and 10^9, inclusive.

Test cases

  1.  
    • n5
     

    Returns1

     
    The input itself is a prime.
  2.  
    • n57
     

    Returns4

     
    Vasa will write down 57, 74 (= 5^2 + 7^2), 65 (= 7^2 + 4^2), and 61 (= 6^2 + 5^2). Number 61 is a prime.
  3.  
    • n1
     

    Returns-1

     
    Vasa begins by writing down the number 1. As 1 is not a prime, he is not done yet. As he already wrote down 1 element of the sequence without finding a prime, he becomes bored and leaves.
  4.  
    • n6498501
     

    Returns2

  5.  
    • n989113
     

    Returns6

  6.  
    • n12366
     

    Returns-1

     
    For n=12366 there are no primes among the first 12366 elements of Vasa's sequence.

暴力循环一下就行了。再记录一下当前这个数在之前计算过没有,如果计算过了那么久返回-1

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <map>
using namespace std; class ExploringNumbers {
public:
int check(int x){
if(x==)return ;
for(int i=;i*i<=x;i++){
if(x%i==)return ;
}
return ;
}
int numberOfSteps(int n) {
map<int,int>mp;
int ans=;
int m=n;
if(n==)return -;
if(check(n))return ;
int sum;
int mark=;
while(check(n)==){
ans++;
sum=;
mp[n]=;
while(n>){
int a=n%;
n/=;
sum+=a*a;
}
n=sum;
if(mp[n]||ans>m){
mark=;break;
}
}
if(mark)return -;
return ans;
}
};

problem 500

Problem Statement

Vasa is a shopkeeper in the small town Deronje. Currently, there are some items for sale in his store. The price of each item is a positive integer. You are given these prices in the int[] A. Each item has a price tag.

Vasa has just learned that a very rich shopper is going to visit his store. Therefore, Vasa wants to alter some of the price tags to make the items in his store more expensive.

Vasa has a collection of stickers. Each of those stickers contains a single digit between 1 and 9, inclusive. Note that he has no stickers with zeros. You are given the description of Vasa's stickers in the int[] D. For each i between 1 and 9, inclusive, Vasa has D[i-1] stickers with the digit i.

Vasa can take any sticker and use it to replace any digit on any price tag. However, there is no extra room on the price tags, so he cannot add new digits, he can only replace existing ones.

Compute and return the maximum total cost of items in Vasa's store after he applies some (possibly none, possibly all) of his stickers to the current price tags.

Definition

  • ClassReplacingDigit
  • MethodgetMaximumStockWorth
  • Parametersvector<int> , vector<int>
  • Returnsint
  • Method signatureint getMaximumStockWorth(vector<int> A, vector<int> D)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Constraints

  • A will contain between 1 and 50 elements, inclusive.
  • Each element of A will be between 1 and 10^6, inclusive.
  • D will contain exactly 9 elements.
  • Each element of D will be between 0 and 10^3, inclusive.

Test cases

  1.  
    • A{ 100, 90 }
    • D{ 0, 0, 0, 0, 2, 1, 0, 0, 0 }
     

    Returns745

     

    Vasa has two digit stickers with digit 5 written on them, and one digit sticker with digit 6 written on it. There are two items in the shop: one with price 100, and the other one with price 90.

    Vasa can use one digit sticker with 5 to replace digit 0 of price tag 90 obtaining a new price of 95 of that item. Then, he can use the remaining stickers 5 and 6 to change the price tag of the other item from 100 to 650. In that case, the worth of his stock would be 650 + 95 = 745.

    Note that the same stock worth could be obtained by applying all the three digit stickers to 100 in order to obtain a new price of 655. In that case, the price 90 of the other item would remain unchanged.

  2.  
    • A{ 9 }
    • D{ 1, 1, 1, 1, 1, 1, 1, 1, 0 }
     

    Returns9

     

    In this example, the maximum stock worth is obtained by leaving the price tag unchanged. Note that Vasa is not required to use all stickers.

  3.  
    • A{ 123456 }
    • D{ 9, 8, 7, 6, 5, 4, 3, 2, 1 }
     

    Returns988777

  4.  
    • A{ 10, 970019, 1976, 10936, 68750, 756309, 37600, 559601, 6750, 76091, 640, 312, 312, 90, 8870 }
    • D{ 11, 2, 8, 10, 7, 6, 3, 1, 3 }
     

    Returns3297500

  5.  
    • A{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    • D{ 111, 111, 111, 111, 111, 111, 111, 111, 111 }
     

    Returns198

    题意就是现在你有1~9这9个数,每个数有D[i]个,然后让你用这9个数 代替 A集合中的数字的某些位,使得最后A集合中的数的和最大。
    贪心思想。
    以9,8,7....1的顺序  从最高位开始改。
     
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#define INF 2000000000
#define ll long long
using namespace std; class ReplacingDigit {
public:
int getMaximumStockWorth(vector<int> A, vector<int> D) {
sort(A.begin(),A.end());
vector<int>v[];
int n=A.size();
for(int i=;i<n;i++){
while(A[i]>){
int a=A[i]%;
v[i].push_back(a);
A[i]/=;
}
}
for(int i=D.size()-;i>=;i--){
while(D[i]>){
int mark=;
for(int t=;t>=;t--){
int ans=INF;
int id;
for(int j=;j<n;j++){
if(v[j].size()>=t){
if(v[j][t-]<ans){
ans=v[j][t-];
id=j;
}
}
}
if(ans<i+){
v[id][t-]=i+;
D[i]--;
mark=;
break;
}
}
if(mark==)break;
}
}
ll sum=;
for(int i=;i<n;i++){
int t=;
for(int j=;j<v[i].size();j++){
sum+=t*v[i][j];
t*=;
}
}
return sum;
}
};

理论上来说做了2题.坑爹的网络,第二题没有提交上..

TCO 2016 Round 1B的更多相关文章

  1. Google Code Jam 2016 Round 1B B

    题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示.现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的. 分析: 类似数位dp,但是很多 ...

  2. TCO 2015 Round 1B DIV1 500 概率题

    [题意]现在有一些线索,每个线索被发现的概率p[i],如果线索i被知道,那么其他线索也可能会被知道,用vector<string> c给出,c[i][j]='Y'表示知道i这个线索,j这个 ...

  3. Google Code Jam 2016 Round 1B Problem C. Technobabble

    题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...

  4. 2019 TCO Round 1B——[ 状压DP ]

    第一题是 EllysSki . 题意:给n个数,求两个方向的最长递减区间. 可以O(n). #include<cstdio> #include<cstring> #includ ...

  5. TCO 2014 Round 1C 概率DP

    TCO round 1C的 250 和500 的题目都太脑残了,不说了. TCO round 1C 950 一个棋子,每次等概率的向左向右移动,然后走n步之后,期望cover的区域大小?求cover, ...

  6. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题

    B. Problems for Round 题目连接: http://www.codeforces.com/contest/673/problem/B Description There are n ...

  7. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B

    B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3

    C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition)只有A题和B题

    连接在这里,->点击<- A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabyte ...

随机推荐

  1. 【bzoj1483】[HNOI2009]梦幻布丁 set

    [bzoj1483][HNOI2009]梦幻布丁 Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2 ...

  2. 「CodePlus 2018 4 月赛」最短路

    $n \leq 100000$,$m \leq 500000$的有向图,两点之间还可以以$a \ \ xor \ \ b$的代价从$a$到$b$,问$s$到$t$的最短路. 被自己蠢哭QAQ 首先两个 ...

  3. 转 Linux命令-文件管理命令

    http://jingyan.baidu.com/article/9113f81bc1c7a72b3214c7d3.html Linux命令-文件管理命令 浏览:4118 | 更新:2012-11-1 ...

  4. R语言入门--画图(一)--ggplot2

    先写一些需要用到的知识点,比如包.函数 dplyr 很好用的包 经常与ggplot2连用 mutate:用于对数据框的列进行重新处理,或者用处理的结果添加新列 数据清洗: 1.na.omit()   ...

  5. Yii 之cookie的使用

    public function actionIndex(){ //设置cookie(注意这里用的是响应组件) $cookies = \YII::$app->response->cookie ...

  6. 洛谷——P1290 欧几里德的游戏

    P1290 欧几里德的游戏 题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的 ...

  7. Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API...

    问题描述:Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API... 解决方法:这种错误是eclips ...

  8. iOS开发之创建颜色渐变视图View

    在iOS开发中有时需要自己自定义一个视图view的背景,而网上有人提出的在循环中不断alloc的方法设置其背景色渐变,会耗费很多内存和资源,极其不明智,而在CALayer中早就提供有图层渐变的类和相应 ...

  9. SolidEdge 工程图中如何快速将同一类元素放到同一个图层

    在图层选项卡中新建一个尺寸线图层   点击聪慧选项(把它点凹下去),然后点击任意尺寸线,弹出聪慧选取选项,点击确定,则自动选择了所有尺寸线   点击移动图元,把刚才选中的所有尺寸线都移动到这个图层即可 ...

  10. MRP Force Reservation的作用

    生产单根据BOM计算出相应的物料需求,生产领料单stock.picking ( internal moves) Stock.picking使用工作流自动计算库存量,如果库存量够,则使用 test_as ...