C. Magic Five

C. Magic Five
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.

Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.

Look at the input part of the statement, s is given in a special form.

Input

In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |ak.

Output

Print a single integer — the required number of ways modulo 1000000007 (109 + 7).

Sample test(s)
input
1256
1
output
4
input
13990
2
output
528
input
555
2
output
63
Note

In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.

In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.

In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.


code:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int vis[100020]; 
char f[100020];

__int64 powe_m(__int64 a,__int64 b,__int64 c)    //注意要int64接收变量  否则会溢出
{
	__int64 ans=1; 
	__int64 tmp=a;
	
	while(b!=0)
	{ 	
		if (b&1) 
			ans=ans*tmp%c;    //???????? ans=ans*ans%c ??????
		
		tmp=tmp*tmp%c;
		b=b>>1;
	}
		
	
	return ans;	 
}

int main()
{
	int  i,k;    注意要int64 变量  否则k在计算k*strlen(f)的时候会溢出   ,或者给k*strlen(f)来个强制转换最好直接在k定义的时候__int64,也是提醒我们该变量不超过int范围不一定就绝对得定义为int,还要考虑计算的过程是否超过int。
	__int64 ans=0; 
	scanf("%s",f);
	scanf("%d",&k);
	
 	for (i=strlen(f)-1;i>=0;i--)
	{
		if (f[i]=='0'||f[i]=='5') 
			vis[i]++;
	}
	int qiandao0=0;
	i=0;
	while(f[i]==0)
	{	qiandao0++;i++;}
                    
	__int64 ccd= powe_m(2,strlen(f),1000000007) -1;
	__int64 ab= powe_m(2,(__int64)(k)*strlen(f),1000000007)-1;
__int64 big= (ab*(powe_m(ccd,(1000000007-2),1000000007 ) ) )%1000000007;   //神一样的公式
////除法取余方法的一种,(a/b)%mod,等价于(a*b^(mod-2))%mod,mod为素数     
   //如果不这样的话。a%mod / b%mod 已经和 (a/b)%mod 不相同了。
//这里有点乱。。其实按原来的话是 下面的for循环每次都要计算k次(k最大10^9) 必然超时,刚好发现能化简,最后是等比数列,所以就有了上面的乱七八糟的计算。
		for (i=strlen(f)-1;i>=0;i--)
		{
			if (vis[i])
			{ 
               ans+= powe_m (2,i,1000000007) ;
				ans%=1000000007;     //自己原来是每次*big   其实可以再最后才*big
				
				
			}
			
		}
		
		
 
	printf("%I64d\n",ans*big%1000000007);
	
	return 0;
	
} 



