Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.
You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.
Solve the problem to show that it's not a NP problem.
The first line contains two integers l and r (2 ≤ l ≤ r ≤ 109).
Print single integer, the integer that appears maximum number of times in the divisors.
If there are multiple answers, print any of them.
19 29
2
3 6
3
Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.html
The first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.
The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.
解:如果l==r,直接输出其中一个就可以了,输出l吧。其他,输出2.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 作者:张宇翔
* 创建日期:2017年5月4日 下午10:33:03
* 描述:永远的ACM,永远的征途
*/
public class Main {
private static final int Max=(int) (1e5+10);
private static int l,r;
public static void main(String[] args) throws Exception{
InitData();
GetAns();
}
private static void InitData() throws Exception{
SC cin=new SC(System.in);
l=cin.nextInt();
r=cin.nextInt();
}
private static void GetAns(){
if(r-l>=3){
System.out.println(2);
}else if(l==r){
System.out.println(l);
}else{
System.out.println(2);
}
}
static class SC
{
BufferedReader br;
StringTokenizer st;
SC(InputStream s)
{
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException
{
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException
{
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException
{
return Long.parseLong(next());
}
}
}
In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.
He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible.
The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.
Print the string that satisfies all the constraints.
If there are multiple answers, print any of them.
2
aa
3
bba
A palindrome is a sequence of characters which reads the same backward and forward.
解:我这里是按照abbabbabbabb....顺序来的,你会发现,无论怎么计算,都不会出现长度为3的回文串,符合题目要求,并且字符串中没有c
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 作者:张宇翔
* 创建日期:2017年5月4日 下午10:33:03
* 描述:永远的ACM,永远的征途
*/
public class Main {
private static final int Max=(int) (1e5+10);
private static int n;
public static void main(String[] args) throws Exception{
InitData();
GetAns();
}
private static void InitData() throws Exception{
SC cin=new SC(System.in);
n=cin.nextInt();
}
private static void GetAns(){
System.out.print("a");
if(n>=1){
int suma=0;
int sumb=0;
boolean ok=false;
for(int i=2;i<=n;i++){
if(!ok&&sumb<2){
System.out.print("b");
sumb++;
if(sumb==2){
ok=true;
sumb=0;
}
}else if(ok&&suma<2){
System.out.print("a");
suma++;
if(suma==2){
ok=false;
suma=0;
}
}
}
}
System.out.println();
}
static class SC
{
BufferedReader br;
StringTokenizer st;
SC(InputStream s)
{
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException
{
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException
{
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException
{
return Long.parseLong(next());
}
}
}
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.
Print single integer: the minimum cost of tickets needed to visit all schools.
2
0
10
4
In the first example we can buy a ticket between the schools that costs .
解:我的想法有点独特,我是先来一个循环,计算i+temp(ps:temp=n+1-i)==n+1,并且i<temp的个数,因为这i,temp这两个城市的价值为0.然后两座城市价值为0,标记一下
,最后计算一下那些没被标记的。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 作者:张宇翔
* 创建日期:2017年5月4日 下午10:33:03
* 描述:永远的ACM,永远的征途
*/
public class Main {
private static final int Max=(int) (1e5+10);
private static int n;
private static int ans;
private static int []vis;
public static void main(String[] args) throws Exception{
InitData();
GetAns();
}
private static void InitData() throws Exception{
SC cin=new SC(System.in);
vis=new int[Max];
n=cin.nextInt();
ans=0;
}
private static void GetAns(){
if(n==3||n==4){
System.out.println(1);
}else if(n==2||n==1){
System.out.println(0);
}else{
for(int i=1;i<=n;i++){
int temp=(n+1)-i;
if(i<temp){
ans++;
vis[i]=1;
vis[temp]=1;
}else{
break;
}
}
if(ans>1){
ans--;
}
for(int i=1;i<=n;i++){
if(vis[i]==0){
ans++;
}
}
System.out.println(ans);
}
}
static class SC
{
BufferedReader br;
StringTokenizer st;
SC(InputStream s)
{
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException
{
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException
{
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException
{
return Long.parseLong(next());
}
}
}
We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.
The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.
The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.
Print the minimum number of steps modulo 109 + 7.
ab
1
aab
3
The first example: "ab" → "bba".
The second example: "aab" → "abba" → "bbaba" → "bbbbaa".
题意:给出一串由‘a’,‘b’组成的字符串,其中的“ab”会被转化成为“bba”,
问对于给定字符串可以进行几次这样的操作
解:这道题是一道找规律题,每一个ab都将会变成bba,也就是将a移至b后面,再增加一个b。分析第二个样例,发现a…ab…ba…ab…b中后面产生的b会对前面a的转移产生影响,每有一个a移至b后将会使b的数量增多一倍,因此可以从末尾开始计算生成b的数量,
遇到一个普通的b数量+1,遇到一个a因为它会让b的数量翻倍,因此要使数量x2.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 作者:张宇翔
* 创建日期:2017年5月4日 下午10:33:03
* 描述:永远的ACM,永远的征途
*/
public class Main {
private static final int Max=(int) (1e5+10);
private static final int MOD=(int) (1e9+7);
private static String s;
public static void main(String[] args) throws Exception{
InitData();
GetAns();
}
private static void InitData() throws Exception{
SC cin=new SC(System.in);
s=cin.next();
}
private static void GetAns(){
int cnt=0;
int ans=0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='a'){
ans+=cnt;
ans%=MOD;
cnt<<=1;
cnt%=MOD;
}else{
cnt++;
}
}
System.out.println(ans%MOD);
}
static class SC
{
BufferedReader br;
StringTokenizer st;
SC(InputStream s)
{
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException
{
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException
{
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException
{
return Long.parseLong(next());
}
}
}