Molly's Chemicals CodeForces - 776C ( 思维 + 前缀和)

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.

Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.

Help her to do so in finding the total number of such segments.

Input
The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, …, an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.

Output
Output a single integer — the number of valid segments.

Example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要创建以下类: 1. Farm:农场类,包含农户和动物。 2. Farmer:农户类,可以喂养动物。 3. Animal:动物类,包括牛、马、羊和鸡。 类图如下: ``` +----------------+ +----------------+ +----------------+ | Farm | | Farmer | | Animal | +----------------+ +----------------+ +----------------+ | - farmers: List<Farmer> | | - name: String | | - name: String | | - animals: List<Animal> | +----------------+ | - type: String | +-----------------------+ | + feed(animal:Animal): void | +----------------+ +--------------------------+ ``` 接下来,我们来编写代码实现模拟开心牧场: ``` // Animal.java public abstract class Animal { private String name; private String type; public Animal(String name, String type) { this.name = name; this.type = type; } public String getName() { return name; } public String getType() { return type; } public abstract void makeSound(); } // Cow.java public class Cow extends Animal { public Cow(String name) { super(name, "cow"); } @Override public void makeSound() { System.out.println("Moo!"); } } // Horse.java public class Horse extends Animal { public Horse(String name) { super(name, "horse"); } @Override public void makeSound() { System.out.println("Neigh!"); } } // Sheep.java public class Sheep extends Animal { public Sheep(String name) { super(name, "sheep"); } @Override public void makeSound() { System.out.println("Baa!"); } } // Chicken.java public class Chicken extends Animal { public Chicken(String name) { super(name, "chicken"); } @Override public void makeSound() { System.out.println("Cluck!"); } } // Farmer.java public class Farmer { private String name; public Farmer(String name) { this.name = name; } public void feed(Animal animal) { System.out.println(name + " is feeding " + animal.getName() + " the " + animal.getType()); } } // Farm.java import java.util.ArrayList; import java.util.List; public class Farm { private List<Farmer> farmers; private List<Animal> animals; public Farm() { farmers = new ArrayList<>(); animals = new ArrayList<>(); } public void addFarmer(Farmer farmer) { farmers.add(farmer); } public void addAnimal(Animal animal) { animals.add(animal); } public void feedAnimals() { for (Farmer farmer : farmers) { for (Animal animal : animals) { farmer.feed(animal); } } } } // Main.java public class Main { public static void main(String[] args) { Farm farm = new Farm(); Farmer farmer1 = new Farmer("John"); Farmer farmer2 = new Farmer("Jane"); farm.addFarmer(farmer1); farm.addFarmer(farmer2); Animal cow1 = new Cow("Bessie"); Animal cow2 = new Cow("Maggie"); Animal horse1 = new Horse("Black Beauty"); Animal sheep1 = new Sheep("Dolly"); Animal sheep2 = new Sheep("Molly"); Animal chicken1 = new Chicken("Chickie"); farm.addAnimal(cow1); farm.addAnimal(cow2); farm.addAnimal(horse1); farm.addAnimal(sheep1); farm.addAnimal(sheep2); farm.addAnimal(chicken1); farm.feedAnimals(); } } ``` 以上代码模拟了开心牧场,包括农户和动物。运行程序将输出以下内容: ``` John is feeding Bessie the cow John is feeding Maggie the cow John is feeding Black Beauty the horse John is feeding Dolly the sheep John is feeding Molly the sheep John is feeding Chickie the chicken Jane is feeding Bessie the cow Jane is feeding Maggie the cow Jane is feeding Black Beauty the horse Jane is feeding Dolly the sheep Jane is feeding Molly the sheep Jane is feeding Chickie the chicken ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值