struct page { 75 unsigned long flags; /* Atomic flags, some possibly 76 * updated asynchronously */ 77 /* 78 * Five words (20/40 bytes) are available in this union. 79 * WARNING: bit 0 of the first word is used for PageTail(). That 80 * means the other users of this union MUST NOT use the bit to 81 * avoid collision and false-positive PageTail(). 82 */ 83 union { 84 struct { /* Page cache and anonymous pages */ 85 /** 86 * @lru: Pageout list, eg. active_list protected by 87 * lruvec->lru_lock. Sometimes used as a generic list 88 * by the page owner. 89 */ 90 union { 91 struct list_head lru; 92 93 /* Or, for the Unevictable "LRU list" slot */ 94 struct { 95 /* Always even, to negate PageTail */ 96 void *__filler; 97 /* Count page's or folio's mlocks */ 98 unsigned int mlock_count; 99 }; 100 101 /* Or, free page */ 102 struct list_head buddy_list; 103 struct list_head pcp_list; 104 }; 105 /* See page-flags.h for PAGE_MAPPING_FLAGS */ 106 struct address_space *mapping; 107 pgoff_t index; /* Our offset within mapping. */ 108 /** 109 * @private: Mapping-private opaque data. 110 * Usually used for buffer_heads if PagePrivate. 111 * Used for swp_entry_t if PageSwapCache. 112 * Indicates order in the buddy system if PageBuddy. 113 */ 114 unsigned long private; 115 }; 116 struct { /* page_pool used by netstack */ 117 /** 118 * @pp_magic: magic value to avoid recycling non 119 * page_pool allocated pages. 120 */ 121 unsigned long pp_magic; 122 struct page_pool *pp; 123 unsigned long _pp_mapping_pad; 124 unsigned long dma_addr; 125 union { 126 /** 127 * dma_addr_upper: might require a 64-bit 128 * value on 32-bit architectures. 129 */ 130 unsigned long dma_addr_upper; 131 /** 132 * For frag page support, not supported in 133 * 32-bit architectures with 64-bit DMA. 134 */ 135 atomic_long_t pp_frag_count; 136 }; 137 };这段代码是有关透明大页的数据结构吗
09-12
<think>首先,用户的问题是:“我想查找C语言中枚举的使用方法 请问C语言枚举的使用方法”。系统指令要求我使用中文回答,结构清晰,逐步解决问题,并保证真实可靠。 系统指令还提到: - 所有行内数学表达式用 $...$(但枚举无关数学,可能不需要)。 - 独立公式用 $$...$$(同样可能不需要)。 - 生成相关问题。 - 引用的段落末尾添加引用标识如 [^1]。 用户提供了引用: - 引用[1]:关于枚举的基本概念和语法。 - 引用[2]:定义枚举变量的三种方式。 - 引用[3]:枚举的输出示例。 系统指令说:“上述引用仅供参考,并不是用户真正的需求,请尽可能多的参考用户上一次的问题和你的回答。” 用户上一次的问题是这个查询本身,所以没有历史对话。我应该基于这些引用和我的知识来回答。 关键点:用户想知道C语言中枚举的使用方法。 我需要结构化回答: 1. **解释枚举的基本概念**:枚举是一种用户定义的数据类型,用于创建一组命名的整数常量。提高代码可读性。 2. **语法和定义**:如何声明枚举类型。 - 基于引用[1]:enum EnumName { CONST1, CONST2, ... }; 3. **定义枚举变量**:如何创建变量。 - 基于引用[2]:三种方式: - 先定义类型,再定义变量。 - 定义类型时同时定义变量。 - 省略枚举名称直接定义。 4. **使用示例**:如何赋值和输出。 - 基于引用[3]:使用printf输出枚举值;默认从0开始。 添加一些额外知识: - 枚举值默认从0开始递增。 - 可以指定初始值:如 enum DAY { MON=1, TUE, ... }; - 枚举变量可以赋值给int变量,但不能直接输入输出,需要通过字符串映射。 确保回答清晰: - 逐步:从概念到定义到使用。 - 用代码示例说明。 生成相关问题在末尾: - 类似示例中的“相关问题”部分。 - 基于枚举主题,如: 1. 枚举的优点和缺点? 2. 如何在switch语句中使用枚举? 3. 枚举与宏定义的区别? 在引用的段落末尾添加引用标识: - 当我引用特定内容时,在末尾加 [^number],但引用是用户提供的,我应该基于它们添加引用。 - 系统指令说:"回答中引用的段落末尾自然地添加引用标识",意思是如果我引用用户提供的引用,加标识。 用户提供了引用[1],[2],[3],所以在回答中,当我说到相关内容时,加 [^1], [^2], [^3]。 回答结构: 1. 介绍枚举的概念。 2. 声明枚举类型。 3. 定义枚举变量(三种方式)。 4. 使用枚举示例(输出)。 5. 注意事项。 6. 生成相关问题。 最后,输出相关问题。 </think>### C语言枚举的使用方法详解 在C语言中,枚举(Enumeration)是一种用户定义的数据类型,用于为一组整型常量指定有意义的名字,从而提高代码的可读性和可维护性。本文将基于引用内容,逐步介绍枚举的语法、定义方式、使用场景及示例,确保回答真实可靠。 #### 1. **枚举的基本概念与声明** 枚举通过`enum`关键字声明,创建一个新的数据类型,其中包含一组命名的整数常量。语法如下: ```c enum EnumName { CONSTANT1, // 常量名 CONSTANT2, ... }; ``` - **默认值**:枚举常量默认从0开始递增(如第一个常量是0,第二个是1,依此类推)。 - **指定值**:您可以显式指定起始值或任意整数值(如`MON=1`),后续常量自动递增[^1]。 - **优点**:避免使用“魔法数字”(magic numbers),使代码更易读(例如,用`MON`代替1来表示星期一)。 #### 2. **定义枚举变量的方法** 声明枚举类型后,需要定义变量来存储枚举常量。引用[^2]提供了三种常见的定义方式: 1. **先定义枚举类型,再定义变量**(最常用方式): ```c enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; // 指定MON为1,后续递增 enum DAY today; // 定义枚举变量today ``` 2. **定义枚举类型的同时定义变量**(简化代码): ```c enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } today; // 类型和变量一起定义 ``` 3. **省略枚举名称直接定义变量**(适用于一次性使用): ```c enum { MON=1, TUE, WED, THU, FRI, SAT, SUN } today; // 无类型名,直接定义today ``` - **注意**:枚举变量本质上存储的是整数值,因此可以赋值给`int`类型变量,但推荐使用枚举名以提高清晰度[^2]。 #### 3. **枚举的使用与输出示例** 枚举变量常用于条件判断、函数参数或输出。引用[^3]展示了如何输出枚举常量: ```c #include <stdio.h> enum OPTIONS { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN }; // 默认值:ONE=0, TWO=1, ..., SIX=5 int main() { enum OPTIONS choice = SIX; // 定义枚举变量并赋值 printf("%d\n", ONE); // 输出:0 printf("%d\n", TWO); // 输出:1 printf("%d\n", choice); // 输出:5(因为SIX对应整数值5) return 0; } ``` - **赋值规则**:枚举变量可以直接赋值为枚举常量(如`choice = SIX;`),或间接通过整数值(但需确保值在枚举范围内)。 - **输出方式**:枚举值以整数形式输出(使用`%d`格式化符),因为C语言不直接支持枚举的字符串输出。若需文本输出,需手动映射(如用`switch`语句将`SIX`转换为"Six")[^3]。 #### 4. **高级技巧与注意事项** - **使用场景**:常用于状态机(如菜单选项)、错误码定义或提高代码可读性(例如替代`#define`宏)。 - **内存占用**:枚举变量占用`int`大小(通常4字节),与整数类型兼容。 - **限制**:枚举常量必须是唯一的整数,且C语言不支持枚举范围的自动检查(赋值时需谨慎)。 - **最佳实践**:在函数参数或结构体中使用枚举类型,以避免硬编码数字(如`void setDay(enum DAY d);`)。 #### 总结 C语言的枚举通过`enum`关键字简化代码,提升可维护性。核心步骤包括:声明枚举类型、定义变量并使用常量。实践中,推荐指定起始值(如`MON=1`)以防止默认0导致的歧义。如需更多细节,请参考C语言标准库文档或进阶教程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值