Lightoj 1136 Division by 3 (idea title)

Source: Internet
Author: User
Tags cas

http://www.lightoj.com/volume_showproblem.php?problem=1136

Start to solve two times with the same, calculate a will find a lot of solutions. --> instead of parsing the sequence structure.

Analysis found:

A number of numbers that consist of three consecutive integers together and bound to be divisible by 3. (X+x+1+x+2=3x+3=3 (x+1))

Thus there are:

1. The number of 3rd K in the title must be divisible by three.

2. The 3rd K + 1 number in the title, its 2nd number to the last number of the sum must be divisible by 3, plus the first number, that is 1, can not be 3 whole.

3. The 3rd K + 2 number in the title, the sum of the 3rd number to the last digit must be divisible by 3, plus the first to second number, which is 1 + 2 = 3, and divisible by 3.

So the 1~k can be divisible by 3 with 2k/3+ (k%3==2?1:0).

Complete code:

01./*0.008s,1088kb*/
.  
#include <cstdio>  
04.typedef unsigned int ll;  
06.ll Calc (ll k)  
07.{  
Return    K/3 * 2 + (k 3 = 2 1:0);  
.  
11.int Main ()  
12.{    int T, cas = 0;    ll A, B;    scanf ("%d", &t);  
While    (t--)  
.    {        scanf ("%u%u", &a, &b);        printf ("Case%d:%u\n", ++cas, Calc (b)-Calc (A-1));  
.    return 0;  
22.}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.