最近无聊,把自己写的东西发一下,省的我给删除连找都没有地方找
package 蓝桥杯习题;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Num1纪念日 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
Date date0=s.parse("1921-7-23");
Date date2=s.parse("2020-7-1");
int b=(int)((date2.getTime()-date0.getTime())/(1000*60));
System.out.println(b);
int sum=0;
for (int i = 1921; i <=2020 ; i++) {
sum = sum+runnian(i);
}
sum = sum -22;
System.out.println(sum*24*60);
}
public static int runnian(int n){
if (n%400==0 ||n%4==0 && n%100!=0){
return 366;
}
else return 365;
}
}
package 蓝桥杯习题;
/*
新冠疫情由新冠病毒引起,最近在 A 国蔓延,为了尽快控制疫情,A 国准 备给大量民众进病毒核酸检测。
然而,用于检测的试剂盒紧缺。 为了解决这一困难,科学家想了一个办法:合并检测。
即将从多个人(k 个)采集的标本放到同一个试剂盒中进行检测。如果结果为阴性,则说明这 k 个人都是阴性,用一个试剂盒完成了 k 个人的检测。如果结果为阳性,则说明 至少有一个人为阳性,
需要将这 k 个人的样本全部重新独立检测(
从理论上看, 如果检测前 k−1 个人都是阴性可以推断出第 k 个人是阳性,但是在实际操作中 不会利用此推断,而
是将 k 个人独立检测),加上最开始的合并检测,一共使用 了 k + 1 个试剂盒完成了 k 个人的检测。
A 国估计被测的民众的感染率大概是 1%,呈均匀分布。请问 k 取多少能 最节省试剂盒?
*/
//设定有一百个人来检测
public class Num2合并检测 {
public static void main(String[] args) {
int sum =0;
int count=Integer.MAX_VALUE;
int shiji=0;
for (int i = 1; i <=100 ; i++) {
if(100%i==0){
sum = 100/i+i;
}else {
sum = 100/i+i+1;
}
if(sum<count){
count = sum;
shiji = i;
}
}
System.out.println(shiji);
}
}
package 蓝桥杯习题;
/*
某市市长获得了若干批口罩,给定每批口罩的数量,每一批口罩的数目如下:
9090400 8499400 5926800 8547000 4958200 4422600 5751200
4175600 6309600 5865200 6604400 4635000 10663400 8087200 4554000
市长要把口罩分配给市内的 2 所医院,由于物流限制,每一批口罩只能全部分配给其中一家医院。
市长希望 2 所医院获得的口罩总数之差越小越好。 请你计算这个差最小是多少?
答案提交
这是一道结果填空题,你只需要算出结果后提交即可。
本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分
*/
public class Num3分配口罩 {
private static int sum=Integer.MAX_VALUE;
private static int[] num = {
9090400, 8499400, 5926800,
8547000, 4958200, 4422600, 5751200, 4175600, 6309600,
5865200, 6604400, 4635000, 10663400, 8087200, 4554000
};
public static void main(String[] args) {
dfs(0,0,0);
System.out.println(sum);
}
public static void dfs(int u ,int s1 ,int s2){
if (u == 15){
sum = Math.min(sum,Math.abs(s1-s2));
return;
}
dfs(u+1,s1+num[u],s2);
dfs(u+1,s1,s2+num[u]);
}
}
package 蓝桥杯习题;
public class Num3测试次数 {
public static void main(String[] args) {
int[][] d = new int[1001][4];//有j个手机测试i层楼需要扔几次
for (int i = 1; i <= 1000; i++) {
d[i][1]=i;//1个手机扔i次可以测试i层楼
}
//2部手机
for (int i = 1; i <= 1000; i++) {
int min=Integer.MAX_VALUE;
//尝试 1~i若干种方案,记录方案中次数最小的
for (int j = 1; j <=i; j++) {//在j层扔第一个手机
min=Math.min(min,Math.max(d[j-1][1]+1,d[i-j][2]+1));
//max里面就是摔坏了和没摔坏两种情况取较大的,也就是运气坏的。
//没摔坏:还有i-j层,手机数目不变
//摔坏了: 还有j-1层,手机数目-1
}
d[i][2]=min;
}
//三部手机
for (int i = 1; i <= 1000; i++) {
int min=Integer.MAX_VALUE;
for (int j = 1; j <=i; j++) {
min=Math.min(min,Math.max(d[j-1][2]+1,d[i-j][3]+1));
}
d[i][3]=min;
}
System.out.println(d[1000][3]);
}
public static void yiwei(){
int []f1=new int[1001];
int []f2=new int[1001];
int []f3=new int[1001];
for(int i=1;i<=1000;i++) {
f1[i]=i;
}
for(int i=1;i<=1000;i++) {
int ans=Integer.MAX_VALUE;
for(int j=1;j<=i;j++) {
int max_=1+ Math.max(f1[j-1], f2[i-j]);
ans=Math.min(ans, max_);
}
f2[i]=ans;
}
for(int i=1;i<=1000;i++) {
int ans=Integer.MAX_VALUE;
for(int j=1;j<=i;j++) {
int max_=1+Math.max(f2[j-1], f3[i-j]);
ans=Math.min(ans, max_);
}
f3[i]=ans;
}
System.out.println(f3[1000]);
}
}
package 蓝桥杯习题;
/*
【问题描述】
斐波那契数列满足 F1 = F2 = 1,从 F3 开始有 Fn = Fn 1 + Fn 2。请你计算
GCD(F2020, F520),其中 GCD(A, B) 表示 A 和 B 的最大公约数。
【答案提交】
这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个
整数,在提交答案时只填写这个整数,填写多余的内容将无法得分
*/
import java.math.BigInteger;
public class Num4菲波那切数列最大公约数 {
public static void main(String[] args) {
BigInteger a = qiu(2020);
System.out.println(a);
BigInteger b = qiu(520);
System.out.println(b);
BigInteger c = a.gcd(b);
System.out.println(c.toString());
}
public static BigInteger qiu(int n){
BigInteger a1 = BigInteger.ONE;
BigInteger a2 = BigInteger.ONE;
BigInteger t = BigInteger.ONE;
for (int i = 3; i <= n; i++) {
t = a1.add(a2);
a1 = a2;
a2 = t;
}
return t;
}
}
package 蓝桥杯习题;
import java.util.Scanner;
/*
输入一个字符串,请输出这个字符串包含多少个大写字母,多少个小写字母,多少个数字。
【输入格式】
输入一行包含一个字符串。
【输出格式】
输出三行,每行一个整数,分别表示大写字母、小写字母和数字的个数。
【样例输入】
1+a=Aab
【样例输出】
1
3
1
*/
public class Num5分类计数 {
private static int a1=0,b1=0,c1=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int a=0,b=0,c=0;
for (int i = 0; i < s.length(); i++) {
char d = s.charAt(i);
if (Character.isDigit(d)){
a++;
}else if (Character.isUpperCase(d)){
b++;
}else if(Character.isLowerCase(d)){
c++;
}
}
System.out.println(b);
System.out.println(c);
System.out.println(a);
jiandan(s);
System.out.println(a1+" "+b1+" "+c1);
}
public static void jiandan(String s){
for (int i = 0; i < s.length(); i++) {
char d = s.charAt(i);
if (d=='0'||d=='1'||d=='2'||d=='3'||d=='4'||d=='5'||d=='6'||d=='7'||d=='8'||d=='9'){
a1++;
} else if (d >= 'a' && d <= 'z') {
c1++;
}else if (d >= 'A' && d <= 'Z') {
b1++;
}
}
}
}
package 蓝桥杯习题;
import java.math.BigInteger;
import java.util.Scanner;
/*
【问题描述】
给定正整数 n, 求 1^8 + 2^8 +···+ n^8 mod 123456789 。其中 mod 表示取余。
【输入格式】
输入的第一行包含一个整数 n。
【输出格式】
输出一行,包含一个整数,表示答案。
【样例输入】
2
【样例输出】
257
【样例输入】
987654
【样例输出】
43636805
*/
public class Num6八次求和 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
qiuhe(n);
}
public static void qiuhe(int n){
BigInteger sum = BigInteger.ZERO;
for (int i = 1; i <=n ; i++) {
sum = sum.add(new BigInteger(i+"").pow(8));
}
System.out.println(sum.mod(new BigInteger(123456789+"")));
}
}
package 蓝桥杯习题;
/*
小明发明了一种给由全大写字母组成的字符串编码的方法。
对于每一个大写字母,小明将它转换成它在 26 个英文字母中序号,即 A → 1, B → 2, … Z →26。
这样一个字符串就能被转化成一个数字序列:比如 ABCXYZ → 123242526。
现在给定一个转换后的数字序列,小明想还原出原本的字符串。
当然这样的还原有可能存在多个符合条件的字符串。
小明希望找出其中字典序最大的字符串。
输入格式
一个数字序列。
输出格式
一个只包含大写字母的字符串,代表答案
样例输入
123242526
样例输出
LCXYZ
*/
import java.util.HashSet;
import java.util.Scanner;
public class Num7字符串编码 {
public static char[] arr= {
'0',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'
};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String sum="";
for (int i = 0; i < s.length()-1; i++) {
String s1 = s.substring(i,i+2);
int num = Integer.valueOf(s1);
if (num <= 26){
sum=sum+arr[num];
i++;
}else{
sum=sum+arr[Integer.valueOf(i+1)];
}
}
System.out.println(sum);
}
}
这题好像有错误
package 蓝桥杯习题;
import java.util.Scanner;
public class Num9数字三角形 {
public static void main(String[] args) {
int[][] a = new int[510][510];
int[][] f = new int[510][510];
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= i; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = n; i >= 1; i--) {
//从最后一排开始走,从下往上。
for (int j = 1; j <= i; j++) {
f[i][j] = Math.max(f[i + 1][j + 1], f[i + 1][j]) + a[i][j];
}
}
System.out.println(f[1][1]);
}
}
package 蓝桥杯习题;
public class Num10卡片问题 {
public static void main(String[] args) {
int[] num = new int[10];
for (int i = 0; i < 10; i++) {
num[i] = 2021;
}
for (int i = 1; i < 20000 ; i++) {
String s = i+"";
char[] chars = s.toCharArray();
for (int j = 0; j < s.length(); j++) {
int k = Integer.valueOf(chars[j]+"");
if(num[k]>0){
num[k]--;
}else{
System.out.println(s);
break;
}
}
}
}
}
package 蓝桥杯习题;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Num11时间显示 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
long mills = scan.nextLong();
ff(mills);
System.out.println();
fff(mills);
Date date = new Date(mills);
SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");
System.out.println(ft.format(date));
scan.close();
}
public static void ff(long n){
//先把毫秒转化成秒
n=n/1000;
//求最后一天的秒数
n=n%(24*60*60);
//求显示的小时
long hour=n/(60*60);
//这个小时内的秒数
n=n%(60*60);
//求分钟
long min=n/60;
//求秒
long s=n%60;
System.out.printf("%02d:%02d:%02d",hour,min,s);
}
public static void fff(Long num){
long allseconds = num/1000;
long seconds = allseconds%60;
long allmins = allseconds/60;
long mins = allmins%60;
long allhours = allmins/60;
long hours = allhours%24;
System.out.print(String.format("%02d", hours)+":"+String.format("%02d", mins)+":"+String.format("%02d", seconds));
}
}
package 蓝桥杯习题;
/*
如果我们按从上到下、从左到右的顺序把所有数排成一列,可以得到如下数列:
1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, \cdots1,1,1,1,2,1,1,3,3,1,1,4,6,4,1,⋯
给定一个正整数 NN,请你输出数列中第一次出现 NN 是在第几个数?
*/
import java.util.Scanner;
public class Num12杨辉三角形 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] yanghui = new int[33][100];
//了解杨辉三角和组合数的联系
for (int i = 0; i < yanghui.length; i++) {
for (int j = 0; j <=i ; j++) {
if (i ==j || j==0){
yanghui[i][j]=1;
}else{
yanghui[i][j] = yanghui[i-1][j]+yanghui[i-1][j-1];
}
//System.out.print(yanghui[i][j]+" ");
}
//System.out.println();
}
int count=0;
for (int i = 0; i < 33; i++) {
for (int j = 0; j <=i ; j++) {
count++;
if (yanghui[i][j] == n){
System.out.println(count);
return;
}
}
}
}
}
package 蓝桥杯习题;
import java.util.Scanner;
public class Num13八皇后问题 {
static Scanner in = new Scanner(System.in);
static int N = 20;
static boolean[] col = new boolean[N], dg = new boolean[N], udg = new boolean[N];
static char[][] chs = new char[N][N];
static int n = 0;
static void dfs(int u) {
if (u == n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(chs[i][j]);
}
System.out.println();
}
System.out.println();
return;
}
for (int i = 0; i < n; i++) { //对行进行遍历
if (!col[i] && !dg[i - u + n] && !udg[i + u]) {
chs[u][i] = 'Q';
col[i] = dg[i - u + n] = udg[i + u] = true;
dfs(u + 1);
col[i] = dg[i - u + n] = udg[i + u] = false;
chs[u][i] = '.';
}
}
}
public static void main(String[] args) {
n = in.nextInt();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) chs[i][j] = '.';
dfs(0);
}
}
package 蓝桥杯习题;
/*
小蓝发现,他将 11 至 10000000071000000007 之间的不同的数与 20212021 相乘后再求除以 10000000071000000007 的余数,
会得到不同的数。
小蓝想知道,能不能在 1 至 1000000007 之间找到一个数,
与 20212021 相乘后 再除以 10000000071000000007 后的余数为 999999999999999999。
如果存在,请在答案中提交这个数; 如果不存在,请在答案中提交 00
*/
import java.math.BigInteger;
public class Num14相乘 {
public static void main(String[] args) {
BigInteger bigInteger2=new BigInteger("2021");
BigInteger bigInteger3=new BigInteger("999999999");
BigInteger bigInteger4=new BigInteger("1000000007");
for (int i = 1; i <= 1000000007; i++) {
BigInteger bigInteger=new BigInteger(String.valueOf(i));
bigInteger=bigInteger.multiply(bigInteger2);
bigInteger=bigInteger.mod(bigInteger4);
if (bigInteger.equals(bigInteger3)) {
System.out.println(i);
return;
}
}
}
}
这题过不了,就是dp思路学习一下,对于我来说
package 蓝桥杯习题;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/*
你有一架天平和 NN 个砝码,这 NN 个砝码重量依次是 W_1, W_2, · · · , W_NW
请你计算一共可以称出多少种不同的重量? 注意砝码可以放在天平两边。
输入格式
输入的第一行包含一个整数 NN。
第二行包含 NN 个整数:W_1, W_2, W_3, · · · , W_NW
输出格式
输出一个整数代表答案。
样例输入
3
1 4 6
*/
public class Num16砝码称重 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] wi = new int[n+1];
int sum = 0;
for (int i = 1; i <= n; i++) {
wi[i] = sc.nextInt();
sum = sum+wi[i];
}
int[][] dp = new int[106][10006];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
dp[i][j] = dp[i-1][j];
if (dp[i][j]==0){
if (j == wi[i]) dp[i][j] = 1;
if (j > wi[i]) dp[i][j] = dp[i - 1][j - wi[i]];
if (j < wi[i]) dp[i][j] = dp[i - 1][wi[i] - j];
}
}
}
int count=0;
for (int i = 1; i <= sum; i++) {
if (dp[n][i]!=0) count++;
}
System.out.println(count);
}
}
package 蓝桥杯习题;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Num16砝码称重2 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Set<Integer> set = new HashSet<>();
set.add(0);
for (int i = 0; i < n; i++) {
ArrayList<Integer> arrayList = new ArrayList<>(set);
for (int k : arrayList) {
set.add(k+arr[i]);
set.add(Math.abs(k-arr[i]));
}
}
System.out.println(set.size()-1);
}
}
这题就是贪心算法里面的合并果子问题
package 蓝桥杯习题;
/*
在很久很久以前,有 nn 个部落居住在平原上,依次编号为 11 到 nn。第 ii 个部落的人数为 t_it
有一年发生了灾荒。年轻的政治家小蓝想要说服所有部落一同应对灾荒,他能通过谈判来说服部落进行联合。
每次谈判,小蓝只能邀请两个部落参加,花费的金币数量为两个部落的人数之和,
谈判的效果是两个部落联合成一个部落(人数为原来两个部落的人数之和)。
输入的第一行包含一个整数 nn,表示部落的数量。
第二行包含 nn 个正整数,依次表示每个部落的人数。
输出描述
输出一个整数,表示最小花费。
输入输出样例
示例 1
输入
4
9 1 3 5
*/
import java.util.PriorityQueue;
import java.util.Scanner;
public class Num17谈判 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<Integer> queue = new PriorityQueue();
for (int i = 0; i < n ; i++) {
int m = sc.nextInt();
queue.add(m);
}
int sum = 0;
while (queue.size()>1){
int a = queue.poll();
int b = queue.poll();
sum = sum + a +b;
queue.add(a+b);
}
System.out.println(sum);
}
}
只会一个全排列还不考
package 蓝桥杯习题;
import java.util.Scanner;
public class Num18全排列 {
public static int[] arr = new int[10];
private static int count =0;
private static boolean[] st= new boolean[210];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
dfs(0,n);
System.out.println(count);
}
public static void dfs(int u ,int n){
if (u == n){
count++;
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" " );
}
System.out.println();
}
else {
for (int i = 1; i <= n ; i++) {
if (!st[i]){
arr[u] = i;
st[i] = true;
dfs(u+1,n);
st[i] = false;
}
}
}
}
}
package 蓝桥杯习题;
/*
100 可以表示为带分数的形式:100=3+69258714
还可以表示为:100=82+3546197
注意特征:带分数中,数字 1∼9 分别出现且只出现一次(不包含 0)。
类似这样的带分数,100 有 11 种表示法。
输入格式
一个正整数。
输出格式
输出输入数字用数码 1∼9 不重复不遗漏地组成带分数表示的全部种数。
*/
import java.util.Scanner;
public class Num19全排列应用1 {
private static int key;
private static int count =0;
private static int[] arr = new int[10];
private static boolean[] st = new boolean[210];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
key = sc.nextInt();
dfs(0);
System.out.println(count);
}
public static void dfs(int u){
if(u == 9){
for (int i = 0; i < 7; i++) { //到7的原因是下一个数到8 就是数组的最大边界数组
for (int j = i+1; j < 8 ; j++) {
int a = sum(0,i);
int b = sum(i+1,j);
int c = sum(j+1,8);
if(a*c + b == key*c) {
count++;
System.out.println(a+"+"+b+"/"+c);
}
}
}
}else {
for (int i = 1; i <= 9 ; i++) {
if(!st[i]){
arr[u] = i;
st[i] = true;
dfs(u+1);
st[i] = false;
}
}
}
}
public static int sum(int l ,int r){
int p =0;
for (int i = l; i <= r ; i++) {
p = p*10+arr[i];
}
return p;
}
}
package 蓝桥杯习题;
/*
如果用 a b c d 这 4 个字母组成一个串,有 4!=24 种,如果把它们排个序,每个串都对应一个序号:
abcd 0
abdc 1acbacdb 3adbc 4adcb 5bacd 6badc 7bcad 8bcda 9bdac 10bdca 11cabd 12cadb 13cbad 14cbda 15cdab 16cdba 17
\cdots⋯
现在有不多于 10 个两两不同的小写字母,给出它们组成的串,你能求出该串在所有排列中的序号吗?
输入描述
输入一行,一个串。
输出描述
输出一行,一个整数,表示该串在其字母所有排列生成的串中的序号。注意:最小的序号是 0。
*/
import java.util.Scanner;
public class Num19全排列应用2 {
private static int size;
private static boolean[] st = new boolean[210];
private static int[] chars = new int[10];
private static int count=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
size = s.length();
dfs(0,s);
// int a ='a';
// System.out.println(a);
}
public static void dfs(int u , String s){
if(u == size){
count++;
int sum = 0;
for (int i = 0; i < s.length(); i++) {
if (Integer.valueOf(s.charAt(i))==chars[i]){
sum++;
}
}
if (sum == size){
System.out.println(count-1);
}
}else {
for (int i = 97; i < 97+size ; i++) {
if (!st[i]){
chars[u] = i;
st[i] = true;
dfs(u+1,s);
st[i] = false;
}
}
}
}
}
package 蓝桥杯习题;
import java.math.BigInteger;
import java.util.Scanner;
/*
斐波那契数列大家都非常熟悉。它的定义是:
f(x) = 1 \cdots (x=1,2)f(x)=1⋯(x=1,2)
f(x) = f(x-1) + f(x-2) \cdots (x>2)f(x)=f(x−1)+f(x−2)⋯(x>2)
对于给定的整数 nn 和 mm,我们希望求出:
f(1) + f(2) + \cdots + f(n)f(1)+f(2)+⋯+f(n) 的值。但这个值可能非常大,所以我们把它对 f(m)f(m) 取模。
公式如下:
但这个数字依然很大,所以需要再对 p 求模。
输入描述
输入描述
输入为一行用空格分开的整数 n, m, p\ (0 < n, m, p < 10^{18})n,m,p (0<n,m,p<10
18
)。
输出描述
输出为 1 个整数。
*/
//错误答案 !!!!!!
public class Num20斐波那契 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger m = sc.nextBigInteger();
BigInteger p = sc.nextBigInteger();
big(n,m,p);
}
public static void big(int n ,BigInteger m ,BigInteger p) {
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ONE;
BigInteger t = BigInteger.ZERO;
BigInteger sum = BigInteger.ZERO;
BigInteger c = new BigInteger("1");
if (n == 1 ) {
sum.add(a);
System.out.println(sum.mod(m).mod(p));
}else if(n==2){
sum.add(a).add(b);
System.out.println(sum.mod(m).mod(p));
}
else {
for (int i = 3; i <= n; i++) {
t = a.add(b);
a = b;
b = t;
sum.add(b);
}
System.out.println(b.mod(m).mod(p));
}
}
}