周结

先补一下昨天晚上的CF吧,好久没有爆0了,昨晚成功实现,也将两场比赛加的分全赔进去了可谓相当惨烈,div2的比赛想出两三个对于现在的我还是很有难度的,直接看题吧

Sequence with Digits

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's define the following recurrence:

an+1=an+minDigit(an)⋅maxDigit(an).an+1=an+minDigit(an)⋅maxDigit(an).

Here minDigit(x)minDigit(x) and maxDigit(x)maxDigit(x) are the minimal and maximal digits in the decimal representation of xx without leading zeroes. For examples refer to notes.

Your task is calculate aKaK for given a1a1 and KK.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of independent test cases.

Each test case consists of a single line containing two integers a1a1 and KK (1≤a1≤10181≤a1≤1018, 1≤K≤10161≤K≤1016) separated by a space.

Output

For each test case print one integer aKaK on a separate line.

Example

input

Copy

8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7

output

Copy

42
487
519
528
544
564
588
628

Note

a1=487a1=487

a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519

a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528

a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544

a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564

a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588

a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628

 

这个题目昨晚就一直保持超时的状态,想了好多种优化的方法但是都改善不多,其实看了正确的代码和自己的相比差别并不是很大,做不出来就是忽略掉很多细节,说实话补题补的心态真的很崩,找不出区别就一直觉得自己很对,但是实际上并不对,还是要细心的

看,实在不行一行一行的比对,这个题优化的方法叫做剪枝,我之前的错误代码也进行了剪纸,但是并没有进行彻底,忽略了其中的一步,也是这个题一直卡的一步,没有这一步都被卡了超时,就是如果最小的数是0那么不需要找到第k个数,直接输出n就行了,因为后面的数都不会改变了 ,下面附上代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,t,k,m;
ll sum,num;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ll s=n;
        for(ll i=2;i<=k;i++)
        {
            ll temp=s;
            ll maxx=temp%10;
            ll minn=temp%10;
            while(temp)
            {
                minn=min(minn,temp%10);
                temp/=10;
                if(minn==0)
                {
                    break;
                }
            }
            if(minn==0) break;//着重强调一下这个最关键的一步
            temp=s;
            while(temp)
            {
                maxx=max(maxx,temp%10);
                temp/=10;
                if(maxx==9)
                {
                    break;
                }
            }
            s=s+minn*maxx;
        }
        cout<<s<<endl;
    }
}

 

Young Explorers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter eiei — his inexperience. Russell decided that an explorer with inexperience ee can only join the group of ee or more people.

Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.

Input

The first line contains the number of independent test cases TT(1≤T≤2⋅1051≤T≤2⋅105). Next 2T2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers NN (1≤N≤2⋅1051≤N≤2⋅105).

The second line contains NN integers e1,e2,…,eNe1,e2,…,eN (1≤ei≤N1≤ei≤N), where eiei is the inexperience of the ii-th explorer.

It's guaranteed that sum of all NN doesn't exceed 3⋅1053⋅105.

Output

Print TT numbers, each number on a separate line.

In ii-th line print the maximum number of groups Russell can form in ii-th test case.

Example

input

Copy

2
3
1 1 1
5
2 3 1 2 2

output

Copy

3
2

Note

In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to 11, so it's not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 11, 22 and 33 will form the first group, and the other two explorers with inexperience equal to 22 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 22, and the second group using only one explorer with inexperience equal to 11. In this case the young explorer with inexperience equal to 33 will not be included in any group.

哇这个题目真的是相当的晦涩难懂啊,题目真是读了一遍又一遍总是理解不到题目的含义,特别是Russell decided that an explorer with inexperience ee can only join the group of ee or more people.意思大概就是在这一组中找到inexperence的最大的那个值,好比是a,那么 分的这一组人最少就有a个人,然后看了题解,这个题目自己还是差了一步就a出来了,真不知道自己什么时候才能打破这个瓶颈。

#include <stdio.h>
#include <iostream>
#define maxn 200010
using namespace std;
int cnt[maxn];
int main(){
	int t,n,i,a,tot,remain;
	scanf("%d",&t);
	while(t--){
		tot=0,remain=0;//remain统计inexperience中多出的部分
		scanf("%d",&n);
		for(i=1;i<=n;i++)cnt[i]=0;
		for(i=1;i<=n;i++)scanf("%d",&a),cnt[a]++;//cnt[i]记录inexperience是i的数量
		for(i=1;i<=n;i++)
			tot+=(cnt[i]+remain)/i,remain=cnt[i]+remain-(cnt[i]+remain)/i*i;//注意i是指1个分组中最小的元素数量
		printf("%d\n",tot);
	}
	return 0;
}

 

Game With Array

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya and Vasya are competing with each other in a new interesting game as they always do.

At the beginning of the game Petya has to come up with an array of NN positive integers. Sum of all elements in his array should be equal to SS. Then Petya has to select an integer KK such that 0≤K≤S0≤K≤S.

In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either KK or S−KS−K. Otherwise Vasya loses.

You are given integers NN and SS. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.

Input

The first line contains two integers NN and SS (1≤N≤S≤1061≤N≤S≤106) — the required length of the array and the required sum of its elements.

Output

If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain NNpositive integers with sum equal to SS. In the third line print KK. If there are many correct answers, you can print any of them.

If Petya can't win, print "NO" (without quotes).

You can print each letter in any register (lowercase or uppercase).

Examples

input

Copy

1 4

output

Copy

YES
4
2

input

Copy

3 4

output

Copy

NO

input

Copy

3 8

output

Copy

YES
2 1 5
4

这个题目时间并不多了,题目意思看的并不完全,错的只能说罪有应得hhh,但是 做了就重新补一下,这个题目需要自己重新构造一下

存在极端情况

前n-1个数,值全设置为1,最后的数,值设置为S-(n-1)*1,

只要能找到k与S-k落在区间[(n-1)*1+1,S-(n-1)*1-1]内,即表明找寻成功

 

#include <stdio.h>
#include <iostream>
#define maxn 1000010
using namespace std;
int a[maxn];
int main(){
	int n,s,k,i,flag=0,l,r;
	scanf("%d%d",&n,&s);
	for(i=1;i<n;i++)a[i]=1;//前n-1个数,值全设置为1
	a[n]=s-1*(n-1);//最后的数,值设置为S-(n-1)*1
	l=1*(n-1),r=a[n];
	for(i=l+1;i<r;i++)
		if(l<s-i&&s-i<r){flag=1;k=i;break;}//只要能找到k与S-k落在区间[(n-1)*1+1,S-(n-1)*1-1]内,即表明找寻成功
	if(!flag)printf("NO\n");
	else{
		printf("YES\n");
		for(i=1;i<n;i++)printf("%d ",a[i]);
		printf("%d\n",a[n]);
		printf("%d\n",k);
	}
	return 0;
}

 再总结一下这个周学习的数论知识,第一个比较好用的可以避免超时的就是素数筛法,但是做vj的第一个题目并没有想那么多就直接用平常求素数的方法做了,结果就一直超时,但是如果用素数筛法就会大大节约时间复杂度 ,下面附上具体的例题,

Everybody knows any number can be combined by the prime number. 
Now, your task is telling me what position of the largest prime factor. 
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc. 
Specially, LPF(1) = 0. 

Input

Each line will contain one integer n(0 < n < 1000000). 

Output

Output the LPF(n). 

Sample Input

1
2
3
4
5

Sample Output

0
1
2
1
3

素数筛法的模板就可以直接参考ac的代码 中的getPrime函数

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000000;
typedef long long LL;
LL prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    int ans=0;
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i])
        {
           ans++;
           for(int j=i;j<MAXN;j+=i)
             prime[j]=ans;
        }
    }
}
int main()
{
    getPrime();
    LL n;
    while(cin>>n)
    {
       printf("%d\n",prime[n]);
    }
    return 0;
}

 

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 

Now given a humble number, please write a program to calculate the number of divisors about this humble number.For examle, 4 is a humble,and it have 3 divisors(1,2,4);12 have 6 divisors. 
 

Input

The input consists of multiple test cases. Each test case consists of one humble number n,and n is in the range of 64-bits signed integer. Input is terminated by a value of zero for n. 

Output

For each test case, output its divisor number, one line per case. 

Sample Input

4
12
0

Sample Output

3
6

 

B题的话做的时候也是一直超时,需要将题目的问题重新排列,按照2、3、5、7来寻找

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000000;
typedef long long LL;
LL prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    int ans=0;
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i])
        {
           ans++;
           for(int j=i;j<MAXN;j+=i)
             prime[j]=ans;
        }
    }
}
int main()
{
    getPrime();
    LL n;
    while(cin>>n)
    {
       printf("%d\n",prime[n]);
    }
    return 0;
}

 

七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" 
人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下: 



数字N的因子就是所有比N小又能被N整除的所有正整数,如12的因子有1,2,3,4,6. 
你想知道你的另一半吗? 

Input

输入数据的第一行是一个数字T(1<=T<=500000),它表明测试数据的组数.然后是T组测试数据,每组测试数据只有一个数字N(1<=N<=500000). 

Output

对于每组测试数据,请输出一个代表输入数据N的另一半的编号. 

Sample Input

3
2
10
20

Sample Output

1
8
22

 最后这个题目还是非常有意思的,这就是找出一个数的除了自己本身的所有因子和;
首先不能用暴力解法,直接循环筛选比n小的数,是否为n的因子,这样会超时的;那么可以用小于根号n为循环的上限,验证时可以的道i因子,还可以得到n/i因子,所以这样可行;注意:当i*i=n时,因子只有i这一个,所以得单独考虑!

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int t,n,sum;
    scanf("%d",&t);
    while(t--)
        {
            scanf("%d",&n);
            sum=1;
            int i;
            for(i=2;i*i<n;i++)
            {    
                    if(n%i==0)
                    {
                          sum=sum+i+n/i;
                    }
             }
            if(i*i==n) sum+=i;
                printf("%d\n",sum);
        }
    return 0;
}

最近做了这么多场cf也算有点小的感悟,有的时候没有思路我们可以通过模拟样例来获取答案,这确实不失为一种好的方法,我们以上面cf第二题为例进行一下分析题目的解法就呼之欲出了

发现选出的数,可涉及的数据如下

1 1+1=2

1+6=7 1+1+6=8

即1 2 7 8,该数组有一个空档区间[3,6],

如何选k?只要k与s-k落在[3,6]区间内即可。

k=3,s-k=8-3=5落在[3,6]区间内

 

输出

YES

1 1 6

3

 通过这样的分析很容易就可以写出上面的算法

继续加油吧,打破这个只差一步就能解成答案的瓶颈就可以蹭蹭涨分了,就当是个美好的期愿,一定会实现的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值